blob: 52f9aa9fca6477ef1959abfaf875b58c029f05ad [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;
John Spurlock1af30c72014-03-10 08:33:35 -040087 private final int mStreamHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070088 private final int mUid;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080089 private final String mPackageName;
Patrick Scott18dd5f02009-07-02 11:31:12 -040090
John Spurlock1af30c72014-03-10 08:33:35 -040091 Vibration(IBinder token, long millis, int streamHint, int uid, String packageName) {
92 this(token, millis, null, 0, streamHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -040093 }
94
John Spurlock1af30c72014-03-10 08:33:35 -040095 Vibration(IBinder token, long[] pattern, int repeat, int streamHint, int uid,
96 String packageName) {
97 this(token, 0, pattern, repeat, streamHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -040098 }
99
100 private Vibration(IBinder token, long millis, long[] pattern,
John Spurlock1af30c72014-03-10 08:33:35 -0400101 int repeat, int streamHint, int uid, String packageName) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400102 mToken = token;
103 mTimeout = millis;
104 mStartTime = SystemClock.uptimeMillis();
105 mPattern = pattern;
106 mRepeat = repeat;
John Spurlock1af30c72014-03-10 08:33:35 -0400107 mStreamHint = streamHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700108 mUid = uid;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800109 mPackageName = packageName;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400110 }
111
112 public void binderDied() {
113 synchronized (mVibrations) {
114 mVibrations.remove(this);
115 if (this == mCurrentVibration) {
116 doCancelVibrateLocked();
117 startNextVibrationLocked();
118 }
119 }
120 }
121
122 public boolean hasLongerTimeout(long millis) {
123 if (mTimeout == 0) {
124 // This is a pattern, return false to play the simple
125 // vibration.
126 return false;
127 }
128 if ((mStartTime + mTimeout)
129 < (SystemClock.uptimeMillis() + millis)) {
130 // If this vibration will end before the time passed in, let
131 // the new vibration play.
132 return false;
133 }
134 return true;
135 }
136 }
137
Mike Lockwood3a322132009-11-24 00:30:52 -0500138 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 // Reset the hardware to a default state, in case this is a runtime
140 // restart instead of a fresh boot.
141 vibratorOff();
142
143 mContext = context;
144 PowerManager pm = (PowerManager)context.getSystemService(
145 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700146 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 mWakeLock.setReferenceCounted(true);
148
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800149 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700150 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
151 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800152
Patrick Scott18dd5f02009-07-02 11:31:12 -0400153 mVibrations = new LinkedList<Vibration>();
154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 IntentFilter filter = new IntentFilter();
156 filter.addAction(Intent.ACTION_SCREEN_OFF);
157 context.registerReceiver(mIntentReceiver, filter);
158 }
159
Jeff Brown7f6c2312012-04-13 20:38:38 -0700160 public void systemReady() {
161 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Jeff Brownd4935962012-09-25 13:27:20 -0700162
Jeff Brown7f6c2312012-04-13 20:38:38 -0700163 mContext.getContentResolver().registerContentObserver(
164 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES), true,
165 new ContentObserver(mH) {
166 @Override
167 public void onChange(boolean selfChange) {
Jeff Brown82065252012-04-16 13:19:05 -0700168 updateInputDeviceVibrators();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700169 }
Jeff Brownd4935962012-09-25 13:27:20 -0700170 }, UserHandle.USER_ALL);
171
172 mContext.registerReceiver(new BroadcastReceiver() {
173 @Override
174 public void onReceive(Context context, Intent intent) {
175 updateInputDeviceVibrators();
176 }
177 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
178
Jeff Brown82065252012-04-16 13:19:05 -0700179 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700180 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700181
182 public boolean hasVibrator() {
183 return doVibratorExists();
184 }
185
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800186 private void verifyIncomingUid(int uid) {
187 if (uid == Binder.getCallingUid()) {
188 return;
189 }
190 if (Binder.getCallingPid() == Process.myPid()) {
191 return;
192 }
193 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
194 Binder.getCallingPid(), Binder.getCallingUid(), null);
195 }
196
John Spurlock1af30c72014-03-10 08:33:35 -0400197 public void vibrate(int uid, String packageName, long milliseconds, int streamHint,
198 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700199 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
200 != PackageManager.PERMISSION_GRANTED) {
201 throw new SecurityException("Requires VIBRATE permission");
202 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800203 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400204 // We're running in the system server so we cannot crash. Check for a
205 // timeout of 0 or negative. This will ensure that a vibration has
206 // either a timeout of > 0 or a non-null pattern.
207 if (milliseconds <= 0 || (mCurrentVibration != null
208 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400209 // Ignore this vibration since the current vibration will play for
210 // longer than milliseconds.
211 return;
212 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700213
John Spurlock1af30c72014-03-10 08:33:35 -0400214 Vibration vib = new Vibration(token, milliseconds, streamHint, uid, packageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800215
216 final long ident = Binder.clearCallingIdentity();
217 try {
218 synchronized (mVibrations) {
219 removeVibrationLocked(token);
220 doCancelVibrateLocked();
221 mCurrentVibration = vib;
222 startVibrationLocked(vib);
223 }
224 } finally {
225 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 }
228
229 private boolean isAll0(long[] pattern) {
230 int N = pattern.length;
231 for (int i = 0; i < N; i++) {
232 if (pattern[i] != 0) {
233 return false;
234 }
235 }
236 return true;
237 }
238
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800239 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock1af30c72014-03-10 08:33:35 -0400240 int streamHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
242 != PackageManager.PERMISSION_GRANTED) {
243 throw new SecurityException("Requires VIBRATE permission");
244 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800245 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 // so wakelock calls will succeed
247 long identity = Binder.clearCallingIdentity();
248 try {
249 if (false) {
250 String s = "";
251 int N = pattern.length;
252 for (int i=0; i<N; i++) {
253 s += " " + pattern[i];
254 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800255 Slog.i(TAG, "vibrating with pattern: " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
257
258 // we're running in the server so we can't fail
259 if (pattern == null || pattern.length == 0
260 || isAll0(pattern)
261 || repeat >= pattern.length || token == null) {
262 return;
263 }
264
John Spurlock1af30c72014-03-10 08:33:35 -0400265 Vibration vib = new Vibration(token, pattern, repeat, streamHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400266 try {
267 token.linkToDeath(vib, 0);
268 } catch (RemoteException e) {
269 return;
270 }
271
272 synchronized (mVibrations) {
273 removeVibrationLocked(token);
274 doCancelVibrateLocked();
275 if (repeat >= 0) {
276 mVibrations.addFirst(vib);
277 startNextVibrationLocked();
278 } else {
279 // A negative repeat means that this pattern is not meant
280 // to repeat. Treat it like a simple vibration.
281 mCurrentVibration = vib;
282 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
285 }
286 finally {
287 Binder.restoreCallingIdentity(identity);
288 }
289 }
290
Patrick Scott18dd5f02009-07-02 11:31:12 -0400291 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 mContext.enforceCallingOrSelfPermission(
293 android.Manifest.permission.VIBRATE,
294 "cancelVibrate");
295
296 // so wakelock calls will succeed
297 long identity = Binder.clearCallingIdentity();
298 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400299 synchronized (mVibrations) {
300 final Vibration vib = removeVibrationLocked(token);
301 if (vib == mCurrentVibration) {
302 doCancelVibrateLocked();
303 startNextVibrationLocked();
304 }
305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
307 finally {
308 Binder.restoreCallingIdentity(identity);
309 }
310 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700311
Patrick Scott18dd5f02009-07-02 11:31:12 -0400312 private final Runnable mVibrationRunnable = new Runnable() {
313 public void run() {
314 synchronized (mVibrations) {
315 doCancelVibrateLocked();
316 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400318 }
319 };
320
321 // Lock held on mVibrations
322 private void doCancelVibrateLocked() {
323 if (mThread != null) {
324 synchronized (mThread) {
325 mThread.mDone = true;
326 mThread.notify();
327 }
328 mThread = null;
329 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700330 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400331 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800332 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400333 }
334
335 // Lock held on mVibrations
336 private void startNextVibrationLocked() {
337 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800338 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200339 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400340 return;
341 }
342 mCurrentVibration = mVibrations.getFirst();
343 startVibrationLocked(mCurrentVibration);
344 }
345
346 // Lock held on mVibrations
347 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800348 try {
John Spurlock1af30c72014-03-10 08:33:35 -0400349 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
350 vib.mStreamHint, vib.mUid, vib.mPackageName);
351 if (mode == AppOpsManager.MODE_ALLOWED) {
352 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700353 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mPackageName);
John Spurlock1af30c72014-03-10 08:33:35 -0400354 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800355 if (mode != AppOpsManager.MODE_ALLOWED) {
356 if (mode == AppOpsManager.MODE_ERRORED) {
357 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
358 }
359 mH.post(mVibrationRunnable);
360 return;
361 }
362 } catch (RemoteException e) {
363 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400364 if (vib.mTimeout != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800365 doVibratorOn(vib.mTimeout, vib.mUid);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400366 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
367 } else {
368 // mThread better be null here. doCancelVibrate should always be
369 // called before startNextVibrationLocked or startVibrationLocked.
370 mThread = new VibrateThread(vib);
371 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
373 }
374
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800375 private void reportFinishVibrationLocked() {
376 if (mCurrentVibration != null) {
377 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700378 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
379 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800380 mCurrentVibration.mPackageName);
381 } catch (RemoteException e) {
382 }
383 mCurrentVibration = null;
384 }
385 }
386
Patrick Scott18dd5f02009-07-02 11:31:12 -0400387 // Lock held on mVibrations
388 private Vibration removeVibrationLocked(IBinder token) {
389 ListIterator<Vibration> iter = mVibrations.listIterator(0);
390 while (iter.hasNext()) {
391 Vibration vib = iter.next();
392 if (vib.mToken == token) {
393 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200394 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400395 return vib;
396 }
397 }
398 // We might be looking for a simple vibration which is only stored in
399 // mCurrentVibration.
400 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200401 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400402 return mCurrentVibration;
403 }
404 return null;
405 }
406
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200407 private void unlinkVibration(Vibration vib) {
408 if (vib.mPattern != null) {
409 // If Vibration object has a pattern,
410 // the Vibration object has also been linkedToDeath.
411 vib.mToken.unlinkToDeath(vib, 0);
412 }
413 }
414
Jeff Brown7f6c2312012-04-13 20:38:38 -0700415 private void updateInputDeviceVibrators() {
416 synchronized (mVibrations) {
417 doCancelVibrateLocked();
418
419 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700420 mVibrateInputDevicesSetting = false;
421 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700422 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
423 mContext.getContentResolver(),
424 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700425 } catch (SettingNotFoundException snfe) {
426 }
427
428 if (mVibrateInputDevicesSetting) {
429 if (!mInputDeviceListenerRegistered) {
430 mInputDeviceListenerRegistered = true;
431 mIm.registerInputDeviceListener(this, mH);
432 }
433 } else {
434 if (mInputDeviceListenerRegistered) {
435 mInputDeviceListenerRegistered = false;
436 mIm.unregisterInputDeviceListener(this);
437 }
438 }
439
Jeff Brown7f6c2312012-04-13 20:38:38 -0700440 mInputDeviceVibrators.clear();
441 if (mVibrateInputDevicesSetting) {
442 int[] ids = mIm.getInputDeviceIds();
443 for (int i = 0; i < ids.length; i++) {
444 InputDevice device = mIm.getInputDevice(ids[i]);
445 Vibrator vibrator = device.getVibrator();
446 if (vibrator.hasVibrator()) {
447 mInputDeviceVibrators.add(vibrator);
448 }
449 }
450 }
451 }
452
453 startNextVibrationLocked();
454 }
455 }
456
457 @Override
458 public void onInputDeviceAdded(int deviceId) {
459 updateInputDeviceVibrators();
460 }
461
462 @Override
463 public void onInputDeviceChanged(int deviceId) {
464 updateInputDeviceVibrators();
465 }
466
467 @Override
468 public void onInputDeviceRemoved(int deviceId) {
469 updateInputDeviceVibrators();
470 }
471
472 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700473 // For now, we choose to ignore the presence of input devices that have vibrators
474 // when reporting whether the device has a vibrator. Applications often use this
475 // information to decide whether to enable certain features so they expect the
476 // result of hasVibrator() to be constant. For now, just report whether
477 // the device has a built-in vibrator.
478 //synchronized (mInputDeviceVibrators) {
479 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
480 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800481 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700482 }
483
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800484 private void doVibratorOn(long millis, int uid) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700485 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800486 try {
487 mBatteryStatsService.noteVibratorOn(uid, millis);
488 mCurVibUid = uid;
489 } catch (RemoteException e) {
490 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700491 final int vibratorCount = mInputDeviceVibrators.size();
492 if (vibratorCount != 0) {
493 for (int i = 0; i < vibratorCount; i++) {
494 mInputDeviceVibrators.get(i).vibrate(millis);
495 }
496 } else {
497 vibratorOn(millis);
498 }
499 }
500 }
501
502 private void doVibratorOff() {
503 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800504 if (mCurVibUid >= 0) {
505 try {
506 mBatteryStatsService.noteVibratorOff(mCurVibUid);
507 } catch (RemoteException e) {
508 }
509 mCurVibUid = -1;
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++) {
514 mInputDeviceVibrators.get(i).cancel();
515 }
516 } else {
517 vibratorOff();
518 }
519 }
520 }
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400523 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700525
Patrick Scott18dd5f02009-07-02 11:31:12 -0400526 VibrateThread(Vibration vib) {
527 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700528 mTmpWorkSource.set(vib.mUid);
529 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 mWakeLock.acquire();
531 }
532
533 private void delay(long duration) {
534 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700535 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 do {
537 try {
538 this.wait(duration);
539 }
540 catch (InterruptedException e) {
541 }
542 if (mDone) {
543 break;
544 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700545 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 } while (duration > 0);
547 }
548 }
549
550 public void run() {
551 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
552 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800553 final long[] pattern = mVibration.mPattern;
554 final int len = pattern.length;
555 final int repeat = mVibration.mRepeat;
556 final int uid = mVibration.mUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 long duration = 0;
559
560 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700561 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 if (index < len) {
563 duration += pattern[index++];
564 }
565
566 // sleep until it is time to start the vibrator
567 delay(duration);
568 if (mDone) {
569 break;
570 }
571
572 if (index < len) {
573 // read on-time duration and start the vibrator
574 // duration is saved for delay() at top of loop
575 duration = pattern[index++];
576 if (duration > 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800577 VibratorService.this.doVibratorOn(duration, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400580 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 break;
582 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400583 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 duration = 0;
585 }
586 }
587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 mWakeLock.release();
589 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400590 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 if (mThread == this) {
592 mThread = null;
593 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400594 if (!mDone) {
595 // If this vibration finished naturally, start the next
596 // vibration.
597 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200598 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400599 startNextVibrationLocked();
600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
602 }
603 };
604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
606 public void onReceive(Context context, Intent intent) {
607 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400608 synchronized (mVibrations) {
609 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700610
611 int size = mVibrations.size();
612 for(int i = 0; i < size; i++) {
613 unlinkVibration(mVibrations.get(i));
614 }
615
Patrick Scott18dd5f02009-07-02 11:31:12 -0400616 mVibrations.clear();
617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
619 }
620 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621}