From 68f64614c7922dfb79e6ce2ae5d9c8a9b1d90383 Mon Sep 17 00:00:00 2001 From: Nishith Khanna Date: Fri, 6 May 2022 15:44:45 +0530 Subject: Add support for auto adapt text color in light mode for widget --- .../e/privacycentralapp/widget/Widget.kt | 18 +++++ .../e/privacycentralapp/widget/WidgetUI.kt | 77 ++++++++++++++++++++-- .../res/drawable/ic_chevron_right_24dp_light.xml | 28 ++++++++ app/src/main/res/drawable/ic_settings_light.xml | 27 ++++++++ app/src/main/res/drawable/ic_shield_off_light.xml | 35 ++++++++++ app/src/main/res/drawable/ic_shield_on_light.xml | 34 ++++++++++ app/src/main/res/layout/widget.xml | 4 ++ app/src/main/res/values/colors.xml | 3 + 8 files changed, 221 insertions(+), 5 deletions(-) create mode 100644 app/src/main/res/drawable/ic_chevron_right_24dp_light.xml create mode 100644 app/src/main/res/drawable/ic_settings_light.xml create mode 100644 app/src/main/res/drawable/ic_shield_off_light.xml create mode 100644 app/src/main/res/drawable/ic_shield_on_light.xml (limited to 'app/src') diff --git a/app/src/main/java/foundation/e/privacycentralapp/widget/Widget.kt b/app/src/main/java/foundation/e/privacycentralapp/widget/Widget.kt index 6fd9470..048b58c 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/widget/Widget.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/widget/Widget.kt @@ -20,6 +20,7 @@ package foundation.e.privacycentralapp import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider import android.content.Context +import android.os.Bundle import foundation.e.privacycentralapp.domain.usecases.GetQuickPrivacyStateUseCase import foundation.e.privacycentralapp.domain.usecases.TrackersStatisticsUseCase import foundation.e.privacycentralapp.widget.State @@ -71,6 +72,9 @@ class Widget : AppWidgetProvider() { private var state: StateFlow = MutableStateFlow(State()) + private const val DARK_TEXT_KEY = "foundation.e.blisslauncher.WIDGET_OPTION_DARK_TEXT" + var isDarkText = false + private fun initState( getPrivacyStateUseCase: GetQuickPrivacyStateUseCase, trackersStatisticsUseCase: TrackersStatisticsUseCase, @@ -135,4 +139,18 @@ class Widget : AppWidgetProvider() { } } } + + @FlowPreview + override fun onAppWidgetOptionsChanged( + context: Context, + appWidgetManager: AppWidgetManager, + appWidgetId: Int, + newOptions: Bundle? + ) { + super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions) + if (newOptions != null) { + isDarkText = newOptions.getBoolean(DARK_TEXT_KEY) + } + render(context, state.value, appWidgetManager) + } } diff --git a/app/src/main/java/foundation/e/privacycentralapp/widget/WidgetUI.kt b/app/src/main/java/foundation/e/privacycentralapp/widget/WidgetUI.kt index ca93617..f95083e 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/widget/WidgetUI.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/widget/WidgetUI.kt @@ -27,6 +27,7 @@ import android.view.View import android.widget.RemoteViews import foundation.e.privacycentralapp.R import foundation.e.privacycentralapp.Widget +import foundation.e.privacycentralapp.Widget.Companion.isDarkText import foundation.e.privacycentralapp.domain.entities.QuickPrivacyState import foundation.e.privacycentralapp.extensions.dpToPxF import foundation.e.privacycentralapp.main.MainActivity @@ -49,6 +50,7 @@ fun render( appWidgetManager: AppWidgetManager, ) { val views = RemoteViews(context.packageName, R.layout.widget) + applyDarkText(context, state, views) views.apply { val openPIntent = PendingIntent.getActivity( context, @@ -59,11 +61,6 @@ fun render( setOnClickPendingIntent(R.id.settings_btn, openPIntent) setOnClickPendingIntent(R.id.widget_container, openPIntent) - setImageViewResource( - R.id.state_icon, - if (state.quickPrivacyState.isEnabled()) R.drawable.ic_shield_on_white - else R.drawable.ic_shield_off_white - ) setTextViewText( R.id.state_label, context.getString( @@ -268,3 +265,73 @@ private const val REQUEST_CODE_DASHBOARD = 1 private const val REQUEST_CODE_TOGGLE = 2 private const val REQUEST_CODE_TRACKERS = 3 private const val REQUEST_CODE_HIGHLIGHT = 100 + +@FlowPreview +fun applyDarkText(context: Context, state: State, views: RemoteViews) { + views.apply { + listOf( + R.id.state_label, + R.id.graph_legend_blocked, + R.id.graph_legend_allowed, + + ) + .forEach { + setTextColor( + it, + context.getColor(if (isDarkText) R.color.on_surface_disabled_light else R.color.on_primary_medium_emphasis) + ) + } + setTextColor( + R.id.widget_title, + context.getColor(if (isDarkText) R.color.on_surface_medium_emphasis_light else R.color.on_surface_high_emphasis) + ) + listOf( + R.id.state_trackers, + R.id.state_geolocation, + R.id.state_ip_address, + R.id.graph_legend, + R.id.graph_view_trackers_btn + ) + .forEach { + setTextColor( + it, + context.getColor(if (isDarkText) R.color.on_surface_medium_emphasis_light else R.color.on_primary_high_emphasis) + ) + } + + listOf( + R.id.trackers_label, + R.id.geolocation_label, + R.id.ip_address_label, + R.id.graph_empty + + ) + .forEach { + setTextColor( + it, + context.getColor(if (isDarkText) R.color.on_surface_disabled_light else R.color.on_primary_disabled) + ) + } + setTextViewCompoundDrawables( + R.id.graph_view_trackers_btn, + 0, + 0, + if (isDarkText) R.drawable.ic_chevron_right_24dp_light else R.drawable.ic_chevron_right_24dp, + 0 + ) + setImageViewResource( + R.id.settings_btn, + if (isDarkText) R.drawable.ic_settings_light else R.drawable.ic_settings + ) + setImageViewResource( + R.id.state_icon, + if (isDarkText) { + if (state.quickPrivacyState.isEnabled()) R.drawable.ic_shield_on_light + else R.drawable.ic_shield_off_light + } else { + if (state.quickPrivacyState.isEnabled()) R.drawable.ic_shield_on_white + else R.drawable.ic_shield_off_white + } + ) + } +} diff --git a/app/src/main/res/drawable/ic_chevron_right_24dp_light.xml b/app/src/main/res/drawable/ic_chevron_right_24dp_light.xml new file mode 100644 index 0000000..acbf2f2 --- /dev/null +++ b/app/src/main/res/drawable/ic_chevron_right_24dp_light.xml @@ -0,0 +1,28 @@ + + + + + + diff --git a/app/src/main/res/drawable/ic_settings_light.xml b/app/src/main/res/drawable/ic_settings_light.xml new file mode 100644 index 0000000..4eca968 --- /dev/null +++ b/app/src/main/res/drawable/ic_settings_light.xml @@ -0,0 +1,27 @@ + + + + + + diff --git a/app/src/main/res/drawable/ic_shield_off_light.xml b/app/src/main/res/drawable/ic_shield_off_light.xml new file mode 100644 index 0000000..c562835 --- /dev/null +++ b/app/src/main/res/drawable/ic_shield_off_light.xml @@ -0,0 +1,35 @@ + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_shield_on_light.xml b/app/src/main/res/drawable/ic_shield_on_light.xml new file mode 100644 index 0000000..ef34c06 --- /dev/null +++ b/app/src/main/res/drawable/ic_shield_on_light.xml @@ -0,0 +1,34 @@ + + + + + + + + + + diff --git a/app/src/main/res/layout/widget.xml b/app/src/main/res/layout/widget.xml index 999a888..d2d8490 100644 --- a/app/src/main/res/layout/widget.xml +++ b/app/src/main/res/layout/widget.xml @@ -29,6 +29,7 @@ android:orientation="horizontal" android:gravity="center_vertical"> #4D666666 #2CCF69 + + #99000000 + #61000000 \ No newline at end of file -- cgit v1.2.1