Made sure that WidgetUpdateService is stopped if necessary

This commit is contained in:
daniel oeh 2012-09-05 14:19:37 +02:00
parent 8a76daeeaf
commit 42a5641439
4 changed files with 36 additions and 16 deletions

View File

@ -118,6 +118,9 @@
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/player_widget_info" />
<intent-filter>
<action android:name="de.danoeh.antennapod.STOP_WIDGET_UPDATE"/>
</intent-filter>
</receiver>
<receiver android:name=".receiver.FeedUpdateReceiver" >
<intent-filter>

View File

@ -11,25 +11,25 @@ import android.util.Log;
public class PlayerWidget extends AppWidgetProvider {
private static final String TAG = "PlayerWidget";
public static final String FORCE_WIDGET_UPDATE = "de.danoeh.antennapod.FORCE_WIDGET_UPDATE";
public static final String STOP_WIDGET_UPDATE = "de.danoeh.antennapod.STOP_WIDGET_UPDATE";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(FORCE_WIDGET_UPDATE)) {
startUpdate(context);
} else if (intent.getAction().equals(STOP_WIDGET_UPDATE)) {
stopUpdate(context);
}
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
if (AppConfig.DEBUG) Log.d(TAG, "Widget enabled");
if (AppConfig.DEBUG)
Log.d(TAG, "Widget enabled");
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
@ -40,4 +40,8 @@ public class PlayerWidget extends AppWidgetProvider {
context.startService(new Intent(context, PlayerWidgetService.class));
}
private void stopUpdate(Context context) {
context.stopService(new Intent(context, PlayerWidgetService.class));
}
}

View File

@ -604,8 +604,8 @@ public class PlaybackService extends Service {
positionSaver.cancel(true);
}
saveCurrentPosition();
stopWidgetUpdater();
setStatus(PlayerStatus.PAUSED);
stopWidgetUpdater();
stopForeground(true);
}
}
@ -658,7 +658,7 @@ public class PlaybackService extends Service {
updateWidget();
refreshRemoteControlClientState();
}
/** Send ACTION_PLAYER_STATUS_CHANGED without changing the status attribute. */
private void postStatusUpdateIntent() {
setStatus(status);
@ -728,6 +728,7 @@ public class PlaybackService extends Service {
if (widgetUpdater != null) {
widgetUpdater.cancel(true);
}
sendBroadcast(new Intent(PlayerWidget.STOP_WIDGET_UPDATE));
}
@SuppressLint("NewApi")
@ -1005,7 +1006,5 @@ public class PlaybackService extends Service {
this.startWhenPrepared = startWhenPrepared;
postStatusUpdateIntent();
}
}

View File

@ -34,10 +34,19 @@ public class PlayerWidgetService extends Service {
@Override
public void onCreate() {
super.onCreate();
if (AppConfig.DEBUG) Log.d(TAG, "Service created");
if (AppConfig.DEBUG)
Log.d(TAG, "Service created");
isUpdating = false;
}
@Override
public void onDestroy() {
super.onDestroy();
if (AppConfig.DEBUG)
Log.d(TAG, "Service is about to be destroyed");
unbindService(mConnection);
}
@Override
public IBinder onBind(Intent intent) {
return null;
@ -55,14 +64,16 @@ public class PlayerWidgetService extends Service {
isUpdating = false;
}
} else {
if (AppConfig.DEBUG) Log.d(TAG,
"Service was called while updating. Ignoring update request");
if (AppConfig.DEBUG)
Log.d(TAG,
"Service was called while updating. Ignoring update request");
}
return Service.START_NOT_STICKY;
}
private void updateViews() {
if (AppConfig.DEBUG) Log.d(TAG, "Updating widget views");
if (AppConfig.DEBUG)
Log.d(TAG, "Updating widget views");
ComponentName playerWidget = new ComponentName(this, PlayerWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
RemoteViews views = new RemoteViews(getPackageName(),
@ -88,7 +99,8 @@ public class PlayerWidgetService extends Service {
views.setOnClickPendingIntent(R.id.butPlay,
createMediaButtonIntent());
} else {
if (AppConfig.DEBUG) Log.d(TAG, "No media playing. Displaying defaultt views");
if (AppConfig.DEBUG)
Log.d(TAG, "No media playing. Displaying defaultt views");
views.setViewVisibility(R.id.txtvProgress, View.INVISIBLE);
views.setTextViewText(R.id.txtvTitle,
this.getString(R.string.no_media_playing_label));
@ -118,7 +130,8 @@ public class PlayerWidgetService extends Service {
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
if (AppConfig.DEBUG) Log.d(TAG, "Connection to service established");
if (AppConfig.DEBUG)
Log.d(TAG, "Connection to service established");
playbackService = ((PlaybackService.LocalBinder) service)
.getService();
updateViews();
@ -128,7 +141,8 @@ public class PlayerWidgetService extends Service {
@Override
public void onServiceDisconnected(ComponentName name) {
playbackService = null;
if (AppConfig.DEBUG) Log.d(TAG, "Disconnected from service");
if (AppConfig.DEBUG)
Log.d(TAG, "Disconnected from service");
}
};