blob: 5ba07cf2c8a6d798ddd7c33006e628059dc71144 [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;
Jorim Jaggi18f18ae2015-09-10 15:48:21 -070062 private static final String SYSTEM_UI_PACKAGE = "com.android.systemui";
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -050063
Patrick Scott18dd5f02009-07-02 11:31:12 -040064 private final LinkedList<Vibration> mVibrations;
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -070065 private final LinkedList<VibrationInfo> mPreviousVibrations;
66 private final int mPreviousVibrationsLimit;
Patrick Scott18dd5f02009-07-02 11:31:12 -040067 private Vibration mCurrentVibration;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070068 private final WorkSource mTmpWorkSource = new WorkSource();
Jeff Brown7f6c2312012-04-13 20:38:38 -070069 private final Handler mH = new Handler();
70
71 private final Context mContext;
72 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080073 private final IAppOpsService mAppOpsService;
74 private final IBatteryStats mBatteryStatsService;
Dianne Hackborneb94fa72014-06-03 17:48:12 -070075 private PowerManagerInternal mPowerManagerInternal;
Jeff Brown7f6c2312012-04-13 20:38:38 -070076 private InputManager mIm;
77
78 volatile VibrateThread mThread;
79
80 // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are
81 // to be acquired
82 private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>();
83 private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators
84 private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators
85
Dianne Hackborna06de0f2012-12-11 16:34:47 -080086 private int mCurVibUid = -1;
Ruchi Kandoi13b03af2014-05-07 20:10:32 -070087 private boolean mLowPowerMode;
88 private SettingsObserver mSettingObserver;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080089
Jeff Brown7f6c2312012-04-13 20:38:38 -070090 native static boolean vibratorExists();
Vincent Beckere6904fb2012-08-10 14:17:33 +020091 native static void vibratorInit();
Jeff Brown7f6c2312012-04-13 20:38:38 -070092 native static void vibratorOn(long milliseconds);
93 native static void vibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -040094
Patrick Scott18dd5f02009-07-02 11:31:12 -040095 private class Vibration implements IBinder.DeathRecipient {
96 private final IBinder mToken;
97 private final long mTimeout;
98 private final long mStartTime;
99 private final long[] mPattern;
100 private final int mRepeat;
John Spurlock7b414672014-07-18 13:02:39 -0400101 private final int mUsageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700102 private final int mUid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400103 private final String mOpPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400104
John Spurlock7b414672014-07-18 13:02:39 -0400105 Vibration(IBinder token, long millis, int usageHint, int uid, String opPkg) {
106 this(token, millis, null, 0, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400107 }
108
John Spurlock7b414672014-07-18 13:02:39 -0400109 Vibration(IBinder token, long[] pattern, int repeat, int usageHint, int uid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400110 String opPkg) {
John Spurlock7b414672014-07-18 13:02:39 -0400111 this(token, 0, pattern, repeat, usageHint, uid, opPkg);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400112 }
113
114 private Vibration(IBinder token, long millis, long[] pattern,
John Spurlock7b414672014-07-18 13:02:39 -0400115 int repeat, int usageHint, int uid, String opPkg) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400116 mToken = token;
117 mTimeout = millis;
118 mStartTime = SystemClock.uptimeMillis();
119 mPattern = pattern;
120 mRepeat = repeat;
John Spurlock7b414672014-07-18 13:02:39 -0400121 mUsageHint = usageHint;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700122 mUid = uid;
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400123 mOpPkg = opPkg;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400124 }
125
126 public void binderDied() {
127 synchronized (mVibrations) {
128 mVibrations.remove(this);
129 if (this == mCurrentVibration) {
130 doCancelVibrateLocked();
131 startNextVibrationLocked();
132 }
133 }
134 }
135
136 public boolean hasLongerTimeout(long millis) {
137 if (mTimeout == 0) {
138 // This is a pattern, return false to play the simple
139 // vibration.
140 return false;
141 }
142 if ((mStartTime + mTimeout)
143 < (SystemClock.uptimeMillis() + millis)) {
144 // If this vibration will end before the time passed in, let
145 // the new vibration play.
146 return false;
147 }
148 return true;
149 }
Jeff Brown969579b2014-05-20 19:29:29 -0700150
151 public boolean isSystemHapticFeedback() {
Jorim Jaggi18f18ae2015-09-10 15:48:21 -0700152 return (mUid == Process.SYSTEM_UID || mUid == 0 || SYSTEM_UI_PACKAGE.equals(mOpPkg))
153 && mRepeat < 0;
Jeff Brown969579b2014-05-20 19:29:29 -0700154 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400155 }
156
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700157 private static class VibrationInfo {
158 long timeout;
159 long startTime;
160 long[] pattern;
161 int repeat;
162 int usageHint;
163 int uid;
164 String opPkg;
165
166 public VibrationInfo(long timeout, long startTime, long[] pattern, int repeat,
167 int usageHint, int uid, String opPkg) {
168 this.timeout = timeout;
169 this.startTime = startTime;
170 this.pattern = pattern;
171 this.repeat = repeat;
172 this.usageHint = usageHint;
173 this.uid = uid;
174 this.opPkg = opPkg;
175 }
176
177 @Override
178 public String toString() {
179 return new StringBuilder()
180 .append("timeout: ")
181 .append(timeout)
182 .append(", startTime: ")
183 .append(startTime)
184 .append(", pattern: ")
185 .append(Arrays.toString(pattern))
186 .append(", repeat: ")
187 .append(repeat)
188 .append(", usageHint: ")
189 .append(usageHint)
190 .append(", uid: ")
191 .append(uid)
192 .append(", opPkg: ")
193 .append(opPkg)
194 .toString();
195 }
196 }
197
Mike Lockwood3a322132009-11-24 00:30:52 -0500198 VibratorService(Context context) {
Vincent Beckere6904fb2012-08-10 14:17:33 +0200199 vibratorInit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 // Reset the hardware to a default state, in case this is a runtime
201 // restart instead of a fresh boot.
202 vibratorOff();
203
204 mContext = context;
205 PowerManager pm = (PowerManager)context.getSystemService(
206 Context.POWER_SERVICE);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700207 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 mWakeLock.setReferenceCounted(true);
209
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800210 mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700211 mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
212 BatteryStats.SERVICE_NAME));
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800213
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700214 mPreviousVibrationsLimit = mContext.getResources().getInteger(
215 com.android.internal.R.integer.config_previousVibrationsDumpLimit);
216
217 mVibrations = new LinkedList<>();
218 mPreviousVibrations = new LinkedList<>();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 IntentFilter filter = new IntentFilter();
221 filter.addAction(Intent.ACTION_SCREEN_OFF);
222 context.registerReceiver(mIntentReceiver, filter);
223 }
224
Jeff Brown7f6c2312012-04-13 20:38:38 -0700225 public void systemReady() {
Yohei Yukawa8ce2a532015-11-25 20:35:04 -0800226 mIm = mContext.getSystemService(InputManager.class);
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700227 mSettingObserver = new SettingsObserver(mH);
Jeff Brownd4935962012-09-25 13:27:20 -0700228
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700229 mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
230 mPowerManagerInternal.registerLowPowerModeObserver(
231 new PowerManagerInternal.LowPowerModeListener() {
232 @Override
233 public void onLowPowerModeChanged(boolean enabled) {
234 updateInputDeviceVibrators();
235 }
236 });
237
Jeff Brown7f6c2312012-04-13 20:38:38 -0700238 mContext.getContentResolver().registerContentObserver(
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700239 Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES),
240 true, mSettingObserver, UserHandle.USER_ALL);
241
Jeff Brownd4935962012-09-25 13:27:20 -0700242 mContext.registerReceiver(new BroadcastReceiver() {
243 @Override
244 public void onReceive(Context context, Intent intent) {
245 updateInputDeviceVibrators();
246 }
247 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH);
248
Jeff Brown82065252012-04-16 13:19:05 -0700249 updateInputDeviceVibrators();
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700250 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700251
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700252 private final class SettingsObserver extends ContentObserver {
253 public SettingsObserver(Handler handler) {
254 super(handler);
255 }
256
257 @Override
258 public void onChange(boolean SelfChange) {
259 updateInputDeviceVibrators();
260 }
261 }
262
Jeff Brown82379ba2014-07-25 19:03:28 -0700263 @Override // Binder call
Jeff Brown7f6c2312012-04-13 20:38:38 -0700264 public boolean hasVibrator() {
265 return doVibratorExists();
266 }
267
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800268 private void verifyIncomingUid(int uid) {
269 if (uid == Binder.getCallingUid()) {
270 return;
271 }
272 if (Binder.getCallingPid() == Process.myPid()) {
273 return;
274 }
275 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
276 Binder.getCallingPid(), Binder.getCallingUid(), null);
277 }
278
Jeff Brown82379ba2014-07-25 19:03:28 -0700279 @Override // Binder call
John Spurlock7b414672014-07-18 13:02:39 -0400280 public void vibrate(int uid, String opPkg, long milliseconds, int usageHint,
John Spurlock1af30c72014-03-10 08:33:35 -0400281 IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700282 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
283 != PackageManager.PERMISSION_GRANTED) {
284 throw new SecurityException("Requires VIBRATE permission");
285 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800286 verifyIncomingUid(uid);
Patrick Scott24f10762009-08-19 09:03:56 -0400287 // We're running in the system server so we cannot crash. Check for a
288 // timeout of 0 or negative. This will ensure that a vibration has
289 // either a timeout of > 0 or a non-null pattern.
290 if (milliseconds <= 0 || (mCurrentVibration != null
291 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400292 // Ignore this vibration since the current vibration will play for
293 // longer than milliseconds.
294 return;
295 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700296
Jeff Brown82379ba2014-07-25 19:03:28 -0700297 if (DEBUG) {
298 Slog.d(TAG, "Vibrating for " + milliseconds + " ms.");
299 }
300
John Spurlock7b414672014-07-18 13:02:39 -0400301 Vibration vib = new Vibration(token, milliseconds, usageHint, uid, opPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800302
303 final long ident = Binder.clearCallingIdentity();
304 try {
305 synchronized (mVibrations) {
306 removeVibrationLocked(token);
307 doCancelVibrateLocked();
308 mCurrentVibration = vib;
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700309 addToPreviousVibrationsLocked(vib);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800310 startVibrationLocked(vib);
311 }
312 } finally {
313 Binder.restoreCallingIdentity(ident);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316
317 private boolean isAll0(long[] pattern) {
318 int N = pattern.length;
319 for (int i = 0; i < N; i++) {
320 if (pattern[i] != 0) {
321 return false;
322 }
323 }
324 return true;
325 }
326
Jeff Brown82379ba2014-07-25 19:03:28 -0700327 @Override // Binder call
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800328 public void vibratePattern(int uid, String packageName, long[] pattern, int repeat,
John Spurlock7b414672014-07-18 13:02:39 -0400329 int usageHint, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
331 != PackageManager.PERMISSION_GRANTED) {
332 throw new SecurityException("Requires VIBRATE permission");
333 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800334 verifyIncomingUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 // so wakelock calls will succeed
336 long identity = Binder.clearCallingIdentity();
337 try {
Jeff Brown82379ba2014-07-25 19:03:28 -0700338 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 String s = "";
340 int N = pattern.length;
341 for (int i=0; i<N; i++) {
342 s += " " + pattern[i];
343 }
Jeff Brown82379ba2014-07-25 19:03:28 -0700344 Slog.d(TAG, "Vibrating with pattern:" + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346
347 // we're running in the server so we can't fail
348 if (pattern == null || pattern.length == 0
349 || isAll0(pattern)
350 || repeat >= pattern.length || token == null) {
351 return;
352 }
353
John Spurlock7b414672014-07-18 13:02:39 -0400354 Vibration vib = new Vibration(token, pattern, repeat, usageHint, uid, packageName);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400355 try {
356 token.linkToDeath(vib, 0);
357 } catch (RemoteException e) {
358 return;
359 }
360
361 synchronized (mVibrations) {
362 removeVibrationLocked(token);
363 doCancelVibrateLocked();
364 if (repeat >= 0) {
365 mVibrations.addFirst(vib);
366 startNextVibrationLocked();
367 } else {
368 // A negative repeat means that this pattern is not meant
369 // to repeat. Treat it like a simple vibration.
370 mCurrentVibration = vib;
371 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700373 addToPreviousVibrationsLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
375 }
376 finally {
377 Binder.restoreCallingIdentity(identity);
378 }
379 }
380
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700381 private void addToPreviousVibrationsLocked(Vibration vib) {
382 if (mPreviousVibrations.size() > mPreviousVibrationsLimit) {
383 mPreviousVibrations.removeFirst();
384 }
385 mPreviousVibrations.addLast(new VibratorService.VibrationInfo(vib.mTimeout, vib.mStartTime,
386 vib.mPattern, vib.mRepeat, vib.mUsageHint, vib.mUid, vib.mOpPkg));
387 }
388
Jeff Brown82379ba2014-07-25 19:03:28 -0700389 @Override // Binder call
Patrick Scott18dd5f02009-07-02 11:31:12 -0400390 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 mContext.enforceCallingOrSelfPermission(
392 android.Manifest.permission.VIBRATE,
393 "cancelVibrate");
394
395 // so wakelock calls will succeed
396 long identity = Binder.clearCallingIdentity();
397 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400398 synchronized (mVibrations) {
399 final Vibration vib = removeVibrationLocked(token);
400 if (vib == mCurrentVibration) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700401 if (DEBUG) {
402 Slog.d(TAG, "Canceling vibration.");
403 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400404 doCancelVibrateLocked();
405 startNextVibrationLocked();
406 }
407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409 finally {
410 Binder.restoreCallingIdentity(identity);
411 }
412 }
Eric Olsenf42f15c2009-10-29 16:42:03 -0700413
Patrick Scott18dd5f02009-07-02 11:31:12 -0400414 private final Runnable mVibrationRunnable = new Runnable() {
Jeff Brown82379ba2014-07-25 19:03:28 -0700415 @Override
Patrick Scott18dd5f02009-07-02 11:31:12 -0400416 public void run() {
417 synchronized (mVibrations) {
418 doCancelVibrateLocked();
419 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400421 }
422 };
423
424 // Lock held on mVibrations
425 private void doCancelVibrateLocked() {
426 if (mThread != null) {
427 synchronized (mThread) {
428 mThread.mDone = true;
429 mThread.notify();
430 }
431 mThread = null;
432 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700433 doVibratorOff();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400434 mH.removeCallbacks(mVibrationRunnable);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800435 reportFinishVibrationLocked();
Patrick Scott18dd5f02009-07-02 11:31:12 -0400436 }
437
438 // Lock held on mVibrations
439 private void startNextVibrationLocked() {
440 if (mVibrations.size() <= 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800441 reportFinishVibrationLocked();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200442 mCurrentVibration = null;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400443 return;
444 }
445 mCurrentVibration = mVibrations.getFirst();
446 startVibrationLocked(mCurrentVibration);
447 }
448
449 // Lock held on mVibrations
450 private void startVibrationLocked(final Vibration vib) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800451 try {
John Spurlock7b414672014-07-18 13:02:39 -0400452 if (mLowPowerMode
Jean-Michel Trivi89c3b292014-07-20 11:41:02 -0700453 && vib.mUsageHint != AudioAttributes.USAGE_NOTIFICATION_RINGTONE) {
Ruchi Kandoi664703d2014-05-09 16:01:31 -0700454 return;
455 }
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700456
Julia Reynoldsd28967f2016-04-14 09:37:08 -0400457 if (vib.mUsageHint == AudioAttributes.USAGE_NOTIFICATION_RINGTONE
458 && Settings.System.getInt(
459 mContext.getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 0) == 0) {
460 return;
461 }
462
John Spurlock1af30c72014-03-10 08:33:35 -0400463 int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE,
John Spurlock7b414672014-07-18 13:02:39 -0400464 vib.mUsageHint, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400465 if (mode == AppOpsManager.MODE_ALLOWED) {
466 mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService),
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400467 AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg);
John Spurlock1af30c72014-03-10 08:33:35 -0400468 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800469 if (mode != AppOpsManager.MODE_ALLOWED) {
470 if (mode == AppOpsManager.MODE_ERRORED) {
471 Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid);
472 }
473 mH.post(mVibrationRunnable);
474 return;
475 }
476 } catch (RemoteException e) {
477 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400478 if (vib.mTimeout != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400479 doVibratorOn(vib.mTimeout, vib.mUid, vib.mUsageHint);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400480 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
481 } else {
482 // mThread better be null here. doCancelVibrate should always be
483 // called before startNextVibrationLocked or startVibrationLocked.
484 mThread = new VibrateThread(vib);
485 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487 }
488
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800489 private void reportFinishVibrationLocked() {
490 if (mCurrentVibration != null) {
491 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700492 mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService),
493 AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid,
Christoph Studer8fd7f1e2014-04-11 17:35:05 -0400494 mCurrentVibration.mOpPkg);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800495 } catch (RemoteException e) {
496 }
497 mCurrentVibration = null;
498 }
499 }
500
Patrick Scott18dd5f02009-07-02 11:31:12 -0400501 // Lock held on mVibrations
502 private Vibration removeVibrationLocked(IBinder token) {
503 ListIterator<Vibration> iter = mVibrations.listIterator(0);
504 while (iter.hasNext()) {
505 Vibration vib = iter.next();
506 if (vib.mToken == token) {
507 iter.remove();
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200508 unlinkVibration(vib);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400509 return vib;
510 }
511 }
512 // We might be looking for a simple vibration which is only stored in
513 // mCurrentVibration.
514 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200515 unlinkVibration(mCurrentVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400516 return mCurrentVibration;
517 }
518 return null;
519 }
520
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200521 private void unlinkVibration(Vibration vib) {
522 if (vib.mPattern != null) {
523 // If Vibration object has a pattern,
524 // the Vibration object has also been linkedToDeath.
525 vib.mToken.unlinkToDeath(vib, 0);
526 }
527 }
528
Jeff Brown7f6c2312012-04-13 20:38:38 -0700529 private void updateInputDeviceVibrators() {
530 synchronized (mVibrations) {
531 doCancelVibrateLocked();
532
533 synchronized (mInputDeviceVibrators) {
Jeff Brown82065252012-04-16 13:19:05 -0700534 mVibrateInputDevicesSetting = false;
535 try {
Jeff Brownd4935962012-09-25 13:27:20 -0700536 mVibrateInputDevicesSetting = Settings.System.getIntForUser(
537 mContext.getContentResolver(),
538 Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
Jeff Brown82065252012-04-16 13:19:05 -0700539 } catch (SettingNotFoundException snfe) {
540 }
541
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700542 mLowPowerMode = mPowerManagerInternal.getLowPowerModeEnabled();
Ruchi Kandoi13b03af2014-05-07 20:10:32 -0700543
Jeff Brown82065252012-04-16 13:19:05 -0700544 if (mVibrateInputDevicesSetting) {
545 if (!mInputDeviceListenerRegistered) {
546 mInputDeviceListenerRegistered = true;
547 mIm.registerInputDeviceListener(this, mH);
548 }
549 } else {
550 if (mInputDeviceListenerRegistered) {
551 mInputDeviceListenerRegistered = false;
552 mIm.unregisterInputDeviceListener(this);
553 }
554 }
555
Jeff Brown7f6c2312012-04-13 20:38:38 -0700556 mInputDeviceVibrators.clear();
557 if (mVibrateInputDevicesSetting) {
558 int[] ids = mIm.getInputDeviceIds();
559 for (int i = 0; i < ids.length; i++) {
560 InputDevice device = mIm.getInputDevice(ids[i]);
561 Vibrator vibrator = device.getVibrator();
562 if (vibrator.hasVibrator()) {
563 mInputDeviceVibrators.add(vibrator);
564 }
565 }
566 }
567 }
568
569 startNextVibrationLocked();
570 }
571 }
572
573 @Override
574 public void onInputDeviceAdded(int deviceId) {
575 updateInputDeviceVibrators();
576 }
577
578 @Override
579 public void onInputDeviceChanged(int deviceId) {
580 updateInputDeviceVibrators();
581 }
582
583 @Override
584 public void onInputDeviceRemoved(int deviceId) {
585 updateInputDeviceVibrators();
586 }
587
588 private boolean doVibratorExists() {
Jeff Brown1064a502012-05-02 16:51:37 -0700589 // For now, we choose to ignore the presence of input devices that have vibrators
590 // when reporting whether the device has a vibrator. Applications often use this
591 // information to decide whether to enable certain features so they expect the
592 // result of hasVibrator() to be constant. For now, just report whether
593 // the device has a built-in vibrator.
594 //synchronized (mInputDeviceVibrators) {
595 // return !mInputDeviceVibrators.isEmpty() || vibratorExists();
596 //}
Dianne Hackbornc2293022013-02-06 23:14:49 -0800597 return vibratorExists();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700598 }
599
John Spurlock7b414672014-07-18 13:02:39 -0400600 private void doVibratorOn(long millis, int uid, int usageHint) {
Jeff Brown7f6c2312012-04-13 20:38:38 -0700601 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700602 if (DEBUG) {
603 Slog.d(TAG, "Turning vibrator on for " + millis + " ms.");
604 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800605 try {
606 mBatteryStatsService.noteVibratorOn(uid, millis);
607 mCurVibUid = uid;
608 } catch (RemoteException e) {
609 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700610 final int vibratorCount = mInputDeviceVibrators.size();
611 if (vibratorCount != 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400612 final AudioAttributes attributes = new AudioAttributes.Builder().setUsage(usageHint)
613 .build();
Jeff Brown7f6c2312012-04-13 20:38:38 -0700614 for (int i = 0; i < vibratorCount; i++) {
John Spurlock7b414672014-07-18 13:02:39 -0400615 mInputDeviceVibrators.get(i).vibrate(millis, attributes);
Jeff Brown7f6c2312012-04-13 20:38:38 -0700616 }
617 } else {
618 vibratorOn(millis);
619 }
620 }
621 }
622
623 private void doVibratorOff() {
624 synchronized (mInputDeviceVibrators) {
Jeff Brown82379ba2014-07-25 19:03:28 -0700625 if (DEBUG) {
626 Slog.d(TAG, "Turning vibrator off.");
627 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800628 if (mCurVibUid >= 0) {
629 try {
630 mBatteryStatsService.noteVibratorOff(mCurVibUid);
631 } catch (RemoteException e) {
632 }
633 mCurVibUid = -1;
634 }
Jeff Brown7f6c2312012-04-13 20:38:38 -0700635 final int vibratorCount = mInputDeviceVibrators.size();
636 if (vibratorCount != 0) {
637 for (int i = 0; i < vibratorCount; i++) {
638 mInputDeviceVibrators.get(i).cancel();
639 }
640 } else {
641 vibratorOff();
642 }
643 }
644 }
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400647 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 boolean mDone;
Eric Olsenf42f15c2009-10-29 16:42:03 -0700649
Patrick Scott18dd5f02009-07-02 11:31:12 -0400650 VibrateThread(Vibration vib) {
651 mVibration = vib;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700652 mTmpWorkSource.set(vib.mUid);
653 mWakeLock.setWorkSource(mTmpWorkSource);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 mWakeLock.acquire();
655 }
656
657 private void delay(long duration) {
658 if (duration > 0) {
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700659 long bedtime = duration + SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 do {
661 try {
662 this.wait(duration);
663 }
664 catch (InterruptedException e) {
665 }
666 if (mDone) {
667 break;
668 }
Vairavan Srinivasane4c56d92011-03-31 13:32:54 -0700669 duration = bedtime - SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 } while (duration > 0);
671 }
672 }
673
674 public void run() {
675 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
676 synchronized (this) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800677 final long[] pattern = mVibration.mPattern;
678 final int len = pattern.length;
679 final int repeat = mVibration.mRepeat;
680 final int uid = mVibration.mUid;
John Spurlock7b414672014-07-18 13:02:39 -0400681 final int usageHint = mVibration.mUsageHint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 int index = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 long duration = 0;
684
685 while (!mDone) {
Eric Olsenf42f15c2009-10-29 16:42:03 -0700686 // add off-time duration to any accumulated on-time duration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 if (index < len) {
688 duration += pattern[index++];
689 }
690
691 // sleep until it is time to start the vibrator
692 delay(duration);
693 if (mDone) {
694 break;
695 }
696
697 if (index < len) {
698 // read on-time duration and start the vibrator
699 // duration is saved for delay() at top of loop
700 duration = pattern[index++];
701 if (duration > 0) {
John Spurlock7b414672014-07-18 13:02:39 -0400702 VibratorService.this.doVibratorOn(duration, uid, usageHint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 }
704 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400705 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 break;
707 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400708 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 duration = 0;
710 }
711 }
712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 mWakeLock.release();
714 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400715 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 if (mThread == this) {
717 mThread = null;
718 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400719 if (!mDone) {
720 // If this vibration finished naturally, start the next
721 // vibration.
Mathias Jeppssonb23949b2010-09-28 14:45:23 +0200722 unlinkVibration(mVibration);
Patrick Scott18dd5f02009-07-02 11:31:12 -0400723 startNextVibrationLocked();
724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
726 }
Jeff Brown969579b2014-05-20 19:29:29 -0700727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
Jeff Brown969579b2014-05-20 19:29:29 -0700730 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 public void onReceive(Context context, Intent intent) {
732 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400733 synchronized (mVibrations) {
Jeff Brown969579b2014-05-20 19:29:29 -0700734 // When the system is entering a non-interactive state, we want
735 // to cancel vibrations in case a misbehaving app has abandoned
736 // them. However it may happen that the system is currently playing
737 // haptic feedback as part of the transition. So we don't cancel
738 // system vibrations.
739 if (mCurrentVibration != null
740 && !mCurrentVibration.isSystemHapticFeedback()) {
741 doCancelVibrateLocked();
Vairavan Srinivasan8a61f492011-05-13 10:47:20 -0700742 }
743
Jeff Brown969579b2014-05-20 19:29:29 -0700744 // Clear all remaining vibrations.
745 Iterator<Vibration> it = mVibrations.iterator();
746 while (it.hasNext()) {
747 Vibration vibration = it.next();
748 if (vibration != mCurrentVibration) {
749 unlinkVibration(vibration);
750 it.remove();
751 }
752 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 }
755 }
756 };
Filip Gruszczynski3a8eb0f2015-06-25 18:35:00 -0700757
758 @Override
759 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
760 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
761 != PackageManager.PERMISSION_GRANTED) {
762
763 pw.println("Permission Denial: can't dump vibrator service from from pid="
764 + Binder.getCallingPid()
765 + ", uid=" + Binder.getCallingUid());
766 return;
767 }
768 pw.println("Previous vibrations:");
769 synchronized (mVibrations) {
770 for (VibrationInfo info : mPreviousVibrations) {
771 pw.print(" ");
772 pw.println(info.toString());
773 }
774 }
775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776}