photos: add photo widget
- Commit
402e005e0d001d6d374748b2e487fb438f832102- Author
- Vayun <[email protected]>
- When
- 2026-01-23 23:18 UTC (10 months ago)
- Parents
0f5f1dd
Diff
photos/build.gradle.kts
diff --git a/photos/build.gradle.kts b/photos/build.gradle.ktsindex 3322f227dc..c693cc62a7 100644--- a/photos/build.gradle.kts+++ b/photos/build.gradle.kts@@ -21,4 +21,10 @@ dependencies { implementation(libs.androidx.room.runtime) implementation(libs.androidx.room.ktx) ksp(libs.androidx.room.compiler)+++ // Glance app widget+ implementation(libs.androidx.glance)+ implementation(libs.androidx.glance.appwidget)+ implementation(libs.androidx.glance.material3) }\ No newline at end of file
photos/src/main/AndroidManifest.xml
diff --git a/photos/src/main/AndroidManifest.xml b/photos/src/main/AndroidManifest.xmlindex 72174f9917..7d22305fb4 100644--- a/photos/src/main/AndroidManifest.xml+++ b/photos/src/main/AndroidManifest.xml@@ -22,6 +22,17 @@ <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>+ <!-- Glance widget receiver -->+ <receiver+ android:name=".glance.PhotoGlanceWidgetReceiver"+ android:exported="false">+ <intent-filter>+ <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />+ </intent-filter>+ <meta-data+ android:name="android.appwidget.provider"+ android:resource="@xml/photo_glance_widget_info" />+ </receiver> </application> </manifest>\ No newline at end of file
photos/src/main/java/com/vayunmathur/photos/glance/PhotoGlanceWidget.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/glance/PhotoGlanceWidget.kt b/photos/src/main/java/com/vayunmathur/photos/glance/PhotoGlanceWidget.ktnew file mode 100644index 0000000000..e1fe7c178c--- /dev/null+++ b/photos/src/main/java/com/vayunmathur/photos/glance/PhotoGlanceWidget.kt@@ -0,0 +1,129 @@+package com.vayunmathur.photos.glance++import android.content.Context+import android.graphics.Bitmap+import android.graphics.BitmapFactory+import android.net.Uri+import android.provider.MediaStore.Images.Media.getBitmap+import androidx.compose.foundation.layout.aspectRatio+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.runtime.Composable+import androidx.compose.runtime.CompositionLocalProvider+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.produceState+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.Modifier+import androidx.compose.ui.unit.dp+import androidx.core.net.toUri+import androidx.glance.GlanceId+import androidx.glance.GlanceModifier+import androidx.glance.Image+import androidx.glance.ImageProvider+import androidx.glance.LocalContext+import androidx.glance.action.actionStartActivity+import androidx.glance.action.clickable+import androidx.glance.appwidget.GlanceAppWidget+import androidx.glance.appwidget.components.Scaffold+import androidx.glance.appwidget.components.TitleBar+import androidx.glance.appwidget.provideContent+import androidx.glance.layout.ContentScale+import androidx.glance.layout.fillMaxSize+import com.vayunmathur.library.ui.DynamicThemeGlance+import com.vayunmathur.library.util.DatabaseViewModel+import com.vayunmathur.library.util.buildDatabase+import com.vayunmathur.photos.ImageLoader+import com.vayunmathur.photos.MainActivity+import com.vayunmathur.photos.R+import com.vayunmathur.photos.data.Photo+import com.vayunmathur.photos.data.PhotoDatabase+import androidx.core.graphics.scale+import androidx.glance.layout.Alignment+import androidx.glance.layout.Box+import androidx.glance.layout.padding+import androidx.glance.layout.size+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.withContext++class PhotoGlanceWidget : GlanceAppWidget() {+ override suspend fun provideGlance(context: Context, id: GlanceId) {++ val db = context.buildDatabase<PhotoDatabase>()+ val viewModel = DatabaseViewModel(Photo::class to db.noteDao())++ provideContent {+ val photos by viewModel.data<Photo>().collectAsState(listOf())+ var photo by remember(photos) { mutableStateOf(photos.randomOrNull()) }+ val bitmap by produceState<Bitmap?>(initialValue = null, photo) {+ value = withContext(Dispatchers.IO) {+ photo?.let { getResizedBitmap(context, it.uri.toUri(), 600) }+ }+ }+ DynamicThemeGlance(context) {+ bitmap?.let {+ Content(photo!!, it) { photo = photos.randomOrNull() }+ }+ }+ }+ }+}++@Composable+fun Content(photo: Photo, bitmap: Bitmap, newPhoto: () -> Unit) {+ Box(+ modifier = GlanceModifier.fillMaxSize(),+ contentAlignment = Alignment.Center+ ) {+ Image(+ provider = ImageProvider(bitmap),+ contentDescription = null,+ modifier = GlanceModifier+ .fillMaxSize()+ .clickable { newPhoto() },+ contentScale = ContentScale.Crop+ )+ }+}++fun getResizedBitmap(context: Context, uri: Uri, maxSize: Int = 600): Bitmap? {+ return try {+ val contentResolver = context.contentResolver++ // 1. Get dimensions only (no memory used for pixels)+ val options = BitmapFactory.Options().apply {+ inJustDecodeBounds = true+ }+ contentResolver.openInputStream(uri)?.use {+ BitmapFactory.decodeStream(it, null, options)+ }++ // 2. Calculate the sample size (power of 2)+ options.inSampleSize = calculateInSampleSize(options, maxSize, maxSize)+ options.inJustDecodeBounds = false++ // 3. Decode the downsampled bitmap+ val downsampledBitmap = contentResolver.openInputStream(uri)?.use {+ BitmapFactory.decodeStream(it, null, options)+ }+ downsampledBitmap+ } catch (e: Exception) {+ e.printStackTrace()+ null+ }+}++private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {+ val (height: Int, width: Int) = options.outHeight to options.outWidth+ var inSampleSize = 1++ if (height > reqHeight || width > reqWidth) {+ val halfHeight = height / 2+ val halfWidth = width / 2+ while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {+ inSampleSize *= 2+ }+ }+ return inSampleSize+}\ No newline at end of file
photos/src/main/java/com/vayunmathur/photos/glance/PhotoGlanceWidgetReceiver.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/glance/PhotoGlanceWidgetReceiver.kt b/photos/src/main/java/com/vayunmathur/photos/glance/PhotoGlanceWidgetReceiver.ktnew file mode 100644index 0000000000..d4180b4abe--- /dev/null+++ b/photos/src/main/java/com/vayunmathur/photos/glance/PhotoGlanceWidgetReceiver.kt@@ -0,0 +1,8 @@+package com.vayunmathur.photos.glance++import androidx.glance.appwidget.GlanceAppWidgetReceiver++class PhotoGlanceWidgetReceiver : GlanceAppWidgetReceiver() {+ override val glanceAppWidget: PhotoGlanceWidget = PhotoGlanceWidget()+}+
photos/src/main/res/layout/glance_widget_initial_layout.xml
diff --git a/photos/src/main/res/layout/glance_widget_initial_layout.xml b/photos/src/main/res/layout/glance_widget_initial_layout.xmlnew file mode 100644index 0000000000..430e0fbfa1--- /dev/null+++ b/photos/src/main/res/layout/glance_widget_initial_layout.xml@@ -0,0 +1,5 @@+<?xml version="1.0" encoding="utf-8"?>+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"+ android:layout_width="match_parent"+ android:layout_height="match_parent" />+
photos/src/main/res/xml/photo_glance_widget_info.xml
diff --git a/photos/src/main/res/xml/photo_glance_widget_info.xml b/photos/src/main/res/xml/photo_glance_widget_info.xmlnew file mode 100644index 0000000000..0907f75d3e--- /dev/null+++ b/photos/src/main/res/xml/photo_glance_widget_info.xml@@ -0,0 +1,9 @@+<?xml version="1.0" encoding="utf-8"?>+<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"+ android:minWidth="100dp"+ android:minHeight="110dp"+ android:updatePeriodMillis="0"+ android:initialLayout="@layout/glance_widget_initial_layout"+ android:resizeMode="horizontal|vertical"+ android:widgetCategory="home_screen" />+