Loading java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +2 −2 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); Loading java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java +41 −15 Original line number Diff line number Diff line Loading @@ -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. * Loading @@ -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) { Loading @@ -80,7 +106,7 @@ public final class AudioAndHapticFeedbackManager { } } private void vibrate(final View viewToPerformHapticFeedbackOn) { private void vibrateInternal(final View viewToPerformHapticFeedbackOn) { if (!mSettingsValues.mVibrateOn) { return; } Loading @@ -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) { Loading java/src/com/android/inputmethod/latin/LatinIME.java +4 −2 Original line number Diff line number Diff line Loading @@ -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(); Loading Loading @@ -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()); Loading java/src/com/android/inputmethod/latin/Settings.java +5 −4 Original line number Diff line number Diff line Loading @@ -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); Loading Loading @@ -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)); Loading Loading @@ -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, Loading Loading @@ -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()); Loading java/src/com/android/inputmethod/latin/SettingsValues.java +6 −10 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); Loading @@ -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) { Loading @@ -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); Loading Loading @@ -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)); } Loading Loading
java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +2 −2 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); Loading
java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java +41 −15 Original line number Diff line number Diff line Loading @@ -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. * Loading @@ -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) { Loading @@ -80,7 +106,7 @@ public final class AudioAndHapticFeedbackManager { } } private void vibrate(final View viewToPerformHapticFeedbackOn) { private void vibrateInternal(final View viewToPerformHapticFeedbackOn) { if (!mSettingsValues.mVibrateOn) { return; } Loading @@ -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) { Loading
java/src/com/android/inputmethod/latin/LatinIME.java +4 −2 Original line number Diff line number Diff line Loading @@ -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(); Loading Loading @@ -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()); Loading
java/src/com/android/inputmethod/latin/Settings.java +5 −4 Original line number Diff line number Diff line Loading @@ -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); Loading Loading @@ -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)); Loading Loading @@ -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, Loading Loading @@ -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()); Loading
java/src/com/android/inputmethod/latin/SettingsValues.java +6 −10 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); Loading @@ -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) { Loading @@ -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); Loading Loading @@ -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)); } Loading