blob: 06dd3edb30155264e10d97b334d68def96891224 [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;
Jeff Brown969579b2014-05-20 19:29:29 -070050import java.util.Iterator;
Patrick Scott18dd5f02009-07-02 11:31:12 -040051import java.util.LinkedList;
52import java.util.ListIterator;
53
Jeff Brown7f6c2312012-04-13 20:38:38 -070054public class VibratorService extends IVibratorService.Stub
55 implements InputManager.InputDeviceListener {
Mike Lockwood3a322132009-11-24 00:30:52 -050056 private static final String TAG = "VibratorService";
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050057
Patrick Scott18dd5f02009-07-02 11:31:12 -040058 private final LinkedList<Vibration> mVibrations;
59 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070060 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070061 private final Handler mH = new Handler();
62
63 private final Context mContext;
64 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080065 private final IAppOpsService mAppOpsService;
66 private final IBatteryStats mBatteryStatsService;
Jeff Brown7f6c2312012-04-13 20:38:38 -070067 private InputManager mIm;
68
69 volatile VibrateThread mThread;
70
71 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
72 // to be acquired
73 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
74 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
75 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
76
Dianne Hackborna06de0f2012-12-11 16:34:47 -080077 private int mCurVibUid = -1;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070078 private boolean mLowPowerMode;
79 private SettingsObserver mSettingObserver;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080080
Jeff Brown7f6c2312012-04-13 20:38:38 -070081 native static boolean vibratorExists();
82 native static void vibratorOn(long milliseconds);
83 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040084
Patrick Scott18dd5f02009-07-02 11:31:12 -040085 private class Vibration implements IBinder.DeathRecipient {
86 private final IBinder mToken;
87 private final long mTimeout;
88 private final long mStartTime;
89 private final long[] mPattern;
90 private final int mRepeat;
John Spurlock1af30c72014-03-10 08:33:35 -040091 private final int mStreamHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070092 private final int mUid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040093 private final String mOpPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -040094
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040095 Vibration(IBinder token, long millis, int streamHint, int uid, String opPkg) {
96 this(token, millis, null, 0, streamHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -040097 }
98
John Spurlock1af30c72014-03-10 08:33:35 -040099 Vibration(IBinder token, long[] pattern, int repeat, int streamHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400100 String opPkg) {
101 this(token, 0, pattern, repeat, streamHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400102 }
103
104 private Vibration(IBinder token, long millis, long[] pattern,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400105 int repeat, int streamHint, int uid, String opPkg) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400106 mToken = token;
107 mTimeout = millis;
108 mStartTime = SystemClock.uptimeMillis();
109 mPattern = pattern;
110 mRepeat = repeat;
John Spurlock1af30c72014-03-10 08:33:35 -0400111 mStreamHint = streamHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700112 mUid = uid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400113 mOpPkg = opPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400114 }
115
116 public void binderDied() {
117 synchronized (mVibrations) {
118 mVibrations.remove(this);
119 if (this == mCurrentVibration) {
120 doCancelVibrateLocked();
121 startNextVibrationLocked();
122 }
123 }
124 }
125
126 public boolean hasLongerTimeout(long millis) {
127 if (mTimeout == 0) {
128 // This is a pattern, return false to play the simple
129 // vibration.
130 return false;
131 }
132 if ((mStartTime + mTimeout)
133 < (SystemClock.uptimeMillis() + millis)) {
134 // If this vibration will end before the time passed in, let
135 // the new vibration play.
136 return false;
137 }
138 return true;
139 }
Jeff Brown969579b2014-05-20 19:29:29 -0700140
141 public boolean isSystemHapticFeedback() {
142 return (mUid == Process.SYSTEM_UID || mUid == 0) && mRepeat < 0;
143 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400144 }
145
Mike Lockwood3a322132009-11-24 00:30:52 -0500146 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 // Reset the hardware to a default state, in case this is a runtime
148 // restart instead of a fresh boot.
149 vibratorOff();
150
151 mContext = context;
152 PowerManager pm = (PowerManager)context.getSystemService(
153 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700154 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 mWakeLock.setReferenceCounted(true);
156
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800157 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700158 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
159 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800160
Patrick Scott18dd5f02009-07-02 11:31:12 -0400161 mVibrations = new LinkedList<Vibration>();
162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 IntentFilter filter = new IntentFilter();
164 filter.addAction(Intent.ACTION_SCREEN_OFF);
165 context.registerReceiver(mIntentReceiver, filter);
166 }
167
Jeff Brown7f6c2312012-04-13 20:38:38 -0700168 public void systemReady() {
169 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700170 mSettingObserver = new SettingsObserver(mH);
Jeff Brownd4935962012-09-25 13:27:20 -0700171
Jeff Brown7f6c2312012-04-13 20:38:38 -0700172 mContext.getContentResolver().registerContentObserver(
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700173 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES),
174 true, mSettingObserver, UserHandle.USER_ALL);
175
176 mContext.getContentResolver().registerContentObserver(
177 Settings.Global.getUriFor(Settings.Global.LOW_POWER_MODE), false,
178 mSettingObserver, UserHandle.USER_ALL);
Jeff Brownd4935962012-09-25 13:27:20 -0700179
180 mContext.registerReceiver(new BroadcastReceiver() {
181 @Override
182 public void onReceive(Context context, Intent intent) {
183 updateInputDeviceVibrators();
184 }
185 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
186
Jeff Brown82065252012-04-16 13:19:05 -0700187 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700188 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700189
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700190 private final class SettingsObserver extends ContentObserver {
191 public SettingsObserver(Handler handler) {
192 super(handler);
193 }
194
195 @Override
196 public void onChange(boolean SelfChange) {
197 updateInputDeviceVibrators();
198 }
199 }
200
Jeff Brown7f6c2312012-04-13 20:38:38 -0700201 public boolean hasVibrator() {
202 return doVibratorExists();
203 }
204
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800205 private void verifyIncomingUid(int uid) {
206 if (uid == Binder.getCallingUid()) {
207 return;
208 }
209 if (Binder.getCallingPid() == Process.myPid()) {
210 return;
211 }
212 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
213 Binder.getCallingPid(), Binder.getCallingUid(), null);
214 }
215
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400216 public void vibrate(int uid, String opPkg, long milliseconds, int streamHint,
John Spurlock1af30c72014-03-10 08:33:35 -0400217 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700218 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
219 != PackageManager.PERMISSION_GRANTED) {
220 throw new SecurityException("Requires VIBRATE permission");
221 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800222 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400223 // We're running in the system server so we cannot crash. Check for a
224 // timeout of 0 or negative. This will ensure that a vibration has
225 // either a timeout of > 0 or a non-null pattern.
226 if (milliseconds <= 0 || (mCurrentVibration != null
227 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400228 // Ignore this vibration since the current vibration will play for
229 // longer than milliseconds.
230 return;
231 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700232
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400233 Vibration vib = new Vibration(token, milliseconds, streamHint, uid, opPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800234
235 final long ident = Binder.clearCallingIdentity();
236 try {
237 synchronized (mVibrations) {
238 removeVibrationLocked(token);
239 doCancelVibrateLocked();
240 mCurrentVibration = vib;
241 startVibrationLocked(vib);
242 }
243 } finally {
244 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
247
248 private boolean isAll0(long[] pattern) {
249 int N = pattern.length;
250 for (int i = 0; i < N; i++) {
251 if (pattern[i] != 0) {
252 return false;
253 }
254 }
255 return true;
256 }
257
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800258 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock1af30c72014-03-10 08:33:35 -0400259 int streamHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
261 != PackageManager.PERMISSION_GRANTED) {
262 throw new SecurityException("Requires VIBRATE permission");
263 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800264 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 // so wakelock calls will succeed
266 long identity = Binder.clearCallingIdentity();
267 try {
268 if (false) {
269 String s = "";
270 int N = pattern.length;
271 for (int i=0; i<N; i++) {
272 s += " " + pattern[i];
273 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800274 Slog.i(TAG, "vibrating with pattern: " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276
277 // we're running in the server so we can't fail
278 if (pattern == null || pattern.length == 0
279 || isAll0(pattern)
280 || repeat >= pattern.length || token == null) {
281 return;
282 }
283
John Spurlock1af30c72014-03-10 08:33:35 -0400284 Vibration vib = new Vibration(token, pattern, repeat, streamHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400285 try {
286 token.linkToDeath(vib, 0);
287 } catch (RemoteException e) {
288 return;
289 }
290
291 synchronized (mVibrations) {
292 removeVibrationLocked(token);
293 doCancelVibrateLocked();
294 if (repeat >= 0) {
295 mVibrations.addFirst(vib);
296 startNextVibrationLocked();
297 } else {
298 // A negative repeat means that this pattern is not meant
299 // to repeat. Treat it like a simple vibration.
300 mCurrentVibration = vib;
301 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
304 }
305 finally {
306 Binder.restoreCallingIdentity(identity);
307 }
308 }
309
Patrick Scott18dd5f02009-07-02 11:31:12 -0400310 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 mContext.enforceCallingOrSelfPermission(
312 android.Manifest.permission.VIBRATE,
313 "cancelVibrate");
314
315 // so wakelock calls will succeed
316 long identity = Binder.clearCallingIdentity();
317 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400318 synchronized (mVibrations) {
319 final Vibration vib = removeVibrationLocked(token);
320 if (vib == mCurrentVibration) {
321 doCancelVibrateLocked();
322 startNextVibrationLocked();
323 }
324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 }
326 finally {
327 Binder.restoreCallingIdentity(identity);
328 }
329 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700330
Patrick Scott18dd5f02009-07-02 11:31:12 -0400331 private final Runnable mVibrationRunnable = new Runnable() {
332 public void run() {
333 synchronized (mVibrations) {
334 doCancelVibrateLocked();
335 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400337 }
338 };
339
340 // Lock held on mVibrations
341 private void doCancelVibrateLocked() {
342 if (mThread != null) {
343 synchronized (mThread) {
344 mThread.mDone = true;
345 mThread.notify();
346 }
347 mThread = null;
348 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700349 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400350 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800351 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400352 }
353
354 // Lock held on mVibrations
355 private void startNextVibrationLocked() {
356 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800357 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200358 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400359 return;
360 }
361 mCurrentVibration = mVibrations.getFirst();
362 startVibrationLocked(mCurrentVibration);
363 }
364
365 // Lock held on mVibrations
366 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800367 try {
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700368 if (mLowPowerMode && vib.mStreamHint != AudioManager.STREAM_RING) {
369 return;
370 }
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700371
John Spurlock1af30c72014-03-10 08:33:35 -0400372 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400373 vib.mStreamHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400374 if (mode == AppOpsManager.MODE_ALLOWED) {
375 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400376 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400377 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800378 if (mode != AppOpsManager.MODE_ALLOWED) {
379 if (mode == AppOpsManager.MODE_ERRORED) {
380 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
381 }
382 mH.post(mVibrationRunnable);
383 return;
384 }
385 } catch (RemoteException e) {
386 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400387 if (vib.mTimeout != 0) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400388 doVibratorOn(vib.mTimeout, vib.mUid, vib.mStreamHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400389 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
390 } else {
391 // mThread better be null here. doCancelVibrate should always be
392 // called before startNextVibrationLocked or startVibrationLocked.
393 mThread = new VibrateThread(vib);
394 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
396 }
397
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800398 private void reportFinishVibrationLocked() {
399 if (mCurrentVibration != null) {
400 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700401 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
402 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400403 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800404 } catch (RemoteException e) {
405 }
406 mCurrentVibration = null;
407 }
408 }
409
Patrick Scott18dd5f02009-07-02 11:31:12 -0400410 // Lock held on mVibrations
411 private Vibration removeVibrationLocked(IBinder token) {
412 ListIterator<Vibration> iter = mVibrations.listIterator(0);
413 while (iter.hasNext()) {
414 Vibration vib = iter.next();
415 if (vib.mToken == token) {
416 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200417 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400418 return vib;
419 }
420 }
421 // We might be looking for a simple vibration which is only stored in
422 // mCurrentVibration.
423 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200424 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400425 return mCurrentVibration;
426 }
427 return null;
428 }
429
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200430 private void unlinkVibration(Vibration vib) {
431 if (vib.mPattern != null) {
432 // If Vibration object has a pattern,
433 // the Vibration object has also been linkedToDeath.
434 vib.mToken.unlinkToDeath(vib, 0);
435 }
436 }
437
Jeff Brown7f6c2312012-04-13 20:38:38 -0700438 private void updateInputDeviceVibrators() {
439 synchronized (mVibrations) {
440 doCancelVibrateLocked();
441
442 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700443 mVibrateInputDevicesSetting = false;
444 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700445 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
446 mContext.getContentResolver(),
447 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700448 } catch (SettingNotFoundException snfe) {
449 }
450
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700451 mLowPowerMode = Settings.Global.getInt(mContext.getContentResolver(),
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700452 Settings.Global.LOW_POWER_MODE, 0) != 0;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700453
Jeff Brown82065252012-04-16 13:19:05 -0700454 if (mVibrateInputDevicesSetting) {
455 if (!mInputDeviceListenerRegistered) {
456 mInputDeviceListenerRegistered = true;
457 mIm.registerInputDeviceListener(this, mH);
458 }
459 } else {
460 if (mInputDeviceListenerRegistered) {
461 mInputDeviceListenerRegistered = false;
462 mIm.unregisterInputDeviceListener(this);
463 }
464 }
465
Jeff Brown7f6c2312012-04-13 20:38:38 -0700466 mInputDeviceVibrators.clear();
467 if (mVibrateInputDevicesSetting) {
468 int[] ids = mIm.getInputDeviceIds();
469 for (int i = 0; i < ids.length; i++) {
470 InputDevice device = mIm.getInputDevice(ids[i]);
471 Vibrator vibrator = device.getVibrator();
472 if (vibrator.hasVibrator()) {
473 mInputDeviceVibrators.add(vibrator);
474 }
475 }
476 }
477 }
478
479 startNextVibrationLocked();
480 }
481 }
482
483 @Override
484 public void onInputDeviceAdded(int deviceId) {
485 updateInputDeviceVibrators();
486 }
487
488 @Override
489 public void onInputDeviceChanged(int deviceId) {
490 updateInputDeviceVibrators();
491 }
492
493 @Override
494 public void onInputDeviceRemoved(int deviceId) {
495 updateInputDeviceVibrators();
496 }
497
498 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700499 // For now, we choose to ignore the presence of input devices that have vibrators
500 // when reporting whether the device has a vibrator. Applications often use this
501 // information to decide whether to enable certain features so they expect the
502 // result of hasVibrator() to be constant. For now, just report whether
503 // the device has a built-in vibrator.
504 //synchronized (mInputDeviceVibrators) {
505 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
506 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800507 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700508 }
509
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400510 private void doVibratorOn(long millis, int uid, int streamHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700511 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800512 try {
513 mBatteryStatsService.noteVibratorOn(uid, millis);
514 mCurVibUid = uid;
515 } catch (RemoteException e) {
516 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700517 final int vibratorCount = mInputDeviceVibrators.size();
518 if (vibratorCount != 0) {
519 for (int i = 0; i < vibratorCount; i++) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400520 mInputDeviceVibrators.get(i).vibrate(millis, streamHint);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700521 }
522 } else {
523 vibratorOn(millis);
524 }
525 }
526 }
527
528 private void doVibratorOff() {
529 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800530 if (mCurVibUid >= 0) {
531 try {
532 mBatteryStatsService.noteVibratorOff(mCurVibUid);
533 } catch (RemoteException e) {
534 }
535 mCurVibUid = -1;
536 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700537 final int vibratorCount = mInputDeviceVibrators.size();
538 if (vibratorCount != 0) {
539 for (int i = 0; i < vibratorCount; i++) {
540 mInputDeviceVibrators.get(i).cancel();
541 }
542 } else {
543 vibratorOff();
544 }
545 }
546 }
547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400549 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700551
Patrick Scott18dd5f02009-07-02 11:31:12 -0400552 VibrateThread(Vibration vib) {
553 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700554 mTmpWorkSource.set(vib.mUid);
555 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 mWakeLock.acquire();
557 }
558
559 private void delay(long duration) {
560 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700561 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 do {
563 try {
564 this.wait(duration);
565 }
566 catch (InterruptedException e) {
567 }
568 if (mDone) {
569 break;
570 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700571 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 } while (duration > 0);
573 }
574 }
575
576 public void run() {
577 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
578 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800579 final long[] pattern = mVibration.mPattern;
580 final int len = pattern.length;
581 final int repeat = mVibration.mRepeat;
582 final int uid = mVibration.mUid;
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400583 final int streamHint = mVibration.mStreamHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 long duration = 0;
586
587 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700588 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 if (index < len) {
590 duration += pattern[index++];
591 }
592
593 // sleep until it is time to start the vibrator
594 delay(duration);
595 if (mDone) {
596 break;
597 }
598
599 if (index < len) {
600 // read on-time duration and start the vibrator
601 // duration is saved for delay() at top of loop
602 duration = pattern[index++];
603 if (duration > 0) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400604 VibratorService.this.doVibratorOn(duration, uid, streamHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400607 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 break;
609 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400610 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 duration = 0;
612 }
613 }
614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 mWakeLock.release();
616 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400617 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 if (mThread == this) {
619 mThread = null;
620 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400621 if (!mDone) {
622 // If this vibration finished naturally, start the next
623 // vibration.
624 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200625 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400626 startNextVibrationLocked();
627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
629 }
Jeff Brown969579b2014-05-20 19:29:29 -0700630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
Jeff Brown969579b2014-05-20 19:29:29 -0700633 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 public void onReceive(Context context, Intent intent) {
635 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400636 synchronized (mVibrations) {
Jeff Brown969579b2014-05-20 19:29:29 -0700637 // When the system is entering a non-interactive state, we want
638 // to cancel vibrations in case a misbehaving app has abandoned
639 // them. However it may happen that the system is currently playing
640 // haptic feedback as part of the transition. So we don't cancel
641 // system vibrations.
642 if (mCurrentVibration != null
643 && !mCurrentVibration.isSystemHapticFeedback()) {
644 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700645 }
646
Jeff Brown969579b2014-05-20 19:29:29 -0700647 // Clear all remaining vibrations.
648 Iterator<Vibration> it = mVibrations.iterator();
649 while (it.hasNext()) {
650 Vibration vibration = it.next();
651 if (vibration != mCurrentVibration) {
652 unlinkVibration(vibration);
653 it.remove();
654 }
655 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 }
658 }
659 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660}