blob: 9b5f8f65cb489c45ff36691798374c87ae208eec [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 {
345 int mode = mAppOpsService.startOperation(AppOpsManager.OP_VIBRATE, vib.mUid, vib.mPackageName);
346 if (mode != AppOpsManager.MODE_ALLOWED) {
347 if (mode == AppOpsManager.MODE_ERRORED) {
348 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
349 }
350 mH.post(mVibrationRunnable);
351 return;
352 }
353 } catch (RemoteException e) {
354 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400355 if (vib.mTimeout != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800356 doVibratorOn(vib.mTimeout, vib.mUid);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400357 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
358 } else {
359 // mThread better be null here. doCancelVibrate should always be
360 // called before startNextVibrationLocked or startVibrationLocked.
361 mThread = new VibrateThread(vib);
362 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
364 }
365
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800366 private void reportFinishVibrationLocked() {
367 if (mCurrentVibration != null) {
368 try {
369 mAppOpsService.finishOperation(AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
370 mCurrentVibration.mPackageName);
371 } catch (RemoteException e) {
372 }
373 mCurrentVibration = null;
374 }
375 }
376
Patrick Scott18dd5f02009-07-02 11:31:12 -0400377 // Lock held on mVibrations
378 private Vibration removeVibrationLocked(IBinder token) {
379 ListIterator<Vibration> iter = mVibrations.listIterator(0);
380 while (iter.hasNext()) {
381 Vibration vib = iter.next();
382 if (vib.mToken == token) {
383 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200384 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400385 return vib;
386 }
387 }
388 // We might be looking for a simple vibration which is only stored in
389 // mCurrentVibration.
390 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200391 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400392 return mCurrentVibration;
393 }
394 return null;
395 }
396
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200397 private void unlinkVibration(Vibration vib) {
398 if (vib.mPattern != null) {
399 // If Vibration object has a pattern,
400 // the Vibration object has also been linkedToDeath.
401 vib.mToken.unlinkToDeath(vib, 0);
402 }
403 }
404
Jeff Brown7f6c2312012-04-13 20:38:38 -0700405 private void updateInputDeviceVibrators() {
406 synchronized (mVibrations) {
407 doCancelVibrateLocked();
408
409 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700410 mVibrateInputDevicesSetting = false;
411 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700412 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
413 mContext.getContentResolver(),
414 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700415 } catch (SettingNotFoundException snfe) {
416 }
417
418 if (mVibrateInputDevicesSetting) {
419 if (!mInputDeviceListenerRegistered) {
420 mInputDeviceListenerRegistered = true;
421 mIm.registerInputDeviceListener(this, mH);
422 }
423 } else {
424 if (mInputDeviceListenerRegistered) {
425 mInputDeviceListenerRegistered = false;
426 mIm.unregisterInputDeviceListener(this);
427 }
428 }
429
Jeff Brown7f6c2312012-04-13 20:38:38 -0700430 mInputDeviceVibrators.clear();
431 if (mVibrateInputDevicesSetting) {
432 int[] ids = mIm.getInputDeviceIds();
433 for (int i = 0; i < ids.length; i++) {
434 InputDevice device = mIm.getInputDevice(ids[i]);
435 Vibrator vibrator = device.getVibrator();
436 if (vibrator.hasVibrator()) {
437 mInputDeviceVibrators.add(vibrator);
438 }
439 }
440 }
441 }
442
443 startNextVibrationLocked();
444 }
445 }
446
447 @Override
448 public void onInputDeviceAdded(int deviceId) {
449 updateInputDeviceVibrators();
450 }
451
452 @Override
453 public void onInputDeviceChanged(int deviceId) {
454 updateInputDeviceVibrators();
455 }
456
457 @Override
458 public void onInputDeviceRemoved(int deviceId) {
459 updateInputDeviceVibrators();
460 }
461
462 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700463 // For now, we choose to ignore the presence of input devices that have vibrators
464 // when reporting whether the device has a vibrator. Applications often use this
465 // information to decide whether to enable certain features so they expect the
466 // result of hasVibrator() to be constant. For now, just report whether
467 // the device has a built-in vibrator.
468 //synchronized (mInputDeviceVibrators) {
469 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
470 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800471 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700472 }
473
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800474 private void doVibratorOn(long millis, int uid) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700475 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800476 try {
477 mBatteryStatsService.noteVibratorOn(uid, millis);
478 mCurVibUid = uid;
479 } catch (RemoteException e) {
480 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700481 final int vibratorCount = mInputDeviceVibrators.size();
482 if (vibratorCount != 0) {
483 for (int i = 0; i < vibratorCount; i++) {
484 mInputDeviceVibrators.get(i).vibrate(millis);
485 }
486 } else {
487 vibratorOn(millis);
488 }
489 }
490 }
491
492 private void doVibratorOff() {
493 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800494 if (mCurVibUid >= 0) {
495 try {
496 mBatteryStatsService.noteVibratorOff(mCurVibUid);
497 } catch (RemoteException e) {
498 }
499 mCurVibUid = -1;
500 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700501 final int vibratorCount = mInputDeviceVibrators.size();
502 if (vibratorCount != 0) {
503 for (int i = 0; i < vibratorCount; i++) {
504 mInputDeviceVibrators.get(i).cancel();
505 }
506 } else {
507 vibratorOff();
508 }
509 }
510 }
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400513 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700515
Patrick Scott18dd5f02009-07-02 11:31:12 -0400516 VibrateThread(Vibration vib) {
517 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700518 mTmpWorkSource.set(vib.mUid);
519 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 mWakeLock.acquire();
521 }
522
523 private void delay(long duration) {
524 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700525 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 do {
527 try {
528 this.wait(duration);
529 }
530 catch (InterruptedException e) {
531 }
532 if (mDone) {
533 break;
534 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700535 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 } while (duration > 0);
537 }
538 }
539
540 public void run() {
541 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
542 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800543 final long[] pattern = mVibration.mPattern;
544 final int len = pattern.length;
545 final int repeat = mVibration.mRepeat;
546 final int uid = mVibration.mUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 long duration = 0;
549
550 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700551 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 if (index < len) {
553 duration += pattern[index++];
554 }
555
556 // sleep until it is time to start the vibrator
557 delay(duration);
558 if (mDone) {
559 break;
560 }
561
562 if (index < len) {
563 // read on-time duration and start the vibrator
564 // duration is saved for delay() at top of loop
565 duration = pattern[index++];
566 if (duration > 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800567 VibratorService.this.doVibratorOn(duration, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400570 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 break;
572 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400573 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 duration = 0;
575 }
576 }
577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 mWakeLock.release();
579 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400580 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 if (mThread == this) {
582 mThread = null;
583 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400584 if (!mDone) {
585 // If this vibration finished naturally, start the next
586 // vibration.
587 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200588 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400589 startNextVibrationLocked();
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
592 }
593 };
594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
596 public void onReceive(Context context, Intent intent) {
597 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400598 synchronized (mVibrations) {
599 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700600
601 int size = mVibrations.size();
602 for(int i = 0; i < size; i++) {
603 unlinkVibration(mVibrations.get(i));
604 }
605
Patrick Scott18dd5f02009-07-02 11:31:12 -0400606 mVibrations.clear();
607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 }
609 }
610 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611}