diff options
Diffstat (limited to 'java/src')
7 files changed, 135 insertions, 109 deletions
diff --git a/java/src/com/android/inputmethod/latin/BaseKeyboardView.java b/java/src/com/android/inputmethod/latin/BaseKeyboardView.java index 61978e4f0..ad0e8d41a 100644 --- a/java/src/com/android/inputmethod/latin/BaseKeyboardView.java +++ b/java/src/com/android/inputmethod/latin/BaseKeyboardView.java @@ -929,10 +929,11 @@ public class BaseKeyboardView extends View implements PointerTracker.UIProxy { // the space key preview and 3) pointer moves off the space key to other letter key, we // should hide the preview of the previous key. final boolean hidePreviewOrShowSpaceKeyPreview = (tracker == null) - || tracker.isSpaceKey(keyIndex) || tracker.isSpaceKey(oldKeyIndex); + || (SubtypeSwitcher.USE_SPACEBAR_LANGUAGE_SWITCHER + && SubtypeSwitcher.getInstance().needsToDisplayLanguage() + && (tracker.isSpaceKey(keyIndex) || tracker.isSpaceKey(oldKeyIndex))); // If key changed and preview is on or the key is space (language switch is enabled) - if (oldKeyIndex != keyIndex && (mShowPreview || (hidePreviewOrShowSpaceKeyPreview - && SubtypeSwitcher.getInstance().isLanguageSwitchEnabled()))) { + if (oldKeyIndex != keyIndex && (mShowPreview || (hidePreviewOrShowSpaceKeyPreview))) { if (keyIndex == NOT_A_KEY) { mHandler.cancelPopupPreview(); mHandler.dismissPreview(mDelayAfterPreview); diff --git a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java index e811a2cdd..4f4f7e3a7 100644 --- a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java +++ b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java @@ -74,7 +74,7 @@ public class InputLanguageSelection extends PreferenceActivity { for (int i = 0; i < mAvailableLanguages.size(); i++) { CheckBoxPreference pref = new CheckBoxPreference(this); Locale locale = mAvailableLanguages.get(i).locale; - pref.setTitle(LanguageSwitcher.toTitleCase(locale.getDisplayName(locale))); + pref.setTitle(SubtypeSwitcher.getLanguageName(locale)); boolean checked = isLocaleIn(locale, languageList); pref.setChecked(checked); if (hasDictionary(locale)) { @@ -167,7 +167,7 @@ public class InputLanguageSelection extends PreferenceActivity { if (finalSize == 0) { preprocess[finalSize++] = - new Loc(LanguageSwitcher.toTitleCase(l.getDisplayName(l)), l); + new Loc(SubtypeSwitcher.getLanguageName(l), l); } else { // check previous entry: // same lang and a country -> upgrade to full name and @@ -175,15 +175,15 @@ public class InputLanguageSelection extends PreferenceActivity { // diff lang -> insert ours with lang-only name if (preprocess[finalSize-1].locale.getLanguage().equals( language)) { - preprocess[finalSize-1].label = LanguageSwitcher.toTitleCase( - preprocess[finalSize-1].locale.getDisplayName()); + preprocess[finalSize-1].label = SubtypeSwitcher.getLanguageName( + preprocess[finalSize-1].locale); preprocess[finalSize++] = - new Loc(LanguageSwitcher.toTitleCase(l.getDisplayName()), l); + new Loc(SubtypeSwitcher.getLanguageName(l), l); } else { String displayName; if (s.equals("zz_ZZ")) { } else { - displayName = LanguageSwitcher.toTitleCase(l.getDisplayName(l)); + displayName = SubtypeSwitcher.getLanguageName(l); preprocess[finalSize++] = new Loc(displayName, l); } } diff --git a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java index bc0a40d97..3f6b6ca33 100644 --- a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java @@ -297,7 +297,6 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha keyboard.setVoiceMode( hasVoiceKey(xml == R.xml.kbd_symbols || xml == R.xml.kbd_symbols_black), mVoiceButtonEnabled); - keyboard.setLanguageSwitcher(mSubtypeSwitcher.getLanguageSwitcher()); keyboard.setImeOptions(res, id.mMode, id.mImeOptions); keyboard.setColorOfSymbolIcons(isBlackSym(id.mColorScheme)); diff --git a/java/src/com/android/inputmethod/latin/LanguageSwitcher.java b/java/src/com/android/inputmethod/latin/LanguageSwitcher.java index cc1a295a5..c2805f506 100644 --- a/java/src/com/android/inputmethod/latin/LanguageSwitcher.java +++ b/java/src/com/android/inputmethod/latin/LanguageSwitcher.java @@ -192,12 +192,4 @@ public class LanguageSwitcher { editor.putString(LatinIME.PREF_INPUT_LANGUAGE, getInputLanguage()); SharedPreferencesCompat.apply(editor); } - - static String toTitleCase(String s) { - if (s.length() == 0) { - return s; - } - - return Character.toUpperCase(s.charAt(0)) + s.substring(1); - } } diff --git a/java/src/com/android/inputmethod/latin/LatinIMEUtil.java b/java/src/com/android/inputmethod/latin/LatinIMEUtil.java index 9ef48a413..e392bfd38 100644 --- a/java/src/com/android/inputmethod/latin/LatinIMEUtil.java +++ b/java/src/com/android/inputmethod/latin/LatinIMEUtil.java @@ -21,6 +21,9 @@ import android.view.inputmethod.InputMethodManager; import android.content.Context; import android.inputmethodservice.InputMethodService; import android.os.AsyncTask; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Process; import android.text.format.DateUtils; import android.text.format.Time; import android.util.Log; @@ -172,7 +175,7 @@ public class LatinIMEUtil { } } public String getLastString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); for (int i = 0; i < mLength; ++i) { char c = mCharBuf[normalize(mEnd - 1 - i)]; if (!((LatinIME)mContext).isWordSeparator(c)) { @@ -247,16 +250,22 @@ public class LatinIMEUtil { private static final String FILENAME = "log.txt"; private static final UsabilityStudyLogUtils sInstance = new UsabilityStudyLogUtils(); + private final Handler mLoggingHandler; private File mFile; private File mDirectory; private InputMethodService mIms; private PrintWriter mWriter; - private Date mDate; - private SimpleDateFormat mDateFormat; + private final Date mDate; + private final SimpleDateFormat mDateFormat; private UsabilityStudyLogUtils() { mDate = new Date(); mDateFormat = new SimpleDateFormat("dd MMM HH:mm:ss.SSS"); + + HandlerThread handlerThread = new HandlerThread("UsabilityStudyLogUtils logging task", + Process.THREAD_PRIORITY_BACKGROUND); + handlerThread.start(); + mLoggingHandler = new Handler(handlerThread.getLooper()); } public static UsabilityStudyLogUtils getInstance() { @@ -266,7 +275,6 @@ public class LatinIMEUtil { public void init(InputMethodService ims) { mIms = ims; mDirectory = ims.getFilesDir(); - createLogFileIfNotExist(); } private void createLogFileIfNotExist() { @@ -301,51 +309,63 @@ public class LatinIMEUtil { LatinImeLogger.onPrintAllUsabilityStudtyLogs(); } - public void write(String log) { - createLogFileIfNotExist(); - final long currentTime = System.currentTimeMillis(); - mDate.setTime(currentTime); + public void write(final String log) { + mLoggingHandler.post(new Runnable() { + public void run() { + createLogFileIfNotExist(); + final long currentTime = System.currentTimeMillis(); + mDate.setTime(currentTime); - final String printString = String.format("%s\t%d\t%s\n", mDateFormat.format(mDate), - currentTime, log); - if (LatinImeLogger.sDBG) { - Log.d(TAG, "Write: " + log); - } - mWriter.print(printString); - mWriter.flush(); + final String printString = String.format("%s\t%d\t%s\n", + mDateFormat.format(mDate), currentTime, log); + if (LatinImeLogger.sDBG) { + Log.d(TAG, "Write: " + log); + } + mWriter.print(printString); + } + }); } public void printAll() { - StringBuffer sb = new StringBuffer(); - BufferedReader br = getBufferedReader(); - String line; - try { - while ((line = br.readLine()) != null) { - sb.append('\n'); - sb.append(line); + mLoggingHandler.post(new Runnable() { + 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) { + Log.e(TAG, "Can't read log file."); + } finally { + if (LatinImeLogger.sDBG) { + Log.d(TAG, "output all logs\n" + sb.toString()); + } + mIms.getCurrentInputConnection().commitText(sb.toString(), 0); + try { + br.close(); + } catch (IOException e) { + } + } } - } catch (IOException e) { - Log.e(TAG, "Can't read log file."); - } finally { - if (LatinImeLogger.sDBG) { - Log.d(TAG, "output all logs\n" + sb.toString()); - } - mIms.getCurrentInputConnection().commitText(sb.toString(), 0); - try { - br.close(); - } catch (IOException e) { - } - } + }); } public void clearAll() { - if (mFile != null && mFile.exists()) { - if (LatinImeLogger.sDBG) { - Log.d(TAG, "Delete log file."); + mLoggingHandler.post(new Runnable() { + public void run() { + if (mFile != null && mFile.exists()) { + if (LatinImeLogger.sDBG) { + Log.d(TAG, "Delete log file."); + } + mFile.delete(); + mWriter.close(); + } } - mFile.delete(); - mWriter.close(); - } + }); } private BufferedReader getBufferedReader() { @@ -365,7 +385,7 @@ public class LatinIMEUtil { mFile.delete(); } } - return new PrintWriter(new FileOutputStream(mFile)); + return new PrintWriter(new FileOutputStream(mFile), true /* autoFlush */); } } } diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboard.java b/java/src/com/android/inputmethod/latin/LatinKeyboard.java index 54d8c47f8..9473eaf84 100644 --- a/java/src/com/android/inputmethod/latin/LatinKeyboard.java +++ b/java/src/com/android/inputmethod/latin/LatinKeyboard.java @@ -66,8 +66,6 @@ public class LatinKeyboard extends BaseKeyboard { private int mSpaceKeyIndex = -1; private int mSpaceDragStartX; private int mSpaceDragLastDiff; - private Locale mLocale; - private LanguageSwitcher mLanguageSwitcher; private final Resources mRes; private final Context mContext; private int mMode; // TODO: remove this and use the corresponding mode in the parent class @@ -389,7 +387,7 @@ public class LatinKeyboard extends BaseKeyboard { private void updateSpaceBarForLocale(boolean isAutoCompletion, boolean isBlack) { final Resources res = mRes; // If application locales are explicitly selected. - if (mLocale != null) { + if (SubtypeSwitcher.getInstance().needsToDisplayLanguage()) { mSpaceKey.icon = new BitmapDrawable(res, drawSpaceBar(OPACITY_FULLY_OPAQUE, isAutoCompletion, isBlack)); } else { @@ -421,7 +419,7 @@ public class LatinKeyboard extends BaseKeyboard { final Rect bounds = new Rect(); // Estimate appropriate language name text size to fit in maxTextWidth. - String language = LanguageSwitcher.toTitleCase(locale.getDisplayLanguage(locale)); + String language = SubtypeSwitcher.getLanguageName(locale); int textWidth = getTextWidth(paint, language, origTextSize, bounds); // Assuming text width and text size are proportional to each other. float textSize = origTextSize * Math.min(maxTextWidth / textWidth, 1.0f); @@ -437,7 +435,7 @@ public class LatinKeyboard extends BaseKeyboard { textSize = origTextSize; } if (useShortName) { - language = LanguageSwitcher.toTitleCase(locale.getLanguage()); + language = SubtypeSwitcher.getShortLanguageName(locale); textWidth = getTextWidth(paint, language, origTextSize, bounds); textSize = origTextSize * Math.min(maxTextWidth / textWidth, 1.0f); } @@ -462,15 +460,16 @@ public class LatinKeyboard extends BaseKeyboard { final Resources res = mRes; canvas.drawColor(res.getColor(R.color.latinkeyboard_transparent), PorterDuff.Mode.CLEAR); + SubtypeSwitcher subtypeSwitcher = SubtypeSwitcher.getInstance(); // If application locales are explicitly selected. - if (mLocale != null) { + if (subtypeSwitcher.needsToDisplayLanguage()) { final Paint paint = new Paint(); paint.setAlpha(opacity); paint.setAntiAlias(true); paint.setTextAlign(Align.CENTER); final boolean allowVariableTextSize = true; - final String language = layoutSpaceBar(paint, mLanguageSwitcher.getInputLocale(), + final String language = layoutSpaceBar(paint, subtypeSwitcher.getInputLocale(), mButtonArrowLeftIcon, mButtonArrowRightIcon, width, height, getTextSizeFromTheme(android.R.style.TextAppearance_Small, 14), allowVariableTextSize); @@ -487,7 +486,8 @@ public class LatinKeyboard extends BaseKeyboard { canvas.drawText(language, width / 2, baseline - descent, paint); // Put arrows that are already layed out on either side of the text - if (mLanguageSwitcher.getLocaleCount() > 1) { + if (SubtypeSwitcher.USE_SPACEBAR_LANGUAGE_SWITCHER + && subtypeSwitcher.getEnabledKeyboardLocaleCount() > 1) { mButtonArrowLeftIcon.draw(canvas); mButtonArrowRightIcon.draw(canvas); } @@ -531,28 +531,13 @@ public class LatinKeyboard extends BaseKeyboard { } public int getLanguageChangeDirection() { - if (mSpaceKey == null || mLanguageSwitcher.getLocaleCount() < 2 - || Math.abs(mSpaceDragLastDiff) < mSpaceKey.width * SPACEBAR_DRAG_THRESHOLD ) { + if (mSpaceKey == null || SubtypeSwitcher.getInstance().getEnabledKeyboardLocaleCount() <= 1 + || Math.abs(mSpaceDragLastDiff) < mSpaceKey.width * SPACEBAR_DRAG_THRESHOLD) { return 0; // No change } return mSpaceDragLastDiff > 0 ? 1 : -1; } - public void setLanguageSwitcher(LanguageSwitcher switcher) { - mLanguageSwitcher = switcher; - Locale locale = mLanguageSwitcher.getLocaleCount() > 0 - ? mLanguageSwitcher.getInputLocale() - : null; - // If the language count is 1 and is the same as the system language, don't show it. - if (locale != null - && mLanguageSwitcher.getLocaleCount() == 1 - && mLanguageSwitcher.getSystemLocale().getLanguage() - .equalsIgnoreCase(locale.getLanguage())) { - locale = null; - } - mLocale = locale; - } - boolean isCurrentlyInSpace() { return mCurrentlyInSpace; } @@ -586,7 +571,8 @@ public class LatinKeyboard extends BaseKeyboard { if (code == KEYCODE_DELETE) x -= key.width / 6; } else if (code == LatinIME.KEYCODE_SPACE) { y += LatinKeyboard.sSpacebarVerticalCorrection; - if (mLanguageSwitcher.getLocaleCount() > 1) { + if (SubtypeSwitcher.USE_SPACEBAR_LANGUAGE_SWITCHER + && SubtypeSwitcher.getInstance().getEnabledKeyboardLocaleCount() > 1) { if (mCurrentlyInSpace) { int diff = x - mSpaceDragStartX; if (Math.abs(diff - mSpaceDragLastDiff) > 0) { @@ -851,9 +837,6 @@ public class LatinKeyboard extends BaseKeyboard { invalidateSelf(); } - private String getLanguageName(Locale locale) { - return LanguageSwitcher.toTitleCase(locale.getDisplayLanguage(locale)); - } @Override public void draw(Canvas canvas) { @@ -867,10 +850,10 @@ public class LatinKeyboard extends BaseKeyboard { final Drawable rArrow = mRightDrawable; canvas.clipRect(0, 0, width, height); if (mCurrentLanguage == null) { - final LanguageSwitcher languageSwitcher = mLanguageSwitcher; - mCurrentLanguage = getLanguageName(languageSwitcher.getInputLocale()); - mNextLanguage = getLanguageName(languageSwitcher.getNextInputLocale()); - mPrevLanguage = getLanguageName(languageSwitcher.getPrevInputLocale()); + SubtypeSwitcher subtypeSwitcher = SubtypeSwitcher.getInstance(); + mCurrentLanguage = subtypeSwitcher.getInputLanguageName(); + mNextLanguage = subtypeSwitcher.getNextInputLanguageName(); + mPrevLanguage = subtypeSwitcher.getPreviousInputLanguageName(); } // Draw language text with shadow final float baseline = mHeight * SPACEBAR_LANGUAGE_BASELINE - paint.descent(); diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index a295957a1..768274ed7 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -31,7 +31,9 @@ import java.util.List; import java.util.Locale; public class SubtypeSwitcher { - private static final boolean USE_LEGACY_LANGUAGE_SWITCHER = true; + // This flag indicates if we support language switching by swipe on space bar. + // We may or may not draw the current language on space bar regardless of this flag. + public static final boolean USE_SPACEBAR_LANGUAGE_SWITCHER = true; private static final String TAG = "SubtypeSwitcher"; private static final SubtypeSwitcher sInstance = new SubtypeSwitcher(); private InputMethodService mService; @@ -46,7 +48,7 @@ public class SubtypeSwitcher { sInstance.mService = service; sInstance.mResources = service.getResources(); sInstance.mSystemLocale = sInstance.mResources.getConfiguration().locale; - if (USE_LEGACY_LANGUAGE_SWITCHER) { + if (USE_SPACEBAR_LANGUAGE_SWITCHER) { sInstance.initLanguageSwitcher(service); } } @@ -61,22 +63,23 @@ public class SubtypeSwitcher { // Language Switching functions // ////////////////////////////////// - private int getEnabledKeyboardLocaleCount() { - if (USE_LEGACY_LANGUAGE_SWITCHER) { + public int getEnabledKeyboardLocaleCount() { + if (USE_SPACEBAR_LANGUAGE_SWITCHER) { return mLanguageSwitcher.getLocaleCount(); } // TODO: Implement for no legacy mode return 0; } - public boolean isLanguageSwitchEnabled() { + // TODO: Cache the value + public boolean needsToDisplayLanguage() { // TODO: Takes care of two-char locale such as "en" in addition to "en_US" - return !(getEnabledKeyboardLocaleCount() == 1 && getSystemLocale().getLanguage( + return !(getEnabledKeyboardLocaleCount() <= 1 && getSystemLocale().getLanguage( ).equalsIgnoreCase(getInputLocale().getLanguage())); } public Locale getInputLocale() { - if (USE_LEGACY_LANGUAGE_SWITCHER) { + if (USE_SPACEBAR_LANGUAGE_SWITCHER) { return mLanguageSwitcher.getInputLocale(); } // TODO: Implement for no legacy mode @@ -85,7 +88,7 @@ public class SubtypeSwitcher { public String getInputLanguage() { String inputLanguage = null; - if (USE_LEGACY_LANGUAGE_SWITCHER) { + if (USE_SPACEBAR_LANGUAGE_SWITCHER) { inputLanguage = mLanguageSwitcher.getInputLanguage(); } // Should return system locale if there is no Language available. @@ -96,7 +99,7 @@ public class SubtypeSwitcher { } public String[] getEnabledLanguages() { - if (USE_LEGACY_LANGUAGE_SWITCHER) { + if (USE_SPACEBAR_LANGUAGE_SWITCHER) { return mLanguageSwitcher.getEnabledLanguages(); } // TODO: Implement for no legacy mode @@ -107,6 +110,7 @@ public class SubtypeSwitcher { return mSystemLocale; } + // TODO: Cache the value for faster processing. public boolean isSystemLocaleSameAsInputLocale() { // TODO: Takes care of two-char locale such as "en" in addition to "en_US" return getSystemLocale().getLanguage().equalsIgnoreCase( @@ -114,7 +118,7 @@ public class SubtypeSwitcher { } public void onConfigurationChanged(Configuration conf) { - if (USE_LEGACY_LANGUAGE_SWITCHER) { + if (USE_SPACEBAR_LANGUAGE_SWITCHER) { // If the system locale changes and is different from the saved // locale (mSystemLocale), then reload the input locale list from the // latin ime settings (shared prefs) and reset the input locale @@ -132,7 +136,7 @@ public class SubtypeSwitcher { } public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (USE_LEGACY_LANGUAGE_SWITCHER) { + if (USE_SPACEBAR_LANGUAGE_SWITCHER) { mLanguageSwitcher.loadLocales(sharedPreferences); return; } @@ -151,6 +155,38 @@ public class SubtypeSwitcher { //////////////////////////////////////////// private LanguageSwitcher mLanguageSwitcher; + public static String getLanguageName(Locale locale) { + return toTitleCase(locale.getDisplayLanguage(locale)); + } + + public static String getShortLanguageName(Locale locale) { + return toTitleCase(locale.getLanguage()); + } + + private static String toTitleCase(String s) { + if (s.length() == 0) { + return s; + } + return Character.toUpperCase(s.charAt(0)) + s.substring(1); + } + + public String getInputLanguageName() { + return getLanguageName(getInputLocale()); + } + + public String getNextInputLanguageName() { + if (USE_SPACEBAR_LANGUAGE_SWITCHER) { + return getLanguageName(mLanguageSwitcher.getNextInputLocale()); + } + return ""; + } + + public String getPreviousInputLanguageName() { + if (USE_SPACEBAR_LANGUAGE_SWITCHER) { + return getLanguageName(mLanguageSwitcher.getPrevInputLocale()); + } + return ""; + } // TODO: This can be an array of String // A list of locales which are supported by default for voice input, unless we get a @@ -183,7 +219,7 @@ public class SubtypeSwitcher { } public void loadSettings(SharedPreferences prefs) { - if (USE_LEGACY_LANGUAGE_SWITCHER) { + if (USE_SPACEBAR_LANGUAGE_SWITCHER) { mLanguageSwitcher.loadLocales(prefs); } } @@ -208,9 +244,4 @@ public class SubtypeSwitcher { mLanguageSwitcher.loadLocales(prefs); mLanguageSwitcher.setSystemLocale(conf.locale); } - - // TODO: remove this function when the refactor for LanguageSwitcher will be finished - public LanguageSwitcher getLanguageSwitcher() { - return mLanguageSwitcher; - } }
\ No newline at end of file |