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 | bed30e1 | 2009-03-31 14:46:20 -0700 | [diff] [blame] | 19 | import com.android.internal.app.IBatteryStats; |
| 20 | import com.android.server.am.BatteryStatsService; |
| 21 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
| 26 | import android.content.pm.PackageManager; |
Joe Onorato | 95e4f70 | 2009-03-24 19:29:09 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.os.Hardware; |
| 29 | import android.os.IHardwareService; |
Joe Onorato | 95e4f70 | 2009-03-24 19:29:09 -0700 | [diff] [blame] | 30 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.Power; |
| 32 | import android.os.PowerManager; |
| 33 | import android.os.Process; |
| 34 | import android.os.RemoteException; |
| 35 | import android.os.IBinder; |
| 36 | import android.os.Binder; |
| 37 | import android.os.SystemClock; |
| 38 | import android.util.Log; |
| 39 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 40 | import java.util.LinkedList; |
| 41 | import java.util.ListIterator; |
| 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | public class HardwareService extends IHardwareService.Stub { |
| 44 | private static final String TAG = "HardwareService"; |
| 45 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 46 | static final int LIGHT_ID_BACKLIGHT = 0; |
| 47 | static final int LIGHT_ID_KEYBOARD = 1; |
| 48 | static final int LIGHT_ID_BUTTONS = 2; |
| 49 | static final int LIGHT_ID_BATTERY = 3; |
| 50 | static final int LIGHT_ID_NOTIFICATIONS = 4; |
| 51 | static final int LIGHT_ID_ATTENTION = 5; |
| 52 | |
| 53 | static final int LIGHT_FLASH_NONE = 0; |
| 54 | static final int LIGHT_FLASH_TIMED = 1; |
| 55 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 56 | private final LinkedList<Vibration> mVibrations; |
| 57 | private Vibration mCurrentVibration; |
| 58 | |
Joe Onorato | 95e4f70 | 2009-03-24 19:29:09 -0700 | [diff] [blame] | 59 | private boolean mAttentionLightOn; |
| 60 | private boolean mPulsing; |
| 61 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 62 | private class Vibration implements IBinder.DeathRecipient { |
| 63 | private final IBinder mToken; |
| 64 | private final long mTimeout; |
| 65 | private final long mStartTime; |
| 66 | private final long[] mPattern; |
| 67 | private final int mRepeat; |
| 68 | |
| 69 | Vibration(IBinder token, long millis) { |
| 70 | this(token, millis, null, 0); |
| 71 | } |
| 72 | |
| 73 | Vibration(IBinder token, long[] pattern, int repeat) { |
| 74 | this(token, 0, pattern, repeat); |
| 75 | } |
| 76 | |
| 77 | private Vibration(IBinder token, long millis, long[] pattern, |
| 78 | int repeat) { |
| 79 | mToken = token; |
| 80 | mTimeout = millis; |
| 81 | mStartTime = SystemClock.uptimeMillis(); |
| 82 | mPattern = pattern; |
| 83 | mRepeat = repeat; |
| 84 | } |
| 85 | |
| 86 | public void binderDied() { |
| 87 | synchronized (mVibrations) { |
| 88 | mVibrations.remove(this); |
| 89 | if (this == mCurrentVibration) { |
| 90 | doCancelVibrateLocked(); |
| 91 | startNextVibrationLocked(); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public boolean hasLongerTimeout(long millis) { |
| 97 | if (mTimeout == 0) { |
| 98 | // This is a pattern, return false to play the simple |
| 99 | // vibration. |
| 100 | return false; |
| 101 | } |
| 102 | if ((mStartTime + mTimeout) |
| 103 | < (SystemClock.uptimeMillis() + millis)) { |
| 104 | // If this vibration will end before the time passed in, let |
| 105 | // the new vibration play. |
| 106 | return false; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | HardwareService(Context context) { |
| 113 | // Reset the hardware to a default state, in case this is a runtime |
| 114 | // restart instead of a fresh boot. |
| 115 | vibratorOff(); |
| 116 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 117 | mNativePointer = init_native(); |
| 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | mContext = context; |
| 120 | PowerManager pm = (PowerManager)context.getSystemService( |
| 121 | Context.POWER_SERVICE); |
| 122 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 123 | mWakeLock.setReferenceCounted(true); |
| 124 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 125 | mVibrations = new LinkedList<Vibration>(); |
| 126 | |
Dianne Hackborn | bed30e1 | 2009-03-31 14:46:20 -0700 | [diff] [blame] | 127 | mBatteryStats = BatteryStatsService.getService(); |
| 128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | IntentFilter filter = new IntentFilter(); |
| 130 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 131 | context.registerReceiver(mIntentReceiver, filter); |
| 132 | } |
| 133 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 134 | protected void finalize() throws Throwable { |
| 135 | finalize_native(mNativePointer); |
| 136 | super.finalize(); |
| 137 | } |
| 138 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 139 | public void vibrate(long milliseconds, IBinder token) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 140 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE) |
| 141 | != PackageManager.PERMISSION_GRANTED) { |
| 142 | throw new SecurityException("Requires VIBRATE permission"); |
| 143 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 144 | if (mCurrentVibration != null |
| 145 | && mCurrentVibration.hasLongerTimeout(milliseconds)) { |
| 146 | // Ignore this vibration since the current vibration will play for |
| 147 | // longer than milliseconds. |
| 148 | return; |
| 149 | } |
| 150 | Vibration vib = new Vibration(token, milliseconds); |
| 151 | synchronized (mVibrations) { |
| 152 | removeVibrationLocked(token); |
| 153 | doCancelVibrateLocked(); |
| 154 | mCurrentVibration = vib; |
| 155 | startVibrationLocked(vib); |
| 156 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | private boolean isAll0(long[] pattern) { |
| 160 | int N = pattern.length; |
| 161 | for (int i = 0; i < N; i++) { |
| 162 | if (pattern[i] != 0) { |
| 163 | return false; |
| 164 | } |
| 165 | } |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | public void vibratePattern(long[] pattern, int repeat, IBinder token) { |
| 170 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE) |
| 171 | != PackageManager.PERMISSION_GRANTED) { |
| 172 | throw new SecurityException("Requires VIBRATE permission"); |
| 173 | } |
| 174 | // so wakelock calls will succeed |
| 175 | long identity = Binder.clearCallingIdentity(); |
| 176 | try { |
| 177 | if (false) { |
| 178 | String s = ""; |
| 179 | int N = pattern.length; |
| 180 | for (int i=0; i<N; i++) { |
| 181 | s += " " + pattern[i]; |
| 182 | } |
| 183 | Log.i(TAG, "vibrating with pattern: " + s); |
| 184 | } |
| 185 | |
| 186 | // we're running in the server so we can't fail |
| 187 | if (pattern == null || pattern.length == 0 |
| 188 | || isAll0(pattern) |
| 189 | || repeat >= pattern.length || token == null) { |
| 190 | return; |
| 191 | } |
| 192 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 193 | Vibration vib = new Vibration(token, pattern, repeat); |
| 194 | try { |
| 195 | token.linkToDeath(vib, 0); |
| 196 | } catch (RemoteException e) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | synchronized (mVibrations) { |
| 201 | removeVibrationLocked(token); |
| 202 | doCancelVibrateLocked(); |
| 203 | if (repeat >= 0) { |
| 204 | mVibrations.addFirst(vib); |
| 205 | startNextVibrationLocked(); |
| 206 | } else { |
| 207 | // A negative repeat means that this pattern is not meant |
| 208 | // to repeat. Treat it like a simple vibration. |
| 209 | mCurrentVibration = vib; |
| 210 | startVibrationLocked(vib); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | finally { |
| 215 | Binder.restoreCallingIdentity(identity); |
| 216 | } |
| 217 | } |
| 218 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 219 | public void cancelVibrate(IBinder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | mContext.enforceCallingOrSelfPermission( |
| 221 | android.Manifest.permission.VIBRATE, |
| 222 | "cancelVibrate"); |
| 223 | |
| 224 | // so wakelock calls will succeed |
| 225 | long identity = Binder.clearCallingIdentity(); |
| 226 | try { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 227 | synchronized (mVibrations) { |
| 228 | final Vibration vib = removeVibrationLocked(token); |
| 229 | if (vib == mCurrentVibration) { |
| 230 | doCancelVibrateLocked(); |
| 231 | startNextVibrationLocked(); |
| 232 | } |
| 233 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | } |
| 235 | finally { |
| 236 | Binder.restoreCallingIdentity(identity); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | public boolean getFlashlightEnabled() { |
| 241 | return Hardware.getFlashlightEnabled(); |
| 242 | } |
| 243 | |
| 244 | public void setFlashlightEnabled(boolean on) { |
| 245 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FLASHLIGHT) |
| 246 | != PackageManager.PERMISSION_GRANTED && |
| 247 | mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST) |
| 248 | != PackageManager.PERMISSION_GRANTED) { |
| 249 | throw new SecurityException("Requires FLASHLIGHT or HARDWARE_TEST permission"); |
| 250 | } |
| 251 | Hardware.setFlashlightEnabled(on); |
| 252 | } |
| 253 | |
| 254 | public void enableCameraFlash(int milliseconds) { |
| 255 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.CAMERA) |
| 256 | != PackageManager.PERMISSION_GRANTED && |
| 257 | mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST) |
| 258 | != PackageManager.PERMISSION_GRANTED) { |
| 259 | throw new SecurityException("Requires CAMERA or HARDWARE_TEST permission"); |
| 260 | } |
| 261 | Hardware.enableCameraFlash(milliseconds); |
| 262 | } |
| 263 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 264 | public void setBacklights(int brightness) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST) |
| 266 | != PackageManager.PERMISSION_GRANTED) { |
| 267 | throw new SecurityException("Requires HARDWARE_TEST permission"); |
| 268 | } |
| 269 | // Don't let applications turn the screen all the way off |
| 270 | brightness = Math.max(brightness, Power.BRIGHTNESS_DIM); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 271 | setLightBrightness_UNCHECKED(LIGHT_ID_BACKLIGHT, brightness); |
| 272 | setLightBrightness_UNCHECKED(LIGHT_ID_KEYBOARD, brightness); |
| 273 | setLightBrightness_UNCHECKED(LIGHT_ID_BUTTONS, brightness); |
Dianne Hackborn | bed30e1 | 2009-03-31 14:46:20 -0700 | [diff] [blame] | 274 | long identity = Binder.clearCallingIdentity(); |
| 275 | try { |
| 276 | mBatteryStats.noteScreenBrightness(brightness); |
| 277 | } catch (RemoteException e) { |
| 278 | Log.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e); |
| 279 | } finally { |
| 280 | Binder.restoreCallingIdentity(identity); |
| 281 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | } |
| 283 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 284 | void setLightOff_UNCHECKED(int light) { |
| 285 | setLight_native(mNativePointer, light, 0, LIGHT_FLASH_NONE, 0, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
| 287 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 288 | void setLightBrightness_UNCHECKED(int light, int brightness) { |
| 289 | int b = brightness & 0x000000ff; |
| 290 | b = 0xff000000 | (b << 16) | (b << 8) | b; |
| 291 | setLight_native(mNativePointer, light, b, LIGHT_FLASH_NONE, 0, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | } |
| 293 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 294 | void setLightColor_UNCHECKED(int light, int color) { |
| 295 | setLight_native(mNativePointer, light, color, LIGHT_FLASH_NONE, 0, 0); |
| 296 | } |
| 297 | |
| 298 | void setLightFlashing_UNCHECKED(int light, int color, int mode, int onMS, int offMS) { |
| 299 | setLight_native(mNativePointer, light, color, mode, onMS, offMS); |
| 300 | } |
| 301 | |
| 302 | public void setAttentionLight(boolean on) { |
| 303 | // Not worthy of a permission. We shouldn't have a flashlight permission. |
Joe Onorato | 95e4f70 | 2009-03-24 19:29:09 -0700 | [diff] [blame] | 304 | synchronized (this) { |
| 305 | mAttentionLightOn = on; |
| 306 | mPulsing = false; |
| 307 | setLight_native(mNativePointer, LIGHT_ID_ATTENTION, on ? 0xffffffff : 0, |
| 308 | LIGHT_FLASH_NONE, 0, 0); |
| 309 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Joe Onorato | 95e4f70 | 2009-03-24 19:29:09 -0700 | [diff] [blame] | 312 | public void pulseBreathingLight() { |
| 313 | synchronized (this) { |
| 314 | // HACK: Added at the last minute of cupcake -- design this better; |
| 315 | // Don't reuse the attention light -- make another one. |
| 316 | if (false) { |
| 317 | Log.d(TAG, "pulseBreathingLight mAttentionLightOn=" + mAttentionLightOn |
| 318 | + " mPulsing=" + mPulsing); |
| 319 | } |
| 320 | if (!mAttentionLightOn && !mPulsing) { |
| 321 | mPulsing = true; |
| 322 | setLight_native(mNativePointer, LIGHT_ID_ATTENTION, 0xff101010, |
| 323 | LIGHT_FLASH_NONE, 0, 0); |
| 324 | mH.sendMessageDelayed(Message.obtain(mH, 1), 3000); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | private Handler mH = new Handler() { |
| 330 | @Override |
| 331 | public void handleMessage(Message msg) { |
| 332 | synchronized (this) { |
| 333 | if (false) { |
| 334 | Log.d(TAG, "pulse cleanup handler firing mPulsing=" + mPulsing); |
| 335 | } |
| 336 | if (mPulsing) { |
| 337 | mPulsing = false; |
| 338 | setLight_native(mNativePointer, LIGHT_ID_ATTENTION, |
| 339 | mAttentionLightOn ? 0xffffffff : 0, |
| 340 | LIGHT_FLASH_NONE, 0, 0); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | }; |
| 345 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 346 | private final Runnable mVibrationRunnable = new Runnable() { |
| 347 | public void run() { |
| 348 | synchronized (mVibrations) { |
| 349 | doCancelVibrateLocked(); |
| 350 | startNextVibrationLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 352 | } |
| 353 | }; |
| 354 | |
| 355 | // Lock held on mVibrations |
| 356 | private void doCancelVibrateLocked() { |
| 357 | if (mThread != null) { |
| 358 | synchronized (mThread) { |
| 359 | mThread.mDone = true; |
| 360 | mThread.notify(); |
| 361 | } |
| 362 | mThread = null; |
| 363 | } |
| 364 | vibratorOff(); |
| 365 | mH.removeCallbacks(mVibrationRunnable); |
| 366 | } |
| 367 | |
| 368 | // Lock held on mVibrations |
| 369 | private void startNextVibrationLocked() { |
| 370 | if (mVibrations.size() <= 0) { |
| 371 | return; |
| 372 | } |
| 373 | mCurrentVibration = mVibrations.getFirst(); |
| 374 | startVibrationLocked(mCurrentVibration); |
| 375 | } |
| 376 | |
| 377 | // Lock held on mVibrations |
| 378 | private void startVibrationLocked(final Vibration vib) { |
| 379 | if (vib.mTimeout != 0) { |
| 380 | vibratorOn(vib.mTimeout); |
| 381 | mH.postDelayed(mVibrationRunnable, vib.mTimeout); |
| 382 | } else { |
| 383 | // mThread better be null here. doCancelVibrate should always be |
| 384 | // called before startNextVibrationLocked or startVibrationLocked. |
| 385 | mThread = new VibrateThread(vib); |
| 386 | mThread.start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 390 | // Lock held on mVibrations |
| 391 | private Vibration removeVibrationLocked(IBinder token) { |
| 392 | ListIterator<Vibration> iter = mVibrations.listIterator(0); |
| 393 | while (iter.hasNext()) { |
| 394 | Vibration vib = iter.next(); |
| 395 | if (vib.mToken == token) { |
| 396 | iter.remove(); |
| 397 | return vib; |
| 398 | } |
| 399 | } |
| 400 | // We might be looking for a simple vibration which is only stored in |
| 401 | // mCurrentVibration. |
| 402 | if (mCurrentVibration != null && mCurrentVibration.mToken == token) { |
| 403 | return mCurrentVibration; |
| 404 | } |
| 405 | return null; |
| 406 | } |
| 407 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | private class VibrateThread extends Thread { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 409 | final Vibration mVibration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | boolean mDone; |
| 411 | |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 412 | VibrateThread(Vibration vib) { |
| 413 | mVibration = vib; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | mWakeLock.acquire(); |
| 415 | } |
| 416 | |
| 417 | private void delay(long duration) { |
| 418 | if (duration > 0) { |
| 419 | long bedtime = SystemClock.uptimeMillis(); |
| 420 | do { |
| 421 | try { |
| 422 | this.wait(duration); |
| 423 | } |
| 424 | catch (InterruptedException e) { |
| 425 | } |
| 426 | if (mDone) { |
| 427 | break; |
| 428 | } |
| 429 | duration = duration |
| 430 | - SystemClock.uptimeMillis() - bedtime; |
| 431 | } while (duration > 0); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | public void run() { |
| 436 | Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY); |
| 437 | synchronized (this) { |
| 438 | int index = 0; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 439 | long[] pattern = mVibration.mPattern; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | int len = pattern.length; |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 441 | int repeat = mVibration.mRepeat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | long duration = 0; |
| 443 | |
| 444 | while (!mDone) { |
| 445 | // add off-time duration to any accumulated on-time duration |
| 446 | if (index < len) { |
| 447 | duration += pattern[index++]; |
| 448 | } |
| 449 | |
| 450 | // sleep until it is time to start the vibrator |
| 451 | delay(duration); |
| 452 | if (mDone) { |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | if (index < len) { |
| 457 | // read on-time duration and start the vibrator |
| 458 | // duration is saved for delay() at top of loop |
| 459 | duration = pattern[index++]; |
| 460 | if (duration > 0) { |
| 461 | HardwareService.this.vibratorOn(duration); |
| 462 | } |
| 463 | } else { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 464 | if (repeat < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | break; |
| 466 | } else { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 467 | index = repeat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | duration = 0; |
| 469 | } |
| 470 | } |
| 471 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | mWakeLock.release(); |
| 473 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 474 | synchronized (mVibrations) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | if (mThread == this) { |
| 476 | mThread = null; |
| 477 | } |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 478 | if (!mDone) { |
| 479 | // If this vibration finished naturally, start the next |
| 480 | // vibration. |
| 481 | mVibrations.remove(mVibration); |
| 482 | startNextVibrationLocked(); |
| 483 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | }; |
| 487 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 489 | public void onReceive(Context context, Intent intent) { |
| 490 | if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { |
Patrick Scott | 18dd5f0 | 2009-07-02 11:31:12 -0400 | [diff] [blame^] | 491 | synchronized (mVibrations) { |
| 492 | doCancelVibrateLocked(); |
| 493 | mVibrations.clear(); |
| 494 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | }; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 498 | |
| 499 | private static native int init_native(); |
| 500 | private static native void finalize_native(int ptr); |
| 501 | |
| 502 | private static native void setLight_native(int ptr, int light, int color, int mode, |
| 503 | int onMS, int offMS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | |
Dianne Hackborn | bed30e1 | 2009-03-31 14:46:20 -0700 | [diff] [blame] | 505 | private final Context mContext; |
| 506 | private final PowerManager.WakeLock mWakeLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | |
Dianne Hackborn | bed30e1 | 2009-03-31 14:46:20 -0700 | [diff] [blame] | 508 | private final IBatteryStats mBatteryStats; |
| 509 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | volatile VibrateThread mThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 512 | private int mNativePointer; |
| 513 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | native static void vibratorOn(long milliseconds); |
| 515 | native static void vibratorOff(); |
| 516 | } |