From 926f99478b682bc6e4cf300c416e3984d8aae2d9 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Sun, 29 Nov 2015 13:49:27 -0500 Subject: [PATCH] store enabledness in preferences, be sure to tell the main fragment what we want --- .../antennapod/receiver/PlayerWidget.java | 25 +++++++++++++------ .../service/PlayerWidgetService.java | 2 ++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/receiver/PlayerWidget.java b/app/src/main/java/de/danoeh/antennapod/receiver/PlayerWidget.java index c6c65f6da..a318c3433 100644 --- a/app/src/main/java/de/danoeh/antennapod/receiver/PlayerWidget.java +++ b/app/src/main/java/de/danoeh/antennapod/receiver/PlayerWidget.java @@ -4,6 +4,7 @@ import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.util.Log; import org.apache.commons.lang3.StringUtils; @@ -14,17 +15,15 @@ import de.danoeh.antennapod.service.PlayerWidgetService; public class PlayerWidget extends AppWidgetProvider { private static final String TAG = "PlayerWidget"; - - // static because there should only ever be one widget... - // and otherwise it just keeps getting reset when it gets messages - private static boolean enabled = false; + private static final String PREFS_NAME = "PlayerWidgetPrefs"; + private static final String KEY_ENABLED = "WidgetEnabled"; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive"); super.onReceive(context, intent); - if (!enabled) { - // do nothing + // don't do anything if we're not enabled + if (!isEnabled(context)) { return; } @@ -40,7 +39,7 @@ public class PlayerWidget extends AppWidgetProvider { public void onEnabled(Context context) { super.onEnabled(context); Log.d(TAG, "Widget enabled"); - enabled = true; + setEnabled(context, true); startUpdate(context); } @@ -55,7 +54,7 @@ public class PlayerWidget extends AppWidgetProvider { public void onDisabled(Context context) { super.onDisabled(context); Log.d(TAG, "Widet disabled"); - enabled = false; + setEnabled(context, false); stopUpdate(context); } @@ -68,4 +67,14 @@ public class PlayerWidget extends AppWidgetProvider { Log.d(TAG, "stopUpdate() called with: " + "context = [" + context + "]"); context.stopService(new Intent(context, PlayerWidgetService.class)); } + + private boolean isEnabled(Context context) { + SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + return prefs.getBoolean(KEY_ENABLED, false); + } + + private void setEnabled(Context context, boolean enabled) { + SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + prefs.edit().putBoolean(KEY_ENABLED, enabled); + } } diff --git a/app/src/main/java/de/danoeh/antennapod/service/PlayerWidgetService.java b/app/src/main/java/de/danoeh/antennapod/service/PlayerWidgetService.java index 91a8dc712..d61a189c2 100644 --- a/app/src/main/java/de/danoeh/antennapod/service/PlayerWidgetService.java +++ b/app/src/main/java/de/danoeh/antennapod/service/PlayerWidgetService.java @@ -23,6 +23,7 @@ import de.danoeh.antennapod.core.service.playback.PlayerStatus; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.playback.Playable; +import de.danoeh.antennapod.fragment.QueueFragment; import de.danoeh.antennapod.receiver.PlayerWidget; /** @@ -115,6 +116,7 @@ public class PlayerWidgetService extends Service { Intent startApp = new Intent(getBaseContext(), MainActivity.class); startApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startApp.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, QueueFragment.TAG); PendingIntent startAppPending = PendingIntent.getActivity(getBaseContext(), 0, startApp, PendingIntent.FLAG_UPDATE_CURRENT); boolean nothingPlaying = false;