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