blob: eaae2ed40b2e0e201bbddd9b1bb1872bf9271fc8 [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 }
Fiona Campbell466dc042019-11-22 12:29:44 +000087 if (brightness == 0) {
88 SurfaceControl.setDisplayBrightness(mDisplayToken, -1.0f);
89 } else {
90 SurfaceControl.setDisplayBrightness(mDisplayToken,
91 (float) (brightness - 1) / (mSurfaceControlMaximumBrightness - 1));
92 }
Dan Gittik832b4972019-02-13 18:17:47 +000093 } 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 Lockwood3cb67a32009-11-27 14:25:58 -050098 }
99 }
100
Adam Lesinski182f73f2013-12-05 16:48:06 -0800101 @Override
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500102 public void setColor(int color) {
103 synchronized (this) {
104 setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, 0);
105 }
106 }
107
Adam Lesinski182f73f2013-12-05 16:48:06 -0800108 @Override
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500109 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 Lesinski182f73f2013-12-05 16:48:06 -0800115 @Override
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500116 public void pulse() {
Mike Lockwood670f9322010-01-20 12:13:36 -0500117 pulse(0x00ffffff, 7);
118 }
119
Adam Lesinski182f73f2013-12-05 16:48:06 -0800120 @Override
Mike Lockwood670f9322010-01-20 12:13:36 -0500121 public void pulse(int color, int onMS) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500122 synchronized (this) {
123 if (mColor == 0 && !mFlashing) {
Santos Cordon3107d292016-09-20 15:50:35 -0700124 setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000,
125 BRIGHTNESS_MODE_USER);
Oskar Anderoe8467192013-12-03 17:41:27 +0100126 mColor = 0;
Mike Lockwood670f9322010-01-20 12:13:36 -0500127 mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500128 }
129 }
130 }
131
Adam Lesinski182f73f2013-12-05 16:48:06 -0800132 @Override
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500133 public void turnOff() {
134 synchronized (this) {
135 setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0);
136 }
137 }
138
Santos Cordon3107d292016-09-20 15:50:35 -0700139 @Override
140 public void setVrMode(boolean enabled) {
141 synchronized (this) {
142 if (mVrModeEnabled != enabled) {
143 mVrModeEnabled = enabled;
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800144
Santos Cordon3107d292016-09-20 15:50:35 -0700145 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 Brunk8ad3f202015-12-28 14:45:11 -0800156 }
157 }
158
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500159 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 Cordon3107d292016-09-20 15:50:35 -0700166 if (shouldBeInLowPersistenceMode()) {
167 brightnessMode = BRIGHTNESS_MODE_LOW_PERSISTENCE;
168 } else if (brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) {
169 brightnessMode = mLastBrightnessMode;
170 }
171
Oleksiy Avramchenko4f4939f2017-04-25 12:30:49 +0200172 if (!mInitialized || color != mColor || mode != mMode || onMS != mOnMS ||
173 offMS != mOffMS || mBrightnessMode != brightnessMode) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800174 if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#"
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800175 + Integer.toHexString(color) + ": brightnessMode=" + brightnessMode);
Oleksiy Avramchenko4f4939f2017-04-25 12:30:49 +0200176 mInitialized = true;
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800177 mLastColor = mColor;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500178 mColor = color;
179 mMode = mode;
180 mOnMS = onMS;
181 mOffMS = offMS;
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800182 mBrightnessMode = brightnessMode;
Jeff Brownfaec22c82015-04-10 12:58:52 -0700183 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x"
184 + Integer.toHexString(color) + ")");
Jeff Brown3edf5272014-08-14 19:25:14 -0700185 try {
Steven Moreland8b9ec4f2016-10-04 17:25:52 -0700186 setLight_native(mId, color, mode, onMS, offMS, brightnessMode);
Jeff Brown3edf5272014-08-14 19:25:14 -0700187 } finally {
188 Trace.traceEnd(Trace.TRACE_TAG_POWER);
189 }
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500190 }
191 }
192
Santos Cordon3107d292016-09-20 15:50:35 -0700193 private boolean shouldBeInLowPersistenceMode() {
194 return mVrModeEnabled && mUseLowPersistenceForVR;
195 }
196
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500197 private int mId;
198 private int mColor;
199 private int mMode;
200 private int mOnMS;
201 private int mOffMS;
202 private boolean mFlashing;
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800203 private int mBrightnessMode;
204 private int mLastBrightnessMode;
205 private int mLastColor;
Santos Cordon3107d292016-09-20 15:50:35 -0700206 private boolean mVrModeEnabled;
207 private boolean mUseLowPersistenceForVR;
Oleksiy Avramchenko4f4939f2017-04-25 12:30:49 +0200208 private boolean mInitialized;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500209 }
Mike Lockwood3a322132009-11-24 00:30:52 -0500210
Jeff Brownb880d882014-02-10 19:47:07 -0800211 public LightsService(Context context) {
212 super(context);
213
Adam Lesinski182f73f2013-12-05 16:48:06 -0800214 for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) {
Dan Gittik832b4972019-02-13 18:17:47 +0000215 mLights[i] = new LightImpl(context, i);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500216 }
Mike Lockwood3a322132009-11-24 00:30:52 -0500217 }
218
Adam Lesinski182f73f2013-12-05 16:48:06 -0800219 @Override
220 public void onStart() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800221 publishLocalService(LightsManager.class, mService);
222 }
223
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800224 @Override
225 public void onBootPhase(int phase) {
Ruben Brunk8ad3f202015-12-28 14:45:11 -0800226 }
227
Ruben Brunk49506e02016-04-18 18:10:47 -0700228 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 Lesinski182f73f2013-12-05 16:48:06 -0800236 private final LightsManager mService = new LightsManager() {
237 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -0700238 public Light getLight(int id) {
Steven Moreland8b9ec4f2016-10-04 17:25:52 -0700239 if (0 <= id && id < LIGHT_ID_COUNT) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800240 return mLights[id];
241 } else {
242 return null;
243 }
244 }
245 };
246
Mike Lockwood3a322132009-11-24 00:30:52 -0500247 private Handler mH = new Handler() {
248 @Override
249 public void handleMessage(Message msg) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800250 LightImpl light = (LightImpl)msg.obj;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500251 light.stopFlashing();
Mike Lockwood3a322132009-11-24 00:30:52 -0500252 }
253 };
254
Steven Moreland8b9ec4f2016-10-04 17:25:52 -0700255 static native void setLight_native(int light, int color, int mode,
Mike Lockwood3a322132009-11-24 00:30:52 -0500256 int onMS, int offMS, int brightnessMode);
Mike Lockwood3a322132009-11-24 00:30:52 -0500257}