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; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame^] | 20 | import com.android.server.vr.VrManagerInternal; |
| 21 | import com.android.server.vr.VrStateListener; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 22 | |
| 23 | import android.content.Context; |
| 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 26 | import android.os.Trace; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 27 | import android.util.Slog; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 28 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 29 | public class LightsService extends SystemService { |
| 30 | static final String TAG = "LightsService"; |
| 31 | static final boolean DEBUG = false; |
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 | final LightImpl mLights[] = new LightImpl[LightsManager.LIGHT_ID_COUNT]; |
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 final class LightImpl extends Light { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 36 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 37 | private LightImpl(int id) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 38 | mId = id; |
| 39 | } |
| 40 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 41 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 42 | public void setBrightness(int brightness) { |
| 43 | setBrightness(brightness, BRIGHTNESS_MODE_USER); |
| 44 | } |
| 45 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 46 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 47 | public void setBrightness(int brightness, int brightnessMode) { |
| 48 | synchronized (this) { |
| 49 | int color = brightness & 0x000000ff; |
| 50 | color = 0xff000000 | (color << 16) | (color << 8) | color; |
| 51 | setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode); |
| 52 | } |
| 53 | } |
| 54 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 55 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 56 | public void setColor(int color) { |
| 57 | synchronized (this) { |
| 58 | setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, 0); |
| 59 | } |
| 60 | } |
| 61 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 62 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 63 | public void setFlashing(int color, int mode, int onMS, int offMS) { |
| 64 | synchronized (this) { |
| 65 | setLightLocked(color, mode, onMS, offMS, BRIGHTNESS_MODE_USER); |
| 66 | } |
| 67 | } |
| 68 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 69 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 70 | public void pulse() { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 71 | pulse(0x00ffffff, 7); |
| 72 | } |
| 73 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 74 | @Override |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 75 | public void pulse(int color, int onMS) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 76 | synchronized (this) { |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame^] | 77 | if (mBrightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) { |
| 78 | return; |
| 79 | } |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 80 | if (mColor == 0 && !mFlashing) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 81 | setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000, BRIGHTNESS_MODE_USER); |
Oskar Andero | e846719 | 2013-12-03 17:41:27 +0100 | [diff] [blame] | 82 | mColor = 0; |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 83 | mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 88 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 89 | public void turnOff() { |
| 90 | synchronized (this) { |
| 91 | setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0); |
| 92 | } |
| 93 | } |
| 94 | |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame^] | 95 | void enableLowPersistence() { |
| 96 | synchronized(this) { |
| 97 | setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_LOW_PERSISTENCE); |
| 98 | mLocked = true; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void disableLowPersistence() { |
| 103 | synchronized(this) { |
| 104 | mLocked = false; |
| 105 | setLightLocked(mLastColor, LIGHT_FLASH_NONE, 0, 0, mLastBrightnessMode); |
| 106 | } |
| 107 | } |
| 108 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 109 | private void stopFlashing() { |
| 110 | synchronized (this) { |
| 111 | setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) { |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame^] | 116 | if (!mLocked && (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS || |
| 117 | mBrightnessMode != brightnessMode)) { |
Dianne Hackborn | ff801ec | 2011-01-22 18:05:38 -0800 | [diff] [blame] | 118 | if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#" |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame^] | 119 | + Integer.toHexString(color) + ": brightnessMode=" + brightnessMode); |
| 120 | mLastColor = mColor; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 121 | mColor = color; |
| 122 | mMode = mode; |
| 123 | mOnMS = onMS; |
| 124 | mOffMS = offMS; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame^] | 125 | mLastBrightnessMode = mBrightnessMode; |
| 126 | mBrightnessMode = brightnessMode; |
Jeff Brown | faec22c8 | 2015-04-10 12:58:52 -0700 | [diff] [blame] | 127 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x" |
| 128 | + Integer.toHexString(color) + ")"); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 129 | try { |
| 130 | setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode); |
| 131 | } finally { |
| 132 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 133 | } |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | private int mId; |
| 138 | private int mColor; |
| 139 | private int mMode; |
| 140 | private int mOnMS; |
| 141 | private int mOffMS; |
| 142 | private boolean mFlashing; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame^] | 143 | private int mBrightnessMode; |
| 144 | private int mLastBrightnessMode; |
| 145 | private int mLastColor; |
| 146 | private boolean mLocked; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 147 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 148 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 149 | public LightsService(Context context) { |
| 150 | super(context); |
| 151 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 152 | mNativePointer = init_native(); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 153 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 154 | for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) { |
| 155 | mLights[i] = new LightImpl(i); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 156 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 157 | } |
| 158 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 159 | @Override |
| 160 | public void onStart() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 161 | publishLocalService(LightsManager.class, mService); |
| 162 | } |
| 163 | |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame^] | 164 | @Override |
| 165 | public void onBootPhase(int phase) { |
| 166 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
| 167 | getLocalService(VrManagerInternal.class).registerListener(mVrStateListener); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private final VrStateListener mVrStateListener = new VrStateListener() { |
| 172 | @Override |
| 173 | public void onVrStateChanged(boolean enabled) { |
| 174 | LightImpl l = mLights[LightsManager.LIGHT_ID_BACKLIGHT]; |
| 175 | if (enabled) { |
| 176 | if (DEBUG) Slog.v(TAG, "VR mode enabled, setting brightness to low persistence"); |
| 177 | l.enableLowPersistence(); |
| 178 | |
| 179 | } else { |
| 180 | if (DEBUG) Slog.v(TAG, "VR mode disabled, resetting brightnes"); |
| 181 | l.disableLowPersistence(); |
| 182 | } |
| 183 | } |
| 184 | }; |
| 185 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 186 | private final LightsManager mService = new LightsManager() { |
| 187 | @Override |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 188 | public Light getLight(int id) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 189 | if (id < LIGHT_ID_COUNT) { |
| 190 | return mLights[id]; |
| 191 | } else { |
| 192 | return null; |
| 193 | } |
| 194 | } |
| 195 | }; |
| 196 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 197 | @Override |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 198 | protected void finalize() throws Throwable { |
| 199 | finalize_native(mNativePointer); |
| 200 | super.finalize(); |
| 201 | } |
| 202 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 203 | private Handler mH = new Handler() { |
| 204 | @Override |
| 205 | public void handleMessage(Message msg) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 206 | LightImpl light = (LightImpl)msg.obj; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 207 | light.stopFlashing(); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 208 | } |
| 209 | }; |
| 210 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 211 | private static native long init_native(); |
| 212 | private static native void finalize_native(long ptr); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 213 | |
Narayan Kamath | 3f7b8d0 | 2014-01-08 12:19:40 +0000 | [diff] [blame] | 214 | 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] | 215 | int onMS, int offMS, int brightnessMode); |
| 216 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 217 | private long mNativePointer; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 218 | } |