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