SystemUI: monet: Allow a more granular control over shades [2/2]

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 <phandinhhungvp2001@gmail.com>
This commit is contained in:
Ido Ben-Hur
2022-12-05 14:05:45 +02:00
committed by Hưng Phan
parent 70da7a554e
commit c811849ab5
3 changed files with 174 additions and 41 deletions

View File

@@ -888,5 +888,11 @@
<string name="color_source_preset">Preset</string>
<string name="accent_color_title">Accent Color</string>
<string name="accent_color_summary">Override default accent color</string>
<string name="luminance_factor_title">Luminance</string>
<string name="luminance_factor_summary">Higher values produce brighter colors</string>
<string name="chroma_factor_title">Chroma</string>
<string name="chroma_factor_summary">Higher values produce stronger colors</string>
<string name="tint_background_title">Tint Background</string>
<string name="tint_background_summary">Make Luminance and Chroma selection affect background colors as well</string>
</resources>

View File

@@ -37,4 +37,32 @@
android:title="@string/accent_color_title"
android:summary="@string/accent_color_summary" />
<com.cherish.settings.preferences.CustomSeekBarPreference
android:key="luminance_factor"
android:title="@string/luminance_factor_title"
android:summary="@string/luminance_factor_summary"
android:max="100"
settings:min="-95"
settings:interval="5"
settings:defaultValue="0"
settings:units="%"
settings:showSign="true" />
<com.cherish.settings.preferences.CustomSeekBarPreference
android:key="chroma_factor"
android:title="@string/chroma_factor_title"
android:summary="@string/chroma_factor_summary"
android:max="100"
settings:min="-95"
settings:interval="5"
settings:defaultValue="0"
settings:units="%"
settings:showSign="true" />
<SwitchPreference
android:key="tint_background"
android:title="@string/tint_background_title"
android:summary="@string/tint_background_summary"
android:defaultValue="false" />
</PreferenceScreen>

View File

@@ -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,18 +166,30 @@ 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) {}
}