Fix clipboard history not reloading after unlock, and lock it on lockscreen

This commit is contained in:
Aleksandras Kostarevas 2024-07-24 22:45:03 +03:00
parent af1bfa23ef
commit 7c90904eaf
8 changed files with 1199 additions and 1201 deletions

View File

@ -476,7 +476,7 @@ public final class KeyboardLayoutSet {
elementParams.mSupportsSplitLayout = a.getBoolean( elementParams.mSupportsSplitLayout = a.getBoolean(
R.styleable.KeyboardLayoutSet_Element_supportsSplitLayout, false); R.styleable.KeyboardLayoutSet_Element_supportsSplitLayout, false);
elementParams.mAllowRedundantMoreKeys = a.getBoolean( elementParams.mAllowRedundantMoreKeys = a.getBoolean(
R.styleable.KeyboardLayoutSet_Element_allowRedundantMoreKeys, true); R.styleable.KeyboardLayoutSet_Element_allowRedundantMoreKeys, false);
mParams.mKeyboardLayoutSetElementIdToParamsMap.put(elementName, elementParams); mParams.mKeyboardLayoutSetElementIdToParamsMap.put(elementName, elementParams);
} finally { } finally {
a.recycle(); a.recycle();

View File

@ -766,6 +766,8 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save
languageModelFacilitator.loadHistoryLog() languageModelFacilitator.loadHistoryLog()
uixManager.onPersistentStatesUnlocked()
// TODO: Spell checker service // TODO: Spell checker service
} }
} }

View File

