Modern Apps git

← Modern-Apps.git

weather: added per-minute widget clock update using TextClock

Commit
8854b5fbea7041f86cbd6d05bfc5936db37c3c4e
Author
Vayun Mathur <[email protected]>
When
2026-06-02 01:36 UTC (6 months ago)
Parents
76c7cd9

Diff

email/src/main/java/com/vayunmathur/email/widget/EmailWidgetReceiver.kt
diff --git a/email/src/main/java/com/vayunmathur/email/widget/EmailWidgetReceiver.kt b/email/src/main/java/com/vayunmathur/email/widget/EmailWidgetReceiver.ktindex 7ab60f54f5..d8a189e5ad 100644--- a/email/src/main/java/com/vayunmathur/email/widget/EmailWidgetReceiver.kt+++ b/email/src/main/java/com/vayunmathur/email/widget/EmailWidgetReceiver.kt@@ -1,8 +1,14 @@ package com.vayunmathur.email.widget -import androidx.glance.appwidget.GlanceAppWidget+import android.content.Context import androidx.glance.appwidget.GlanceAppWidgetReceiver+import com.vayunmathur.library.widgets.scheduleHourlyUpdate  class EmailWidgetReceiver : GlanceAppWidgetReceiver() {-    override val glanceAppWidget: GlanceAppWidget = EmailWidget()+    override val glanceAppWidget: EmailWidget = EmailWidget()++    override fun onEnabled(context: Context) {+        super.onEnabled(context)+        context.scheduleHourlyUpdate(EmailWidget::class)+    } }
weather/src/main/java/com/vayunmathur/weather/glance/WeatherGlanceWidget.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/glance/WeatherGlanceWidget.kt b/weather/src/main/java/com/vayunmathur/weather/glance/WeatherGlanceWidget.ktindex c865e87a53..22cdc180c3 100644--- a/weather/src/main/java/com/vayunmathur/weather/glance/WeatherGlanceWidget.kt+++ b/weather/src/main/java/com/vayunmathur/weather/glance/WeatherGlanceWidget.kt@@ -31,6 +31,9 @@ import androidx.glance.layout.width import androidx.glance.text.FontWeight import androidx.glance.text.Text import androidx.glance.text.TextStyle+import android.widget.RemoteViews+import androidx.glance.appwidget.AndroidRemoteViews+import com.vayunmathur.weather.R import com.vayunmathur.library.util.buildDatabase import com.vayunmathur.library.widgets.DynamicThemeGlance import com.vayunmathur.weather.MainActivity@@ -104,25 +107,53 @@ class WeatherGlanceWidget : GlanceAppWidget() { }  /**- * Generic intent that asks the user's default clock app to open. Falls- * back gracefully — every Android device ships a handler for- * [AlarmClock.ACTION_SHOW_ALARMS].+ * Intent that opens the Clock app. Tries to open the app from this project+ * first (com.vayunmathur.clock), falls back to the system clock app.  */-private fun clockIntent(): Intent = Intent(AlarmClock.ACTION_SHOW_ALARMS).apply {-    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+private fun clockIntent(context: Context): Intent {+    // Try to launch the clock app from this project+    val explicitIntent = Intent().apply {+        setClassName("com.vayunmathur.clock", "com.vayunmathur.clock.MainActivity")+        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+    }+    +    // Check if the explicit intent can be resolved+    return if (explicitIntent.resolveActivity(context.packageManager) != null) {+        explicitIntent+    } else {+        // Fall back to generic alarm clock intent+        Intent(AlarmClock.ACTION_SHOW_ALARMS).apply {+            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+        }+    } }  /**- * Generic intent that opens the user's default calendar app via the- * standard [Intent.CATEGORY_APP_CALENDAR] launcher category.+ * Intent that opens the Calendar app. Tries to open the app from this project+ * first (com.vayunmathur.calendar), falls back to the system calendar app.  */-private fun calendarIntent(): Intent = Intent(Intent.ACTION_MAIN).apply {-    addCategory(Intent.CATEGORY_APP_CALENDAR)-    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+private fun calendarIntent(context: Context): Intent {+    // Try to launch the calendar app from this project+    val explicitIntent = Intent().apply {+        setClassName("com.vayunmathur.calendar", "com.vayunmathur.calendar.MainActivity")+        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+    }+    +    // Check if the explicit intent can be resolved+    return if (explicitIntent.resolveActivity(context.packageManager) != null) {+        explicitIntent+    } else {+        // Fall back to generic calendar intent+        Intent(Intent.ACTION_MAIN).apply {+            addCategory(Intent.CATEGORY_APP_CALENDAR)+            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+        }+    } }  @Composable private fun Content(weather: WidgetWeather?) {+    val context = LocalContext.current     val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())     Row(         modifier = GlanceModifier@@ -140,12 +171,12 @@ private fun Content(weather: WidgetWeather?) {         ) {             Box(                 modifier = GlanceModifier-                    .clickable(actionStartActivity(clockIntent())),-            ) { TimeBlock(now) }+                    .clickable(actionStartActivity(clockIntent(context))),+            ) { TimeBlock(context) }             Box(                 modifier = GlanceModifier                     .padding(top = 2.dp)-                    .clickable(actionStartActivity(calendarIntent())),+                    .clickable(actionStartActivity(calendarIntent(context))),             ) { DateBlock(now) }         }         // Right: large weather icon + temperature, clickable into this app.@@ -161,18 +192,9 @@ private fun Content(weather: WidgetWeather?) { }  @Composable-private fun TimeBlock(now: LocalDateTime) {-    val timeFormat = LocalDateTime.Format {-        hour(Padding.NONE); char(':'); minute(Padding.ZERO)-    }-    Text(-        text = now.format(timeFormat),-        style = TextStyle(-            color = GlanceTheme.colors.onSurface,-            fontSize = 40.sp,-            fontWeight = FontWeight.Bold,-        ),-    )+private fun TimeBlock(context: Context) {+    val remoteViews = RemoteViews(context.packageName, R.layout.widget_text_clock)+    AndroidRemoteViews(remoteViews) }  @Composable
weather/src/main/res/layout/widget_text_clock.xml
diff --git a/weather/src/main/res/layout/widget_text_clock.xml b/weather/src/main/res/layout/widget_text_clock.xmlnew file mode 100644index 0000000000..915fd91022--- /dev/null+++ b/weather/src/main/res/layout/widget_text_clock.xml@@ -0,0 +1,10 @@+<?xml version="1.0" encoding="utf-8"?>+<TextClock xmlns:android="http://schemas.android.com/apk/res/android"+    android:id="@+id/widget_clock"+    android:layout_width="wrap_content"+    android:layout_height="wrap_content"+    android:format12Hour="h:mm"+    android:format24Hour="H:mm"+    android:textSize="40sp"+    android:textStyle="bold"+    android:textColor="?android:attr/textColorPrimary" />