Skip to content

Commit c3772cd

Browse files
Merge pull request openboard-team#695 from Pkshields/auto-correction-confidence
Add Auto-correction Confidence settings
2 parents a42e866 + 0109899 commit c3772cd

6 files changed

Lines changed: 39 additions & 40 deletions

File tree

app/src/main/java/org/dslul/openboard/inputmethod/latin/settings/CorrectionSettingsFragment.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.dslul.openboard.inputmethod.latin.settings;
1818

19-
import android.Manifest;
2019
import android.app.Activity;
2120
import android.content.Context;
2221
import android.content.Intent;
@@ -25,12 +24,8 @@
2524
import android.content.pm.ResolveInfo;
2625
import android.os.Bundle;
2726
import android.preference.Preference;
28-
import android.preference.SwitchPreference;
29-
import android.text.TextUtils;
3027

3128
import org.dslul.openboard.inputmethod.latin.R;
32-
import org.dslul.openboard.inputmethod.latin.permissions.PermissionsManager;
33-
import org.dslul.openboard.inputmethod.latin.permissions.PermissionsUtil;
3429
import org.dslul.openboard.inputmethod.latin.userdictionary.UserDictionaryList;
3530
import org.dslul.openboard.inputmethod.latin.userdictionary.UserDictionarySettings;
3631

@@ -44,6 +39,7 @@
4439
* - Add-on dictionaries
4540
* - Block offensive words
4641
* - Auto-correction
42+
* - Auto-correction confidence
4743
* - Show correction suggestions
4844
* - Personalized suggestions
4945
* - Suggest Contact names
@@ -73,6 +69,18 @@ public void onCreate(final Bundle icicle) {
7369
if (ri == null) {
7470
overwriteUserDictionaryPreference(editPersonalDictionary);
7571
}
72+
73+
refreshEnabledSettings();
74+
}
75+
76+
@Override
77+
public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
78+
refreshEnabledSettings();
79+
}
80+
81+
private void refreshEnabledSettings() {
82+
setPreferenceEnabled(Settings.PREF_AUTO_CORRECTION_CONFIDENCE,
83+
Settings.readAutoCorrectEnabled(getSharedPreferences(), getResources()));
7684
}
7785

