blob: 20f6f1ccffecc6b14555a0617ed1ad1583d4d2e6 [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
Jeff Brown7f6c2312012-04-13 20:38:38 -070050import java.util.ArrayList;
Jeff Brown969579b2014-05-20 19:29:29 -070051import java.util.Iterator;
Patrick Scott18dd5f02009-07-02 11:31:12 -040052import java.util.LinkedList;
53import java.util.ListIterator;
54
Jeff Brown7f6c2312012-04-13 20:38:38 -070055public class VibratorService extends IVibratorService.Stub
56 implements InputManager.InputDeviceListener {
Mike Lockwood3a322132009-11-24 00:30:52 -050057 private static final String TAG = "VibratorService";
Jeff Brown82379ba2014-07-25 19:03:28 -070058 private static final boolean DEBUG = false;
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050059
Patrick Scott18dd5f02009-07-02 11:31:12 -040060 private final LinkedList<Vibration> mVibrations;
61 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070062 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070063 private final Handler mH = new Handler();
64
65 private final Context mContext;
66 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080067 private final IAppOpsService mAppOpsService;
68 private final IBatteryStats mBatteryStatsService;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070069 private PowerManagerInternal mPowerManagerInternal;
Jeff Brown7f6c2312012-04-13 20:38:38 -070070 private InputManager mIm;
71
72 volatile VibrateThread mThread;
73
74 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
75 // to be acquired
76 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
77 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
78 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
79
Dianne Hackborna06de0f2012-12-11 16:34:47 -080080 private int mCurVibUid = -1;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070081 private boolean mLowPowerMode;
82 private SettingsObserver mSettingObserver;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080083
Jeff Brown7f6c2312012-04-13 20:38:38 -070084 native static boolean vibratorExists();
85 native static void vibratorOn(long milliseconds);
86 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040087
Patrick Scott18dd5f02009-07-02 11:31:12 -040088 private class Vibration implements IBinder.DeathRecipient {
89 private final IBinder mToken;
90 private final long mTimeout;
91 private final long mStartTime;
92 private final long[] mPattern;
93 private final int mRepeat;
John Spurlock7b414672014-07-18 13:02:39 -040094 private final int mUsageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070095 private final int mUid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040096 private final String mOpPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -040097
John Spurlock7b414672014-07-18 13:02:39 -040098 Vibration(IBinder token, long millis, int usageHint, int uid, String opPkg) {
99 this(token, millis, null, 0, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400100 }
101
John Spurlock7b414672014-07-18 13:02:39 -0400102 Vibration(IBinder token, long[] pattern, int repeat, int usageHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400103 String opPkg) {
John Spurlock7b414672014-07-18 13:02:39 -0400104 this(token, 0, pattern, repeat, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400105 }
106
107 private Vibration(IBinder token, long millis, long[] pattern,
John Spurlock7b414672014-07-18 13:02:39 -0400108 int repeat, int usageHint, int uid, String opPkg) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400109 mToken = token;
110 mTimeout = millis;
111 mStartTime = SystemClock.uptimeMillis();
112 mPattern = pattern;
113 mRepeat = repeat;
John Spurlock7b414672014-07-18 13:02:39 -0400114 mUsageHint = usageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700115 mUid = uid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400116 mOpPkg = opPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400117 }
118
119 public void binderDied() {
120 synchronized (mVibrations) {
121 mVibrations.remove(this);
122 if (this == mCurrentVibration) {
123 doCancelVibrateLocked();
124 startNextVibrationLocked();
125 }
126 }
127 }
128
129 public boolean hasLongerTimeout(long millis) {
130 if (mTimeout == 0) {
131 // This is a pattern, return false to play the simple
132 // vibration.
133 return false;
134 }
135 if ((mStartTime + mTimeout)
136 < (SystemClock.uptimeMillis() + millis)) {
137 // If this vibration will end before the time passed in, let
138 // the new vibration play.
139 return false;
140 }
141 return true;
142 }
Jeff Brown969579b2014-05-20 19:29:29 -0700143
144 public boolean isSystemHapticFeedback() {
145 return (mUid == Process.SYSTEM_UID || mUid == 0) && mRepeat < 0;
146 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400147 }
148
Mike Lockwood3a322132009-11-24 00:30:52 -0500149 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 // Reset the hardware to a default state, in case this is a runtime
151 // restart instead of a fresh boot.
152 vibratorOff();
153
154 mContext = context;
155 PowerManager pm = (PowerManager)context.getSystemService(
156 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700157 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 mWakeLock.setReferenceCounted(true);
159
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800160 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700161 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
162 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800163
Patrick Scott18dd5f02009-07-02 11:31:12 -0400164 mVibrations = new LinkedList<Vibration>();
165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 IntentFilter filter = new IntentFilter();
167 filter.addAction(Intent.ACTION_SCREEN_OFF);
168 context.registerReceiver(mIntentReceiver, filter);
169 }
170
Jeff Brown7f6c2312012-04-13 20:38:38 -0700171 public void systemReady() {
172 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700173 mSettingObserver = new SettingsObserver(mH);
Jeff Brownd4935962012-09-25 13:27:20 -0700174
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700175 mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
176 mPowerManagerInternal.registerLowPowerModeObserver(
177 new PowerManagerInternal.LowPowerModeListener() {
178 @Override
179 public void onLowPowerModeChanged(boolean enabled) {
180 updateInputDeviceVibrators();
181 }
182 });
183
Jeff Brown7f6c2312012-04-13 20:38:38 -0700184 mContext.getContentResolver().registerContentObserver(
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700185 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES),
186 true, mSettingObserver, UserHandle.USER_ALL);
187
Jeff Brownd4935962012-09-25 13:27:20 -0700188 mContext.registerReceiver(new BroadcastReceiver() {
189 @Override
190 public void onReceive(Context context, Intent intent) {
191 updateInputDeviceVibrators();
192 }
193 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
194
Jeff Brown82065252012-04-16 13:19:05 -0700195 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700196 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700197
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700198 private final class SettingsObserver extends ContentObserver {
199 public SettingsObserver(Handler handler) {
200 super(handler);
201 }
202
203 @Override
204 public void onChange(boolean SelfChange) {
205 updateInputDeviceVibrators();
206 }
207 }
208
Jeff Brown82379ba2014-07-25 19:03:28 -0700209 @Override // Binder call
Jeff Brown7f6c2312012-04-13 20:38:38 -0700210 public boolean hasVibrator() {
211 return doVibratorExists();
212 }
213
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800214 private void verifyIncomingUid(int uid) {
215 if (uid == Binder.getCallingUid()) {
216 return;
217 }
218 if (Binder.getCallingPid() == Process.myPid()) {
219 return;
220 }
221 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
222 Binder.getCallingPid(), Binder.getCallingUid(), null);
223 }
224
Jeff Brown82379ba2014-07-25 19:03:28 -0700225 @Override // Binder call
John Spurlock7b414672014-07-18 13:02:39 -0400226 public void vibrate(int uid, String opPkg, long milliseconds, int usageHint,
John Spurlock1af30c72014-03-10 08:33:35 -0400227 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700228 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
229 != PackageManager.PERMISSION_GRANTED) {
230 throw new SecurityException("Requires VIBRATE permission");
231 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800232 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400233 // We're running in the system server so we cannot crash. Check for a
234 // timeout of 0 or negative. This will ensure that a vibration has
235 // either a timeout of > 0 or a non-null pattern.
236 if (milliseconds <= 0 || (mCurrentVibration != null
237 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400238 // Ignore this vibration since the current vibration will play for
239 // longer than milliseconds.
240 return;
241 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700242
Jeff Brown82379ba2014-07-25 19:03:28 -0700243 if (DEBUG) {
244 Slog.d(TAG, "Vibrating for " + milliseconds + " ms.");
245 }
246
John Spurlock7b414672014-07-18 13:02:39 -0400247 Vibration vib = new Vibration(token, milliseconds, usageHint, uid, opPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800248
249 final long ident = Binder.clearCallingIdentity();
250 try {
251 synchronized (mVibrations) {
252 removeVibrationLocked(token);
253 doCancelVibrateLocked();
254 mCurrentVibration = vib;
255 startVibrationLocked(vib);
256 }
257 } finally {
258 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
261
262 private boolean isAll0(long[] pattern) {
263 int N = pattern.length;
264 for (int i = 0; i < N; i++) {
265 if (pattern[i] != 0) {
266 return false;
267 }
268 }
269 return true;
270 }
271
Jeff Brown82379ba2014-07-25 19:03:28 -0700272 @Override // Binder call
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800273 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock7b414672014-07-18 13:02:39 -0400274 int usageHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
276 != PackageManager.PERMISSION_GRANTED) {
277 throw new SecurityException("Requires VIBRATE permission");
278 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800279 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 // so wakelock calls will succeed
281 long identity = Binder.clearCallingIdentity();
282 try {
Jeff Brown82379ba2014-07-25 19:03:28 -0700283 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 String s = "";
285 int N = pattern.length;
286 for (int i=0; i<N; i++) {
287 s += " " + pattern[i];
288 }
Jeff Brown82379ba2014-07-25 19:03:28 -0700289 Slog.d(TAG, "Vibrating with pattern:" + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 }
291
292 // we're running in the server so we can't fail
293 if (pattern == null || pattern.length == 0
294 || isAll0(pattern)
295 || repeat >= pattern.length || token == null) {
296 return;
297 }
298
John Spurlock7b414672014-07-18 13:02:39 -0400299 Vibration vib = new Vibration(token, pattern, repeat, usageHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400300 try {
301 token.linkToDeath(vib, 0);
302 } catch (RemoteException e) {
303 return;
304 }
305
306 synchronized (mVibrations) {
307 removeVibrationLocked(token);
308 doCancelVibrateLocked();
309 if (repeat >= 0) {
310 mVibrations.addFirst(vib);
311 startNextVibrationLocked();
312 } else {
313 // A negative repeat means that this pattern is not meant
314 // to repeat. Treat it like a simple vibration.
315 mCurrentVibration = vib;
316 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
319 }
320 finally {
321 Binder.restoreCallingIdentity(identity);
322 }
323 }
324
Jeff Brown82379ba2014-07-25 19:03:28 -0700325 @Override // Binder call
Patrick Scott18dd5f02009-07-02 11:31:12 -0400326 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 mContext.enforceCallingOrSelfPermission(
328 android.Manifest.permission.VIBRATE,
329 "cancelVibrate");
330
331 // so wakelock calls will succeed
332 long identity = Binder.clearCallingIdentity();
333 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400334 synchronized (mVibrations) {
335 final Vibration vib = removeVibrationLocked(token);
336 if (vib == mCurrentVibration) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700337 if (DEBUG) {
338 Slog.d(TAG, "Canceling vibration.");
339 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400340 doCancelVibrateLocked();
341 startNextVibrationLocked();
342 }
343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 }
345 finally {
346 Binder.restoreCallingIdentity(identity);
347 }
348 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700349
Patrick Scott18dd5f02009-07-02 11:31:12 -0400350 private final Runnable mVibrationRunnable = new Runnable() {
Jeff Brown82379ba2014-07-25 19:03:28 -0700351 @Override
Patrick Scott18dd5f02009-07-02 11:31:12 -0400352 public void run() {
353 synchronized (mVibrations) {
354 doCancelVibrateLocked();
355 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400357 }
358 };
359
360 // Lock held on mVibrations
361 private void doCancelVibrateLocked() {
362 if (mThread != null) {
363 synchronized (mThread) {
364 mThread.mDone = true;
365 mThread.notify();
366 }
367 mThread = null;
368 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700369 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400370 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800371 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400372 }
373
374 // Lock held on mVibrations
375 private void startNextVibrationLocked() {
376 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800377 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200378 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400379 return;
380 }
381 mCurrentVibration = mVibrations.getFirst();
382 startVibrationLocked(mCurrentVibration);
383 }
384
385 // Lock held on mVibrations
386 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800387 try {
John Spurlock7b414672014-07-18 13:02:39 -0400388 if (mLowPowerMode
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -0700389 && vib.mUsageHint != AudioAttributes.USAGE_NOTIFICATION_RINGTONE) {
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700390 return;
391 }
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700392
John Spurlock1af30c72014-03-10 08:33:35 -0400393 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
John Spurlock7b414672014-07-18 13:02:39 -0400394 vib.mUsageHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400395 if (mode == AppOpsManager.MODE_ALLOWED) {
396 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400397 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400398 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800399 if (mode != AppOpsManager.MODE_ALLOWED) {
400 if (mode == AppOpsManager.MODE_ERRORED) {
401 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
402 }
403 mH.post(mVibrationRunnable);
404 return;
405 }
406 } catch (RemoteException e) {
407 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400408 if (vib.mTimeout != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400409 doVibratorOn(vib.mTimeout, vib.mUid, vib.mUsageHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400410 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
411 } else {
412 // mThread better be null here. doCancelVibrate should always be
413 // called before startNextVibrationLocked or startVibrationLocked.
414 mThread = new VibrateThread(vib);
415 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417 }
418
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800419 private void reportFinishVibrationLocked() {
420 if (mCurrentVibration != null) {
421 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700422 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
423 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400424 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800425 } catch (RemoteException e) {
426 }
427 mCurrentVibration = null;
428 }
429 }
430
Patrick Scott18dd5f02009-07-02 11:31:12 -0400431 // Lock held on mVibrations
432 private Vibration removeVibrationLocked(IBinder token) {
433 ListIterator<Vibration> iter = mVibrations.listIterator(0);
434 while (iter.hasNext()) {
435 Vibration vib = iter.next();
436 if (vib.mToken == token) {
437 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200438 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400439 return vib;
440 }
441 }
442 // We might be looking for a simple vibration which is only stored in
443 // mCurrentVibration.
444 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200445 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400446 return mCurrentVibration;
447 }
448 return null;
449 }
450
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200451 private void unlinkVibration(Vibration vib) {
452 if (vib.mPattern != null) {
453 // If Vibration object has a pattern,
454 // the Vibration object has also been linkedToDeath.
455 vib.mToken.unlinkToDeath(vib, 0);
456 }
457 }
458
Jeff Brown7f6c2312012-04-13 20:38:38 -0700459 private void updateInputDeviceVibrators() {
460 synchronized (mVibrations) {
461 doCancelVibrateLocked();
462
463 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700464 mVibrateInputDevicesSetting = false;
465 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700466 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
467 mContext.getContentResolver(),
468 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700469 } catch (SettingNotFoundException snfe) {
470 }
471
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700472 mLowPowerMode = mPowerManagerInternal.getLowPowerModeEnabled();
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700473
Jeff Brown82065252012-04-16 13:19:05 -0700474 if (mVibrateInputDevicesSetting) {
475 if (!mInputDeviceListenerRegistered) {
476 mInputDeviceListenerRegistered = true;
477 mIm.registerInputDeviceListener(this, mH);
478 }
479 } else {
480 if (mInputDeviceListenerRegistered) {
481 mInputDeviceListenerRegistered = false;
482 mIm.unregisterInputDeviceListener(this);
483 }
484 }
485
Jeff Brown7f6c2312012-04-13 20:38:38 -0700486 mInputDeviceVibrators.clear();
487 if (mVibrateInputDevicesSetting) {
488 int[] ids = mIm.getInputDeviceIds();
489 for (int i = 0; i < ids.length; i++) {
490 InputDevice device = mIm.getInputDevice(ids[i]);
491 Vibrator vibrator = device.getVibrator();
492 if (vibrator.hasVibrator()) {
493 mInputDeviceVibrators.add(vibrator);
494 }
495 }
496 }
497 }
498
499 startNextVibrationLocked();
500 }
501 }
502
503 @Override
504 public void onInputDeviceAdded(int deviceId) {
505 updateInputDeviceVibrators();
506 }
507
508 @Override
509 public void onInputDeviceChanged(int deviceId) {
510 updateInputDeviceVibrators();
511 }
512
513 @Override
514 public void onInputDeviceRemoved(int deviceId) {
515 updateInputDeviceVibrators();
516 }
517
518 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700519 // For now, we choose to ignore the presence of input devices that have vibrators
520 // when reporting whether the device has a vibrator. Applications often use this
521 // information to decide whether to enable certain features so they expect the
522 // result of hasVibrator() to be constant. For now, just report whether
523 // the device has a built-in vibrator.
524 //synchronized (mInputDeviceVibrators) {
525 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
526 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800527 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700528 }
529
John Spurlock7b414672014-07-18 13:02:39 -0400530 private void doVibratorOn(long millis, int uid, int usageHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700531 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700532 if (DEBUG) {
533 Slog.d(TAG, "Turning vibrator on for " + millis + " ms.");
534 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800535 try {
536 mBatteryStatsService.noteVibratorOn(uid, millis);
537 mCurVibUid = uid;
538 } catch (RemoteException e) {
539 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700540 final int vibratorCount = mInputDeviceVibrators.size();
541 if (vibratorCount != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400542 final AudioAttributes attributes = new AudioAttributes.Builder().setUsage(usageHint)
543 .build();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700544 for (int i = 0; i < vibratorCount; i++) {
John Spurlock7b414672014-07-18 13:02:39 -0400545 mInputDeviceVibrators.get(i).vibrate(millis, attributes);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700546 }
547 } else {
548 vibratorOn(millis);
549 }
550 }
551 }
552
553 private void doVibratorOff() {
554 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700555 if (DEBUG) {
556 Slog.d(TAG, "Turning vibrator off.");
557 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800558 if (mCurVibUid >= 0) {
559 try {
560 mBatteryStatsService.noteVibratorOff(mCurVibUid);
561 } catch (RemoteException e) {
562 }
563 mCurVibUid = -1;
564 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700565 final int vibratorCount = mInputDeviceVibrators.size();
566 if (vibratorCount != 0) {
567 for (int i = 0; i < vibratorCount; i++) {
568 mInputDeviceVibrators.get(i).cancel();
569 }
570 } else {
571 vibratorOff();
572 }
573 }
574 }
575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400577 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700579
Patrick Scott18dd5f02009-07-02 11:31:12 -0400580 VibrateThread(Vibration vib) {
581 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700582 mTmpWorkSource.set(vib.mUid);
583 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 mWakeLock.acquire();
585 }
586
587 private void delay(long duration) {
588 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700589 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 do {
591 try {
592 this.wait(duration);
593 }
594 catch (InterruptedException e) {
595 }
596 if (mDone) {
597 break;
598 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700599 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 } while (duration > 0);
601 }
602 }
603
604 public void run() {
605 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
606 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800607 final long[] pattern = mVibration.mPattern;
608 final int len = pattern.length;
609 final int repeat = mVibration.mRepeat;
610 final int uid = mVibration.mUid;
John Spurlock7b414672014-07-18 13:02:39 -0400611 final int usageHint = mVibration.mUsageHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 long duration = 0;
614
615 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700616 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 if (index < len) {
618 duration += pattern[index++];
619 }
620
621 // sleep until it is time to start the vibrator
622 delay(duration);
623 if (mDone) {
624 break;
625 }
626
627 if (index < len) {
628 // read on-time duration and start the vibrator
629 // duration is saved for delay() at top of loop
630 duration = pattern[index++];
631 if (duration > 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400632 VibratorService.this.doVibratorOn(duration, uid, usageHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400635 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 break;
637 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400638 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 duration = 0;
640 }
641 }
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 mWakeLock.release();
644 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400645 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 if (mThread == this) {
647 mThread = null;
648 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400649 if (!mDone) {
650 // If this vibration finished naturally, start the next
651 // vibration.
652 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200653 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400654 startNextVibrationLocked();
655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 }
657 }
Jeff Brown969579b2014-05-20 19:29:29 -0700658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
Jeff Brown969579b2014-05-20 19:29:29 -0700661 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 public void onReceive(Context context, Intent intent) {
663 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400664 synchronized (mVibrations) {
Jeff Brown969579b2014-05-20 19:29:29 -0700665 // When the system is entering a non-interactive state, we want
666 // to cancel vibrations in case a misbehaving app has abandoned
667 // them. However it may happen that the system is currently playing
668 // haptic feedback as part of the transition. So we don't cancel
669 // system vibrations.
670 if (mCurrentVibration != null
671 && !mCurrentVibration.isSystemHapticFeedback()) {
672 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700673 }
674
Jeff Brown969579b2014-05-20 19:29:29 -0700675 // Clear all remaining vibrations.
676 Iterator<Vibration> it = mVibrations.iterator();
677 while (it.hasNext()) {
678 Vibration vibration = it.next();
679 if (vibration != mCurrentVibration) {
680 unlinkVibration(vibration);
681 it.remove();
682 }
683 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 }
686 }
687 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688}