• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.notification;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.media.AudioAttributes;
23 import android.os.Process;
24 import android.os.VibrationAttributes;
25 import android.os.VibrationEffect;
26 import android.os.Vibrator;
27 import android.util.Slog;
28 
29 import com.android.internal.R;
30 import com.android.server.pm.PackageManagerService;
31 
32 import java.util.Arrays;
33 
34 /**
35  * NotificationManagerService helper for functionality related to the vibrator.
36  */
37 public final class VibratorHelper {
38     private static final String TAG = "NotificationVibratorHelper";
39 
40     private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
41     private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
42 
43     private final Vibrator mVibrator;
44     private final long[] mDefaultPattern;
45     private final long[] mFallbackPattern;
46 
VibratorHelper(Context context)47     public VibratorHelper(Context context) {
48         mVibrator = context.getSystemService(Vibrator.class);
49         mDefaultPattern = getLongArray(
50                 context.getResources(),
51                 com.android.internal.R.array.config_defaultNotificationVibePattern,
52                 VIBRATE_PATTERN_MAXLEN,
53                 DEFAULT_VIBRATE_PATTERN);
54         mFallbackPattern = getLongArray(context.getResources(),
55                 R.array.config_notificationFallbackVibePattern,
56                 VIBRATE_PATTERN_MAXLEN,
57                 DEFAULT_VIBRATE_PATTERN);
58     }
59 
60     /**
61      * Safely create a {@link VibrationEffect} from given vibration {@code pattern}.
62      *
63      * <p>This method returns {@code null} if the pattern is also {@code null} or invalid.
64      *
65      * @param pattern The off/on vibration pattern, where each item is a duration in milliseconds.
66      * @param insistent {@code true} if the vibration should loop until it is cancelled.
67      */
68     @Nullable
createWaveformVibration(@ullable long[] pattern, boolean insistent)69     public static VibrationEffect createWaveformVibration(@Nullable long[] pattern,
70             boolean insistent) {
71         try {
72             if (pattern != null) {
73                 return VibrationEffect.createWaveform(pattern, /* repeat= */ insistent ? 0 : -1);
74             }
75         } catch (IllegalArgumentException e) {
76             Slog.e(TAG, "Error creating vibration waveform with pattern: "
77                     + Arrays.toString(pattern));
78         }
79         return null;
80     }
81 
82     /**
83      * Vibrate the device with given {@code effect}.
84      *
85      * <p>We need to vibrate as "android" so we can breakthrough DND.
86      */
vibrate(VibrationEffect effect, AudioAttributes attrs, String reason)87     public void vibrate(VibrationEffect effect, AudioAttributes attrs, String reason) {
88         mVibrator.vibrate(Process.SYSTEM_UID, PackageManagerService.PLATFORM_PACKAGE_NAME,
89                 effect, reason, attrs);
90     }
91 
92     /** Stop all notification vibrations (ringtone, alarm, notification usages). */
cancelVibration()93     public void cancelVibration() {
94         int usageFilter =
95                 VibrationAttributes.USAGE_CLASS_ALARM | ~VibrationAttributes.USAGE_CLASS_MASK;
96         mVibrator.cancel(usageFilter);
97     }
98 
99     /**
100      * Creates a vibration to be used as fallback when the device is in vibrate mode.
101      *
102      * @param insistent {@code true} if the vibration should loop until it is cancelled.
103      */
createFallbackVibration(boolean insistent)104     public VibrationEffect createFallbackVibration(boolean insistent) {
105         return createWaveformVibration(mFallbackPattern, insistent);
106     }
107 
108     /**
109      * Creates a vibration to be used by notifications without a custom pattern.
110      *
111      * @param insistent {@code true} if the vibration should loop until it is cancelled.
112      */
createDefaultVibration(boolean insistent)113     public VibrationEffect createDefaultVibration(boolean insistent) {
114         return createWaveformVibration(mDefaultPattern, insistent);
115     }
116 
getLongArray(Resources resources, int resId, int maxLength, long[] def)117     private static long[] getLongArray(Resources resources, int resId, int maxLength, long[] def) {
118         int[] ar = resources.getIntArray(resId);
119         if (ar == null) {
120             return def;
121         }
122         final int len = ar.length > maxLength ? maxLength : ar.length;
123         long[] out = new long[len];
124         for (int i = 0; i < len; i++) {
125             out[i] = ar[i];
126         }
127         return out;
128     }
129 }
130