mirror of https://github.com/M66B/NetGuard.git
Better handle service theme changes
This commit is contained in:
parent
c9392c1d81
commit
051fc0a8d7
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue