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 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 17 | package com.android.server.lights; |
| 18 | |
| 19 | import com.android.server.SystemService; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 20 | |
| 21 | import android.content.Context; |
| 22 | import android.os.Handler; |
| 23 | import android.os.Message; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 24 | import android.os.Trace; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 25 | import android.util.Slog; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 26 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 27 | public class LightsService extends SystemService { |
| 28 | static final String TAG = "LightsService"; |
| 29 | static final boolean DEBUG = false; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 30 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 31 | final LightImpl mLights[] = new LightImpl[LightsManager.LIGHT_ID_COUNT]; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 32 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 33 | private final class LightImpl extends Light { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 34 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 35 | private LightImpl(int id) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 36 | mId = id; |
| 37 | } |
| 38 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 39 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 40 | public void setBrightness(int brightness) { |
| 41 | setBrightness(brightness, BRIGHTNESS_MODE_USER); |
| 42 | } |
| 43 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 44 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 45 | public void setBrightness(int brightness, int brightnessMode) { |
| 46 | synchronized (this) { |
| 47 | int color = brightness & 0x000000ff; |
| 48 | color = 0xff000000 | (color << 16) | (color << 8) | color; |
| 49 | setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode); |
| 50 | } |
| 51 | } |
| 52 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 53 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 54 | public void setColor(int color) { |
| 55 | synchronized (this) { |
| 56 | setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, 0); |
| 57 | } |
| 58 | } |
| 59 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 60 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 61 | public void setFlashing(int color, int mode, int onMS, int offMS) { |
| 62 | synchronized (this) { |
| 63 | setLightLocked(color, mode, onMS, offMS, BRIGHTNESS_MODE_USER); |
| 64 | } |
| 65 | } |
| 66 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 67 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 68 | public void pulse() { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 69 | pulse(0x00ffffff, 7); |
| 70 | } |
| 71 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 72 | @Override |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 73 | public void pulse(int color, int onMS) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 74 | synchronized (this) { |
| 75 | if (mColor == 0 && !mFlashing) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 76 | setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000, BRIGHTNESS_MODE_USER); |
Oskar Andero | e846719 | 2013-12-03 17:41:27 +0100 | [diff] [blame] | 77 | mColor = 0; |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 78 | mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 83 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 84 | public void turnOff() { |
| 85 | synchronized (this) { |
| 86 | setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private void stopFlashing() { |
| 91 | synchronized (this) { |
| 92 | setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) { |
| 97 | if (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS) { |
Dianne Hackborn | ff801ec | 2011-01-22 18:05:38 -0800 | [diff] [blame] | 98 | if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#" |
| 99 | + Integer.toHexString(color)); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 100 | mColor = color; |
| 101 | mMode = mode; |
| 102 | mOnMS = onMS; |
| 103 | mOffMS = offMS; |
Jeff Brown | faec22c8 | 2015-04-10 12:58:52 -0700 | [diff] [blame] | 104 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x" |
| 105 | + Integer.toHexString(color) + ")"); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 106 | try { |
| 107 | setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode); |
| 108 | } finally { |
| 109 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 110 | } |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 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 Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 121 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 122 | public LightsService(Context context) { |
| 123 | super(context); |
| 124 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 125 | mNativePointer = init_native(); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 126 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 127 | for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) { |
| 128 | mLights[i] = new LightImpl(i); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 129 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 132 | @Override |
| 133 | public void onStart() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 134 | publishLocalService(LightsManager.class, mService); |
| 135 | } |
| 136 | |
| 137 | private final LightsManager mService = new LightsManager() { |
| 138 | @Override |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 139 | public Light getLight(int id) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 140 | if (id < LIGHT_ID_COUNT) { |
| 141 | return mLights[id]; |
| 142 | } else { |
| 143 | return null; |
| 144 | } |
| 145 | } |
| 146 | }; |
| 147 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 148 | @Override |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 149 | protected void finalize() throws Throwable { |
| 150 | finalize_native(mNativePointer); |
| 151 | super.finalize(); |
| 152 | } |
| 153 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 154 | private Handler mH = new Handler() { |
| 155 | @Override |
| 156 | public void handleMessage(Message msg) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 157 | LightImpl light = (LightImpl)msg.obj; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 158 | light.stopFlashing(); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 159 | } |
| 160 | }; |
| 161 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 162 | private static native long init_native(); |
| 163 | private static native void finalize_native(long ptr); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 164 | |
Narayan Kamath | 3f7b8d0 | 2014-01-08 12:19:40 +0000 | [diff] [blame] | 165 | static native void setLight_native(long ptr, int light, int color, int mode, |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 166 | int onMS, int offMS, int brightnessMode); |
| 167 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 168 | private long mNativePointer; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 169 | } |