blob: b1d58ce8d1b010a6d8ec3037446a2af6ff99a4d8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
Dianne Hackbornbed30e12009-03-31 14:46:20 -070019import com.android.internal.app.IBatteryStats;
20import com.android.server.am.BatteryStatsService;
21
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.content.pm.PackageManager;
Joe Onorato95e4f702009-03-24 19:29:09 -070027import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.os.Hardware;
29import android.os.IHardwareService;
Joe Onorato95e4f702009-03-24 19:29:09 -070030import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.Power;
32import android.os.PowerManager;
33import android.os.Process;
34import android.os.RemoteException;
35import android.os.IBinder;
36import android.os.Binder;
37import android.os.SystemClock;
38import android.util.Log;
39
Patrick Scott18dd5f02009-07-02 11:31:12 -040040import java.util.LinkedList;
41import java.util.ListIterator;
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043public class HardwareService extends IHardwareService.Stub {
44 private static final String TAG = "HardwareService";
45
The Android Open Source Project10592532009-03-18 17:39:46 -070046 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 Scott18dd5f02009-07-02 11:31:12 -040056 private final LinkedList<Vibration> mVibrations;
57 private Vibration mCurrentVibration;
58
Joe Onorato95e4f702009-03-24 19:29:09 -070059 private boolean mAttentionLightOn;
60 private boolean mPulsing;
61
Patrick Scott18dd5f02009-07-02 11:31:12 -040062 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 Project9066cfe2009-03-03 19:31:44 -0800112 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 Project10592532009-03-18 17:39:46 -0700117 mNativePointer = init_native();
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 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 Scott18dd5f02009-07-02 11:31:12 -0400125 mVibrations = new LinkedList<Vibration>();
126
Dianne Hackbornbed30e12009-03-31 14:46:20 -0700127 mBatteryStats = BatteryStatsService.getService();
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 IntentFilter filter = new IntentFilter();
130 filter.addAction(Intent.ACTION_SCREEN_OFF);
131 context.registerReceiver(mIntentReceiver, filter);
132 }
133
The Android Open Source Project10592532009-03-18 17:39:46 -0700134 protected void finalize() throws Throwable {
135 finalize_native(mNativePointer);
136 super.finalize();
137 }
138
Patrick Scott18dd5f02009-07-02 11:31:12 -0400139 public void vibrate(long milliseconds, IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700140 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
141 != PackageManager.PERMISSION_GRANTED) {
142 throw new SecurityException("Requires VIBRATE permission");
143 }
Patrick Scott24f10762009-08-19 09:03:56 -0400144 // We're running in the system server so we cannot crash. Check for a
145 // timeout of 0 or negative. This will ensure that a vibration has
146 // either a timeout of > 0 or a non-null pattern.
147 if (milliseconds <= 0 || (mCurrentVibration != null
148 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400149 // Ignore this vibration since the current vibration will play for
150 // longer than milliseconds.
151 return;
152 }
153 Vibration vib = new Vibration(token, milliseconds);
154 synchronized (mVibrations) {
155 removeVibrationLocked(token);
156 doCancelVibrateLocked();
157 mCurrentVibration = vib;
158 startVibrationLocked(vib);
159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 }
161
162 private boolean isAll0(long[] pattern) {
163 int N = pattern.length;
164 for (int i = 0; i < N; i++) {
165 if (pattern[i] != 0) {
166 return false;
167 }
168 }
169 return true;
170 }
171
172 public void vibratePattern(long[] pattern, int repeat, IBinder token) {
173 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
174 != PackageManager.PERMISSION_GRANTED) {
175 throw new SecurityException("Requires VIBRATE permission");
176 }
177 // so wakelock calls will succeed
178 long identity = Binder.clearCallingIdentity();
179 try {
180 if (false) {
181 String s = "";
182 int N = pattern.length;
183 for (int i=0; i<N; i++) {
184 s += " " + pattern[i];
185 }
186 Log.i(TAG, "vibrating with pattern: " + s);
187 }
188
189 // we're running in the server so we can't fail
190 if (pattern == null || pattern.length == 0
191 || isAll0(pattern)
192 || repeat >= pattern.length || token == null) {
193 return;
194 }
195
Patrick Scott18dd5f02009-07-02 11:31:12 -0400196 Vibration vib = new Vibration(token, pattern, repeat);
197 try {
198 token.linkToDeath(vib, 0);
199 } catch (RemoteException e) {
200 return;
201 }
202
203 synchronized (mVibrations) {
204 removeVibrationLocked(token);
205 doCancelVibrateLocked();
206 if (repeat >= 0) {
207 mVibrations.addFirst(vib);
208 startNextVibrationLocked();
209 } else {
210 // A negative repeat means that this pattern is not meant
211 // to repeat. Treat it like a simple vibration.
212 mCurrentVibration = vib;
213 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 }
216 }
217 finally {
218 Binder.restoreCallingIdentity(identity);
219 }
220 }
221
Patrick Scott18dd5f02009-07-02 11:31:12 -0400222 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 mContext.enforceCallingOrSelfPermission(
224 android.Manifest.permission.VIBRATE,
225 "cancelVibrate");
226
227 // so wakelock calls will succeed
228 long identity = Binder.clearCallingIdentity();
229 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400230 synchronized (mVibrations) {
231 final Vibration vib = removeVibrationLocked(token);
232 if (vib == mCurrentVibration) {
233 doCancelVibrateLocked();
234 startNextVibrationLocked();
235 }
236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 }
238 finally {
239 Binder.restoreCallingIdentity(identity);
240 }
241 }
242
243 public boolean getFlashlightEnabled() {
244 return Hardware.getFlashlightEnabled();
245 }
246
247 public void setFlashlightEnabled(boolean on) {
248 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FLASHLIGHT)
249 != PackageManager.PERMISSION_GRANTED &&
250 mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST)
251 != PackageManager.PERMISSION_GRANTED) {
252 throw new SecurityException("Requires FLASHLIGHT or HARDWARE_TEST permission");
253 }
254 Hardware.setFlashlightEnabled(on);
255 }
256
257 public void enableCameraFlash(int milliseconds) {
258 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.CAMERA)
259 != PackageManager.PERMISSION_GRANTED &&
260 mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST)
261 != PackageManager.PERMISSION_GRANTED) {
262 throw new SecurityException("Requires CAMERA or HARDWARE_TEST permission");
263 }
264 Hardware.enableCameraFlash(milliseconds);
265 }
266
The Android Open Source Project10592532009-03-18 17:39:46 -0700267 void setLightOff_UNCHECKED(int light) {
268 setLight_native(mNativePointer, light, 0, LIGHT_FLASH_NONE, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 }
270
The Android Open Source Project10592532009-03-18 17:39:46 -0700271 void setLightBrightness_UNCHECKED(int light, int brightness) {
272 int b = brightness & 0x000000ff;
273 b = 0xff000000 | (b << 16) | (b << 8) | b;
274 setLight_native(mNativePointer, light, b, LIGHT_FLASH_NONE, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276
The Android Open Source Project10592532009-03-18 17:39:46 -0700277 void setLightColor_UNCHECKED(int light, int color) {
278 setLight_native(mNativePointer, light, color, LIGHT_FLASH_NONE, 0, 0);
279 }
280
281 void setLightFlashing_UNCHECKED(int light, int color, int mode, int onMS, int offMS) {
282 setLight_native(mNativePointer, light, color, mode, onMS, offMS);
283 }
284
285 public void setAttentionLight(boolean on) {
286 // Not worthy of a permission. We shouldn't have a flashlight permission.
Joe Onorato95e4f702009-03-24 19:29:09 -0700287 synchronized (this) {
288 mAttentionLightOn = on;
289 mPulsing = false;
290 setLight_native(mNativePointer, LIGHT_ID_ATTENTION, on ? 0xffffffff : 0,
291 LIGHT_FLASH_NONE, 0, 0);
292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 }
294
Joe Onorato95e4f702009-03-24 19:29:09 -0700295 public void pulseBreathingLight() {
296 synchronized (this) {
297 // HACK: Added at the last minute of cupcake -- design this better;
298 // Don't reuse the attention light -- make another one.
299 if (false) {
300 Log.d(TAG, "pulseBreathingLight mAttentionLightOn=" + mAttentionLightOn
301 + " mPulsing=" + mPulsing);
302 }
303 if (!mAttentionLightOn && !mPulsing) {
304 mPulsing = true;
305 setLight_native(mNativePointer, LIGHT_ID_ATTENTION, 0xff101010,
306 LIGHT_FLASH_NONE, 0, 0);
307 mH.sendMessageDelayed(Message.obtain(mH, 1), 3000);
308 }
309 }
310 }
311
312 private Handler mH = new Handler() {
313 @Override
314 public void handleMessage(Message msg) {
315 synchronized (this) {
316 if (false) {
317 Log.d(TAG, "pulse cleanup handler firing mPulsing=" + mPulsing);
318 }
319 if (mPulsing) {
320 mPulsing = false;
321 setLight_native(mNativePointer, LIGHT_ID_ATTENTION,
322 mAttentionLightOn ? 0xffffffff : 0,
323 LIGHT_FLASH_NONE, 0, 0);
324 }
325 }
326 }
327 };
328
Patrick Scott18dd5f02009-07-02 11:31:12 -0400329 private final Runnable mVibrationRunnable = new Runnable() {
330 public void run() {
331 synchronized (mVibrations) {
332 doCancelVibrateLocked();
333 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400335 }
336 };
337
338 // Lock held on mVibrations
339 private void doCancelVibrateLocked() {
340 if (mThread != null) {
341 synchronized (mThread) {
342 mThread.mDone = true;
343 mThread.notify();
344 }
345 mThread = null;
346 }
347 vibratorOff();
348 mH.removeCallbacks(mVibrationRunnable);
349 }
350
351 // Lock held on mVibrations
352 private void startNextVibrationLocked() {
353 if (mVibrations.size() <= 0) {
354 return;
355 }
356 mCurrentVibration = mVibrations.getFirst();
357 startVibrationLocked(mCurrentVibration);
358 }
359
360 // Lock held on mVibrations
361 private void startVibrationLocked(final Vibration vib) {
362 if (vib.mTimeout != 0) {
363 vibratorOn(vib.mTimeout);
364 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
365 } else {
366 // mThread better be null here. doCancelVibrate should always be
367 // called before startNextVibrationLocked or startVibrationLocked.
368 mThread = new VibrateThread(vib);
369 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
371 }
372
Patrick Scott18dd5f02009-07-02 11:31:12 -0400373 // Lock held on mVibrations
374 private Vibration removeVibrationLocked(IBinder token) {
375 ListIterator<Vibration> iter = mVibrations.listIterator(0);
376 while (iter.hasNext()) {
377 Vibration vib = iter.next();
378 if (vib.mToken == token) {
379 iter.remove();
380 return vib;
381 }
382 }
383 // We might be looking for a simple vibration which is only stored in
384 // mCurrentVibration.
385 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
386 return mCurrentVibration;
387 }
388 return null;
389 }
390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400392 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 boolean mDone;
394
Patrick Scott18dd5f02009-07-02 11:31:12 -0400395 VibrateThread(Vibration vib) {
396 mVibration = vib;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 mWakeLock.acquire();
398 }
399
400 private void delay(long duration) {
401 if (duration > 0) {
402 long bedtime = SystemClock.uptimeMillis();
403 do {
404 try {
405 this.wait(duration);
406 }
407 catch (InterruptedException e) {
408 }
409 if (mDone) {
410 break;
411 }
412 duration = duration
413 - SystemClock.uptimeMillis() - bedtime;
414 } while (duration > 0);
415 }
416 }
417
418 public void run() {
419 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
420 synchronized (this) {
421 int index = 0;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400422 long[] pattern = mVibration.mPattern;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 int len = pattern.length;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400424 int repeat = mVibration.mRepeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 long duration = 0;
426
427 while (!mDone) {
428 // add off-time duration to any accumulated on-time duration
429 if (index < len) {
430 duration += pattern[index++];
431 }
432
433 // sleep until it is time to start the vibrator
434 delay(duration);
435 if (mDone) {
436 break;
437 }
438
439 if (index < len) {
440 // read on-time duration and start the vibrator
441 // duration is saved for delay() at top of loop
442 duration = pattern[index++];
443 if (duration > 0) {
444 HardwareService.this.vibratorOn(duration);
445 }
446 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400447 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 break;
449 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400450 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 duration = 0;
452 }
453 }
454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 mWakeLock.release();
456 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400457 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 if (mThread == this) {
459 mThread = null;
460 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400461 if (!mDone) {
462 // If this vibration finished naturally, start the next
463 // vibration.
464 mVibrations.remove(mVibration);
465 startNextVibrationLocked();
466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
468 }
469 };
470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
472 public void onReceive(Context context, Intent intent) {
473 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400474 synchronized (mVibrations) {
475 doCancelVibrateLocked();
476 mVibrations.clear();
477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
479 }
480 };
The Android Open Source Project10592532009-03-18 17:39:46 -0700481
482 private static native int init_native();
483 private static native void finalize_native(int ptr);
484
485 private static native void setLight_native(int ptr, int light, int color, int mode,
486 int onMS, int offMS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
Dianne Hackbornbed30e12009-03-31 14:46:20 -0700488 private final Context mContext;
489 private final PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490
Dianne Hackbornbed30e12009-03-31 14:46:20 -0700491 private final IBatteryStats mBatteryStats;
492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 volatile VibrateThread mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494
The Android Open Source Project10592532009-03-18 17:39:46 -0700495 private int mNativePointer;
496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 native static void vibratorOn(long milliseconds);
498 native static void vibratorOff();
499}