From eff83ec9a76cd5995b40dba6b19f6274a4ab2942 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 29 Oct 2022 19:33:29 +0200 Subject: [PATCH] Enabled widget background opacity for Android 12+ --- .../eu/faircode/email/ActivityWidget.java | 17 ++++++++++--- .../eu/faircode/email/ActivityWidgetSync.java | 24 ++++++++++++++++--- .../faircode/email/ActivityWidgetUnified.java | 24 ++++++++++++++++--- .../main/java/eu/faircode/email/Widget.java | 8 +++++-- .../java/eu/faircode/email/WidgetSync.java | 8 +++++-- .../java/eu/faircode/email/WidgetUnified.java | 8 +++++-- 6 files changed, 74 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityWidget.java b/app/src/main/java/eu/faircode/email/ActivityWidget.java index 2838a5a889..6b32ca6d69 100644 --- a/app/src/main/java/eu/faircode/email/ActivityWidget.java +++ b/app/src/main/java/eu/faircode/email/ActivityWidget.java @@ -81,8 +81,12 @@ public class ActivityWidget extends ActivityBase { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); long account = prefs.getLong("widget." + appWidgetId + ".account", -1L); boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false); - boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); - int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); + boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S); + int background = prefs.getInt("widget." + appWidgetId + ".background", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S + ? Color.TRANSPARENT + : ColorUtils.setAlphaComponent(Color.BLACK, 127)); int layout = prefs.getInt("widget." + appWidgetId + ".layout", 1 /* new */); daynight = daynight && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S); @@ -117,6 +121,8 @@ public class ActivityWidget extends ActivityBase { cbSemiTransparent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + btnColor.setColor(Color.TRANSPARENT); setBackground(); } }); @@ -124,6 +130,7 @@ public class ActivityWidget extends ActivityBase { btnColor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + int color = btnColor.getColor(); int editTextColor = Helper.resolveColor(ActivityWidget.this, android.R.attr.editTextColor); ColorPickerDialogBuilder @@ -133,10 +140,14 @@ public class ActivityWidget extends ActivityBase { .setColorEditTextColor(editTextColor) .wheelType(ColorPickerView.WHEEL_TYPE.FLOWER) .density(6) - .lightnessSliderOnly() + .initialColor(color == Color.TRANSPARENT ? Color.WHITE : color) + .showLightnessSlider(true) + .showAlphaSlider(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) .setPositiveButton(android.R.string.ok, new ColorPickerClickListener() { @Override public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + cbSemiTransparent.setChecked(false); btnColor.setColor(selectedColor); setBackground(); } diff --git a/app/src/main/java/eu/faircode/email/ActivityWidgetSync.java b/app/src/main/java/eu/faircode/email/ActivityWidgetSync.java index e81a1e25e0..e433df4abb 100644 --- a/app/src/main/java/eu/faircode/email/ActivityWidgetSync.java +++ b/app/src/main/java/eu/faircode/email/ActivityWidgetSync.java @@ -31,6 +31,7 @@ import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; +import androidx.core.graphics.ColorUtils; import androidx.preference.PreferenceManager; import com.flask.colorpicker.ColorPickerView; @@ -60,8 +61,12 @@ public class ActivityWidgetSync extends ActivityBase { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false); - boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); - int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); + boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S); + int background = prefs.getInt("widget." + appWidgetId + ".background", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S + ? Color.TRANSPARENT + : ColorUtils.setAlphaComponent(Color.BLACK, 127)); daynight = daynight && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S); @@ -85,9 +90,18 @@ public class ActivityWidgetSync extends ActivityBase { } }); + cbSemiTransparent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + btnColor.setColor(Color.TRANSPARENT); + } + }); + btnColor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + int color = btnColor.getColor(); int editTextColor = Helper.resolveColor(ActivityWidgetSync.this, android.R.attr.editTextColor); ColorPickerDialogBuilder @@ -97,10 +111,14 @@ public class ActivityWidgetSync extends ActivityBase { .setColorEditTextColor(editTextColor) .wheelType(ColorPickerView.WHEEL_TYPE.FLOWER) .density(6) - .lightnessSliderOnly() + .initialColor(color == Color.TRANSPARENT ? Color.WHITE : color) + .showLightnessSlider(true) + .showAlphaSlider(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) .setPositiveButton(android.R.string.ok, new ColorPickerClickListener() { @Override public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + cbSemiTransparent.setChecked(false); btnColor.setColor(selectedColor); } }) diff --git a/app/src/main/java/eu/faircode/email/ActivityWidgetUnified.java b/app/src/main/java/eu/faircode/email/ActivityWidgetUnified.java index 452584bfe1..de58e5f4da 100644 --- a/app/src/main/java/eu/faircode/email/ActivityWidgetUnified.java +++ b/app/src/main/java/eu/faircode/email/ActivityWidgetUnified.java @@ -40,6 +40,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.constraintlayout.widget.Group; +import androidx.core.graphics.ColorUtils; import androidx.preference.PreferenceManager; import com.flask.colorpicker.ColorPickerView; @@ -99,8 +100,12 @@ public class ActivityWidgetUnified extends ActivityBase { boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false); boolean highlight = prefs.getBoolean("widget." + appWidgetId + ".highlight", false); int highlight_color = prefs.getInt("widget." + appWidgetId + ".highlight_color", Color.TRANSPARENT); - boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); - int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); + boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S); + int background = prefs.getInt("widget." + appWidgetId + ".background", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S + ? Color.TRANSPARENT + : ColorUtils.setAlphaComponent(Color.BLACK, 127)); boolean separators = prefs.getBoolean("widget." + appWidgetId + ".separators", true); int font = prefs.getInt("widget." + appWidgetId + ".font", 0); int padding = prefs.getInt("widget." + appWidgetId + ".padding", 0); @@ -146,6 +151,14 @@ public class ActivityWidgetUnified extends ActivityBase { } }); + cbSemiTransparent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + btnColor.setColor(Color.TRANSPARENT); + } + }); + cbHighlight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -187,6 +200,7 @@ public class ActivityWidgetUnified extends ActivityBase { btnColor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + int color = btnColor.getColor(); int editTextColor = Helper.resolveColor(ActivityWidgetUnified.this, android.R.attr.editTextColor); ColorPickerDialogBuilder @@ -196,10 +210,14 @@ public class ActivityWidgetUnified extends ActivityBase { .setColorEditTextColor(editTextColor) .wheelType(ColorPickerView.WHEEL_TYPE.FLOWER) .density(6) - .lightnessSliderOnly() + .initialColor(color == Color.TRANSPARENT ? Color.WHITE : color) + .showLightnessSlider(true) + .showAlphaSlider(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) .setPositiveButton(android.R.string.ok, new ColorPickerClickListener() { @Override public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + cbSemiTransparent.setChecked(false); btnColor.setColor(selectedColor); } }) diff --git a/app/src/main/java/eu/faircode/email/Widget.java b/app/src/main/java/eu/faircode/email/Widget.java index f51b4ac4fe..ad70ab9236 100644 --- a/app/src/main/java/eu/faircode/email/Widget.java +++ b/app/src/main/java/eu/faircode/email/Widget.java @@ -60,8 +60,12 @@ public class Widget extends AppWidgetProvider { String name = prefs.getString("widget." + appWidgetId + ".name", null); long account = prefs.getLong("widget." + appWidgetId + ".account", -1L); boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false); - boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); - int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); + boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S); + int background = prefs.getInt("widget." + appWidgetId + ".background", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S + ? Color.TRANSPARENT + : ColorUtils.setAlphaComponent(Color.BLACK, 127)); int layout = prefs.getInt("widget." + appWidgetId + ".layout", 0); int version = prefs.getInt("widget." + appWidgetId + ".version", 0); diff --git a/app/src/main/java/eu/faircode/email/WidgetSync.java b/app/src/main/java/eu/faircode/email/WidgetSync.java index ed747862da..084d6f0631 100644 --- a/app/src/main/java/eu/faircode/email/WidgetSync.java +++ b/app/src/main/java/eu/faircode/email/WidgetSync.java @@ -51,8 +51,12 @@ public class WidgetSync extends AppWidgetProvider { for (int appWidgetId : appWidgetIds) { boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false); - boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); - int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); + boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S); + int background = prefs.getInt("widget." + appWidgetId + ".background", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S + ? Color.TRANSPARENT + : ColorUtils.setAlphaComponent(Color.BLACK, 127)); int version = prefs.getInt("widget." + appWidgetId + ".version", 0); if (version <= 1550) diff --git a/app/src/main/java/eu/faircode/email/WidgetUnified.java b/app/src/main/java/eu/faircode/email/WidgetUnified.java index 98803adeb5..2de34d7b3b 100644 --- a/app/src/main/java/eu/faircode/email/WidgetUnified.java +++ b/app/src/main/java/eu/faircode/email/WidgetUnified.java @@ -51,8 +51,12 @@ public class WidgetUnified extends AppWidgetProvider { long folder = prefs.getLong("widget." + appWidgetId + ".folder", -1L); String type = prefs.getString("widget." + appWidgetId + ".type", null); boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false); - boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); - int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); + boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S); + int background = prefs.getInt("widget." + appWidgetId + ".background", + Build.VERSION.SDK_INT < Build.VERSION_CODES.S + ? Color.TRANSPARENT + : ColorUtils.setAlphaComponent(Color.BLACK, 127)); boolean separators = prefs.getBoolean("widget." + appWidgetId + ".separators", true); int font = prefs.getInt("widget." + appWidgetId + ".font", 0); int padding = prefs.getInt("widget." + appWidgetId + ".padding", 0);