blob: 42367ef86a8ef2b486032c76dca9d325741b075c [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
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.pm.PackageManager;
Joe Onorato95e4f702009-03-24 19:29:09 -070024import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.os.Hardware;
26import android.os.IHardwareService;
Joe Onorato95e4f702009-03-24 19:29:09 -070027import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.os.Power;
29import android.os.PowerManager;
30import android.os.Process;
31import android.os.RemoteException;
32import android.os.IBinder;
33import android.os.Binder;
34import android.os.SystemClock;
35import android.util.Log;
36
37public class HardwareService extends IHardwareService.Stub {
38 private static final String TAG = "HardwareService";
39
The Android Open Source Project10592532009-03-18 17:39:46 -070040 static final int LIGHT_ID_BACKLIGHT = 0;
41 static final int LIGHT_ID_KEYBOARD = 1;
42 static final int LIGHT_ID_BUTTONS = 2;
43 static final int LIGHT_ID_BATTERY = 3;
44 static final int LIGHT_ID_NOTIFICATIONS = 4;
45 static final int LIGHT_ID_ATTENTION = 5;
46
47 static final int LIGHT_FLASH_NONE = 0;
48 static final int LIGHT_FLASH_TIMED = 1;
49
Joe Onorato95e4f702009-03-24 19:29:09 -070050 private boolean mAttentionLightOn;
51 private boolean mPulsing;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 HardwareService(Context context) {
54 // Reset the hardware to a default state, in case this is a runtime
55 // restart instead of a fresh boot.
56 vibratorOff();
57
The Android Open Source Project10592532009-03-18 17:39:46 -070058 mNativePointer = init_native();
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 mContext = context;
61 PowerManager pm = (PowerManager)context.getSystemService(
62 Context.POWER_SERVICE);
63 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
64 mWakeLock.setReferenceCounted(true);
65
66 IntentFilter filter = new IntentFilter();
67 filter.addAction(Intent.ACTION_SCREEN_OFF);
68 context.registerReceiver(mIntentReceiver, filter);
69 }
70
The Android Open Source Project10592532009-03-18 17:39:46 -070071 protected void finalize() throws Throwable {
72 finalize_native(mNativePointer);
73 super.finalize();
74 }
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 public void vibrate(long milliseconds) {
The Android Open Source Project10592532009-03-18 17:39:46 -070077 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
78 != PackageManager.PERMISSION_GRANTED) {
79 throw new SecurityException("Requires VIBRATE permission");
80 }
81 doCancelVibrate();
82 vibratorOn(milliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 }
84
85 private boolean isAll0(long[] pattern) {
86 int N = pattern.length;
87 for (int i = 0; i < N; i++) {
88 if (pattern[i] != 0) {
89 return false;
90 }
91 }
92 return true;
93 }
94
95 public void vibratePattern(long[] pattern, int repeat, IBinder token) {
96 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
97 != PackageManager.PERMISSION_GRANTED) {
98 throw new SecurityException("Requires VIBRATE permission");
99 }
100 // so wakelock calls will succeed
101 long identity = Binder.clearCallingIdentity();
102 try {
103 if (false) {
104 String s = "";
105 int N = pattern.length;
106 for (int i=0; i<N; i++) {
107 s += " " + pattern[i];
108 }
109 Log.i(TAG, "vibrating with pattern: " + s);
110 }
111
112 // we're running in the server so we can't fail
113 if (pattern == null || pattern.length == 0
114 || isAll0(pattern)
115 || repeat >= pattern.length || token == null) {
116 return;
117 }
118
119 synchronized (this) {
120 Death death = new Death(token);
121 try {
122 token.linkToDeath(death, 0);
123 } catch (RemoteException e) {
124 return;
125 }
126
127 Thread oldThread = mThread;
128
129 if (oldThread != null) {
130 // stop the old one
131 synchronized (mThread) {
132 mThread.mDone = true;
133 mThread.notify();
134 }
135 }
136
137 if (mDeath != null) {
138 mToken.unlinkToDeath(mDeath, 0);
139 }
140
141 mDeath = death;
142 mToken = token;
143
144 // start the new thread
145 mThread = new VibrateThread(pattern, repeat);
146 mThread.start();
147 }
148 }
149 finally {
150 Binder.restoreCallingIdentity(identity);
151 }
152 }
153
154 public void cancelVibrate() {
155 mContext.enforceCallingOrSelfPermission(
156 android.Manifest.permission.VIBRATE,
157 "cancelVibrate");
158
159 // so wakelock calls will succeed
160 long identity = Binder.clearCallingIdentity();
161 try {
162 doCancelVibrate();
163 }
164 finally {
165 Binder.restoreCallingIdentity(identity);
166 }
167 }
168
169 public boolean getFlashlightEnabled() {
170 return Hardware.getFlashlightEnabled();
171 }
172
173 public void setFlashlightEnabled(boolean on) {
174 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FLASHLIGHT)
175 != PackageManager.PERMISSION_GRANTED &&
176 mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST)
177 != PackageManager.PERMISSION_GRANTED) {
178 throw new SecurityException("Requires FLASHLIGHT or HARDWARE_TEST permission");
179 }
180 Hardware.setFlashlightEnabled(on);
181 }
182
183 public void enableCameraFlash(int milliseconds) {
184 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.CAMERA)
185 != PackageManager.PERMISSION_GRANTED &&
186 mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST)
187 != PackageManager.PERMISSION_GRANTED) {
188 throw new SecurityException("Requires CAMERA or HARDWARE_TEST permission");
189 }
190 Hardware.enableCameraFlash(milliseconds);
191 }
192
The Android Open Source Project10592532009-03-18 17:39:46 -0700193 public void setBacklights(int brightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST)
195 != PackageManager.PERMISSION_GRANTED) {
196 throw new SecurityException("Requires HARDWARE_TEST permission");
197 }
198 // Don't let applications turn the screen all the way off
199 brightness = Math.max(brightness, Power.BRIGHTNESS_DIM);
The Android Open Source Project10592532009-03-18 17:39:46 -0700200 setLightBrightness_UNCHECKED(LIGHT_ID_BACKLIGHT, brightness);
201 setLightBrightness_UNCHECKED(LIGHT_ID_KEYBOARD, brightness);
202 setLightBrightness_UNCHECKED(LIGHT_ID_BUTTONS, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
204
The Android Open Source Project10592532009-03-18 17:39:46 -0700205 void setLightOff_UNCHECKED(int light) {
206 setLight_native(mNativePointer, light, 0, LIGHT_FLASH_NONE, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 }
208
The Android Open Source Project10592532009-03-18 17:39:46 -0700209 void setLightBrightness_UNCHECKED(int light, int brightness) {
210 int b = brightness & 0x000000ff;
211 b = 0xff000000 | (b << 16) | (b << 8) | b;
212 setLight_native(mNativePointer, light, b, LIGHT_FLASH_NONE, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 }
214
The Android Open Source Project10592532009-03-18 17:39:46 -0700215 void setLightColor_UNCHECKED(int light, int color) {
216 setLight_native(mNativePointer, light, color, LIGHT_FLASH_NONE, 0, 0);
217 }
218
219 void setLightFlashing_UNCHECKED(int light, int color, int mode, int onMS, int offMS) {
220 setLight_native(mNativePointer, light, color, mode, onMS, offMS);
221 }
222
223 public void setAttentionLight(boolean on) {
224 // Not worthy of a permission. We shouldn't have a flashlight permission.
Joe Onorato95e4f702009-03-24 19:29:09 -0700225 synchronized (this) {
226 mAttentionLightOn = on;
227 mPulsing = false;
228 setLight_native(mNativePointer, LIGHT_ID_ATTENTION, on ? 0xffffffff : 0,
229 LIGHT_FLASH_NONE, 0, 0);
230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 }
232
Joe Onorato95e4f702009-03-24 19:29:09 -0700233 public void pulseBreathingLight() {
234 synchronized (this) {
235 // HACK: Added at the last minute of cupcake -- design this better;
236 // Don't reuse the attention light -- make another one.
237 if (false) {
238 Log.d(TAG, "pulseBreathingLight mAttentionLightOn=" + mAttentionLightOn
239 + " mPulsing=" + mPulsing);
240 }
241 if (!mAttentionLightOn && !mPulsing) {
242 mPulsing = true;
243 setLight_native(mNativePointer, LIGHT_ID_ATTENTION, 0xff101010,
244 LIGHT_FLASH_NONE, 0, 0);
245 mH.sendMessageDelayed(Message.obtain(mH, 1), 3000);
246 }
247 }
248 }
249
250 private Handler mH = new Handler() {
251 @Override
252 public void handleMessage(Message msg) {
253 synchronized (this) {
254 if (false) {
255 Log.d(TAG, "pulse cleanup handler firing mPulsing=" + mPulsing);
256 }
257 if (mPulsing) {
258 mPulsing = false;
259 setLight_native(mNativePointer, LIGHT_ID_ATTENTION,
260 mAttentionLightOn ? 0xffffffff : 0,
261 LIGHT_FLASH_NONE, 0, 0);
262 }
263 }
264 }
265 };
266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 private void doCancelVibrate() {
268 synchronized (this) {
269 if (mThread != null) {
270 synchronized (mThread) {
271 mThread.mDone = true;
272 mThread.notify();
273 }
274 mThread = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700276 vibratorOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 }
278 }
279
280 private class VibrateThread extends Thread {
281 long[] mPattern;
282 int mRepeat;
283 boolean mDone;
284
285 VibrateThread(long[] pattern, int repeat) {
286 mPattern = pattern;
287 mRepeat = repeat;
288 mWakeLock.acquire();
289 }
290
291 private void delay(long duration) {
292 if (duration > 0) {
293 long bedtime = SystemClock.uptimeMillis();
294 do {
295 try {
296 this.wait(duration);
297 }
298 catch (InterruptedException e) {
299 }
300 if (mDone) {
301 break;
302 }
303 duration = duration
304 - SystemClock.uptimeMillis() - bedtime;
305 } while (duration > 0);
306 }
307 }
308
309 public void run() {
310 Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
311 synchronized (this) {
312 int index = 0;
313 long[] pattern = mPattern;
314 int len = pattern.length;
315 long duration = 0;
316
317 while (!mDone) {
318 // add off-time duration to any accumulated on-time duration
319 if (index < len) {
320 duration += pattern[index++];
321 }
322
323 // sleep until it is time to start the vibrator
324 delay(duration);
325 if (mDone) {
326 break;
327 }
328
329 if (index < len) {
330 // read on-time duration and start the vibrator
331 // duration is saved for delay() at top of loop
332 duration = pattern[index++];
333 if (duration > 0) {
334 HardwareService.this.vibratorOn(duration);
335 }
336 } else {
337 if (mRepeat < 0) {
338 break;
339 } else {
340 index = mRepeat;
341 duration = 0;
342 }
343 }
344 }
345 if (mDone) {
346 // make sure vibrator is off if we were cancelled.
347 // otherwise, it will turn off automatically
348 // when the last timeout expires.
349 HardwareService.this.vibratorOff();
350 }
351 mWakeLock.release();
352 }
353 synchronized (HardwareService.this) {
354 if (mThread == this) {
355 mThread = null;
356 }
357 }
358 }
359 };
360
361 private class Death implements IBinder.DeathRecipient {
362 IBinder mMe;
363
364 Death(IBinder me) {
365 mMe = me;
366 }
367
368 public void binderDied() {
369 synchronized (HardwareService.this) {
370 if (mMe == mToken) {
371 doCancelVibrate();
372 }
373 }
374 }
375 }
376
377 BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
378 public void onReceive(Context context, Intent intent) {
379 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
380 doCancelVibrate();
381 }
382 }
383 };
The Android Open Source Project10592532009-03-18 17:39:46 -0700384
385 private static native int init_native();
386 private static native void finalize_native(int ptr);
387
388 private static native void setLight_native(int ptr, int light, int color, int mode,
389 int onMS, int offMS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390
391 private Context mContext;
392 private PowerManager.WakeLock mWakeLock;
393
394 volatile VibrateThread mThread;
395 volatile Death mDeath;
396 volatile IBinder mToken;
397
The Android Open Source Project10592532009-03-18 17:39:46 -0700398 private int mNativePointer;
399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 native static void vibratorOn(long milliseconds);
401 native static void vibratorOff();
402}