diff --git a/app/src/main/java/com/leanbitlab/lwidget/AwidgetProvider.kt b/app/src/main/java/com/leanbitlab/lwidget/AwidgetProvider.kt index 8c0678e..1dc598a 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/AwidgetProvider.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/AwidgetProvider.kt @@ -160,30 +160,6 @@ class AwidgetProvider : AppWidgetProvider() { } } - private fun scheduleWork(context: Context) { - val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager - val intent = Intent(context, AwidgetProvider::class.java).apply { - action = ACTION_BATTERY_UPDATE - } - val pendingIntent = PendingIntent.getBroadcast( - context, - 500, - intent, - PendingIntent.FLAG_IMMUTABLE - ) - - val prefs = context.getSharedPreferences("com.leanbitlab.lwidget.PREFS", Context.MODE_PRIVATE) - val intervalMinutes = prefs.getFloat("update_interval", 15f) - val intervalMillis = (intervalMinutes * 60f * 1000f).toLong().coerceAtLeast(60000L) - - alarmManager.setInexactRepeating( - AlarmManager.RTC, - System.currentTimeMillis() + intervalMillis, - intervalMillis, - pendingIntent - ) - } - private fun cancelWork(context: Context) { val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager val intent = Intent(context, AwidgetProvider::class.java).apply { @@ -205,6 +181,34 @@ class AwidgetProvider : AppWidgetProvider() { /** goAsync() broadcasts must finish well inside the ~10s system limit. */ private const val ASYNC_BUDGET_MS = 8_000L + /** + * (Re)schedules the periodic refresh. setInexactRepeating replaces any existing alarm + * for the same PendingIntent, so this is safe to call whenever the interval changes. + */ + fun scheduleWork(context: Context) { + val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager + val intent = Intent(context, AwidgetProvider::class.java).apply { + action = ACTION_BATTERY_UPDATE + } + val pendingIntent = PendingIntent.getBroadcast( + context, + 500, + intent, + PendingIntent.FLAG_IMMUTABLE + ) + + val prefs = context.getSharedPreferences("com.leanbitlab.lwidget.PREFS", Context.MODE_PRIVATE) + val intervalMinutes = prefs.getFloat("update_interval", 15f) + val intervalMillis = (intervalMinutes * 60f * 1000f).toLong().coerceAtLeast(60000L) + + alarmManager.setInexactRepeating( + AlarmManager.RTC, + System.currentTimeMillis() + intervalMillis, + intervalMillis, + pendingIntent + ) + } + const val ACTION_NOOP = "com.leanbitlab.lwidget.ACTION_NOOP" const val ACTION_BATTERY_UPDATE = SystemStatsFetcher.ACTION_BATTERY_UPDATE const val PERMISSION_READ_TASKS_ORG = TasksFetcher.PERMISSION_READ_TASKS_ORG diff --git a/app/src/main/java/com/leanbitlab/lwidget/ColorResolver.kt b/app/src/main/java/com/leanbitlab/lwidget/ColorResolver.kt index f294f0f..7a1eab4 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/ColorResolver.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/ColorResolver.kt @@ -4,8 +4,42 @@ import android.content.Context import android.content.SharedPreferences import android.graphics.Color import android.os.Build +import com.leanbitlab.lwidget.render.WidgetColor object ColorResolver { + /** Picks the light, dark or "follow system" (night-qualified) variant of a color role. */ + fun themed(light: Int, dark: Int, auto: Int, isLight: Boolean, followSystem: Boolean): Int = + if (followSystem) auto else if (isLight) light else dark + + /** + * Like [resolveColor], but also carries a color resource on Android 12+ for theme-dependent + * choices, so the launcher can follow wallpaper and dark-mode changes on its own. + */ + fun resolveWidgetColor( + context: Context, + prefs: SharedPreferences, + useDynamicColors: Boolean, + idx: Int, + isPrimary: Boolean, + isLight: Boolean, + followSystem: Boolean, + sdkInt: Int = Build.VERSION.SDK_INT + ): WidgetColor { + val argb = resolveColor(context, prefs, useDynamicColors, idx, isPrimary, isLight, sdkInt) + if (sdkInt < Build.VERSION_CODES.S) return WidgetColor(argb) + val res = when { + useDynamicColors -> if (isPrimary) { + themed(R.color.widget_dyn_primary_light, R.color.widget_dyn_primary_dark, R.color.widget_dyn_primary_auto, isLight, followSystem) + } else { + themed(R.color.widget_dyn_secondary_light, R.color.widget_dyn_secondary_dark, R.color.widget_dyn_secondary_auto, isLight, followSystem) + } + idx == 0 && followSystem -> if (isPrimary) R.color.widget_text_auto else R.color.widget_text_secondary_auto + idx == 1 -> android.R.color.system_accent1_500 + else -> 0 + } + return WidgetColor(argb, res) + } + fun resolveColor( context: Context, prefs: SharedPreferences, diff --git a/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt b/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt index 4c4470b..d0b1529 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt @@ -143,7 +143,11 @@ class MainActivity : AppCompatActivity() { updateLivePreview() // Advanced Section - bindSlider(R.id.row_update_interval, getString(R.string.row_update_interval), "update_interval", 15f, 1f, 60f, "m") + bindSlider(R.id.row_update_interval, getString(R.string.row_update_interval), "update_interval", 15f, 1f, 60f, "m") { + // The repeating alarm only reads the interval when scheduled, so reschedule it now + val ids = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, AwidgetProvider::class.java)) + if (ids.isNotEmpty()) AwidgetProvider.scheduleWork(this) + } // Setup Changelog val versionName = try { diff --git a/app/src/main/java/com/leanbitlab/lwidget/SettingsBinders.kt b/app/src/main/java/com/leanbitlab/lwidget/SettingsBinders.kt index a224017..532eae1 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/SettingsBinders.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/SettingsBinders.kt @@ -62,7 +62,8 @@ internal fun MainActivity.bindToggle( internal fun MainActivity.bindSlider( viewId: Int, title: String, prefKey: String, defValue: Float, - minValue: Float, maxValue: Float, suffix: String = "%" + minValue: Float, maxValue: Float, suffix: String = "%", + onValueChanged: ((Float) -> Unit)? = null ) { val row = findViewById(viewId) val tvTitle = row.findViewById(R.id.row_label) @@ -81,6 +82,7 @@ internal fun MainActivity.bindSlider( if (fromUser) { tvValue.text = "${value.toInt()}$suffix" prefs.edit().putFloat(prefKey, value).apply() + onValueChanged?.invoke(value) scheduleWidgetUpdate() } } diff --git a/app/src/main/java/com/leanbitlab/lwidget/calendar/CalendarFetcher.kt b/app/src/main/java/com/leanbitlab/lwidget/calendar/CalendarFetcher.kt index eb8ea76..30129df 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/calendar/CalendarFetcher.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/calendar/CalendarFetcher.kt @@ -1,5 +1,6 @@ package com.leanbitlab.lwidget.calendar +import com.leanbitlab.lwidget.render.WidgetColor import android.app.PendingIntent import android.content.Context import android.content.Intent @@ -196,8 +197,8 @@ object CalendarFetcher { views: RemoteViews, events: List, textSizeSp: Float, - primaryColor: Int, - secondaryColor: Int, + primaryColor: WidgetColor, + secondaryColor: WidgetColor, eventViews: List, prefs: SharedPreferences ) { @@ -207,7 +208,7 @@ object CalendarFetcher { if (events.isEmpty()) { views.setTextViewText(eventViews[0], "No events today") - views.setTextColor(eventViews[0], secondaryColor) + secondaryColor.applyTextColor(views, eventViews[0]) views.setTextViewTextSize(eventViews[0], android.util.TypedValue.COMPLEX_UNIT_SP, textSizeSp) views.setViewVisibility(eventViews[0], android.view.View.VISIBLE) @@ -239,7 +240,7 @@ object CalendarFetcher { spannable.setSpan(ForegroundColorSpan(accentColor), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) views.setTextViewText(eventViews[i], spannable) - views.setTextColor(eventViews[i], if (event.isLocal) primaryColor else secondaryColor) + (if (event.isLocal) primaryColor else secondaryColor).applyTextColor(views, eventViews[i]) views.setTextViewTextSize(eventViews[i], android.util.TypedValue.COMPLEX_UNIT_SP, textSizeSp) views.setViewVisibility(eventViews[i], android.view.View.VISIBLE) @@ -259,8 +260,8 @@ object CalendarFetcher { context: Context, views: RemoteViews, textSizeSp: Float, - primaryColor: Int, - secondaryColor: Int, + primaryColor: WidgetColor, + secondaryColor: WidgetColor, prefs: SharedPreferences ) { val eventViews = listOf( @@ -275,7 +276,7 @@ object CalendarFetcher { ) != android.content.pm.PackageManager.PERMISSION_GRANTED) { // Interactive permission prompt in widget views.setTextViewText(eventViews[0], "• Tap to grant calendar permission") - views.setTextColor(eventViews[0], secondaryColor) + secondaryColor.applyTextColor(views, eventViews[0]) views.setTextViewTextSize(eventViews[0], android.util.TypedValue.COMPLEX_UNIT_SP, textSizeSp) views.setViewVisibility(eventViews[0], android.view.View.VISIBLE) diff --git a/app/src/main/java/com/leanbitlab/lwidget/render/WidgetColor.kt b/app/src/main/java/com/leanbitlab/lwidget/render/WidgetColor.kt new file mode 100644 index 0000000..6dd5247 --- /dev/null +++ b/app/src/main/java/com/leanbitlab/lwidget/render/WidgetColor.kt @@ -0,0 +1,35 @@ +package com.leanbitlab.lwidget.render + +import android.content.res.ColorStateList +import android.os.Build +import android.widget.RemoteViews +import androidx.annotation.ColorInt +import androidx.annotation.ColorRes + +/** + * A widget color: the resolved [argb] value, plus an optional color resource [res]. + * + * On Android 12+ a non-zero [res] is handed to the launcher, which resolves it every time it + * draws the widget. Wallpaper (Material You) and dark-mode changes then apply immediately, + * without waiting for the next widget update. Older versions, or [res] == 0, use [argb]. + */ +class WidgetColor(@ColorInt val argb: Int, @ColorRes val res: Int = 0) { + private val useRes get() = res != 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S + + fun applyTextColor(views: RemoteViews, viewId: Int) { + if (useRes) views.setColor(viewId, "setTextColor", res) else views.setTextColor(viewId, argb) + } + + fun applyColorFilter(views: RemoteViews, viewId: Int) { + if (useRes) views.setColor(viewId, "setColorFilter", res) else views.setInt(viewId, "setColorFilter", argb) + } + + fun applyImageTint(views: RemoteViews, viewId: Int) { + when { + useRes -> views.setColorStateList(viewId, "setImageTintList", res) + Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> + views.setColorStateList(viewId, "setImageTintList", ColorStateList.valueOf(argb)) + else -> views.setInt(viewId, "setColorFilter", argb) + } + } +} diff --git a/app/src/main/java/com/leanbitlab/lwidget/render/WidgetRenderer.kt b/app/src/main/java/com/leanbitlab/lwidget/render/WidgetRenderer.kt index 34ff588..d79da33 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/render/WidgetRenderer.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/render/WidgetRenderer.kt @@ -5,12 +5,12 @@ import android.appwidget.AppWidgetManager import android.content.Context import android.content.Intent import android.content.SharedPreferences -import android.content.res.ColorStateList import android.graphics.Color import android.os.Build import android.os.SystemClock import android.view.View import android.widget.RemoteViews +import androidx.core.graphics.drawable.toBitmap import com.leanbitlab.lwidget.AwidgetProvider import com.leanbitlab.lwidget.ColorResolver import com.leanbitlab.lwidget.FallbackPreferences @@ -26,6 +26,9 @@ object WidgetRenderer { private const val TAG = "WidgetRenderer" + /** Bolt height relative to the battery text size (roughly the digit cap height). */ + private const val CHARGING_ICON_SCALE = 0.68f + private data class CacheEntry(val intent: Intent?, val timestamp: Long) private val intentCache = ConcurrentHashMap() private const val CACHE_TTL_MS = 60000L // 60 seconds TTL @@ -125,6 +128,8 @@ object WidgetRenderer { 2 -> false else -> !isSystemInNightMode } + // Follow-system colors use night-qualified resources the launcher resolves at draw time + val followSystem = themeMode != 1 && themeMode != 2 val timeFormatIdx = prefs.getInt("time_format_idx", 0) val dateFormatIdx = prefs.getInt("date_format_idx", 0) @@ -181,6 +186,25 @@ object WidgetRenderer { val layoutId = getLayout(fontStyle) + val isS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S + val dynamic = useDynamicColors && isS + + /** A light/dark color role; "follow system" hands the launcher the night-qualified resource. */ + fun role(light: Int, dark: Int, auto: Int) = WidgetColor( + context.getColor(if (useLightTheme) light else dark), + ColorResolver.themed(light, dark, auto, useLightTheme, followSystem) + ) + fun systemColor(res: Int) = WidgetColor(context.getColor(res), res) + fun prefRgb(prefix: String) = WidgetColor( + Color.rgb(prefs.getInt("${prefix}_r", 255), prefs.getInt("${prefix}_g", 255), prefs.getInt("${prefix}_b", 255)) + ) + + val alarmColor = if (dynamic) { + role(R.color.widget_dyn_alarm_light, R.color.widget_dyn_alarm_dark, R.color.widget_dyn_alarm_auto) + } else { + role(R.color.widget_alarm_light, R.color.widget_alarm_dark, R.color.widget_alarm_auto) + } + // Branching for partial TICK/event updates if (mode == UpdateMode.STEPS_ONLY) { val stepViews = RemoteViews(context.packageName, layoutId) @@ -197,19 +221,18 @@ object WidgetRenderer { return tickViews } else if (mode == UpdateMode.CALENDAR_ONLY) { val calViews = RemoteViews(context.packageName, layoutId) - val primaryColor = ColorResolver.resolveColor(context, prefs, useDynamicColors, textColorPrimaryIdx, isPrimary = true, isLight = useLightTheme) - val secondaryColor = ColorResolver.resolveColor(context, prefs, useDynamicColors, textColorSecondaryIdx, isPrimary = false, isLight = useLightTheme) + val primaryColor = ColorResolver.resolveWidgetColor(context, prefs, useDynamicColors, textColorPrimaryIdx, isPrimary = true, isLight = useLightTheme, followSystem = followSystem) + val secondaryColor = ColorResolver.resolveWidgetColor(context, prefs, useDynamicColors, textColorSecondaryIdx, isPrimary = false, isLight = useLightTheme, followSystem = followSystem) if (showEvents) CalendarFetcher.loadCalendarEvents(context, calViews, sizeEvents, primaryColor, secondaryColor, prefs) return calViews } else if (mode == UpdateMode.TASKS_ONLY) { val taskViews = RemoteViews(context.packageName, layoutId) - val primaryColor = ColorResolver.resolveColor(context, prefs, useDynamicColors, textColorPrimaryIdx, isPrimary = true, isLight = useLightTheme) + val primaryColor = ColorResolver.resolveWidgetColor(context, prefs, useDynamicColors, textColorPrimaryIdx, isPrimary = true, isLight = useLightTheme, followSystem = followSystem) if (showTasks) TasksFetcher.loadTasks(context, taskViews, sizeTasks, primaryColor) return taskViews } else if (mode == UpdateMode.ALARM_ONLY) { val alarmViews = RemoteViews(context.packageName, layoutId) - val secondaryColor = ColorResolver.resolveColor(context, prefs, useDynamicColors, textColorSecondaryIdx, isPrimary = false, isLight = useLightTheme) - if (showNextAlarm) SystemStatsFetcher.loadNextAlarm(context, alarmViews, sizeNextAlarm, secondaryColor, prefs, showDate || showWorldClock) + if (showNextAlarm) SystemStatsFetcher.loadNextAlarm(context, alarmViews, sizeNextAlarm, alarmColor, prefs, showDate || showWorldClock) return alarmViews } @@ -218,96 +241,48 @@ object WidgetRenderer { // Background & Outline views.setImageViewResource(R.id.widget_background, R.drawable.widget_bg_fill) - fun resolveBgColor(idx: Int, isLight: Boolean): Int { - return when (idx) { - 0 -> if (isLight) Color.WHITE else context.getColor(R.color.widget_bg_dark) - 1 -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - context.getColor(android.R.color.system_accent1_500) - } else { - context.getColor(R.color.widget_fallback_cyan) - } - 2 -> { - val r = prefs.getInt("bg_color_r", 255) - val g = prefs.getInt("bg_color_g", 255) - val b = prefs.getInt("bg_color_b", 255) - Color.rgb(r, g, b) - } - else -> if (isLight) Color.WHITE else context.getColor(R.color.widget_bg_dark) - } - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - views.setColorStateList(R.id.widget_background, "setImageTintList", ColorStateList.valueOf(resolveBgColor(bgColorIdx, useLightTheme))) - } else { - views.setInt(R.id.widget_background, "setColorFilter", resolveBgColor(bgColorIdx, useLightTheme)) + val bgColor = when { + dynamic -> role(R.color.widget_dyn_bg_light, R.color.widget_dyn_bg_dark, R.color.widget_dyn_bg_auto) + bgColorIdx == 1 -> if (isS) systemColor(android.R.color.system_accent1_500) else WidgetColor(context.getColor(R.color.widget_fallback_cyan)) + bgColorIdx == 2 -> prefRgb("bg_color") + else -> role(R.color.widget_bg_light, R.color.widget_bg_dark, R.color.widget_bg_auto) } + bgColor.applyImageTint(views, R.id.widget_background) val alpha255 = (bgOpacity * 255 / 100).toInt().coerceIn(0, 255) views.setInt(R.id.widget_background, "setImageAlpha", alpha255) - fun resolveOutlineColor(idx: Int): Int { - return when (idx) { - 0 -> context.getColor(R.color.widget_outline) - 1 -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - context.getColor(android.R.color.system_accent1_500) - } else { - context.getColor(R.color.widget_fallback_cyan) - } - 2 -> { - val r = prefs.getInt("outline_color_r", 255) - val g = prefs.getInt("outline_color_g", 255) - val b = prefs.getInt("outline_color_b", 255) - Color.rgb(r, g, b) - } - else -> context.getColor(R.color.widget_outline) - } - } - val outlineColorIdx = prefs.getInt("outline_color_idx", 0) val showOutline = prefs.getBoolean("show_outline", false) - val outlineColor = resolveOutlineColor(outlineColorIdx) + val outlineColor = when { + dynamic -> role(R.color.widget_dyn_outline_light, R.color.widget_dyn_outline_dark, R.color.widget_dyn_outline_auto) + outlineColorIdx == 1 -> if (isS) systemColor(android.R.color.system_accent1_500) else WidgetColor(context.getColor(R.color.widget_fallback_cyan)) + outlineColorIdx == 2 -> prefRgb("outline_color") + else -> systemColor(R.color.widget_outline) + } views.setImageViewResource(R.id.widget_outline, R.drawable.widget_bg_outline) views.setViewVisibility(R.id.widget_outline, if (showOutline) View.VISIBLE else View.GONE) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - views.setColorStateList(R.id.widget_outline, "setImageTintList", ColorStateList.valueOf(outlineColor)) - } else { - views.setInt(R.id.widget_outline, "setColorFilter", outlineColor) - } + outlineColor.applyImageTint(views, R.id.widget_outline) views.setInt(R.id.widget_outline, "setImageAlpha", 255) - val primaryColor = ColorResolver.resolveColor(context, prefs, useDynamicColors, textColorPrimaryIdx, true, useLightTheme) - val secondaryColor = ColorResolver.resolveColor(context, prefs, useDynamicColors, textColorSecondaryIdx, false, useLightTheme) + val primaryColor = ColorResolver.resolveWidgetColor(context, prefs, useDynamicColors, textColorPrimaryIdx, true, useLightTheme, followSystem) + val secondaryColor = ColorResolver.resolveWidgetColor(context, prefs, useDynamicColors, textColorSecondaryIdx, false, useLightTheme, followSystem) val dateColorIdx = prefs.getInt("date_color_idx", 0) - val dateColor = if (useDynamicColors && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - context.getColor(if (useLightTheme) android.R.color.system_accent1_800 else android.R.color.system_accent2_100) - } else { - when (dateColorIdx) { - 2 -> Color.rgb(prefs.getInt("date_color_r", 255), prefs.getInt("date_color_g", 255), prefs.getInt("date_color_b", 255)) - 1 -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) context.getColor(android.R.color.system_accent2_500) else context.getColor(R.color.widget_fallback_yellow) - else -> if (useLightTheme) context.getColor(R.color.widget_date_light) else context.getColor(R.color.widget_date_dark) - } + val dateColor = when { + dynamic -> role(R.color.widget_dyn_date_light, R.color.widget_dyn_date_dark, R.color.widget_dyn_date_auto) + dateColorIdx == 2 -> prefRgb("date_color") + dateColorIdx == 1 -> if (isS) systemColor(android.R.color.system_accent2_500) else WidgetColor(context.getColor(R.color.widget_fallback_yellow)) + else -> role(R.color.widget_date_light, R.color.widget_date_dark, R.color.widget_date_auto) } - val alarmColor = if (useDynamicColors && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - context.getColor(if (useLightTheme) android.R.color.system_accent3_800 else android.R.color.system_accent3_100) - } else { - if (useLightTheme) context.getColor(R.color.widget_alarm_light) else context.getColor(R.color.widget_alarm_dark) - } - - if (useDynamicColors && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - views.setColorStateList(R.id.widget_background, "setImageTintList", ColorStateList.valueOf(context.getColor(if (useLightTheme) android.R.color.system_neutral2_50 else android.R.color.system_neutral1_800))) - if (showOutline) { - views.setColorStateList(R.id.widget_outline, "setImageTintList", ColorStateList.valueOf(context.getColor(if (useLightTheme) android.R.color.system_accent1_300 else android.R.color.system_accent1_400))) - } - } // Apply Time val timeVisible = showTime || showWorldClock views.setViewVisibility(R.id.time_container, if (timeVisible) View.VISIBLE else View.GONE) views.setViewVisibility(R.id.clock_time, if (showTime) View.VISIBLE else View.GONE) views.setTextViewTextSize(R.id.clock_time, android.util.TypedValue.COMPLEX_UNIT_SP, sizeTime) - views.setTextColor(R.id.clock_time, primaryColor) + primaryColor.applyTextColor(views, R.id.clock_time) val (timeFormat12, timeFormat24) = when (timeFormatIdx) { 0 -> "h:mm" to "H:mm" @@ -329,7 +304,7 @@ object WidgetRenderer { views.setViewVisibility(R.id.date_container, if (dateVisible) View.VISIBLE else View.GONE) views.setViewVisibility(R.id.clock_date, if (showDate) View.VISIBLE else View.GONE) views.setTextViewTextSize(R.id.clock_date, android.util.TypedValue.COMPLEX_UNIT_SP, sizeDate) - views.setTextColor(R.id.clock_date, dateColor) + dateColor.applyTextColor(views, R.id.clock_date) val (dateFormat12, dateFormat24) = when (dateFormatIdx) { 0 -> "EEEE, MMMM dd" to "EEEE, MMMM dd" @@ -341,12 +316,24 @@ object WidgetRenderer { views.setCharSequence(R.id.clock_date, "setFormat24Hour", dateFormat24) // Apply Battery & Temp - views.setViewVisibility(R.id.text_battery, if (showBattery) View.VISIBLE else View.GONE) + views.setViewVisibility(R.id.layout_battery, if (showBattery) View.VISIBLE else View.GONE) views.setTextViewTextSize(R.id.text_battery, android.util.TypedValue.COMPLEX_UNIT_SP, sizeBattery) + if (showBattery) { + // Bitmap sized to the digits so the bolt scales with the battery text size on all API levels + val boltHeightPx = (android.util.TypedValue.applyDimension( + android.util.TypedValue.COMPLEX_UNIT_SP, sizeBattery, context.resources.displayMetrics + ) * CHARGING_ICON_SCALE).toInt().coerceAtLeast(1) + val bolt = androidx.core.content.ContextCompat.getDrawable(context, R.drawable.ic_charging) + if (bolt != null) { + val boltWidthPx = (boltHeightPx * bolt.intrinsicWidth / bolt.intrinsicHeight.toFloat()).toInt().coerceAtLeast(1) + views.setImageViewBitmap(R.id.icon_charging, bolt.toBitmap(boltWidthPx, boltHeightPx)) + secondaryColor.applyColorFilter(views, R.id.icon_charging) + } + } views.setViewVisibility(R.id.text_temp, if (showTemp) View.VISIBLE else View.GONE) views.setTextViewTextSize(R.id.text_temp, android.util.TypedValue.COMPLEX_UNIT_SP, sizeTemp) - views.setTextColor(R.id.text_battery, secondaryColor) - views.setTextColor(R.id.text_temp, secondaryColor) + secondaryColor.applyTextColor(views, R.id.text_battery) + secondaryColor.applyTextColor(views, R.id.text_temp) SystemStatsFetcher.updateBatteryAndTemp(context, views, prefs, showBattery, showTemp) @@ -409,7 +396,7 @@ object WidgetRenderer { } views.setImageViewResource(R.id.icon_weather, weatherDrawableRes) - views.setInt(R.id.icon_weather, "setColorFilter", secondaryColor) + secondaryColor.applyColorFilter(views, R.id.icon_weather) views.setViewVisibility(R.id.icon_weather, View.VISIBLE) if (showWeatherIconOnly && !hasWarning) { @@ -428,7 +415,7 @@ object WidgetRenderer { if (boldWeather) weatherSpan.setSpan(android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, weatherSpan.length, android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) views.setTextViewText(R.id.text_weather_condition, weatherSpan) views.setTextViewTextSize(R.id.text_weather_condition, android.util.TypedValue.COMPLEX_UNIT_SP, sizeWeather) - views.setTextColor(R.id.text_weather_condition, secondaryColor) + secondaryColor.applyTextColor(views, R.id.text_weather_condition) } val launchIntent = getLaunchIntent(context, "org.breezyweather") @@ -443,7 +430,7 @@ object WidgetRenderer { views.setViewVisibility(R.id.text_data_usage, if (showData) View.VISIBLE else View.GONE) if (showData) { views.setTextViewTextSize(R.id.text_data_usage, android.util.TypedValue.COMPLEX_UNIT_SP, sizeData) - views.setTextColor(R.id.text_data_usage, secondaryColor) + secondaryColor.applyTextColor(views, R.id.text_data_usage) SystemStatsFetcher.updateDataUsage(context, views, prefs) } @@ -451,7 +438,7 @@ object WidgetRenderer { views.setViewVisibility(R.id.text_storage, if (showStorage) View.VISIBLE else View.GONE) if (showStorage) { views.setTextViewTextSize(R.id.text_storage, android.util.TypedValue.COMPLEX_UNIT_SP, sizeStorage) - views.setTextColor(R.id.text_storage, secondaryColor) + secondaryColor.applyTextColor(views, R.id.text_storage) SystemStatsFetcher.updateStorageStats(views, prefs) } @@ -459,7 +446,7 @@ object WidgetRenderer { views.setViewVisibility(R.id.text_ram, if (showRam) View.VISIBLE else View.GONE) if (showRam) { views.setTextViewTextSize(R.id.text_ram, android.util.TypedValue.COMPLEX_UNIT_SP, sizeRam) - views.setTextColor(R.id.text_ram, secondaryColor) + secondaryColor.applyTextColor(views, R.id.text_ram) SystemStatsFetcher.updateRamStats(context, views, prefs) } @@ -467,8 +454,8 @@ object WidgetRenderer { views.setViewVisibility(R.id.layout_steps, if (showSteps) View.VISIBLE else View.GONE) if (showSteps) { views.setTextViewTextSize(R.id.text_steps, android.util.TypedValue.COMPLEX_UNIT_SP, sizeSteps) - views.setTextColor(R.id.text_steps, secondaryColor) - views.setInt(R.id.icon_steps, "setColorFilter", secondaryColor) + secondaryColor.applyTextColor(views, R.id.text_steps) + secondaryColor.applyColorFilter(views, R.id.icon_steps) SystemStatsFetcher.loadStepCount(views, prefs) } @@ -476,8 +463,8 @@ object WidgetRenderer { views.setViewVisibility(R.id.layout_screen_time, if (showScreenTime) View.VISIBLE else View.GONE) if (showScreenTime) { views.setTextViewTextSize(R.id.text_screen_time, android.util.TypedValue.COMPLEX_UNIT_SP, sizeScreenTime) - views.setTextColor(R.id.text_screen_time, secondaryColor) - views.setInt(R.id.icon_screen_time, "setColorFilter", secondaryColor) + secondaryColor.applyTextColor(views, R.id.text_screen_time) + secondaryColor.applyColorFilter(views, R.id.icon_screen_time) SystemStatsFetcher.updateScreenTime(context, views, prefs) } @@ -519,7 +506,7 @@ object WidgetRenderer { data class StackEntry(val viewId: Int, val isVisible: Boolean, val size: Float, val key: String) val allRightItems = listOf( - StackEntry(R.id.text_battery, showBattery, sizeBattery, "show_battery"), + StackEntry(R.id.layout_battery, showBattery, sizeBattery, "show_battery"), StackEntry(R.id.text_temp, showTemp, sizeTemp, "show_temp"), StackEntry(R.id.layout_weather_condition, showWeather, sizeWeather, "show_weather_condition"), StackEntry(R.id.text_data_usage, showData, sizeData, "show_data_usage"), @@ -590,7 +577,7 @@ object WidgetRenderer { val batteryIntent = Intent(Intent.ACTION_POWER_USAGE_SUMMARY) val batteryPendingIntent = PendingIntent.getActivity(context, 2, batteryIntent, PendingIntent.FLAG_IMMUTABLE) - views.setOnClickPendingIntent(R.id.text_battery, batteryPendingIntent) + views.setOnClickPendingIntent(R.id.layout_battery, batteryPendingIntent) views.setOnClickPendingIntent(R.id.text_temp, batteryPendingIntent) val storageIntent = Intent(android.provider.Settings.ACTION_INTERNAL_STORAGE_SETTINGS) diff --git a/app/src/main/java/com/leanbitlab/lwidget/stats/SystemStatsFetcher.kt b/app/src/main/java/com/leanbitlab/lwidget/stats/SystemStatsFetcher.kt index 9263e7e..660f818 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/stats/SystemStatsFetcher.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/stats/SystemStatsFetcher.kt @@ -1,5 +1,6 @@ package com.leanbitlab.lwidget.stats +import com.leanbitlab.lwidget.render.WidgetColor import android.app.ActivityManager import android.app.AlarmManager import android.app.AppOpsManager @@ -77,16 +78,9 @@ object SystemStatsFetcher { val isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL if (showBattery) { - val batteryText = if (isCharging) "⚡${batteryPct}%" else "${batteryPct}%" - val batterySpannable = SpannableString(batteryText) - if (isCharging) { - batterySpannable.setSpan( - RelativeSizeSpan(0.8f), - 0, - 1, - Spannable.SPAN_EXCLUSIVE_EXCLUSIVE - ) - } + // Charging bolt is a tinted icon (WidgetRenderer sets its bitmap and color); only toggle it here + views.setViewVisibility(R.id.icon_charging, if (isCharging) View.VISIBLE else View.GONE) + val batterySpannable = SpannableString("${batteryPct}%") batterySpannable.setSpan( RelativeSizeSpan(0.5f), batterySpannable.length - 1, @@ -304,7 +298,7 @@ object SystemStatsFetcher { } } - fun loadWorldClock(views: RemoteViews, textSizeSp: Float, textColor: Int, zoneIdStr: String, is12Hour: Boolean) { + fun loadWorldClock(views: RemoteViews, textSizeSp: Float, textColor: WidgetColor, zoneIdStr: String, is12Hour: Boolean) { try { val zoneId = ZoneId.of(zoneIdStr) views.setString(R.id.text_world_clock, "setTimeZone", zoneId.id) @@ -314,8 +308,8 @@ object SystemStatsFetcher { views.setCharSequence(R.id.text_world_clock, "setFormat24Hour", format24) views.setTextViewTextSize(R.id.text_world_clock, android.util.TypedValue.COMPLEX_UNIT_SP, textSizeSp) - views.setTextColor(R.id.text_world_clock, textColor) - views.setInt(R.id.icon_world_clock, "setColorFilter", textColor) + textColor.applyTextColor(views, R.id.text_world_clock) + textColor.applyColorFilter(views, R.id.icon_world_clock) views.setViewVisibility(R.id.layout_world_clock, View.VISIBLE) } catch (e: Exception) { views.setViewVisibility(R.id.layout_world_clock, View.GONE) @@ -326,7 +320,7 @@ object SystemStatsFetcher { context: Context, views: RemoteViews, textSizeSp: Float, - textColor: Int, + textColor: WidgetColor, prefs: SharedPreferences, hasPrecedingDate: Boolean = true ) { @@ -342,10 +336,10 @@ object SystemStatsFetcher { views.setTextViewText(R.id.text_next_alarm, timeText) views.setTextViewTextSize(R.id.text_next_alarm, android.util.TypedValue.COMPLEX_UNIT_SP, textSizeSp) - views.setTextColor(R.id.text_next_alarm, textColor) + textColor.applyTextColor(views, R.id.text_next_alarm) views.setTextViewTextSize(R.id.text_alarm_divider, android.util.TypedValue.COMPLEX_UNIT_SP, textSizeSp) - views.setTextColor(R.id.text_alarm_divider, textColor) - views.setInt(R.id.icon_next_alarm, "setColorFilter", textColor) + textColor.applyTextColor(views, R.id.text_alarm_divider) + textColor.applyColorFilter(views, R.id.icon_next_alarm) views.setViewVisibility(R.id.text_alarm_divider, if (hasPrecedingDate) View.VISIBLE else View.GONE) views.setViewVisibility(R.id.layout_next_alarm, View.VISIBLE) } else { diff --git a/app/src/main/java/com/leanbitlab/lwidget/tasks/TasksFetcher.kt b/app/src/main/java/com/leanbitlab/lwidget/tasks/TasksFetcher.kt index 0cd0349..f81f64f 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/tasks/TasksFetcher.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/tasks/TasksFetcher.kt @@ -1,5 +1,6 @@ package com.leanbitlab.lwidget.tasks +import com.leanbitlab.lwidget.render.WidgetColor import android.app.PendingIntent import android.content.Context import android.content.Intent @@ -77,7 +78,7 @@ object TasksFetcher { } } - fun loadTasks(context: Context, views: RemoteViews, textSizeSp: Float, primaryColor: Int) { + fun loadTasks(context: Context, views: RemoteViews, textSizeSp: Float, primaryColor: WidgetColor) { val eventViews = listOf( R.id.text_event_1, R.id.text_event_2, R.id.text_event_3, R.id.text_event_4, R.id.text_event_5, R.id.text_event_6, @@ -124,7 +125,7 @@ object TasksFetcher { spannable.setSpan(ForegroundColorSpan(accentColor), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) views.setTextViewText(eventViews[i], spannable) - views.setTextColor(eventViews[i], primaryColor) + primaryColor.applyTextColor(views, eventViews[i]) views.setTextViewTextSize(eventViews[i], android.util.TypedValue.COMPLEX_UNIT_SP, textSizeSp) views.setViewVisibility(eventViews[i], android.view.View.VISIBLE) diff --git a/app/src/main/res/drawable/ic_charging.xml b/app/src/main/res/drawable/ic_charging.xml new file mode 100644 index 0000000..d548e9a --- /dev/null +++ b/app/src/main/res/drawable/ic_charging.xml @@ -0,0 +1,15 @@ + + + + + + + diff --git a/app/src/main/res/layout/widget_layout.xml b/app/src/main/res/layout/widget_layout.xml index 89aafba..763f264 100644 --- a/app/src/main/res/layout/widget_layout.xml +++ b/app/src/main/res/layout/widget_layout.xml @@ -59,20 +59,34 @@ - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + - + android:orientation="horizontal" + android:gravity="center_vertical"> + + + + + + @color/widget_text_dark + @color/widget_text_secondary_dark + @color/widget_date_dark + @color/widget_alarm_dark + @color/widget_bg_dark + @color/widget_dyn_primary_dark + @color/widget_dyn_secondary_dark + @color/widget_dyn_date_dark + @color/widget_dyn_alarm_dark + @color/widget_dyn_bg_dark + @color/widget_dyn_outline_dark + diff --git a/app/src/main/res/values-v31/colors.xml b/app/src/main/res/values-v31/colors.xml index a778e61..d242722 100644 --- a/app/src/main/res/values-v31/colors.xml +++ b/app/src/main/res/values-v31/colors.xml @@ -1,4 +1,18 @@ @android:color/system_accent1_200 + + + @android:color/system_accent1_900 + @android:color/system_accent1_50 + @android:color/system_neutral1_800 + @android:color/system_neutral2_300 + @android:color/system_accent1_800 + @android:color/system_accent2_100 + @android:color/system_accent3_800 + @android:color/system_accent3_100 + @android:color/system_neutral2_50 + @android:color/system_neutral1_800 + @android:color/system_accent1_300 + @android:color/system_accent1_400 diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index d8964aa..8fad1d2 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -14,4 +14,35 @@ #BBAACCDD #00FFFF #FFFF00 + #FFFFFFFF + #CCFFFFFF + #FFFFFFFF + + + @color/widget_text_light + @color/widget_text_dark + @color/widget_text_secondary_light + @color/widget_text_secondary_dark + @color/widget_date_light + @color/widget_date_dark + @color/widget_alarm_light + @color/widget_alarm_dark + @color/widget_bg_light + @color/widget_bg_dark + @color/widget_outline + @color/widget_outline + + + @color/widget_text_light + @color/widget_text_secondary_light + @color/widget_date_light + @color/widget_alarm_light + @color/widget_bg_light + @color/widget_dyn_primary_light + @color/widget_dyn_secondary_light + @color/widget_dyn_date_light + @color/widget_dyn_alarm_light + @color/widget_dyn_bg_light + @color/widget_dyn_outline_light diff --git a/app/src/test/java/com/leanbitlab/lwidget/ColorResolverTest.kt b/app/src/test/java/com/leanbitlab/lwidget/ColorResolverTest.kt index 6f3301b..b40a5df 100644 --- a/app/src/test/java/com/leanbitlab/lwidget/ColorResolverTest.kt +++ b/app/src/test/java/com/leanbitlab/lwidget/ColorResolverTest.kt @@ -355,4 +355,32 @@ class ColorResolverTest { assertEquals(Color.parseColor("#CCFFFFFF"), result) } + + private fun widgetColor(dynamic: Boolean, idx: Int, isPrimary: Boolean, isLight: Boolean, followSystem: Boolean, sdkInt: Int = Build.VERSION_CODES.S) = + ColorResolver.resolveWidgetColor(mock(), mock(), dynamic, idx, isPrimary, isLight, followSystem, sdkInt) + + @Test + fun testResolveWidgetColor_Dynamic_FollowSystem_UsesNightQualifiedResource() { + assertEquals(R.color.widget_dyn_primary_auto, widgetColor(dynamic = true, idx = 0, isPrimary = true, isLight = true, followSystem = true).res) + } + + @Test + fun testResolveWidgetColor_Dynamic_ForcedDark_UsesDarkResource() { + assertEquals(R.color.widget_dyn_secondary_dark, widgetColor(dynamic = true, idx = 0, isPrimary = false, isLight = false, followSystem = false).res) + } + + @Test + fun testResolveWidgetColor_Default_FollowSystem_UsesAutoResource() { + assertEquals(R.color.widget_text_secondary_auto, widgetColor(dynamic = false, idx = 0, isPrimary = false, isLight = true, followSystem = true).res) + } + + @Test + fun testResolveWidgetColor_Default_ForcedTheme_HasNoResource() { + assertEquals(0, widgetColor(dynamic = false, idx = 0, isPrimary = true, isLight = true, followSystem = false).res) + } + + @Test + fun testResolveWidgetColor_PreS_HasNoResource() { + assertEquals(0, widgetColor(dynamic = true, idx = 0, isPrimary = true, isLight = true, followSystem = true, sdkInt = Build.VERSION_CODES.R).res) + } } diff --git a/app/src/test/java/com/leanbitlab/lwidget/SystemStatsFetcherTest.kt b/app/src/test/java/com/leanbitlab/lwidget/SystemStatsFetcherTest.kt index 200e42a..9231b08 100644 --- a/app/src/test/java/com/leanbitlab/lwidget/SystemStatsFetcherTest.kt +++ b/app/src/test/java/com/leanbitlab/lwidget/SystemStatsFetcherTest.kt @@ -1,5 +1,6 @@ package com.leanbitlab.lwidget +import com.leanbitlab.lwidget.render.WidgetColor import android.content.Context import android.widget.RemoteViews import androidx.test.core.app.ApplicationProvider @@ -18,13 +19,13 @@ class SystemStatsFetcherTest { val views = RemoteViews(context.packageName, R.layout.widget_layout) // Test 12-hour mode - SystemStatsFetcher.loadWorldClock(views, 14f, 0xFFFFFFFF.toInt(), "UTC", is12Hour = true) + SystemStatsFetcher.loadWorldClock(views, 14f, WidgetColor(0xFFFFFFFF.toInt()), "UTC", is12Hour = true) // Test 24-hour mode - SystemStatsFetcher.loadWorldClock(views, 14f, 0xFFFFFFFF.toInt(), "America/New_York", is12Hour = false) + SystemStatsFetcher.loadWorldClock(views, 14f, WidgetColor(0xFFFFFFFF.toInt()), "America/New_York", is12Hour = false) // Test invalid timezone does not crash and hides view - SystemStatsFetcher.loadWorldClock(views, 14f, 0xFFFFFFFF.toInt(), "Invalid/Zone", is12Hour = true) + SystemStatsFetcher.loadWorldClock(views, 14f, WidgetColor(0xFFFFFFFF.toInt()), "Invalid/Zone", is12Hour = true) } @Test