Consolidate VibratorUtils with AudioAndHapticFeedbackManager
Change-Id: Ica4e3193f369330da62a259c718b779ddf9fbde5main
parent
28de7223e6
commit
b17b887425
|
@ -66,7 +66,8 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
|
||||||
new KeyboardTheme(5, R.style.KeyboardTheme_IceCreamSandwich),
|
new KeyboardTheme(5, R.style.KeyboardTheme_IceCreamSandwich),
|
||||||
};
|
};
|
||||||
|
|
||||||
private AudioAndHapticFeedbackManager mFeedbackManager;
|
private final AudioAndHapticFeedbackManager mFeedbackManager =
|
||||||
|
AudioAndHapticFeedbackManager.getInstance();
|
||||||
private SubtypeSwitcher mSubtypeSwitcher;
|
private SubtypeSwitcher mSubtypeSwitcher;
|
||||||
private SharedPreferences mPrefs;
|
private SharedPreferences mPrefs;
|
||||||
|
|
||||||
|
@ -104,7 +105,6 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
|
||||||
private void initInternal(final LatinIME latinIme, final 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);
|
|
||||||
mPrefs = prefs;
|
mPrefs = prefs;
|
||||||
mSubtypeSwitcher = SubtypeSwitcher.getInstance();
|
mSubtypeSwitcher = SubtypeSwitcher.getInstance();
|
||||||
mState = new KeyboardState(this);
|
mState = new KeyboardState(this);
|
||||||
|
|
|
@ -18,11 +18,10 @@ package com.android.inputmethod.latin;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
|
import android.os.Vibrator;
|
||||||
import android.view.HapticFeedbackConstants;
|
import android.view.HapticFeedbackConstants;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import com.android.inputmethod.latin.VibratorUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class gathers audio feedback and haptic feedback functions.
|
* This class gathers audio feedback and haptic feedback functions.
|
||||||
*
|
*
|
||||||
|
@ -32,34 +31,61 @@ import com.android.inputmethod.latin.VibratorUtils;
|
||||||
public final class AudioAndHapticFeedbackManager {
|
public final class AudioAndHapticFeedbackManager {
|
||||||
public static final int MAX_KEYPRESS_VIBRATION_DURATION = 250; // millisecond
|
public static final int MAX_KEYPRESS_VIBRATION_DURATION = 250; // millisecond
|
||||||
|
|
||||||
private final AudioManager mAudioManager;
|
private AudioManager mAudioManager;
|
||||||
private final VibratorUtils mVibratorUtils;
|
private Vibrator mVibrator;
|
||||||
|
|
||||||
private SettingsValues mSettingsValues;
|
private SettingsValues mSettingsValues;
|
||||||
private boolean mSoundOn;
|
private boolean mSoundOn;
|
||||||
|
|
||||||
public AudioAndHapticFeedbackManager(final LatinIME latinIme) {
|
private static final AudioAndHapticFeedbackManager sInstance =
|
||||||
mVibratorUtils = VibratorUtils.getInstance(latinIme);
|
new AudioAndHapticFeedbackManager();
|
||||||
mAudioManager = (AudioManager) latinIme.getSystemService(Context.AUDIO_SERVICE);
|
|
||||||
|
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,
|
public void hapticAndAudioFeedback(final int primaryCode,
|
||||||
final View viewToPerformHapticFeedbackOn) {
|
final View viewToPerformHapticFeedbackOn) {
|
||||||
vibrate(viewToPerformHapticFeedbackOn);
|
vibrateInternal(viewToPerformHapticFeedbackOn);
|
||||||
playKeyClick(primaryCode);
|
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() {
|
private boolean reevaluateIfSoundIsOn() {
|
||||||
if (mSettingsValues == null || !mSettingsValues.mSoundOn || mAudioManager == null) {
|
if (mSettingsValues == null || !mSettingsValues.mSoundOn || mAudioManager == null) {
|
||||||
return false;
|
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 is null, we can't play a sound anyway, so return
|
||||||
if (mAudioManager == null) return;
|
if (mAudioManager == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (mSoundOn) {
|
if (mSoundOn) {
|
||||||
final int sound;
|
final int sound;
|
||||||
switch (primaryCode) {
|
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) {
|
if (!mSettingsValues.mVibrateOn) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -91,9 +117,9 @@ public final class AudioAndHapticFeedbackManager {
|
||||||
HapticFeedbackConstants.KEYBOARD_TAP,
|
HapticFeedbackConstants.KEYBOARD_TAP,
|
||||||
HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
|
HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
|
||||||
}
|
}
|
||||||
} else if (mVibratorUtils != null) {
|
return;
|
||||||
mVibratorUtils.vibrate(mSettingsValues.mKeypressVibrationDuration);
|
|
||||||
}
|
}
|
||||||
|
vibrate(mSettingsValues.mKeypressVibrationDuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onSettingsChanged(final SettingsValues settingsValues) {
|
public void onSettingsChanged(final SettingsValues settingsValues) {
|
||||||
|
|
|
@ -421,6 +421,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
|
||||||
mRichImm = RichInputMethodManager.getInstance();
|
mRichImm = RichInputMethodManager.getInstance();
|
||||||
SubtypeSwitcher.init(this);
|
SubtypeSwitcher.init(this);
|
||||||
KeyboardSwitcher.init(this);
|
KeyboardSwitcher.init(this);
|
||||||
|
AudioAndHapticFeedbackManager.init(this);
|
||||||
AccessibilityUtils.init(this);
|
AccessibilityUtils.init(this);
|
||||||
|
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
@ -461,12 +462,13 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
|
||||||
// Note that the calling sequence of onCreate() and onCurrentInputMethodSubtypeChanged()
|
// 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.
|
// is not guaranteed. It may even be called at the same time on a different thread.
|
||||||
if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
|
final SharedPreferences prefs = mPrefs;
|
||||||
final InputAttributes inputAttributes =
|
final InputAttributes inputAttributes =
|
||||||
new InputAttributes(getCurrentInputEditorInfo(), isFullscreenMode());
|
new InputAttributes(getCurrentInputEditorInfo(), isFullscreenMode());
|
||||||
final RunInLocale<SettingsValues> job = new RunInLocale<SettingsValues>() {
|
final RunInLocale<SettingsValues> job = new RunInLocale<SettingsValues>() {
|
||||||
@Override
|
@Override
|
||||||
protected SettingsValues job(Resources res) {
|
protected SettingsValues job(final Resources res) {
|
||||||
return new SettingsValues(mPrefs, inputAttributes, LatinIME.this);
|
return new SettingsValues(prefs, res, inputAttributes);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
mCurrentSettings = job.runInLocale(mResources, mSubtypeSwitcher.getCurrentSubtypeLocale());
|
mCurrentSettings = job.runInLocale(mResources, mSubtypeSwitcher.getCurrentSubtypeLocale());
|
||||||
|
|
|
@ -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} class may not have been initialized. It is safe to call
|
||||||
// {@link SubtypeLocale#init(Context)} multiple times.
|
// {@link SubtypeLocale#init(Context)} multiple times.
|
||||||
SubtypeLocale.init(context);
|
SubtypeLocale.init(context);
|
||||||
|
AudioAndHapticFeedbackManager.init(context);
|
||||||
mVoicePreference = (ListPreference) findPreference(PREF_VOICE_MODE);
|
mVoicePreference = (ListPreference) findPreference(PREF_VOICE_MODE);
|
||||||
mShowCorrectionSuggestionsPreference =
|
mShowCorrectionSuggestionsPreference =
|
||||||
(ListPreference) findPreference(PREF_SHOW_SUGGESTIONS_SETTING);
|
(ListPreference) findPreference(PREF_SHOW_SUGGESTIONS_SETTING);
|
||||||
|
@ -154,7 +155,7 @@ public final class Settings extends InputMethodSettingsFragment
|
||||||
|
|
||||||
final PreferenceGroup advancedSettings =
|
final PreferenceGroup advancedSettings =
|
||||||
(PreferenceGroup) findPreference(PREF_ADVANCED_SETTINGS);
|
(PreferenceGroup) findPreference(PREF_ADVANCED_SETTINGS);
|
||||||
if (!VibratorUtils.getInstance(context).hasVibrator()) {
|
if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) {
|
||||||
generalSettings.removePreference(findPreference(PREF_VIBRATE_ON));
|
generalSettings.removePreference(findPreference(PREF_VIBRATE_ON));
|
||||||
if (null != advancedSettings) { // Theoretically advancedSettings cannot be null
|
if (null != advancedSettings) { // Theoretically advancedSettings cannot be null
|
||||||
advancedSettings.removePreference(findPreference(PREF_VIBRATION_DURATION_SETTINGS));
|
advancedSettings.removePreference(findPreference(PREF_VIBRATION_DURATION_SETTINGS));
|
||||||
|
@ -327,8 +328,8 @@ public final class Settings extends InputMethodSettingsFragment
|
||||||
private void refreshEnablingsOfKeypressSoundAndVibrationSettings(
|
private void refreshEnablingsOfKeypressSoundAndVibrationSettings(
|
||||||
final SharedPreferences sp, final Resources res) {
|
final SharedPreferences sp, final Resources res) {
|
||||||
if (mKeypressVibrationDurationSettingsPref != null) {
|
if (mKeypressVibrationDurationSettingsPref != null) {
|
||||||
final boolean hasVibratorHardware = VibratorUtils.getInstance(getActivity())
|
final boolean hasVibratorHardware =
|
||||||
.hasVibrator();
|
AudioAndHapticFeedbackManager.getInstance().hasVibrator();
|
||||||
final boolean vibrateOnByUser = sp.getBoolean(Settings.PREF_VIBRATE_ON,
|
final boolean vibrateOnByUser = sp.getBoolean(Settings.PREF_VIBRATE_ON,
|
||||||
res.getBoolean(R.bool.config_default_vibration_enabled));
|
res.getBoolean(R.bool.config_default_vibration_enabled));
|
||||||
setPreferenceEnabled(mKeypressVibrationDurationSettingsPref,
|
setPreferenceEnabled(mKeypressVibrationDurationSettingsPref,
|
||||||
|
@ -359,7 +360,7 @@ public final class Settings extends InputMethodSettingsFragment
|
||||||
@Override
|
@Override
|
||||||
public void onStopTrackingTouch(final SeekBarDialog dialog) {
|
public void onStopTrackingTouch(final SeekBarDialog dialog) {
|
||||||
final int ms = dialog.getValue();
|
final int ms = dialog.getValue();
|
||||||
VibratorUtils.getInstance(context).vibrate(ms);
|
AudioAndHapticFeedbackManager.getInstance().vibrate(ms);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
final int currentMs = SettingsValues.getCurrentVibrationDuration(sp, getResources());
|
final int currentMs = SettingsValues.getCurrentVibrationDuration(sp, getResources());
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
package com.android.inputmethod.latin;
|
package com.android.inputmethod.latin;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
@ -96,10 +95,8 @@ public final class SettingsValues {
|
||||||
private final boolean mVoiceKeyEnabled;
|
private final boolean mVoiceKeyEnabled;
|
||||||
private final boolean mVoiceKeyOnMain;
|
private final boolean mVoiceKeyOnMain;
|
||||||
|
|
||||||
public SettingsValues(final SharedPreferences prefs, final InputAttributes inputAttributes,
|
public SettingsValues(final SharedPreferences prefs, final Resources res,
|
||||||
final Context context) {
|
final InputAttributes inputAttributes) {
|
||||||
final Resources res = context.getResources();
|
|
||||||
|
|
||||||
// Get the resources
|
// Get the resources
|
||||||
mDelayUpdateOldSuggestions = res.getInteger(R.integer.config_delay_update_old_suggestions);
|
mDelayUpdateOldSuggestions = res.getInteger(R.integer.config_delay_update_old_suggestions);
|
||||||
mWeakSpaceStrippers = res.getString(R.string.weak_space_stripping_symbols);
|
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);
|
res.getString(R.string.symbols_excluded_from_word_separators);
|
||||||
mWordSeparators = createWordSeparators(mWeakSpaceStrippers, mWeakSpaceSwappers,
|
mWordSeparators = createWordSeparators(mWeakSpaceStrippers, mWeakSpaceSwappers,
|
||||||
mSymbolsExcludedFromWordSeparators, res);
|
mSymbolsExcludedFromWordSeparators, res);
|
||||||
mHintToSaveText = context.getText(R.string.hint_add_to_dictionary);
|
mHintToSaveText = res.getText(R.string.hint_add_to_dictionary);
|
||||||
|
|
||||||
// Store the input attributes
|
// Store the input attributes
|
||||||
if (null == inputAttributes) {
|
if (null == inputAttributes) {
|
||||||
|
@ -132,7 +129,7 @@ public final class SettingsValues {
|
||||||
|
|
||||||
// Get the settings preferences
|
// Get the settings preferences
|
||||||
mAutoCap = prefs.getBoolean(Settings.PREF_AUTO_CAP, true);
|
mAutoCap = prefs.getBoolean(Settings.PREF_AUTO_CAP, true);
|
||||||
mVibrateOn = isVibrateOn(context, prefs, res);
|
mVibrateOn = isVibrateOn(prefs, res);
|
||||||
mSoundOn = prefs.getBoolean(Settings.PREF_SOUND_ON,
|
mSoundOn = prefs.getBoolean(Settings.PREF_SOUND_ON,
|
||||||
res.getBoolean(R.bool.config_default_sound_enabled));
|
res.getBoolean(R.bool.config_default_sound_enabled));
|
||||||
mKeyPreviewPopupOn = isKeyPreviewPopupEnabled(prefs, res);
|
mKeyPreviewPopupOn = isKeyPreviewPopupEnabled(prefs, res);
|
||||||
|
@ -214,9 +211,8 @@ public final class SettingsValues {
|
||||||
throw new RuntimeException("Bug: visibility string is not configured correctly");
|
throw new RuntimeException("Bug: visibility string is not configured correctly");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isVibrateOn(final Context context, final SharedPreferences prefs,
|
private static boolean isVibrateOn(final SharedPreferences prefs, final Resources res) {
|
||||||
final Resources res) {
|
final boolean hasVibrator = AudioAndHapticFeedbackManager.getInstance().hasVibrator();
|
||||||
final boolean hasVibrator = VibratorUtils.getInstance(context).hasVibrator();
|
|
||||||
return hasVibrator && prefs.getBoolean(Settings.PREF_VIBRATE_ON,
|
return hasVibrator && prefs.getBoolean(Settings.PREF_VIBRATE_ON,
|
||||||
res.getBoolean(R.bool.config_default_vibration_enabled));
|
res.getBoolean(R.bool.config_default_vibration_enabled));
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue