-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.music;
-
-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.net.Uri;
-import android.view.View;
-import android.widget.RemoteViews;
-
-/**
- * Simple widget to show currently playing album art along with play/pause and
- * next track buttons.
- */
-public class MediaAppWidgetProvider1x1 extends AppWidgetProvider {
- static final String TAG = "MusicAppWidgetProvider1x1";
-
- public static final String CMDAPPWIDGETUPDATE = "appwidgetupdate1x1";
-
- private static MediaAppWidgetProvider1x1 sInstance;
-
- static synchronized MediaAppWidgetProvider1x1 getInstance() {
- if (sInstance == null) {
- sInstance = new MediaAppWidgetProvider1x1();
- }
- return sInstance;
- }
-
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- defaultAppWidget(context, appWidgetIds);
-
- // Send broadcast intent to any running MediaPlaybackService so it can
- // wrap around with an immediate update.
- Intent updateIntent = new Intent(MediaPlaybackService.SERVICECMD);
- updateIntent.putExtra(MediaPlaybackService.CMDNAME,
- MediaAppWidgetProvider1x1.CMDAPPWIDGETUPDATE);
- updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
- appWidgetIds);
- updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
- context.sendBroadcast(updateIntent);
- }
-
- /**
- * Initialize given widgets to default state, where we launch Music on
- * default click and hide actions if service not running.
- */
- private void defaultAppWidget(Context context, int[] appWidgetIds) {
- final RemoteViews views = new RemoteViews(context.getPackageName(),
- R.layout.album_appwidget1x1);
-
- views.setImageViewResource(R.id.albumart, View.GONE);
-
- linkButtons(context, views, false /* not playing */);
- pushUpdate(context, appWidgetIds, views);
- }
-
- private void pushUpdate(Context context, int[] appWidgetIds,
- RemoteViews views) {
- // Update specific list of appWidgetIds if given, otherwise default to
- // all
- final AppWidgetManager gm = AppWidgetManager.getInstance(context);
- if (appWidgetIds != null) {
- gm.updateAppWidget(appWidgetIds, views);
- } else {
- gm.updateAppWidget(new ComponentName(context, this.getClass()),
- views);
- }
- }
-
- /**
- * Check against {@link AppWidgetManager} if there are any instances of this
- * widget.
- */
- private boolean hasInstances(Context context) {
- AppWidgetManager appWidgetManager = AppWidgetManager
- .getInstance(context);
- int[] appWidgetIds = appWidgetManager
- .getAppWidgetIds(new ComponentName(context, this.getClass()));
- return (appWidgetIds.length > 0);
- }
-
- /**
- * Handle a change notification coming over from
- * {@link MediaPlaybackService}
- */
- void notifyChange(MediaPlaybackService service, String what) {
- if (hasInstances(service)) {
- if (MediaPlaybackService.META_CHANGED.equals(what)
- || MediaPlaybackService.PLAYSTATE_CHANGED.equals(what)
- || MediaPlaybackService.REPEATMODE_CHANGED.equals(what)
- || MediaPlaybackService.SHUFFLEMODE_CHANGED.equals(what)) {
- performUpdate(service, null);
- }
- }
- }
-
- /**
- * Update all active widget instances by pushing changes
- */
- void performUpdate(MediaPlaybackService service, int[] appWidgetIds) {
- final RemoteViews views = new RemoteViews(service.getPackageName(),
- R.layout.album_appwidget1x1);
-
- CharSequence titleName = service.getTrackName();
- CharSequence artistName = service.getArtistName();
- long albumId = service.getAlbumId();
- long songId = service.getAudioId();
- {
- // No error, so show normal titles and artwork
- views.setTextViewText(R.id.title, titleName);
- views.setTextViewText(R.id.artist, artistName);
- views.setViewVisibility(R.id.albumart, View.VISIBLE);
- // Set album art
- Uri uri = MusicUtils.getArtworkUri(service, songId, albumId);
- if (uri != null) {
- views.setImageViewUri(R.id.albumart, uri);
- } else {
- views.setImageViewResource(R.id.albumart,
- R.drawable.albumart_mp_unknown);
- }
- }
- // Set correct drawable for pause state
- final boolean playing = service.isPlaying();
- if (playing) {
- views.setImageViewResource(R.id.play_pause,
- R.drawable.appwidget_pause_normal);
- } else {
- views.setImageViewResource(R.id.play_pause,
- R.drawable.appwidget_play_normal);
- }
-
- // Link actions buttons to intents
- linkButtons(service, views, playing);
-
- pushUpdate(service, appWidgetIds, views);
- }
-
- /**
- * Link up various button actions using {@link PendingIntents}.
- *
- * @param playerActive
- * True if player is active in background, which means widget
- * click will launch {@link MediaPlaybackActivity}, otherwise we
- * launch {@link MusicBrowserActivity}.
- */
- private void linkButtons(Context context, RemoteViews views,
- boolean playerActive) {
- // Connect up various buttons and touch events
- Intent intent;
- PendingIntent pendingIntent;
-
- final ComponentName serviceName = new ComponentName(context,
- MediaPlaybackService.class);
-
- intent = new Intent(MediaPlaybackService.TOGGLEPAUSE_ACTION);
- intent.setComponent(serviceName);
- pendingIntent = PendingIntent.getService(context,
- 0 /* no requestCode */, intent, 0 /* no flags */);
- views.setOnClickPendingIntent(R.id.albumart, pendingIntent);
-
- }
-}
+/*\r
+ * Copyright (C) 2009 The Android Open Source Project\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.android.music;\r
+\r
+import android.app.PendingIntent;\r
+import android.appwidget.AppWidgetManager;\r
+import android.appwidget.AppWidgetProvider;\r
+import android.content.ComponentName;\r
+import android.content.Context;\r
+import android.content.Intent;\r
+import android.net.Uri;\r
+import android.view.View;\r
+import android.widget.RemoteViews;\r
+\r
+/**\r
+ * Simple widget to show currently playing album art along with play/pause and\r
+ * next track buttons.\r
+ */\r
+public class MediaAppWidgetProvider1x1 extends AppWidgetProvider {\r
+ static final String TAG = "MusicAppWidgetProvider1x1";\r
+\r
+ public static final String CMDAPPWIDGETUPDATE = "appwidgetupdate1x1";\r
+\r
+ private static MediaAppWidgetProvider1x1 sInstance;\r
+\r
+ static synchronized MediaAppWidgetProvider1x1 getInstance() {\r
+ if (sInstance == null) {\r
+ sInstance = new MediaAppWidgetProvider1x1();\r
+ }\r
+ return sInstance;\r
+ }\r
+\r
+ @Override\r
+ public void onUpdate(Context context, AppWidgetManager appWidgetManager,\r
+ int[] appWidgetIds) {\r
+ defaultAppWidget(context, appWidgetIds);\r
+\r
+ // Send broadcast intent to any running MediaPlaybackService so it can\r
+ // wrap around with an immediate update.\r
+ Intent updateIntent = new Intent(MediaPlaybackService.SERVICECMD);\r
+ updateIntent.putExtra(MediaPlaybackService.CMDNAME,\r
+ MediaAppWidgetProvider1x1.CMDAPPWIDGETUPDATE);\r
+ updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,\r
+ appWidgetIds);\r
+ updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);\r
+ context.sendBroadcast(updateIntent);\r
+ }\r
+\r
+ /**\r
+ * Initialize given widgets to default state, where we launch Music on\r
+ * default click and hide actions if service not running.\r
+ */\r
+ private void defaultAppWidget(Context context, int[] appWidgetIds) {\r
+ final RemoteViews views = new RemoteViews(context.getPackageName(),\r
+ R.layout.album_appwidget1x1);\r
+\r
+ views.setImageViewResource(R.id.albumart, View.GONE);\r
+\r
+ linkButtons(context, views, false /* not playing */);\r
+ pushUpdate(context, appWidgetIds, views);\r
+ }\r
+\r
+ private void pushUpdate(Context context, int[] appWidgetIds,\r
+ RemoteViews views) {\r
+ // Update specific list of appWidgetIds if given, otherwise default to\r
+ // all\r
+ final AppWidgetManager gm = AppWidgetManager.getInstance(context);\r
+ if (appWidgetIds != null) {\r
+ gm.updateAppWidget(appWidgetIds, views);\r
+ } else {\r
+ gm.updateAppWidget(new ComponentName(context, this.getClass()),\r
+ views);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Check against {@link AppWidgetManager} if there are any instances of this\r
+ * widget.\r
+ */\r
+ private boolean hasInstances(Context context) {\r
+ AppWidgetManager appWidgetManager = AppWidgetManager\r
+ .getInstance(context);\r
+ int[] appWidgetIds = appWidgetManager\r
+ .getAppWidgetIds(new ComponentName(context, this.getClass()));\r
+ return (appWidgetIds.length > 0);\r
+ }\r
+\r
+ /**\r
+ * Handle a change notification coming over from\r
+ * {@link MediaPlaybackService}\r
+ */\r
+ void notifyChange(MediaPlaybackService service, String what) {\r
+ if (hasInstances(service)) {\r
+ if (MediaPlaybackService.META_CHANGED.equals(what)\r
+ || MediaPlaybackService.PLAYSTATE_CHANGED.equals(what)\r
+ || MediaPlaybackService.REPEATMODE_CHANGED.equals(what)\r
+ || MediaPlaybackService.SHUFFLEMODE_CHANGED.equals(what)) {\r
+ performUpdate(service, null);\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Update all active widget instances by pushing changes\r
+ */\r
+ void performUpdate(MediaPlaybackService service, int[] appWidgetIds) {\r
+ final RemoteViews views = new RemoteViews(service.getPackageName(),\r
+ R.layout.album_appwidget1x1);\r
+\r
+ CharSequence titleName = service.getTrackName();\r
+ CharSequence artistName = service.getArtistName();\r
+ long albumId = service.getAlbumId();\r
+ long songId = service.getAudioId();\r
+ {\r
+ // No error, so show normal titles and artwork\r
+ views.setTextViewText(R.id.title, titleName);\r
+ views.setTextViewText(R.id.artist, artistName);\r
+ views.setViewVisibility(R.id.albumart, View.VISIBLE);\r
+ // Set album art\r
+ Uri uri = MusicUtils.getArtworkUri(service, songId, albumId);\r
+ if (uri != null) {\r
+ views.setImageViewUri(R.id.albumart, uri);\r
+ } else {\r
+ views.setImageViewResource(R.id.albumart,\r
+ R.drawable.albumart_mp_unknown);\r
+ }\r
+ }\r
+ // Set correct drawable for pause state\r
+ final boolean playing = service.isPlaying();\r
+ if (playing) {\r
+ views.setImageViewResource(R.id.play_pause,\r
+ R.drawable.appwidget_pause_normal);\r
+ } else {\r
+ views.setImageViewResource(R.id.play_pause,\r
+ R.drawable.appwidget_play_normal);\r
+ }\r
+\r
+ // Link actions buttons to intents\r
+ linkButtons(service, views, playing);\r
+\r
+ pushUpdate(service, appWidgetIds, views);\r
+ }\r
+\r
+ /**\r
+ * Link up various button actions using {@link PendingIntents}.\r
+ * \r
+ * @param playerActive\r
+ * True if player is active in background, which means widget\r
+ * click will launch {@link MediaPlaybackActivity}, otherwise we\r
+ * launch {@link MusicBrowserActivity}.\r
+ */\r
+ private void linkButtons(Context context, RemoteViews views,\r
+ boolean playerActive) {\r
+ // Connect up various buttons and touch events\r
+ Intent intent;\r
+ PendingIntent pendingIntent;\r
+\r
+ final ComponentName serviceName = new ComponentName(context,\r
+ MediaPlaybackService.class);\r
+\r
+ intent = new Intent(MediaPlaybackService.TOGGLEPAUSE_ACTION);\r
+ intent.setComponent(serviceName);\r
+ pendingIntent = PendingIntent.getService(context,\r
+ 0 /* no requestCode */, intent, 0 /* no flags */);\r
+ views.setOnClickPendingIntent(R.id.albumart, pendingIntent);\r
+\r
+ }\r
+}\r