Skip to content
Closed
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
13 changes: 13 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />

<!-- no INTERNET permission here on purpose: the external Dictionnary app downloads the
auto-correct language files and shares them with us through content URIs -->
<uses-permission android:name="org.fossify.dictionnary.ACCESS_DICTIONARIES" />

<queries>
<provider android:authorities="org.fossify.dictionnary.provider" />
<package android:name="org.fossify.dictionnary" />
<package android:name="org.fossify.dictionnary.debug" />
<intent>
<action android:name="org.fossify.dictionnary.action.DOWNLOAD_DICTIONARY" />
</intent>
</queries>

<application
android:name="org.fossify.keyboard.App"
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ import org.fossify.commons.extensions.getContrastColor
import org.fossify.commons.extensions.getProperPrimaryColor
import org.fossify.commons.extensions.hideKeyboard
import org.fossify.commons.extensions.launchMoreAppsFromUsIntent
import org.fossify.commons.extensions.toast
import org.fossify.commons.extensions.updateTextColors
import org.fossify.commons.extensions.viewBinding
import org.fossify.commons.helpers.LICENSE_GSON
import org.fossify.commons.models.FAQItem
import org.fossify.keyboard.BuildConfig
import org.fossify.keyboard.R
import org.fossify.keyboard.databinding.ActivityMainBinding
import org.fossify.keyboard.extensions.config
import org.fossify.keyboard.extensions.inputMethodManager
import org.fossify.keyboard.helpers.DICTIONNARY_EXTRA_LANG
import org.fossify.keyboard.helpers.DictionnaryClient
import org.fossify.keyboard.helpers.dictionaryLangForKeyboardLanguage

class MainActivity : SimpleActivity() {
private val binding by viewBinding(ActivityMainBinding::inflate)

companion object {
private const val REQUEST_DOWNLOAD_DICTIONARY = 1001
private val REQUEST_PICK_DICTIONNARY_APK = DictionnaryClient.REQUEST_PICK_DICTIONNARY_APK
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
Expand Down Expand Up @@ -61,11 +71,59 @@ class MainActivity : SimpleActivity() {
}
}
}
maybeProposeDictionaryLanguage()

updateTextColors(binding.mainNestedScrollview)
updateChangeKeyboardColor()
}

