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