7886
private void overwriteUserDictionaryPreference(final Preference userDictionaryPreference) {

app/src/main/java/org/dslul/openboard/inputmethod/latin/settings/Settings.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
6565
public static final String PREF_VOICE_INPUT_KEY = "pref_voice_input_key";
6666
public static final String PREF_CLIPBOARD_CLIPBOARD_KEY = "pref_clipboard_clipboard_key";
6767
public static final String PREF_EDIT_PERSONAL_DICTIONARY = "edit_personal_dictionary";
68-
// PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE is obsolete. Use PREF_AUTO_CORRECTION instead.
69-
public static final String PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE =
70-
"auto_correction_threshold";
7168
public static final String PREF_AUTO_CORRECTION = "pref_key_auto_correction";
69+
public static final String PREF_AUTO_CORRECTION_CONFIDENCE = "pref_key_auto_correction_confidence";
7270
// PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE is obsolete. Use PREF_SHOW_SUGGESTIONS instead.
7371
public static final String PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE = "show_suggestions_setting";
7472
public static final String PREF_SHOW_SUGGESTIONS = "show_suggestions";
@@ -173,7 +171,6 @@ private void onCreate(final Context context) {
173171
mRes = context.getResources();
174172
mPrefs = DeviceProtectedUtils.getSharedPreferences(context);
175173
mPrefs.registerOnSharedPreferenceChangeListener(this);
176-
upgradeAutocorrectionSettings(mPrefs, mRes);
177174
}
178175

179176
public void onDestroy() {
@@ -247,6 +244,12 @@ public static boolean readAutoCorrectEnabled(final SharedPreferences prefs,
247244
return prefs.getBoolean(PREF_AUTO_CORRECTION, true);
248245
}
249246

247+
public static String readAutoCorrectConfidence(final SharedPreferences prefs,
248+
final Resources res) {
249+
return prefs.getString(PREF_AUTO_CORRECTION_CONFIDENCE,
250+
res.getString(R.string.auto_correction_threshold_mode_index_modest));
251+
}
252+
250253
public static float readPlausibilityThreshold(final Resources res) {
251254
return Float.parseFloat(res.getString(R.string.plausibility_threshold));
252255
}
@@ -513,21 +516,4 @@ public static int readLastShownEmojiCategoryPageId(
513516
final SharedPreferences prefs, final int defValue) {
514517
return prefs.getInt(PREF_LAST_SHOWN_EMOJI_CATEGORY_PAGE_ID, defValue);
515518
}
516-
517-
private void upgradeAutocorrectionSettings(final SharedPreferences prefs, final Resources res) {
518-
final String thresholdSetting =
519-
prefs.getString(PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE, null);
520-
if (thresholdSetting != null) {
521-
SharedPreferences.Editor editor = prefs.edit();
522-
editor.remove(PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE);
523-
final String autoCorrectionOff =
524-
res.getString(R.string.auto_correction_threshold_mode_index_off);
525-
if (thresholdSetting.equals(autoCorrectionOff)) {
526-
editor.putBoolean(PREF_AUTO_CORRECTION, false);
527-
} else {
528-
editor.putBoolean(PREF_AUTO_CORRECTION, true);
529-
}
530-
editor.commit();
531-
}
532-
}
533519
}

app/src/main/java/org/dslul/openboard/inputmethod/latin/settings/SettingsValues.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class SettingsValues {
5353
private static final String FLOAT_NEGATIVE_INFINITY_MARKER_STRING = "floatNegativeInfinity";
5454
private static final int TIMEOUT_TO_GET_TARGET_PACKAGE = 5; // seconds
5555
public static final float DEFAULT_SIZE_SCALE = 1.0f; // 100%
56+
public static final float AUTO_CORRECTION_DISABLED_THRESHOLD = Float.MAX_VALUE;
5657

5758
// From resources:
5859
public final SpacingAndPunctuations mSpacingAndPunctuations;
@@ -163,9 +164,9 @@ public SettingsValues(final Context context, final SharedPreferences prefs, fina
163164
&& inputAttributes.mIsGeneralTextInput;
164165
mBlockPotentiallyOffensive = Settings.readBlockPotentiallyOffensive(prefs, res);
165166
mAutoCorrectEnabled = Settings.readAutoCorrectEnabled(prefs, res);
166-
final String autoCorrectionThresholdRawValue = mAutoCorrectEnabled
167-
? res.getString(R.string.auto_correction_threshold_mode_index_modest)
168-
: res.getString(R.string.auto_correction_threshold_mode_index_off);
167+
mAutoCorrectionThreshold = mAutoCorrectEnabled
168+
? readAutoCorrectionThreshold(res, prefs)
169+
: AUTO_CORRECTION_DISABLED_THRESHOLD;
169170
mBigramPredictionEnabled = readBigramPredictionEnabled(prefs, res);
170171
mDoubleSpacePeriodTimeout = res.getInteger(R.integer.config_double_space_period_timeout);
171172
mHasHardwareKeyboard = Settings.readHasHardwareKeyboard(res.getConfiguration());
@@ -183,8 +184,6 @@ public SettingsValues(final Context context, final SharedPreferences prefs, fina
183184
Settings.PREF_ENABLE_EMOJI_ALT_PHYSICAL_KEY, true);
184185
mShowAppIcon = Settings.readShowSetupWizardIcon(prefs, context);
185186
mIsShowAppIconSettingInPreferences = prefs.contains(Settings.PREF_SHOW_SETUP_WIZARD_ICON);
186-
mAutoCorrectionThreshold = readAutoCorrectionThreshold(res,
187-
autoCorrectionThresholdRawValue);
188187
mPlausibilityThreshold = Settings.readPlausibilityThreshold(res);
189188
mGestureInputEnabled = Settings.readGestureInputEnabled(prefs, res);
190189
mGestureTrailEnabled = prefs.getBoolean(Settings.PREF_GESTURE_PREVIEW_TRAIL, true);
@@ -331,7 +330,8 @@ private static boolean readBigramPredictionEnabled(final SharedPreferences prefs
331330
}
332331

333332
private static float readAutoCorrectionThreshold(final Resources res,
334-
final String currentAutoCorrectionSetting) {
333+
final SharedPreferences prefs) {
334+
final String currentAutoCorrectionSetting = Settings.readAutoCorrectConfidence(prefs, res);
335335
final String[] autoCorrectionThresholdValues = res.getStringArray(
336336
R.array.auto_correction_threshold_values);
337337
// When autoCorrectionThreshold is greater than 1.0, it's like auto correction is off.

app/src/main/res/values/config-auto-correction-thresholds.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
<resources>
2222
<!-- The array of auto correction threshold values. -->
2323
<string-array name="auto_correction_threshold_values" translatable="false">
24-
<!-- Off, When auto correction setting is Off, this value is not used. -->
25-
<item>floatMaxValue</item>
2624
<!-- Modest : Suggestion whose normalized score is greater than this value
2725
will be subject to auto-correction. -->
2826
<item>0.185</item>
@@ -41,21 +39,18 @@
4139
<string name="plausibility_threshold" translatable="false">0.065</string>
4240

4341
<!-- The index of the auto correction threshold values array. -->
44-
<string name="auto_correction_threshold_mode_index_off" translatable="false">0</string>
45-
<string name="auto_correction_threshold_mode_index_modest" translatable="false">1</string>
46-
<string name="auto_correction_threshold_mode_index_aggressive" translatable="false">2</string>
47-
<string name="auto_correction_threshold_mode_index_very_aggressive" translatable="false">3</string>
42+
<string name="auto_correction_threshold_mode_index_modest" translatable="false">0</string>
43+
<string name="auto_correction_threshold_mode_index_aggressive" translatable="false">1</string>
44+
<string name="auto_correction_threshold_mode_index_very_aggressive" translatable="false">2</string>
4845

4946
<!-- The array of the auto correction threshold settings values. -->
5047
<string-array name="auto_correction_threshold_mode_indexes" translatable="false">
51-
<item>@string/auto_correction_threshold_mode_index_off</item>
5248
<item>@string/auto_correction_threshold_mode_index_modest</item>
5349
<item>@string/auto_correction_threshold_mode_index_aggressive</item>
5450
<item>@string/auto_correction_threshold_mode_index_very_aggressive</item>
5551
</string-array>
5652
<!-- The array of the human readable auto correction threshold settings entries. -->
5753
<string-array name="auto_correction_threshold_modes" translatable="false">
58-
<item>@string/auto_correction_threshold_mode_off</item>
5954
<item>@string/auto_correction_threshold_mode_modest</item>
6055
<item>@string/auto_correction_threshold_mode_aggressive</item>
6156
<item>@string/auto_correction_threshold_mode_very_aggressive</item>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140
<string name="auto_correction">Auto-correction</string>
141141
<!-- Description for auto correction [CHAR LIMIT=65 (two lines) or 30 (fits on one line, preferable)] -->
142142
<string name="auto_correction_summary">Spacebar and punctuation automatically correct mistyped words</string>
143+
<!-- Option to change the confidence level of the auto correction [CHAR LIMIT=20] -->
144+
<string name="auto_correction_confidence">Auto-correction confidence</string>
143145
<!-- Option to disable auto correction. [CHAR LIMIT=20] -->
144146
<string name="auto_correction_threshold_mode_off">Off</string>
145147
<!-- Option to suggest auto correction suggestions modestly. Auto-corrects only to a word which has small edit distance from typed word. [CHAR LIMIT=20] -->

app/src/main/res/xml/prefs_screen_correction.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141
android:defaultValue="true"
4242
android:persistent="true" />
4343

44+
<ListPreference
45+
android:key="pref_key_auto_correction_confidence"
46+
android:title="@string/auto_correction_confidence"
47+
android:summary="%s"
48+
android:entries="@array/auto_correction_threshold_modes"
49+
android:entryValues="@array/auto_correction_threshold_mode_indexes"
50+
android:defaultValue="@string/auto_correction_threshold_mode_index_modest" />
51+
4452
<CheckBoxPreference
4553
android:key="auto_cap"
4654
android:title="@string/auto_cap"

0 commit comments

Comments
 (0)