Skip to content

Commit de9fed6

Browse files
committed
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.
1 parent 37dc847 commit de9fed6

4 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎app/src/main/java/helium314/keyboard/latin/gif/GifPickerView.kt‎

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode
4343
import helium314.keyboard.latin.R
4444
import helium314.keyboard.latin.common.ColorType
4545
import helium314.keyboard.latin.common.Constants
46+
import helium314.keyboard.latin.settings.Defaults
4647
import helium314.keyboard.latin.settings.Settings
4748
import helium314.keyboard.latin.utils.dpToPx
4849
import helium314.keyboard.latin.utils.prefs
@@ -195,6 +196,8 @@ class GifPickerView @JvmOverloads constructor(
195196
fun open() {
196197
if (opened) return
197198
opened = true
199+
adapter.autoplay = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P &&
200+
context.prefs().getBoolean(Settings.PREF_GIF_AUTOPLAY, Defaults.PREF_GIF_AUTOPLAY)
198201
loadSeq++
199202
query.clear()
200203
cursorPos = 0
@@ -1171,6 +1174,8 @@ class GifPickerView @JvmOverloads constructor(
11711174
private var insertingId: String? = null
11721175
private var liveItemId: String? = null
11731176
private var liveJob: Job? = null
1177+
/** Play every attached (visible) tile instead of the long-pressed one; set on open, Android 9+ only. */
1178+
var autoplay = false
11741179

11751180
@SuppressLint("NotifyDataSetChanged")
11761181
fun submit(new: List<GifItem>) {
@@ -1204,6 +1209,49 @@ class GifPickerView @JvmOverloads constructor(
12041209
val liveBadge: TextView = itemView.findViewById(R.id.liveBadge)
12051210
val packLabel: TextView = itemView.findViewById(R.id.packLabel)
12061211
val insertOverlay: View = itemView.findViewById(R.id.insertOverlay)
1212+
var autoplayUri: Uri? = null
1213+
var autoplayJob: Job? = null
1214+
}
1215+
1216+
override fun onViewAttachedToWindow(holder: VH) {
1217+
super.onViewAttachedToWindow(holder)
1218+
startAutoplay(holder)
1219+
}
1220+
1221+
override fun onViewDetachedFromWindow(holder: VH) {
1222+
super.onViewDetachedFromWindow(holder)
1223+
stopAutoplay(holder)
1224+
}
1225+
1226+
/** Decodes the tile downsampled to its height and plays it; only attached tiles run, so off-screen ones cost no CPU. */
1227+
private fun startAutoplay(holder: VH) {
1228+
if (!autoplay || Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return
1229+
val uri = holder.autoplayUri ?: return
1230+
(holder.imageView.drawable as? AnimatedImageDrawable)?.let { it.start(); return }
1231+
holder.autoplayJob?.cancel()
1232+
val h = context.resources.getDimensionPixelSize(R.dimen.gif_thumb_height)
1233+
holder.autoplayJob = scope.launch {
1234+
val drawable = withContext(Dispatchers.IO) {
1235+
try {
1236+
ImageDecoder.decodeDrawable(ImageDecoder.createSource(context.contentResolver, uri)) { decoder, info, _ ->
1237+
if (info.size.height > h) decoder.setTargetSize(info.size.width * h / info.size.height, h)
1238+
}
1239+
} catch (_: Exception) {
1240+
null
1241+
}
1242+
}
1243+
if (drawable !is AnimatedImageDrawable || holder.autoplayUri != uri || !holder.itemView.isAttachedToWindow) return@launch
1244+
holder.imageView.dispose()
1245+
holder.imageView.setImageDrawable(drawable)
1246+
drawable.repeatCount = AnimatedImageDrawable.REPEAT_INFINITE
1247+
drawable.start()
1248+
}
1249+
}
1250+
1251+
private fun stopAutoplay(holder: VH) {
1252+
holder.autoplayJob?.cancel()
1253+
holder.autoplayJob = null
1254+
stopAnimation(holder.imageView)
12071255
}
12081256

12091257
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
@@ -1213,7 +1261,8 @@ class GifPickerView @JvmOverloads constructor(
12131261

12141262
override fun onViewRecycled(holder: VH) {
12151263
super.onViewRecycled(holder)
1216-
stopAnimation(holder.imageView)
1264+
holder.autoplayUri = null
1265+
stopAutoplay(holder)
12171266
holder.imageView.dispose()
12181267
holder.imageView.setImageDrawable(null)
12191268
}
@@ -1251,7 +1300,9 @@ class GifPickerView @JvmOverloads constructor(
12511300
holder.packLabel.visibility = View.GONE
12521301
}
12531302

1254-
val isLive = item.id == liveItemId
1303+
stopAutoplay(holder)
1304+
holder.autoplayUri = if (autoplay && !item.isPack) uri else null
1305+
val isLive = !autoplay && item.id == liveItemId
12551306
holder.liveBadge.visibility = if (isLive) View.VISIBLE else View.GONE
12561307

12571308
if (isLive && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
@@ -1265,12 +1316,13 @@ class GifPickerView @JvmOverloads constructor(
12651316
placeholder(R.drawable.ic_gif_placeholder)
12661317
precision(Precision.INEXACT)
12671318
}
1319+
if (holder.itemView.isAttachedToWindow) startAutoplay(holder)
12681320
}
12691321

12701322
holder.itemView.setOnClickListener { onClick(item) }
12711323
holder.itemView.setOnLongClickListener {
1272-
// a pack tile only opens, its cover is not previewed live
1273-
if (item.isPack) return@setOnLongClickListener false
1324+
// a pack tile only opens, its cover is not previewed live; with autoplay every tile already plays
1325+
if (item.isPack || autoplay) return@setOnLongClickListener false
12741326
it.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
12751327
val oldLiveId = liveItemId
12761328
liveItemId = if (liveItemId == item.id) null else item.id

‎app/src/main/java/helium314/keyboard/latin/settings/Defaults.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ object Defaults {
294294
const val PREF_SHOW_DEBUG_SETTINGS = false
295295
val PREF_DEBUG_MODE = BuildConfig.DEBUG
296296
const val PREF_SHOW_SUGGESTION_INFOS = false
297+
const val PREF_GIF_AUTOPLAY = false
297298
const val PREF_FORCE_NON_DISTINCT_MULTITOUCH = false
298299
const val PREF_SLIDING_KEY_INPUT_PREVIEW = true
299300
const val PREF_USER_COLORS = "[]"

‎app/src/main/java/helium314/keyboard/latin/settings/Settings.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ class Settings private constructor() : SharedPreferences.OnSharedPreferenceChang
390390
const val PREF_ENABLE_APP_SYNC_LISTENER = "enable_app_sync_listener"
391391
const val PREF_GIF_PROVIDER = "pref_gif_provider"
392392
const val PREF_GIF_PROVIDER_ENABLED_PREFIX = "pref_gif_provider_enabled_"
393+
const val PREF_GIF_AUTOPLAY = "pref_gif_autoplay"
393394

394395
fun isGifProviderEnabled(prefs: SharedPreferences, providerId: String): Boolean {
395396
return prefs.getBoolean("${PREF_GIF_PROVIDER_ENABLED_PREFIX}$providerId", true)

‎app/src/main/java/helium314/keyboard/settings/screens/GifSettingsScreen.kt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package helium314.keyboard.settings.screens
44
import android.content.Intent
55
import android.net.Uri
66
import android.widget.Toast
7+
import android.os.Build
78
import androidx.compose.foundation.layout.Arrangement
89
import androidx.compose.foundation.layout.Column
910
import androidx.compose.foundation.layout.FlowRow
@@ -45,10 +46,12 @@ import com.leanbitlab.leantype.gif.ProviderInfo
4546
import helium314.keyboard.latin.R
4647
import helium314.keyboard.latin.common.Links
4748
import helium314.keyboard.latin.gif.GifPluginManager
49+
import helium314.keyboard.latin.settings.Defaults
4850
import helium314.keyboard.latin.settings.Settings
4951
import helium314.keyboard.latin.utils.Log
5052
import helium314.keyboard.latin.utils.prefs
5153
import helium314.keyboard.settings.SearchSettingsScreen
54+
import helium314.keyboard.settings.preferences.SwitchPreference
5255
import helium314.keyboard.settings.dialogs.PreferenceDialog
5356
import helium314.keyboard.settings.preferences.Preference
5457
import helium314.keyboard.settings.preferences.PreferenceCategory
@@ -218,6 +221,13 @@ fun GifSettingsScreen(
218221
icon = R.drawable.ic_gif,
219222
onClick = { showGifPluginDialog = true }
220223
)
224+
// plays visible GIFs without holding them; needs AnimatedImageDrawable (Android 9+)
225+
SwitchPreference(
226+
name = stringResource(R.string.sound_pack_preview),
227+
key = Settings.PREF_GIF_AUTOPLAY,
228+
default = Defaults.PREF_GIF_AUTOPLAY,
229+
enabled = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
230+
)
221231
}
222232
}
223233

0 commit comments

Comments
 (0)