blob: 1937b043f530a5a6c0d762f64ab35f6021927a47 [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;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050033 static final int LIGHT_ID_BLUETOOTH = 6;
34 static final int LIGHT_ID_WIFI = 7;
35 static final int LIGHT_ID_COUNT = 8;
Mike Lockwood3a322132009-11-24 00:30:52 -050036
37 static final int LIGHT_FLASH_NONE = 0;
38 static final int LIGHT_FLASH_TIMED = 1;
39 static final int LIGHT_FLASH_HARDWARE = 2;
40
41 /**
42 * Light brightness is managed by a user setting.
43 */
44 static final int BRIGHTNESS_MODE_USER = 0;
45
46 /**
47 * Light brightness is managed by a light sensor.
48 */
49 static final int BRIGHTNESS_MODE_SENSOR = 1;
50
Mike Lockwood3cb67a32009-11-27 14:25:58 -050051 private final Light mLights[] = new Light[LIGHT_ID_COUNT];
52
53 public final class Light {
54
55 private Light(int id) {
56 mId = id;
57 }
58
59 public void setBrightness(int brightness) {
60 setBrightness(brightness, BRIGHTNESS_MODE_USER);
61 }
62
63 public void setBrightness(int brightness, int brightnessMode) {
64 synchronized (this) {
65 int color = brightness & 0x000000ff;
66 color = 0xff000000 | (color << 16) | (color << 8) | color;
67 setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode);
68 }
69 }
70
71 public void setColor(int color) {
72 synchronized (this) {
73 setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, 0);
74 }
75 }
76
77 public void setFlashing(int color, int mode, int onMS, int offMS) {
78 synchronized (this) {
79 setLightLocked(color, mode, onMS, offMS, BRIGHTNESS_MODE_USER);
80 }
81 }
82
83 public void pulse() {
84 synchronized (this) {
85 if (mColor == 0 && !mFlashing) {
86 setLightLocked(0x00ffffff, LIGHT_FLASH_HARDWARE, 7, 0, BRIGHTNESS_MODE_USER);
87 mH.sendMessageDelayed(Message.obtain(mH, 1, this), 3000);
88 }
89 }
90 }
91
92 public void turnOff() {
93 synchronized (this) {
94 setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0);
95 }
96 }
97
98 private void stopFlashing() {
99 synchronized (this) {
100 setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER);
101 }
102 }
103
104 private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) {
105 if (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS) {
106 mColor = color;
107 mMode = mode;
108 mOnMS = onMS;
109 mOffMS = offMS;
110 setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode);
111 }
112 }
113
114 private int mId;
115 private int mColor;
116 private int mMode;
117 private int mOnMS;
118 private int mOffMS;
119 private boolean mFlashing;
120 }
Mike Lockwood3a322132009-11-24 00:30:52 -0500121
122 LightsService(Context context) {
123
124 mNativePointer = init_native();
125 mContext = context;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500126
127 for (int i = 0; i < LIGHT_ID_COUNT; i++) {
128 mLights[i] = new Light(i);
129 }
Mike Lockwood3a322132009-11-24 00:30:52 -0500130 }
131
132 protected void finalize() throws Throwable {
133 finalize_native(mNativePointer);
134 super.finalize();
135 }
136
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500137 public Light getLight(int id) {
138 return mLights[id];
Mike Lockwood3a322132009-11-24 00:30:52 -0500139 }
140
141 private Handler mH = new Handler() {
142 @Override
143 public void handleMessage(Message msg) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500144 Light light = (Light)msg.obj;
145 light.stopFlashing();
Mike Lockwood3a322132009-11-24 00:30:52 -0500146 }
147 };
148
149 private static native int init_native();
150 private static native void finalize_native(int ptr);
151
152 private static native void setLight_native(int ptr, int light, int color, int mode,
153 int onMS, int offMS, int brightnessMode);
154
155 private final Context mContext;
156
157 private int mNativePointer;
158}