From b17b88742528d09eaa36adb3dae5348b99c234e0 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Tue, 8 Jan 2013 14:58:08 +0900 Subject: [PATCH] Consolidate VibratorUtils with AudioAndHapticFeedbackManager Change-Id: Ica4e3193f369330da62a259c718b779ddf9fbde5 --- .../keyboard/KeyboardSwitcher.java | 4 +- .../latin/AudioAndHapticFeedbackManager.java | 56 ++++++++++++++----- .../android/inputmethod/latin/LatinIME.java | 6 +- .../android/inputmethod/latin/Settings.java | 9 +-- .../inputmethod/latin/SettingsValues.java | 16 ++---- .../inputmethod/latin/VibratorUtils.java | 50 ----------------- 6 files changed, 58 insertions(+), 83 deletions(-) delete mode 100644 java/src/com/android/inputmethod/latin/VibratorUtils.java diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index 51517823a..1088fdab4 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -66,7 +66,8 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { new KeyboardTheme(5, R.style.KeyboardTheme_IceCreamSandwich), }; - private AudioAndHapticFeedbackManager mFeedbackManager; + private final AudioAndHapticFeedbackManager mFeedbackManager = + AudioAndHapticFeedbackManager.getInstance(); private SubtypeSwitcher mSubtypeSwitcher; private SharedPreferences mPrefs; @@ -104,7 +105,6 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { private void initInternal(final LatinIME latinIme, final SharedPreferences prefs) { mLatinIME = latinIme; mResources = latinIme.getResources(); - mFeedbackManager = new AudioAndHapticFeedbackManager(latinIme); mPrefs = prefs; mSubtypeSwitcher = SubtypeSwitcher.getInstance(); mState = new KeyboardState(this); diff --git a/java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java b/java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java index 0e7f891ff..6367156ef 100644 --- a/java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java +++ b/java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java @@ -18,11 +18,10 @@ package com.android.inputmethod.latin; import android.content.Context; import android.media.AudioManager; +import android.os.Vibrator; import android.view.HapticFeedbackConstants; import android.view.View; -import com.android.inputmethod.latin.VibratorUtils; - /** * This class gathers audio feedback and haptic feedback functions. * @@ -32,34 +31,61 @@ import com.android.inputmethod.latin.VibratorUtils; public final class AudioAndHapticFeedbackManager { public static final int MAX_KEYPRESS_VIBRATION_DURATION = 250; // millisecond - private final AudioManager mAudioManager; - private final VibratorUtils mVibratorUtils; + private AudioManager mAudioManager; + private Vibrator mVibrator; private SettingsValues mSettingsValues; private boolean mSoundOn; - public AudioAndHapticFeedbackManager(final LatinIME latinIme) { - mVibratorUtils = VibratorUtils.getInstance(latinIme); - mAudioManager = (AudioManager) latinIme.getSystemService(Context.AUDIO_SERVICE); + private static final AudioAndHapticFeedbackManager sInstance = + new AudioAndHapticFeedbackManager(); + + public static AudioAndHapticFeedbackManager getInstance() { + return sInstance; + } + + private AudioAndHapticFeedbackManager() { + // Intentional empty constructor for singleton. + } + + public static void init(final Context context) { + sInstance.initInternal(context); + } + + private void initInternal(final Context context) { + mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); } public void hapticAndAudioFeedback(final int primaryCode, final View viewToPerformHapticFeedbackOn) { - vibrate(viewToPerformHapticFeedbackOn); + vibrateInternal(viewToPerformHapticFeedbackOn); playKeyClick(primaryCode); } + public boolean hasVibrator() { + return mVibrator != null && mVibrator.hasVibrator(); + } + + public void vibrate(final long milliseconds) { + if (mVibrator == null) { + return; + } + mVibrator.vibrate(milliseconds); + } + private boolean reevaluateIfSoundIsOn() { if (mSettingsValues == null || !mSettingsValues.mSoundOn || mAudioManager == null) { return false; - } else { - return mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL; } + return mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL; } - private void playKeyClick(int primaryCode) { + private void playKeyClick(final int primaryCode) { // if mAudioManager is null, we can't play a sound anyway, so return - if (mAudioManager == null) return; + if (mAudioManager == null) { + return; + } if (mSoundOn) { final int sound; switch (primaryCode) { @@ -80,7 +106,7 @@ public final class AudioAndHapticFeedbackManager { } } - private void vibrate(final View viewToPerformHapticFeedbackOn) { + private void vibrateInternal(final View viewToPerformHapticFeedbackOn) { if (!mSettingsValues.mVibrateOn) { return; } @@ -91,9 +117,9 @@ public final class AudioAndHapticFeedbackManager { HapticFeedbackConstants.KEYBOARD_TAP, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); } - } else if (mVibratorUtils != null) { - mVibratorUtils.vibrate(mSettingsValues.mKeypressVibrationDuration); + return; } + vibrate(mSettingsValues.mKeypressVibrationDuration); } public void onSettingsChanged(final SettingsValues settingsValues) { diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index f65bbe4a1..eeb08d942 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -421,6 +421,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction mRichImm = RichInputMethodManager.getInstance(); SubtypeSwitcher.init(this); KeyboardSwitcher.init(this); + AudioAndHapticFeedbackManager.init(this); AccessibilityUtils.init(this); super.onCreate(); @@ -461,12 +462,13 @@ public final class LatinIME extends InputMethodService implements KeyboardAction // Note that the calling sequence of onCreate() and onCurrentInputMethodSubtypeChanged() // is not guaranteed. It may even be called at the same time on a different thread. if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this); + final SharedPreferences prefs = mPrefs; final InputAttributes inputAttributes = new InputAttributes(getCurrentInputEditorInfo(), isFullscreenMode()); final RunInLocale job = new RunInLocale() { @Override - protected SettingsValues job(Resources res) { - return new SettingsValues(mPrefs, inputAttributes, LatinIME.this); + protected SettingsValues job(final Resources res) { + return new SettingsValues(prefs, res, inputAttributes); } }; mCurrentSettings = job.runInLocale(mResources, mSubtypeSwitcher.getCurrentSubtypeLocale()); diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java index 222adcb2e..9ed35f24c 100644 --- a/java/src/com/android/inputmethod/latin/Settings.java +++ b/java/src/com/android/inputmethod/latin/Settings.java @@ -114,6 +114,7 @@ public final class Settings extends InputMethodSettingsFragment // {@link SubtypeLocale} class may not have been initialized. It is safe to call // {@link SubtypeLocale#init(Context)} multiple times. SubtypeLocale.init(context); + AudioAndHapticFeedbackManager.init(context); mVoicePreference = (ListPreference) findPreference(PREF_VOICE_MODE); mShowCorrectionSuggestionsPreference = (ListPreference) findPreference(PREF_SHOW_SUGGESTIONS_SETTING); @@ -154,7 +155,7 @@ public final class Settings extends InputMethodSettingsFragment final PreferenceGroup advancedSettings = (PreferenceGroup) findPreference(PREF_ADVANCED_SETTINGS); - if (!VibratorUtils.getInstance(context).hasVibrator()) { + if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) { generalSettings.removePreference(findPreference(PREF_VIBRATE_ON)); if (null != advancedSettings) { // Theoretically advancedSettings cannot be null advancedSettings.removePreference(findPreference(PREF_VIBRATION_DURATION_SETTINGS)); @@ -327,8 +328,8 @@ public final class Settings extends InputMethodSettingsFragment private void refreshEnablingsOfKeypressSoundAndVibrationSettings( final SharedPreferences sp, final Resources res) { if (mKeypressVibrationDurationSettingsPref != null) { - final boolean hasVibratorHardware = VibratorUtils.getInstance(getActivity()) - .hasVibrator(); + final boolean hasVibratorHardware = + AudioAndHapticFeedbackManager.getInstance().hasVibrator(); final boolean vibrateOnByUser = sp.getBoolean(Settings.PREF_VIBRATE_ON, res.getBoolean(R.bool.config_default_vibration_enabled)); setPreferenceEnabled(mKeypressVibrationDurationSettingsPref, @@ -359,7 +360,7 @@ public final class Settings extends InputMethodSettingsFragment @Override public void onStopTrackingTouch(final SeekBarDialog dialog) { final int ms = dialog.getValue(); - VibratorUtils.getInstance(context).vibrate(ms); + AudioAndHapticFeedbackManager.getInstance().vibrate(ms); } }; final int currentMs = SettingsValues.getCurrentVibrationDuration(sp, getResources()); diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java index 157684437..c4ccb7d1a 100644 --- a/java/src/com/android/inputmethod/latin/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/SettingsValues.java @@ -16,7 +16,6 @@ package com.android.inputmethod.latin; -import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; @@ -96,10 +95,8 @@ public final class SettingsValues { private final boolean mVoiceKeyEnabled; private final boolean mVoiceKeyOnMain; - public SettingsValues(final SharedPreferences prefs, final InputAttributes inputAttributes, - final Context context) { - final Resources res = context.getResources(); - + public SettingsValues(final SharedPreferences prefs, final Resources res, + final InputAttributes inputAttributes) { // Get the resources mDelayUpdateOldSuggestions = res.getInteger(R.integer.config_delay_update_old_suggestions); mWeakSpaceStrippers = res.getString(R.string.weak_space_stripping_symbols); @@ -121,7 +118,7 @@ public final class SettingsValues { res.getString(R.string.symbols_excluded_from_word_separators); mWordSeparators = createWordSeparators(mWeakSpaceStrippers, mWeakSpaceSwappers, mSymbolsExcludedFromWordSeparators, res); - mHintToSaveText = context.getText(R.string.hint_add_to_dictionary); + mHintToSaveText = res.getText(R.string.hint_add_to_dictionary); // Store the input attributes if (null == inputAttributes) { @@ -132,7 +129,7 @@ public final class SettingsValues { // Get the settings preferences mAutoCap = prefs.getBoolean(Settings.PREF_AUTO_CAP, true); - mVibrateOn = isVibrateOn(context, prefs, res); + mVibrateOn = isVibrateOn(prefs, res); mSoundOn = prefs.getBoolean(Settings.PREF_SOUND_ON, res.getBoolean(R.bool.config_default_sound_enabled)); mKeyPreviewPopupOn = isKeyPreviewPopupEnabled(prefs, res); @@ -214,9 +211,8 @@ public final class SettingsValues { throw new RuntimeException("Bug: visibility string is not configured correctly"); } - private static boolean isVibrateOn(final Context context, final SharedPreferences prefs, - final Resources res) { - final boolean hasVibrator = VibratorUtils.getInstance(context).hasVibrator(); + private static boolean isVibrateOn(final SharedPreferences prefs, final Resources res) { + final boolean hasVibrator = AudioAndHapticFeedbackManager.getInstance().hasVibrator(); return hasVibrator && prefs.getBoolean(Settings.PREF_VIBRATE_ON, res.getBoolean(R.bool.config_default_vibration_enabled)); } diff --git a/java/src/com/android/inputmethod/latin/VibratorUtils.java b/java/src/com/android/inputmethod/latin/VibratorUtils.java deleted file mode 100644 index b6696cec0..000000000 --- a/java/src/com/android/inputmethod/latin/VibratorUtils.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import android.content.Context; -import android.os.Vibrator; - -public final class VibratorUtils { - private static final VibratorUtils sInstance = new VibratorUtils(); - private Vibrator mVibrator; - - private VibratorUtils() { - // This utility class is not publicly instantiable. - } - - public static VibratorUtils getInstance(Context context) { - if (sInstance.mVibrator == null) { - sInstance.mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); - } - return sInstance; - } - - public boolean hasVibrator() { - if (mVibrator == null) { - return false; - } - return mVibrator.hasVibrator(); - } - - public void vibrate(long milliseconds) { - if (mVibrator == null) { - return; - } - mVibrator.vibrate(milliseconds); - } -}