From f1f9d59ce0b54a5742771fb1311029390cdd832d Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 8 Jul 2020 18:38:05 +0200 Subject: [PATCH] Auto adjust widget text color --- .../main/java/eu/faircode/email/ActivityWidget.java | 12 ++++++++++++ app/src/main/java/eu/faircode/email/Widget.java | 10 ++++++++++ .../main/java/eu/faircode/email/WidgetUnified.java | 5 +++++ .../email/WidgetUnifiedRemoteViewsFactory.java | 12 ++++++++++++ app/src/main/res/layout/widget_unified.xml | 3 ++- 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityWidget.java b/app/src/main/java/eu/faircode/email/ActivityWidget.java index 791039389a..12ee31c2df 100644 --- a/app/src/main/java/eu/faircode/email/ActivityWidget.java +++ b/app/src/main/java/eu/faircode/email/ActivityWidget.java @@ -31,11 +31,13 @@ import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; +import android.widget.ImageView; import android.widget.RadioButton; import android.widget.Spinner; import android.widget.TextView; import androidx.constraintlayout.widget.Group; +import androidx.core.graphics.ColorUtils; import androidx.preference.PreferenceManager; import com.flask.colorpicker.ColorPickerView; @@ -230,6 +232,16 @@ public class ActivityWidget extends ActivityBase { } else { inOld.setBackgroundColor(background); inNew.setBackgroundColor(background); + float lum = (float) ColorUtils.calculateLuminance(background); + int color = (lum > 0.7 ? Color.BLACK : getResources().getColor(R.color.colorWidgetForeground)); + + ((ImageView) inOld.findViewById(R.id.ivMessage)).setColorFilter(color); + ((TextView) inOld.findViewById(R.id.tvCount)).setTextColor(color); + ((TextView) inOld.findViewById(R.id.tvAccount)).setTextColor(color); + + ((ImageView) inNew.findViewById(R.id.ivMessage)).setColorFilter(color); + ((TextView) inNew.findViewById(R.id.tvCount)).setTextColor(color); + ((TextView) inNew.findViewById(R.id.tvAccount)).setTextColor(color); } } } diff --git a/app/src/main/java/eu/faircode/email/Widget.java b/app/src/main/java/eu/faircode/email/Widget.java index fbf7fdb517..e5344c287d 100644 --- a/app/src/main/java/eu/faircode/email/Widget.java +++ b/app/src/main/java/eu/faircode/email/Widget.java @@ -31,6 +31,7 @@ import android.text.TextUtils; import android.view.View; import android.widget.RemoteViews; +import androidx.core.graphics.ColorUtils; import androidx.preference.PreferenceManager; import java.text.NumberFormat; @@ -118,6 +119,15 @@ public class Widget extends AppWidgetProvider { views.setViewVisibility(R.id.tvAccount, ViewStripe.VISIBLE); } + if (!semi && background != Color.TRANSPARENT) { + float lum = (float) ColorUtils.calculateLuminance(background); + if (lum > 0.7f) { + views.setInt(R.id.ivMessage, "setColorFilter", Color.BLACK); + views.setTextColor(R.id.tvCount, Color.BLACK); + views.setTextColor(R.id.tvAccount, Color.BLACK); + } + } + appWidgetManager.updateAppWidget(appWidgetId, views); } } diff --git a/app/src/main/java/eu/faircode/email/WidgetUnified.java b/app/src/main/java/eu/faircode/email/WidgetUnified.java index 8e0a9cb18c..c86938c7d6 100644 --- a/app/src/main/java/eu/faircode/email/WidgetUnified.java +++ b/app/src/main/java/eu/faircode/email/WidgetUnified.java @@ -31,6 +31,7 @@ import android.net.Uri; import android.util.TypedValue; import android.widget.RemoteViews; +import androidx.core.graphics.ColorUtils; import androidx.preference.PreferenceManager; public class WidgetUnified extends AppWidgetProvider { @@ -69,6 +70,10 @@ public class WidgetUnified extends AppWidgetProvider { views.setViewPadding(R.id.title, px, px, px, px); } + float lum = (float) ColorUtils.calculateLuminance(background); + if (lum > 0.7f) + views.setTextColor(R.id.title, Color.BLACK); + if (name == null) views.setTextViewText(R.id.title, context.getString(R.string.title_folder_unified)); else diff --git a/app/src/main/java/eu/faircode/email/WidgetUnifiedRemoteViewsFactory.java b/app/src/main/java/eu/faircode/email/WidgetUnifiedRemoteViewsFactory.java index 9849ecb478..c990c92f81 100644 --- a/app/src/main/java/eu/faircode/email/WidgetUnifiedRemoteViewsFactory.java +++ b/app/src/main/java/eu/faircode/email/WidgetUnifiedRemoteViewsFactory.java @@ -23,6 +23,7 @@ import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Color; import android.graphics.Typeface; import android.text.SpannableString; import android.text.Spanned; @@ -34,6 +35,7 @@ import android.widget.RemoteViews; import android.widget.RemoteViewsService; import androidx.core.content.ContextCompat; +import androidx.core.graphics.ColorUtils; import androidx.preference.PreferenceManager; import java.util.ArrayList; @@ -51,6 +53,8 @@ public class WidgetUnifiedRemoteViewsFactory implements RemoteViewsService.Remot private long account; private boolean unseen; private boolean flagged; + private boolean semi; + private int background; private int font; private int padding; private int colorWidgetForeground; @@ -85,12 +89,20 @@ public class WidgetUnifiedRemoteViewsFactory implements RemoteViewsService.Remot folder = prefs.getLong("widget." + appWidgetId + ".folder", -1L); unseen = prefs.getBoolean("widget." + appWidgetId + ".unseen", false); flagged = prefs.getBoolean("widget." + appWidgetId + ".flagged", false); + semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); + background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); font = prefs.getInt("widget." + appWidgetId + ".font", 0); padding = prefs.getInt("widget." + appWidgetId + ".padding", 0); colorWidgetForeground = ContextCompat.getColor(context, R.color.colorWidgetForeground); colorWidgetRead = ContextCompat.getColor(context, R.color.colorWidgetRead); colorSeparator = ContextCompat.getColor(context, R.color.lightColorSeparator); + float lum = (float) ColorUtils.calculateLuminance(background); + if (lum > 0.7f) { + colorWidgetForeground = ColorUtils.blendARGB(colorWidgetForeground, Color.BLACK, 1.0f); + colorWidgetRead = ColorUtils.blendARGB(colorWidgetRead, Color.BLACK, 1.0f); + } + pro = ActivityBilling.isPro(context); DB db = DB.getInstance(context); diff --git a/app/src/main/res/layout/widget_unified.xml b/app/src/main/res/layout/widget_unified.xml index c64b028f66..e6950e0b0b 100644 --- a/app/src/main/res/layout/widget_unified.xml +++ b/app/src/main/res/layout/widget_unified.xml @@ -21,9 +21,10 @@ android:textStyle="bold" /> + android:background="@color/colorWidgetRead" />