Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions app/src/main/java/com/leanbitlab/lwidget/AwidgetProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/leanbitlab/lwidget/ColorResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/leanbitlab/lwidget/SettingsBinders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<View>(viewId)
val tvTitle = row.findViewById<TextView>(R.id.row_label)
Expand All @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -196,8 +197,8 @@ object CalendarFetcher {
views: RemoteViews,
events: List<EventInfo>,
textSizeSp: Float,
primaryColor: Int,
secondaryColor: Int,
primaryColor: WidgetColor,
secondaryColor: WidgetColor,
eventViews: List<Int>,
prefs: SharedPreferences
) {
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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(
Expand All @@ -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)

Expand Down
35 changes: 35 additions & 0 deletions app/src/main/java/com/leanbitlab/lwidget/render/WidgetColor.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Loading
Loading