Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4b852d60 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka Committed by Android (Google) Code Review
Browse files

Merge "Consolidate VibratorUtils with AudioAndHapticFeedbackManager"

parents 26f2e453 b17b8874
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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);
+41 −15
Original line number Diff line number Diff line
@@ -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) {
+4 −2
Original line number Diff line number Diff line
@@ -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());
+5 −4
Original line number Diff line number Diff line
@@ -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());
+6 −10
Original line number Diff line number Diff line
@@ -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));
    }
Loading