Santos Cordon | 3107d29 | 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 | |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 18 | import android.app.ActivityManager; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.os.Handler; |
Dan Gittik | 832b497 | 2019-02-13 18:17:47 +0000 | [diff] [blame] | 21 | import android.os.IBinder; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 22 | import android.os.Message; |
Dan Gittik | 832b497 | 2019-02-13 18:17:47 +0000 | [diff] [blame] | 23 | import android.os.PowerManager; |
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; |
Dan Gittik | 832b497 | 2019-02-13 18:17:47 +0000 | [diff] [blame] | 27 | import android.view.SurfaceControl; |
| 28 | |
| 29 | import com.android.server.SystemService; |
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 | public class LightsService extends SystemService { |
| 32 | static final String TAG = "LightsService"; |
| 33 | static final boolean DEBUG = false; |
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 | final LightImpl mLights[] = new LightImpl[LightsManager.LIGHT_ID_COUNT]; |
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 final class LightImpl extends Light { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 38 | |
Dan Gittik | 832b497 | 2019-02-13 18:17:47 +0000 | [diff] [blame] | 39 | private final IBinder mDisplayToken; |
| 40 | private final int mSurfaceControlMaximumBrightness; |
| 41 | |
| 42 | private LightImpl(Context context, int id) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 43 | mId = id; |
Dan Gittik | 832b497 | 2019-02-13 18:17:47 +0000 | [diff] [blame] | 44 | mDisplayToken = SurfaceControl.getInternalDisplayToken(); |
| 45 | final boolean brightnessSupport = SurfaceControl.getDisplayBrightnessSupport( |
| 46 | mDisplayToken); |
| 47 | if (DEBUG) { |
| 48 | Slog.d(TAG, "Display brightness support: " + brightnessSupport); |
| 49 | } |
| 50 | int maximumBrightness = 0; |
| 51 | if (brightnessSupport) { |
| 52 | PowerManager pm = context.getSystemService(PowerManager.class); |
| 53 | if (pm != null) { |
| 54 | maximumBrightness = pm.getMaximumScreenBrightnessSetting(); |
| 55 | } |
| 56 | } |
| 57 | mSurfaceControlMaximumBrightness = maximumBrightness; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 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 setBrightness(int brightness) { |
| 62 | setBrightness(brightness, BRIGHTNESS_MODE_USER); |
| 63 | } |
| 64 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 65 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 66 | public void setBrightness(int brightness, int brightnessMode) { |
| 67 | synchronized (this) { |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 68 | // LOW_PERSISTENCE cannot be manually set |
| 69 | if (brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) { |
| 70 | Slog.w(TAG, "setBrightness with LOW_PERSISTENCE unexpected #" + mId + |
| 71 | ": brightness=0x" + Integer.toHexString(brightness)); |
| 72 | return; |
| 73 | } |
Dan Gittik | 832b497 | 2019-02-13 18:17:47 +0000 | [diff] [blame] | 74 | // Ideally, we'd like to set the brightness mode through the SF/HWC as well, but |
| 75 | // right now we just fall back to the old path through Lights brightessMode is |
| 76 | // anything but USER or the device shouldBeInLowPersistenceMode(). |
| 77 | if (brightnessMode == BRIGHTNESS_MODE_USER && !shouldBeInLowPersistenceMode() |
| 78 | && mSurfaceControlMaximumBrightness == 255) { |
| 79 | // TODO: the last check should be mSurfaceControlMaximumBrightness != 0; the |
| 80 | // reason we enforce 255 right now is to stay consistent with the old path. In |
| 81 | // the future, the framework should be refactored so that brightness is a float |
| 82 | // between 0.0f and 1.0f, and the actual number of supported brightness levels |
| 83 | // is determined in the device-specific implementation. |
| 84 | if (DEBUG) { |
| 85 | Slog.d(TAG, "Using new setBrightness path!"); |
| 86 | } |
Fiona Campbell | 466dc04 | 2019-11-22 12:29:44 +0000 | [diff] [blame^] | 87 | if (brightness == 0) { |
| 88 | SurfaceControl.setDisplayBrightness(mDisplayToken, -1.0f); |
| 89 | } else { |
| 90 | SurfaceControl.setDisplayBrightness(mDisplayToken, |
| 91 | (float) (brightness - 1) / (mSurfaceControlMaximumBrightness - 1)); |
| 92 | } |
Dan Gittik | 832b497 | 2019-02-13 18:17:47 +0000 | [diff] [blame] | 93 | } else { |
| 94 | int color = brightness & 0x000000ff; |
| 95 | color = 0xff000000 | (color << 16) | (color << 8) | color; |
| 96 | setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode); |
| 97 | } |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 101 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 102 | public void setColor(int color) { |
| 103 | synchronized (this) { |
| 104 | setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, 0); |
| 105 | } |
| 106 | } |
| 107 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 108 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 109 | public void setFlashing(int color, int mode, int onMS, int offMS) { |
| 110 | synchronized (this) { |
| 111 | setLightLocked(color, mode, onMS, offMS, BRIGHTNESS_MODE_USER); |
| 112 | } |
| 113 | } |
| 114 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 115 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 116 | public void pulse() { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 117 | pulse(0x00ffffff, 7); |
| 118 | } |
| 119 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 120 | @Override |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 121 | public void pulse(int color, int onMS) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 122 | synchronized (this) { |
| 123 | if (mColor == 0 && !mFlashing) { |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 124 | setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000, |
| 125 | BRIGHTNESS_MODE_USER); |
Oskar Andero | e846719 | 2013-12-03 17:41:27 +0100 | [diff] [blame] | 126 | mColor = 0; |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 127 | mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 132 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 133 | public void turnOff() { |
| 134 | synchronized (this) { |
| 135 | setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0); |
| 136 | } |
| 137 | } |
| 138 | |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 139 | @Override |
| 140 | public void setVrMode(boolean enabled) { |
| 141 | synchronized (this) { |
| 142 | if (mVrModeEnabled != enabled) { |
| 143 | mVrModeEnabled = enabled; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 144 | |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 145 | mUseLowPersistenceForVR = |
| 146 | (getVrDisplayMode() == Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE); |
| 147 | if (shouldBeInLowPersistenceMode()) { |
| 148 | mLastBrightnessMode = mBrightnessMode; |
| 149 | } |
| 150 | |
| 151 | // NOTE: We do not trigger a call to setLightLocked here. We do not know the |
| 152 | // current brightness or other values when leaving VR so we avoid any incorrect |
| 153 | // jumps. The code that calls this method will immediately issue a brightness |
| 154 | // update which is when the change will occur. |
| 155 | } |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 159 | private void stopFlashing() { |
| 160 | synchronized (this) { |
| 161 | setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) { |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 166 | if (shouldBeInLowPersistenceMode()) { |
| 167 | brightnessMode = BRIGHTNESS_MODE_LOW_PERSISTENCE; |
| 168 | } else if (brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) { |
| 169 | brightnessMode = mLastBrightnessMode; |
| 170 | } |
| 171 | |
Oleksiy Avramchenko | 4f4939f | 2017-04-25 12:30:49 +0200 | [diff] [blame] | 172 | if (!mInitialized || color != mColor || mode != mMode || onMS != mOnMS || |
| 173 | offMS != mOffMS || mBrightnessMode != brightnessMode) { |
Dianne Hackborn | ff801ec | 2011-01-22 18:05:38 -0800 | [diff] [blame] | 174 | if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#" |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 175 | + Integer.toHexString(color) + ": brightnessMode=" + brightnessMode); |
Oleksiy Avramchenko | 4f4939f | 2017-04-25 12:30:49 +0200 | [diff] [blame] | 176 | mInitialized = true; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 177 | mLastColor = mColor; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 178 | mColor = color; |
| 179 | mMode = mode; |
| 180 | mOnMS = onMS; |
| 181 | mOffMS = offMS; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 182 | mBrightnessMode = brightnessMode; |
Jeff Brown | faec22c8 | 2015-04-10 12:58:52 -0700 | [diff] [blame] | 183 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x" |
| 184 | + Integer.toHexString(color) + ")"); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 185 | try { |
Steven Moreland | 8b9ec4f | 2016-10-04 17:25:52 -0700 | [diff] [blame] | 186 | setLight_native(mId, color, mode, onMS, offMS, brightnessMode); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 187 | } finally { |
| 188 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 189 | } |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 193 | private boolean shouldBeInLowPersistenceMode() { |
| 194 | return mVrModeEnabled && mUseLowPersistenceForVR; |
| 195 | } |
| 196 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 197 | private int mId; |
| 198 | private int mColor; |
| 199 | private int mMode; |
| 200 | private int mOnMS; |
| 201 | private int mOffMS; |
| 202 | private boolean mFlashing; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 203 | private int mBrightnessMode; |
| 204 | private int mLastBrightnessMode; |
| 205 | private int mLastColor; |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 206 | private boolean mVrModeEnabled; |
| 207 | private boolean mUseLowPersistenceForVR; |
Oleksiy Avramchenko | 4f4939f | 2017-04-25 12:30:49 +0200 | [diff] [blame] | 208 | private boolean mInitialized; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 209 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 210 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 211 | public LightsService(Context context) { |
| 212 | super(context); |
| 213 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 214 | for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) { |
Dan Gittik | 832b497 | 2019-02-13 18:17:47 +0000 | [diff] [blame] | 215 | mLights[i] = new LightImpl(context, i); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 216 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 219 | @Override |
| 220 | public void onStart() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 221 | publishLocalService(LightsManager.class, mService); |
| 222 | } |
| 223 | |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 224 | @Override |
| 225 | public void onBootPhase(int phase) { |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 228 | private int getVrDisplayMode() { |
| 229 | int currentUser = ActivityManager.getCurrentUser(); |
| 230 | return Settings.Secure.getIntForUser(getContext().getContentResolver(), |
| 231 | Settings.Secure.VR_DISPLAY_MODE, |
| 232 | /*default*/Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE, |
| 233 | currentUser); |
| 234 | } |
| 235 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 236 | private final LightsManager mService = new LightsManager() { |
| 237 | @Override |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 238 | public Light getLight(int id) { |
Steven Moreland | 8b9ec4f | 2016-10-04 17:25:52 -0700 | [diff] [blame] | 239 | if (0 <= id && id < LIGHT_ID_COUNT) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 240 | return mLights[id]; |
| 241 | } else { |
| 242 | return null; |
| 243 | } |
| 244 | } |
| 245 | }; |
| 246 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 247 | private Handler mH = new Handler() { |
| 248 | @Override |
| 249 | public void handleMessage(Message msg) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 250 | LightImpl light = (LightImpl)msg.obj; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 251 | light.stopFlashing(); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 252 | } |
| 253 | }; |
| 254 | |
Steven Moreland | 8b9ec4f | 2016-10-04 17:25:52 -0700 | [diff] [blame] | 255 | static native void setLight_native(int light, int color, int mode, |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 256 | int onMS, int offMS, int brightnessMode); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 257 | } |