From 1a4458ef72581b33e860735062472893dd9446ce Mon Sep 17 00:00:00 2001
From: tehcneko <7764726+tehcneko@users.noreply.github.com>
Date: Mon, 15 Feb 2021 21:06:53 +0800
Subject: [PATCH] [app] Save dark mode setting with string
---
.../java/io/github/lsposed/manager/App.java | 3 +-
.../manager/ui/activity/SettingsActivity.java | 12 +--
.../ui/widget/IntegerListPreference.java | 83 -------------------
.../lsposed/manager/util/theme/ThemeUtil.java | 21 +++++
app/src/main/res/values/arrays.xml | 8 +-
app/src/main/res/xml/prefs.xml | 6 +-
6 files changed, 36 insertions(+), 97 deletions(-)
delete mode 100644 app/src/main/java/io/github/lsposed/manager/ui/widget/IntegerListPreference.java
diff --git a/app/src/main/java/io/github/lsposed/manager/App.java b/app/src/main/java/io/github/lsposed/manager/App.java
index 3200a07b..3c0b1f36 100644
--- a/app/src/main/java/io/github/lsposed/manager/App.java
+++ b/app/src/main/java/io/github/lsposed/manager/App.java
@@ -37,6 +37,7 @@ import java.io.StringWriter;
import io.github.lsposed.manager.repo.RepoLoader;
import io.github.lsposed.manager.ui.activity.CrashReportActivity;
import io.github.lsposed.manager.util.DoHDNS;
+import io.github.lsposed.manager.util.theme.ThemeUtil;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import rikka.material.app.DayNightDelegate;
@@ -93,7 +94,7 @@ public class App extends Application {
pref = PreferenceManager.getDefaultSharedPreferences(this);
DayNightDelegate.setApplicationContext(this);
- DayNightDelegate.setDefaultNightMode(pref.getInt("theme", -1));
+ DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme());
RepoLoader.getInstance().loadRemoteData();
}
diff --git a/app/src/main/java/io/github/lsposed/manager/ui/activity/SettingsActivity.java b/app/src/main/java/io/github/lsposed/manager/ui/activity/SettingsActivity.java
index cb1916ea..839972d6 100644
--- a/app/src/main/java/io/github/lsposed/manager/ui/activity/SettingsActivity.java
+++ b/app/src/main/java/io/github/lsposed/manager/ui/activity/SettingsActivity.java
@@ -58,8 +58,8 @@ import io.github.lsposed.manager.R;
import io.github.lsposed.manager.databinding.ActivitySettingsBinding;
import io.github.lsposed.manager.ui.activity.base.BaseActivity;
import io.github.lsposed.manager.ui.fragment.StatusDialogBuilder;
-import io.github.lsposed.manager.ui.widget.IntegerListPreference;
import io.github.lsposed.manager.util.BackupUtils;
+import io.github.lsposed.manager.util.theme.ThemeUtil;
import rikka.core.util.ResourceUtils;
import rikka.material.app.DayNightDelegate;
import rikka.recyclerview.RecyclerViewKt;
@@ -278,16 +278,16 @@ public class SettingsActivity extends BaseActivity {
if (restore != null) {
restore.setEnabled(installed);
restore.setOnPreferenceClickListener(preference -> {
- restoreLauncher.launch(new String[]{"*/*" });
+ restoreLauncher.launch(new String[]{"*/*"});
return true;
});
}
- IntegerListPreference theme = findPreference("theme");
+ Preference theme = findPreference("dark_theme");
if (theme != null) {
theme.setOnPreferenceChangeListener((preference, newValue) -> {
- if (preferences.getInt("theme", -1) != Integer.parseInt((String) newValue)) {
- DayNightDelegate.setDefaultNightMode(Integer.parseInt((String) newValue));
+ if (!preferences.getString("dark_theme", ThemeUtil.MODE_NIGHT_FOLLOW_SYSTEM).equals(newValue)) {
+ DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme((String) newValue));
SettingsActivity activity = (SettingsActivity) getActivity();
if (activity != null) {
activity.restart();
@@ -297,7 +297,7 @@ public class SettingsActivity extends BaseActivity {
});
}
- SwitchPreference black_dark_theme = findPreference("black_dark_theme");
+ Preference black_dark_theme = findPreference("black_dark_theme");
if (black_dark_theme != null) {
black_dark_theme.setOnPreferenceChangeListener((preference, newValue) -> {
SettingsActivity activity = (SettingsActivity) getActivity();
diff --git a/app/src/main/java/io/github/lsposed/manager/ui/widget/IntegerListPreference.java b/app/src/main/java/io/github/lsposed/manager/ui/widget/IntegerListPreference.java
deleted file mode 100644
index 371386d2..00000000
--- a/app/src/main/java/io/github/lsposed/manager/ui/widget/IntegerListPreference.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * This file is part of LSPosed.
- *
- * LSPosed is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * LSPosed is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with LSPosed. If not, see .
- *
- * Copyright (C) 2020 EdXposed Contributors
- * Copyright (C) 2021 LSPosed Contributors
- */
-
-package io.github.lsposed.manager.ui.widget;
-
-import android.content.Context;
-import android.content.SharedPreferences;
-import android.util.AttributeSet;
-
-import com.takisoft.preferencex.SimpleMenuPreference;
-
-@SuppressWarnings("unused")
-public class IntegerListPreference extends SimpleMenuPreference {
- public IntegerListPreference(Context context) {
- super(context);
- }
-
- public IntegerListPreference(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- private static int getIntValue(String value) {
- if (value == null)
- return 0;
-
- return (int) ((value.startsWith("0x"))
- ? Long.parseLong(value.substring(2), 16)
- : Long.parseLong(value));
- }
-
- @Override
- public void setValue(String value) {
- super.setValue(value);
- notifyChanged();
- }
-
- @Override
- protected boolean persistString(String value) {
- return value != null && persistInt(getIntValue(value));
-
- }
-
- @Override
- protected String getPersistedString(String defaultReturnValue) {
- SharedPreferences pref = getPreferenceManager().getSharedPreferences();
- String key = getKey();
- if (!shouldPersist() || !pref.contains(key))
- return defaultReturnValue;
-
- return String.valueOf(pref.getInt(key, 0));
- }
-
- @Override
- public int findIndexOfValue(String value) {
- CharSequence[] entryValues = getEntryValues();
- int intValue = getIntValue(value);
- if (value != null && entryValues != null) {
- for (int i = entryValues.length - 1; i >= 0; i--) {
- if (getIntValue(entryValues[i].toString()) == intValue) {
- return i;
- }
- }
- }
- return -1;
- }
-}
\ No newline at end of file
diff --git a/app/src/main/java/io/github/lsposed/manager/util/theme/ThemeUtil.java b/app/src/main/java/io/github/lsposed/manager/util/theme/ThemeUtil.java
index 0a5ece18..95b1ec03 100644
--- a/app/src/main/java/io/github/lsposed/manager/util/theme/ThemeUtil.java
+++ b/app/src/main/java/io/github/lsposed/manager/util/theme/ThemeUtil.java
@@ -31,11 +31,16 @@ import java.util.Map;
import io.github.lsposed.manager.App;
import io.github.lsposed.manager.R;
import rikka.core.util.ResourceUtils;
+import rikka.material.app.DayNightDelegate;
public class ThemeUtil {
private static final Map colorThemeMap = new HashMap<>();
private static final SharedPreferences preferences;
+ public static final String MODE_NIGHT_FOLLOW_SYSTEM = "MODE_NIGHT_FOLLOW_SYSTEM";
+ public static final String MODE_NIGHT_NO = "MODE_NIGHT_NO";
+ public static final String MODE_NIGHT_YES = "MODE_NIGHT_YES";
+
static {
preferences = App.getPreferences();
colorThemeMap.put("COLOR_PRIMARY", R.style.ThemeOverlay_color_primary);
@@ -139,4 +144,20 @@ public class ThemeUtil {
return resourceId;
}
}
+
+ public static int getDarkTheme(String mode) {
+ switch (mode) {
+ case MODE_NIGHT_FOLLOW_SYSTEM:
+ default:
+ return DayNightDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
+ case MODE_NIGHT_YES:
+ return DayNightDelegate.MODE_NIGHT_YES;
+ case MODE_NIGHT_NO:
+ return DayNightDelegate.MODE_NIGHT_NO;
+ }
+ }
+
+ public static int getDarkTheme() {
+ return getDarkTheme(preferences.getString("dark_theme", MODE_NIGHT_FOLLOW_SYSTEM));
+ }
}
diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml
index f50460cb..1d0a5cdc 100644
--- a/app/src/main/res/values/arrays.xml
+++ b/app/src/main/res/values/arrays.xml
@@ -22,15 +22,15 @@
- - @string/dark_theme_follow_system
- @string/dark_theme_off
- @string/dark_theme_on
+ - @string/dark_theme_follow_system
- - -1
- - 1
- - 2
+ - MODE_NIGHT_NO
+ - MODE_NIGHT_YES
+ - MODE_NIGHT_FOLLOW_SYSTEM
diff --git a/app/src/main/res/xml/prefs.xml b/app/src/main/res/xml/prefs.xml
index 4c866beb..ce9e9579 100644
--- a/app/src/main/res/xml/prefs.xml
+++ b/app/src/main/res/xml/prefs.xml
@@ -42,11 +42,11 @@
android:key="theme_color"
android:title="@string/theme_color"
app:iconSpaceReserved="false" />
-