The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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; |
| 18 | |
Alexey Kuzmin | 3fe97b0 | 2018-12-12 14:21:55 +0000 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 20 | import android.app.AppOpsManager; |
Alexey Kuzmin | 3fe97b0 | 2018-12-12 14:21:55 +0000 | [diff] [blame] | 21 | import android.app.IUidObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
| 26 | import android.content.pm.PackageManager; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 27 | import android.content.res.Resources; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 28 | import android.database.ContentObserver; |
| 29 | import android.hardware.input.InputManager; |
Michael Wright | f268bf5 | 2018-02-07 23:23:34 +0000 | [diff] [blame] | 30 | import android.hardware.vibrator.V1_0.EffectStrength; |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 31 | import android.icu.text.DateFormat; |
Bookatz | a7020bd | 2018-08-28 16:29:35 -0700 | [diff] [blame] | 32 | import android.media.AudioAttributes; |
Brad Ebinger | 2d1c3b3 | 2016-05-12 18:05:17 -0700 | [diff] [blame] | 33 | import android.media.AudioManager; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 34 | import android.os.BatteryStats; |
Bookatz | a7020bd | 2018-08-28 16:29:35 -0700 | [diff] [blame] | 35 | import android.os.Binder; |
Joe Onorato | 95e4f70 | 2009-03-24 19:29:09 -0700 | [diff] [blame] | 36 | import android.os.Handler; |
Bookatz | a7020bd | 2018-08-28 16:29:35 -0700 | [diff] [blame] | 37 | import android.os.IBinder; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 38 | import android.os.IVibratorService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.os.PowerManager; |
Bookatz | a7020bd | 2018-08-28 16:29:35 -0700 | [diff] [blame] | 40 | import android.os.PowerManager.ServiceType; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 41 | import android.os.PowerManagerInternal; |
Bookatz | a7020bd | 2018-08-28 16:29:35 -0700 | [diff] [blame] | 42 | import android.os.PowerSaveState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.os.Process; |
| 44 | import android.os.RemoteException; |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 45 | import android.os.ResultReceiver; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 46 | import android.os.ServiceManager; |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 47 | import android.os.ShellCallback; |
| 48 | import android.os.ShellCommand; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.os.SystemClock; |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 50 | import android.os.Trace; |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 51 | import android.os.UserHandle; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 52 | import android.os.VibrationEffect; |
Bookatz | a7020bd | 2018-08-28 16:29:35 -0700 | [diff] [blame] | 53 | import android.os.Vibrator; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 54 | import android.os.WorkSource; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 55 | import android.provider.Settings; |
| 56 | import android.provider.Settings.SettingNotFoundException; |
Felipe Leme | 5e2e632 | 2017-07-14 17:25:59 -0700 | [diff] [blame] | 57 | import android.util.DebugUtils; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 58 | import android.util.Slog; |
Alexey Kuzmin | 5a0a26f | 2018-03-20 18:25:51 +0000 | [diff] [blame] | 59 | import android.util.SparseArray; |
Bookatz | a7020bd | 2018-08-28 16:29:35 -0700 | [diff] [blame] | 60 | import android.util.StatsLog; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 61 | import android.view.InputDevice; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
Alexey Kuzmin | 7f38fb1 | 2018-02-07 13:51:28 +0000 | [diff] [blame] | 63 | import com.android.internal.annotations.GuardedBy; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 64 | import com.android.internal.app.IBatteryStats; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 65 | import com.android.internal.util.DumpUtils; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 66 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 67 | import java.io.FileDescriptor; |
| 68 | import java.io.PrintWriter; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 69 | import java.util.ArrayList; |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 70 | import java.util.Date; |
Bookatz | a7020bd | 2018-08-28 16:29:35 -0700 | [diff] [blame] | 71 | import java.util.LinkedList; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 72 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 73 | public class VibratorService extends IVibratorService.Stub |
| 74 | implements InputManager.InputDeviceListener { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 75 | private static final String TAG = "VibratorService"; |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 76 | private static final boolean DEBUG = false; |
Jorim Jaggi | 18f18ae | 2015-09-10 15:48:21 -0700 | [diff] [blame] | 77 | private static final String SYSTEM_UI_PACKAGE = "com.android.systemui"; |
Mike Lockwood | cc9a63d | 2009-11-10 07:50:28 -0500 | [diff] [blame] | 78 | |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 79 | private static final long[] DOUBLE_CLICK_EFFECT_FALLBACK_TIMINGS = { 0, 30, 100, 30 }; |
| 80 | |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 81 | // Scale levels. Each level is defined as the delta between the current setting and the default |
| 82 | // intensity for that type of vibration (i.e. current - default). |
| 83 | private static final int SCALE_VERY_LOW = -2; |
| 84 | private static final int SCALE_LOW = -1; |
| 85 | private static final int SCALE_NONE = 0; |
| 86 | private static final int SCALE_HIGH = 1; |
| 87 | private static final int SCALE_VERY_HIGH = 2; |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 88 | |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 89 | // Gamma adjustments for scale levels. |
| 90 | private static final float SCALE_VERY_LOW_GAMMA = 2.0f; |
| 91 | private static final float SCALE_LOW_GAMMA = 1.5f; |
| 92 | private static final float SCALE_NONE_GAMMA = 1.0f; |
| 93 | private static final float SCALE_HIGH_GAMMA = 0.5f; |
| 94 | private static final float SCALE_VERY_HIGH_GAMMA = 0.25f; |
| 95 | |
| 96 | // Max amplitudes for scale levels. If one is not listed, then the max amplitude is the default |
| 97 | // max amplitude. |
| 98 | private static final int SCALE_VERY_LOW_MAX_AMPLITUDE = 168; // 2/3 * 255 |
| 99 | private static final int SCALE_LOW_MAX_AMPLITUDE = 192; // 3/4 * 255 |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 100 | |
| 101 | // If a vibration is playing for longer than 5s, it's probably not haptic feedback. |
| 102 | private static final long MAX_HAPTIC_FEEDBACK_DURATION = 5000; |
| 103 | |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 104 | |
| 105 | // A mapping from the intensity adjustment to the scaling to apply, where the intensity |
| 106 | // adjustment is defined as the delta between the default intensity level and the user selected |
| 107 | // intensity level. It's important that we apply the scaling on the delta between the two so |
| 108 | // that the default intensity level applies no scaling to application provided effects. |
| 109 | private final SparseArray<ScaleLevel> mScaleLevels; |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 110 | private final LinkedList<VibrationInfo> mPreviousVibrations; |
| 111 | private final int mPreviousVibrationsLimit; |
Tyler Freeman | 319a34a | 2017-05-04 17:23:35 -0700 | [diff] [blame] | 112 | private final boolean mAllowPriorityVibrationsInLowPowerMode; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 113 | private final boolean mSupportsAmplitudeControl; |
| 114 | private final int mDefaultVibrationAmplitude; |
Alexey Kuzmin | 5a0a26f | 2018-03-20 18:25:51 +0000 | [diff] [blame] | 115 | private final SparseArray<VibrationEffect> mFallbackEffects; |
Alexey Kuzmin | 3fe97b0 | 2018-12-12 14:21:55 +0000 | [diff] [blame] | 116 | private final SparseArray<Integer> mProcStatesCache = new SparseArray(); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 117 | private final WorkSource mTmpWorkSource = new WorkSource(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 118 | private final Handler mH = new Handler(); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 119 | private final Object mLock = new Object(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 120 | |
| 121 | private final Context mContext; |
| 122 | private final PowerManager.WakeLock mWakeLock; |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 123 | private final AppOpsManager mAppOps; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 124 | private final IBatteryStats mBatteryStatsService; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 125 | private PowerManagerInternal mPowerManagerInternal; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 126 | private InputManager mIm; |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 127 | private Vibrator mVibrator; |
| 128 | private SettingsObserver mSettingObserver; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 129 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 130 | private volatile VibrateThread mThread; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 131 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 132 | // mInputDeviceVibrators lock should be acquired after mLock, if both are |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 133 | // to be acquired |
| 134 | private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>(); |
| 135 | private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators |
| 136 | private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators |
| 137 | |
Alexey Kuzmin | 7f38fb1 | 2018-02-07 13:51:28 +0000 | [diff] [blame] | 138 | @GuardedBy("mLock") |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 139 | private Vibration mCurrentVibration; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 140 | private int mCurVibUid = -1; |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 141 | private boolean mLowPowerMode; |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 142 | private int mHapticFeedbackIntensity; |
| 143 | private int mNotificationIntensity; |
Alexey Kuzmin | ccdaebb | 2018-12-10 12:02:51 +0000 | [diff] [blame] | 144 | private int mRingIntensity; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 145 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 146 | native static boolean vibratorExists(); |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 147 | native static void vibratorInit(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 148 | native static void vibratorOn(long milliseconds); |
| 149 | native static void vibratorOff(); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 150 | native static boolean vibratorSupportsAmplitudeControl(); |
| 151 | native static void vibratorSetAmplitude(int amplitude); |
| 152 | native static long vibratorPerformEffect(long effect, long strength); |
Harpreet "Eli" Sangha | a456f08 | 2018-12-14 12:06:10 +0900 | [diff] [blame] | 153 | static native boolean vibratorSupportsExternalControl(); |
| 154 | static native void vibratorSetExternalControl(boolean enabled); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 155 | |
Alexey Kuzmin | 3fe97b0 | 2018-12-12 14:21:55 +0000 | [diff] [blame] | 156 | private final IUidObserver mUidObserver = new IUidObserver.Stub() { |
| 157 | @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
| 158 | mProcStatesCache.put(uid, procState); |
| 159 | } |
| 160 | |
| 161 | @Override public void onUidGone(int uid, boolean disabled) { |
| 162 | mProcStatesCache.delete(uid); |
| 163 | } |
| 164 | |
| 165 | @Override public void onUidActive(int uid) { |
| 166 | } |
| 167 | |
| 168 | @Override public void onUidIdle(int uid, boolean disabled) { |
| 169 | } |
| 170 | |
| 171 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
| 172 | } |
| 173 | }; |
| 174 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 175 | private class Vibration implements IBinder.DeathRecipient { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 176 | public final IBinder token; |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 177 | // Start time in CLOCK_BOOTTIME base. |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 178 | public final long startTime; |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 179 | // Start time in unix epoch time. Only to be used for debugging purposes and to correlate |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 180 | // with other system events, any duration calculations should be done use startTime so as |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 181 | // not to be affected by discontinuities created by RTC adjustments. |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 182 | public final long startTimeDebug; |
| 183 | public final int usageHint; |
| 184 | public final int uid; |
| 185 | public final String opPkg; |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 186 | public final String reason; |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 187 | |
| 188 | // The actual effect to be played. |
| 189 | public VibrationEffect effect; |
| 190 | // The original effect that was requested. This is non-null only when the original effect |
| 191 | // differs from the effect that's being played. Typically these two things differ because |
| 192 | // the effect was scaled based on the users vibration intensity settings. |
| 193 | public VibrationEffect originalEffect; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 194 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 195 | private Vibration(IBinder token, VibrationEffect effect, |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 196 | int usageHint, int uid, String opPkg, String reason) { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 197 | this.token = token; |
| 198 | this.effect = effect; |
| 199 | this.startTime = SystemClock.elapsedRealtime(); |
| 200 | this.startTimeDebug = System.currentTimeMillis(); |
| 201 | this.usageHint = usageHint; |
| 202 | this.uid = uid; |
| 203 | this.opPkg = opPkg; |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 204 | this.reason = reason; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | public void binderDied() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 208 | synchronized (mLock) { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 209 | if (this == mCurrentVibration) { |
| 210 | doCancelVibrateLocked(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 215 | public boolean hasTimeoutLongerThan(long millis) { |
| 216 | final long duration = effect.getDuration(); |
| 217 | return duration >= 0 && duration > millis; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 218 | } |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 219 | |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 220 | public boolean isHapticFeedback() { |
| 221 | if (effect instanceof VibrationEffect.Prebaked) { |
| 222 | VibrationEffect.Prebaked prebaked = (VibrationEffect.Prebaked) effect; |
| 223 | switch (prebaked.getId()) { |
| 224 | case VibrationEffect.EFFECT_CLICK: |
| 225 | case VibrationEffect.EFFECT_DOUBLE_CLICK: |
Alexey Kuzmin | 5a0a26f | 2018-03-20 18:25:51 +0000 | [diff] [blame] | 226 | case VibrationEffect.EFFECT_HEAVY_CLICK: |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 227 | case VibrationEffect.EFFECT_TICK: |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 228 | case VibrationEffect.EFFECT_POP: |
| 229 | case VibrationEffect.EFFECT_THUD: |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 230 | return true; |
| 231 | default: |
| 232 | Slog.w(TAG, "Unknown prebaked vibration effect, " |
| 233 | + "assuming it isn't haptic feedback."); |
| 234 | return false; |
| 235 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 236 | } |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 237 | final long duration = effect.getDuration(); |
| 238 | return duration >= 0 && duration < MAX_HAPTIC_FEEDBACK_DURATION; |
| 239 | } |
| 240 | |
| 241 | public boolean isNotification() { |
| 242 | switch (usageHint) { |
| 243 | case AudioAttributes.USAGE_NOTIFICATION: |
| 244 | case AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 245 | case AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 246 | case AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 247 | return true; |
| 248 | default: |
| 249 | return false; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | public boolean isRingtone() { |
| 254 | return usageHint == AudioAttributes.USAGE_NOTIFICATION_RINGTONE; |
| 255 | } |
| 256 | |
| 257 | public boolean isFromSystem() { |
| 258 | return uid == Process.SYSTEM_UID || uid == 0 || SYSTEM_UI_PACKAGE.equals(opPkg); |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 259 | } |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 260 | |
| 261 | public VibrationInfo toInfo() { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 262 | return new VibrationInfo( |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 263 | startTimeDebug, effect, originalEffect, usageHint, uid, opPkg, reason); |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 264 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 265 | } |
| 266 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 267 | private static class VibrationInfo { |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 268 | private final long mStartTimeDebug; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 269 | private final VibrationEffect mEffect; |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 270 | private final VibrationEffect mOriginalEffect; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 271 | private final int mUsageHint; |
| 272 | private final int mUid; |
| 273 | private final String mOpPkg; |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 274 | private final String mReason; |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 275 | |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 276 | public VibrationInfo(long startTimeDebug, VibrationEffect effect, |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 277 | VibrationEffect originalEffect, int usageHint, int uid, |
| 278 | String opPkg, String reason) { |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 279 | mStartTimeDebug = startTimeDebug; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 280 | mEffect = effect; |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 281 | mOriginalEffect = originalEffect; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 282 | mUsageHint = usageHint; |
| 283 | mUid = uid; |
| 284 | mOpPkg = opPkg; |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 285 | mReason = reason; |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public String toString() { |
| 290 | return new StringBuilder() |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 291 | .append("startTime: ") |
| 292 | .append(DateFormat.getDateTimeInstance().format(new Date(mStartTimeDebug))) |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 293 | .append(", effect: ") |
| 294 | .append(mEffect) |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 295 | .append(", originalEffect: ") |
| 296 | .append(mOriginalEffect) |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 297 | .append(", usageHint: ") |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 298 | .append(mUsageHint) |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 299 | .append(", uid: ") |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 300 | .append(mUid) |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 301 | .append(", opPkg: ") |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 302 | .append(mOpPkg) |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 303 | .append(", reason: ") |
| 304 | .append(mReason) |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 305 | .toString(); |
| 306 | } |
| 307 | } |
| 308 | |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 309 | private static final class ScaleLevel { |
| 310 | public final float gamma; |
| 311 | public final int maxAmplitude; |
| 312 | |
| 313 | public ScaleLevel(float gamma) { |
| 314 | this(gamma, VibrationEffect.MAX_AMPLITUDE); |
| 315 | } |
| 316 | |
| 317 | public ScaleLevel(float gamma, int maxAmplitude) { |
| 318 | this.gamma = gamma; |
| 319 | this.maxAmplitude = maxAmplitude; |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public String toString() { |
| 324 | return "ScaleLevel{gamma=" + gamma + ", maxAmplitude=" + maxAmplitude + "}"; |
| 325 | } |
| 326 | } |
| 327 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 328 | VibratorService(Context context) { |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 329 | vibratorInit(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | // Reset the hardware to a default state, in case this is a runtime |
| 331 | // restart instead of a fresh boot. |
| 332 | vibratorOff(); |
| 333 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 334 | mSupportsAmplitudeControl = vibratorSupportsAmplitudeControl(); |
| 335 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | mContext = context; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 337 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 338 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | mWakeLock.setReferenceCounted(true); |
| 340 | |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 341 | mAppOps = mContext.getSystemService(AppOpsManager.class); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 342 | mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 343 | BatteryStats.SERVICE_NAME)); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 344 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 345 | mPreviousVibrationsLimit = mContext.getResources().getInteger( |
| 346 | com.android.internal.R.integer.config_previousVibrationsDumpLimit); |
| 347 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 348 | mDefaultVibrationAmplitude = mContext.getResources().getInteger( |
| 349 | com.android.internal.R.integer.config_defaultVibrationAmplitude); |
| 350 | |
Tyler Freeman | 319a34a | 2017-05-04 17:23:35 -0700 | [diff] [blame] | 351 | mAllowPriorityVibrationsInLowPowerMode = mContext.getResources().getBoolean( |
| 352 | com.android.internal.R.bool.config_allowPriorityVibrationsInLowPowerMode); |
| 353 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 354 | mPreviousVibrations = new LinkedList<>(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 355 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | IntentFilter filter = new IntentFilter(); |
| 357 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 358 | context.registerReceiver(mIntentReceiver, filter); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 359 | |
Michael Wright | d39cbec | 2018-04-16 19:35:13 +0100 | [diff] [blame] | 360 | VibrationEffect clickEffect = createEffectFromResource( |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 361 | com.android.internal.R.array.config_virtualKeyVibePattern); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 362 | VibrationEffect doubleClickEffect = VibrationEffect.createWaveform( |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 363 | DOUBLE_CLICK_EFFECT_FALLBACK_TIMINGS, -1 /*repeatIndex*/); |
Michael Wright | d39cbec | 2018-04-16 19:35:13 +0100 | [diff] [blame] | 364 | VibrationEffect heavyClickEffect = createEffectFromResource( |
| 365 | com.android.internal.R.array.config_longPressVibePattern); |
| 366 | VibrationEffect tickEffect = createEffectFromResource( |
Michael Wright | 57d94d9 | 2017-05-31 14:44:45 +0100 | [diff] [blame] | 367 | com.android.internal.R.array.config_clockTickVibePattern); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 368 | |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 369 | mFallbackEffects = new SparseArray<>(); |
Alexey Kuzmin | 5a0a26f | 2018-03-20 18:25:51 +0000 | [diff] [blame] | 370 | mFallbackEffects.put(VibrationEffect.EFFECT_CLICK, clickEffect); |
| 371 | mFallbackEffects.put(VibrationEffect.EFFECT_DOUBLE_CLICK, doubleClickEffect); |
| 372 | mFallbackEffects.put(VibrationEffect.EFFECT_TICK, tickEffect); |
Michael Wright | d39cbec | 2018-04-16 19:35:13 +0100 | [diff] [blame] | 373 | mFallbackEffects.put(VibrationEffect.EFFECT_HEAVY_CLICK, heavyClickEffect); |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 374 | |
| 375 | mScaleLevels = new SparseArray<>(); |
| 376 | mScaleLevels.put(SCALE_VERY_LOW, |
| 377 | new ScaleLevel(SCALE_VERY_LOW_GAMMA, SCALE_VERY_LOW_MAX_AMPLITUDE)); |
| 378 | mScaleLevels.put(SCALE_LOW, new ScaleLevel(SCALE_LOW_GAMMA, SCALE_LOW_MAX_AMPLITUDE)); |
| 379 | mScaleLevels.put(SCALE_NONE, new ScaleLevel(SCALE_NONE_GAMMA)); |
| 380 | mScaleLevels.put(SCALE_HIGH, new ScaleLevel(SCALE_HIGH_GAMMA)); |
| 381 | mScaleLevels.put(SCALE_VERY_HIGH, new ScaleLevel(SCALE_VERY_HIGH_GAMMA)); |
Michael Wright | 57d94d9 | 2017-05-31 14:44:45 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Michael Wright | d39cbec | 2018-04-16 19:35:13 +0100 | [diff] [blame] | 384 | private VibrationEffect createEffectFromResource(int resId) { |
| 385 | long[] timings = getLongIntArray(mContext.getResources(), resId); |
| 386 | return createEffectFromTimings(timings); |
| 387 | } |
| 388 | |
| 389 | private static VibrationEffect createEffectFromTimings(long[] timings) { |
Michael Wright | 57d94d9 | 2017-05-31 14:44:45 +0100 | [diff] [blame] | 390 | if (timings == null || timings.length == 0) { |
| 391 | return null; |
| 392 | } else if (timings.length == 1) { |
| 393 | return VibrationEffect.createOneShot(timings[0], VibrationEffect.DEFAULT_AMPLITUDE); |
| 394 | } else { |
| 395 | return VibrationEffect.createWaveform(timings, -1); |
| 396 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 399 | public void systemReady() { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 400 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "VibratorService#systemReady"); |
| 401 | try { |
| 402 | mIm = mContext.getSystemService(InputManager.class); |
| 403 | mVibrator = mContext.getSystemService(Vibrator.class); |
| 404 | mSettingObserver = new SettingsObserver(mH); |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 405 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 406 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 407 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 408 | new PowerManagerInternal.LowPowerModeListener() { |
| 409 | @Override |
| 410 | public int getServiceType() { |
| 411 | return ServiceType.VIBRATION; |
| 412 | } |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 413 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 414 | @Override |
| 415 | public void onLowPowerModeChanged(PowerSaveState result) { |
| 416 | updateVibrators(); |
| 417 | } |
| 418 | }); |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 419 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 420 | mContext.getContentResolver().registerContentObserver( |
| 421 | Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES), |
| 422 | true, mSettingObserver, UserHandle.USER_ALL); |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 423 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 424 | mContext.getContentResolver().registerContentObserver( |
| 425 | Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_INTENSITY), |
| 426 | true, mSettingObserver, UserHandle.USER_ALL); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 427 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 428 | mContext.getContentResolver().registerContentObserver( |
| 429 | Settings.System.getUriFor(Settings.System.NOTIFICATION_VIBRATION_INTENSITY), |
| 430 | true, mSettingObserver, UserHandle.USER_ALL); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 431 | |
Alexey Kuzmin | ccdaebb | 2018-12-10 12:02:51 +0000 | [diff] [blame] | 432 | mContext.getContentResolver().registerContentObserver( |
| 433 | Settings.System.getUriFor(Settings.System.RING_VIBRATION_INTENSITY), |
| 434 | true, mSettingObserver, UserHandle.USER_ALL); |
| 435 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 436 | mContext.registerReceiver(new BroadcastReceiver() { |
| 437 | @Override |
| 438 | public void onReceive(Context context, Intent intent) { |
| 439 | updateVibrators(); |
| 440 | } |
| 441 | }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH); |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 442 | |
Alexey Kuzmin | 3fe97b0 | 2018-12-12 14:21:55 +0000 | [diff] [blame] | 443 | try { |
| 444 | ActivityManager.getService().registerUidObserver(mUidObserver, |
| 445 | ActivityManager.UID_OBSERVER_PROCSTATE | ActivityManager.UID_OBSERVER_GONE, |
| 446 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
| 447 | } catch (RemoteException e) { |
| 448 | // ignored; both services live in system_server |
| 449 | } |
| 450 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 451 | updateVibrators(); |
| 452 | } finally { |
| 453 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
| 454 | } |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 455 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 456 | |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 457 | private final class SettingsObserver extends ContentObserver { |
| 458 | public SettingsObserver(Handler handler) { |
| 459 | super(handler); |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | public void onChange(boolean SelfChange) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 464 | updateVibrators(); |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 468 | @Override // Binder call |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 469 | public boolean hasVibrator() { |
| 470 | return doVibratorExists(); |
| 471 | } |
| 472 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 473 | @Override // Binder call |
| 474 | public boolean hasAmplitudeControl() { |
| 475 | synchronized (mInputDeviceVibrators) { |
| 476 | // Input device vibrators don't support amplitude controls yet, but are still used over |
| 477 | // the system vibrator when connected. |
| 478 | return mSupportsAmplitudeControl && mInputDeviceVibrators.isEmpty(); |
| 479 | } |
| 480 | } |
| 481 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 482 | private void verifyIncomingUid(int uid) { |
| 483 | if (uid == Binder.getCallingUid()) { |
| 484 | return; |
| 485 | } |
| 486 | if (Binder.getCallingPid() == Process.myPid()) { |
| 487 | return; |
| 488 | } |
| 489 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 490 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 491 | } |
| 492 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 493 | /** |
| 494 | * Validate the incoming VibrationEffect. |
| 495 | * |
| 496 | * We can't throw exceptions here since we might be called from some system_server component, |
| 497 | * which would bring the whole system down. |
| 498 | * |
| 499 | * @return whether the VibrationEffect is valid |
| 500 | */ |
| 501 | private static boolean verifyVibrationEffect(VibrationEffect effect) { |
| 502 | if (effect == null) { |
| 503 | // Effect must not be null. |
| 504 | Slog.wtf(TAG, "effect must not be null"); |
| 505 | return false; |
| 506 | } |
| 507 | try { |
| 508 | effect.validate(); |
| 509 | } catch (Exception e) { |
| 510 | Slog.wtf(TAG, "Encountered issue when verifying VibrationEffect.", e); |
| 511 | return false; |
| 512 | } |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | private static long[] getLongIntArray(Resources r, int resid) { |
| 517 | int[] ar = r.getIntArray(resid); |
| 518 | if (ar == null) { |
| 519 | return null; |
| 520 | } |
| 521 | long[] out = new long[ar.length]; |
| 522 | for (int i = 0; i < ar.length; i++) { |
| 523 | out[i] = ar[i]; |
| 524 | } |
| 525 | return out; |
| 526 | } |
| 527 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 528 | @Override // Binder call |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 529 | public void vibrate(int uid, String opPkg, VibrationEffect effect, int usageHint, String reason, |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 530 | IBinder token) { |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 531 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "vibrate, reason = " + reason); |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 532 | try { |
| 533 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE) |
| 534 | != PackageManager.PERMISSION_GRANTED) { |
| 535 | throw new SecurityException("Requires VIBRATE permission"); |
Alexey Kuzmin | 7f38fb1 | 2018-02-07 13:51:28 +0000 | [diff] [blame] | 536 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 537 | if (token == null) { |
| 538 | Slog.e(TAG, "token must not be null"); |
| 539 | return; |
| 540 | } |
| 541 | verifyIncomingUid(uid); |
| 542 | if (!verifyVibrationEffect(effect)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 546 | // If our current vibration is longer than the new vibration and is the same amplitude, |
| 547 | // then just let the current one finish. |
| 548 | synchronized (mLock) { |
| 549 | if (effect instanceof VibrationEffect.OneShot |
| 550 | && mCurrentVibration != null |
| 551 | && mCurrentVibration.effect instanceof VibrationEffect.OneShot) { |
| 552 | VibrationEffect.OneShot newOneShot = (VibrationEffect.OneShot) effect; |
| 553 | VibrationEffect.OneShot currentOneShot = |
| 554 | (VibrationEffect.OneShot) mCurrentVibration.effect; |
| 555 | if (mCurrentVibration.hasTimeoutLongerThan(newOneShot.getDuration()) |
| 556 | && newOneShot.getAmplitude() == currentOneShot.getAmplitude()) { |
| 557 | if (DEBUG) { |
| 558 | Slog.d(TAG, |
| 559 | "Ignoring incoming vibration in favor of current vibration"); |
| 560 | } |
| 561 | return; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | // If the current vibration is repeating and the incoming one is non-repeating, |
| 566 | // then ignore the non-repeating vibration. This is so that we don't cancel |
| 567 | // vibrations that are meant to grab the attention of the user, like ringtones and |
| 568 | // alarms, in favor of one-shot vibrations that are likely quite short. |
| 569 | if (!isRepeatingVibration(effect) |
| 570 | && mCurrentVibration != null |
| 571 | && isRepeatingVibration(mCurrentVibration.effect)) { |
| 572 | if (DEBUG) { |
| 573 | Slog.d(TAG, "Ignoring incoming vibration in favor of alarm vibration"); |
| 574 | } |
| 575 | return; |
| 576 | } |
| 577 | |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 578 | Vibration vib = new Vibration(token, effect, usageHint, uid, opPkg, reason); |
Alexey Kuzmin | ddebe59 | 2019-01-08 16:07:41 +0000 | [diff] [blame] | 579 | if (mProcStatesCache.get(uid, ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) |
| 580 | > ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND |
| 581 | && vib.isHapticFeedback()) { |
| 582 | Slog.e(TAG, "Ignoring incoming vibration as process with uid = " |
| 583 | + uid + " is background"); |
| 584 | return; |
| 585 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 586 | linkVibration(vib); |
| 587 | long ident = Binder.clearCallingIdentity(); |
| 588 | try { |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 589 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 590 | doCancelVibrateLocked(); |
| 591 | startVibrationLocked(vib); |
| 592 | addToPreviousVibrationsLocked(vib); |
| 593 | } finally { |
| 594 | Binder.restoreCallingIdentity(ident); |
| 595 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 597 | } finally { |
| 598 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
Michael Wright | 58c4631 | 2017-10-05 14:04:14 -0400 | [diff] [blame] | 602 | private static boolean isRepeatingVibration(VibrationEffect effect) { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 603 | return effect.getDuration() == Long.MAX_VALUE; |
Michael Wright | 58c4631 | 2017-10-05 14:04:14 -0400 | [diff] [blame] | 604 | } |
| 605 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 606 | private void addToPreviousVibrationsLocked(Vibration vib) { |
| 607 | if (mPreviousVibrations.size() > mPreviousVibrationsLimit) { |
| 608 | mPreviousVibrations.removeFirst(); |
| 609 | } |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 610 | mPreviousVibrations.addLast(vib.toInfo()); |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 613 | @Override // Binder call |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 614 | public void cancelVibrate(IBinder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | mContext.enforceCallingOrSelfPermission( |
| 616 | android.Manifest.permission.VIBRATE, |
| 617 | "cancelVibrate"); |
| 618 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 619 | synchronized (mLock) { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 620 | if (mCurrentVibration != null && mCurrentVibration.token == token) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 621 | if (DEBUG) { |
| 622 | Slog.d(TAG, "Canceling vibration."); |
| 623 | } |
| 624 | long ident = Binder.clearCallingIdentity(); |
| 625 | try { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 626 | doCancelVibrateLocked(); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 627 | } finally { |
| 628 | Binder.restoreCallingIdentity(ident); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 629 | } |
| 630 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | } |
Eric Olsen | f42f15c | 2009-10-29 16:42:03 -0700 | [diff] [blame] | 633 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 634 | private final Runnable mVibrationEndRunnable = new Runnable() { |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 635 | @Override |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 636 | public void run() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 637 | onVibrationFinished(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 638 | } |
| 639 | }; |
| 640 | |
Alexey Kuzmin | 7f38fb1 | 2018-02-07 13:51:28 +0000 | [diff] [blame] | 641 | @GuardedBy("mLock") |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 642 | private void doCancelVibrateLocked() { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 643 | Trace.asyncTraceEnd(Trace.TRACE_TAG_VIBRATOR, "vibration", 0); |
| 644 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "doCancelVibrateLocked"); |
| 645 | try { |
| 646 | mH.removeCallbacks(mVibrationEndRunnable); |
| 647 | if (mThread != null) { |
| 648 | mThread.cancel(); |
| 649 | mThread = null; |
| 650 | } |
| 651 | doVibratorOff(); |
| 652 | reportFinishVibrationLocked(); |
| 653 | } finally { |
| 654 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 655 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 656 | } |
| 657 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 658 | // Callback for whenever the current vibration has finished played out |
| 659 | public void onVibrationFinished() { |
| 660 | if (DEBUG) { |
| 661 | Slog.e(TAG, "Vibration finished, cleaning up"); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 662 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 663 | synchronized (mLock) { |
| 664 | // Make sure the vibration is really done. This also reports that the vibration is |
| 665 | // finished. |
| 666 | doCancelVibrateLocked(); |
| 667 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 668 | } |
| 669 | |
Alexey Kuzmin | 7f38fb1 | 2018-02-07 13:51:28 +0000 | [diff] [blame] | 670 | @GuardedBy("mLock") |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 671 | private void startVibrationLocked(final Vibration vib) { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 672 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "startVibrationLocked"); |
| 673 | try { |
| 674 | if (!isAllowedToVibrateLocked(vib)) { |
| 675 | return; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 676 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 677 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 678 | final int intensity = getCurrentIntensityLocked(vib); |
| 679 | if (intensity == Vibrator.VIBRATION_INTENSITY_OFF) { |
| 680 | return; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 681 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 682 | |
| 683 | if (vib.isRingtone() && !shouldVibrateForRingtone()) { |
| 684 | if (DEBUG) { |
| 685 | Slog.e(TAG, "Vibrate ignored, not vibrating for ringtones"); |
| 686 | } |
| 687 | return; |
| 688 | } |
| 689 | |
| 690 | final int mode = getAppOpMode(vib); |
| 691 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 692 | if (mode == AppOpsManager.MODE_ERRORED) { |
| 693 | // We might be getting calls from within system_server, so we don't actually |
| 694 | // want to throw a SecurityException here. |
| 695 | Slog.w(TAG, "Would be an error: vibrate from uid " + vib.uid); |
| 696 | } |
| 697 | return; |
| 698 | } |
| 699 | applyVibrationIntensityScalingLocked(vib, intensity); |
| 700 | startVibrationInnerLocked(vib); |
| 701 | } finally { |
| 702 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 703 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Alexey Kuzmin | 7f38fb1 | 2018-02-07 13:51:28 +0000 | [diff] [blame] | 706 | @GuardedBy("mLock") |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 707 | private void startVibrationInnerLocked(Vibration vib) { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 708 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "startVibrationInnerLocked"); |
| 709 | try { |
| 710 | mCurrentVibration = vib; |
| 711 | if (vib.effect instanceof VibrationEffect.OneShot) { |
| 712 | Trace.asyncTraceBegin(Trace.TRACE_TAG_VIBRATOR, "vibration", 0); |
| 713 | VibrationEffect.OneShot oneShot = (VibrationEffect.OneShot) vib.effect; |
| 714 | doVibratorOn(oneShot.getDuration(), oneShot.getAmplitude(), vib.uid, vib.usageHint); |
| 715 | mH.postDelayed(mVibrationEndRunnable, oneShot.getDuration()); |
| 716 | } else if (vib.effect instanceof VibrationEffect.Waveform) { |
| 717 | // mThread better be null here. doCancelVibrate should always be |
| 718 | // called before startNextVibrationLocked or startVibrationLocked. |
| 719 | Trace.asyncTraceBegin(Trace.TRACE_TAG_VIBRATOR, "vibration", 0); |
| 720 | VibrationEffect.Waveform waveform = (VibrationEffect.Waveform) vib.effect; |
| 721 | mThread = new VibrateThread(waveform, vib.uid, vib.usageHint); |
| 722 | mThread.start(); |
| 723 | } else if (vib.effect instanceof VibrationEffect.Prebaked) { |
| 724 | Trace.asyncTraceBegin(Trace.TRACE_TAG_VIBRATOR, "vibration", 0); |
| 725 | long timeout = doVibratorPrebakedEffectLocked(vib); |
| 726 | if (timeout > 0) { |
| 727 | mH.postDelayed(mVibrationEndRunnable, timeout); |
| 728 | } |
| 729 | } else { |
| 730 | Slog.e(TAG, "Unknown vibration type, ignoring"); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 731 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 732 | } finally { |
| 733 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 737 | private boolean isAllowedToVibrateLocked(Vibration vib) { |
Tyler Freeman | 319a34a | 2017-05-04 17:23:35 -0700 | [diff] [blame] | 738 | if (!mLowPowerMode) { |
| 739 | return true; |
| 740 | } |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 741 | |
| 742 | if (vib.usageHint == AudioAttributes.USAGE_NOTIFICATION_RINGTONE) { |
Tyler Freeman | 319a34a | 2017-05-04 17:23:35 -0700 | [diff] [blame] | 743 | return true; |
| 744 | } |
Tyler Freeman | 319a34a | 2017-05-04 17:23:35 -0700 | [diff] [blame] | 745 | |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 746 | if (vib.usageHint == AudioAttributes.USAGE_ALARM || |
| 747 | vib.usageHint == AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY || |
| 748 | vib.usageHint == AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST) { |
Tyler Freeman | 319a34a | 2017-05-04 17:23:35 -0700 | [diff] [blame] | 749 | return true; |
| 750 | } |
| 751 | |
| 752 | return false; |
| 753 | } |
| 754 | |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 755 | private int getCurrentIntensityLocked(Vibration vib) { |
Alexey Kuzmin | ccdaebb | 2018-12-10 12:02:51 +0000 | [diff] [blame] | 756 | if (vib.isRingtone()) { |
| 757 | return mRingIntensity; |
| 758 | } else if (vib.isNotification()) { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 759 | return mNotificationIntensity; |
| 760 | } else if (vib.isHapticFeedback()) { |
| 761 | return mHapticFeedbackIntensity; |
| 762 | } else { |
| 763 | return Vibrator.VIBRATION_INTENSITY_MEDIUM; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Scale the vibration effect by the intensity as appropriate based its intent. |
| 769 | */ |
| 770 | private void applyVibrationIntensityScalingLocked(Vibration vib, int intensity) { |
| 771 | if (vib.effect instanceof VibrationEffect.Prebaked) { |
| 772 | // Prebaked effects are always just a direct translation from intensity to |
| 773 | // EffectStrength. |
| 774 | VibrationEffect.Prebaked prebaked = (VibrationEffect.Prebaked)vib.effect; |
| 775 | prebaked.setEffectStrength(intensityToEffectStrength(intensity)); |
| 776 | return; |
| 777 | } |
| 778 | |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 779 | final int defaultIntensity; |
Alexey Kuzmin | ccdaebb | 2018-12-10 12:02:51 +0000 | [diff] [blame] | 780 | if (vib.isRingtone()) { |
| 781 | defaultIntensity = mVibrator.getDefaultRingVibrationIntensity(); |
| 782 | } else if (vib.isNotification()) { |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 783 | defaultIntensity = mVibrator.getDefaultNotificationVibrationIntensity(); |
| 784 | } else if (vib.isHapticFeedback()) { |
| 785 | defaultIntensity = mVibrator.getDefaultHapticFeedbackIntensity(); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 786 | } else { |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 787 | // If we don't know what kind of vibration we're playing then just skip scaling for |
| 788 | // now. |
| 789 | return; |
| 790 | } |
| 791 | |
| 792 | final ScaleLevel scale = mScaleLevels.get(intensity - defaultIntensity); |
| 793 | if (scale == null) { |
| 794 | // We should have scaling levels for all cases, so not being able to scale because of a |
| 795 | // missing level is unexpected. |
| 796 | Slog.e(TAG, "No configured scaling level!" |
| 797 | + " (current=" + intensity + ", default= " + defaultIntensity + ")"); |
| 798 | return; |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | VibrationEffect scaledEffect = null; |
| 802 | if (vib.effect instanceof VibrationEffect.OneShot) { |
| 803 | VibrationEffect.OneShot oneShot = (VibrationEffect.OneShot) vib.effect; |
Alexey Kuzmin | 59efe97 | 2018-04-24 12:58:13 +0100 | [diff] [blame] | 804 | oneShot = oneShot.resolve(mDefaultVibrationAmplitude); |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 805 | scaledEffect = oneShot.scale(scale.gamma, scale.maxAmplitude); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 806 | } else if (vib.effect instanceof VibrationEffect.Waveform) { |
| 807 | VibrationEffect.Waveform waveform = (VibrationEffect.Waveform) vib.effect; |
Alexey Kuzmin | 59efe97 | 2018-04-24 12:58:13 +0100 | [diff] [blame] | 808 | waveform = waveform.resolve(mDefaultVibrationAmplitude); |
Michael Wright | 0dbb516 | 2018-05-25 15:13:36 +0100 | [diff] [blame] | 809 | scaledEffect = waveform.scale(scale.gamma, scale.maxAmplitude); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 810 | } else { |
| 811 | Slog.w(TAG, "Unable to apply intensity scaling, unknown VibrationEffect type"); |
| 812 | } |
| 813 | |
| 814 | if (scaledEffect != null) { |
| 815 | vib.originalEffect = vib.effect; |
| 816 | vib.effect = scaledEffect; |
| 817 | } |
| 818 | } |
| 819 | |
Brad Ebinger | 2d1c3b3 | 2016-05-12 18:05:17 -0700 | [diff] [blame] | 820 | private boolean shouldVibrateForRingtone() { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 821 | AudioManager audioManager = mContext.getSystemService(AudioManager.class); |
Brad Ebinger | dcbdc0d | 2016-06-23 17:42:30 -0700 | [diff] [blame] | 822 | int ringerMode = audioManager.getRingerModeInternal(); |
Brad Ebinger | 2d1c3b3 | 2016-05-12 18:05:17 -0700 | [diff] [blame] | 823 | // "Also vibrate for calls" Setting in Sound |
| 824 | if (Settings.System.getInt( |
| 825 | mContext.getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 0) != 0) { |
| 826 | return ringerMode != AudioManager.RINGER_MODE_SILENT; |
| 827 | } else { |
| 828 | return ringerMode == AudioManager.RINGER_MODE_VIBRATE; |
| 829 | } |
| 830 | } |
| 831 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 832 | private int getAppOpMode(Vibration vib) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 833 | int mode = mAppOps.checkAudioOpNoThrow(AppOpsManager.OP_VIBRATE, |
| 834 | vib.usageHint, vib.uid, vib.opPkg); |
| 835 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 836 | mode = mAppOps.startOpNoThrow(AppOpsManager.OP_VIBRATE, vib.uid, vib.opPkg); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 837 | } |
| 838 | return mode; |
| 839 | } |
| 840 | |
Alexey Kuzmin | 7f38fb1 | 2018-02-07 13:51:28 +0000 | [diff] [blame] | 841 | @GuardedBy("mLock") |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 842 | private void reportFinishVibrationLocked() { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 843 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "reportFinishVibrationLocked"); |
| 844 | try { |
| 845 | if (mCurrentVibration != null) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 846 | mAppOps.finishOp(AppOpsManager.OP_VIBRATE, mCurrentVibration.uid, |
| 847 | mCurrentVibration.opPkg); |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 848 | unlinkVibration(mCurrentVibration); |
| 849 | mCurrentVibration = null; |
| 850 | } |
| 851 | } finally { |
| 852 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 856 | private void linkVibration(Vibration vib) { |
| 857 | // Only link against waveforms since they potentially don't have a finish if |
| 858 | // they're repeating. Let other effects just play out until they're done. |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 859 | if (vib.effect instanceof VibrationEffect.Waveform) { |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 860 | try { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 861 | vib.token.linkToDeath(vib, 0); |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 862 | } catch (RemoteException e) { |
| 863 | return; |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
Mathias Jeppsson | b23949b | 2010-09-28 14:45:23 +0200 | [diff] [blame] | 868 | private void unlinkVibration(Vibration vib) { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 869 | if (vib.effect instanceof VibrationEffect.Waveform) { |
| 870 | vib.token.unlinkToDeath(vib, 0); |
Mathias Jeppsson | b23949b | 2010-09-28 14:45:23 +0200 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 874 | private void updateVibrators() { |
| 875 | synchronized (mLock) { |
| 876 | boolean devicesUpdated = updateInputDeviceVibratorsLocked(); |
| 877 | boolean lowPowerModeUpdated = updateLowPowerModeLocked(); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 878 | updateVibrationIntensityLocked(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 879 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 880 | if (devicesUpdated || lowPowerModeUpdated) { |
| 881 | // If the state changes out from under us then just reset. |
| 882 | doCancelVibrateLocked(); |
| 883 | } |
| 884 | } |
| 885 | } |
Jeff Brown | 8206525 | 2012-04-16 13:19:05 -0700 | [diff] [blame] | 886 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 887 | private boolean updateInputDeviceVibratorsLocked() { |
| 888 | boolean changed = false; |
| 889 | boolean vibrateInputDevices = false; |
| 890 | try { |
| 891 | vibrateInputDevices = Settings.System.getIntForUser( |
| 892 | mContext.getContentResolver(), |
| 893 | Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0; |
| 894 | } catch (SettingNotFoundException snfe) { |
| 895 | } |
| 896 | if (vibrateInputDevices != mVibrateInputDevicesSetting) { |
| 897 | changed = true; |
| 898 | mVibrateInputDevicesSetting = vibrateInputDevices; |
| 899 | } |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 900 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 901 | if (mVibrateInputDevicesSetting) { |
| 902 | if (!mInputDeviceListenerRegistered) { |
| 903 | mInputDeviceListenerRegistered = true; |
| 904 | mIm.registerInputDeviceListener(this, mH); |
| 905 | } |
| 906 | } else { |
| 907 | if (mInputDeviceListenerRegistered) { |
| 908 | mInputDeviceListenerRegistered = false; |
| 909 | mIm.unregisterInputDeviceListener(this); |
| 910 | } |
| 911 | } |
Jeff Brown | 8206525 | 2012-04-16 13:19:05 -0700 | [diff] [blame] | 912 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 913 | mInputDeviceVibrators.clear(); |
| 914 | if (mVibrateInputDevicesSetting) { |
| 915 | int[] ids = mIm.getInputDeviceIds(); |
| 916 | for (int i = 0; i < ids.length; i++) { |
| 917 | InputDevice device = mIm.getInputDevice(ids[i]); |
| 918 | Vibrator vibrator = device.getVibrator(); |
| 919 | if (vibrator.hasVibrator()) { |
| 920 | mInputDeviceVibrators.add(vibrator); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 921 | } |
| 922 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 923 | return true; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 924 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 925 | return changed; |
| 926 | } |
| 927 | |
| 928 | private boolean updateLowPowerModeLocked() { |
| 929 | boolean lowPowerMode = mPowerManagerInternal |
| 930 | .getLowPowerState(ServiceType.VIBRATION).batterySaverEnabled; |
| 931 | if (lowPowerMode != mLowPowerMode) { |
| 932 | mLowPowerMode = lowPowerMode; |
| 933 | return true; |
| 934 | } |
| 935 | return false; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 938 | private void updateVibrationIntensityLocked() { |
| 939 | mHapticFeedbackIntensity = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 940 | Settings.System.HAPTIC_FEEDBACK_INTENSITY, |
| 941 | mVibrator.getDefaultHapticFeedbackIntensity(), UserHandle.USER_CURRENT); |
| 942 | mNotificationIntensity = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 943 | Settings.System.NOTIFICATION_VIBRATION_INTENSITY, |
| 944 | mVibrator.getDefaultNotificationVibrationIntensity(), UserHandle.USER_CURRENT); |
Alexey Kuzmin | ccdaebb | 2018-12-10 12:02:51 +0000 | [diff] [blame] | 945 | mRingIntensity = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 946 | Settings.System.RING_VIBRATION_INTENSITY, |
| 947 | mVibrator.getDefaultRingVibrationIntensity(), UserHandle.USER_CURRENT); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 950 | @Override |
| 951 | public void onInputDeviceAdded(int deviceId) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 952 | updateVibrators(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | @Override |
| 956 | public void onInputDeviceChanged(int deviceId) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 957 | updateVibrators(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | @Override |
| 961 | public void onInputDeviceRemoved(int deviceId) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 962 | updateVibrators(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | private boolean doVibratorExists() { |
Jeff Brown | 1064a50 | 2012-05-02 16:51:37 -0700 | [diff] [blame] | 966 | // For now, we choose to ignore the presence of input devices that have vibrators |
| 967 | // when reporting whether the device has a vibrator. Applications often use this |
| 968 | // information to decide whether to enable certain features so they expect the |
| 969 | // result of hasVibrator() to be constant. For now, just report whether |
| 970 | // the device has a built-in vibrator. |
| 971 | //synchronized (mInputDeviceVibrators) { |
| 972 | // return !mInputDeviceVibrators.isEmpty() || vibratorExists(); |
| 973 | //} |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 974 | return vibratorExists(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 977 | private void doVibratorOn(long millis, int amplitude, int uid, int usageHint) { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 978 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "doVibratorOn"); |
| 979 | try { |
| 980 | synchronized (mInputDeviceVibrators) { |
| 981 | if (amplitude == VibrationEffect.DEFAULT_AMPLITUDE) { |
| 982 | amplitude = mDefaultVibrationAmplitude; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 983 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 984 | if (DEBUG) { |
| 985 | Slog.d(TAG, "Turning vibrator on for " + millis + " ms" + |
| 986 | " with amplitude " + amplitude + "."); |
| 987 | } |
| 988 | noteVibratorOnLocked(uid, millis); |
| 989 | final int vibratorCount = mInputDeviceVibrators.size(); |
| 990 | if (vibratorCount != 0) { |
| 991 | final AudioAttributes attributes = |
| 992 | new AudioAttributes.Builder().setUsage(usageHint).build(); |
| 993 | for (int i = 0; i < vibratorCount; i++) { |
| 994 | mInputDeviceVibrators.get(i).vibrate(millis, attributes); |
| 995 | } |
| 996 | } else { |
| 997 | // Note: ordering is important here! Many haptic drivers will reset their |
| 998 | // amplitude when enabled, so we always have to enable frst, then set the |
| 999 | // amplitude. |
| 1000 | vibratorOn(millis); |
| 1001 | doVibratorSetAmplitude(amplitude); |
| 1002 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 1003 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1004 | } finally { |
| 1005 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1009 | private void doVibratorSetAmplitude(int amplitude) { |
| 1010 | if (mSupportsAmplitudeControl) { |
| 1011 | vibratorSetAmplitude(amplitude); |
| 1012 | } |
| 1013 | } |
| 1014 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 1015 | private void doVibratorOff() { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1016 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "doVibratorOff"); |
| 1017 | try { |
| 1018 | synchronized (mInputDeviceVibrators) { |
| 1019 | if (DEBUG) { |
| 1020 | Slog.d(TAG, "Turning vibrator off."); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 1021 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1022 | noteVibratorOffLocked(); |
| 1023 | final int vibratorCount = mInputDeviceVibrators.size(); |
| 1024 | if (vibratorCount != 0) { |
| 1025 | for (int i = 0; i < vibratorCount; i++) { |
| 1026 | mInputDeviceVibrators.get(i).cancel(); |
| 1027 | } |
| 1028 | } else { |
| 1029 | vibratorOff(); |
| 1030 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 1031 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1032 | } finally { |
| 1033 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
Alexey Kuzmin | 7f38fb1 | 2018-02-07 13:51:28 +0000 | [diff] [blame] | 1037 | @GuardedBy("mLock") |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1038 | private long doVibratorPrebakedEffectLocked(Vibration vib) { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1039 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "doVibratorPrebakedEffectLocked"); |
| 1040 | try { |
| 1041 | final VibrationEffect.Prebaked prebaked = (VibrationEffect.Prebaked) vib.effect; |
| 1042 | final boolean usingInputDeviceVibrators; |
| 1043 | synchronized (mInputDeviceVibrators) { |
| 1044 | usingInputDeviceVibrators = !mInputDeviceVibrators.isEmpty(); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 1045 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1046 | // Input devices don't support prebaked effect, so skip trying it with them. |
| 1047 | if (!usingInputDeviceVibrators) { |
| 1048 | long timeout = vibratorPerformEffect(prebaked.getId(), |
| 1049 | prebaked.getEffectStrength()); |
| 1050 | if (timeout > 0) { |
| 1051 | noteVibratorOnLocked(vib.uid, timeout); |
| 1052 | return timeout; |
| 1053 | } |
| 1054 | } |
| 1055 | if (!prebaked.shouldFallback()) { |
| 1056 | return 0; |
| 1057 | } |
| 1058 | VibrationEffect effect = getFallbackEffect(prebaked.getId()); |
| 1059 | if (effect == null) { |
| 1060 | Slog.w(TAG, "Failed to play prebaked effect, no fallback"); |
| 1061 | return 0; |
| 1062 | } |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 1063 | Vibration fallbackVib = new Vibration(vib.token, effect, vib.usageHint, vib.uid, |
| 1064 | vib.opPkg, vib.reason + " (fallback)"); |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1065 | final int intensity = getCurrentIntensityLocked(fallbackVib); |
| 1066 | linkVibration(fallbackVib); |
| 1067 | applyVibrationIntensityScalingLocked(fallbackVib, intensity); |
| 1068 | startVibrationInnerLocked(fallbackVib); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 1069 | return 0; |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1070 | } finally { |
| 1071 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 1072 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1073 | } |
Eric Olsen | f42f15c | 2009-10-29 16:42:03 -0700 | [diff] [blame] | 1074 | |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 1075 | private VibrationEffect getFallbackEffect(int effectId) { |
Alexey Kuzmin | 5a0a26f | 2018-03-20 18:25:51 +0000 | [diff] [blame] | 1076 | return mFallbackEffects.get(effectId); |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 1079 | /** |
| 1080 | * Return the current desired effect strength. |
| 1081 | * |
| 1082 | * If the returned value is < 0 then the vibration shouldn't be played at all. |
| 1083 | */ |
| 1084 | private static int intensityToEffectStrength(int intensity) { |
| 1085 | switch (intensity) { |
| 1086 | case Vibrator.VIBRATION_INTENSITY_LOW: |
| 1087 | return EffectStrength.LIGHT; |
| 1088 | case Vibrator.VIBRATION_INTENSITY_MEDIUM: |
| 1089 | return EffectStrength.MEDIUM; |
| 1090 | case Vibrator.VIBRATION_INTENSITY_HIGH: |
| 1091 | return EffectStrength.STRONG; |
| 1092 | default: |
| 1093 | Slog.w(TAG, "Got unexpected vibration intensity: " + intensity); |
| 1094 | return EffectStrength.STRONG; |
| 1095 | } |
| 1096 | } |
| 1097 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1098 | private void noteVibratorOnLocked(int uid, long millis) { |
| 1099 | try { |
| 1100 | mBatteryStatsService.noteVibratorOn(uid, millis); |
Bookatz | a7020bd | 2018-08-28 16:29:35 -0700 | [diff] [blame] | 1101 | StatsLog.write_non_chained(StatsLog.VIBRATOR_STATE_CHANGED, uid, null, |
| 1102 | StatsLog.VIBRATOR_STATE_CHANGED__STATE__ON, millis); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1103 | mCurVibUid = uid; |
| 1104 | } catch (RemoteException e) { |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | private void noteVibratorOffLocked() { |
| 1109 | if (mCurVibUid >= 0) { |
| 1110 | try { |
| 1111 | mBatteryStatsService.noteVibratorOff(mCurVibUid); |
Bookatz | a7020bd | 2018-08-28 16:29:35 -0700 | [diff] [blame] | 1112 | StatsLog.write_non_chained(StatsLog.VIBRATOR_STATE_CHANGED, mCurVibUid, null, |
| 1113 | StatsLog.VIBRATOR_STATE_CHANGED__STATE__OFF, 0); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1114 | } catch (RemoteException e) { } |
| 1115 | mCurVibUid = -1; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | private class VibrateThread extends Thread { |
| 1120 | private final VibrationEffect.Waveform mWaveform; |
| 1121 | private final int mUid; |
| 1122 | private final int mUsageHint; |
| 1123 | |
| 1124 | private boolean mForceStop; |
| 1125 | |
| 1126 | VibrateThread(VibrationEffect.Waveform waveform, int uid, int usageHint) { |
| 1127 | mWaveform = waveform; |
| 1128 | mUid = uid; |
| 1129 | mUsageHint = usageHint; |
| 1130 | mTmpWorkSource.set(uid); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1131 | mWakeLock.setWorkSource(mTmpWorkSource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1134 | private long delayLocked(long duration) { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1135 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "delayLocked"); |
| 1136 | try { |
| 1137 | long durationRemaining = duration; |
| 1138 | if (duration > 0) { |
| 1139 | final long bedtime = duration + SystemClock.uptimeMillis(); |
| 1140 | do { |
| 1141 | try { |
| 1142 | this.wait(durationRemaining); |
| 1143 | } |
| 1144 | catch (InterruptedException e) { } |
| 1145 | if (mForceStop) { |
| 1146 | break; |
| 1147 | } |
| 1148 | durationRemaining = bedtime - SystemClock.uptimeMillis(); |
| 1149 | } while (durationRemaining > 0); |
| 1150 | return duration - durationRemaining; |
| 1151 | } |
| 1152 | return 0; |
| 1153 | } finally { |
| 1154 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | public void run() { |
| 1159 | Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1160 | mWakeLock.acquire(); |
| 1161 | try { |
| 1162 | boolean finished = playWaveform(); |
| 1163 | if (finished) { |
| 1164 | onVibrationFinished(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1166 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | mWakeLock.release(); |
| 1168 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * Play the waveform. |
| 1173 | * |
| 1174 | * @return true if it finished naturally, false otherwise (e.g. it was canceled). |
| 1175 | */ |
| 1176 | public boolean playWaveform() { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1177 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "playWaveform"); |
| 1178 | try { |
| 1179 | synchronized (this) { |
| 1180 | final long[] timings = mWaveform.getTimings(); |
| 1181 | final int[] amplitudes = mWaveform.getAmplitudes(); |
| 1182 | final int len = timings.length; |
| 1183 | final int repeat = mWaveform.getRepeatIndex(); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1184 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1185 | int index = 0; |
| 1186 | long onDuration = 0; |
| 1187 | while (!mForceStop) { |
| 1188 | if (index < len) { |
| 1189 | final int amplitude = amplitudes[index]; |
| 1190 | final long duration = timings[index++]; |
| 1191 | if (duration <= 0) { |
| 1192 | continue; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1193 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1194 | if (amplitude != 0) { |
| 1195 | if (onDuration <= 0) { |
| 1196 | // Telling the vibrator to start multiple times usually causes |
| 1197 | // effects to feel "choppy" because the motor resets at every on |
| 1198 | // command. Instead we figure out how long our next "on" period |
| 1199 | // is going to be, tell the motor to stay on for the full |
| 1200 | // duration, and then wake up to change the amplitude at the |
| 1201 | // appropriate intervals. |
| 1202 | onDuration = getTotalOnDuration(timings, amplitudes, index - 1, |
| 1203 | repeat); |
| 1204 | doVibratorOn(onDuration, amplitude, mUid, mUsageHint); |
| 1205 | } else { |
| 1206 | doVibratorSetAmplitude(amplitude); |
| 1207 | } |
| 1208 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1209 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1210 | long waitTime = delayLocked(duration); |
| 1211 | if (amplitude != 0) { |
| 1212 | onDuration -= waitTime; |
| 1213 | } |
| 1214 | } else if (repeat < 0) { |
| 1215 | break; |
| 1216 | } else { |
| 1217 | index = repeat; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1218 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1219 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1220 | return !mForceStop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | } |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1222 | } finally { |
| 1223 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | public void cancel() { |
| 1228 | synchronized (this) { |
| 1229 | mThread.mForceStop = true; |
| 1230 | mThread.notify(); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /** |
| 1235 | * Get the duration the vibrator will be on starting at startIndex until the next time it's |
| 1236 | * off. |
| 1237 | */ |
| 1238 | private long getTotalOnDuration( |
| 1239 | long[] timings, int[] amplitudes, int startIndex, int repeatIndex) { |
| 1240 | int i = startIndex; |
| 1241 | long timing = 0; |
| 1242 | while(amplitudes[i] != 0) { |
| 1243 | timing += timings[i++]; |
| 1244 | if (i >= timings.length) { |
| 1245 | if (repeatIndex >= 0) { |
| 1246 | i = repeatIndex; |
Harpreet "Eli" Sangha | c5526dd | 2018-10-30 16:14:33 +0900 | [diff] [blame] | 1247 | // prevent infinite loop |
| 1248 | repeatIndex = -1; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1249 | } else { |
| 1250 | break; |
| 1251 | } |
| 1252 | } |
| 1253 | if (i == startIndex) { |
| 1254 | return 1000; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 1255 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1257 | return timing; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | } |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 1259 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1260 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1261 | BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 1262 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | public void onReceive(Context context, Intent intent) { |
| 1264 | if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1265 | synchronized (mLock) { |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 1266 | // When the system is entering a non-interactive state, we want |
| 1267 | // to cancel vibrations in case a misbehaving app has abandoned |
| 1268 | // them. However it may happen that the system is currently playing |
| 1269 | // haptic feedback as part of the transition. So we don't cancel |
| 1270 | // system vibrations. |
| 1271 | if (mCurrentVibration != null |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 1272 | && !(mCurrentVibration.isHapticFeedback() |
| 1273 | && mCurrentVibration.isFromSystem())) { |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 1274 | doCancelVibrateLocked(); |
Vairavan Srinivasan | 8a61f49 | 2011-05-13 10:47:20 -0700 | [diff] [blame] | 1275 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 1276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | }; |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 1280 | |
| 1281 | @Override |
| 1282 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1283 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 1284 | |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 1285 | pw.println("Vibrator Service:"); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1286 | synchronized (mLock) { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 1287 | pw.print(" mCurrentVibration="); |
| 1288 | if (mCurrentVibration != null) { |
| 1289 | pw.println(mCurrentVibration.toInfo().toString()); |
| 1290 | } else { |
| 1291 | pw.println("null"); |
| 1292 | } |
| 1293 | pw.println(" mLowPowerMode=" + mLowPowerMode); |
| 1294 | pw.println(" mHapticFeedbackIntensity=" + mHapticFeedbackIntensity); |
| 1295 | pw.println(" mNotificationIntensity=" + mNotificationIntensity); |
Alexey Kuzmin | ccdaebb | 2018-12-10 12:02:51 +0000 | [diff] [blame] | 1296 | pw.println(" mRingIntensity=" + mRingIntensity); |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 1297 | pw.println(""); |
| 1298 | pw.println(" Previous vibrations:"); |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 1299 | for (VibrationInfo info : mPreviousVibrations) { |
Michael Wright | 35a0c67 | 2018-01-24 00:32:53 +0000 | [diff] [blame] | 1300 | pw.print(" "); |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 1301 | pw.println(info.toString()); |
| 1302 | } |
| 1303 | } |
| 1304 | } |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 1305 | |
| 1306 | @Override |
| 1307 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 1308 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) |
| 1309 | throws RemoteException { |
| 1310 | new VibratorShellCommand(this).exec(this, in, out, err, args, callback, resultReceiver); |
| 1311 | } |
| 1312 | |
| 1313 | private final class VibratorShellCommand extends ShellCommand { |
| 1314 | |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 1315 | private final IBinder mToken; |
| 1316 | |
| 1317 | private VibratorShellCommand(IBinder token) { |
| 1318 | mToken = token; |
| 1319 | } |
| 1320 | |
| 1321 | @Override |
| 1322 | public int onCommand(String cmd) { |
| 1323 | if ("vibrate".equals(cmd)) { |
| 1324 | return runVibrate(); |
Harpreet "Eli" Sangha | c5526dd | 2018-10-30 16:14:33 +0900 | [diff] [blame] | 1325 | } else if ("waveform".equals(cmd)) { |
| 1326 | return runWaveform(); |
Harpreet "Eli" Sangha | 5ecc536 | 2018-10-16 17:27:58 +0900 | [diff] [blame] | 1327 | } else if ("prebaked".equals(cmd)) { |
| 1328 | return runPrebaked(); |
Harpreet "Eli" Sangha | c5526dd | 2018-10-30 16:14:33 +0900 | [diff] [blame] | 1329 | } else if ("cancel".equals(cmd)) { |
| 1330 | cancelVibrate(mToken); |
| 1331 | return 0; |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 1332 | } |
| 1333 | return handleDefaultCommands(cmd); |
| 1334 | } |
| 1335 | |
Harpreet "Eli" Sangha | 5ecc536 | 2018-10-16 17:27:58 +0900 | [diff] [blame] | 1336 | private boolean checkDoNotDisturb() { |
| 1337 | try { |
| 1338 | final int zenMode = Settings.Global.getInt(mContext.getContentResolver(), |
| 1339 | Settings.Global.ZEN_MODE); |
| 1340 | if (zenMode != Settings.Global.ZEN_MODE_OFF) { |
| 1341 | try (PrintWriter pw = getOutPrintWriter();) { |
| 1342 | pw.print("Ignoring because device is on DND mode "); |
| 1343 | pw.println(DebugUtils.flagsToString(Settings.Global.class, "ZEN_MODE_", |
| 1344 | zenMode)); |
| 1345 | return true; |
| 1346 | } |
| 1347 | } |
| 1348 | } catch (SettingNotFoundException e) { |
| 1349 | // ignore |
| 1350 | } |
| 1351 | |
| 1352 | return false; |
| 1353 | } |
| 1354 | |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 1355 | private int runVibrate() { |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1356 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "runVibrate"); |
Felipe Leme | 5e2e632 | 2017-07-14 17:25:59 -0700 | [diff] [blame] | 1357 | try { |
Harpreet "Eli" Sangha | 5ecc536 | 2018-10-16 17:27:58 +0900 | [diff] [blame] | 1358 | if (checkDoNotDisturb()) { |
| 1359 | return 0; |
Felipe Leme | 5e2e632 | 2017-07-14 17:25:59 -0700 | [diff] [blame] | 1360 | } |
Felipe Leme | 5e2e632 | 2017-07-14 17:25:59 -0700 | [diff] [blame] | 1361 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1362 | final long duration = Long.parseLong(getNextArgRequired()); |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1363 | String description = getNextArg(); |
| 1364 | if (description == null) { |
| 1365 | description = "Shell command"; |
| 1366 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1367 | |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1368 | VibrationEffect effect = |
| 1369 | VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE); |
| 1370 | vibrate(Binder.getCallingUid(), description, effect, AudioAttributes.USAGE_UNKNOWN, |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 1371 | "Shell Command", mToken); |
Alexey Kuzmin | e59145a | 2018-02-10 15:19:03 +0000 | [diff] [blame] | 1372 | return 0; |
| 1373 | } finally { |
| 1374 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
| 1375 | } |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 1376 | } |
| 1377 | |
Harpreet "Eli" Sangha | c5526dd | 2018-10-30 16:14:33 +0900 | [diff] [blame] | 1378 | private int runWaveform() { |
| 1379 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "runWaveform"); |
| 1380 | try { |
| 1381 | if (checkDoNotDisturb()) { |
| 1382 | return 0; |
| 1383 | } |
| 1384 | |
| 1385 | String description = "Shell command"; |
| 1386 | int repeat = -1; |
| 1387 | ArrayList<Integer> amplitudesList = null; |
| 1388 | |
| 1389 | String opt; |
| 1390 | while ((opt = getNextOption()) != null) { |
| 1391 | switch (opt) { |
| 1392 | case "-d": |
| 1393 | description = getNextArgRequired(); |
| 1394 | break; |
| 1395 | case "-r": |
| 1396 | repeat = Integer.parseInt(getNextArgRequired()); |
| 1397 | break; |
| 1398 | case "-a": |
| 1399 | if (amplitudesList == null) { |
| 1400 | amplitudesList = new ArrayList<Integer>(); |
| 1401 | } |
| 1402 | break; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | ArrayList<Long> timingsList = new ArrayList<Long>(); |
| 1407 | |
| 1408 | String arg; |
| 1409 | while ((arg = getNextArg()) != null) { |
| 1410 | if (amplitudesList != null && amplitudesList.size() < timingsList.size()) { |
| 1411 | amplitudesList.add(Integer.parseInt(arg)); |
| 1412 | } else { |
| 1413 | timingsList.add(Long.parseLong(arg)); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | VibrationEffect effect; |
| 1418 | long[] timings = timingsList.stream().mapToLong(Long::longValue).toArray(); |
| 1419 | if (amplitudesList == null) { |
| 1420 | effect = VibrationEffect.createWaveform(timings, repeat); |
| 1421 | } else { |
| 1422 | int[] amplitudes = |
| 1423 | amplitudesList.stream().mapToInt(Integer::intValue).toArray(); |
| 1424 | effect = VibrationEffect.createWaveform(timings, amplitudes, repeat); |
| 1425 | } |
| 1426 | vibrate(Binder.getCallingUid(), description, effect, AudioAttributes.USAGE_UNKNOWN, |
| 1427 | "Shell Command", mToken); |
| 1428 | return 0; |
| 1429 | } finally { |
| 1430 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
| 1431 | } |
| 1432 | } |
| 1433 | |
Harpreet "Eli" Sangha | 5ecc536 | 2018-10-16 17:27:58 +0900 | [diff] [blame] | 1434 | private int runPrebaked() { |
| 1435 | Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "runPrebaked"); |
| 1436 | try { |
| 1437 | if (checkDoNotDisturb()) { |
| 1438 | return 0; |
| 1439 | } |
| 1440 | |
| 1441 | final int id = Integer.parseInt(getNextArgRequired()); |
| 1442 | |
| 1443 | String description = getNextArg(); |
| 1444 | if (description == null) { |
| 1445 | description = "Shell command"; |
| 1446 | } |
| 1447 | |
| 1448 | VibrationEffect effect = |
| 1449 | VibrationEffect.get(id, false); |
| 1450 | vibrate(Binder.getCallingUid(), description, effect, AudioAttributes.USAGE_UNKNOWN, |
| 1451 | "Shell Command", mToken); |
| 1452 | return 0; |
| 1453 | } finally { |
| 1454 | Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR); |
| 1455 | } |
| 1456 | } |
| 1457 | |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 1458 | @Override |
| 1459 | public void onHelp() { |
| 1460 | try (PrintWriter pw = getOutPrintWriter();) { |
| 1461 | pw.println("Vibrator commands:"); |
| 1462 | pw.println(" help"); |
| 1463 | pw.println(" Prints this help text."); |
| 1464 | pw.println(""); |
| 1465 | pw.println(" vibrate duration [description]"); |
Felipe Leme | 5e2e632 | 2017-07-14 17:25:59 -0700 | [diff] [blame] | 1466 | pw.println(" Vibrates for duration milliseconds; ignored when device is on DND "); |
| 1467 | pw.println(" (Do Not Disturb) mode."); |
Harpreet "Eli" Sangha | c5526dd | 2018-10-30 16:14:33 +0900 | [diff] [blame] | 1468 | pw.println(" waveform [-d description] [-r index] [-a] duration [amplitude] ..."); |
| 1469 | pw.println(" Vibrates for durations and amplitudes in list;"); |
| 1470 | pw.println(" ignored when device is on DND (Do Not Disturb) mode."); |
| 1471 | pw.println(" If -r is provided, the waveform loops back to the specified"); |
| 1472 | pw.println(" index (e.g. 0 loops from the beginning)"); |
| 1473 | pw.println(" If -a is provided, the command accepts duration-amplitude pairs;"); |
| 1474 | pw.println(" otherwise, it accepts durations only and alternates off/on"); |
| 1475 | pw.println(" Duration is in milliseconds; amplitude is a scale of 1-255."); |
Harpreet "Eli" Sangha | 5ecc536 | 2018-10-16 17:27:58 +0900 | [diff] [blame] | 1476 | pw.println(" prebaked effect-id [description]"); |
| 1477 | pw.println(" Vibrates with prebaked effect; ignored when device is on DND "); |
| 1478 | pw.println(" (Do Not Disturb) mode."); |
Harpreet "Eli" Sangha | c5526dd | 2018-10-30 16:14:33 +0900 | [diff] [blame] | 1479 | pw.println(" cancel"); |
| 1480 | pw.println(" Cancels any active vibration"); |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 1481 | pw.println(""); |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1486 | } |