blob: c2284224502d027e516cbdab85c45281b6f307c6 [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;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070027import android.os.BatteryStats;
Joe Onorato95e4f702009-03-24 19:29:09 -070028import android.os.Handler;
Mike Lockwood3a322132009-11-24 00:30:52 -050029import android.os.IVibratorService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.PowerManager;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070031import android.os.PowerManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.Process;
33import android.os.RemoteException;
34import android.os.IBinder;
35import android.os.Binder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080036import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.SystemClock;
Jeff Brownd4935962012-09-25 13:27:20 -070038import android.os.UserHandle;
Jeff Brown7f6c2312012-04-13 20:38:38 -070039import android.os.Vibrator;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070040import android.os.WorkSource;
Jeff Brown7f6c2312012-04-13 20:38:38 -070041import android.provider.Settings;
42import android.provider.Settings.SettingNotFoundException;
Joe Onorato8a9b2202010-02-26 18:56:32 -080043import android.util.Slog;
Jeff Brown7f6c2312012-04-13 20:38:38 -070044import android.view.InputDevice;
John Spurlock7b414672014-07-18 13:02:39 -040045import android.media.AudioAttributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Dianne Hackborna06de0f2012-12-11 16:34:47 -080047import com.android.internal.app.IAppOpsService;
48import com.android.internal.app.IBatteryStats;
49
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -070050import java.io.FileDescriptor;
51import java.io.PrintWriter;
Jeff Brown7f6c2312012-04-13 20:38:38 -070052import java.util.ArrayList;
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -070053import java.util.Arrays;
Jeff Brown969579b2014-05-20 19:29:29 -070054import java.util.Iterator;
Patrick Scott18dd5f02009-07-02 11:31:12 -040055import java.util.LinkedList;
56import java.util.ListIterator;
57
Jeff Brown7f6c2312012-04-13 20:38:38 -070058public class VibratorService extends IVibratorService.Stub
59 implements InputManager.InputDeviceListener {
Mike Lockwood3a322132009-11-24 00:30:52 -050060 private static final String TAG = "VibratorService";
Jeff Brown82379ba2014-07-25 19:03:28 -070061 private static final boolean DEBUG = false;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -070062 private static final String SYSTEM_UI_PACKAGE = "com.android.systemui";
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050063
Patrick Scott18dd5f02009-07-02 11:31:12 -040064 private final LinkedList<Vibration> mVibrations;
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -070065 private final LinkedList<VibrationInfo> mPreviousVibrations;
66 private final int mPreviousVibrationsLimit;
Patrick Scott18dd5f02009-07-02 11:31:12 -040067 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070068 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070069 private final Handler mH = new Handler();
70
71 private final Context mContext;
72 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080073 private final IAppOpsService mAppOpsService;
74 private final IBatteryStats mBatteryStatsService;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070075 private PowerManagerInternal mPowerManagerInternal;
Jeff Brown7f6c2312012-04-13 20:38:38 -070076 private InputManager mIm;
77
78 volatile VibrateThread mThread;
79
80 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
81 // to be acquired
82 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
83 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
84 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
85
Dianne Hackborna06de0f2012-12-11 16:34:47 -080086 private int mCurVibUid = -1;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070087 private boolean mLowPowerMode;
88 private SettingsObserver mSettingObserver;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080089
Jeff Brown7f6c2312012-04-13 20:38:38 -070090 native static boolean vibratorExists();
91 native static void vibratorOn(long milliseconds);
92 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040093
Patrick Scott18dd5f02009-07-02 11:31:12 -040094 private class Vibration implements IBinder.DeathRecipient {
95 private final IBinder mToken;
96 private final long mTimeout;
97 private final long mStartTime;
98 private final long[] mPattern;
99 private final int mRepeat;
John Spurlock7b414672014-07-18 13:02:39 -0400100 private final int mUsageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700101 private final int mUid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400102 private final String mOpPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400103
John Spurlock7b414672014-07-18 13:02:39 -0400104 Vibration(IBinder token, long millis, int usageHint, int uid, String opPkg) {
105 this(token, millis, null, 0, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400106 }
107
John Spurlock7b414672014-07-18 13:02:39 -0400108 Vibration(IBinder token, long[] pattern, int repeat, int usageHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400109 String opPkg) {
John Spurlock7b414672014-07-18 13:02:39 -0400110 this(token, 0, pattern, repeat, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400111 }
112
113 private Vibration(IBinder token, long millis, long[] pattern,
John Spurlock7b414672014-07-18 13:02:39 -0400114 int repeat, int usageHint, int uid, String opPkg) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400115 mToken = token;
116 mTimeout = millis;
117 mStartTime = SystemClock.uptimeMillis();
118 mPattern = pattern;
119 mRepeat = repeat;
John Spurlock7b414672014-07-18 13:02:39 -0400120 mUsageHint = usageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700121 mUid = uid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400122 mOpPkg = opPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400123 }
124
125 public void binderDied() {
126 synchronized (mVibrations) {
127 mVibrations.remove(this);
128 if (this == mCurrentVibration) {
129 doCancelVibrateLocked();
130 startNextVibrationLocked();
131 }
132 }
133 }
134
135 public boolean hasLongerTimeout(long millis) {
136 if (mTimeout == 0) {
137 // This is a pattern, return false to play the simple
138 // vibration.
139 return false;
140 }
141 if ((mStartTime + mTimeout)
142 < (SystemClock.uptimeMillis() + millis)) {
143 // If this vibration will end before the time passed in, let
144 // the new vibration play.
145 return false;
146 }
147 return true;
148 }
Jeff Brown969579b2014-05-20 19:29:29 -0700149
150 public boolean isSystemHapticFeedback() {
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700151 return (mUid == Process.SYSTEM_UID || mUid == 0 || SYSTEM_UI_PACKAGE.equals(mOpPkg))
152 && mRepeat < 0;
Jeff Brown969579b2014-05-20 19:29:29 -0700153 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400154 }
155
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700156 private static class VibrationInfo {
157 long timeout;
158 long startTime;
159 long[] pattern;
160 int repeat;
161 int usageHint;
162 int uid;
163 String opPkg;
164
165 public VibrationInfo(long timeout, long startTime, long[] pattern, int repeat,
166 int usageHint, int uid, String opPkg) {
167 this.timeout = timeout;
168 this.startTime = startTime;
169 this.pattern = pattern;
170 this.repeat = repeat;
171 this.usageHint = usageHint;
172 this.uid = uid;
173 this.opPkg = opPkg;
174 }
175
176 @Override
177 public String toString() {
178 return new StringBuilder()
179 .append("timeout: ")
180 .append(timeout)
181 .append(", startTime: ")
182 .append(startTime)
183 .append(", pattern: ")
184 .append(Arrays.toString(pattern))
185 .append(", repeat: ")
186 .append(repeat)
187 .append(", usageHint: ")
188 .append(usageHint)
189 .append(", uid: ")
190 .append(uid)
191 .append(", opPkg: ")
192 .append(opPkg)
193 .toString();
194 }
195 }
196
Mike Lockwood3a322132009-11-24 00:30:52 -0500197 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 // Reset the hardware to a default state, in case this is a runtime
199 // restart instead of a fresh boot.
200 vibratorOff();
201
202 mContext = context;
203 PowerManager pm = (PowerManager)context.getSystemService(
204 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700205 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 mWakeLock.setReferenceCounted(true);
207
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800208 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700209 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
210 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800211
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700212 mPreviousVibrationsLimit = mContext.getResources().getInteger(
213 com.android.internal.R.integer.config_previousVibrationsDumpLimit);
214
215 mVibrations = new LinkedList<>();
216 mPreviousVibrations = new LinkedList<>();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 IntentFilter filter = new IntentFilter();
219 filter.addAction(Intent.ACTION_SCREEN_OFF);
220 context.registerReceiver(mIntentReceiver, filter);
221 }
222
Jeff Brown7f6c2312012-04-13 20:38:38 -0700223 public void systemReady() {
224 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700225 mSettingObserver = new SettingsObserver(mH);
Jeff Brownd4935962012-09-25 13:27:20 -0700226
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700227 mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
228 mPowerManagerInternal.registerLowPowerModeObserver(
229 new PowerManagerInternal.LowPowerModeListener() {
230 @Override
231 public void onLowPowerModeChanged(boolean enabled) {
232 updateInputDeviceVibrators();
233 }
234 });
235
Jeff Brown7f6c2312012-04-13 20:38:38 -0700236 mContext.getContentResolver().registerContentObserver(
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700237 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES),
238 true, mSettingObserver, UserHandle.USER_ALL);
239
Jeff Brownd4935962012-09-25 13:27:20 -0700240 mContext.registerReceiver(new BroadcastReceiver() {
241 @Override
242 public void onReceive(Context context, Intent intent) {
243 updateInputDeviceVibrators();
244 }
245 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
246
Jeff Brown82065252012-04-16 13:19:05 -0700247 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700248 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700249
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700250 private final class SettingsObserver extends ContentObserver {
251 public SettingsObserver(Handler handler) {
252 super(handler);
253 }
254
255 @Override
256 public void onChange(boolean SelfChange) {
257 updateInputDeviceVibrators();
258 }
259 }
260
Jeff Brown82379ba2014-07-25 19:03:28 -0700261 @Override // Binder call
Jeff Brown7f6c2312012-04-13 20:38:38 -0700262 public boolean hasVibrator() {
263 return doVibratorExists();
264 }
265
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800266 private void verifyIncomingUid(int uid) {
267 if (uid == Binder.getCallingUid()) {
268 return;
269 }
270 if (Binder.getCallingPid() == Process.myPid()) {
271 return;
272 }
273 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
274 Binder.getCallingPid(), Binder.getCallingUid(), null);
275 }
276
Jeff Brown82379ba2014-07-25 19:03:28 -0700277 @Override // Binder call
John Spurlock7b414672014-07-18 13:02:39 -0400278 public void vibrate(int uid, String opPkg, long milliseconds, int usageHint,
John Spurlock1af30c72014-03-10 08:33:35 -0400279 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700280 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
281 != PackageManager.PERMISSION_GRANTED) {
282 throw new SecurityException("Requires VIBRATE permission");
283 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800284 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400285 // We're running in the system server so we cannot crash. Check for a
286 // timeout of 0 or negative. This will ensure that a vibration has
287 // either a timeout of > 0 or a non-null pattern.
288 if (milliseconds <= 0 || (mCurrentVibration != null
289 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400290 // Ignore this vibration since the current vibration will play for
291 // longer than milliseconds.
292 return;
293 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700294
Jeff Brown82379ba2014-07-25 19:03:28 -0700295 if (DEBUG) {
296 Slog.d(TAG, "Vibrating for " + milliseconds + " ms.");
297 }
298
John Spurlock7b414672014-07-18 13:02:39 -0400299 Vibration vib = new Vibration(token, milliseconds, usageHint, uid, opPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800300
301 final long ident = Binder.clearCallingIdentity();
302 try {
303 synchronized (mVibrations) {
304 removeVibrationLocked(token);
305 doCancelVibrateLocked();
306 mCurrentVibration = vib;
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700307 addToPreviousVibrationsLocked(vib);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800308 startVibrationLocked(vib);
309 }
310 } finally {
311 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 }
314
315 private boolean isAll0(long[] pattern) {
316 int N = pattern.length;
317 for (int i = 0; i < N; i++) {
318 if (pattern[i] != 0) {
319 return false;
320 }
321 }
322 return true;
323 }
324
Jeff Brown82379ba2014-07-25 19:03:28 -0700325 @Override // Binder call
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800326 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock7b414672014-07-18 13:02:39 -0400327 int usageHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
329 != PackageManager.PERMISSION_GRANTED) {
330 throw new SecurityException("Requires VIBRATE permission");
331 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800332 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 // so wakelock calls will succeed
334 long identity = Binder.clearCallingIdentity();
335 try {
Jeff Brown82379ba2014-07-25 19:03:28 -0700336 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 String s = "";
338 int N = pattern.length;
339 for (int i=0; i<N; i++) {
340 s += " " + pattern[i];
341 }
Jeff Brown82379ba2014-07-25 19:03:28 -0700342 Slog.d(TAG, "Vibrating with pattern:" + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
344
345 // we're running in the server so we can't fail
346 if (pattern == null || pattern.length == 0
347 || isAll0(pattern)
348 || repeat >= pattern.length || token == null) {
349 return;
350 }
351
John Spurlock7b414672014-07-18 13:02:39 -0400352 Vibration vib = new Vibration(token, pattern, repeat, usageHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400353 try {
354 token.linkToDeath(vib, 0);
355 } catch (RemoteException e) {
356 return;
357 }
358
359 synchronized (mVibrations) {
360 removeVibrationLocked(token);
361 doCancelVibrateLocked();
362 if (repeat >= 0) {
363 mVibrations.addFirst(vib);
364 startNextVibrationLocked();
365 } else {
366 // A negative repeat means that this pattern is not meant
367 // to repeat. Treat it like a simple vibration.
368 mCurrentVibration = vib;
369 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700371 addToPreviousVibrationsLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
373 }
374 finally {
375 Binder.restoreCallingIdentity(identity);
376 }
377 }
378
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700379 private void addToPreviousVibrationsLocked(Vibration vib) {
380 if (mPreviousVibrations.size() > mPreviousVibrationsLimit) {
381 mPreviousVibrations.removeFirst();
382 }
383 mPreviousVibrations.addLast(new VibratorService.VibrationInfo(vib.mTimeout, vib.mStartTime,
384 vib.mPattern, vib.mRepeat, vib.mUsageHint, vib.mUid, vib.mOpPkg));
385 }
386
Jeff Brown82379ba2014-07-25 19:03:28 -0700387 @Override // Binder call
Patrick Scott18dd5f02009-07-02 11:31:12 -0400388 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 mContext.enforceCallingOrSelfPermission(
390 android.Manifest.permission.VIBRATE,
391 "cancelVibrate");
392
393 // so wakelock calls will succeed
394 long identity = Binder.clearCallingIdentity();
395 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400396 synchronized (mVibrations) {
397 final Vibration vib = removeVibrationLocked(token);
398 if (vib == mCurrentVibration) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700399 if (DEBUG) {
400 Slog.d(TAG, "Canceling vibration.");
401 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400402 doCancelVibrateLocked();
403 startNextVibrationLocked();
404 }
405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407 finally {
408 Binder.restoreCallingIdentity(identity);
409 }
410 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700411
Patrick Scott18dd5f02009-07-02 11:31:12 -0400412 private final Runnable mVibrationRunnable = new Runnable() {
Jeff Brown82379ba2014-07-25 19:03:28 -0700413 @Override
Patrick Scott18dd5f02009-07-02 11:31:12 -0400414 public void run() {
415 synchronized (mVibrations) {
416 doCancelVibrateLocked();
417 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400419 }
420 };
421
422 // Lock held on mVibrations
423 private void doCancelVibrateLocked() {
424 if (mThread != null) {
425 synchronized (mThread) {
426 mThread.mDone = true;
427 mThread.notify();
428 }
429 mThread = null;
430 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700431 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400432 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800433 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400434 }
435
436 // Lock held on mVibrations
437 private void startNextVibrationLocked() {
438 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800439 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200440 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400441 return;
442 }
443 mCurrentVibration = mVibrations.getFirst();
444 startVibrationLocked(mCurrentVibration);
445 }
446
447 // Lock held on mVibrations
448 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800449 try {
John Spurlock7b414672014-07-18 13:02:39 -0400450 if (mLowPowerMode
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -0700451 && vib.mUsageHint != AudioAttributes.USAGE_NOTIFICATION_RINGTONE) {
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700452 return;
453 }
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700454
John Spurlock1af30c72014-03-10 08:33:35 -0400455 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
John Spurlock7b414672014-07-18 13:02:39 -0400456 vib.mUsageHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400457 if (mode == AppOpsManager.MODE_ALLOWED) {
458 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400459 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400460 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800461 if (mode != AppOpsManager.MODE_ALLOWED) {
462 if (mode == AppOpsManager.MODE_ERRORED) {
463 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
464 }
465 mH.post(mVibrationRunnable);
466 return;
467 }
468 } catch (RemoteException e) {
469 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400470 if (vib.mTimeout != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400471 doVibratorOn(vib.mTimeout, vib.mUid, vib.mUsageHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400472 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
473 } else {
474 // mThread better be null here. doCancelVibrate should always be
475 // called before startNextVibrationLocked or startVibrationLocked.
476 mThread = new VibrateThread(vib);
477 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
479 }
480
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800481 private void reportFinishVibrationLocked() {
482 if (mCurrentVibration != null) {
483 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700484 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
485 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400486 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800487 } catch (RemoteException e) {
488 }
489 mCurrentVibration = null;
490 }
491 }
492
Patrick Scott18dd5f02009-07-02 11:31:12 -0400493 // Lock held on mVibrations
494 private Vibration removeVibrationLocked(IBinder token) {
495 ListIterator<Vibration> iter = mVibrations.listIterator(0);
496 while (iter.hasNext()) {
497 Vibration vib = iter.next();
498 if (vib.mToken == token) {
499 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200500 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400501 return vib;
502 }
503 }
504 // We might be looking for a simple vibration which is only stored in
505 // mCurrentVibration.
506 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200507 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400508 return mCurrentVibration;
509 }
510 return null;
511 }
512
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200513 private void unlinkVibration(Vibration vib) {
514 if (vib.mPattern != null) {
515 // If Vibration object has a pattern,
516 // the Vibration object has also been linkedToDeath.
517 vib.mToken.unlinkToDeath(vib, 0);
518 }
519 }
520
Jeff Brown7f6c2312012-04-13 20:38:38 -0700521 private void updateInputDeviceVibrators() {
522 synchronized (mVibrations) {
523 doCancelVibrateLocked();
524
525 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700526 mVibrateInputDevicesSetting = false;
527 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700528 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
529 mContext.getContentResolver(),
530 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700531 } catch (SettingNotFoundException snfe) {
532 }
533
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700534 mLowPowerMode = mPowerManagerInternal.getLowPowerModeEnabled();
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700535
Jeff Brown82065252012-04-16 13:19:05 -0700536 if (mVibrateInputDevicesSetting) {
537 if (!mInputDeviceListenerRegistered) {
538 mInputDeviceListenerRegistered = true;
539 mIm.registerInputDeviceListener(this, mH);
540 }
541 } else {
542 if (mInputDeviceListenerRegistered) {
543 mInputDeviceListenerRegistered = false;
544 mIm.unregisterInputDeviceListener(this);
545 }
546 }
547
Jeff Brown7f6c2312012-04-13 20:38:38 -0700548 mInputDeviceVibrators.clear();
549 if (mVibrateInputDevicesSetting) {
550 int[] ids = mIm.getInputDeviceIds();
551 for (int i = 0; i < ids.length; i++) {
552 InputDevice device = mIm.getInputDevice(ids[i]);
553 Vibrator vibrator = device.getVibrator();
554 if (vibrator.hasVibrator()) {
555 mInputDeviceVibrators.add(vibrator);
556 }
557 }
558 }
559 }
560
561 startNextVibrationLocked();
562 }
563 }
564
565 @Override
566 public void onInputDeviceAdded(int deviceId) {
567 updateInputDeviceVibrators();
568 }
569
570 @Override
571 public void onInputDeviceChanged(int deviceId) {
572 updateInputDeviceVibrators();
573 }
574
575 @Override
576 public void onInputDeviceRemoved(int deviceId) {
577 updateInputDeviceVibrators();
578 }
579
580 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700581 // For now, we choose to ignore the presence of input devices that have vibrators
582 // when reporting whether the device has a vibrator. Applications often use this
583 // information to decide whether to enable certain features so they expect the
584 // result of hasVibrator() to be constant. For now, just report whether
585 // the device has a built-in vibrator.
586 //synchronized (mInputDeviceVibrators) {
587 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
588 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800589 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700590 }
591
John Spurlock7b414672014-07-18 13:02:39 -0400592 private void doVibratorOn(long millis, int uid, int usageHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700593 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700594 if (DEBUG) {
595 Slog.d(TAG, "Turning vibrator on for " + millis + " ms.");
596 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800597 try {
598 mBatteryStatsService.noteVibratorOn(uid, millis);
599 mCurVibUid = uid;
600 } catch (RemoteException e) {
601 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700602 final int vibratorCount = mInputDeviceVibrators.size();
603 if (vibratorCount != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400604 final AudioAttributes attributes = new AudioAttributes.Builder().setUsage(usageHint)
605 .build();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700606 for (int i = 0; i < vibratorCount; i++) {
John Spurlock7b414672014-07-18 13:02:39 -0400607 mInputDeviceVibrators.get(i).vibrate(millis, attributes);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700608 }
609 } else {
610 vibratorOn(millis);
611 }
612 }
613 }
614
615 private void doVibratorOff() {
616 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700617 if (DEBUG) {
618 Slog.d(TAG, "Turning vibrator off.");
619 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800620 if (mCurVibUid >= 0) {
621 try {
622 mBatteryStatsService.noteVibratorOff(mCurVibUid);
623 } catch (RemoteException e) {
624 }
625 mCurVibUid = -1;
626 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700627 final int vibratorCount = mInputDeviceVibrators.size();
628 if (vibratorCount != 0) {
629 for (int i = 0; i < vibratorCount; i++) {
630 mInputDeviceVibrators.get(i).cancel();
631 }
632 } else {
633 vibratorOff();
634 }
635 }
636 }
637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400639 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700641
Patrick Scott18dd5f02009-07-02 11:31:12 -0400642 VibrateThread(Vibration vib) {
643 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700644 mTmpWorkSource.set(vib.mUid);
645 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 mWakeLock.acquire();
647 }
648
649 private void delay(long duration) {
650 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700651 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 do {
653 try {
654 this.wait(duration);
655 }
656 catch (InterruptedException e) {
657 }
658 if (mDone) {
659 break;
660 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700661 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 } while (duration > 0);
663 }
664 }
665
666 public void run() {
667 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
668 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800669 final long[] pattern = mVibration.mPattern;
670 final int len = pattern.length;
671 final int repeat = mVibration.mRepeat;
672 final int uid = mVibration.mUid;
John Spurlock7b414672014-07-18 13:02:39 -0400673 final int usageHint = mVibration.mUsageHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 long duration = 0;
676
677 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700678 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 if (index < len) {
680 duration += pattern[index++];
681 }
682
683 // sleep until it is time to start the vibrator
684 delay(duration);
685 if (mDone) {
686 break;
687 }
688
689 if (index < len) {
690 // read on-time duration and start the vibrator
691 // duration is saved for delay() at top of loop
692 duration = pattern[index++];
693 if (duration > 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400694 VibratorService.this.doVibratorOn(duration, uid, usageHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 }
696 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400697 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 break;
699 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400700 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 duration = 0;
702 }
703 }
704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 mWakeLock.release();
706 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400707 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 if (mThread == this) {
709 mThread = null;
710 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400711 if (!mDone) {
712 // If this vibration finished naturally, start the next
713 // vibration.
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200714 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400715 startNextVibrationLocked();
716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 }
718 }
Jeff Brown969579b2014-05-20 19:29:29 -0700719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
Jeff Brown969579b2014-05-20 19:29:29 -0700722 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 public void onReceive(Context context, Intent intent) {
724 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400725 synchronized (mVibrations) {
Jeff Brown969579b2014-05-20 19:29:29 -0700726 // When the system is entering a non-interactive state, we want
727 // to cancel vibrations in case a misbehaving app has abandoned
728 // them. However it may happen that the system is currently playing
729 // haptic feedback as part of the transition. So we don't cancel
730 // system vibrations.
731 if (mCurrentVibration != null
732 && !mCurrentVibration.isSystemHapticFeedback()) {
733 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700734 }
735
Jeff Brown969579b2014-05-20 19:29:29 -0700736 // Clear all remaining vibrations.
737 Iterator<Vibration> it = mVibrations.iterator();
738 while (it.hasNext()) {
739 Vibration vibration = it.next();
740 if (vibration != mCurrentVibration) {
741 unlinkVibration(vibration);
742 it.remove();
743 }
744 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747 }
748 };
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700749
750 @Override
751 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
752 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
753 != PackageManager.PERMISSION_GRANTED) {
754
755 pw.println("Permission Denial: can't dump vibrator service from from pid="
756 + Binder.getCallingPid()
757 + ", uid=" + Binder.getCallingUid());
758 return;
759 }
760 pw.println("Previous vibrations:");
761 synchronized (mVibrations) {
762 for (VibrationInfo info : mPreviousVibrations) {
763 pw.print(" ");
764 pw.println(info.toString());
765 }
766 }
767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768}