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