Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 1 | /* * Copyright (C) 2008 The Android Open Source Project |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 16 | package com.android.server.lights; |
| 17 | |
| 18 | import com.android.server.SystemService; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 19 | |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 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; |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 25 | import android.provider.Settings; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 26 | import android.util.Slog; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 27 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 28 | public class LightsService extends SystemService { |
| 29 | static final String TAG = "LightsService"; |
| 30 | static final boolean DEBUG = false; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 31 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 32 | final LightImpl mLights[] = new LightImpl[LightsManager.LIGHT_ID_COUNT]; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 33 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 34 | private final class LightImpl extends Light { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 35 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 36 | private LightImpl(int id) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 37 | mId = id; |
| 38 | } |
| 39 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 40 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 41 | public void setBrightness(int brightness) { |
| 42 | setBrightness(brightness, BRIGHTNESS_MODE_USER); |
| 43 | } |
| 44 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 45 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 46 | public void setBrightness(int brightness, int brightnessMode) { |
| 47 | synchronized (this) { |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 48 | // LOW_PERSISTENCE cannot be manually set |
| 49 | if (brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) { |
| 50 | Slog.w(TAG, "setBrightness with LOW_PERSISTENCE unexpected #" + mId + |
| 51 | ": brightness=0x" + Integer.toHexString(brightness)); |
| 52 | return; |
| 53 | } |
| 54 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 55 | int color = brightness & 0x000000ff; |
| 56 | color = 0xff000000 | (color << 16) | (color << 8) | color; |
| 57 | setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode); |
| 58 | } |
| 59 | } |
| 60 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 61 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 62 | public void setColor(int color) { |
| 63 | synchronized (this) { |
| 64 | setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, 0); |
| 65 | } |
| 66 | } |
| 67 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 68 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 69 | public void setFlashing(int color, int mode, int onMS, int offMS) { |
| 70 | synchronized (this) { |
| 71 | setLightLocked(color, mode, onMS, offMS, BRIGHTNESS_MODE_USER); |
| 72 | } |
| 73 | } |
| 74 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 75 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 76 | public void pulse() { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 77 | pulse(0x00ffffff, 7); |
| 78 | } |
| 79 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 80 | @Override |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 81 | public void pulse(int color, int onMS) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 82 | synchronized (this) { |
| 83 | if (mColor == 0 && !mFlashing) { |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 84 | setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000, |
| 85 | BRIGHTNESS_MODE_USER); |
Oskar Andero | e846719 | 2013-12-03 17:41:27 +0100 | [diff] [blame] | 86 | mColor = 0; |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 87 | mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 92 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 93 | public void turnOff() { |
| 94 | synchronized (this) { |
| 95 | setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0); |
| 96 | } |
| 97 | } |
| 98 | |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 99 | @Override |
| 100 | public void setVrMode(boolean enabled) { |
| 101 | synchronized (this) { |
| 102 | if (mVrModeEnabled != enabled) { |
| 103 | mVrModeEnabled = enabled; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 104 | |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 105 | mUseLowPersistenceForVR = |
| 106 | (getVrDisplayMode() == Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE); |
| 107 | if (shouldBeInLowPersistenceMode()) { |
| 108 | mLastBrightnessMode = mBrightnessMode; |
| 109 | } |
| 110 | |
| 111 | // NOTE: We do not trigger a call to setLightLocked here. We do not know the |
| 112 | // current brightness or other values when leaving VR so we avoid any incorrect |
| 113 | // jumps. The code that calls this method will immediately issue a brightness |
| 114 | // update which is when the change will occur. |
| 115 | } |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 119 | private void stopFlashing() { |
| 120 | synchronized (this) { |
| 121 | setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) { |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 126 | if (shouldBeInLowPersistenceMode()) { |
| 127 | brightnessMode = BRIGHTNESS_MODE_LOW_PERSISTENCE; |
| 128 | } else if (brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) { |
| 129 | brightnessMode = mLastBrightnessMode; |
| 130 | } |
| 131 | |
Oleksiy Avramchenko | 4f4939f | 2017-04-25 12:30:49 +0200 | [diff] [blame^] | 132 | if (!mInitialized || color != mColor || mode != mMode || onMS != mOnMS || |
| 133 | offMS != mOffMS || mBrightnessMode != brightnessMode) { |
Dianne Hackborn | ff801ec | 2011-01-22 18:05:38 -0800 | [diff] [blame] | 134 | if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#" |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 135 | + Integer.toHexString(color) + ": brightnessMode=" + brightnessMode); |
Oleksiy Avramchenko | 4f4939f | 2017-04-25 12:30:49 +0200 | [diff] [blame^] | 136 | mInitialized = true; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 137 | mLastColor = mColor; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 138 | mColor = color; |
| 139 | mMode = mode; |
| 140 | mOnMS = onMS; |
| 141 | mOffMS = offMS; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 142 | mBrightnessMode = brightnessMode; |
Jeff Brown | faec22c8 | 2015-04-10 12:58:52 -0700 | [diff] [blame] | 143 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x" |
| 144 | + Integer.toHexString(color) + ")"); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 145 | try { |
| 146 | setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode); |
| 147 | } finally { |
| 148 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 149 | } |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 153 | private boolean shouldBeInLowPersistenceMode() { |
| 154 | return mVrModeEnabled && mUseLowPersistenceForVR; |
| 155 | } |
| 156 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 157 | private int mId; |
| 158 | private int mColor; |
| 159 | private int mMode; |
| 160 | private int mOnMS; |
| 161 | private int mOffMS; |
| 162 | private boolean mFlashing; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 163 | private int mBrightnessMode; |
| 164 | private int mLastBrightnessMode; |
| 165 | private int mLastColor; |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 166 | private boolean mVrModeEnabled; |
| 167 | private boolean mUseLowPersistenceForVR; |
Oleksiy Avramchenko | 4f4939f | 2017-04-25 12:30:49 +0200 | [diff] [blame^] | 168 | private boolean mInitialized; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 169 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 170 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 171 | public LightsService(Context context) { |
| 172 | super(context); |
| 173 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 174 | mNativePointer = init_native(); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 175 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 176 | for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) { |
| 177 | mLights[i] = new LightImpl(i); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 178 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 181 | @Override |
| 182 | public void onStart() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 183 | publishLocalService(LightsManager.class, mService); |
| 184 | } |
| 185 | |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 186 | @Override |
| 187 | public void onBootPhase(int phase) { |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 190 | private int getVrDisplayMode() { |
| 191 | int currentUser = ActivityManager.getCurrentUser(); |
| 192 | return Settings.Secure.getIntForUser(getContext().getContentResolver(), |
| 193 | Settings.Secure.VR_DISPLAY_MODE, |
| 194 | /*default*/Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE, |
| 195 | currentUser); |
| 196 | } |
| 197 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 198 | private final LightsManager mService = new LightsManager() { |
| 199 | @Override |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 200 | public Light getLight(int id) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 201 | if (id < LIGHT_ID_COUNT) { |
| 202 | return mLights[id]; |
| 203 | } else { |
| 204 | return null; |
| 205 | } |
| 206 | } |
| 207 | }; |
| 208 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 209 | @Override |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 210 | protected void finalize() throws Throwable { |
| 211 | finalize_native(mNativePointer); |
| 212 | super.finalize(); |
| 213 | } |
| 214 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 215 | private Handler mH = new Handler() { |
| 216 | @Override |
| 217 | public void handleMessage(Message msg) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 218 | LightImpl light = (LightImpl)msg.obj; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 219 | light.stopFlashing(); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 220 | } |
| 221 | }; |
| 222 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 223 | private static native long init_native(); |
| 224 | private static native void finalize_native(long ptr); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 225 | |
Narayan Kamath | 3f7b8d0 | 2014-01-08 12:19:40 +0000 | [diff] [blame] | 226 | 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] | 227 | int onMS, int offMS, int brightnessMode); |
| 228 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 229 | private long mNativePointer; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 230 | } |