blob: 82c13e0d4394700bfa43df196d86be243555657e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Dianne Hackborna06de0f2012-12-11 16:34:47 -080019import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.content.pm.PackageManager;
Jeff Brown7f6c2312012-04-13 20:38:38 -070025import android.database.ContentObserver;
26import android.hardware.input.InputManager;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070027import android.os.BatteryStats;
Joe Onorato95e4f702009-03-24 19:29:09 -070028import android.os.Handler;
Mike Lockwood3a322132009-11-24 00:30:52 -050029import android.os.IVibratorService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.PowerManager;
31import android.os.Process;
32import android.os.RemoteException;
33import android.os.IBinder;
34import android.os.Binder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080035import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.SystemClock;
Jeff Brownd4935962012-09-25 13:27:20 -070037import android.os.UserHandle;
Jeff Brown7f6c2312012-04-13 20:38:38 -070038import android.os.Vibrator;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070039import android.os.WorkSource;
Jeff Brown7f6c2312012-04-13 20:38:38 -070040import android.provider.Settings;
41import android.provider.Settings.SettingNotFoundException;
Joe Onorato8a9b2202010-02-26 18:56:32 -080042import android.util.Slog;
Jeff Brown7f6c2312012-04-13 20:38:38 -070043import android.view.InputDevice;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070044import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
Dianne Hackborna06de0f2012-12-11 16:34:47 -080046import com.android.internal.app.IAppOpsService;
47import com.android.internal.app.IBatteryStats;
48
Jeff Brown7f6c2312012-04-13 20:38:38 -070049import java.util.ArrayList;
Patrick Scott18dd5f02009-07-02 11:31:12 -040050import java.util.LinkedList;
51import java.util.ListIterator;
52
Jeff Brown7f6c2312012-04-13 20:38:38 -070053public class VibratorService extends IVibratorService.Stub
54 implements InputManager.InputDeviceListener {
Mike Lockwood3a322132009-11-24 00:30:52 -050055 private static final String TAG = "VibratorService";
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050056
Patrick Scott18dd5f02009-07-02 11:31:12 -040057 private final LinkedList<Vibration> mVibrations;
58 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070059 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070060 private final Handler mH = new Handler();
61
62 private final Context mContext;
63 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080064 private final IAppOpsService mAppOpsService;
65 private final IBatteryStats mBatteryStatsService;
Jeff Brown7f6c2312012-04-13 20:38:38 -070066 private InputManager mIm;
67
68 volatile VibrateThread mThread;
69
70 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
71 // to be acquired
72 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
73 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
74 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
75
Dianne Hackborna06de0f2012-12-11 16:34:47 -080076 private int mCurVibUid = -1;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070077 private boolean mLowPowerMode;
78 private SettingsObserver mSettingObserver;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080079
Jeff Brown7f6c2312012-04-13 20:38:38 -070080 native static boolean vibratorExists();
81 native static void vibratorOn(long milliseconds);
82 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040083
Patrick Scott18dd5f02009-07-02 11:31:12 -040084 private class Vibration implements IBinder.DeathRecipient {
85 private final IBinder mToken;
86 private final long mTimeout;
87 private final long mStartTime;
88 private final long[] mPattern;
89 private final int mRepeat;
John Spurlock1af30c72014-03-10 08:33:35 -040090 private final int mStreamHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070091 private final int mUid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040092 private final String mOpPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -040093
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040094 Vibration(IBinder token, long millis, int streamHint, int uid, String opPkg) {
95 this(token, millis, null, 0, streamHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -040096 }
97
John Spurlock1af30c72014-03-10 08:33:35 -040098 Vibration(IBinder token, long[] pattern, int repeat, int streamHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040099 String opPkg) {
100 this(token, 0, pattern, repeat, streamHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400101 }
102
103 private Vibration(IBinder token, long millis, long[] pattern,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400104 int repeat, int streamHint, int uid, String opPkg) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400105 mToken = token;
106 mTimeout = millis;
107 mStartTime = SystemClock.uptimeMillis();
108 mPattern = pattern;
109 mRepeat = repeat;
John Spurlock1af30c72014-03-10 08:33:35 -0400110 mStreamHint = streamHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700111 mUid = uid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400112 mOpPkg = opPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400113 }
114
115 public void binderDied() {
116 synchronized (mVibrations) {
117 mVibrations.remove(this);
118 if (this == mCurrentVibration) {
119 doCancelVibrateLocked();
120 startNextVibrationLocked();
121 }
122 }
123 }
124
125 public boolean hasLongerTimeout(long millis) {
126 if (mTimeout == 0) {
127 // This is a pattern, return false to play the simple
128 // vibration.
129 return false;
130 }
131 if ((mStartTime + mTimeout)
132 < (SystemClock.uptimeMillis() + millis)) {
133 // If this vibration will end before the time passed in, let
134 // the new vibration play.
135 return false;
136 }
137 return true;
138 }
139 }
140
Mike Lockwood3a322132009-11-24 00:30:52 -0500141 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 // Reset the hardware to a default state, in case this is a runtime
143 // restart instead of a fresh boot.
144 vibratorOff();
145
146 mContext = context;
147 PowerManager pm = (PowerManager)context.getSystemService(
148 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700149 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 mWakeLock.setReferenceCounted(true);
151
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800152 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700153 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
154 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800155
Patrick Scott18dd5f02009-07-02 11:31:12 -0400156 mVibrations = new LinkedList<Vibration>();
157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 IntentFilter filter = new IntentFilter();
159 filter.addAction(Intent.ACTION_SCREEN_OFF);
160 context.registerReceiver(mIntentReceiver, filter);
161 }
162
Jeff Brown7f6c2312012-04-13 20:38:38 -0700163 public void systemReady() {
164 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700165 mSettingObserver = new SettingsObserver(mH);
Jeff Brownd4935962012-09-25 13:27:20 -0700166
Jeff Brown7f6c2312012-04-13 20:38:38 -0700167 mContext.getContentResolver().registerContentObserver(
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700168 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES),
169 true, mSettingObserver, UserHandle.USER_ALL);
170
171 mContext.getContentResolver().registerContentObserver(
172 Settings.Global.getUriFor(Settings.Global.LOW_POWER_MODE), false,
173 mSettingObserver, UserHandle.USER_ALL);
Jeff Brownd4935962012-09-25 13:27:20 -0700174
175 mContext.registerReceiver(new BroadcastReceiver() {
176 @Override
177 public void onReceive(Context context, Intent intent) {
178 updateInputDeviceVibrators();
179 }
180 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
181
Jeff Brown82065252012-04-16 13:19:05 -0700182 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700183 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700184
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700185 private final class SettingsObserver extends ContentObserver {
186 public SettingsObserver(Handler handler) {
187 super(handler);
188 }
189
190 @Override
191 public void onChange(boolean SelfChange) {
192 updateInputDeviceVibrators();
193 }
194 }
195
Jeff Brown7f6c2312012-04-13 20:38:38 -0700196 public boolean hasVibrator() {
197 return doVibratorExists();
198 }
199
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800200 private void verifyIncomingUid(int uid) {
201 if (uid == Binder.getCallingUid()) {
202 return;
203 }
204 if (Binder.getCallingPid() == Process.myPid()) {
205 return;
206 }
207 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
208 Binder.getCallingPid(), Binder.getCallingUid(), null);
209 }
210
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400211 public void vibrate(int uid, String opPkg, long milliseconds, int streamHint,
John Spurlock1af30c72014-03-10 08:33:35 -0400212 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700213 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
214 != PackageManager.PERMISSION_GRANTED) {
215 throw new SecurityException("Requires VIBRATE permission");
216 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800217 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400218 // We're running in the system server so we cannot crash. Check for a
219 // timeout of 0 or negative. This will ensure that a vibration has
220 // either a timeout of > 0 or a non-null pattern.
221 if (milliseconds <= 0 || (mCurrentVibration != null
222 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400223 // Ignore this vibration since the current vibration will play for
224 // longer than milliseconds.
225 return;
226 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700227
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400228 Vibration vib = new Vibration(token, milliseconds, streamHint, uid, opPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800229
230 final long ident = Binder.clearCallingIdentity();
231 try {
232 synchronized (mVibrations) {
233 removeVibrationLocked(token);
234 doCancelVibrateLocked();
235 mCurrentVibration = vib;
236 startVibrationLocked(vib);
237 }
238 } finally {
239 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242
243 private boolean isAll0(long[] pattern) {
244 int N = pattern.length;
245 for (int i = 0; i < N; i++) {
246 if (pattern[i] != 0) {
247 return false;
248 }
249 }
250 return true;
251 }
252
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800253 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock1af30c72014-03-10 08:33:35 -0400254 int streamHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
256 != PackageManager.PERMISSION_GRANTED) {
257 throw new SecurityException("Requires VIBRATE permission");
258 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800259 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 // so wakelock calls will succeed
261 long identity = Binder.clearCallingIdentity();
262 try {
263 if (false) {
264 String s = "";
265 int N = pattern.length;
266 for (int i=0; i<N; i++) {
267 s += " " + pattern[i];
268 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800269 Slog.i(TAG, "vibrating with pattern: " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271
272 // we're running in the server so we can't fail
273 if (pattern == null || pattern.length == 0
274 || isAll0(pattern)
275 || repeat >= pattern.length || token == null) {
276 return;
277 }
278
John Spurlock1af30c72014-03-10 08:33:35 -0400279 Vibration vib = new Vibration(token, pattern, repeat, streamHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400280 try {
281 token.linkToDeath(vib, 0);
282 } catch (RemoteException e) {
283 return;
284 }
285
286 synchronized (mVibrations) {
287 removeVibrationLocked(token);
288 doCancelVibrateLocked();
289 if (repeat >= 0) {
290 mVibrations.addFirst(vib);
291 startNextVibrationLocked();
292 } else {
293 // A negative repeat means that this pattern is not meant
294 // to repeat. Treat it like a simple vibration.
295 mCurrentVibration = vib;
296 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 }
299 }
300 finally {
301 Binder.restoreCallingIdentity(identity);
302 }
303 }
304
Patrick Scott18dd5f02009-07-02 11:31:12 -0400305 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 mContext.enforceCallingOrSelfPermission(
307 android.Manifest.permission.VIBRATE,
308 "cancelVibrate");
309
310 // so wakelock calls will succeed
311 long identity = Binder.clearCallingIdentity();
312 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400313 synchronized (mVibrations) {
314 final Vibration vib = removeVibrationLocked(token);
315 if (vib == mCurrentVibration) {
316 doCancelVibrateLocked();
317 startNextVibrationLocked();
318 }
319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
321 finally {
322 Binder.restoreCallingIdentity(identity);
323 }
324 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700325
Patrick Scott18dd5f02009-07-02 11:31:12 -0400326 private final Runnable mVibrationRunnable = new Runnable() {
327 public void run() {
328 synchronized (mVibrations) {
329 doCancelVibrateLocked();
330 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400332 }
333 };
334
335 // Lock held on mVibrations
336 private void doCancelVibrateLocked() {
337 if (mThread != null) {
338 synchronized (mThread) {
339 mThread.mDone = true;
340 mThread.notify();
341 }
342 mThread = null;
343 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700344 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400345 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800346 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400347 }
348
349 // Lock held on mVibrations
350 private void startNextVibrationLocked() {
351 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800352 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200353 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400354 return;
355 }
356 mCurrentVibration = mVibrations.getFirst();
357 startVibrationLocked(mCurrentVibration);
358 }
359
360 // Lock held on mVibrations
361 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800362 try {
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700363 if (mLowPowerMode && vib.mStreamHint != AudioManager.STREAM_RING) {
364 return;
365 }
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700366
John Spurlock1af30c72014-03-10 08:33:35 -0400367 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400368 vib.mStreamHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400369 if (mode == AppOpsManager.MODE_ALLOWED) {
370 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400371 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400372 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800373 if (mode != AppOpsManager.MODE_ALLOWED) {
374 if (mode == AppOpsManager.MODE_ERRORED) {
375 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
376 }
377 mH.post(mVibrationRunnable);
378 return;
379 }
380 } catch (RemoteException e) {
381 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400382 if (vib.mTimeout != 0) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400383 doVibratorOn(vib.mTimeout, vib.mUid, vib.mStreamHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400384 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
385 } else {
386 // mThread better be null here. doCancelVibrate should always be
387 // called before startNextVibrationLocked or startVibrationLocked.
388 mThread = new VibrateThread(vib);
389 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 }
391 }
392
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800393 private void reportFinishVibrationLocked() {
394 if (mCurrentVibration != null) {
395 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700396 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
397 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400398 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800399 } catch (RemoteException e) {
400 }
401 mCurrentVibration = null;
402 }
403 }
404
Patrick Scott18dd5f02009-07-02 11:31:12 -0400405 // Lock held on mVibrations
406 private Vibration removeVibrationLocked(IBinder token) {
407 ListIterator<Vibration> iter = mVibrations.listIterator(0);
408 while (iter.hasNext()) {
409 Vibration vib = iter.next();
410 if (vib.mToken == token) {
411 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200412 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400413 return vib;
414 }
415 }
416 // We might be looking for a simple vibration which is only stored in
417 // mCurrentVibration.
418 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200419 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400420 return mCurrentVibration;
421 }
422 return null;
423 }
424
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200425 private void unlinkVibration(Vibration vib) {
426 if (vib.mPattern != null) {
427 // If Vibration object has a pattern,
428 // the Vibration object has also been linkedToDeath.
429 vib.mToken.unlinkToDeath(vib, 0);
430 }
431 }
432
Jeff Brown7f6c2312012-04-13 20:38:38 -0700433 private void updateInputDeviceVibrators() {
434 synchronized (mVibrations) {
435 doCancelVibrateLocked();
436
437 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700438 mVibrateInputDevicesSetting = false;
439 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700440 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
441 mContext.getContentResolver(),
442 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700443 } catch (SettingNotFoundException snfe) {
444 }
445
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700446 mLowPowerMode = Settings.Global.getInt(mContext.getContentResolver(),
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700447 Settings.Global.LOW_POWER_MODE, 0) != 0;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700448
Jeff Brown82065252012-04-16 13:19:05 -0700449 if (mVibrateInputDevicesSetting) {
450 if (!mInputDeviceListenerRegistered) {
451 mInputDeviceListenerRegistered = true;
452 mIm.registerInputDeviceListener(this, mH);
453 }
454 } else {
455 if (mInputDeviceListenerRegistered) {
456 mInputDeviceListenerRegistered = false;
457 mIm.unregisterInputDeviceListener(this);
458 }
459 }
460
Jeff Brown7f6c2312012-04-13 20:38:38 -0700461 mInputDeviceVibrators.clear();
462 if (mVibrateInputDevicesSetting) {
463 int[] ids = mIm.getInputDeviceIds();
464 for (int i = 0; i < ids.length; i++) {
465 InputDevice device = mIm.getInputDevice(ids[i]);
466 Vibrator vibrator = device.getVibrator();
467 if (vibrator.hasVibrator()) {
468 mInputDeviceVibrators.add(vibrator);
469 }
470 }
471 }
472 }
473
474 startNextVibrationLocked();
475 }
476 }
477
478 @Override
479 public void onInputDeviceAdded(int deviceId) {
480 updateInputDeviceVibrators();
481 }
482
483 @Override
484 public void onInputDeviceChanged(int deviceId) {
485 updateInputDeviceVibrators();
486 }
487
488 @Override
489 public void onInputDeviceRemoved(int deviceId) {
490 updateInputDeviceVibrators();
491 }
492
493 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700494 // For now, we choose to ignore the presence of input devices that have vibrators
495 // when reporting whether the device has a vibrator. Applications often use this
496 // information to decide whether to enable certain features so they expect the
497 // result of hasVibrator() to be constant. For now, just report whether
498 // the device has a built-in vibrator.
499 //synchronized (mInputDeviceVibrators) {
500 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
501 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800502 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700503 }
504
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400505 private void doVibratorOn(long millis, int uid, int streamHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700506 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800507 try {
508 mBatteryStatsService.noteVibratorOn(uid, millis);
509 mCurVibUid = uid;
510 } catch (RemoteException e) {
511 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700512 final int vibratorCount = mInputDeviceVibrators.size();
513 if (vibratorCount != 0) {
514 for (int i = 0; i < vibratorCount; i++) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400515 mInputDeviceVibrators.get(i).vibrate(millis, streamHint);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700516 }
517 } else {
518 vibratorOn(millis);
519 }
520 }
521 }
522
523 private void doVibratorOff() {
524 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800525 if (mCurVibUid >= 0) {
526 try {
527 mBatteryStatsService.noteVibratorOff(mCurVibUid);
528 } catch (RemoteException e) {
529 }
530 mCurVibUid = -1;
531 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700532 final int vibratorCount = mInputDeviceVibrators.size();
533 if (vibratorCount != 0) {
534 for (int i = 0; i < vibratorCount; i++) {
535 mInputDeviceVibrators.get(i).cancel();
536 }
537 } else {
538 vibratorOff();
539 }
540 }
541 }
542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400544 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700546
Patrick Scott18dd5f02009-07-02 11:31:12 -0400547 VibrateThread(Vibration vib) {
548 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700549 mTmpWorkSource.set(vib.mUid);
550 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 mWakeLock.acquire();
552 }
553
554 private void delay(long duration) {
555 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700556 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 do {
558 try {
559 this.wait(duration);
560 }
561 catch (InterruptedException e) {
562 }
563 if (mDone) {
564 break;
565 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700566 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 } while (duration > 0);
568 }
569 }
570
571 public void run() {
572 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
573 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800574 final long[] pattern = mVibration.mPattern;
575 final int len = pattern.length;
576 final int repeat = mVibration.mRepeat;
577 final int uid = mVibration.mUid;
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400578 final int streamHint = mVibration.mStreamHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 long duration = 0;
581
582 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700583 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 if (index < len) {
585 duration += pattern[index++];
586 }
587
588 // sleep until it is time to start the vibrator
589 delay(duration);
590 if (mDone) {
591 break;
592 }
593
594 if (index < len) {
595 // read on-time duration and start the vibrator
596 // duration is saved for delay() at top of loop
597 duration = pattern[index++];
598 if (duration > 0) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400599 VibratorService.this.doVibratorOn(duration, uid, streamHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400602 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 break;
604 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400605 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 duration = 0;
607 }
608 }
609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 mWakeLock.release();
611 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400612 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 if (mThread == this) {
614 mThread = null;
615 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400616 if (!mDone) {
617 // If this vibration finished naturally, start the next
618 // vibration.
619 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200620 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400621 startNextVibrationLocked();
622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 }
624 }
625 };
626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
628 public void onReceive(Context context, Intent intent) {
629 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400630 synchronized (mVibrations) {
631 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700632
633 int size = mVibrations.size();
634 for(int i = 0; i < size; i++) {
635 unlinkVibration(mVibrations.get(i));
636 }
637
Patrick Scott18dd5f02009-07-02 11:31:12 -0400638 mVibrations.clear();
639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641 }
642 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643}