blob: 7597f851e3a6e13bb38684b49291b71b2fdac9ed [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 Scott18dd5f02009-07-02 11:31:12 -0400144 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 Project9066cfe2009-03-03 19:31:44 -0800157 }
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 Scott18dd5f02009-07-02 11:31:12 -0400193 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 Project9066cfe2009-03-03 19:31:44 -0800211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
213 }
214 finally {
215 Binder.restoreCallingIdentity(identity);
216 }
217 }
218
Patrick Scott18dd5f02009-07-02 11:31:12 -0400219 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 mContext.enforceCallingOrSelfPermission(
221 android.Manifest.permission.VIBRATE,
222 "cancelVibrate");
223
224 // so wakelock calls will succeed
225 long identity = Binder.clearCallingIdentity();
226 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400227 synchronized (mVibrations) {
228 final Vibration vib = removeVibrationLocked(token);
229 if (vib == mCurrentVibration) {
230 doCancelVibrateLocked();
231 startNextVibrationLocked();
232 }
233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 }
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 Project10592532009-03-18 17:39:46 -0700264 public void setBacklights(int brightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 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 Project10592532009-03-18 17:39:46 -0700271 setLightBrightness_UNCHECKED(LIGHT_ID_BACKLIGHT, brightness);
272 setLightBrightness_UNCHECKED(LIGHT_ID_KEYBOARD, brightness);
273 setLightBrightness_UNCHECKED(LIGHT_ID_BUTTONS, brightness);
Dianne Hackbornbed30e12009-03-31 14:46:20 -0700274 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 Project9066cfe2009-03-03 19:31:44 -0800282 }
283
The Android Open Source Project10592532009-03-18 17:39:46 -0700284 void setLightOff_UNCHECKED(int light) {
285 setLight_native(mNativePointer, light, 0, LIGHT_FLASH_NONE, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 }
287
The Android Open Source Project10592532009-03-18 17:39:46 -0700288 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 Project9066cfe2009-03-03 19:31:44 -0800292 }
293
The Android Open Source Project10592532009-03-18 17:39:46 -0700294 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 Onorato95e4f702009-03-24 19:29:09 -0700304 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 Project9066cfe2009-03-03 19:31:44 -0800310 }
311
Joe Onorato95e4f702009-03-24 19:29:09 -0700312 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 Scott18dd5f02009-07-02 11:31:12 -0400346 private final Runnable mVibrationRunnable = new Runnable() {
347 public void run() {
348 synchronized (mVibrations) {
349 doCancelVibrateLocked();
350 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400352 }
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 Project9066cfe2009-03-03 19:31:44 -0800387 }
388 }
389
Patrick Scott18dd5f02009-07-02 11:31:12 -0400390 // 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 Project9066cfe2009-03-03 19:31:44 -0800408 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400409 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 boolean mDone;
411
Patrick Scott18dd5f02009-07-02 11:31:12 -0400412 VibrateThread(Vibration vib) {
413 mVibration = vib;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 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 Scott18dd5f02009-07-02 11:31:12 -0400439 long[] pattern = mVibration.mPattern;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 int len = pattern.length;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400441 int repeat = mVibration.mRepeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 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 Scott18dd5f02009-07-02 11:31:12 -0400464 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 break;
466 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400467 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 duration = 0;
469 }
470 }
471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 mWakeLock.release();
473 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400474 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 if (mThread == this) {
476 mThread = null;
477 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400478 if (!mDone) {
479 // If this vibration finished naturally, start the next
480 // vibration.
481 mVibrations.remove(mVibration);
482 startNextVibrationLocked();
483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
485 }
486 };
487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
489 public void onReceive(Context context, Intent intent) {
490 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400491 synchronized (mVibrations) {
492 doCancelVibrateLocked();
493 mVibrations.clear();
494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496 }
497 };
The Android Open Source Project10592532009-03-18 17:39:46 -0700498
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 Project9066cfe2009-03-03 19:31:44 -0800504
Dianne Hackbornbed30e12009-03-31 14:46:20 -0700505 private final Context mContext;
506 private final PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507
Dianne Hackbornbed30e12009-03-31 14:46:20 -0700508 private final IBatteryStats mBatteryStats;
509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 volatile VibrateThread mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
The Android Open Source Project10592532009-03-18 17:39:46 -0700512 private int mNativePointer;
513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 native static void vibratorOn(long milliseconds);
515 native static void vibratorOff();
516}