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