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