feat(wallpaper-store): enhance previews, EXIF handling, and shuffle
Android Release / Build signed release APK (push) Successful in 34m17s
Android Release / Build signed release APK (push) Successful in 34m17s
These changes add image previews and robust image handling across platforms. Android, desktop, and TS UI now share imagePreviews data and a persistent shuffle queue, improving ordering and previews. EXIF orientation is applied when decoding images and rendering crops. Manifest and rendering paths were adjusted for portrait mode. - Introduce imagePreviews in models and UI to show per-image crop data. - Implement EXIF orientation handling and enhanced cropping. - Improve shuffle with persistent queue and seen-tracking.
This commit is contained in:
@@ -8,6 +8,9 @@ import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.Matrix
|
||||
import android.graphics.Paint
|
||||
import android.graphics.RectF
|
||||
import android.media.ExifInterface
|
||||
import android.os.Build
|
||||
import android.util.Base64
|
||||
import android.util.LruCache
|
||||
import app.tauri.plugin.JSArray
|
||||
@@ -15,6 +18,7 @@ import app.tauri.plugin.JSObject
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.security.MessageDigest
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
object WallpaperStore {
|
||||
@@ -23,11 +27,14 @@ object WallpaperStore {
|
||||
private const val KEY_CURRENT_ID = "current_entry_id"
|
||||
private const val KEY_INTERVAL = "interval_minutes"
|
||||
private const val KEY_SHUFFLE_SEEN_IDS = "shuffle_seen_ids"
|
||||
private const val KEY_SHUFFLE_QUEUE_IDS = "shuffle_queue_ids"
|
||||
private const val CROP_PREFIX = "crop_"
|
||||
private const val INVALID_PREFIX = "invalid_"
|
||||
private const val RENDER_VERSION = 7
|
||||
private const val THUMBNAIL_VERSION = 2
|
||||
private const val RENDER_CACHE_MAX_BYTES = 128L * 1024 * 1024
|
||||
private const val THUMBNAIL_CACHE_MAX_BYTES = 64L * 1024 * 1024
|
||||
private const val HOME_PREVIEW_LIMIT = 12
|
||||
private const val HOME_PREVIEW_LIMIT = 6
|
||||
private val thumbnailCache = LruCache<String, String>(48)
|
||||
|
||||
private data class CropSettings(val mode: String = "cover", val zoom: Double = 1.0, val x: Double = 0.5, val y: Double = 0.5, val rotation: Int = 0)
|
||||
@@ -93,6 +100,38 @@ object WallpaperStore {
|
||||
return legacy
|
||||
}
|
||||
|
||||
private fun addedAt(entry: Entry): Long = when (entry) {
|
||||
is Entry.Local -> entry.file.name.substringBefore('-').toLongOrNull() ?: entry.file.lastModified()
|
||||
is Entry.Immich -> entry.addedAt
|
||||
}
|
||||
|
||||
private fun writeShuffleQueue(context: Context, ids: List<String>) {
|
||||
prefs(context).edit().putString(KEY_SHUFFLE_QUEUE_IDS, JSONArray(ids).toString()).apply()
|
||||
}
|
||||
|
||||
private fun shuffleOrder(context: Context, items: List<Entry>, currentId: String): Pair<MutableSet<String>, MutableList<String>> {
|
||||
val availableIds = items.mapTo(linkedSetOf()) { it.id }
|
||||
val preferences = prefs(context)
|
||||
val seenIds = preferences.getStringSet(KEY_SHUFFLE_SEEN_IDS, emptySet())
|
||||
.orEmpty().filterTo(linkedSetOf()) { it in availableIds }
|
||||
seenIds.add(currentId)
|
||||
if (seenIds.size >= availableIds.size && availableIds.size > 1) {
|
||||
seenIds.clear()
|
||||
seenIds.add(currentId)
|
||||
}
|
||||
|
||||
val storedQueue = runCatching {
|
||||
val json = JSONArray(preferences.getString(KEY_SHUFFLE_QUEUE_IDS, "[]"))
|
||||
(0 until json.length()).map { json.getString(it) }
|
||||
}.getOrDefault(emptyList())
|
||||
val queue = storedQueue.filterTo(mutableListOf()) { it in availableIds && it !in seenIds }
|
||||
val missing = availableIds.filter { it !in seenIds && it !in queue }.shuffled()
|
||||
queue.addAll(missing)
|
||||
preferences.edit().putStringSet(KEY_SHUFFLE_SEEN_IDS, seenIds).apply()
|
||||
writeShuffleQueue(context, queue)
|
||||
return seenIds to queue
|
||||
}
|
||||
|
||||
fun intervalMinutes(context: Context): Int {
|
||||
val preferences = prefs(context)
|
||||
return if (preferences.contains(KEY_INTERVAL)) preferences.getInt(KEY_INTERVAL, 0)
|
||||
@@ -109,7 +148,7 @@ object WallpaperStore {
|
||||
fun set(context: Context, name: String, value: Boolean) {
|
||||
require(name in setOf("shuffle", "lockScreenOnly", "allowMobileData", "prefetchImmich")) { "Unbekannte Einstellung" }
|
||||
val editor = prefs(context).edit().putBoolean(name, value)
|
||||
if (name == "shuffle") editor.remove(KEY_SHUFFLE_SEEN_IDS)
|
||||
if (name == "shuffle") editor.remove(KEY_SHUFFLE_SEEN_IDS).remove(KEY_SHUFFLE_QUEUE_IDS)
|
||||
editor.apply()
|
||||
if (name == "prefetchImmich") {
|
||||
if (value && ImmichClient.configured(context)) ImmichPrefetchWorker.enqueue(context)
|
||||
@@ -126,8 +165,15 @@ object WallpaperStore {
|
||||
fun state(context: Context, includePreviews: Boolean = true): JSObject {
|
||||
val items = entries(context)
|
||||
val index = currentIndex(context, items)
|
||||
val previewItems = if (index < HOME_PREVIEW_LIMIT) items.take(HOME_PREVIEW_LIMIT) else listOf(items[index]) + items.take(HOME_PREVIEW_LIMIT - 1)
|
||||
val previewIndex = if (index < HOME_PREVIEW_LIMIT) index else 0
|
||||
val previewItems = if (items.isEmpty()) emptyList() else {
|
||||
val current = items[index]
|
||||
if (shuffle(context) && items.size > 1) {
|
||||
val queue = shuffleOrder(context, items, current.id).second
|
||||
listOf(current) + queue.take(HOME_PREVIEW_LIMIT - 1).mapNotNull { id -> items.firstOrNull { it.id == id } }
|
||||
} else {
|
||||
(0 until minOf(HOME_PREVIEW_LIMIT, items.size)).map { items[(index + it).mod(items.size)] }
|
||||
}
|
||||
}
|
||||
return JSObject().apply {
|
||||
put("imageCount", items.size)
|
||||
put("enabled", enabled(context))
|
||||
@@ -136,7 +182,7 @@ object WallpaperStore {
|
||||
put("lockScreenOnly", lockOnly(context))
|
||||
put("allowMobileData", allowMobileData(context))
|
||||
put("prefetchImmich", prefetchImmich(context))
|
||||
put("currentIndex", previewIndex)
|
||||
put("currentIndex", 0)
|
||||
put("currentId", items.getOrNull(index)?.id ?: JSONObject.NULL)
|
||||
val ids = JSArray()
|
||||
previewItems.forEach { ids.put(it.id) }
|
||||
@@ -144,20 +190,24 @@ object WallpaperStore {
|
||||
val previews = JSArray()
|
||||
if (includePreviews) previewItems.forEach { previews.put(thumbnailDataUrl(context, it.preview)) }
|
||||
put("imageUrls", previews)
|
||||
val imagePreviews = JSArray()
|
||||
if (includePreviews) previewItems.forEach { imagePreviews.put(galleryImage(context, it, it.id == items.getOrNull(index)?.id)) }
|
||||
put("imagePreviews", imagePreviews)
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun gallery(context: Context, offset: Int, limit: Int): JSObject {
|
||||
val items = entries(context)
|
||||
val selectedIndex = currentIndex(context, items)
|
||||
val safeOffset = offset.coerceAtLeast(0).coerceAtMost(items.size)
|
||||
val selectedId = items.getOrNull(currentIndex(context, items))?.id
|
||||
val galleryItems = items.sortedWith(compareByDescending<Entry> { addedAt(it) }.thenByDescending { it.id })
|
||||
val safeOffset = offset.coerceAtLeast(0).coerceAtMost(galleryItems.size)
|
||||
val safeLimit = limit.coerceIn(1, 100)
|
||||
val page = JSArray()
|
||||
items.drop(safeOffset).take(safeLimit).forEachIndexed { pageIndex, entry ->
|
||||
page.put(galleryImage(context, entry, safeOffset + pageIndex == selectedIndex))
|
||||
galleryItems.drop(safeOffset).take(safeLimit).forEach { entry ->
|
||||
page.put(galleryImage(context, entry, entry.id == selectedId))
|
||||
}
|
||||
return JSObject().apply { put("total", items.size); put("items", page) }
|
||||
return JSObject().apply { put("total", galleryItems.size); put("items", page) }
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
@@ -215,8 +265,16 @@ object WallpaperStore {
|
||||
val json = JSONObject().apply {
|
||||
put("mode", normalized.mode); put("zoom", normalized.zoom); put("x", normalized.x); put("y", normalized.y); put("rotation", normalized.rotation)
|
||||
}
|
||||
prefs(context).edit().putString(cropKey(id), json.toString()).apply()
|
||||
return galleryImage(context, entry, items.indexOf(entry) == currentIndex(context, items))
|
||||
val selected = items.indexOf(entry) == currentIndex(context, items)
|
||||
check(prefs(context).edit().putString(cropKey(id), json.toString()).commit()) {
|
||||
"Bildausschnitt konnte nicht gespeichert werden"
|
||||
}
|
||||
if (selected) {
|
||||
check(apply(context, id)) {
|
||||
"Bildausschnitt wurde gespeichert, konnte aber nicht angewendet werden"
|
||||
}
|
||||
}
|
||||
return galleryImage(context, entry, selected)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
@@ -288,27 +346,42 @@ object WallpaperStore {
|
||||
val metrics = context.resources.displayMetrics
|
||||
val targetWidth = metrics.widthPixels.coerceAtLeast(1)
|
||||
val targetHeight = metrics.heightPixels.coerceAtLeast(1)
|
||||
val quarterTurn = crop.rotation == 90 || crop.rotation == 270
|
||||
val rotatedWidth = if (quarterTurn) source.height else source.width
|
||||
val rotatedHeight = if (quarterTurn) source.width else source.height
|
||||
val rotated = if (crop.rotation == 0) source else Bitmap.createBitmap(
|
||||
source,
|
||||
0,
|
||||
0,
|
||||
source.width,
|
||||
source.height,
|
||||
Matrix().apply { setRotate(crop.rotation.toFloat()) },
|
||||
true,
|
||||
)
|
||||
val rotatedWidth = rotated.width
|
||||
val rotatedHeight = rotated.height
|
||||
val widthScale = targetWidth.toDouble() / rotatedWidth
|
||||
val heightScale = targetHeight.toDouble() / rotatedHeight
|
||||
val baseScale = if (crop.mode == "contain") minOf(widthScale, heightScale) else maxOf(widthScale, heightScale)
|
||||
val scale = (baseScale * crop.zoom).toFloat()
|
||||
val baseWidth = rotatedWidth * baseScale
|
||||
val baseHeight = rotatedHeight * baseScale
|
||||
val scaledWidth = rotatedWidth * scale
|
||||
val scaledHeight = rotatedHeight * scale
|
||||
val left = ((targetWidth - scaledWidth) * crop.x).toFloat()
|
||||
val top = ((targetHeight - scaledHeight) * crop.y).toFloat()
|
||||
return Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888).also { output ->
|
||||
val canvas = Canvas(output)
|
||||
canvas.drawColor(Color.BLACK)
|
||||
val matrix = Matrix().apply {
|
||||
postTranslate(-source.width / 2f, -source.height / 2f)
|
||||
postRotate(crop.rotation.toFloat())
|
||||
postScale(scale, scale)
|
||||
postTranslate(left + scaledWidth / 2f, top + scaledHeight / 2f)
|
||||
val baseLeft = (targetWidth - baseWidth) * crop.x
|
||||
val baseTop = (targetHeight - baseHeight) * crop.y
|
||||
val left = (targetWidth / 2.0 + (baseLeft - targetWidth / 2.0) * crop.zoom).toFloat()
|
||||
val top = (targetHeight / 2.0 + (baseTop - targetHeight / 2.0) * crop.zoom).toFloat()
|
||||
return try {
|
||||
Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888).also { output ->
|
||||
val canvas = Canvas(output)
|
||||
canvas.drawColor(Color.BLACK)
|
||||
canvas.drawBitmap(
|
||||
rotated,
|
||||
null,
|
||||
RectF(left, top, left + scaledWidth, top + scaledHeight),
|
||||
Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG),
|
||||
)
|
||||
}
|
||||
canvas.drawBitmap(source, matrix, Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG))
|
||||
} finally {
|
||||
if (rotated !== source) rotated.recycle()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,11 +392,34 @@ object WallpaperStore {
|
||||
val targetLongSide = maxOf(context.resources.displayMetrics.widthPixels, context.resources.displayMetrics.heightPixels).coerceAtLeast(1)
|
||||
var sample = 1
|
||||
while (maxOf(bounds.outWidth, bounds.outHeight) / (sample * 2) >= targetLongSide) sample *= 2
|
||||
return BitmapFactory.decodeFile(file.absolutePath, BitmapFactory.Options().apply { inSampleSize = sample; inPreferredConfig = Bitmap.Config.ARGB_8888 })
|
||||
val decoded = BitmapFactory.decodeFile(file.absolutePath, BitmapFactory.Options().apply { inSampleSize = sample; inPreferredConfig = Bitmap.Config.ARGB_8888 }) ?: return null
|
||||
return applyExifOrientation(file, decoded)
|
||||
}
|
||||
|
||||
private fun applyExifOrientation(file: File, bitmap: Bitmap): Bitmap {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return bitmap
|
||||
val orientation = runCatching {
|
||||
ExifInterface(file.absolutePath).getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
|
||||
}.getOrDefault(ExifInterface.ORIENTATION_NORMAL)
|
||||
val matrix = Matrix().apply {
|
||||
when (orientation) {
|
||||
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> setScale(-1f, 1f)
|
||||
ExifInterface.ORIENTATION_ROTATE_180 -> setRotate(180f)
|
||||
ExifInterface.ORIENTATION_FLIP_VERTICAL -> setScale(1f, -1f)
|
||||
ExifInterface.ORIENTATION_TRANSPOSE -> { setRotate(90f); postScale(-1f, 1f) }
|
||||
ExifInterface.ORIENTATION_ROTATE_90 -> setRotate(90f)
|
||||
ExifInterface.ORIENTATION_TRANSVERSE -> { setRotate(-90f); postScale(-1f, 1f) }
|
||||
ExifInterface.ORIENTATION_ROTATE_270 -> setRotate(-90f)
|
||||
}
|
||||
}
|
||||
if (matrix.isIdentity) return bitmap
|
||||
val oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
|
||||
if (oriented !== bitmap) bitmap.recycle()
|
||||
return oriented
|
||||
}
|
||||
|
||||
private fun thumbnailDataUrl(context: Context, file: File): String {
|
||||
val cacheKey = "${file.absolutePath}:${file.lastModified()}:${file.length()}"
|
||||
val cacheKey = "$THUMBNAIL_VERSION:${file.absolutePath}:${file.lastModified()}:${file.length()}"
|
||||
thumbnailCache.get(cacheKey)?.let { return it }
|
||||
val cached = File(thumbnailDirectory(context), "${digest(cacheKey)}.jpg")
|
||||
if (cached.isFile) {
|
||||
@@ -336,7 +432,8 @@ object WallpaperStore {
|
||||
BitmapFactory.decodeFile(file.absolutePath, bounds)
|
||||
var sample = 1
|
||||
while (bounds.outWidth / sample > 360 || bounds.outHeight / sample > 480) sample *= 2
|
||||
val bitmap = BitmapFactory.decodeFile(file.absolutePath, BitmapFactory.Options().apply { inSampleSize = sample }) ?: return ""
|
||||
val decoded = BitmapFactory.decodeFile(file.absolutePath, BitmapFactory.Options().apply { inSampleSize = sample }) ?: return ""
|
||||
val bitmap = applyExifOrientation(file, decoded)
|
||||
val result = ByteArrayOutputStream().use { out ->
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 76, out); bitmap.recycle()
|
||||
val bytes = out.toByteArray()
|
||||
@@ -348,7 +445,7 @@ object WallpaperStore {
|
||||
}
|
||||
|
||||
private fun removeThumbnail(file: File) {
|
||||
thumbnailCache.remove("${file.absolutePath}:${file.lastModified()}:${file.length()}")
|
||||
thumbnailCache.remove("$THUMBNAIL_VERSION:${file.absolutePath}:${file.lastModified()}:${file.length()}")
|
||||
}
|
||||
|
||||
private fun digest(value: String): String = MessageDigest.getInstance("SHA-256")
|
||||
@@ -360,7 +457,7 @@ object WallpaperStore {
|
||||
is Entry.Local -> "${entry.file.lastModified()}:${entry.file.length()}"
|
||||
is Entry.Immich -> entry.assetId
|
||||
}
|
||||
val key = "${entry.id}:$sourceVersion:${metrics.widthPixels}x${metrics.heightPixels}:${prefs(context).getString(cropKey(entry.id), "")}"
|
||||
val key = "$RENDER_VERSION:${entry.id}:$sourceVersion:${metrics.widthPixels}x${metrics.heightPixels}:${prefs(context).getString(cropKey(entry.id), "")}"
|
||||
return File(renderedDirectory(context), "${digest(key)}.jpg")
|
||||
}
|
||||
|
||||
@@ -394,18 +491,15 @@ object WallpaperStore {
|
||||
if (items.isEmpty()) return false
|
||||
val previous = currentIndex(context, items)
|
||||
if (shuffle(context) && items.size > 1) {
|
||||
val availableIds = items.mapTo(mutableSetOf()) { it.id }
|
||||
val seenIds = prefs(context).getStringSet(KEY_SHUFFLE_SEEN_IDS, emptySet())
|
||||
.orEmpty().filterTo(mutableSetOf()) { it in availableIds }
|
||||
seenIds.add(items[previous].id)
|
||||
|
||||
var candidates = items.indices.filter { items[it].id !in seenIds }.shuffled()
|
||||
if (candidates.isEmpty()) {
|
||||
seenIds.clear()
|
||||
seenIds.add(items[previous].id)
|
||||
candidates = items.indices.filter { it != previous }.shuffled()
|
||||
val (seenIds, queue) = shuffleOrder(context, items, items[previous].id)
|
||||
val candidates = queue.mapNotNull { id -> items.indexOfFirst { it.id == id }.takeIf { it >= 0 } }
|
||||
val applied = applyCandidates(context, items, candidates, automatic, seenIds)
|
||||
if (applied) {
|
||||
val selectedId = prefs(context).getString(KEY_CURRENT_ID, null)
|
||||
val selectedPosition = queue.indexOf(selectedId)
|
||||
writeShuffleQueue(context, if (selectedPosition >= 0) queue.drop(selectedPosition + 1) else queue.filter { it != selectedId })
|
||||
}
|
||||
return applyCandidates(context, items, candidates, automatic, seenIds)
|
||||
return applied
|
||||
}
|
||||
val candidates = (1..items.size).map { (previous + it).mod(items.size) }
|
||||
return applyCandidates(context, items, candidates, automatic)
|
||||
|
||||
@@ -15,6 +15,20 @@ pub struct Wallpaper<R: Runtime>(AppHandle<R>);
|
||||
|
||||
impl<R: Runtime> Wallpaper<R> {
|
||||
fn demo() -> WallpaperState {
|
||||
let image_previews = ["alpine", "waterfall", "coast"]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(index, name)| GalleryImage {
|
||||
id: format!("demo-{index}"),
|
||||
url: format!("/wallpapers/{name}.png"),
|
||||
selected: index == 0,
|
||||
crop_mode: "cover".into(),
|
||||
crop_zoom: 1.0,
|
||||
crop_position_x: 0.5,
|
||||
crop_position_y: 0.5,
|
||||
crop_rotation: 0,
|
||||
})
|
||||
.collect();
|
||||
WallpaperState {
|
||||
image_count: 3,
|
||||
enabled: true,
|
||||
@@ -31,6 +45,7 @@ impl<R: Runtime> Wallpaper<R> {
|
||||
"/wallpapers/waterfall.png".into(),
|
||||
"/wallpapers/coast.png".into(),
|
||||
],
|
||||
image_previews,
|
||||
}
|
||||
}
|
||||
pub fn get_state(&self) -> crate::Result<WallpaperState> {
|
||||
|
||||
@@ -14,6 +14,8 @@ pub struct WallpaperState {
|
||||
pub current_id: Option<String>,
|
||||
pub image_ids: Vec<String>,
|
||||
pub image_urls: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub image_previews: Vec<GalleryImage>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
|
||||
Reference in New Issue
Block a user