Merge "Consolidate VibratorUtils with AudioAndHapticFeedbackManager"

main
Tadashi G. Takaoka 2013-01-07 23:00:04 -08:00 committed by Android (Google) Code Review
commit 4b852d6044
6 changed files with 58 additions and 83 deletions

View File

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

View File

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

View File

@ -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<SettingsValues> job = new RunInLocale<SettingsValues>() {
@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());

View File

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

View File

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

View File

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