From 81363832ecf27a048a90c1a7a92d742aa94fe621 Mon Sep 17 00:00:00 2001 From: doubleangels Date: Fri, 24 Jan 2025 02:22:52 -0700 Subject: [PATCH 1/2] Fix broken dark mode --- .../nextdnsmanagement/MainActivity.java | 12 +++-- .../nextdnsmanagement/SettingsActivity.java | 32 ++++++------- .../SharedPreferencesManager.java | 45 +++++++++++++++++++ 3 files changed, 64 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/com/doubleangels/nextdnsmanagement/sharedpreferences/SharedPreferencesManager.java diff --git a/app/src/main/java/com/doubleangels/nextdnsmanagement/MainActivity.java b/app/src/main/java/com/doubleangels/nextdnsmanagement/MainActivity.java index 6e1dc2b..d1bcea3 100644 --- a/app/src/main/java/com/doubleangels/nextdnsmanagement/MainActivity.java +++ b/app/src/main/java/com/doubleangels/nextdnsmanagement/MainActivity.java @@ -3,11 +3,10 @@ import static android.Manifest.permission.POST_NOTIFICATIONS; -import android.content.ComponentCallbacks2; import android.annotation.SuppressLint; import android.app.DownloadManager; +import android.content.ComponentCallbacks2; import android.content.Intent; -import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.net.Uri; @@ -35,13 +34,13 @@ import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.lifecycle.LifecycleOwner; -import androidx.preference.PreferenceManager; import androidx.webkit.WebSettingsCompat; import androidx.webkit.WebViewFeature; import com.doubleangels.nextdnsmanagement.protocol.VisualIndicator; import com.doubleangels.nextdnsmanagement.sentry.SentryInitializer; import com.doubleangels.nextdnsmanagement.sentry.SentryManager; +import com.doubleangels.nextdnsmanagement.sharedpreferences.SharedPreferencesManager; import com.jakewharton.processphoenix.ProcessPhoenix; import java.util.Locale; @@ -75,7 +74,7 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); if (!ProcessPhoenix.isPhoenixProcess(this)) { SentryManager sentryManager = new SentryManager(this); - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + SharedPreferencesManager.init(this); try { if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) == PackageManager.PERMISSION_DENIED) { ActivityCompat.requestPermissions(this, new String[]{POST_NOTIFICATIONS}, 1); @@ -88,7 +87,7 @@ protected void onCreate(Bundle savedInstanceState) { setupToolbarForActivity(); String appLocale = setupLanguageForActivity(); sentryManager.captureMessage("Using locale: " + appLocale); - setupDarkModeForActivity(sentryManager, sharedPreferences); + setupDarkModeForActivity(sentryManager, SharedPreferencesManager.getString("dark_mode", "match")); setupVisualIndicatorForActivity(sentryManager, this); setupWebViewForActivity(getString(R.string.main_url)); } catch (Exception e) { @@ -171,8 +170,7 @@ private String setupLanguageForActivity() { return appLocale.getLanguage(); } - private void setupDarkModeForActivity(SentryManager sentryManager, SharedPreferences sharedPreferences) { - String darkMode = sharedPreferences.getString("dark_mode", "match"); + private void setupDarkModeForActivity(SentryManager sentryManager, String darkMode) { switch (darkMode) { case "match": AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); diff --git a/app/src/main/java/com/doubleangels/nextdnsmanagement/SettingsActivity.java b/app/src/main/java/com/doubleangels/nextdnsmanagement/SettingsActivity.java index 51e0ce4..9f4ea59 100644 --- a/app/src/main/java/com/doubleangels/nextdnsmanagement/SettingsActivity.java +++ b/app/src/main/java/com/doubleangels/nextdnsmanagement/SettingsActivity.java @@ -4,7 +4,6 @@ import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.content.res.Configuration; import android.net.Uri; import android.os.Build; @@ -18,11 +17,11 @@ import androidx.preference.ListPreference; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; -import androidx.preference.PreferenceManager; import androidx.preference.SwitchPreference; import com.doubleangels.nextdnsmanagement.sentry.SentryInitializer; import com.doubleangels.nextdnsmanagement.sentry.SentryManager; +import com.doubleangels.nextdnsmanagement.sharedpreferences.SharedPreferencesManager; import com.jakewharton.processphoenix.ProcessPhoenix; import java.util.Locale; @@ -36,14 +35,14 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); sentryManager = new SentryManager(this); - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + SharedPreferencesManager.init(this); try { if (sentryManager.isEnabled()) { SentryInitializer.initialize(this); } String appLocale = setupLanguageForActivity(); sentryManager.captureMessage("Using locale: " + appLocale); - setupDarkModeForActivity(sharedPreferences); + setupDarkModeForActivity(SharedPreferencesManager.getString("dark_mode", "match")); initializeViews(); } catch (Exception e) { sentryManager.captureException(e); @@ -60,9 +59,8 @@ private String setupLanguageForActivity() { return appLocale.getLanguage(); } - private void setupDarkModeForActivity(SharedPreferences sharedPreferences) { + private void setupDarkModeForActivity(String darkMode) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { - String darkMode = sharedPreferences.getString("dark_mode", "match"); sentryManager.captureMessage("Dark mode setting: " + darkMode); if (darkMode.contains("match")) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); @@ -86,15 +84,16 @@ public static class SettingsFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { setPreferencesFromResource(R.xml.root_preferences, rootKey); - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()); - setInitialSentryVisibility(sharedPreferences); + SharedPreferencesManager.init(requireContext()); + setInitialSentryVisibility(SharedPreferencesManager.getBoolean("sentry_enable", false)); SwitchPreference sentryEnablePreference = findPreference("sentry_enable"); ListPreference darkModePreference = findPreference("dark_mode"); if (sentryEnablePreference != null) { - setupSentryChangeListener(sentryEnablePreference, sharedPreferences); + Log.d("TEST", sentryEnablePreference.toString()); + setupSentryChangeListener(sentryEnablePreference); } if (darkModePreference != null) { - setupDarkModeChangeListener(darkModePreference, sharedPreferences); + setupDarkModeChangeListener(darkModePreference); } setupButton("whitelist_domain_1_button", R.string.whitelist_domain_1); @@ -118,8 +117,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { } } - private void setInitialSentryVisibility(SharedPreferences sharedPreferences) { - boolean visibility = sharedPreferences.getBoolean("sentry_enable", false); + private void setInitialSentryVisibility(Boolean visibility) { setPreferenceVisibility("whitelist_domains", visibility); setPreferenceVisibility("whitelist_domain_1_button", visibility); setPreferenceVisibility("whitelist_domain_2_button", visibility); @@ -159,25 +157,23 @@ private void setupButtonForIntent(String buttonKey) { }); } - private void setupDarkModeChangeListener(ListPreference setting, SharedPreferences sharedPreferences) { + private void setupDarkModeChangeListener(ListPreference setting) { setting.setOnPreferenceChangeListener((preference, newValue) -> { Log.i("Output","Output: " + newValue.toString()); - sharedPreferences.edit().putString("dark_mode", newValue.toString()).apply(); + SharedPreferencesManager.putString("dark_mode", newValue.toString()); ProcessPhoenix.triggerRebirth(requireContext()); return true; }); } - private void setupSentryChangeListener(SwitchPreference switchPreference, SharedPreferences sharedPreferences) { + private void setupSentryChangeListener(SwitchPreference switchPreference) { if (switchPreference != null) { switchPreference.setOnPreferenceChangeListener((preference, newValue) -> { boolean isEnabled = (boolean) newValue; - SharedPreferences.Editor preferenceEdit = sharedPreferences.edit(); - preferenceEdit.putBoolean("sentry_enable", isEnabled); + SharedPreferencesManager.putBoolean("sentry_enable", isEnabled); setPreferenceVisibility("whitelist_domains", isEnabled); setPreferenceVisibility("whitelist_domain_1_button", isEnabled); setPreferenceVisibility("whitelist_domain_2_button", isEnabled); - preferenceEdit.apply(); return true; }); } diff --git a/app/src/main/java/com/doubleangels/nextdnsmanagement/sharedpreferences/SharedPreferencesManager.java b/app/src/main/java/com/doubleangels/nextdnsmanagement/sharedpreferences/SharedPreferencesManager.java new file mode 100644 index 0000000..f96800d --- /dev/null +++ b/app/src/main/java/com/doubleangels/nextdnsmanagement/sharedpreferences/SharedPreferencesManager.java @@ -0,0 +1,45 @@ +package com.doubleangels.nextdnsmanagement.sharedpreferences; + +import android.content.Context; +import android.content.SharedPreferences; + +public class SharedPreferencesManager { + private static final String PREF_NAME = "MyAppPreferences"; + private static SharedPreferences sharedPreferences; + + private SharedPreferencesManager() { + throw new UnsupportedOperationException("Cannot instantiate SharedPreferencesManager."); + } + + public static synchronized void init(Context context) { + if (sharedPreferences == null) { + sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + } + } + + public static void putString(String key, String value) { + checkInitialization(); + sharedPreferences.edit().putString(key, value).apply(); + } + + public static String getString(String key, String defaultValue) { + checkInitialization(); + return sharedPreferences.getString(key, defaultValue); + } + + public static void putBoolean(String key, boolean value) { + checkInitialization(); + sharedPreferences.edit().putBoolean(key, value).apply(); + } + + public static boolean getBoolean(String key, boolean defaultValue) { + checkInitialization(); + return sharedPreferences.getBoolean(key, defaultValue); + } + + private static void checkInitialization() { + if (sharedPreferences == null) { + throw new IllegalStateException("SharedPreferencesManager is not initialized. Call init() before using it."); + } + } +} From a0b8969796733105e8aa739ec1a96bdcce7bcce5 Mon Sep 17 00:00:00 2001 From: doubleangels Date: Fri, 24 Jan 2025 02:23:37 -0700 Subject: [PATCH 2/2] New build for release --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d3a6ab7..bdbad1a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "com.doubleangels.nextdnsmanagement" minSdkVersion 32 targetSdk 35 - versionCode 239 - versionName '5.4.5' + versionCode 240 + versionName '5.4.6' resourceConfigurations += ["en", "zh", "nl", "fi", "fr", "de", "in", "it", "ja", "pl", "pt", "es", "sv", "tr"] }