blob: 28eb948d416910a528a37f017ac11e702da942a4 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Dianne Hackborna06de0f2012-12-11 16:34:47 -080045import com.android.internal.app.IAppOpsService;
46import com.android.internal.app.IBatteryStats;
47
Jeff Brown7f6c2312012-04-13 20:38:38 -070048import java.util.ArrayList;
Patrick Scott18dd5f02009-07-02 11:31:12 -040049import java.util.LinkedList;
50import java.util.ListIterator;
51
Jeff Brown7f6c2312012-04-13 20:38:38 -070052public class VibratorService extends IVibratorService.Stub
53 implements InputManager.InputDeviceListener {
Mike Lockwood3a322132009-11-24 00:30:52 -050054 private static final String TAG = "VibratorService";
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050055
Patrick Scott18dd5f02009-07-02 11:31:12 -040056 private final LinkedList<Vibration> mVibrations;
57 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070058 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070059 private final Handler mH = new Handler();
60
61 private final Context mContext;
62 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080063 private final IAppOpsService mAppOpsService;
64 private final IBatteryStats mBatteryStatsService;
Jeff Brown7f6c2312012-04-13 20:38:38 -070065 private InputManager mIm;
66
67 volatile VibrateThread mThread;
68
69 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
70 // to be acquired
71 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
72 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
73 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
74
Dianne Hackborna06de0f2012-12-11 16:34:47 -080075 private int mCurVibUid = -1;
76
Jeff Brown7f6c2312012-04-13 20:38:38 -070077 native static boolean vibratorExists();
78 native static void vibratorOn(long milliseconds);
79 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040080
Patrick Scott18dd5f02009-07-02 11:31:12 -040081 private class Vibration implements IBinder.DeathRecipient {
82 private final IBinder mToken;
83 private final long mTimeout;
84 private final long mStartTime;
85 private final long[] mPattern;
86 private final int mRepeat;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070087 private final int mUid;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080088 private final String mPackageName;
Patrick Scott18dd5f02009-07-02 11:31:12 -040089
Dianne Hackborna06de0f2012-12-11 16:34:47 -080090 Vibration(IBinder token, long millis, int uid, String packageName) {
91 this(token, millis, null, 0, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -040092 }
93
Dianne Hackborna06de0f2012-12-11 16:34:47 -080094 Vibration(IBinder token, long[] pattern, int repeat, int uid, String packageName) {
95 this(token, 0, pattern, repeat, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -040096 }
97
98 private Vibration(IBinder token, long millis, long[] pattern,
Dianne Hackborna06de0f2012-12-11 16:34:47 -080099 int repeat, int uid, String packageName) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400100 mToken = token;
101 mTimeout = millis;
102 mStartTime = SystemClock.uptimeMillis();
103 mPattern = pattern;
104 mRepeat = repeat;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700105 mUid = uid;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800106 mPackageName = packageName;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400107 }
108
109 public void binderDied() {
110 synchronized (mVibrations) {
111 mVibrations.remove(this);
112 if (this == mCurrentVibration) {
113 doCancelVibrateLocked();
114 startNextVibrationLocked();
115 }
116 }
117 }
118
119 public boolean hasLongerTimeout(long millis) {
120 if (mTimeout == 0) {
121 // This is a pattern, return false to play the simple
122 // vibration.
123 return false;
124 }
125 if ((mStartTime + mTimeout)
126 < (SystemClock.uptimeMillis() + millis)) {
127 // If this vibration will end before the time passed in, let
128 // the new vibration play.
129 return false;
130 }
131 return true;
132 }
133 }
134
Mike Lockwood3a322132009-11-24 00:30:52 -0500135 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 // Reset the hardware to a default state, in case this is a runtime
137 // restart instead of a fresh boot.
138 vibratorOff();
139
140 mContext = context;
141 PowerManager pm = (PowerManager)context.getSystemService(
142 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700143 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 mWakeLock.setReferenceCounted(true);
145
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800146 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700147 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
148 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800149
Patrick Scott18dd5f02009-07-02 11:31:12 -0400150 mVibrations = new LinkedList<Vibration>();
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 IntentFilter filter = new IntentFilter();
153 filter.addAction(Intent.ACTION_SCREEN_OFF);
154 context.registerReceiver(mIntentReceiver, filter);
155 }
156
Jeff Brown7f6c2312012-04-13 20:38:38 -0700157 public void systemReady() {
158 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Jeff Brownd4935962012-09-25 13:27:20 -0700159
Jeff Brown7f6c2312012-04-13 20:38:38 -0700160 mContext.getContentResolver().registerContentObserver(
161 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES), true,
162 new ContentObserver(mH) {
163 @Override
164 public void onChange(boolean selfChange) {
Jeff Brown82065252012-04-16 13:19:05 -0700165 updateInputDeviceVibrators();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700166 }
Jeff Brownd4935962012-09-25 13:27:20 -0700167 }, UserHandle.USER_ALL);
168
169 mContext.registerReceiver(new BroadcastReceiver() {
170 @Override
171 public void onReceive(Context context, Intent intent) {
172 updateInputDeviceVibrators();
173 }
174 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
175
Jeff Brown82065252012-04-16 13:19:05 -0700176 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700177 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700178
179 public boolean hasVibrator() {
180 return doVibratorExists();
181 }
182
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800183 private void verifyIncomingUid(int uid) {
184 if (uid == Binder.getCallingUid()) {
185 return;
186 }
187 if (Binder.getCallingPid() == Process.myPid()) {
188 return;
189 }
190 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
191 Binder.getCallingPid(), Binder.getCallingUid(), null);
192 }
193
194 public void vibrate(int uid, String packageName, long milliseconds, IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700195 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
196 != PackageManager.PERMISSION_GRANTED) {
197 throw new SecurityException("Requires VIBRATE permission");
198 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800199 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400200 // We're running in the system server so we cannot crash. Check for a
201 // timeout of 0 or negative. This will ensure that a vibration has
202 // either a timeout of > 0 or a non-null pattern.
203 if (milliseconds <= 0 || (mCurrentVibration != null
204 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400205 // Ignore this vibration since the current vibration will play for
206 // longer than milliseconds.
207 return;
208 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700209
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800210 Vibration vib = new Vibration(token, milliseconds, uid, packageName);
211
212 final long ident = Binder.clearCallingIdentity();
213 try {
214 synchronized (mVibrations) {
215 removeVibrationLocked(token);
216 doCancelVibrateLocked();
217 mCurrentVibration = vib;
218 startVibrationLocked(vib);
219 }
220 } finally {
221 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 }
224
225 private boolean isAll0(long[] pattern) {
226 int N = pattern.length;
227 for (int i = 0; i < N; i++) {
228 if (pattern[i] != 0) {
229 return false;
230 }
231 }
232 return true;
233 }
234
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800235 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
236 IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
238 != PackageManager.PERMISSION_GRANTED) {
239 throw new SecurityException("Requires VIBRATE permission");
240 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800241 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 // so wakelock calls will succeed
243 long identity = Binder.clearCallingIdentity();
244 try {
245 if (false) {
246 String s = "";
247 int N = pattern.length;
248 for (int i=0; i<N; i++) {
249 s += " " + pattern[i];
250 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800251 Slog.i(TAG, "vibrating with pattern: " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 }
253
254 // we're running in the server so we can't fail
255 if (pattern == null || pattern.length == 0
256 || isAll0(pattern)
257 || repeat >= pattern.length || token == null) {
258 return;
259 }
260
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800261 Vibration vib = new Vibration(token, pattern, repeat, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400262 try {
263 token.linkToDeath(vib, 0);
264 } catch (RemoteException e) {
265 return;
266 }
267
268 synchronized (mVibrations) {
269 removeVibrationLocked(token);
270 doCancelVibrateLocked();
271 if (repeat >= 0) {
272 mVibrations.addFirst(vib);
273 startNextVibrationLocked();
274 } else {
275 // A negative repeat means that this pattern is not meant
276 // to repeat. Treat it like a simple vibration.
277 mCurrentVibration = vib;
278 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281 }
282 finally {
283 Binder.restoreCallingIdentity(identity);
284 }
285 }
286
Patrick Scott18dd5f02009-07-02 11:31:12 -0400287 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 mContext.enforceCallingOrSelfPermission(
289 android.Manifest.permission.VIBRATE,
290 "cancelVibrate");
291
292 // so wakelock calls will succeed
293 long identity = Binder.clearCallingIdentity();
294 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400295 synchronized (mVibrations) {
296 final Vibration vib = removeVibrationLocked(token);
297 if (vib == mCurrentVibration) {
298 doCancelVibrateLocked();
299 startNextVibrationLocked();
300 }
301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303 finally {
304 Binder.restoreCallingIdentity(identity);
305 }
306 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700307
Patrick Scott18dd5f02009-07-02 11:31:12 -0400308 private final Runnable mVibrationRunnable = new Runnable() {
309 public void run() {
310 synchronized (mVibrations) {
311 doCancelVibrateLocked();
312 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400314 }
315 };
316
317 // Lock held on mVibrations
318 private void doCancelVibrateLocked() {
319 if (mThread != null) {
320 synchronized (mThread) {
321 mThread.mDone = true;
322 mThread.notify();
323 }
324 mThread = null;
325 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700326 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400327 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800328 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400329 }
330
331 // Lock held on mVibrations
332 private void startNextVibrationLocked() {
333 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800334 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200335 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400336 return;
337 }
338 mCurrentVibration = mVibrations.getFirst();
339 startVibrationLocked(mCurrentVibration);
340 }
341
342 // Lock held on mVibrations
343 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800344 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700345 int mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
346 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mPackageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800347 if (mode != AppOpsManager.MODE_ALLOWED) {
348 if (mode == AppOpsManager.MODE_ERRORED) {
349 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
350 }
351 mH.post(mVibrationRunnable);
352 return;
353 }
354 } catch (RemoteException e) {
355 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400356 if (vib.mTimeout != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800357 doVibratorOn(vib.mTimeout, vib.mUid);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400358 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
359 } else {
360 // mThread better be null here. doCancelVibrate should always be
361 // called before startNextVibrationLocked or startVibrationLocked.
362 mThread = new VibrateThread(vib);
363 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
365 }
366
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800367 private void reportFinishVibrationLocked() {
368 if (mCurrentVibration != null) {
369 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700370 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
371 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800372 mCurrentVibration.mPackageName);
373 } catch (RemoteException e) {
374 }
375 mCurrentVibration = null;
376 }
377 }
378
Patrick Scott18dd5f02009-07-02 11:31:12 -0400379 // Lock held on mVibrations
380 private Vibration removeVibrationLocked(IBinder token) {
381 ListIterator<Vibration> iter = mVibrations.listIterator(0);
382 while (iter.hasNext()) {
383 Vibration vib = iter.next();
384 if (vib.mToken == token) {
385 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200386 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400387 return vib;
388 }
389 }
390 // We might be looking for a simple vibration which is only stored in
391 // mCurrentVibration.
392 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200393 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400394 return mCurrentVibration;
395 }
396 return null;
397 }
398
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200399 private void unlinkVibration(Vibration vib) {
400 if (vib.mPattern != null) {
401 // If Vibration object has a pattern,
402 // the Vibration object has also been linkedToDeath.
403 vib.mToken.unlinkToDeath(vib, 0);
404 }
405 }
406
Jeff Brown7f6c2312012-04-13 20:38:38 -0700407 private void updateInputDeviceVibrators() {
408 synchronized (mVibrations) {
409 doCancelVibrateLocked();
410
411 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700412 mVibrateInputDevicesSetting = false;
413 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700414 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
415 mContext.getContentResolver(),
416 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700417 } catch (SettingNotFoundException snfe) {
418 }
419
420 if (mVibrateInputDevicesSetting) {
421 if (!mInputDeviceListenerRegistered) {
422 mInputDeviceListenerRegistered = true;
423 mIm.registerInputDeviceListener(this, mH);
424 }
425 } else {
426 if (mInputDeviceListenerRegistered) {
427 mInputDeviceListenerRegistered = false;
428 mIm.unregisterInputDeviceListener(this);
429 }
430 }
431
Jeff Brown7f6c2312012-04-13 20:38:38 -0700432 mInputDeviceVibrators.clear();
433 if (mVibrateInputDevicesSetting) {
434 int[] ids = mIm.getInputDeviceIds();
435 for (int i = 0; i < ids.length; i++) {
436 InputDevice device = mIm.getInputDevice(ids[i]);
437 Vibrator vibrator = device.getVibrator();
438 if (vibrator.hasVibrator()) {
439 mInputDeviceVibrators.add(vibrator);
440 }
441 }
442 }
443 }
444
445 startNextVibrationLocked();
446 }
447 }
448
449 @Override
450 public void onInputDeviceAdded(int deviceId) {
451 updateInputDeviceVibrators();
452 }
453
454 @Override
455 public void onInputDeviceChanged(int deviceId) {
456 updateInputDeviceVibrators();
457 }
458
459 @Override
460 public void onInputDeviceRemoved(int deviceId) {
461 updateInputDeviceVibrators();
462 }
463
464 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700465 // For now, we choose to ignore the presence of input devices that have vibrators
466 // when reporting whether the device has a vibrator. Applications often use this
467 // information to decide whether to enable certain features so they expect the
468 // result of hasVibrator() to be constant. For now, just report whether
469 // the device has a built-in vibrator.
470 //synchronized (mInputDeviceVibrators) {
471 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
472 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800473 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700474 }
475
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800476 private void doVibratorOn(long millis, int uid) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700477 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800478 try {
479 mBatteryStatsService.noteVibratorOn(uid, millis);
480 mCurVibUid = uid;
481 } catch (RemoteException e) {
482 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700483 final int vibratorCount = mInputDeviceVibrators.size();
484 if (vibratorCount != 0) {
485 for (int i = 0; i < vibratorCount; i++) {
486 mInputDeviceVibrators.get(i).vibrate(millis);
487 }
488 } else {
489 vibratorOn(millis);
490 }
491 }
492 }
493
494 private void doVibratorOff() {
495 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800496 if (mCurVibUid >= 0) {
497 try {
498 mBatteryStatsService.noteVibratorOff(mCurVibUid);
499 } catch (RemoteException e) {
500 }
501 mCurVibUid = -1;
502 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700503 final int vibratorCount = mInputDeviceVibrators.size();
504 if (vibratorCount != 0) {
505 for (int i = 0; i < vibratorCount; i++) {
506 mInputDeviceVibrators.get(i).cancel();
507 }
508 } else {
509 vibratorOff();
510 }
511 }
512 }
513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400515 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700517
Patrick Scott18dd5f02009-07-02 11:31:12 -0400518 VibrateThread(Vibration vib) {
519 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700520 mTmpWorkSource.set(vib.mUid);
521 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 mWakeLock.acquire();
523 }
524
525 private void delay(long duration) {
526 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700527 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 do {
529 try {
530 this.wait(duration);
531 }
532 catch (InterruptedException e) {
533 }
534 if (mDone) {
535 break;
536 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700537 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 } while (duration > 0);
539 }
540 }
541
542 public void run() {
543 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
544 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800545 final long[] pattern = mVibration.mPattern;
546 final int len = pattern.length;
547 final int repeat = mVibration.mRepeat;
548 final int uid = mVibration.mUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 long duration = 0;
551
552 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700553 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 if (index < len) {
555 duration += pattern[index++];
556 }
557
558 // sleep until it is time to start the vibrator
559 delay(duration);
560 if (mDone) {
561 break;
562 }
563
564 if (index < len) {
565 // read on-time duration and start the vibrator
566 // duration is saved for delay() at top of loop
567 duration = pattern[index++];
568 if (duration > 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800569 VibratorService.this.doVibratorOn(duration, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400572 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 break;
574 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400575 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 duration = 0;
577 }
578 }
579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 mWakeLock.release();
581 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400582 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 if (mThread == this) {
584 mThread = null;
585 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400586 if (!mDone) {
587 // If this vibration finished naturally, start the next
588 // vibration.
589 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200590 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400591 startNextVibrationLocked();
592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
594 }
595 };
596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
598 public void onReceive(Context context, Intent intent) {
599 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400600 synchronized (mVibrations) {
601 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700602
603 int size = mVibrations.size();
604 for(int i = 0; i < size; i++) {
605 unlinkVibration(mVibrations.get(i));
606 }
607
Patrick Scott18dd5f02009-07-02 11:31:12 -0400608 mVibrations.clear();
609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
611 }
612 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613}