blob: 8888325152779ba82cb5c869b7a1dfc94a0f9d44 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
Dianne Hackborna06de0f2012-12-11 16:34:47 -080019import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.content.pm.PackageManager;
Jeff Brown7f6c2312012-04-13 20:38:38 -070025import android.database.ContentObserver;
26import android.hardware.input.InputManager;
Brad Ebinger2d1c3b32016-05-12 18:05:17 -070027import android.media.AudioManager;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070028import android.os.BatteryStats;
Joe Onorato95e4f702009-03-24 19:29:09 -070029import android.os.Handler;
Mike Lockwood3a322132009-11-24 00:30:52 -050030import android.os.IVibratorService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.PowerManager;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070032import android.os.PowerManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Process;
34import android.os.RemoteException;
Felipe Lemea5281002017-02-10 15:13:48 -080035import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.IBinder;
37import android.os.Binder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080038import android.os.ServiceManager;
Felipe Lemea5281002017-02-10 15:13:48 -080039import android.os.ShellCallback;
40import android.os.ShellCommand;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.SystemClock;
Jeff Brownd4935962012-09-25 13:27:20 -070042import android.os.UserHandle;
Jeff Brown7f6c2312012-04-13 20:38:38 -070043import android.os.Vibrator;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070044import android.os.WorkSource;
Jeff Brown7f6c2312012-04-13 20:38:38 -070045import android.provider.Settings;
46import android.provider.Settings.SettingNotFoundException;
Joe Onorato8a9b2202010-02-26 18:56:32 -080047import android.util.Slog;
Jeff Brown7f6c2312012-04-13 20:38:38 -070048import android.view.InputDevice;
John Spurlock7b414672014-07-18 13:02:39 -040049import android.media.AudioAttributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Dianne Hackborna06de0f2012-12-11 16:34:47 -080051import com.android.internal.app.IAppOpsService;
52import com.android.internal.app.IBatteryStats;
53
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -070054import java.io.FileDescriptor;
55import java.io.PrintWriter;
Jeff Brown7f6c2312012-04-13 20:38:38 -070056import java.util.ArrayList;
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -070057import java.util.Arrays;
Jeff Brown969579b2014-05-20 19:29:29 -070058import java.util.Iterator;
Patrick Scott18dd5f02009-07-02 11:31:12 -040059import java.util.LinkedList;
60import java.util.ListIterator;
61
Jeff Brown7f6c2312012-04-13 20:38:38 -070062public class VibratorService extends IVibratorService.Stub
63 implements InputManager.InputDeviceListener {
Mike Lockwood3a322132009-11-24 00:30:52 -050064 private static final String TAG = "VibratorService";
Jeff Brown82379ba2014-07-25 19:03:28 -070065 private static final boolean DEBUG = false;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -070066 private static final String SYSTEM_UI_PACKAGE = "com.android.systemui";
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050067
Patrick Scott18dd5f02009-07-02 11:31:12 -040068 private final LinkedList<Vibration> mVibrations;
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -070069 private final LinkedList<VibrationInfo> mPreviousVibrations;
70 private final int mPreviousVibrationsLimit;
Patrick Scott18dd5f02009-07-02 11:31:12 -040071 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070072 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070073 private final Handler mH = new Handler();
74
75 private final Context mContext;
76 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080077 private final IAppOpsService mAppOpsService;
78 private final IBatteryStats mBatteryStatsService;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070079 private PowerManagerInternal mPowerManagerInternal;
Jeff Brown7f6c2312012-04-13 20:38:38 -070080 private InputManager mIm;
81
82 volatile VibrateThread mThread;
83
84 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
85 // to be acquired
86 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
87 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
88 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
89
Dianne Hackborna06de0f2012-12-11 16:34:47 -080090 private int mCurVibUid = -1;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070091 private boolean mLowPowerMode;
92 private SettingsObserver mSettingObserver;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080093
Jeff Brown7f6c2312012-04-13 20:38:38 -070094 native static boolean vibratorExists();
Vincent Beckere6904fb2012-08-10 14:17:33 +020095 native static void vibratorInit();
Jeff Brown7f6c2312012-04-13 20:38:38 -070096 native static void vibratorOn(long milliseconds);
97 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040098
Patrick Scott18dd5f02009-07-02 11:31:12 -040099 private class Vibration implements IBinder.DeathRecipient {
100 private final IBinder mToken;
101 private final long mTimeout;
102 private final long mStartTime;
103 private final long[] mPattern;
104 private final int mRepeat;
John Spurlock7b414672014-07-18 13:02:39 -0400105 private final int mUsageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700106 private final int mUid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400107 private final String mOpPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400108
John Spurlock7b414672014-07-18 13:02:39 -0400109 Vibration(IBinder token, long millis, int usageHint, int uid, String opPkg) {
110 this(token, millis, null, 0, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400111 }
112
John Spurlock7b414672014-07-18 13:02:39 -0400113 Vibration(IBinder token, long[] pattern, int repeat, int usageHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400114 String opPkg) {
John Spurlock7b414672014-07-18 13:02:39 -0400115 this(token, 0, pattern, repeat, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400116 }
117
118 private Vibration(IBinder token, long millis, long[] pattern,
John Spurlock7b414672014-07-18 13:02:39 -0400119 int repeat, int usageHint, int uid, String opPkg) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400120 mToken = token;
121 mTimeout = millis;
122 mStartTime = SystemClock.uptimeMillis();
123 mPattern = pattern;
124 mRepeat = repeat;
John Spurlock7b414672014-07-18 13:02:39 -0400125 mUsageHint = usageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700126 mUid = uid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400127 mOpPkg = opPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400128 }
129
130 public void binderDied() {
131 synchronized (mVibrations) {
132 mVibrations.remove(this);
133 if (this == mCurrentVibration) {
134 doCancelVibrateLocked();
135 startNextVibrationLocked();
136 }
137 }
138 }
139
140 public boolean hasLongerTimeout(long millis) {
141 if (mTimeout == 0) {
142 // This is a pattern, return false to play the simple
143 // vibration.
144 return false;
145 }
146 if ((mStartTime + mTimeout)
147 < (SystemClock.uptimeMillis() + millis)) {
148 // If this vibration will end before the time passed in, let
149 // the new vibration play.
150 return false;
151 }
152 return true;
153 }
Jeff Brown969579b2014-05-20 19:29:29 -0700154
155 public boolean isSystemHapticFeedback() {
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700156 return (mUid == Process.SYSTEM_UID || mUid == 0 || SYSTEM_UI_PACKAGE.equals(mOpPkg))
157 && mRepeat < 0;
Jeff Brown969579b2014-05-20 19:29:29 -0700158 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400159 }
160
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700161 private static class VibrationInfo {
162 long timeout;
163 long startTime;
164 long[] pattern;
165 int repeat;
166 int usageHint;
167 int uid;
168 String opPkg;
169
170 public VibrationInfo(long timeout, long startTime, long[] pattern, int repeat,
171 int usageHint, int uid, String opPkg) {
172 this.timeout = timeout;
173 this.startTime = startTime;
174 this.pattern = pattern;
175 this.repeat = repeat;
176 this.usageHint = usageHint;
177 this.uid = uid;
178 this.opPkg = opPkg;
179 }
180
181 @Override
182 public String toString() {
183 return new StringBuilder()
184 .append("timeout: ")
185 .append(timeout)
186 .append(", startTime: ")
187 .append(startTime)
188 .append(", pattern: ")
189 .append(Arrays.toString(pattern))
190 .append(", repeat: ")
191 .append(repeat)
192 .append(", usageHint: ")
193 .append(usageHint)
194 .append(", uid: ")
195 .append(uid)
196 .append(", opPkg: ")
197 .append(opPkg)
198 .toString();
199 }
200 }
201
Mike Lockwood3a322132009-11-24 00:30:52 -0500202 VibratorService(Context context) {
Vincent Beckere6904fb2012-08-10 14:17:33 +0200203 vibratorInit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 // Reset the hardware to a default state, in case this is a runtime
205 // restart instead of a fresh boot.
206 vibratorOff();
207
208 mContext = context;
209 PowerManager pm = (PowerManager)context.getSystemService(
210 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700211 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 mWakeLock.setReferenceCounted(true);
213
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800214 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700215 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
216 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800217
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700218 mPreviousVibrationsLimit = mContext.getResources().getInteger(
219 com.android.internal.R.integer.config_previousVibrationsDumpLimit);
220
221 mVibrations = new LinkedList<>();
222 mPreviousVibrations = new LinkedList<>();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 IntentFilter filter = new IntentFilter();
225 filter.addAction(Intent.ACTION_SCREEN_OFF);
226 context.registerReceiver(mIntentReceiver, filter);
227 }
228
Jeff Brown7f6c2312012-04-13 20:38:38 -0700229 public void systemReady() {
Yohei Yukawa8ce2a532015-11-25 20:35:04 -0800230 mIm = mContext.getSystemService(InputManager.class);
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700231 mSettingObserver = new SettingsObserver(mH);
Jeff Brownd4935962012-09-25 13:27:20 -0700232
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700233 mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
234 mPowerManagerInternal.registerLowPowerModeObserver(
235 new PowerManagerInternal.LowPowerModeListener() {
236 @Override
237 public void onLowPowerModeChanged(boolean enabled) {
238 updateInputDeviceVibrators();
239 }
240 });
241
Jeff Brown7f6c2312012-04-13 20:38:38 -0700242 mContext.getContentResolver().registerContentObserver(
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700243 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES),
244 true, mSettingObserver, UserHandle.USER_ALL);
245
Jeff Brownd4935962012-09-25 13:27:20 -0700246 mContext.registerReceiver(new BroadcastReceiver() {
247 @Override
248 public void onReceive(Context context, Intent intent) {
249 updateInputDeviceVibrators();
250 }
251 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
252
Jeff Brown82065252012-04-16 13:19:05 -0700253 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700254 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700255
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700256 private final class SettingsObserver extends ContentObserver {
257 public SettingsObserver(Handler handler) {
258 super(handler);
259 }
260
261 @Override
262 public void onChange(boolean SelfChange) {
263 updateInputDeviceVibrators();
264 }
265 }
266
Jeff Brown82379ba2014-07-25 19:03:28 -0700267 @Override // Binder call
Jeff Brown7f6c2312012-04-13 20:38:38 -0700268 public boolean hasVibrator() {
269 return doVibratorExists();
270 }
271
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800272 private void verifyIncomingUid(int uid) {
273 if (uid == Binder.getCallingUid()) {
274 return;
275 }
276 if (Binder.getCallingPid() == Process.myPid()) {
277 return;
278 }
279 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
280 Binder.getCallingPid(), Binder.getCallingUid(), null);
281 }
282
Jeff Brown82379ba2014-07-25 19:03:28 -0700283 @Override // Binder call
John Spurlock7b414672014-07-18 13:02:39 -0400284 public void vibrate(int uid, String opPkg, long milliseconds, int usageHint,
John Spurlock1af30c72014-03-10 08:33:35 -0400285 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700286 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
287 != PackageManager.PERMISSION_GRANTED) {
288 throw new SecurityException("Requires VIBRATE permission");
289 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800290 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400291 // We're running in the system server so we cannot crash. Check for a
292 // timeout of 0 or negative. This will ensure that a vibration has
293 // either a timeout of > 0 or a non-null pattern.
294 if (milliseconds <= 0 || (mCurrentVibration != null
295 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400296 // Ignore this vibration since the current vibration will play for
297 // longer than milliseconds.
298 return;
299 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700300
Jeff Brown82379ba2014-07-25 19:03:28 -0700301 if (DEBUG) {
302 Slog.d(TAG, "Vibrating for " + milliseconds + " ms.");
303 }
304
John Spurlock7b414672014-07-18 13:02:39 -0400305 Vibration vib = new Vibration(token, milliseconds, usageHint, uid, opPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800306
307 final long ident = Binder.clearCallingIdentity();
308 try {
309 synchronized (mVibrations) {
310 removeVibrationLocked(token);
311 doCancelVibrateLocked();
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700312 addToPreviousVibrationsLocked(vib);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800313 startVibrationLocked(vib);
314 }
315 } finally {
316 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
319
320 private boolean isAll0(long[] pattern) {
321 int N = pattern.length;
322 for (int i = 0; i < N; i++) {
323 if (pattern[i] != 0) {
324 return false;
325 }
326 }
327 return true;
328 }
329
Jeff Brown82379ba2014-07-25 19:03:28 -0700330 @Override // Binder call
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800331 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock7b414672014-07-18 13:02:39 -0400332 int usageHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
334 != PackageManager.PERMISSION_GRANTED) {
335 throw new SecurityException("Requires VIBRATE permission");
336 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800337 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 // so wakelock calls will succeed
339 long identity = Binder.clearCallingIdentity();
340 try {
Jeff Brown82379ba2014-07-25 19:03:28 -0700341 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 String s = "";
343 int N = pattern.length;
344 for (int i=0; i<N; i++) {
345 s += " " + pattern[i];
346 }
Jeff Brown82379ba2014-07-25 19:03:28 -0700347 Slog.d(TAG, "Vibrating with pattern:" + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
349
350 // we're running in the server so we can't fail
351 if (pattern == null || pattern.length == 0
352 || isAll0(pattern)
353 || repeat >= pattern.length || token == null) {
354 return;
355 }
356
John Spurlock7b414672014-07-18 13:02:39 -0400357 Vibration vib = new Vibration(token, pattern, repeat, usageHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400358 try {
359 token.linkToDeath(vib, 0);
360 } catch (RemoteException e) {
361 return;
362 }
363
364 synchronized (mVibrations) {
365 removeVibrationLocked(token);
366 doCancelVibrateLocked();
367 if (repeat >= 0) {
368 mVibrations.addFirst(vib);
369 startNextVibrationLocked();
370 } else {
371 // A negative repeat means that this pattern is not meant
372 // to repeat. Treat it like a simple vibration.
Patrick Scott18dd5f02009-07-02 11:31:12 -0400373 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700375 addToPreviousVibrationsLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377 }
378 finally {
379 Binder.restoreCallingIdentity(identity);
380 }
381 }
382
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700383 private void addToPreviousVibrationsLocked(Vibration vib) {
384 if (mPreviousVibrations.size() > mPreviousVibrationsLimit) {
385 mPreviousVibrations.removeFirst();
386 }
387 mPreviousVibrations.addLast(new VibratorService.VibrationInfo(vib.mTimeout, vib.mStartTime,
388 vib.mPattern, vib.mRepeat, vib.mUsageHint, vib.mUid, vib.mOpPkg));
389 }
390
Jeff Brown82379ba2014-07-25 19:03:28 -0700391 @Override // Binder call
Patrick Scott18dd5f02009-07-02 11:31:12 -0400392 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 mContext.enforceCallingOrSelfPermission(
394 android.Manifest.permission.VIBRATE,
395 "cancelVibrate");
396
397 // so wakelock calls will succeed
398 long identity = Binder.clearCallingIdentity();
399 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400400 synchronized (mVibrations) {
401 final Vibration vib = removeVibrationLocked(token);
402 if (vib == mCurrentVibration) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700403 if (DEBUG) {
404 Slog.d(TAG, "Canceling vibration.");
405 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400406 doCancelVibrateLocked();
407 startNextVibrationLocked();
408 }
409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411 finally {
412 Binder.restoreCallingIdentity(identity);
413 }
414 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700415
Patrick Scott18dd5f02009-07-02 11:31:12 -0400416 private final Runnable mVibrationRunnable = new Runnable() {
Jeff Brown82379ba2014-07-25 19:03:28 -0700417 @Override
Patrick Scott18dd5f02009-07-02 11:31:12 -0400418 public void run() {
419 synchronized (mVibrations) {
420 doCancelVibrateLocked();
421 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400423 }
424 };
425
426 // Lock held on mVibrations
427 private void doCancelVibrateLocked() {
428 if (mThread != null) {
429 synchronized (mThread) {
430 mThread.mDone = true;
431 mThread.notify();
432 }
433 mThread = null;
434 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700435 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400436 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800437 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400438 }
439
440 // Lock held on mVibrations
441 private void startNextVibrationLocked() {
442 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800443 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200444 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400445 return;
446 }
Julia Reynolds4357e522016-06-01 14:17:58 -0400447 startVibrationLocked(mVibrations.getFirst());
Patrick Scott18dd5f02009-07-02 11:31:12 -0400448 }
449
450 // Lock held on mVibrations
451 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800452 try {
John Spurlock7b414672014-07-18 13:02:39 -0400453 if (mLowPowerMode
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -0700454 && vib.mUsageHint != AudioAttributes.USAGE_NOTIFICATION_RINGTONE) {
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700455 return;
456 }
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700457
Brad Ebinger2d1c3b32016-05-12 18:05:17 -0700458 if (vib.mUsageHint == AudioAttributes.USAGE_NOTIFICATION_RINGTONE &&
459 !shouldVibrateForRingtone()) {
Julia Reynoldsd28967f2016-04-14 09:37:08 -0400460 return;
461 }
462
John Spurlock1af30c72014-03-10 08:33:35 -0400463 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
John Spurlock7b414672014-07-18 13:02:39 -0400464 vib.mUsageHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400465 if (mode == AppOpsManager.MODE_ALLOWED) {
466 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400467 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400468 }
Julia Reynolds4357e522016-06-01 14:17:58 -0400469 if (mode == AppOpsManager.MODE_ALLOWED) {
470 mCurrentVibration = vib;
471 } else {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800472 if (mode == AppOpsManager.MODE_ERRORED) {
473 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
474 }
475 mH.post(mVibrationRunnable);
476 return;
477 }
478 } catch (RemoteException e) {
479 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400480 if (vib.mTimeout != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400481 doVibratorOn(vib.mTimeout, vib.mUid, vib.mUsageHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400482 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
483 } else {
484 // mThread better be null here. doCancelVibrate should always be
485 // called before startNextVibrationLocked or startVibrationLocked.
486 mThread = new VibrateThread(vib);
487 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
489 }
490
Brad Ebinger2d1c3b32016-05-12 18:05:17 -0700491 private boolean shouldVibrateForRingtone() {
492 AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
Brad Ebingerdcbdc0d2016-06-23 17:42:30 -0700493 int ringerMode = audioManager.getRingerModeInternal();
Brad Ebinger2d1c3b32016-05-12 18:05:17 -0700494 // "Also vibrate for calls" Setting in Sound
495 if (Settings.System.getInt(
496 mContext.getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 0) != 0) {
497 return ringerMode != AudioManager.RINGER_MODE_SILENT;
498 } else {
499 return ringerMode == AudioManager.RINGER_MODE_VIBRATE;
500 }
501 }
502
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800503 private void reportFinishVibrationLocked() {
504 if (mCurrentVibration != null) {
505 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700506 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
507 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400508 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800509 } catch (RemoteException e) {
510 }
511 mCurrentVibration = null;
512 }
513 }
514
Patrick Scott18dd5f02009-07-02 11:31:12 -0400515 // Lock held on mVibrations
516 private Vibration removeVibrationLocked(IBinder token) {
517 ListIterator<Vibration> iter = mVibrations.listIterator(0);
518 while (iter.hasNext()) {
519 Vibration vib = iter.next();
520 if (vib.mToken == token) {
521 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200522 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400523 return vib;
524 }
525 }
526 // We might be looking for a simple vibration which is only stored in
527 // mCurrentVibration.
528 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200529 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400530 return mCurrentVibration;
531 }
532 return null;
533 }
534
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200535 private void unlinkVibration(Vibration vib) {
536 if (vib.mPattern != null) {
537 // If Vibration object has a pattern,
538 // the Vibration object has also been linkedToDeath.
539 vib.mToken.unlinkToDeath(vib, 0);
540 }
541 }
542
Jeff Brown7f6c2312012-04-13 20:38:38 -0700543 private void updateInputDeviceVibrators() {
544 synchronized (mVibrations) {
545 doCancelVibrateLocked();
546
547 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700548 mVibrateInputDevicesSetting = false;
549 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700550 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
551 mContext.getContentResolver(),
552 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700553 } catch (SettingNotFoundException snfe) {
554 }
555
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700556 mLowPowerMode = mPowerManagerInternal.getLowPowerModeEnabled();
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700557
Jeff Brown82065252012-04-16 13:19:05 -0700558 if (mVibrateInputDevicesSetting) {
559 if (!mInputDeviceListenerRegistered) {
560 mInputDeviceListenerRegistered = true;
561 mIm.registerInputDeviceListener(this, mH);
562 }
563 } else {
564 if (mInputDeviceListenerRegistered) {
565 mInputDeviceListenerRegistered = false;
566 mIm.unregisterInputDeviceListener(this);
567 }
568 }
569
Jeff Brown7f6c2312012-04-13 20:38:38 -0700570 mInputDeviceVibrators.clear();
571 if (mVibrateInputDevicesSetting) {
572 int[] ids = mIm.getInputDeviceIds();
573 for (int i = 0; i < ids.length; i++) {
574 InputDevice device = mIm.getInputDevice(ids[i]);
575 Vibrator vibrator = device.getVibrator();
576 if (vibrator.hasVibrator()) {
577 mInputDeviceVibrators.add(vibrator);
578 }
579 }
580 }
581 }
582
583 startNextVibrationLocked();
584 }
585 }
586
587 @Override
588 public void onInputDeviceAdded(int deviceId) {
589 updateInputDeviceVibrators();
590 }
591
592 @Override
593 public void onInputDeviceChanged(int deviceId) {
594 updateInputDeviceVibrators();
595 }
596
597 @Override
598 public void onInputDeviceRemoved(int deviceId) {
599 updateInputDeviceVibrators();
600 }
601
602 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700603 // For now, we choose to ignore the presence of input devices that have vibrators
604 // when reporting whether the device has a vibrator. Applications often use this
605 // information to decide whether to enable certain features so they expect the
606 // result of hasVibrator() to be constant. For now, just report whether
607 // the device has a built-in vibrator.
608 //synchronized (mInputDeviceVibrators) {
609 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
610 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800611 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700612 }
613
John Spurlock7b414672014-07-18 13:02:39 -0400614 private void doVibratorOn(long millis, int uid, int usageHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700615 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700616 if (DEBUG) {
617 Slog.d(TAG, "Turning vibrator on for " + millis + " ms.");
618 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800619 try {
620 mBatteryStatsService.noteVibratorOn(uid, millis);
621 mCurVibUid = uid;
622 } catch (RemoteException e) {
623 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700624 final int vibratorCount = mInputDeviceVibrators.size();
625 if (vibratorCount != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400626 final AudioAttributes attributes = new AudioAttributes.Builder().setUsage(usageHint)
627 .build();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700628 for (int i = 0; i < vibratorCount; i++) {
John Spurlock7b414672014-07-18 13:02:39 -0400629 mInputDeviceVibrators.get(i).vibrate(millis, attributes);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700630 }
631 } else {
632 vibratorOn(millis);
633 }
634 }
635 }
636
637 private void doVibratorOff() {
638 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700639 if (DEBUG) {
640 Slog.d(TAG, "Turning vibrator off.");
641 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800642 if (mCurVibUid >= 0) {
643 try {
644 mBatteryStatsService.noteVibratorOff(mCurVibUid);
645 } catch (RemoteException e) {
646 }
647 mCurVibUid = -1;
648 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700649 final int vibratorCount = mInputDeviceVibrators.size();
650 if (vibratorCount != 0) {
651 for (int i = 0; i < vibratorCount; i++) {
652 mInputDeviceVibrators.get(i).cancel();
653 }
654 } else {
655 vibratorOff();
656 }
657 }
658 }
659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400661 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700663
Patrick Scott18dd5f02009-07-02 11:31:12 -0400664 VibrateThread(Vibration vib) {
665 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700666 mTmpWorkSource.set(vib.mUid);
667 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 mWakeLock.acquire();
669 }
670
671 private void delay(long duration) {
672 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700673 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 do {
675 try {
676 this.wait(duration);
677 }
678 catch (InterruptedException e) {
679 }
680 if (mDone) {
681 break;
682 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700683 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 } while (duration > 0);
685 }
686 }
687
688 public void run() {
689 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
690 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800691 final long[] pattern = mVibration.mPattern;
692 final int len = pattern.length;
693 final int repeat = mVibration.mRepeat;
694 final int uid = mVibration.mUid;
John Spurlock7b414672014-07-18 13:02:39 -0400695 final int usageHint = mVibration.mUsageHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 long duration = 0;
698
699 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700700 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 if (index < len) {
702 duration += pattern[index++];
703 }
704
705 // sleep until it is time to start the vibrator
706 delay(duration);
707 if (mDone) {
708 break;
709 }
710
711 if (index < len) {
712 // read on-time duration and start the vibrator
713 // duration is saved for delay() at top of loop
714 duration = pattern[index++];
715 if (duration > 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400716 VibratorService.this.doVibratorOn(duration, uid, usageHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 }
718 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400719 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 break;
721 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400722 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 duration = 0;
724 }
725 }
726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 mWakeLock.release();
728 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400729 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 if (mThread == this) {
731 mThread = null;
732 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400733 if (!mDone) {
734 // If this vibration finished naturally, start the next
735 // vibration.
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200736 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400737 startNextVibrationLocked();
738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 }
740 }
Jeff Brown969579b2014-05-20 19:29:29 -0700741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
Jeff Brown969579b2014-05-20 19:29:29 -0700744 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 public void onReceive(Context context, Intent intent) {
746 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400747 synchronized (mVibrations) {
Jeff Brown969579b2014-05-20 19:29:29 -0700748 // When the system is entering a non-interactive state, we want
749 // to cancel vibrations in case a misbehaving app has abandoned
750 // them. However it may happen that the system is currently playing
751 // haptic feedback as part of the transition. So we don't cancel
752 // system vibrations.
753 if (mCurrentVibration != null
754 && !mCurrentVibration.isSystemHapticFeedback()) {
755 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700756 }
757
Jeff Brown969579b2014-05-20 19:29:29 -0700758 // Clear all remaining vibrations.
759 Iterator<Vibration> it = mVibrations.iterator();
760 while (it.hasNext()) {
761 Vibration vibration = it.next();
762 if (vibration != mCurrentVibration) {
763 unlinkVibration(vibration);
764 it.remove();
765 }
766 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769 }
770 };
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700771
772 @Override
773 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
774 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
775 != PackageManager.PERMISSION_GRANTED) {
776
777 pw.println("Permission Denial: can't dump vibrator service from from pid="
778 + Binder.getCallingPid()
779 + ", uid=" + Binder.getCallingUid());
780 return;
781 }
782 pw.println("Previous vibrations:");
783 synchronized (mVibrations) {
784 for (VibrationInfo info : mPreviousVibrations) {
785 pw.print(" ");
786 pw.println(info.toString());
787 }
788 }
789 }
Felipe Lemea5281002017-02-10 15:13:48 -0800790
791 @Override
792 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
793 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
794 throws RemoteException {
795 new VibratorShellCommand(this).exec(this, in, out, err, args, callback, resultReceiver);
796 }
797
798 private final class VibratorShellCommand extends ShellCommand {
799
800 private static final long MAX_VIBRATION_MS = 200;
801
802 private final IBinder mToken;
803
804 private VibratorShellCommand(IBinder token) {
805 mToken = token;
806 }
807
808 @Override
809 public int onCommand(String cmd) {
810 if ("vibrate".equals(cmd)) {
811 return runVibrate();
812 }
813 return handleDefaultCommands(cmd);
814 }
815
816 private int runVibrate() {
817 final long duration = Long.parseLong(getNextArgRequired());
818 if (duration > MAX_VIBRATION_MS) {
819 throw new IllegalArgumentException("maximum duration is " + MAX_VIBRATION_MS);
820 }
821 String description = getNextArg();
822 if (description == null) {
823 description = "Shell command";
824 }
825 vibrate(Binder.getCallingUid(), description, duration, AudioAttributes.USAGE_UNKNOWN,
826 mToken);
827 return 0;
828 }
829
830 @Override
831 public void onHelp() {
832 try (PrintWriter pw = getOutPrintWriter();) {
833 pw.println("Vibrator commands:");
834 pw.println(" help");
835 pw.println(" Prints this help text.");
836 pw.println("");
837 pw.println(" vibrate duration [description]");
838 pw.println(" Vibrates for duration milliseconds.");
839 pw.println("");
840 }
841 }
842 }
843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844}