Move shorcut IME related code from SubtypeSwitcher to RichInputMethodManager

Change-Id: I0f95f603d80570c937e8c38ded88df341c11795e
main
Tadashi G. Takaoka 2014-11-06 14:27:47 +09:00
parent 2a7da0ab87
commit 8a2c4afad4
6 changed files with 121 additions and 133 deletions

View File

@ -163,7 +163,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
currentSettingsValues.mKeyPreviewDismissEndXScale, currentSettingsValues.mKeyPreviewDismissEndXScale,
currentSettingsValues.mKeyPreviewDismissEndYScale, currentSettingsValues.mKeyPreviewDismissEndYScale,
currentSettingsValues.mKeyPreviewDismissDuration); currentSettingsValues.mKeyPreviewDismissDuration);
keyboardView.updateShortcutKey(mSubtypeSwitcher.isShortcutImeReady()); keyboardView.updateShortcutKey(RichInputMethodManager.getInstance().isShortcutImeReady());
final boolean subtypeChanged = (oldKeyboard == null) final boolean subtypeChanged = (oldKeyboard == null)
|| !keyboard.mId.mSubtype.equals(oldKeyboard.mId.mSubtype); || !keyboard.mId.mSubtype.equals(oldKeyboard.mId.mSubtype);
final int languageOnSpacebarFormatType = mSubtypeSwitcher.getLanguageOnSpacebarFormatType( final int languageOnSpacebarFormatType = mSubtypeSwitcher.getLanguageOnSpacebarFormatType(
@ -414,9 +414,10 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
} }
public void onNetworkStateChanged() { public void onNetworkStateChanged() {
if (mKeyboardView != null) { if (mKeyboardView == null) {
mKeyboardView.updateShortcutKey(mSubtypeSwitcher.isShortcutImeReady()); return;
} }
mKeyboardView.updateShortcutKey(RichInputMethodManager.getInstance().isShortcutImeReady());
} }
public int getKeyboardShiftMode() { public int getKeyboardShiftMode() {

View File

@ -1424,7 +1424,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// completely replace #onCodeInput. // completely replace #onCodeInput.
public void onEvent(@Nonnull final Event event) { public void onEvent(@Nonnull final Event event) {
if (Constants.CODE_SHORTCUT == event.mKeyCode) { if (Constants.CODE_SHORTCUT == event.mKeyCode) {
mSubtypeSwitcher.switchToShortcutIME(this); mRichImm.switchToShortcutIME(this);
} }
final InputTransaction completeInputTransaction = final InputTransaction completeInputTransaction =
mInputLogic.onCodeInput(mSettings.getCurrent(), event, mInputLogic.onCodeInput(mSettings.getCurrent(), event,
@ -1811,7 +1811,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
public void onReceive(final Context context, final Intent intent) { public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction(); final String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
mSubtypeSwitcher.onNetworkStateChanged(intent); mRichImm.onNetworkStateChanged(intent);
} else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) { } else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
AudioAndHapticFeedbackManager.getInstance().onRingerModeChanged(); AudioAndHapticFeedbackManager.getInstance().onRingerModeChanged();
} }

View File

@ -17,9 +17,15 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import static com.android.inputmethod.latin.common.Constants.Subtype.KEYBOARD_MODE; import static com.android.inputmethod.latin.common.Constants.Subtype.KEYBOARD_MODE;
import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.REQ_NETWORK_CONNECTIVITY;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.inputmethodservice.InputMethodService;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -29,8 +35,7 @@ import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils; import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.latin.common.Constants;
import com.android.inputmethod.latin.settings.AdditionalFeaturesSettingUtils; import com.android.inputmethod.latin.settings.AdditionalFeaturesSettingUtils;
import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
@ -41,6 +46,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -51,6 +57,7 @@ import javax.annotation.Nonnull;
// non final for easy mocking. // non final for easy mocking.
public class RichInputMethodManager { public class RichInputMethodManager {
private static final String TAG = RichInputMethodManager.class.getSimpleName(); private static final String TAG = RichInputMethodManager.class.getSimpleName();
private static final boolean DEBUG = false;
private RichInputMethodManager() { private RichInputMethodManager() {
// This utility class is not publicly instantiable. // This utility class is not publicly instantiable.
@ -61,6 +68,9 @@ public class RichInputMethodManager {
private Context mContext; private Context mContext;
private InputMethodManagerCompatWrapper mImmWrapper; private InputMethodManagerCompatWrapper mImmWrapper;
private InputMethodInfoCache mInputMethodInfoCache; private InputMethodInfoCache mInputMethodInfoCache;
private InputMethodInfo mShortcutInputMethodInfo;
private InputMethodSubtype mShortcutSubtype;
private boolean mIsNetworkConnected;
final HashMap<InputMethodInfo, List<InputMethodSubtype>> final HashMap<InputMethodInfo, List<InputMethodSubtype>>
mSubtypeListCacheWithImplicitlySelectedSubtypes = new HashMap<>(); mSubtypeListCacheWithImplicitlySelectedSubtypes = new HashMap<>();
final HashMap<InputMethodInfo, List<InputMethodSubtype>> final HashMap<InputMethodInfo, List<InputMethodSubtype>>
@ -100,6 +110,11 @@ public class RichInputMethodManager {
SubtypeLocaleUtils.init(context); SubtypeLocaleUtils.init(context);
final InputMethodSubtype[] additionalSubtypes = getAdditionalSubtypes(context); final InputMethodSubtype[] additionalSubtypes = getAdditionalSubtypes(context);
setAdditionalInputMethodSubtypes(additionalSubtypes); setAdditionalInputMethodSubtypes(additionalSubtypes);
final ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
mIsNetworkConnected = (info != null && info.isConnected());
} }
public InputMethodSubtype[] getAdditionalSubtypes(final Context context) { public InputMethodSubtype[] getAdditionalSubtypes(final Context context) {
@ -462,4 +477,96 @@ public class RichInputMethodManager {
} }
return true; return true;
} }
// TODO: Make this private
void updateShortcutIME() {
if (DEBUG) {
Log.d(TAG, "Update shortcut IME from : "
+ (mShortcutInputMethodInfo == null
? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
+ (mShortcutSubtype == null ? "<null>" : (
mShortcutSubtype.getLocale() + ", " + mShortcutSubtype.getMode())));
}
// TODO: Update an icon for shortcut IME
final Map<InputMethodInfo, List<InputMethodSubtype>> shortcuts =
getInputMethodManager().getShortcutInputMethodsAndSubtypes();
mShortcutInputMethodInfo = null;
mShortcutSubtype = null;
for (final InputMethodInfo imi : shortcuts.keySet()) {
final List<InputMethodSubtype> subtypes = shortcuts.get(imi);
// TODO: Returns the first found IMI for now. Should handle all shortcuts as
// appropriate.
mShortcutInputMethodInfo = imi;
// TODO: Pick up the first found subtype for now. Should handle all subtypes
// as appropriate.
mShortcutSubtype = subtypes.size() > 0 ? subtypes.get(0) : null;
break;
}
if (DEBUG) {
Log.d(TAG, "Update shortcut IME to : "
+ (mShortcutInputMethodInfo == null
? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
+ (mShortcutSubtype == null ? "<null>" : (
mShortcutSubtype.getLocale() + ", " + mShortcutSubtype.getMode())));
}
}
public void switchToShortcutIME(final InputMethodService context) {
if (mShortcutInputMethodInfo == null) {
return;
}
final String imiId = mShortcutInputMethodInfo.getId();
switchToTargetIME(imiId, mShortcutSubtype, context);
}
private void switchToTargetIME(final String imiId, final InputMethodSubtype subtype,
final InputMethodService context) {
final IBinder token = context.getWindow().getWindow().getAttributes().token;
if (token == null) {
return;
}
final InputMethodManager imm = getInputMethodManager();
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
imm.setInputMethodAndSubtype(token, imiId, subtype);
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
public boolean isShortcutImeEnabled() {
updateShortcutIME();
if (mShortcutInputMethodInfo == null) {
return false;
}
if (mShortcutSubtype == null) {
return true;
}
return checkIfSubtypeBelongsToImeAndEnabled(
mShortcutInputMethodInfo, mShortcutSubtype);
}
public boolean isShortcutImeReady() {
updateShortcutIME();
if (mShortcutInputMethodInfo == null) {
return false;
}
if (mShortcutSubtype == null) {
return true;
}
if (mShortcutSubtype.containsExtraValueKey(REQ_NETWORK_CONNECTIVITY)) {
return mIsNetworkConnected;
}
return true;
}
public void onNetworkStateChanged(final Intent intent) {
final boolean noConnection = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
mIsNetworkConnected = !noConnection;
KeyboardSwitcher.getInstance().onNetworkStateChanged();
}
} }

View File

@ -16,30 +16,18 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.REQ_NETWORK_CONNECTIVITY;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.inputmethodservice.InputMethodService;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper; import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper;
import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.define.DebugFlags;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -54,10 +42,7 @@ public final class SubtypeSwitcher {
private final LanguageOnSpacebarHelper mLanguageOnSpacebarHelper = private final LanguageOnSpacebarHelper mLanguageOnSpacebarHelper =
new LanguageOnSpacebarHelper(); new LanguageOnSpacebarHelper();
private InputMethodInfo mShortcutInputMethodInfo;
private InputMethodSubtype mShortcutSubtype;
private RichInputMethodSubtype mCurrentRichInputMethodSubtype; private RichInputMethodSubtype mCurrentRichInputMethodSubtype;
private boolean mIsNetworkConnected;
public static SubtypeSwitcher getInstance() { public static SubtypeSwitcher getInstance() {
return sInstance; return sInstance;
@ -79,11 +64,6 @@ public final class SubtypeSwitcher {
} }
mResources = context.getResources(); mResources = context.getResources();
mRichImm = RichInputMethodManager.getInstance(); mRichImm = RichInputMethodManager.getInstance();
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
mIsNetworkConnected = (info != null && info.isConnected());
onSubtypeChanged(mRichImm.getCurrentRawSubtype()); onSubtypeChanged(mRichImm.getCurrentRawSubtype());
updateParametersOnStartInputView(); updateParametersOnStartInputView();
@ -97,39 +77,7 @@ public final class SubtypeSwitcher {
final List<InputMethodSubtype> enabledSubtypesOfThisIme = final List<InputMethodSubtype> enabledSubtypesOfThisIme =
mRichImm.getMyEnabledInputMethodSubtypeList(true); mRichImm.getMyEnabledInputMethodSubtypeList(true);
mLanguageOnSpacebarHelper.updateEnabledSubtypes(enabledSubtypesOfThisIme); mLanguageOnSpacebarHelper.updateEnabledSubtypes(enabledSubtypesOfThisIme);
updateShortcutIME(); mRichImm.updateShortcutIME();
}
private void updateShortcutIME() {
if (DBG) {
Log.d(TAG, "Update shortcut IME from : "
+ (mShortcutInputMethodInfo == null
? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
+ (mShortcutSubtype == null ? "<null>" : (
mShortcutSubtype.getLocale() + ", " + mShortcutSubtype.getMode())));
}
// TODO: Update an icon for shortcut IME
final Map<InputMethodInfo, List<InputMethodSubtype>> shortcuts =
mRichImm.getInputMethodManager().getShortcutInputMethodsAndSubtypes();
mShortcutInputMethodInfo = null;
mShortcutSubtype = null;
for (final InputMethodInfo imi : shortcuts.keySet()) {
final List<InputMethodSubtype> subtypes = shortcuts.get(imi);
// TODO: Returns the first found IMI for now. Should handle all shortcuts as
// appropriate.
mShortcutInputMethodInfo = imi;
// TODO: Pick up the first found subtype for now. Should handle all subtypes
// as appropriate.
mShortcutSubtype = subtypes.size() > 0 ? subtypes.get(0) : null;
break;
}
if (DBG) {
Log.d(TAG, "Update shortcut IME to : "
+ (mShortcutInputMethodInfo == null
? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
+ (mShortcutSubtype == null ? "<null>" : (
mShortcutSubtype.getLocale() + ", " + mShortcutSubtype.getMode())));
}
} }
// Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function. // Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function.
@ -155,76 +103,9 @@ public final class SubtypeSwitcher {
mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage( mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(
sameLocale || (sameLanguage && implicitlyEnabled)); sameLocale || (sameLanguage && implicitlyEnabled));
} }
updateShortcutIME(); mRichImm.updateShortcutIME();
} }
////////////////////////////
// Shortcut IME functions //
////////////////////////////
public void switchToShortcutIME(final InputMethodService context) {
if (mShortcutInputMethodInfo == null) {
return;
}
final String imiId = mShortcutInputMethodInfo.getId();
switchToTargetIME(imiId, mShortcutSubtype, context);
}
private void switchToTargetIME(final String imiId, final InputMethodSubtype subtype,
final InputMethodService context) {
final IBinder token = context.getWindow().getWindow().getAttributes().token;
if (token == null) {
return;
}
final InputMethodManager imm = mRichImm.getInputMethodManager();
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
imm.setInputMethodAndSubtype(token, imiId, subtype);
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
public boolean isShortcutImeEnabled() {
updateShortcutIME();
if (mShortcutInputMethodInfo == null) {
return false;
}
if (mShortcutSubtype == null) {
return true;
}
return mRichImm.checkIfSubtypeBelongsToImeAndEnabled(
mShortcutInputMethodInfo, mShortcutSubtype);
}
public boolean isShortcutImeReady() {
updateShortcutIME();
if (mShortcutInputMethodInfo == null) {
return false;
}
if (mShortcutSubtype == null) {
return true;
}
if (mShortcutSubtype.containsExtraValueKey(REQ_NETWORK_CONNECTIVITY)) {
return mIsNetworkConnected;
}
return true;
}
public void onNetworkStateChanged(final Intent intent) {
final boolean noConnection = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
mIsNetworkConnected = !noConnection;
KeyboardSwitcher.getInstance().onNetworkStateChanged();
}
//////////////////////////////////
// Subtype Switching functions //
//////////////////////////////////
public int getLanguageOnSpacebarFormatType(final RichInputMethodSubtype subtype) { public int getLanguageOnSpacebarFormatType(final RichInputMethodSubtype subtype) {
return mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(subtype); return mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(subtype);
} }

View File

@ -24,7 +24,7 @@ import android.preference.Preference;
import com.android.inputmethod.latin.AudioAndHapticFeedbackManager; import com.android.inputmethod.latin.AudioAndHapticFeedbackManager;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SubtypeSwitcher; import com.android.inputmethod.latin.RichInputMethodManager;
/** /**
* "Preferences" settings sub screen. * "Preferences" settings sub screen.
@ -49,7 +49,7 @@ public final class PreferencesSettingsFragment extends SubScreenFragment {
// When we are called from the Settings application but we are not already running, some // When we are called from the Settings application but we are not already running, some
// singleton and utility classes may not have been initialized. We have to call // singleton and utility classes may not have been initialized. We have to call
// initialization method of these classes here. See {@link LatinIME#onCreate()}. // initialization method of these classes here. See {@link LatinIME#onCreate()}.
SubtypeSwitcher.init(context); RichInputMethodManager.init(context);
final boolean showVoiceKeyOption = res.getBoolean( final boolean showVoiceKeyOption = res.getBoolean(
R.bool.config_enable_show_voice_key_option); R.bool.config_enable_show_voice_key_option);
@ -71,7 +71,7 @@ public final class PreferencesSettingsFragment extends SubScreenFragment {
super.onResume(); super.onResume();
final Preference voiceInputKeyOption = findPreference(Settings.PREF_VOICE_INPUT_KEY); final Preference voiceInputKeyOption = findPreference(Settings.PREF_VOICE_INPUT_KEY);
if (voiceInputKeyOption != null) { if (voiceInputKeyOption != null) {
final boolean isShortcutImeEnabled = SubtypeSwitcher.getInstance() final boolean isShortcutImeEnabled = RichInputMethodManager.getInstance()
.isShortcutImeEnabled(); .isShortcutImeEnabled();
voiceInputKeyOption.setEnabled(isShortcutImeEnabled); voiceInputKeyOption.setEnabled(isShortcutImeEnabled);
voiceInputKeyOption.setSummary( voiceInputKeyOption.setSummary(

View File

@ -28,7 +28,6 @@ import com.android.inputmethod.compat.AppWorkaroundsUtils;
import com.android.inputmethod.latin.InputAttributes; import com.android.inputmethod.latin.InputAttributes;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.RichInputMethodManager; import com.android.inputmethod.latin.RichInputMethodManager;
import com.android.inputmethod.latin.SubtypeSwitcher;
import com.android.inputmethod.latin.utils.AsyncResultHolder; import com.android.inputmethod.latin.utils.AsyncResultHolder;
import com.android.inputmethod.latin.utils.ResourceUtils; import com.android.inputmethod.latin.utils.ResourceUtils;
import com.android.inputmethod.latin.utils.TargetPackageInfoGetterTask; import com.android.inputmethod.latin.utils.TargetPackageInfoGetterTask;
@ -140,7 +139,7 @@ public class SettingsValues {
DebugSettings.PREF_SLIDING_KEY_INPUT_PREVIEW, true); DebugSettings.PREF_SLIDING_KEY_INPUT_PREVIEW, true);
mShowsVoiceInputKey = needsToShowVoiceInputKey(prefs, res) mShowsVoiceInputKey = needsToShowVoiceInputKey(prefs, res)
&& mInputAttributes.mShouldShowVoiceInputKey && mInputAttributes.mShouldShowVoiceInputKey
&& SubtypeSwitcher.getInstance().isShortcutImeEnabled(); && RichInputMethodManager.getInstance().isShortcutImeEnabled();
final String autoCorrectionThresholdRawValue = prefs.getString( final String autoCorrectionThresholdRawValue = prefs.getString(
Settings.PREF_AUTO_CORRECTION_THRESHOLD, Settings.PREF_AUTO_CORRECTION_THRESHOLD,
res.getString(R.string.auto_correction_threshold_mode_index_modest)); res.getString(R.string.auto_correction_threshold_mode_index_modest));