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