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

Commit e9a85e9a authored by Jorge Ruesga's avatar Jorge Ruesga Committed by Gerrit Code Review
Browse files

quiethours: fix vibration logic

While QuietHours have different modes for haptic feedback and vibration, indeed
both uses the underlying VibratorService. This change adds some basic logic to
detect what quiethours mode should be applied to block access to vibrate calls
during quiethours.

This patch assumes:

1.- Single pattern vibrate calls are treated as haptic feedback if milliseconds
vibration is lower than 70 milliseconds
2.- Multiple pattern vibrate calls are treated as haptic feedback if the total
duration of the pattern is lower than 70 milliseconds and they doesn't need to
be repeated.

JIRA: NIGHTLIES-175
https://jira.cyanogenmod.org/browse/NIGHTLIES-175


Signed-off-by: default avatarJorge Ruesga <jorge@ruesga.com>

Change-Id: I0d090ca1189a5d132bf6a835e9921cf41e02d1fb
parent 193c5edc
Loading
Loading
Loading
Loading
+32 −5
Original line number Diff line number Diff line
@@ -54,6 +54,11 @@ public class VibratorService extends IVibratorService.Stub
        implements InputManager.InputDeviceListener {
    private static final String TAG = "VibratorService";

    // QuietHours have different modes for haptic feedback and vibration, but both
    // use this vibrator class. This constant defines the line to determine vibrate call
    // as a haptic feedback or as a vibration
    private static final int HAPTIC_FEEDBACK_MAX_DURATION = 70;

    private final LinkedList<Vibration> mVibrations;
    private Vibration mCurrentVibration;
    private final WorkSource mTmpWorkSource = new WorkSource();
@@ -197,18 +202,26 @@ public class VibratorService extends IVibratorService.Stub
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires VIBRATE permission");
        }
        verifyIncomingUid(uid);

        // Determine the quiethours mode in base the haptic constant
        String quietHoursMode = milliseconds > HAPTIC_FEEDBACK_MAX_DURATION
                ? Settings.System.QUIET_HOURS_STILL : Settings.System.QUIET_HOURS_HAPTIC;
        if (QuietHoursUtils.inQuietHours(mContext, quietHoursMode)) {
            return;
        }

        // We're running in the system server so we cannot crash. Check for a
        // timeout of 0 or negative. This will ensure that a vibration has
        // either a timeout of > 0 or a non-null pattern.
        if (milliseconds <= 0 || (mCurrentVibration != null
                && mCurrentVibration.hasLongerTimeout(milliseconds))
                || QuietHoursUtils.inQuietHours(mContext, Settings.System.QUIET_HOURS_HAPTIC)) {
                && mCurrentVibration.hasLongerTimeout(milliseconds))) {
            // Ignore this vibration since the current vibration will play for
            // longer than milliseconds.
            return;
        }

        verifyIncomingUid(uid);

        Vibration vib = new Vibration(token, milliseconds, uid, packageName);

        final long ident = Binder.clearCallingIdentity();
@@ -240,11 +253,17 @@ public class VibratorService extends IVibratorService.Stub
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires VIBRATE permission");
        }
        if (QuietHoursUtils.inQuietHours(mContext, Settings.System.QUIET_HOURS_HAPTIC)) {

        // Determine the quiethours mode according to the haptic constant. Will assume that
        // repeating a pattern is a vibration
        String quietHoursMode = !isHapticFeedbackPattern(pattern) || repeat >= 0
                ? Settings.System.QUIET_HOURS_STILL : Settings.System.QUIET_HOURS_HAPTIC;
        if (QuietHoursUtils.inQuietHours(mContext, quietHoursMode)) {
            return;
        }
        verifyIncomingUid(uid);

        // so wakelock calls will succeed
        verifyIncomingUid(uid);
        long identity = Binder.clearCallingIdentity();
        try {
            if (false) {
@@ -516,6 +535,14 @@ public class VibratorService extends IVibratorService.Stub
        }
    }

    private static boolean isHapticFeedbackPattern(long[] pattern) {
        long milliseconds = 0;
        for (long millis : pattern) {
            milliseconds += millis;
        }
        return milliseconds <= HAPTIC_FEEDBACK_MAX_DURATION;
    }

    private class VibrateThread extends Thread {
        final Vibration mVibration;
        boolean mDone;