blob: 9557ebde794b4ef2df08442d54e8afeed7f7f76a [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();
Vincent Beckere6904fb2012-08-10 14:17:33 +020085 native static void vibratorInit();
Jeff Brown7f6c2312012-04-13 20:38:38 -070086 native static void vibratorOn(long milliseconds);
87 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040088
Patrick Scott18dd5f02009-07-02 11:31:12 -040089 private class Vibration implements IBinder.DeathRecipient {
90 private final IBinder mToken;
91 private final long mTimeout;
92 private final long mStartTime;
93 private final long[] mPattern;
94 private final int mRepeat;
John Spurlock7b414672014-07-18 13:02:39 -040095 private final int mUsageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070096 private final int mUid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040097 private final String mOpPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -040098
John Spurlock7b414672014-07-18 13:02:39 -040099 Vibration(IBinder token, long millis, int usageHint, int uid, String opPkg) {
100 this(token, millis, null, 0, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400101 }
102
John Spurlock7b414672014-07-18 13:02:39 -0400103 Vibration(IBinder token, long[] pattern, int repeat, int usageHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400104 String opPkg) {
John Spurlock7b414672014-07-18 13:02:39 -0400105 this(token, 0, pattern, repeat, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400106 }
107
108 private Vibration(IBinder token, long millis, long[] pattern,
John Spurlock7b414672014-07-18 13:02:39 -0400109 int repeat, int usageHint, int uid, String opPkg) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400110 mToken = token;
111 mTimeout = millis;
112 mStartTime = SystemClock.uptimeMillis();
113 mPattern = pattern;
114 mRepeat = repeat;
John Spurlock7b414672014-07-18 13:02:39 -0400115 mUsageHint = usageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700116 mUid = uid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400117 mOpPkg = opPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400118 }
119
120 public void binderDied() {
121 synchronized (mVibrations) {
122 mVibrations.remove(this);
123 if (this == mCurrentVibration) {
124 doCancelVibrateLocked();
125 startNextVibrationLocked();
126 }
127 }
128 }
129
130 public boolean hasLongerTimeout(long millis) {
131 if (mTimeout == 0) {
132 // This is a pattern, return false to play the simple
133 // vibration.
134 return false;
135 }
136 if ((mStartTime + mTimeout)
137 < (SystemClock.uptimeMillis() + millis)) {
138 // If this vibration will end before the time passed in, let
139 // the new vibration play.
140 return false;
141 }
142 return true;
143 }
Jeff Brown969579b2014-05-20 19:29:29 -0700144
145 public boolean isSystemHapticFeedback() {
146 return (mUid == Process.SYSTEM_UID || mUid == 0) && mRepeat < 0;
147 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400148 }
149
Mike Lockwood3a322132009-11-24 00:30:52 -0500150 VibratorService(Context context) {
Vincent Beckere6904fb2012-08-10 14:17:33 +0200151 vibratorInit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 // Reset the hardware to a default state, in case this is a runtime
153 // restart instead of a fresh boot.
154 vibratorOff();
155
156 mContext = context;
157 PowerManager pm = (PowerManager)context.getSystemService(
158 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700159 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 mWakeLock.setReferenceCounted(true);
161
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800162 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700163 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
164 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800165
Patrick Scott18dd5f02009-07-02 11:31:12 -0400166 mVibrations = new LinkedList<Vibration>();
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 IntentFilter filter = new IntentFilter();
169 filter.addAction(Intent.ACTION_SCREEN_OFF);
170 context.registerReceiver(mIntentReceiver, filter);
171 }
172
Jeff Brown7f6c2312012-04-13 20:38:38 -0700173 public void systemReady() {
174 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700175 mSettingObserver = new SettingsObserver(mH);
Jeff Brownd4935962012-09-25 13:27:20 -0700176
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700177 mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
178 mPowerManagerInternal.registerLowPowerModeObserver(
179 new PowerManagerInternal.LowPowerModeListener() {
180 @Override
181 public void onLowPowerModeChanged(boolean enabled) {
182 updateInputDeviceVibrators();
183 }
184 });
185
Jeff Brown7f6c2312012-04-13 20:38:38 -0700186 mContext.getContentResolver().registerContentObserver(
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700187 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES),
188 true, mSettingObserver, UserHandle.USER_ALL);
189
Jeff Brownd4935962012-09-25 13:27:20 -0700190 mContext.registerReceiver(new BroadcastReceiver() {
191 @Override
192 public void onReceive(Context context, Intent intent) {
193 updateInputDeviceVibrators();
194 }
195 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
196
Jeff Brown82065252012-04-16 13:19:05 -0700197 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700198 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700199
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700200 private final class SettingsObserver extends ContentObserver {
201 public SettingsObserver(Handler handler) {
202 super(handler);
203 }
204
205 @Override
206 public void onChange(boolean SelfChange) {
207 updateInputDeviceVibrators();
208 }
209 }
210
Jeff Brown82379ba2014-07-25 19:03:28 -0700211 @Override // Binder call
Jeff Brown7f6c2312012-04-13 20:38:38 -0700212 public boolean hasVibrator() {
213 return doVibratorExists();
214 }
215
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800216 private void verifyIncomingUid(int uid) {
217 if (uid == Binder.getCallingUid()) {
218 return;
219 }
220 if (Binder.getCallingPid() == Process.myPid()) {
221 return;
222 }
223 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
224 Binder.getCallingPid(), Binder.getCallingUid(), null);
225 }
226
Jeff Brown82379ba2014-07-25 19:03:28 -0700227 @Override // Binder call
John Spurlock7b414672014-07-18 13:02:39 -0400228 public void vibrate(int uid, String opPkg, long milliseconds, int usageHint,
John Spurlock1af30c72014-03-10 08:33:35 -0400229 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700230 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
231 != PackageManager.PERMISSION_GRANTED) {
232 throw new SecurityException("Requires VIBRATE permission");
233 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800234 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400235 // We're running in the system server so we cannot crash. Check for a
236 // timeout of 0 or negative. This will ensure that a vibration has
237 // either a timeout of > 0 or a non-null pattern.
238 if (milliseconds <= 0 || (mCurrentVibration != null
239 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400240 // Ignore this vibration since the current vibration will play for
241 // longer than milliseconds.
242 return;
243 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700244
Jeff Brown82379ba2014-07-25 19:03:28 -0700245 if (DEBUG) {
246 Slog.d(TAG, "Vibrating for " + milliseconds + " ms.");
247 }
248
John Spurlock7b414672014-07-18 13:02:39 -0400249 Vibration vib = new Vibration(token, milliseconds, usageHint, uid, opPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800250
251 final long ident = Binder.clearCallingIdentity();
252 try {
253 synchronized (mVibrations) {
254 removeVibrationLocked(token);
255 doCancelVibrateLocked();
256 mCurrentVibration = vib;
257 startVibrationLocked(vib);
258 }
259 } finally {
260 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
263
264 private boolean isAll0(long[] pattern) {
265 int N = pattern.length;
266 for (int i = 0; i < N; i++) {
267 if (pattern[i] != 0) {
268 return false;
269 }
270 }
271 return true;
272 }
273
Jeff Brown82379ba2014-07-25 19:03:28 -0700274 @Override // Binder call
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800275 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock7b414672014-07-18 13:02:39 -0400276 int usageHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
278 != PackageManager.PERMISSION_GRANTED) {
279 throw new SecurityException("Requires VIBRATE permission");
280 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800281 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 // so wakelock calls will succeed
283 long identity = Binder.clearCallingIdentity();
284 try {
Jeff Brown82379ba2014-07-25 19:03:28 -0700285 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 String s = "";
287 int N = pattern.length;
288 for (int i=0; i<N; i++) {
289 s += " " + pattern[i];
290 }
Jeff Brown82379ba2014-07-25 19:03:28 -0700291 Slog.d(TAG, "Vibrating with pattern:" + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 }
293
294 // we're running in the server so we can't fail
295 if (pattern == null || pattern.length == 0
296 || isAll0(pattern)
297 || repeat >= pattern.length || token == null) {
298 return;
299 }
300
John Spurlock7b414672014-07-18 13:02:39 -0400301 Vibration vib = new Vibration(token, pattern, repeat, usageHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400302 try {
303 token.linkToDeath(vib, 0);
304 } catch (RemoteException e) {
305 return;
306 }
307
308 synchronized (mVibrations) {
309 removeVibrationLocked(token);
310 doCancelVibrateLocked();
311 if (repeat >= 0) {
312 mVibrations.addFirst(vib);
313 startNextVibrationLocked();
314 } else {
315 // A negative repeat means that this pattern is not meant
316 // to repeat. Treat it like a simple vibration.
317 mCurrentVibration = vib;
318 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
321 }
322 finally {
323 Binder.restoreCallingIdentity(identity);
324 }
325 }
326
Jeff Brown82379ba2014-07-25 19:03:28 -0700327 @Override // Binder call
Patrick Scott18dd5f02009-07-02 11:31:12 -0400328 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 mContext.enforceCallingOrSelfPermission(
330 android.Manifest.permission.VIBRATE,
331 "cancelVibrate");
332
333 // so wakelock calls will succeed
334 long identity = Binder.clearCallingIdentity();
335 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400336 synchronized (mVibrations) {
337 final Vibration vib = removeVibrationLocked(token);
338 if (vib == mCurrentVibration) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700339 if (DEBUG) {
340 Slog.d(TAG, "Canceling vibration.");
341 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400342 doCancelVibrateLocked();
343 startNextVibrationLocked();
344 }
345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347 finally {
348 Binder.restoreCallingIdentity(identity);
349 }
350 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700351
Patrick Scott18dd5f02009-07-02 11:31:12 -0400352 private final Runnable mVibrationRunnable = new Runnable() {
Jeff Brown82379ba2014-07-25 19:03:28 -0700353 @Override
Patrick Scott18dd5f02009-07-02 11:31:12 -0400354 public void run() {
355 synchronized (mVibrations) {
356 doCancelVibrateLocked();
357 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400359 }
360 };
361
362 // Lock held on mVibrations
363 private void doCancelVibrateLocked() {
364 if (mThread != null) {
365 synchronized (mThread) {
366 mThread.mDone = true;
367 mThread.notify();
368 }
369 mThread = null;
370 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700371 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400372 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800373 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400374 }
375
376 // Lock held on mVibrations
377 private void startNextVibrationLocked() {
378 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800379 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200380 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400381 return;
382 }
383 mCurrentVibration = mVibrations.getFirst();
384 startVibrationLocked(mCurrentVibration);
385 }
386
387 // Lock held on mVibrations
388 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800389 try {
John Spurlock7b414672014-07-18 13:02:39 -0400390 if (mLowPowerMode
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -0700391 && vib.mUsageHint != AudioAttributes.USAGE_NOTIFICATION_RINGTONE) {
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700392 return;
393 }
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700394
John Spurlock1af30c72014-03-10 08:33:35 -0400395 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
John Spurlock7b414672014-07-18 13:02:39 -0400396 vib.mUsageHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400397 if (mode == AppOpsManager.MODE_ALLOWED) {
398 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400399 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400400 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800401 if (mode != AppOpsManager.MODE_ALLOWED) {
402 if (mode == AppOpsManager.MODE_ERRORED) {
403 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
404 }
405 mH.post(mVibrationRunnable);
406 return;
407 }
408 } catch (RemoteException e) {
409 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400410 if (vib.mTimeout != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400411 doVibratorOn(vib.mTimeout, vib.mUid, vib.mUsageHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400412 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
413 } else {
414 // mThread better be null here. doCancelVibrate should always be
415 // called before startNextVibrationLocked or startVibrationLocked.
416 mThread = new VibrateThread(vib);
417 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 }
419 }
420
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800421 private void reportFinishVibrationLocked() {
422 if (mCurrentVibration != null) {
423 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700424 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
425 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400426 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800427 } catch (RemoteException e) {
428 }
429 mCurrentVibration = null;
430 }
431 }
432
Patrick Scott18dd5f02009-07-02 11:31:12 -0400433 // Lock held on mVibrations
434 private Vibration removeVibrationLocked(IBinder token) {
435 ListIterator<Vibration> iter = mVibrations.listIterator(0);
436 while (iter.hasNext()) {
437 Vibration vib = iter.next();
438 if (vib.mToken == token) {
439 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200440 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400441 return vib;
442 }
443 }
444 // We might be looking for a simple vibration which is only stored in
445 // mCurrentVibration.
446 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200447 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400448 return mCurrentVibration;
449 }
450 return null;
451 }
452
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200453 private void unlinkVibration(Vibration vib) {
454 if (vib.mPattern != null) {
455 // If Vibration object has a pattern,
456 // the Vibration object has also been linkedToDeath.
457 vib.mToken.unlinkToDeath(vib, 0);
458 }
459 }
460
Jeff Brown7f6c2312012-04-13 20:38:38 -0700461 private void updateInputDeviceVibrators() {
462 synchronized (mVibrations) {
463 doCancelVibrateLocked();
464
465 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700466 mVibrateInputDevicesSetting = false;
467 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700468 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
469 mContext.getContentResolver(),
470 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700471 } catch (SettingNotFoundException snfe) {
472 }
473
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700474 mLowPowerMode = mPowerManagerInternal.getLowPowerModeEnabled();
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700475
Jeff Brown82065252012-04-16 13:19:05 -0700476 if (mVibrateInputDevicesSetting) {
477 if (!mInputDeviceListenerRegistered) {
478 mInputDeviceListenerRegistered = true;
479 mIm.registerInputDeviceListener(this, mH);
480 }
481 } else {
482 if (mInputDeviceListenerRegistered) {
483 mInputDeviceListenerRegistered = false;
484 mIm.unregisterInputDeviceListener(this);
485 }
486 }
487
Jeff Brown7f6c2312012-04-13 20:38:38 -0700488 mInputDeviceVibrators.clear();
489 if (mVibrateInputDevicesSetting) {
490 int[] ids = mIm.getInputDeviceIds();
491 for (int i = 0; i < ids.length; i++) {
492 InputDevice device = mIm.getInputDevice(ids[i]);
493 Vibrator vibrator = device.getVibrator();
494 if (vibrator.hasVibrator()) {
495 mInputDeviceVibrators.add(vibrator);
496 }
497 }
498 }
499 }
500
501 startNextVibrationLocked();
502 }
503 }
504
505 @Override
506 public void onInputDeviceAdded(int deviceId) {
507 updateInputDeviceVibrators();
508 }
509
510 @Override
511 public void onInputDeviceChanged(int deviceId) {
512 updateInputDeviceVibrators();
513 }
514
515 @Override
516 public void onInputDeviceRemoved(int deviceId) {
517 updateInputDeviceVibrators();
518 }
519
520 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700521 // For now, we choose to ignore the presence of input devices that have vibrators
522 // when reporting whether the device has a vibrator. Applications often use this
523 // information to decide whether to enable certain features so they expect the
524 // result of hasVibrator() to be constant. For now, just report whether
525 // the device has a built-in vibrator.
526 //synchronized (mInputDeviceVibrators) {
527 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
528 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800529 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700530 }
531
John Spurlock7b414672014-07-18 13:02:39 -0400532 private void doVibratorOn(long millis, int uid, int usageHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700533 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700534 if (DEBUG) {
535 Slog.d(TAG, "Turning vibrator on for " + millis + " ms.");
536 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800537 try {
538 mBatteryStatsService.noteVibratorOn(uid, millis);
539 mCurVibUid = uid;
540 } catch (RemoteException e) {
541 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700542 final int vibratorCount = mInputDeviceVibrators.size();
543 if (vibratorCount != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400544 final AudioAttributes attributes = new AudioAttributes.Builder().setUsage(usageHint)
545 .build();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700546 for (int i = 0; i < vibratorCount; i++) {
John Spurlock7b414672014-07-18 13:02:39 -0400547 mInputDeviceVibrators.get(i).vibrate(millis, attributes);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700548 }
549 } else {
550 vibratorOn(millis);
551 }
552 }
553 }
554
555 private void doVibratorOff() {
556 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700557 if (DEBUG) {
558 Slog.d(TAG, "Turning vibrator off.");
559 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800560 if (mCurVibUid >= 0) {
561 try {
562 mBatteryStatsService.noteVibratorOff(mCurVibUid);
563 } catch (RemoteException e) {
564 }
565 mCurVibUid = -1;
566 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700567 final int vibratorCount = mInputDeviceVibrators.size();
568 if (vibratorCount != 0) {
569 for (int i = 0; i < vibratorCount; i++) {
570 mInputDeviceVibrators.get(i).cancel();
571 }
572 } else {
573 vibratorOff();
574 }
575 }
576 }
577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400579 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700581
Patrick Scott18dd5f02009-07-02 11:31:12 -0400582 VibrateThread(Vibration vib) {
583 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700584 mTmpWorkSource.set(vib.mUid);
585 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 mWakeLock.acquire();
587 }
588
589 private void delay(long duration) {
590 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700591 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 do {
593 try {
594 this.wait(duration);
595 }
596 catch (InterruptedException e) {
597 }
598 if (mDone) {
599 break;
600 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700601 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 } while (duration > 0);
603 }
604 }
605
606 public void run() {
607 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
608 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800609 final long[] pattern = mVibration.mPattern;
610 final int len = pattern.length;
611 final int repeat = mVibration.mRepeat;
612 final int uid = mVibration.mUid;
John Spurlock7b414672014-07-18 13:02:39 -0400613 final int usageHint = mVibration.mUsageHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 long duration = 0;
616
617 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700618 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 if (index < len) {
620 duration += pattern[index++];
621 }
622
623 // sleep until it is time to start the vibrator
624 delay(duration);
625 if (mDone) {
626 break;
627 }
628
629 if (index < len) {
630 // read on-time duration and start the vibrator
631 // duration is saved for delay() at top of loop
632 duration = pattern[index++];
633 if (duration > 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400634 VibratorService.this.doVibratorOn(duration, uid, usageHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 }
636 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400637 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 break;
639 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400640 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 duration = 0;
642 }
643 }
644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 mWakeLock.release();
646 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400647 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 if (mThread == this) {
649 mThread = null;
650 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400651 if (!mDone) {
652 // If this vibration finished naturally, start the next
653 // vibration.
654 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200655 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400656 startNextVibrationLocked();
657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
659 }
Jeff Brown969579b2014-05-20 19:29:29 -0700660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
Jeff Brown969579b2014-05-20 19:29:29 -0700663 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 public void onReceive(Context context, Intent intent) {
665 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400666 synchronized (mVibrations) {
Jeff Brown969579b2014-05-20 19:29:29 -0700667 // When the system is entering a non-interactive state, we want
668 // to cancel vibrations in case a misbehaving app has abandoned
669 // them. However it may happen that the system is currently playing
670 // haptic feedback as part of the transition. So we don't cancel
671 // system vibrations.
672 if (mCurrentVibration != null
673 && !mCurrentVibration.isSystemHapticFeedback()) {
674 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700675 }
676
Jeff Brown969579b2014-05-20 19:29:29 -0700677 // Clear all remaining vibrations.
678 Iterator<Vibration> it = mVibrations.iterator();
679 while (it.hasNext()) {
680 Vibration vibration = it.next();
681 if (vibration != mCurrentVibration) {
682 unlinkVibration(vibration);
683 it.remove();
684 }
685 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
688 }
689 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690}