blob: ea9de1e83cccd3a261451deeca14069201bb1571 [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;
31import android.os.Process;
32import android.os.RemoteException;
33import android.os.IBinder;
34import android.os.Binder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080035import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.SystemClock;
Jeff Brownd4935962012-09-25 13:27:20 -070037import android.os.UserHandle;
Jeff Brown7f6c2312012-04-13 20:38:38 -070038import android.os.Vibrator;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070039import android.os.WorkSource;
Jeff Brown7f6c2312012-04-13 20:38:38 -070040import android.provider.Settings;
41import android.provider.Settings.SettingNotFoundException;
Joe Onorato8a9b2202010-02-26 18:56:32 -080042import android.util.Slog;
Jeff Brown7f6c2312012-04-13 20:38:38 -070043import android.view.InputDevice;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070044import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
Dianne Hackborna06de0f2012-12-11 16:34:47 -080046import com.android.internal.app.IAppOpsService;
47import com.android.internal.app.IBatteryStats;
48
Jeff Brown7f6c2312012-04-13 20:38:38 -070049import java.util.ArrayList;
Patrick Scott18dd5f02009-07-02 11:31:12 -040050import java.util.LinkedList;
51import java.util.ListIterator;
52
Jeff Brown7f6c2312012-04-13 20:38:38 -070053public class VibratorService extends IVibratorService.Stub
54 implements InputManager.InputDeviceListener {
Mike Lockwood3a322132009-11-24 00:30:52 -050055 private static final String TAG = "VibratorService";
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050056
Patrick Scott18dd5f02009-07-02 11:31:12 -040057 private final LinkedList<Vibration> mVibrations;
58 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070059 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070060 private final Handler mH = new Handler();
61
62 private final Context mContext;
63 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080064 private final IAppOpsService mAppOpsService;
65 private final IBatteryStats mBatteryStatsService;
Jeff Brown7f6c2312012-04-13 20:38:38 -070066 private InputManager mIm;
67
68 volatile VibrateThread mThread;
69
70 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
71 // to be acquired
72 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
73 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
74 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
75
Dianne Hackborna06de0f2012-12-11 16:34:47 -080076 private int mCurVibUid = -1;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070077 private boolean mLowPowerMode;
78 private SettingsObserver mSettingObserver;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080079
Jeff Brown7f6c2312012-04-13 20:38:38 -070080 native static boolean vibratorExists();
81 native static void vibratorOn(long milliseconds);
82 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040083
Patrick Scott18dd5f02009-07-02 11:31:12 -040084 private class Vibration implements IBinder.DeathRecipient {
85 private final IBinder mToken;
86 private final long mTimeout;
87 private final long mStartTime;
88 private final long[] mPattern;
89 private final int mRepeat;
John Spurlock1af30c72014-03-10 08:33:35 -040090 private final int mStreamHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070091 private final int mUid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040092 private final String mOpPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -040093
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040094 Vibration(IBinder token, long millis, int streamHint, int uid, String opPkg) {
95 this(token, millis, null, 0, streamHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -040096 }
97
John Spurlock1af30c72014-03-10 08:33:35 -040098 Vibration(IBinder token, long[] pattern, int repeat, int streamHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040099 String opPkg) {
100 this(token, 0, pattern, repeat, streamHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400101 }
102
103 private Vibration(IBinder token, long millis, long[] pattern,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400104 int repeat, int streamHint, int uid, String opPkg) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400105 mToken = token;
106 mTimeout = millis;
107 mStartTime = SystemClock.uptimeMillis();
108 mPattern = pattern;
109 mRepeat = repeat;
John Spurlock1af30c72014-03-10 08:33:35 -0400110 mStreamHint = streamHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700111 mUid = uid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400112 mOpPkg = opPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400113 }
114
115 public void binderDied() {
116 synchronized (mVibrations) {
117 mVibrations.remove(this);
118 if (this == mCurrentVibration) {
119 doCancelVibrateLocked();
120 startNextVibrationLocked();
121 }
122 }
123 }
124
125 public boolean hasLongerTimeout(long millis) {
126 if (mTimeout == 0) {
127 // This is a pattern, return false to play the simple
128 // vibration.
129 return false;
130 }
131 if ((mStartTime + mTimeout)
132 < (SystemClock.uptimeMillis() + millis)) {
133 // If this vibration will end before the time passed in, let
134 // the new vibration play.
135 return false;
136 }
137 return true;
138 }
139 }
140
Mike Lockwood3a322132009-11-24 00:30:52 -0500141 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 // Reset the hardware to a default state, in case this is a runtime
143 // restart instead of a fresh boot.
144 vibratorOff();
145
146 mContext = context;
147 PowerManager pm = (PowerManager)context.getSystemService(
148 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700149 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 mWakeLock.setReferenceCounted(true);
151
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800152 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700153 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
154 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800155
Patrick Scott18dd5f02009-07-02 11:31:12 -0400156 mVibrations = new LinkedList<Vibration>();
157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 IntentFilter filter = new IntentFilter();
159 filter.addAction(Intent.ACTION_SCREEN_OFF);
160 context.registerReceiver(mIntentReceiver, filter);
161 }
162
Jeff Brown7f6c2312012-04-13 20:38:38 -0700163 public void systemReady() {
164 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700165 mSettingObserver = new SettingsObserver(mH);
Jeff Brownd4935962012-09-25 13:27:20 -0700166
Jeff Brown7f6c2312012-04-13 20:38:38 -0700167 mContext.getContentResolver().registerContentObserver(
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700168 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES),
169 true, mSettingObserver, UserHandle.USER_ALL);
170
171 mContext.getContentResolver().registerContentObserver(
172 Settings.Global.getUriFor(Settings.Global.LOW_POWER_MODE), false,
173 mSettingObserver, UserHandle.USER_ALL);
Jeff Brownd4935962012-09-25 13:27:20 -0700174
175 mContext.registerReceiver(new BroadcastReceiver() {
176 @Override
177 public void onReceive(Context context, Intent intent) {
178 updateInputDeviceVibrators();
179 }
180 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
181
Jeff Brown82065252012-04-16 13:19:05 -0700182 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700183 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700184
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700185 private final class SettingsObserver extends ContentObserver {
186 public SettingsObserver(Handler handler) {
187 super(handler);
188 }
189
190 @Override
191 public void onChange(boolean SelfChange) {
192 updateInputDeviceVibrators();
193 }
194 }
195
Jeff Brown7f6c2312012-04-13 20:38:38 -0700196 public boolean hasVibrator() {
197 return doVibratorExists();
198 }
199
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800200 private void verifyIncomingUid(int uid) {
201 if (uid == Binder.getCallingUid()) {
202 return;
203 }
204 if (Binder.getCallingPid() == Process.myPid()) {
205 return;
206 }
207 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
208 Binder.getCallingPid(), Binder.getCallingUid(), null);
209 }
210
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400211 public void vibrate(int uid, String opPkg, long milliseconds, int streamHint,
John Spurlock1af30c72014-03-10 08:33:35 -0400212 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700213 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
214 != PackageManager.PERMISSION_GRANTED) {
215 throw new SecurityException("Requires VIBRATE permission");
216 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800217 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400218 // We're running in the system server so we cannot crash. Check for a
219 // timeout of 0 or negative. This will ensure that a vibration has
220 // either a timeout of > 0 or a non-null pattern.
221 if (milliseconds <= 0 || (mCurrentVibration != null
222 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400223 // Ignore this vibration since the current vibration will play for
224 // longer than milliseconds.
225 return;
226 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700227
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400228 Vibration vib = new Vibration(token, milliseconds, streamHint, uid, opPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800229
230 final long ident = Binder.clearCallingIdentity();
231 try {
232 synchronized (mVibrations) {
233 removeVibrationLocked(token);
234 doCancelVibrateLocked();
235 mCurrentVibration = vib;
236 startVibrationLocked(vib);
237 }
238 } finally {
239 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242
243 private boolean isAll0(long[] pattern) {
244 int N = pattern.length;
245 for (int i = 0; i < N; i++) {
246 if (pattern[i] != 0) {
247 return false;
248 }
249 }
250 return true;
251 }
252
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800253 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock1af30c72014-03-10 08:33:35 -0400254 int streamHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
256 != PackageManager.PERMISSION_GRANTED) {
257 throw new SecurityException("Requires VIBRATE permission");
258 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800259 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 // so wakelock calls will succeed
261 long identity = Binder.clearCallingIdentity();
262 try {
263 if (false) {
264 String s = "";
265 int N = pattern.length;
266 for (int i=0; i<N; i++) {
267 s += " " + pattern[i];
268 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800269 Slog.i(TAG, "vibrating with pattern: " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271
272 // we're running in the server so we can't fail
273 if (pattern == null || pattern.length == 0
274 || isAll0(pattern)
275 || repeat >= pattern.length || token == null) {
276 return;
277 }
278
John Spurlock1af30c72014-03-10 08:33:35 -0400279 Vibration vib = new Vibration(token, pattern, repeat, streamHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400280 try {
281 token.linkToDeath(vib, 0);
282 } catch (RemoteException e) {
283 return;
284 }
285
286 synchronized (mVibrations) {
287 removeVibrationLocked(token);
288 doCancelVibrateLocked();
289 if (repeat >= 0) {
290 mVibrations.addFirst(vib);
291 startNextVibrationLocked();
292 } else {
293 // A negative repeat means that this pattern is not meant
294 // to repeat. Treat it like a simple vibration.
295 mCurrentVibration = vib;
296 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 }
299 }
300 finally {
301 Binder.restoreCallingIdentity(identity);
302 }
303 }
304
Patrick Scott18dd5f02009-07-02 11:31:12 -0400305 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 mContext.enforceCallingOrSelfPermission(
307 android.Manifest.permission.VIBRATE,
308 "cancelVibrate");
309
310 // so wakelock calls will succeed
311 long identity = Binder.clearCallingIdentity();
312 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400313 synchronized (mVibrations) {
314 final Vibration vib = removeVibrationLocked(token);
315 if (vib == mCurrentVibration) {
316 doCancelVibrateLocked();
317 startNextVibrationLocked();
318 }
319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
321 finally {
322 Binder.restoreCallingIdentity(identity);
323 }
324 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700325
Patrick Scott18dd5f02009-07-02 11:31:12 -0400326 private final Runnable mVibrationRunnable = new Runnable() {
327 public void run() {
328 synchronized (mVibrations) {
329 doCancelVibrateLocked();
330 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400332 }
333 };
334
335 // Lock held on mVibrations
336 private void doCancelVibrateLocked() {
337 if (mThread != null) {
338 synchronized (mThread) {
339 mThread.mDone = true;
340 mThread.notify();
341 }
342 mThread = null;
343 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700344 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400345 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800346 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400347 }
348
349 // Lock held on mVibrations
350 private void startNextVibrationLocked() {
351 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800352 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200353 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400354 return;
355 }
356 mCurrentVibration = mVibrations.getFirst();
357 startVibrationLocked(mCurrentVibration);
358 }
359
360 // Lock held on mVibrations
361 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800362 try {
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700363 if (mLowPowerMode && vib.mStreamHint != AudioManager.STREAM_RING)
364 return;
365
John Spurlock1af30c72014-03-10 08:33:35 -0400366 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400367 vib.mStreamHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400368 if (mode == AppOpsManager.MODE_ALLOWED) {
369 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400370 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400371 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800372 if (mode != AppOpsManager.MODE_ALLOWED) {
373 if (mode == AppOpsManager.MODE_ERRORED) {
374 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
375 }
376 mH.post(mVibrationRunnable);
377 return;
378 }
379 } catch (RemoteException e) {
380 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400381 if (vib.mTimeout != 0) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400382 doVibratorOn(vib.mTimeout, vib.mUid, vib.mStreamHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400383 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
384 } else {
385 // mThread better be null here. doCancelVibrate should always be
386 // called before startNextVibrationLocked or startVibrationLocked.
387 mThread = new VibrateThread(vib);
388 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 }
390 }
391
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800392 private void reportFinishVibrationLocked() {
393 if (mCurrentVibration != null) {
394 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700395 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
396 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400397 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800398 } catch (RemoteException e) {
399 }
400 mCurrentVibration = null;
401 }
402 }
403
Patrick Scott18dd5f02009-07-02 11:31:12 -0400404 // Lock held on mVibrations
405 private Vibration removeVibrationLocked(IBinder token) {
406 ListIterator<Vibration> iter = mVibrations.listIterator(0);
407 while (iter.hasNext()) {
408 Vibration vib = iter.next();
409 if (vib.mToken == token) {
410 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200411 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400412 return vib;
413 }
414 }
415 // We might be looking for a simple vibration which is only stored in
416 // mCurrentVibration.
417 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200418 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400419 return mCurrentVibration;
420 }
421 return null;
422 }
423
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200424 private void unlinkVibration(Vibration vib) {
425 if (vib.mPattern != null) {
426 // If Vibration object has a pattern,
427 // the Vibration object has also been linkedToDeath.
428 vib.mToken.unlinkToDeath(vib, 0);
429 }
430 }
431
Jeff Brown7f6c2312012-04-13 20:38:38 -0700432 private void updateInputDeviceVibrators() {
433 synchronized (mVibrations) {
434 doCancelVibrateLocked();
435
436 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700437 mVibrateInputDevicesSetting = false;
438 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700439 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
440 mContext.getContentResolver(),
441 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700442 } catch (SettingNotFoundException snfe) {
443 }
444
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700445 mLowPowerMode = Settings.Global.getInt(mContext.getContentResolver(),
446 Settings.Global.LOW_POWER_MODE, 0) != 0;
447
Jeff Brown82065252012-04-16 13:19:05 -0700448 if (mVibrateInputDevicesSetting) {
449 if (!mInputDeviceListenerRegistered) {
450 mInputDeviceListenerRegistered = true;
451 mIm.registerInputDeviceListener(this, mH);
452 }
453 } else {
454 if (mInputDeviceListenerRegistered) {
455 mInputDeviceListenerRegistered = false;
456 mIm.unregisterInputDeviceListener(this);
457 }
458 }
459
Jeff Brown7f6c2312012-04-13 20:38:38 -0700460 mInputDeviceVibrators.clear();
461 if (mVibrateInputDevicesSetting) {
462 int[] ids = mIm.getInputDeviceIds();
463 for (int i = 0; i < ids.length; i++) {
464 InputDevice device = mIm.getInputDevice(ids[i]);
465 Vibrator vibrator = device.getVibrator();
466 if (vibrator.hasVibrator()) {
467 mInputDeviceVibrators.add(vibrator);
468 }
469 }
470 }
471 }
472
473 startNextVibrationLocked();
474 }
475 }
476
477 @Override
478 public void onInputDeviceAdded(int deviceId) {
479 updateInputDeviceVibrators();
480 }
481
482 @Override
483 public void onInputDeviceChanged(int deviceId) {
484 updateInputDeviceVibrators();
485 }
486
487 @Override
488 public void onInputDeviceRemoved(int deviceId) {
489 updateInputDeviceVibrators();
490 }
491
492 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700493 // For now, we choose to ignore the presence of input devices that have vibrators
494 // when reporting whether the device has a vibrator. Applications often use this
495 // information to decide whether to enable certain features so they expect the
496 // result of hasVibrator() to be constant. For now, just report whether
497 // the device has a built-in vibrator.
498 //synchronized (mInputDeviceVibrators) {
499 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
500 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800501 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700502 }
503
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400504 private void doVibratorOn(long millis, int uid, int streamHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700505 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800506 try {
507 mBatteryStatsService.noteVibratorOn(uid, millis);
508 mCurVibUid = uid;
509 } catch (RemoteException e) {
510 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700511 final int vibratorCount = mInputDeviceVibrators.size();
512 if (vibratorCount != 0) {
513 for (int i = 0; i < vibratorCount; i++) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400514 mInputDeviceVibrators.get(i).vibrate(millis, streamHint);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700515 }
516 } else {
517 vibratorOn(millis);
518 }
519 }
520 }
521
522 private void doVibratorOff() {
523 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800524 if (mCurVibUid >= 0) {
525 try {
526 mBatteryStatsService.noteVibratorOff(mCurVibUid);
527 } catch (RemoteException e) {
528 }
529 mCurVibUid = -1;
530 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700531 final int vibratorCount = mInputDeviceVibrators.size();
532 if (vibratorCount != 0) {
533 for (int i = 0; i < vibratorCount; i++) {
534 mInputDeviceVibrators.get(i).cancel();
535 }
536 } else {
537 vibratorOff();
538 }
539 }
540 }
541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400543 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700545
Patrick Scott18dd5f02009-07-02 11:31:12 -0400546 VibrateThread(Vibration vib) {
547 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700548 mTmpWorkSource.set(vib.mUid);
549 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 mWakeLock.acquire();
551 }
552
553 private void delay(long duration) {
554 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700555 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 do {
557 try {
558 this.wait(duration);
559 }
560 catch (InterruptedException e) {
561 }
562 if (mDone) {
563 break;
564 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700565 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 } while (duration > 0);
567 }
568 }
569
570 public void run() {
571 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
572 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800573 final long[] pattern = mVibration.mPattern;
574 final int len = pattern.length;
575 final int repeat = mVibration.mRepeat;
576 final int uid = mVibration.mUid;
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400577 final int streamHint = mVibration.mStreamHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 long duration = 0;
580
581 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700582 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 if (index < len) {
584 duration += pattern[index++];
585 }
586
587 // sleep until it is time to start the vibrator
588 delay(duration);
589 if (mDone) {
590 break;
591 }
592
593 if (index < len) {
594 // read on-time duration and start the vibrator
595 // duration is saved for delay() at top of loop
596 duration = pattern[index++];
597 if (duration > 0) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400598 VibratorService.this.doVibratorOn(duration, uid, streamHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
600 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400601 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 break;
603 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400604 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 duration = 0;
606 }
607 }
608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 mWakeLock.release();
610 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400611 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 if (mThread == this) {
613 mThread = null;
614 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400615 if (!mDone) {
616 // If this vibration finished naturally, start the next
617 // vibration.
618 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200619 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400620 startNextVibrationLocked();
621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623 }
624 };
625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
627 public void onReceive(Context context, Intent intent) {
628 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400629 synchronized (mVibrations) {
630 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700631
632 int size = mVibrations.size();
633 for(int i = 0; i < size; i++) {
634 unlinkVibration(mVibrations.get(i));
635 }
636
Patrick Scott18dd5f02009-07-02 11:31:12 -0400637 mVibrations.clear();
638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 }
640 }
641 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642}