2010-08-20 05:35:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 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;
|
|
|
|
|
2011-03-23 21:01:05 +00:00
|
|
|
import com.android.inputmethod.compat.InputMethodInfoCompatWrapper;
|
2011-03-18 05:49:06 +00:00
|
|
|
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
|
2011-06-07 11:44:46 +00:00
|
|
|
import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper;
|
2011-03-25 06:28:29 +00:00
|
|
|
import com.android.inputmethod.compat.InputTypeCompatUtils;
|
2011-04-20 08:33:47 +00:00
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
2011-02-20 03:54:14 +00:00
|
|
|
import com.android.inputmethod.keyboard.KeyboardId;
|
|
|
|
|
2011-03-29 01:45:49 +00:00
|
|
|
import android.content.Context;
|
2011-04-27 06:43:03 +00:00
|
|
|
import android.content.res.Configuration;
|
2011-02-23 08:23:29 +00:00
|
|
|
import android.content.res.Resources;
|
2010-11-16 06:52:34 +00:00
|
|
|
import android.inputmethodservice.InputMethodService;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.os.AsyncTask;
|
2010-11-18 09:37:46 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.HandlerThread;
|
|
|
|
import android.os.Process;
|
2011-02-20 03:54:14 +00:00
|
|
|
import android.text.InputType;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.text.format.DateUtils;
|
|
|
|
import android.util.Log;
|
2011-02-20 03:54:14 +00:00
|
|
|
import android.view.inputmethod.EditorInfo;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2010-11-16 06:52:34 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.text.SimpleDateFormat;
|
2011-06-07 11:44:46 +00:00
|
|
|
import java.util.ArrayList;
|
2010-11-16 06:52:34 +00:00
|
|
|
import java.util.Date;
|
2011-05-17 08:34:17 +00:00
|
|
|
import java.util.HashMap;
|
2011-06-07 11:44:46 +00:00
|
|
|
import java.util.List;
|
2011-04-27 06:43:03 +00:00
|
|
|
import java.util.Locale;
|
2010-11-16 06:52:34 +00:00
|
|
|
|
2010-12-09 12:06:26 +00:00
|
|
|
public class Utils {
|
2011-01-18 08:22:01 +00:00
|
|
|
private static final String TAG = Utils.class.getSimpleName();
|
2011-01-26 16:43:06 +00:00
|
|
|
private static final int MINIMUM_SAFETY_NET_CHAR_LENGTH = 4;
|
2011-01-18 08:22:01 +00:00
|
|
|
private static boolean DBG = LatinImeLogger.sDBG;
|
2011-04-26 05:50:54 +00:00
|
|
|
private static boolean DBG_EDIT_DISTANCE = false;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2011-02-20 13:04:50 +00:00
|
|
|
private Utils() {
|
|
|
|
// Intentional empty constructor for utility class.
|
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
/**
|
|
|
|
* Cancel an {@link AsyncTask}.
|
|
|
|
*
|
|
|
|
* @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
|
|
|
|
* task should be interrupted; otherwise, in-progress tasks are allowed
|
|
|
|
* to complete.
|
|
|
|
*/
|
|
|
|
public static void cancelTask(AsyncTask<?, ?, ?> task, boolean mayInterruptIfRunning) {
|
|
|
|
if (task != null && task.getStatus() != AsyncTask.Status.FINISHED) {
|
|
|
|
task.cancel(mayInterruptIfRunning);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class GCUtils {
|
2011-02-20 03:54:14 +00:00
|
|
|
private static final String GC_TAG = GCUtils.class.getSimpleName();
|
2010-08-20 05:35:02 +00:00
|
|
|
public static final int GC_TRY_COUNT = 2;
|
|
|
|
// GC_TRY_LOOP_MAX is used for the hard limit of GC wait,
|
|
|
|
// GC_TRY_LOOP_MAX should be greater than GC_TRY_COUNT.
|
|
|
|
public static final int GC_TRY_LOOP_MAX = 5;
|
|
|
|
private static final long GC_INTERVAL = DateUtils.SECOND_IN_MILLIS;
|
|
|
|
private static GCUtils sInstance = new GCUtils();
|
|
|
|
private int mGCTryCount = 0;
|
|
|
|
|
|
|
|
public static GCUtils getInstance() {
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reset() {
|
|
|
|
mGCTryCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean tryGCOrWait(String metaData, Throwable t) {
|
|
|
|
if (mGCTryCount == 0) {
|
|
|
|
System.gc();
|
|
|
|
}
|
|
|
|
if (++mGCTryCount > GC_TRY_COUNT) {
|
|
|
|
LatinImeLogger.logOnException(metaData, t);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
Thread.sleep(GC_INTERVAL);
|
|
|
|
return true;
|
|
|
|
} catch (InterruptedException e) {
|
2011-02-20 03:54:14 +00:00
|
|
|
Log.e(GC_TAG, "Sleep was interrupted.");
|
2010-08-20 05:35:02 +00:00
|
|
|
LatinImeLogger.logOnException(metaData, t);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-15 04:37:52 +00:00
|
|
|
|
2011-03-18 05:49:06 +00:00
|
|
|
public static boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManagerCompatWrapper imm) {
|
2011-06-07 11:44:46 +00:00
|
|
|
final List<InputMethodInfoCompatWrapper> enabledImis = imm.getEnabledInputMethodList();
|
|
|
|
|
|
|
|
// Filters out IMEs that have auxiliary subtypes only (including either implicitly or
|
|
|
|
// explicitly enabled ones).
|
|
|
|
final ArrayList<InputMethodInfoCompatWrapper> filteredImis =
|
|
|
|
new ArrayList<InputMethodInfoCompatWrapper>();
|
|
|
|
|
|
|
|
outerloop:
|
|
|
|
for (InputMethodInfoCompatWrapper imi : enabledImis) {
|
|
|
|
// We can return true immediately after we find two or more filtered IMEs.
|
|
|
|
if (filteredImis.size() > 1) return true;
|
|
|
|
final List<InputMethodSubtypeCompatWrapper> subtypes =
|
|
|
|
imm.getEnabledInputMethodSubtypeList(imi, true);
|
|
|
|
// IMEs that have no subtypes should be included.
|
|
|
|
if (subtypes.isEmpty()) {
|
|
|
|
filteredImis.add(imi);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// IMEs that have one or more non-auxiliary subtypes should be included.
|
|
|
|
for (InputMethodSubtypeCompatWrapper subtype : subtypes) {
|
|
|
|
if (!subtype.isAuxiliary()) {
|
|
|
|
filteredImis.add(imi);
|
|
|
|
continue outerloop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredImis.size() > 1
|
2010-12-20 14:51:17 +00:00
|
|
|
// imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled
|
|
|
|
// input method subtype (The current IME should be LatinIME.)
|
|
|
|
|| imm.getEnabledInputMethodSubtypeList(null, false).size() > 1;
|
2010-09-15 04:37:52 +00:00
|
|
|
}
|
2010-09-24 06:46:06 +00:00
|
|
|
|
2011-03-18 05:49:06 +00:00
|
|
|
public static String getInputMethodId(InputMethodManagerCompatWrapper imm, String packageName) {
|
2011-03-29 01:45:49 +00:00
|
|
|
return getInputMethodInfo(imm, packageName).getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static InputMethodInfoCompatWrapper getInputMethodInfo(
|
|
|
|
InputMethodManagerCompatWrapper imm, String packageName) {
|
2011-03-23 21:01:05 +00:00
|
|
|
for (final InputMethodInfoCompatWrapper imi : imm.getEnabledInputMethodList()) {
|
2011-01-20 13:52:02 +00:00
|
|
|
if (imi.getPackageName().equals(packageName))
|
2011-03-29 01:45:49 +00:00
|
|
|
return imi;
|
2011-01-20 13:52:02 +00:00
|
|
|
}
|
|
|
|
throw new RuntimeException("Can not find input method id for " + packageName);
|
|
|
|
}
|
2011-01-18 08:22:01 +00:00
|
|
|
|
2011-01-26 16:43:06 +00:00
|
|
|
public static boolean shouldBlockedBySafetyNetForAutoCorrection(SuggestedWords suggestions,
|
|
|
|
Suggest suggest) {
|
2011-01-18 08:22:01 +00:00
|
|
|
// Safety net for auto correction.
|
|
|
|
// Actually if we hit this safety net, it's actually a bug.
|
|
|
|
if (suggestions.size() <= 1 || suggestions.mTypedWordValid) return false;
|
2011-01-26 16:43:06 +00:00
|
|
|
// If user selected aggressive auto correction mode, there is no need to use the safety
|
|
|
|
// net.
|
|
|
|
if (suggest.isAggressiveAutoCorrectionMode()) return false;
|
2011-01-18 08:22:01 +00:00
|
|
|
CharSequence typedWord = suggestions.getWord(0);
|
2011-01-26 16:43:06 +00:00
|
|
|
// If the length of typed word is less than MINIMUM_SAFETY_NET_CHAR_LENGTH,
|
|
|
|
// we should not use net because relatively edit distance can be big.
|
|
|
|
if (typedWord.length() < MINIMUM_SAFETY_NET_CHAR_LENGTH) return false;
|
2011-01-18 08:22:01 +00:00
|
|
|
CharSequence candidateWord = suggestions.getWord(1);
|
|
|
|
final int typedWordLength = typedWord.length();
|
|
|
|
final int maxEditDistanceOfNativeDictionary = typedWordLength < 5 ? 2 : typedWordLength / 2;
|
|
|
|
final int distance = Utils.editDistance(typedWord, candidateWord);
|
|
|
|
if (DBG) {
|
|
|
|
Log.d(TAG, "Autocorrected edit distance = " + distance
|
|
|
|
+ ", " + maxEditDistanceOfNativeDictionary);
|
|
|
|
}
|
|
|
|
if (distance > maxEditDistanceOfNativeDictionary) {
|
2011-01-28 03:40:27 +00:00
|
|
|
if (DBG) {
|
|
|
|
Log.d(TAG, "Safety net: before = " + typedWord + ", after = " + candidateWord);
|
|
|
|
Log.w(TAG, "(Error) The edit distance of this correction exceeds limit. "
|
|
|
|
+ "Turning off auto-correction.");
|
|
|
|
}
|
2011-01-18 08:22:01 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-24 06:46:06 +00:00
|
|
|
/* package */ static class RingCharBuffer {
|
|
|
|
private static RingCharBuffer sRingCharBuffer = new RingCharBuffer();
|
|
|
|
private static final char PLACEHOLDER_DELIMITER_CHAR = '\uFFFC';
|
|
|
|
private static final int INVALID_COORDINATE = -2;
|
|
|
|
/* package */ static final int BUFSIZE = 20;
|
2010-11-16 06:52:34 +00:00
|
|
|
private InputMethodService mContext;
|
2010-09-24 06:46:06 +00:00
|
|
|
private boolean mEnabled = false;
|
2010-11-16 06:52:34 +00:00
|
|
|
private boolean mUsabilityStudy = false;
|
2010-09-24 06:46:06 +00:00
|
|
|
private int mEnd = 0;
|
|
|
|
/* package */ int mLength = 0;
|
|
|
|
private char[] mCharBuf = new char[BUFSIZE];
|
|
|
|
private int[] mXBuf = new int[BUFSIZE];
|
|
|
|
private int[] mYBuf = new int[BUFSIZE];
|
|
|
|
|
|
|
|
private RingCharBuffer() {
|
2010-12-10 06:24:28 +00:00
|
|
|
// Intentional empty constructor for singleton.
|
2010-09-24 06:46:06 +00:00
|
|
|
}
|
|
|
|
public static RingCharBuffer getInstance() {
|
|
|
|
return sRingCharBuffer;
|
|
|
|
}
|
2010-11-16 06:52:34 +00:00
|
|
|
public static RingCharBuffer init(InputMethodService context, boolean enabled,
|
|
|
|
boolean usabilityStudy) {
|
2010-09-24 06:46:06 +00:00
|
|
|
sRingCharBuffer.mContext = context;
|
2010-11-16 06:52:34 +00:00
|
|
|
sRingCharBuffer.mEnabled = enabled || usabilityStudy;
|
|
|
|
sRingCharBuffer.mUsabilityStudy = usabilityStudy;
|
|
|
|
UsabilityStudyLogUtils.getInstance().init(context);
|
2010-09-24 06:46:06 +00:00
|
|
|
return sRingCharBuffer;
|
|
|
|
}
|
|
|
|
private int normalize(int in) {
|
|
|
|
int ret = in % BUFSIZE;
|
|
|
|
return ret < 0 ? ret + BUFSIZE : ret;
|
|
|
|
}
|
|
|
|
public void push(char c, int x, int y) {
|
|
|
|
if (!mEnabled) return;
|
2010-11-16 06:52:34 +00:00
|
|
|
if (mUsabilityStudy) {
|
|
|
|
UsabilityStudyLogUtils.getInstance().writeChar(c, x, y);
|
|
|
|
}
|
2010-09-24 06:46:06 +00:00
|
|
|
mCharBuf[mEnd] = c;
|
|
|
|
mXBuf[mEnd] = x;
|
|
|
|
mYBuf[mEnd] = y;
|
|
|
|
mEnd = normalize(mEnd + 1);
|
|
|
|
if (mLength < BUFSIZE) {
|
|
|
|
++mLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public char pop() {
|
|
|
|
if (mLength < 1) {
|
|
|
|
return PLACEHOLDER_DELIMITER_CHAR;
|
|
|
|
} else {
|
|
|
|
mEnd = normalize(mEnd - 1);
|
|
|
|
--mLength;
|
|
|
|
return mCharBuf[mEnd];
|
|
|
|
}
|
|
|
|
}
|
2011-04-06 02:14:20 +00:00
|
|
|
public char getBackwardNthChar(int n) {
|
|
|
|
if (mLength <= n || n < 0) {
|
2010-09-24 06:46:06 +00:00
|
|
|
return PLACEHOLDER_DELIMITER_CHAR;
|
|
|
|
} else {
|
2011-04-06 02:14:20 +00:00
|
|
|
return mCharBuf[normalize(mEnd - n - 1)];
|
2010-09-24 06:46:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public int getPreviousX(char c, int back) {
|
|
|
|
int index = normalize(mEnd - 2 - back);
|
|
|
|
if (mLength <= back
|
|
|
|
|| Character.toLowerCase(c) != Character.toLowerCase(mCharBuf[index])) {
|
|
|
|
return INVALID_COORDINATE;
|
|
|
|
} else {
|
|
|
|
return mXBuf[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public int getPreviousY(char c, int back) {
|
|
|
|
int index = normalize(mEnd - 2 - back);
|
|
|
|
if (mLength <= back
|
|
|
|
|| Character.toLowerCase(c) != Character.toLowerCase(mCharBuf[index])) {
|
|
|
|
return INVALID_COORDINATE;
|
|
|
|
} else {
|
|
|
|
return mYBuf[index];
|
|
|
|
}
|
|
|
|
}
|
2011-04-06 02:14:20 +00:00
|
|
|
public String getLastWord(int ignoreCharCount) {
|
2010-11-18 09:37:46 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2011-04-06 02:14:20 +00:00
|
|
|
int i = ignoreCharCount;
|
|
|
|
for (; i < mLength; ++i) {
|
|
|
|
char c = mCharBuf[normalize(mEnd - 1 - i)];
|
|
|
|
if (!((LatinIME)mContext).isWordSeparator(c)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (; i < mLength; ++i) {
|
2010-09-24 06:46:06 +00:00
|
|
|
char c = mCharBuf[normalize(mEnd - 1 - i)];
|
|
|
|
if (!((LatinIME)mContext).isWordSeparator(c)) {
|
|
|
|
sb.append(c);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.reverse().toString();
|
|
|
|
}
|
|
|
|
public void reset() {
|
|
|
|
mLength = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-09-28 03:23:26 +00:00
|
|
|
|
2011-04-20 05:52:11 +00:00
|
|
|
|
|
|
|
/* Damerau-Levenshtein distance */
|
2010-09-28 03:23:26 +00:00
|
|
|
public static int editDistance(CharSequence s, CharSequence t) {
|
|
|
|
if (s == null || t == null) {
|
|
|
|
throw new IllegalArgumentException("editDistance: Arguments should not be null.");
|
|
|
|
}
|
|
|
|
final int sl = s.length();
|
|
|
|
final int tl = t.length();
|
|
|
|
int[][] dp = new int [sl + 1][tl + 1];
|
|
|
|
for (int i = 0; i <= sl; i++) {
|
|
|
|
dp[i][0] = i;
|
|
|
|
}
|
|
|
|
for (int j = 0; j <= tl; j++) {
|
|
|
|
dp[0][j] = j;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < sl; ++i) {
|
|
|
|
for (int j = 0; j < tl; ++j) {
|
2011-04-20 05:52:11 +00:00
|
|
|
final char sc = Character.toLowerCase(s.charAt(i));
|
|
|
|
final char tc = Character.toLowerCase(t.charAt(j));
|
|
|
|
final int cost = sc == tc ? 0 : 1;
|
|
|
|
dp[i + 1][j + 1] = Math.min(
|
|
|
|
dp[i][j + 1] + 1, Math.min(dp[i + 1][j] + 1, dp[i][j] + cost));
|
|
|
|
// Overwrite for transposition cases
|
|
|
|
if (i > 0 && j > 0
|
|
|
|
&& sc == Character.toLowerCase(t.charAt(j - 1))
|
|
|
|
&& tc == Character.toLowerCase(s.charAt(i - 1))) {
|
|
|
|
dp[i + 1][j + 1] = Math.min(dp[i + 1][j + 1], dp[i - 1][j - 1] + cost);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-26 05:50:54 +00:00
|
|
|
if (DBG_EDIT_DISTANCE) {
|
2011-04-20 05:52:11 +00:00
|
|
|
Log.d(TAG, "editDistance:" + s + "," + t);
|
|
|
|
for (int i = 0; i < dp.length; ++i) {
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
for (int j = 0; j < dp[i].length; ++j) {
|
|
|
|
sb.append(dp[i][j]).append(',');
|
2010-09-28 03:23:26 +00:00
|
|
|
}
|
2011-04-20 05:52:11 +00:00
|
|
|
Log.d(TAG, i + ":" + sb.toString());
|
2010-09-28 03:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return dp[sl][tl];
|
|
|
|
}
|
|
|
|
|
2011-02-10 07:28:42 +00:00
|
|
|
// Get the current stack trace
|
|
|
|
public static String getStackTrace() {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
try {
|
|
|
|
throw new RuntimeException();
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
StackTraceElement[] frames = e.getStackTrace();
|
|
|
|
// Start at 1 because the first frame is here and we don't care about it
|
|
|
|
for (int j = 1; j < frames.length; ++j) sb.append(frames[j].toString() + "\n");
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
2010-09-28 03:23:26 +00:00
|
|
|
// In dictionary.cpp, getSuggestion() method,
|
|
|
|
// suggestion scores are computed using the below formula.
|
2011-03-15 18:46:32 +00:00
|
|
|
// original score
|
2010-09-28 03:23:26 +00:00
|
|
|
// := pow(mTypedLetterMultiplier (this is defined 2),
|
|
|
|
// (the number of matched characters between typed word and suggested word))
|
|
|
|
// * (individual word's score which defined in the unigram dictionary,
|
|
|
|
// and this score is defined in range [0, 255].)
|
2011-02-18 08:50:58 +00:00
|
|
|
// Then, the following processing is applied.
|
|
|
|
// - If the dictionary word is matched up to the point of the user entry
|
|
|
|
// (full match up to min(before.length(), after.length())
|
|
|
|
// => Then multiply by FULL_MATCHED_WORDS_PROMOTION_RATE (this is defined 1.2)
|
|
|
|
// - If the word is a true full match except for differences in accents or
|
2011-03-15 18:46:32 +00:00
|
|
|
// capitalization, then treat it as if the score was 255.
|
2011-02-18 08:50:58 +00:00
|
|
|
// - If before.length() == after.length()
|
|
|
|
// => multiply by mFullWordMultiplier (this is defined 2))
|
|
|
|
// So, maximum original score is pow(2, min(before.length(), after.length())) * 255 * 2 * 1.2
|
|
|
|
// For historical reasons we ignore the 1.2 modifier (because the measure for a good
|
|
|
|
// autocorrection threshold was done at a time when it didn't exist). This doesn't change
|
|
|
|
// the result.
|
|
|
|
// So, we can normalize original score by dividing pow(2, min(b.l(),a.l())) * 255 * 2.
|
2010-09-28 03:23:26 +00:00
|
|
|
private static final int MAX_INITIAL_SCORE = 255;
|
|
|
|
private static final int TYPED_LETTER_MULTIPLIER = 2;
|
2011-02-18 08:50:58 +00:00
|
|
|
private static final int FULL_WORD_MULTIPLIER = 2;
|
2011-04-26 05:50:54 +00:00
|
|
|
private static final int S_INT_MAX = 2147483647;
|
2010-09-28 03:23:26 +00:00
|
|
|
public static double calcNormalizedScore(CharSequence before, CharSequence after, int score) {
|
|
|
|
final int beforeLength = before.length();
|
|
|
|
final int afterLength = after.length();
|
2011-01-26 18:23:39 +00:00
|
|
|
if (beforeLength == 0 || afterLength == 0) return 0;
|
2010-09-28 03:23:26 +00:00
|
|
|
final int distance = editDistance(before, after);
|
2011-01-26 18:23:39 +00:00
|
|
|
// If afterLength < beforeLength, the algorithm is suggesting a word by excessive character
|
|
|
|
// correction.
|
2011-04-20 08:33:47 +00:00
|
|
|
int spaceCount = 0;
|
|
|
|
for (int i = 0; i < afterLength; ++i) {
|
|
|
|
if (after.charAt(i) == Keyboard.CODE_SPACE) {
|
|
|
|
++spaceCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (spaceCount == afterLength) return 0;
|
2011-04-26 05:50:54 +00:00
|
|
|
final double maximumScore = score == S_INT_MAX ? S_INT_MAX : MAX_INITIAL_SCORE
|
2011-04-20 08:33:47 +00:00
|
|
|
* Math.pow(
|
|
|
|
TYPED_LETTER_MULTIPLIER, Math.min(beforeLength, afterLength - spaceCount))
|
2011-02-18 08:50:58 +00:00
|
|
|
* FULL_WORD_MULTIPLIER;
|
2010-09-28 03:23:26 +00:00
|
|
|
// add a weight based on edit distance.
|
|
|
|
// distance <= max(afterLength, beforeLength) == afterLength,
|
|
|
|
// so, 0 <= distance / afterLength <= 1
|
|
|
|
final double weight = 1.0 - (double) distance / afterLength;
|
|
|
|
return (score / maximumScore) * weight;
|
|
|
|
}
|
2010-11-16 06:52:34 +00:00
|
|
|
|
|
|
|
public static class UsabilityStudyLogUtils {
|
2011-02-20 13:04:50 +00:00
|
|
|
private static final String USABILITY_TAG = UsabilityStudyLogUtils.class.getSimpleName();
|
2010-11-16 06:52:34 +00:00
|
|
|
private static final String FILENAME = "log.txt";
|
|
|
|
private static final UsabilityStudyLogUtils sInstance =
|
|
|
|
new UsabilityStudyLogUtils();
|
2010-11-18 09:37:46 +00:00
|
|
|
private final Handler mLoggingHandler;
|
2010-11-16 06:52:34 +00:00
|
|
|
private File mFile;
|
|
|
|
private File mDirectory;
|
|
|
|
private InputMethodService mIms;
|
|
|
|
private PrintWriter mWriter;
|
2010-11-18 09:37:46 +00:00
|
|
|
private final Date mDate;
|
|
|
|
private final SimpleDateFormat mDateFormat;
|
2010-11-16 06:52:34 +00:00
|
|
|
|
|
|
|
private UsabilityStudyLogUtils() {
|
|
|
|
mDate = new Date();
|
|
|
|
mDateFormat = new SimpleDateFormat("dd MMM HH:mm:ss.SSS");
|
2010-11-18 09:37:46 +00:00
|
|
|
|
|
|
|
HandlerThread handlerThread = new HandlerThread("UsabilityStudyLogUtils logging task",
|
|
|
|
Process.THREAD_PRIORITY_BACKGROUND);
|
|
|
|
handlerThread.start();
|
|
|
|
mLoggingHandler = new Handler(handlerThread.getLooper());
|
2010-11-16 06:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static UsabilityStudyLogUtils getInstance() {
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void init(InputMethodService ims) {
|
|
|
|
mIms = ims;
|
|
|
|
mDirectory = ims.getFilesDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createLogFileIfNotExist() {
|
|
|
|
if ((mFile == null || !mFile.exists())
|
|
|
|
&& (mDirectory != null && mDirectory.exists())) {
|
|
|
|
try {
|
|
|
|
mWriter = getPrintWriter(mDirectory, FILENAME, false);
|
|
|
|
} catch (IOException e) {
|
2011-02-20 13:04:50 +00:00
|
|
|
Log.e(USABILITY_TAG, "Can't create log file.");
|
2010-11-16 06:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-16 08:32:11 +00:00
|
|
|
public void writeBackSpace() {
|
|
|
|
UsabilityStudyLogUtils.getInstance().write("<backspace>\t0\t0");
|
|
|
|
}
|
|
|
|
|
2010-11-16 06:52:34 +00:00
|
|
|
public void writeChar(char c, int x, int y) {
|
|
|
|
String inputChar = String.valueOf(c);
|
|
|
|
switch (c) {
|
|
|
|
case '\n':
|
|
|
|
inputChar = "<enter>";
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
inputChar = "<tab>";
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
inputChar = "<space>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
UsabilityStudyLogUtils.getInstance().write(inputChar + "\t" + x + "\t" + y);
|
2010-12-28 10:49:43 +00:00
|
|
|
LatinImeLogger.onPrintAllUsabilityStudyLogs();
|
2010-11-16 06:52:34 +00:00
|
|
|
}
|
|
|
|
|
2010-11-18 09:37:46 +00:00
|
|
|
public void write(final String log) {
|
|
|
|
mLoggingHandler.post(new Runnable() {
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2010-11-18 09:37:46 +00:00
|
|
|
public void run() {
|
|
|
|
createLogFileIfNotExist();
|
|
|
|
final long currentTime = System.currentTimeMillis();
|
|
|
|
mDate.setTime(currentTime);
|
2010-11-16 06:52:34 +00:00
|
|
|
|
2010-11-18 09:37:46 +00:00
|
|
|
final String printString = String.format("%s\t%d\t%s\n",
|
|
|
|
mDateFormat.format(mDate), currentTime, log);
|
|
|
|
if (LatinImeLogger.sDBG) {
|
2011-02-20 13:04:50 +00:00
|
|
|
Log.d(USABILITY_TAG, "Write: " + log);
|
2010-11-18 09:37:46 +00:00
|
|
|
}
|
|
|
|
mWriter.print(printString);
|
|
|
|
}
|
|
|
|
});
|
2010-11-16 06:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void printAll() {
|
2010-11-18 09:37:46 +00:00
|
|
|
mLoggingHandler.post(new Runnable() {
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2010-11-18 09:37:46 +00:00
|
|
|
public void run() {
|
|
|
|
mWriter.flush();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
BufferedReader br = getBufferedReader();
|
|
|
|
String line;
|
|
|
|
try {
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
sb.append('\n');
|
|
|
|
sb.append(line);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2011-02-20 13:04:50 +00:00
|
|
|
Log.e(USABILITY_TAG, "Can't read log file.");
|
2010-11-18 09:37:46 +00:00
|
|
|
} finally {
|
|
|
|
if (LatinImeLogger.sDBG) {
|
2011-02-20 13:04:50 +00:00
|
|
|
Log.d(USABILITY_TAG, "output all logs\n" + sb.toString());
|
2010-11-18 09:37:46 +00:00
|
|
|
}
|
|
|
|
mIms.getCurrentInputConnection().commitText(sb.toString(), 0);
|
|
|
|
try {
|
|
|
|
br.close();
|
|
|
|
} catch (IOException e) {
|
2010-12-10 06:24:28 +00:00
|
|
|
// ignore.
|
2010-11-18 09:37:46 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-16 06:52:34 +00:00
|
|
|
}
|
2010-11-18 09:37:46 +00:00
|
|
|
});
|
2010-11-16 06:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clearAll() {
|
2010-11-18 09:37:46 +00:00
|
|
|
mLoggingHandler.post(new Runnable() {
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2010-11-18 09:37:46 +00:00
|
|
|
public void run() {
|
|
|
|
if (mFile != null && mFile.exists()) {
|
|
|
|
if (LatinImeLogger.sDBG) {
|
2011-02-20 13:04:50 +00:00
|
|
|
Log.d(USABILITY_TAG, "Delete log file.");
|
2010-11-18 09:37:46 +00:00
|
|
|
}
|
|
|
|
mFile.delete();
|
|
|
|
mWriter.close();
|
|
|
|
}
|
2010-11-16 06:52:34 +00:00
|
|
|
}
|
2010-11-18 09:37:46 +00:00
|
|
|
});
|
2010-11-16 06:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private BufferedReader getBufferedReader() {
|
|
|
|
createLogFileIfNotExist();
|
|
|
|
try {
|
|
|
|
return new BufferedReader(new FileReader(mFile));
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private PrintWriter getPrintWriter(
|
|
|
|
File dir, String filename, boolean renew) throws IOException {
|
|
|
|
mFile = new File(dir, filename);
|
|
|
|
if (mFile.exists()) {
|
|
|
|
if (renew) {
|
|
|
|
mFile.delete();
|
|
|
|
}
|
|
|
|
}
|
2010-11-18 09:37:46 +00:00
|
|
|
return new PrintWriter(new FileOutputStream(mFile), true /* autoFlush */);
|
2010-11-16 06:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-20 03:54:14 +00:00
|
|
|
|
2011-02-20 13:04:50 +00:00
|
|
|
public static int getKeyboardMode(EditorInfo attribute) {
|
|
|
|
if (attribute == null)
|
|
|
|
return KeyboardId.MODE_TEXT;
|
|
|
|
|
|
|
|
final int inputType = attribute.inputType;
|
|
|
|
final int variation = inputType & InputType.TYPE_MASK_VARIATION;
|
|
|
|
|
|
|
|
switch (inputType & InputType.TYPE_MASK_CLASS) {
|
|
|
|
case InputType.TYPE_CLASS_NUMBER:
|
|
|
|
case InputType.TYPE_CLASS_DATETIME:
|
|
|
|
return KeyboardId.MODE_NUMBER;
|
|
|
|
case InputType.TYPE_CLASS_PHONE:
|
|
|
|
return KeyboardId.MODE_PHONE;
|
|
|
|
case InputType.TYPE_CLASS_TEXT:
|
2011-03-25 06:28:29 +00:00
|
|
|
if (InputTypeCompatUtils.isEmailVariation(variation)) {
|
2011-02-20 13:04:50 +00:00
|
|
|
return KeyboardId.MODE_EMAIL;
|
|
|
|
} else if (variation == InputType.TYPE_TEXT_VARIATION_URI) {
|
|
|
|
return KeyboardId.MODE_URL;
|
|
|
|
} else if (variation == InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE) {
|
|
|
|
return KeyboardId.MODE_IM;
|
|
|
|
} else if (variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
|
|
|
|
return KeyboardId.MODE_TEXT;
|
|
|
|
} else {
|
|
|
|
return KeyboardId.MODE_TEXT;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return KeyboardId.MODE_TEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-23 08:23:29 +00:00
|
|
|
public static boolean containsInCsv(String key, String csv) {
|
|
|
|
if (csv == null)
|
|
|
|
return false;
|
|
|
|
for (String option : csv.split(",")) {
|
|
|
|
if (option.equals(key))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean inPrivateImeOptions(String packageName, String key,
|
|
|
|
EditorInfo attribute) {
|
|
|
|
if (attribute == null)
|
|
|
|
return false;
|
|
|
|
return containsInCsv(packageName != null ? packageName + "." + key : key,
|
|
|
|
attribute.privateImeOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a main dictionary resource id
|
|
|
|
* @return main dictionary resource id
|
|
|
|
*/
|
|
|
|
public static int getMainDictionaryResourceId(Resources res) {
|
2011-03-14 18:46:15 +00:00
|
|
|
final String MAIN_DIC_NAME = "main";
|
|
|
|
String packageName = LatinIME.class.getPackage().getName();
|
|
|
|
return res.getIdentifier(MAIN_DIC_NAME, "raw", packageName);
|
2011-02-23 08:23:29 +00:00
|
|
|
}
|
2011-02-25 09:21:02 +00:00
|
|
|
|
|
|
|
public static void loadNativeLibrary() {
|
|
|
|
try {
|
|
|
|
System.loadLibrary("jni_latinime");
|
|
|
|
} catch (UnsatisfiedLinkError ule) {
|
|
|
|
Log.e(TAG, "Could not load native library jni_latinime");
|
|
|
|
}
|
|
|
|
}
|
2011-03-15 18:46:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a and b are equal ignoring the case of the character.
|
|
|
|
* @param a first character to check
|
|
|
|
* @param b second character to check
|
|
|
|
* @return {@code true} if a and b are equal, {@code false} otherwise.
|
|
|
|
*/
|
|
|
|
public static boolean equalsIgnoreCase(char a, char b) {
|
|
|
|
// Some language, such as Turkish, need testing both cases.
|
|
|
|
return a == b
|
|
|
|
|| Character.toLowerCase(a) == Character.toLowerCase(b)
|
|
|
|
|| Character.toUpperCase(a) == Character.toUpperCase(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a and b are equal ignoring the case of the characters, including if they are
|
|
|
|
* both null.
|
|
|
|
* @param a first CharSequence to check
|
|
|
|
* @param b second CharSequence to check
|
|
|
|
* @return {@code true} if a and b are equal, {@code false} otherwise.
|
|
|
|
*/
|
|
|
|
public static boolean equalsIgnoreCase(CharSequence a, CharSequence b) {
|
|
|
|
if (a == b)
|
|
|
|
return true; // including both a and b are null.
|
|
|
|
if (a == null || b == null)
|
|
|
|
return false;
|
|
|
|
final int length = a.length();
|
|
|
|
if (length != b.length())
|
|
|
|
return false;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (!equalsIgnoreCase(a.charAt(i), b.charAt(i)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a and b are equal ignoring the case of the characters, including if a is null
|
|
|
|
* and b is zero length.
|
|
|
|
* @param a CharSequence to check
|
|
|
|
* @param b character array to check
|
|
|
|
* @param offset start offset of array b
|
|
|
|
* @param length length of characters in array b
|
|
|
|
* @return {@code true} if a and b are equal, {@code false} otherwise.
|
|
|
|
* @throws IndexOutOfBoundsException
|
|
|
|
* if {@code offset < 0 || length < 0 || offset + length > data.length}.
|
|
|
|
* @throws NullPointerException if {@code b == null}.
|
|
|
|
*/
|
|
|
|
public static boolean equalsIgnoreCase(CharSequence a, char[] b, int offset, int length) {
|
|
|
|
if (offset < 0 || length < 0 || length > b.length - offset)
|
|
|
|
throw new IndexOutOfBoundsException("array.length=" + b.length + " offset=" + offset
|
|
|
|
+ " length=" + length);
|
|
|
|
if (a == null)
|
|
|
|
return length == 0; // including a is null and b is zero length.
|
|
|
|
if (a.length() != length)
|
|
|
|
return false;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (!equalsIgnoreCase(a.charAt(i), b[offset + i]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-03-29 01:45:49 +00:00
|
|
|
|
|
|
|
public static float getDipScale(Context context) {
|
|
|
|
final float scale = context.getResources().getDisplayMetrics().density;
|
|
|
|
return scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Convert pixel to DIP */
|
|
|
|
public static int dipToPixel(float scale, int dip) {
|
2011-04-26 07:28:56 +00:00
|
|
|
return (int) (dip * scale + 0.5);
|
2011-03-29 01:45:49 +00:00
|
|
|
}
|
2011-04-27 06:43:03 +00:00
|
|
|
|
|
|
|
public static Locale setSystemLocale(Resources res, Locale newLocale) {
|
|
|
|
final Configuration conf = res.getConfiguration();
|
|
|
|
final Locale saveLocale = conf.locale;
|
|
|
|
conf.locale = newLocale;
|
|
|
|
res.updateConfiguration(conf, res.getDisplayMetrics());
|
|
|
|
return saveLocale;
|
|
|
|
}
|
2011-05-17 08:34:17 +00:00
|
|
|
|
|
|
|
private static final HashMap<String, Locale> sLocaleCache = new HashMap<String, Locale>();
|
|
|
|
|
|
|
|
public static Locale constructLocaleFromString(String localeStr) {
|
|
|
|
if (localeStr == null)
|
|
|
|
return null;
|
|
|
|
synchronized (sLocaleCache) {
|
|
|
|
if (sLocaleCache.containsKey(localeStr))
|
|
|
|
return sLocaleCache.get(localeStr);
|
|
|
|
Locale retval = null;
|
|
|
|
String[] localeParams = localeStr.split("_", 3);
|
|
|
|
if (localeParams.length == 1) {
|
|
|
|
retval = new Locale(localeParams[0]);
|
|
|
|
} else if (localeParams.length == 2) {
|
|
|
|
retval = new Locale(localeParams[0], localeParams[1]);
|
|
|
|
} else if (localeParams.length == 3) {
|
|
|
|
retval = new Locale(localeParams[0], localeParams[1], localeParams[2]);
|
|
|
|
}
|
|
|
|
if (retval != null) {
|
|
|
|
sLocaleCache.put(localeStr, retval);
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|