blob: 29c13e04f7f833f49af0810d47c0193dee4ef4f2 [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
Dan Murphy951764b2009-08-27 14:59:03 -050062 private boolean mAutoBrightnessAvailable;
63
Patrick Scott18dd5f02009-07-02 11:31:12 -040064 private class Vibration implements IBinder.DeathRecipient {
65 private final IBinder mToken;
66 private final long mTimeout;
67 private final long mStartTime;
68 private final long[] mPattern;
69 private final int mRepeat;
70
71 Vibration(IBinder token, long millis) {
72 this(token, millis, null, 0);
73 }
74
75 Vibration(IBinder token, long[] pattern, int repeat) {
76 this(token, 0, pattern, repeat);
77 }
78
79 private Vibration(IBinder token, long millis, long[] pattern,
80 int repeat) {
81 mToken = token;
82 mTimeout = millis;
83 mStartTime = SystemClock.uptimeMillis();
84 mPattern = pattern;
85 mRepeat = repeat;
86 }
87
88 public void binderDied() {
89 synchronized (mVibrations) {
90 mVibrations.remove(this);
91 if (this == mCurrentVibration) {
92 doCancelVibrateLocked();
93 startNextVibrationLocked();
94 }
95 }
96 }
97
98 public boolean hasLongerTimeout(long millis) {
99 if (mTimeout == 0) {
100 // This is a pattern, return false to play the simple
101 // vibration.
102 return false;
103 }
104 if ((mStartTime + mTimeout)
105 < (SystemClock.uptimeMillis() + millis)) {
106 // If this vibration will end before the time passed in, let
107 // the new vibration play.
108 return false;
109 }
110 return true;
111 }
112 }
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 HardwareService(Context context) {
115 // Reset the hardware to a default state, in case this is a runtime
116 // restart instead of a fresh boot.
117 vibratorOff();
118
The Android Open Source Project10592532009-03-18 17:39:46 -0700119 mNativePointer = init_native();
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 mContext = context;
122 PowerManager pm = (PowerManager)context.getSystemService(
123 Context.POWER_SERVICE);
124 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
125 mWakeLock.setReferenceCounted(true);
126
Patrick Scott18dd5f02009-07-02 11:31:12 -0400127 mVibrations = new LinkedList<Vibration>();
128
Dianne Hackbornbed30e12009-03-31 14:46:20 -0700129 mBatteryStats = BatteryStatsService.getService();
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 IntentFilter filter = new IntentFilter();
132 filter.addAction(Intent.ACTION_SCREEN_OFF);
133 context.registerReceiver(mIntentReceiver, filter);
Dan Murphy951764b2009-08-27 14:59:03 -0500134
135 mAutoBrightnessAvailable = context.getResources().getBoolean(
136 com.android.internal.R.bool.config_automatic_brightness_available);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
138
The Android Open Source Project10592532009-03-18 17:39:46 -0700139 protected void finalize() throws Throwable {
140 finalize_native(mNativePointer);
141 super.finalize();
142 }
143
Patrick Scott18dd5f02009-07-02 11:31:12 -0400144 public void vibrate(long milliseconds, IBinder token) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700145 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
146 != PackageManager.PERMISSION_GRANTED) {
147 throw new SecurityException("Requires VIBRATE permission");
148 }
Patrick Scott24f10762009-08-19 09:03:56 -0400149 // We're running in the system server so we cannot crash. Check for a
150 // timeout of 0 or negative. This will ensure that a vibration has
151 // either a timeout of > 0 or a non-null pattern.
152 if (milliseconds <= 0 || (mCurrentVibration != null
153 && mCurrentVibration.hasLongerTimeout(milliseconds))) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400154 // Ignore this vibration since the current vibration will play for
155 // longer than milliseconds.
156 return;
157 }
158 Vibration vib = new Vibration(token, milliseconds);
159 synchronized (mVibrations) {
160 removeVibrationLocked(token);
161 doCancelVibrateLocked();
162 mCurrentVibration = vib;
163 startVibrationLocked(vib);
164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 }
166
167 private boolean isAll0(long[] pattern) {
168 int N = pattern.length;
169 for (int i = 0; i < N; i++) {
170 if (pattern[i] != 0) {
171 return false;
172 }
173 }
174 return true;
175 }
176
177 public void vibratePattern(long[] pattern, int repeat, IBinder token) {
178 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
179 != PackageManager.PERMISSION_GRANTED) {
180 throw new SecurityException("Requires VIBRATE permission");
181 }
182 // so wakelock calls will succeed
183 long identity = Binder.clearCallingIdentity();
184 try {
185 if (false) {
186 String s = "";
187 int N = pattern.length;
188 for (int i=0; i<N; i++) {
189 s += " " + pattern[i];
190 }
191 Log.i(TAG, "vibrating with pattern: " + s);
192 }
193
194 // we're running in the server so we can't fail
195 if (pattern == null || pattern.length == 0
196 || isAll0(pattern)
197 || repeat >= pattern.length || token == null) {
198 return;
199 }
200
Patrick Scott18dd5f02009-07-02 11:31:12 -0400201 Vibration vib = new Vibration(token, pattern, repeat);
202 try {
203 token.linkToDeath(vib, 0);
204 } catch (RemoteException e) {
205 return;
206 }
207
208 synchronized (mVibrations) {
209 removeVibrationLocked(token);
210 doCancelVibrateLocked();
211 if (repeat >= 0) {
212 mVibrations.addFirst(vib);
213 startNextVibrationLocked();
214 } else {
215 // A negative repeat means that this pattern is not meant
216 // to repeat. Treat it like a simple vibration.
217 mCurrentVibration = vib;
218 startVibrationLocked(vib);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
221 }
222 finally {
223 Binder.restoreCallingIdentity(identity);
224 }
225 }
226
Patrick Scott18dd5f02009-07-02 11:31:12 -0400227 public void cancelVibrate(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 mContext.enforceCallingOrSelfPermission(
229 android.Manifest.permission.VIBRATE,
230 "cancelVibrate");
231
232 // so wakelock calls will succeed
233 long identity = Binder.clearCallingIdentity();
234 try {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400235 synchronized (mVibrations) {
236 final Vibration vib = removeVibrationLocked(token);
237 if (vib == mCurrentVibration) {
238 doCancelVibrateLocked();
239 startNextVibrationLocked();
240 }
241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 }
243 finally {
244 Binder.restoreCallingIdentity(identity);
245 }
246 }
247
248 public boolean getFlashlightEnabled() {
249 return Hardware.getFlashlightEnabled();
250 }
251
252 public void setFlashlightEnabled(boolean on) {
253 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FLASHLIGHT)
254 != PackageManager.PERMISSION_GRANTED &&
255 mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST)
256 != PackageManager.PERMISSION_GRANTED) {
257 throw new SecurityException("Requires FLASHLIGHT or HARDWARE_TEST permission");
258 }
259 Hardware.setFlashlightEnabled(on);
260 }
261
262 public void enableCameraFlash(int milliseconds) {
263 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.CAMERA)
264 != PackageManager.PERMISSION_GRANTED &&
265 mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST)
266 != PackageManager.PERMISSION_GRANTED) {
267 throw new SecurityException("Requires CAMERA or HARDWARE_TEST permission");
268 }
269 Hardware.enableCameraFlash(milliseconds);
270 }
271
The Android Open Source Project10592532009-03-18 17:39:46 -0700272 void setLightOff_UNCHECKED(int light) {
273 setLight_native(mNativePointer, light, 0, LIGHT_FLASH_NONE, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275
The Android Open Source Project10592532009-03-18 17:39:46 -0700276 void setLightBrightness_UNCHECKED(int light, int brightness) {
277 int b = brightness & 0x000000ff;
278 b = 0xff000000 | (b << 16) | (b << 8) | b;
279 setLight_native(mNativePointer, light, b, LIGHT_FLASH_NONE, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281
The Android Open Source Project10592532009-03-18 17:39:46 -0700282 void setLightColor_UNCHECKED(int light, int color) {
283 setLight_native(mNativePointer, light, color, LIGHT_FLASH_NONE, 0, 0);
284 }
285
286 void setLightFlashing_UNCHECKED(int light, int color, int mode, int onMS, int offMS) {
287 setLight_native(mNativePointer, light, color, mode, onMS, offMS);
288 }
289
Dan Murphy951764b2009-08-27 14:59:03 -0500290 void setAutoBrightness_UNCHECKED(boolean on) {
291 if (mAutoBrightnessAvailable) {
292 setAutoBrightness_native(mNativePointer, on);
293 }
294 }
295
The Android Open Source Project10592532009-03-18 17:39:46 -0700296 public void setAttentionLight(boolean on) {
297 // Not worthy of a permission. We shouldn't have a flashlight permission.
Joe Onorato95e4f702009-03-24 19:29:09 -0700298 synchronized (this) {
299 mAttentionLightOn = on;
300 mPulsing = false;
301 setLight_native(mNativePointer, LIGHT_ID_ATTENTION, on ? 0xffffffff : 0,
302 LIGHT_FLASH_NONE, 0, 0);
303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 }
305
Joe Onorato95e4f702009-03-24 19:29:09 -0700306 public void pulseBreathingLight() {
307 synchronized (this) {
308 // HACK: Added at the last minute of cupcake -- design this better;
309 // Don't reuse the attention light -- make another one.
310 if (false) {
311 Log.d(TAG, "pulseBreathingLight mAttentionLightOn=" + mAttentionLightOn
312 + " mPulsing=" + mPulsing);
313 }
314 if (!mAttentionLightOn && !mPulsing) {
315 mPulsing = true;
316 setLight_native(mNativePointer, LIGHT_ID_ATTENTION, 0xff101010,
317 LIGHT_FLASH_NONE, 0, 0);
318 mH.sendMessageDelayed(Message.obtain(mH, 1), 3000);
319 }
320 }
321 }
322
323 private Handler mH = new Handler() {
324 @Override
325 public void handleMessage(Message msg) {
326 synchronized (this) {
327 if (false) {
328 Log.d(TAG, "pulse cleanup handler firing mPulsing=" + mPulsing);
329 }
330 if (mPulsing) {
331 mPulsing = false;
332 setLight_native(mNativePointer, LIGHT_ID_ATTENTION,
333 mAttentionLightOn ? 0xffffffff : 0,
334 LIGHT_FLASH_NONE, 0, 0);
335 }
336 }
337 }
338 };
339
Patrick Scott18dd5f02009-07-02 11:31:12 -0400340 private final Runnable mVibrationRunnable = new Runnable() {
341 public void run() {
342 synchronized (mVibrations) {
343 doCancelVibrateLocked();
344 startNextVibrationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400346 }
347 };
348
349 // Lock held on mVibrations
350 private void doCancelVibrateLocked() {
351 if (mThread != null) {
352 synchronized (mThread) {
353 mThread.mDone = true;
354 mThread.notify();
355 }
356 mThread = null;
357 }
358 vibratorOff();
359 mH.removeCallbacks(mVibrationRunnable);
360 }
361
362 // Lock held on mVibrations
363 private void startNextVibrationLocked() {
364 if (mVibrations.size() <= 0) {
365 return;
366 }
367 mCurrentVibration = mVibrations.getFirst();
368 startVibrationLocked(mCurrentVibration);
369 }
370
371 // Lock held on mVibrations
372 private void startVibrationLocked(final Vibration vib) {
373 if (vib.mTimeout != 0) {
374 vibratorOn(vib.mTimeout);
375 mH.postDelayed(mVibrationRunnable, vib.mTimeout);
376 } else {
377 // mThread better be null here. doCancelVibrate should always be
378 // called before startNextVibrationLocked or startVibrationLocked.
379 mThread = new VibrateThread(vib);
380 mThread.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382 }
383
Patrick Scott18dd5f02009-07-02 11:31:12 -0400384 // Lock held on mVibrations
385 private Vibration removeVibrationLocked(IBinder token) {
386 ListIterator<Vibration> iter = mVibrations.listIterator(0);
387 while (iter.hasNext()) {
388 Vibration vib = iter.next();
389 if (vib.mToken == token) {
390 iter.remove();
391 return vib;
392 }
393 }
394 // We might be looking for a simple vibration which is only stored in
395 // mCurrentVibration.
396 if (mCurrentVibration != null && mCurrentVibration.mToken == token) {
397 return mCurrentVibration;
398 }
399 return null;
400 }
401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 private class VibrateThread extends Thread {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400403 final Vibration mVibration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 boolean mDone;
405
Patrick Scott18dd5f02009-07-02 11:31:12 -0400406 VibrateThread(Vibration vib) {
407 mVibration = vib;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 mWakeLock.acquire();
409 }
410
411 private void delay(long duration) {
412 if (duration > 0) {
413 long bedtime = SystemClock.uptimeMillis();
414 do {
415 try {
416 this.wait(duration);
417 }
418 catch (InterruptedException e) {
419 }
420 if (mDone) {
421 break;
422 }
423 duration = duration
424 - SystemClock.uptimeMillis() - bedtime;
425 } while (duration > 0);
426 }
427 }
428
429 public void run() {
430 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
431 synchronized (this) {
432 int index = 0;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400433 long[] pattern = mVibration.mPattern;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 int len = pattern.length;
Patrick Scott18dd5f02009-07-02 11:31:12 -0400435 int repeat = mVibration.mRepeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 long duration = 0;
437
438 while (!mDone) {
439 // add off-time duration to any accumulated on-time duration
440 if (index < len) {
441 duration += pattern[index++];
442 }
443
444 // sleep until it is time to start the vibrator
445 delay(duration);
446 if (mDone) {
447 break;
448 }
449
450 if (index < len) {
451 // read on-time duration and start the vibrator
452 // duration is saved for delay() at top of loop
453 duration = pattern[index++];
454 if (duration > 0) {
455 HardwareService.this.vibratorOn(duration);
456 }
457 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400458 if (repeat < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 break;
460 } else {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400461 index = repeat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 duration = 0;
463 }
464 }
465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 mWakeLock.release();
467 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400468 synchronized (mVibrations) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 if (mThread == this) {
470 mThread = null;
471 }
Patrick Scott18dd5f02009-07-02 11:31:12 -0400472 if (!mDone) {
473 // If this vibration finished naturally, start the next
474 // vibration.
475 mVibrations.remove(mVibration);
476 startNextVibrationLocked();
477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
479 }
480 };
481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
483 public void onReceive(Context context, Intent intent) {
484 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Patrick Scott18dd5f02009-07-02 11:31:12 -0400485 synchronized (mVibrations) {
486 doCancelVibrateLocked();
487 mVibrations.clear();
488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
490 }
491 };
The Android Open Source Project10592532009-03-18 17:39:46 -0700492
493 private static native int init_native();
494 private static native void finalize_native(int ptr);
495
Dan Murphy951764b2009-08-27 14:59:03 -0500496 private static native void setAutoBrightness_native(int ptr, boolean automatic);
The Android Open Source Project10592532009-03-18 17:39:46 -0700497 private static native void setLight_native(int ptr, int light, int color, int mode,
498 int onMS, int offMS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499
Dianne Hackbornbed30e12009-03-31 14:46:20 -0700500 private final Context mContext;
501 private final PowerManager.WakeLock mWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502
Dianne Hackbornbed30e12009-03-31 14:46:20 -0700503 private final IBatteryStats mBatteryStats;
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 volatile VibrateThread mThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506
The Android Open Source Project10592532009-03-18 17:39:46 -0700507 private int mNativePointer;
508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 native static void vibratorOn(long milliseconds);
510 native static void vibratorOff();
511}