blob: ae8d3212085aa5de3429d812664545e7e9388315 [file] [log] [blame]
Mike Lockwood3a322132009-11-24 00:30:52 -05001/*
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.Context;
20import android.os.Handler;
21import android.os.Message;
22import android.util.Log;
23
24public class LightsService {
25 private static final String TAG = "LightsService";
26
27 static final int LIGHT_ID_BACKLIGHT = 0;
28 static final int LIGHT_ID_KEYBOARD = 1;
29 static final int LIGHT_ID_BUTTONS = 2;
30 static final int LIGHT_ID_BATTERY = 3;
31 static final int LIGHT_ID_NOTIFICATIONS = 4;
32 static final int LIGHT_ID_ATTENTION = 5;
33
34 static final int LIGHT_FLASH_NONE = 0;
35 static final int LIGHT_FLASH_TIMED = 1;
36 static final int LIGHT_FLASH_HARDWARE = 2;
37
38 /**
39 * Light brightness is managed by a user setting.
40 */
41 static final int BRIGHTNESS_MODE_USER = 0;
42
43 /**
44 * Light brightness is managed by a light sensor.
45 */
46 static final int BRIGHTNESS_MODE_SENSOR = 1;
47
48 private boolean mAttentionLightOn;
49 private boolean mPulsing;
50
51 LightsService(Context context) {
52
53 mNativePointer = init_native();
54 mContext = context;
55 }
56
57 protected void finalize() throws Throwable {
58 finalize_native(mNativePointer);
59 super.finalize();
60 }
61
62 void setLightOff(int light) {
63 setLight_native(mNativePointer, light, 0, LIGHT_FLASH_NONE, 0, 0, 0);
64 }
65
66 void setLightBrightness(int light, int brightness, int brightnessMode) {
67 int b = brightness & 0x000000ff;
68 b = 0xff000000 | (b << 16) | (b << 8) | b;
69 setLight_native(mNativePointer, light, b, LIGHT_FLASH_NONE, 0, 0, brightnessMode);
70 }
71
72 void setLightColor(int light, int color) {
73 setLight_native(mNativePointer, light, color, LIGHT_FLASH_NONE, 0, 0, 0);
74 }
75
76 void setLightFlashing(int light, int color, int mode, int onMS, int offMS) {
77 setLight_native(mNativePointer, light, color, mode, onMS, offMS, 0);
78 }
79
80 public void setAttentionLight(boolean on, int color) {
81 // Not worthy of a permission. We shouldn't have a flashlight permission.
82 synchronized (this) {
83 mAttentionLightOn = on;
84 mPulsing = false;
85 setLight_native(mNativePointer, LIGHT_ID_ATTENTION, color,
86 LIGHT_FLASH_HARDWARE, on ? 3 : 0, 0, 0);
87 }
88 }
89
90 public void pulseBreathingLight() {
91 synchronized (this) {
92 // HACK: Added at the last minute of cupcake -- design this better;
93 // Don't reuse the attention light -- make another one.
94 if (false) {
95 Log.d(TAG, "pulseBreathingLight mAttentionLightOn=" + mAttentionLightOn
96 + " mPulsing=" + mPulsing);
97 }
98 if (!mAttentionLightOn && !mPulsing) {
99 mPulsing = true;
100 setLight_native(mNativePointer, LIGHT_ID_ATTENTION, 0x00ffffff,
101 LIGHT_FLASH_HARDWARE, 7, 0, 0);
102 mH.sendMessageDelayed(Message.obtain(mH, 1), 3000);
103 }
104 }
105 }
106
107 private Handler mH = new Handler() {
108 @Override
109 public void handleMessage(Message msg) {
110 synchronized (this) {
111 if (false) {
112 Log.d(TAG, "pulse cleanup handler firing mPulsing=" + mPulsing);
113 }
114 if (mPulsing) {
115 mPulsing = false;
116 setLight_native(mNativePointer, LIGHT_ID_ATTENTION,
117 mAttentionLightOn ? 0xffffffff : 0,
118 LIGHT_FLASH_NONE, 0, 0, 0);
119 }
120 }
121 }
122 };
123
124 private static native int init_native();
125 private static native void finalize_native(int ptr);
126
127 private static native void setLight_native(int ptr, int light, int color, int mode,
128 int onMS, int offMS, int brightnessMode);
129
130 private final Context mContext;
131
132 private int mNativePointer;
133}