blob: fdaf55e4f9a4d6d9700563a2ed5efd8eb628f756 [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;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070045import android.media.AudioManager;
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 Spurlock1af30c72014-03-10 08:33:35 -040093 private final int mStreamHint;
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
Christoph Studer8fd7f1e2014-04-11 17:35:05 -040097 Vibration(IBinder token, long millis, int streamHint, int uid, String opPkg) {
98 this(token, millis, null, 0, streamHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -040099 }
100
John Spurlock1af30c72014-03-10 08:33:35 -0400101 Vibration(IBinder token, long[] pattern, int repeat, int streamHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400102 String opPkg) {
103 this(token, 0, pattern, repeat, streamHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400104 }
105
106 private Vibration(IBinder token, long millis, long[] pattern,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400107 int repeat, int streamHint, 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 Spurlock1af30c72014-03-10 08:33:35 -0400113 mStreamHint = streamHint;
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
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400223 public void vibrate(int uid, String opPkg, long milliseconds, int streamHint,
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
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400240 Vibration vib = new Vibration(token, milliseconds, streamHint, 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 Spurlock1af30c72014-03-10 08:33:35 -0400266 int streamHint, 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 Spurlock1af30c72014-03-10 08:33:35 -0400291 Vibration vib = new Vibration(token, pattern, repeat, streamHint, 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 {
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700375 if (mLowPowerMode && vib.mStreamHint != AudioManager.STREAM_RING) {
376 return;
377 }
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700378
John Spurlock1af30c72014-03-10 08:33:35 -0400379 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400380 vib.mStreamHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400381 if (mode == AppOpsManager.MODE_ALLOWED) {
382 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400383 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400384 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800385 if (mode != AppOpsManager.MODE_ALLOWED) {
386 if (mode == AppOpsManager.MODE_ERRORED) {
387 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
388 }
389 mH.post(mVibrationRunnable);
390 return;
391 }
392 } catch (RemoteException e) {
393 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400394 if (vib.mTimeout != 0) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400395 doVibratorOn(vib.mTimeout, vib.mUid, vib.mStreamHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400396 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
397 } else {
398 // mThread better be null here. doCancelVibrate should always be
399 // called before startNextVibrationLocked or startVibrationLocked.
400 mThread = new VibrateThread(vib);
401 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 }
403 }
404
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800405 private void reportFinishVibrationLocked() {
406 if (mCurrentVibration != null) {
407 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700408 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
409 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400410 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800411 } catch (RemoteException e) {
412 }
413 mCurrentVibration = null;
414 }
415 }
416
Patrick Scott18dd5f02009-07-02 11:31:12 -0400417 // Lock held on mVibrations
418 private Vibration removeVibrationLocked(IBinder token) {
419 ListIterator<Vibration> iter = mVibrations.listIterator(0);
420 while (iter.hasNext()) {
421 Vibration vib = iter.next();
422 if (vib.mToken == token) {
423 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200424 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400425 return vib;
426 }
427 }
428 // We might be looking for a simple vibration which is only stored in
429 // mCurrentVibration.
430 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200431 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400432 return mCurrentVibration;
433 }
434 return null;
435 }
436
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200437 private void unlinkVibration(Vibration vib) {
438 if (vib.mPattern != null) {
439 // If Vibration object has a pattern,
440 // the Vibration object has also been linkedToDeath.
441 vib.mToken.unlinkToDeath(vib, 0);
442 }
443 }
444
Jeff Brown7f6c2312012-04-13 20:38:38 -0700445 private void updateInputDeviceVibrators() {
446 synchronized (mVibrations) {
447 doCancelVibrateLocked();
448
449 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700450 mVibrateInputDevicesSetting = false;
451 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700452 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
453 mContext.getContentResolver(),
454 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700455 } catch (SettingNotFoundException snfe) {
456 }
457
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700458 mLowPowerMode = mPowerManagerInternal.getLowPowerModeEnabled();
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700459
Jeff Brown82065252012-04-16 13:19:05 -0700460 if (mVibrateInputDevicesSetting) {
461 if (!mInputDeviceListenerRegistered) {
462 mInputDeviceListenerRegistered = true;
463 mIm.registerInputDeviceListener(this, mH);
464 }
465 } else {
466 if (mInputDeviceListenerRegistered) {
467 mInputDeviceListenerRegistered = false;
468 mIm.unregisterInputDeviceListener(this);
469 }
470 }
471
Jeff Brown7f6c2312012-04-13 20:38:38 -0700472 mInputDeviceVibrators.clear();
473 if (mVibrateInputDevicesSetting) {
474 int[] ids = mIm.getInputDeviceIds();
475 for (int i = 0; i < ids.length; i++) {
476 InputDevice device = mIm.getInputDevice(ids[i]);
477 Vibrator vibrator = device.getVibrator();
478 if (vibrator.hasVibrator()) {
479 mInputDeviceVibrators.add(vibrator);
480 }
481 }
482 }
483 }
484
485 startNextVibrationLocked();
486 }
487 }
488
489 @Override
490 public void onInputDeviceAdded(int deviceId) {
491 updateInputDeviceVibrators();
492 }
493
494 @Override
495 public void onInputDeviceChanged(int deviceId) {
496 updateInputDeviceVibrators();
497 }
498
499 @Override
500 public void onInputDeviceRemoved(int deviceId) {
501 updateInputDeviceVibrators();
502 }
503
504 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700505 // For now, we choose to ignore the presence of input devices that have vibrators
506 // when reporting whether the device has a vibrator. Applications often use this
507 // information to decide whether to enable certain features so they expect the
508 // result of hasVibrator() to be constant. For now, just report whether
509 // the device has a built-in vibrator.
510 //synchronized (mInputDeviceVibrators) {
511 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
512 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800513 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700514 }
515
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400516 private void doVibratorOn(long millis, int uid, int streamHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700517 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800518 try {
519 mBatteryStatsService.noteVibratorOn(uid, millis);
520 mCurVibUid = uid;
521 } catch (RemoteException e) {
522 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700523 final int vibratorCount = mInputDeviceVibrators.size();
524 if (vibratorCount != 0) {
525 for (int i = 0; i < vibratorCount; i++) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400526 mInputDeviceVibrators.get(i).vibrate(millis, streamHint);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700527 }
528 } else {
529 vibratorOn(millis);
530 }
531 }
532 }
533
534 private void doVibratorOff() {
535 synchronized (mInputDeviceVibrators) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800536 if (mCurVibUid >= 0) {
537 try {
538 mBatteryStatsService.noteVibratorOff(mCurVibUid);
539 } catch (RemoteException e) {
540 }
541 mCurVibUid = -1;
542 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700543 final int vibratorCount = mInputDeviceVibrators.size();
544 if (vibratorCount != 0) {
545 for (int i = 0; i < vibratorCount; i++) {
546 mInputDeviceVibrators.get(i).cancel();
547 }
548 } else {
549 vibratorOff();
550 }
551 }
552 }
553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400555 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700557
Patrick Scott18dd5f02009-07-02 11:31:12 -0400558 VibrateThread(Vibration vib) {
559 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700560 mTmpWorkSource.set(vib.mUid);
561 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 mWakeLock.acquire();
563 }
564
565 private void delay(long duration) {
566 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700567 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 do {
569 try {
570 this.wait(duration);
571 }
572 catch (InterruptedException e) {
573 }
574 if (mDone) {
575 break;
576 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700577 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 } while (duration > 0);
579 }
580 }
581
582 public void run() {
583 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
584 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800585 final long[] pattern = mVibration.mPattern;
586 final int len = pattern.length;
587 final int repeat = mVibration.mRepeat;
588 final int uid = mVibration.mUid;
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400589 final int streamHint = mVibration.mStreamHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 long duration = 0;
592
593 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700594 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 if (index < len) {
596 duration += pattern[index++];
597 }
598
599 // sleep until it is time to start the vibrator
600 delay(duration);
601 if (mDone) {
602 break;
603 }
604
605 if (index < len) {
606 // read on-time duration and start the vibrator
607 // duration is saved for delay() at top of loop
608 duration = pattern[index++];
609 if (duration > 0) {
John Spurlockf9e1a0b2014-03-19 22:09:06 -0400610 VibratorService.this.doVibratorOn(duration, uid, streamHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 }
612 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400613 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 break;
615 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400616 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 duration = 0;
618 }
619 }
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 mWakeLock.release();
622 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400623 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 if (mThread == this) {
625 mThread = null;
626 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400627 if (!mDone) {
628 // If this vibration finished naturally, start the next
629 // vibration.
630 mVibrations.remove(mVibration);
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200631 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400632 startNextVibrationLocked();
633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 }
635 }
Jeff Brown969579b2014-05-20 19:29:29 -0700636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
Jeff Brown969579b2014-05-20 19:29:29 -0700639 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 public void onReceive(Context context, Intent intent) {
641 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400642 synchronized (mVibrations) {
Jeff Brown969579b2014-05-20 19:29:29 -0700643 // When the system is entering a non-interactive state, we want
644 // to cancel vibrations in case a misbehaving app has abandoned
645 // them. However it may happen that the system is currently playing
646 // haptic feedback as part of the transition. So we don't cancel
647 // system vibrations.
648 if (mCurrentVibration != null
649 && !mCurrentVibration.isSystemHapticFeedback()) {
650 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700651 }
652
Jeff Brown969579b2014-05-20 19:29:29 -0700653 // Clear all remaining vibrations.
654 Iterator<Vibration> it = mVibrations.iterator();
655 while (it.hasNext()) {
656 Vibration vibration = it.next();
657 if (vibration != mCurrentVibration) {
658 unlinkVibration(vibration);
659 it.remove();
660 }
661 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
664 }
665 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666}