/**
* At launch, in addition to asking to add the keyboard to the enabled input methods,
* ask the user to install the dictionary of the current keyboard language: launch the
* Dictionnary app's download flow when it is installed, otherwise propose to install
* that app. Asked only once, and again later if the dictionary is missing again.
*/
private fun maybeProposeDictionaryLanguage() {
if (!config.showSuggestions && !config.autoCorrectOnSpace) {
return
}
val lang = dictionaryLangForKeyboardLanguage(config.keyboardLanguage) ?: return
if (DictionnaryClient.isLocalDictionaryAvailable(this, lang)) {
config.wasDictionaryLanguageProposed = false
return
}
if (config.wasDictionaryLanguageProposed) {
return
}
config.wasDictionaryLanguageProposed = true
if (DictionnaryClient.isDictionnaryInstalled(this)) {
DictionnaryClient.launchDictionaryDownload(this, lang, REQUEST_DOWNLOAD_DICTIONARY)
} else {
DictionnaryClient.proposeInstallDictionnary(this, lang, REQUEST_PICK_DICTIONNARY_APK)
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
when (requestCode) {
REQUEST_DOWNLOAD_DICTIONARY -> {
val lang = resultData?.getStringExtra(DICTIONNARY_EXTRA_LANG)
val file = DictionnaryClient.handleDownloadResult(this, resultCode, resultData)
if (file != null) {
toast(getString(R.string.dictionary_downloaded_ok, lang.orEmpty()))
} else if (lang != null) {
toast(getString(R.string.dictionary_download_failed, lang))
}
}

REQUEST_PICK_DICTIONNARY_APK -> {
if (resultCode == RESULT_OK) {
resultData?.data?.let { DictionnaryClient.installApkFromUri(this, it) }
}
}
}
}

private fun setupOptionsMenu() {
binding.mainToolbar.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,33 @@ import org.fossify.keyboard.helpers.KEYBOARD_HEIGHT_100_PERCENT
import org.fossify.keyboard.helpers.KEYBOARD_HEIGHT_120_PERCENT
import org.fossify.keyboard.helpers.KEYBOARD_HEIGHT_140_PERCENT
import org.fossify.keyboard.helpers.KEYBOARD_HEIGHT_160_PERCENT
import org.fossify.keyboard.helpers.DEFAULT_DICTIONARY_LANG
import org.fossify.keyboard.helpers.DICTIONNARY_EXTRA_LANG
import org.fossify.keyboard.helpers.DictionnaryClient
import org.fossify.keyboard.helpers.KEYBOARD_HEIGHT_70_PERCENT
import org.fossify.keyboard.helpers.KEYBOARD_HEIGHT_80_PERCENT
import org.fossify.keyboard.helpers.KEYBOARD_HEIGHT_90_PERCENT
import org.fossify.keyboard.helpers.SOUND_ALWAYS
import org.fossify.keyboard.helpers.SOUND_NONE
import org.fossify.keyboard.helpers.SOUND_SYSTEM
import org.fossify.keyboard.helpers.dictionaryLangForKeyboardLanguage
import java.util.Locale
import kotlin.system.exitProcess

class SettingsActivity : SimpleActivity() {
private val binding by viewBinding(ActivitySettingsBinding::inflate)

companion object {
private const val REQUEST_DOWNLOAD_DICTIONARY = 1001
private val REQUEST_PICK_DICTIONNARY_APK = DictionnaryClient.REQUEST_PICK_DICTIONNARY_APK
}

private var installProposalShown = false

// languages a download was already attempted for during this settings visit, so a failing
// download is not relaunched in a loop from onActivityResult
private val dictionaryDownloadAttempts = mutableSetOf<String>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
Expand Down Expand Up @@ -66,7 +81,11 @@ class SettingsActivity : SimpleActivity() {
setupShowClipboardContent()
setupSentencesCapitalization()
setupShowNumbersRow()
setupShowSuggestions()
setupAutoCorrect()
setupDictionnaryStatus()
setupVoiceInputMethod()
maybeProposeDictionnaryInstall()

binding.apply {
updateTextColors(settingsNestedScrollview)
Expand Down Expand Up @@ -186,11 +205,113 @@ class SettingsActivity : SimpleActivity() {
settingsManageKeyboardLanguagesHolder.setOnClickListener {
ManageKeyboardLanguagesDialog(this@SettingsActivity) {
settingsKeyboardLanguage.text = getKeyboardLanguageText(config.keyboardLanguage)
ensureDictionariesForSelectedLanguages()
}
}
}
}

/**
* For every selected keyboard language that has a dictionary but no local copy yet,
* launches the Dictionnary app to download it (or proposes to install that app).
*/
private fun ensureDictionariesForSelectedLanguages() {
val missingLangs = config.selectedLanguages
.mapNotNull { dictionaryLangForKeyboardLanguage(it) }
.distinct()
.filter { !DictionnaryClient.isLocalDictionaryAvailable(this, it) }
if (missingLangs.isNotEmpty()) {
ensureDictionary(missingLangs.first())
}
}

private fun ensureDictionaryForCurrentLanguage() {
val lang = dictionaryLangForKeyboardLanguage(config.keyboardLanguage) ?: return
if (!DictionnaryClient.isLocalDictionaryAvailable(this, lang)) {
ensureDictionary(lang)
}
}

private fun ensureDictionary(lang: String) {
if (!dictionaryDownloadAttempts.add(lang)) {
return
}
if (DictionnaryClient.isDictionnaryInstalled(this)) {
DictionnaryClient.launchDictionaryDownload(this, lang, REQUEST_DOWNLOAD_DICTIONARY)
} else {
DictionnaryClient.proposeInstallDictionnary(this, lang, REQUEST_PICK_DICTIONNARY_APK)
}
}

/** settings row showing whether the Dictionnary app is installed, tap to install/open it */
private fun setupDictionnaryStatus() {
binding.apply {
val installed = DictionnaryClient.isDictionnaryInstalled(this@SettingsActivity)
settingsDictionnaryValue.text = getString(
if (installed) R.string.dictionnary_installed else R.string.dictionnary_not_installed
)
settingsDictionnaryHolder.setOnClickListener {
val lang = dictionaryLangForKeyboardLanguage(config.keyboardLanguage)
when {
!installed -> {
if (lang != null) {
DictionnaryClient.proposeInstallDictionnary(this@SettingsActivity, lang, REQUEST_PICK_DICTIONNARY_APK)
} else {
DictionnaryClient.proposeInstallDictionnary(this@SettingsActivity, DEFAULT_DICTIONARY_LANG, REQUEST_PICK_DICTIONNARY_APK)
}
}

lang != null && !DictionnaryClient.isLocalDictionaryAvailable(this@SettingsActivity, lang) ->
ensureDictionary(lang)

else -> DictionnaryClient.openDictionnaryApp(this@SettingsActivity)
}
}
}
}

/**
* If suggestions or auto-correct are enabled but the dictionary is missing and the
* Dictionnary app is not installed, propose to install it (once per settings visit).
*/
private fun maybeProposeDictionnaryInstall() {
if (installProposalShown) {
return
}
if (!config.showSuggestions && !config.autoCorrectOnSpace) {
return
}
val lang = dictionaryLangForKeyboardLanguage(config.keyboardLanguage) ?: return
if (DictionnaryClient.isLocalDictionaryAvailable(this, lang)) {
return
}
if (DictionnaryClient.isDictionnaryInstalled(this)) {
return
}
installProposalShown = true
DictionnaryClient.proposeInstallDictionnary(this, lang, REQUEST_PICK_DICTIONNARY_APK)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == REQUEST_DOWNLOAD_DICTIONARY) {
val lang = resultData?.getStringExtra(DICTIONNARY_EXTRA_LANG)
val file = DictionnaryClient.handleDownloadResult(this, resultCode, resultData)
if (file != null) {
toast(getString(R.string.dictionary_downloaded_ok, lang.orEmpty()))
} else if (lang != null) {
toast(getString(R.string.dictionary_download_failed, lang))
}
// continue with the other missing languages, if any
ensureDictionariesForSelectedLanguages()
} else if (requestCode == REQUEST_PICK_DICTIONNARY_APK && resultCode == RESULT_OK) {
resultData?.data?.let {
// hand the picked APK to the system package installer
DictionnaryClient.installApkFromUri(this, it)
}
}
}

private fun setupKeyboardLanguage() {
binding.apply {
settingsKeyboardLanguage.text = getKeyboardLanguageText(config.keyboardLanguage)
Expand Down Expand Up @@ -310,6 +431,32 @@ class SettingsActivity : SimpleActivity() {
}
}

private fun setupShowSuggestions() {
binding.apply {
settingsShowSuggestions.isChecked = config.showSuggestions
settingsShowSuggestionsHolder.setOnClickListener {
settingsShowSuggestions.toggle()
config.showSuggestions = settingsShowSuggestions.isChecked
if (settingsShowSuggestions.isChecked) {
ensureDictionaryForCurrentLanguage()
}
}
}
}

private fun setupAutoCorrect() {
binding.apply {
settingsAutoCorrect.isChecked = config.autoCorrectOnSpace
settingsAutoCorrectHolder.setOnClickListener {
settingsAutoCorrect.toggle()
config.autoCorrectOnSpace = settingsAutoCorrect.isChecked
if (settingsAutoCorrect.isChecked) {
ensureDictionaryForCurrentLanguage()
}
}
}
}

private fun setupVoiceInputMethod() {
binding.apply {
settingsVoiceInputMethodValue.text =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import org.fossify.commons.extensions.lightenColor
import org.fossify.keyboard.R

fun Context.getKeyboardBackgroundColor(): Int {
val color = if (isDynamicTheme()) {
// use safeStorageContext: these helpers also run inside the directBootAware IME service,
// where credential encrypted SharedPreferences are not available before user unlock
val color = if (safeStorageContext.isDynamicTheme()) {
resources.getColor(R.color.you_keyboard_background_color, theme)
} else {
getProperBackgroundColor().darkenColor(2)
safeStorageContext.getProperBackgroundColor().darkenColor(2)
}

// use darker background color when key borders are enabled
Expand All @@ -30,7 +32,7 @@ fun Context.getKeyboardBackgroundColor(): Int {
}

fun Context.getStrokeColor(): Int {
return if (isDynamicTheme()) {
return if (safeStorageContext.isDynamicTheme()) {
if (isSystemInDarkMode()) {
resources.getColor(R.color.md_grey_800, theme)
} else {
Expand Down
49 changes: 43 additions & 6 deletions app/src/main/kotlin/org/fossify/keyboard/helpers/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,52 @@ class Config(context: Context) : BaseConfig(context) {
prefs.edit().putStringSet(SELECTED_LANGUAGES, stringSet).apply()
}

fun getDefaultLanguage(): Int {
val conf = context.resources.configuration
return if (conf.locale.toString().lowercase(Locale.getDefault()).startsWith("ru_")) {
LANGUAGE_RUSSIAN
} else {
LANGUAGE_ENGLISH_QWERTY
fun getDefaultLanguage(): Int = languageForLocale(context.resources.configuration.locale)

/** maps the system locale to the closest supported keyboard language (English QWERTY as fallback) */
private fun languageForLocale(locale: Locale): Int {
val country = locale.country.uppercase(Locale.getDefault())
return when (locale.language.lowercase(Locale.getDefault())) {
"fr" -> LANGUAGE_FRENCH_AZERTY
"de" -> if (country == "DE") LANGUAGE_GERMAN else LANGUAGE_GERMAN_QWERTZ
"es" -> LANGUAGE_SPANISH
"it" -> LANGUAGE_ITALIAN
"pt" -> LANGUAGE_PORTUGUESE
"nl" -> LANGUAGE_DUTCH
"pl" -> LANGUAGE_POLISH
"sv" -> LANGUAGE_SWEDISH
"da" -> LANGUAGE_DANISH
"nb", "no" -> LANGUAGE_NORWEGIAN
"ru" -> LANGUAGE_RUSSIAN
"uk" -> LANGUAGE_UKRAINIAN
"cs" -> LANGUAGE_CZECH_QWERTY
"tr" -> LANGUAGE_TURKISH
"el" -> LANGUAGE_GREEK
"he" -> LANGUAGE_HEBREW
"ar" -> LANGUAGE_ARABIC
"bn" -> LANGUAGE_BENGALI
"lv" -> LANGUAGE_LATVIAN
"lt" -> LANGUAGE_LITHUANIAN
"ro" -> LANGUAGE_ROMANIAN
"sl" -> LANGUAGE_SLOVENIAN
"bg" -> LANGUAGE_BULGARIAN
"eo" -> LANGUAGE_ESPERANTO
else -> LANGUAGE_ENGLISH_QWERTY
}
}

var wasDictionaryLanguageProposed: Boolean
get() = prefs.getBoolean(DICTIONARY_LANGUAGE_PROPOSED, false)
set(wasDictionaryLanguageProposed) = prefs.edit().putBoolean(DICTIONARY_LANGUAGE_PROPOSED, wasDictionaryLanguageProposed).apply()

var showSuggestions: Boolean
get() = prefs.getBoolean(SHOW_SUGGESTIONS, false)
set(showSuggestions) = prefs.edit().putBoolean(SHOW_SUGGESTIONS, showSuggestions).apply()

var autoCorrectOnSpace: Boolean
get() = prefs.getBoolean(AUTO_CORRECT_ON_SPACE, false)
set(autoCorrectOnSpace) = prefs.edit().putBoolean(AUTO_CORRECT_ON_SPACE, autoCorrectOnSpace).apply()

var recentlyUsedEmojis: List<String>
get() = prefs.getString(RECENTLY_USED_EMOJIS, "\uD83D\uDC30")!!.split("|").filter { it.isNotEmpty() }
set(recentlyUsedEmojis) = prefs.edit().putString(
Expand Down
Loading
Loading