Refactor keyboard mode holding variables

Change-Id: Id8d4e780a98292fda63b1e5bbf739eda78935a3b
This commit is contained in:
Tadashi G. Takaoka 2011-02-20 22:04:50 +09:00
parent f5f834afcd
commit cb97c2f140
4 changed files with 103 additions and 98 deletions

View file

@ -67,8 +67,6 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
private final HashMap<KeyboardId, SoftReference<LatinKeyboard>> mKeyboardCache = private final HashMap<KeyboardId, SoftReference<LatinKeyboard>> mKeyboardCache =
new HashMap<KeyboardId, SoftReference<LatinKeyboard>>(); new HashMap<KeyboardId, SoftReference<LatinKeyboard>>();
// TODO: clean mMode up and use mAttribute instead.
private int mMode = KeyboardId.MODE_TEXT; /* default value */
private EditorInfo mAttribute; private EditorInfo mAttribute;
private boolean mIsSymbols; private boolean mIsSymbols;
/** mIsAutoCorrectionActive indicates that auto corrected word will be input instead of /** mIsAutoCorrectionActive indicates that auto corrected word will be input instead of
@ -124,11 +122,10 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
prefs.registerOnSharedPreferenceChangeListener(sInstance); prefs.registerOnSharedPreferenceChangeListener(sInstance);
} }
private void makeSymbolsKeyboardIds() { private void makeSymbolsKeyboardIds(final int mode) {
final Locale locale = mSubtypeSwitcher.getInputLocale(); final Locale locale = mSubtypeSwitcher.getInputLocale();
final Resources res = mInputMethodService.getResources(); final Resources res = mInputMethodService.getResources();
final int orientation = res.getConfiguration().orientation; final int orientation = res.getConfiguration().orientation;
final int mode = mMode;
final int colorScheme = getColorScheme(); final int colorScheme = getColorScheme();
final boolean hasVoiceKey = mVoiceKeyEnabled && !mVoiceButtonOnPrimary; final boolean hasVoiceKey = mVoiceKeyEnabled && !mVoiceButtonOnPrimary;
// Note: This comment is only applied for phone number keyboard layout. // Note: This comment is only applied for phone number keyboard layout.
@ -151,37 +148,36 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
return mVoiceKeyEnabled && (isSymbols != mVoiceButtonOnPrimary); return mVoiceKeyEnabled && (isSymbols != mVoiceButtonOnPrimary);
} }
public void loadKeyboard(int mode, EditorInfo attribute, boolean voiceKeyEnabled, public void loadKeyboard(EditorInfo attribute, boolean voiceKeyEnabled,
boolean voiceButtonOnPrimary) { boolean voiceButtonOnPrimary) {
mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_ALPHA; mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_ALPHA;
try { try {
loadKeyboardInternal(mode, attribute, voiceKeyEnabled, voiceButtonOnPrimary, false); loadKeyboardInternal(attribute, voiceKeyEnabled, voiceButtonOnPrimary, false);
} catch (RuntimeException e) { } catch (RuntimeException e) {
// Get KeyboardId to record which keyboard has been failed to load. // Get KeyboardId to record which keyboard has been failed to load.
final KeyboardId id = getKeyboardId(mode, attribute, false); final KeyboardId id = getKeyboardId(attribute, false);
Log.w(TAG, "loading keyboard failed: " + id, e); Log.w(TAG, "loading keyboard failed: " + id, e);
LatinImeLogger.logOnException(id.toString(), e); LatinImeLogger.logOnException(id.toString(), e);
} }
} }
private void loadKeyboardInternal(int mode, EditorInfo attribute, boolean voiceButtonEnabled, private void loadKeyboardInternal(EditorInfo attribute, boolean voiceButtonEnabled,
boolean voiceButtonOnPrimary, boolean isSymbols) { boolean voiceButtonOnPrimary, boolean isSymbols) {
if (mInputView == null) return; if (mInputView == null) return;
mMode = mode;
mAttribute = attribute; mAttribute = attribute;
mVoiceKeyEnabled = voiceButtonEnabled; mVoiceKeyEnabled = voiceButtonEnabled;
mVoiceButtonOnPrimary = voiceButtonOnPrimary; mVoiceButtonOnPrimary = voiceButtonOnPrimary;
mIsSymbols = isSymbols; mIsSymbols = isSymbols;
// Update the settings key state because number of enabled IMEs could have been changed // Update the settings key state because number of enabled IMEs could have been changed
mHasSettingsKey = getSettingsKeyMode(mPrefs, mInputMethodService); mHasSettingsKey = getSettingsKeyMode(mPrefs, mInputMethodService);
final KeyboardId id = getKeyboardId(mode, attribute, isSymbols); final KeyboardId id = getKeyboardId(attribute, isSymbols);
final Keyboard oldKeyboard = mInputView.getKeyboard(); final Keyboard oldKeyboard = mInputView.getKeyboard();
if (oldKeyboard != null && oldKeyboard.mId.equals(id)) if (oldKeyboard != null && oldKeyboard.mId.equals(id))
return; return;
makeSymbolsKeyboardIds(); makeSymbolsKeyboardIds(id.mMode);
mCurrentId = id; mCurrentId = id;
mInputView.setPreviewEnabled(mInputMethodService.getPopupOn()); mInputView.setPreviewEnabled(mInputMethodService.getPopupOn());
setKeyboard(getKeyboard(id)); setKeyboard(getKeyboard(id));
@ -228,7 +224,8 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
return keyboard; return keyboard;
} }
private KeyboardId getKeyboardId(int mode, EditorInfo attribute, boolean isSymbols) { private KeyboardId getKeyboardId(EditorInfo attribute, boolean isSymbols) {
final int mode = Utils.getKeyboardMode(attribute);
final boolean hasVoiceKey = hasVoiceKey(isSymbols); final boolean hasVoiceKey = hasVoiceKey(isSymbols);
final int charColorId = getColorScheme(); final int charColorId = getColorScheme();
final int xmlId; final int xmlId;
@ -265,7 +262,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
} }
public int getKeyboardMode() { public int getKeyboardMode() {
return mMode; return mCurrentId != null ? mCurrentId.mMode : KeyboardId.MODE_TEXT;
} }
public boolean isAlphabetMode() { public boolean isAlphabetMode() {
@ -569,8 +566,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
} }
private void toggleKeyboardMode() { private void toggleKeyboardMode() {
loadKeyboardInternal(mMode, mAttribute, mVoiceKeyEnabled, mVoiceButtonOnPrimary, loadKeyboardInternal(mAttribute, mVoiceKeyEnabled, mVoiceButtonOnPrimary, !mIsSymbols);
!mIsSymbols);
if (mIsSymbols) { if (mIsSymbols) {
mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_SYMBOL_BEGIN; mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_SYMBOL_BEGIN;
} else { } else {

View file

@ -18,7 +18,6 @@ package com.android.inputmethod.latin;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardActionListener; import com.android.inputmethod.keyboard.KeyboardActionListener;
import com.android.inputmethod.keyboard.KeyboardId;
import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.KeyboardView; import com.android.inputmethod.keyboard.KeyboardView;
import com.android.inputmethod.keyboard.LatinKeyboard; import com.android.inputmethod.keyboard.LatinKeyboard;
@ -494,11 +493,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
return container; return container;
} }
private static boolean isEmailVariation(int variation) {
return variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|| variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS;
}
@Override @Override
public void onStartInputView(EditorInfo attribute, boolean restarting) { public void onStartInputView(EditorInfo attribute, boolean restarting) {
final KeyboardSwitcher switcher = mKeyboardSwitcher; final KeyboardSwitcher switcher = mKeyboardSwitcher;
@ -522,7 +516,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mVoiceConnector.resetVoiceStates(Utils.isPasswordInputType(attribute.inputType) mVoiceConnector.resetVoiceStates(Utils.isPasswordInputType(attribute.inputType)
|| Utils.isVisiblePasswordInputType(attribute.inputType)); || Utils.isVisiblePasswordInputType(attribute.inputType));
final int mode = initializeInputAttributesAndGetMode(attribute); initializeInputAttributes(attribute);
inputView.closing(); inputView.closing();
mEnteredText = null; mEnteredText = null;
@ -533,7 +527,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
loadSettings(attribute); loadSettings(attribute);
if (mSubtypeSwitcher.isKeyboardMode()) { if (mSubtypeSwitcher.isKeyboardMode()) {
switcher.loadKeyboard(mode, attribute, switcher.loadKeyboard(attribute,
mVoiceConnector.isVoiceButtonEnabled(), mVoiceConnector.isVoiceButtonEnabled(),
mVoiceConnector.isVoiceButtonOnPrimary()); mVoiceConnector.isVoiceButtonOnPrimary());
switcher.updateShiftState(); switcher.updateShiftState();
@ -557,10 +551,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (TRACE) Debug.startMethodTracing("/data/trace/latinime"); if (TRACE) Debug.startMethodTracing("/data/trace/latinime");
} }
// TODO: Separate calculating keyboard mode from initializing attributes, and make it an private void initializeInputAttributes(EditorInfo attribute) {
// utility method in {@link Utils}. if (attribute == null)
private int initializeInputAttributesAndGetMode(EditorInfo attribute) { return;
if (attribute == null) return KeyboardId.MODE_TEXT;
final int inputType = attribute.inputType; final int inputType = attribute.inputType;
final int variation = inputType & InputType.TYPE_MASK_VARIATION; final int variation = inputType & InputType.TYPE_MASK_VARIATION;
mAutoSpace = false; mAutoSpace = false;
@ -569,70 +562,48 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mApplicationSpecifiedCompletionOn = false; mApplicationSpecifiedCompletionOn = false;
mApplicationSpecifiedCompletions = null; mApplicationSpecifiedCompletions = null;
final int mode; if ((inputType & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_TEXT) {
switch (inputType & InputType.TYPE_MASK_CLASS) { mIsSettingsSuggestionStripOn = true;
case InputType.TYPE_CLASS_NUMBER: // Make sure that passwords are not displayed in candidate view
case InputType.TYPE_CLASS_DATETIME: if (Utils.isPasswordInputType(inputType)
mode = KeyboardId.MODE_NUMBER; || Utils.isVisiblePasswordInputType(inputType)) {
break; mIsSettingsSuggestionStripOn = false;
case InputType.TYPE_CLASS_PHONE: }
mode = KeyboardId.MODE_PHONE; if (Utils.isEmailVariation(variation)
break; || variation == InputType.TYPE_TEXT_VARIATION_PERSON_NAME) {
case InputType.TYPE_CLASS_TEXT: mAutoSpace = false;
mIsSettingsSuggestionStripOn = true; } else {
// Make sure that passwords are not displayed in candidate view mAutoSpace = true;
if (Utils.isPasswordInputType(inputType) }
|| Utils.isVisiblePasswordInputType(inputType)) { if (Utils.isEmailVariation(variation)) {
mIsSettingsSuggestionStripOn = false; mIsSettingsSuggestionStripOn = false;
} } else if (variation == InputType.TYPE_TEXT_VARIATION_URI) {
if (LatinIME.isEmailVariation(variation) mIsSettingsSuggestionStripOn = false;
|| variation == InputType.TYPE_TEXT_VARIATION_PERSON_NAME) { } else if (variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
mAutoSpace = false; mIsSettingsSuggestionStripOn = false;
} else { } else if (variation == InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT) {
mAutoSpace = true; // If it's a browser edit field and auto correct is not ON explicitly, then
} // disable auto correction, but keep suggestions on.
if (LatinIME.isEmailVariation(variation)) { if ((inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT) == 0) {
mIsSettingsSuggestionStripOn = false; mInputTypeNoAutoCorrect = true;
mode = KeyboardId.MODE_EMAIL;
} else if (variation == InputType.TYPE_TEXT_VARIATION_URI) {
mIsSettingsSuggestionStripOn = false;
mode = KeyboardId.MODE_URL;
} else if (variation == InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE) {
mode = KeyboardId.MODE_IM;
} else if (variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
mIsSettingsSuggestionStripOn = false;
mode = KeyboardId.MODE_TEXT;
} else if (variation == InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT) {
mode = KeyboardId.MODE_WEB;
// If it's a browser edit field and auto correct is not ON explicitly, then
// disable auto correction, but keep suggestions on.
if ((inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT) == 0) {
mInputTypeNoAutoCorrect = true;
}
} else {
mode = KeyboardId.MODE_TEXT;
} }
}
// If NO_SUGGESTIONS is set, don't do prediction. // If NO_SUGGESTIONS is set, don't do prediction.
if ((inputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) != 0) { if ((inputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) != 0) {
mIsSettingsSuggestionStripOn = false; mIsSettingsSuggestionStripOn = false;
mInputTypeNoAutoCorrect = true; mInputTypeNoAutoCorrect = true;
} }
// If it's not multiline and the autoCorrect flag is not set, then don't correct // If it's not multiline and the autoCorrect flag is not set, then don't correct
if ((inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT) == 0 && if ((inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT) == 0
(inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE) == 0) { && (inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE) == 0) {
mInputTypeNoAutoCorrect = true; mInputTypeNoAutoCorrect = true;
} }
if ((inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) { if ((inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) {
mIsSettingsSuggestionStripOn = false; mIsSettingsSuggestionStripOn = false;
mApplicationSpecifiedCompletionOn = isFullscreenMode(); mApplicationSpecifiedCompletionOn = isFullscreenMode();
} }
break;
default:
mode = KeyboardId.MODE_TEXT;
break;
} }
return mode;
} }
private void checkReCorrectionOnStart() { private void checkReCorrectionOnStart() {
@ -1909,9 +1880,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mSubtypeSwitcher.toggleLanguage(reset, next); mSubtypeSwitcher.toggleLanguage(reset, next);
} }
// Reload keyboard because the current language has been changed. // Reload keyboard because the current language has been changed.
final EditorInfo attribute = getCurrentInputEditorInfo(); mKeyboardSwitcher.loadKeyboard(getCurrentInputEditorInfo(),
final int mode = initializeInputAttributesAndGetMode(attribute);
mKeyboardSwitcher.loadKeyboard(mode, attribute,
mVoiceConnector.isVoiceButtonEnabled(), mVoiceConnector.isVoiceButtonOnPrimary()); mVoiceConnector.isVoiceButtonEnabled(), mVoiceConnector.isVoiceButtonOnPrimary());
initSuggest(); initSuggest();
mKeyboardSwitcher.updateShiftState(); mKeyboardSwitcher.updateShiftState();

View file

@ -18,7 +18,6 @@ package com.android.inputmethod.latin;
import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.LatinKeyboard; import com.android.inputmethod.keyboard.LatinKeyboard;
import com.android.inputmethod.keyboard.LatinKeyboardView;
import com.android.inputmethod.voice.SettingsUtil; import com.android.inputmethod.voice.SettingsUtil;
import com.android.inputmethod.voice.VoiceIMEConnector; import com.android.inputmethod.voice.VoiceIMEConnector;
import com.android.inputmethod.voice.VoiceInput; import com.android.inputmethod.voice.VoiceInput;

View file

@ -45,6 +45,10 @@ public class Utils {
private static final int MINIMUM_SAFETY_NET_CHAR_LENGTH = 4; private static final int MINIMUM_SAFETY_NET_CHAR_LENGTH = 4;
private static boolean DBG = LatinImeLogger.sDBG; private static boolean DBG = LatinImeLogger.sDBG;
private Utils() {
// Intentional empty constructor for utility class.
}
/** /**
* Cancel an {@link AsyncTask}. * Cancel an {@link AsyncTask}.
* *
@ -319,7 +323,7 @@ public class Utils {
} }
public static class UsabilityStudyLogUtils { public static class UsabilityStudyLogUtils {
private static final String TAG = "UsabilityStudyLogUtils"; private static final String USABILITY_TAG = UsabilityStudyLogUtils.class.getSimpleName();
private static final String FILENAME = "log.txt"; private static final String FILENAME = "log.txt";
private static final UsabilityStudyLogUtils sInstance = private static final UsabilityStudyLogUtils sInstance =
new UsabilityStudyLogUtils(); new UsabilityStudyLogUtils();
@ -356,7 +360,7 @@ public class Utils {
try { try {
mWriter = getPrintWriter(mDirectory, FILENAME, false); mWriter = getPrintWriter(mDirectory, FILENAME, false);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Can't create log file."); Log.e(USABILITY_TAG, "Can't create log file.");
} }
} }
} }
@ -393,7 +397,7 @@ public class Utils {
final String printString = String.format("%s\t%d\t%s\n", final String printString = String.format("%s\t%d\t%s\n",
mDateFormat.format(mDate), currentTime, log); mDateFormat.format(mDate), currentTime, log);
if (LatinImeLogger.sDBG) { if (LatinImeLogger.sDBG) {
Log.d(TAG, "Write: " + log); Log.d(USABILITY_TAG, "Write: " + log);
} }
mWriter.print(printString); mWriter.print(printString);
} }
@ -414,10 +418,10 @@ public class Utils {
sb.append(line); sb.append(line);
} }
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Can't read log file."); Log.e(USABILITY_TAG, "Can't read log file.");
} finally { } finally {
if (LatinImeLogger.sDBG) { if (LatinImeLogger.sDBG) {
Log.d(TAG, "output all logs\n" + sb.toString()); Log.d(USABILITY_TAG, "output all logs\n" + sb.toString());
} }
mIms.getCurrentInputConnection().commitText(sb.toString(), 0); mIms.getCurrentInputConnection().commitText(sb.toString(), 0);
try { try {
@ -436,7 +440,7 @@ public class Utils {
public void run() { public void run() {
if (mFile != null && mFile.exists()) { if (mFile != null && mFile.exists()) {
if (LatinImeLogger.sDBG) { if (LatinImeLogger.sDBG) {
Log.d(TAG, "Delete log file."); Log.d(USABILITY_TAG, "Delete log file.");
} }
mFile.delete(); mFile.delete();
mWriter.close(); mWriter.close();
@ -466,6 +470,43 @@ public class Utils {
} }
} }
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:
if (Utils.isEmailVariation(variation)) {
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 if (variation == InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT) {
return KeyboardId.MODE_WEB;
} else {
return KeyboardId.MODE_TEXT;
}
default:
return KeyboardId.MODE_TEXT;
}
}
public static boolean isEmailVariation(int variation) {
return variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|| variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS;
}
// Please refer to TextView.isPasswordInputType // Please refer to TextView.isPasswordInputType
public static boolean isPasswordInputType(int inputType) { public static boolean isPasswordInputType(int inputType) {
final int variation = final int variation =