Add a 2x2 widget

This commit is contained in:
Christopher Eby 2010-07-25 02:34:17 -05:00
parent 0cf4e0e097
commit 03ffdfbaf3
6 changed files with 270 additions and 0 deletions

View File

@ -73,6 +73,16 @@
android:name="android.appwidget.provider"
android:resource="@xml/four_long_widget" />
</receiver>
<receiver
android:name=".FourSquareWidget"
android:label="Vanilla Music 2x2">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/four_square_widget" />
</receiver>
<receiver android:name="MediaButtonReceiver" >
<intent-filter android:priority="1000">
<action android:name="android.intent.action.MEDIA_BUTTON" />

BIN
res/drawable/black.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
This file is part of Vanilla Music Player.
Vanilla Music Player is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Vanilla Music Player is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/cover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:padding="5dip"
android:background="#B2191919"
android:scaleType="fitCenter" />
<LinearLayout
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/cover"
android:layout_alignLeft="@+id/cover"
android:layout_alignRight="@+id/cover"
android:layout_margin="10dip"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#fff"
android:textSize="18dip"
android:shadowColor="#f000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="3"
android:textStyle="bold"
android:ellipsize="none"
android:singleLine="true" />
<TextView
android:id="@+id/artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#fff"
android:shadowColor="#f000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="3"
android:textSize="15dip"
android:ellipsize="none"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:id="@+id/buttons"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/cover"
android:layout_alignLeft="@+id/cover"
android:layout_alignRight="@+id/cover">
<ImageButton
android:id="@+id/play_pause"
android:src="@drawable/hidden_play"
android:background="#0000"
android:layout_height="wrap_content"
android:layout_width="0px"
android:layout_weight="1" />
<ImageButton
android:id="@+id/next"
android:src="@drawable/hidden_next"
android:background="#0000"
android:layout_height="wrap_content"
android:layout_width="0px"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
This file is part of Vanilla Music Player.
Vanilla Music Player is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Vanilla Music Player is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="144dip"
android:minHeight="144dip"
android:initialLayout="@layout/four_square_widget"
/>

View File

@ -129,6 +129,7 @@ public class ContextApplication extends Application {
{
OneCellWidget.receive(intent);
FourLongWidget.receive(intent);
FourSquareWidget.receive(intent);
ArrayList<Activity> list = mActivities;
if (list != null) {

View File

@ -0,0 +1,143 @@
/*
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
*
* This file is part of Vanilla Music Player.
*
* Vanilla Music Player is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Vanilla Music Player is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kreed.vanilla;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.RemoteViews;
/**
* Simple 1x4 widget to show currently playing song, album art, and play/pause,
* next, and previous buttons. Uses artwork from the default Android music
* widget.
*/
public class FourSquareWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager manager, int[] ids)
{
Song song;
int state;
if (ContextApplication.hasService()) {
PlaybackService service = ContextApplication.getService();
song = service.getSong(0);
state = service.getState();
} else {
SongTimeline timeline = new SongTimeline();
timeline.loadState(context);
song = timeline.getSong(0);
state = 0;
// If we generated a new current song (because the PlaybackService has
// never been started), then we need to save the state.
timeline.saveState(context, 0);
}
updateWidget(context, manager, ids, song, state);
}
/**
* Receive a broadcast sent by the PlaybackService and update the widget
* accordingly.
*
* @param intent The intent that was broadcast.
*/
public static void receive(Intent intent)
{
String action = intent.getAction();
if (PlaybackService.EVENT_CHANGED.equals(action) || PlaybackService.EVENT_REPLACE_SONG.equals(action)) {
Context context = ContextApplication.getContext();
Song song = intent.getParcelableExtra("song");
int state = intent.getIntExtra("state", -1);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
int[] ids = manager.getAppWidgetIds(new ComponentName(context, FourSquareWidget.class));
updateWidget(context, manager, ids, song, state);
}
}
/**
* Populate the widgets with the given ids with the given info.
*
* @param context A Context to use.
* @param manager The AppWidgetManager that will be used to update the
* widget.
* @param ids An array containing the ids of all the widgets to update.
* @param song The current Song in PlaybackService.
* @param state The current PlaybackService state.
*/
public static void updateWidget(Context context, AppWidgetManager manager, int[] ids, Song song, int state)
{
if (ids == null || ids.length == 0)
return;
Resources res = context.getResources();
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.four_square_widget);
Bitmap cover = null;
if (song == null) {
views.setViewVisibility(R.id.title, View.GONE);
views.setViewVisibility(R.id.buttons, View.GONE);
views.setTextViewText(R.id.artist, res.getText(R.string.no_songs));
} else {
views.setViewVisibility(R.id.title, View.VISIBLE);
views.setViewVisibility(R.id.buttons, View.VISIBLE);
views.setTextViewText(R.id.title, song.title);
views.setTextViewText(R.id.artist, song.artist);
cover = song.getCover();
}
if (cover == null)
views.setImageViewResource(R.id.cover, R.drawable.black);
else
views.setImageViewBitmap(R.id.cover, cover);
if (state != -1) {
boolean playing = (state & PlaybackService.FLAG_PLAYING) != 0;
views.setImageViewResource(R.id.play_pause, playing ? R.drawable.hidden_pause : R.drawable.hidden_play);
}
Intent intent;
PendingIntent pendingIntent;
ComponentName service = new ComponentName(context, PlaybackService.class);
intent = new Intent(context, LaunchActivity.class);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.text, pendingIntent);
intent = new Intent(PlaybackService.ACTION_TOGGLE_PLAYBACK).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.play_pause, pendingIntent);
intent = new Intent(PlaybackService.ACTION_NEXT_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.next, pendingIntent);
manager.updateAppWidget(ids, views);
}
}