blob: 69379f17c3fc38212e5bbd00ab049aa1662c3450 [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;
Joe Onorato95e4f702009-03-24 19:29:09 -070027import android.os.Handler;
Mike Lockwood3a322132009-11-24 00:30:52 -050028import android.os.IVibratorService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.os.PowerManager;
30import android.os.Process;
31import android.os.RemoteException;
32import android.os.IBinder;
33import android.os.Binder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080034import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.SystemClock;
Jeff Brownd4935962012-09-25 13:27:20 -070036import android.os.UserHandle;
Jeff Brown7f6c2312012-04-13 20:38:38 -070037import android.os.Vibrator;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070038import android.os.WorkSource;
Jeff Brown7f6c2312012-04-13 20:38:38 -070039import android.provider.Settings;
40import android.provider.Settings.SettingNotFoundException;
Joe Onorato8a9b2202010-02-26 18:56:32 -080041import android.util.Slog;
Jeff Brown7f6c2312012-04-13 20:38:38 -070042import android.view.InputDevice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Dianne Hackborna06de0f2012-12-11 16:34:47 -080044import com.android.internal.app.IAppOpsService;
45import com.android.internal.app.IBatteryStats;
46
Jeff Brown7f6c2312012-04-13 20:38:38 -070047import java.util.ArrayList;
Patrick Scott18dd5f02009-07-02 11:31:12 -040048import java.util.LinkedList;
49import java.util.ListIterator;
50
Jeff Brown7f6c2312012-04-13 20:38:38 -070051public class VibratorService extends IVibratorService.Stub
52 implements InputManager.InputDeviceListener {
Mike Lockwood3a322132009-11-24 00:30:52 -050053 private static final String TAG = "VibratorService";
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050054
Patrick Scott18dd5f02009-07-02 11:31:12 -040055 private final LinkedList<Vibration> mVibrations;
56 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070057 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070058 private final Handler mH = new Handler();
59
60 private final Context mContext;
61 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080062 private final IAppOpsService mAppOpsService;
63 private final IBatteryStats mBatteryStatsService;
Jeff Brown7f6c2312012-04-13 20:38:38 -070064 private InputManager mIm;
65
66 volatile VibrateThread mThread;
67
68 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
69 // to be acquired
70 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
71 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
72 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
73
Dianne Hackborna06de0f2012-12-11 16:34:47 -080074 private int mCurVibUid = -1;
75
Jeff Brown7f6c2312012-04-13 20:38:38 -070076 native static boolean vibratorExists();
77 native static void vibratorOn(long milliseconds);
78 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040079
Patrick Scott18dd5f02009-07-02 11:31:12 -040080 private class Vibration implements IBinder.DeathRecipient {
81 private final IBinder mToken;
82 private final long mTimeout;
83 private final long mStartTime;
84 private final long[] mPattern;
85 private final int mRepeat;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070086 private final int mUid;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080087 private final String mPackageName;
Patrick Scott18dd5f02009-07-02 11:31:12 -040088
Dianne Hackborna06de0f2012-12-11 16:34:47 -080089 Vibration(IBinder token, long millis, int uid, String packageName) {
90 this(token, millis, null, 0, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -040091 }
92
Dianne Hackborna06de0f2012-12-11 16:34:47 -080093 Vibration(IBinder token, long[] pattern, int repeat, int uid, String packageName) {
94 this(token, 0, pattern, repeat, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -040095 }
96
97 private Vibration(IBinder token, long millis, long[] pattern,
Dianne Hackborna06de0f2012-12-11 16:34:47 -080098 int repeat, int uid, String packageName) {
Patrick Scott18dd5f02009-07-02 11:31:12 -040099 mToken = token;
100 mTimeout = millis;
101 mStartTime = SystemClock.uptimeMillis();
102 mPattern = pattern;
103 mRepeat = repeat;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700104 mUid = uid;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800105 mPackageName = packageName;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400106 }
107
108 public void binderDied() {
109 synchronized (mVibrations) {
110 mVibrations.remove(this);
111 if (this == mCurrentVibration) {
112 doCancelVibrateLocked();
113 startNextVibrationLocked();
114 }
115 }
116 }
117
118 public boolean hasLongerTimeout(long millis) {
119 if (mTimeout == 0) {
120 // This is a pattern, return false to play the simple
121 // vibration.
122 return false;
123 }
124 if ((mStartTime + mTimeout)
125 < (SystemClock.uptimeMillis() + millis)) {
126 // If this vibration will end before the time passed in, let
127 // the new vibration play.
128 return false;
129 }
130 return true;
131 }
132 }
133
Mike Lockwood3a322132009-11-24 00:30:52 -0500134 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 // Reset the hardware to a default state, in case this is a runtime
136 // restart instead of a fresh boot.
137 vibratorOff();
138
139 mContext = context;
140 PowerManager pm = (PowerManager)context.getSystemService(
141 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700142 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 mWakeLock.setReferenceCounted(true);
144
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800145 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
146 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo"));
147
Patrick Scott18dd5f02009-07-02 11:31:12 -0400148 mVibrations = new LinkedList<Vibration>();
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 IntentFilter filter = new IntentFilter();
151 filter.addAction(Intent.ACTION_SCREEN_OFF);
152 context.registerReceiver(mIntentReceiver, filter);
153 }
154
Jeff Brown7f6c2312012-04-13 20:38:38 -0700155 public void systemReady() {
156 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Jeff Brownd4935962012-09-25 13:27:20 -0700157
Jeff Brown7f6c2312012-04-13 20:38:38 -0700158 mContext.getContentResolver().registerContentObserver(
159 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES), true,
160 new ContentObserver(mH) {
161 @Override
162 public void onChange(boolean selfChange) {
Jeff Brown82065252012-04-16 13:19:05 -0700163 updateInputDeviceVibrators();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700164 }
Jeff Brownd4935962012-09-25 13:27:20 -0700165 }, UserHandle.USER_ALL);
166
167 mContext.registerReceiver(new BroadcastReceiver() {
168 @Override
169 public void onReceive(Context context, Intent intent) {
170 updateInputDeviceVibrators();
171 }
172 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
173
Jeff Brown82065252012-04-16 13:19:05 -0700174 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700175 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700176
177 public boolean hasVibrator() {
178 return doVibratorExists();
179 }
180
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800181 public void vibrate(String packageName, long milliseconds, IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700182 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
183 != PackageManager.PERMISSION_GRANTED) {
184 throw new SecurityException("Requires VIBRATE permission");
185 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700186 int uid = Binder.getCallingUid();
Patrick Scott24f10762009-08-19 09:03:56 -0400187 // We're running in the system server so we cannot crash. Check for a
188 // timeout of 0 or negative. This will ensure that a vibration has
189 // either a timeout of > 0 or a non-null pattern.
190 if (milliseconds <= 0 || (mCurrentVibration != null
191 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400192 // Ignore this vibration since the current vibration will play for
193 // longer than milliseconds.
194 return;
195 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700196
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800197 Vibration vib = new Vibration(token, milliseconds, uid, packageName);
198
199 final long ident = Binder.clearCallingIdentity();
200 try {
201 synchronized (mVibrations) {
202 removeVibrationLocked(token);
203 doCancelVibrateLocked();
204 mCurrentVibration = vib;
205 startVibrationLocked(vib);
206 }
207 } finally {
208 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211
212 private boolean isAll0(long[] pattern) {
213 int N = pattern.length;
214 for (int i = 0; i < N; i++) {
215 if (pattern[i] != 0) {
216 return false;
217 }
218 }
219 return true;
220 }
221
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800222 public void vibratePattern(String packageName, long[] pattern, int repeat, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
224 != PackageManager.PERMISSION_GRANTED) {
225 throw new SecurityException("Requires VIBRATE permission");
226 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700227 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 // so wakelock calls will succeed
229 long identity = Binder.clearCallingIdentity();
230 try {
231 if (false) {
232 String s = "";
233 int N = pattern.length;
234 for (int i=0; i<N; i++) {
235 s += " " + pattern[i];
236 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800237 Slog.i(TAG, "vibrating with pattern: " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
239
240 // we're running in the server so we can't fail
241 if (pattern == null || pattern.length == 0
242 || isAll0(pattern)
243 || repeat >= pattern.length || token == null) {
244 return;
245 }
246
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800247 Vibration vib = new Vibration(token, pattern, repeat, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400248 try {
249 token.linkToDeath(vib, 0);
250 } catch (RemoteException e) {
251 return;
252 }
253
254 synchronized (mVibrations) {
255 removeVibrationLocked(token);
256 doCancelVibrateLocked();
257 if (repeat >= 0) {
258 mVibrations.addFirst(vib);
259 startNextVibrationLocked();
260 } else {
261 // A negative repeat means that this pattern is not meant
262 // to repeat. Treat it like a simple vibration.
263 mCurrentVibration = vib;
264 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 }
267 }
268 finally {
269 Binder.restoreCallingIdentity(identity);
270 }
271 }
272
Patrick Scott18dd5f02009-07-02 11:31:12 -0400273 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 mContext.enforceCallingOrSelfPermission(
275 android.Manifest.permission.VIBRATE,
276 "cancelVibrate");
277
278 // so wakelock calls will succeed
279 long identity = Binder.clearCallingIdentity();
280 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400281 synchronized (mVibrations) {
282 final Vibration vib = removeVibrationLocked(token);
283 if (vib == mCurrentVibration) {
284 doCancelVibrateLocked();
285 startNextVibrationLocked();
286 }
287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 }
289 finally {
290 Binder.restoreCallingIdentity(identity);
291 }
292 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700293
Patrick Scott18dd5f02009-07-02 11:31:12 -0400294 private final Runnable mVibrationRunnable = new Runnable() {
295 public void run() {
296 synchronized (mVibrations) {
297 doCancelVibrateLocked();
298 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400300 }
301 };
302
303 // Lock held on mVibrations
304 private void doCancelVibrateLocked() {
305 if (mThread != null) {
306 synchronized (mThread) {
307 mThread.mDone = true;
308 mThread.notify();
309 }
310 mThread = null;
311 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700312 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400313 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800314 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400315 }
316
317 // Lock held on mVibrations
318 private void startNextVibrationLocked() {
319 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800320 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200321 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400322 return;
323 }
324 mCurrentVibration = mVibrations.getFirst();
325 startVibrationLocked(mCurrentVibration);
326 }
327
328 // Lock held on mVibrations
329 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800330 try {
331 int mode = mAppOpsService.startOperation(AppOpsManager.OP_VIBRATE, vib.mUid, vib.mPackageName);
332 if (mode != AppOpsManager.MODE_ALLOWED) {
333 if (mode == AppOpsManager.MODE_ERRORED) {
334 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
335 }
336 mH.post(mVibrationRunnable);
337 return;
338 }
339 } catch (RemoteException e) {
340 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400341 if (vib.mTimeout != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800342 doVibratorOn(vib.mTimeout, vib.mUid);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400343 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
344 } else {
345 // mThread better be null here. doCancelVibrate should always be
346 // called before startNextVibrationLocked or startVibrationLocked.
347 mThread = new VibrateThread(vib);
348 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
350 }
351
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800352 private void reportFinishVibrationLocked() {
353 if (mCurrentVibration != null) {
354 try {
355 mAppOpsService.finishOperation(AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
356 mCurrentVibration.mPackageName);
357 } catch (RemoteException e) {
358 }
359 mCurrentVibration = null;
360 }
361 }
362
Patrick Scott18dd5f02009-07-02 11:31:12 -0400363 // Lock held on mVibrations
364 private Vibration removeVibrationLocked(IBinder token) {
365 ListIterator<Vibration> iter = mVibrations.listIterator(0);
366 while (iter.hasNext()) {
367 Vibration vib = iter.next();
368 if (vib.mToken == token) {
369 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200370 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400371 return vib;
372 }
373 }
374 // We might be looking for a simple vibration which is only stored in
375 // mCurrentVibration.
376 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200377 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400378 return mCurrentVibration;
379 }
380 return null;
381 }
382
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200383 private void unlinkVibration(Vibration vib) {
384 if (vib.mPattern != null) {
385 // If Vibration object has a pattern,
386 // the Vibration object has also been linkedToDeath.
387 vib.mToken.unlinkToDeath(vib, 0);
388 }
389 }
390
Jeff Brown7f6c2312012-04-13 20:38:38 -0700391 private void updateInputDeviceVibrators() {
392 synchronized (mVibrations) {
393 doCancelVibrateLocked();
394
395 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700396 mVibrateInputDevicesSetting = false;
397 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700398 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
399 mContext.getContentResolver(),
400 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700401 } catch (SettingNotFoundException snfe) {
402 }
403
404 if (mVibrateInputDevicesSetting) {
405 if (!mInputDeviceListenerRegistered) {
406 mInputDeviceListenerRegistered = true;
407 mIm.registerInputDeviceListener(this, mH);
408 }
409 } else {
410 if (mInputDeviceListenerRegistered) {
411 mInputDeviceListenerRegistered = false;
412 mIm.unregisterInputDeviceListener(this);
413 }
414 }
415
Jeff Brown7f6c2312012-04-13 20:38:38 -0700416 mInputDeviceVibrators.clear();
417 if (mVibrateInputDevicesSetting) {
418 int[] ids = mIm.getInputDeviceIds();
419 for (int i = 0; i < ids.length; i++) {
420 InputDevice device = mIm.getInputDevice(ids[i]);
421 Vibrator vibrator = device.getVibrator();
422 if (vibrator.hasVibrator()) {
423 mInputDeviceVibrators.add(vibrator);
424 }
425 }
426 }
427 }
428
429 startNextVibrationLocked();
430 }
431 }
432
433 @Override
434 public void onInputDeviceAdded(int deviceId) {
435 updateInputDeviceVibrators();
436 }
437
438 @Override
439 public void onInputDeviceChanged(int deviceId) {
440 updateInputDeviceVibrators();
441 }
442
443 @Override
444 public void onInputDeviceRemoved(int deviceId) {
445 updateInputDeviceVibrators();
446 }
447
448 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700449 // For now, we choose to ignore the presence of input devices that have vibrators
450 // when reporting whether the device has a vibrator. Applications often use this
451 // information to decide whether to enable certain features so they expect the
452 // result of hasVibrator() to be constant. For now, just report whether
453 // the device has a built-in vibrator.
454 //synchronized (mInputDeviceVibrators) {
455 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
456 //}
457 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700458 }
459
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800460 private void doVibratorOn(long millis, int uid) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700461 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800462 try {
463 mBatteryStatsService.noteVibratorOn(uid, millis);
464 mCurVibUid = uid;
465 } catch (RemoteException e) {
466 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700467 final int vibratorCount = mInputDeviceVibrators.size();
468 if (vibratorCount != 0) {
469 for (int i = 0; i < vibratorCount; i++) {
470 mInputDeviceVibrators.get(i).vibrate(millis);
471 }
472 } else {
473 vibratorOn(millis);
474 }
475 }
476 }
477
478 private void doVibratorOff() {
479 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800480 if (mCurVibUid >= 0) {
481 try {
482 mBatteryStatsService.noteVibratorOff(mCurVibUid);
483 } catch (RemoteException e) {
484 }
485 mCurVibUid = -1;
486 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700487 final int vibratorCount = mInputDeviceVibrators.size();
488 if (vibratorCount != 0) {
489 for (int i = 0; i < vibratorCount; i++) {
490 mInputDeviceVibrators.get(i).cancel();
491 }
492 } else {
493 vibratorOff();
494 }
495 }
496 }
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400499 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700501
Patrick Scott18dd5f02009-07-02 11:31:12 -0400502 VibrateThread(Vibration vib) {
503 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700504 mTmpWorkSource.set(vib.mUid);
505 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 mWakeLock.acquire();
507 }
508
509 private void delay(long duration) {
510 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700511 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 do {
513 try {
514 this.wait(duration);
515 }
516 catch (InterruptedException e) {
517 }
518 if (mDone) {
519 break;
520 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700521 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 } while (duration > 0);
523 }
524 }
525
526 public void run() {
527 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
528 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800529 final long[] pattern = mVibration.mPattern;
530 final int len = pattern.length;
531 final int repeat = mVibration.mRepeat;
532 final int uid = mVibration.mUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 long duration = 0;
535
536 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700537 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 if (index < len) {
539 duration += pattern[index++];
540 }
541
542 // sleep until it is time to start the vibrator
543 delay(duration);
544 if (mDone) {
545 break;
546 }
547
548 if (index < len) {
549 // read on-time duration and start the vibrator
550 // duration is saved for delay() at top of loop
551 duration = pattern[index++];
552 if (duration > 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800553 VibratorService.this.doVibratorOn(duration, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 }
555 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400556 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 break;
558 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400559 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 duration = 0;
561 }
562 }
563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 mWakeLock.release();
565 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400566 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 if (mThread == this) {
568 mThread = null;
569 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400570 if (!mDone) {
571 // If this vibration finished naturally, start the next
572 // vibration.
573 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200574 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400575 startNextVibrationLocked();
576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
578 }
579 };
580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
582 public void onReceive(Context context, Intent intent) {
583 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400584 synchronized (mVibrations) {
585 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700586
587 int size = mVibrations.size();
588 for(int i = 0; i < size; i++) {
589 unlinkVibration(mVibrations.get(i));
590 }
591
Patrick Scott18dd5f02009-07-02 11:31:12 -0400592 mVibrations.clear();
593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
595 }
596 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597}