diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-06-01 07:34:00 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-06-01 14:57:25 +0200 |
commit | 32d669a661edec69f88f5d2646071de761b87630 (patch) | |
tree | c671e1583f9962373233e2c84c1409a91ea687dd /app/src/main/java/com/wireguard/android/activity/SettingsActivity.java | |
parent | 918076a6703268307e7856dcd5f25c4f3dc870ab (diff) |
theme: add dark theme with toggle
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/activity/SettingsActivity.java')
-rw-r--r-- | app/src/main/java/com/wireguard/android/activity/SettingsActivity.java | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/app/src/main/java/com/wireguard/android/activity/SettingsActivity.java b/app/src/main/java/com/wireguard/android/activity/SettingsActivity.java index 040c5efc..d358c532 100644 --- a/app/src/main/java/com/wireguard/android/activity/SettingsActivity.java +++ b/app/src/main/java/com/wireguard/android/activity/SettingsActivity.java @@ -6,12 +6,14 @@ package com.wireguard.android.activity; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; +import android.support.v7.app.AppCompatDelegate; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceFragmentCompat; import android.view.MenuItem; @@ -19,6 +21,7 @@ import android.view.MenuItem; import com.wireguard.android.Application; import com.wireguard.android.R; import com.wireguard.android.backend.WgQuickBackend; +import com.wireguard.android.util.Topic; import java.util.ArrayList; import java.util.Arrays; @@ -30,7 +33,7 @@ import java.util.Map; * Interface for changing application-global persistent settings. */ -public class SettingsActivity extends AppCompatActivity { +public class SettingsActivity extends AppCompatActivity implements Topic.Subscriber { private final Map<Integer, PermissionRequestCallback> permissionRequestCallbacks = new HashMap<>(); private int permissionRequestCounter; @@ -56,6 +59,7 @@ public class SettingsActivity extends AppCompatActivity { @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); + subscribeTopics(); if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) { getSupportFragmentManager().beginTransaction() .add(android.R.id.content, new SettingsFragment()) @@ -64,6 +68,12 @@ public class SettingsActivity extends AppCompatActivity { } @Override + protected void onDestroy() { + unsubscribeTopics(); + super.onDestroy(); + } + + @Override public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { case android.R.id.home: @@ -89,7 +99,17 @@ public class SettingsActivity extends AppCompatActivity { void done(String[] permissions, int[] grantResults); } - public static class SettingsFragment extends PreferenceFragmentCompat { + @Override + public void onTopicPublished(Topic topic) { + recreate(); + } + + @Override + public Topic[] getSubscription() { + return new Topic[] { Application.getComponent().getThemeChangeTopic() }; + } + + public static class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { @Override public void onCreatePreferences(final Bundle savedInstanceState, final String key) { addPreferencesFromResource(R.xml.preferences); @@ -100,5 +120,26 @@ public class SettingsActivity extends AppCompatActivity { getPreferenceScreen().removePreference(pref); } } + + @Override + public void onResume() { + super.onResume(); + getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); + } + + @Override + public void onPause() { + getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); + super.onPause(); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if ("dark_theme".equals(key)) { + AppCompatDelegate.setDefaultNightMode( + sharedPreferences.getBoolean(key, false) ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO); + Application.getComponent().getThemeChangeTopic().publish(false); + } + } } } |