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 | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 20 | import com.android.server.vr.VrManagerService; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 21 | |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 26 | import android.os.RemoteException; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 27 | import android.os.Trace; |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 28 | import android.os.UserHandle; |
| 29 | import android.provider.Settings; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 30 | import android.service.vr.IVrManager; |
| 31 | import android.service.vr.IVrStateCallbacks; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 32 | import android.util.Slog; |
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 | public class LightsService extends SystemService { |
| 35 | static final String TAG = "LightsService"; |
| 36 | static final boolean DEBUG = false; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 37 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 38 | final LightImpl mLights[] = new LightImpl[LightsManager.LIGHT_ID_COUNT]; |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 39 | private boolean mVrModeEnabled; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 40 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 41 | private final class LightImpl extends Light { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 42 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 43 | private LightImpl(int id) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 44 | mId = id; |
| 45 | } |
| 46 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 47 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 48 | public void setBrightness(int brightness) { |
| 49 | setBrightness(brightness, BRIGHTNESS_MODE_USER); |
| 50 | } |
| 51 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 52 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 53 | public void setBrightness(int brightness, int brightnessMode) { |
| 54 | synchronized (this) { |
| 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) { |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 83 | if (mBrightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) { |
| 84 | return; |
| 85 | } |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 86 | if (mColor == 0 && !mFlashing) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 87 | setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000, BRIGHTNESS_MODE_USER); |
Oskar Andero | e846719 | 2013-12-03 17:41:27 +0100 | [diff] [blame] | 88 | mColor = 0; |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 89 | mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 94 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 95 | public void turnOff() { |
| 96 | synchronized (this) { |
| 97 | setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0); |
| 98 | } |
| 99 | } |
| 100 | |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 101 | void enableLowPersistence() { |
| 102 | synchronized(this) { |
| 103 | setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_LOW_PERSISTENCE); |
| 104 | mLocked = true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void disableLowPersistence() { |
| 109 | synchronized(this) { |
| 110 | mLocked = false; |
| 111 | setLightLocked(mLastColor, LIGHT_FLASH_NONE, 0, 0, mLastBrightnessMode); |
| 112 | } |
| 113 | } |
| 114 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 115 | private void stopFlashing() { |
| 116 | synchronized (this) { |
| 117 | setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | 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] | 122 | if (!mLocked && (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS || |
| 123 | mBrightnessMode != brightnessMode)) { |
Dianne Hackborn | ff801ec | 2011-01-22 18:05:38 -0800 | [diff] [blame] | 124 | if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#" |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 125 | + Integer.toHexString(color) + ": brightnessMode=" + brightnessMode); |
| 126 | mLastColor = mColor; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 127 | mColor = color; |
| 128 | mMode = mode; |
| 129 | mOnMS = onMS; |
| 130 | mOffMS = offMS; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 131 | mLastBrightnessMode = mBrightnessMode; |
| 132 | mBrightnessMode = brightnessMode; |
Jeff Brown | faec22c8 | 2015-04-10 12:58:52 -0700 | [diff] [blame] | 133 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x" |
| 134 | + Integer.toHexString(color) + ")"); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 135 | try { |
Steven Moreland | 8b9ec4f | 2016-10-04 17:25:52 -0700 | [diff] [blame^] | 136 | setLight_native(mId, color, mode, onMS, offMS, brightnessMode); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 137 | } finally { |
| 138 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 139 | } |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | private int mId; |
| 144 | private int mColor; |
| 145 | private int mMode; |
| 146 | private int mOnMS; |
| 147 | private int mOffMS; |
| 148 | private boolean mFlashing; |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 149 | private int mBrightnessMode; |
| 150 | private int mLastBrightnessMode; |
| 151 | private int mLastColor; |
| 152 | private boolean mLocked; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 153 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 154 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 155 | public LightsService(Context context) { |
| 156 | super(context); |
| 157 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 158 | for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) { |
| 159 | mLights[i] = new LightImpl(i); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 160 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 163 | @Override |
| 164 | public void onStart() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 165 | publishLocalService(LightsManager.class, mService); |
| 166 | } |
| 167 | |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 168 | @Override |
| 169 | public void onBootPhase(int phase) { |
| 170 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 171 | IVrManager vrManager = |
| 172 | (IVrManager) getBinderService(VrManagerService.VR_MANAGER_BINDER_SERVICE); |
Wei Liu | ef89d21 | 2016-09-19 15:10:33 -0700 | [diff] [blame] | 173 | if (vrManager != null) { |
| 174 | try { |
| 175 | vrManager.registerListener(mVrStateCallbacks); |
| 176 | } catch (RemoteException e) { |
| 177 | Slog.e(TAG, "Failed to register VR mode state listener: " + e); |
| 178 | } |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 179 | } |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 183 | private int getVrDisplayMode() { |
| 184 | int currentUser = ActivityManager.getCurrentUser(); |
| 185 | return Settings.Secure.getIntForUser(getContext().getContentResolver(), |
| 186 | Settings.Secure.VR_DISPLAY_MODE, |
| 187 | /*default*/Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE, |
| 188 | currentUser); |
| 189 | } |
| 190 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 191 | private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() { |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 192 | @Override |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 193 | public void onVrStateChanged(boolean enabled) throws RemoteException { |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 194 | LightImpl l = mLights[LightsManager.LIGHT_ID_BACKLIGHT]; |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 195 | int vrDisplayMode = getVrDisplayMode(); |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 196 | |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 197 | // User leaves VR mode before altering display settings. |
| 198 | if (enabled && vrDisplayMode == Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE) { |
| 199 | if (!mVrModeEnabled) { |
| 200 | if (DEBUG) |
| 201 | Slog.v(TAG, "VR mode enabled, setting brightness to low persistence"); |
| 202 | l.enableLowPersistence(); |
| 203 | mVrModeEnabled = true; |
| 204 | } |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 205 | } else { |
Ruben Brunk | 49506e0 | 2016-04-18 18:10:47 -0700 | [diff] [blame] | 206 | if (mVrModeEnabled) { |
| 207 | if (DEBUG) Slog.v(TAG, "VR mode disabled, resetting brightnes"); |
| 208 | l.disableLowPersistence(); |
| 209 | mVrModeEnabled = false; |
| 210 | } |
Ruben Brunk | 8ad3f20 | 2015-12-28 14:45:11 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | }; |
| 214 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 215 | private final LightsManager mService = new LightsManager() { |
| 216 | @Override |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 217 | public Light getLight(int id) { |
Steven Moreland | 8b9ec4f | 2016-10-04 17:25:52 -0700 | [diff] [blame^] | 218 | if (0 <= id && id < LIGHT_ID_COUNT) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 219 | return mLights[id]; |
| 220 | } else { |
| 221 | return null; |
| 222 | } |
| 223 | } |
| 224 | }; |
| 225 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 226 | private Handler mH = new Handler() { |
| 227 | @Override |
| 228 | public void handleMessage(Message msg) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 229 | LightImpl light = (LightImpl)msg.obj; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 230 | light.stopFlashing(); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 231 | } |
| 232 | }; |
| 233 | |
Steven Moreland | 8b9ec4f | 2016-10-04 17:25:52 -0700 | [diff] [blame^] | 234 | static native void setLight_native(int light, int color, int mode, |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 235 | int onMS, int offMS, int brightnessMode); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 236 | } |