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;
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;
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;
// 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;
next(false);
} else {
notifyChange(PLAYBACK_COMPLETE);
- pushGadgetUpdate();
}
break;
case RELEASE_WAKELOCK:
} 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);
}
}
};
@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;
stop(true);
notifyChange(QUEUE_CHANGED);
notifyChange(META_CHANGED);
- pushGadgetUpdate();
}
/**
reloadQueue();
notifyChange(QUEUE_CHANGED);
notifyChange(META_CHANGED);
- pushGadgetUpdate();
}
}
};
} else {
saveQueue(false);
}
+
+ // Share this notification directly with our widgets
+ mAppWidgetProvider.notifyChange(this, what);
}
private void ensurePlayListCapacity(int size) {
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();
}
}
}
if (mShuffleMode == SHUFFLE_AUTO) {
mShuffleMode = SHUFFLE_NORMAL;
}
+ int oldId = getAudioId();
int listlength = list.length;
boolean newlist = true;
if (mPlayListLen == listlength) {
saveBookmarkIfNeeded();
openCurrent();
- if (!newlist && mPlayPos != oldpos) {
- // the queue didn't change, but the position did
+ if (oldId != getAudioId()) {
notifyChange(META_CHANGED);
}
}
/**
* Starts playback of a previously opened file.
*/
- public void play(boolean shouldPushUpdate) {
+ public void play() {
if (mPlayer.isInitialized()) {
mPlayer.start();
setForeground(true);
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
}
}
- /**
- * 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();
mWasPlaying = false;
notifyChange(PLAYSTATE_CHANGED);
saveBookmarkIfNeeded();
- pushGadgetUpdate();
}
}
saveBookmarkIfNeeded();
stop(false);
openCurrent();
- play(false /* we push update */);
+ play();
notifyChange(META_CHANGED);
- pushGadgetUpdate();
}
}
// all done
gotoIdleState();
notifyChange(PLAYBACK_COMPLETE);
- pushGadgetUpdate();
return;
} else if (mRepeatMode == REPEAT_ALL || force) {
mPlayPos = 0;
saveBookmarkIfNeeded();
stop(false);
openCurrent();
- play(false /* we push update */);
+ play();
notifyChange(META_CHANGED);
- pushGadgetUpdate();
}
}
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
stop(false);
mPlayPos = pos;
openCurrent();
- play(false /* we push update */);
+ play();
notifyChange(META_CHANGED);
- pushGadgetUpdate();
}
}