From c811849ab5793f03350030bda5cdf1614c530a73 Mon Sep 17 00:00:00 2001 From: Ido Ben-Hur Date: Mon, 5 Dec 2022 14:05:45 +0200 Subject: [PATCH] SystemUI: monet: Allow a more granular control over shades [2/2] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do that by letting the user control the luminance and chroma factors of accent / background colors Use Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES as an existing medium to store choices Also refactor the code here Signed-off-by: Hưng Phan --- res/values/cherish_strings.xml | 6 + res/xml/monet_settings.xml | 28 +++ .../settings/fragments/MonetSettings.java | 181 ++++++++++++++---- 3 files changed, 174 insertions(+), 41 deletions(-) diff --git a/res/values/cherish_strings.xml b/res/values/cherish_strings.xml index 60c16ea..67cfa49 100644 --- a/res/values/cherish_strings.xml +++ b/res/values/cherish_strings.xml @@ -888,5 +888,11 @@ Preset Accent Color Override default accent color + Luminance + Higher values produce brighter colors + Chroma + Higher values produce stronger colors + Tint Background + Make Luminance and Chroma selection affect background colors as well diff --git a/res/xml/monet_settings.xml b/res/xml/monet_settings.xml index 874fe58..5d0c260 100644 --- a/res/xml/monet_settings.xml +++ b/res/xml/monet_settings.xml @@ -37,4 +37,32 @@ android:title="@string/accent_color_title" android:summary="@string/accent_color_summary" /> + + + + + + diff --git a/src/com/cherish/settings/fragments/MonetSettings.java b/src/com/cherish/settings/fragments/MonetSettings.java index 6ac609d..f50a49e 100644 --- a/src/com/cherish/settings/fragments/MonetSettings.java +++ b/src/com/cherish/settings/fragments/MonetSettings.java @@ -24,6 +24,7 @@ import androidx.preference.ListPreference; import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import androidx.preference.Preference.OnPreferenceChangeListener; +import androidx.preference.SwitchPreference; import com.android.internal.logging.nano.MetricsProto; import com.android.settings.SettingsPreferenceFragment; @@ -32,6 +33,7 @@ import com.android.settings.R; import com.android.settingslib.search.SearchIndexable; import net.margaritov.preference.colorpicker.ColorPickerPreference; +import com.cherish.settings.preferences.CustomSeekBarPreference; import java.lang.CharSequence; @@ -52,6 +54,12 @@ public class MonetSettings extends SettingsPreferenceFragment implements "android.theme.customization.color_source"; private static final String OVERLAY_COLOR_BOTH = "android.theme.customization.color_both"; + private static final String OVERLAY_LUMINANCE_FACTOR = + "android.theme.customization.luminance_factor"; + private static final String OVERLAY_CHROMA_FACTOR = + "android.theme.customization.chroma_factor"; + private static final String OVERLAY_TINT_BACKGROUND = + "android.theme.customization.tint_background"; private static final String COLOR_SOURCE_PRESET = "preset"; private static final String COLOR_SOURCE_HOME = "home_wallpaper"; private static final String COLOR_SOURCE_LOCK = "lock_wallpaper"; @@ -59,10 +67,16 @@ public class MonetSettings extends SettingsPreferenceFragment implements private static final String PREF_THEME_STYLE = "theme_style"; private static final String PREF_COLOR_SOURCE = "color_source"; private static final String PREF_ACCENT_COLOR = "accent_color"; + private static final String PREF_LUMINANCE_FACTOR = "luminance_factor"; + private static final String PREF_CHROMA_FACTOR = "chroma_factor"; + private static final String PREF_TINT_BACKGROUND = "tint_background"; private ListPreference mThemeStylePref; private ListPreference mColorSourcePref; private ColorPickerPreference mAccentColorPref; + private CustomSeekBarPreference mLuminancePref; + private CustomSeekBarPreference mChromaPref; + private SwitchPreference mTintBackgroundPref; @Override public void onCreate(Bundle icicle) { @@ -72,7 +86,27 @@ public class MonetSettings extends SettingsPreferenceFragment implements mThemeStylePref = findPreference(PREF_THEME_STYLE); mColorSourcePref = findPreference(PREF_COLOR_SOURCE); mAccentColorPref = findPreference(PREF_ACCENT_COLOR); + mLuminancePref = findPreference(PREF_LUMINANCE_FACTOR); + mChromaPref = findPreference(PREF_CHROMA_FACTOR); + mTintBackgroundPref = findPreference(PREF_TINT_BACKGROUND); + updatePreferences(); + + mThemeStylePref.setOnPreferenceChangeListener(this); + mColorSourcePref.setOnPreferenceChangeListener(this); + mAccentColorPref.setOnPreferenceChangeListener(this); + mLuminancePref.setOnPreferenceChangeListener(this); + mChromaPref.setOnPreferenceChangeListener(this); + mTintBackgroundPref.setOnPreferenceChangeListener(this); + } + + @Override + public void onResume() { + super.onResume(); + updatePreferences(); + } + + private void updatePreferences() { final String overlayPackageJson = Settings.Secure.getStringForUser( getActivity().getContentResolver(), Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES, @@ -82,8 +116,11 @@ public class MonetSettings extends SettingsPreferenceFragment implements final JSONObject object = new JSONObject(overlayPackageJson); final String style = object.optString(OVERLAY_CATEGORY_THEME_STYLE, null); final String source = object.optString(OVERLAY_COLOR_SOURCE, null); - final boolean both = object.optInt(OVERLAY_COLOR_BOTH, 0) == 1; final String color = object.optString(OVERLAY_CATEGORY_SYSTEM_PALETTE, null); + final boolean both = object.optInt(OVERLAY_COLOR_BOTH, 0) == 1; + final boolean tintBG = object.optInt(OVERLAY_TINT_BACKGROUND, 0) == 1; + final float lumin = (float) object.optDouble(OVERLAY_LUMINANCE_FACTOR, 1d); + final float chroma = (float) object.optDouble(OVERLAY_CHROMA_FACTOR, 1d); // style handling boolean styleUpdated = false; if (style != null && !style.isEmpty()) { @@ -110,12 +147,18 @@ public class MonetSettings extends SettingsPreferenceFragment implements mAccentColorPref.setNewPreviewColor( ColorPickerPreference.convertToColorInt(color)); } + // etc + int luminV = 0; + if (lumin > 1d) luminV = Math.round((lumin - 1f) * 100f); + else if (lumin < 1d) luminV = -1 * Math.round((1f - lumin) * 100f); + mLuminancePref.setValue(luminV); + int chromaV = 0; + if (chroma > 1d) chromaV = Math.round((chroma - 1f) * 100f); + else if (chroma < 1d) chromaV = -1 * Math.round((1f - chroma) * 100f); + mChromaPref.setValue(chromaV); + mTintBackgroundPref.setChecked(tintBG); } catch (JSONException | IllegalArgumentException ignored) {} } - - mThemeStylePref.setOnPreferenceChangeListener(this); - mColorSourcePref.setOnPreferenceChangeListener(this); - mAccentColorPref.setOnPreferenceChangeListener(this); } @Override @@ -123,20 +166,32 @@ public class MonetSettings extends SettingsPreferenceFragment implements final ContentResolver resolver = getActivity().getContentResolver(); if (preference == mThemeStylePref) { String value = (String) newValue; - setSettingsValues(value, null, null); + setStyleValue(value); updateListByValue(mThemeStylePref, value, false); return true; } else if (preference == mColorSourcePref) { String value = (String) newValue; - setSettingsValues(null, value, null); + setSourceValue(value); updateListByValue(mColorSourcePref, value, false); updateAccentEnablement(value); return true; } else if (preference == mAccentColorPref) { int value = (Integer) newValue; - setSettingsValues(null, null, value); + setColorValue(value); return true; - } + } else if (preference == mLuminancePref) { + int value = (Integer) newValue; + setLuminanceValue(value); + return true; + } else if (preference == mChromaPref) { + int value = (Integer) newValue; + setChromaValue(value); + return true; + } else if (preference == mTintBackgroundPref) { + boolean value = (Boolean) newValue; + setTintBackgroundValue(value); + return true; + } return false; } @@ -156,45 +211,89 @@ public class MonetSettings extends SettingsPreferenceFragment implements return shouldEnable; } - private void setSettingsValues(String style, String source, Integer color) { - final ContentResolver resolver = getActivity().getContentResolver(); + private JSONObject getSettingsJson() throws JSONException { final String overlayPackageJson = Settings.Secure.getStringForUser( - resolver, Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES, + getActivity().getContentResolver(), + Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES, UserHandle.USER_CURRENT); + JSONObject object; + if (overlayPackageJson == null || overlayPackageJson.isEmpty()) + return new JSONObject(); + return new JSONObject(overlayPackageJson); + } + private void putSettingsJson(JSONObject object) { + Settings.Secure.putStringForUser( + getActivity().getContentResolver(), + Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES, + object.toString(), UserHandle.USER_CURRENT); + } + + private void setStyleValue(String style) { try { - JSONObject object; - if (overlayPackageJson == null || overlayPackageJson.isEmpty()) - object = new JSONObject(); + JSONObject object = getSettingsJson(); + object.putOpt(OVERLAY_CATEGORY_THEME_STYLE, style); + putSettingsJson(object); + } catch (JSONException | IllegalArgumentException ignored) {} + } + + private void setSourceValue(String source) { + try { + JSONObject object = getSettingsJson(); + if (source.equals("both")) { + object.putOpt(OVERLAY_COLOR_BOTH, 1); + object.putOpt(OVERLAY_COLOR_SOURCE, COLOR_SOURCE_HOME); + } else { + object.remove(OVERLAY_COLOR_BOTH); + object.putOpt(OVERLAY_COLOR_SOURCE, source); + } + if (!source.equals(COLOR_SOURCE_PRESET)) { + object.remove(OVERLAY_CATEGORY_ACCENT_COLOR); + object.remove(OVERLAY_CATEGORY_SYSTEM_PALETTE); + } + putSettingsJson(object); + } catch (JSONException | IllegalArgumentException ignored) {} + } + + private void setColorValue(int color) { + try { + JSONObject object = getSettingsJson(); + final String rgbColor = ColorPickerPreference.convertToRGB(color).replace("#", ""); + object.putOpt(OVERLAY_CATEGORY_ACCENT_COLOR, rgbColor); + object.putOpt(OVERLAY_CATEGORY_SYSTEM_PALETTE, rgbColor); + object.putOpt(OVERLAY_COLOR_SOURCE, COLOR_SOURCE_PRESET); + putSettingsJson(object); + } catch (JSONException | IllegalArgumentException ignored) {} + } + + private void setLuminanceValue(int lumin) { + try { + JSONObject object = getSettingsJson(); + if (lumin == 0) + object.remove(OVERLAY_LUMINANCE_FACTOR); else - object = new JSONObject(overlayPackageJson); + object.putOpt(OVERLAY_LUMINANCE_FACTOR, 1d + ((double) lumin / 100d)); + putSettingsJson(object); + } catch (JSONException | IllegalArgumentException ignored) {} + } - if (style != null) { - object.putOpt(OVERLAY_CATEGORY_THEME_STYLE, style); - } - if (source != null) { - if (source.equals("both")) { - object.putOpt(OVERLAY_COLOR_BOTH, 1); - object.putOpt(OVERLAY_COLOR_SOURCE, COLOR_SOURCE_HOME); - } else { - object.remove(OVERLAY_COLOR_BOTH); - object.putOpt(OVERLAY_COLOR_SOURCE, source); - } - if (!source.equals(COLOR_SOURCE_PRESET)) { - object.remove(OVERLAY_CATEGORY_ACCENT_COLOR); - object.remove(OVERLAY_CATEGORY_SYSTEM_PALETTE); - } - } - if (color != null) { - final String rgbColor = ColorPickerPreference.convertToRGB(color).replace("#", ""); - object.putOpt(OVERLAY_CATEGORY_ACCENT_COLOR, rgbColor); - object.putOpt(OVERLAY_CATEGORY_SYSTEM_PALETTE, rgbColor); - object.putOpt(OVERLAY_COLOR_SOURCE, COLOR_SOURCE_PRESET); - } + private void setChromaValue(int chroma) { + try { + JSONObject object = getSettingsJson(); + if (chroma == 0) + object.remove(OVERLAY_CHROMA_FACTOR); + else + object.putOpt(OVERLAY_CHROMA_FACTOR, 1d + ((double) chroma / 100d)); + putSettingsJson(object); + } catch (JSONException | IllegalArgumentException ignored) {} + } - Settings.Secure.putStringForUser( - resolver, Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES, - object.toString(), UserHandle.USER_CURRENT); + private void setTintBackgroundValue(boolean tint) { + try { + JSONObject object = getSettingsJson(); + if (!tint) object.remove(OVERLAY_TINT_BACKGROUND); + else object.putOpt(OVERLAY_TINT_BACKGROUND, 1); + putSettingsJson(object); } catch (JSONException | IllegalArgumentException ignored) {} }