The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 19 | import android.app.AppOpsManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.content.pm.PackageManager; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 25 | import android.database.ContentObserver; |
| 26 | import android.hardware.input.InputManager; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 27 | import android.os.BatteryStats; |
Joe Onorato | 95e4f70 | 2009-03-24 19:29:09 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 29 | import android.os.IVibratorService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.os.PowerManager; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 31 | import android.os.PowerManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.os.Process; |
| 33 | import android.os.RemoteException; |
| 34 | import android.os.IBinder; |
| 35 | import android.os.Binder; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 36 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.SystemClock; |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 38 | import android.os.UserHandle; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 39 | import android.os.Vibrator; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 40 | import android.os.WorkSource; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 41 | import android.provider.Settings; |
| 42 | import android.provider.Settings.SettingNotFoundException; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 43 | import android.util.Slog; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 44 | import android.view.InputDevice; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 45 | import android.media.AudioAttributes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 47 | import com.android.internal.app.IAppOpsService; |
| 48 | import com.android.internal.app.IBatteryStats; |
| 49 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 50 | import java.util.ArrayList; |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 51 | import java.util.Iterator; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 52 | import java.util.LinkedList; |
| 53 | import java.util.ListIterator; |
| 54 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 55 | public class VibratorService extends IVibratorService.Stub |
| 56 | implements InputManager.InputDeviceListener { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 57 | private static final String TAG = "VibratorService"; |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 58 | private static final boolean DEBUG = false; |
Mike Lockwood | cc9a63d | 2009-11-10 07:50:28 -0500 | [diff] [blame] | 59 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 60 | private final LinkedList<Vibration> mVibrations; |
| 61 | private Vibration mCurrentVibration; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 62 | private final WorkSource mTmpWorkSource = new WorkSource(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 63 | private final Handler mH = new Handler(); |
| 64 | |
| 65 | private final Context mContext; |
| 66 | private final PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 67 | private final IAppOpsService mAppOpsService; |
| 68 | private final IBatteryStats mBatteryStatsService; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 69 | private PowerManagerInternal mPowerManagerInternal; |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 70 | private InputManager mIm; |
| 71 | |
| 72 | volatile VibrateThread mThread; |
| 73 | |
| 74 | // mInputDeviceVibrators lock should be acquired after mVibrations lock, if both are |
| 75 | // to be acquired |
| 76 | private final ArrayList<Vibrator> mInputDeviceVibrators = new ArrayList<Vibrator>(); |
| 77 | private boolean mVibrateInputDevicesSetting; // guarded by mInputDeviceVibrators |
| 78 | private boolean mInputDeviceListenerRegistered; // guarded by mInputDeviceVibrators |
| 79 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 80 | private int mCurVibUid = -1; |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 81 | private boolean mLowPowerMode; |
| 82 | private SettingsObserver mSettingObserver; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 83 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 84 | native static boolean vibratorExists(); |
| 85 | native static void vibratorOn(long milliseconds); |
| 86 | native static void vibratorOff(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 87 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 88 | private class Vibration implements IBinder.DeathRecipient { |
| 89 | private final IBinder mToken; |
| 90 | private final long mTimeout; |
| 91 | private final long mStartTime; |
| 92 | private final long[] mPattern; |
| 93 | private final int mRepeat; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 94 | private final int mUsageHint; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 95 | private final int mUid; |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 96 | private final String mOpPkg; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 97 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 98 | Vibration(IBinder token, long millis, int usageHint, int uid, String opPkg) { |
| 99 | this(token, millis, null, 0, usageHint, uid, opPkg); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 100 | } |
| 101 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 102 | Vibration(IBinder token, long[] pattern, int repeat, int usageHint, int uid, |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 103 | String opPkg) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 104 | this(token, 0, pattern, repeat, usageHint, uid, opPkg); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private Vibration(IBinder token, long millis, long[] pattern, |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 108 | int repeat, int usageHint, int uid, String opPkg) { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 109 | mToken = token; |
| 110 | mTimeout = millis; |
| 111 | mStartTime = SystemClock.uptimeMillis(); |
| 112 | mPattern = pattern; |
| 113 | mRepeat = repeat; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 114 | mUsageHint = usageHint; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 115 | mUid = uid; |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 116 | mOpPkg = opPkg; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public void binderDied() { |
| 120 | synchronized (mVibrations) { |
| 121 | mVibrations.remove(this); |
| 122 | if (this == mCurrentVibration) { |
| 123 | doCancelVibrateLocked(); |
| 124 | startNextVibrationLocked(); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public boolean hasLongerTimeout(long millis) { |
| 130 | if (mTimeout == 0) { |
| 131 | // This is a pattern, return false to play the simple |
| 132 | // vibration. |
| 133 | return false; |
| 134 | } |
| 135 | if ((mStartTime + mTimeout) |
| 136 | < (SystemClock.uptimeMillis() + millis)) { |
| 137 | // If this vibration will end before the time passed in, let |
| 138 | // the new vibration play. |
| 139 | return false; |
| 140 | } |
| 141 | return true; |
| 142 | } |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 143 | |
| 144 | public boolean isSystemHapticFeedback() { |
| 145 | return (mUid == Process.SYSTEM_UID || mUid == 0) && mRepeat < 0; |
| 146 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 149 | VibratorService(Context context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | // Reset the hardware to a default state, in case this is a runtime |
| 151 | // restart instead of a fresh boot. |
| 152 | vibratorOff(); |
| 153 | |
| 154 | mContext = context; |
| 155 | PowerManager pm = (PowerManager)context.getSystemService( |
| 156 | Context.POWER_SERVICE); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 157 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | mWakeLock.setReferenceCounted(true); |
| 159 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 160 | mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE)); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 161 | mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 162 | BatteryStats.SERVICE_NAME)); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 163 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 164 | mVibrations = new LinkedList<Vibration>(); |
| 165 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | IntentFilter filter = new IntentFilter(); |
| 167 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 168 | context.registerReceiver(mIntentReceiver, filter); |
| 169 | } |
| 170 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 171 | public void systemReady() { |
| 172 | mIm = (InputManager)mContext.getSystemService(Context.INPUT_SERVICE); |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 173 | mSettingObserver = new SettingsObserver(mH); |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 174 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 175 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 176 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 177 | new PowerManagerInternal.LowPowerModeListener() { |
| 178 | @Override |
| 179 | public void onLowPowerModeChanged(boolean enabled) { |
| 180 | updateInputDeviceVibrators(); |
| 181 | } |
| 182 | }); |
| 183 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 184 | mContext.getContentResolver().registerContentObserver( |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 185 | Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES), |
| 186 | true, mSettingObserver, UserHandle.USER_ALL); |
| 187 | |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 188 | mContext.registerReceiver(new BroadcastReceiver() { |
| 189 | @Override |
| 190 | public void onReceive(Context context, Intent intent) { |
| 191 | updateInputDeviceVibrators(); |
| 192 | } |
| 193 | }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mH); |
| 194 | |
Jeff Brown | 8206525 | 2012-04-16 13:19:05 -0700 | [diff] [blame] | 195 | updateInputDeviceVibrators(); |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 196 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 197 | |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 198 | private final class SettingsObserver extends ContentObserver { |
| 199 | public SettingsObserver(Handler handler) { |
| 200 | super(handler); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void onChange(boolean SelfChange) { |
| 205 | updateInputDeviceVibrators(); |
| 206 | } |
| 207 | } |
| 208 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 209 | @Override // Binder call |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 210 | public boolean hasVibrator() { |
| 211 | return doVibratorExists(); |
| 212 | } |
| 213 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 214 | private void verifyIncomingUid(int uid) { |
| 215 | if (uid == Binder.getCallingUid()) { |
| 216 | return; |
| 217 | } |
| 218 | if (Binder.getCallingPid() == Process.myPid()) { |
| 219 | return; |
| 220 | } |
| 221 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 222 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 223 | } |
| 224 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 225 | @Override // Binder call |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 226 | public void vibrate(int uid, String opPkg, long milliseconds, int usageHint, |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 227 | IBinder token) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 228 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE) |
| 229 | != PackageManager.PERMISSION_GRANTED) { |
| 230 | throw new SecurityException("Requires VIBRATE permission"); |
| 231 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 232 | verifyIncomingUid(uid); |
Patrick Scott | 24f1076 | 2009-08-19 09:03:56 -0400 | [diff] [blame] | 233 | // We're running in the system server so we cannot crash. Check for a |
| 234 | // timeout of 0 or negative. This will ensure that a vibration has |
| 235 | // either a timeout of > 0 or a non-null pattern. |
| 236 | if (milliseconds <= 0 || (mCurrentVibration != null |
| 237 | && mCurrentVibration.hasLongerTimeout(milliseconds))) { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 238 | // Ignore this vibration since the current vibration will play for |
| 239 | // longer than milliseconds. |
| 240 | return; |
| 241 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 242 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 243 | if (DEBUG) { |
| 244 | Slog.d(TAG, "Vibrating for " + milliseconds + " ms."); |
| 245 | } |
| 246 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 247 | Vibration vib = new Vibration(token, milliseconds, usageHint, uid, opPkg); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 248 | |
| 249 | final long ident = Binder.clearCallingIdentity(); |
| 250 | try { |
| 251 | synchronized (mVibrations) { |
| 252 | removeVibrationLocked(token); |
| 253 | doCancelVibrateLocked(); |
| 254 | mCurrentVibration = vib; |
| 255 | startVibrationLocked(vib); |
| 256 | } |
| 257 | } finally { |
| 258 | Binder.restoreCallingIdentity(ident); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 259 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | private boolean isAll0(long[] pattern) { |
| 263 | int N = pattern.length; |
| 264 | for (int i = 0; i < N; i++) { |
| 265 | if (pattern[i] != 0) { |
| 266 | return false; |
| 267 | } |
| 268 | } |
| 269 | return true; |
| 270 | } |
| 271 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 272 | @Override // Binder call |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 273 | public void vibratePattern(int uid, String packageName, long[] pattern, int repeat, |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 274 | int usageHint, IBinder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE) |
| 276 | != PackageManager.PERMISSION_GRANTED) { |
| 277 | throw new SecurityException("Requires VIBRATE permission"); |
| 278 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 279 | verifyIncomingUid(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | // so wakelock calls will succeed |
| 281 | long identity = Binder.clearCallingIdentity(); |
| 282 | try { |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 283 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | String s = ""; |
| 285 | int N = pattern.length; |
| 286 | for (int i=0; i<N; i++) { |
| 287 | s += " " + pattern[i]; |
| 288 | } |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 289 | Slog.d(TAG, "Vibrating with pattern:" + s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // we're running in the server so we can't fail |
| 293 | if (pattern == null || pattern.length == 0 |
| 294 | || isAll0(pattern) |
| 295 | || repeat >= pattern.length || token == null) { |
| 296 | return; |
| 297 | } |
| 298 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 299 | Vibration vib = new Vibration(token, pattern, repeat, usageHint, uid, packageName); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 300 | try { |
| 301 | token.linkToDeath(vib, 0); |
| 302 | } catch (RemoteException e) { |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | synchronized (mVibrations) { |
| 307 | removeVibrationLocked(token); |
| 308 | doCancelVibrateLocked(); |
| 309 | if (repeat >= 0) { |
| 310 | mVibrations.addFirst(vib); |
| 311 | startNextVibrationLocked(); |
| 312 | } else { |
| 313 | // A negative repeat means that this pattern is not meant |
| 314 | // to repeat. Treat it like a simple vibration. |
| 315 | mCurrentVibration = vib; |
| 316 | startVibrationLocked(vib); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | finally { |
| 321 | Binder.restoreCallingIdentity(identity); |
| 322 | } |
| 323 | } |
| 324 | |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 325 | @Override // Binder call |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 326 | public void cancelVibrate(IBinder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | mContext.enforceCallingOrSelfPermission( |
| 328 | android.Manifest.permission.VIBRATE, |
| 329 | "cancelVibrate"); |
| 330 | |
| 331 | // so wakelock calls will succeed |
| 332 | long identity = Binder.clearCallingIdentity(); |
| 333 | try { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 334 | synchronized (mVibrations) { |
| 335 | final Vibration vib = removeVibrationLocked(token); |
| 336 | if (vib == mCurrentVibration) { |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 337 | if (DEBUG) { |
| 338 | Slog.d(TAG, "Canceling vibration."); |
| 339 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 340 | doCancelVibrateLocked(); |
| 341 | startNextVibrationLocked(); |
| 342 | } |
| 343 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |
| 345 | finally { |
| 346 | Binder.restoreCallingIdentity(identity); |
| 347 | } |
| 348 | } |
Eric Olsen | f42f15c | 2009-10-29 16:42:03 -0700 | [diff] [blame] | 349 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 350 | private final Runnable mVibrationRunnable = new Runnable() { |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 351 | @Override |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 352 | public void run() { |
| 353 | synchronized (mVibrations) { |
| 354 | doCancelVibrateLocked(); |
| 355 | startNextVibrationLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 357 | } |
| 358 | }; |
| 359 | |
| 360 | // Lock held on mVibrations |
| 361 | private void doCancelVibrateLocked() { |
| 362 | if (mThread != null) { |
| 363 | synchronized (mThread) { |
| 364 | mThread.mDone = true; |
| 365 | mThread.notify(); |
| 366 | } |
| 367 | mThread = null; |
| 368 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 369 | doVibratorOff(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 370 | mH.removeCallbacks(mVibrationRunnable); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 371 | reportFinishVibrationLocked(); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // Lock held on mVibrations |
| 375 | private void startNextVibrationLocked() { |
| 376 | if (mVibrations.size() <= 0) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 377 | reportFinishVibrationLocked(); |
Mathias Jeppsson | b23949b | 2010-09-28 14:45:23 +0200 | [diff] [blame] | 378 | mCurrentVibration = null; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 379 | return; |
| 380 | } |
| 381 | mCurrentVibration = mVibrations.getFirst(); |
| 382 | startVibrationLocked(mCurrentVibration); |
| 383 | } |
| 384 | |
| 385 | // Lock held on mVibrations |
| 386 | private void startVibrationLocked(final Vibration vib) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 387 | try { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 388 | if (mLowPowerMode |
Jean-Michel Trivi | 89c3b29 | 2014-07-20 11:41:02 -0700 | [diff] [blame] | 389 | && vib.mUsageHint != AudioAttributes.USAGE_NOTIFICATION_RINGTONE) { |
Ruchi Kandoi | 664703d | 2014-05-09 16:01:31 -0700 | [diff] [blame] | 390 | return; |
| 391 | } |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 392 | |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 393 | int mode = mAppOpsService.checkAudioOperation(AppOpsManager.OP_VIBRATE, |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 394 | vib.mUsageHint, vib.mUid, vib.mOpPkg); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 395 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 396 | mode = mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService), |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 397 | AppOpsManager.OP_VIBRATE, vib.mUid, vib.mOpPkg); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 398 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 399 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 400 | if (mode == AppOpsManager.MODE_ERRORED) { |
| 401 | Slog.w(TAG, "Would be an error: vibrate from uid " + vib.mUid); |
| 402 | } |
| 403 | mH.post(mVibrationRunnable); |
| 404 | return; |
| 405 | } |
| 406 | } catch (RemoteException e) { |
| 407 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 408 | if (vib.mTimeout != 0) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 409 | doVibratorOn(vib.mTimeout, vib.mUid, vib.mUsageHint); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 410 | mH.postDelayed(mVibrationRunnable, vib.mTimeout); |
| 411 | } else { |
| 412 | // mThread better be null here. doCancelVibrate should always be |
| 413 | // called before startNextVibrationLocked or startVibrationLocked. |
| 414 | mThread = new VibrateThread(vib); |
| 415 | mThread.start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 419 | private void reportFinishVibrationLocked() { |
| 420 | if (mCurrentVibration != null) { |
| 421 | try { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 422 | mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService), |
| 423 | AppOpsManager.OP_VIBRATE, mCurrentVibration.mUid, |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 424 | mCurrentVibration.mOpPkg); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 425 | } catch (RemoteException e) { |
| 426 | } |
| 427 | mCurrentVibration = null; |
| 428 | } |
| 429 | } |
| 430 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 431 | // Lock held on mVibrations |
| 432 | private Vibration removeVibrationLocked(IBinder token) { |
| 433 | ListIterator<Vibration> iter = mVibrations.listIterator(0); |
| 434 | while (iter.hasNext()) { |
| 435 | Vibration vib = iter.next(); |
| 436 | if (vib.mToken == token) { |
| 437 | iter.remove(); |
Mathias Jeppsson | b23949b | 2010-09-28 14:45:23 +0200 | [diff] [blame] | 438 | unlinkVibration(vib); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 439 | return vib; |
| 440 | } |
| 441 | } |
| 442 | // We might be looking for a simple vibration which is only stored in |
| 443 | // mCurrentVibration. |
| 444 | if (mCurrentVibration != null && mCurrentVibration.mToken == token) { |
Mathias Jeppsson | b23949b | 2010-09-28 14:45:23 +0200 | [diff] [blame] | 445 | unlinkVibration(mCurrentVibration); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 446 | return mCurrentVibration; |
| 447 | } |
| 448 | return null; |
| 449 | } |
| 450 | |
Mathias Jeppsson | b23949b | 2010-09-28 14:45:23 +0200 | [diff] [blame] | 451 | private void unlinkVibration(Vibration vib) { |
| 452 | if (vib.mPattern != null) { |
| 453 | // If Vibration object has a pattern, |
| 454 | // the Vibration object has also been linkedToDeath. |
| 455 | vib.mToken.unlinkToDeath(vib, 0); |
| 456 | } |
| 457 | } |
| 458 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 459 | private void updateInputDeviceVibrators() { |
| 460 | synchronized (mVibrations) { |
| 461 | doCancelVibrateLocked(); |
| 462 | |
| 463 | synchronized (mInputDeviceVibrators) { |
Jeff Brown | 8206525 | 2012-04-16 13:19:05 -0700 | [diff] [blame] | 464 | mVibrateInputDevicesSetting = false; |
| 465 | try { |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 466 | mVibrateInputDevicesSetting = Settings.System.getIntForUser( |
| 467 | mContext.getContentResolver(), |
| 468 | Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0; |
Jeff Brown | 8206525 | 2012-04-16 13:19:05 -0700 | [diff] [blame] | 469 | } catch (SettingNotFoundException snfe) { |
| 470 | } |
| 471 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 472 | mLowPowerMode = mPowerManagerInternal.getLowPowerModeEnabled(); |
Ruchi Kandoi | 13b03af | 2014-05-07 20:10:32 -0700 | [diff] [blame] | 473 | |
Jeff Brown | 8206525 | 2012-04-16 13:19:05 -0700 | [diff] [blame] | 474 | if (mVibrateInputDevicesSetting) { |
| 475 | if (!mInputDeviceListenerRegistered) { |
| 476 | mInputDeviceListenerRegistered = true; |
| 477 | mIm.registerInputDeviceListener(this, mH); |
| 478 | } |
| 479 | } else { |
| 480 | if (mInputDeviceListenerRegistered) { |
| 481 | mInputDeviceListenerRegistered = false; |
| 482 | mIm.unregisterInputDeviceListener(this); |
| 483 | } |
| 484 | } |
| 485 | |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 486 | mInputDeviceVibrators.clear(); |
| 487 | if (mVibrateInputDevicesSetting) { |
| 488 | int[] ids = mIm.getInputDeviceIds(); |
| 489 | for (int i = 0; i < ids.length; i++) { |
| 490 | InputDevice device = mIm.getInputDevice(ids[i]); |
| 491 | Vibrator vibrator = device.getVibrator(); |
| 492 | if (vibrator.hasVibrator()) { |
| 493 | mInputDeviceVibrators.add(vibrator); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | startNextVibrationLocked(); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public void onInputDeviceAdded(int deviceId) { |
| 505 | updateInputDeviceVibrators(); |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public void onInputDeviceChanged(int deviceId) { |
| 510 | updateInputDeviceVibrators(); |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public void onInputDeviceRemoved(int deviceId) { |
| 515 | updateInputDeviceVibrators(); |
| 516 | } |
| 517 | |
| 518 | private boolean doVibratorExists() { |
Jeff Brown | 1064a50 | 2012-05-02 16:51:37 -0700 | [diff] [blame] | 519 | // For now, we choose to ignore the presence of input devices that have vibrators |
| 520 | // when reporting whether the device has a vibrator. Applications often use this |
| 521 | // information to decide whether to enable certain features so they expect the |
| 522 | // result of hasVibrator() to be constant. For now, just report whether |
| 523 | // the device has a built-in vibrator. |
| 524 | //synchronized (mInputDeviceVibrators) { |
| 525 | // return !mInputDeviceVibrators.isEmpty() || vibratorExists(); |
| 526 | //} |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 527 | return vibratorExists(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 528 | } |
| 529 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 530 | private void doVibratorOn(long millis, int uid, int usageHint) { |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 531 | synchronized (mInputDeviceVibrators) { |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 532 | if (DEBUG) { |
| 533 | Slog.d(TAG, "Turning vibrator on for " + millis + " ms."); |
| 534 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 535 | try { |
| 536 | mBatteryStatsService.noteVibratorOn(uid, millis); |
| 537 | mCurVibUid = uid; |
| 538 | } catch (RemoteException e) { |
| 539 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 540 | final int vibratorCount = mInputDeviceVibrators.size(); |
| 541 | if (vibratorCount != 0) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 542 | final AudioAttributes attributes = new AudioAttributes.Builder().setUsage(usageHint) |
| 543 | .build(); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 544 | for (int i = 0; i < vibratorCount; i++) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 545 | mInputDeviceVibrators.get(i).vibrate(millis, attributes); |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 546 | } |
| 547 | } else { |
| 548 | vibratorOn(millis); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | private void doVibratorOff() { |
| 554 | synchronized (mInputDeviceVibrators) { |
Jeff Brown | 82379ba | 2014-07-25 19:03:28 -0700 | [diff] [blame^] | 555 | if (DEBUG) { |
| 556 | Slog.d(TAG, "Turning vibrator off."); |
| 557 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 558 | if (mCurVibUid >= 0) { |
| 559 | try { |
| 560 | mBatteryStatsService.noteVibratorOff(mCurVibUid); |
| 561 | } catch (RemoteException e) { |
| 562 | } |
| 563 | mCurVibUid = -1; |
| 564 | } |
Jeff Brown | 7f6c231 | 2012-04-13 20:38:38 -0700 | [diff] [blame] | 565 | final int vibratorCount = mInputDeviceVibrators.size(); |
| 566 | if (vibratorCount != 0) { |
| 567 | for (int i = 0; i < vibratorCount; i++) { |
| 568 | mInputDeviceVibrators.get(i).cancel(); |
| 569 | } |
| 570 | } else { |
| 571 | vibratorOff(); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | private class VibrateThread extends Thread { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 577 | final Vibration mVibration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | boolean mDone; |
Eric Olsen | f42f15c | 2009-10-29 16:42:03 -0700 | [diff] [blame] | 579 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 580 | VibrateThread(Vibration vib) { |
| 581 | mVibration = vib; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 582 | mTmpWorkSource.set(vib.mUid); |
| 583 | mWakeLock.setWorkSource(mTmpWorkSource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | mWakeLock.acquire(); |
| 585 | } |
| 586 | |
| 587 | private void delay(long duration) { |
| 588 | if (duration > 0) { |
Vairavan Srinivasan | e4c56d9 | 2011-03-31 13:32:54 -0700 | [diff] [blame] | 589 | long bedtime = duration + SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | do { |
| 591 | try { |
| 592 | this.wait(duration); |
| 593 | } |
| 594 | catch (InterruptedException e) { |
| 595 | } |
| 596 | if (mDone) { |
| 597 | break; |
| 598 | } |
Vairavan Srinivasan | e4c56d9 | 2011-03-31 13:32:54 -0700 | [diff] [blame] | 599 | duration = bedtime - SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | } while (duration > 0); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | public void run() { |
| 605 | Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY); |
| 606 | synchronized (this) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 607 | final long[] pattern = mVibration.mPattern; |
| 608 | final int len = pattern.length; |
| 609 | final int repeat = mVibration.mRepeat; |
| 610 | final int uid = mVibration.mUid; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 611 | final int usageHint = mVibration.mUsageHint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | int index = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | long duration = 0; |
| 614 | |
| 615 | while (!mDone) { |
Eric Olsen | f42f15c | 2009-10-29 16:42:03 -0700 | [diff] [blame] | 616 | // add off-time duration to any accumulated on-time duration |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | if (index < len) { |
| 618 | duration += pattern[index++]; |
| 619 | } |
| 620 | |
| 621 | // sleep until it is time to start the vibrator |
| 622 | delay(duration); |
| 623 | if (mDone) { |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | if (index < len) { |
| 628 | // read on-time duration and start the vibrator |
| 629 | // duration is saved for delay() at top of loop |
| 630 | duration = pattern[index++]; |
| 631 | if (duration > 0) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 632 | VibratorService.this.doVibratorOn(duration, uid, usageHint); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | } |
| 634 | } else { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 635 | if (repeat < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | break; |
| 637 | } else { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 638 | index = repeat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | duration = 0; |
| 640 | } |
| 641 | } |
| 642 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | mWakeLock.release(); |
| 644 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 645 | synchronized (mVibrations) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | if (mThread == this) { |
| 647 | mThread = null; |
| 648 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 649 | if (!mDone) { |
| 650 | // If this vibration finished naturally, start the next |
| 651 | // vibration. |
| 652 | mVibrations.remove(mVibration); |
Mathias Jeppsson | b23949b | 2010-09-28 14:45:23 +0200 | [diff] [blame] | 653 | unlinkVibration(mVibration); |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 654 | startNextVibrationLocked(); |
| 655 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | } |
| 657 | } |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 658 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 661 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | public void onReceive(Context context, Intent intent) { |
| 663 | if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 664 | synchronized (mVibrations) { |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 665 | // When the system is entering a non-interactive state, we want |
| 666 | // to cancel vibrations in case a misbehaving app has abandoned |
| 667 | // them. However it may happen that the system is currently playing |
| 668 | // haptic feedback as part of the transition. So we don't cancel |
| 669 | // system vibrations. |
| 670 | if (mCurrentVibration != null |
| 671 | && !mCurrentVibration.isSystemHapticFeedback()) { |
| 672 | doCancelVibrateLocked(); |
Vairavan Srinivasan | 8a61f49 | 2011-05-13 10:47:20 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Jeff Brown | 969579b | 2014-05-20 19:29:29 -0700 | [diff] [blame] | 675 | // Clear all remaining vibrations. |
| 676 | Iterator<Vibration> it = mVibrations.iterator(); |
| 677 | while (it.hasNext()) { |
| 678 | Vibration vibration = it.next(); |
| 679 | if (vibration != mCurrentVibration) { |
| 680 | unlinkVibration(vibration); |
| 681 | it.remove(); |
| 682 | } |
| 683 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame] | 684 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | } |