blob: 9ffe89c61a44eb22224a8ee0379520055383e8fa [file] [log] [blame]
Dianne Hackborn7299c412010-03-04 18:41:49 -08001/*
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
17package com.android.server;
18
Tyler Gunndaef6682019-09-30 15:12:24 -070019import android.annotation.IntRange;
Justin Klaassen908b86c2016-08-08 09:18:42 -070020import android.annotation.Nullable;
Dianne Hackborn7299c412010-03-04 18:41:49 -080021import android.app.Activity;
Jeff Brown62c82e42012-09-26 01:30:41 -070022import android.app.ActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070023import android.app.ActivityTaskManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080024import android.app.IUiModeManager;
25import android.app.Notification;
26import android.app.NotificationManager;
27import android.app.PendingIntent;
28import android.app.StatusBarManager;
29import android.app.UiModeManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080030import android.content.BroadcastReceiver;
31import android.content.Context;
32import android.content.Intent;
33import android.content.IntentFilter;
34import android.content.pm.PackageManager;
35import android.content.res.Configuration;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080036import android.content.res.Resources;
Salvador Martinezc500b272019-05-02 14:32:12 -070037import android.database.ContentObserver;
38import android.net.Uri;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050039import android.os.BatteryManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080040import android.os.Binder;
Dianne Hackborn7299c412010-03-04 18:41:49 -080041import android.os.Handler;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050042import android.os.PowerManager;
Kweku Adams4db6a3c2019-02-04 16:06:13 -080043import android.os.PowerManager.ServiceType;
44import android.os.PowerManagerInternal;
Tyler Gunndaef6682019-09-30 15:12:24 -070045import android.os.Process;
Dianne Hackborn7299c412010-03-04 18:41:49 -080046import android.os.RemoteException;
Alan Viveretteba0d98f2017-01-30 10:36:54 -050047import android.os.ResultReceiver;
Zak Cohen1a705732017-01-09 12:54:34 -080048import android.os.ServiceManager;
Alan Viveretteba0d98f2017-01-30 10:36:54 -050049import android.os.ShellCallback;
50import android.os.ShellCommand;
Beverly28e48fb2019-06-14 14:53:09 -040051import android.os.SystemProperties;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070052import android.os.UserHandle;
Salvador Martinezc500b272019-05-02 14:32:12 -070053import android.provider.Settings.Secure;
Jeff Brown11159e92012-10-11 15:58:37 -070054import android.service.dreams.Sandman;
Zak Cohen1a705732017-01-09 12:54:34 -080055import android.service.vr.IVrManager;
56import android.service.vr.IVrStateCallbacks;
Tyler Gunndaef6682019-09-30 15:12:24 -070057import android.util.ArraySet;
Dianne Hackborn7299c412010-03-04 18:41:49 -080058import android.util.Slog;
Jeff Sharkey5f116e12019-12-07 16:15:29 +000059
Dianne Hackborn7299c412010-03-04 18:41:49 -080060import com.android.internal.R;
Jay Aliomer8b2671b2019-10-24 13:18:06 -040061import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborn7299c412010-03-04 18:41:49 -080062import com.android.internal.app.DisableCarModeActivity;
Chris Wren282cfef2017-03-27 15:01:44 -040063import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050064import com.android.internal.notification.SystemNotificationChannels;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060065import com.android.internal.util.DumpUtils;
Adam Lesinski182f73f2013-12-05 16:48:06 -080066import com.android.server.twilight.TwilightListener;
67import com.android.server.twilight.TwilightManager;
68import com.android.server.twilight.TwilightState;
Jay Aliomer8b2671b2019-10-24 13:18:06 -040069import com.android.server.wm.WindowManagerInternal;
Dianne Hackborn7299c412010-03-04 18:41:49 -080070
Lucas Dupinb1f0c762018-09-18 10:33:38 -070071import java.io.FileDescriptor;
72import java.io.PrintWriter;
Tyler Gunndaef6682019-09-30 15:12:24 -070073import java.util.HashMap;
74import java.util.Map;
75import java.util.Set;
Lucas Dupinb1f0c762018-09-18 10:33:38 -070076
Adam Lesinski182f73f2013-12-05 16:48:06 -080077final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080078 private static final String TAG = UiModeManager.class.getSimpleName();
79 private static final boolean LOG = false;
80
Daniel Sandler11ddf532011-11-16 11:10:03 -080081 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040082 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Beverly28e48fb2019-06-14 14:53:09 -040083 private static final String SYSTEM_PROPERTY_DEVICE_THEME = "persist.sys.theme";
Daniel Sandler11ddf532011-11-16 11:10:03 -080084
Dianne Hackborn7299c412010-03-04 18:41:49 -080085 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080086 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010087
Adam Lesinski182f73f2013-12-05 16:48:06 -080088 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080089 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Jay Aliomer8b2671b2019-10-24 13:18:06 -040090 // we use the override auto mode
91 // for example: force night mode off in the night time while in auto mode
92 private int mNightModeOverride = mNightMode;
93 protected static final String OVERRIDE_NIGHT_MODE = Secure.UI_NIGHT_MODE + "_override";
Adam Lesinski182f73f2013-12-05 16:48:06 -080094
Tyler Gunndaef6682019-09-30 15:12:24 -070095 private Map<Integer, String> mCarModePackagePriority = new HashMap<>();
Dianne Hackborn7299c412010-03-04 18:41:49 -080096 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050097 private boolean mCharging = false;
Lucas Dupin53c6e292018-07-12 18:42:53 -070098 private boolean mPowerSave = false;
Jeff Sharkey5f116e12019-12-07 16:15:29 +000099 // Do not change configuration now. wait until screen turns off.
100 // This prevents jank and activity restart when the user
101 // is actively using the device
102 private boolean mWaitForScreenOff = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800103 private int mDefaultUiModeType;
104 private boolean mCarModeKeepsScreenOn;
105 private boolean mDeskModeKeepsScreenOn;
106 private boolean mTelevision;
JianYang Liub444a972019-12-10 14:32:30 -0800107 private boolean mCar;
John Spurlock6c191292014-04-03 16:37:27 -0400108 private boolean mWatch;
Zak Cohen1a705732017-01-09 12:54:34 -0800109 private boolean mVrHeadset;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800110 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700111 private int mCarModeEnableFlags;
Salvador Martinezc500b272019-05-02 14:32:12 -0700112 private boolean mSetupWizardComplete;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800113
keunyounga7710492015-09-23 11:33:58 -0700114 // flag set by resource, whether to enable Car dock launch when starting car mode.
115 private boolean mEnableCarDockLaunch = true;
116 // flag set by resource, whether to lock UI mode to the default one or not.
117 private boolean mUiModeLocked = false;
118 // flag set by resource, whether to night mode change for normal all or not.
119 private boolean mNightModeLocked = false;
120
Adam Lesinski182f73f2013-12-05 16:48:06 -0800121 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800122 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800123 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800124
Dianne Hackborn7299c412010-03-04 18:41:49 -0800125 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800126 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100127
Adam Lesinski182f73f2013-12-05 16:48:06 -0800128 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800129
Adam Lesinski182f73f2013-12-05 16:48:06 -0800130 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800131 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800132 private StatusBarManager mStatusBarManager;
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400133 private WindowManagerInternal mWindowManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700134
Adam Lesinski182f73f2013-12-05 16:48:06 -0800135 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800136
Felipe Lemeff9ec382018-09-24 11:07:56 -0700137 private final LocalService mLocalService = new LocalService();
138
Jeff Brownb880d882014-02-10 19:47:07 -0800139 public UiModeManagerService(Context context) {
140 super(context);
141 }
142
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400143 @VisibleForTesting
144 protected UiModeManagerService(Context context, WindowManagerInternal wm,
145 PowerManager.WakeLock wl, TwilightManager tm,
146 boolean setupWizardComplete) {
147 super(context);
148 mWindowManager = wm;
149 mWakeLock = wl;
150 mTwilightManager = tm;
151 mSetupWizardComplete = setupWizardComplete;
152 }
153
Adam Lesinski182f73f2013-12-05 16:48:06 -0800154 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700155 Intent intent = new Intent(Intent.ACTION_MAIN);
156 intent.addCategory(category);
157 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
158 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
159 return intent;
160 }
John Spurlock960779d2012-05-29 14:37:05 -0400161
Dianne Hackborn7299c412010-03-04 18:41:49 -0800162 // The broadcast receiver which receives the result of the ordered broadcast sent when
163 // the dock state changes. The original ordered broadcast is sent with an initial result
164 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
165 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
166 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
167 @Override
168 public void onReceive(Context context, Intent intent) {
169 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400170 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400171 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400172 + ": canceled: " + getResultCode());
173 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800174 return;
175 }
176
Jeff Brown62c82e42012-09-26 01:30:41 -0700177 final int enableFlags = intent.getIntExtra("enableFlags", 0);
178 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800179 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700180 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800181 }
182 }
183 };
184
Dianne Hackborn7299c412010-03-04 18:41:49 -0800185 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
186 @Override
187 public void onReceive(Context context, Intent intent) {
188 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
189 Intent.EXTRA_DOCK_STATE_UNDOCKED);
190 updateDockState(state);
191 }
192 };
193
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500194 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
195 @Override
196 public void onReceive(Context context, Intent intent) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700197 switch (intent.getAction()) {
198 case Intent.ACTION_BATTERY_CHANGED:
199 mCharging = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
200 break;
Lucas Dupin53c6e292018-07-12 18:42:53 -0700201 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500202 synchronized (mLock) {
203 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700204 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500205 }
206 }
207 }
208 };
209
Adam Lesinski182f73f2013-12-05 16:48:06 -0800210 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700211 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700212 public void onTwilightStateChanged(@Nullable TwilightState state) {
213 synchronized (mLock) {
214 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
JianYang Liub444a972019-12-10 14:32:30 -0800215 if (mCar) {
216 updateLocked(0, 0);
217 } else {
218 registerScreenOffEvent();
219 }
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400220 }
221 }
222 }
223 };
224
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000225 /**
226 * DO NOT USE DIRECTLY
227 * see register registerScreenOffEvent and unregisterScreenOffEvent
228 */
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400229 private final BroadcastReceiver mOnScreenOffHandler = new BroadcastReceiver() {
230 @Override
231 public void onReceive(Context context, Intent intent) {
232 synchronized (mLock) {
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000233 // must unregister first before updating
234 unregisterScreenOffEvent();
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400235 updateLocked(0, 0);
Justin Klaassen908b86c2016-08-08 09:18:42 -0700236 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700237 }
238 };
239
Zak Cohen1a705732017-01-09 12:54:34 -0800240 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
241 @Override
242 public void onVrStateChanged(boolean enabled) {
243 synchronized (mLock) {
244 mVrHeadset = enabled;
245 if (mSystemReady) {
246 updateLocked(0, 0);
247 }
248 }
249 }
250 };
251
Salvador Martinezc500b272019-05-02 14:32:12 -0700252 private final ContentObserver mSetupWizardObserver = new ContentObserver(mHandler) {
253 @Override
254 public void onChange(boolean selfChange, Uri uri) {
255 // setup wizard is done now so we can unblock
256 if (setupWizardCompleteForCurrentUser()) {
257 mSetupWizardComplete = true;
258 getContext().getContentResolver().unregisterContentObserver(mSetupWizardObserver);
259 // update night mode
260 Context context = getContext();
261 updateNightModeFromSettings(context, context.getResources(),
262 UserHandle.getCallingUserId());
263 updateLocked(0, 0);
264 }
265 }
266 };
267
Beverly4f2ff8e2019-07-22 17:15:58 -0400268 private final ContentObserver mDarkThemeObserver = new ContentObserver(mHandler) {
269 @Override
270 public void onChange(boolean selfChange, Uri uri) {
271 int mode = Secure.getIntForUser(getContext().getContentResolver(), Secure.UI_NIGHT_MODE,
272 mNightMode, 0);
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400273 mode = mode == UiModeManager.MODE_NIGHT_AUTO
274 ? UiModeManager.MODE_NIGHT_YES : UiModeManager.MODE_NIGHT_NO;
Beverly4f2ff8e2019-07-22 17:15:58 -0400275 SystemProperties.set(SYSTEM_PROPERTY_DEVICE_THEME, Integer.toString(mode));
276 }
277 };
278
Salvador Martinezc500b272019-05-02 14:32:12 -0700279 @Override
280 public void onSwitchUser(int userHandle) {
281 super.onSwitchUser(userHandle);
282 getContext().getContentResolver().unregisterContentObserver(mSetupWizardObserver);
283 verifySetupWizardCompleted();
284 }
285
Adam Lesinski182f73f2013-12-05 16:48:06 -0800286 @Override
287 public void onStart() {
288 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800289
Adam Lesinski182f73f2013-12-05 16:48:06 -0800290 final PowerManager powerManager =
291 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
292 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400293 mWindowManager = LocalServices.getService(WindowManagerInternal.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800294
Salvador Martinezc500b272019-05-02 14:32:12 -0700295 // If setup isn't complete for this user listen for completion so we can unblock
296 // being able to send a night mode configuration change event
297 verifySetupWizardCompleted();
298
Adam Lesinski182f73f2013-12-05 16:48:06 -0800299 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800300 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Lucas Dupin53c6e292018-07-12 18:42:53 -0700301 IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Lucas Dupin53c6e292018-07-12 18:42:53 -0700302 context.registerReceiver(mBatteryReceiver, batteryFilter);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500303
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800304 PowerManagerInternal localPowerManager =
305 LocalServices.getService(PowerManagerInternal.class);
306 mPowerSave = localPowerManager.getLowPowerState(ServiceType.NIGHT_MODE).batterySaverEnabled;
307 localPowerManager.registerLowPowerModeObserver(ServiceType.NIGHT_MODE,
308 state -> {
309 synchronized (mLock) {
310 if (mPowerSave == state.batterySaverEnabled) {
311 return;
312 }
313 mPowerSave = state.batterySaverEnabled;
314 if (mSystemReady) {
315 updateLocked(0, 0);
316 }
317 }
318 });
319
Dianne Hackborn7299c412010-03-04 18:41:49 -0800320 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500321
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800322 final Resources res = context.getResources();
323 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800324 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800325 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500326 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800327 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500328 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700329 mEnableCarDockLaunch = res.getBoolean(
330 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
331 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
332 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700333
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800334 final PackageManager pm = context.getPackageManager();
335 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
336 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
JianYang Liub444a972019-12-10 14:32:30 -0800337 mCar = pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800338 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
339
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700340 updateNightModeFromSettings(context, res, UserHandle.getCallingUserId());
Jeff Brown2416e092012-08-21 22:12:20 -0700341
Adam Lesinski05199e82015-03-19 14:37:11 -0700342 // Update the initial, static configurations.
Felipe Lemeb68b7692019-10-09 10:43:03 -0700343 SystemServerInitThreadPool.submit(() -> {
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800344 synchronized (mLock) {
345 updateConfigurationLocked();
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000346 applyConfigurationExternallyLocked();
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800347 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800348
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800349 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800350 publishBinderService(Context.UI_MODE_SERVICE, mService);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700351 publishLocalService(UiModeManagerInternal.class, mLocalService);
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700352
353 IntentFilter filter = new IntentFilter();
354 filter.addAction(Intent.ACTION_USER_SWITCHED);
355 context.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
Beverly4f2ff8e2019-07-22 17:15:58 -0400356
357 context.getContentResolver().registerContentObserver(Secure.getUriFor(Secure.UI_NIGHT_MODE),
358 false, mDarkThemeObserver, 0);
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700359 }
360
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400361 @VisibleForTesting
362 protected IUiModeManager getService() {
363 return mService;
364 }
365
366 @VisibleForTesting
367 protected Configuration getConfiguration() {
368 return mConfiguration;
369 }
370
Salvador Martinezc500b272019-05-02 14:32:12 -0700371 // Records whether setup wizard has happened or not and adds an observer for this user if not.
372 private void verifySetupWizardCompleted() {
373 final Context context = getContext();
374 final int userId = UserHandle.getCallingUserId();
375 if (!setupWizardCompleteForCurrentUser()) {
376 mSetupWizardComplete = false;
377 context.getContentResolver().registerContentObserver(
378 Secure.getUriFor(
379 Secure.USER_SETUP_COMPLETE), false, mSetupWizardObserver, userId);
380 } else {
381 mSetupWizardComplete = true;
382 }
383 }
384
385 private boolean setupWizardCompleteForCurrentUser() {
386 return Secure.getIntForUser(getContext().getContentResolver(),
387 Secure.USER_SETUP_COMPLETE, 0, UserHandle.getCallingUserId()) == 1;
388 }
389
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700390 /**
391 * Updates the night mode setting in Settings.Global and returns if the value was successfully
392 * changed.
393 * @param context A valid context
394 * @param res A valid resource object
395 * @param userId The user to update the setting for
396 * @return True if the new value is different from the old value. False otherwise.
397 */
398 private boolean updateNightModeFromSettings(Context context, Resources res, int userId) {
399 final int defaultNightMode = res.getInteger(
400 com.android.internal.R.integer.config_defaultNightMode);
401 int oldNightMode = mNightMode;
Salvador Martinezc500b272019-05-02 14:32:12 -0700402 if (mSetupWizardComplete) {
403 mNightMode = Secure.getIntForUser(context.getContentResolver(),
404 Secure.UI_NIGHT_MODE, defaultNightMode, userId);
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400405 mNightModeOverride = Secure.getIntForUser(context.getContentResolver(),
406 OVERRIDE_NIGHT_MODE, defaultNightMode, userId);
Salvador Martinezc500b272019-05-02 14:32:12 -0700407 } else {
408 mNightMode = defaultNightMode;
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400409 mNightModeOverride = defaultNightMode;
Salvador Martinezc500b272019-05-02 14:32:12 -0700410 }
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700411
Beverly28e48fb2019-06-14 14:53:09 -0400412 return oldNightMode != mNightMode;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800413 }
414
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000415 private void registerScreenOffEvent() {
416 mWaitForScreenOff = true;
417 final IntentFilter intentFilter =
418 new IntentFilter(Intent.ACTION_SCREEN_OFF);
419 getContext().registerReceiver(mOnScreenOffHandler, intentFilter);
420 }
421
422 private void unregisterScreenOffEvent() {
423 mWaitForScreenOff = false;
424 try {
425 getContext().unregisterReceiver(mOnScreenOffHandler);
426 } catch (IllegalArgumentException e) {
427 // we ignore this exception if the receiver is unregistered already.
428 }
429 }
430
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500431 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800432 @Override
Tyler Gunndaef6682019-09-30 15:12:24 -0700433 public void enableCarMode(@UiModeManager.EnableCarMode int flags,
434 @IntRange(from = 0) int priority, String callingPackage) {
keunyounga7710492015-09-23 11:33:58 -0700435 if (isUiModeLocked()) {
436 Slog.e(TAG, "enableCarMode while UI mode is locked");
437 return;
438 }
Tyler Gunndaef6682019-09-30 15:12:24 -0700439
440 if (priority != UiModeManager.DEFAULT_PRIORITY
441 && getContext().checkCallingOrSelfPermission(
442 android.Manifest.permission.ENTER_CAR_MODE_PRIORITIZED)
443 != PackageManager.PERMISSION_GRANTED) {
444 throw new SecurityException("Enabling car mode with a priority requires "
445 + "permission ENTER_CAR_MODE_PRIORITIZED");
446 }
447
Adam Lesinski182f73f2013-12-05 16:48:06 -0800448 final long ident = Binder.clearCallingIdentity();
449 try {
450 synchronized (mLock) {
Tyler Gunndaef6682019-09-30 15:12:24 -0700451 setCarModeLocked(true, flags, priority, callingPackage);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800452 if (mSystemReady) {
453 updateLocked(flags, 0);
454 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700455 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800456 } finally {
457 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500458 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800459 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800460
Tyler Gunndaef6682019-09-30 15:12:24 -0700461 /**
462 * This method is only kept around for the time being; the AIDL has an UnsupportedAppUsage
463 * tag which means this method is technically considered part of the greylist "API".
464 * @param flags
465 */
Adam Lesinski182f73f2013-12-05 16:48:06 -0800466 @Override
Tyler Gunndaef6682019-09-30 15:12:24 -0700467 public void disableCarMode(@UiModeManager.DisableCarMode int flags) {
468 disableCarModeByCallingPackage(flags, null /* callingPackage */);
469 }
470
471 /**
472 * Handles requests to disable car mode.
473 * @param flags Disable car mode flags
474 * @param callingPackage
475 */
476 @Override
477 public void disableCarModeByCallingPackage(@UiModeManager.DisableCarMode int flags,
478 String callingPackage) {
keunyounga7710492015-09-23 11:33:58 -0700479 if (isUiModeLocked()) {
480 Slog.e(TAG, "disableCarMode while UI mode is locked");
481 return;
482 }
Tyler Gunndaef6682019-09-30 15:12:24 -0700483
484 // If the caller is the system, we will allow the DISABLE_CAR_MODE_ALL_PRIORITIES car
485 // mode flag to be specified; this is so that the user can disable car mode at all
486 // priorities using the persistent notification.
487 boolean isSystemCaller = Binder.getCallingUid() == Process.SYSTEM_UID;
488 final int carModeFlags =
489 isSystemCaller ? flags : flags & ~UiModeManager.DISABLE_CAR_MODE_ALL_PRIORITIES;
490
Adam Lesinski182f73f2013-12-05 16:48:06 -0800491 final long ident = Binder.clearCallingIdentity();
492 try {
493 synchronized (mLock) {
Tyler Gunndaef6682019-09-30 15:12:24 -0700494 // Determine if the caller has enabled car mode at a priority other than the
495 // default one. If they have, then attempt to disable at that priority.
496 int priority = mCarModePackagePriority.entrySet()
497 .stream()
498 .filter(e -> e.getValue().equals(callingPackage))
499 .findFirst()
500 .map(Map.Entry::getKey)
501 .orElse(UiModeManager.DEFAULT_PRIORITY);
502
503 setCarModeLocked(false, carModeFlags, priority, callingPackage);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800504 if (mSystemReady) {
505 updateLocked(0, flags);
506 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700507 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800508 } finally {
509 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500510 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700511 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100512
Adam Lesinski182f73f2013-12-05 16:48:06 -0800513 @Override
514 public int getCurrentModeType() {
515 final long ident = Binder.clearCallingIdentity();
516 try {
517 synchronized (mLock) {
518 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700519 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800520 } finally {
521 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800522 }
523 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100524
Adam Lesinski182f73f2013-12-05 16:48:06 -0800525 @Override
526 public void setNightMode(int mode) {
Lucas Dupinc5aaf452018-09-20 16:10:07 -0700527 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
keunyounga7710492015-09-23 11:33:58 -0700528 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
529 != PackageManager.PERMISSION_GRANTED)) {
Lucas Dupinc5aaf452018-09-20 16:10:07 -0700530 Slog.e(TAG, "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
keunyounga7710492015-09-23 11:33:58 -0700531 return;
532 }
Salvador Martinezc500b272019-05-02 14:32:12 -0700533 if (!mSetupWizardComplete) {
534 Slog.d(TAG, "Night mode cannot be changed before setup wizard completes.");
535 return;
536 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800537 switch (mode) {
538 case UiModeManager.MODE_NIGHT_NO:
539 case UiModeManager.MODE_NIGHT_YES:
540 case UiModeManager.MODE_NIGHT_AUTO:
541 break;
542 default:
543 throw new IllegalArgumentException("Unknown mode: " + mode);
544 }
545
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700546 final int user = UserHandle.getCallingUserId();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800547 final long ident = Binder.clearCallingIdentity();
548 try {
549 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800550 if (mNightMode != mode) {
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400551 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000552 unregisterScreenOffEvent();
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400553 }
Winsonadc69672019-01-28 14:56:12 -0800554
Adam Lesinski182f73f2013-12-05 16:48:06 -0800555 mNightMode = mode;
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400556 mNightModeOverride = mode;
Jay Aliomere2815f22019-12-12 14:17:22 -0500557 // Only persist setting if not in car mode
558 if (!mCarModeEnabled) {
559 persistNightMode(user);
560 }
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000561 // on screen off will update configuration instead
JianYang Liub444a972019-12-10 14:32:30 -0800562 if (mNightMode != UiModeManager.MODE_NIGHT_AUTO || mCar) {
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400563 updateLocked(0, 0);
564 } else {
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000565 registerScreenOffEvent();
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400566 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800567 }
568 }
569 } finally {
570 Binder.restoreCallingIdentity(ident);
571 }
572 }
573
574 @Override
575 public int getNightMode() {
576 synchronized (mLock) {
577 return mNightMode;
578 }
579 }
580
581 @Override
keunyounga7710492015-09-23 11:33:58 -0700582 public boolean isUiModeLocked() {
583 synchronized (mLock) {
584 return mUiModeLocked;
585 }
586 }
587
588 @Override
589 public boolean isNightModeLocked() {
590 synchronized (mLock) {
591 return mNightModeLocked;
592 }
593 }
594
595 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500596 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
597 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
598 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
599 }
600
601 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800602 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600603 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800604 dumpImpl(pw);
605 }
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400606
607 @Override
608 public boolean setNightModeActivated(boolean active) {
609 synchronized (mLock) {
Jay Aliomere2815f22019-12-12 14:17:22 -0500610 final int user = UserHandle.getCallingUserId();
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400611 final long ident = Binder.clearCallingIdentity();
612 try {
613 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000614 unregisterScreenOffEvent();
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400615 mNightModeOverride = active
616 ? UiModeManager.MODE_NIGHT_YES : UiModeManager.MODE_NIGHT_NO;
617 } else if (mNightMode == UiModeManager.MODE_NIGHT_NO
618 && active) {
619 mNightMode = UiModeManager.MODE_NIGHT_YES;
620 } else if (mNightMode == UiModeManager.MODE_NIGHT_YES
621 && !active) {
622 mNightMode = UiModeManager.MODE_NIGHT_NO;
623 }
624 updateConfigurationLocked();
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000625 applyConfigurationExternallyLocked();
Jay Aliomere2815f22019-12-12 14:17:22 -0500626 persistNightMode(user);
Jay Aliomer8b2671b2019-10-24 13:18:06 -0400627 return true;
628 } finally {
629 Binder.restoreCallingIdentity(ident);
630 }
631 }
632 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800633 };
634
635 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700636 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800637 pw.println("Current UI Mode Service state:");
638 pw.print(" mDockState="); pw.print(mDockState);
Tyler Gunndaef6682019-09-30 15:12:24 -0700639 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
640
Felipe Lemeff9ec382018-09-24 11:07:56 -0700641 pw.print(" mNightMode="); pw.print(mNightMode); pw.print(" (");
Tyler Gunndaef6682019-09-30 15:12:24 -0700642 pw.print(Shell.nightModeToStr(mNightMode)); pw.print(") ");
643 pw.print(" mNightModeLocked="); pw.println(mNightModeLocked);
644
645 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
646 pw.print(" (carModeApps=");
647 for (Map.Entry<Integer, String> entry : mCarModePackagePriority.entrySet()) {
648 pw.print(entry.getKey());
649 pw.print(":");
650 pw.print(entry.getValue());
651 pw.print(" ");
652 }
653 pw.println("");
654 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
655 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
656 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
657
Adam Lesinski182f73f2013-12-05 16:48:06 -0800658 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
Tyler Gunndaef6682019-09-30 15:12:24 -0700659 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
660 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
661
Adam Lesinski182f73f2013-12-05 16:48:06 -0800662 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
Tyler Gunndaef6682019-09-30 15:12:24 -0700663 pw.print(" mSystemReady="); pw.println(mSystemReady);
664
Adam Lesinski05199e82015-03-19 14:37:11 -0700665 if (mTwilightManager != null) {
666 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700667 pw.print(" mTwilightService.getLastTwilightState()=");
668 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700669 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700670 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800671 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100672
Adam Lesinski182f73f2013-12-05 16:48:06 -0800673 @Override
674 public void onBootPhase(int phase) {
675 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
676 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700677 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800678 mSystemReady = true;
679 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
680 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800681 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800682 updateLocked(0, 0);
683 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800684 }
685 }
686
Tyler Gunndaef6682019-09-30 15:12:24 -0700687 /**
688 * Updates the global car mode state.
689 * The device is considered to be in car mode if there exists an app at any priority level which
690 * has entered car mode.
691 *
692 * @param enabled {@code true} if the caller wishes to enable car mode, {@code false} otherwise.
693 * @param flags Flags used when enabling/disabling car mode.
694 * @param priority The priority level for entering or exiting car mode; defaults to
695 * {@link UiModeManager#DEFAULT_PRIORITY} for callers using
696 * {@link UiModeManager#enableCarMode(int)}. Callers using
697 * {@link UiModeManager#enableCarMode(int, int)} may specify a priority.
698 * @param packageName The package name of the app which initiated the request to enable or
699 * disable car mode.
700 */
701 void setCarModeLocked(boolean enabled, int flags, int priority, String packageName) {
702 if (enabled) {
703 enableCarMode(priority, packageName);
704 } else {
705 disableCarMode(flags, priority, packageName);
706 }
707 boolean isCarModeNowEnabled = isCarModeEnabled();
Winsonadc69672019-01-28 14:56:12 -0800708
Tyler Gunndaef6682019-09-30 15:12:24 -0700709 if (mCarModeEnabled != isCarModeNowEnabled) {
710 mCarModeEnabled = isCarModeNowEnabled;
Winson3f5ad732019-03-04 15:21:04 -0800711 // When exiting car mode, restore night mode from settings
Tyler Gunndaef6682019-09-30 15:12:24 -0700712 if (!isCarModeNowEnabled) {
Winsonadc69672019-01-28 14:56:12 -0800713 Context context = getContext();
714 updateNightModeFromSettings(context,
715 context.getResources(),
716 UserHandle.getCallingUserId());
717 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800718 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700719 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800720 }
721
Tyler Gunndaef6682019-09-30 15:12:24 -0700722 /**
723 * Handles disabling car mode.
724 * <p>
725 * Car mode can be disabled at a priority level if any of the following is true:
726 * 1. The priority being disabled is the {@link UiModeManager#DEFAULT_PRIORITY}.
727 * 2. The priority level is enabled and the caller is the app who originally enabled it.
728 * 3. The {@link UiModeManager#DISABLE_CAR_MODE_ALL_PRIORITIES} flag was specified, meaning all
729 * car mode priorities are disabled.
730 *
731 * @param flags Car mode flags.
732 * @param priority The priority level at which to disable car mode.
733 * @param packageName The calling package which initiated the request.
734 */
735 private void disableCarMode(int flags, int priority, String packageName) {
736 boolean isDisableAll = (flags & UiModeManager.DISABLE_CAR_MODE_ALL_PRIORITIES) != 0;
737 boolean isPriorityTracked = mCarModePackagePriority.keySet().contains(priority);
738 boolean isDefaultPriority = priority == UiModeManager.DEFAULT_PRIORITY;
739 boolean isChangeAllowed =
740 // Anyone can disable the default priority.
741 isDefaultPriority
742 // If priority was enabled, only enabling package can disable it.
743 || isPriorityTracked && mCarModePackagePriority.get(priority).equals(packageName)
744 // Disable all priorities flag can disable all regardless.
745 || isDisableAll;
746 if (isChangeAllowed) {
747 Slog.d(TAG, "disableCarMode: disabling, priority=" + priority
748 + ", packageName=" + packageName);
749 if (isDisableAll) {
750 Set<Map.Entry<Integer, String>> entries =
751 new ArraySet<>(mCarModePackagePriority.entrySet());
752 mCarModePackagePriority.clear();
753
754 for (Map.Entry<Integer, String> entry : entries) {
755 notifyCarModeDisabled(entry.getKey(), entry.getValue());
756 }
757 } else {
758 mCarModePackagePriority.remove(priority);
759 notifyCarModeDisabled(priority, packageName);
760 }
761 }
762 }
763
764 /**
765 * Handles enabling car mode.
766 * <p>
767 * Car mode can be enabled at any priority if it has not already been enabled at that priority.
768 * The calling package is tracked for the first app which enters priority at the
769 * {@link UiModeManager#DEFAULT_PRIORITY}, though any app can disable it at that priority.
770 *
771 * @param priority The priority for enabling car mode.
772 * @param packageName The calling package which initiated the request.
773 */
774 private void enableCarMode(int priority, String packageName) {
775 boolean isPriorityTracked = mCarModePackagePriority.containsKey(priority);
776 boolean isPackagePresent = mCarModePackagePriority.containsValue(packageName);
777 if (!isPriorityTracked && !isPackagePresent) {
778 Slog.d(TAG, "enableCarMode: enabled at priority=" + priority + ", packageName="
779 + packageName);
780 mCarModePackagePriority.put(priority, packageName);
781 notifyCarModeEnabled(priority, packageName);
782 } else {
783 Slog.d(TAG, "enableCarMode: car mode at priority " + priority + " already enabled.");
784 }
785
786 }
787
788 private void notifyCarModeEnabled(int priority, String packageName) {
789 Intent intent = new Intent(UiModeManager.ACTION_ENTER_CAR_MODE_PRIORITIZED);
790 intent.putExtra(UiModeManager.EXTRA_CALLING_PACKAGE, packageName);
791 intent.putExtra(UiModeManager.EXTRA_PRIORITY, priority);
792 getContext().sendBroadcastAsUser(intent, UserHandle.ALL,
793 android.Manifest.permission.HANDLE_CAR_MODE_CHANGES);
794 }
795
796 private void notifyCarModeDisabled(int priority, String packageName) {
797 Intent intent = new Intent(UiModeManager.ACTION_EXIT_CAR_MODE_PRIORITIZED);
798 intent.putExtra(UiModeManager.EXTRA_CALLING_PACKAGE, packageName);
799 intent.putExtra(UiModeManager.EXTRA_PRIORITY, priority);
800 getContext().sendBroadcastAsUser(intent, UserHandle.ALL,
801 android.Manifest.permission.HANDLE_CAR_MODE_CHANGES);
802 }
803
804 /**
805 * Determines if car mode is enabled at any priority level.
806 * @return {@code true} if car mode is enabled, {@code false} otherwise.
807 */
808 private boolean isCarModeEnabled() {
809 return mCarModePackagePriority.size() > 0;
810 }
811
Jeff Brown487bb6e2012-10-11 13:35:42 -0700812 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800813 synchronized (mLock) {
814 if (newState != mDockState) {
815 mDockState = newState;
Tyler Gunndaef6682019-09-30 15:12:24 -0700816 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0,
817 UiModeManager.DEFAULT_PRIORITY, "" /* packageName */);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800818 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700819 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800820 }
821 }
822 }
823 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500824
Jeff Brown487bb6e2012-10-11 13:35:42 -0700825 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400826 switch (state) {
827 case Intent.EXTRA_DOCK_STATE_DESK:
828 case Intent.EXTRA_DOCK_STATE_LE_DESK:
829 case Intent.EXTRA_DOCK_STATE_HE_DESK:
830 return true;
831 default:
832 return false;
833 }
834 }
835
Jay Aliomere2815f22019-12-12 14:17:22 -0500836 private void persistNightMode(int user) {
837 Secure.putIntForUser(getContext().getContentResolver(),
838 Secure.UI_NIGHT_MODE, mNightMode, user);
839 Secure.putIntForUser(getContext().getContentResolver(),
840 OVERRIDE_NIGHT_MODE, mNightModeOverride, user);
841 }
842
Jeff Brown487bb6e2012-10-11 13:35:42 -0700843 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400844 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700845 if (mUiModeLocked) {
846 // no-op, keeps default one
847 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400848 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
849 } else if (mWatch) {
850 uiMode = Configuration.UI_MODE_TYPE_WATCH;
851 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800852 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400853 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800854 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800855 } else if (mVrHeadset) {
856 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800857 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800858
859 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700860 if (mTwilightManager != null) {
861 mTwilightManager.registerListener(mTwilightListener, mHandler);
862 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800863 updateComputedNightModeLocked();
864 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
865 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800866 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700867 if (mTwilightManager != null) {
868 mTwilightManager.unregisterListener(mTwilightListener);
869 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800870 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400871 }
872
Winson3f5ad732019-03-04 15:21:04 -0800873 // Override night mode in power save mode if not in car mode
874 if (mPowerSave && !mCarModeEnabled) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700875 uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
876 uiMode |= Configuration.UI_MODE_NIGHT_YES;
877 }
878
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400879 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400880 Slog.d(TAG,
881 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400882 + "; mCarMode=" + mCarModeEnabled
883 + "; mNightMode=" + mNightMode
884 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800885 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100886
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800887 mCurUiMode = uiMode;
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000888 if (!mHoldingConfiguration || !mWaitForScreenOff) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700889 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700890 }
891 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100892
Jeff Sharkey5f116e12019-12-07 16:15:29 +0000893 private void applyConfigurationExternallyLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700894 if (mSetUiMode != mConfiguration.uiMode) {
895 mSetUiMode = mConfiguration.uiMode;
Jay Aliomerb45934a2019-12-09 14:45:09 -0500896 // load splash screen instead of screenshot
897 mWindowManager.clearSnapshotCache();
Jeff Brown62c82e42012-09-26 01:30:41 -0700898 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700899 ActivityTaskManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700900 } catch (RemoteException e) {
901 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800902 }
903 }
904 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100905
Adam Lesinski182f73f2013-12-05 16:48:06 -0800906 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700907 String action = null;
908 String oldAction = null;
909 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
910 adjustStatusBarCarModeLocked();
911 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
912 } else if (isDeskDockState(mLastBroadcastState)) {
913 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
914 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100915
Jeff Brown487bb6e2012-10-11 13:35:42 -0700916 if (mCarModeEnabled) {
917 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100918 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700919 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700920 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700921 }
922 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
923 action = UiModeManager.ACTION_ENTER_CAR_MODE;
924 }
925 } else if (isDeskDockState(mDockState)) {
926 if (!isDeskDockState(mLastBroadcastState)) {
927 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700928 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700929 }
930 mLastBroadcastState = mDockState;
931 action = UiModeManager.ACTION_ENTER_DESK_MODE;
932 }
933 } else {
934 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
935 action = oldAction;
936 }
937
938 if (action != null) {
939 if (LOG) {
940 Slog.v(TAG, String.format(
941 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
942 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800943 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100944
Jeff Brown487bb6e2012-10-11 13:35:42 -0700945 // Send the ordered broadcast; the result receiver will receive after all
946 // broadcasts have been sent. If any broadcast receiver changes the result
947 // code from the initial value of RESULT_OK, then the result receiver will
948 // not launch the corresponding dock application. This gives apps a chance
949 // to override the behavior and stay in their app even when the device is
950 // placed into a dock.
951 Intent intent = new Intent(action);
952 intent.putExtra("enableFlags", enableFlags);
953 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700954 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800955 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700956 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100957
Jeff Brown487bb6e2012-10-11 13:35:42 -0700958 // Attempting to make this transition a little more clean, we are going
959 // to hold off on doing a configuration change until we have finished
960 // the broadcast and started the home activity.
961 mHoldingConfiguration = true;
962 updateConfigurationLocked();
963 } else {
964 String category = null;
965 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700966 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700967 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
968 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800969 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400970 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700971 if (ENABLE_LAUNCH_DESK_DOCK_APP
972 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
973 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800974 }
975 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700976 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
977 category = Intent.CATEGORY_HOME;
978 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800979 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100980
Jeff Brown487bb6e2012-10-11 13:35:42 -0700981 if (LOG) {
982 Slog.v(TAG, "updateLocked: null action, mDockState="
983 + mDockState +", category=" + category);
984 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400985
Jeff Brown487bb6e2012-10-11 13:35:42 -0700986 sendConfigurationAndStartDreamOrDockAppLocked(category);
987 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700988
Jeff Brown487bb6e2012-10-11 13:35:42 -0700989 // keep screen on when charging and in car mode
990 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700991 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700992 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700993 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
994 if (keepScreenOn != mWakeLock.isHeld()) {
995 if (keepScreenOn) {
996 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700997 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700998 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800999 }
Dianne Hackborn7299c412010-03-04 18:41:49 -08001000 }
1001 }
1002
Adrian Roos2fbf4d52016-10-20 14:40:51 -07001003 private void sendForegroundBroadcastToAllUsers(String action) {
1004 getContext().sendBroadcastAsUser(new Intent(action)
1005 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
1006 }
1007
Jeff Brown62c82e42012-09-26 01:30:41 -07001008 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
1009 // Launch a dock activity
1010 String category = null;
1011 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
1012 // Only launch car home when car mode is enabled and the caller
1013 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -07001014 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -07001015 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
1016 category = Intent.CATEGORY_CAR_DOCK;
1017 }
1018 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
1019 // Only launch car home when desk mode is enabled and the caller
1020 // has asked us to switch to it. Currently re-using the car
1021 // mode flag since we don't have a formal API for "desk mode".
1022 if (ENABLE_LAUNCH_DESK_DOCK_APP
1023 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
1024 category = Intent.CATEGORY_DESK_DOCK;
1025 }
1026 } else {
1027 // Launch the standard home app if requested.
1028 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
1029 category = Intent.CATEGORY_HOME;
1030 }
1031 }
1032
1033 if (LOG) {
1034 Slog.v(TAG, String.format(
1035 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
1036 + "category=%s",
1037 action, enableFlags, disableFlags, category));
1038 }
1039
1040 sendConfigurationAndStartDreamOrDockAppLocked(category);
1041 }
1042
1043 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
1044 // Update the configuration but don't send it yet.
1045 mHoldingConfiguration = false;
1046 updateConfigurationLocked();
1047
1048 // Start the dock app, if there is one.
1049 boolean dockAppStarted = false;
1050 if (category != null) {
1051 // Now we are going to be careful about switching the
1052 // configuration and starting the activity -- we need to
1053 // do this in a specific order under control of the
1054 // activity manager, to do it cleanly. So compute the
1055 // new config, but don't set it yet, and let the
1056 // activity manager take care of both the start and config
1057 // change.
1058 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -08001059 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -07001060 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001061 int result = ActivityTaskManager.getService().startActivityWithConfig(
Andrii Kulian9e0a7512019-07-26 17:25:39 -07001062 null, getContext().getBasePackageName(), homeIntent, null, null, null,
1063 0, 0, mConfiguration, null, UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -07001064 if (ActivityManager.isStartResultSuccessful(result)) {
Jeff Brown11159e92012-10-11 15:58:37 -07001065 dockAppStarted = true;
1066 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
1067 Slog.e(TAG, "Could not start dock app: " + homeIntent
1068 + ", startActivityWithConfig result " + result);
1069 }
1070 } catch (RemoteException ex) {
1071 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -07001072 }
Jeff Brown62c82e42012-09-26 01:30:41 -07001073 }
1074 }
1075
1076 // Send the new configuration.
Jeff Sharkey5f116e12019-12-07 16:15:29 +00001077 applyConfigurationExternallyLocked();
Jeff Brown62c82e42012-09-26 01:30:41 -07001078
1079 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -07001080 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -08001081 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -07001082 }
1083 }
1084
Dianne Hackborn7299c412010-03-04 18:41:49 -08001085 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -08001086 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -08001087 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -07001088 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -08001089 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -08001090 }
1091
Joe Onorato089de882010-04-12 08:18:45 -07001092 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -08001093 // features of the status bar; these are ORed together to form the
1094 // active disabled list. So if (for example) the device is locked and
1095 // the status bar should be totally disabled, the calls below will
1096 // have no effect until the device is unlocked.
1097 if (mStatusBarManager != null) {
1098 mStatusBarManager.disable(mCarModeEnabled
1099 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
1100 : StatusBarManager.DISABLE_NONE);
1101 }
1102
1103 if (mNotificationManager == null) {
1104 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -08001105 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -08001106 }
1107
1108 if (mNotificationManager != null) {
1109 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -08001110 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -08001111
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001112 Notification.Builder n =
1113 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001114 .setSmallIcon(R.drawable.stat_notify_car_mode)
1115 .setDefaults(Notification.DEFAULT_LIGHTS)
1116 .setOngoing(true)
1117 .setWhen(0)
1118 .setColor(context.getColor(
1119 com.android.internal.R.color.system_notification_accent_color))
1120 .setContentTitle(
1121 context.getString(R.string.car_mode_disable_notification_title))
1122 .setContentText(
1123 context.getString(R.string.car_mode_disable_notification_message))
1124 .setContentIntent(
1125 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
1126 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -07001127 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04001128 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -08001129 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -07001130 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04001131 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -08001132 }
1133 }
1134 }
1135
Jeff Brown2416e092012-08-21 22:12:20 -07001136 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -07001137 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -07001138 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -07001139 if (state != null) {
1140 mComputedNightMode = state.isNight();
1141 }
Jay Aliomer8b2671b2019-10-24 13:18:06 -04001142 if (mNightModeOverride == UiModeManager.MODE_NIGHT_YES && !mComputedNightMode) {
1143 mComputedNightMode = true;
1144 return;
1145 }
1146 if (mNightModeOverride == UiModeManager.MODE_NIGHT_NO && mComputedNightMode) {
1147 mComputedNightMode = false;
1148 return;
1149 }
1150
1151 mNightModeOverride = mNightMode;
1152 final int user = UserHandle.getCallingUserId();
1153 Secure.putIntForUser(getContext().getContentResolver(),
1154 OVERRIDE_NIGHT_MODE, mNightModeOverride, user);
Dianne Hackborn7299c412010-03-04 18:41:49 -08001155 }
Dianne Hackborn7299c412010-03-04 18:41:49 -08001156 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +01001157
Zak Cohen1a705732017-01-09 12:54:34 -08001158 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -08001159 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
1160 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -08001161 try {
1162 if (vrManager != null) {
1163 vrManager.registerListener(mVrStateCallbacks);
1164 }
1165 } catch (RemoteException e) {
1166 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
1167 }
1168 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +01001169
Alan Viveretteba0d98f2017-01-30 10:36:54 -05001170 /**
1171 * Handles "adb shell" commands.
1172 */
1173 private static class Shell extends ShellCommand {
1174 public static final String NIGHT_MODE_STR_YES = "yes";
1175 public static final String NIGHT_MODE_STR_NO = "no";
1176 public static final String NIGHT_MODE_STR_AUTO = "auto";
1177 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
1178 private final IUiModeManager mInterface;
1179
1180 Shell(IUiModeManager iface) {
1181 mInterface = iface;
1182 }
1183
1184 @Override
1185 public void onHelp() {
1186 final PrintWriter pw = getOutPrintWriter();
1187 pw.println("UiModeManager service (uimode) commands:");
1188 pw.println(" help");
1189 pw.println(" Print this help text.");
1190 pw.println(" night [yes|no|auto]");
1191 pw.println(" Set or read night mode.");
1192 }
1193
1194 @Override
1195 public int onCommand(String cmd) {
1196 if (cmd == null) {
1197 return handleDefaultCommands(cmd);
1198 }
1199
1200 try {
1201 switch (cmd) {
1202 case "night":
1203 return handleNightMode();
1204 default:
1205 return handleDefaultCommands(cmd);
1206 }
1207 } catch (RemoteException e) {
1208 final PrintWriter err = getErrPrintWriter();
1209 err.println("Remote exception: " + e);
1210 }
1211 return -1;
1212 }
1213
1214 private int handleNightMode() throws RemoteException {
1215 final PrintWriter err = getErrPrintWriter();
1216 final String modeStr = getNextArg();
1217 if (modeStr == null) {
1218 printCurrentNightMode();
1219 return 0;
1220 }
1221
1222 final int mode = strToNightMode(modeStr);
1223 if (mode >= 0) {
1224 mInterface.setNightMode(mode);
1225 printCurrentNightMode();
1226 return 0;
1227 } else {
1228 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
1229 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
1230 return -1;
1231 }
1232 }
1233
1234 private void printCurrentNightMode() throws RemoteException {
1235 final PrintWriter pw = getOutPrintWriter();
1236 final int currMode = mInterface.getNightMode();
1237 final String currModeStr = nightModeToStr(currMode);
1238 pw.println("Night mode: " + currModeStr);
1239 }
1240
1241 private static String nightModeToStr(int mode) {
1242 switch (mode) {
1243 case UiModeManager.MODE_NIGHT_YES:
1244 return NIGHT_MODE_STR_YES;
1245 case UiModeManager.MODE_NIGHT_NO:
1246 return NIGHT_MODE_STR_NO;
1247 case UiModeManager.MODE_NIGHT_AUTO:
1248 return NIGHT_MODE_STR_AUTO;
1249 default:
1250 return NIGHT_MODE_STR_UNKNOWN;
1251 }
1252 }
1253
1254 private static int strToNightMode(String modeStr) {
1255 switch (modeStr) {
1256 case NIGHT_MODE_STR_YES:
1257 return UiModeManager.MODE_NIGHT_YES;
1258 case NIGHT_MODE_STR_NO:
1259 return UiModeManager.MODE_NIGHT_NO;
1260 case NIGHT_MODE_STR_AUTO:
1261 return UiModeManager.MODE_NIGHT_AUTO;
1262 default:
1263 return -1;
1264 }
1265 }
1266 }
Felipe Lemeff9ec382018-09-24 11:07:56 -07001267
1268 public final class LocalService extends UiModeManagerInternal {
1269
1270 @Override
1271 public boolean isNightMode() {
1272 synchronized (mLock) {
1273 final boolean isIt = (mConfiguration.uiMode & Configuration.UI_MODE_NIGHT_YES) != 0;
1274 if (LOG) {
1275 Slog.d(TAG,
1276 "LocalService.isNightMode(): mNightMode=" + mNightMode
1277 + "; mComputedNightMode=" + mComputedNightMode
1278 + "; uiMode=" + mConfiguration.uiMode
1279 + "; isIt=" + isIt);
1280 }
1281 return isIt;
1282 }
1283 }
1284 }
Salvador Martinez2f792ef2018-09-26 17:33:51 -07001285
1286 private final class UserSwitchedReceiver extends BroadcastReceiver {
1287 @Override
1288 public void onReceive(Context context, Intent intent) {
1289 synchronized (mLock) {
1290 final int currentId = intent.getIntExtra(
1291 Intent.EXTRA_USER_HANDLE, UserHandle.USER_SYSTEM);
1292 // only update if the value is actually changed
1293 if (updateNightModeFromSettings(context, context.getResources(), currentId)) {
1294 updateLocked(0, 0);
1295 }
1296 }
1297 }
1298 }
Dianne Hackborn7299c412010-03-04 18:41:49 -08001299}