Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

feat(gallery): a Recent artwork home screen widget that opens the newest artwork

Josh89badfb90c26
diff --git a/apps/procraft-android/android/app/src/main/AndroidManifest.xml b/apps/procraft-android/android/app/src/main/AndroidManifest.xml
index 1317db8..ac62e4f 100644
--- a/apps/procraft-android/android/app/src/main/AndroidManifest.xml
+++ b/apps/procraft-android/android/app/src/main/AndroidManifest.xml
@@ -45,6 +45,19 @@
</intent-filter>
</activity>
+ <!-- the home screen's Recent artwork widget -->
+ <receiver
+ android:name=".gallery.RecentArtworkWidget"
+ android:exported="true"
+ android:label="@string/widget_recent_name">
+ <intent-filter>
+ <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
+ </intent-filter>
+ <meta-data
+ android:name="android.appwidget.provider"
+ android:resource="@xml/widget_recent" />
+ </receiver>
+
<!-- hands exported pictures to other apps -->
<provider
android:name="androidx.core.content.FileProvider"
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/MainActivity.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/MainActivity.kt
index be7edf1..54e37ad 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/MainActivity.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/MainActivity.kt
@@ -60,6 +60,12 @@ class MainActivity : ComponentActivity() {
*/
private fun importOpened(intent: android.content.Intent?) {
intent ?: return
+ // the home screen's Recent artwork widget: straight into the newest artwork
+ if (intent.action == io.github.artcraftandroid.procraft.gallery.RecentArtworkWidget.OPEN_RECENT) {
+ intent.action = null
+ vm.openMostRecent()
+ return
+ }
val uri = when (intent.action) {
android.content.Intent.ACTION_VIEW -> intent.data
android.content.Intent.ACTION_SEND ->
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
index 5d0e811..b7c085a 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
@@ -781,6 +781,8 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
refreshGallery()
}
}
+ // the home screen's widget shows the newest artwork
+ viewModelScope.launch(Dispatchers.IO) { io.github.artcraftandroid.procraft.gallery.RecentArtworkWidget.refresh(getApplication<Application>()) }
}
fun showStack(s: io.github.artcraftandroid.procraft.gallery.Stack?) {
@@ -1592,6 +1594,23 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
}
}
+ /**
+ * The home screen's Recent artwork widget was tapped: the newest artwork opens, once the window
+ * has its size (the app may only now be starting); nothing if one is already open.
+ */
+ fun openMostRecent() {
+ if (opening || session != null) return
+ viewModelScope.launch {
+ var waited = 0L
+ while (windowPx.width <= 1 && waited < WINDOW_WAIT_MS) {
+ kotlinx.coroutines.delay(CANVAS_POLL_MS)
+ waited += CANVAS_POLL_MS
+ }
+ val a = withContext(Dispatchers.IO) { store.mostRecent() } ?: return@launch
+ openArtwork(a)
+ }
+ }
+
/** Opens artwork [a], the gallery zooming into its cell; then [then]. */
fun openArtwork(a: Artwork, then: (() -> Unit)? = null) {
// one artwork open at a time: a second session on it would leave the screen on the first
@@ -1824,7 +1843,11 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
val dir = artworkDir ?: return
noteTime()
keepChoices(s)
- viewModelScope.launch(Dispatchers.IO) { runCatching { s.save(dir.path) } }
+ viewModelScope.launch(Dispatchers.IO) {
+ runCatching { s.save(dir.path) }
+ // its new picture on the home screen's widget
+ io.github.artcraftandroid.procraft.gallery.RecentArtworkWidget.refresh(getApplication<Application>())
+ }
}
/**
@@ -5904,6 +5927,9 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
private const val SELECTION_KIND = "selectionKind"
private const val SELECTION_THRESHOLD = "selectionThreshold"
private const val DROP_THRESHOLD = "dropThreshold"
+
+ /** How long a widget's tap waits for the window to be measured before opening anyway. */
+ private const val WINDOW_WAIT_MS = 3000L
private const val RECOLOR_THRESHOLD = "recolorThreshold"
/** What undo and redo call a Recolor fill. */
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/ArtworkStore.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/ArtworkStore.kt
index 6925243..b976917 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/ArtworkStore.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/ArtworkStore.kt
@@ -35,6 +35,13 @@ class ArtworkStore(val root: File) {
read(dir) ?: if (at == root) readStack(dir) else null
}.sortedByDescending { it.modified }
+ /** The artwork changed last, in the gallery or a stack. */
+ fun mostRecent(): Artwork? = when (val e = list().firstOrNull()) {
+ is Artwork -> e
+ is Stack -> e.artworks.firstOrNull()
+ null -> null
+ }
+
fun newFolder(at: File = root): File = File(at, UUID.randomUUID().toString()).also { it.mkdirs() }
fun delete(a: Artwork) {
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/RecentArtworkWidget.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/RecentArtworkWidget.kt
new file mode 100644
index 0000000..6db159b
--- /dev/null
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/gallery/RecentArtworkWidget.kt
@@ -0,0 +1,69 @@
+package io.github.artcraftandroid.procraft.gallery
+
+import android.app.PendingIntent
+import android.appwidget.AppWidgetManager
+import android.appwidget.AppWidgetProvider
+import android.content.ComponentName
+import android.content.Context
+import android.content.Intent
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.view.View
+import android.widget.RemoteViews
+import io.github.artcraftandroid.procraft.MainActivity
+import io.github.artcraftandroid.procraft.R
+import java.io.File
+
+/**
+ * The home screen's Recent artwork widget: the newest artwork's picture filling it (the app's mark
+ * when there is none); a tap opens the app at that artwork.
+ */
+class RecentArtworkWidget : AppWidgetProvider() {
+ override fun onUpdate(context: Context, manager: AppWidgetManager, ids: IntArray) {
+ val views = views(context)
+ for (id in ids) manager.updateAppWidget(id, views)
+ }
+
+ companion object {
+ /** What a tap on the widget asks of the app. */
+ const val OPEN_RECENT = "io.github.artcraftandroid.procraft.OPEN_RECENT"
+
+ /** The longest side the picture is given at (the home screen holds it in memory). */
+ private const val PICTURE_MOST = 720
+
+ /** Shows the newest artwork on every Recent artwork widget there is. */
+ fun refresh(context: Context) {
+ val manager = AppWidgetManager.getInstance(context)
+ val ids = runCatching { manager.getAppWidgetIds(ComponentName(context, RecentArtworkWidget::class.java)) }.getOrNull()
+ if (ids == null || ids.isEmpty()) return
+ val views = views(context)
+ for (id in ids) manager.updateAppWidget(id, views)
+ }
+
+ private fun views(context: Context): RemoteViews {
+ val views = RemoteViews(context.packageName, R.layout.widget_recent)
+ val picture = ArtworkStore(File(context.filesDir, "artworks")).mostRecent()?.thumbnail?.let(::picture)
+ if (picture != null) {
+ views.setImageViewBitmap(R.id.widget_picture, picture)
+ views.setViewVisibility(R.id.widget_picture, View.VISIBLE)
+ views.setViewVisibility(R.id.widget_mark, View.GONE)
+ } else {
+ views.setViewVisibility(R.id.widget_picture, View.GONE)
+ views.setViewVisibility(R.id.widget_mark, View.VISIBLE)
+ }
+ val open = Intent(context, MainActivity::class.java).setAction(OPEN_RECENT).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
+ views.setOnClickPendingIntent(R.id.widget_root, PendingIntent.getActivity(context, 0, open, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT))
+ return views
+ }
+
+ /** The thumbnail read at most [PICTURE_MOST] across. */
+ private fun picture(f: File): Bitmap? {
+ if (!f.isFile) return null
+ val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
+ BitmapFactory.decodeFile(f.path, bounds)
+ var sample = 1
+ while (maxOf(bounds.outWidth, bounds.outHeight) / (sample * 2) >= PICTURE_MOST) sample *= 2
+ return runCatching { BitmapFactory.decodeFile(f.path, BitmapFactory.Options().apply { inSampleSize = sample }) }.getOrNull()
+ }
+ }
+}
diff --git a/apps/procraft-android/android/app/src/main/res/drawable-v31/widget_back.xml b/apps/procraft-android/android/app/src/main/res/drawable-v31/widget_back.xml
new file mode 100644
index 0000000..6095adc
--- /dev/null
+++ b/apps/procraft-android/android/app/src/main/res/drawable-v31/widget_back.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- The widget's field, rounded as the home screen rounds its widgets. -->
+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
+ <solid android:color="#FF1C1C1E" />
+ <corners android:radius="@android:dimen/system_app_widget_background_radius" />
+</shape>
diff --git a/apps/procraft-android/android/app/src/main/res/drawable/widget_back.xml b/apps/procraft-android/android/app/src/main/res/drawable/widget_back.xml
new file mode 100644
index 0000000..283a523
--- /dev/null
+++ b/apps/procraft-android/android/app/src/main/res/drawable/widget_back.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- The widget's field, under its picture: the app icon's dark grey, rounded. -->
+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
+ <solid android:color="#FF1C1C1E" />
+ <corners android:radius="16dp" />
+</shape>
diff --git a/apps/procraft-android/android/app/src/main/res/layout/widget_recent.xml b/apps/procraft-android/android/app/src/main/res/layout/widget_recent.xml
new file mode 100644
index 0000000..05da6ac
--- /dev/null
+++ b/apps/procraft-android/android/app/src/main/res/layout/widget_recent.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- The Recent artwork widget: the newest artwork's picture filling it, or the app's mark. -->
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/widget_root"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:background="@drawable/widget_back"
+ android:clipToOutline="true">
+
+ <ImageView
+ android:id="@+id/widget_picture"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:contentDescription="@string/widget_recent_name"
+ android:scaleType="centerCrop"
+ android:visibility="gone" />
+
+ <ImageView
+ android:id="@+id/widget_mark"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:contentDescription="@string/app_name"
+ android:scaleType="fitCenter"
+ android:src="@drawable/ic_launcher_foreground" />
+</FrameLayout>
diff --git a/apps/procraft-android/android/app/src/main/res/values/strings.xml b/apps/procraft-android/android/app/src/main/res/values/strings.xml
index 6d915a4..7cbb591 100644
--- a/apps/procraft-android/android/app/src/main/res/values/strings.xml
+++ b/apps/procraft-android/android/app/src/main/res/values/strings.xml
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ProCraft</string>
+ <string name="widget_recent_name">Recent artwork</string>
+ <string name="widget_recent_description">Opens ProCraft at the artwork you worked on last.</string>
</resources>
diff --git a/apps/procraft-android/android/app/src/main/res/xml/widget_recent.xml b/apps/procraft-android/android/app/src/main/res/xml/widget_recent.xml
new file mode 100644
index 0000000..e681873
--- /dev/null
+++ b/apps/procraft-android/android/app/src/main/res/xml/widget_recent.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Any size from one cell up: small, wide and large, as the user makes it. -->
+<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
+ android:description="@string/widget_recent_description"
+ android:initialLayout="@layout/widget_recent"
+ android:minWidth="110dp"
+ android:minHeight="110dp"
+ android:minResizeWidth="40dp"
+ android:minResizeHeight="40dp"
+ android:previewLayout="@layout/widget_recent"
+ android:resizeMode="horizontal|vertical"
+ android:targetCellWidth="2"
+ android:targetCellHeight="2"
+ android:updatePeriodMillis="0"
+ android:widgetCategory="home_screen" />