Merge "Fix: crash when opening a broken dictionary."
diff --git a/java/res/layout/suggestions_strip.xml b/java/res/layout/suggestions_strip.xml
index b962abe..0b61499 100644
--- a/java/res/layout/suggestions_strip.xml
+++ b/java/res/layout/suggestions_strip.xml
@@ -52,7 +52,6 @@
android:layout_height="match_parent">
<TextView
android:id="@+id/important_notice_title"
- android:text="@string/important_notice_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6sp"
diff --git a/java/res/xml/prefs.xml b/java/res/xml/prefs.xml
index a39ce4a..c9be449 100644
--- a/java/res/xml/prefs.xml
+++ b/java/res/xml/prefs.xml
@@ -90,11 +90,11 @@
android:entries="@array/prefs_suggestion_visibilities"
android:defaultValue="@string/prefs_suggestion_visibility_default_value" />
<CheckBoxPreference
- android:key="pref_key_use_personalized_dicts"
+ android:key="pref_use_personalized_dicts"
android:title="@string/use_personalized_dicts"
android:summary="@string/use_personalized_dicts_summary"
android:persistent="true"
- android:defaultValue="true" />
+ android:defaultValue="false" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/gesture_typing_category"
diff --git a/java/src/com/android/inputmethod/keyboard/KeyDetector.java b/java/src/com/android/inputmethod/keyboard/KeyDetector.java
index 282c8e8..03d9def 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyDetector.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyDetector.java
@@ -16,8 +16,6 @@
package com.android.inputmethod.keyboard;
-import com.android.inputmethod.latin.Constants;
-
/**
* This class handles key detection.
*/
@@ -41,13 +39,15 @@
* @param keyHysteresisDistanceForSlidingModifier the same parameter for sliding input that
* starts from a modifier key such as shift and symbols key.
*/
- public KeyDetector(float keyHysteresisDistance, float keyHysteresisDistanceForSlidingModifier) {
+ public KeyDetector(final float keyHysteresisDistance,
+ final float keyHysteresisDistanceForSlidingModifier) {
mKeyHysteresisDistanceSquared = (int)(keyHysteresisDistance * keyHysteresisDistance);
mKeyHysteresisDistanceForSlidingModifierSquared = (int)(
keyHysteresisDistanceForSlidingModifier * keyHysteresisDistanceForSlidingModifier);
}
- public void setKeyboard(Keyboard keyboard, float correctionX, float correctionY) {
+ public void setKeyboard(final Keyboard keyboard, final float correctionX,
+ final float correctionY) {
if (keyboard == null) {
throw new NullPointerException();
}
@@ -56,24 +56,21 @@
mKeyboard = keyboard;
}
- public int getKeyHysteresisDistanceSquared(boolean isSlidingFromModifier) {
+ public int getKeyHysteresisDistanceSquared(final boolean isSlidingFromModifier) {
return isSlidingFromModifier
? mKeyHysteresisDistanceForSlidingModifierSquared : mKeyHysteresisDistanceSquared;
}
- public int getTouchX(int x) {
+ public int getTouchX(final int x) {
return x + mCorrectionX;
}
// TODO: Remove vertical correction.
- public int getTouchY(int y) {
+ public int getTouchY(final int y) {
return y + mCorrectionY;
}
public Keyboard getKeyboard() {
- if (mKeyboard == null) {
- throw new IllegalStateException("keyboard isn't set");
- }
return mKeyboard;
}
@@ -88,7 +85,7 @@
* @param y The y-coordinate of a touch point
* @return the key that the touch point hits.
*/
- public Key detectHitKey(int x, int y) {
+ public Key detectHitKey(final int x, final int y) {
final int touchX = getTouchX(x);
final int touchY = getTouchY(y);
@@ -113,20 +110,4 @@
}
return primaryKey;
}
-
- public static String printableCode(Key key) {
- return key != null ? Constants.printableCode(key.getCode()) : "none";
- }
-
- public static String printableCodes(int[] codes) {
- final StringBuilder sb = new StringBuilder();
- boolean addDelimiter = false;
- for (final int code : codes) {
- if (code == Constants.NOT_A_CODE) break;
- if (addDelimiter) sb.append(", ");
- sb.append(Constants.printableCode(code));
- addDelimiter = true;
- }
- return "[" + sb + "]";
- }
}
diff --git a/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java b/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java
index 4a80279..abff202 100644
--- a/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java
+++ b/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java
@@ -33,13 +33,17 @@
}
@Override
- public Key detectHitKey(int x, int y) {
+ public Key detectHitKey(final int x, final int y) {
+ final Keyboard keyboard = getKeyboard();
+ if (keyboard == null) {
+ return null;
+ }
final int touchX = getTouchX(x);
final int touchY = getTouchY(y);
Key nearestKey = null;
int nearestDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare;
- for (final Key key : getKeyboard().getKeys()) {
+ for (final Key key : keyboard.getKeys()) {
final int dist = key.squaredDistanceToEdge(touchX, touchY);
if (dist < nearestDist) {
nearestKey = key;
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index befc4e6..59cf64d 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -257,12 +257,15 @@
}
public static void setKeyDetector(final KeyDetector keyDetector) {
+ final Keyboard keyboard = keyDetector.getKeyboard();
+ if (keyboard == null) {
+ return;
+ }
final int trackersSize = sTrackers.size();
for (int i = 0; i < trackersSize; ++i) {
final PointerTracker tracker = sTrackers.get(i);
tracker.setKeyDetectorInner(keyDetector);
}
- final Keyboard keyboard = keyDetector.getKeyboard();
sGestureEnabler.setPasswordMode(keyboard.mId.passwordInput());
}
@@ -301,7 +304,7 @@
final boolean ignoreModifierKey = mIsInDraggingFinger && key.isModifier();
if (DEBUG_LISTENER) {
Log.d(TAG, String.format("[%d] onPress : %s%s%s%s", mPointerId,
- KeyDetector.printableCode(key),
+ (key == null ? "none" : Constants.printableCode(key.getCode())),
ignoreModifierKey ? " ignoreModifier" : "",
key.isEnabled() ? "" : " disabled",
repeatCount > 0 ? " repeatCount=" + repeatCount : ""));
@@ -402,11 +405,14 @@
private void setKeyDetectorInner(final KeyDetector keyDetector) {
final Keyboard keyboard = keyDetector.getKeyboard();
+ if (keyboard == null) {
+ return;
+ }
if (keyDetector == mKeyDetector && keyboard == mKeyboard) {
return;
}
mKeyDetector = keyDetector;
- mKeyboard = keyDetector.getKeyboard();
+ mKeyboard = keyboard;
// Mark that keyboard layout has been changed.
mKeyboardLayoutHasBeenChanged = true;
final int keyWidth = mKeyboard.mMostCommonKeyWidth;
@@ -1235,7 +1241,7 @@
private void printTouchEvent(final String title, final int x, final int y,
final long eventTime) {
final Key key = mKeyDetector.detectHitKey(x, y);
- final String code = KeyDetector.printableCode(key);
+ final String code = (key == null ? "none" : Constants.printableCode(key.getCode()));
Log.d(TAG, String.format("[%d]%s%s %4d %4d %5d %s", mPointerId,
(mIsTrackingForActionDisabled ? "-" : " "), title, x, y, eventTime, code));
}
diff --git a/java/src/com/android/inputmethod/latin/Constants.java b/java/src/com/android/inputmethod/latin/Constants.java
index d6ac71f..d1ff714 100644
--- a/java/src/com/android/inputmethod/latin/Constants.java
+++ b/java/src/com/android/inputmethod/latin/Constants.java
@@ -252,6 +252,18 @@
}
}
+ public static String printableCodes(final int[] codes) {
+ final StringBuilder sb = new StringBuilder();
+ boolean addDelimiter = false;
+ for (final int code : codes) {
+ if (code == NOT_A_CODE) break;
+ if (addDelimiter) sb.append(", ");
+ sb.append(printableCode(code));
+ addDelimiter = true;
+ }
+ return "[" + sb + "]";
+ }
+
public static final int MAX_INT_BIT_COUNT = 32;
/**
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
index 259c137..138a626 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
@@ -512,7 +512,6 @@
}
}
- @UsedForTesting
public void clearUserHistoryDictionary() {
if (mUserHistoryDictionary == null) {
return;
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 459f6d8..34940b4 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -25,6 +25,9 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
+import android.content.DialogInterface.OnClickListener;
+import android.content.DialogInterface.OnDismissListener;
+import android.content.DialogInterface.OnShowListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
@@ -531,17 +534,28 @@
}
private void refreshPersonalizationDictionarySession() {
+ final Suggest suggest = mInputLogic.mSuggest;
+ final boolean shouldKeepUserHistoryDictionaries;
+ final boolean shouldKeepPersonalizationDictionaries;
if (mSettings.getCurrent().mUsePersonalizedDicts) {
- if (mSubtypeSwitcher.isSystemLocaleSameAsLocaleOfAllEnabledSubtypes()) {
- final DictionaryFacilitatorForSuggest dictionaryFacilitator =
- (mInputLogic.mSuggest == null) ?
- null : mInputLogic.mSuggest.mDictionaryFacilitator;
- PersonalizationDictionarySessionRegistrar.init(this, dictionaryFacilitator);
- } else {
- PersonalizationDictionarySessionRegistrar.close(this);
- }
+ shouldKeepUserHistoryDictionaries = true;
+ // TODO: Eliminate this restriction
+ shouldKeepPersonalizationDictionaries =
+ mSubtypeSwitcher.isSystemLocaleSameAsLocaleOfAllEnabledSubtypes();
} else {
- PersonalizationHelper.removeAllPersonalizedDictionaries(this);
+ shouldKeepUserHistoryDictionaries = false;
+ shouldKeepPersonalizationDictionaries = false;
+ }
+ if (!shouldKeepUserHistoryDictionaries) {
+ // Remove user history dictionaries.
+ PersonalizationHelper.removeAllUserHistoryDictionaries(this);
+ if (suggest != null) {
+ suggest.mDictionaryFacilitator.clearUserHistoryDictionary();
+ }
+ }
+ if (!shouldKeepPersonalizationDictionaries) {
+ // Remove personalization dictionaries.
+ PersonalizationHelper.removeAllPersonalizationDictionaries(this);
PersonalizationDictionarySessionRegistrar.resetAll(this);
}
}
@@ -1165,27 +1179,48 @@
@Override
public void showImportantNoticeContents() {
final Context context = this;
- final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
+ final OnShowListener onShowListener = new OnShowListener() {
@Override
- public void onClick(final DialogInterface di, final int position) {
- di.dismiss();
+ public void onShow(final DialogInterface dialog) {
ImportantNoticeUtils.updateLastImportantNoticeVersion(context);
- if (position == DialogInterface.BUTTON_POSITIVE) {
- setNeutralSuggestionStrip();
- return;
- }
+ onShowImportantNoticeDialog(
+ ImportantNoticeUtils.getCurrentImportantNoticeVersion(context));
+ }
+ };
+ final OnClickListener onClickListener = new OnClickListener() {
+ @Override
+ public void onClick(final DialogInterface dialog, final int position) {
if (position == DialogInterface.BUTTON_NEGATIVE) {
launchSettings();
- return;
}
}
};
- final AlertDialog.Builder builder =
- new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_DARK);
- builder.setMessage(R.string.important_notice_contents)
- .setPositiveButton(android.R.string.ok, listener)
- .setNegativeButton(R.string.go_to_settings, listener);
- showOptionDialog(builder.create(), true /* cancelable */);
+ final OnDismissListener onDismissListener = new OnDismissListener() {
+ @Override
+ public void onDismiss(DialogInterface dialog) {
+ setNeutralSuggestionStrip();
+ }
+ };
+ final String importantNoticeContents = ImportantNoticeUtils.getImportantNoticeContents(
+ context);
+ final AlertDialog.Builder builder = new AlertDialog.Builder(
+ context, AlertDialog.THEME_HOLO_DARK);
+ builder.setMessage(importantNoticeContents)
+ .setPositiveButton(android.R.string.ok, null /* listener */)
+ .setNegativeButton(R.string.go_to_settings, onClickListener);
+ final AlertDialog importantNoticeDialog = builder.create();
+ importantNoticeDialog.setOnShowListener(onShowListener);
+ importantNoticeDialog.setOnDismissListener(onDismissListener);
+ showOptionDialog(importantNoticeDialog);
+ }
+
+ private void onShowImportantNoticeDialog(final int importantNoticeVersion) {
+ if (importantNoticeVersion ==
+ ImportantNoticeUtils.VERSION_TO_ENABLE_PERSONALIZED_SUGGESTIONS) {
+ mSettings.writeUsePersonalizationDictionary(true /* enabled */);
+ loadSettings();
+ initSuggest();
+ }
}
public void displaySettingsDialog() {
@@ -1639,7 +1674,7 @@
getString(R.string.language_selection_title),
getString(ApplicationUtils.getActivityTitleResId(this, SettingsActivity.class)),
};
- final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
+ final OnClickListener listener = new OnClickListener() {
@Override
public void onClick(DialogInterface di, int position) {
di.dismiss();
@@ -1660,18 +1695,18 @@
};
final AlertDialog.Builder builder =
new AlertDialog.Builder(this).setItems(items, listener).setTitle(title);
- showOptionDialog(builder.create(), true /*cancelable */);
+ showOptionDialog(builder.create());
}
// TODO: Move this method out of {@link LatinIME}.
- private void showOptionDialog(final AlertDialog dialog, final boolean cancelable) {
+ private void showOptionDialog(final AlertDialog dialog) {
final IBinder windowToken = mKeyboardSwitcher.getMainKeyboardView().getWindowToken();
if (windowToken == null) {
return;
}
- dialog.setCancelable(cancelable);
- dialog.setCanceledOnTouchOutside(cancelable);
+ dialog.setCancelable(true /* cancelable */);
+ dialog.setCanceledOnTouchOutside(true /* cancelable */);
final Window window = dialog.getWindow();
final WindowManager.LayoutParams lp = window.getAttributes();
diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java
index df64bce..5ae2fb6 100644
--- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java
+++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java
@@ -93,13 +93,16 @@
}
}
- public static void removeAllPersonalizedDictionaries(final Context context) {
- removeAllDictionaries(context, sLangUserHistoryDictCache,
- UserHistoryDictionary.NAME);
+ public static void removeAllPersonalizationDictionaries(final Context context) {
removeAllDictionaries(context, sLangPersonalizationDictCache,
PersonalizationDictionary.NAME);
}
+ public static void removeAllUserHistoryDictionaries(final Context context) {
+ removeAllDictionaries(context, sLangUserHistoryDictCache,
+ UserHistoryDictionary.NAME);
+ }
+
private static <T extends DecayingExpandableBinaryDictionaryBase> void removeAllDictionaries(
final Context context, final ConcurrentHashMap<String, SoftReference<T>> dictionaryMap,
final String dictNamePrefix) {
diff --git a/java/src/com/android/inputmethod/latin/settings/Settings.java b/java/src/com/android/inputmethod/latin/settings/Settings.java
index b51c765..6a1503f 100644
--- a/java/src/com/android/inputmethod/latin/settings/Settings.java
+++ b/java/src/com/android/inputmethod/latin/settings/Settings.java
@@ -55,7 +55,7 @@
public static final String PREF_MISC_SETTINGS = "misc_settings";
public static final String PREF_ADVANCED_SETTINGS = "pref_advanced_settings";
public static final String PREF_KEY_USE_CONTACTS_DICT = "pref_key_use_contacts_dict";
- public static final String PREF_KEY_USE_PERSONALIZED_DICTS = "pref_key_use_personalized_dicts";
+ public static final String PREF_USE_PERSONALIZED_DICTS = "pref_use_personalized_dicts";
public static final String PREF_KEY_USE_DOUBLE_SPACE_PERIOD =
"pref_key_use_double_space_period";
public static final String PREF_BLOCK_POTENTIALLY_OFFENSIVE =
@@ -421,6 +421,10 @@
return mPrefs.getStringSet(PREF_CORPUS_HANDLES_FOR_PERSONALIZATION, emptySet);
}
+ public void writeUsePersonalizationDictionary(final boolean enabled) {
+ mPrefs.edit().putBoolean(PREF_USE_PERSONALIZED_DICTS, enabled).apply();
+ }
+
public static void writeEmojiRecentKeys(final SharedPreferences prefs, String str) {
prefs.edit().putString(PREF_EMOJI_RECENT_KEYS, str).apply();
}
diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsValues.java b/java/src/com/android/inputmethod/latin/settings/SettingsValues.java
index 77968f7..0f3deea 100644
--- a/java/src/com/android/inputmethod/latin/settings/SettingsValues.java
+++ b/java/src/com/android/inputmethod/latin/settings/SettingsValues.java
@@ -128,7 +128,7 @@
Settings.PREF_INCLUDE_OTHER_IMES_IN_LANGUAGE_SWITCH_LIST, false);
mShowsLanguageSwitchKey = Settings.readShowsLanguageSwitchKey(prefs);
mUseContactsDict = prefs.getBoolean(Settings.PREF_KEY_USE_CONTACTS_DICT, true);
- mUsePersonalizedDicts = prefs.getBoolean(Settings.PREF_KEY_USE_PERSONALIZED_DICTS, true);
+ mUsePersonalizedDicts = prefs.getBoolean(Settings.PREF_USE_PERSONALIZED_DICTS, false);
mUseDoubleSpacePeriod = prefs.getBoolean(Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD, true);
mBlockPotentiallyOffensive = Settings.readBlockPotentiallyOffensive(prefs, res);
mAutoCorrectEnabled = Settings.readAutoCorrectEnabled(autoCorrectionThresholdRawValue, res);
diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java
index e77c550..8ea7128 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java
@@ -504,12 +504,13 @@
hintView, 1.0f - mCenterSuggestionWeight, ViewGroup.LayoutParams.MATCH_PARENT);
}
- public void layoutImportantNotice(final View importantNoticeStrip, final int stripWidth) {
+ public void layoutImportantNotice(final View importantNoticeStrip, final int stripWidth,
+ final String importantNoticeTitle) {
final TextView titleView = (TextView)importantNoticeStrip.findViewById(
R.id.important_notice_title);
final int width = stripWidth - titleView.getPaddingLeft() - titleView.getPaddingRight();
titleView.setTextColor(mColorAutoCorrect);
- final CharSequence importantNoticeTitle = titleView.getText();
+ titleView.setText(importantNoticeTitle);
titleView.setTextScaleX(1.0f); // Reset textScaleX.
final float titleScaleX = getTextScaleX(
importantNoticeTitle, width, titleView.getPaint());
diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
index 1f80c4c..2966a8b 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
@@ -20,6 +20,7 @@
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v4.view.ViewCompat;
+import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.GestureDetector;
@@ -236,7 +237,12 @@
if (width <= 0) {
return false;
}
- mLayoutHelper.layoutImportantNotice(mImportantNoticeStrip, width);
+ final String importantNoticeTitle = ImportantNoticeUtils.getImportantNoticeTitle(
+ getContext());
+ if (TextUtils.isEmpty(importantNoticeTitle)) {
+ return false;
+ }
+ mLayoutHelper.layoutImportantNotice(mImportantNoticeStrip, width, importantNoticeTitle);
mStripVisibilityGroup.showImportantNoticeStrip();
mImportantNoticeStrip.setOnClickListener(this);
return true;
diff --git a/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java b/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java
index 50a9423..dd418b8 100644
--- a/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java
@@ -32,6 +32,7 @@
// displayed to users.
private static final String PREFERENCE_NAME = "important_notice";
private static final String KEY_IMPORTANT_NOTICE_VERSION = "important_notice_version";
+ public static final int VERSION_TO_ENABLE_PERSONALIZED_SUGGESTIONS = 2;
// Copy of the hidden {@link Settings.Secure#USER_SETUP_COMPLETE} settings key.
// The value is zero until each multiuser completes system setup wizard.
@@ -59,7 +60,7 @@
return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
- private static int getCurrentImportantNoticeVersion(final Context context) {
+ public static int getCurrentImportantNoticeVersion(final Context context) {
return context.getResources().getInteger(R.integer.config_important_notice_version);
}
@@ -83,4 +84,22 @@
.putInt(KEY_IMPORTANT_NOTICE_VERSION, getCurrentImportantNoticeVersion(context))
.apply();
}
+
+ public static String getImportantNoticeTitle(final Context context) {
+ switch (getCurrentImportantNoticeVersion(context)) {
+ case VERSION_TO_ENABLE_PERSONALIZED_SUGGESTIONS:
+ return context.getString(R.string.important_notice_title);
+ default:
+ return null;
+ }
+ }
+
+ public static String getImportantNoticeContents(final Context context) {
+ switch (getCurrentImportantNoticeVersion(context)) {
+ case VERSION_TO_ENABLE_PERSONALIZED_SUGGESTIONS:
+ return context.getString(R.string.important_notice_contents);
+ default:
+ return null;
+ }
+ }
}