Simplify singleton class initialization

Change-Id: I16a27f2ed6ea66184bfdc9903180372cd7ea2fd1
main
Tadashi G. Takaoka 2013-01-07 18:40:59 +09:00
parent b6ca354431
commit f90fc105ab
8 changed files with 31 additions and 28 deletions

View File

@ -19,6 +19,7 @@ package com.android.inputmethod.keyboard;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
@ -50,7 +51,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
// Note: The themeId should be aligned with "themeId" attribute of Keyboard style
// in values/style.xml.
public KeyboardTheme(int themeId, int styleId) {
public KeyboardTheme(final int themeId, final int styleId) {
mThemeId = themeId;
mStyleId = styleId;
}
@ -95,11 +96,12 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
// Intentional empty constructor for singleton.
}
public static void init(LatinIME latinIme, SharedPreferences prefs) {
public static void init(final LatinIME latinIme) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(latinIme);
sInstance.initInternal(latinIme, prefs);
}
private void initInternal(LatinIME latinIme, SharedPreferences prefs) {
private void initInternal(final LatinIME latinIme, final SharedPreferences prefs) {
mLatinIME = latinIme;
mResources = latinIme.getResources();
mFeedbackManager = new AudioAndHapticFeedbackManager(latinIme);
@ -109,7 +111,8 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
setContextThemeWrapper(latinIme, getKeyboardTheme(latinIme, prefs));
}
private static KeyboardTheme getKeyboardTheme(Context context, SharedPreferences prefs) {
private static KeyboardTheme getKeyboardTheme(final Context context,
final SharedPreferences prefs) {
final String defaultIndex = context.getString(R.string.config_default_keyboard_theme_index);
final String themeIndex = prefs.getString(PREF_KEYBOARD_LAYOUT, defaultIndex);
try {
@ -124,7 +127,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
return KEYBOARD_THEMES[0];
}
private void setContextThemeWrapper(Context context, KeyboardTheme keyboardTheme) {
private void setContextThemeWrapper(final Context context, final KeyboardTheme keyboardTheme) {
if (mThemeContext == null || mKeyboardTheme.mThemeId != keyboardTheme.mThemeId) {
mKeyboardTheme = keyboardTheme;
mThemeContext = new ContextThemeWrapper(context, keyboardTheme.mStyleId);
@ -132,7 +135,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
}
}
public void loadKeyboard(EditorInfo editorInfo, SettingsValues settingsValues) {
public void loadKeyboard(final EditorInfo editorInfo, final SettingsValues settingsValues) {
final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(
mThemeContext, editorInfo);
final Resources res = mThemeContext.getResources();
@ -210,14 +213,14 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
mState.onResetKeyboardStateToAlphabet();
}
public void onPressKey(int code) {
public void onPressKey(final int code) {
if (isVibrateAndSoundFeedbackRequired()) {
mFeedbackManager.hapticAndAudioFeedback(code, mKeyboardView);
}
mState.onPressKey(code, isSinglePointer(), mLatinIME.getCurrentAutoCapsState());
}
public void onReleaseKey(int code, boolean withSliding) {
public void onReleaseKey(final int code, final boolean withSliding) {
mState.onReleaseKey(code, withSliding);
}
@ -303,7 +306,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
// Implements {@link KeyboardState.SwitchActions}.
@Override
public void startLongPressTimer(int code) {
public void startLongPressTimer(final int code) {
final MainKeyboardView keyboardView = getMainKeyboardView();
if (keyboardView != null) {
final TimerProxy timer = keyboardView.getTimerProxy();
@ -323,11 +326,11 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
// Implements {@link KeyboardState.SwitchActions}.
@Override
public void hapticAndAudioFeedback(int code) {
public void hapticAndAudioFeedback(final int code) {
mFeedbackManager.hapticAndAudioFeedback(code, mKeyboardView);
}
public void onLongPressTimeout(int code) {
public void onLongPressTimeout(final int code) {
mState.onLongPressTimeout(code);
}
@ -346,7 +349,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
/**
* Updates state machine to figure out when to automatically switch back to the previous mode.
*/
public void onCodeInput(int code) {
public void onCodeInput(final int code) {
mState.onCodeInput(code, isSinglePointer(), mLatinIME.getCurrentAutoCapsState());
}
@ -354,7 +357,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
return mKeyboardView;
}
public View onCreateInputView(boolean isHardwareAcceleratedDrawingEnabled) {
public View onCreateInputView(final boolean isHardwareAcceleratedDrawingEnabled) {
if (mKeyboardView != null) {
mKeyboardView.closing();
}
@ -383,7 +386,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
}
}
public void onAutoCorrectionStateChanged(boolean isAutoCorrection) {
public void onAutoCorrectionStateChanged(final boolean isAutoCorrection) {
if (mIsAutoCorrectionActive != isAutoCorrection) {
mIsAutoCorrectionActive = isAutoCorrection;
if (mKeyboardView != null) {

View File

@ -387,7 +387,7 @@ public final class AdditionalSubtypeSettings extends PreferenceFragment {
super.onCreate(savedInstanceState);
mPrefs = getPreferenceManager().getSharedPreferences();
RichInputMethodManager.init(getActivity(), mPrefs);
RichInputMethodManager.init(getActivity());
mRichImm = RichInputMethodManager.getInstance();
addPreferencesFromResource(R.xml.additional_subtype_settings);
setHasOptionsMenu(true);

View File

@ -413,14 +413,14 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mResources = getResources();
LatinImeLogger.init(this, mPrefs);
LatinImeLogger.init(this);
if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.getInstance().init(this, mPrefs);
ResearchLogger.getInstance().init(this);
}
RichInputMethodManager.init(this, mPrefs);
RichInputMethodManager.init(this);
mRichImm = RichInputMethodManager.getInstance();
SubtypeSwitcher.init(this);
KeyboardSwitcher.init(this, mPrefs);
KeyboardSwitcher.init(this);
AccessibilityUtils.init(this);
super.onCreate();

View File

@ -31,7 +31,7 @@ public final class LatinImeLogger implements SharedPreferences.OnSharedPreferenc
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
}
public static void init(LatinIME context, SharedPreferences prefs) {
public static void init(LatinIME context) {
}
public static void commit() {

View File

@ -21,6 +21,7 @@ import static com.android.inputmethod.latin.Constants.Subtype.KEYBOARD_MODE;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
@ -50,7 +51,8 @@ public final class RichInputMethodManager {
return sInstance;
}
public static void init(final Context context, final SharedPreferences prefs) {
public static void init(final Context context) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
sInstance.initInternal(context, prefs);
}

View File

@ -38,6 +38,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
@ -161,7 +162,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
return sInstance;
}
public void init(final LatinIME latinIME, final SharedPreferences prefs) {
public void init(final LatinIME latinIME) {
assert latinIME != null;
if (latinIME == null) {
Log.w(TAG, "IMS is null; logging is off");
@ -171,6 +172,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
Log.w(TAG, "IME storage directory does not exist.");
}
}
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(latinIME);
if (prefs != null) {
mUUIDString = getUUID(prefs);
if (!prefs.contains(PREF_USABILITY_STUDY_MODE)) {

View File

@ -18,7 +18,6 @@ package com.android.inputmethod.keyboard;
import android.content.Context;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;
import android.view.inputmethod.InputMethodSubtype;
@ -42,8 +41,7 @@ public class SpacebarTextTests extends AndroidTestCase {
protected void setUp() throws Exception {
super.setUp();
final Context context = getContext();
RichInputMethodManager.init(
context, PreferenceManager.getDefaultSharedPreferences(context));
RichInputMethodManager.init(context);
mRichImm = RichInputMethodManager.getInstance();
mRes = context.getResources();
SubtypeLocale.init(context);

View File

@ -18,7 +18,6 @@ package com.android.inputmethod.latin;
import android.content.Context;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;
import android.view.inputmethod.InputMethodSubtype;
@ -38,8 +37,7 @@ public class SubtypeLocaleTests extends AndroidTestCase {
protected void setUp() throws Exception {
super.setUp();
final Context context = getContext();
RichInputMethodManager.init(
context, PreferenceManager.getDefaultSharedPreferences(context));
RichInputMethodManager.init(context);
mRichImm = RichInputMethodManager.getInstance();
mRes = context.getResources();
SubtypeLocale.init(context);