diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
22 files changed, 1529 insertions, 138 deletions
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 18e712212..c8c7bb456 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -139,19 +139,31 @@ public final class BinaryDictionary extends Dictionary { inputSize, 0 /* commitPoint */, isGesture, prevWordCodePointArray, mUseFullEditDistance, mOutputCodePoints, mOutputScores, mSpaceIndices, mOutputTypes); + final boolean blockPotentiallyOffensive = + Settings.getInstance().getBlockPotentiallyOffensive(); final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList(); for (int j = 0; j < count; ++j) { - if (composerSize > 0 && mOutputScores[j] < 1) break; final int start = j * MAX_WORD_LENGTH; int len = 0; while (len < MAX_WORD_LENGTH && mOutputCodePoints[start + len] != 0) { ++len; } if (len > 0) { - final int score = SuggestedWordInfo.KIND_WHITELIST == mOutputTypes[j] + final int flags = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_FLAGS; + if (blockPotentiallyOffensive + && 0 != (flags & SuggestedWordInfo.KIND_FLAG_POSSIBLY_OFFENSIVE) + && 0 == (flags & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH)) { + // If we block potentially offensive words, and if the word is possibly + // offensive, then we don't output it unless it's also an exact match. + continue; + } + final int kind = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_KIND; + final int score = SuggestedWordInfo.KIND_WHITELIST == kind ? SuggestedWordInfo.MAX_SCORE : mOutputScores[j]; + // TODO: check that all users of the `kind' parameter are ready to accept + // flags too and pass mOutputTypes[j] instead of kind suggestions.add(new SuggestedWordInfo(new String(mOutputCodePoints, start, len), - score, mOutputTypes[j], mDictType)); + score, kind, mDictType)); } } return suggestions; diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java index 42f713697..a9b58de44 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java @@ -81,6 +81,7 @@ public final class BinaryDictionaryFileDumper { private static final String QUERY_PATH_METADATA = "metadata"; private static final String INSERT_METADATA_CLIENT_ID_COLUMN = "clientid"; private static final String INSERT_METADATA_METADATA_URI_COLUMN = "uri"; + private static final String INSERT_METADATA_METADATA_ADDITIONAL_ID_COLUMN = "additionalid"; // Prevents this class to be accidentally instantiated. private BinaryDictionaryFileDumper() { @@ -209,7 +210,7 @@ public final class BinaryDictionaryFileDumper { * to the cache file name designated by its id and locale, overwriting it if already present * and creating it (and its containing directory) if necessary. */ - private static AssetFileAddress cacheWordList(final String wordlistId, final String locale, + private static void cacheWordList(final String wordlistId, final String locale, final ContentProviderClient providerClient, final Context context) { final int COMPRESSED_CRYPTED_COMPRESSED = 0; final int CRYPTED_COMPRESSED = 1; @@ -227,7 +228,7 @@ public final class BinaryDictionaryFileDumper { providerClient, QUERY_PATH_DATAFILE, wordlistId /* extraPath */); } catch (RemoteException e) { Log.e(TAG, "Can't communicate with the dictionary pack", e); - return null; + return; } final String finalFileName = DictionaryInfoUtils.getCacheFileName(wordlistId, locale, context); @@ -236,11 +237,11 @@ public final class BinaryDictionaryFileDumper { tempFileName = BinaryDictionaryGetter.getTempFileName(wordlistId, context); } catch (IOException e) { Log.e(TAG, "Can't open the temporary file", e); - return null; + return; } for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) { - InputStream originalSourceStream = null; + final InputStream originalSourceStream; InputStream inputStream = null; InputStream uncompressedStream = null; InputStream decryptedStream = null; @@ -253,7 +254,7 @@ public final class BinaryDictionaryFileDumper { // Open input. afd = openAssetFileDescriptor(providerClient, wordListUri); // If we can't open it at all, don't even try a number of times. - if (null == afd) return null; + if (null == afd) return; originalSourceStream = afd.createInputStream(); // Open output. outputFile = new File(tempFileName); @@ -304,7 +305,7 @@ public final class BinaryDictionaryFileDumper { } BinaryDictionaryGetter.removeFilesWithIdExcept(context, wordlistId, finalFile); // Success! Close files (through the finally{} clause) and return. - return AssetFileAddress.makeFromFileName(finalFileName); + return; } catch (Exception e) { if (DEBUG) { Log.i(TAG, "Can't open word list in mode " + mode, e); @@ -319,7 +320,7 @@ public final class BinaryDictionaryFileDumper { } finally { // Ignore exceptions while closing files. try { - // inputStream.close() will close afd, we should not call afd.close(). + if (null != afd) afd.close(); if (null != inputStream) inputStream.close(); if (null != uncompressedStream) uncompressedStream.close(); if (null != decryptedStream) decryptedStream.close(); @@ -349,7 +350,6 @@ public final class BinaryDictionaryFileDumper { } catch (RemoteException e) { Log.e(TAG, "In addition, communication with the dictionary provider was cut", e); } - return null; } /** @@ -358,30 +358,23 @@ public final class BinaryDictionaryFileDumper { * This will query a content provider for word list data for a given locale, and copy the * files locally so that they can be mmap'ed. This may overwrite previously cached word lists * with newer versions if a newer version is made available by the content provider. - * @returns the addresses of the word list files, or null if no data could be obtained. * @throw FileNotFoundException if the provider returns non-existent data. * @throw IOException if the provider-returned data could not be read. */ - public static List<AssetFileAddress> cacheWordListsFromContentProvider(final Locale locale, + public static void cacheWordListsFromContentProvider(final Locale locale, final Context context, final boolean hasDefaultWordList) { final ContentProviderClient providerClient = context.getContentResolver(). acquireContentProviderClient(getProviderUriBuilder("").build()); if (null == providerClient) { Log.e(TAG, "Can't establish communication with the dictionary provider"); - return CollectionUtils.newArrayList(); + return; } try { final List<WordListInfo> idList = getWordListWordListInfos(locale, context, hasDefaultWordList); - final ArrayList<AssetFileAddress> fileAddressList = CollectionUtils.newArrayList(); for (WordListInfo id : idList) { - final AssetFileAddress afd = - cacheWordList(id.mId, id.mLocale, providerClient, context); - if (null != afd) { - fileAddressList.add(afd); - } + cacheWordList(id.mId, id.mLocale, providerClient, context); } - return fileAddressList; } finally { providerClient.release(); } @@ -423,6 +416,7 @@ public final class BinaryDictionaryFileDumper { private static void reinitializeClientRecordInDictionaryContentProvider(final Context context, final ContentProviderClient client, final String clientId) throws RemoteException { final String metadataFileUri = MetadataFileUriGetter.getMetadataUri(context); + final String metadataAdditionalId = MetadataFileUriGetter.getMetadataAdditionalId(context); if (TextUtils.isEmpty(metadataFileUri)) return; // Tell the content provider to reset all information about this client id final Uri metadataContentUri = getProviderUriBuilder(clientId) @@ -434,6 +428,7 @@ public final class BinaryDictionaryFileDumper { final ContentValues metadataValues = new ContentValues(); metadataValues.put(INSERT_METADATA_CLIENT_ID_COLUMN, clientId); metadataValues.put(INSERT_METADATA_METADATA_URI_COLUMN, metadataFileUri); + metadataValues.put(INSERT_METADATA_METADATA_ADDITIONAL_ID_COLUMN, metadataAdditionalId); client.insert(metadataContentUri, metadataValues); // Update the dictionary list. diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 294312843..98eadcacb 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -72,10 +72,16 @@ final class BinaryDictionaryGetter { public static String getTempFileName(final String id, final Context context) throws IOException { final String safeId = DictionaryInfoUtils.replaceFileNameDangerousCharacters(id); + final File directory = new File(DictionaryInfoUtils.getWordListTempDirectory(context)); + if (!directory.exists()) { + if (!directory.mkdirs()) { + Log.e(TAG, "Could not create the temporary directory"); + } + } // If the first argument is less than three chars, createTempFile throws a // RuntimeException. We don't really care about what name we get, so just // put a three-chars prefix makes us safe. - return File.createTempFile("xxx" + safeId, null).getAbsolutePath(); + return File.createTempFile("xxx" + safeId, null, directory).getAbsolutePath(); } /** @@ -89,8 +95,16 @@ final class BinaryDictionaryGetter { + fallbackResId); return null; } - return AssetFileAddress.makeFromFileNameAndOffset( - context.getApplicationInfo().sourceDir, afd.getStartOffset(), afd.getLength()); + try { + return AssetFileAddress.makeFromFileNameAndOffset( + context.getApplicationInfo().sourceDir, afd.getStartOffset(), afd.getLength()); + } finally { + try { + afd.close(); + } catch (IOException e) { + // Ignored + } + } } private static final class DictPackSettings { @@ -276,9 +290,6 @@ final class BinaryDictionaryGetter { final Context context) { final boolean hasDefaultWordList = DictionaryFactory.isDictionaryAvailable(context, locale); - // cacheWordListsFromContentProvider returns the list of files it copied to local - // storage, but we don't really care about what was copied NOW: what we want is the - // list of everything we ever cached, so we ignore the return value. // TODO: The development-only-diagnostic version is not supported by the Dictionary Pack // Service yet if (!ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { diff --git a/java/src/com/android/inputmethod/latin/DebugSettings.java b/java/src/com/android/inputmethod/latin/DebugSettings.java index c2aade64d..5969a63de 100644 --- a/java/src/com/android/inputmethod/latin/DebugSettings.java +++ b/java/src/com/android/inputmethod/latin/DebugSettings.java @@ -121,18 +121,8 @@ public final class DebugSettings extends PreferenceFragment return; } boolean isDebugMode = mDebugMode.isChecked(); - String version = ""; - try { - final Context context = getActivity(); - if (context == null) { - return; - } - final String packageName = context.getPackageName(); - PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); - version = "Version " + info.versionName; - } catch (NameNotFoundException e) { - Log.e(TAG, "Could not find version info."); - } + final String version = getResources().getString( + R.string.version_text, Utils.getVersionName(getActivity())); if (!isDebugMode) { mDebugMode.setTitle(version); mDebugMode.setSummary(""); diff --git a/java/src/com/android/inputmethod/latin/DictionaryInfoUtils.java b/java/src/com/android/inputmethod/latin/DictionaryInfoUtils.java index dcfa483f8..df7bad8d0 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryInfoUtils.java +++ b/java/src/com/android/inputmethod/latin/DictionaryInfoUtils.java @@ -129,6 +129,13 @@ public class DictionaryInfoUtils { } /** + * Helper method to get the top level temp directory. + */ + public static String getWordListTempDirectory(final Context context) { + return context.getFilesDir() + File.separator + "tmp"; + } + + /** * Reverse escaping done by replaceFileNameDangerousCharacters. */ public static String getWordListIdFromFileName(final String fname) { diff --git a/java/src/com/android/inputmethod/latin/FeedbackUtils.java b/java/src/com/android/inputmethod/latin/FeedbackUtils.java index 1e5260e34..0582763fe 100644 --- a/java/src/com/android/inputmethod/latin/FeedbackUtils.java +++ b/java/src/com/android/inputmethod/latin/FeedbackUtils.java @@ -17,6 +17,7 @@ package com.android.inputmethod.latin; import android.content.Context; +import android.content.Intent; public class FeedbackUtils { public static boolean isFeedbackFormSupported() { @@ -25,4 +26,12 @@ public class FeedbackUtils { public static void showFeedbackForm(Context context) { } + + public static int getAboutKeyboardTitleResId() { + return 0; + } + + public static Intent getAboutKeyboardIntent(Context context) { + return null; + } } diff --git a/java/src/com/android/inputmethod/latin/MetadataFileUriGetter.java b/java/src/com/android/inputmethod/latin/MetadataFileUriGetter.java index e6dc6db8f..a98ecc7b6 100644 --- a/java/src/com/android/inputmethod/latin/MetadataFileUriGetter.java +++ b/java/src/com/android/inputmethod/latin/MetadataFileUriGetter.java @@ -19,10 +19,18 @@ package com.android.inputmethod.latin; import android.content.Context; /** - * Helper class to get the metadata URI. + * Helper class to get the metadata URI and the additional ID. */ public class MetadataFileUriGetter { - public static String getMetadataUri(Context context) { + private MetadataFileUriGetter() { + // This helper class is not instantiable. + } + + public static String getMetadataUri(final Context context) { return context.getString(R.string.dictionary_pack_metadata_uri); } + + public static String getMetadataAdditionalId(final Context context) { + return ""; + } } diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java index e39aae958..3f7be99e5 100644 --- a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java +++ b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java @@ -22,6 +22,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.IBinder; import android.preference.PreferenceManager; +import android.util.Log; import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodSubtype; @@ -46,6 +47,8 @@ public final class RichInputMethodManager { private InputMethodManagerCompatWrapper mImmWrapper; private InputMethodInfo mInputMethodInfoOfThisIme; + private static final int INDEX_NOT_FOUND = -1; + public static RichInputMethodManager getInstance() { sInstance.checkInitialized(); return sInstance; @@ -98,11 +101,100 @@ public final class RichInputMethodManager { } public boolean switchToNextInputMethod(final IBinder token, final boolean onlyCurrentIme) { - final boolean result = mImmWrapper.switchToNextInputMethod(token, onlyCurrentIme); - if (!result) { - mImmWrapper.mImm.switchToLastInputMethod(token); + if (mImmWrapper.switchToNextInputMethod(token, onlyCurrentIme)) { + return true; + } + // Was not able to call {@link InputMethodManager#switchToNextInputMethodIBinder,boolean)} + // because the current device is running ICS or previous and lacks the API. + if (switchToNextInputSubtypeInThisIme(token, onlyCurrentIme)) { + return true; + } + return switchToNextInputMethodAndSubtype(token); + } + + private boolean switchToNextInputSubtypeInThisIme(final IBinder token, + final boolean onlyCurrentIme) { + final InputMethodManager imm = mImmWrapper.mImm; + final InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype(); + final List<InputMethodSubtype> enabledSubtypes = imm.getEnabledInputMethodSubtypeList( + mInputMethodInfoOfThisIme, true /* allowsImplicitlySelectedSubtypes */); + final int currentIndex = getSubtypeIndexInList(currentSubtype, enabledSubtypes); + if (currentIndex == INDEX_NOT_FOUND) { + Log.w(TAG, "Can't find current subtype in enabled subtypes: subtype=" + + SubtypeLocale.getSubtypeDisplayName(currentSubtype)); + return false; + } + final int nextIndex = (currentIndex + 1) % enabledSubtypes.size(); + if (nextIndex <= currentIndex && !onlyCurrentIme) { + // The current subtype is the last or only enabled one and it needs to switch to + // next IME. return false; } + final InputMethodSubtype nextSubtype = enabledSubtypes.get(nextIndex); + setInputMethodAndSubtype(token, nextSubtype); + return true; + } + + private boolean switchToNextInputMethodAndSubtype(final IBinder token) { + final InputMethodManager imm = mImmWrapper.mImm; + final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); + final int currentIndex = getImiIndexInList(mInputMethodInfoOfThisIme, enabledImis); + if (currentIndex == INDEX_NOT_FOUND) { + Log.w(TAG, "Can't find current IME in enabled IMEs: IME package=" + + mInputMethodInfoOfThisIme.getPackageName()); + return false; + } + final InputMethodInfo nextImi = getNextNonAuxiliaryIme(currentIndex, enabledImis); + final List<InputMethodSubtype> enabledSubtypes = imm.getEnabledInputMethodSubtypeList( + nextImi, true /* allowsImplicitlySelectedSubtypes */); + if (enabledSubtypes.isEmpty()) { + // The next IME has no subtype. + imm.setInputMethod(token, nextImi.getId()); + return true; + } + final InputMethodSubtype firstSubtype = enabledSubtypes.get(0); + imm.setInputMethodAndSubtype(token, nextImi.getId(), firstSubtype); + return true; + } + + private static int getImiIndexInList(final InputMethodInfo inputMethodInfo, + final List<InputMethodInfo> imiList) { + final int count = imiList.size(); + for (int index = 0; index < count; index++) { + final InputMethodInfo imi = imiList.get(index); + if (imi.equals(inputMethodInfo)) { + return index; + } + } + return INDEX_NOT_FOUND; + } + + // This method mimics {@link InputMethodManager#switchToNextInputMethod(IBinder,boolean)}. + private static InputMethodInfo getNextNonAuxiliaryIme(final int currentIndex, + final List<InputMethodInfo> imiList) { + final int count = imiList.size(); + for (int i = 1; i < count; i++) { + final int nextIndex = (currentIndex + i) % count; + final InputMethodInfo nextImi = imiList.get(nextIndex); + if (!isAuxiliaryIme(nextImi)) { + return nextImi; + } + } + return imiList.get(currentIndex); + } + + // Copied from {@link InputMethodInfo}. See how auxiliary of IME is determined. + private static boolean isAuxiliaryIme(final InputMethodInfo imi) { + final int count = imi.getSubtypeCount(); + if (count == 0) { + return false; + } + for (int index = 0; index < count; index++) { + final InputMethodSubtype subtype = imi.getSubtypeAt(index); + if (!subtype.isAuxiliary()) { + return false; + } + } return true; } @@ -136,24 +228,35 @@ public final class RichInputMethodManager { private static boolean checkIfSubtypeBelongsToList(final InputMethodSubtype subtype, final List<InputMethodSubtype> subtypes) { - for (final InputMethodSubtype ims : subtypes) { + return getSubtypeIndexInList(subtype, subtypes) != INDEX_NOT_FOUND; + } + + private static int getSubtypeIndexInList(final InputMethodSubtype subtype, + final List<InputMethodSubtype> subtypes) { + final int count = subtypes.size(); + for (int index = 0; index < count; index++) { + final InputMethodSubtype ims = subtypes.get(index); if (ims.equals(subtype)) { - return true; + return index; } } - return false; + return INDEX_NOT_FOUND; } public boolean checkIfSubtypeBelongsToThisIme(final InputMethodSubtype subtype) { - final InputMethodInfo myImi = mInputMethodInfoOfThisIme; - final int count = myImi.getSubtypeCount(); - for (int i = 0; i < count; i++) { - final InputMethodSubtype ims = myImi.getSubtypeAt(i); + return getSubtypeIndexInIme(subtype, mInputMethodInfoOfThisIme) != INDEX_NOT_FOUND; + } + + private static int getSubtypeIndexInIme(final InputMethodSubtype subtype, + final InputMethodInfo imi) { + final int count = imi.getSubtypeCount(); + for (int index = 0; index < count; index++) { + final InputMethodSubtype ims = imi.getSubtypeAt(index); if (ims.equals(subtype)) { - return true; + return index; } } - return false; + return INDEX_NOT_FOUND; } public InputMethodSubtype getCurrentInputMethodSubtype( diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java index 72e08700a..9fefb58a6 100644 --- a/java/src/com/android/inputmethod/latin/Settings.java +++ b/java/src/com/android/inputmethod/latin/Settings.java @@ -36,6 +36,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang public static final String PREF_POPUP_ON = "popup_on"; public static final String PREF_VOICE_MODE = "voice_mode"; public static final String PREF_CORRECTION_SETTINGS = "correction_settings"; + public static final String PREF_EDIT_PERSONAL_DICTIONARY = "edit_personal_dictionary"; public static final String PREF_CONFIGURE_DICTIONARIES_KEY = "configure_dictionaries_key"; public static final String PREF_AUTO_CORRECTION_THRESHOLD = "auto_correction_threshold"; public static final String PREF_SHOW_SUGGESTIONS_SETTING = "show_suggestions_setting"; @@ -46,6 +47,8 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang public static final String PREF_KEY_USE_CONTACTS_DICT = "pref_key_use_contacts_dict"; public static final String PREF_KEY_USE_DOUBLE_SPACE_PERIOD = "pref_key_use_double_space_period"; + public static final String PREF_BLOCK_POTENTIALLY_OFFENSIVE = + "pref_key_block_potentially_offensive"; public static final String PREF_SHOW_LANGUAGE_SWITCH_KEY = "pref_show_language_switch_key"; public static final String PREF_INCLUDE_OTHER_IMES_IN_LANGUAGE_SWITCH_LIST = @@ -78,6 +81,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang "pref_suppress_language_switch_key"; public static final String PREF_SEND_FEEDBACK = "send_feedback"; + public static final String PREF_ABOUT_KEYBOARD = "about_keyboard"; private Resources mRes; private SharedPreferences mPrefs; @@ -142,6 +146,10 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang return mCurrentLocale; } + public boolean getBlockPotentiallyOffensive() { + return mSettingsValues.mBlockPotentiallyOffensive; + } + // Accessed from the settings interface, hence public public static boolean readKeypressSoundEnabled(final SharedPreferences prefs, final Resources res) { @@ -163,6 +171,12 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang return !currentAutoCorrectionSetting.equals(autoCorrectionOff); } + public static boolean readBlockPotentiallyOffensive(final SharedPreferences prefs, + final Resources res) { + return prefs.getBoolean(Settings.PREF_BLOCK_POTENTIALLY_OFFENSIVE, + res.getBoolean(R.bool.config_block_potentially_offensive)); + } + public static boolean readFromBuildConfigIfGestureInputEnabled(final Resources res) { return res.getBoolean(R.bool.config_gesture_input_enabled_by_build_config); } diff --git a/java/src/com/android/inputmethod/latin/SettingsFragment.java b/java/src/com/android/inputmethod/latin/SettingsFragment.java index a96c997c8..835ef7b46 100644 --- a/java/src/com/android/inputmethod/latin/SettingsFragment.java +++ b/java/src/com/android/inputmethod/latin/SettingsFragment.java @@ -16,10 +16,13 @@ package com.android.inputmethod.latin; +import android.app.Activity; import android.app.backup.BackupManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.media.AudioManager; import android.os.Bundle; @@ -31,13 +34,19 @@ import android.preference.PreferenceGroup; import android.preference.PreferenceScreen; import android.view.inputmethod.InputMethodSubtype; +import java.util.TreeSet; + import com.android.inputmethod.dictionarypack.DictionarySettingsActivity; import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.setup.LauncherIconVisibilityManager; +import com.android.inputmethod.latin.userdictionary.UserDictionaryList; +import com.android.inputmethod.latin.userdictionary.UserDictionarySettings; import com.android.inputmethodcommon.InputMethodSettingsFragment; public final class SettingsFragment extends InputMethodSettingsFragment implements SharedPreferences.OnSharedPreferenceChangeListener { + private static final boolean DBG_USE_INTERNAL_USER_DICTIONARY_SETTINGS = false; + private ListPreference mVoicePreference; private ListPreference mShowCorrectionSuggestionsPreference; private ListPreference mAutoCorrectionThresholdPreference; @@ -77,10 +86,13 @@ public final class SettingsFragment extends InputMethodSettingsFragment final Resources res = getResources(); final Context context = getActivity(); - // When we are called from the Settings application but we are not already running, the - // {@link SubtypeLocale} class may not have been initialized. It is safe to call - // {@link SubtypeLocale#init(Context)} multiple times. + // When we are called from the Settings application but we are not already running, some + // singleton and utility classes may not have been initialized. We have to call + // initialization method of these classes here. See {@link LatinIME#onCreate()}. + SubtypeSwitcher.init(context); SubtypeLocale.init(context); + AudioAndHapticFeedbackManager.init(context); + mVoicePreference = (ListPreference) findPreference(Settings.PREF_VOICE_MODE); mShowCorrectionSuggestionsPreference = (ListPreference) findPreference(Settings.PREF_SHOW_SUGGESTIONS_SETTING); @@ -110,6 +122,7 @@ public final class SettingsFragment extends InputMethodSettingsFragment } final Preference feedbackSettings = findPreference(Settings.PREF_SEND_FEEDBACK); + final Preference aboutSettings = findPreference(Settings.PREF_ABOUT_KEYBOARD); if (feedbackSettings != null) { if (FeedbackUtils.isFeedbackFormSupported()) { feedbackSettings.setOnPreferenceClickListener(new OnPreferenceClickListener() { @@ -119,8 +132,11 @@ public final class SettingsFragment extends InputMethodSettingsFragment return true; } }); + aboutSettings.setTitle(FeedbackUtils.getAboutKeyboardTitleResId()); + aboutSettings.setIntent(FeedbackUtils.getAboutKeyboardIntent(getActivity())); } else { miscSettings.removePreference(feedbackSettings); + miscSettings.removePreference(aboutSettings); } } @@ -180,6 +196,15 @@ public final class SettingsFragment extends InputMethodSettingsFragment textCorrectionGroup.removePreference(dictionaryLink); } + final Preference editPersonalDictionary = + findPreference(Settings.PREF_EDIT_PERSONAL_DICTIONARY); + final Intent editPersonalDictionaryIntent = editPersonalDictionary.getIntent(); + final ResolveInfo ri = context.getPackageManager().resolveActivity( + editPersonalDictionaryIntent, PackageManager.MATCH_DEFAULT_ONLY); + if (DBG_USE_INTERNAL_USER_DICTIONARY_SETTINGS || ri == null) { + updateUserDictionaryPreference(editPersonalDictionary); + } + if (!Settings.readFromBuildConfigIfGestureInputEnabled(res)) { removePreference(Settings.PREF_GESTURE_SETTINGS, getPreferenceScreen()); } @@ -386,4 +411,28 @@ public final class SettingsFragment extends InputMethodSettingsFragment } }); } + + private void updateUserDictionaryPreference(Preference userDictionaryPreference) { + final Activity activity = getActivity(); + final TreeSet<String> localeList = UserDictionaryList.getUserDictionaryLocalesSet(activity); + if (null == localeList) { + // The locale list is null if and only if the user dictionary service is + // not present or disabled. In this case we need to remove the preference. + getPreferenceScreen().removePreference(userDictionaryPreference); + } else if (localeList.size() <= 1) { + userDictionaryPreference.setFragment(UserDictionarySettings.class.getName()); + // If the size of localeList is 0, we don't set the locale parameter in the + // extras. This will be interpreted by the UserDictionarySettings class as + // meaning "the current locale". + // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesSet() + // the locale list always has at least one element, since it always includes the current + // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesSet(). + if (localeList.size() == 1) { + final String locale = (String)localeList.toArray()[0]; + userDictionaryPreference.getExtras().putString("locale", locale); + } + } else { + userDictionaryPreference.setFragment(UserDictionaryList.class.getName()); + } + } } diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java index f77a92885..615b2dfab 100644 --- a/java/src/com/android/inputmethod/latin/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/SettingsValues.java @@ -34,6 +34,9 @@ import java.util.Arrays; */ public final class SettingsValues { private static final String TAG = SettingsValues.class.getSimpleName(); + // "floatNegativeInfinity" is a special marker string for Float.NEGATIVE_INFINITE + // currently used for auto-correction + private static final String FLOAT_NEGATIVE_INFINITY_MARKER_STRING = "floatNegativeInfinity"; // From resources: public final int mDelayUpdateOldSuggestions; @@ -54,6 +57,7 @@ public final class SettingsValues { public final boolean mShowsLanguageSwitchKey; public final boolean mUseContactsDict; public final boolean mUseDoubleSpacePeriod; + public final boolean mBlockPotentiallyOffensive; // Use bigrams to predict the next word when there is no input for it yet public final boolean mBigramPredictionEnabled; public final boolean mGestureInputEnabled; @@ -123,6 +127,7 @@ public final class SettingsValues { mShowsLanguageSwitchKey = Settings.readShowsLanguageSwitchKey(prefs); mUseContactsDict = prefs.getBoolean(Settings.PREF_KEY_USE_CONTACTS_DICT, true); mUseDoubleSpacePeriod = prefs.getBoolean(Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD, true); + mBlockPotentiallyOffensive = Settings.readBlockPotentiallyOffensive(prefs, res); mAutoCorrectEnabled = Settings.readAutoCorrectEnabled(autoCorrectionThresholdRawValue, res); mBigramPredictionEnabled = readBigramPredictionEnabled(prefs, res); @@ -266,8 +271,12 @@ public final class SettingsValues { try { final int arrayIndex = Integer.valueOf(currentAutoCorrectionSetting); if (arrayIndex >= 0 && arrayIndex < autoCorrectionThresholdValues.length) { - autoCorrectionThreshold = Float.parseFloat( - autoCorrectionThresholdValues[arrayIndex]); + final String val = autoCorrectionThresholdValues[arrayIndex]; + if (FLOAT_NEGATIVE_INFINITY_MARKER_STRING.equals(val)) { + autoCorrectionThreshold = Float.NEGATIVE_INFINITY; + } else { + autoCorrectionThreshold = Float.parseFloat(val); + } } } catch (NumberFormatException e) { // Whenever the threshold settings are correct, never come here. @@ -275,7 +284,7 @@ public final class SettingsValues { Log.w(TAG, "Cannot load auto correction threshold setting." + " currentAutoCorrectionSetting: " + currentAutoCorrectionSetting + ", autoCorrectionThresholdValues: " - + Arrays.toString(autoCorrectionThresholdValues)); + + Arrays.toString(autoCorrectionThresholdValues), e); } return autoCorrectionThreshold; } diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index 2f9e34ff1..bef8a3cf1 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -80,6 +80,7 @@ public final class SubtypeSwitcher { public static void init(final Context context) { SubtypeLocale.init(context); + RichInputMethodManager.init(context); sInstance.initialize(context); } @@ -87,10 +88,13 @@ public final class SubtypeSwitcher { // Intentional empty constructor for singleton. } - private void initialize(final Context service) { - mResources = service.getResources(); + private void initialize(final Context context) { + if (mResources != null) { + return; + } + mResources = context.getResources(); mRichImm = RichInputMethodManager.getInstance(); - mConnectivityManager = (ConnectivityManager) service.getSystemService( + mConnectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE); mNoLanguageSubtype = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet( SubtypeLocale.NO_LANGUAGE, SubtypeLocale.QWERTY); diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java index 616e1911b..dfddb0ffe 100644 --- a/java/src/com/android/inputmethod/latin/SuggestedWords.java +++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java @@ -122,6 +122,7 @@ public final class SuggestedWords { public static final class SuggestedWordInfo { public static final int MAX_SCORE = Integer.MAX_VALUE; + public static final int KIND_MASK_KIND = 0xFF; // Mask to get only the kind public static final int KIND_TYPED = 0; // What user typed public static final int KIND_CORRECTION = 1; // Simple correction/suggestion public static final int KIND_COMPLETION = 2; // Completion (suggestion with appended chars) @@ -132,6 +133,11 @@ public final class SuggestedWords { public static final int KIND_SHORTCUT = 7; // A shortcut public static final int KIND_PREDICTION = 8; // A prediction (== a suggestion with no input) public static final int KIND_RESUMED = 9; // A resumed suggestion (comes from a span) + + public static final int KIND_MASK_FLAGS = 0xFFFFFF00; // Mask to get the flags + public static final int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; + public static final int KIND_FLAG_EXACT_MATCH = 0x40000000; + public final String mWord; public final int mScore; public final int mKind; // one of the KIND_* constants above diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java index aff5d17d7..0f96c54dc 100644 --- a/java/src/com/android/inputmethod/latin/Utils.java +++ b/java/src/com/android/inputmethod/latin/Utils.java @@ -21,6 +21,7 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; +import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.inputmethodservice.InputMethodService; @@ -473,4 +474,18 @@ public final class Utils { } return 0; } + + public static String getVersionName(Context context) { + try { + if (context == null) { + return ""; + } + final String packageName = context.getPackageName(); + PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); + return info.versionName; + } catch (NameNotFoundException e) { + Log.e(TAG, "Could not find version info.", e); + } + return ""; + } } diff --git a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java index 15d0bac37..044180bd6 100644 --- a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java +++ b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java @@ -17,18 +17,21 @@ package com.android.inputmethod.latin.setup; import android.app.Activity; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.res.Resources; -import android.graphics.PorterDuff; -import android.graphics.drawable.Drawable; +import android.media.MediaPlayer; +import android.net.Uri; import android.os.Bundle; import android.os.Message; import android.provider.Settings; +import android.util.Log; import android.view.View; import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; import android.widget.TextView; +import android.widget.VideoView; import com.android.inputmethod.compat.TextViewCompatUtils; import com.android.inputmethod.compat.ViewCompatUtils; @@ -38,16 +41,28 @@ import com.android.inputmethod.latin.RichInputMethodManager; import com.android.inputmethod.latin.SettingsActivity; import com.android.inputmethod.latin.StaticInnerHandlerWrapper; -import java.util.HashMap; - -public final class SetupActivity extends Activity { - private SetupStepIndicatorView mStepIndicatorView; - private final SetupStepGroup mSetupSteps = new SetupStepGroup(); +import java.util.ArrayList; + +// TODO: Use Fragment to implement welcome screen and setup steps. +public final class SetupActivity extends Activity implements View.OnClickListener { + private static final String TAG = SetupActivity.class.getSimpleName(); + + private View mWelcomeScreen; + private View mSetupScreen; + private Uri mWelcomeVideoUri; + private VideoView mWelcomeVideoView; + private View mActionStart; + private View mActionNext; + private TextView mStep1Bullet; + private TextView mActionFinish; + private SetupStepGroup mSetupStepGroup; private static final String STATE_STEP = "step"; private int mStepNumber; + private static final int STEP_0 = 0; private static final int STEP_1 = 1; private static final int STEP_2 = 2; private static final int STEP_3 = 3; + private boolean mWasLanguageAndInputSettingsInvoked; private final SettingsPoolingHandler mHandler = new SettingsPoolingHandler(this); @@ -109,17 +124,26 @@ public final class SetupActivity extends Activity { return; } - // TODO: Use sans-serif-thin font family depending on the system locale white list and - // the SDK version. - final TextView titleView = (TextView)findViewById(R.id.setup_title); - final int appName = getApplicationInfo().labelRes; - titleView.setText(getString(R.string.setup_title, getString(appName))); - - mStepIndicatorView = (SetupStepIndicatorView)findViewById(R.id.setup_step_indicator); - - final SetupStep step1 = new SetupStep(findViewById(R.id.setup_step1), - appName, R.string.setup_step1_title, R.string.setup_step1_instruction, - R.drawable.ic_settings_language, R.string.language_settings); + final String applicationName = getResources().getString(getApplicationInfo().labelRes); + mWelcomeScreen = findViewById(R.id.setup_welcome_screen); + final TextView welcomeTitle = (TextView)findViewById(R.id.setup_welcome_title); + welcomeTitle.setText(getString(R.string.setup_welcome_title, applicationName)); + + mSetupScreen = findViewById(R.id.setup_steps_screen); + final TextView stepsTitle = (TextView)findViewById(R.id.setup_title); + stepsTitle.setText(getString(R.string.setup_steps_title, applicationName)); + + final SetupStepIndicatorView indicatorView = + (SetupStepIndicatorView)findViewById(R.id.setup_step_indicator); + mSetupStepGroup = new SetupStepGroup(indicatorView); + + mStep1Bullet = (TextView)findViewById(R.id.setup_step1_bullet); + mStep1Bullet.setOnClickListener(this); + final SetupStep step1 = new SetupStep(STEP_1, applicationName, + mStep1Bullet, findViewById(R.id.setup_step1), + R.string.setup_step1_title, R.string.setup_step1_instruction, + R.string.setup_step1_finished_instruction, R.drawable.ic_setup_step1, + R.string.setup_step1_action); step1.setAction(new Runnable() { @Override public void run() { @@ -127,11 +151,13 @@ public final class SetupActivity extends Activity { mHandler.startPollingImeSettings(); } }); - mSetupSteps.addStep(STEP_1, step1); + mSetupStepGroup.addStep(step1); - final SetupStep step2 = new SetupStep(findViewById(R.id.setup_step2), - appName, R.string.setup_step2_title, R.string.setup_step2_instruction, - 0 /* actionIcon */, R.string.select_input_method); + final SetupStep step2 = new SetupStep(STEP_2, applicationName, + (TextView)findViewById(R.id.setup_step2_bullet), findViewById(R.id.setup_step2), + R.string.setup_step2_title, R.string.setup_step2_instruction, + 0 /* finishedInstruction */, R.drawable.ic_setup_step2, + R.string.setup_step2_action); step2.setAction(new Runnable() { @Override public void run() { @@ -140,18 +166,81 @@ public final class SetupActivity extends Activity { .showInputMethodPicker(); } }); - mSetupSteps.addStep(STEP_2, step2); + mSetupStepGroup.addStep(step2); - final SetupStep step3 = new SetupStep(findViewById(R.id.setup_step3), - appName, R.string.setup_step3_title, 0 /* instruction */, - R.drawable.sym_keyboard_language_switch, R.string.setup_step3_instruction); + final SetupStep step3 = new SetupStep(STEP_3, applicationName, + (TextView)findViewById(R.id.setup_step3_bullet), findViewById(R.id.setup_step3), + R.string.setup_step3_title, R.string.setup_step3_instruction, + 0 /* finishedInstruction */, R.drawable.ic_setup_step3, + R.string.setup_step3_action); step3.setAction(new Runnable() { @Override public void run() { invokeSubtypeEnablerOfThisIme(); } }); - mSetupSteps.addStep(STEP_3, step3); + mSetupStepGroup.addStep(step3); + + mWelcomeVideoUri = new Uri.Builder() + .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) + .authority(getPackageName()) + .path(Integer.toString(R.raw.setup_welcome_video)) + .build(); + mWelcomeVideoView = (VideoView)findViewById(R.id.setup_welcome_video); + mWelcomeVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { + @Override + public void onCompletion(final MediaPlayer mp) { + mp.start(); + } + }); + mWelcomeVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { + @Override + public void onPrepared(final MediaPlayer mp) { + // Now VideoView has been laid-out and ready to play, remove background of it to + // reveal the video. + mWelcomeVideoView.setBackgroundResource(0); + } + }); + mWelcomeVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { + @Override + public boolean onError(final MediaPlayer mp, final int what, final int extra) { + Log.e(TAG, "Playing welcome video causes error: what=" + what + " extra=" + extra); + mWelcomeVideoView.setVisibility(View.GONE); + return true; + } + }); + + mActionStart = findViewById(R.id.setup_start_label); + mActionStart.setOnClickListener(this); + mActionNext = findViewById(R.id.setup_next); + mActionNext.setOnClickListener(this); + mActionFinish = (TextView)findViewById(R.id.setup_finish); + TextViewCompatUtils.setCompoundDrawablesRelativeWithIntrinsicBounds(mActionFinish, + getResources().getDrawable(R.drawable.ic_setup_finish), null, null, null); + mActionFinish.setOnClickListener(this); + } + + @Override + public void onClick(final View v) { + if (v == mActionFinish) { + finish(); + return; + } + final int stepState = determineSetupState(); + final int nextStep; + if (v == mActionStart) { + nextStep = STEP_1; + } else if (v == mActionNext) { + nextStep = mStepNumber + 1; + } else if (v == mStep1Bullet && stepState == STEP_2) { + nextStep = STEP_1; + } else { + nextStep = mStepNumber; + } + if (mStepNumber != nextStep) { + mStepNumber = nextStep; + updateSetupStepView(); + } } private void invokeSetupWizardOfThisIme() { @@ -175,6 +264,7 @@ public final class SetupActivity extends Activity { intent.setAction(Settings.ACTION_INPUT_METHOD_SETTINGS); intent.addCategory(Intent.CATEGORY_DEFAULT); startActivity(intent); + mWasLanguageAndInputSettingsInvoked = true; } private void invokeSubtypeEnablerOfThisIme() { @@ -222,7 +312,7 @@ public final class SetupActivity extends Activity { return myImi.getId().equals(currentImeId); } - private int determineSetupStepNumber() { + private int determineSetupState() { mHandler.cancelPollingImeSettings(); if (!isThisImeEnabled(this)) { return STEP_1; @@ -233,6 +323,14 @@ public final class SetupActivity extends Activity { return STEP_3; } + private int determineSetupStepNumber() { + final int stepState = determineSetupState(); + if (stepState == STEP_1) { + return mWasLanguageAndInputSettingsInvoked ? STEP_1 : STEP_0; + } + return stepState; + } + @Override protected void onSaveInstanceState(final Bundle outState) { super.onSaveInstanceState(outState); @@ -264,6 +362,22 @@ public final class SetupActivity extends Activity { } @Override + public void onBackPressed() { + if (mStepNumber == STEP_1) { + mStepNumber = STEP_0; + updateSetupStepView(); + return; + } + super.onBackPressed(); + } + + @Override + protected void onPause() { + mWelcomeVideoView.stopPlayback(); + super.onPause(); + } + + @Override public void onWindowFocusChanged(final boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (!hasFocus) { @@ -274,57 +388,67 @@ public final class SetupActivity extends Activity { } private void updateSetupStepView() { - final int layoutDirection = ViewCompatUtils.getLayoutDirection(mStepIndicatorView); - mStepIndicatorView.setIndicatorPosition( - getIndicatorPosition(mStepNumber, mSetupSteps.getTotalStep(), layoutDirection)); - mSetupSteps.enableStep(mStepNumber); - } - - private static float getIndicatorPosition(final int step, final int totalStep, - final int layoutDirection) { - final float pos = ((step - STEP_1) * 2 + 1) / (float)(totalStep * 2); - return (layoutDirection == ViewCompatUtils.LAYOUT_DIRECTION_RTL) ? 1.0f - pos : pos; + final boolean welcomeScreen = (mStepNumber == STEP_0); + mWelcomeScreen.setVisibility(welcomeScreen ? View.VISIBLE : View.GONE); + mSetupScreen.setVisibility(welcomeScreen ? View.GONE: View.VISIBLE); + if (welcomeScreen) { + mWelcomeVideoView.setVideoURI(mWelcomeVideoUri); + mWelcomeVideoView.start(); + return; + } + mWelcomeVideoView.stopPlayback(); + final boolean isStepActionAlreadyDone = mStepNumber < determineSetupState(); + mSetupStepGroup.enableStep(mStepNumber, isStepActionAlreadyDone); + mActionNext.setVisibility(isStepActionAlreadyDone ? View.VISIBLE : View.GONE); + mActionFinish.setVisibility((mStepNumber == STEP_3) ? View.VISIBLE : View.GONE); } static final class SetupStep implements View.OnClickListener { - private final View mRootView; + public final int mStepNo; + private final View mStepView; + private final TextView mBulletView; + private final int mActivatedColor; + private final int mDeactivatedColor; + private final String mInstruction; + private final String mFinishedInstruction; private final TextView mActionLabel; private Runnable mAction; - public SetupStep(final View rootView, final int appName, final int title, - final int instruction, final int actionIcon, final int actionLabel) { - mRootView = rootView; - final Resources res = rootView.getResources(); - final String applicationName = res.getString(appName); - - final TextView titleView = (TextView)rootView.findViewById(R.id.setup_step_title); + public SetupStep(final int stepNo, final String applicationName, final TextView bulletView, + final View stepView, final int title, final int instruction, + final int finishedInstruction,final int actionIcon, final int actionLabel) { + mStepNo = stepNo; + mStepView = stepView; + mBulletView = bulletView; + final Resources res = stepView.getResources(); + mActivatedColor = res.getColor(R.color.setup_text_action); + mDeactivatedColor = res.getColor(R.color.setup_text_dark); + + final TextView titleView = (TextView)mStepView.findViewById(R.id.setup_step_title); titleView.setText(res.getString(title, applicationName)); + mInstruction = (instruction == 0) ? null + : res.getString(instruction, applicationName); + mFinishedInstruction = (finishedInstruction == 0) ? null + : res.getString(finishedInstruction, applicationName); - final TextView instructionView = (TextView)rootView.findViewById( - R.id.setup_step_instruction); - if (instruction == 0) { - instructionView.setVisibility(View.GONE); - } else { - instructionView.setText(res.getString(instruction, applicationName)); - } - - mActionLabel = (TextView)rootView.findViewById(R.id.setup_step_action_label); + mActionLabel = (TextView)mStepView.findViewById(R.id.setup_step_action_label); mActionLabel.setText(res.getString(actionLabel)); if (actionIcon == 0) { final int paddingEnd = ViewCompatUtils.getPaddingEnd(mActionLabel); ViewCompatUtils.setPaddingRelative(mActionLabel, paddingEnd, 0, paddingEnd, 0); } else { - final int overrideColor = res.getColor(R.color.setup_text_action); - final Drawable icon = res.getDrawable(actionIcon); - icon.setColorFilter(overrideColor, PorterDuff.Mode.MULTIPLY); - icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); - TextViewCompatUtils.setCompoundDrawablesRelative( - mActionLabel, icon, null, null, null); + TextViewCompatUtils.setCompoundDrawablesRelativeWithIntrinsicBounds( + mActionLabel, res.getDrawable(actionIcon), null, null, null); } } - public void setEnabled(final boolean enabled) { - mRootView.setVisibility(enabled ? View.VISIBLE : View.GONE); + public void setEnabled(final boolean enabled, final boolean isStepActionAlreadyDone) { + mStepView.setVisibility(enabled ? View.VISIBLE : View.GONE); + mBulletView.setTextColor(enabled ? mActivatedColor : mDeactivatedColor); + final TextView instructionView = (TextView)mStepView.findViewById( + R.id.setup_step_instruction); + instructionView.setText(isStepActionAlreadyDone ? mFinishedInstruction : mInstruction); + mActionLabel.setVisibility(isStepActionAlreadyDone ? View.GONE : View.VISIBLE); } public void setAction(final Runnable action) { @@ -334,28 +458,30 @@ public final class SetupActivity extends Activity { @Override public void onClick(final View v) { - if (mAction != null) { + if (v == mActionLabel && mAction != null) { mAction.run(); + return; } } } static final class SetupStepGroup { - private final HashMap<Integer, SetupStep> mGroup = CollectionUtils.newHashMap(); + private final SetupStepIndicatorView mIndicatorView; + private final ArrayList<SetupStep> mGroup = CollectionUtils.newArrayList(); - public void addStep(final int stepNo, final SetupStep step) { - mGroup.put(stepNo, step); + public SetupStepGroup(final SetupStepIndicatorView indicatorView) { + mIndicatorView = indicatorView; } - public void enableStep(final int enableStepNo) { - for (final Integer stepNo : mGroup.keySet()) { - final SetupStep step = mGroup.get(stepNo); - step.setEnabled(stepNo == enableStepNo); - } + public void addStep(final SetupStep step) { + mGroup.add(step); } - public int getTotalStep() { - return mGroup.size(); + public void enableStep(final int enableStepNo, final boolean isStepActionAlreadyDone) { + for (final SetupStep step : mGroup) { + step.setEnabled(step.mStepNo == enableStepNo, isStepActionAlreadyDone); + } + mIndicatorView.setIndicatorPosition(enableStepNo - STEP_1, mGroup.size()); } } } diff --git a/java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java b/java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java new file mode 100644 index 000000000..ca974f6b8 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.setup; + +import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Path; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.android.inputmethod.compat.ViewCompatUtils; +import com.android.inputmethod.latin.R; + +public final class SetupStartIndicatorView extends LinearLayout { + public SetupStartIndicatorView(final Context context, final AttributeSet attrs) { + super(context, attrs); + setOrientation(HORIZONTAL); + LayoutInflater.from(context).inflate(R.layout.setup_start_indicator_label, this); + + final LabelView labelView = (LabelView)findViewById(R.id.setup_start_label); + labelView.setIndicatorView(findViewById(R.id.setup_start_indicator)); + } + + public static final class LabelView extends TextView { + private View mIndicatorView; + + public LabelView(final Context context, final AttributeSet attrs) { + super(context, attrs); + } + + public void setIndicatorView(final View indicatorView) { + mIndicatorView = indicatorView; + } + + @Override + public void setPressed(final boolean pressed) { + super.setPressed(pressed); + if (mIndicatorView != null) { + mIndicatorView.setPressed(pressed); + } + } + } + + public static final class IndicatorView extends View { + private final Path mIndicatorPath = new Path(); + private final Paint mIndicatorPaint = new Paint(); + private final ColorStateList mIndicatorColor; + + public IndicatorView(final Context context, final AttributeSet attrs) { + super(context, attrs); + mIndicatorColor = getResources().getColorStateList( + R.color.setup_step_action_background); + mIndicatorPaint.setStyle(Paint.Style.FILL); + } + + @Override + public void setPressed(final boolean pressed) { + super.setPressed(pressed); + invalidate(); + } + + @Override + protected void onDraw(final Canvas canvas) { + super.onDraw(canvas); + final int layoutDirection = ViewCompatUtils.getLayoutDirection(this); + final int width = getWidth(); + final int height = getHeight(); + final float halfHeight = height / 2.0f; + final Path path = mIndicatorPath; + path.rewind(); + if (layoutDirection == ViewCompatUtils.LAYOUT_DIRECTION_RTL) { + // Left arrow + path.moveTo(width, 0.0f); + path.lineTo(0.0f, halfHeight); + path.lineTo(width, height); + } else { // LAYOUT_DIRECTION_LTR + // Right arrow + path.moveTo(0.0f, 0.0f); + path.lineTo(width, halfHeight); + path.lineTo(0.0f, height); + } + path.close(); + final int[] stateSet = getDrawableState(); + final int color = mIndicatorColor.getColorForState(stateSet, 0); + mIndicatorPaint.setColor(color); + canvas.drawPath(path, mIndicatorPaint); + } + } +} diff --git a/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java b/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java index 077a21793..c909507c6 100644 --- a/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java +++ b/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java @@ -23,6 +23,7 @@ import android.graphics.Path; import android.util.AttributeSet; import android.view.View; +import com.android.inputmethod.compat.ViewCompatUtils; import com.android.inputmethod.latin.R; public final class SetupStepIndicatorView extends View { @@ -36,8 +37,13 @@ public final class SetupStepIndicatorView extends View { mIndicatorPaint.setStyle(Paint.Style.FILL); } - public void setIndicatorPosition(final float xRatio) { - mXRatio = xRatio; + public void setIndicatorPosition(final int stepPos, final int totalStepNum) { + final int layoutDirection = ViewCompatUtils.getLayoutDirection(this); + // The indicator position is the center of the partition that is equally divided into + // the total step number. + final float partionWidth = 1.0f / totalStepNum; + final float pos = stepPos * partionWidth + partionWidth / 2.0f; + mXRatio = (layoutDirection == ViewCompatUtils.LAYOUT_DIRECTION_RTL) ? 1.0f - pos : pos; invalidate(); } diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java new file mode 100644 index 000000000..2b6fda381 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java @@ -0,0 +1,261 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.userdictionary; + +import com.android.inputmethod.compat.UserDictionaryCompatUtils; +import com.android.inputmethod.latin.LocaleUtils; +import com.android.inputmethod.latin.R; + +import android.app.Activity; +import android.content.ContentResolver; +import android.content.Context; +import android.database.Cursor; +import android.os.Bundle; +import android.provider.UserDictionary; +import android.text.TextUtils; +import android.view.View; +import android.widget.EditText; + +import java.util.ArrayList; +import java.util.Locale; +import java.util.TreeSet; + +// Caveat: This class is basically taken from +// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java +// in order to deal with some devices that have issues with the user dictionary handling + +/** + * A container class to factor common code to UserDictionaryAddWordFragment + * and UserDictionaryAddWordActivity. + */ +public class UserDictionaryAddWordContents { + public static final String EXTRA_MODE = "mode"; + public static final String EXTRA_WORD = "word"; + public static final String EXTRA_SHORTCUT = "shortcut"; + public static final String EXTRA_LOCALE = "locale"; + public static final String EXTRA_ORIGINAL_WORD = "originalWord"; + public static final String EXTRA_ORIGINAL_SHORTCUT = "originalShortcut"; + + public static final int MODE_EDIT = 0; + public static final int MODE_INSERT = 1; + + /* package */ static final int CODE_WORD_ADDED = 0; + /* package */ static final int CODE_CANCEL = 1; + /* package */ static final int CODE_ALREADY_PRESENT = 2; + + private static final int FREQUENCY_FOR_USER_DICTIONARY_ADDS = 250; + + private final int mMode; // Either MODE_EDIT or MODE_INSERT + private final EditText mWordEditText; + private final EditText mShortcutEditText; + private String mLocale; + private final String mOldWord; + private final String mOldShortcut; + + /* package */ UserDictionaryAddWordContents(final View view, final Bundle args) { + mWordEditText = (EditText)view.findViewById(R.id.user_dictionary_add_word_text); + mShortcutEditText = (EditText)view.findViewById(R.id.user_dictionary_add_shortcut); + if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + mShortcutEditText.setVisibility(View.GONE); + view.findViewById(R.id.user_dictionary_add_shortcut_label).setVisibility(View.GONE); + } + final String word = args.getString(EXTRA_WORD); + if (null != word) { + mWordEditText.setText(word); + mWordEditText.setSelection(word.length()); + } + final String shortcut; + if (UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + shortcut = args.getString(EXTRA_SHORTCUT); + if (null != shortcut && null != mShortcutEditText) { + mShortcutEditText.setText(shortcut); + } + mOldShortcut = args.getString(EXTRA_SHORTCUT); + } else { + shortcut = null; + mOldShortcut = null; + } + mMode = args.getInt(EXTRA_MODE); // default return value for #getInt() is 0 = MODE_EDIT + mOldWord = args.getString(EXTRA_WORD); + updateLocale(args.getString(EXTRA_LOCALE)); + } + + // locale may be null, this means default locale + // It may also be the empty string, which means "all locales" + /* package */ void updateLocale(final String locale) { + mLocale = null == locale ? Locale.getDefault().toString() : locale; + } + + /* package */ void saveStateIntoBundle(final Bundle outState) { + outState.putString(EXTRA_WORD, mWordEditText.getText().toString()); + outState.putString(EXTRA_ORIGINAL_WORD, mOldWord); + if (null != mShortcutEditText) { + outState.putString(EXTRA_SHORTCUT, mShortcutEditText.getText().toString()); + } + if (null != mOldShortcut) { + outState.putString(EXTRA_ORIGINAL_SHORTCUT, mOldShortcut); + } + outState.putString(EXTRA_LOCALE, mLocale); + } + + /* package */ void delete(final Context context) { + if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { + // Mode edit: remove the old entry. + final ContentResolver resolver = context.getContentResolver(); + UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); + } + // If we are in add mode, nothing was added, so we don't need to do anything. + } + + /* package */ + int apply(final Context context, final Bundle outParameters) { + if (null != outParameters) saveStateIntoBundle(outParameters); + final ContentResolver resolver = context.getContentResolver(); + if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { + // Mode edit: remove the old entry. + UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); + } + final String newWord = mWordEditText.getText().toString(); + final String newShortcut; + if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + newShortcut = null; + } else if (null == mShortcutEditText) { + newShortcut = null; + } else { + final String tmpShortcut = mShortcutEditText.getText().toString(); + if (TextUtils.isEmpty(tmpShortcut)) { + newShortcut = null; + } else { + newShortcut = tmpShortcut; + } + } + if (TextUtils.isEmpty(newWord)) { + // If the word is somehow empty, don't insert it. + return CODE_CANCEL; + } + // If there is no shortcut, and the word already exists in the database, then we + // should not insert, because either A. the word exists with no shortcut, in which + // case the exact same thing we want to insert is already there, or B. the word + // exists with at least one shortcut, in which case it has priority on our word. + if (hasWord(newWord, context)) return CODE_ALREADY_PRESENT; + + // Disallow duplicates. If the same word with no shortcut is defined, remove it; if + // the same word with the same shortcut is defined, remove it; but we don't mind if + // there is the same word with a different, non-empty shortcut. + UserDictionarySettings.deleteWord(newWord, null, resolver); + if (!TextUtils.isEmpty(newShortcut)) { + // If newShortcut is empty we just deleted this, no need to do it again + UserDictionarySettings.deleteWord(newWord, newShortcut, resolver); + } + + // In this class we use the empty string to represent 'all locales' and mLocale cannot + // be null. However the addWord method takes null to mean 'all locales'. + UserDictionaryCompatUtils.addWord(context, newWord.toString(), + FREQUENCY_FOR_USER_DICTIONARY_ADDS, newShortcut, TextUtils.isEmpty(mLocale) ? + null : LocaleUtils.constructLocaleFromString(mLocale)); + + return CODE_WORD_ADDED; + } + + private static final String[] HAS_WORD_PROJECTION = { UserDictionary.Words.WORD }; + private static final String HAS_WORD_SELECTION_ONE_LOCALE = UserDictionary.Words.WORD + + "=? AND " + UserDictionary.Words.LOCALE + "=?"; + private static final String HAS_WORD_SELECTION_ALL_LOCALES = UserDictionary.Words.WORD + + "=? AND " + UserDictionary.Words.LOCALE + " is null"; + private boolean hasWord(final String word, final Context context) { + final Cursor cursor; + // mLocale == "" indicates this is an entry for all languages. Here, mLocale can't + // be null at all (it's ensured by the updateLocale method). + if ("".equals(mLocale)) { + cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI, + HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ALL_LOCALES, + new String[] { word }, null /* sort order */); + } else { + cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI, + HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ONE_LOCALE, + new String[] { word, mLocale }, null /* sort order */); + } + try { + if (null == cursor) return false; + return cursor.getCount() > 0; + } finally { + if (null != cursor) cursor.close(); + } + } + + public static class LocaleRenderer { + private final String mLocaleString; + private final String mDescription; + // LocaleString may NOT be null. + public LocaleRenderer(final Context context, final String localeString) { + mLocaleString = localeString; + if (null == localeString) { + mDescription = context.getString(R.string.user_dict_settings_more_languages); + } else if ("".equals(localeString)) { + mDescription = context.getString(R.string.user_dict_settings_all_languages); + } else { + mDescription = LocaleUtils.constructLocaleFromString(localeString).getDisplayName(); + } + } + @Override + public String toString() { + return mDescription; + } + public String getLocaleString() { + return mLocaleString; + } + // "More languages..." is null ; "All languages" is the empty string. + public boolean isMoreLanguages() { + return null == mLocaleString; + } + } + + private static void addLocaleDisplayNameToList(final Context context, + final ArrayList<LocaleRenderer> list, final String locale) { + if (null != locale) { + list.add(new LocaleRenderer(context, locale)); + } + } + + // Helper method to get the list of locales to display for this word + public ArrayList<LocaleRenderer> getLocalesList(final Activity activity) { + final TreeSet<String> locales = UserDictionaryList.getUserDictionaryLocalesSet(activity); + // Remove our locale if it's in, because we're always gonna put it at the top + locales.remove(mLocale); // mLocale may not be null + final String systemLocale = Locale.getDefault().toString(); + // The system locale should be inside. We want it at the 2nd spot. + locales.remove(systemLocale); // system locale may not be null + locales.remove(""); // Remove the empty string if it's there + final ArrayList<LocaleRenderer> localesList = new ArrayList<LocaleRenderer>(); + // Add the passed locale, then the system locale at the top of the list. Add an + // "all languages" entry at the bottom of the list. + addLocaleDisplayNameToList(activity, localesList, mLocale); + if (!systemLocale.equals(mLocale)) { + addLocaleDisplayNameToList(activity, localesList, systemLocale); + } + for (final String l : locales) { + // TODO: sort in unicode order + addLocaleDisplayNameToList(activity, localesList, l); + } + if (!"".equals(mLocale)) { + // If mLocale is "", then we already inserted the "all languages" item, so don't do it + addLocaleDisplayNameToList(activity, localesList, ""); // meaning: all languages + } + localesList.add(new LocaleRenderer(activity, null)); // meaning: select another locale + return localesList; + } +} diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java new file mode 100644 index 000000000..5f4c44636 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.userdictionary; + +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.userdictionary.UserDictionaryAddWordContents.LocaleRenderer; +import com.android.inputmethod.latin.userdictionary.UserDictionaryLocalePicker.LocationChangedListener; + +import android.app.Fragment; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.Spinner; + +import java.util.ArrayList; +import java.util.Locale; + +// Caveat: This class is basically taken from +// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java +// in order to deal with some devices that have issues with the user dictionary handling + +/** + * Fragment to add a word/shortcut to the user dictionary. + * + * As opposed to the UserDictionaryActivity, this is only invoked within Settings + * from the UserDictionarySettings. + */ +public class UserDictionaryAddWordFragment extends Fragment + implements AdapterView.OnItemSelectedListener, LocationChangedListener { + + private static final int OPTIONS_MENU_ADD = Menu.FIRST; + private static final int OPTIONS_MENU_DELETE = Menu.FIRST + 1; + + private UserDictionaryAddWordContents mContents; + private View mRootView; + private boolean mIsDeleting = false; + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + setHasOptionsMenu(true); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { + mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null); + mIsDeleting = false; + if (null == mContents) { + mContents = new UserDictionaryAddWordContents(mRootView, getArguments()); + } + return mRootView; + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + final MenuItem actionItemDelete = menu.add(0, OPTIONS_MENU_DELETE, 0, + R.string.user_dict_settings_delete).setIcon(android.R.drawable.ic_menu_delete); + actionItemDelete.setShowAsAction( + MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + final MenuItem actionItemAdd = menu.add(0, OPTIONS_MENU_ADD, 0, + R.string.user_dict_settings_delete).setIcon(R.drawable.ic_menu_add); + actionItemAdd.setShowAsAction( + MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + } + + /** + * Callback for the framework when a menu option is pressed. + * + * @param MenuItem the item that was pressed + * @return false to allow normal menu processing to proceed, true to consume it here + */ + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == OPTIONS_MENU_ADD) { + // added the entry in "onPause" + getActivity().onBackPressed(); + return true; + } + if (item.getItemId() == OPTIONS_MENU_DELETE) { + mContents.delete(getActivity()); + mIsDeleting = true; + getActivity().onBackPressed(); + return true; + } + return false; + } + + @Override + public void onResume() { + super.onResume(); + // We are being shown: display the word + updateSpinner(); + } + + private void updateSpinner() { + final ArrayList<LocaleRenderer> localesList = mContents.getLocalesList(getActivity()); + + final Spinner localeSpinner = + (Spinner)mRootView.findViewById(R.id.user_dictionary_add_locale); + final ArrayAdapter<LocaleRenderer> adapter = new ArrayAdapter<LocaleRenderer>(getActivity(), + android.R.layout.simple_spinner_item, localesList); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + localeSpinner.setAdapter(adapter); + localeSpinner.setOnItemSelectedListener(this); + } + + @Override + public void onPause() { + super.onPause(); + // We are being hidden: commit changes to the user dictionary, unless we were deleting it + if (!mIsDeleting) { + mContents.apply(getActivity(), null); + } + } + + @Override + public void onItemSelected(final AdapterView<?> parent, final View view, final int pos, + final long id) { + final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos); + if (locale.isMoreLanguages()) { + PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity(); + preferenceActivity.startPreferenceFragment(new UserDictionaryLocalePicker(), true); + } else { + mContents.updateLocale(locale.getLocaleString()); + } + } + + @Override + public void onNothingSelected(final AdapterView<?> parent) { + // I'm not sure we can come here, but if we do, that's the right thing to do. + final Bundle args = getArguments(); + mContents.updateLocale(args.getString(UserDictionaryAddWordContents.EXTRA_LOCALE)); + } + + // Called by the locale picker + @Override + public void onLocaleSelected(final Locale locale) { + mContents.updateLocale(locale.toString()); + getActivity().onBackPressed(); + } +} diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java new file mode 100644 index 000000000..6e64882b6 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.userdictionary; + +import com.android.inputmethod.latin.LocaleUtils; +import com.android.inputmethod.latin.R; + +import android.app.Activity; +import android.content.Intent; +import android.database.Cursor; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.PreferenceFragment; +import android.preference.PreferenceGroup; +import android.provider.UserDictionary; +import android.text.TextUtils; + +import java.util.Locale; +import java.util.TreeSet; + +// Caveat: This class is basically taken from +// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryList.java +// in order to deal with some devices that have issues with the user dictionary handling + +public class UserDictionaryList extends PreferenceFragment { + + public static final String USER_DICTIONARY_SETTINGS_INTENT_ACTION = + "android.settings.USER_DICTIONARY_SETTINGS"; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity())); + } + + public static TreeSet<String> getUserDictionaryLocalesSet(Activity activity) { + @SuppressWarnings("deprecation") + final Cursor cursor = activity.managedQuery(UserDictionary.Words.CONTENT_URI, + new String[] { UserDictionary.Words.LOCALE }, + null, null, null); + final TreeSet<String> localeList = new TreeSet<String>(); + boolean addedAllLocale = false; + if (null == cursor) { + // The user dictionary service is not present or disabled. Return null. + return null; + } else if (cursor.moveToFirst()) { + final int columnIndex = cursor.getColumnIndex(UserDictionary.Words.LOCALE); + do { + final String locale = cursor.getString(columnIndex); + final boolean allLocale = TextUtils.isEmpty(locale); + localeList.add(allLocale ? "" : locale); + if (allLocale) { + addedAllLocale = true; + } + } while (cursor.moveToNext()); + } + if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED && !addedAllLocale) { + // For ICS, we need to show "For all languages" in case that the keyboard locale + // is different from the system locale + localeList.add(""); + } + localeList.add(Locale.getDefault().toString()); + return localeList; + } + + /** + * Creates the entries that allow the user to go into the user dictionary for each locale. + * @param userDictGroup The group to put the settings in. + */ + protected void createUserDictSettings(PreferenceGroup userDictGroup) { + final Activity activity = getActivity(); + userDictGroup.removeAll(); + final TreeSet<String> localeList = + UserDictionaryList.getUserDictionaryLocalesSet(activity); + + if (localeList.isEmpty()) { + userDictGroup.addPreference(createUserDictionaryPreference(null, activity)); + } else { + for (String locale : localeList) { + userDictGroup.addPreference(createUserDictionaryPreference(locale, activity)); + } + } + } + + /** + * Create a single User Dictionary Preference object, with its parameters set. + * @param locale The locale for which this user dictionary is for. + * @return The corresponding preference. + */ + protected Preference createUserDictionaryPreference(String locale, Activity activity) { + final Preference newPref = new Preference(getActivity()); + final Intent intent = new Intent(USER_DICTIONARY_SETTINGS_INTENT_ACTION); + if (null == locale) { + newPref.setTitle(Locale.getDefault().getDisplayName()); + } else { + if ("".equals(locale)) + newPref.setTitle(getString(R.string.user_dict_settings_all_languages)); + else + newPref.setTitle(LocaleUtils.constructLocaleFromString(locale).getDisplayName()); + intent.putExtra("locale", locale); + newPref.getExtras().putString("locale", locale); + } + newPref.setIntent(intent); + newPref.setFragment(UserDictionarySettings.class.getName()); + return newPref; + } + + @Override + public void onResume() { + super.onResume(); + createUserDictSettings(getPreferenceScreen()); + } +} diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryLocalePicker.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryLocalePicker.java new file mode 100644 index 000000000..58d3fb91c --- /dev/null +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryLocalePicker.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.userdictionary; + +import android.app.Fragment; + +import java.util.Locale; + +// Caveat: This class is basically taken from +// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryLocalePicker.java +// in order to deal with some devices that have issues with the user dictionary handling + +public class UserDictionaryLocalePicker extends Fragment { + public UserDictionaryLocalePicker() { + super(); + // TODO: implement + } + + public interface LocationChangedListener { + public void onLocaleSelected(Locale locale); + } +} diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java new file mode 100644 index 000000000..36bc5ba49 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java @@ -0,0 +1,333 @@ +/** + * Copyright (C) 2013 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.android.inputmethod.latin.userdictionary; + +import com.android.inputmethod.latin.R; + +import android.app.ListFragment; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.os.Build; +import android.os.Bundle; +import android.provider.UserDictionary; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AlphabetIndexer; +import android.widget.ListAdapter; +import android.widget.ListView; +import android.widget.SectionIndexer; +import android.widget.SimpleCursorAdapter; +import android.widget.TextView; + +import java.util.Locale; + +// Caveat: This class is basically taken from +// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionarySettings.java +// in order to deal with some devices that have issues with the user dictionary handling + +public class UserDictionarySettings extends ListFragment { + + public static final boolean IS_SHORTCUT_API_SUPPORTED = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; + + private static final String[] QUERY_PROJECTION_SHORTCUT_UNSUPPORTED = + { UserDictionary.Words._ID, UserDictionary.Words.WORD}; + private static final String[] QUERY_PROJECTION_SHORTCUT_SUPPORTED = + { UserDictionary.Words._ID, UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT}; + private static final String[] QUERY_PROJECTION = + IS_SHORTCUT_API_SUPPORTED ? + QUERY_PROJECTION_SHORTCUT_SUPPORTED : QUERY_PROJECTION_SHORTCUT_UNSUPPORTED; + + // The index of the shortcut in the above array. + private static final int INDEX_SHORTCUT = 2; + + private static final String[] ADAPTER_FROM_SHORTCUT_UNSUPPORTED = { + UserDictionary.Words.WORD, + }; + + private static final String[] ADAPTER_FROM_SHORTCUT_SUPPORTED = { + UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT + }; + + private static final String[] ADAPTER_FROM = IS_SHORTCUT_API_SUPPORTED ? + ADAPTER_FROM_SHORTCUT_SUPPORTED : ADAPTER_FROM_SHORTCUT_UNSUPPORTED; + + private static final int[] ADAPTER_TO_SHORTCUT_UNSUPPORTED = { + android.R.id.text1, + }; + + private static final int[] ADAPTER_TO_SHORTCUT_SUPPORTED = { + android.R.id.text1, android.R.id.text2 + }; + + private static final int[] ADAPTER_TO = IS_SHORTCUT_API_SUPPORTED ? + ADAPTER_TO_SHORTCUT_SUPPORTED : ADAPTER_TO_SHORTCUT_UNSUPPORTED; + + // Either the locale is empty (means the word is applicable to all locales) + // or the word equals our current locale + private static final String QUERY_SELECTION = + UserDictionary.Words.LOCALE + "=?"; + private static final String QUERY_SELECTION_ALL_LOCALES = + UserDictionary.Words.LOCALE + " is null"; + + private static final String DELETE_SELECTION_WITH_SHORTCUT = UserDictionary.Words.WORD + + "=? AND " + UserDictionary.Words.SHORTCUT + "=?"; + private static final String DELETE_SELECTION_WITHOUT_SHORTCUT = UserDictionary.Words.WORD + + "=? AND " + UserDictionary.Words.SHORTCUT + " is null OR " + + UserDictionary.Words.SHORTCUT + "=''"; + private static final String DELETE_SELECTION_SHORTCUT_UNSUPPORTED = + UserDictionary.Words.WORD + "=?"; + + private static final int OPTIONS_MENU_ADD = Menu.FIRST; + + private Cursor mCursor; + + protected String mLocale; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Override + public View onCreateView( + LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate( + R.layout.user_dictionary_preference_list_fragment, container, false); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + final Intent intent = getActivity().getIntent(); + final String localeFromIntent = + null == intent ? null : intent.getStringExtra("locale"); + + final Bundle arguments = getArguments(); + final String localeFromArguments = + null == arguments ? null : arguments.getString("locale"); + + final String locale; + if (null != localeFromArguments) { + locale = localeFromArguments; + } else if (null != localeFromIntent) { + locale = localeFromIntent; + } else { + locale = null; + } + + mLocale = locale; + mCursor = createCursor(locale); + TextView emptyView = (TextView) getView().findViewById(android.R.id.empty); + emptyView.setText(R.string.user_dict_settings_empty_text); + + final ListView listView = getListView(); + listView.setAdapter(createAdapter()); + listView.setFastScrollEnabled(true); + listView.setEmptyView(emptyView); + + setHasOptionsMenu(true); + + } + + @SuppressWarnings("deprecation") + private Cursor createCursor(final String locale) { + // Locale can be any of: + // - The string representation of a locale, as returned by Locale#toString() + // - The empty string. This means we want a cursor returning words valid for all locales. + // - null. This means we want a cursor for the current locale, whatever this is. + // Note that this contrasts with the data inside the database, where NULL means "all + // locales" and there should never be an empty string. The confusion is called by the + // historical use of null for "all locales". + // TODO: it should be easy to make this more readable by making the special values + // human-readable, like "all_locales" and "current_locales" strings, provided they + // can be guaranteed not to match locales that may exist. + if ("".equals(locale)) { + // Case-insensitive sort + return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, + QUERY_SELECTION_ALL_LOCALES, null, + "UPPER(" + UserDictionary.Words.WORD + ")"); + } else { + final String queryLocale = null != locale ? locale : Locale.getDefault().toString(); + return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, + QUERY_SELECTION, new String[] { queryLocale }, + "UPPER(" + UserDictionary.Words.WORD + ")"); + } + } + + private ListAdapter createAdapter() { + return new MyAdapter(getActivity(), R.layout.user_dictionary_item, mCursor, + ADAPTER_FROM, ADAPTER_TO, this); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + final String word = getWord(position); + final String shortcut = getShortcut(position); + if (word != null) { + showAddOrEditDialog(word, shortcut); + } + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + final Locale systemLocale = getResources().getConfiguration().locale; + if (!TextUtils.isEmpty(mLocale) && !mLocale.equals(systemLocale.toString())) { + // Hide the add button for ICS because it doesn't support specifying a locale + // for an entry. This new "locale"-aware API has been added in conjunction + // with the shortcut API. + return; + } + } + MenuItem actionItem = + menu.add(0, OPTIONS_MENU_ADD, 0, R.string.user_dict_settings_add_menu_title) + .setIcon(R.drawable.ic_menu_add); + actionItem.setShowAsAction( + MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == OPTIONS_MENU_ADD) { + showAddOrEditDialog(null, null); + return true; + } + return false; + } + + /** + * Add or edit a word. If editingWord is null, it's an add; otherwise, it's an edit. + * @param editingWord the word to edit, or null if it's an add. + * @param editingShortcut the shortcut for this entry, or null if none. + */ + private void showAddOrEditDialog(final String editingWord, final String editingShortcut) { + final Bundle args = new Bundle(); + args.putInt(UserDictionaryAddWordContents.EXTRA_MODE, null == editingWord + ? UserDictionaryAddWordContents.MODE_INSERT + : UserDictionaryAddWordContents.MODE_EDIT); + args.putString(UserDictionaryAddWordContents.EXTRA_WORD, editingWord); + args.putString(UserDictionaryAddWordContents.EXTRA_SHORTCUT, editingShortcut); + args.putString(UserDictionaryAddWordContents.EXTRA_LOCALE, mLocale); + android.preference.PreferenceActivity pa = + (android.preference.PreferenceActivity)getActivity(); + pa.startPreferencePanel(UserDictionaryAddWordFragment.class.getName(), + args, R.string.user_dict_settings_add_dialog_title, null, null, 0); + } + + private String getWord(final int position) { + if (null == mCursor) return null; + mCursor.moveToPosition(position); + // Handle a possible race-condition + if (mCursor.isAfterLast()) return null; + + return mCursor.getString( + mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD)); + } + + private String getShortcut(final int position) { + if (!IS_SHORTCUT_API_SUPPORTED) return null; + if (null == mCursor) return null; + mCursor.moveToPosition(position); + // Handle a possible race-condition + if (mCursor.isAfterLast()) return null; + + return mCursor.getString( + mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT)); + } + + public static void deleteWord(final String word, final String shortcut, + final ContentResolver resolver) { + if (!IS_SHORTCUT_API_SUPPORTED) { + resolver.delete(UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_SHORTCUT_UNSUPPORTED, + new String[] { word }); + } else if (TextUtils.isEmpty(shortcut)) { + resolver.delete( + UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT, + new String[] { word }); + } else { + resolver.delete( + UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT, + new String[] { word, shortcut }); + } + } + + private static class MyAdapter extends SimpleCursorAdapter implements SectionIndexer { + + private AlphabetIndexer mIndexer; + + private ViewBinder mViewBinder = new ViewBinder() { + + @Override + public boolean setViewValue(View v, Cursor c, int columnIndex) { + if (!IS_SHORTCUT_API_SUPPORTED) { + // just let SimpleCursorAdapter set the view values + return false; + } + if (columnIndex == INDEX_SHORTCUT) { + final String shortcut = c.getString(INDEX_SHORTCUT); + if (TextUtils.isEmpty(shortcut)) { + v.setVisibility(View.GONE); + } else { + ((TextView)v).setText(shortcut); + v.setVisibility(View.VISIBLE); + } + v.invalidate(); + return true; + } + + return false; + } + }; + + @SuppressWarnings("deprecation") + public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to, + UserDictionarySettings settings) { + super(context, layout, c, from, to); + + if (null != c) { + final String alphabet = context.getString(R.string.user_dict_fast_scroll_alphabet); + final int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD); + mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet); + } + setViewBinder(mViewBinder); + } + + @Override + public int getPositionForSection(int section) { + return null == mIndexer ? 0 : mIndexer.getPositionForSection(section); + } + + @Override + public int getSectionForPosition(int position) { + return null == mIndexer ? 0 : mIndexer.getSectionForPosition(position); + } + + @Override + public Object[] getSections() { + return null == mIndexer ? null : mIndexer.getSections(); + } + } +} |