From 67a129ec756f79a7ebc16a6487061f77fd2a8979 Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Thu, 19 Nov 2026 05:28:33 +0530 Subject: [PATCH] feat(gif): autoplay visible GIF tiles with a Preview toggle A Preview switch in GIF settings plays the tiles on screen without a long-press. Only attached tiles decode and animate (downsampled to the tile height) and they stop on detach, so off-screen items cost no CPU. Pack tiles stay static. Needs Android 9+, the switch is greyed out below. --- .../keyboard/latin/gif/GifPickerView.kt | 60 +++++++++++++++++-- .../keyboard/latin/settings/Defaults.kt | 1 + .../keyboard/latin/settings/Settings.kt | 1 + .../settings/screens/GifSettingsScreen.kt | 10 ++++ 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/gif/GifPickerView.kt b/app/src/main/java/helium314/keyboard/latin/gif/GifPickerView.kt index b1123e0db..9725de60b 100644 --- a/app/src/main/java/helium314/keyboard/latin/gif/GifPickerView.kt +++ b/app/src/main/java/helium314/keyboard/latin/gif/GifPickerView.kt @@ -43,6 +43,7 @@ import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode import helium314.keyboard.latin.R import helium314.keyboard.latin.common.ColorType import helium314.keyboard.latin.common.Constants +import helium314.keyboard.latin.settings.Defaults import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.utils.dpToPx import helium314.keyboard.latin.utils.prefs @@ -195,6 +196,8 @@ class GifPickerView @JvmOverloads constructor( fun open() { if (opened) return opened = true + adapter.autoplay = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && + context.prefs().getBoolean(Settings.PREF_GIF_AUTOPLAY, Defaults.PREF_GIF_AUTOPLAY) loadSeq++ query.clear() cursorPos = 0 @@ -1171,6 +1174,8 @@ class GifPickerView @JvmOverloads constructor( private var insertingId: String? = null private var liveItemId: String? = null private var liveJob: Job? = null + /** Play every attached (visible) tile instead of the long-pressed one; set on open, Android 9+ only. */ + var autoplay = false @SuppressLint("NotifyDataSetChanged") fun submit(new: List) { @@ -1204,6 +1209,49 @@ class GifPickerView @JvmOverloads constructor( val liveBadge: TextView = itemView.findViewById(R.id.liveBadge) val packLabel: TextView = itemView.findViewById(R.id.packLabel) val insertOverlay: View = itemView.findViewById(R.id.insertOverlay) + var autoplayUri: Uri? = null + var autoplayJob: Job? = null + } + + override fun onViewAttachedToWindow(holder: VH) { + super.onViewAttachedToWindow(holder) + startAutoplay(holder) + } + + override fun onViewDetachedFromWindow(holder: VH) { + super.onViewDetachedFromWindow(holder) + stopAutoplay(holder) + } + + /** Decodes the tile downsampled to its height and plays it; only attached tiles run, so off-screen ones cost no CPU. */ + private fun startAutoplay(holder: VH) { + if (!autoplay || Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return + val uri = holder.autoplayUri ?: return + (holder.imageView.drawable as? AnimatedImageDrawable)?.let { it.start(); return } + holder.autoplayJob?.cancel() + val h = context.resources.getDimensionPixelSize(R.dimen.gif_thumb_height) + holder.autoplayJob = scope.launch { + val drawable = withContext(Dispatchers.IO) { + try { + ImageDecoder.decodeDrawable(ImageDecoder.createSource(context.contentResolver, uri)) { decoder, info, _ -> + if (info.size.height > h) decoder.setTargetSize(info.size.width * h / info.size.height, h) + } + } catch (_: Exception) { + null + } + } + if (drawable !is AnimatedImageDrawable || holder.autoplayUri != uri || !holder.itemView.isAttachedToWindow) return@launch + holder.imageView.dispose() + holder.imageView.setImageDrawable(drawable) + drawable.repeatCount = AnimatedImageDrawable.REPEAT_INFINITE + drawable.start() + } + } + + private fun stopAutoplay(holder: VH) { + holder.autoplayJob?.cancel() + holder.autoplayJob = null + stopAnimation(holder.imageView) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH { @@ -1213,7 +1261,8 @@ class GifPickerView @JvmOverloads constructor( override fun onViewRecycled(holder: VH) { super.onViewRecycled(holder) - stopAnimation(holder.imageView) + holder.autoplayUri = null + stopAutoplay(holder) holder.imageView.dispose() holder.imageView.setImageDrawable(null) } @@ -1251,7 +1300,9 @@ class GifPickerView @JvmOverloads constructor( holder.packLabel.visibility = View.GONE } - val isLive = item.id == liveItemId + stopAutoplay(holder) + holder.autoplayUri = if (autoplay && !item.isPack) uri else null + val isLive = !autoplay && item.id == liveItemId holder.liveBadge.visibility = if (isLive) View.VISIBLE else View.GONE if (isLive && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { @@ -1265,12 +1316,13 @@ class GifPickerView @JvmOverloads constructor( placeholder(R.drawable.ic_gif_placeholder) precision(Precision.INEXACT) } + if (holder.itemView.isAttachedToWindow) startAutoplay(holder) } holder.itemView.setOnClickListener { onClick(item) } holder.itemView.setOnLongClickListener { - // a pack tile only opens, its cover is not previewed live - if (item.isPack) return@setOnLongClickListener false + // a pack tile only opens, its cover is not previewed live; with autoplay every tile already plays + if (item.isPack || autoplay) return@setOnLongClickListener false it.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) val oldLiveId = liveItemId liveItemId = if (liveItemId == item.id) null else item.id diff --git a/app/src/main/java/helium314/keyboard/latin/settings/Defaults.kt b/app/src/main/java/helium314/keyboard/latin/settings/Defaults.kt index 66ee902b5..592e82692 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/Defaults.kt +++ b/app/src/main/java/helium314/keyboard/latin/settings/Defaults.kt @@ -294,6 +294,7 @@ object Defaults { const val PREF_SHOW_DEBUG_SETTINGS = false val PREF_DEBUG_MODE = BuildConfig.DEBUG const val PREF_SHOW_SUGGESTION_INFOS = false + const val PREF_GIF_AUTOPLAY = false const val PREF_FORCE_NON_DISTINCT_MULTITOUCH = false const val PREF_SLIDING_KEY_INPUT_PREVIEW = true const val PREF_USER_COLORS = "[]" diff --git a/app/src/main/java/helium314/keyboard/latin/settings/Settings.kt b/app/src/main/java/helium314/keyboard/latin/settings/Settings.kt index 24dd62162..5a6986955 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/Settings.kt +++ b/app/src/main/java/helium314/keyboard/latin/settings/Settings.kt @@ -390,6 +390,7 @@ class Settings private constructor() : SharedPreferences.OnSharedPreferenceChang const val PREF_ENABLE_APP_SYNC_LISTENER = "enable_app_sync_listener" const val PREF_GIF_PROVIDER = "pref_gif_provider" const val PREF_GIF_PROVIDER_ENABLED_PREFIX = "pref_gif_provider_enabled_" + const val PREF_GIF_AUTOPLAY = "pref_gif_autoplay" fun isGifProviderEnabled(prefs: SharedPreferences, providerId: String): Boolean { return prefs.getBoolean("${PREF_GIF_PROVIDER_ENABLED_PREFIX}$providerId", true) diff --git a/app/src/main/java/helium314/keyboard/settings/screens/GifSettingsScreen.kt b/app/src/main/java/helium314/keyboard/settings/screens/GifSettingsScreen.kt index dbcc56cb3..52290f016 100644 --- a/app/src/main/java/helium314/keyboard/settings/screens/GifSettingsScreen.kt +++ b/app/src/main/java/helium314/keyboard/settings/screens/GifSettingsScreen.kt @@ -4,6 +4,7 @@ package helium314.keyboard.settings.screens import android.content.Intent import android.net.Uri import android.widget.Toast +import android.os.Build import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.FlowRow @@ -45,10 +46,12 @@ import com.leanbitlab.leantype.gif.ProviderInfo import helium314.keyboard.latin.R import helium314.keyboard.latin.common.Links import helium314.keyboard.latin.gif.GifPluginManager +import helium314.keyboard.latin.settings.Defaults import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.utils.Log import helium314.keyboard.latin.utils.prefs import helium314.keyboard.settings.SearchSettingsScreen +import helium314.keyboard.settings.preferences.SwitchPreference import helium314.keyboard.settings.dialogs.PreferenceDialog import helium314.keyboard.settings.preferences.Preference import helium314.keyboard.settings.preferences.PreferenceCategory @@ -218,6 +221,13 @@ fun GifSettingsScreen( icon = R.drawable.ic_gif, onClick = { showGifPluginDialog = true } ) + // plays visible GIFs without holding them; needs AnimatedImageDrawable (Android 9+) + SwitchPreference( + name = stringResource(R.string.sound_pack_preview), + key = Settings.PREF_GIF_AUTOPLAY, + default = Defaults.PREF_GIF_AUTOPLAY, + enabled = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P + ) } }