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

Commit 0563a82f authored by Jean-Michel Trivi's avatar Jean-Michel Trivi Committed by android-build-merger
Browse files

Merge "Delay notification vibration to synchronize with notif sound" into oc-dev

am: 29db4b35

Change-Id: Icfad056884cfb620cfd27e12146b280a5cb7c300
parents 59541bcb 29db4b35
Loading
Loading
Loading
Loading
+31 −12
Original line number Original line Diff line number Diff line
@@ -3714,7 +3714,7 @@ public class NotificationManagerService extends SystemService {
                    if (!mInCall && hasValidVibrate && !ringerModeSilent) {
                    if (!mInCall && hasValidVibrate && !ringerModeSilent) {
                        mVibrateNotificationKey = key;
                        mVibrateNotificationKey = key;


                        buzz = playVibration(record, vibration);
                        buzz = playVibration(record, vibration, hasValidSound);
                    }
                    }
                }
                }
            }
            }
@@ -3788,22 +3788,41 @@ public class NotificationManagerService extends SystemService {
        return false;
        return false;
    }
    }


    private boolean playVibration(final NotificationRecord record, long[] vibration) {
    private boolean playVibration(final NotificationRecord record, long[] vibration,
            boolean delayVibForSound) {
        // Escalate privileges so we can use the vibrator even if the
        // Escalate privileges so we can use the vibrator even if the
        // notifying app does not have the VIBRATE permission.
        // notifying app does not have the VIBRATE permission.
        long identity = Binder.clearCallingIdentity();
        long identity = Binder.clearCallingIdentity();
        try {
            final VibrationEffect effect;
            try {
            try {
                final boolean insistent =
                final boolean insistent =
                        (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0;
                        (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0;
            final VibrationEffect effect = VibrationEffect.createWaveform(
                effect = VibrationEffect.createWaveform(
                        vibration, insistent ? 0 : -1 /*repeatIndex*/);
                        vibration, insistent ? 0 : -1 /*repeatIndex*/);
            mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(),
                    effect, record.getAudioAttributes());
            return true;
            } catch (IllegalArgumentException e) {
            } catch (IllegalArgumentException e) {
                Slog.e(TAG, "Error creating vibration waveform with pattern: " +
                Slog.e(TAG, "Error creating vibration waveform with pattern: " +
                        Arrays.toString(vibration));
                        Arrays.toString(vibration));
                return false;
                return false;
            }
            if (delayVibForSound) {
                new Thread(() -> {
                    // delay the vibration by the same amount as the notification sound
                    final int waitMs = mAudioManager.getFocusRampTimeMs(
                            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
                            record.getAudioAttributes());
                    if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms");
                    try {
                        Thread.sleep(waitMs);
                    } catch (InterruptedException e) { }
                    mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(),
                            effect, record.getAudioAttributes());
                }).start();
            } else {
                mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(),
                        effect, record.getAudioAttributes());
            }
            return true;
        } finally{
        } finally{
            Binder.restoreCallingIdentity(identity);
            Binder.restoreCallingIdentity(identity);
        }
        }
+12 −5
Original line number Original line Diff line number Diff line
@@ -61,6 +61,7 @@ import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.when;
@@ -102,6 +103,7 @@ public class BuzzBeepBlinkTest extends NotificationTestCase {
    private static final long[] FALLBACK_VIBRATION_PATTERN = new long[] {100, 100, 100};
    private static final long[] FALLBACK_VIBRATION_PATTERN = new long[] {100, 100, 100};
    private static final VibrationEffect FALLBACK_VIBRATION =
    private static final VibrationEffect FALLBACK_VIBRATION =
            VibrationEffect.createWaveform(FALLBACK_VIBRATION_PATTERN, -1);
            VibrationEffect.createWaveform(FALLBACK_VIBRATION_PATTERN, -1);
    private static final int MAX_VIBRATION_DELAY = 1000;


    @Before
    @Before
    public void setUp() {
    public void setUp() {
@@ -309,6 +311,11 @@ public class BuzzBeepBlinkTest extends NotificationTestCase {
                (AudioAttributes) anyObject());
                (AudioAttributes) anyObject());
    }
    }


    private void verifyDelayedVibrateLooped() {
        verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(),
                argThat(mVibrateLoopMatcher), (AudioAttributes) anyObject());
    }

    private void verifyStopVibrate() {
    private void verifyStopVibrate() {
        verify(mVibrator, times(1)).cancel();
        verify(mVibrator, times(1)).cancel();
    }
    }
@@ -506,8 +513,8 @@ public class BuzzBeepBlinkTest extends NotificationTestCase {


        VibrationEffect effect = VibrationEffect.createWaveform(r.getVibration(), -1);
        VibrationEffect effect = VibrationEffect.createWaveform(r.getVibration(), -1);


        verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), eq(effect),
        verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(),
                    (AudioAttributes) anyObject());
                eq(effect), (AudioAttributes) anyObject());
    }
    }


    @Test
    @Test
@@ -521,8 +528,8 @@ public class BuzzBeepBlinkTest extends NotificationTestCase {


        mService.buzzBeepBlinkLocked(r);
        mService.buzzBeepBlinkLocked(r);


        verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), eq(FALLBACK_VIBRATION),
        verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(),
                (AudioAttributes) anyObject());
                eq(FALLBACK_VIBRATION), (AudioAttributes) anyObject());
        verify(mRingtonePlayer, never()).playAsync
        verify(mRingtonePlayer, never()).playAsync
                (anyObject(), anyObject(), anyBoolean(), anyObject());
                (anyObject(), anyObject(), anyBoolean(), anyObject());
    }
    }
@@ -539,7 +546,7 @@ public class BuzzBeepBlinkTest extends NotificationTestCase {


        mService.buzzBeepBlinkLocked(r);
        mService.buzzBeepBlinkLocked(r);


        verifyVibrateLooped();
        verifyDelayedVibrateLooped();
    }
    }


    @Test
    @Test