diff options
Diffstat (limited to 'java/src')
10 files changed, 477 insertions, 239 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index ed873a70d..e1e1ca9cf 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -227,7 +227,7 @@ public class Key { row.setXPos(keyXPos + keyWidth); mBackgroundType = style.getInt(keyAttr, - R.styleable.Keyboard_Key_backgroundType, BACKGROUND_TYPE_NORMAL); + R.styleable.Keyboard_Key_backgroundType, row.getDefaultBackgroundType()); mVisualInsetsLeft = Math.round(Keyboard.Builder.getDimensionOrFraction(keyAttr, R.styleable.Keyboard_Key_visualInsetsLeft, params.mBaseWidth, 0)); diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java index 0be4cf3a7..6fc630d05 100644 --- a/java/src/com/android/inputmethod/keyboard/Keyboard.java +++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java @@ -89,7 +89,8 @@ public class Keyboard { private static final int MINIMUM_LETTER_CODE = CODE_TAB; /** Special keys code. Must be negative. - * These should be aligned with values/keycodes.xml + * These should be aligned with KeyboardCodesSet.ID_TO_NAME[], + * KeyboardCodesSet.DEFAULT[] and KeyboardCodesSet.RTL[] */ public static final int CODE_SHIFT = -1; public static final int CODE_SWITCH_ALPHA_SYMBOL = -2; @@ -101,8 +102,9 @@ public class Keyboard { public static final int CODE_ACTION_NEXT = -8; public static final int CODE_ACTION_PREVIOUS = -9; public static final int CODE_LANGUAGE_SWITCH = -10; + public static final int CODE_RESEARCH = -11; // Code value representing the code is not specified. - public static final int CODE_UNSPECIFIED = -11; + public static final int CODE_UNSPECIFIED = -12; public final KeyboardId mId; public final int mThemeId; @@ -422,67 +424,67 @@ public class Keyboard { * This class parses Keyboard XML file and eventually build a Keyboard. * The Keyboard XML file looks like: * <pre> - * >!-- xml/keyboard.xml --< - * >Keyboard keyboard_attributes*< - * >!-- Keyboard Content --< - * >Row row_attributes*< - * >!-- Row Content --< - * >Key key_attributes* /< - * >Spacer horizontalGap="32.0dp" /< - * >include keyboardLayout="@xml/other_keys"< + * <!-- xml/keyboard.xml --> + * <Keyboard keyboard_attributes*> + * <!-- Keyboard Content --> + * <Row row_attributes*> + * <!-- Row Content --> + * <Key key_attributes* /> + * <Spacer horizontalGap="32.0dp" /> + * <include keyboardLayout="@xml/other_keys"> * ... - * >/Row< - * >include keyboardLayout="@xml/other_rows"< + * </Row> + * <include keyboardLayout="@xml/other_rows"> * ... - * >/Keyboard< + * </Keyboard> * </pre> - * The XML file which is included in other file must have >merge< as root element, + * The XML file which is included in other file must have <merge> as root element, * such as: * <pre> - * >!-- xml/other_keys.xml --< - * >merge< - * >Key key_attributes* /< + * <!-- xml/other_keys.xml --> + * <merge> + * <Key key_attributes* /> * ... - * >/merge< + * </merge> * </pre> * and * <pre> - * >!-- xml/other_rows.xml --< - * >merge< - * >Row row_attributes*< - * >Key key_attributes* /< - * >/Row< + * <!-- xml/other_rows.xml --> + * <merge> + * <Row row_attributes*> + * <Key key_attributes* /> + * </Row> * ... - * >/merge< + * </merge> * </pre> * You can also use switch-case-default tags to select Rows and Keys. * <pre> - * >switch< - * >case case_attribute*< - * >!-- Any valid tags at switch position --< - * >/case< + * <switch> + * <case case_attribute*> + * <!-- Any valid tags at switch position --> + * </case> * ... - * >default< - * >!-- Any valid tags at switch position --< - * >/default< - * >/switch< + * <default> + * <!-- Any valid tags at switch position --> + * </default> + * </switch> * </pre> * You can declare Key style and specify styles within Key tags. * <pre> - * >switch< - * >case mode="email"< - * >key-style styleName="f1-key" parentStyle="modifier-key" + * <switch> + * <case mode="email"> + * <key-style styleName="f1-key" parentStyle="modifier-key" * keyLabel=".com" - * /< - * >/case< - * >case mode="url"< - * >key-style styleName="f1-key" parentStyle="modifier-key" + * /> + * </case> + * <case mode="url"> + * <key-style styleName="f1-key" parentStyle="modifier-key" * keyLabel="http://" - * /< - * >/case< - * >/switch< + * /> + * </case> + * </switch> * ... - * >Key keyStyle="shift-key" ... /< + * <Key keyStyle="shift-key" ... /> * </pre> */ @@ -533,6 +535,8 @@ public class Keyboard { public final int mRowHeight; /** Default keyLabelFlags in this row. */ private int mDefaultKeyLabelFlags; + /** Default backgroundType for this row */ + private int mDefaultBackgroundType; private final int mCurrentY; // Will be updated by {@link Key}'s constructor. @@ -551,8 +555,11 @@ public class Keyboard { mDefaultKeyWidth = Builder.getDimensionOrFraction(keyAttr, R.styleable.Keyboard_Key_keyWidth, params.mBaseWidth, params.mDefaultKeyWidth); + mDefaultBackgroundType = keyAttr.getInt(R.styleable.Keyboard_Key_backgroundType, + Key.BACKGROUND_TYPE_NORMAL); keyAttr.recycle(); + // TODO: Initialize this with <Row> attribute as backgroundType is done. mDefaultKeyLabelFlags = 0; mCurrentY = y; mCurrentX = 0.0f; @@ -574,6 +581,14 @@ public class Keyboard { mDefaultKeyLabelFlags = keyLabelFlags; } + public int getDefaultBackgroundType() { + return mDefaultBackgroundType; + } + + public void setDefaultBackgroundType(int backgroundType) { + mDefaultBackgroundType = backgroundType; + } + public void setXPos(float keyXPos) { mCurrentX = keyXPos; } @@ -952,6 +967,7 @@ public class Keyboard { int keyboardLayout = 0; float savedDefaultKeyWidth = 0; int savedDefaultKeyLabelFlags = 0; + int savedDefaultBackgroundType = Key.BACKGROUND_TYPE_NORMAL; try { XmlParseUtils.checkAttributeExists(keyboardAttr, R.styleable.Keyboard_Include_keyboardLayout, "keyboardLayout", @@ -959,22 +975,26 @@ public class Keyboard { keyboardLayout = keyboardAttr.getResourceId( R.styleable.Keyboard_Include_keyboardLayout, 0); if (row != null) { - savedDefaultKeyWidth = row.getDefaultKeyWidth(); - savedDefaultKeyLabelFlags = row.getDefaultKeyLabelFlags(); if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyXPos)) { // Override current x coordinate. row.setXPos(row.getKeyX(keyAttr)); } + // TODO: Remove this if-clause and do the same as backgroundType below. + savedDefaultKeyWidth = row.getDefaultKeyWidth(); if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyWidth)) { // Override default key width. row.setDefaultKeyWidth(row.getKeyWidth(keyAttr)); } - if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyLabelFlags)) { - // Override default key label flags. - row.setDefaultKeyLabelFlags( - keyAttr.getInt(R.styleable.Keyboard_Key_keyLabelFlags, 0) - | savedDefaultKeyLabelFlags); - } + savedDefaultKeyLabelFlags = row.getDefaultKeyLabelFlags(); + // Bitwise-or default keyLabelFlag if exists. + row.setDefaultKeyLabelFlags(keyAttr.getInt( + R.styleable.Keyboard_Key_keyLabelFlags, 0) + | savedDefaultKeyLabelFlags); + savedDefaultBackgroundType = row.getDefaultBackgroundType(); + // Override default backgroundType if exists. + row.setDefaultBackgroundType(keyAttr.getInt( + R.styleable.Keyboard_Key_backgroundType, + savedDefaultBackgroundType)); } } finally { keyboardAttr.recycle(); @@ -991,9 +1011,10 @@ public class Keyboard { parseMerge(parserForInclude, row, skip); } finally { if (row != null) { - // Restore default key width and key label flags. + // Restore default keyWidth, keyLabelFlags, and backgroundType. row.setDefaultKeyWidth(savedDefaultKeyWidth); row.setDefaultKeyLabelFlags(savedDefaultKeyLabelFlags); + row.setDefaultBackgroundType(savedDefaultBackgroundType); } parserForInclude.close(); } diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java index 67cb74f4d..f7981a320 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java @@ -52,6 +52,7 @@ public class KeyboardCodesSet { "key_action_next", "key_action_previous", "key_language_switch", + "key_research", "key_unspecified", "key_left_parenthesis", "key_right_parenthesis", @@ -86,6 +87,7 @@ public class KeyboardCodesSet { Keyboard.CODE_ACTION_NEXT, Keyboard.CODE_ACTION_PREVIOUS, Keyboard.CODE_LANGUAGE_SWITCH, + Keyboard.CODE_RESEARCH, Keyboard.CODE_UNSPECIFIED, CODE_LEFT_PARENTHESIS, CODE_RIGHT_PARENTHESIS, @@ -112,6 +114,7 @@ public class KeyboardCodesSet { DEFAULT[11], DEFAULT[12], DEFAULT[13], + DEFAULT[14], CODE_RIGHT_PARENTHESIS, CODE_LEFT_PARENTHESIS, CODE_GREATER_THAN_SIGN, diff --git a/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java index 34308dfb3..10e511eaf 100644 --- a/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java @@ -52,6 +52,9 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary { /** The number of contacts in the most recent dictionary rebuild. */ static private int sContactCountAtLastRebuild = 0; + /** The locale for this contacts dictionary. Controls name bigram predictions. */ + public final Locale mLocale; + private ContentObserver mObserver; /** @@ -61,6 +64,7 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary { public ContactsBinaryDictionary(final Context context, final int dicTypeId, Locale locale) { super(context, getFilenameWithLocale(NAME, locale.toString()), dicTypeId); + mLocale = locale; mUseFirstLastBigrams = useFirstLastBigramsForLocale(locale); registerObserver(context); diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 7092b4e7e..ae9e197a1 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -111,17 +111,17 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // TODO: migrate this to SettingsValues private int mSuggestionVisibility; - private static final int SUGGESTION_VISIBILILTY_SHOW_VALUE + private static final int SUGGESTION_VISIBILITY_SHOW_VALUE = R.string.prefs_suggestion_visibility_show_value; - private static final int SUGGESTION_VISIBILILTY_SHOW_ONLY_PORTRAIT_VALUE + private static final int SUGGESTION_VISIBILITY_SHOW_ONLY_PORTRAIT_VALUE = R.string.prefs_suggestion_visibility_show_only_portrait_value; - private static final int SUGGESTION_VISIBILILTY_HIDE_VALUE + private static final int SUGGESTION_VISIBILITY_HIDE_VALUE = R.string.prefs_suggestion_visibility_hide_value; private static final int[] SUGGESTION_VISIBILITY_VALUE_ARRAY = new int[] { - SUGGESTION_VISIBILILTY_SHOW_VALUE, - SUGGESTION_VISIBILILTY_SHOW_ONLY_PORTRAIT_VALUE, - SUGGESTION_VISIBILILTY_HIDE_VALUE + SUGGESTION_VISIBILITY_SHOW_VALUE, + SUGGESTION_VISIBILITY_SHOW_ONLY_PORTRAIT_VALUE, + SUGGESTION_VISIBILITY_HIDE_VALUE }; private static final int SPACE_STATE_NONE = 0; @@ -497,7 +497,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // Note that the calling sequence of onCreate() and onCurrentInputMethodSubtypeChanged() // is not guaranteed. It may even be called at the same time on a different thread. if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this); - mUserHistoryDictionary = new UserHistoryDictionary( + mUserHistoryDictionary = UserHistoryDictionary.getInstance( this, localeStr, Suggest.DIC_USER_HISTORY, mPrefs); mSuggest.setUserHistoryDictionary(mUserHistoryDictionary); } @@ -505,9 +505,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen /** * Resets the contacts dictionary in mSuggest according to the user settings. * - * This method takes an optional contacts dictionary to use. Since the contacts dictionary - * does not depend on the locale, it can be reused across different instances of Suggest. - * The dictionary will also be opened or closed as necessary depending on the settings. + * This method takes an optional contacts dictionary to use when the locale hasn't changed + * since the contacts dictionary can be opened or closed as necessary depending on the settings. * * @param oldContactsDictionary an optional dictionary to use, or null */ @@ -520,21 +519,35 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // so it's safe to call it anyways. if (null != oldContactsDictionary) oldContactsDictionary.close(); dictionaryToUse = null; - } else if (null != oldContactsDictionary) { - // Make sure the old contacts dictionary is opened. If it is already open, this is a - // no-op, so it's safe to call it anyways. - if (USE_BINARY_CONTACTS_DICTIONARY) { - ((ContactsBinaryDictionary)oldContactsDictionary).reopen(this); - } else { - ((ContactsDictionary)oldContactsDictionary).reopen(this); - } - dictionaryToUse = oldContactsDictionary; } else { - if (USE_BINARY_CONTACTS_DICTIONARY) { - dictionaryToUse = new ContactsBinaryDictionary(this, Suggest.DIC_CONTACTS, - mSubtypeSwitcher.getCurrentSubtypeLocale()); + final Locale locale = mSubtypeSwitcher.getCurrentSubtypeLocale(); + if (null != oldContactsDictionary) { + if (USE_BINARY_CONTACTS_DICTIONARY) { + ContactsBinaryDictionary oldContactsBinaryDictionary = + (ContactsBinaryDictionary)oldContactsDictionary; + if (!oldContactsBinaryDictionary.mLocale.equals(locale)) { + // If the locale has changed then recreate the contacts dictionary. This + // allows locale dependent rules for handling bigram name predictions. + oldContactsDictionary.close(); + dictionaryToUse = new ContactsBinaryDictionary( + this, Suggest.DIC_CONTACTS, locale); + } else { + // Make sure the old contacts dictionary is opened. If it is already open, + // this is a no-op, so it's safe to call it anyways. + oldContactsBinaryDictionary.reopen(this); + dictionaryToUse = oldContactsDictionary; + } + } else { + ((ContactsDictionary)oldContactsDictionary).reopen(this); + dictionaryToUse = oldContactsDictionary; + } } else { - dictionaryToUse = new ContactsDictionary(this, Suggest.DIC_CONTACTS); + if (USE_BINARY_CONTACTS_DICTIONARY) { + dictionaryToUse = new ContactsBinaryDictionary(this, Suggest.DIC_CONTACTS, + locale); + } else { + dictionaryToUse = new ContactsDictionary(this, Suggest.DIC_CONTACTS); + } } } @@ -1332,6 +1345,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen case Keyboard.CODE_LANGUAGE_SWITCH: handleLanguageSwitchKey(); break; + case Keyboard.CODE_RESEARCH: + if (ProductionFlag.IS_EXPERIMENTAL) { + ResearchLogger.getInstance().presentResearchDialog(this); + } + break; default: if (primaryCode == Keyboard.CODE_TAB && mInputAttributes.mEditorAction == EditorInfo.IME_ACTION_NEXT) { @@ -1724,8 +1742,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } public boolean isShowingSuggestionsStrip() { - return (mSuggestionVisibility == SUGGESTION_VISIBILILTY_SHOW_VALUE) - || (mSuggestionVisibility == SUGGESTION_VISIBILILTY_SHOW_ONLY_PORTRAIT_VALUE + return (mSuggestionVisibility == SUGGESTION_VISIBILITY_SHOW_VALUE) + || (mSuggestionVisibility == SUGGESTION_VISIBILITY_SHOW_ONLY_PORTRAIT_VALUE && mDisplayOrientation == Configuration.ORIENTATION_PORTRAIT); } @@ -2446,10 +2464,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final AlertDialog.Builder builder = new AlertDialog.Builder(this) .setItems(items, listener) .setTitle(title); - showOptionDialogInternal(builder.create()); + showOptionDialog(builder.create()); } - private void showOptionDialogInternal(AlertDialog dialog) { + /* package */ void showOptionDialog(AlertDialog dialog) { final IBinder windowToken = mKeyboardSwitcher.getKeyboardView().getWindowToken(); if (windowToken == null) return; diff --git a/java/src/com/android/inputmethod/latin/ResearchLogger.java b/java/src/com/android/inputmethod/latin/ResearchLogger.java index 16285091b..bb003f766 100644 --- a/java/src/com/android/inputmethod/latin/ResearchLogger.java +++ b/java/src/com/android/inputmethod/latin/ResearchLogger.java @@ -18,6 +18,8 @@ package com.android.inputmethod.latin; import static com.android.inputmethod.latin.Constants.Subtype.ExtraValue.KEYBOARD_LAYOUT_SET; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.inputmethodservice.InputMethodService; @@ -33,9 +35,9 @@ import android.view.MotionEvent; import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; +import android.widget.Toast; import com.android.inputmethod.keyboard.Key; -import com.android.inputmethod.keyboard.KeyDetector; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardId; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; @@ -134,12 +136,16 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } if (prefs != null) { sIsLogging = prefs.getBoolean(PREF_USABILITY_STUDY_MODE, false); - prefs.registerOnSharedPreferenceChangeListener(sInstance); + prefs.registerOnSharedPreferenceChangeListener(this); } } public synchronized void start() { Log.d(TAG, "start called"); + if (!sIsLogging) { + // Log.w(TAG, "not in usability mode; not logging"); + return; + } if (mFilesDir == null || !mFilesDir.exists()) { Log.w(TAG, "IME storage directory does not exist. Cannot start logging."); } else { @@ -192,16 +198,17 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); - } - mJsonWriter = NULL_JSON_WRITER; - mFile = null; - mLoggingState = LOGGING_STATE_OFF; - if (DEBUG) { - Log.d(TAG, "logfile closed"); - } - Log.d(TAG, "finished stop(), notifying"); - synchronized (ResearchLogger.this) { - ResearchLogger.this.notify(); + } finally { + mJsonWriter = NULL_JSON_WRITER; + mFile = null; + mLoggingState = LOGGING_STATE_OFF; + if (DEBUG) { + Log.d(TAG, "logfile closed"); + } + Log.d(TAG, "finished stop(), notifying"); + synchronized (ResearchLogger.this) { + ResearchLogger.this.notify(); + } } } }); @@ -213,6 +220,38 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } } + public synchronized boolean abort() { + Log.d(TAG, "abort called"); + boolean isLogFileDeleted = false; + if (mLoggingHandler != null && mLoggingState == LOGGING_STATE_ON) { + mLoggingState = LOGGING_STATE_STOPPING; + try { + Log.d(TAG, "closing jsonwriter"); + mJsonWriter.endArray(); + mJsonWriter.close(); + } catch (IllegalStateException e1) { + // assume that this is just the json not being terminated properly. + // ignore + e1.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + mJsonWriter = NULL_JSON_WRITER; + // delete file + final boolean isDeleted = mFile.delete(); + if (isDeleted) { + isLogFileDeleted = true; + } + mFile = null; + mLoggingState = LOGGING_STATE_OFF; + if (DEBUG) { + Log.d(TAG, "logfile closed"); + } + } + } + return isLogFileDeleted; + } + /* package */ synchronized void flush() { try { mJsonWriter.flush(); @@ -227,6 +266,50 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang return; } sIsLogging = prefs.getBoolean(PREF_USABILITY_STUDY_MODE, false); + if (sIsLogging == false) { + abort(); + } + } + + /* package */ void presentResearchDialog(final LatinIME latinIME) { + final CharSequence title = latinIME.getString(R.string.english_ime_research_log); + final CharSequence[] items = new CharSequence[] { + latinIME.getString(R.string.note_timestamp_for_researchlog), + latinIME.getString(R.string.do_not_log_this_session), + }; + final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface di, int position) { + di.dismiss(); + switch (position) { + case 0: + ResearchLogger.getInstance().userTimestamp(); + Toast.makeText(latinIME, R.string.notify_recorded_timestamp, + Toast.LENGTH_LONG).show(); + break; + case 1: + Toast toast = Toast.makeText(latinIME, + R.string.notify_session_log_deleting, Toast.LENGTH_LONG); + toast.show(); + final ResearchLogger logger = ResearchLogger.getInstance(); + boolean isLogDeleted = logger.abort(); + toast.cancel(); + if (isLogDeleted) { + Toast.makeText(latinIME, R.string.notify_session_log_deleted, + Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(latinIME, + R.string.notify_session_log_not_deleted, Toast.LENGTH_LONG) + .show(); + } + break; + } + } + }; + final AlertDialog.Builder builder = new AlertDialog.Builder(latinIME) + .setItems(items, listener) + .setTitle(title); + latinIME.showOptionDialog(builder.create()); } private static final String CURRENT_TIME_KEY = "_ct"; @@ -756,4 +839,11 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang getInstance().writeEvent(EVENTKEYS_SUGGESTIONSVIEW_SETSUGGESTIONS, values); } } + + private static final String[] EVENTKEYS_USER_TIMESTAMP = { + "UserTimestamp" + }; + public void userTimestamp() { + getInstance().writeEvent(EVENTKEYS_USER_TIMESTAMP, EVENTKEYS_NULLVALUES); + } } diff --git a/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java b/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java index a6ff8953d..9c54e0b81 100644 --- a/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java @@ -29,7 +29,9 @@ import android.util.Log; import com.android.inputmethod.latin.UserHistoryForgettingCurveUtils.ForgettingCurveParams; +import java.lang.ref.SoftReference; import java.util.HashMap; +import java.util.concurrent.ConcurrentHashMap; /** * Locally gathers stats about the words user types and various other signals like auto-correction @@ -38,6 +40,7 @@ import java.util.HashMap; public class UserHistoryDictionary extends ExpandableDictionary { private static final String TAG = "UserHistoryDictionary"; public static final boolean DBG_SAVE_RESTORE = false; + public static final boolean PROFILE_SAVE_RESTORE = LatinImeLogger.sDBG; /** Any pair being typed or picked */ private static final int FREQUENCY_FOR_TYPED = 2; @@ -77,13 +80,14 @@ public class UserHistoryDictionary extends ExpandableDictionary { /** Locale for which this user history dictionary is storing words */ private final String mLocale; - private UserHistoryDictionaryBigramList mBigramList = + private final UserHistoryDictionaryBigramList mBigramList = new UserHistoryDictionaryBigramList(); - private final Object mPendingWritesLock = new Object(); private static volatile boolean sUpdatingDB = false; private final SharedPreferences mPrefs; private final static HashMap<String, String> sDictProjectionMap; + private final static ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>> + sLangDictCache = new ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>>(); static { sDictProjectionMap = new HashMap<String, String>(); @@ -107,28 +111,48 @@ public class UserHistoryDictionary extends ExpandableDictionary { sDeleteHistoryBigrams = deleteHistoryBigram; } - public UserHistoryDictionary(final Context context, final String locale, final int dicTypeId, + public synchronized static UserHistoryDictionary getInstance( + final Context context, final String locale, + final int dictTypeId, final SharedPreferences sp) { + if (sLangDictCache.containsKey(locale)) { + final SoftReference<UserHistoryDictionary> ref = sLangDictCache.get(locale); + final UserHistoryDictionary dict = ref == null ? null : ref.get(); + if (dict != null) { + if (PROFILE_SAVE_RESTORE) { + Log.w(TAG, "Use cached UserHistoryDictionary for " + locale); + } + return dict; + } + } + final UserHistoryDictionary dict = + new UserHistoryDictionary(context, locale, dictTypeId, sp); + sLangDictCache.put(locale, new SoftReference<UserHistoryDictionary>(dict)); + return dict; + } + + private UserHistoryDictionary(final Context context, final String locale, final int dicTypeId, SharedPreferences sp) { super(context, dicTypeId); mLocale = locale; + mPrefs = sp; if (sOpenHelper == null) { sOpenHelper = new DatabaseHelper(getContext()); } if (mLocale != null && mLocale.length() > 1) { loadDictionary(); } - mPrefs = sp; } @Override public void close() { flushPendingWrites(); - SettingsValues.setLastUserHistoryWriteTime(mPrefs, mLocale); // Don't close the database as locale changes will require it to be reopened anyway // Also, the database is written to somewhat frequently, so it needs to be kept alive // throughout the life of the process. // mOpenHelper.close(); - super.close(); + // Ignore close because we cache UserHistoryDictionary for each language. See getInstance() + // above. + // super.close(); } /** @@ -160,7 +184,7 @@ public class UserHistoryDictionary extends ExpandableDictionary { } else { freq = super.setBigramAndGetFrequency(word1, word2, new ForgettingCurveParams(isValid)); } - synchronized (mPendingWritesLock) { + synchronized (mBigramList) { mBigramList.addBigram(word1, word2); } @@ -168,7 +192,7 @@ public class UserHistoryDictionary extends ExpandableDictionary { } public boolean cancelAddingUserHistory(String word1, String word2) { - synchronized (mPendingWritesLock) { + synchronized (mBigramList) { if (mBigramList.removeBigram(word1, word2)) { return super.removeBigram(word1, word2); } @@ -180,19 +204,17 @@ public class UserHistoryDictionary extends ExpandableDictionary { * Schedules a background thread to write any pending words to the database. */ private void flushPendingWrites() { - synchronized (mPendingWritesLock) { + synchronized (mBigramList) { // Nothing pending? Return if (mBigramList.isEmpty()) return; // Create a background thread to write the pending entries - new UpdateDbTask(sOpenHelper, mBigramList, mLocale, this).execute(); - // Create a new map for writing new entries into while the old one is written to db - mBigramList = new UserHistoryDictionaryBigramList(); + new UpdateDbTask(sOpenHelper, mBigramList, mLocale, this, mPrefs).execute(); } } /** Used for testing purpose **/ void waitUntilUpdateDBDone() { - synchronized (mPendingWritesLock) { + synchronized (mBigramList) { while (sUpdatingDB) { try { Thread.sleep(100); @@ -205,41 +227,48 @@ public class UserHistoryDictionary extends ExpandableDictionary { @Override public void loadDictionaryAsync() { - final long last = SettingsValues.getLastUserHistoryWriteTime(mPrefs, mLocale); - final long now = System.currentTimeMillis(); - // Load the words that correspond to the current input locale - final Cursor cursor = query(MAIN_COLUMN_LOCALE + "=?", new String[] { mLocale }); - if (null == cursor) return; - try { - if (cursor.moveToFirst()) { - final int word1Index = cursor.getColumnIndex(MAIN_COLUMN_WORD1); - final int word2Index = cursor.getColumnIndex(MAIN_COLUMN_WORD2); - final int fcIndex = cursor.getColumnIndex(COLUMN_FORGETTING_CURVE_VALUE); - while (!cursor.isAfterLast()) { - final String word1 = cursor.getString(word1Index); - final String word2 = cursor.getString(word2Index); - final int fc = cursor.getInt(fcIndex); - if (DBG_SAVE_RESTORE) { - Log.d(TAG, "--- Load user history: " + word1 + ", " + word2 + "," - + mLocale + "," + this); - } - // Safeguard against adding really long words. Stack may overflow due - // to recursive lookup - if (null == word1) { - super.addWord(word2, null /* shortcut */, fc); - } else if (word1.length() < BinaryDictionary.MAX_WORD_LENGTH - && word2.length() < BinaryDictionary.MAX_WORD_LENGTH) { - super.setBigramAndGetFrequency( - word1, word2, new ForgettingCurveParams(fc, now, last)); - } - synchronized(mPendingWritesLock) { + synchronized(mBigramList) { + final long last = SettingsValues.getLastUserHistoryWriteTime(mPrefs, mLocale); + final boolean initializing = last == 0; + final long now = System.currentTimeMillis(); + // Load the words that correspond to the current input locale + final Cursor cursor = query(MAIN_COLUMN_LOCALE + "=?", new String[] { mLocale }); + if (null == cursor) return; + try { + if (cursor.moveToFirst()) { + final int word1Index = cursor.getColumnIndex(MAIN_COLUMN_WORD1); + final int word2Index = cursor.getColumnIndex(MAIN_COLUMN_WORD2); + final int fcIndex = cursor.getColumnIndex(COLUMN_FORGETTING_CURVE_VALUE); + while (!cursor.isAfterLast()) { + final String word1 = cursor.getString(word1Index); + final String word2 = cursor.getString(word2Index); + final int fc = cursor.getInt(fcIndex); + if (DBG_SAVE_RESTORE) { + Log.d(TAG, "--- Load user history: " + word1 + ", " + word2 + "," + + mLocale + "," + this); + } + // Safeguard against adding really long words. Stack may overflow due + // to recursive lookup + if (null == word1) { + super.addWord(word2, null /* shortcut */, fc); + } else if (word1.length() < BinaryDictionary.MAX_WORD_LENGTH + && word2.length() < BinaryDictionary.MAX_WORD_LENGTH) { + super.setBigramAndGetFrequency( + word1, word2, initializing ? new ForgettingCurveParams(true) + : new ForgettingCurveParams(fc, now, last)); + } mBigramList.addBigram(word1, word2, (byte)fc); + cursor.moveToNext(); } - cursor.moveToNext(); + } + } finally { + cursor.close(); + if (PROFILE_SAVE_RESTORE) { + final long diff = System.currentTimeMillis() - now; + Log.w(TAG, "PROF: Load User HistoryDictionary: " + + mLocale + ", " + diff + "ms."); } } - } finally { - cursor.close(); } } @@ -317,14 +346,16 @@ public class UserHistoryDictionary extends ExpandableDictionary { private final DatabaseHelper mDbHelper; private final String mLocale; private final UserHistoryDictionary mUserHistoryDictionary; + private final SharedPreferences mPrefs; public UpdateDbTask( DatabaseHelper openHelper, UserHistoryDictionaryBigramList pendingWrites, - String locale, UserHistoryDictionary dict) { + String locale, UserHistoryDictionary dict, SharedPreferences prefs) { mBigramList = pendingWrites; mLocale = locale; mDbHelper = openHelper; mUserHistoryDictionary = dict; + mPrefs = prefs; } /** Prune any old data if the database is getting too big. */ @@ -363,37 +394,39 @@ public class UserHistoryDictionary extends ExpandableDictionary { @Override protected Void doInBackground(Void... v) { - SQLiteDatabase db = null; - try { - db = mDbHelper.getWritableDatabase(); - } catch (android.database.sqlite.SQLiteCantOpenDatabaseException e) { - // If we can't open the db, don't do anything. Exit through the next test - // for non-nullity of the db variable. - } - if (null == db) { - // Not much we can do. Just exit. - sUpdatingDB = false; - return null; - } - db.execSQL("PRAGMA foreign_keys = ON;"); - final boolean addLevel0Bigram = mBigramList.size() <= sMaxHistoryBigrams; - - // Write all the entries to the db - for (String word1 : mBigramList.keySet()) { - final HashMap<String, Byte> word1Bigrams = mBigramList.getBigrams(word1); - for (String word2 : word1Bigrams.keySet()) { - // Get new frequency. Do not insert shortcuts/bigrams which freq is "-1". - final int freq; // -1, or 0~255 - if (word1 == null) { - freq = FREQUENCY_FOR_TYPED; - } else { - final NextWord nw = mUserHistoryDictionary.getBigramWord(word1, word2); - if (nw != null) { - final ForgettingCurveParams fcp = nw.getFcParams(); + synchronized(mBigramList) { + final long now = PROFILE_SAVE_RESTORE ? System.currentTimeMillis() : 0; + int profTotal = 0; + int profInsert = 0; + int profDelete = 0; + SQLiteDatabase db = null; + try { + db = mDbHelper.getWritableDatabase(); + } catch (android.database.sqlite.SQLiteCantOpenDatabaseException e) { + // If we can't open the db, don't do anything. Exit through the next test + // for non-nullity of the db variable. + } + if (null == db) { + // Not much we can do. Just exit. + sUpdatingDB = false; + return null; + } + db.execSQL("PRAGMA foreign_keys = ON;"); + final boolean addLevel0Bigram = mBigramList.size() <= sMaxHistoryBigrams; + + // Write all the entries to the db + for (String word1 : mBigramList.keySet()) { + final HashMap<String, Byte> word1Bigrams = mBigramList.getBigrams(word1); + for (String word2 : word1Bigrams.keySet()) { + if (PROFILE_SAVE_RESTORE) { + ++profTotal; + } + // Get new frequency. Do not insert unigrams/bigrams which freq is "-1". + final int freq; // -1, or 0~255 + if (word1 == null) { // unigram + freq = FREQUENCY_FOR_TYPED; final byte prevFc = word1Bigrams.get(word2); - final byte fc = (byte)fcp.getFc(); - final boolean isValid = fcp.isValid(); - if (prevFc > 0 && prevFc == fc) { + if (prevFc == FREQUENCY_FOR_TYPED) { // No need to update since we found no changes for this entry. // Just skip to the next entry. if (DBG_SAVE_RESTORE) { @@ -401,67 +434,100 @@ public class UserHistoryDictionary extends ExpandableDictionary { + "," + prevFc); } continue; - } else if (UserHistoryForgettingCurveUtils. - needsToSave(fc, isValid, addLevel0Bigram)) { - freq = fc; + } + } else { // bigram + final NextWord nw = mUserHistoryDictionary.getBigramWord(word1, word2); + if (nw != null) { + final ForgettingCurveParams fcp = nw.getFcParams(); + final byte prevFc = word1Bigrams.get(word2); + final byte fc = (byte)fcp.getFc(); + final boolean isValid = fcp.isValid(); + if (prevFc > 0 && prevFc == fc) { + // No need to update since we found no changes for this entry. + // Just skip to the next entry. + if (DBG_SAVE_RESTORE) { + Log.d(TAG, "Skip update user history: " + word1 + "," + + word2 + "," + prevFc); + } + continue; + } else if (UserHistoryForgettingCurveUtils. + needsToSave(fc, isValid, addLevel0Bigram)) { + freq = fc; + } else { + freq = -1; + } } else { freq = -1; } - } else { - freq = -1; - } - } - // TODO: this process of making a text search for each pair each time - // is terribly inefficient. Optimize this. - // Find pair id - Cursor c = null; - try { - if (null != word1) { - c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID }, - MAIN_COLUMN_WORD1 + "=? AND " + MAIN_COLUMN_WORD2 + "=? AND " - + MAIN_COLUMN_LOCALE + "=?", - new String[] { word1, word2, mLocale }, null, null, - null); - } else { - c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID }, - MAIN_COLUMN_WORD1 + " IS NULL AND " + MAIN_COLUMN_WORD2 - + "=? AND " + MAIN_COLUMN_LOCALE + "=?", - new String[] { word2, mLocale }, null, null, null); } + // TODO: this process of making a text search for each pair each time + // is terribly inefficient. Optimize this. + // Find pair id + Cursor c = null; + try { + if (null != word1) { + c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID }, + MAIN_COLUMN_WORD1 + "=? AND " + MAIN_COLUMN_WORD2 + "=? AND " + + MAIN_COLUMN_LOCALE + "=?", + new String[] { word1, word2, mLocale }, null, null, + null); + } else { + c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID }, + MAIN_COLUMN_WORD1 + " IS NULL AND " + MAIN_COLUMN_WORD2 + + "=? AND " + MAIN_COLUMN_LOCALE + "=?", + new String[] { word2, mLocale }, null, null, null); + } - final int pairId; - if (c.moveToFirst()) { - // Delete existing pair - pairId = c.getInt(c.getColumnIndex(MAIN_COLUMN_ID)); - db.delete(FREQ_TABLE_NAME, FREQ_COLUMN_PAIR_ID + "=?", - new String[] { Integer.toString(pairId) }); - } else { - // Create new pair - Long pairIdLong = db.insert(MAIN_TABLE_NAME, null, - getContentValues(word1, word2, mLocale)); - pairId = pairIdLong.intValue(); - } - if (freq > 0) { - if (DBG_SAVE_RESTORE) { - Log.d(TAG, "--- Save user history: " + word1 + ", " + word2 - + mLocale + "," + this); + final int pairId; + if (c.moveToFirst()) { + if (PROFILE_SAVE_RESTORE) { + ++profDelete; + } + // Delete existing pair + pairId = c.getInt(c.getColumnIndex(MAIN_COLUMN_ID)); + db.delete(FREQ_TABLE_NAME, FREQ_COLUMN_PAIR_ID + "=?", + new String[] { Integer.toString(pairId) }); + } else { + // Create new pair + Long pairIdLong = db.insert(MAIN_TABLE_NAME, null, + getContentValues(word1, word2, mLocale)); + pairId = pairIdLong.intValue(); + } + if (freq > 0) { + if (PROFILE_SAVE_RESTORE) { + ++profInsert; + } + if (DBG_SAVE_RESTORE) { + Log.d(TAG, "--- Save user history: " + word1 + ", " + word2 + + mLocale + "," + this); + } + // Insert new frequency + db.insert(FREQ_TABLE_NAME, null, + getFrequencyContentValues(pairId, freq)); + // Update an existing bigram entry in mBigramList too in order to + // synchronize the SQL DB and mBigramList. + mBigramList.updateBigram(word1, word2, (byte)freq); + } + } finally { + if (c != null) { + c.close(); } - // Insert new frequency - db.insert(FREQ_TABLE_NAME, null, - getFrequencyContentValues(pairId, freq)); - } - } finally { - if (c != null) { - c.close(); } } } - } - checkPruneData(db); - sUpdatingDB = false; - - return null; + checkPruneData(db); + // Save the timestamp after we finish writing the SQL DB. + SettingsValues.setLastUserHistoryWriteTime(mPrefs, mLocale); + sUpdatingDB = false; + if (PROFILE_SAVE_RESTORE) { + final long diff = System.currentTimeMillis() - now; + Log.w(TAG, "PROF: Write User HistoryDictionary: " + mLocale + ", "+ diff + + "ms. Total: " + profTotal + ". Insert: " + profInsert + ". Delete: " + + profDelete); + } + return null; + } // synchronized } private static ContentValues getContentValues(String word1, String word2, String locale) { diff --git a/java/src/com/android/inputmethod/latin/UserHistoryDictionaryBigramList.java b/java/src/com/android/inputmethod/latin/UserHistoryDictionaryBigramList.java index 96400dd48..28847745e 100644 --- a/java/src/com/android/inputmethod/latin/UserHistoryDictionaryBigramList.java +++ b/java/src/com/android/inputmethod/latin/UserHistoryDictionaryBigramList.java @@ -39,13 +39,19 @@ public class UserHistoryDictionaryBigramList { mBigramMap.clear(); } + /** + * Called when the user typed a word. + */ public void addBigram(String word1, String word2) { addBigram(word1, word2, FORGETTING_CURVE_INITIAL_VALUE); } + /** + * Called when loaded from the SQL DB. + */ public void addBigram(String word1, String word2, byte fcValue) { if (UserHistoryDictionary.DBG_SAVE_RESTORE) { - Log.d(TAG, "--- add bigram: " + word1 + ", " + word2); + Log.d(TAG, "--- add bigram: " + word1 + ", " + word2 + ", " + fcValue); } final HashMap<String, Byte> map; if (mBigramMap.containsKey(word1)) { @@ -60,6 +66,25 @@ public class UserHistoryDictionaryBigramList { } } + /** + * Called when inserted to the SQL DB. + */ + public void updateBigram(String word1, String word2, byte fcValue) { + if (UserHistoryDictionary.DBG_SAVE_RESTORE) { + Log.d(TAG, "--- update bigram: " + word1 + ", " + word2 + ", " + fcValue); + } + final HashMap<String, Byte> map; + if (mBigramMap.containsKey(word1)) { + map = mBigramMap.get(word1); + } else { + return; + } + if (!map.containsKey(word2)) { + return; + } + map.put(word2, fcValue); + } + public int size() { return mSize; } diff --git a/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java b/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java index 9cd8c6778..e5516dc62 100644 --- a/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java +++ b/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java @@ -30,6 +30,7 @@ public class UserHistoryForgettingCurveUtils { private static final long ELAPSED_TIME_INTERVAL_MILLIS = ELAPSED_TIME_INTERVAL_HOURS * DateUtils.HOUR_IN_MILLIS; private static final int HALF_LIFE_HOURS = 48; + private static final int MAX_PUSH_ELAPSED = (FC_LEVEL_MAX + 1) * (ELAPSED_TIME_MAX + 1); private UserHistoryForgettingCurveUtils() { // This utility class is not publicly instantiable. @@ -94,6 +95,11 @@ public class UserHistoryForgettingCurveUtils { if (elapsedTimeCount <= 0) { return; } + if (elapsedTimeCount >= MAX_PUSH_ELAPSED) { + mLastTouchedTime = now; + mFc = 0; + return; + } for (int i = 0; i < elapsedTimeCount; ++i) { mLastTouchedTime += ELAPSED_TIME_INTERVAL_MILLIS; mFc = pushElapsedTime(mFc); diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 802322ccd..d34cad205 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -372,27 +372,32 @@ public class AndroidSpellCheckerService extends SpellCheckerService mUserDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>()); final Map<String, Dictionary> oldWhitelistDictionaries = mWhitelistDictionaries; mWhitelistDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>()); - for (DictionaryPool pool : oldPools.values()) { - pool.close(); - } - for (Dictionary dict : oldUserDictionaries.values()) { - dict.close(); - } - for (Dictionary dict : oldWhitelistDictionaries.values()) { - dict.close(); - } - synchronized (mUseContactsLock) { - if (null != mContactsDictionary) { - // The synchronously loaded contacts dictionary should have been in one - // or several pools, but it is shielded against multiple closing and it's - // safe to call it several times. - final Dictionary dictToClose = mContactsDictionary; - // TODO: revert to the concrete type when USE_BINARY_CONTACTS_DICTIONARY is no - // longer needed - mContactsDictionary = null; - dictToClose.close(); + new Thread("spellchecker_close_dicts") { + @Override + public void run() { + for (DictionaryPool pool : oldPools.values()) { + pool.close(); + } + for (Dictionary dict : oldUserDictionaries.values()) { + dict.close(); + } + for (Dictionary dict : oldWhitelistDictionaries.values()) { + dict.close(); + } + synchronized (mUseContactsLock) { + if (null != mContactsDictionary) { + // The synchronously loaded contacts dictionary should have been in one + // or several pools, but it is shielded against multiple closing and it's + // safe to call it several times. + final Dictionary dictToClose = mContactsDictionary; + // TODO: revert to the concrete type when USE_BINARY_CONTACTS_DICTIONARY + // is no longer needed + mContactsDictionary = null; + dictToClose.close(); + } + } } - } + }.start(); } private DictionaryPool getDictionaryPool(final String locale) { |