diff --git a/app/src/main/java/helium314/keyboard/latin/utils/JniUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/JniUtils.kt index d35ce8aad..4a561be9b 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/JniUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/JniUtils.kt @@ -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] diff --git a/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt b/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt index d2e19e772..c96846e2c 100644 --- a/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt +++ b/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt @@ -89,6 +89,9 @@ open class SettingsActivity : ComponentActivity(), SharedPreferences.OnSharedPre private val crashReportFiles = MutableStateFlow>(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() @@ -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 -> @@ -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() } ) diff --git a/app/src/main/java/helium314/keyboard/settings/WelcomeWizard.kt b/app/src/main/java/helium314/keyboard/settings/WelcomeWizard.kt index 19949e4aa..680eb08e6 100644 --- a/app/src/main/java/helium314/keyboard/settings/WelcomeWizard.kt +++ b/app/src/main/java/helium314/keyboard/settings/WelcomeWizard.kt @@ -1,22 +1,26 @@ // SPDX-License-Identifier: GPL-3.0-only package helium314.keyboard.settings +import helium314.keyboard.latin.common.LocaleUtils.localizedDisplayName +import java.io.File +import android.view.inputmethod.InputMethodSubtype +import androidx.compose.runtime.key +import helium314.keyboard.latin.common.LocaleUtils +import helium314.keyboard.latin.common.LocaleUtils.constructLocale +import helium314.keyboard.latin.utils.DictionaryInfoUtils +import helium314.keyboard.latin.utils.DownloadableDictionaryRow +import helium314.keyboard.latin.utils.getKnownDictionariesForLocale import android.content.Context import android.content.Intent -import android.net.Uri -import android.os.Build -import android.Manifest import android.provider.Settings as AndroidSettings import helium314.keyboard.latin.settings.Settings import android.view.inputmethod.InputMethodManager import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.background -import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.ColumnScope import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize @@ -36,7 +40,6 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment @@ -51,29 +54,21 @@ import androidx.compose.ui.unit.dp import androidx.core.content.edit import helium314.keyboard.latin.BuildConfig import helium314.keyboard.latin.R -import helium314.keyboard.latin.permissions.PermissionsUtil import helium314.keyboard.latin.settings.Defaults -import helium314.keyboard.latin.settings.createPrefKeyForBooleanSettings -import helium314.keyboard.latin.utils.JniUtils import helium314.keyboard.latin.utils.SubtypeSettings import helium314.keyboard.settings.dialogs.MultiListPickerDialog import helium314.keyboard.settings.WithSmallTitle -import helium314.keyboard.settings.DropDownField import helium314.keyboard.latin.utils.SubtypeLocaleUtils.displayName import helium314.keyboard.latin.utils.locale import helium314.keyboard.latin.utils.UncachedInputMethodManagerUtils -import helium314.keyboard.latin.utils.getActivity import helium314.keyboard.latin.utils.prefs import helium314.keyboard.settings.preferences.ListPreference import helium314.keyboard.settings.preferences.LoadEmojiLibPreference import helium314.keyboard.settings.preferences.LoadGestureLibPreference -import helium314.keyboard.settings.preferences.MultiSliderPreference +import helium314.keyboard.settings.preferences.isGestureLibInstalled import helium314.keyboard.settings.preferences.Preference -import helium314.keyboard.settings.preferences.SwitchPreference import helium314.keyboard.settings.preferences.TextInputPreference import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import kotlin.math.roundToInt @Composable fun WelcomeWizard( @@ -90,26 +85,24 @@ fun WelcomeWizard( var step by rememberSaveable { mutableIntStateOf(determineStep()) } var requiresRestart by rememberSaveable { mutableStateOf(false) } var refreshTrigger by remember { mutableIntStateOf(0) } - val scope = rememberCoroutineScope() - + LaunchedEffect(step) { - if (step == 2) - scope.launch { - while (step == 2 && !UncachedInputMethodManagerUtils.isThisImeCurrent(ctx, imm)) { - delay(50) - } - step = 3 + if (step == 2) { + while (!UncachedInputMethodManagerUtils.isThisImeCurrent(ctx, imm)) { + delay(50) } + step = 3 + } } val useWideLayout = isWideScreen() val appName = stringResource(ctx.applicationInfo.labelRes) @Composable fun bigText() { val resource = if (step == 0) R.string.setup_welcome_title else R.string.setup_steps_title - Column(Modifier.padding(bottom = 36.dp), horizontalAlignment = Alignment.CenterHorizontally) { + Column(Modifier.fillMaxWidth().padding(bottom = if (step == 0) 36.dp else 16.dp), horizontalAlignment = Alignment.CenterHorizontally) { Text( stringResource(resource, appName), - style = MaterialTheme.typography.displayMedium, + style = if (step == 0) MaterialTheme.typography.displayMedium else MaterialTheme.typography.headlineMedium, textAlign = TextAlign.Center, color = MaterialTheme.colorScheme.onSurface, ) @@ -126,67 +119,75 @@ fun WelcomeWizard( } @Composable - fun ColumnScope.Step( - currentStep: Int, - title: String, - instruction: String, - actionText: String, - icon: Painter, - action: () -> Unit, - onBack: (() -> Unit)? = null, + fun Step( + currentStep: Int, + title: String, + instruction: String, + actionText: String, + icon: Painter, + action: () -> Unit, + onBack: (() -> Unit)? = null, content: @Composable () -> Unit = {} ) { - // Progress indicator - Row(Modifier.fillMaxWidth().padding(bottom = 24.dp), horizontalArrangement = Arrangement.SpaceEvenly) { - for (i in 1..8) { - Box( - modifier = Modifier - .height(6.dp) - .weight(1f) - .padding(horizontal = 4.dp) - .background( - if (i <= currentStep) MaterialTheme.colorScheme.primary - else MaterialTheme.colorScheme.surfaceVariant, - androidx.compose.foundation.shape.CircleShape - ) - ) + Column(Modifier.fillMaxSize()) { + // Progress indicator + Row(Modifier.fillMaxWidth().padding(bottom = 16.dp), horizontalArrangement = Arrangement.SpaceEvenly) { + for (i in 1..7) { + Box( + modifier = Modifier + .height(6.dp) + .weight(1f) + .padding(horizontal = 4.dp) + .background( + if (i <= currentStep) MaterialTheme.colorScheme.primary + else MaterialTheme.colorScheme.surfaceVariant, + androidx.compose.foundation.shape.CircleShape + ) + ) + } } - } - - androidx.compose.material3.ElevatedCard( - modifier = Modifier.fillMaxWidth(), - ) { - Column(Modifier.padding(24.dp)) { - Text(title, style = MaterialTheme.typography.titleLarge) - Spacer(Modifier.height(8.dp)) - Text(instruction, style = MaterialTheme.typography.bodyLarge, color = MaterialTheme.colorScheme.onSurfaceVariant) - - Spacer(Modifier.height(16.dp)) - content() - - Spacer(Modifier.height(24.dp)) - - // Action Buttons - Row( - Modifier.fillMaxWidth(), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.SpaceBetween - ) { - if (onBack != null) { - androidx.compose.material3.FilledTonalButton(onClick = onBack) { - Text(stringResource(R.string.setup_step_previous)) - } - } else { - androidx.compose.material3.TextButton(onClick = close) { - Text(stringResource(R.string.setup_skip_action)) + + androidx.compose.material3.ElevatedCard( + modifier = Modifier.fillMaxWidth().weight(1f), + ) { + Column(Modifier.padding(16.dp)) { + // keyed so that each page starts at its top + key(currentStep) { + Column(Modifier.weight(1f).verticalScroll(rememberScrollState())) { + Text(title, style = MaterialTheme.typography.titleLarge) + if (instruction.isNotEmpty()) { + Spacer(Modifier.height(8.dp)) + Text(instruction, style = MaterialTheme.typography.bodyLarge, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + Spacer(Modifier.height(16.dp)) + content() } } - - Spacer(Modifier.weight(1f)) - - androidx.compose.material3.FilledTonalButton(onClick = action) { - Icon(icon, null, Modifier.padding(end = 8.dp).size(20.dp)) - Text(actionText) + + Spacer(Modifier.height(12.dp)) + + // Action Buttons + Row( + Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.SpaceBetween + ) { + if (onBack != null) { + androidx.compose.material3.FilledTonalButton(onClick = onBack) { + Text(stringResource(R.string.setup_step_previous)) + } + } else { + androidx.compose.material3.TextButton(onClick = close) { + Text(stringResource(R.string.setup_skip_action)) + } + } + + Spacer(Modifier.weight(1f)) + + androidx.compose.material3.FilledTonalButton(onClick = action) { + Icon(icon, null, Modifier.padding(end = 8.dp).size(20.dp)) + Text(actionText) + } } } } @@ -197,7 +198,7 @@ fun WelcomeWizard( if (step == 0) Step0(onClick = { step = 1 }, onSkip = close) else - Column(Modifier.verticalScroll(rememberScrollState())) { + Column(Modifier.fillMaxSize()) { val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { step = determineStep() } @@ -227,7 +228,7 @@ fun WelcomeWizard( null ) } else if (step == 3) { - var showDialog by remember { mutableStateOf(false) } + var showDialog by rememberSaveable { mutableStateOf(false) } val allSubtypes = remember { SubtypeSettings.getAllAvailableSubtypes() } var enabledSubtypes by remember { mutableStateOf(SubtypeSettings.getEnabledSubtypes(true)) } @@ -245,9 +246,8 @@ fun WelcomeWizard( stringResource(R.string.setup_step3_title_languages), stringResource(R.string.setup_step3_instruction_languages), stringResource(R.string.next_step), - painterResource(R.drawable.sym_keyboard_language_switch), - { step++ }, - { step-- } + painterResource(R.drawable.ic_arrow_right), + { step++ } ) { WithSmallTitle(stringResource(R.string.setup_typing_languages_title)) { Column( @@ -290,6 +290,8 @@ fun WelcomeWizard( } } + SetupMainDictionaries(enabledSubtypes) + if (showDialog) { MultiListPickerDialog( onDismissRequest = { showDialog = false }, @@ -319,14 +321,14 @@ fun WelcomeWizard( stringResource(R.string.setup_step4_title_libraries), stringResource(R.string.setup_step4_instruction_libraries), stringResource(R.string.next_step), - painterResource(R.drawable.sym_keyboard_language_switch), + painterResource(R.drawable.ic_arrow_right), { step++ }, { step-- } ) { val trigger = refreshTrigger // Force recomposition val locale = helium314.keyboard.latin.RichInputMethodManager.getInstance().currentSubtype.locale val emojiLibInstalled = java.io.File(helium314.keyboard.latin.utils.DictionaryInfoUtils.getCacheDirectoryForLocale(locale, ctx), "emoji_${locale.language}.dict").exists() - val gestureLibInstalled = java.io.File(ctx.filesDir, "libjni_latinime.so").exists() || JniUtils.sHaveNativeGestureLib + val gestureLibInstalled = isGestureLibInstalled(ctx) Box(Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.surfaceVariant, MaterialTheme.shapes.medium)) { LoadEmojiLibPreference( @@ -385,6 +387,46 @@ fun WelcomeWizard( } } } else if (step == 5) { + Step( + 5, + stringResource(R.string.settings_screen_appearance), + "", + stringResource(R.string.next_step), + painterResource(R.drawable.ic_arrow_right), + { step++ }, + { step-- } + ) { + SetupSettings( + Settings.PREF_THEME_STYLE, + Settings.PREF_THEME_COLORS, + Settings.PREF_THEME_KEY_BORDERS, + Settings.PREF_KEYBOARD_HEIGHT_SCALE_PREFIX, + Settings.PREF_SHOW_NUMBER_ROW, + Settings.PREF_POPUP_ON, + Settings.PREF_VIBRATE_ON, + Settings.PREF_SOUND_ON, + ) + } + } else if (step == 6) { + Step( + 6, + stringResource(R.string.settings_screen_correction), + "", + stringResource(R.string.next_step), + painterResource(R.drawable.ic_arrow_right), + { step++ }, + { step-- } + ) { + SetupSettings( + Settings.PREF_AUTO_CORRECTION, + Settings.PREF_BACKSPACE_REVERTS_AUTOCORRECT, + Settings.PREF_SHOW_SUGGESTIONS, + Settings.PREF_AUTO_CAP, + Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD, + Settings.PREF_SUGGEST_SCREENSHOTS, + ) + } + } else { // last page val stepTitle = when (BuildConfig.FLAVOR) { "offline" -> stringResource(R.string.setup_step5_title_offline_ai) else -> stringResource(R.string.setup_step5_title_cloud_ai) @@ -395,12 +437,22 @@ fun WelcomeWizard( } Step( - 5, + 7, stepTitle, stepInstruction, - stringResource(R.string.next_step), - painterResource(R.drawable.sym_keyboard_language_switch), - { step++ }, + stringResource(R.string.setup_finish_action), + painterResource(R.drawable.ic_setup_check), + { + if (SubtypeSettings.getEnabledSubtypes(false).isEmpty()) { + SubtypeSettings.getEnabledSubtypes(true).forEach { subtype -> + SubtypeSettings.addEnabledSubtype(ctx.prefs(), subtype) + } + } + finish() + if (requiresRestart) { + Runtime.getRuntime().exit(0) + } + }, { step-- } ) { if (BuildConfig.FLAVOR == "standard" || BuildConfig.FLAVOR == "standardfull") { @@ -461,6 +513,7 @@ fun WelcomeWizard( } else if (BuildConfig.FLAVOR == "offline") { val service = remember { helium314.keyboard.latin.utils.ProofreadService(ctx) } var modelPath by remember { mutableStateOf(service.getModelPath()) } + val trigger = refreshTrigger // redraw after the plugin is loaded or deleted val hasPlugin = helium314.keyboard.latin.ai.OfflineAiLoader.hasPlugin(ctx) val pluginVersion = helium314.keyboard.latin.ai.OfflineAiLoader.getPluginVersion(ctx) @@ -485,7 +538,12 @@ fun WelcomeWizard( title = stringResource(R.string.load_offline_ai_plugin), summary = if (hasPlugin) stringResource(R.string.setup_offline_ai_plugin_active, pluginVersion ?: "1.0") else stringResource(R.string.setup_offline_ai_plugin_needed), icon = R.drawable.ic_proofread, - onSuccess = { refreshTrigger++ } + // restarting now would drop the user out of the wizard + restartOnSuccess = false, + onSuccess = { + requiresRestart = true + refreshTrigger++ + } ) if (hasPlugin) { Icon(painterResource(R.drawable.ic_setup_check), null, Modifier.align(Alignment.CenterEnd).padding(end = 16.dp), tint = MaterialTheme.colorScheme.primary) @@ -540,111 +598,33 @@ fun WelcomeWizard( } } } - } - } else if (step == 6) { - Step( - 6, - stringResource(R.string.setup_step6_title_screenshots), - stringResource(R.string.setup_step6_instruction_screenshots), - stringResource(R.string.next_step), - painterResource(R.drawable.sym_keyboard_language_switch), - { step++ }, - { step-- } - ) { - val trigger = refreshTrigger - val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - Manifest.permission.READ_MEDIA_IMAGES - } else { - Manifest.permission.READ_EXTERNAL_STORAGE - } - val granted = PermissionsUtil.checkAllPermissionsGranted(ctx.getActivity(), permission) - val screenshotsEnabled = ctx.prefs().getBoolean(Settings.PREF_SUGGEST_SCREENSHOTS, Defaults.PREF_SUGGEST_SCREENSHOTS) - Box(Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.surfaceVariant, MaterialTheme.shapes.medium)) { - helium314.keyboard.settings.Setting(ctx, Settings.PREF_SUGGEST_SCREENSHOTS, R.string.suggest_screenshots, R.string.suggest_screenshots_summary) { setting -> - val activity = ctx.getActivity() ?: return@Setting - var isGranted by remember { mutableStateOf(PermissionsUtil.checkAllPermissionsGranted(activity, permission)) } - val permLauncher = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { - isGranted = it - if (isGranted) - activity.prefs().edit { putBoolean(setting.key, true) } - refreshTrigger++ - } - SwitchPreference(setting, Defaults.PREF_SUGGEST_SCREENSHOTS, allowCheckedChange = { - if (it && !isGranted) { - permLauncher.launch(permission) - false - } else true - }) - }.Preference() - } - } - } else if (step == 7) { - Step( - 7, - stringResource(R.string.setup_step7_title_height), - stringResource(R.string.setup_step7_instruction_height), - stringResource(R.string.next_step), - painterResource(R.drawable.sym_keyboard_language_switch), - { step++ }, - { step-- } - ) { - val trigger = refreshTrigger - val portraitHeightKey = createPrefKeyForBooleanSettings(Settings.PREF_KEYBOARD_HEIGHT_SCALE_PREFIX, 0, 1) - val landscapeHeightKey = createPrefKeyForBooleanSettings(Settings.PREF_KEYBOARD_HEIGHT_SCALE_PREFIX, 1, 1) - val heightSet = ctx.prefs().contains(portraitHeightKey) || ctx.prefs().contains(landscapeHeightKey) - - Box(Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.surfaceVariant, MaterialTheme.shapes.medium)) { - helium314.keyboard.settings.Setting(ctx, Settings.PREF_KEYBOARD_HEIGHT_SCALE_PREFIX, R.string.prefs_keyboard_height_scale) { setting -> - MultiSliderPreference( - name = setting.title, - baseKey = setting.key, - dimensions = listOf(stringResource(R.string.landscape)), - defaults = Defaults.PREF_KEYBOARD_HEIGHT_SCALE, - range = 0.3f..1.5f, - description = { "${(100 * it).roundToInt()}%" }, - defaultVariantName = stringResource(R.string.portrait) - ) { - refreshTrigger++ - } - }.Preference() - - if (heightSet) { - Icon(painterResource(R.drawable.ic_setup_check), null, Modifier.align(Alignment.TopEnd).padding(top = 16.dp, end = 16.dp), tint = MaterialTheme.colorScheme.primary) + // leaving the wizard here would skip the restart that loads the gesture library + if (!requiresRestart) { + Spacer(Modifier.height(8.dp)) + Box(Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.surfaceVariant, MaterialTheme.shapes.medium)) { + Preference( + name = stringResource(R.string.libraries_hub_title), + onClick = { + close() + SettingsDestination.navigateTo(SettingsDestination.Libraries) + }, + icon = R.drawable.ic_emoji_objects + ) } } } - } else { // step 8 - Step( - 8, - stringResource(R.string.setup_step3_title), - stringResource(R.string.setup_step3_instruction, appName), - stringResource(R.string.setup_finish_action), - painterResource(R.drawable.ic_setup_check), - { - if (SubtypeSettings.getEnabledSubtypes(false).isEmpty()) { - SubtypeSettings.getEnabledSubtypes(true).forEach { subtype -> - SubtypeSettings.addEnabledSubtype(ctx.prefs(), subtype) - } - } - finish() - if (requiresRestart) { - Runtime.getRuntime().exit(0) - } - }, - { step-- } - ) } } } Surface(color = MaterialTheme.colorScheme.background) { Box( - modifier = Modifier.fillMaxSize().padding(32.dp), - contentAlignment = Alignment.Center + modifier = Modifier.fillMaxSize().padding(horizontal = 20.dp, vertical = 16.dp), + contentAlignment = Alignment.TopCenter ) { if (useWideLayout) Row { - Box(Modifier.weight(0.4f)) { + Box(Modifier.weight(0.4f).padding(end = 16.dp)) { bigText() } Box(Modifier.weight(0.6f)) { @@ -660,6 +640,89 @@ fun WelcomeWizard( } } +/** download rows for the chosen languages that have no bundled main dictionary */ +@Composable +private fun SetupMainDictionaries(subtypes: List) { + val ctx = LocalContext.current + var refreshTrigger by remember { mutableIntStateOf(0) } + val picker = dictionaryFilePicker(null) + val dictionaries = remember(subtypes) { + val bundled = DictionaryInfoUtils.getAssetsDictionaryList(ctx).orEmpty() + .filter { it.startsWith(DictionaryInfoUtils.MAIN_DICT_PREFIX) } + subtypes.map { it.locale() }.distinct() + .filter { locale -> + LocaleUtils.getBestMatch(locale, bundled) { DictionaryInfoUtils.extractLocaleFromAssetsDictionaryFile(it) } == null + } + .flatMap { locale -> + val known = getKnownDictionariesForLocale(locale, ctx) + .filter { it.second.substringAfterLast("/").startsWith(DictionaryInfoUtils.MAIN_DICT_PREFIX) } + // only the closest region: English (US) should not be offered the UK dictionary + fun level(link: String) = LocaleUtils.getMatchLevel( + locale, + link.substringAfterLast("/").substringAfter("_").substringBefore(".dict").substringBefore("__").constructLocale() + ) + val closest = known.maxOfOrNull { level(it.second) } + known.filter { level(it.second) == closest }.map { Triple(locale, it.first, it.second) } + } + .distinctBy { it.third } + } + if (dictionaries.isEmpty()) return + Spacer(Modifier.height(12.dp)) + Column( + Modifier.fillMaxWidth() + .background(MaterialTheme.colorScheme.surfaceVariant, MaterialTheme.shapes.medium) + .padding(horizontal = 12.dp, vertical = 8.dp) + ) { + dictionaries.forEach { (locale, description, link) -> + key(link) { + DownloadableDictionaryRow(locale, description, link, refreshTrigger) { refreshTrigger++ } + } + } + // imported files are separate from the repository ones the rows above track + subtypes.map { it.locale() }.distinct().forEach { locale -> + val imported = DictionaryInfoUtils.getCacheDirectoryForLocale(locale, ctx)?.let { + File(it, DictionaryInfoUtils.MAIN_DICT_PREFIX + DictionaryInfoUtils.USER_DICTIONARY_SUFFIX).exists() + } == true + if (imported) { + Column(Modifier.padding(vertical = 6.dp, horizontal = 4.dp)) { + Text( + "${stringResource(R.string.main_dictionary)}: ${locale.localizedDisplayName(ctx.resources)}", + style = MaterialTheme.typography.bodyMedium + ) + Text( + stringResource(R.string.installed), + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.primary + ) + } + } + } + // offline downloads in the browser, so the file has to be imported from here + if (BuildConfig.FLAVOR == "offline") { + androidx.compose.material3.OutlinedButton( + onClick = { + picker.launch( + Intent(Intent.ACTION_OPEN_DOCUMENT) + .addCategory(Intent.CATEGORY_OPENABLE) + .setType("application/octet-stream") + ) + }, + modifier = Modifier.fillMaxWidth() + ) { + Text(stringResource(R.string.button_load_from_file)) + } + } + } +} + +/** the same preferences as on the settings screens, grouped on one background */ +@Composable +private fun SetupSettings(vararg keys: String) { + Column(Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.surfaceVariant, MaterialTheme.shapes.medium)) { + keys.forEach { SettingsActivity.settingsContainer[it]?.Preference() } + } +} + @Composable fun Step0(onClick: () -> Unit, onSkip: () -> Unit = {}) { Column( diff --git a/app/src/main/java/helium314/keyboard/settings/preferences/LoadCjkPluginPreference.kt b/app/src/main/java/helium314/keyboard/settings/preferences/LoadCjkPluginPreference.kt index 108665276..68bc61fd3 100644 --- a/app/src/main/java/helium314/keyboard/settings/preferences/LoadCjkPluginPreference.kt +++ b/app/src/main/java/helium314/keyboard/settings/preferences/LoadCjkPluginPreference.kt @@ -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(null) } var isCheckingUpdate by remember { mutableStateOf(false) } diff --git a/app/src/main/java/helium314/keyboard/settings/preferences/LoadEmojiLibPreference.kt b/app/src/main/java/helium314/keyboard/settings/preferences/LoadEmojiLibPreference.kt index 037e9dda5..c5e0bc0fb 100644 --- a/app/src/main/java/helium314/keyboard/settings/preferences/LoadEmojiLibPreference.kt +++ b/app/src/main/java/helium314/keyboard/settings/preferences/LoadEmojiLibPreference.kt @@ -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 @@ -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() @@ -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) { @@ -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() diff --git a/app/src/main/java/helium314/keyboard/settings/preferences/LoadGestureLibPreference.kt b/app/src/main/java/helium314/keyboard/settings/preferences/LoadGestureLibPreference.kt index eefbb1a2e..c6872e628 100644 --- a/app/src/main/java/helium314/keyboard/settings/preferences/LoadGestureLibPreference.kt +++ b/app/src/main/java/helium314/keyboard/settings/preferences/LoadGestureLibPreference.kt @@ -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 @@ -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 @@ -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() @@ -88,6 +101,8 @@ fun LoadGestureLibPreference( putBoolean(Settings.PREF_GESTURE_INPUT, true) } file.copyTo(libFile) + removedUntilRestart = false + libChanges++ libFile.setReadOnly() file.delete() try { @@ -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 } ) @@ -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), @@ -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 @@ -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) ?: "") } ) diff --git a/app/src/main/java/helium314/keyboard/settings/preferences/LoadHandwritingPluginPreference.kt b/app/src/main/java/helium314/keyboard/settings/preferences/LoadHandwritingPluginPreference.kt index 48b249a46..fa830db75 100644 --- a/app/src/main/java/helium314/keyboard/settings/preferences/LoadHandwritingPluginPreference.kt +++ b/app/src/main/java/helium314/keyboard/settings/preferences/LoadHandwritingPluginPreference.kt @@ -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(null) } var updateAvailable by remember { mutableStateOf(false) } diff --git a/app/src/main/java/helium314/keyboard/settings/preferences/LoadOcrPluginPreference.kt b/app/src/main/java/helium314/keyboard/settings/preferences/LoadOcrPluginPreference.kt index db3703172..00f1f0311 100644 --- a/app/src/main/java/helium314/keyboard/settings/preferences/LoadOcrPluginPreference.kt +++ b/app/src/main/java/helium314/keyboard/settings/preferences/LoadOcrPluginPreference.kt @@ -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(null) } var isCheckingUpdate by remember { mutableStateOf(false) } diff --git a/app/src/main/java/helium314/keyboard/settings/preferences/LoadOfflineAiPluginPreference.kt b/app/src/main/java/helium314/keyboard/settings/preferences/LoadOfflineAiPluginPreference.kt index 95069d1e7..b1fa98e2f 100644 --- a/app/src/main/java/helium314/keyboard/settings/preferences/LoadOfflineAiPluginPreference.kt +++ b/app/src/main/java/helium314/keyboard/settings/preferences/LoadOfflineAiPluginPreference.kt @@ -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(null) } var updateAvailable by remember { mutableStateOf(false) } @@ -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) { @@ -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) { @@ -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) { diff --git a/app/src/main/java/helium314/keyboard/settings/preferences/LoadTranslationPluginPreference.kt b/app/src/main/java/helium314/keyboard/settings/preferences/LoadTranslationPluginPreference.kt index 0b0038279..fd8d0ea17 100644 --- a/app/src/main/java/helium314/keyboard/settings/preferences/LoadTranslationPluginPreference.kt +++ b/app/src/main/java/helium314/keyboard/settings/preferences/LoadTranslationPluginPreference.kt @@ -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(null) } var updateAvailable by remember { mutableStateOf(false) } diff --git a/app/src/main/java/helium314/keyboard/settings/screens/MainSettingsScreen.kt b/app/src/main/java/helium314/keyboard/settings/screens/MainSettingsScreen.kt index 5b90c4bd3..57e8c115d 100644 --- a/app/src/main/java/helium314/keyboard/settings/screens/MainSettingsScreen.kt +++ b/app/src/main/java/helium314/keyboard/settings/screens/MainSettingsScreen.kt @@ -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 }, @@ -138,7 +139,7 @@ fun MainSettingsScreen( ) } - if (showSponsorDialog) { + if (showSponsorDialog && !wizardShowing) { LaunchedEffect(Unit) { prefs.edit { putLong(Settings.PREF_LAST_SPONSOR_DIALOG_SHOWN, System.currentTimeMillis()) } }