OSDN Git Service

Automated import from //branches/donutburger/...@140588,140588
[android-x86/packages-apps-Music.git] / src / com / android / music / MediaPlaybackService.java
index ed5325d..deedf27 100644 (file)
@@ -20,6 +20,7 @@ import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.Service;
+import android.appwidget.AppWidgetManager;
 import android.content.ContentResolver;
 import android.content.ContentUris;
 import android.content.ContentValues;
@@ -31,7 +32,6 @@ import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteException;
-import android.gadget.GadgetManager;
 import android.media.AudioManager;
 import android.media.MediaFile;
 import android.media.MediaPlayer;
@@ -45,6 +45,7 @@ import android.os.PowerManager;
 import android.os.SystemClock;
 import android.os.PowerManager.WakeLock;
 import android.provider.MediaStore;
+import android.util.Log;
 import android.widget.RemoteViews;
 import android.widget.Toast;
 import com.android.internal.telephony.Phone;
@@ -146,6 +147,8 @@ public class MediaPlaybackService extends Service {
     // This will have to change if we want to support multiple simultaneous cards.
     private int mCardId;
     
+    private MediaAppWidgetProvider mAppWidgetProvider = MediaAppWidgetProvider.getInstance();
+    
     // interval after which we stop the service when idle
     private static final int IDLE_DELAY = 60000; 
 
@@ -226,7 +229,6 @@ public class MediaPlaybackService extends Service {
                         next(false);
                     } else {
                         notifyChange(PLAYBACK_COMPLETE);
-                        pushGadgetUpdate();
                     }
                     break;
                 case RELEASE_WAKELOCK:
@@ -258,11 +260,11 @@ public class MediaPlaybackService extends Service {
             } else if (CMDSTOP.equals(cmd)) {
                 pause();
                 seek(0);
-            } else if (MediaGadgetProvider.CMDGADGETUPDATE.equals(cmd)) {
-                // Someone asked us to refresh a set of specific gadgets, probably
+            } else if (MediaAppWidgetProvider.CMDAPPWIDGETUPDATE.equals(cmd)) {
+                // Someone asked us to refresh a set of specific widgets, probably
                 // because they were just added.
-                int[] gadgetIds = intent.getIntArrayExtra(GadgetManager.EXTRA_GADGET_IDS);
-                MediaGadgetProvider.updateAllGadgets(MediaPlaybackService.this, gadgetIds);
+                int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
+                mAppWidgetProvider.performUpdate(MediaPlaybackService.this, appWidgetIds);
             }
         }
     };
@@ -312,6 +314,14 @@ public class MediaPlaybackService extends Service {
 
     @Override
     public void onDestroy() {
+        // Check that we're not being destroyed while something is still playing.
+        if (isPlaying()) {
+            Log.e("MediaPlaybackService", "Service being destroyed while still playing.");
+        }
+        // and for good measure, call mPlayer.stop(), which calls MediaPlayer.reset(), which
+        // releases the MediaPlayer's wake lock, if any.
+        mPlayer.stop();
+        
         if (mCursor != null) {
             mCursor.close();
             mCursor = null;
@@ -588,7 +598,6 @@ public class MediaPlaybackService extends Service {
         stop(true);
         notifyChange(QUEUE_CHANGED);
         notifyChange(META_CHANGED);
-        pushGadgetUpdate();
     }
 
     /**
@@ -614,7 +623,6 @@ public class MediaPlaybackService extends Service {
                         reloadQueue();
                         notifyChange(QUEUE_CHANGED);
                         notifyChange(META_CHANGED);
-                        pushGadgetUpdate();
                     }
                 }
             };
@@ -659,6 +667,9 @@ public class MediaPlaybackService extends Service {
         } else {
             saveQueue(false);
         }
+        
+        // Share this notification directly with our widgets
+        mAppWidgetProvider.notifyChange(this, what);
     }
 
     private void ensurePlayListCapacity(int size) {
@@ -723,18 +734,16 @@ public class MediaPlaybackService extends Service {
                 if (action == NOW) {
                     mPlayPos = mPlayListLen - list.length;
                     openCurrent();
-                    play(false /* we push update */);
+                    play();
                     notifyChange(META_CHANGED);
-                    pushGadgetUpdate();
                     return;
                 }
             }
             if (mPlayPos < 0) {
                 mPlayPos = 0;
                 openCurrent();
-                play(false /* we push update */);
+                play();
                 notifyChange(META_CHANGED);
-                pushGadgetUpdate();
             }
         }
     }
