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(
R.styleable.KeyboardLayoutSet_Element_supportsSplitLayout, false);
elementParams.mAllowRedundantMoreKeys = a.getBoolean(
R.styleable.KeyboardLayoutSet_Element_allowRedundantMoreKeys, true);
R.styleable.KeyboardLayoutSet_Element_allowRedundantMoreKeys, false);
mParams.mKeyboardLayoutSetElementIdToParamsMap.put(elementName, elementParams);
} finally {
a.recycle();

View File

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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
package org.futo.inputmethod.latin.uix
import android.app.Activity
import android.app.KeyguardManager
import android.content.ClipDescription
import android.content.Context
import android.content.Intent
@ -280,6 +281,10 @@ class UixActionKeyboardManager(val uixManager: UixManager, val latinIME: LatinIM
uixManager.showActionEditor()
}
override fun isDeviceLocked(): Boolean {
return getContext().isDeviceLocked
}
override fun getLatinIMEForDebug(): LatinIME = latinIME
}
@ -847,4 +852,12 @@ class UixManager(private val latinIME: LatinIME) {
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
import android.app.Dialog
import android.app.KeyguardManager
import android.content.Context
import android.util.TypedValue
import android.view.Gravity
@ -108,4 +109,13 @@ fun DialogComposeView.show() {
fun DialogComposeView.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,
mimeTypes = listOf()
))
override suspend fun onDeviceUnlocked() {
loadClipboard()
}
init {
coroutineScope.launch {
@ -379,7 +383,7 @@ val ClipboardHistoryAction = Action(
},
persistentStateInitialization = PersistentStateInitialization.OnKeyboardLoad,
windowImpl = { manager, persistent ->
val unlocked = manager.getContext().isDirectBootUnlocked
val unlocked = !manager.isDeviceLocked()
val clipboardHistoryManager = persistent as ClipboardHistoryManager
manager.getLifecycleScope().launch { clipboardHistoryManager.pruneOldItems() }
@ -396,52 +400,58 @@ val ClipboardHistoryAction = Action(
val clipboardHistory = useDataStore(ClipboardHistoryEnabled, blocking = true)
if(!clipboardHistory.value) return
IconButton(onClick = {
val numUnpinnedItems = clipboardHistoryManager.clipboardHistory.count { !it.pinned }
if(clipboardHistoryManager.clipboardHistory.size == 0) {
manager.requestDialog(
"There are no items to clear. Disable clipboard history?",
listOf(
DialogRequestItem("Cancel") {},
DialogRequestItem("Disable") {
clipboardHistory.setValue(false)
},
),
{}
)
} else if(numUnpinnedItems == 0) {
manager.requestDialog(
"There are no unpinned items to clear. Unpin all items?",
listOf(
DialogRequestItem("Cancel") {},
DialogRequestItem("Unpin") {
clipboardHistoryManager.clipboardHistory.toList().forEach {
if(it.pinned) {
clipboardHistoryManager.onPin(it)
if(unlocked) {
IconButton(onClick = {
val numUnpinnedItems =
clipboardHistoryManager.clipboardHistory.count { !it.pinned }
if (clipboardHistoryManager.clipboardHistory.size == 0) {
manager.requestDialog(
"There are no items to clear. Disable clipboard history?",
listOf(
DialogRequestItem("Cancel") {},
DialogRequestItem("Disable") {
clipboardHistory.setValue(false)
},
),
{}
)
} else if (numUnpinnedItems == 0) {
manager.requestDialog(
"There are no unpinned items to clear. Unpin all items?",
listOf(
DialogRequestItem("Cancel") {},
DialogRequestItem("Unpin") {
clipboardHistoryManager.clipboardHistory.toList().forEach {
if (it.pinned) {
clipboardHistoryManager.onPin(it)
}
}
}
},
),
{}
)
} else {
manager.requestDialog(
"Clear all unpinned items?",
listOf(
DialogRequestItem("Cancel") {},
DialogRequestItem("Clear") {
clipboardHistoryManager.clipboardHistory.toList().forEach {
if (!it.pinned) {
clipboardHistoryManager.onRemove(it)
},
),
{}
)
} else {
manager.requestDialog(
"Clear all unpinned items?",
listOf(
DialogRequestItem("Cancel") {},
DialogRequestItem("Clear") {
clipboardHistoryManager.clipboardHistory.toList().forEach {
if (!it.pinned) {
clipboardHistoryManager.onRemove(it)
}
}
}
},
),
{}
},
),
{}
)
}
}) {
Icon(
painterResource(id = R.drawable.close),
contentDescription = "Clear clipboard"
)
}
}) {
Icon(painterResource(id = R.drawable.close), contentDescription = "Clear clipboard")
}
}