@ -67,6 +67,7 @@ interface KeyboardManagerForAction {
fun showActionEditor() fun showActionEditor()
fun getLatinIMEForDebug(): LatinIME fun getLatinIMEForDebug(): LatinIME
fun isDeviceLocked(): Boolean
} }
interface ActionWindow { interface ActionWindow {
@ -95,6 +96,9 @@ interface PersistentActionState {
* after this. * after this.
*/ */
suspend fun cleanUp() suspend fun cleanUp()
suspend fun onDeviceUnlocked() { }
} }
enum class PersistentStateInitialization { enum class PersistentStateInitialization {

View File

@ -10,12 +10,16 @@ val lastUsedColor = stringPreferencesKey("last_used_color")
object EmojiTracker { object EmojiTracker {
suspend fun Context.setLastUsedColor(color: String) { suspend fun Context.setLastUsedColor(color: String) {
if(isDeviceLocked) return
dataStore.edit { dataStore.edit {
it[lastUsedColor] = color it[lastUsedColor] = color
} }
} }
suspend fun Context.useEmoji(emoji: String) { suspend fun Context.useEmoji(emoji: String) {
if(isDeviceLocked) return
dataStore.edit { dataStore.edit {
val combined = emoji + "<|>" + (it[lastUsedEmoji] ?: "") val combined = emoji + "<|>" + (it[lastUsedEmoji] ?: "")
it[lastUsedEmoji] = combined.split("<|>").take(128).joinToString("<|>") it[lastUsedEmoji] = combined.split("<|>").take(128).joinToString("<|>")
@ -23,6 +27,8 @@ object EmojiTracker {
} }
suspend fun Context.unuseEmoji(emoji: String) { suspend fun Context.unuseEmoji(emoji: String) {
if(isDeviceLocked) return
dataStore.edit { dataStore.edit {
val split = (it[lastUsedEmoji] ?: "").split("<|>") val split = (it[lastUsedEmoji] ?: "").split("<|>")
val idxToRemove = split.indexOfFirst { v -> v == emoji || v.trim() == emoji.trim() } val idxToRemove = split.indexOfFirst { v -> v == emoji || v.trim() == emoji.trim() }
@ -31,6 +37,8 @@ object EmojiTracker {
} }
suspend fun Context.getRecentEmojis(): List<String> { suspend fun Context.getRecentEmojis(): List<String> {
if(isDeviceLocked) return listOf()
return getSetting(lastUsedEmoji, "") return getSetting(lastUsedEmoji, "")
.split("<|>") .split("<|>")
.filter { it.isNotBlank() } .filter { it.isNotBlank() }
@ -38,6 +46,8 @@ object EmojiTracker {
} }
suspend fun Context.resetRecentEmojis() { suspend fun Context.resetRecentEmojis() {
if(isDeviceLocked) return
setSetting(lastUsedEmoji, "") setSetting(lastUsedEmoji, "")
} }
} }

View File

@ -1,6 +1,7 @@
package org.futo.inputmethod.latin.uix package org.futo.inputmethod.latin.uix
import android.app.Activity import android.app.Activity
import android.app.KeyguardManager
import android.content.ClipDescription import android.content.ClipDescription
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@ -280,6 +281,10 @@ class UixActionKeyboardManager(val uixManager: UixManager, val latinIME: LatinIM
uixManager.showActionEditor() uixManager.showActionEditor()
} }
override fun isDeviceLocked(): Boolean {
return getContext().isDeviceLocked
}
override fun getLatinIMEForDebug(): LatinIME = latinIME override fun getLatinIMEForDebug(): LatinIME = latinIME
} }
@ -847,4 +852,12 @@ class UixManager(private val latinIME: LatinIME) {
isActionsExpanded.value = latinIME.getSettingBlocking(ActionBarExpanded) isActionsExpanded.value = latinIME.getSettingBlocking(ActionBarExpanded)
} }
fun onPersistentStatesUnlocked() {
persistentStates.forEach {
latinIME.lifecycleScope.launch {
it.value?.onDeviceUnlocked()
}
}
}
} }

View File

@ -1,6 +1,7 @@
package org.futo.inputmethod.latin.uix package org.futo.inputmethod.latin.uix
import android.app.Dialog import android.app.Dialog
import android.app.KeyguardManager
import android.content.Context import android.content.Context
import android.util.TypedValue import android.util.TypedValue
import android.view.Gravity import android.view.Gravity
@ -108,4 +109,13 @@ fun DialogComposeView.show() {
fun DialogComposeView.dismiss() { fun DialogComposeView.dismiss() {
dialog.dismiss() dialog.dismiss()
} }
val Context.isDeviceLocked: Boolean
get() {
if(!isDirectBootUnlocked) return false
val keyguardManager: KeyguardManager? = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager?
return keyguardManager?.let { it.isDeviceLocked || it.isKeyguardLocked } ?: false
}

View File

@ -192,6 +192,10 @@ class ClipboardHistoryManager(val context: Context, val coroutineScope: Lifecycl
uri = null, uri = null,
mimeTypes = listOf() mimeTypes = listOf()
)) ))
override suspend fun onDeviceUnlocked() {
loadClipboard()
}
init { init {
coroutineScope.launch { coroutineScope.launch {
@ -379,7 +383,7 @@ val ClipboardHistoryAction = Action(
}, },
persistentStateInitialization = PersistentStateInitialization.OnKeyboardLoad, persistentStateInitialization = PersistentStateInitialization.OnKeyboardLoad,
windowImpl = { manager, persistent -> windowImpl = { manager, persistent ->
val unlocked = manager.getContext().isDirectBootUnlocked val unlocked = !manager.isDeviceLocked()
val clipboardHistoryManager = persistent as ClipboardHistoryManager val clipboardHistoryManager = persistent as ClipboardHistoryManager
manager.getLifecycleScope().launch { clipboardHistoryManager.pruneOldItems() } manager.getLifecycleScope().launch { clipboardHistoryManager.pruneOldItems() }
@ -396,52 +400,58 @@ val ClipboardHistoryAction = Action(
val clipboardHistory = useDataStore(ClipboardHistoryEnabled, blocking = true) val clipboardHistory = useDataStore(ClipboardHistoryEnabled, blocking = true)
if(!clipboardHistory.value) return if(!clipboardHistory.value) return
IconButton(onClick = { if(unlocked) {
val numUnpinnedItems = clipboardHistoryManager.clipboardHistory.count { !it.pinned } IconButton(onClick = {
if(clipboardHistoryManager.clipboardHistory.size == 0) { val numUnpinnedItems =
manager.requestDialog( clipboardHistoryManager.clipboardHistory.count { !it.pinned }
"There are no items to clear. Disable clipboard history?", if (clipboardHistoryManager.clipboardHistory.size == 0) {
listOf( manager.requestDialog(
DialogRequestItem("Cancel") {}, "There are no items to clear. Disable clipboard history?",
DialogRequestItem("Disable") { listOf(
clipboardHistory.setValue(false) DialogRequestItem("Cancel") {},
}, DialogRequestItem("Disable") {
), clipboardHistory.setValue(false)
{} },
) ),
} else if(numUnpinnedItems == 0) { {}
manager.requestDialog( )
"There are no unpinned items to clear. Unpin all items?", } else if (numUnpinnedItems == 0) {
listOf( manager.requestDialog(
DialogRequestItem("Cancel") {}, "There are no unpinned items to clear. Unpin all items?",
DialogRequestItem("Unpin") { listOf(
clipboardHistoryManager.clipboardHistory.toList().forEach { DialogRequestItem("Cancel") {},
if(it.pinned) { DialogRequestItem("Unpin") {
clipboardHistoryManager.onPin(it) clipboardHistoryManager.clipboardHistory.toList().forEach {
if (it.pinned) {
clipboardHistoryManager.onPin(it)
}
} }
} },
}, ),
), {}
{} )
) } else {
} else { manager.requestDialog(
manager.requestDialog( "Clear all unpinned items?",
"Clear all unpinned items?", listOf(
listOf( DialogRequestItem("Cancel") {},
DialogRequestItem("Cancel") {}, DialogRequestItem("Clear") {
DialogRequestItem("Clear") { clipboardHistoryManager.clipboardHistory.toList().forEach {
clipboardHistoryManager.clipboardHistory.toList().forEach { if (!it.pinned) {
if (!it.pinned) { clipboardHistoryManager.onRemove(it)
clipboardHistoryManager.onRemove(it) }
} }
} },
}, ),
), {}
{} )
}
}) {
Icon(
painterResource(id = R.drawable.close),
contentDescription = "Clear clipboard"
) )
} }
}) {
Icon(painterResource(id = R.drawable.close), contentDescription = "Clear clipboard")
} }
} }