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