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.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import android.view.LayoutInflater; 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 // Note: The themeId should be aligned with "themeId" attribute of Keyboard style
// in values/style.xml. // in values/style.xml.
public KeyboardTheme(int themeId, int styleId) { public KeyboardTheme(final int themeId, final int styleId) {
mThemeId = themeId; mThemeId = themeId;
mStyleId = styleId; mStyleId = styleId;
} }
@ -95,11 +96,12 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
// Intentional empty constructor for singleton. // 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); sInstance.initInternal(latinIme, prefs);
} }
private void initInternal(LatinIME latinIme, SharedPreferences prefs) { private void initInternal(final LatinIME latinIme, final SharedPreferences prefs) {
mLatinIME = latinIme; mLatinIME = latinIme;
mResources = latinIme.getResources(); mResources = latinIme.getResources();
mFeedbackManager = new AudioAndHapticFeedbackManager(latinIme); mFeedbackManager = new AudioAndHapticFeedbackManager(latinIme);
@ -109,7 +111,8 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
setContextThemeWrapper(latinIme, getKeyboardTheme(latinIme, prefs)); 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 defaultIndex = context.getString(R.string.config_default_keyboard_theme_index);
final String themeIndex = prefs.getString(PREF_KEYBOARD_LAYOUT, defaultIndex); final String themeIndex = prefs.getString(PREF_KEYBOARD_LAYOUT, defaultIndex);
try { try {
@ -124,7 +127,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
return KEYBOARD_THEMES[0]; 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) { if (mThemeContext == null || mKeyboardTheme.mThemeId != keyboardTheme.mThemeId) {
mKeyboardTheme = keyboardTheme; mKeyboardTheme = keyboardTheme;
mThemeContext = new ContextThemeWrapper(context, keyboardTheme.mStyleId); 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( final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(
mThemeContext, editorInfo); mThemeContext, editorInfo);
final Resources res = mThemeContext.getResources(); final Resources res = mThemeContext.getResources();
@ -210,14 +213,14 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
mState.onResetKeyboardStateToAlphabet(); mState.onResetKeyboardStateToAlphabet();
} }
public void onPressKey(int code) { public void onPressKey(final int code) {
if (isVibrateAndSoundFeedbackRequired()) { if (isVibrateAndSoundFeedbackRequired()) {
mFeedbackManager.hapticAndAudioFeedback(code, mKeyboardView); mFeedbackManager.hapticAndAudioFeedback(code, mKeyboardView);
} }
mState.onPressKey(code, isSinglePointer(), mLatinIME.getCurrentAutoCapsState()); 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); mState.onReleaseKey(code, withSliding);
} }
@ -303,7 +306,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
// Implements {@link KeyboardState.SwitchActions}. // Implements {@link KeyboardState.SwitchActions}.
@Override @Override
public void startLongPressTimer(int code) { public void startLongPressTimer(final int code) {
final MainKeyboardView keyboardView = getMainKeyboardView(); final MainKeyboardView keyboardView = getMainKeyboardView();
if (keyboardView != null) { if (keyboardView != null) {
final TimerProxy timer = keyboardView.getTimerProxy(); final TimerProxy timer = keyboardView.getTimerProxy();
@ -323,11 +326,11 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
// Implements {@link KeyboardState.SwitchActions}. // Implements {@link KeyboardState.SwitchActions}.
@Override @Override
public void hapticAndAudioFeedback(int code) { public void hapticAndAudioFeedback(final int code) {
mFeedbackManager.hapticAndAudioFeedback(code, mKeyboardView); mFeedbackManager.hapticAndAudioFeedback(code, mKeyboardView);
} }
public void onLongPressTimeout(int code) { public void onLongPressTimeout(final int code) {
mState.onLongPressTimeout(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. * 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()); mState.onCodeInput(code, isSinglePointer(), mLatinIME.getCurrentAutoCapsState());
} }
@ -354,7 +357,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
return mKeyboardView; return mKeyboardView;
} }
public View onCreateInputView(boolean isHardwareAcceleratedDrawingEnabled) { public View onCreateInputView(final boolean isHardwareAcceleratedDrawingEnabled) {
if (mKeyboardView != null) { if (mKeyboardView != null) {
mKeyboardView.closing(); 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) { if (mIsAutoCorrectionActive != isAutoCorrection) {
mIsAutoCorrectionActive = isAutoCorrection; mIsAutoCorrectionActive = isAutoCorrection;
if (mKeyboardView != null) { if (mKeyboardView != null) {

View File

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

View File

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

View File

@ -31,7 +31,7 @@ public final class LatinImeLogger implements SharedPreferences.OnSharedPreferenc
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 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() { 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.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager;
import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
@ -50,7 +51,8 @@ public final class RichInputMethodManager {
return sInstance; 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); sInstance.initInternal(context, prefs);
} }

View File

@ -38,6 +38,7 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.util.Log; import android.util.Log;
@ -161,7 +162,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
return sInstance; return sInstance;
} }
public void init(final LatinIME latinIME, final SharedPreferences prefs) { public void init(final LatinIME latinIME) {
assert latinIME != null; assert latinIME != null;
if (latinIME == null) { if (latinIME == null) {
Log.w(TAG, "IMS is null; logging is off"); 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."); Log.w(TAG, "IME storage directory does not exist.");
} }
} }
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(latinIME);
if (prefs != null) { if (prefs != null) {
mUUIDString = getUUID(prefs); mUUIDString = getUUID(prefs);
if (!prefs.contains(PREF_USABILITY_STUDY_MODE)) { 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.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
@ -42,8 +41,7 @@ public class SpacebarTextTests extends AndroidTestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
final Context context = getContext(); final Context context = getContext();
RichInputMethodManager.init( RichInputMethodManager.init(context);
context, PreferenceManager.getDefaultSharedPreferences(context));
mRichImm = RichInputMethodManager.getInstance(); mRichImm = RichInputMethodManager.getInstance();
mRes = context.getResources(); mRes = context.getResources();
SubtypeLocale.init(context); SubtypeLocale.init(context);

View File

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