From 6ee1a39fb4e5913e4f7f75004b51ff06c25c1fd0 Mon Sep 17 00:00:00 2001 From: Ishan09811 <156402647+Ishan09811@users.noreply.github.com> Date: Sun, 18 Feb 2024 02:55:48 +0530 Subject: [PATCH] Add Dynamic colors option for Android 12 or above (#8) * Dynamic color for android 12 or above @Ishan09811 --- .../pandroid/app/PandroidApplication.java | 7 ++++++ .../preferences/AppearancePreferences.java | 22 ++++++++++++++++++- .../pandroid/data/config/GlobalConfig.java | 1 + .../app/src/main/res/values/strings.xml | 5 ++++- .../main/res/xml/appearance_preference.xml | 18 +++++++++++++-- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/PandroidApplication.java b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/PandroidApplication.java index b0cdc935..9d440865 100644 --- a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/PandroidApplication.java +++ b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/PandroidApplication.java @@ -5,6 +5,8 @@ import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; +import com.google.android.material.color.DynamicColors; +import com.google.android.material.color.DynamicColorsOptions; import com.panda3ds.pandroid.AlberDriver; import com.panda3ds.pandroid.R; @@ -30,6 +32,11 @@ public class PandroidApplication extends Application { if (GlobalConfig.get(GlobalConfig.KEY_LOGGER_SERVICE)) { startService(new Intent(this, LoggerService.class)); } + if (GlobalConfig.get(GlobalConfig.KEY_DYNAMIC_COLORS)) { + DynamicColorsOptions dynamicColorsOptions = new DynamicColorsOptions.Builder() + .build(); + DynamicColors.applyToActivitiesIfAvailable(this, dynamicColorsOptions); + } } public static int getThemeId() { diff --git a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/preferences/AppearancePreferences.java b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/preferences/AppearancePreferences.java index 04c89d9a..3e499aab 100644 --- a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/preferences/AppearancePreferences.java +++ b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/preferences/AppearancePreferences.java @@ -3,6 +3,8 @@ package com.panda3ds.pandroid.app.preferences; import android.os.Bundle; import androidx.annotation.Nullable; +import androidx.preference.SwitchPreference; +import com.google.android.material.color.DynamicColors; import com.panda3ds.pandroid.R; import com.panda3ds.pandroid.app.BaseActivity; @@ -23,5 +25,23 @@ public class AppearancePreferences extends BasePreferenceFragment { GlobalConfig.set(GlobalConfig.KEY_APP_THEME, (int) value); return false; }); + setItemClick("dynamic_colors", pref -> GlobalConfig.set(GlobalConfig.KEY_DYNAMIC_COLORS, ((SwitchPreference) pref).isChecked())); + + refresh(); + } + + @Override + public void onResume() { + super.onResume(); + refresh(); + } + + private void refresh() { + ((SwitchPreference) findPreference("dynamic_colors")).setChecked(GlobalConfig.get(GlobalConfig.KEY_DYNAMIC_COLORS)); + if (!DynamicColors.isDynamicColorAvailable()) { + ((SwitchPreference) findPreference("dynamic_colors")).setEnabled(false); + } + } -} + + } diff --git a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/data/config/GlobalConfig.java b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/data/config/GlobalConfig.java index bff1f9e0..f2f5d9b5 100644 --- a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/data/config/GlobalConfig.java +++ b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/data/config/GlobalConfig.java @@ -20,6 +20,7 @@ public class GlobalConfig { public static final Key KEY_SHADER_JIT = new Key<>("emu.shader_jit", false); public static final Key KEY_SHOW_PERFORMANCE_OVERLAY = new Key<>("dev.performanceOverlay", false); + public static final Key KEY_DYNAMIC_COLORS = new Key<>("app.dynamic_colors", false); public static final Key KEY_LOGGER_SERVICE = new Key<>("dev.loggerService", false); public static final Key KEY_APP_THEME = new Key<>("app.theme", THEME_ANDROID); public static final Key KEY_SCREEN_GAMEPAD_VISIBLE = new Key<>("app.screen_gamepad.visible", true); diff --git a/src/pandroid/app/src/main/res/values/strings.xml b/src/pandroid/app/src/main/res/values/strings.xml index 20e6c5c8..a902816f 100644 --- a/src/pandroid/app/src/main/res/values/strings.xml +++ b/src/pandroid/app/src/main/res/values/strings.xml @@ -34,6 +34,9 @@ Light Dark Black + Colors + Use Dynamic Colors + Dynamic colors will be used. Actions Exit @@ -56,8 +59,8 @@ Show overlay with fps, memory, etc. Logger Store application logs to file. + Graphics Shader JIT Use shader recompiler. - Graphics Loading diff --git a/src/pandroid/app/src/main/res/xml/appearance_preference.xml b/src/pandroid/app/src/main/res/xml/appearance_preference.xml index dd1ed483..2210e71a 100644 --- a/src/pandroid/app/src/main/res/xml/appearance_preference.xml +++ b/src/pandroid/app/src/main/res/xml/appearance_preference.xml @@ -1,5 +1,7 @@ - + - \ No newline at end of file + + + + + + +