From 1ee5267d6de2acdbe0ab658a23a77a44c17e4783 Mon Sep 17 00:00:00 2001 From: Ido Ben-Hur Date: Sun, 7 Jun 2020 20:32:02 +0300 Subject: [PATCH] Settings: Preview ringtone vibration pattern Signed-off-by: Pranav Vashi --- .../VibrationPatternPreferenceController.java | 149 +++++++++++++++++- 1 file changed, 148 insertions(+), 1 deletion(-) diff --git a/src/com/android/settings/notification/VibrationPatternPreferenceController.java b/src/com/android/settings/notification/VibrationPatternPreferenceController.java index 0b82ee04ef1..2f27310de16 100644 --- a/src/com/android/settings/notification/VibrationPatternPreferenceController.java +++ b/src/com/android/settings/notification/VibrationPatternPreferenceController.java @@ -18,6 +18,10 @@ package com.android.settings.notification; import android.content.ContentResolver; import android.content.Context; +import android.media.AudioAttributes; +import android.net.Uri; +import android.os.VibrationEffect; +import android.os.Vibrator; import android.provider.Settings; import androidx.preference.ListPreference; @@ -48,6 +52,99 @@ public class VibrationPatternPreferenceController extends AbstractPreferenceCont private CustomSeekBarPreference mCustomVib2; private CustomSeekBarPreference mCustomVib3; + private static class VibrationEffectProxy { + public VibrationEffect createWaveform(long[] timings, int[] amplitudes, int repeat) { + return VibrationEffect.createWaveform(timings, amplitudes, repeat); + } + } + + private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder() + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) + .build(); + + private static final long[] SIMPLE_VIBRATION_PATTERN = { + 0, // No delay before starting + 800, // How long to vibrate + 800, // How long to wait before vibrating again + }; + + private static final long[] DZZZ_DA_VIBRATION_PATTERN = { + 0, // No delay before starting + 500, // How long to vibrate + 200, // Delay + 20, // How long to vibrate + 720, // How long to wait before vibrating again + }; + + private static final long[] MM_MM_MM_VIBRATION_PATTERN = { + 0, // No delay before starting + 300, // How long to vibrate + 400, // Delay + 300, // How long to vibrate + 400, // Delay + 300, // How long to vibrate + 1400, // How long to wait before vibrating again + }; + + private static final long[] DA_DA_DZZZ_VIBRATION_PATTERN = { + 0, // No delay before starting + 30, // How long to vibrate + 80, // Delay + 30, // How long to vibrate + 80, // Delay + 50, // How long to vibrate + 180, // Delay + 600, // How long to vibrate + 1050, // How long to wait before vibrating again + }; + + private static final long[] DA_DZZZ_DA_VIBRATION_PATTERN = { + 0, // No delay before starting + 80, // How long to vibrate + 200, // Delay + 600, // How long to vibrate + 150, // Delay + 20, // How long to vibrate + 1050, // How long to wait before vibrating again + }; + + private static final int[] NINE_ELEMENTS_VIBRATION_AMPLITUDE = { + 0, // No delay before starting + 255, // Vibrate full amplitude + 0, // No amplitude while waiting + 255, + 0, + 255, + 0, + 255, + 0, + }; + + private static final int[] SEVEN_ELEMENTS_VIBRATION_AMPLITUDE = { + 0, // No delay before starting + 255, // Vibrate full amplitude + 0, // No amplitude while waiting + 255, + 0, + 255, + 0, + }; + + private static final int[] FIVE_ELEMENTS_VIBRATION_AMPLITUDE = { + 0, // No delay before starting + 255, // Vibrate full amplitude + 0, // No amplitude while waiting + 255, + 0, + }; + + private static final int[] SIMPLE_VIBRATION_AMPLITUDE = { + 0, // No delay before starting + 255, // Vibrate full amplitude + 0, // No amplitude while waiting + }; + public VibrationPatternPreferenceController(Context context) { super(context); } @@ -86,7 +183,9 @@ public class VibrationPatternPreferenceController extends AbstractPreferenceCont Settings.System.putInt(mContext.getContentResolver(), Settings.System.RINGTONE_VIBRATION_PATTERN, vibPattern); mVibPattern.setSummary(mVibPattern.getEntries()[vibPattern]); - updateCustomVibVisibility(vibPattern == 5); + boolean isCustom = vibPattern == 5; + updateCustomVibVisibility(isCustom); + if (!isCustom) previewPattern(); return true; } else if (preference == mCustomVib1) { updateCustomVib(0, (Integer) newValue); @@ -136,5 +235,53 @@ public class VibrationPatternPreferenceController extends AbstractPreferenceCont Settings.System.putString(mContext.getContentResolver(), Settings.System.CUSTOM_RINGTONE_VIBRATION_PATTERN, String.join( ",", customPattern[0], customPattern[1], customPattern[2])); + previewPattern(); + } + + private void previewPattern() { + Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); + VibrationEffect effect; + VibrationEffectProxy vibrationEffectProxy = new VibrationEffectProxy(); + int vibPattern = Settings.System.getInt(mContext.getContentResolver(), + Settings.System.RINGTONE_VIBRATION_PATTERN, 0); + switch (vibPattern) { + case 1: + effect = vibrationEffectProxy.createWaveform(DZZZ_DA_VIBRATION_PATTERN, + FIVE_ELEMENTS_VIBRATION_AMPLITUDE, -1); + break; + case 2: + effect = vibrationEffectProxy.createWaveform(MM_MM_MM_VIBRATION_PATTERN, + SEVEN_ELEMENTS_VIBRATION_AMPLITUDE, -1); + break; + case 3: + effect = vibrationEffectProxy.createWaveform(DA_DA_DZZZ_VIBRATION_PATTERN, + NINE_ELEMENTS_VIBRATION_AMPLITUDE, -1); + break; + case 4: + effect = vibrationEffectProxy.createWaveform(DA_DZZZ_DA_VIBRATION_PATTERN, + SEVEN_ELEMENTS_VIBRATION_AMPLITUDE, -1); + break; + case 5: + String[] customVib = Settings.System.getString( + mContext.getContentResolver(), + Settings.System.CUSTOM_RINGTONE_VIBRATION_PATTERN).split(",", 3); + long[] customVibPattern = { + 0, // No delay before starting + Long.parseLong(customVib[0]), // How long to vibrate + 400, // Delay + Long.parseLong(customVib[1]), // How long to vibrate + 400, // Delay + Long.parseLong(customVib[2]), // How long to vibrate + 400, // How long to wait before vibrating again + }; + effect = vibrationEffectProxy.createWaveform(customVibPattern, + SEVEN_ELEMENTS_VIBRATION_AMPLITUDE, -1); + break; + default: + effect = vibrationEffectProxy.createWaveform(SIMPLE_VIBRATION_PATTERN, + SIMPLE_VIBRATION_AMPLITUDE, -1); + break; + } + vibrator.vibrate(effect, VIBRATION_ATTRIBUTES); } }