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