aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
-rw-r--r--java/src/com/android/inputmethod/latin/BinaryDictionary.java24
-rw-r--r--java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java31
-rw-r--r--java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java23
-rw-r--r--java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java36
-rw-r--r--java/src/com/android/inputmethod/latin/DebugSettings.java14
-rw-r--r--java/src/com/android/inputmethod/latin/Dictionary.java9
-rw-r--r--java/src/com/android/inputmethod/latin/DictionaryCollection.java7
-rw-r--r--java/src/com/android/inputmethod/latin/DictionaryInfoUtils.java7
-rw-r--r--java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java6
-rw-r--r--java/src/com/android/inputmethod/latin/ExpandableDictionary.java3
-rw-r--r--java/src/com/android/inputmethod/latin/FeedbackUtils.java9
-rw-r--r--java/src/com/android/inputmethod/latin/JniUtils.java14
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java64
-rw-r--r--java/src/com/android/inputmethod/latin/MetadataFileUriGetter.java12
-rw-r--r--java/src/com/android/inputmethod/latin/ResourceUtils.java135
-rw-r--r--java/src/com/android/inputmethod/latin/RichInputConnection.java5
-rw-r--r--java/src/com/android/inputmethod/latin/RichInputMethodManager.java140
-rw-r--r--java/src/com/android/inputmethod/latin/SeekBarDialogPreference.java2
-rw-r--r--java/src/com/android/inputmethod/latin/Settings.java14
-rw-r--r--java/src/com/android/inputmethod/latin/SettingsFragment.java55
-rw-r--r--java/src/com/android/inputmethod/latin/SettingsValues.java15
-rw-r--r--java/src/com/android/inputmethod/latin/StringUtils.java17
-rw-r--r--java/src/com/android/inputmethod/latin/SubtypeSwitcher.java12
-rw-r--r--java/src/com/android/inputmethod/latin/Suggest.java17
-rw-r--r--java/src/com/android/inputmethod/latin/SuggestedWords.java6
-rw-r--r--java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsBinaryDictionary.java5
-rw-r--r--java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserBinaryDictionary.java5
-rw-r--r--java/src/com/android/inputmethod/latin/Utils.java15
-rw-r--r--java/src/com/android/inputmethod/latin/WordComposer.java4
-rw-r--r--java/src/com/android/inputmethod/latin/personalization/AccountUtils.java47
-rw-r--r--java/src/com/android/inputmethod/latin/setup/SetupActivity.java296
-rw-r--r--java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java123
-rw-r--r--java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java10
-rw-r--r--java/src/com/android/inputmethod/latin/setup/SetupWizardActivity.java493
-rw-r--r--java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java38
-rw-r--r--java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java28
-rw-r--r--java/src/com/android/inputmethod/latin/spellcheck/DictAndKeyboard.java56
-rw-r--r--java/src/com/android/inputmethod/latin/spellcheck/DictAndProximity.java32
-rw-r--r--java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java25
-rw-r--r--java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java462
-rw-r--r--java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java34
-rw-r--r--java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java25
-rw-r--r--java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java12
-rw-r--r--java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java261
-rw-r--r--java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java162
-rw-r--r--java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java127
-rw-r--r--java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryLocalePicker.java36
-rw-r--r--java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java334
48 files changed, 2297 insertions, 1010 deletions
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
index dbc2b9082..4fc1919dc 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
@@ -107,13 +107,16 @@ public final class BinaryDictionary extends Dictionary {
@Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
- final String prevWord, final ProximityInfo proximityInfo) {
- return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, 0);
+ final String prevWord, final ProximityInfo proximityInfo,
+ final boolean blockOffensiveWords) {
+ return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, blockOffensiveWords,
+ 0 /* sessionId */);
}
@Override
public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
- final String prevWord, final ProximityInfo proximityInfo, int sessionId) {
+ final String prevWord, final ProximityInfo proximityInfo,
+ final boolean blockOffensiveWords, final int sessionId) {
if (!isValidDictionary()) return null;
Arrays.fill(mInputCodePoints, Constants.NOT_A_CODE);
@@ -147,10 +150,21 @@ public final class BinaryDictionary extends Dictionary {
++len;
}
if (len > 0) {
- final int score = SuggestedWordInfo.KIND_WHITELIST == mOutputTypes[j]
+ final int flags = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_FLAGS;
+ if (blockOffensiveWords
+ && 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/ContactsBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java
index 75c2cf2c8..b9db9a092 100644
--- a/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java
@@ -16,21 +16,26 @@
package com.android.inputmethod.latin;
+import com.android.inputmethod.latin.personalization.AccountUtils;
+
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
+import android.net.Uri;
import android.os.SystemClock;
import android.provider.BaseColumns;
+import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.text.TextUtils;
import android.util.Log;
+import java.util.List;
import java.util.Locale;
public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
- private static final String[] PROJECTION = {BaseColumns._ID, Contacts.DISPLAY_NAME,};
+ private static final String[] PROJECTION = {BaseColumns._ID, Contacts.DISPLAY_NAME};
private static final String[] PROJECTION_ID_ONLY = {BaseColumns._ID};
private static final String TAG = ContactsBinaryDictionary.class.getSimpleName();
@@ -102,9 +107,32 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
@Override
public void loadDictionaryAsync() {
+ clearFusionDictionary();
+ loadDeviceAccountsEmailAddresses();
+ loadDictionaryAsyncForUri(ContactsContract.Profile.CONTENT_URI);
+ // TODO: Switch this URL to the newer ContactsContract too
+ loadDictionaryAsyncForUri(Contacts.CONTENT_URI);
+ }
+
+ private void loadDeviceAccountsEmailAddresses() {
+ final List<String> accountVocabulary =
+ AccountUtils.getDeviceAccountsEmailAddresses(mContext);
+ if (accountVocabulary == null || accountVocabulary.isEmpty()) {
+ return;
+ }
+ for (String word : accountVocabulary) {
+ if (DEBUG) {
+ Log.d(TAG, "loadAccountVocabulary: " + word);
+ }
+ super.addWord(word, null /* shortcut */, FREQUENCY_FOR_CONTACTS,
+ false /* isNotAWord */);
+ }
+ }
+
+ private void loadDictionaryAsyncForUri(final Uri uri) {
try {
Cursor cursor = mContext.getContentResolver()
- .query(Contacts.CONTENT_URI, PROJECTION, null, null, null);
+ .query(uri, PROJECTION, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
@@ -129,7 +157,6 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
}
private void addWords(final Cursor cursor) {
- clearFusionDictionary();
int count = 0;
while (!cursor.isAfterLast() && count < MAX_CONTACT_COUNT) {
String name = cursor.getString(INDEX_NAME);
@@ -173,6 +200,9 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
// capitalization of i.
final int wordLen = StringUtils.codePointCount(word);
if (wordLen < MAX_WORD_LENGTH && wordLen > 1) {
+ if (DEBUG) {
+ Log.d(TAG, "addName " + name + ", " + word + ", " + prevWord);
+ }
super.addWord(word, null /* shortcut */, FREQUENCY_FOR_CONTACTS,
false /* isNotAWord */);
if (!TextUtils.isEmpty(prevWord)) {
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/Dictionary.java b/java/src/com/android/inputmethod/latin/Dictionary.java
index 9691fa231..acd7c2aa1 100644
--- a/java/src/com/android/inputmethod/latin/Dictionary.java
+++ b/java/src/com/android/inputmethod/latin/Dictionary.java
@@ -51,18 +51,21 @@ public abstract class Dictionary {
* @param composer the key sequence to match with coordinate info, as a WordComposer
* @param prevWord the previous word, or null if none
* @param proximityInfo the object for key proximity. May be ignored by some implementations.
+ * @param blockOffensiveWords whether to block potentially offensive words
* @return the list of suggestions (possibly null if none)
*/
// TODO: pass more context than just the previous word, to enable better suggestions (n-gram
// and more)
abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
- final String prevWord, final ProximityInfo proximityInfo);
+ final String prevWord, final ProximityInfo proximityInfo,
+ final boolean blockOffensiveWords);
// The default implementation of this method ignores sessionId.
// Subclasses that want to use sessionId need to override this method.
public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
- final String prevWord, final ProximityInfo proximityInfo, final int sessionId) {
- return getSuggestions(composer, prevWord, proximityInfo);
+ final String prevWord, final ProximityInfo proximityInfo,
+ final boolean blockOffensiveWords, final int sessionId) {
+ return getSuggestions(composer, prevWord, proximityInfo, blockOffensiveWords);
}
/**
diff --git a/java/src/com/android/inputmethod/latin/DictionaryCollection.java b/java/src/com/android/inputmethod/latin/DictionaryCollection.java
index 2832ad43c..ed2b44223 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryCollection.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryCollection.java
@@ -56,18 +56,19 @@ public final class DictionaryCollection extends Dictionary {
@Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
- final String prevWord, final ProximityInfo proximityInfo) {
+ final String prevWord, final ProximityInfo proximityInfo,
+ final boolean blockOffensiveWords) {
final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries;
if (dictionaries.isEmpty()) return null;
// To avoid creating unnecessary objects, we get the list out of the first
// dictionary and add the rest to it if not null, hence the get(0)
ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composer,
- prevWord, proximityInfo);
+ prevWord, proximityInfo, blockOffensiveWords);
if (null == suggestions) suggestions = CollectionUtils.newArrayList();
final int length = dictionaries.size();
for (int i = 1; i < length; ++ i) {
final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer,
- prevWord, proximityInfo);
+ prevWord, proximityInfo, blockOffensiveWords);
if (null != sugg) suggestions.addAll(sugg);
}
return suggestions;
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/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
index 4b1975a00..887d657e9 100644
--- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
@@ -200,12 +200,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
@Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
- final String prevWord, final ProximityInfo proximityInfo) {
+ final String prevWord, final ProximityInfo proximityInfo,
+ final boolean blockOffensiveWords) {
asyncReloadDictionaryIfRequired();
if (mLocalDictionaryController.tryLock()) {
try {
if (mBinaryDictionary != null) {
- return mBinaryDictionary.getSuggestions(composer, prevWord, proximityInfo);
+ return mBinaryDictionary.getSuggestions(composer, prevWord, proximityInfo,
+ blockOffensiveWords);
}
} finally {
mLocalDictionaryController.unlock();
diff --git a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java
index fd81d13ca..0dabdb835 100644
--- a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java
@@ -253,7 +253,8 @@ public class ExpandableDictionary extends Dictionary {
@Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
- final String prevWord, final ProximityInfo proximityInfo) {
+ final String prevWord, final ProximityInfo proximityInfo,
+ final boolean blockOffensiveWords) {
if (reloadDictionaryIfRequired()) return null;
if (composer.size() > 1) {
if (composer.size() >= Constants.Dictionary.MAX_WORD_LENGTH) {
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/JniUtils.java b/java/src/com/android/inputmethod/latin/JniUtils.java
index f9305991a..8aedee576 100644
--- a/java/src/com/android/inputmethod/latin/JniUtils.java
+++ b/java/src/com/android/inputmethod/latin/JniUtils.java
@@ -23,15 +23,19 @@ import com.android.inputmethod.latin.define.JniLibName;
public final class JniUtils {
private static final String TAG = JniUtils.class.getSimpleName();
- private JniUtils() {
- // This utility class is not publicly instantiable.
- }
-
- public static void loadNativeLibrary() {
+ static {
try {
System.loadLibrary(JniLibName.JNI_LIB_NAME);
} catch (UnsatisfiedLinkError ule) {
Log.e(TAG, "Could not load native library " + JniLibName.JNI_LIB_NAME, ule);
}
}
+
+ private JniUtils() {
+ // This utility class is not publicly instantiable.
+ }
+
+ public static void loadNativeLibrary() {
+ // Ensures the static initializer is called
+ }
}
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 5ef2120c6..347a4c63a 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -90,7 +90,7 @@ import java.util.TreeSet;
/**
* Input method implementation for Qwerty'ish keyboard.
*/
-public final class LatinIME extends InputMethodService implements KeyboardActionListener,
+public class LatinIME extends InputMethodService implements KeyboardActionListener,
SuggestionStripView.Listener, TargetApplicationGetter.OnTargetApplicationKnownListener,
Suggest.SuggestInitializationListener {
private static final String TAG = LatinIME.class.getSimpleName();
@@ -188,6 +188,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// Keeps track of most recently inserted text (multi-character key) for reverting
private String mEnteredText;
+ // TODO: This boolean is persistent state and causes large side effects at unexpected times.
+ // Find a way to remove it for readability.
private boolean mIsAutoCorrectionIndicatorOn;
private AlertDialog mOptionsDialog;
@@ -250,6 +252,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
}
public void postResumeSuggestions() {
+ removeMessages(MSG_RESUME_SUGGESTIONS);
sendMessageDelayed(obtainMessage(MSG_RESUME_SUGGESTIONS), mDelayUpdateSuggestions);
}
@@ -759,7 +762,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
}
mSuggestedWords = SuggestedWords.EMPTY;
- mConnection.resetCachesUponCursorMove(editorInfo.initialSelStart);
+ mConnection.resetCachesUponCursorMove(editorInfo.initialSelStart,
+ false /* shouldFinishComposition */);
if (isDifferentTextField) {
mainKeyboardView.closing();
@@ -900,7 +904,12 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// we know for sure the cursor moved while we were composing and we should reset
// the state.
final boolean noComposingSpan = composingSpanStart == -1 && composingSpanEnd == -1;
- if (!mExpectingUpdateSelection
+ // If the keyboard is not visible, we don't need to do all the housekeeping work, as it
+ // will be reset when the keyboard shows up anyway.
+ // TODO: revisit this when LatinIME supports hardware keyboards.
+ // NOTE: the test harness subclasses LatinIME and overrides isInputViewShown().
+ // TODO: find a better way to simulate actual execution.
+ if (isInputViewShown() && !mExpectingUpdateSelection
&& !mConnection.isBelatedExpectedUpdate(oldSelStart, newSelStart)) {
// TAKE CARE: there is a race condition when we enter this test even when the user
// did not explicitly move the cursor. This happens when typing fast, where two keys
@@ -1148,13 +1157,14 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// This will reset the whole input state to the starting state. It will clear
// the composing word, reset the last composed word, tell the inputconnection about it.
private void resetEntireInputState(final int newCursorPosition) {
+ final boolean shouldFinishComposition = mWordComposer.isComposingWord();
resetComposingState(true /* alsoResetLastComposedWord */);
if (mSettings.getCurrent().mBigramPredictionEnabled) {
clearSuggestionStrip();
} else {
setSuggestedWords(mSettings.getCurrent().mSuggestPuncList, false);
}
- mConnection.resetCachesUponCursorMove(newCursorPosition);
+ mConnection.resetCachesUponCursorMove(newCursorPosition, shouldFinishComposition);
}
private void resetComposingState(final boolean alsoResetLastComposedWord) {
@@ -1577,21 +1587,11 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
commitTyped(LastComposedWord.NOT_A_SEPARATOR);
}
mExpectingUpdateSelection = true;
- // The following is necessary for the case where the user typed something but didn't
- // manual pick it and didn't input any separator: we want to put a space between what
- // has been entered and the coming gesture input result, so we go into phantom space
- // state, which will be promoted to a space when the gesture result is committed. But if
- // the current input ends in a word connector on the other hand, then we want to have
- // the next input stick to the current input so we don't switch to phantom space state.
- if (!mSettings.getCurrent().isWordConnector(lastChar)) {
- mSpaceState = SPACE_STATE_PHANTOM;
- }
- } else {
- final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor();
- if (Character.isLetter(codePointBeforeCursor)
- || mSettings.getCurrent().isUsuallyFollowedBySpace(codePointBeforeCursor)) {
- mSpaceState = SPACE_STATE_PHANTOM;
- }
+ }
+ final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor();
+ if (Character.isLetterOrDigit(codePointBeforeCursor)
+ || mSettings.getCurrent().isUsuallyFollowedBySpace(codePointBeforeCursor)) {
+ mSpaceState = SPACE_STATE_PHANTOM;
}
mConnection.endBatchEdit();
mWordComposer.setCapitalizedModeAtStartComposingTime(getActualCapsMode());
@@ -1759,9 +1759,16 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// Called from PointerTracker through the KeyboardActionListener interface
@Override
+ public void onFinishSlidingInput() {
+ // User finished sliding input.
+ mKeyboardSwitcher.onFinishSlidingInput();
+ }
+
+ // Called from PointerTracker through the KeyboardActionListener interface
+ @Override
public void onCancelInput() {
// User released a finger outside any key
- mKeyboardSwitcher.onCancelInput();
+ // Nothing to do so far.
}
@Override
@@ -1905,6 +1912,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
final int y, final int spaceState) {
boolean isComposingWord = mWordComposer.isComposingWord();
+ // TODO: remove isWordConnector() and use isUsuallyFollowedBySpace() instead.
+ // See onStartBatchInput() to see how to do it.
if (SPACE_STATE_PHANTOM == spaceState &&
!mSettings.getCurrent().isWordConnector(primaryCode)) {
if (isComposingWord) {
@@ -2185,6 +2194,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
mConnection.getNthPreviousWord(mSettings.getCurrent().mWordSeparators,
mWordComposer.isComposingWord() ? 2 : 1);
return mSuggest.getSuggestedWords(mWordComposer, prevWord, keyboard.getProximityInfo(),
+ mSettings.getBlockPotentiallyOffensive(),
mSettings.getCurrent().mCorrectionEnabled, sessionId);
}
@@ -2443,10 +2453,15 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
private void restartSuggestionsOnWordTouchedByCursor() {
// If the cursor is not touching a word, or if there is a selection, return right away.
if (mLastSelectionStart != mLastSelectionEnd) return;
+ // If we don't know the cursor location, return.
+ if (mLastSelectionStart < 0) return;
if (!mConnection.isCursorTouchingWord(mSettings.getCurrent())) return;
final Range range = mConnection.getWordRangeAtCursor(mSettings.getWordSeparators(),
0 /* additionalPrecedingWordsCount */);
if (null == range) return; // Happens if we don't have an input connection at all
+ // If for some strange reason (editor bug or so) we measure the text before the cursor as
+ // longer than what the entire text is supposed to be, the safe thing to do is bail out.
+ if (range.mCharsBefore > mLastSelectionStart) return;
final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
final String typedWord = range.mWord.toString();
if (range.mWord instanceof SpannableString) {
@@ -2499,7 +2514,10 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// Note that it's very important here that suggestedWords.mWillAutoCorrect is false.
// We never want to auto-correct on a resumed suggestion. Please refer to the three
- // places above where suggestedWords is affected.
+ // places above where suggestedWords is affected. We also need to reset
+ // mIsAutoCorrectionIndicatorOn to avoid showSuggestionStrip touching the text to adapt it.
+ // TODO: remove mIsAutoCorrectionIndicator on (see comment on definition)
+ mIsAutoCorrectionIndicatorOn = false;
showSuggestionStrip(suggestedWords, typedWord);
}
@@ -2611,8 +2629,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// Callback called by PointerTracker through the KeyboardActionListener. This is called when a
// key is depressed; release matching call is onReleaseKey below.
@Override
- public void onPressKey(final int primaryCode) {
- mKeyboardSwitcher.onPressKey(primaryCode);
+ public void onPressKey(final int primaryCode, final boolean isSinglePointer) {
+ mKeyboardSwitcher.onPressKey(primaryCode, isSinglePointer);
}
// Callback by PointerTracker through the KeyboardActionListener. This is called when a key
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/ResourceUtils.java b/java/src/com/android/inputmethod/latin/ResourceUtils.java
index b74b979b5..a9fba5348 100644
--- a/java/src/com/android/inputmethod/latin/ResourceUtils.java
+++ b/java/src/com/android/inputmethod/latin/ResourceUtils.java
@@ -19,14 +19,17 @@ package com.android.inputmethod.latin;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
+import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
+import com.android.inputmethod.annotations.UsedForTesting;
+
+import java.util.ArrayList;
import java.util.HashMap;
public final class ResourceUtils {
private static final String TAG = ResourceUtils.class.getSimpleName();
- private static final boolean DEBUG = false;
public static final float UNDEFINED_RATIO = -1.0f;
public static final int UNDEFINED_DIMENSION = -1;
@@ -35,11 +38,32 @@ public final class ResourceUtils {
// This utility class is not publicly instantiable.
}
- private static final String DEFAULT_PREFIX = "DEFAULT,";
- private static final String HARDWARE_PREFIX = Build.HARDWARE + ",";
private static final HashMap<String, String> sDeviceOverrideValueMap =
CollectionUtils.newHashMap();
+ private static final String[] BUILD_KEYS_AND_VALUES = {
+ "HARDWARE", Build.HARDWARE,
+ "MODEL", Build.MODEL,
+ "BRAND", Build.BRAND,
+ "MANUFACTURER", Build.MANUFACTURER
+ };
+ private static final HashMap<String, String> sBuildKeyValues;
+ private static final String sBuildKeyValuesDebugString;
+
+ static {
+ sBuildKeyValues = CollectionUtils.newHashMap();
+ final ArrayList<String> keyValuePairs = CollectionUtils.newArrayList();
+ final int keyCount = BUILD_KEYS_AND_VALUES.length / 2;
+ for (int i = 0; i < keyCount; i++) {
+ final int index = i * 2;
+ final String key = BUILD_KEYS_AND_VALUES[index];
+ final String value = BUILD_KEYS_AND_VALUES[index + 1];
+ sBuildKeyValues.put(key, value);
+ keyValuePairs.add(key + '=' + value);
+ }
+ sBuildKeyValuesDebugString = "[" + TextUtils.join(" ", keyValuePairs) + "]";
+ }
+
public static String getDeviceOverrideValue(final Resources res, final int overrideResId) {
final int orientation = res.getConfiguration().orientation;
final String key = overrideResId + "-" + orientation;
@@ -48,33 +72,114 @@ public final class ResourceUtils {
}
final String[] overrideArray = res.getStringArray(overrideResId);
- final String overrideValue = StringUtils.findPrefixedString(HARDWARE_PREFIX, overrideArray);
+ final String overrideValue = findConstantForKeyValuePairs(sBuildKeyValues, overrideArray);
// The overrideValue might be an empty string.
if (overrideValue != null) {
- if (DEBUG) {
- Log.d(TAG, "Find override value:"
- + " resource="+ res.getResourceEntryName(overrideResId)
- + " Build.HARDWARE=" + Build.HARDWARE + " override=" + overrideValue);
- }
+ Log.i(TAG, "Find override value:"
+ + " resource="+ res.getResourceEntryName(overrideResId)
+ + " build=" + sBuildKeyValuesDebugString
+ + " override=" + overrideValue);
sDeviceOverrideValueMap.put(key, overrideValue);
return overrideValue;
}
- final String defaultValue = StringUtils.findPrefixedString(DEFAULT_PREFIX, overrideArray);
+ final String defaultValue = findDefaultConstant(overrideArray);
// The defaultValue might be an empty string.
if (defaultValue == null) {
Log.w(TAG, "Couldn't find override value nor default value:"
+ " resource="+ res.getResourceEntryName(overrideResId)
- + " Build.HARDWARE=" + Build.HARDWARE);
- } else if (DEBUG) {
- Log.d(TAG, "Found default value:"
- + " resource="+ res.getResourceEntryName(overrideResId)
- + " Build.HARDWARE=" + Build.HARDWARE + " default=" + defaultValue);
+ + " build=" + sBuildKeyValuesDebugString);
+ } else {
+ Log.i(TAG, "Found default value:"
+ + " resource="+ res.getResourceEntryName(overrideResId)
+ + " build=" + sBuildKeyValuesDebugString
+ + " default=" + defaultValue);
}
sDeviceOverrideValueMap.put(key, defaultValue);
return defaultValue;
}
+ /**
+ * Find the condition that fulfills specified key value pairs from an array of
+ * "condition,constant", and return the corresponding string constant. A condition is
+ * "pattern1[:pattern2...] (or an empty string for the default). A pattern is
+ * "key=regexp_value" string. The condition matches only if all patterns of the condition
+ * are true for the specified key value pairs.
+ *
+ * For example, "condition,constant" has the following format.
+ * (See {@link ResourceUtilsTests#testFindConstantForKeyValuePairsRegexp()})
+ * - HARDWARE=mako,constantForNexus4
+ * - MODEL=Nexus 4:MANUFACTURER=LGE,constantForNexus4
+ * - ,defaultConstant
+ *
+ * @param keyValuePairs attributes to be used to look for a matched condition.
+ * @param conditionConstantArray an array of "condition,constant" elements to be searched.
+ * @return the constant part of the matched "condition,constant" element. Returns null if no
+ * condition matches.
+ */
+ @UsedForTesting
+ static String findConstantForKeyValuePairs(final HashMap<String, String> keyValuePairs,
+ final String[] conditionConstantArray) {
+ if (conditionConstantArray == null || keyValuePairs == null) {
+ return null;
+ }
+ for (final String conditionConstant : conditionConstantArray) {
+ final int posComma = conditionConstant.indexOf(',');
+ if (posComma < 0) {
+ throw new RuntimeException("Array element has no comma: " + conditionConstant);
+ }
+ final String condition = conditionConstant.substring(0, posComma);
+ if (condition.isEmpty()) {
+ // Default condition. The default condition should be searched by
+ // {@link #findConstantForDefault(String[])}.
+ continue;
+ }
+ if (fulfillsCondition(keyValuePairs, condition)) {
+ return conditionConstant.substring(posComma + 1);
+ }
+ }
+ return null;
+ }
+
+ private static boolean fulfillsCondition(final HashMap<String,String> keyValuePairs,
+ final String condition) {
+ final String[] patterns = condition.split(":");
+ // Check all patterns in a condition are true
+ for (final String pattern : patterns) {
+ final int posEqual = pattern.indexOf('=');
+ if (posEqual < 0) {
+ throw new RuntimeException("Pattern has no '=': " + condition);
+ }
+ final String key = pattern.substring(0, posEqual);
+ final String value = keyValuePairs.get(key);
+ if (value == null) {
+ throw new RuntimeException("Found unknown key: " + condition);
+ }
+ final String patternRegexpValue = pattern.substring(posEqual + 1);
+ if (!value.matches(patternRegexpValue)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @UsedForTesting
+ static String findDefaultConstant(final String[] conditionConstantArray) {
+ if (conditionConstantArray == null) {
+ return null;
+ }
+ for (final String condition : conditionConstantArray) {
+ final int posComma = condition.indexOf(',');
+ if (posComma < 0) {
+ throw new RuntimeException("Array element has no comma: " + condition);
+ }
+ if (posComma == 0) { // condition is empty.
+ return condition.substring(posComma + 1);
+ }
+ }
+ return null;
+ }
+
public static boolean isValidFraction(final float fraction) {
return fraction >= 0.0f;
}
diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java
index 8ed7ab264..980215de6 100644
--- a/java/src/com/android/inputmethod/latin/RichInputConnection.java
+++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java
@@ -135,13 +135,14 @@ public final class RichInputConnection {
if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
}
- public void resetCachesUponCursorMove(final int newCursorPosition) {
+ public void resetCachesUponCursorMove(final int newCursorPosition,
+ final boolean shouldFinishComposition) {
mCurrentCursorPosition = newCursorPosition;
mComposingText.setLength(0);
mCommittedTextBeforeComposingText.setLength(0);
final CharSequence textBeforeCursor = getTextBeforeCursor(DEFAULT_TEXT_CACHE_SIZE, 0);
if (null != textBeforeCursor) mCommittedTextBeforeComposingText.append(textBeforeCursor);
- if (null != mIC) {
+ if (null != mIC && shouldFinishComposition) {
mIC.finishComposingText();
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
ResearchLogger.richInputConnection_finishComposingText();
diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
index e39aae958..94513e635 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;
@@ -97,12 +100,107 @@ public final class RichInputMethodManager {
throw new RuntimeException("Input method id for " + packageName + " not found.");
}
+ public List<InputMethodSubtype> getMyEnabledInputMethodSubtypeList(
+ boolean allowsImplicitlySelectedSubtypes) {
+ return mImmWrapper.mImm.getEnabledInputMethodSubtypeList(
+ mInputMethodInfoOfThisIme, allowsImplicitlySelectedSubtypes);
+ }
+
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 = getMyEnabledInputMethodSubtypeList(
+ 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;
}
@@ -122,8 +220,8 @@ public final class RichInputMethodManager {
final InputMethodSubtype subtype) {
final boolean subtypeEnabled = checkIfSubtypeBelongsToThisImeAndEnabled(subtype);
final boolean subtypeExplicitlyEnabled = checkIfSubtypeBelongsToList(
- subtype, mImmWrapper.mImm.getEnabledInputMethodSubtypeList(
- mInputMethodInfoOfThisIme, false /* allowsImplicitlySelectedSubtypes */));
+ subtype, getMyEnabledInputMethodSubtypeList(
+ false /* allowsImplicitlySelectedSubtypes */));
return subtypeEnabled && !subtypeExplicitlyEnabled;
}
@@ -136,24 +234,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(
@@ -209,8 +318,7 @@ public final class RichInputMethodManager {
if (filteredImisCount > 1) {
return true;
}
- final List<InputMethodSubtype> subtypes =
- mImmWrapper.mImm.getEnabledInputMethodSubtypeList(null, true);
+ final List<InputMethodSubtype> subtypes = getMyEnabledInputMethodSubtypeList(true);
int keyboardCount = 0;
// imm.getEnabledInputMethodSubtypeList(null, true) will return the current IME's
// both explicitly and implicitly enabled input method subtype.
diff --git a/java/src/com/android/inputmethod/latin/SeekBarDialogPreference.java b/java/src/com/android/inputmethod/latin/SeekBarDialogPreference.java
index 9819a02ef..7c4156c48 100644
--- a/java/src/com/android/inputmethod/latin/SeekBarDialogPreference.java
+++ b/java/src/com/android/inputmethod/latin/SeekBarDialogPreference.java
@@ -59,7 +59,7 @@ public final class SeekBarDialogPreference extends DialogPreference
public void setInterface(final ValueProxy proxy) {
mValueProxy = proxy;
- setSummary(getValueText(proxy.readValue(getKey())));
+ setSummary(getValueText(clipValue(proxy.readValue(getKey()))));
}
private String getValueText(final int value) {
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/StringUtils.java b/java/src/com/android/inputmethod/latin/StringUtils.java
index d5ee58a63..ab050d7a3 100644
--- a/java/src/com/android/inputmethod/latin/StringUtils.java
+++ b/java/src/com/android/inputmethod/latin/StringUtils.java
@@ -65,23 +65,6 @@ public final class StringUtils {
}
/**
- * Find a string that start with specified prefix from an array.
- *
- * @param prefix a prefix string to find.
- * @param array an string array to be searched.
- * @return the rest part of the string that starts with the prefix.
- * Returns null if it couldn't be found.
- */
- public static String findPrefixedString(final String prefix, final String[] array) {
- for (final String element : array) {
- if (element.startsWith(prefix)) {
- return element.substring(prefix.length());
- }
- }
- return null;
- }
-
- /**
* Remove duplicates from an array of strings.
*
* This method will always keep the first occurrence of all strings at their position
diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
index 2f9e34ff1..282b5794f 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);
@@ -111,7 +115,7 @@ public final class SubtypeSwitcher {
*/
public void updateParametersOnStartInputView() {
final List<InputMethodSubtype> enabledSubtypesOfThisIme =
- mRichImm.getInputMethodManager().getEnabledInputMethodSubtypeList(null, true);
+ mRichImm.getMyEnabledInputMethodSubtypeList(true);
mNeedsToDisplayLanguage.updateEnabledSubtypeCount(enabledSubtypesOfThisIme.size());
updateShortcutIME();
}
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 59d0207f6..dc9bef22a 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -174,21 +174,22 @@ public final class Suggest {
public SuggestedWords getSuggestedWords(final WordComposer wordComposer,
final String prevWordForBigram, final ProximityInfo proximityInfo,
- final boolean isCorrectionEnabled, final int sessionId) {
+ final boolean blockOffensiveWords, final boolean isCorrectionEnabled,
+ final int sessionId) {
LatinImeLogger.onStartSuggestion(prevWordForBigram);
if (wordComposer.isBatchMode()) {
return getSuggestedWordsForBatchInput(
- wordComposer, prevWordForBigram, proximityInfo, sessionId);
+ wordComposer, prevWordForBigram, proximityInfo, blockOffensiveWords, sessionId);
} else {
return getSuggestedWordsForTypingInput(wordComposer, prevWordForBigram, proximityInfo,
- isCorrectionEnabled);
+ blockOffensiveWords, isCorrectionEnabled);
}
}
// Retrieves suggestions for the typing input.
private SuggestedWords getSuggestedWordsForTypingInput(final WordComposer wordComposer,
final String prevWordForBigram, final ProximityInfo proximityInfo,
- final boolean isCorrectionEnabled) {
+ final boolean blockOffensiveWords, final boolean isCorrectionEnabled) {
final int trailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount();
final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
MAX_SUGGESTIONS);
@@ -212,7 +213,7 @@ public final class Suggest {
for (final String key : mDictionaries.keySet()) {
final Dictionary dictionary = mDictionaries.get(key);
suggestionsSet.addAll(dictionary.getSuggestions(
- wordComposerForLookup, prevWordForBigram, proximityInfo));
+ wordComposerForLookup, prevWordForBigram, proximityInfo, blockOffensiveWords));
}
final String whitelistedWord;
@@ -301,7 +302,7 @@ public final class Suggest {
// Retrieves suggestions for the batch input.
private SuggestedWords getSuggestedWordsForBatchInput(final WordComposer wordComposer,
final String prevWordForBigram, final ProximityInfo proximityInfo,
- final int sessionId) {
+ final boolean blockOffensiveWords, final int sessionId) {
final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
MAX_SUGGESTIONS);
@@ -314,8 +315,8 @@ public final class Suggest {
continue;
}
final Dictionary dictionary = mDictionaries.get(key);
- suggestionsSet.addAll(dictionary.getSuggestionsWithSessionId(
- wordComposer, prevWordForBigram, proximityInfo, sessionId));
+ suggestionsSet.addAll(dictionary.getSuggestionsWithSessionId(wordComposer,
+ prevWordForBigram, proximityInfo, blockOffensiveWords, sessionId));
}
for (SuggestedWordInfo wordInfo : suggestionsSet) {
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/SynchronouslyLoadedContactsBinaryDictionary.java b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsBinaryDictionary.java
index ec4dc1436..92f96c027 100644
--- a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsBinaryDictionary.java
@@ -33,9 +33,10 @@ public final class SynchronouslyLoadedContactsBinaryDictionary extends ContactsB
@Override
public synchronized ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer codes,
- final String prevWordForBigrams, final ProximityInfo proximityInfo) {
+ final String prevWordForBigrams, final ProximityInfo proximityInfo,
+ final boolean blockOffensiveWords) {
syncReloadDictionaryIfRequired();
- return super.getSuggestions(codes, prevWordForBigrams, proximityInfo);
+ return super.getSuggestions(codes, prevWordForBigrams, proximityInfo, blockOffensiveWords);
}
@Override
diff --git a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserBinaryDictionary.java
index 4bdaf2039..33fe89611 100644
--- a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserBinaryDictionary.java
@@ -36,9 +36,10 @@ public final class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDic
@Override
public synchronized ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer codes,
- final String prevWordForBigrams, final ProximityInfo proximityInfo) {
+ final String prevWordForBigrams, final ProximityInfo proximityInfo,
+ final boolean blockOffensiveWords) {
syncReloadDictionaryIfRequired();
- return super.getSuggestions(codes, prevWordForBigrams, proximityInfo);
+ return super.getSuggestions(codes, prevWordForBigrams, proximityInfo, blockOffensiveWords);
}
@Override
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/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java
index 51bd901fb..e078f03f4 100644
--- a/java/src/com/android/inputmethod/latin/WordComposer.java
+++ b/java/src/com/android/inputmethod/latin/WordComposer.java
@@ -16,7 +16,6 @@
package com.android.inputmethod.latin;
-import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.Keyboard;
@@ -211,9 +210,8 @@ public final class WordComposer {
}
/**
- * Internal method to retrieve reasonable proximity info for a character.
+ * Add a dummy key by retrieving reasonable coordinates
*/
- @UsedForTesting
public void addKeyInfo(final int codePoint, final Keyboard keyboard) {
final int x, y;
final Key key;
diff --git a/java/src/com/android/inputmethod/latin/personalization/AccountUtils.java b/java/src/com/android/inputmethod/latin/personalization/AccountUtils.java
new file mode 100644
index 000000000..93687e193
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/personalization/AccountUtils.java
@@ -0,0 +1,47 @@
+/*
+ * 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.personalization;
+
+import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.content.Context;
+import android.util.Patterns;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class AccountUtils {
+ private AccountUtils() {
+ // This utility class is not publicly instantiable.
+ }
+
+ private static Account[] getAccounts(final Context context) {
+ return AccountManager.get(context).getAccounts();
+ }
+
+ public static List<String> getDeviceAccountsEmailAddresses(final Context context) {
+ final ArrayList<String> retval = new ArrayList<String>();
+ for (final Account account : getAccounts(context)) {
+ final String name = account.name;
+ if (Patterns.EMAIL_ADDRESS.matcher(name).matches()) {
+ retval.add(name);
+ retval.add(name.split("@")[0]);
+ }
+ }
+ return retval;
+ }
+}
diff --git a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
index 15d0bac37..8a2de887d 100644
--- a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
+++ b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
@@ -19,172 +19,25 @@ package com.android.inputmethod.latin.setup;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
-import android.content.res.Resources;
-import android.graphics.PorterDuff;
-import android.graphics.drawable.Drawable;
import android.os.Bundle;
-import android.os.Message;
import android.provider.Settings;
-import android.view.View;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
-import android.widget.TextView;
-import com.android.inputmethod.compat.TextViewCompatUtils;
-import com.android.inputmethod.compat.ViewCompatUtils;
-import com.android.inputmethod.latin.CollectionUtils;
-import com.android.inputmethod.latin.R;
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();
- private static final String STATE_STEP = "step";
- private int mStepNumber;
- private static final int STEP_1 = 1;
- private static final int STEP_2 = 2;
- private static final int STEP_3 = 3;
-
- private final SettingsPoolingHandler mHandler = new SettingsPoolingHandler(this);
-
- static final class SettingsPoolingHandler extends StaticInnerHandlerWrapper<SetupActivity> {
- private static final int MSG_POLLING_IME_SETTINGS = 0;
- private static final long IME_SETTINGS_POLLING_INTERVAL = 200;
-
- public SettingsPoolingHandler(final SetupActivity outerInstance) {
- super(outerInstance);
- }
-
- @Override
- public void handleMessage(final Message msg) {
- final SetupActivity setupActivity = getOuterInstance();
- if (setupActivity == null) {
- return;
- }
- switch (msg.what) {
- case MSG_POLLING_IME_SETTINGS:
- if (SetupActivity.isThisImeEnabled(setupActivity)) {
- setupActivity.invokeSetupWizardOfThisIme();
- return;
- }
- startPollingImeSettings();
- break;
- }
- }
-
- public void startPollingImeSettings() {
- sendMessageDelayed(obtainMessage(MSG_POLLING_IME_SETTINGS),
- IME_SETTINGS_POLLING_INTERVAL);
- }
-
- public void cancelPollingImeSettings() {
- removeMessages(MSG_POLLING_IME_SETTINGS);
- }
- }
-
@Override
protected void onCreate(final Bundle savedInstanceState) {
- setTheme(android.R.style.Theme_DeviceDefault_Light_NoActionBar);
super.onCreate(savedInstanceState);
-
- setContentView(R.layout.setup_wizard);
-
- RichInputMethodManager.init(this);
-
- if (savedInstanceState == null) {
- mStepNumber = determineSetupStepNumber();
- } else {
- mStepNumber = savedInstanceState.getInt(STATE_STEP);
- }
-
- if (mStepNumber == STEP_3) {
- // This IME already has been enabled and set as current IME.
- // TODO: Implement tutorial.
- invokeSettingsOfThisIme();
- finish();
- 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);
- step1.setAction(new Runnable() {
- @Override
- public void run() {
- invokeLanguageAndInputSettings();
- mHandler.startPollingImeSettings();
- }
- });
- mSetupSteps.addStep(STEP_1, 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);
- step2.setAction(new Runnable() {
- @Override
- public void run() {
- // Invoke input method picker.
- RichInputMethodManager.getInstance().getInputMethodManager()
- .showInputMethodPicker();
- }
- });
- mSetupSteps.addStep(STEP_2, 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);
- step3.setAction(new Runnable() {
- @Override
- public void run() {
- invokeSubtypeEnablerOfThisIme();
- }
- });
- mSetupSteps.addStep(STEP_3, step3);
- }
-
- private void invokeSetupWizardOfThisIme() {
final Intent intent = new Intent();
- intent.setClass(this, SetupActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
- | Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- }
-
- private void invokeSettingsOfThisIme() {
- final Intent intent = new Intent();
- intent.setClass(this, SettingsActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
- | Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- }
-
- private void invokeLanguageAndInputSettings() {
- final Intent intent = new Intent();
- intent.setAction(Settings.ACTION_INPUT_METHOD_SETTINGS);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- startActivity(intent);
- }
-
- private void invokeSubtypeEnablerOfThisIme() {
- final InputMethodInfo imi =
- RichInputMethodManager.getInstance().getInputMethodInfoOfThisIme();
- final Intent intent = new Intent();
- intent.setAction(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, imi.getId());
+ intent.setClass(this, SetupWizardActivity.class);
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
+ | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
+ if (!isFinishing()) {
+ finish();
+ }
}
/**
@@ -221,141 +74,4 @@ public final class SetupActivity extends Activity {
context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
return myImi.getId().equals(currentImeId);
}
-
- private int determineSetupStepNumber() {
- mHandler.cancelPollingImeSettings();
- if (!isThisImeEnabled(this)) {
- return STEP_1;
- }
- if (!isThisImeCurrent(this)) {
- return STEP_2;
- }
- return STEP_3;
- }
-
- @Override
- protected void onSaveInstanceState(final Bundle outState) {
- super.onSaveInstanceState(outState);
- outState.putInt(STATE_STEP, mStepNumber);
- }
-
- @Override
- protected void onRestoreInstanceState(final Bundle savedInstanceState) {
- super.onRestoreInstanceState(savedInstanceState);
- mStepNumber = savedInstanceState.getInt(STATE_STEP);
- }
-
- @Override
- protected void onStart() {
- super.onStart();
- mStepNumber = determineSetupStepNumber();
- }
-
- @Override
- protected void onRestart() {
- super.onRestart();
- mStepNumber = determineSetupStepNumber();
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- updateSetupStepView();
- }
-
- @Override
- public void onWindowFocusChanged(final boolean hasFocus) {
- super.onWindowFocusChanged(hasFocus);
- if (!hasFocus) {
- return;
- }
- mStepNumber = determineSetupStepNumber();
- updateSetupStepView();
- }
-
- 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;
- }
-
- static final class SetupStep implements View.OnClickListener {
- private final View mRootView;
- 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);
- titleView.setText(res.getString(title, 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.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);
- }
- }
-
- public void setEnabled(final boolean enabled) {
- mRootView.setVisibility(enabled ? View.VISIBLE : View.GONE);
- }
-
- public void setAction(final Runnable action) {
- mActionLabel.setOnClickListener(this);
- mAction = action;
- }
-
- @Override
- public void onClick(final View v) {
- if (mAction != null) {
- mAction.run();
- }
- }
- }
-
- static final class SetupStepGroup {
- private final HashMap<Integer, SetupStep> mGroup = CollectionUtils.newHashMap();
-
- public void addStep(final int stepNo, final SetupStep step) {
- mGroup.put(stepNo, step);
- }
-
- public void enableStep(final int enableStepNo) {
- for (final Integer stepNo : mGroup.keySet()) {
- final SetupStep step = mGroup.get(stepNo);
- step.setEnabled(stepNo == enableStepNo);
- }
- }
-
- public int getTotalStep() {
- return 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..974dfddd3
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java
@@ -0,0 +1,123 @@
+/*
+ * 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;
+ }
+
+ // TODO: Once we stop supporting ICS, uncomment {@link #setPressed(boolean)} method and
+ // remove this method.
+ @Override
+ protected void drawableStateChanged() {
+ super.drawableStateChanged();
+ for (final int state : getDrawableState()) {
+ if (state == android.R.attr.state_pressed) {
+ updateIndicatorView(true /* pressed */);
+ return;
+ }
+ }
+ updateIndicatorView(false /* pressed */);
+ }
+
+ // TODO: Once we stop supporting ICS, uncomment this method and remove
+ // {@link #drawableStateChanged()} method.
+// @Override
+// public void setPressed(final boolean pressed) {
+// super.setPressed(pressed);
+// updateIndicatorView(pressed);
+// }
+
+ private void updateIndicatorView(final boolean pressed) {
+ if (mIndicatorView != null) {
+ mIndicatorView.setPressed(pressed);
+ mIndicatorView.invalidate();
+ }
+ }
+ }
+
+ 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
+ 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/setup/SetupWizardActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupWizardActivity.java
new file mode 100644
index 000000000..78a6478c6
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/setup/SetupWizardActivity.java
@@ -0,0 +1,493 @@
+/*
+ * 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.app.Activity;
+import android.content.ContentResolver;
+import android.content.Intent;
+import android.content.res.Resources;
+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.widget.ImageView;
+import android.widget.TextView;
+import android.widget.VideoView;
+
+import com.android.inputmethod.compat.TextViewCompatUtils;
+import com.android.inputmethod.compat.ViewCompatUtils;
+import com.android.inputmethod.latin.CollectionUtils;
+import com.android.inputmethod.latin.R;
+import com.android.inputmethod.latin.RichInputMethodManager;
+import com.android.inputmethod.latin.SettingsActivity;
+import com.android.inputmethod.latin.StaticInnerHandlerWrapper;
+
+import java.util.ArrayList;
+
+// TODO: Use Fragment to implement welcome screen and setup steps.
+public final class SetupWizardActivity extends Activity implements View.OnClickListener {
+ static final String TAG = SetupWizardActivity.class.getSimpleName();
+
+ private static final boolean ENABLE_WELCOME_VIDEO = true;
+
+ private View mSetupWizard;
+ private View mWelcomeScreen;
+ private View mSetupScreen;
+ private Uri mWelcomeVideoUri;
+ private VideoView mWelcomeVideoView;
+ private ImageView mWelcomeImageView;
+ 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 boolean mNeedsToAdjustStepNumberToSystemState;
+ private static final int STEP_WELCOME = 0;
+ private static final int STEP_1 = 1;
+ private static final int STEP_2 = 2;
+ private static final int STEP_3 = 3;
+ private static final int STEP_LAUNCHING_IME_SETTINGS = 4;
+ private static final int STEP_BACK_FROM_IME_SETTINGS = 5;
+
+ final SettingsPoolingHandler mHandler = new SettingsPoolingHandler(this);
+
+ static final class SettingsPoolingHandler
+ extends StaticInnerHandlerWrapper<SetupWizardActivity> {
+ private static final int MSG_POLLING_IME_SETTINGS = 0;
+ private static final long IME_SETTINGS_POLLING_INTERVAL = 200;
+
+ public SettingsPoolingHandler(final SetupWizardActivity outerInstance) {
+ super(outerInstance);
+ }
+
+ @Override
+ public void handleMessage(final Message msg) {
+ final SetupWizardActivity setupWizardActivity = getOuterInstance();
+ if (setupWizardActivity == null) {
+ return;
+ }
+ switch (msg.what) {
+ case MSG_POLLING_IME_SETTINGS:
+ if (SetupActivity.isThisImeEnabled(setupWizardActivity)) {
+ setupWizardActivity.invokeSetupWizardOfThisIme();
+ return;
+ }
+ startPollingImeSettings();
+ break;
+ }
+ }
+
+ public void startPollingImeSettings() {
+ sendMessageDelayed(obtainMessage(MSG_POLLING_IME_SETTINGS),
+ IME_SETTINGS_POLLING_INTERVAL);
+ }
+
+ public void cancelPollingImeSettings() {
+ removeMessages(MSG_POLLING_IME_SETTINGS);
+ }
+ }
+
+ @Override
+ protected void onCreate(final Bundle savedInstanceState) {
+ setTheme(android.R.style.Theme_Translucent_NoTitleBar);
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.setup_wizard);
+ mSetupWizard = findViewById(R.id.setup_wizard);
+
+ RichInputMethodManager.init(this);
+
+ if (savedInstanceState == null) {
+ mStepNumber = determineSetupStepNumberFromLauncher();
+ } else {
+ mStepNumber = savedInstanceState.getInt(STATE_STEP);
+ }
+
+ 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() {
+ invokeLanguageAndInputSettings();
+ mHandler.startPollingImeSettings();
+ }
+ });
+ mSetupStepGroup.addStep(step1);
+
+ 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() {
+ invokeInputMethodPicker();
+ }
+ });
+ mSetupStepGroup.addStep(step2);
+
+ 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();
+ }
+ });
+ mSetupStepGroup.addStep(step3);
+
+ mWelcomeVideoUri = new Uri.Builder()
+ .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
+ .authority(getPackageName())
+ .path(Integer.toString(R.raw.setup_welcome_video))
+ .build();
+ final VideoView welcomeVideoView = (VideoView)findViewById(R.id.setup_welcome_video);
+ welcomeVideoView.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.
+ welcomeVideoView.setBackgroundResource(0);
+ mp.setLooping(true);
+ }
+ });
+ welcomeVideoView.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);
+ hideWelcomeVideoAndShowWelcomeImage();
+ return true;
+ }
+ });
+ mWelcomeVideoView = welcomeVideoView;
+ mWelcomeImageView = (ImageView)findViewById(R.id.setup_welcome_image);
+
+ 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 currentStep = determineSetupStepNumber();
+ final int nextStep;
+ if (v == mActionStart) {
+ nextStep = STEP_1;
+ } else if (v == mActionNext) {
+ nextStep = mStepNumber + 1;
+ } else if (v == mStep1Bullet && currentStep == STEP_2) {
+ nextStep = STEP_1;
+ } else {
+ nextStep = mStepNumber;
+ }
+ if (mStepNumber != nextStep) {
+ mStepNumber = nextStep;
+ updateSetupStepView();
+ }
+ }
+
+ void invokeSetupWizardOfThisIme() {
+ final Intent intent = new Intent();
+ intent.setClass(this, SetupWizardActivity.class);
+ intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
+ | Intent.FLAG_ACTIVITY_SINGLE_TOP
+ | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ startActivity(intent);
+ mNeedsToAdjustStepNumberToSystemState = true;
+ }
+
+ private void invokeSettingsOfThisIme() {
+ final Intent intent = new Intent();
+ intent.setClass(this, SettingsActivity.class);
+ intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
+ | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ startActivity(intent);
+ }
+
+ void invokeLanguageAndInputSettings() {
+ final Intent intent = new Intent();
+ intent.setAction(Settings.ACTION_INPUT_METHOD_SETTINGS);
+ intent.addCategory(Intent.CATEGORY_DEFAULT);
+ startActivity(intent);
+ mNeedsToAdjustStepNumberToSystemState = true;
+ }
+
+ void invokeInputMethodPicker() {
+ // Invoke input method picker.
+ RichInputMethodManager.getInstance().getInputMethodManager()
+ .showInputMethodPicker();
+ mNeedsToAdjustStepNumberToSystemState = true;
+ }
+
+ void invokeSubtypeEnablerOfThisIme() {
+ final InputMethodInfo imi =
+ RichInputMethodManager.getInstance().getInputMethodInfoOfThisIme();
+ final Intent intent = new Intent();
+ intent.setAction(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
+ intent.addCategory(Intent.CATEGORY_DEFAULT);
+ intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, imi.getId());
+ startActivity(intent);
+ }
+
+ private int determineSetupStepNumberFromLauncher() {
+ final int stepNumber = determineSetupStepNumber();
+ if (stepNumber == STEP_1) {
+ return STEP_WELCOME;
+ }
+ if (stepNumber == STEP_3) {
+ return STEP_LAUNCHING_IME_SETTINGS;
+ }
+ return stepNumber;
+ }
+
+ private int determineSetupStepNumber() {
+ mHandler.cancelPollingImeSettings();
+ if (!SetupActivity.isThisImeEnabled(this)) {
+ return STEP_1;
+ }
+ if (!SetupActivity.isThisImeCurrent(this)) {
+ return STEP_2;
+ }
+ return STEP_3;
+ }
+
+ @Override
+ protected void onSaveInstanceState(final Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putInt(STATE_STEP, mStepNumber);
+ }
+
+ @Override
+ protected void onRestoreInstanceState(final Bundle savedInstanceState) {
+ super.onRestoreInstanceState(savedInstanceState);
+ mStepNumber = savedInstanceState.getInt(STATE_STEP);
+ }
+
+ private static boolean isInSetupSteps(final int stepNumber) {
+ return stepNumber >= STEP_1 && stepNumber <= STEP_3;
+ }
+
+ @Override
+ protected void onRestart() {
+ super.onRestart();
+ // Probably the setup wizard has been invoked from "Recent" menu. The setup step number
+ // needs to be adjusted to system state, because the state (IME is enabled and/or current)
+ // may have been changed.
+ if (isInSetupSteps(mStepNumber)) {
+ mStepNumber = determineSetupStepNumber();
+ }
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ if (mStepNumber == STEP_LAUNCHING_IME_SETTINGS) {
+ // Prevent white screen flashing while launching settings activity.
+ mSetupWizard.setVisibility(View.INVISIBLE);
+ invokeSettingsOfThisIme();
+ mStepNumber = STEP_BACK_FROM_IME_SETTINGS;
+ return;
+ }
+ if (mStepNumber == STEP_BACK_FROM_IME_SETTINGS) {
+ finish();
+ return;
+ }
+ updateSetupStepView();
+ }
+
+ @Override
+ public void onBackPressed() {
+ if (mStepNumber == STEP_1) {
+ mStepNumber = STEP_WELCOME;
+ updateSetupStepView();
+ return;
+ }
+ super.onBackPressed();
+ }
+
+ void hideWelcomeVideoAndShowWelcomeImage() {
+ mWelcomeVideoView.setVisibility(View.GONE);
+ mWelcomeImageView.setImageResource(R.raw.setup_welcome_image);
+ mWelcomeImageView.setVisibility(View.VISIBLE);
+ }
+
+ private void showAndStartWelcomeVideo() {
+ mWelcomeVideoView.setVisibility(View.VISIBLE);
+ mWelcomeVideoView.setVideoURI(mWelcomeVideoUri);
+ mWelcomeVideoView.start();
+ }
+
+ private void hideAndStopWelcomeVideo() {
+ mWelcomeVideoView.stopPlayback();
+ mWelcomeVideoView.setVisibility(View.GONE);
+ }
+
+ @Override
+ protected void onPause() {
+ hideAndStopWelcomeVideo();
+ super.onPause();
+ }
+
+ @Override
+ public void onWindowFocusChanged(final boolean hasFocus) {
+ super.onWindowFocusChanged(hasFocus);
+ if (hasFocus && mNeedsToAdjustStepNumberToSystemState) {
+ mNeedsToAdjustStepNumberToSystemState = false;
+ mStepNumber = determineSetupStepNumber();
+ updateSetupStepView();
+ }
+ }
+
+ private void updateSetupStepView() {
+ mSetupWizard.setVisibility(View.VISIBLE);
+ final boolean welcomeScreen = (mStepNumber == STEP_WELCOME);
+ mWelcomeScreen.setVisibility(welcomeScreen ? View.VISIBLE : View.GONE);
+ mSetupScreen.setVisibility(welcomeScreen ? View.GONE : View.VISIBLE);
+ if (welcomeScreen) {
+ if (ENABLE_WELCOME_VIDEO) {
+ showAndStartWelcomeVideo();
+ } else {
+ hideWelcomeVideoAndShowWelcomeImage();
+ }
+ return;
+ }
+ hideAndStopWelcomeVideo();
+ final boolean isStepActionAlreadyDone = mStepNumber < determineSetupStepNumber();
+ 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 {
+ 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 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);
+
+ 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 {
+ TextViewCompatUtils.setCompoundDrawablesRelativeWithIntrinsicBounds(
+ mActionLabel, res.getDrawable(actionIcon), null, null, null);
+ }
+ }
+
+ 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) {
+ mActionLabel.setOnClickListener(this);
+ mAction = action;
+ }
+
+ @Override
+ public void onClick(final View v) {
+ if (v == mActionLabel && mAction != null) {
+ mAction.run();
+ return;
+ }
+ }
+ }
+
+ static final class SetupStepGroup {
+ private final SetupStepIndicatorView mIndicatorView;
+ private final ArrayList<SetupStep> mGroup = CollectionUtils.newArrayList();
+
+ public SetupStepGroup(final SetupStepIndicatorView indicatorView) {
+ mIndicatorView = indicatorView;
+ }
+
+ public void addStep(final SetupStep step) {
+ mGroup.add(step);
+ }
+
+ 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/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
index 166705954..13fcaf48a 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
@@ -23,7 +23,7 @@ import android.service.textservice.SpellCheckerService;
import android.util.Log;
import android.view.textservice.SuggestionsInfo;
-import com.android.inputmethod.keyboard.ProximityInfo;
+import com.android.inputmethod.keyboard.KeyboardLayoutSet;
import com.android.inputmethod.latin.BinaryDictionary;
import com.android.inputmethod.latin.CollectionUtils;
import com.android.inputmethod.latin.ContactsBinaryDictionary;
@@ -64,8 +64,6 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
CollectionUtils.newSynchronizedTreeMap();
private ContactsBinaryDictionary mContactsDictionary;
- // The threshold for a candidate to be offered as a suggestion.
- private float mSuggestionThreshold;
// The threshold for a suggestion to be considered "recommended".
private float mRecommendedThreshold;
// Whether to use the contacts dictionary
@@ -112,8 +110,6 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
@Override public void onCreate() {
super.onCreate();
- mSuggestionThreshold =
- Float.parseFloat(getString(R.string.spellchecker_suggestion_threshold_value));
mRecommendedThreshold =
Float.parseFloat(getString(R.string.spellchecker_recommended_threshold_value));
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@@ -130,6 +126,19 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
return script;
}
+ private static String getKeyboardLayoutNameForScript(final int script) {
+ switch (script) {
+ case AndroidSpellCheckerService.SCRIPT_LATIN:
+ return "qwerty";
+ case AndroidSpellCheckerService.SCRIPT_CYRILLIC:
+ return "east_slavic";
+ case AndroidSpellCheckerService.SCRIPT_GREEK:
+ return "greek";
+ default:
+ throw new RuntimeException("Wrong script supplied: " + script);
+ }
+ }
+
@Override
public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
if (!PREF_USE_CONTACTS_KEY.equals(key)) return;
@@ -198,8 +207,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
}
public SuggestionsGatherer newSuggestionsGatherer(final String text, int maxLength) {
- return new SuggestionsGatherer(
- text, mSuggestionThreshold, mRecommendedThreshold, maxLength);
+ return new SuggestionsGatherer(text, mRecommendedThreshold, maxLength);
}
// TODO: remove this class and replace it by storage local to the session.
@@ -217,7 +225,6 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
private final ArrayList<String> mSuggestions;
private final int[] mScores;
private final String mOriginalText;
- private final float mSuggestionThreshold;
private final float mRecommendedThreshold;
private final int mMaxLength;
private int mLength = 0;
@@ -227,10 +234,9 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
private String mBestSuggestion = null;
private int mBestScore = Integer.MIN_VALUE; // As small as possible
- SuggestionsGatherer(final String originalText, final float suggestionThreshold,
- final float recommendedThreshold, final int maxLength) {
+ SuggestionsGatherer(final String originalText, final float recommendedThreshold,
+ final int maxLength) {
mOriginalText = originalText;
- mSuggestionThreshold = suggestionThreshold;
mRecommendedThreshold = recommendedThreshold;
mMaxLength = maxLength;
mSuggestions = CollectionUtils.newArrayList(maxLength + 1);
@@ -392,9 +398,13 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
return pool;
}
- public DictAndProximity createDictAndProximity(final Locale locale) {
+ public DictAndKeyboard createDictAndKeyboard(final Locale locale) {
final int script = getScriptFromLocale(locale);
- final ProximityInfo proximityInfo = new SpellCheckerProximityInfo(script);
+ final String keyboardLayoutName = getKeyboardLayoutNameForScript(script);
+ final KeyboardLayoutSet keyboardLayoutSet =
+ KeyboardLayoutSet.createKeyboardSetForSpellChecker(this, locale.toString(),
+ keyboardLayoutName);
+
final DictionaryCollection dictionaryCollection =
DictionaryFactory.createMainDictionaryFromManager(this, locale,
true /* useFullEditDistance */);
@@ -419,6 +429,6 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
mDictionaryCollectionsList.add(
new WeakReference<DictionaryCollection>(dictionaryCollection));
}
- return new DictAndProximity(dictionaryCollection, proximityInfo);
+ return new DictAndKeyboard(dictionaryCollection, keyboardLayoutSet);
}
}
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
index da8657201..16e9fb77e 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
@@ -257,7 +257,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
}
if (shouldFilterOut(inText, mScript)) {
- DictAndProximity dictInfo = null;
+ DictAndKeyboard dictInfo = null;
try {
dictInfo = mDictionaryPool.pollWithDefaultTimeout();
if (!DictionaryPool.isAValidDictionary(dictInfo)) {
@@ -283,32 +283,26 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
//suggestionsLimit);
final SuggestionsGatherer suggestionsGatherer = mService.newSuggestionsGatherer(
text, suggestionsLimit);
- final WordComposer composer = new WordComposer();
- final int length = text.length();
- for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) {
- final int codePoint = text.codePointAt(i);
- // The getXYForCodePointAndScript method returns (Y << 16) + X
- final int xy = SpellCheckerProximityInfo.getXYForCodePointAndScript(
- codePoint, mScript);
- if (SpellCheckerProximityInfo.NOT_A_COORDINATE_PAIR == xy) {
- composer.add(codePoint,
- Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE);
- } else {
- composer.add(codePoint, xy & 0xFFFF, xy >> 16);
- }
- }
final int capitalizeType = StringUtils.getCapitalizationType(text);
boolean isInDict = true;
- DictAndProximity dictInfo = null;
+ DictAndKeyboard dictInfo = null;
try {
dictInfo = mDictionaryPool.pollWithDefaultTimeout();
if (!DictionaryPool.isAValidDictionary(dictInfo)) {
return AndroidSpellCheckerService.getNotInDictEmptySuggestions();
}
+ final WordComposer composer = new WordComposer();
+ final int length = text.length();
+ for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) {
+ final int codePoint = text.codePointAt(i);
+ composer.addKeyInfo(codePoint, dictInfo.getKeyboard(codePoint));
+ }
+ // TODO: make a spell checker option to block offensive words or not
final ArrayList<SuggestedWordInfo> suggestions =
dictInfo.mDictionary.getSuggestions(composer, prevWord,
- dictInfo.mProximityInfo);
+ dictInfo.getProximityInfo(),
+ true /* blockOffensiveWords */);
for (final SuggestedWordInfo suggestion : suggestions) {
final String suggestionStr = suggestion.mWord;
suggestionsGatherer.addWord(suggestionStr.toCharArray(), null, 0,
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/DictAndKeyboard.java b/java/src/com/android/inputmethod/latin/spellcheck/DictAndKeyboard.java
new file mode 100644
index 000000000..b77f3e2c5
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/spellcheck/DictAndKeyboard.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2011 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.spellcheck;
+
+import com.android.inputmethod.latin.Dictionary;
+import com.android.inputmethod.keyboard.Keyboard;
+import com.android.inputmethod.keyboard.KeyboardId;
+import com.android.inputmethod.keyboard.KeyboardLayoutSet;
+import com.android.inputmethod.keyboard.ProximityInfo;
+
+/**
+ * A container for a Dictionary and a Keyboard.
+ */
+public final class DictAndKeyboard {
+ public final Dictionary mDictionary;
+ private final Keyboard mKeyboard;
+ private final Keyboard mManualShiftedKeyboard;
+
+ public DictAndKeyboard(
+ final Dictionary dictionary, final KeyboardLayoutSet keyboardLayoutSet) {
+ mDictionary = dictionary;
+ if (keyboardLayoutSet == null) {
+ mKeyboard = null;
+ mManualShiftedKeyboard = null;
+ return;
+ }
+ mKeyboard = keyboardLayoutSet.getKeyboard(KeyboardId.ELEMENT_ALPHABET);
+ mManualShiftedKeyboard =
+ keyboardLayoutSet.getKeyboard(KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED);
+ }
+
+ public Keyboard getKeyboard(final int codePoint) {
+ if (mKeyboard == null) {
+ return null;
+ }
+ return mKeyboard.getKey(codePoint) != null ? mKeyboard : mManualShiftedKeyboard;
+ }
+
+ public ProximityInfo getProximityInfo() {
+ return mKeyboard == null ? null : mKeyboard.getProximityInfo();
+ }
+}
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/DictAndProximity.java b/java/src/com/android/inputmethod/latin/spellcheck/DictAndProximity.java
deleted file mode 100644
index 017a4f555..000000000
--- a/java/src/com/android/inputmethod/latin/spellcheck/DictAndProximity.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (C) 2011 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.spellcheck;
-
-import com.android.inputmethod.latin.Dictionary;
-import com.android.inputmethod.keyboard.ProximityInfo;
-
-/**
- * A simple container for both a Dictionary and a ProximityInfo.
- */
-public final class DictAndProximity {
- public final Dictionary mDictionary;
- public final ProximityInfo mProximityInfo;
- public DictAndProximity(final Dictionary dictionary, final ProximityInfo proximityInfo) {
- mDictionary = dictionary;
- mProximityInfo = proximityInfo;
- }
-}
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java b/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java
index 81dd92d9e..a20e09ee8 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java
@@ -36,7 +36,7 @@ import java.util.concurrent.TimeUnit;
* the client code, but may help with sloppy clients.
*/
@SuppressWarnings("serial")
-public final class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> {
+public final class DictionaryPool extends LinkedBlockingQueue<DictAndKeyboard> {
private final static String TAG = DictionaryPool.class.getSimpleName();
// How many seconds we wait for a dictionary to become available. Past this delay, we give up in
// fear some bug caused a deadlock, and reset the whole pool.
@@ -47,11 +47,12 @@ public final class DictionaryPool extends LinkedBlockingQueue<DictAndProximity>
private int mSize;
private volatile boolean mClosed;
final static ArrayList<SuggestedWordInfo> noSuggestions = CollectionUtils.newArrayList();
- private final static DictAndProximity dummyDict = new DictAndProximity(
+ private final static DictAndKeyboard dummyDict = new DictAndKeyboard(
new Dictionary(Dictionary.TYPE_MAIN) {
@Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
- final String prevWord, final ProximityInfo proximityInfo) {
+ final String prevWord, final ProximityInfo proximityInfo,
+ final boolean blockOffensiveWords) {
return noSuggestions;
}
@Override
@@ -63,7 +64,7 @@ public final class DictionaryPool extends LinkedBlockingQueue<DictAndProximity>
}
}, null);
- static public boolean isAValidDictionary(final DictAndProximity dictInfo) {
+ static public boolean isAValidDictionary(final DictAndKeyboard dictInfo) {
return null != dictInfo && dummyDict != dictInfo;
}
@@ -78,32 +79,32 @@ public final class DictionaryPool extends LinkedBlockingQueue<DictAndProximity>
}
@Override
- public DictAndProximity poll(final long timeout, final TimeUnit unit)
+ public DictAndKeyboard poll(final long timeout, final TimeUnit unit)
throws InterruptedException {
- final DictAndProximity dict = poll();
+ final DictAndKeyboard dict = poll();
if (null != dict) return dict;
synchronized(this) {
if (mSize >= mMaxSize) {
// Our pool is already full. Wait until some dictionary is ready, or TIMEOUT
// expires to avoid a deadlock.
- final DictAndProximity result = super.poll(timeout, unit);
+ final DictAndKeyboard result = super.poll(timeout, unit);
if (null == result) {
Log.e(TAG, "Deadlock detected ! Resetting dictionary pool");
clear();
mSize = 1;
- return mService.createDictAndProximity(mLocale);
+ return mService.createDictAndKeyboard(mLocale);
} else {
return result;
}
} else {
++mSize;
- return mService.createDictAndProximity(mLocale);
+ return mService.createDictAndKeyboard(mLocale);
}
}
}
// Convenience method
- public DictAndProximity pollWithDefaultTimeout() {
+ public DictAndKeyboard pollWithDefaultTimeout() {
try {
return poll(TIMEOUT, TimeUnit.SECONDS);
} catch (InterruptedException e) {
@@ -114,7 +115,7 @@ public final class DictionaryPool extends LinkedBlockingQueue<DictAndProximity>
public void close() {
synchronized(this) {
mClosed = true;
- for (DictAndProximity dict : this) {
+ for (DictAndKeyboard dict : this) {
dict.mDictionary.close();
}
clear();
@@ -122,7 +123,7 @@ public final class DictionaryPool extends LinkedBlockingQueue<DictAndProximity>
}
@Override
- public boolean offer(final DictAndProximity dict) {
+ public boolean offer(final DictAndKeyboard dict) {
if (mClosed) {
dict.mDictionary.close();
return super.offer(dummyDict);
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java b/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java
deleted file mode 100644
index 0c480eaba..000000000
--- a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java
+++ /dev/null
@@ -1,462 +0,0 @@
-/*
- * Copyright (C) 2011 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.spellcheck;
-
-import android.util.SparseIntArray;
-
-import com.android.inputmethod.keyboard.ProximityInfo;
-import com.android.inputmethod.latin.Constants;
-
-public final class SpellCheckerProximityInfo extends ProximityInfo {
- public SpellCheckerProximityInfo(final int script) {
- super(getProximityForScript(script), PROXIMITY_GRID_WIDTH, PROXIMITY_GRID_HEIGHT);
- }
-
- private static final int NUL = Constants.NOT_A_CODE;
-
- // This must be the same as MAX_PROXIMITY_CHARS_SIZE else it will not work inside
- // native code - this value is passed at creation of the binary object and reused
- // as the size of the passed array afterwards so they can't be different.
- private static final int ROW_SIZE = ProximityInfo.MAX_PROXIMITY_CHARS_SIZE;
-
- // The number of keys in a row of the grid used by the spell checker.
- private static final int PROXIMITY_GRID_WIDTH = 11;
- // The number of rows in the grid used by the spell checker.
- private static final int PROXIMITY_GRID_HEIGHT = 3;
-
- private static final int NOT_AN_INDEX = -1;
- public static final int NOT_A_COORDINATE_PAIR = -1;
-
- // Helper methods
- static void buildProximityIndices(final int[] proximity, final int rowSize,
- final SparseIntArray indices) {
- for (int i = 0; i < proximity.length; i += rowSize) {
- if (NUL != proximity[i]) indices.put(proximity[i], i / rowSize);
- }
- }
-
- private static final class Latin {
- // The proximity here is the union of
- // - the proximity for a QWERTY keyboard.
- // - the proximity for an AZERTY keyboard.
- // - the proximity for a QWERTZ keyboard.
- // ...plus, add all characters in the ('a', 'e', 'i', 'o', 'u') set to each other.
- //
- // The reasoning behind this construction is, almost any alphabetic text we may want
- // to spell check has been entered with one of the keyboards above. Also, specifically
- // to English, many spelling errors consist of the last vowel of the word being wrong
- // because in English vowels tend to merge with each other in pronunciation.
- /*
- The Qwerty layout this represents looks like the following:
- q w e r t y u i o p
- a s d f g h j k l
- z x c v b n m
- */
- static final int[] PROXIMITY = {
- // Proximity for row 1. This must have exactly ROW_SIZE entries for each letter,
- // and exactly PROXIMITY_GRID_WIDTH letters for a row. Pad with NUL's.
- // The number of rows must be exactly PROXIMITY_GRID_HEIGHT.
- 'q', 'w', 's', 'a', 'z', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'w', 'q', 'a', 's', 'd', 'e', 'x', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'e', 'w', 's', 'd', 'f', 'r', 'a', 'i', 'o', 'u', NUL, NUL, NUL, NUL, NUL, NUL,
- 'r', 'e', 'd', 'f', 'g', 't', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 't', 'r', 'f', 'g', 'h', 'y', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'y', 't', 'g', 'h', 'j', 'u', 'a', 's', 'd', 'x', NUL, NUL, NUL, NUL, NUL, NUL,
- 'u', 'y', 'h', 'j', 'k', 'i', 'a', 'e', 'o', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'i', 'u', 'j', 'k', 'l', 'o', 'a', 'e', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'o', 'i', 'k', 'l', 'p', 'a', 'e', 'u', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'p', 'o', 'l', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- // Proximity for row 2. See comment above about size.
- 'a', 'z', 'x', 's', 'w', 'q', 'e', 'i', 'o', 'u', NUL, NUL, NUL, NUL, NUL, NUL,
- 's', 'q', 'a', 'z', 'x', 'c', 'd', 'e', 'w', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'd', 'w', 's', 'x', 'c', 'v', 'f', 'r', 'e', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'f', 'e', 'd', 'c', 'v', 'b', 'g', 't', 'r', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'g', 'r', 'f', 'v', 'b', 'n', 'h', 'y', 't', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'h', 't', 'g', 'b', 'n', 'm', 'j', 'u', 'y', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'j', 'y', 'h', 'n', 'm', 'k', 'i', 'u', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'k', 'u', 'j', 'm', 'l', 'o', 'i', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'l', 'i', 'k', 'p', 'o', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- // Proximity for row 3. See comment above about size.
- 'z', 'a', 's', 'd', 'x', 't', 'g', 'h', 'j', 'u', 'q', 'e', NUL, NUL, NUL, NUL,
- 'x', 'z', 'a', 's', 'd', 'c', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'c', 'x', 's', 'd', 'f', 'v', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'v', 'c', 'd', 'f', 'g', 'b', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'b', 'v', 'f', 'g', 'h', 'n', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'n', 'b', 'g', 'h', 'j', 'm', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'm', 'n', 'h', 'j', 'k', 'l', 'o', 'p', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- };
-
- // This is a mapping array from the code point to the index in the PROXIMITY array.
- // When we check the spelling of a word, we need to pass (x,y) coordinates to the native
- // code for each letter of the word. These are most easily computed from the index in the
- // PROXIMITY array. Since we'll need to do that very often, the index lookup from the code
- // point needs to be as fast as possible, and a map is probably the best way to do this.
- // To avoid unnecessary boxing conversion to Integer, here we use SparseIntArray.
- static final SparseIntArray INDICES = new SparseIntArray(PROXIMITY.length / ROW_SIZE);
-
- static {
- buildProximityIndices(PROXIMITY, ROW_SIZE, INDICES);
- }
- }
-
- private static final class Cyrillic {
- // TODO: The following table is solely based on the keyboard layout. Consult with Russian
- // speakers on commonly misspelled words/letters.
- /*
- The Russian layout this represents looks like the following:
- й ц у к е н г ш щ з х
- ф ы в а п р о л д ж э
- я ч с м и т ь б ю
-
- This gives us the following table:
- 'й', 'ц', 'ф', 'ы', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ц', 'й', 'ф', 'ы', 'в', 'у', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'у', 'ц', 'ы', 'в', 'а', 'к', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'к', 'у', 'в', 'а', 'п', 'е', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'е', 'к', 'а', 'п', 'р', 'н', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'н', 'е', 'п', 'р', 'о', 'г', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'г', 'н', 'р', 'о', 'л', 'ш', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ш', 'г', 'о', 'л', 'д', 'щ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'щ', 'ш', 'л', 'д', 'ж', 'з', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'з', 'щ', 'д', 'ж', 'э', 'х', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'х', 'з', 'ж', 'э', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- 'ф', 'й', 'ц', 'ы', 'я', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ы', 'й', 'ц', 'у', 'ф', 'в', 'я', 'ч', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'в', 'ц', 'у', 'к', 'ы', 'а', 'я', 'ч', 'с', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'а', 'у', 'к', 'е', 'в', 'п', 'ч', 'с', 'м', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'п', 'к', 'е', 'н', 'а', 'р', 'с', 'м', 'и', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'р', 'е', 'н', 'г', 'п', 'о', 'м', 'и', 'т', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'о', 'н', 'г', 'ш', 'р', 'л', 'и', 'т', 'ь', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'л', 'г', 'ш', 'щ', 'о', 'д', 'т', 'ь', 'б', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'д', 'ш', 'щ', 'з', 'л', 'ж', 'ь', 'б', 'ю', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ж', 'щ', 'з', 'х', 'д', 'э', 'б', 'ю', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'э', 'з', 'х', 'ю', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- 'я', 'ф', 'ы', 'в', 'ч', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ч', 'ы', 'в', 'а', 'я', 'с', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'с', 'в', 'а', 'п', 'ч', 'м', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'м', 'а', 'п', 'р', 'с', 'и', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'и', 'п', 'р', 'о', 'м', 'т', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'т', 'р', 'о', 'л', 'и', 'ь', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ь', 'о', 'л', 'д', 'т', 'б', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'б', 'л', 'д', 'ж', 'ь', 'ю', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ю', 'д', 'ж', 'э', 'б', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- Using the following characters:
- */
- private static final int CY_SHORT_I = '\u0439'; // й
- private static final int CY_TSE = '\u0446'; // ц
- private static final int CY_U = '\u0443'; // у
- private static final int CY_KA = '\u043A'; // к
- private static final int CY_IE = '\u0435'; // е
- private static final int CY_EN = '\u043D'; // н
- private static final int CY_GHE = '\u0433'; // г
- private static final int CY_SHA = '\u0448'; // ш
- private static final int CY_SHCHA = '\u0449'; // щ
- private static final int CY_ZE = '\u0437'; // з
- private static final int CY_HA = '\u0445'; // х
- private static final int CY_EF = '\u0444'; // ф
- private static final int CY_YERU = '\u044B'; // ы
- private static final int CY_VE = '\u0432'; // в
- private static final int CY_A = '\u0430'; // а
- private static final int CY_PE = '\u043F'; // п
- private static final int CY_ER = '\u0440'; // р
- private static final int CY_O = '\u043E'; // о
- private static final int CY_EL = '\u043B'; // л
- private static final int CY_DE = '\u0434'; // д
- private static final int CY_ZHE = '\u0436'; // ж
- private static final int CY_E = '\u044D'; // э
- private static final int CY_YA = '\u044F'; // я
- private static final int CY_CHE = '\u0447'; // ч
- private static final int CY_ES = '\u0441'; // с
- private static final int CY_EM = '\u043C'; // м
- private static final int CY_I = '\u0438'; // и
- private static final int CY_TE = '\u0442'; // т
- private static final int CY_SOFT_SIGN = '\u044C'; // ь
- private static final int CY_BE = '\u0431'; // б
- private static final int CY_YU = '\u044E'; // ю
- static final int[] PROXIMITY = {
- // Proximity for row 1. This must have exactly ROW_SIZE entries for each letter,
- // and exactly PROXIMITY_GRID_WIDTH letters for a row. Pad with NUL's.
- // The number of rows must be exactly PROXIMITY_GRID_HEIGHT.
- CY_SHORT_I, CY_TSE, CY_EF, CY_YERU, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_TSE, CY_SHORT_I, CY_EF, CY_YERU, CY_VE, CY_U, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_U, CY_TSE, CY_YERU, CY_VE, CY_A, CY_KA, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_KA, CY_U, CY_VE, CY_A, CY_PE, CY_IE, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_IE, CY_KA, CY_A, CY_PE, CY_ER, CY_EN, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_EN, CY_IE, CY_PE, CY_ER, CY_O, CY_GHE, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_GHE, CY_EN, CY_ER, CY_O, CY_EL, CY_SHA, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_SHA, CY_GHE, CY_O, CY_EL, CY_DE, CY_SHCHA, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_SHCHA, CY_SHA, CY_EL, CY_DE, CY_ZHE, CY_ZE, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_ZE, CY_SHCHA, CY_DE, CY_ZHE, CY_E, CY_HA, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_HA, CY_ZE, CY_ZHE, CY_E, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- // Proximity for row 2. See comment above about size.
- CY_EF, CY_SHORT_I, CY_TSE, CY_YERU, CY_YA, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_YERU, CY_SHORT_I, CY_TSE, CY_U, CY_EF, CY_VE, CY_YA, CY_CHE,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_VE, CY_TSE, CY_U, CY_KA, CY_YERU, CY_A, CY_YA, CY_CHE,
- CY_ES, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_A, CY_U, CY_KA, CY_IE, CY_VE, CY_PE, CY_CHE, CY_ES,
- CY_EM, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_PE, CY_KA, CY_IE, CY_EN, CY_A, CY_ER, CY_ES, CY_EM,
- CY_I, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_ER, CY_IE, CY_EN, CY_GHE, CY_PE, CY_O, CY_EM, CY_I,
- CY_TE, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_O, CY_EN, CY_GHE, CY_SHA, CY_ER, CY_EL, CY_I, CY_TE,
- CY_SOFT_SIGN, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_EL, CY_GHE, CY_SHA, CY_SHCHA, CY_O, CY_DE, CY_TE, CY_SOFT_SIGN,
- CY_BE, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_DE, CY_SHA, CY_SHCHA, CY_ZE, CY_EL, CY_ZHE, CY_SOFT_SIGN, CY_BE,
- CY_YU, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_ZHE, CY_SHCHA, CY_ZE, CY_HA, CY_DE, CY_E, CY_BE, CY_YU,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_E, CY_ZE, CY_HA, CY_YU, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- // Proximity for row 3. See comment above about size.
- CY_YA, CY_EF, CY_YERU, CY_VE, CY_CHE, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_CHE, CY_YERU, CY_VE, CY_A, CY_YA, CY_ES, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_ES, CY_VE, CY_A, CY_PE, CY_CHE, CY_EM, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_EM, CY_A, CY_PE, CY_ER, CY_ES, CY_I, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_I, CY_PE, CY_ER, CY_O, CY_EM, CY_TE, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_TE, CY_ER, CY_O, CY_EL, CY_I, CY_SOFT_SIGN, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_SOFT_SIGN, CY_O, CY_EL, CY_DE, CY_TE, CY_BE, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_BE, CY_EL, CY_DE, CY_ZHE, CY_SOFT_SIGN, CY_YU, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- CY_YU, CY_DE, CY_ZHE, CY_E, CY_BE, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- };
-
- static final SparseIntArray INDICES = new SparseIntArray(PROXIMITY.length / ROW_SIZE);
-
- static {
- buildProximityIndices(PROXIMITY, ROW_SIZE, INDICES);
- }
- }
-
- private static final class Greek {
- // TODO: The following table is solely based on the keyboard layout. Consult with Greek
- // speakers on commonly misspelled words/letters.
- /*
- The Greek layout this represents looks like the following:
- ; ς ε ρ τ υ θ ι ο π
- α σ δ φ γ η ξ κ λ
- ζ χ ψ ω β ν μ
-
- This gives us the following table:
- 'ς', 'ε', 'α', 'σ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ε', 'ς', 'ρ', 'σ', 'δ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ρ', 'ε', 'τ', 'δ', 'φ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'τ', 'ρ', 'υ', 'φ', 'γ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'υ', 'τ', 'θ', 'γ', 'η', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'θ', 'υ', 'ι', 'η', 'ξ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ι', 'θ', 'ο', 'ξ', 'κ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ο', 'ι', 'π', 'κ', 'λ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'π', 'ο', 'λ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- 'α', 'ς', 'σ', 'ζ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'σ', 'ς', 'ε', 'α', 'δ', 'ζ', 'χ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'δ', 'ε', 'ρ', 'σ', 'φ', 'ζ', 'χ', 'ψ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'φ', 'ρ', 'τ', 'δ', 'γ', 'χ', 'ψ', 'ω', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'γ', 'τ', 'υ', 'φ', 'η', 'ψ', 'ω', 'β', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'η', 'υ', 'θ', 'γ', 'ξ', 'ω', 'β', 'ν', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ξ', 'θ', 'ι', 'η', 'κ', 'β', 'ν', 'μ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'κ', 'ι', 'ο', 'ξ', 'λ', 'ν', 'μ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'λ', 'ο', 'π', 'κ', 'μ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- 'ζ', 'α', 'σ', 'δ', 'χ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'χ', 'σ', 'δ', 'φ', 'ζ', 'ψ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ψ', 'δ', 'φ', 'γ', 'χ', 'ω', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ω', 'φ', 'γ', 'η', 'ψ', 'β', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'β', 'γ', 'η', 'ξ', 'ω', 'ν', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'ν', 'η', 'ξ', 'κ', 'β', 'μ', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- 'μ', 'ξ', 'κ', 'λ', 'ν', NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- Using the following characters:
- */
- private static final int GR_FINAL_SIGMA = '\u03C2'; // ς
- private static final int GR_EPSILON = '\u03B5'; // ε
- private static final int GR_RHO = '\u03C1'; // ρ
- private static final int GR_TAU = '\u03C4'; // τ
- private static final int GR_UPSILON = '\u03C5'; // υ
- private static final int GR_THETA = '\u03B8'; // θ
- private static final int GR_IOTA = '\u03B9'; // ι
- private static final int GR_OMICRON = '\u03BF'; // ο
- private static final int GR_PI = '\u03C0'; // π
- private static final int GR_ALPHA = '\u03B1'; // α
- private static final int GR_SIGMA = '\u03C3'; // σ
- private static final int GR_DELTA = '\u03B4'; // δ
- private static final int GR_PHI = '\u03C6'; // φ
- private static final int GR_GAMMA = '\u03B3'; // γ
- private static final int GR_ETA = '\u03B7'; // η
- private static final int GR_XI = '\u03BE'; // ξ
- private static final int GR_KAPPA = '\u03BA'; // κ
- private static final int GR_LAMDA = '\u03BB'; // λ
- private static final int GR_ZETA = '\u03B6'; // ζ
- private static final int GR_CHI = '\u03C7'; // χ
- private static final int GR_PSI = '\u03C8'; // ψ
- private static final int GR_OMEGA = '\u03C9'; // ω
- private static final int GR_BETA = '\u03B2'; // β
- private static final int GR_NU = '\u03BD'; // ν
- private static final int GR_MU = '\u03BC'; // μ
- static final int[] PROXIMITY = {
- // Proximity for row 1. This must have exactly ROW_SIZE entries for each letter,
- // and exactly PROXIMITY_GRID_WIDTH letters for a row. Pad with NUL's.
- // The number of rows must be exactly PROXIMITY_GRID_HEIGHT.
- GR_FINAL_SIGMA, GR_EPSILON, GR_ALPHA, GR_SIGMA, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_EPSILON, GR_FINAL_SIGMA, GR_RHO, GR_SIGMA, GR_DELTA, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_RHO, GR_EPSILON, GR_TAU, GR_DELTA, GR_PHI, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_TAU, GR_RHO, GR_UPSILON, GR_PHI, GR_GAMMA, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_UPSILON, GR_TAU, GR_THETA, GR_GAMMA, GR_ETA, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_THETA, GR_UPSILON, GR_IOTA, GR_ETA, GR_XI, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_IOTA, GR_THETA, GR_OMICRON, GR_XI, GR_KAPPA, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_OMICRON, GR_IOTA, GR_PI, GR_KAPPA, GR_LAMDA, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_PI, GR_OMICRON, GR_LAMDA, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- GR_ALPHA, GR_FINAL_SIGMA, GR_SIGMA, GR_ZETA, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_SIGMA, GR_FINAL_SIGMA, GR_EPSILON, GR_ALPHA, GR_DELTA, GR_ZETA, GR_CHI, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_DELTA, GR_EPSILON, GR_RHO, GR_SIGMA, GR_PHI, GR_ZETA, GR_CHI, GR_PSI,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_PHI, GR_RHO, GR_TAU, GR_DELTA, GR_GAMMA, GR_CHI, GR_PSI, GR_OMEGA,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_GAMMA, GR_TAU, GR_UPSILON, GR_PHI, GR_ETA, GR_PSI, GR_OMEGA, GR_BETA,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_ETA, GR_UPSILON, GR_THETA, GR_GAMMA, GR_XI, GR_OMEGA, GR_BETA, GR_NU,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_XI, GR_THETA, GR_IOTA, GR_ETA, GR_KAPPA, GR_BETA, GR_NU, GR_MU,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_KAPPA, GR_IOTA, GR_OMICRON, GR_XI, GR_LAMDA, GR_NU, GR_MU, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_LAMDA, GR_OMICRON, GR_PI, GR_KAPPA, GR_MU, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
-
- GR_ZETA, GR_ALPHA, GR_SIGMA, GR_DELTA, GR_CHI, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_CHI, GR_SIGMA, GR_DELTA, GR_PHI, GR_ZETA, GR_PSI, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_PSI, GR_DELTA, GR_PHI, GR_GAMMA, GR_CHI, GR_OMEGA, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_OMEGA, GR_PHI, GR_GAMMA, GR_ETA, GR_PSI, GR_BETA, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_BETA, GR_GAMMA, GR_ETA, GR_XI, GR_OMEGA, GR_NU, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_NU, GR_ETA, GR_XI, GR_KAPPA, GR_BETA, GR_MU, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- GR_MU, GR_XI, GR_KAPPA, GR_LAMDA, GR_NU, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
- };
-
- static final SparseIntArray INDICES = new SparseIntArray(PROXIMITY.length / ROW_SIZE);
-
- static {
- buildProximityIndices(PROXIMITY, ROW_SIZE, INDICES);
- }
- }
-
- private static int[] getProximityForScript(final int script) {
- switch (script) {
- case AndroidSpellCheckerService.SCRIPT_LATIN:
- return Latin.PROXIMITY;
- case AndroidSpellCheckerService.SCRIPT_CYRILLIC:
- return Cyrillic.PROXIMITY;
- case AndroidSpellCheckerService.SCRIPT_GREEK:
- return Greek.PROXIMITY;
- default:
- throw new RuntimeException("Wrong script supplied: " + script);
- }
- }
-
- private static int getIndexOfCodeForScript(final int codePoint, final int script) {
- switch (script) {
- case AndroidSpellCheckerService.SCRIPT_LATIN:
- return Latin.INDICES.get(codePoint, NOT_AN_INDEX);
- case AndroidSpellCheckerService.SCRIPT_CYRILLIC:
- return Cyrillic.INDICES.get(codePoint, NOT_AN_INDEX);
- case AndroidSpellCheckerService.SCRIPT_GREEK:
- return Greek.INDICES.get(codePoint, NOT_AN_INDEX);
- default:
- throw new RuntimeException("Wrong script supplied: " + script);
- }
- }
-
- // Returns (Y << 16) + X to avoid creating a temporary object. This is okay because
- // X and Y are limited to PROXIMITY_GRID_WIDTH resp. PROXIMITY_GRID_HEIGHT which is very
- // inferior to 1 << 16
- // As an exception, this returns NOT_A_COORDINATE_PAIR if the key is not on the grid
- public static int getXYForCodePointAndScript(final int codePoint, final int script) {
- final int index = getIndexOfCodeForScript(codePoint, script);
- if (NOT_AN_INDEX == index) return NOT_A_COORDINATE_PAIR;
- final int y = index / PROXIMITY_GRID_WIDTH;
- final int x = index % PROXIMITY_GRID_WIDTH;
- if (y > PROXIMITY_GRID_HEIGHT) {
- // Safety check, should be entirely useless
- throw new RuntimeException("Wrong y coordinate in spell checker proximity");
- }
- return (y << 16) + x;
- }
-}
diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java
index 3037669c0..09f81d4c7 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java
@@ -23,19 +23,28 @@ import android.graphics.drawable.Drawable;
import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.Keyboard;
+import com.android.inputmethod.keyboard.KeyboardActionListener;
import com.android.inputmethod.keyboard.TypefaceUtils;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
import com.android.inputmethod.keyboard.internal.KeyboardParams;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SuggestedWords;
+import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.Utils;
public final class MoreSuggestions extends Keyboard {
public static final int SUGGESTION_CODE_BASE = 1024;
- MoreSuggestions(final MoreSuggestionsParam params) {
+ public final SuggestedWords mSuggestedWords;
+
+ public static abstract class MoreSuggestionsListener extends KeyboardActionListener.Adapter {
+ public abstract void onSuggestionSelected(final int index, final SuggestedWordInfo info);
+ }
+
+ MoreSuggestions(final MoreSuggestionsParam params, final SuggestedWords suggestedWords) {
super(params);
+ mSuggestedWords = suggestedWords;
}
private static final class MoreSuggestionsParam extends KeyboardParams {
@@ -52,8 +61,9 @@ public final class MoreSuggestions extends Keyboard {
super();
}
- public int layout(final SuggestedWords suggestions, final int fromPos, final int maxWidth,
- final int minWidth, final int maxRow, final Paint paint, final Resources res) {
+ public int layout(final SuggestedWords suggestedWords, final int fromPos,
+ final int maxWidth, final int minWidth, final int maxRow, final Paint paint,
+ final Resources res) {
clearKeys();
mDivider = res.getDrawable(R.drawable.more_suggestions_divider);
mDividerWidth = mDivider.getIntrinsicWidth();
@@ -61,9 +71,9 @@ public final class MoreSuggestions extends Keyboard {
int row = 0;
int pos = fromPos, rowStartPos = fromPos;
- final int size = Math.min(suggestions.size(), SuggestionStripView.MAX_SUGGESTIONS);
+ final int size = Math.min(suggestedWords.size(), SuggestionStripView.MAX_SUGGESTIONS);
while (pos < size) {
- final String word = suggestions.getWord(pos);
+ final String word = suggestedWords.getWord(pos);
// TODO: Should take care of text x-scaling.
mWidths[pos] = (int)(TypefaceUtils.getLabelWidth(word, paint) + padding);
final int numColumn = pos - rowStartPos + 1;
@@ -163,7 +173,7 @@ public final class MoreSuggestions extends Keyboard {
public static final class Builder extends KeyboardBuilder<MoreSuggestionsParam> {
private final MoreSuggestionsView mPaneView;
- private SuggestedWords mSuggestions;
+ private SuggestedWords mSuggestedWords;
private int mFromPos;
private int mToPos;
@@ -172,7 +182,7 @@ public final class MoreSuggestions extends Keyboard {
mPaneView = paneView;
}
- public Builder layout(final SuggestedWords suggestions, final int fromPos,
+ public Builder layout(final SuggestedWords suggestedWords, final int fromPos,
final int maxWidth, final int minWidth, final int maxRow,
final Keyboard parentKeyboard) {
final int xmlId = R.xml.kbd_suggestions_pane_template;
@@ -180,11 +190,11 @@ public final class MoreSuggestions extends Keyboard {
mParams.mVerticalGap = mParams.mTopPadding = parentKeyboard.mVerticalGap / 2;
mPaneView.updateKeyboardGeometry(mParams.mDefaultRowHeight);
- final int count = mParams.layout(suggestions, fromPos, maxWidth, minWidth, maxRow,
+ final int count = mParams.layout(suggestedWords, fromPos, maxWidth, minWidth, maxRow,
mPaneView.newLabelPaint(null /* key */), mResources);
mFromPos = fromPos;
mToPos = fromPos + count;
- mSuggestions = suggestions;
+ mSuggestedWords = suggestedWords;
return this;
}
@@ -195,8 +205,8 @@ public final class MoreSuggestions extends Keyboard {
final int x = params.getX(pos);
final int y = params.getY(pos);
final int width = params.getWidth(pos);
- final String word = mSuggestions.getWord(pos).toString();
- final String info = Utils.getDebugInfo(mSuggestions, pos);
+ final String word = mSuggestedWords.getWord(pos);
+ final String info = Utils.getDebugInfo(mSuggestedWords, pos);
final int index = pos + SUGGESTION_CODE_BASE;
final Key key = new Key(
params, word, info, KeyboardIconsSet.ICON_UNDEFINED, index, null, x, y,
@@ -211,7 +221,7 @@ public final class MoreSuggestions extends Keyboard {
params.onAddKey(divider);
}
}
- return new MoreSuggestions(params);
+ return new MoreSuggestions(params, mSuggestedWords);
}
}
diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
index 6509f394b..d585b5c7f 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
@@ -18,15 +18,21 @@ package com.android.inputmethod.latin.suggestions;
import android.content.Context;
import android.util.AttributeSet;
+import android.util.Log;
+import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.MoreKeysKeyboardView;
import com.android.inputmethod.latin.R;
+import com.android.inputmethod.latin.SuggestedWords;
+import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionsListener;
/**
* A view that renders a virtual {@link MoreSuggestions}. It handles rendering of keys and detecting
* key presses and touch movements.
*/
public final class MoreSuggestionsView extends MoreKeysKeyboardView {
+ private static final String TAG = MoreSuggestionsView.class.getSimpleName();
+
public MoreSuggestionsView(final Context context, final AttributeSet attrs) {
this(context, attrs, R.attr.moreSuggestionsViewStyle);
}
@@ -54,9 +60,24 @@ public final class MoreSuggestionsView extends MoreKeysKeyboardView {
@Override
public void onCodeInput(final int code, final int x, final int y) {
+ final Keyboard keyboard = getKeyboard();
+ if (!(keyboard instanceof MoreSuggestions)) {
+ Log.e(TAG, "Expected keyboard is MoreSuggestions, but found "
+ + keyboard.getClass().getName());
+ return;
+ }
+ final SuggestedWords suggestedWords = ((MoreSuggestions)keyboard).mSuggestedWords;
final int index = code - MoreSuggestions.SUGGESTION_CODE_BASE;
- if (index >= 0 && index < SuggestionStripView.MAX_SUGGESTIONS) {
- mListener.onCustomRequest(index);
+ if (index < 0 || index >= suggestedWords.size()) {
+ Log.e(TAG, "Selected suggestion has an illegal index: " + index);
+ return;
+ }
+ if (!(mListener instanceof MoreSuggestionsListener)) {
+ Log.e(TAG, "Expected mListener is MoreSuggestionsListener, but found "
+ + mListener.getClass().getName());
+ return;
}
+ ((MoreSuggestionsListener)mListener).onSuggestionSelected(
+ index, suggestedWords.getInfo(index));
}
}
diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
index 2a21ec2f5..ad350a02f 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
@@ -50,7 +50,6 @@ import android.widget.RelativeLayout;
import android.widget.TextView;
import com.android.inputmethod.keyboard.Keyboard;
-import com.android.inputmethod.keyboard.KeyboardActionListener;
import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.MainKeyboardView;
import com.android.inputmethod.keyboard.MoreKeysPanel;
@@ -65,6 +64,7 @@ import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.Utils;
import com.android.inputmethod.latin.define.ProductionFlag;
+import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionsListener;
import com.android.inputmethod.research.ResearchLogger;
import java.util.ArrayList;
@@ -93,7 +93,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
private final ArrayList<View> mDividers = CollectionUtils.newArrayList();
Listener mListener;
- SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
+ private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
private final SuggestionStripViewParams mParams;
private static final float MIN_TEXT_XSCALE = 0.70f;
@@ -652,15 +652,11 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
dismissMoreSuggestions();
}
- private final KeyboardActionListener mMoreSuggestionsListener =
- new KeyboardActionListener.Adapter() {
+ private final MoreSuggestionsListener mMoreSuggestionsListener = new MoreSuggestionsListener() {
@Override
- public boolean onCustomRequest(final int requestCode) {
- final int index = requestCode;
- final SuggestedWordInfo wordInfo = mSuggestedWords.getInfo(index);
+ public void onSuggestionSelected(final int index, final SuggestedWordInfo wordInfo) {
mListener.pickSuggestionManually(index, wordInfo);
dismissMoreSuggestions();
- return true;
}
@Override
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..58c8f266c
--- /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 actionItemAdd = menu.add(0, OPTIONS_MENU_ADD, 0,
+ R.string.user_dict_settings_add_menu_title).setIcon(R.drawable.ic_menu_add);
+ actionItemAdd.setShowAsAction(
+ MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
+ 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);
+ }
+
+ /**
+ * 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..50dda9663
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java
@@ -0,0 +1,334 @@
+/**
+ * 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);
+ getActivity().getActionBar().setTitle(R.string.edit_personal_dictionary);
+ }
+
+ @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();
+ }
+ }
+}