@@ -751,6 +760,7 @@ public class MediaPlaybackService extends Service {
             if (mShuffleMode == SHUFFLE_AUTO) {
                 mShuffleMode = SHUFFLE_NORMAL;
             }
+            int oldId = getAudioId();
             int listlength = list.length;
             boolean newlist = true;
             if (mPlayListLen == listlength) {
@@ -777,8 +787,7 @@ public class MediaPlaybackService extends Service {
 
             saveBookmarkIfNeeded();
             openCurrent();
-            if (!newlist && mPlayPos != oldpos) {
-                // the queue didn't change, but the position did
+            if (oldId != getAudioId()) {
                 notifyChange(META_CHANGED);
             }
         }
@@ -966,7 +975,7 @@ public class MediaPlaybackService extends Service {
     /**
      * Starts playback of a previously opened file.
      */
-    public void play(boolean shouldPushUpdate) {
+    public void play() {
         if (mPlayer.isInitialized()) {
             mPlayer.start();
             setForeground(true);
@@ -1009,9 +1018,6 @@ public class MediaPlaybackService extends Service {
                 notifyChange(PLAYSTATE_CHANGED);
             }
             mWasPlaying = true;
-            if (shouldPushUpdate) {
-                pushGadgetUpdate();
-            }
         } else if (mPlayListLen <= 0) {
             // This is mostly so that if you press 'play' on a bluetooth headset
             // without every having played anything before, it will still play
@@ -1020,21 +1026,6 @@ public class MediaPlaybackService extends Service {
         }
     }
     
-    /**
-     * Starts playback of a previously opened file.
-     */
-    public void play() {
-        // Default play action should push gadget updates
-        play(true);
-    }
-    
-    /**
-     * Push an update to all music gadgets.
-     */
-    private void pushGadgetUpdate() {
-        MediaGadgetProvider.updateAllGadgets(this, null);
-    }
-
     private void stop(boolean remove_status_icon) {
         if (mPlayer.isInitialized()) {
             mPlayer.stop();
@@ -1071,7 +1062,6 @@ public class MediaPlaybackService extends Service {
             mWasPlaying = false;
             notifyChange(PLAYSTATE_CHANGED);
             saveBookmarkIfNeeded();
-            pushGadgetUpdate();
         }
     }
 
@@ -1137,9 +1127,8 @@ public class MediaPlaybackService extends Service {
             saveBookmarkIfNeeded();
             stop(false);
             openCurrent();
-            play(false /* we push update */);
+            play();
             notifyChange(META_CHANGED);
-            pushGadgetUpdate();
         }
     }
 
@@ -1219,7 +1208,6 @@ public class MediaPlaybackService extends Service {
                         // all done
                         gotoIdleState();
                         notifyChange(PLAYBACK_COMPLETE);
-                        pushGadgetUpdate();
                         return;
                     } else if (mRepeatMode == REPEAT_ALL || force) {
                         mPlayPos = 0;
@@ -1231,9 +1219,8 @@ public class MediaPlaybackService extends Service {
             saveBookmarkIfNeeded();
             stop(false);
             openCurrent();
-            play(false /* we push update */);
+            play();
             notifyChange(META_CHANGED);
-            pushGadgetUpdate();
         }
     }
     
@@ -1430,9 +1417,8 @@ public class MediaPlaybackService extends Service {
                     doAutoShuffleUpdate();
                     mPlayPos = 0;
                     openCurrent();
-                    play(false /* we push update */);
+                    play();
                     notifyChange(META_CHANGED);
-                    pushGadgetUpdate();
                     return;
                 } else {
                     // failed to build a list of files to shuffle
@@ -1500,9 +1486,8 @@ public class MediaPlaybackService extends Service {
             stop(false);
             mPlayPos = pos;
             openCurrent();
-            play(false /* we push update */);
+            play();
             notifyChange(META_CHANGED);
-            pushGadgetUpdate();
         }
     }