blob: ac906bb23d3221bff0ba65b5b16087afa108dda9 [file] [log] [blame]
Santos Cordon3107d292016-09-20 15:50:35 -07001/* * Copyright (C) 2008 The Android Open Source Project
Mike Lockwood3a322132009-11-24 00:30:52 -05002 *
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 Lesinski182f73f2013-12-05 16:48:06 -080016package com.android.server.lights;
17
Ruben Brunk49506e02016-04-18 18:10:47 -070018import android.app.ActivityManager;
Mike Lockwood3a322132009-11-24 00:30:52 -050019import android.content.Context;
20import android.os.Handler;
Dan Gittik832b4972019-02-13 18:17:47 +000021import android.os.IBinder;
Mike Lockwood3a322132009-11-24 00:30:52 -050022import android.os.Message;
Dan Gittik832b4972019-02-13 18:17:47 +000023import android.os.PowerManager;
Jeff Brown3edf5272014-08-14 19:25:14 -070024import android.os.Trace;
Ruben Brunk49506e02016-04-18 18:10:47 -070025import android.provider.Settings;
Joe Onorato8a9b2202010-02-26 18:56:32 -080026import android.util.Slog;
Dan Gittik832b4972019-02-13 18:17:47 +000027import android.view.SurfaceControl;
28
29import com.android.server.SystemService;
Mike Lockwood3a322132009-11-24 00:30:52 -050030
Adam Lesinski182f73f2013-12-05 16:48:06 -080031public class LightsService extends SystemService {
32 static final String TAG = "LightsService";
33 static final boolean DEBUG = false;
Mike Lockwood3a322132009-11-24 00:30:52 -050034
Adam Lesinski182f73f2013-12-05 16:48:06 -080035 final LightImpl mLights[] = new LightImpl[LightsManager.LIGHT_ID_COUNT];
Mike Lockwood3a322132009-11-24 00:30:52 -050036
Adam Lesinski182f73f2013-12-05 16:48:06 -080037 private final class LightImpl extends Light {
Mike Lockwood3a322132009-11-24 00:30:52 -050038
Dan Gittik832b4972019-02-13 18:17:47 +000039 private final IBinder mDisplayToken;
40 private final int mSurfaceControlMaximumBrightness;
41
42 private LightImpl(Context context, int id) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -050043 mId = id;
Dan Gittik832b4972019-02-13 18:17:47 +000044 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 Lockwood3cb67a32009-11-27 14:25:58 -050058 }
59
Adam Lesinski182f73f2013-12-05 16:48:06 -080060 @Override
Mike Lockwood3cb67a32009-11-27 14:25:58 -050061 public void setBrightness(int brightness) {
62 setBrightness(brightness, BRIGHTNESS_MODE_USER);
63 }
64
Adam Lesinski182f73f2013-12-05 16:48:06 -080065 @Override
Mike Lockwood3cb67a32009-11-27 14:25:58 -050066 public void setBrightness(int brightness, int brightnessMode) {
67 synchronized (this) {
Santos Cordon3107d292016-09-20 15:50:35 -070068 // 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 Gittik832b4972019-02-13 18:17:47 +000074 // 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 }
87 SurfaceControl.setDisplayBrightness(mDisplayToken,
88 (float) brightness / mSurfaceControlMaximumBrightness);
89 } else {
90 int color = brightness & 0x000000ff;
91 color = 0xff000000 | (color << 16) | (color << 8) | color;
92 setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode);
93 }
Mike Lockwood3cb67a32009-11-27 14:25:58 -050094 }
95 }
96
Adam Lesinski182f73f2013-12-05 16:48:06 -080097 @Override
Mike Lockwood3cb67a32009-11-27 14:25:58 -050098 public void setColor(int color) {
99 synchronized (this) {
100 setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, 0);
101 }
102 }
103
Adam Lesinski182f73f2013-12-05 16:48:06 -0800104 @Override
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500105 public void setFlashing(int color, int mode, int onMS, int offMS) {
106 synchronized (this) {
107 setLightLocked(color, mode, onMS, offMS, BRIGHTNESS_MODE_USER);
108 }
109 }
110
Adam Lesinski182f73f2013-12-05 16:48:06 -0800111 @Override
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500112 public void pulse() {
Mike Lockwood670f9322010-01-20 12:13:36 -0500113 pulse(0x00ffffff, 7);
114 }
115
Adam Lesinski182f73f2013-12-05 16:48:06 -0800116 @Override
Mike Lockwood670f9322010-01-20 12:13:36 -0500117 public void pulse(int color, int onMS) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500118 synchronized (this) {
119 if (mColor == 0 && !mFlashing) {
Santos Cordon3107d292016-09-20 15:50:35 -0700120 setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000,
121 BRIGHTNESS_MODE_USER);
Oskar Anderoe8467192013-12-03 17:41:27 +0100122 mColor = 0;
Mike Lockwood670f9322010-01-20 12:13:36 -0500123 mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500124 }
125 }
126 }
127
Adam Lesinski182f73f2013-12-05 16:48:06 -0800128 @Override
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500129 public void turnOff() {
130 synchronized (this) {
131 setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0);
132 }
133 }
134
Santos Cordon3107d292016-09-20 15:50:35 -0700135 @Override
136 public void setVrMode(boolean enabled) {
137 synchronized (this) {
138 if (mVrModeEnabled != enabled) {
139 mVrModeEnabled = enabled;
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800140
Santos Cordon3107d292016-09-20 15:50:35 -0700141 mUseLowPersistenceForVR =
142 (getVrDisplayMode() == Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE);
143 if (shouldBeInLowPersistenceMode()) {
144 mLastBrightnessMode = mBrightnessMode;
145 }
146
147 // NOTE: We do not trigger a call to setLightLocked here. We do not know the
148 // current brightness or other values when leaving VR so we avoid any incorrect
149 // jumps. The code that calls this method will immediately issue a brightness
150 // update which is when the change will occur.
151 }
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800152 }
153 }
154
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500155 private void stopFlashing() {
156 synchronized (this) {
157 setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER);
158 }
159 }
160
161 private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) {
Santos Cordon3107d292016-09-20 15:50:35 -0700162 if (shouldBeInLowPersistenceMode()) {
163 brightnessMode = BRIGHTNESS_MODE_LOW_PERSISTENCE;
164 } else if (brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) {
165 brightnessMode = mLastBrightnessMode;
166 }
167
Oleksiy Avramchenko4f4939f2017-04-25 12:30:49 +0200168 if (!mInitialized || color != mColor || mode != mMode || onMS != mOnMS ||
169 offMS != mOffMS || mBrightnessMode != brightnessMode) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800170 if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#"
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800171 + Integer.toHexString(color) + ": brightnessMode=" + brightnessMode);
Oleksiy Avramchenko4f4939f2017-04-25 12:30:49 +0200172 mInitialized = true;
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800173 mLastColor = mColor;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500174 mColor = color;
175 mMode = mode;
176 mOnMS = onMS;
177 mOffMS = offMS;
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800178 mBrightnessMode = brightnessMode;
Jeff Brownfaec22c82015-04-10 12:58:52 -0700179 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x"
180 + Integer.toHexString(color) + ")");
Jeff Brown3edf5272014-08-14 19:25:14 -0700181 try {
Steven Moreland8b9ec4f2016-10-04 17:25:52 -0700182 setLight_native(mId, color, mode, onMS, offMS, brightnessMode);
Jeff Brown3edf5272014-08-14 19:25:14 -0700183 } finally {
184 Trace.traceEnd(Trace.TRACE_TAG_POWER);
185 }
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500186 }
187 }
188
Santos Cordon3107d292016-09-20 15:50:35 -0700189 private boolean shouldBeInLowPersistenceMode() {
190 return mVrModeEnabled && mUseLowPersistenceForVR;
191 }
192
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500193 private int mId;
194 private int mColor;
195 private int mMode;
196 private int mOnMS;
197 private int mOffMS;
198 private boolean mFlashing;
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800199 private int mBrightnessMode;
200 private int mLastBrightnessMode;
201 private int mLastColor;
Santos Cordon3107d292016-09-20 15:50:35 -0700202 private boolean mVrModeEnabled;
203 private boolean mUseLowPersistenceForVR;
Oleksiy Avramchenko4f4939f2017-04-25 12:30:49 +0200204 private boolean mInitialized;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500205 }
Mike Lockwood3a322132009-11-24 00:30:52 -0500206
Jeff Brownb880d882014-02-10 19:47:07 -0800207 public LightsService(Context context) {
208 super(context);
209
Adam Lesinski182f73f2013-12-05 16:48:06 -0800210 for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) {
Dan Gittik832b4972019-02-13 18:17:47 +0000211 mLights[i] = new LightImpl(context, i);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500212 }
Mike Lockwood3a322132009-11-24 00:30:52 -0500213 }
214
Adam Lesinski182f73f2013-12-05 16:48:06 -0800215 @Override
216 public void onStart() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800217 publishLocalService(LightsManager.class, mService);
218 }
219
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800220 @Override
221 public void onBootPhase(int phase) {
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800222 }
223
Ruben Brunk49506e02016-04-18 18:10:47 -0700224 private int getVrDisplayMode() {
225 int currentUser = ActivityManager.getCurrentUser();
226 return Settings.Secure.getIntForUser(getContext().getContentResolver(),
227 Settings.Secure.VR_DISPLAY_MODE,
228 /*default*/Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE,
229 currentUser);
230 }
231
Adam Lesinski182f73f2013-12-05 16:48:06 -0800232 private final LightsManager mService = new LightsManager() {
233 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -0700234 public Light getLight(int id) {
Steven Moreland8b9ec4f2016-10-04 17:25:52 -0700235 if (0 <= id && id < LIGHT_ID_COUNT) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800236 return mLights[id];
237 } else {
238 return null;
239 }
240 }
241 };
242
Mike Lockwood3a322132009-11-24 00:30:52 -0500243 private Handler mH = new Handler() {
244 @Override
245 public void handleMessage(Message msg) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800246 LightImpl light = (LightImpl)msg.obj;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500247 light.stopFlashing();
Mike Lockwood3a322132009-11-24 00:30:52 -0500248 }
249 };
250
Steven Moreland8b9ec4f2016-10-04 17:25:52 -0700251 static native void setLight_native(int light, int color, int mode,
Mike Lockwood3a322132009-11-24 00:30:52 -0500252 int onMS, int offMS, int brightnessMode);
Mike Lockwood3a322132009-11-24 00:30:52 -0500253}