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
4 changes: 2 additions & 2 deletions app/src/main/java/helium314/keyboard/latin/utils/JniUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ object JniUtils {
const val JNI_LIB_IMPORT_FILE_NAME = "libjni_latinime.so"
private const val CHECKSUM_ARM64 = "b1049983e6ac5cfc6d1c66e38959751044fad213dff0637a6cf1d2a2703e754f"
private const val CHECKSUM_ARM32 = "442a2a8bfcb25489564bc9433a916fa4dc0dba9000fe6f6f03f5939b985091e6"
private const val CHECKSUM_X86_64 = "c882e12e6d48dd946e0b644c66868a720bd11ac3fecf152000e21a3d5abd59c9"
private const val CHECKSUM_X86 = "bd946d126c957b5a6dea3bafa07fa36a27950b30e2b684dffc60746d0a1c7ad8"
private const val CHECKSUM_X86_64 = "bd946d126c957b5a6dea3bafa07fa36a27950b30e2b684dffc60746d0a1c7ad8"
private const val CHECKSUM_X86 = "c882e12e6d48dd946e0b644c66868a720bd11ac3fecf152000e21a3d5abd59c9"

fun expectedDefaultChecksum(): String {
val abi = Build.SUPPORTED_ABIS[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ open class SettingsActivity : ComponentActivity(), SharedPreferences.OnSharedPre
private val crashReportFiles = MutableStateFlow<List<File>>(emptyList())
private var paused = true
private var onReopenWelcomeWizard: (() -> Unit)? = null
/** popups of the settings screen below wait while the wizard covers it */
var welcomeWizardShowing by mutableStateOf(false)
private set

fun reopenWelcomeWizard() {
onReopenWelcomeWizard?.invoke()
Expand Down Expand Up @@ -140,6 +143,7 @@ open class SettingsActivity : ComponentActivity(), SharedPreferences.OnSharedPre
onReopenWelcomeWizard = {
showWelcomeWizard = true
}
welcomeWizardShowing = showWelcomeWizard
val snackbarHostState = androidx.compose.runtime.remember { androidx.compose.material3.SnackbarHostState() }
androidx.compose.runtime.LaunchedEffect(Unit) {
FeedbackManager.messages.collect { message ->
Expand Down Expand Up @@ -183,7 +187,8 @@ open class SettingsActivity : ComponentActivity(), SharedPreferences.OnSharedPre
showWelcomeWizard = false
},
finish = {
prefs.edit().putBoolean(Settings.PREF_SETUP_WIZARD_DISMISSED, true).apply()
// commit, because the wizard may exit the process right after
prefs.edit().putBoolean(Settings.PREF_SETUP_WIZARD_DISMISSED, true).commit()
this@SettingsActivity.finish()
}
)
Expand Down
435 changes: 249 additions & 186 deletions app/src/main/java/helium314/keyboard/settings/WelcomeWizard.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fun LoadCjkPluginPreference(
onSuccess: (() -> Unit)? = null,
) {
var showDialog by rememberSaveable { mutableStateOf(false) }
var isDownloading by rememberSaveable { mutableStateOf(false) }
var isDownloading by remember { mutableStateOf(false) }
var downloadProgress by remember { mutableFloatStateOf(0f) }
var remoteVersion by remember { mutableStateOf<String?>(null) }
var isCheckingUpdate by remember { mutableStateOf(false) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.settings.preferences

import android.net.Uri
import helium314.keyboard.latin.BuildConfig
import android.content.Intent
import android.widget.Toast
import androidx.annotation.DrawableRes
Expand Down Expand Up @@ -62,7 +64,7 @@ fun LoadEmojiLibPreference(
onSuccess: (() -> Unit)? = null,
) {
var showDialog by rememberSaveable { mutableStateOf(false) }
var isDownloading by rememberSaveable { mutableStateOf(false) }
var isDownloading by remember { mutableStateOf(false) }
var downloadProgress by remember { mutableFloatStateOf(0f) }
val ctx = LocalContext.current
val scope = rememberCoroutineScope()
Expand All @@ -84,6 +86,14 @@ fun LoadEmojiLibPreference(
}

fun startDownload() {
// no internet permission in offline: the browser downloads, "Load from file" imports
if (BuildConfig.FLAVOR == "offline") {
showDialog = false
val url = "${Links.DICTIONARY_URL}${Links.DICTIONARY_DOWNLOAD_SUFFIX}${Links.DICTIONARY_EMOJI_CLDR_SUFFIX}$dictName"
ctx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
FeedbackManager.message(ctx, R.string.downloading_in_browser_import)
return
}
isDownloading = true
downloadProgress = 0f
scope.launch(Dispatchers.IO) {
Expand Down Expand Up @@ -118,7 +128,7 @@ fun LoadEmojiLibPreference(
putString("pref_dict_download_link_emoji_${locale.toLanguageTag()}", urlStr)
}
withContext(Dispatchers.Main) {
FeedbackManager.message(ctx, R.string.load_gesture_library_download_success) // Reusing success string
FeedbackManager.message(ctx, R.string.emoji_dict_loaded_success)
isDownloading = false
showDialog = false
refreshAndLoad()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package helium314.keyboard.settings.preferences

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
Expand Down Expand Up @@ -30,6 +31,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
Expand Down Expand Up @@ -61,17 +63,28 @@ import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException

// a deleted library stays loaded until the process restarts, so sHaveNativeGestureLib alone is not enough
private var removedUntilRestart = false
// the library file is not observable, this redraws its readers after an import or delete
private var libChanges by mutableIntStateOf(0)

fun isGestureLibInstalled(context: Context): Boolean {
libChanges // read to subscribe
return File(context.filesDir, JniUtils.JNI_LIB_IMPORT_FILE_NAME).exists()
|| (JniUtils.sHaveNativeGestureLib && !removedUntilRestart)
}

@SuppressLint("ApplySharedPref")
@Composable
fun LoadGestureLibPreference(
title: String,
summary: String? = if (JniUtils.sHaveNativeGestureLib) stringResource(R.string.libraries_status_active) else stringResource(R.string.libraries_status_not_installed),
summary: String? = null,
@DrawableRes icon: Int? = null,
restartOnSuccess: Boolean = true,
onSuccess: (() -> Unit)? = null,
) {
var showDialog by rememberSaveable { mutableStateOf(false) }
var isDownloading by rememberSaveable { mutableStateOf(false) }
var isDownloading by remember { mutableStateOf(false) }
var downloadProgress by remember { mutableFloatStateOf(0f) }
val ctx = LocalContext.current
val prefs = ctx.protectedPrefs()
Expand All @@ -88,6 +101,8 @@ fun LoadGestureLibPreference(
putBoolean(Settings.PREF_GESTURE_INPUT, true)
}
file.copyTo(libFile)
removedUntilRestart = false
libChanges++
libFile.setReadOnly()
file.delete()
try {
Expand Down Expand Up @@ -148,12 +163,16 @@ fun LoadGestureLibPreference(
}
} catch (e: IOException) {
tmpfile.delete()
FeedbackManager.message(ctx, R.string.file_read_error)
}
}

Preference(
name = title,
description = summary,
description = summary ?: stringResource(
if (isGestureLibInstalled(ctx)) R.string.libraries_status_active
else R.string.libraries_status_not_installed
),
icon = icon,
onClick = { showDialog = true }
)
Expand All @@ -166,7 +185,7 @@ fun LoadGestureLibPreference(
}

if (showDialog) {
val isInstalled = libFile.exists() || JniUtils.sHaveNativeGestureLib
val isInstalled = isGestureLibInstalled(ctx)
helium314.keyboard.settings.dialogs.PreferenceDialog(
onDismissRequest = { if (!isDownloading) showDialog = false },
title = stringResource(R.string.load_gesture_library),
Expand Down Expand Up @@ -261,6 +280,8 @@ fun LoadGestureLibPreference(
Button(
onClick = {
libFile.delete()
removedUntilRestart = true
libChanges++
prefs.edit(commit = true) { remove(Settings.PREF_LIBRARY_CHECKSUM) }
onSuccess?.invoke()
showDialog = false
Expand Down Expand Up @@ -304,6 +325,7 @@ fun LoadGestureLibPreference(
content = { Text(stringResource(R.string.checksum_mismatch_message, abi)) },
onConfirmed = {
val tempFile = File(currentTempPath)
tempFilePath = null
renameToLibFileAndRestart(tempFile, ChecksumCalculator.checksum(tempFile) ?: "")
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fun LoadHandwritingPluginPreference(
onSuccess: (() -> Unit)? = null,
) {
var showDialog by rememberSaveable { mutableStateOf(false) }
var isDownloading by rememberSaveable { mutableStateOf(false) }
var isDownloading by remember { mutableStateOf(false) }
var downloadProgress by remember { mutableFloatStateOf(0f) }
var remoteVersion by remember { mutableStateOf<String?>(null) }
var updateAvailable by remember { mutableStateOf(false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fun LoadOcrPluginPreference(
onSuccess: (() -> Unit)? = null,
) {
var showDialog by rememberSaveable { mutableStateOf(false) }
var isDownloading by rememberSaveable { mutableStateOf(false) }
var isDownloading by remember { mutableStateOf(false) }
var downloadProgress by remember { mutableFloatStateOf(0f) }
var remoteVersion by remember { mutableStateOf<String?>(null) }
var isCheckingUpdate by remember { mutableStateOf(false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fun LoadOfflineAiPluginPreference(
onSuccess: (() -> Unit)? = null,
) {
var showDialog by rememberSaveable { mutableStateOf(false) }
var isDownloading by rememberSaveable { mutableStateOf(false) }
var isDownloading by remember { mutableStateOf(false) }
var downloadProgress by remember { mutableFloatStateOf(0f) }
var remoteVersion by remember { mutableStateOf<String?>(null) }
var updateAvailable by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -91,7 +91,7 @@ fun LoadOfflineAiPluginPreference(
val launcher = filePicker { uri ->
val success = OfflineAiLoader.loadPlugin(ctx, uri)
if (success) {
FeedbackManager.message(ctx, R.string.offline_ai_plugin_loaded_restarting)
if (restartOnSuccess) FeedbackManager.message(ctx, R.string.offline_ai_plugin_loaded_restarting)
onSuccess?.invoke()
showDialog = false
if (restartOnSuccess) {
Expand Down Expand Up @@ -145,7 +145,7 @@ fun LoadOfflineAiPluginPreference(
isDownloading = false
downloadProgress = 0f
if (success) {
FeedbackManager.message(ctx, R.string.offline_ai_plugin_loaded_restarting)
if (restartOnSuccess) FeedbackManager.message(ctx, R.string.offline_ai_plugin_loaded_restarting)
onSuccess?.invoke()
showDialog = false
if (restartOnSuccess) {
Expand Down Expand Up @@ -250,7 +250,7 @@ fun LoadOfflineAiPluginPreference(
Button(
onClick = {
OfflineAiLoader.removePlugin(ctx)
FeedbackManager.message(ctx, R.string.offline_ai_plugin_removed_restarting)
if (restartOnSuccess) FeedbackManager.message(ctx, R.string.offline_ai_plugin_removed_restarting)
onSuccess?.invoke()
showDialog = false
if (restartOnSuccess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fun LoadTranslationPluginPreference(
onSuccess: (() -> Unit)? = null,
) {
var showDialog by rememberSaveable { mutableStateOf(false) }
var isDownloading by rememberSaveable { mutableStateOf(false) }
var isDownloading by remember { mutableStateOf(false) }
var downloadProgress by remember { mutableFloatStateOf(0f) }
var remoteVersion by remember { mutableStateOf<String?>(null) }
var updateAvailable by remember { mutableStateOf(false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ fun MainSettingsScreen(
var showSponsorDialog by rememberSaveable { mutableStateOf(false) }

// what's new takes precedence, the sponsor dialog then shows on a later settings open
if (shouldShow && !whatsNewPending) {
val wizardShowing = (ctx.getActivity() as? SettingsActivity)?.welcomeWizardShowing == true
if (shouldShow && !whatsNewPending && !wizardShowing) {
LaunchedEffect(Unit) {
showSponsorDialog = true
}
}

if (showWhatsNew) {
if (showWhatsNew && !wizardShowing) {
WhatsNewDialog(
changelogItems = currentChangelogItems,
onDismissRequest = { showWhatsNew = false },
Expand All @@ -138,7 +139,7 @@ fun MainSettingsScreen(
)
}

if (showSponsorDialog) {
if (showSponsorDialog && !wizardShowing) {
LaunchedEffect(Unit) {
prefs.edit { putLong(Settings.PREF_LAST_SPONSOR_DIALOG_SHOWN, System.currentTimeMillis()) }
}
Expand Down
Loading