From 051fc0a8d792f766b99953181149f888d262d1e4 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 9 Jan 2016 09:00:18 +0100 Subject: [PATCH] Better handle service theme changes --- .../faircode/netguard/ActivitySettings.java | 2 - .../eu/faircode/netguard/SinkholeService.java | 54 +++++++++---------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/ActivitySettings.java b/app/src/main/java/eu/faircode/netguard/ActivitySettings.java index 9094b202..c4a622ab 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivitySettings.java +++ b/app/src/main/java/eu/faircode/netguard/ActivitySettings.java @@ -344,7 +344,6 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_delay, prefs.getString(name, "0"))); else if ("theme".equals(name) || "dark_theme".equals(name)) { - SinkholeService.setTheme(this); recreate(); } else if ("wifi_homes".equals(name)) { @@ -563,7 +562,6 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere if (ex == null) { Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show(); recreate(); - SinkholeService.setTheme(ActivitySettings.this); } else Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show(); } diff --git a/app/src/main/java/eu/faircode/netguard/SinkholeService.java b/app/src/main/java/eu/faircode/netguard/SinkholeService.java index 5ce3cc32..98ed5098 100644 --- a/app/src/main/java/eu/faircode/netguard/SinkholeService.java +++ b/app/src/main/java/eu/faircode/netguard/SinkholeService.java @@ -75,7 +75,7 @@ import java.util.List; import java.util.Set; import java.util.TreeMap; -public class SinkholeService extends VpnService { +public class SinkholeService extends VpnService implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG = "NetGuard.Service"; private State state = State.none; @@ -109,7 +109,7 @@ public class SinkholeService extends VpnService { private enum State {none, waiting, enforcing, stats} - public enum Command {run, start, reload, stop, stats, set, theme} + public enum Command {run, start, reload, stop, stats, set} private static volatile PowerManager.WakeLock wlInstance = null; @@ -284,10 +284,6 @@ public class SinkholeService extends VpnService { case set: set(intent); break; - - case theme: - setTheme(intent); - break; } // Update main view @@ -560,19 +556,6 @@ public class SinkholeService extends VpnService { } } - private void setTheme(Intent intent) { - Util.setTheme(this); - if (state != State.none) { - Log.d(TAG, "Stop foreground state=" + state.toString()); - stopForeground(true); - } - if (state == State.enforcing) - startForeground(NOTIFY_ENFORCING, getEnforcingNotification(0, 0)); - else if (state != State.none) - startForeground(NOTIFY_WAITING, getWaitingNotification()); - Log.d(TAG, "Start foreground state=" + state.toString()); - } - private ParcelFileDescriptor startVPN() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); @@ -890,6 +873,10 @@ public class SinkholeService extends VpnService { @Override public void onCreate() { Log.i(TAG, "Create"); + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + prefs.registerOnSharedPreferenceChangeListener(this); + Util.setTheme(this); super.onCreate(); @@ -926,6 +913,23 @@ public class SinkholeService extends VpnService { registerReceiver(packageAddedReceiver, ifPackage); } + @Override + public void onSharedPreferenceChanged(SharedPreferences prefs, String name) { + if ("theme".equals(name)) { + Log.i(TAG, "Theme changed"); + Util.setTheme(this); + if (state != State.none) { + Log.d(TAG, "Stop foreground state=" + state.toString()); + stopForeground(true); + } + if (state == State.enforcing) + startForeground(NOTIFY_ENFORCING, getEnforcingNotification(0, 0)); + else if (state != State.none) + startForeground(NOTIFY_WAITING, getWaitingNotification()); + Log.d(TAG, "Start foreground state=" + state.toString()); + } + } + @Override public int onStartCommand(Intent intent, int flags, int startId) { // Keep awake @@ -1006,6 +1010,9 @@ public class SinkholeService extends VpnService { vpn = null; } + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + prefs.unregisterOnSharedPreferenceChangeListener(this); + super.onDestroy(); } @@ -1117,15 +1124,6 @@ public class SinkholeService extends VpnService { context.startService(intent); } - public static void setTheme(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (prefs.getBoolean("enabled", false)) { - Intent intent = new Intent(context, SinkholeService.class); - intent.putExtra(EXTRA_COMMAND, Command.theme); - context.startService(intent); - } - } - public static void reloadStats(String reason, Context context) { Intent intent = new Intent(context, SinkholeService.class); intent.putExtra(EXTRA_COMMAND, Command.stats);