From 03ffdfbaf3e9924a551d7d49f9ed92625cd17606 Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Sun, 25 Jul 2010 02:34:17 -0500 Subject: [PATCH] Add a 2x2 widget --- AndroidManifest.xml | 10 ++ res/drawable/black.png | Bin 0 -> 3058 bytes res/layout/four_square_widget.xml | 92 +++++++++++ res/xml/four_square_widget.xml | 24 +++ src/org/kreed/vanilla/ContextApplication.java | 1 + src/org/kreed/vanilla/FourSquareWidget.java | 143 ++++++++++++++++++ 6 files changed, 270 insertions(+) create mode 100644 res/drawable/black.png create mode 100644 res/layout/four_square_widget.xml create mode 100644 res/xml/four_square_widget.xml create mode 100644 src/org/kreed/vanilla/FourSquareWidget.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 0e8ca92f..da0eff59 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -73,6 +73,16 @@ android:name="android.appwidget.provider" android:resource="@xml/four_long_widget" /> + + + + + + diff --git a/res/drawable/black.png b/res/drawable/black.png new file mode 100644 index 0000000000000000000000000000000000000000..e6f80a41ed868cb9f77ab91866bc83a355ba3869 GIT binary patch literal 3058 zcmeAS@N?(olHy`uVBq!ia0y~yV15C@985rwLkFEV11ZMhAa^H*b?0PW0y*p@p1!W^ zx7Z~)7&O;BoM8bJVoUONcVYMsf(!O8p9~b?EbxddW?F! zZx1r^0+k$E@ZCR`pIrpV90j8xFd71*Aut*OqaiRF0;3@?8UmvsFd71shroxVv)&Ah Ta_g?F1)1gP>gTe~DWM4ftYax` literal 0 HcmV?d00001 diff --git a/res/layout/four_square_widget.xml b/res/layout/four_square_widget.xml new file mode 100644 index 00000000..8ef5aa03 --- /dev/null +++ b/res/layout/four_square_widget.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/xml/four_square_widget.xml b/res/xml/four_square_widget.xml new file mode 100644 index 00000000..48590595 --- /dev/null +++ b/res/xml/four_square_widget.xml @@ -0,0 +1,24 @@ + + + \ No newline at end of file diff --git a/src/org/kreed/vanilla/ContextApplication.java b/src/org/kreed/vanilla/ContextApplication.java index dbe12013..465b9832 100644 --- a/src/org/kreed/vanilla/ContextApplication.java +++ b/src/org/kreed/vanilla/ContextApplication.java @@ -129,6 +129,7 @@ public class ContextApplication extends Application { { OneCellWidget.receive(intent); FourLongWidget.receive(intent); + FourSquareWidget.receive(intent); ArrayList list = mActivities; if (list != null) { diff --git a/src/org/kreed/vanilla/FourSquareWidget.java b/src/org/kreed/vanilla/FourSquareWidget.java new file mode 100644 index 00000000..c90e3af9 --- /dev/null +++ b/src/org/kreed/vanilla/FourSquareWidget.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2010 Christopher Eby + * + * 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 . + */ + +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); + } +}