Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [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.Context; |
| 20 | import android.os.Handler; |
| 21 | import android.os.Message; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | public 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 Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 33 | static final int LIGHT_ID_BLUETOOTH = 6; |
| 34 | static final int LIGHT_ID_WIFI = 7; |
| 35 | static final int LIGHT_ID_COUNT = 8; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 36 | |
| 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 Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 51 | 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 | |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame^] | 83 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 84 | public void pulse() { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame^] | 85 | pulse(0x00ffffff, 7); |
| 86 | } |
| 87 | |
| 88 | public void pulse(int color, int onMS) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 89 | synchronized (this) { |
| 90 | if (mColor == 0 && !mFlashing) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame^] | 91 | setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000, BRIGHTNESS_MODE_USER); |
| 92 | mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public void turnOff() { |
| 98 | synchronized (this) { |
| 99 | setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private void stopFlashing() { |
| 104 | synchronized (this) { |
| 105 | setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) { |
| 110 | if (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS) { |
| 111 | mColor = color; |
| 112 | mMode = mode; |
| 113 | mOnMS = onMS; |
| 114 | mOffMS = offMS; |
| 115 | setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private int mId; |
| 120 | private int mColor; |
| 121 | private int mMode; |
| 122 | private int mOnMS; |
| 123 | private int mOffMS; |
| 124 | private boolean mFlashing; |
| 125 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 126 | |
| 127 | LightsService(Context context) { |
| 128 | |
| 129 | mNativePointer = init_native(); |
| 130 | mContext = context; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 131 | |
| 132 | for (int i = 0; i < LIGHT_ID_COUNT; i++) { |
| 133 | mLights[i] = new Light(i); |
| 134 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | protected void finalize() throws Throwable { |
| 138 | finalize_native(mNativePointer); |
| 139 | super.finalize(); |
| 140 | } |
| 141 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 142 | public Light getLight(int id) { |
| 143 | return mLights[id]; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private Handler mH = new Handler() { |
| 147 | @Override |
| 148 | public void handleMessage(Message msg) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 149 | Light light = (Light)msg.obj; |
| 150 | light.stopFlashing(); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 151 | } |
| 152 | }; |
| 153 | |
| 154 | private static native int init_native(); |
| 155 | private static native void finalize_native(int ptr); |
| 156 | |
| 157 | private static native void setLight_native(int ptr, int light, int color, int mode, |
| 158 | int onMS, int offMS, int brightnessMode); |
| 159 | |
| 160 | private final Context mContext; |
| 161 | |
| 162 | private int mNativePointer; |
| 163 | } |