Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions app/src/main/java/helium314/keyboard/latin/gif/GifPickerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<GifItem>) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
}

Expand Down
Loading