@@ -43,6 +43,7 @@ import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode
4343import helium314.keyboard.latin.R
4444import helium314.keyboard.latin.common.ColorType
4545import helium314.keyboard.latin.common.Constants
46+ import helium314.keyboard.latin.settings.Defaults
4647import helium314.keyboard.latin.settings.Settings
4748import helium314.keyboard.latin.utils.dpToPx
4849import 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
0 commit comments