blob: 30f4dce310ca07ced20ea4e3bc441b954d6e800d [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
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -070050import java.io.FileDescriptor;
51import java.io.PrintWriter;
Jeff Brown7f6c2312012-04-13 20:38:38 -070052import java.util.ArrayList;
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -070053import java.util.Arrays;
Jeff Brown969579b2014-05-20 19:29:29 -070054import java.util.Iterator;
Patrick Scott18dd5f02009-07-02 11:31:12 -040055import java.util.LinkedList;
56import java.util.ListIterator;
57
Jeff Brown7f6c2312012-04-13 20:38:38 -070058public class VibratorService extends IVibratorService.Stub
59 implements InputManager.InputDeviceListener {
Mike Lockwood3a322132009-11-24 00:30:52 -050060 private static final String TAG = "VibratorService";
Jeff Brown82379ba2014-07-25 19:03:28 -070061 private static final boolean DEBUG = false;
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050062
Patrick Scott18dd5f02009-07-02 11:31:12 -040063 private final LinkedList<Vibration> mVibrations;
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -070064 private final LinkedList<VibrationInfo> mPreviousVibrations;
65 private final int mPreviousVibrationsLimit;
Patrick Scott18dd5f02009-07-02 11:31:12 -040066 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070067 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070068 private final Handler mH = new Handler();
69
70 private final Context mContext;
71 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080072 private final IAppOpsService mAppOpsService;
73 private final IBatteryStats mBatteryStatsService;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070074 private PowerManagerInternal mPowerManagerInternal;
Jeff Brown7f6c2312012-04-13 20:38:38 -070075 private InputManager mIm;
76
77 volatile VibrateThread mThread;
78
79 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
80 // to be acquired
81 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
82 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
83 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
84
Dianne Hackborna06de0f2012-12-11 16:34:47 -080085 private int mCurVibUid = -1;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070086 private boolean mLowPowerMode;
87 private SettingsObserver mSettingObserver;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080088
Jeff Brown7f6c2312012-04-13 20:38:38 -070089 native static boolean vibratorExists();
90 native static void vibratorOn(long milliseconds);
91 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040092
Patrick Scott18dd5f02009-07-02 11:31:12 -040093 private class Vibration implements IBinder.DeathRecipient {
94 private final IBinder mToken;
95 private final long mTimeout;
96 private final long mStartTime;
97 private final long[] mPattern;
98 private final int mRepeat;
John Spurlock7b414672014-07-18 13:02:39 -040099 private final int mUsageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700100 private final int mUid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400101 private final String mOpPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400102
John Spurlock7b414672014-07-18 13:02:39 -0400103 Vibration(IBinder token, long millis, int usageHint, int uid, String opPkg) {
104 this(token, millis, null, 0, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400105 }
106
John Spurlock7b414672014-07-18 13:02:39 -0400107 Vibration(IBinder token, long[] pattern, int repeat, int usageHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400108 String opPkg) {
John Spurlock7b414672014-07-18 13:02:39 -0400109 this(token, 0, pattern, repeat, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400110 }
111
112 private Vibration(IBinder token, long millis, long[] pattern,
John Spurlock7b414672014-07-18 13:02:39 -0400113 int repeat, int usageHint, int uid, String opPkg) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400114 mToken = token;
115 mTimeout = millis;
116 mStartTime = SystemClock.uptimeMillis();
117 mPattern = pattern;
118 mRepeat = repeat;
John Spurlock7b414672014-07-18 13:02:39 -0400119 mUsageHint = usageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700120 mUid = uid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400121 mOpPkg = opPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400122 }
123
124 public void binderDied() {
125 synchronized (mVibrations) {
126 mVibrations.remove(this);
127 if (this == mCurrentVibration) {
128 doCancelVibrateLocked();
129 startNextVibrationLocked();
130 }
131 }
132 }
133
134 public boolean hasLongerTimeout(long millis) {
135 if (mTimeout == 0) {
136 // This is a pattern, return false to play the simple
137 // vibration.
138 return false;
139 }
140 if ((mStartTime + mTimeout)
141 < (SystemClock.uptimeMillis() + millis)) {
142 // If this vibration will end before the time passed in, let
143 // the new vibration play.
144 return false;
145 }
146 return true;
147 }
Jeff Brown969579b2014-05-20 19:29:29 -0700148
149 public boolean isSystemHapticFeedback() {
150 return (mUid == Process.SYSTEM_UID || mUid == 0) && mRepeat < 0;
151 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400152 }
153
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700154 private static class VibrationInfo {
155 long timeout;
156 long startTime;
157 long[] pattern;
158 int repeat;
159 int usageHint;
160 int uid;
161 String opPkg;
162
163 public VibrationInfo(long timeout, long startTime, long[] pattern, int repeat,
164 int usageHint, int uid, String opPkg) {
165 this.timeout = timeout;
166 this.startTime = startTime;
167 this.pattern = pattern;
168 this.repeat = repeat;
169 this.usageHint = usageHint;
170 this.uid = uid;
171 this.opPkg = opPkg;
172 }
173
174 @Override
175 public String toString() {
176 return new StringBuilder()
177 .append("timeout: ")
178 .append(timeout)
179 .append(", startTime: ")
180 .append(startTime)
181 .append(", pattern: ")
182 .append(Arrays.toString(pattern))
183 .append(", repeat: ")
184 .append(repeat)
185 .append(", usageHint: ")
186 .append(usageHint)
187 .append(", uid: ")
188 .append(uid)
189 .append(", opPkg: ")
190 .append(opPkg)
191 .toString();
192 }
193 }
194
Mike Lockwood3a322132009-11-24 00:30:52 -0500195 VibratorService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 // Reset the hardware to a default state, in case this is a runtime
197 // restart instead of a fresh boot.
198 vibratorOff();
199
200 mContext = context;
201 PowerManager pm = (PowerManager)context.getSystemService(
202 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700203 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 mWakeLock.setReferenceCounted(true);
205
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800206 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700207 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
208 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800209
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700210 mPreviousVibrationsLimit = mContext.getResources().getInteger(
211 com.android.internal.R.integer.config_previousVibrationsDumpLimit);
212
213 mVibrations = new LinkedList<>();
214 mPreviousVibrations = new LinkedList<>();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 IntentFilter filter = new IntentFilter();
217 filter.addAction(Intent.ACTION_SCREEN_OFF);
218 context.registerReceiver(mIntentReceiver, filter);
219 }
220
Jeff Brown7f6c2312012-04-13 20:38:38 -0700221 public void systemReady() {
222 mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE);
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700223 mSettingObserver = new SettingsObserver(mH);
Jeff Brownd4935962012-09-25 13:27:20 -0700224
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700225 mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
226 mPowerManagerInternal.registerLowPowerModeObserver(
227 new PowerManagerInternal.LowPowerModeListener() {
228 @Override
229 public void onLowPowerModeChanged(boolean enabled) {
230 updateInputDeviceVibrators();
231 }
232 });
233
Jeff Brown7f6c2312012-04-13 20:38:38 -0700234 mContext.getContentResolver().registerContentObserver(
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700235 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES),
236 true, mSettingObserver, UserHandle.USER_ALL);
237
Jeff Brownd4935962012-09-25 13:27:20 -0700238 mContext.registerReceiver(new BroadcastReceiver() {
239 @Override
240 public void onReceive(Context context, Intent intent) {
241 updateInputDeviceVibrators();
242 }
243 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
244
Jeff Brown82065252012-04-16 13:19:05 -0700245 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700246 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700247
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700248 private final class SettingsObserver extends ContentObserver {
249 public SettingsObserver(Handler handler) {
250 super(handler);
251 }
252
253 @Override
254 public void onChange(boolean SelfChange) {
255 updateInputDeviceVibrators();
256 }
257 }
258
Jeff Brown82379ba2014-07-25 19:03:28 -0700259 @Override // Binder call
Jeff Brown7f6c2312012-04-13 20:38:38 -0700260 public boolean hasVibrator() {
261 return doVibratorExists();
262 }
263
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800264 private void verifyIncomingUid(int uid) {
265 if (uid == Binder.getCallingUid()) {
266 return;
267 }
268 if (Binder.getCallingPid() == Process.myPid()) {
269 return;
270 }
271 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
272 Binder.getCallingPid(), Binder.getCallingUid(), null);
273 }
274
Jeff Brown82379ba2014-07-25 19:03:28 -0700275 @Override // Binder call
John Spurlock7b414672014-07-18 13:02:39 -0400276 public void vibrate(int uid, String opPkg, long milliseconds, int usageHint,
John Spurlock1af30c72014-03-10 08:33:35 -0400277 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700278 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
279 != PackageManager.PERMISSION_GRANTED) {
280 throw new SecurityException("Requires VIBRATE permission");
281 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800282 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400283 // We're running in the system server so we cannot crash. Check for a
284 // timeout of 0 or negative. This will ensure that a vibration has
285 // either a timeout of > 0 or a non-null pattern.
286 if (milliseconds <= 0 || (mCurrentVibration != null
287 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400288 // Ignore this vibration since the current vibration will play for
289 // longer than milliseconds.
290 return;
291 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700292
Jeff Brown82379ba2014-07-25 19:03:28 -0700293 if (DEBUG) {
294 Slog.d(TAG, "Vibrating for " + milliseconds + " ms.");
295 }
296
John Spurlock7b414672014-07-18 13:02:39 -0400297 Vibration vib = new Vibration(token, milliseconds, usageHint, uid, opPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800298
299 final long ident = Binder.clearCallingIdentity();
300 try {
301 synchronized (mVibrations) {
302 removeVibrationLocked(token);
303 doCancelVibrateLocked();
304 mCurrentVibration = vib;
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700305 addToPreviousVibrationsLocked(vib);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800306 startVibrationLocked(vib);
307 }
308 } finally {
309 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
312
313 private boolean isAll0(long[] pattern) {
314 int N = pattern.length;
315 for (int i = 0; i < N; i++) {
316 if (pattern[i] != 0) {
317 return false;
318 }
319 }
320 return true;
321 }
322
Jeff Brown82379ba2014-07-25 19:03:28 -0700323 @Override // Binder call
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800324 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock7b414672014-07-18 13:02:39 -0400325 int usageHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
327 != PackageManager.PERMISSION_GRANTED) {
328 throw new SecurityException("Requires VIBRATE permission");
329 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800330 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 // so wakelock calls will succeed
332 long identity = Binder.clearCallingIdentity();
333 try {
Jeff Brown82379ba2014-07-25 19:03:28 -0700334 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 String s = "";
336 int N = pattern.length;
337 for (int i=0; i<N; i++) {
338 s += " " + pattern[i];
339 }
Jeff Brown82379ba2014-07-25 19:03:28 -0700340 Slog.d(TAG, "Vibrating with pattern:" + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 }
342
343 // we're running in the server so we can't fail
344 if (pattern == null || pattern.length == 0
345 || isAll0(pattern)
346 || repeat >= pattern.length || token == null) {
347 return;
348 }
349
John Spurlock7b414672014-07-18 13:02:39 -0400350 Vibration vib = new Vibration(token, pattern, repeat, usageHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400351 try {
352 token.linkToDeath(vib, 0);
353 } catch (RemoteException e) {
354 return;
355 }
356
357 synchronized (mVibrations) {
358 removeVibrationLocked(token);
359 doCancelVibrateLocked();
360 if (repeat >= 0) {
361 mVibrations.addFirst(vib);
362 startNextVibrationLocked();
363 } else {
364 // A negative repeat means that this pattern is not meant
365 // to repeat. Treat it like a simple vibration.
366 mCurrentVibration = vib;
367 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700369 addToPreviousVibrationsLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
371 }
372 finally {
373 Binder.restoreCallingIdentity(identity);
374 }
375 }
376
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700377 private void addToPreviousVibrationsLocked(Vibration vib) {
378 if (mPreviousVibrations.size() > mPreviousVibrationsLimit) {
379 mPreviousVibrations.removeFirst();
380 }
381 mPreviousVibrations.addLast(new VibratorService.VibrationInfo(vib.mTimeout, vib.mStartTime,
382 vib.mPattern, vib.mRepeat, vib.mUsageHint, vib.mUid, vib.mOpPkg));
383 }
384
Jeff Brown82379ba2014-07-25 19:03:28 -0700385 @Override // Binder call
Patrick Scott18dd5f02009-07-02 11:31:12 -0400386 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 mContext.enforceCallingOrSelfPermission(
388 android.Manifest.permission.VIBRATE,
389 "cancelVibrate");
390
391 // so wakelock calls will succeed
392 long identity = Binder.clearCallingIdentity();
393 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400394 synchronized (mVibrations) {
395 final Vibration vib = removeVibrationLocked(token);
396 if (vib == mCurrentVibration) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700397 if (DEBUG) {
398 Slog.d(TAG, "Canceling vibration.");
399 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400400 doCancelVibrateLocked();
401 startNextVibrationLocked();
402 }
403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 }
405 finally {
406 Binder.restoreCallingIdentity(identity);
407 }
408 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700409
Patrick Scott18dd5f02009-07-02 11:31:12 -0400410 private final Runnable mVibrationRunnable = new Runnable() {
Jeff Brown82379ba2014-07-25 19:03:28 -0700411 @Override
Patrick Scott18dd5f02009-07-02 11:31:12 -0400412 public void run() {
413 synchronized (mVibrations) {
414 doCancelVibrateLocked();
415 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400417 }
418 };
419
420 // Lock held on mVibrations
421 private void doCancelVibrateLocked() {
422 if (mThread != null) {
423 synchronized (mThread) {
424 mThread.mDone = true;
425 mThread.notify();
426 }
427 mThread = null;
428 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700429 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400430 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800431 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400432 }
433
434 // Lock held on mVibrations
435 private void startNextVibrationLocked() {
436 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800437 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200438 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400439 return;
440 }
441 mCurrentVibration = mVibrations.getFirst();
442 startVibrationLocked(mCurrentVibration);
443 }
444
445 // Lock held on mVibrations
446 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800447 try {
John Spurlock7b414672014-07-18 13:02:39 -0400448 if (mLowPowerMode
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -0700449 && vib.mUsageHint != AudioAttributes.USAGE_NOTIFICATION_RINGTONE) {
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700450 return;
451 }
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700452
John Spurlock1af30c72014-03-10 08:33:35 -0400453 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
John Spurlock7b414672014-07-18 13:02:39 -0400454 vib.mUsageHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400455 if (mode == AppOpsManager.MODE_ALLOWED) {
456 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400457 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400458 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800459 if (mode != AppOpsManager.MODE_ALLOWED) {
460 if (mode == AppOpsManager.MODE_ERRORED) {
461 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
462 }
463 mH.post(mVibrationRunnable);
464 return;
465 }
466 } catch (RemoteException e) {
467 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400468 if (vib.mTimeout != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400469 doVibratorOn(vib.mTimeout, vib.mUid, vib.mUsageHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400470 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
471 } else {
472 // mThread better be null here. doCancelVibrate should always be
473 // called before startNextVibrationLocked or startVibrationLocked.
474 mThread = new VibrateThread(vib);
475 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477 }
478
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800479 private void reportFinishVibrationLocked() {
480 if (mCurrentVibration != null) {
481 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700482 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
483 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400484 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800485 } catch (RemoteException e) {
486 }
487 mCurrentVibration = null;
488 }
489 }
490
Patrick Scott18dd5f02009-07-02 11:31:12 -0400491 // Lock held on mVibrations
492 private Vibration removeVibrationLocked(IBinder token) {
493 ListIterator<Vibration> iter = mVibrations.listIterator(0);
494 while (iter.hasNext()) {
495 Vibration vib = iter.next();
496 if (vib.mToken == token) {
497 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200498 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400499 return vib;
500 }
501 }
502 // We might be looking for a simple vibration which is only stored in
503 // mCurrentVibration.
504 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200505 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400506 return mCurrentVibration;
507 }
508 return null;
509 }
510
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200511 private void unlinkVibration(Vibration vib) {
512 if (vib.mPattern != null) {
513 // If Vibration object has a pattern,
514 // the Vibration object has also been linkedToDeath.
515 vib.mToken.unlinkToDeath(vib, 0);
516 }
517 }
518
Jeff Brown7f6c2312012-04-13 20:38:38 -0700519 private void updateInputDeviceVibrators() {
520 synchronized (mVibrations) {
521 doCancelVibrateLocked();
522
523 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700524 mVibrateInputDevicesSetting = false;
525 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700526 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
527 mContext.getContentResolver(),
528 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700529 } catch (SettingNotFoundException snfe) {
530 }
531
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700532 mLowPowerMode = mPowerManagerInternal.getLowPowerModeEnabled();
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700533
Jeff Brown82065252012-04-16 13:19:05 -0700534 if (mVibrateInputDevicesSetting) {
535 if (!mInputDeviceListenerRegistered) {
536 mInputDeviceListenerRegistered = true;
537 mIm.registerInputDeviceListener(this, mH);
538 }
539 } else {
540 if (mInputDeviceListenerRegistered) {
541 mInputDeviceListenerRegistered = false;
542 mIm.unregisterInputDeviceListener(this);
543 }
544 }
545
Jeff Brown7f6c2312012-04-13 20:38:38 -0700546 mInputDeviceVibrators.clear();
547 if (mVibrateInputDevicesSetting) {
548 int[] ids = mIm.getInputDeviceIds();
549 for (int i = 0; i < ids.length; i++) {
550 InputDevice device = mIm.getInputDevice(ids[i]);
551 Vibrator vibrator = device.getVibrator();
552 if (vibrator.hasVibrator()) {
553 mInputDeviceVibrators.add(vibrator);
554 }
555 }
556 }
557 }
558
559 startNextVibrationLocked();
560 }
561 }
562
563 @Override
564 public void onInputDeviceAdded(int deviceId) {
565 updateInputDeviceVibrators();
566 }
567
568 @Override
569 public void onInputDeviceChanged(int deviceId) {
570 updateInputDeviceVibrators();
571 }
572
573 @Override
574 public void onInputDeviceRemoved(int deviceId) {
575 updateInputDeviceVibrators();
576 }
577
578 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700579 // For now, we choose to ignore the presence of input devices that have vibrators
580 // when reporting whether the device has a vibrator. Applications often use this
581 // information to decide whether to enable certain features so they expect the
582 // result of hasVibrator() to be constant. For now, just report whether
583 // the device has a built-in vibrator.
584 //synchronized (mInputDeviceVibrators) {
585 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
586 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800587 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700588 }
589
John Spurlock7b414672014-07-18 13:02:39 -0400590 private void doVibratorOn(long millis, int uid, int usageHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700591 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700592 if (DEBUG) {
593 Slog.d(TAG, "Turning vibrator on for " + millis + " ms.");
594 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800595 try {
596 mBatteryStatsService.noteVibratorOn(uid, millis);
597 mCurVibUid = uid;
598 } catch (RemoteException e) {
599 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700600 final int vibratorCount = mInputDeviceVibrators.size();
601 if (vibratorCount != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400602 final AudioAttributes attributes = new AudioAttributes.Builder().setUsage(usageHint)
603 .build();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700604 for (int i = 0; i < vibratorCount; i++) {
John Spurlock7b414672014-07-18 13:02:39 -0400605 mInputDeviceVibrators.get(i).vibrate(millis, attributes);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700606 }
607 } else {
608 vibratorOn(millis);
609 }
610 }
611 }
612
613 private void doVibratorOff() {
614 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700615 if (DEBUG) {
616 Slog.d(TAG, "Turning vibrator off.");
617 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800618 if (mCurVibUid >= 0) {
619 try {
620 mBatteryStatsService.noteVibratorOff(mCurVibUid);
621 } catch (RemoteException e) {
622 }
623 mCurVibUid = -1;
624 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700625 final int vibratorCount = mInputDeviceVibrators.size();
626 if (vibratorCount != 0) {
627 for (int i = 0; i < vibratorCount; i++) {
628 mInputDeviceVibrators.get(i).cancel();
629 }
630 } else {
631 vibratorOff();
632 }
633 }
634 }
635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400637 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700639
Patrick Scott18dd5f02009-07-02 11:31:12 -0400640 VibrateThread(Vibration vib) {
641 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700642 mTmpWorkSource.set(vib.mUid);
643 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 mWakeLock.acquire();
645 }
646
647 private void delay(long duration) {
648 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700649 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 do {
651 try {
652 this.wait(duration);
653 }
654 catch (InterruptedException e) {
655 }
656 if (mDone) {
657 break;
658 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700659 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 } while (duration > 0);
661 }
662 }
663
664 public void run() {
665 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
666 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800667 final long[] pattern = mVibration.mPattern;
668 final int len = pattern.length;
669 final int repeat = mVibration.mRepeat;
670 final int uid = mVibration.mUid;
John Spurlock7b414672014-07-18 13:02:39 -0400671 final int usageHint = mVibration.mUsageHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 long duration = 0;
674
675 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700676 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 if (index < len) {
678 duration += pattern[index++];
679 }
680
681 // sleep until it is time to start the vibrator
682 delay(duration);
683 if (mDone) {
684 break;
685 }
686
687 if (index < len) {
688 // read on-time duration and start the vibrator
689 // duration is saved for delay() at top of loop
690 duration = pattern[index++];
691 if (duration > 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400692 VibratorService.this.doVibratorOn(duration, uid, usageHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
694 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400695 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 break;
697 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400698 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 duration = 0;
700 }
701 }
702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 mWakeLock.release();
704 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400705 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 if (mThread == this) {
707 mThread = null;
708 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400709 if (!mDone) {
710 // If this vibration finished naturally, start the next
711 // vibration.
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200712 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400713 startNextVibrationLocked();
714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
716 }
Jeff Brown969579b2014-05-20 19:29:29 -0700717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
Jeff Brown969579b2014-05-20 19:29:29 -0700720 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 public void onReceive(Context context, Intent intent) {
722 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400723 synchronized (mVibrations) {
Jeff Brown969579b2014-05-20 19:29:29 -0700724 // When the system is entering a non-interactive state, we want
725 // to cancel vibrations in case a misbehaving app has abandoned
726 // them. However it may happen that the system is currently playing
727 // haptic feedback as part of the transition. So we don't cancel
728 // system vibrations.
729 if (mCurrentVibration != null
730 && !mCurrentVibration.isSystemHapticFeedback()) {
731 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700732 }
733
Jeff Brown969579b2014-05-20 19:29:29 -0700734 // Clear all remaining vibrations.
735 Iterator<Vibration> it = mVibrations.iterator();
736 while (it.hasNext()) {
737 Vibration vibration = it.next();
738 if (vibration != mCurrentVibration) {
739 unlinkVibration(vibration);
740 it.remove();
741 }
742 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745 }
746 };
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700747
748 @Override
749 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
750 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
751 != PackageManager.PERMISSION_GRANTED) {
752
753 pw.println("Permission Denial: can't dump vibrator service from from pid="
754 + Binder.getCallingPid()
755 + ", uid=" + Binder.getCallingUid());
756 return;
757 }
758 pw.println("Previous vibrations:");
759 synchronized (mVibrations) {
760 for (VibrationInfo info : mPreviousVibrations) {
761 pw.print(" ");
762 pw.println(info.toString());
763 }
764 }
765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766}