blob: 248a3037dc1f32e299af6940fa0cc49b4465c902 [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;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070031import android.os.PowerManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.Process;
33import android.os.RemoteException;
34import android.os.IBinder;
35import android.os.Binder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080036import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.SystemClock;
Jeff Brownd4935962012-09-25 13:27:20 -070038import android.os.UserHandle;
Jeff Brown7f6c2312012-04-13 20:38:38 -070039import android.os.Vibrator;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070040import android.os.WorkSource;
Jeff Brown7f6c2312012-04-13 20:38:38 -070041import android.provider.Settings;
42import android.provider.Settings.SettingNotFoundException;
Joe Onorato8a9b2202010-02-26 18:56:32 -080043import android.util.Slog;
Jeff Brown7f6c2312012-04-13 20:38:38 -070044import android.view.InputDevice;
John Spurlock7b414672014-07-18 13:02:39 -040045import android.media.AudioAttributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Dianne Hackborna06de0f2012-12-11 16:34:47 -080047import com.android.internal.app.IAppOpsService;
48import com.android.internal.app.IBatteryStats;
49
Jeff Brown7f6c2312012-04-13 20:38:38 -070050import java.util.ArrayList;
Jeff Brown969579b2014-05-20 19:29:29 -070051import java.util.Iterator;
Patrick Scott18dd5f02009-07-02 11:31:12 -040052import java.util.LinkedList;
53import java.util.ListIterator;
54
Jeff Brown7f6c2312012-04-13 20:38:38 -070055public class VibratorService extends IVibratorService.Stub
56 implements InputManager.InputDeviceListener {
Mike Lockwood3a322132009-11-24 00:30:52 -050057 private static final String TAG = "VibratorService";
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050058
Patrick Scott18dd5f02009-07-02 11:31:12 -040059 private final LinkedList<Vibration> mVibrations;
60 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070061 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070062 private final Handler mH = new Handler();
63
64 private final Context mContext;
65 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080066 private final IAppOpsService mAppOpsService;
67 private final IBatteryStats mBatteryStatsService;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070068 private PowerManagerInternal mPowerManagerInternal;
Jeff Brown7f6c2312012-04-13 20:38:38 -070069 private InputManager mIm;
70
71 volatile VibrateThread mThread;
72
73 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
74 // to be acquired
75 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
76 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
77 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
78
Dianne Hackborna06de0f2012-12-11 16:34:47 -080079 private int mCurVibUid = -1;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070080 private boolean mLowPowerMode;
81 private SettingsObserver mSettingObserver;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080082
Jeff Brown7f6c2312012-04-13 20:38:38 -070083 native static boolean vibratorExists();
84 native static void vibratorOn(long milliseconds);
85 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040086
Patrick Scott18dd5f02009-07-02 11:31:12 -040087 private class Vibration implements IBinder.DeathRecipient {
88 private final IBinder mToken;
89 private final long mTimeout;
90 private final long mStartTime;
91 private final long[] mPattern;
92 private final int mRepeat;
John Spurlock7b414672014-07-18 13:02:39 -040093 private final int mUsageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070094 private final int mUid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040095 private final String mOpPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -040096
John Spurlock7b414672014-07-18 13:02:39 -040097 Vibration(IBinder token, long millis, int usageHint, int uid, String opPkg) {
98 this(token, millis, null, 0, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -040099 }
100
John Spurlock7b414672014-07-18 13:02:39 -0400101 Vibration(IBinder token, long[] pattern, int repeat, int usageHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400102 String opPkg) {
John Spurlock7b414672014-07-18 13:02:39 -0400103 this(token, 0, pattern, repeat, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400104 }
105
106 private Vibration(IBinder token, long millis, long[] pattern,
John Spurlock7b414672014-07-18 13:02:39 -0400107 int repeat, int usageHint, int uid, String opPkg) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400108 mToken = token;
109 mTimeout = millis;
110 mStartTime = SystemClock.uptimeMillis();
111 mPattern = pattern;
112 mRepeat = repeat;
John Spurlock7b414672014-07-18 13:02:39 -0400113 mUsageHint = usageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700114 mUid = uid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400115 mOpPkg = opPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400116 }
117
118 public void binderDied() {
119 synchronized (mVibrations) {
120 mVibrations.remove(this);
121 if (this == mCurrentVibration) {
122 doCancelVibrateLocked();
123 startNextVibrationLocked();
124 }
125 }
126 }
127
128 public boolean hasLongerTimeout(long millis) {
129 if (mTimeout == 0) {
130 // This is a pattern, return false to play the simple
131 // vibration.
132 return false;
133 }
134 if ((mStartTime + mTimeout)
135 < (SystemClock.uptimeMillis() + millis)) {
136 // If this vibration will end before the time passed in, let
137 // the new vibration play.
138 return false;
139 }
140 return true;
141 }
Jeff Brown969579b2014-05-20 19:29:29 -0700142
143 public boolean isSystemHapticFeedback() {
144 return (mUid == Process.SYSTEM_UID || mUid == 0) && mRepeat < 0;
145 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400146 }
147
Mike Lockwood3a322132009-11-24 00:30:52 -0500148 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 // Reset the hardware to a default state, in case this is a runtime
150 // restart instead of a fresh boot.
151 vibratorOff();
152
153 mContext = context;
154 PowerManager pm = (PowerManager)context.getSystemService(
155 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700156 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 mWakeLock.setReferenceCounted(true);
158
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800159 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700160 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
161 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800162
Patrick Scott18dd5f02009-07-02 11:31:12 -0400163 mVibrations = new LinkedList<Vibration>();
164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 IntentFilter filter = new IntentFilter();
166 filter.addAction(Intent.ACTION_SCREEN_OFF);
167 context.registerReceiver(mIntentReceiver, filter);
168 }
169
Jeff Brown7f6c2312012-04-13 20:38:38 -0700170 public void systemReady() {
171 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700172 mSettingObserver = new SettingsObserver(mH);
Jeff Brownd4935962012-09-25 13:27:20 -0700173
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700174 mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
175 mPowerManagerInternal.registerLowPowerModeObserver(
176 new PowerManagerInternal.LowPowerModeListener() {
177 @Override
178 public void onLowPowerModeChanged(boolean enabled) {
179 updateInputDeviceVibrators();
180 }
181 });
182
Jeff Brown7f6c2312012-04-13 20:38:38 -0700183 mContext.getContentResolver().registerContentObserver(
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700184 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES),
185 true, mSettingObserver, UserHandle.USER_ALL);
186
Jeff Brownd4935962012-09-25 13:27:20 -0700187 mContext.registerReceiver(new BroadcastReceiver() {
188 @Override
189 public void onReceive(Context context, Intent intent) {
190 updateInputDeviceVibrators();
191 }
192 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
193
Jeff Brown82065252012-04-16 13:19:05 -0700194 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700195 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700196
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700197 private final class SettingsObserver extends ContentObserver {
198 public SettingsObserver(Handler handler) {
199 super(handler);
200 }
201
202 @Override
203 public void onChange(boolean SelfChange) {
204 updateInputDeviceVibrators();
205 }
206 }
207
Jeff Brown7f6c2312012-04-13 20:38:38 -0700208 public boolean hasVibrator() {
209 return doVibratorExists();
210 }
211
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800212 private void verifyIncomingUid(int uid) {
213 if (uid == Binder.getCallingUid()) {
214 return;
215 }
216 if (Binder.getCallingPid() == Process.myPid()) {
217 return;
218 }
219 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
220 Binder.getCallingPid(), Binder.getCallingUid(), null);
221 }
222
John Spurlock7b414672014-07-18 13:02:39 -0400223 public void vibrate(int uid, String opPkg, long milliseconds, int usageHint,
John Spurlock1af30c72014-03-10 08:33:35 -0400224 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700225 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
226 != PackageManager.PERMISSION_GRANTED) {
227 throw new SecurityException("Requires VIBRATE permission");
228 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800229 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400230 // We're running in the system server so we cannot crash. Check for a
231 // timeout of 0 or negative. This will ensure that a vibration has
232 // either a timeout of > 0 or a non-null pattern.
233 if (milliseconds <= 0 || (mCurrentVibration != null
234 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400235 // Ignore this vibration since the current vibration will play for
236 // longer than milliseconds.
237 return;
238 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700239
John Spurlock7b414672014-07-18 13:02:39 -0400240 Vibration vib = new Vibration(token, milliseconds, usageHint, uid, opPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800241
242 final long ident = Binder.clearCallingIdentity();
243 try {
244 synchronized (mVibrations) {
245 removeVibrationLocked(token);
246 doCancelVibrateLocked();
247 mCurrentVibration = vib;
248 startVibrationLocked(vib);
249 }
250 } finally {
251 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 }
254
255 private boolean isAll0(long[] pattern) {
256 int N = pattern.length;
257 for (int i = 0; i < N; i++) {
258 if (pattern[i] != 0) {
259 return false;
260 }
261 }
262 return true;
263 }
264
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800265 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock7b414672014-07-18 13:02:39 -0400266 int usageHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
268 != PackageManager.PERMISSION_GRANTED) {
269 throw new SecurityException("Requires VIBRATE permission");
270 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800271 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 // so wakelock calls will succeed
273 long identity = Binder.clearCallingIdentity();
274 try {
275 if (false) {
276 String s = "";
277 int N = pattern.length;
278 for (int i=0; i<N; i++) {
279 s += " " + pattern[i];
280 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800281 Slog.i(TAG, "vibrating with pattern: " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
283
284 // we're running in the server so we can't fail
285 if (pattern == null || pattern.length == 0
286 || isAll0(pattern)
287 || repeat >= pattern.length || token == null) {
288 return;
289 }
290
John Spurlock7b414672014-07-18 13:02:39 -0400291 Vibration vib = new Vibration(token, pattern, repeat, usageHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400292 try {
293 token.linkToDeath(vib, 0);
294 } catch (RemoteException e) {
295 return;
296 }
297
298 synchronized (mVibrations) {
299 removeVibrationLocked(token);
300 doCancelVibrateLocked();
301 if (repeat >= 0) {
302 mVibrations.addFirst(vib);
303 startNextVibrationLocked();
304 } else {
305 // A negative repeat means that this pattern is not meant
306 // to repeat. Treat it like a simple vibration.
307 mCurrentVibration = vib;
308 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
311 }
312 finally {
313 Binder.restoreCallingIdentity(identity);
314 }
315 }
316
Patrick Scott18dd5f02009-07-02 11:31:12 -0400317 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 mContext.enforceCallingOrSelfPermission(
319 android.Manifest.permission.VIBRATE,
320 "cancelVibrate");
321
322 // so wakelock calls will succeed
323 long identity = Binder.clearCallingIdentity();
324 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400325 synchronized (mVibrations) {
326 final Vibration vib = removeVibrationLocked(token);
327 if (vib == mCurrentVibration) {
328 doCancelVibrateLocked();
329 startNextVibrationLocked();
330 }
331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 }
333 finally {
334 Binder.restoreCallingIdentity(identity);
335 }
336 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700337
Patrick Scott18dd5f02009-07-02 11:31:12 -0400338 private final Runnable mVibrationRunnable = new Runnable() {
339 public void run() {
340 synchronized (mVibrations) {
341 doCancelVibrateLocked();
342 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400344 }
345 };
346
347 // Lock held on mVibrations
348 private void doCancelVibrateLocked() {
349 if (mThread != null) {
350 synchronized (mThread) {
351 mThread.mDone = true;
352 mThread.notify();
353 }
354 mThread = null;
355 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700356 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400357 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800358 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400359 }
360
361 // Lock held on mVibrations
362 private void startNextVibrationLocked() {
363 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800364 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200365 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400366 return;
367 }
368 mCurrentVibration = mVibrations.getFirst();
369 startVibrationLocked(mCurrentVibration);
370 }
371
372 // Lock held on mVibrations
373 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800374 try {
John Spurlock7b414672014-07-18 13:02:39 -0400375 if (mLowPowerMode
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -0700376 && vib.mUsageHint != AudioAttributes.USAGE_NOTIFICATION_RINGTONE) {
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700377 return;
378 }
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700379
John Spurlock1af30c72014-03-10 08:33:35 -0400380 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
John Spurlock7b414672014-07-18 13:02:39 -0400381 vib.mUsageHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400382 if (mode == AppOpsManager.MODE_ALLOWED) {
383 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400384 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400385 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800386 if (mode != AppOpsManager.MODE_ALLOWED) {
387 if (mode == AppOpsManager.MODE_ERRORED) {
388 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
389 }
390 mH.post(mVibrationRunnable);
391 return;
392 }
393 } catch (RemoteException e) {
394 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400395 if (vib.mTimeout != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400396 doVibratorOn(vib.mTimeout, vib.mUid, vib.mUsageHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400397 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
398 } else {
399 // mThread better be null here. doCancelVibrate should always be
400 // called before startNextVibrationLocked or startVibrationLocked.
401 mThread = new VibrateThread(vib);
402 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
404 }
405
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800406 private void reportFinishVibrationLocked() {
407 if (mCurrentVibration != null) {
408 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700409 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
410 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400411 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800412 } catch (RemoteException e) {
413 }
414 mCurrentVibration = null;
415 }
416 }
417
Patrick Scott18dd5f02009-07-02 11:31:12 -0400418 // Lock held on mVibrations
419 private Vibration removeVibrationLocked(IBinder token) {
420 ListIterator<Vibration> iter = mVibrations.listIterator(0);
421 while (iter.hasNext()) {
422 Vibration vib = iter.next();
423 if (vib.mToken == token) {
424 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200425 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400426 return vib;
427 }
428 }
429 // We might be looking for a simple vibration which is only stored in
430 // mCurrentVibration.
431 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200432 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400433 return mCurrentVibration;
434 }
435 return null;
436 }
437
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200438 private void unlinkVibration(Vibration vib) {
439 if (vib.mPattern != null) {
440 // If Vibration object has a pattern,
441 // the Vibration object has also been linkedToDeath.
442 vib.mToken.unlinkToDeath(vib, 0);
443 }
444 }
445
Jeff Brown7f6c2312012-04-13 20:38:38 -0700446 private void updateInputDeviceVibrators() {
447 synchronized (mVibrations) {
448 doCancelVibrateLocked();
449
450 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700451 mVibrateInputDevicesSetting = false;
452 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700453 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
454 mContext.getContentResolver(),
455 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700456 } catch (SettingNotFoundException snfe) {
457 }
458
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700459 mLowPowerMode = mPowerManagerInternal.getLowPowerModeEnabled();
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700460
Jeff Brown82065252012-04-16 13:19:05 -0700461 if (mVibrateInputDevicesSetting) {
462 if (!mInputDeviceListenerRegistered) {
463 mInputDeviceListenerRegistered = true;
464 mIm.registerInputDeviceListener(this, mH);
465 }
466 } else {
467 if (mInputDeviceListenerRegistered) {
468 mInputDeviceListenerRegistered = false;
469 mIm.unregisterInputDeviceListener(this);
470 }
471 }
472
Jeff Brown7f6c2312012-04-13 20:38:38 -0700473 mInputDeviceVibrators.clear();
474 if (mVibrateInputDevicesSetting) {
475 int[] ids = mIm.getInputDeviceIds();
476 for (int i = 0; i < ids.length; i++) {
477 InputDevice device = mIm.getInputDevice(ids[i]);
478 Vibrator vibrator = device.getVibrator();
479 if (vibrator.hasVibrator()) {
480 mInputDeviceVibrators.add(vibrator);
481 }
482 }
483 }
484 }
485
486 startNextVibrationLocked();
487 }
488 }
489
490 @Override
491 public void onInputDeviceAdded(int deviceId) {
492 updateInputDeviceVibrators();
493 }
494
495 @Override
496 public void onInputDeviceChanged(int deviceId) {
497 updateInputDeviceVibrators();
498 }
499
500 @Override
501 public void onInputDeviceRemoved(int deviceId) {
502 updateInputDeviceVibrators();
503 }
504
505 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700506 // For now, we choose to ignore the presence of input devices that have vibrators
507 // when reporting whether the device has a vibrator. Applications often use this
508 // information to decide whether to enable certain features so they expect the
509 // result of hasVibrator() to be constant. For now, just report whether
510 // the device has a built-in vibrator.
511 //synchronized (mInputDeviceVibrators) {
512 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
513 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800514 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700515 }
516
John Spurlock7b414672014-07-18 13:02:39 -0400517 private void doVibratorOn(long millis, int uid, int usageHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700518 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800519 try {
520 mBatteryStatsService.noteVibratorOn(uid, millis);
521 mCurVibUid = uid;
522 } catch (RemoteException e) {
523 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700524 final int vibratorCount = mInputDeviceVibrators.size();
525 if (vibratorCount != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400526 final AudioAttributes attributes = new AudioAttributes.Builder().setUsage(usageHint)
527 .build();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700528 for (int i = 0; i < vibratorCount; i++) {
John Spurlock7b414672014-07-18 13:02:39 -0400529 mInputDeviceVibrators.get(i).vibrate(millis, attributes);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700530 }
531 } else {
532 vibratorOn(millis);
533 }
534 }
535 }
536
537 private void doVibratorOff() {
538 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800539 if (mCurVibUid >= 0) {
540 try {
541 mBatteryStatsService.noteVibratorOff(mCurVibUid);
542 } catch (RemoteException e) {
543 }
544 mCurVibUid = -1;
545 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700546 final int vibratorCount = mInputDeviceVibrators.size();
547 if (vibratorCount != 0) {
548 for (int i = 0; i < vibratorCount; i++) {
549 mInputDeviceVibrators.get(i).cancel();
550 }
551 } else {
552 vibratorOff();
553 }
554 }
555 }
556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400558 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700560
Patrick Scott18dd5f02009-07-02 11:31:12 -0400561 VibrateThread(Vibration vib) {
562 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700563 mTmpWorkSource.set(vib.mUid);
564 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 mWakeLock.acquire();
566 }
567
568 private void delay(long duration) {
569 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700570 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 do {
572 try {
573 this.wait(duration);
574 }
575 catch (InterruptedException e) {
576 }
577 if (mDone) {
578 break;
579 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700580 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 } while (duration > 0);
582 }
583 }
584
585 public void run() {
586 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
587 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800588 final long[] pattern = mVibration.mPattern;
589 final int len = pattern.length;
590 final int repeat = mVibration.mRepeat;
591 final int uid = mVibration.mUid;
John Spurlock7b414672014-07-18 13:02:39 -0400592 final int usageHint = mVibration.mUsageHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 long duration = 0;
595
596 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700597 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 if (index < len) {
599 duration += pattern[index++];
600 }
601
602 // sleep until it is time to start the vibrator
603 delay(duration);
604 if (mDone) {
605 break;
606 }
607
608 if (index < len) {
609 // read on-time duration and start the vibrator
610 // duration is saved for delay() at top of loop
611 duration = pattern[index++];
612 if (duration > 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400613 VibratorService.this.doVibratorOn(duration, uid, usageHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
615 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400616 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 break;
618 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400619 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 duration = 0;
621 }
622 }
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 mWakeLock.release();
625 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400626 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 if (mThread == this) {
628 mThread = null;
629 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400630 if (!mDone) {
631 // If this vibration finished naturally, start the next
632 // vibration.
633 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200634 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400635 startNextVibrationLocked();
636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
638 }
Jeff Brown969579b2014-05-20 19:29:29 -0700639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
Jeff Brown969579b2014-05-20 19:29:29 -0700642 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 public void onReceive(Context context, Intent intent) {
644 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400645 synchronized (mVibrations) {
Jeff Brown969579b2014-05-20 19:29:29 -0700646 // When the system is entering a non-interactive state, we want
647 // to cancel vibrations in case a misbehaving app has abandoned
648 // them. However it may happen that the system is currently playing
649 // haptic feedback as part of the transition. So we don't cancel
650 // system vibrations.
651 if (mCurrentVibration != null
652 && !mCurrentVibration.isSystemHapticFeedback()) {
653 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700654 }
655
Jeff Brown969579b2014-05-20 19:29:29 -0700656 // Clear all remaining vibrations.
657 Iterator<Vibration> it = mVibrations.iterator();
658 while (it.hasNext()) {
659 Vibration vibration = it.next();
660 if (vibration != mCurrentVibration) {
661 unlinkVibration(vibration);
662 it.remove();
663 }
664 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667 }
668 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669}