Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,10 @@ public void showToast(final String text, final boolean briefToast){
}
}

public void closeDictionaries() {
mLatinIME.closeDictionaries();
}

private static int getSecondaryStripVisibility() {
return Settings.getValues().mSecondaryStripVisible? View.VISIBLE : View.GONE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import helium314.keyboard.keyboard.internal.KeyVisualAttributes;
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode;
import helium314.keyboard.latin.AudioAndHapticFeedbackManager;
import helium314.keyboard.latin.Dictionary;
import helium314.keyboard.latin.DictionaryFactory;
import helium314.keyboard.latin.R;
import helium314.keyboard.latin.RichInputMethodManager;
Expand Down Expand Up @@ -452,7 +453,7 @@ private void initDictionaryFacilitator() {
var locale = RichInputMethodManager.getInstance().getCurrentSubtype().getLocale();
if (sDictionaryFacilitator == null || ! sDictionaryFacilitator.isForLocale(locale)) {
closeDictionaryFacilitator();
var dictFile = DictionaryInfoUtils.getCachedDictForLocaleAndType(locale, "emoji", getContext());
var dictFile = DictionaryInfoUtils.getCachedDictForLocaleAndType(locale, Dictionary.TYPE_EMOJI, getContext());
var dictionary = dictFile != null? DictionaryFactory.getDictionary(dictFile, locale) : null;
sDictionaryFacilitator = dictionary != null? new SingleDictionaryFacilitator(dictionary) : null;
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/helium314/keyboard/latin/Dictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public abstract class Dictionary {
public static final String TYPE_USER = "user";
// User history dictionary internal to LatinIME.
public static final String TYPE_USER_HISTORY = "history";
public static final String TYPE_EMOJI = "emoji";
public final String mDictType;
// The locale for this dictionary. May be null if unknown (phony dictionary for example).
public final Locale mLocale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void resetDictionaries(
final boolean useContactsDict,
final boolean useAppsDict,
final boolean usePersonalizedDicts,
boolean useEmojiDict,
final boolean forceReloadMainDictionary,
final String dictNamePrefix,
@Nullable final DictionaryInitializationListener listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class DictionaryFacilitatorImpl : DictionaryFacilitator {
useContactsDict: Boolean,
useAppsDict: Boolean,
usePersonalizedDicts: Boolean,
useEmojiDict: Boolean,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer reading the setting as part of resetDictionaries, instead of putting useEmojiDict in the interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the pattern of passing the other settings. Why should it be done differently?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other settings are checked in usesSameSettings to check whether a reload is necessary. This is not done with useEmojiDict, here a completely different way of reloading is used (resetDictionaryFacilitatorIfNecessary).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Done.

forceReloadMainDictionary: Boolean,
dictNamePrefix: String,
listener: DictionaryInitializationListener?
Expand All @@ -148,7 +149,7 @@ class DictionaryFacilitatorImpl : DictionaryFacilitator {
oldDictionaryGroups = dictionaryGroups
dictionaryGroups = newDictionaryGroups
if (hasAtLeastOneUninitializedMainDictionary()) {
asyncReloadUninitializedMainDictionaries(context, locales, listener)
asyncReloadUninitializedMainDictionaries(context, locales, useEmojiDict, listener)
}
}

Expand Down Expand Up @@ -223,7 +224,7 @@ class DictionaryFacilitatorImpl : DictionaryFacilitator {
}

private fun asyncReloadUninitializedMainDictionaries(
context: Context, locales: Collection<Locale>, listener: DictionaryInitializationListener?
context: Context, locales: Collection<Locale>, useEmojiDict: Boolean, listener: DictionaryInitializationListener?
) {
val latchForWaitingLoadingMainDictionary = CountDownLatch(1)
mLatchForWaitingLoadingMainDictionaries = latchForWaitingLoadingMainDictionary
Expand All @@ -236,7 +237,7 @@ class DictionaryFacilitatorImpl : DictionaryFacilitator {
return@mapNotNull null // This should never happen
}
if (dictionaryGroup.getDict(Dictionary.TYPE_MAIN)?.isInitialized == true) null
else dictionaryGroup to DictionaryFactory.createMainDictionaryCollection(context, it)
else dictionaryGroup to DictionaryFactory.createMainDictionaryCollection(context, it, useEmojiDict)
}
synchronized(this) {
dictGroupsWithNewMainDict.forEach { (dictGroup, mainDict) ->
Expand Down Expand Up @@ -532,7 +533,7 @@ class DictionaryFacilitatorImpl : DictionaryFacilitator {
)
continue

if (word.length == 1 && info.mSourceDict.mDictType == "emoji" && !StringUtils.mightBeEmoji(word[0].code))
if (word.length == 1 && info.mSourceDict.mDictType == Dictionary.TYPE_EMOJI && !StringUtils.mightBeEmoji(word[0].code))
continue

suggestions.add(info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void resetDictionariesForLocaleLocked() {
if (mLocale != null) {
// Note: Given that personalized dictionaries are not used here; we can pass null account.
mDictionaryFacilitator.resetDictionaries(mContext, mLocale, mUseContactsDictionary,
mUseAppsDictionary, false, false, mDictionaryNamePrefix, null);
mUseAppsDictionary, false, false, false, mDictionaryNamePrefix, null);
}
}

Expand Down
15 changes: 8 additions & 7 deletions app/src/main/java/helium314/keyboard/latin/DictionaryFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ object DictionaryFactory {
// todo:
// expose the weight so users can adjust dictionary "importance" (useful for addons like emoji dict)
// allow users to block certain dictionaries (not sure how this should work exactly)
fun createMainDictionaryCollection(context: Context, locale: Locale): DictionaryCollection {
fun createMainDictionaryCollection(context: Context, locale: Locale, useEmojiDict: Boolean): DictionaryCollection {
val dictList = LinkedList<Dictionary>()
val (extracted, nonExtracted) = getAvailableDictsForLocale(locale, context)
val (extracted, nonExtracted) = getAvailableDictsForLocale(locale, context, useEmojiDict)
extracted.sortedBy { !it.name.endsWith(DictionaryInfoUtils.USER_DICTIONARY_SUFFIX) }.forEach {
// we sort to have user dicts first, so they have priority over internal dicts of the same type
checkAndAddDictionaryToListNewType(it, dictList, locale)
checkAndAddDictionaryToListIfNewType(it, dictList, locale)
}
nonExtracted.forEach { filename ->
val type = filename.substringBefore("_")
if (dictList.any { it.mDictType == type }) return@forEach
val extractedFile = DictionaryInfoUtils.extractAssetsDictionary(filename, locale, context) ?: return@forEach
checkAndAddDictionaryToListNewType(extractedFile, dictList, locale)
checkAndAddDictionaryToListIfNewType(extractedFile, dictList, locale)
}
return DictionaryCollection(Dictionary.TYPE_MAIN, locale, dictList, FloatArray(dictList.size) { 1f })
}

fun getAvailableDictsForLocale(locale: Locale, context: Context): Pair<Array<out File>, List<String>> {
val cachedDicts = DictionaryInfoUtils.getCachedDictsForLocale(locale, context)
fun getAvailableDictsForLocale(locale: Locale, context: Context, useEmojiDict: Boolean): Pair<Array<out File>, List<String>> {
var cachedDicts = DictionaryInfoUtils.getCachedDictsForLocale(locale, context)
if (!useEmojiDict) cachedDicts = cachedDicts.filter { it.name.substringBefore("_") != Dictionary.TYPE_EMOJI }.toTypedArray()

val nonExtractedDicts = mutableListOf<String>()
DictionaryInfoUtils.getAssetsDictionaryList(context)
Expand All @@ -63,7 +64,7 @@ object DictionaryFactory {
* if [file] cannot be loaded it is deleted
* if the dictionary type already exists in [dicts], the [file] is skipped
*/
private fun checkAndAddDictionaryToListNewType(file: File, dicts: MutableList<Dictionary>, locale: Locale) {
private fun checkAndAddDictionaryToListIfNewType(file: File, dicts: MutableList<Dictionary>, locale: Locale) {
val dictionary = getDictionary(file, locale) ?: return
if (dicts.any { it.mDictType == dictionary.mDictType }) {
dictionary.close()
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/helium314/keyboard/latin/LatinIME.java
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ private void resetDictionaryFacilitator(@NonNull final Locale locale) {
try {
mDictionaryFacilitator.resetDictionaries(this, locale,
settingsValues.mUseContactsDictionary, settingsValues.mUseAppsDictionary,
settingsValues.mUsePersonalizedDicts, false, "", this);
settingsValues.mUsePersonalizedDicts, settingsValues.mSuggestEmojis, false, "", this);
} catch (Throwable e) {
// this should not happen, but in case it does we at least want to show a keyboard
Log.e(TAG, "Could not reset dictionary facilitator, please fix ASAP", e);
Expand All @@ -763,7 +763,7 @@ private void resetDictionaryFacilitator(@NonNull final Locale locale) {
final SettingsValues settingsValues = mSettings.getCurrent();
mDictionaryFacilitator.resetDictionaries(this, mDictionaryFacilitator.getMainLocale(),
settingsValues.mUseContactsDictionary, settingsValues.mUseAppsDictionary,
settingsValues.mUsePersonalizedDicts, true, "", this);
settingsValues.mUsePersonalizedDicts, settingsValues.mSuggestEmojis, true, "", this);
}

// used for debug
Expand All @@ -786,6 +786,10 @@ public void onDestroy() {
deallocateMemory();
}

public void closeDictionaries() {
mDictionaryFacilitator.closeDictionaries();
}

private boolean isImeSuppressedByHardwareKeyboard() {
final KeyboardSwitcher switcher = KeyboardSwitcher.getInstance();
return !onEvaluateInputViewShown() && switcher.isImeSuppressedByHardwareKeyboard(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class SingleDictionaryFacilitator(private val dict: Dictionary) : DictionaryFaci
}

override fun resetDictionaries(context: Context, newLocale: Locale, useContactsDict: Boolean, useAppsDict: Boolean,
usePersonalizedDicts: Boolean, forceReloadMainDictionary: Boolean, dictNamePrefix: String, listener: DictionaryInitializationListener?
usePersonalizedDicts: Boolean, useEmojiDict: Boolean, forceReloadMainDictionary: Boolean,
dictNamePrefix: String, listener: DictionaryInitializationListener?
) { }

override fun hasAtLeastOneInitializedMainDictionary(): Boolean = dict.isInitialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ object Defaults {
const val PREF_VIBRATE_ON = false
const val PREF_VIBRATE_IN_DND_MODE = false
const val PREF_SOUND_ON = false
const val PREF_SUGGEST_EMOJIS = true
const val PREF_SHOW_EMOJI_DESCRIPTIONS = true
@JvmField
var PREF_POPUP_ON = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_VIBRATE_ON = "vibrate_on";
public static final String PREF_VIBRATE_IN_DND_MODE = "vibrate_in_dnd_mode";
public static final String PREF_SOUND_ON = "sound_on";
public static final String PREF_SUGGEST_EMOJIS = "suggest_emojis";
public static final String PREF_SHOW_EMOJI_DESCRIPTIONS = "show_emoji_descriptions";
public static final String PREF_POPUP_ON = "popup_on";
public static final String PREF_AUTO_CORRECTION = "auto_correction";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class SettingsValues {
public final boolean mVibrateOn;
public final boolean mVibrateInDndMode;
public final boolean mSoundOn;
public final boolean mSuggestEmojis;
public final boolean mShowEmojiDescriptions;
public final boolean mKeyPreviewPopupOn;
public final boolean mShowsVoiceInputKey;
Expand Down Expand Up @@ -170,6 +171,7 @@ public SettingsValues(final Context context, final SharedPreferences prefs, fina
mVibrateOn = Settings.readVibrationEnabled(prefs);
mVibrateInDndMode = prefs.getBoolean(Settings.PREF_VIBRATE_IN_DND_MODE, Defaults.PREF_VIBRATE_IN_DND_MODE);
mSoundOn = prefs.getBoolean(Settings.PREF_SOUND_ON, Defaults.PREF_SOUND_ON);
mSuggestEmojis = prefs.getBoolean(Settings.PREF_SUGGEST_EMOJIS, Defaults.PREF_SUGGEST_EMOJIS);
mShowEmojiDescriptions = prefs.getBoolean(Settings.PREF_SHOW_EMOJI_DESCRIPTIONS, Defaults.PREF_SHOW_EMOJI_DESCRIPTIONS);
mKeyPreviewPopupOn = prefs.getBoolean(Settings.PREF_POPUP_ON, Defaults.PREF_POPUP_ON);
mSlidingKeyInputPreviewEnabled = prefs.getBoolean(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ import helium314.keyboard.latin.utils.ToolbarMode
import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.NextScreenIcon
import helium314.keyboard.settings.preferences.ListPreference
import helium314.keyboard.settings.SettingsWithoutKey
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.preferences.Preference
import helium314.keyboard.settings.SearchSettingsScreen
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.SettingsDestination
import helium314.keyboard.settings.preferences.SwitchPreference
import helium314.keyboard.settings.SettingsWithoutKey
import helium314.keyboard.settings.Theme
import helium314.keyboard.settings.dialogs.ConfirmationDialog
import helium314.keyboard.settings.initPreview
import helium314.keyboard.settings.preferences.ListPreference
import helium314.keyboard.settings.preferences.Preference
import helium314.keyboard.settings.preferences.SwitchPreference
import helium314.keyboard.settings.previewDark

@Composable
Expand Down Expand Up @@ -76,6 +76,7 @@ fun TextCorrectionScreen(
if (suggestionsEnabled && prefs.getBoolean(Settings.PREF_ALWAYS_SHOW_SUGGESTIONS, Defaults.PREF_ALWAYS_SHOW_SUGGESTIONS))
Settings.PREF_ALWAYS_SHOW_SUGGESTIONS_EXCEPT_WEB_TEXT else null,
if (suggestionsEnabled) Settings.PREF_CENTER_SUGGESTION_TEXT_TO_ENTER else null,
if (suggestionsEnabled || autocorrectEnabled) Settings.PREF_SUGGEST_EMOJIS else null,
Settings.PREF_KEY_USE_PERSONALIZED_DICTS,
Settings.PREF_BIGRAM_PREDICTIONS,
Settings.PREF_SUGGEST_CLIPBOARD_CONTENT,
Expand Down Expand Up @@ -233,6 +234,11 @@ fun createCorrectionSettings(context: Context) = listOf(
) { setting ->
SwitchPreference(setting, Defaults.PREF_USE_APPS)
},
Setting(
context, Settings.PREF_SUGGEST_EMOJIS, R.string.suggest_emojis, R.string.suggest_emojis_summary
) {
SwitchPreference(it, Defaults.PREF_SUGGEST_EMOJIS) { KeyboardSwitcher.getInstance().closeDictionaries() }
},
Setting(context, Settings.PREF_ADD_TO_PERSONAL_DICTIONARY,
R.string.add_to_personal_dictionary, R.string.add_to_personal_dictionary_summary
) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<string name="vibrate_in_dnd_mode">Vibrate in do not disturb mode</string>
<!-- Option to play back sound on keypress in soft keyboard -->
<string name="sound_on_keypress">Sound on keypress</string>
<!-- Option to suggest emojis -->
<string name="suggest_emojis">Suggest emojis</string>
<!-- Description for option to suggest emojis -->
<string name="suggest_emojis_summary">Use emoji dictionary in normal suggestions</string>
<!-- Option to show emoji descriptions on long press -->
<string name="show_emoji_descriptions">Show emoji description on long press</string>
<!-- Description for option to show emoji descriptions on long press -->
Expand Down
Loading