Skip to content

Commit

Permalink
Merge pull request #378 from doubleangels/dev
Browse files Browse the repository at this point in the history
Fix broken dark mode
doubleangels authored Jan 24, 2025
2 parents 2ff606f + a0b8969 commit 4eae526
Showing 4 changed files with 66 additions and 27 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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"]
}

Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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;
});
}
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}

0 comments on commit 4eae526

Please sign in to comment.