Move functions in LatinKeyboard related to LanguageSwitcher to SubtypeSwitcher

Change-Id: I777db896bd0287931ce7c810b080ccee1121d34e
main
satok 2010-11-18 07:58:14 +09:00
parent 0ed7191b4d
commit 3b776b7892
6 changed files with 70 additions and 65 deletions

View File

@ -932,7 +932,7 @@ public class BaseKeyboardView extends View implements PointerTracker.UIProxy {
|| 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()))) {
&& SubtypeSwitcher.getInstance().needsToDisplayLanguage()))) {
if (keyIndex == NOT_A_KEY) {
mHandler.cancelPopupPreview();
mHandler.dismissPreview(mDelayAfterPreview);

View File

@ -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);
}
}

View File

@ -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));

View File

@ -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);
}
}

View File

@ -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();

View File

@ -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(
).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;
}
}