1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.music; 18 19import android.app.PendingIntent; 20import android.appwidget.AppWidgetManager; 21import android.appwidget.AppWidgetProvider; 22import android.content.ComponentName; 23import android.content.Context; 24import android.content.Intent; 25import android.content.res.Resources; 26import android.os.Environment; 27import android.view.View; 28import android.widget.RemoteViews; 29 30/** 31 * Simple widget to show currently playing album art along 32 * with play/pause and next track buttons. 33 */ 34public class MediaAppWidgetProvider extends AppWidgetProvider { 35 static final String TAG = "MusicAppWidgetProvider"; 36 37 public static final String CMDAPPWIDGETUPDATE = "appwidgetupdate"; 38 39 private static MediaAppWidgetProvider sInstance; 40 41 static synchronized MediaAppWidgetProvider getInstance() { 42 if (sInstance == null) { 43 sInstance = new MediaAppWidgetProvider(); 44 } 45 return sInstance; 46 } 47 48 @Override 49 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 50 defaultAppWidget(context, appWidgetIds); 51 52 // Send broadcast intent to any running MediaPlaybackService so it can 53 // wrap around with an immediate update. 54 Intent updateIntent = new Intent(MediaPlaybackService.SERVICECMD); 55 updateIntent.putExtra( 56 MediaPlaybackService.CMDNAME, MediaAppWidgetProvider.CMDAPPWIDGETUPDATE); 57 updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); 58 updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); 59 context.sendBroadcast(updateIntent); 60 } 61 62 /** 63 * Initialize given widgets to default state, where we launch Music on default click 64 * and hide actions if service not running. 65 */ 66 private void defaultAppWidget(Context context, int[] appWidgetIds) { 67 final Resources res = context.getResources(); 68 final RemoteViews views = 69 new RemoteViews(context.getPackageName(), R.layout.album_appwidget); 70 71 views.setViewVisibility(R.id.title, View.GONE); 72 views.setTextViewText(R.id.artist, res.getText(R.string.widget_initial_text)); 73 74 linkButtons(context, views, false /* not playing */); 75 pushUpdate(context, appWidgetIds, views); 76 } 77 78 private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) { 79 // Update specific list of appWidgetIds if given, otherwise default to all 80 final AppWidgetManager gm = AppWidgetManager.getInstance(context); 81 if (appWidgetIds != null) { 82 gm.updateAppWidget(appWidgetIds, views); 83 } else { 84 gm.updateAppWidget(new ComponentName(context, this.getClass()), views); 85 } 86 } 87 88 /** 89 * Check against {@link AppWidgetManager} if there are any instances of this widget. 90 */ 91 private boolean hasInstances(Context context) { 92 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 93 int[] appWidgetIds = 94 appWidgetManager.getAppWidgetIds(new ComponentName(context, this.getClass())); 95 return (appWidgetIds.length > 0); 96 } 97 98 /** 99 * Handle a change notification coming over from {@link MediaPlaybackService} 100 */ 101 void notifyChange(MediaPlaybackService service, String what) { 102 if (hasInstances(service)) { 103 if (MediaPlaybackService.META_CHANGED.equals(what) 104 || MediaPlaybackService.PLAYSTATE_CHANGED.equals(what)) { 105 performUpdate(service, null); 106 } 107 } 108 } 109 110 /** 111 * Update all active widget instances by pushing changes 112 */ 113 void performUpdate(MediaPlaybackService service, int[] appWidgetIds) { 114 final Resources res = service.getResources(); 115 final RemoteViews views = 116 new RemoteViews(service.getPackageName(), R.layout.album_appwidget); 117 118 CharSequence titleName = service.getTrackName(); 119 CharSequence artistName = service.getArtistName(); 120 CharSequence errorState = null; 121 122 // Format title string with track number, or show SD card message 123 String status = Environment.getExternalStorageState(); 124 if (status.equals(Environment.MEDIA_SHARED) || status.equals(Environment.MEDIA_UNMOUNTED)) { 125 if (android.os.Environment.isExternalStorageRemovable()) { 126 errorState = res.getText(R.string.sdcard_busy_title); 127 } else { 128 errorState = res.getText(R.string.sdcard_busy_title_nosdcard); 129 } 130 } else if (status.equals(Environment.MEDIA_REMOVED)) { 131 if (android.os.Environment.isExternalStorageRemovable()) { 132 errorState = res.getText(R.string.sdcard_missing_title); 133 } else { 134 errorState = res.getText(R.string.sdcard_missing_title_nosdcard); 135 } 136 } else if (titleName == null) { 137 errorState = res.getText(R.string.emptyplaylist); 138 } 139 140 if (errorState != null) { 141 // Show error state to user 142 views.setViewVisibility(R.id.title, View.GONE); 143 views.setTextViewText(R.id.artist, errorState); 144 145 } else { 146 // No error, so show normal titles 147 views.setViewVisibility(R.id.title, View.VISIBLE); 148 views.setTextViewText(R.id.title, titleName); 149 views.setTextViewText(R.id.artist, artistName); 150 } 151 152 // Set correct drawable for pause state 153 final boolean playing = service.isPlaying(); 154 if (playing) { 155 views.setImageViewResource(R.id.control_play, R.drawable.ic_appwidget_music_pause); 156 } else { 157 views.setImageViewResource(R.id.control_play, R.drawable.ic_appwidget_music_play); 158 } 159 160 // Link actions buttons to intents 161 linkButtons(service, views, playing); 162 163 pushUpdate(service, appWidgetIds, views); 164 } 165 166 /** 167 * Link up various button actions using {@link PendingIntents}. 168 * 169 * @param playerActive True if player is active in background, which means 170 * widget click will launch {@link MediaPlaybackActivity}, 171 * otherwise we launch {@link MusicBrowserActivity}. 172 */ 173 private void linkButtons(Context context, RemoteViews views, boolean playerActive) { 174 // Connect up various buttons and touch events 175 Intent intent; 176 PendingIntent pendingIntent; 177 178 final ComponentName serviceName = new ComponentName(context, MediaPlaybackService.class); 179 180 if (playerActive) { 181 intent = new Intent(context, MediaPlaybackActivity.class); 182 pendingIntent = PendingIntent.getActivity( 183 context, 0 /* no requestCode */, intent, 0 /* no flags */); 184 views.setOnClickPendingIntent(R.id.album_appwidget, pendingIntent); 185 } else { 186 intent = new Intent(context, MusicBrowserActivity.class); 187 pendingIntent = PendingIntent.getActivity( 188 context, 0 /* no requestCode */, intent, 0 /* no flags */); 189 views.setOnClickPendingIntent(R.id.album_appwidget, pendingIntent); 190 } 191 192 intent = new Intent(MediaPlaybackService.TOGGLEPAUSE_ACTION); 193 intent.setComponent(serviceName); 194 pendingIntent = 195 PendingIntent.getService(context, 0 /* no requestCode */, intent, 0 /* no flags */); 196 views.setOnClickPendingIntent(R.id.control_play, pendingIntent); 197 198 intent = new Intent(MediaPlaybackService.NEXT_ACTION); 199 intent.setComponent(serviceName); 200 pendingIntent = 201 PendingIntent.getService(context, 0 /* no requestCode */, intent, 0 /* no flags */); 202 views.setOnClickPendingIntent(R.id.control_next, pendingIntent); 203 } 204} 205