Skip to content

Commit

Permalink
Let Shizuku be dark mode by default on watch device. (#107)
Browse files Browse the repository at this point in the history
Delete the enter from last pr.
  • Loading branch information
ryuunoakaihitomi authored Apr 30, 2021
1 parent 75ba480 commit 6b52b66
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion manager/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@

</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Locale;

import moe.shizuku.manager.utils.EmptySharedPreferencesImpl;
import moe.shizuku.manager.utils.EnvironmentUtils;
import rikka.material.app.DayNightDelegate;
import rikka.material.app.DayNightDelegate.NightMode;

Expand Down Expand Up @@ -85,7 +86,11 @@ public static void setLastLaunchMode(@LaunchMethod int method) {

@NightMode
public static int getNightMode() {
return getPreferences().getInt(NIGHT_MODE, DayNightDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
int defValue = DayNightDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
if (EnvironmentUtils.isWatch(DayNightDelegate.getApplicationContext())) {
defValue = DayNightDelegate.MODE_NIGHT_YES;
}
return getPreferences().getInt(NIGHT_MODE, defValue);
}

public static Locale getLocale() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import moe.shizuku.manager.R;
import moe.shizuku.manager.ShizukuSettings;
import moe.shizuku.manager.utils.EnvironmentUtils;
import rikka.core.util.ResourceUtils;

public class ThemeHelper {
Expand All @@ -16,17 +17,18 @@ public class ThemeHelper {
public static final String KEY_LIGHT_THEME = "light_theme";
public static final String KEY_BLACK_NIGHT_THEME = "black_night_theme";

public static boolean isBlackNightTheme() {
return ShizukuSettings.getPreferences().getBoolean(KEY_BLACK_NIGHT_THEME, false);
public static boolean isBlackNightTheme(Context context) {
return ShizukuSettings.getPreferences().getBoolean(KEY_BLACK_NIGHT_THEME, EnvironmentUtils.isWatch(context));
}

public static String getTheme(Context context) {
if (isBlackNightTheme()
if (isBlackNightTheme(context)
&& ResourceUtils.isNightMode(context.getResources().getConfiguration()))
return THEME_BLACK;

return ShizukuSettings.getPreferences().getString(KEY_LIGHT_THEME, THEME_DEFAULT);
}

@StyleRes
public static int getThemeStyleRes(Context context) {
switch (getTheme(context)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.recyclerview.widget.RecyclerView
import moe.shizuku.manager.R
import moe.shizuku.manager.ShizukuSettings
import moe.shizuku.manager.ShizukuSettings.KEEP_START_ON_BOOT
import moe.shizuku.manager.app.ThemeHelper
import moe.shizuku.manager.app.ThemeHelper.KEY_BLACK_NIGHT_THEME
import moe.shizuku.manager.ktx.isComponentEnabled
import moe.shizuku.manager.ktx.setComponentEnabled
Expand Down Expand Up @@ -41,7 +42,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
}

private lateinit var languagePreference: ListPreference
private lateinit var nightModePreference: Preference
private lateinit var nightModePreference: IntegerSimpleMenuPreference
private lateinit var blackNightThemePreference: SwitchPreference
private lateinit var startOnBootPreference: SwitchPreference
private lateinit var startupPreference: PreferenceCategory
Expand Down Expand Up @@ -114,6 +115,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
val name = localeNameUser[index - 1]
languagePreference.summary = name
}
nightModePreference.value = ShizukuSettings.getNightMode()
nightModePreference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _: Preference?, value: Any? ->
if (value is Int) {
if (ShizukuSettings.getNightMode() != value) {
Expand All @@ -123,6 +125,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
true
}
blackNightThemePreference.isChecked = ThemeHelper.isBlackNightTheme(context)
blackNightThemePreference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _: Preference?, _: Any? ->
if (ResourceUtils.isNightMode(requireContext().resources.configuration)) activity?.recreate()
true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package moe.shizuku.manager.utils;

import android.app.UiModeManager;
import android.content.Context;
import android.content.res.Configuration;

public class EnvironmentUtils {

private EnvironmentUtils() {
}

public static boolean isWatch(Context context) {
return context.getSystemService(UiModeManager.class).getCurrentModeType()
== Configuration.UI_MODE_TYPE_WATCH;
}
}
2 changes: 0 additions & 2 deletions manager/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@
android:title="@string/settings_user_interface">

<moe.shizuku.manager.settings.IntegerSimpleMenuPreference
android:defaultValue="-1"
android:entries="@array/night_mode"
android:entryValues="@array/night_mode_value"
android:key="night_mode"
android:summary="%s"
android:title="@string/dark_theme" />

<SwitchPreference
android:defaultValue="false"
android:key="black_night_theme"
android:summary="@string/settings_black_night_theme_summary"
android:title="@string/settings_black_night_theme" />
Expand Down

0 comments on commit 6b52b66

Please sign in to comment.