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

Commit e38c7db6 authored by Danny Baumann's avatar Danny Baumann
Browse files

Apply haptic feedback to all buttons, not just the sound button.

The feedback for all other buttons is significantly weaker. I've also
decreased strength of sound button feedback, as it was too strong.

Change-Id: Iaee471b29b306e9ebcc6a81799f7e36872951002
parent 52f8d547
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import android.graphics.PorterDuff.Mode;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
@@ -66,6 +67,11 @@ public abstract class PowerButton {
    private View.OnClickListener mExternalClickListener;
    private View.OnLongClickListener mExternalLongClickListener;

    protected boolean mHapticFeedback;
    protected Vibrator mVibrator;
    private long[] mClickPattern;
    private long[] mLongClickPattern;

    // we use this to ensure we update our views on the UI thread
    private Handler mViewUpdateHandler = new Handler() {
        @Override
@@ -122,6 +128,13 @@ public abstract class PowerButton {
        // to a changed setting
    }

    /* package */ void setHapticFeedback(boolean enabled,
            long[] clickPattern, long[] longClickPattern) {
        mHapticFeedback = enabled;
        mClickPattern = clickPattern;
        mLongClickPattern = longClickPattern;
    }

    protected IntentFilter getBroadcastIntentFilter() {
        return new IntentFilter();
    }
@@ -139,6 +152,7 @@ public abstract class PowerButton {

            mIconView = (ImageView) mView.findViewById(R.id.power_widget_button_image);
            mIndicatorView = (ImageView) mView.findViewById(R.id.power_widget_button_indic);
            mVibrator = (Vibrator) mView.getContext().getSystemService(Context.VIBRATOR_SERVICE);
        } else {
            mIconView = null;
            mIndicatorView = null;
@@ -151,6 +165,10 @@ public abstract class PowerButton {

    private View.OnClickListener mClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            if (mHapticFeedback && mClickPattern != null) {
                mVibrator.vibrate(mClickPattern, -1);
            }

            toggleState(v.getContext());
            update(v.getContext());

@@ -164,6 +182,10 @@ public abstract class PowerButton {
        public boolean onLongClick(View v) {
            boolean result = handleLongClick(v.getContext());

            if (result && mHapticFeedback && mLongClickPattern != null) {
                mVibrator.vibrate(mLongClickPattern, -1);
            }

            if (result && mExternalLongClickListener != null) {
                mExternalLongClickListener.onLongClick(v);
            }
+43 −0
Original line number Diff line number Diff line
@@ -167,6 +167,7 @@ public class PowerWidget extends FrameLayout {
            }
        }
        recreateButtonLayout();
        updateHapticFeedbackSetting();

        // set up a broadcast receiver for our intents, based off of what our power buttons have been loaded
        setupBroadcastReceiver();
@@ -353,6 +354,32 @@ public class PowerWidget extends FrameLayout {
        mScrollView.setHorizontalScrollBarEnabled(!hideScrollBar);
    }

    private void updateHapticFeedbackSetting() {
        ContentResolver cr = mContext.getContentResolver();
        int expandedHapticFeedback = Settings.System.getInt(cr,
                Settings.System.EXPANDED_HAPTIC_FEEDBACK, 2);
        long[] clickPattern = null, longClickPattern = null;
        boolean hapticFeedback;

        if (expandedHapticFeedback == 2) {
             hapticFeedback = Settings.System.getInt(cr,
                     Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) == 1;
        } else {
            hapticFeedback = (expandedHapticFeedback == 1);
        }

        if (hapticFeedback) {
            clickPattern = Settings.System.getLongArray(cr,
                    Settings.System.HAPTIC_DOWN_ARRAY, null);
            longClickPattern = Settings.System.getLongArray(cr,
                    Settings.System.HAPTIC_LONG_ARRAY, null);
        }

        for (PowerButton button : mButtons.values()) {
            button.setHapticFeedback(hapticFeedback, clickPattern, longClickPattern);
        }
    }

    // our own broadcast receiver :D
    private class WidgetBroadcastReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
@@ -399,6 +426,15 @@ public class PowerWidget extends FrameLayout {
            resolver.registerContentObserver(
                    Settings.System.getUriFor(Settings.System.EXPANDED_HAPTIC_FEEDBACK),
                            false, this);
            resolver.registerContentObserver(
                    Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED),
                            false, this);
            resolver.registerContentObserver(
                    Settings.System.getUriFor(Settings.System.HAPTIC_DOWN_ARRAY),
                            false, this);
            resolver.registerContentObserver(
                    Settings.System.getUriFor(Settings.System.HAPTIC_LONG_ARRAY),
                            false, this);

            // watch for changes in buttons
            resolver.registerContentObserver(
@@ -438,6 +474,13 @@ public class PowerWidget extends FrameLayout {
                updateScrollbar();
            }

            if (uri.equals(Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED))
                    || uri.equals(Settings.System.getUriFor(Settings.System.HAPTIC_DOWN_ARRAY))
                    || uri.equals(Settings.System.getUriFor(Settings.System.HAPTIC_LONG_ARRAY))
                    || uri.equals(Settings.System.getUriFor(Settings.System.EXPANDED_HAPTIC_FEEDBACK))) {
                updateHapticFeedbackSetting();
            }

            // do whatever the individual buttons must
            for (PowerButton button : mButtons.values()) {
                if (button.getObservedUris().contains(uri)) {
+2 −18
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Vibrator;
import android.preference.MultiSelectListPreference;
import android.provider.Settings;
import android.view.View;
@@ -21,7 +20,7 @@ public class SoundButton extends PowerButton {

    private static final String TAG = "SoundButton";

    private static final int VIBRATE_DURATION = 500; // 0.5s
    private static final int VIBRATE_DURATION = 250; // 0.25s

    private static final IntentFilter INTENT_FILTER = new IntentFilter();
    static {
@@ -32,8 +31,6 @@ public class SoundButton extends PowerButton {
    private static final List<Uri> OBSERVED_URIS = new ArrayList<Uri>();
    static {
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.EXPANDED_RING_MODE));
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.EXPANDED_HAPTIC_FEEDBACK));
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED));
    }

    private final Ringer mSilentRinger = new Ringer(false, AudioManager.VIBRATE_SETTING_OFF,
@@ -54,10 +51,7 @@ public class SoundButton extends PowerButton {
    };
    private int mRingerValuesIndex = 2;

    private boolean mHapticFeedbackEnabled = false;

    private AudioManager mAudioManager;
    private Vibrator mVibrator;

    public SoundButton() {
        mType = BUTTON_SOUND;
@@ -69,7 +63,6 @@ public class SoundButton extends PowerButton {
        if (mView != null) {
            Context context = mView.getContext();
            mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            updateSettings(context.getContentResolver());
        }
    }
@@ -142,15 +135,6 @@ public class SoundButton extends PowerButton {
    }

    private void updateSettings(ContentResolver resolver) {
        int expandedHapticFeedback = Settings.System.getInt(resolver,
                Settings.System.EXPANDED_HAPTIC_FEEDBACK, 2);
        if (expandedHapticFeedback == 2) {
            mHapticFeedbackEnabled = (Settings.System.getInt(resolver,
                    Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) == 1);
        } else {
            mHapticFeedbackEnabled = (expandedHapticFeedback == 1);
        }

        String[] modes = MultiSelectListPreference.parseStoredValue(Settings.System.getString(
                resolver, Settings.System.EXPANDED_RING_MODE));
        if (modes == null || modes.length == 0) {
@@ -222,7 +206,7 @@ public class SoundButton extends PowerButton {
            ensureAudioManager(context);
            mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, mVibrateSetting);
            mAudioManager.setRingerMode(mRingerMode);
            if (mDoHapticFeedback && mHapticFeedbackEnabled) {
            if (mDoHapticFeedback && mHapticFeedback) {
                mVibrator.vibrate(VIBRATE_DURATION);
            }
        }