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 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 19 | import android.app.AppOpsManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.content.pm.PackageManager; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 25 | import android.content.res.Resources; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 26 | import android.database.ContentObserver; |
| 27 | import android.hardware.input.InputManager; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 28 | import android.hardware.vibrator.V1_0.Constants.EffectStrength; |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 29 | import android.icu.text.DateFormat; |
Brad Ebinger | 2d1c3b3 | 2016-05-12 18:05:17 -0700 | [diff] [blame] | 30 | import android.media.AudioManager; |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 31 | import android.os.PowerManager.ServiceType; |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 32 | import android.os.PowerSaveState; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 33 | import android.os.BatteryStats; |
Joe Onorato | 95e4f70 | 2009-03-24 19:29:09 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 35 | import android.os.IVibratorService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.os.PowerManager; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 37 | import android.os.PowerManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.os.Process; |
| 39 | import android.os.RemoteException; |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 40 | import android.os.ResultReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.os.IBinder; |
| 42 | import android.os.Binder; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 43 | import android.os.ServiceManager; |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 44 | import android.os.ShellCallback; |
| 45 | import android.os.ShellCommand; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.SystemClock; |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 47 | import android.os.UserHandle; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 48 | import android.os.Vibrator; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 49 | import android.os.VibrationEffect; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 50 | import android.os.WorkSource; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 51 | import android.provider.Settings; |
| 52 | import android.provider.Settings.SettingNotFoundException; |
Felipe Leme | 5e2e632 | 2017-07-14 17:25:59 -0700 | [diff] [blame] | 53 | import android.util.DebugUtils; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 54 | import android.util.Slog; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 55 | import android.view.InputDevice; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 56 | import android.media.AudioAttributes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 58 | import com.android.internal.app.IAppOpsService; |
| 59 | import com.android.internal.app.IBatteryStats; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 60 | import com.android.internal.util.DumpUtils; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 61 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 62 | import java.io.FileDescriptor; |
| 63 | import java.io.PrintWriter; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 64 | import java.util.ArrayList; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 65 | import java.util.LinkedList; |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 66 | import java.util.Date; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 67 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 68 | public class VibratorService extends IVibratorService.Stub |
| 69 | implements InputManager.InputDeviceListener { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 70 | private static final String TAG = "VibratorService"; |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 71 | private static final boolean DEBUG = false; |
Jorim Jaggi | 18f18ae | 2015-09-10 15:48:21 -0700 | [diff] [blame] | 72 | private static final String SYSTEM_UI_PACKAGE = "com.android.systemui"; |
Mike Lockwood | cc9a63d | 2009-11-10 07:50:28 -0500 | [diff] [blame] | 73 | |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 74 | private static final long[] DOUBLE_CLICK_EFFECT_FALLBACK_TIMINGS = { 0, 30, 100, 30 }; |
| 75 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 76 | private final LinkedList<VibrationInfo> mPreviousVibrations; |
| 77 | private final int mPreviousVibrationsLimit; |
Tyler Freeman | 319a34a | 2017-05-04 17:23:35 -0700 | [diff] [blame] | 78 | private final boolean mAllowPriorityVibrationsInLowPowerMode; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 79 | private final boolean mSupportsAmplitudeControl; |
| 80 | private final int mDefaultVibrationAmplitude; |
| 81 | private final VibrationEffect[] mFallbackEffects; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 82 | private final WorkSource mTmpWorkSource = new WorkSource(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 83 | private final Handler mH = new Handler(); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 84 | private final Object mLock = new Object(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 85 | |
| 86 | private final Context mContext; |
| 87 | private final PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 88 | private final IAppOpsService mAppOpsService; |
| 89 | private final IBatteryStats mBatteryStatsService; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 90 | private PowerManagerInternal mPowerManagerInternal; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 91 | private InputManager mIm; |
| 92 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 93 | private volatile VibrateThread mThread; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 94 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 95 | // mInputDeviceVibrators lock should be acquired after mLock, if both are |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 96 | // to be acquired |
| 97 | private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>(); |
| 98 | private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators |
| 99 | private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators |
| 100 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 101 | private Vibration mCurrentVibration; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 102 | private int mCurVibUid = -1; |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 103 | private boolean mLowPowerMode; |
| 104 | private SettingsObserver mSettingObserver; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 105 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 106 | native static boolean vibratorExists(); |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 107 | native static void vibratorInit(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 108 | native static void vibratorOn(long milliseconds); |
| 109 | native static void vibratorOff(); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 110 | native static boolean vibratorSupportsAmplitudeControl(); |
| 111 | native static void vibratorSetAmplitude(int amplitude); |
| 112 | native static long vibratorPerformEffect(long effect, long strength); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 113 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 114 | private class Vibration implements IBinder.DeathRecipient { |
| 115 | private final IBinder mToken; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 116 | private final VibrationEffect mEffect; |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 117 | // Start time in CLOCK_BOOTTIME base. |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 118 | private final long mStartTime; |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 119 | // Start time in unix epoch time. Only to be used for debugging purposes and to correlate |
| 120 | // with other system events, any duration calculations should be done use mStartTime so as |
| 121 | // not to be affected by discontinuities created by RTC adjustments. |
| 122 | private final long mStartTimeDebug; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 123 | private final int mUsageHint; |
| 124 | private final int mUid; |
| 125 | private final String mOpPkg; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 126 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 127 | private Vibration(IBinder token, VibrationEffect effect, |
| 128 | int usageHint, int uid, String opPkg) { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 129 | mToken = token; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 130 | mEffect = effect; |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 131 | mStartTime = SystemClock.elapsedRealtime(); |
| 132 | mStartTimeDebug = System.currentTimeMillis(); |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 133 | mUsageHint = usageHint; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 134 | mUid = uid; |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 135 | mOpPkg = opPkg; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | public void binderDied() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 139 | synchronized (mLock) { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 140 | if (this == mCurrentVibration) { |
| 141 | doCancelVibrateLocked(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | public boolean hasLongerTimeout(long millis) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 147 | // If the current effect is a one shot vibration that will end after the given timeout |
| 148 | // for the new one shot vibration, then just let the current vibration finish. All |
| 149 | // other effect types will get pre-empted. |
| 150 | if (mEffect instanceof VibrationEffect.OneShot) { |
| 151 | VibrationEffect.OneShot oneShot = (VibrationEffect.OneShot) mEffect; |
| 152 | return mStartTime + oneShot.getTiming() > SystemClock.uptimeMillis() + millis; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 153 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 154 | return false; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 155 | } |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 156 | |
| 157 | public boolean isSystemHapticFeedback() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 158 | boolean repeating = false; |
| 159 | if (mEffect instanceof VibrationEffect.Waveform) { |
| 160 | VibrationEffect.Waveform waveform = (VibrationEffect.Waveform) mEffect; |
| 161 | repeating = (waveform.getRepeatIndex() < 0); |
| 162 | } |
Jorim Jaggi | 18f18ae | 2015-09-10 15:48:21 -0700 | [diff] [blame] | 163 | return (mUid == Process.SYSTEM_UID || mUid == 0 || SYSTEM_UI_PACKAGE.equals(mOpPkg)) |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 164 | && !repeating; |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 165 | } |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 166 | |
| 167 | public VibrationInfo toInfo() { |
| 168 | return new VibrationInfo(mStartTimeDebug, mEffect, mUsageHint, mUid, mOpPkg); |
| 169 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 172 | private static class VibrationInfo { |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 173 | private final long mStartTimeDebug; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 174 | private final VibrationEffect mEffect; |
| 175 | private final int mUsageHint; |
| 176 | private final int mUid; |
| 177 | private final String mOpPkg; |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 178 | |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 179 | public VibrationInfo(long startTimeDebug, VibrationEffect effect, |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 180 | int usageHint, int uid, String opPkg) { |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 181 | mStartTimeDebug = startTimeDebug; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 182 | mEffect = effect; |
| 183 | mUsageHint = usageHint; |
| 184 | mUid = uid; |
| 185 | mOpPkg = opPkg; |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public String toString() { |
| 190 | return new StringBuilder() |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 191 | .append("startTime: ") |
| 192 | .append(DateFormat.getDateTimeInstance().format(new Date(mStartTimeDebug))) |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 193 | .append(", effect: ") |
| 194 | .append(mEffect) |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 195 | .append(", usageHint: ") |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 196 | .append(mUsageHint) |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 197 | .append(", uid: ") |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 198 | .append(mUid) |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 199 | .append(", opPkg: ") |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 200 | .append(mOpPkg) |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 201 | .toString(); |
| 202 | } |
| 203 | } |
| 204 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 205 | VibratorService(Context context) { |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 206 | vibratorInit(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | // Reset the hardware to a default state, in case this is a runtime |
| 208 | // restart instead of a fresh boot. |
| 209 | vibratorOff(); |
| 210 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 211 | mSupportsAmplitudeControl = vibratorSupportsAmplitudeControl(); |
| 212 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | mContext = context; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 214 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 215 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | mWakeLock.setReferenceCounted(true); |
| 217 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 218 | mAppOpsService = |
| 219 | IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE)); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 220 | mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 221 | BatteryStats.SERVICE_NAME)); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 222 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 223 | mPreviousVibrationsLimit = mContext.getResources().getInteger( |
| 224 | com.android.internal.R.integer.config_previousVibrationsDumpLimit); |
| 225 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 226 | mDefaultVibrationAmplitude = mContext.getResources().getInteger( |
| 227 | com.android.internal.R.integer.config_defaultVibrationAmplitude); |
| 228 | |
Tyler Freeman | 319a34a | 2017-05-04 17:23:35 -0700 | [diff] [blame] | 229 | mAllowPriorityVibrationsInLowPowerMode = mContext.getResources().getBoolean( |
| 230 | com.android.internal.R.bool.config_allowPriorityVibrationsInLowPowerMode); |
| 231 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 232 | mPreviousVibrations = new LinkedList<>(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 233 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | IntentFilter filter = new IntentFilter(); |
| 235 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 236 | context.registerReceiver(mIntentReceiver, filter); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 237 | |
| 238 | long[] clickEffectTimings = getLongIntArray(context.getResources(), |
| 239 | com.android.internal.R.array.config_virtualKeyVibePattern); |
Michael Wright | 57d94d9 | 2017-05-31 14:44:45 +0100 | [diff] [blame] | 240 | VibrationEffect clickEffect = createEffect(clickEffectTimings); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 241 | VibrationEffect doubleClickEffect = VibrationEffect.createWaveform( |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 242 | DOUBLE_CLICK_EFFECT_FALLBACK_TIMINGS, -1 /*repeatIndex*/); |
Michael Wright | 57d94d9 | 2017-05-31 14:44:45 +0100 | [diff] [blame] | 243 | long[] tickEffectTimings = getLongIntArray(context.getResources(), |
| 244 | com.android.internal.R.array.config_clockTickVibePattern); |
| 245 | VibrationEffect tickEffect = createEffect(tickEffectTimings); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 246 | |
Michael Wright | 57d94d9 | 2017-05-31 14:44:45 +0100 | [diff] [blame] | 247 | mFallbackEffects = new VibrationEffect[] { clickEffect, doubleClickEffect, tickEffect }; |
| 248 | } |
| 249 | |
| 250 | private static VibrationEffect createEffect(long[] timings) { |
| 251 | if (timings == null || timings.length == 0) { |
| 252 | return null; |
| 253 | } else if (timings.length == 1) { |
| 254 | return VibrationEffect.createOneShot(timings[0], VibrationEffect.DEFAULT_AMPLITUDE); |
| 255 | } else { |
| 256 | return VibrationEffect.createWaveform(timings, -1); |
| 257 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 260 | public void systemReady() { |
Yohei Yukawa | 8ce2a53 | 2015-11-25 20:35:04 -0800 | [diff] [blame] | 261 | mIm = mContext.getSystemService(InputManager.class); |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 262 | mSettingObserver = new SettingsObserver(mH); |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 263 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 264 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 265 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 266 | new PowerManagerInternal.LowPowerModeListener() { |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 267 | @Override |
| 268 | public int getServiceType() { |
| 269 | return ServiceType.VIBRATION; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public void onLowPowerModeChanged(PowerSaveState result) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 274 | updateVibrators(); |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 275 | } |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 276 | }); |
| 277 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 278 | mContext.getContentResolver().registerContentObserver( |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 279 | Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES), |
| 280 | true, mSettingObserver, UserHandle.USER_ALL); |
| 281 | |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 282 | mContext.registerReceiver(new BroadcastReceiver() { |
| 283 | @Override |
| 284 | public void onReceive(Context context, Intent intent) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 285 | updateVibrators(); |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 286 | } |
| 287 | }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH); |
| 288 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 289 | updateVibrators(); |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 290 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 291 | |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 292 | private final class SettingsObserver extends ContentObserver { |
| 293 | public SettingsObserver(Handler handler) { |
| 294 | super(handler); |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public void onChange(boolean SelfChange) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 299 | updateVibrators(); |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 303 | @Override // Binder call |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 304 | public boolean hasVibrator() { |
| 305 | return doVibratorExists(); |
| 306 | } |
| 307 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 308 | @Override // Binder call |
| 309 | public boolean hasAmplitudeControl() { |
| 310 | synchronized (mInputDeviceVibrators) { |
| 311 | // Input device vibrators don't support amplitude controls yet, but are still used over |
| 312 | // the system vibrator when connected. |
| 313 | return mSupportsAmplitudeControl && mInputDeviceVibrators.isEmpty(); |
| 314 | } |
| 315 | } |
| 316 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 317 | private void verifyIncomingUid(int uid) { |
| 318 | if (uid == Binder.getCallingUid()) { |
| 319 | return; |
| 320 | } |
| 321 | if (Binder.getCallingPid() == Process.myPid()) { |
| 322 | return; |
| 323 | } |
| 324 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 325 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 326 | } |
| 327 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 328 | /** |
| 329 | * Validate the incoming VibrationEffect. |
| 330 | * |
| 331 | * We can't throw exceptions here since we might be called from some system_server component, |
| 332 | * which would bring the whole system down. |
| 333 | * |
| 334 | * @return whether the VibrationEffect is valid |
| 335 | */ |
| 336 | private static boolean verifyVibrationEffect(VibrationEffect effect) { |
| 337 | if (effect == null) { |
| 338 | // Effect must not be null. |
| 339 | Slog.wtf(TAG, "effect must not be null"); |
| 340 | return false; |
| 341 | } |
| 342 | try { |
| 343 | effect.validate(); |
| 344 | } catch (Exception e) { |
| 345 | Slog.wtf(TAG, "Encountered issue when verifying VibrationEffect.", e); |
| 346 | return false; |
| 347 | } |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | private static long[] getLongIntArray(Resources r, int resid) { |
| 352 | int[] ar = r.getIntArray(resid); |
| 353 | if (ar == null) { |
| 354 | return null; |
| 355 | } |
| 356 | long[] out = new long[ar.length]; |
| 357 | for (int i = 0; i < ar.length; i++) { |
| 358 | out[i] = ar[i]; |
| 359 | } |
| 360 | return out; |
| 361 | } |
| 362 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 363 | @Override // Binder call |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 364 | public void vibrate(int uid, String opPkg, VibrationEffect effect, int usageHint, |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 365 | IBinder token) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 366 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE) |
| 367 | != PackageManager.PERMISSION_GRANTED) { |
| 368 | throw new SecurityException("Requires VIBRATE permission"); |
| 369 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 370 | if (token == null) { |
| 371 | Slog.e(TAG, "token must not be null"); |
| 372 | return; |
| 373 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 374 | verifyIncomingUid(uid); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 375 | if (!verifyVibrationEffect(effect)) { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 376 | return; |
| 377 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 378 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 379 | // If our current vibration is longer than the new vibration and is the same amplitude, |
| 380 | // then just let the current one finish. |
| 381 | if (effect instanceof VibrationEffect.OneShot |
| 382 | && mCurrentVibration != null |
| 383 | && mCurrentVibration.mEffect instanceof VibrationEffect.OneShot) { |
| 384 | VibrationEffect.OneShot newOneShot = (VibrationEffect.OneShot) effect; |
| 385 | VibrationEffect.OneShot currentOneShot = |
| 386 | (VibrationEffect.OneShot) mCurrentVibration.mEffect; |
| 387 | if (mCurrentVibration.hasLongerTimeout(newOneShot.getTiming()) |
| 388 | && newOneShot.getAmplitude() == currentOneShot.getAmplitude()) { |
| 389 | if (DEBUG) { |
Michael Wright | 58c4631 | 2017-10-05 14:04:14 -0400 | [diff] [blame] | 390 | Slog.d(TAG, "Ignoring incoming vibration in favor of current vibration"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | return; |
| 393 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 394 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | |
Michael Wright | 58c4631 | 2017-10-05 14:04:14 -0400 | [diff] [blame] | 396 | // If the current vibration is repeating and the incoming one is non-repeating, then ignore |
| 397 | // the non-repeating vibration. This is so that we don't cancel vibrations that are meant |
| 398 | // to grab the attention of the user, like ringtones and alarms, in favor of one-shot |
| 399 | // vibrations that are likely quite short. |
| 400 | if (!isRepeatingVibration(effect) |
| 401 | && mCurrentVibration != null && isRepeatingVibration(mCurrentVibration.mEffect)) { |
| 402 | if (DEBUG) { |
| 403 | Slog.d(TAG, "Ignoring incoming vibration in favor of alarm vibration"); |
| 404 | } |
| 405 | return; |
| 406 | } |
| 407 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 408 | Vibration vib = new Vibration(token, effect, usageHint, uid, opPkg); |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 409 | linkVibration(vib); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 410 | |
| 411 | long ident = Binder.clearCallingIdentity(); |
| 412 | try { |
| 413 | synchronized (mLock) { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 414 | doCancelVibrateLocked(); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 415 | startVibrationLocked(vib); |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 416 | addToPreviousVibrationsLocked(vib); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 418 | } finally { |
| 419 | Binder.restoreCallingIdentity(ident); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Michael Wright | 58c4631 | 2017-10-05 14:04:14 -0400 | [diff] [blame] | 423 | private static boolean isRepeatingVibration(VibrationEffect effect) { |
| 424 | if (effect instanceof VibrationEffect.Waveform) { |
| 425 | final VibrationEffect.Waveform waveform = (VibrationEffect.Waveform) effect; |
| 426 | if (waveform.getRepeatIndex() >= 0) { |
| 427 | return true; |
| 428 | } |
| 429 | } |
| 430 | return false; |
| 431 | } |
| 432 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 433 | private void addToPreviousVibrationsLocked(Vibration vib) { |
| 434 | if (mPreviousVibrations.size() > mPreviousVibrationsLimit) { |
| 435 | mPreviousVibrations.removeFirst(); |
| 436 | } |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 437 | mPreviousVibrations.addLast(vib.toInfo()); |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 440 | @Override // Binder call |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 441 | public void cancelVibrate(IBinder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | mContext.enforceCallingOrSelfPermission( |
| 443 | android.Manifest.permission.VIBRATE, |
| 444 | "cancelVibrate"); |
| 445 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 446 | synchronized (mLock) { |
| 447 | if (mCurrentVibration != null && mCurrentVibration.mToken == token) { |
| 448 | if (DEBUG) { |
| 449 | Slog.d(TAG, "Canceling vibration."); |
| 450 | } |
| 451 | long ident = Binder.clearCallingIdentity(); |
| 452 | try { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 453 | doCancelVibrateLocked(); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 454 | } finally { |
| 455 | Binder.restoreCallingIdentity(ident); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 456 | } |
| 457 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | } |
Eric Olsen | f42f15c | 2009-10-29 16:42:03 -0700 | [diff] [blame] | 460 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 461 | private final Runnable mVibrationEndRunnable = new Runnable() { |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 462 | @Override |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 463 | public void run() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 464 | onVibrationFinished(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 465 | } |
| 466 | }; |
| 467 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 468 | private void doCancelVibrateLocked() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 469 | mH.removeCallbacks(mVibrationEndRunnable); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 470 | if (mThread != null) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 471 | mThread.cancel(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 472 | mThread = null; |
| 473 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 474 | doVibratorOff(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 475 | reportFinishVibrationLocked(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 476 | } |
| 477 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 478 | // Callback for whenever the current vibration has finished played out |
| 479 | public void onVibrationFinished() { |
| 480 | if (DEBUG) { |
| 481 | Slog.e(TAG, "Vibration finished, cleaning up"); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 482 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 483 | synchronized (mLock) { |
| 484 | // Make sure the vibration is really done. This also reports that the vibration is |
| 485 | // finished. |
| 486 | doCancelVibrateLocked(); |
| 487 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 488 | } |
| 489 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 490 | private void startVibrationLocked(final Vibration vib) { |
Tyler Freeman | 319a34a | 2017-05-04 17:23:35 -0700 | [diff] [blame] | 491 | if (!isAllowedToVibrate(vib)) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 492 | if (DEBUG) { |
| 493 | Slog.e(TAG, "Vibrate ignored, low power mode"); |
Ruchi Kandoi | 664703d | 2014-05-09 16:01:31 -0700 | [diff] [blame] | 494 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 495 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 496 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 497 | |
| 498 | if (vib.mUsageHint == AudioAttributes.USAGE_NOTIFICATION_RINGTONE && |
| 499 | !shouldVibrateForRingtone()) { |
| 500 | if (DEBUG) { |
| 501 | Slog.e(TAG, "Vibrate ignored, not vibrating for ringtones"); |
| 502 | } |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | final int mode = getAppOpMode(vib); |
| 507 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 508 | if (mode == AppOpsManager.MODE_ERRORED) { |
| 509 | // We might be getting calls from within system_server, so we don't actually want |
| 510 | // to throw a SecurityException here. |
| 511 | Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid); |
| 512 | } |
| 513 | return; |
| 514 | } |
| 515 | startVibrationInnerLocked(vib); |
| 516 | } |
| 517 | |
| 518 | private void startVibrationInnerLocked(Vibration vib) { |
| 519 | mCurrentVibration = vib; |
| 520 | if (vib.mEffect instanceof VibrationEffect.OneShot) { |
| 521 | VibrationEffect.OneShot oneShot = (VibrationEffect.OneShot) vib.mEffect; |
| 522 | doVibratorOn(oneShot.getTiming(), oneShot.getAmplitude(), vib.mUid, vib.mUsageHint); |
| 523 | mH.postDelayed(mVibrationEndRunnable, oneShot.getTiming()); |
| 524 | } else if (vib.mEffect instanceof VibrationEffect.Waveform) { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 525 | // mThread better be null here. doCancelVibrate should always be |
| 526 | // called before startNextVibrationLocked or startVibrationLocked. |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 527 | VibrationEffect.Waveform waveform = (VibrationEffect.Waveform) vib.mEffect; |
| 528 | mThread = new VibrateThread(waveform, vib.mUid, vib.mUsageHint); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 529 | mThread.start(); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 530 | } else if (vib.mEffect instanceof VibrationEffect.Prebaked) { |
| 531 | long timeout = doVibratorPrebakedEffectLocked(vib); |
| 532 | if (timeout > 0) { |
| 533 | mH.postDelayed(mVibrationEndRunnable, timeout); |
| 534 | } |
| 535 | } else { |
| 536 | Slog.e(TAG, "Unknown vibration type, ignoring"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Tyler Freeman | 319a34a | 2017-05-04 17:23:35 -0700 | [diff] [blame] | 540 | private boolean isAllowedToVibrate(Vibration vib) { |
| 541 | if (!mLowPowerMode) { |
| 542 | return true; |
| 543 | } |
| 544 | if (vib.mUsageHint == AudioAttributes.USAGE_NOTIFICATION_RINGTONE) { |
| 545 | return true; |
| 546 | } |
| 547 | if (!mAllowPriorityVibrationsInLowPowerMode) { |
| 548 | return false; |
| 549 | } |
| 550 | if (vib.mUsageHint == AudioAttributes.USAGE_ALARM || |
| 551 | vib.mUsageHint == AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY || |
| 552 | vib.mUsageHint == AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST) { |
| 553 | |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | return false; |
| 558 | } |
| 559 | |
Brad Ebinger | 2d1c3b3 | 2016-05-12 18:05:17 -0700 | [diff] [blame] | 560 | private boolean shouldVibrateForRingtone() { |
| 561 | AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
Brad Ebinger | dcbdc0d | 2016-06-23 17:42:30 -0700 | [diff] [blame] | 562 | int ringerMode = audioManager.getRingerModeInternal(); |
Brad Ebinger | 2d1c3b3 | 2016-05-12 18:05:17 -0700 | [diff] [blame] | 563 | // "Also vibrate for calls" Setting in Sound |
| 564 | if (Settings.System.getInt( |
| 565 | mContext.getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 0) != 0) { |
| 566 | return ringerMode != AudioManager.RINGER_MODE_SILENT; |
| 567 | } else { |
| 568 | return ringerMode == AudioManager.RINGER_MODE_VIBRATE; |
| 569 | } |
| 570 | } |
| 571 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 572 | private int getAppOpMode(Vibration vib) { |
| 573 | int mode; |
| 574 | try { |
| 575 | mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE, |
| 576 | vib.mUsageHint, vib.mUid, vib.mOpPkg); |
| 577 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 578 | mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService), |
| 579 | AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg); |
| 580 | } |
| 581 | } catch (RemoteException e) { |
| 582 | Slog.e(TAG, "Failed to get appop mode for vibration!", e); |
| 583 | mode = AppOpsManager.MODE_IGNORED; |
| 584 | } |
| 585 | return mode; |
| 586 | } |
| 587 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 588 | private void reportFinishVibrationLocked() { |
| 589 | if (mCurrentVibration != null) { |
| 590 | try { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 591 | mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService), |
| 592 | AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid, |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 593 | mCurrentVibration.mOpPkg); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 594 | } catch (RemoteException e) { } |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 595 | unlinkVibration(mCurrentVibration); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 596 | mCurrentVibration = null; |
| 597 | } |
| 598 | } |
| 599 | |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 600 | private void linkVibration(Vibration vib) { |
| 601 | // Only link against waveforms since they potentially don't have a finish if |
| 602 | // they're repeating. Let other effects just play out until they're done. |
| 603 | if (vib.mEffect instanceof VibrationEffect.Waveform) { |
| 604 | try { |
| 605 | vib.mToken.linkToDeath(vib, 0); |
| 606 | } catch (RemoteException e) { |
| 607 | return; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
Mathias Jeppsson | b23949b | 2010-09-28 14:45:23 +0200 | [diff] [blame] | 612 | private void unlinkVibration(Vibration vib) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 613 | if (vib.mEffect instanceof VibrationEffect.Waveform) { |
Mathias Jeppsson | b23949b | 2010-09-28 14:45:23 +0200 | [diff] [blame] | 614 | vib.mToken.unlinkToDeath(vib, 0); |
| 615 | } |
| 616 | } |
| 617 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 618 | private void updateVibrators() { |
| 619 | synchronized (mLock) { |
| 620 | boolean devicesUpdated = updateInputDeviceVibratorsLocked(); |
| 621 | boolean lowPowerModeUpdated = updateLowPowerModeLocked(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 622 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 623 | if (devicesUpdated || lowPowerModeUpdated) { |
| 624 | // If the state changes out from under us then just reset. |
| 625 | doCancelVibrateLocked(); |
| 626 | } |
| 627 | } |
| 628 | } |
Jeff Brown | 8206525 | 2012-04-16 13:19:05 -0700 | [diff] [blame] | 629 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 630 | private boolean updateInputDeviceVibratorsLocked() { |
| 631 | boolean changed = false; |
| 632 | boolean vibrateInputDevices = false; |
| 633 | try { |
| 634 | vibrateInputDevices = Settings.System.getIntForUser( |
| 635 | mContext.getContentResolver(), |
| 636 | Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0; |
| 637 | } catch (SettingNotFoundException snfe) { |
| 638 | } |
| 639 | if (vibrateInputDevices != mVibrateInputDevicesSetting) { |
| 640 | changed = true; |
| 641 | mVibrateInputDevicesSetting = vibrateInputDevices; |
| 642 | } |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 643 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 644 | if (mVibrateInputDevicesSetting) { |
| 645 | if (!mInputDeviceListenerRegistered) { |
| 646 | mInputDeviceListenerRegistered = true; |
| 647 | mIm.registerInputDeviceListener(this, mH); |
| 648 | } |
| 649 | } else { |
| 650 | if (mInputDeviceListenerRegistered) { |
| 651 | mInputDeviceListenerRegistered = false; |
| 652 | mIm.unregisterInputDeviceListener(this); |
| 653 | } |
| 654 | } |
Jeff Brown | 8206525 | 2012-04-16 13:19:05 -0700 | [diff] [blame] | 655 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 656 | mInputDeviceVibrators.clear(); |
| 657 | if (mVibrateInputDevicesSetting) { |
| 658 | int[] ids = mIm.getInputDeviceIds(); |
| 659 | for (int i = 0; i < ids.length; i++) { |
| 660 | InputDevice device = mIm.getInputDevice(ids[i]); |
| 661 | Vibrator vibrator = device.getVibrator(); |
| 662 | if (vibrator.hasVibrator()) { |
| 663 | mInputDeviceVibrators.add(vibrator); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 666 | return true; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 667 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 668 | return changed; |
| 669 | } |
| 670 | |
| 671 | private boolean updateLowPowerModeLocked() { |
| 672 | boolean lowPowerMode = mPowerManagerInternal |
| 673 | .getLowPowerState(ServiceType.VIBRATION).batterySaverEnabled; |
| 674 | if (lowPowerMode != mLowPowerMode) { |
| 675 | mLowPowerMode = lowPowerMode; |
| 676 | return true; |
| 677 | } |
| 678 | return false; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | @Override |
| 682 | public void onInputDeviceAdded(int deviceId) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 683 | updateVibrators(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | @Override |
| 687 | public void onInputDeviceChanged(int deviceId) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 688 | updateVibrators(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | @Override |
| 692 | public void onInputDeviceRemoved(int deviceId) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 693 | updateVibrators(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | private boolean doVibratorExists() { |
Jeff Brown | 1064a50 | 2012-05-02 16:51:37 -0700 | [diff] [blame] | 697 | // For now, we choose to ignore the presence of input devices that have vibrators |
| 698 | // when reporting whether the device has a vibrator. Applications often use this |
| 699 | // information to decide whether to enable certain features so they expect the |
| 700 | // result of hasVibrator() to be constant. For now, just report whether |
| 701 | // the device has a built-in vibrator. |
| 702 | //synchronized (mInputDeviceVibrators) { |
| 703 | // return !mInputDeviceVibrators.isEmpty() || vibratorExists(); |
| 704 | //} |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 705 | return vibratorExists(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 708 | private void doVibratorOn(long millis, int amplitude, int uid, int usageHint) { |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 709 | synchronized (mInputDeviceVibrators) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 710 | if (amplitude == VibrationEffect.DEFAULT_AMPLITUDE) { |
| 711 | amplitude = mDefaultVibrationAmplitude; |
| 712 | } |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 713 | if (DEBUG) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 714 | Slog.d(TAG, "Turning vibrator on for " + millis + " ms" + |
| 715 | " with amplitude " + amplitude + "."); |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 716 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 717 | noteVibratorOnLocked(uid, millis); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 718 | final int vibratorCount = mInputDeviceVibrators.size(); |
| 719 | if (vibratorCount != 0) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 720 | final AudioAttributes attributes = |
| 721 | new AudioAttributes.Builder().setUsage(usageHint).build(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 722 | for (int i = 0; i < vibratorCount; i++) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 723 | mInputDeviceVibrators.get(i).vibrate(millis, attributes); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 724 | } |
| 725 | } else { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 726 | // Note: ordering is important here! Many haptic drivers will reset their amplitude |
| 727 | // when enabled, so we always have to enable frst, then set the amplitude. |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 728 | vibratorOn(millis); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 729 | doVibratorSetAmplitude(amplitude); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 734 | private void doVibratorSetAmplitude(int amplitude) { |
| 735 | if (mSupportsAmplitudeControl) { |
| 736 | vibratorSetAmplitude(amplitude); |
| 737 | } |
| 738 | } |
| 739 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 740 | private void doVibratorOff() { |
| 741 | synchronized (mInputDeviceVibrators) { |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame] | 742 | if (DEBUG) { |
| 743 | Slog.d(TAG, "Turning vibrator off."); |
| 744 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 745 | noteVibratorOffLocked(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 746 | final int vibratorCount = mInputDeviceVibrators.size(); |
| 747 | if (vibratorCount != 0) { |
| 748 | for (int i = 0; i < vibratorCount; i++) { |
| 749 | mInputDeviceVibrators.get(i).cancel(); |
| 750 | } |
| 751 | } else { |
| 752 | vibratorOff(); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 757 | private long doVibratorPrebakedEffectLocked(Vibration vib) { |
| 758 | synchronized (mInputDeviceVibrators) { |
| 759 | VibrationEffect.Prebaked prebaked = (VibrationEffect.Prebaked) vib.mEffect; |
| 760 | // Input devices don't support prebaked effect, so skip trying it with them. |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 761 | if (mInputDeviceVibrators.isEmpty()) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 762 | long timeout = vibratorPerformEffect(prebaked.getId(), EffectStrength.MEDIUM); |
| 763 | if (timeout > 0) { |
| 764 | noteVibratorOnLocked(vib.mUid, timeout); |
| 765 | return timeout; |
| 766 | } |
| 767 | } |
Michael Wright | dc2b3be | 2017-08-02 20:44:45 +0100 | [diff] [blame] | 768 | if (!prebaked.shouldFallback()) { |
| 769 | return 0; |
| 770 | } |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 771 | VibrationEffect effect = getFallbackEffect(prebaked.getId()); |
| 772 | if (effect == null) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 773 | Slog.w(TAG, "Failed to play prebaked effect, no fallback"); |
| 774 | return 0; |
| 775 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 776 | Vibration fallbackVib = |
| 777 | new Vibration(vib.mToken, effect, vib.mUsageHint, vib.mUid, vib.mOpPkg); |
| 778 | startVibrationInnerLocked(fallbackVib); |
| 779 | } |
| 780 | return 0; |
| 781 | } |
Eric Olsen | f42f15c | 2009-10-29 16:42:03 -0700 | [diff] [blame] | 782 | |
Michael Wright | 36d873f | 2018-01-08 15:54:05 +0000 | [diff] [blame] | 783 | private VibrationEffect getFallbackEffect(int effectId) { |
| 784 | if (effectId < 0 || effectId >= mFallbackEffects.length) { |
| 785 | return null; |
| 786 | } |
| 787 | return mFallbackEffects[effectId]; |
| 788 | } |
| 789 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 790 | private void noteVibratorOnLocked(int uid, long millis) { |
| 791 | try { |
| 792 | mBatteryStatsService.noteVibratorOn(uid, millis); |
| 793 | mCurVibUid = uid; |
| 794 | } catch (RemoteException e) { |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | private void noteVibratorOffLocked() { |
| 799 | if (mCurVibUid >= 0) { |
| 800 | try { |
| 801 | mBatteryStatsService.noteVibratorOff(mCurVibUid); |
| 802 | } catch (RemoteException e) { } |
| 803 | mCurVibUid = -1; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | private class VibrateThread extends Thread { |
| 808 | private final VibrationEffect.Waveform mWaveform; |
| 809 | private final int mUid; |
| 810 | private final int mUsageHint; |
| 811 | |
| 812 | private boolean mForceStop; |
| 813 | |
| 814 | VibrateThread(VibrationEffect.Waveform waveform, int uid, int usageHint) { |
| 815 | mWaveform = waveform; |
| 816 | mUid = uid; |
| 817 | mUsageHint = usageHint; |
| 818 | mTmpWorkSource.set(uid); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 819 | mWakeLock.setWorkSource(mTmpWorkSource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 822 | private long delayLocked(long duration) { |
| 823 | long durationRemaining = duration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | if (duration > 0) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 825 | final long bedtime = duration + SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | do { |
| 827 | try { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 828 | this.wait(durationRemaining); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 830 | catch (InterruptedException e) { } |
| 831 | if (mForceStop) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | break; |
| 833 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 834 | durationRemaining = bedtime - SystemClock.uptimeMillis(); |
| 835 | } while (durationRemaining > 0); |
| 836 | return duration - durationRemaining; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 838 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | public void run() { |
| 842 | Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 843 | mWakeLock.acquire(); |
| 844 | try { |
| 845 | boolean finished = playWaveform(); |
| 846 | if (finished) { |
| 847 | onVibrationFinished(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 849 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | mWakeLock.release(); |
| 851 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /** |
| 855 | * Play the waveform. |
| 856 | * |
| 857 | * @return true if it finished naturally, false otherwise (e.g. it was canceled). |
| 858 | */ |
| 859 | public boolean playWaveform() { |
| 860 | synchronized (this) { |
| 861 | final long[] timings = mWaveform.getTimings(); |
| 862 | final int[] amplitudes = mWaveform.getAmplitudes(); |
| 863 | final int len = timings.length; |
| 864 | final int repeat = mWaveform.getRepeatIndex(); |
| 865 | |
| 866 | int index = 0; |
| 867 | long onDuration = 0; |
| 868 | while (!mForceStop) { |
| 869 | if (index < len) { |
| 870 | final int amplitude = amplitudes[index]; |
| 871 | final long duration = timings[index++]; |
| 872 | if (duration <= 0) { |
| 873 | continue; |
| 874 | } |
| 875 | if (amplitude != 0) { |
| 876 | if (onDuration <= 0) { |
| 877 | // Telling the vibrator to start multiple times usually causes |
| 878 | // effects to feel "choppy" because the motor resets at every on |
| 879 | // command. Instead we figure out how long our next "on" period is |
| 880 | // going to be, tell the motor to stay on for the full duration, |
| 881 | // and then wake up to change the amplitude at the appropriate |
| 882 | // intervals. |
| 883 | onDuration = |
| 884 | getTotalOnDuration(timings, amplitudes, index - 1, repeat); |
| 885 | doVibratorOn(onDuration, amplitude, mUid, mUsageHint); |
| 886 | } else { |
| 887 | doVibratorSetAmplitude(amplitude); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | long waitTime = delayLocked(duration); |
| 892 | if (amplitude != 0) { |
| 893 | onDuration -= waitTime; |
| 894 | } |
| 895 | } else if (repeat < 0) { |
| 896 | break; |
| 897 | } else { |
| 898 | index = repeat; |
| 899 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 901 | return !mForceStop; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | public void cancel() { |
| 906 | synchronized (this) { |
| 907 | mThread.mForceStop = true; |
| 908 | mThread.notify(); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * Get the duration the vibrator will be on starting at startIndex until the next time it's |
| 914 | * off. |
| 915 | */ |
| 916 | private long getTotalOnDuration( |
| 917 | long[] timings, int[] amplitudes, int startIndex, int repeatIndex) { |
| 918 | int i = startIndex; |
| 919 | long timing = 0; |
| 920 | while(amplitudes[i] != 0) { |
| 921 | timing += timings[i++]; |
| 922 | if (i >= timings.length) { |
| 923 | if (repeatIndex >= 0) { |
| 924 | i = repeatIndex; |
| 925 | } else { |
| 926 | break; |
| 927 | } |
| 928 | } |
| 929 | if (i == startIndex) { |
| 930 | return 1000; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 931 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 933 | return timing; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | } |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 935 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 938 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | public void onReceive(Context context, Intent intent) { |
| 940 | if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 941 | synchronized (mLock) { |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 942 | // When the system is entering a non-interactive state, we want |
| 943 | // to cancel vibrations in case a misbehaving app has abandoned |
| 944 | // them. However it may happen that the system is currently playing |
| 945 | // haptic feedback as part of the transition. So we don't cancel |
| 946 | // system vibrations. |
| 947 | if (mCurrentVibration != null |
| 948 | && !mCurrentVibration.isSystemHapticFeedback()) { |
| 949 | doCancelVibrateLocked(); |
Vairavan Srinivasan | 8a61f49 | 2011-05-13 10:47:20 -0700 | [diff] [blame] | 950 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 951 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | }; |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 955 | |
| 956 | @Override |
| 957 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 958 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 959 | |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 960 | pw.println("Previous vibrations:"); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 961 | synchronized (mLock) { |
Filip Gruszczynski | 3a8eb0f | 2015-06-25 18:35:00 -0700 | [diff] [blame] | 962 | for (VibrationInfo info : mPreviousVibrations) { |
| 963 | pw.print(" "); |
| 964 | pw.println(info.toString()); |
| 965 | } |
| 966 | } |
| 967 | } |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 968 | |
| 969 | @Override |
| 970 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 971 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) |
| 972 | throws RemoteException { |
| 973 | new VibratorShellCommand(this).exec(this, in, out, err, args, callback, resultReceiver); |
| 974 | } |
| 975 | |
| 976 | private final class VibratorShellCommand extends ShellCommand { |
| 977 | |
| 978 | private static final long MAX_VIBRATION_MS = 200; |
| 979 | |
| 980 | private final IBinder mToken; |
| 981 | |
| 982 | private VibratorShellCommand(IBinder token) { |
| 983 | mToken = token; |
| 984 | } |
| 985 | |
| 986 | @Override |
| 987 | public int onCommand(String cmd) { |
| 988 | if ("vibrate".equals(cmd)) { |
| 989 | return runVibrate(); |
| 990 | } |
| 991 | return handleDefaultCommands(cmd); |
| 992 | } |
| 993 | |
| 994 | private int runVibrate() { |
Felipe Leme | 5e2e632 | 2017-07-14 17:25:59 -0700 | [diff] [blame] | 995 | try { |
| 996 | final int zenMode = Settings.Global.getInt(mContext.getContentResolver(), |
| 997 | Settings.Global.ZEN_MODE); |
| 998 | if (zenMode != Settings.Global.ZEN_MODE_OFF) { |
| 999 | try (PrintWriter pw = getOutPrintWriter();) { |
| 1000 | pw.print("Ignoring because device is on DND mode "); |
| 1001 | pw.println(DebugUtils.flagsToString(Settings.Global.class, "ZEN_MODE_", |
| 1002 | zenMode)); |
| 1003 | return 0; |
| 1004 | } |
| 1005 | } |
| 1006 | } catch (SettingNotFoundException e) { |
| 1007 | // ignore |
| 1008 | } |
| 1009 | |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 1010 | final long duration = Long.parseLong(getNextArgRequired()); |
| 1011 | if (duration > MAX_VIBRATION_MS) { |
| 1012 | throw new IllegalArgumentException("maximum duration is " + MAX_VIBRATION_MS); |
| 1013 | } |
| 1014 | String description = getNextArg(); |
| 1015 | if (description == null) { |
| 1016 | description = "Shell command"; |
| 1017 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1018 | |
| 1019 | VibrationEffect effect = |
| 1020 | VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE); |
| 1021 | vibrate(Binder.getCallingUid(), description, effect, AudioAttributes.USAGE_UNKNOWN, |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 1022 | mToken); |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | @Override |
| 1027 | public void onHelp() { |
| 1028 | try (PrintWriter pw = getOutPrintWriter();) { |
| 1029 | pw.println("Vibrator commands:"); |
| 1030 | pw.println(" help"); |
| 1031 | pw.println(" Prints this help text."); |
| 1032 | pw.println(""); |
| 1033 | pw.println(" vibrate duration [description]"); |
Felipe Leme | 5e2e632 | 2017-07-14 17:25:59 -0700 | [diff] [blame] | 1034 | pw.println(" Vibrates for duration milliseconds; ignored when device is on DND "); |
| 1035 | pw.println(" (Do Not Disturb) mode."); |
Felipe Leme | a528100 | 2017-02-10 15:13:48 -0800 | [diff] [blame] | 1036 | pw.println(""); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | } |