Define stats for UserDictionaryLookup.

Bug 20071513.

Change-Id: Iaab909575da29bfe6e17bb3865ce51d1e7720e7c
main
Dan Zivkovic 2015-04-06 09:19:23 -07:00
parent c79ed10cf7
commit ec2891b007
3 changed files with 89 additions and 4 deletions

View File

@ -31,6 +31,7 @@ public class DictionaryStats {
public final String mDictFileName; public final String mDictFileName;
public final long mDictFileSize; public final long mDictFileSize;
public final int mContentVersion; public final int mContentVersion;
public final int mWordCount;
public DictionaryStats( public DictionaryStats(
@Nonnull final Locale locale, @Nonnull final Locale locale,
@ -43,6 +44,19 @@ public class DictionaryStats {
mDictFileSize = (dictFile == null || !dictFile.exists()) ? 0 : dictFile.length(); mDictFileSize = (dictFile == null || !dictFile.exists()) ? 0 : dictFile.length();
mDictFileName = dictFileName; mDictFileName = dictFileName;
mContentVersion = contentVersion; mContentVersion = contentVersion;
mWordCount = -1;
}
public DictionaryStats(
@Nonnull final Locale locale,
@Nonnull final String dictType,
final int wordCount) {
mLocale = locale;
mDictType = dictType;
mDictFileSize = wordCount;
mDictFileName = null;
mContentVersion = 0;
mWordCount = wordCount;
} }
public String getFileSizeString() { public String getFileSizeString() {
@ -67,9 +81,14 @@ public class DictionaryStats {
builder.append(")"); builder.append(")");
} }
builder.append(": "); builder.append(": ");
builder.append(mDictFileName); if (mWordCount > -1) {
builder.append(" / "); builder.append(mWordCount);
builder.append(getFileSizeString()); builder.append(" words");
} else {
builder.append(mDictFileName);
builder.append(" / ");
builder.append(getFileSizeString());
}
return builder.toString(); return builder.toString();
} }

View File

@ -33,6 +33,7 @@ import com.android.inputmethod.latin.utils.ExecutorUtils;
import java.io.Closeable; import java.io.Closeable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
@ -186,6 +187,8 @@ public class UserDictionaryLookup implements Closeable {
*/ */
private volatile ScheduledFuture<?> mReloadFuture; private volatile ScheduledFuture<?> mReloadFuture;
private volatile List<DictionaryStats> mDictionaryStats;
/** /**
* @param context the context from which to obtain content resolver * @param context the context from which to obtain content resolver
*/ */
@ -195,11 +198,18 @@ public class UserDictionaryLookup implements Closeable {
Log.i(mTag, "create()"); Log.i(mTag, "create()");
mServiceName = serviceName; mServiceName = serviceName;
mDictionaryStats = new ArrayList<DictionaryStats>();
mDictionaryStats.add(new DictionaryStats(ANY_LOCALE, Dictionary.TYPE_USER, 0));
mDictionaryStats.add(new DictionaryStats(ANY_LOCALE, Dictionary.TYPE_USER_SHORTCUT, 0));
// Obtain a content resolver. // Obtain a content resolver.
mResolver = context.getContentResolver(); mResolver = context.getContentResolver();
} }
public List<DictionaryStats> getDictionaryStats() {
return mDictionaryStats;
}
public void open() { public void open() {
Log.i(mTag, "open()"); Log.i(mTag, "open()");
@ -506,6 +516,15 @@ public class UserDictionaryLookup implements Closeable {
} }
} }
List<DictionaryStats> stats = new ArrayList<>();
stats.add(new DictionaryStats(ANY_LOCALE, Dictionary.TYPE_USER, dictWords.size()));
int numShortcuts = 0;
for (HashMap<String, String> shortcuts : shortcutsPerLocale.values()) {
numShortcuts += shortcuts.size();
}
stats.add(new DictionaryStats(ANY_LOCALE, Dictionary.TYPE_USER_SHORTCUT, numShortcuts));
mDictionaryStats = stats;
// Atomically replace the copy of mDictWords and mShortcuts. // Atomically replace the copy of mDictWords and mShortcuts.
mDictWords = dictWords; mDictWords = dictWords;
mShortcutsPerLocale = shortcutsPerLocale; mShortcutsPerLocale = shortcutsPerLocale;
@ -513,6 +532,7 @@ public class UserDictionaryLookup implements Closeable {
// Allow other calls to loadUserDictionary to execute now. // Allow other calls to loadUserDictionary to execute now.
mIsLoading.set(false); mIsLoading.set(false);
Log.i(mTag, "loadUserDictionary() : Loaded " + mDictWords.size() + " words"); Log.i(mTag, "loadUserDictionary() : Loaded " + mDictWords.size()
+ " words and " + numShortcuts + " shortcuts");
} }
} }

View File

@ -309,6 +309,52 @@ public class UserDictionaryLookupTest extends AndroidTestCase {
lookup.close(); lookup.close();
} }
public void testDictionaryStats() {
Log.d(TAG, "testDictionaryStats");
// Insert "foo" and "bar". Only "foo" has a shortcut.
Uri uri = addWord("foo", Locale.GERMANY, 17, "f");
addWord("bar", Locale.GERMANY, 17, null);
// Create the UserDictionaryLookup and wait until it's loaded.
UserDictionaryLookup lookup = new UserDictionaryLookup(mContext, ExecutorUtils.SPELLING);
lookup.open();
while (!lookup.isLoaded()) {
}
// "foo" should match.
assertTrue(lookup.isValidWord("foo", Locale.GERMANY));
// "bar" should match.
assertTrue(lookup.isValidWord("bar", Locale.GERMANY));
// "foo" should have a shortcut.
assertEquals("foo", lookup.expandShortcut("f", Locale.GERMANY));
// Now delete "foo".
deleteWord(uri);
// Wait a little bit before expecting a change. The time we wait should be greater than
// UserDictionaryLookup.RELOAD_DELAY_MS.
try {
Thread.sleep(UserDictionaryLookup.RELOAD_DELAY_MS + 1000);
} catch (InterruptedException e) {
}
// Perform lookups again. Reload should have occured.
//
// "foo" should not match.
assertFalse(lookup.isValidWord("foo", Locale.GERMANY));
// "foo" should not have a shortcut.
assertNull(lookup.expandShortcut("f", Locale.GERMANY));
// "bar" should still match.
assertTrue(lookup.isValidWord("bar", Locale.GERMANY));
lookup.close();
}
public void testClose() { public void testClose() {
Log.d(TAG, "testClose"); Log.d(TAG, "testClose");