blob: 7e63ba6efe77bd9d3f92e21799acd600c0596caa [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
Justin Klaassen908b86c2016-08-08 09:18:42 -070019import android.annotation.Nullable;
Dianne Hackborn7299c412010-03-04 18:41:49 -080020import android.app.Activity;
Jeff Brown62c82e42012-09-26 01:30:41 -070021import android.app.ActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070022import android.app.ActivityTaskManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080023import android.app.IUiModeManager;
24import android.app.Notification;
25import android.app.NotificationManager;
26import android.app.PendingIntent;
27import android.app.StatusBarManager;
28import android.app.UiModeManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080029import android.content.BroadcastReceiver;
30import android.content.Context;
31import android.content.Intent;
32import android.content.IntentFilter;
Winsonadc69672019-01-28 14:56:12 -080033import android.content.pm.ApplicationInfo;
Dianne Hackborn7299c412010-03-04 18:41:49 -080034import android.content.pm.PackageManager;
35import android.content.res.Configuration;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080036import android.content.res.Resources;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050037import android.os.BatteryManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080038import android.os.Binder;
Winsonadc69672019-01-28 14:56:12 -080039import android.os.Build;
Dianne Hackborn7299c412010-03-04 18:41:49 -080040import android.os.Handler;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050041import android.os.PowerManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080042import android.os.RemoteException;
Alan Viveretteba0d98f2017-01-30 10:36:54 -050043import android.os.ResultReceiver;
Zak Cohen1a705732017-01-09 12:54:34 -080044import android.os.ServiceManager;
Alan Viveretteba0d98f2017-01-30 10:36:54 -050045import android.os.ShellCallback;
46import android.os.ShellCommand;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070047import android.os.UserHandle;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070048import android.provider.Settings;
Jeff Brown11159e92012-10-11 15:58:37 -070049import android.service.dreams.Sandman;
Zak Cohen1a705732017-01-09 12:54:34 -080050import android.service.vr.IVrManager;
51import android.service.vr.IVrStateCallbacks;
Dianne Hackborn7299c412010-03-04 18:41:49 -080052import android.util.Slog;
53
Dianne Hackborn7299c412010-03-04 18:41:49 -080054import com.android.internal.R;
55import com.android.internal.app.DisableCarModeActivity;
Chris Wren282cfef2017-03-27 15:01:44 -040056import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050057import com.android.internal.notification.SystemNotificationChannels;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060058import com.android.internal.util.DumpUtils;
Adam Lesinski182f73f2013-12-05 16:48:06 -080059import com.android.server.twilight.TwilightListener;
60import com.android.server.twilight.TwilightManager;
61import com.android.server.twilight.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080062
Lucas Dupinb1f0c762018-09-18 10:33:38 -070063import java.io.FileDescriptor;
64import java.io.PrintWriter;
65
Adam Lesinski182f73f2013-12-05 16:48:06 -080066final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080067 private static final String TAG = UiModeManager.class.getSimpleName();
68 private static final boolean LOG = false;
69
Daniel Sandler11ddf532011-11-16 11:10:03 -080070 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040071 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080072
Dianne Hackborn7299c412010-03-04 18:41:49 -080073 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080074 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010075
Adam Lesinski182f73f2013-12-05 16:48:06 -080076 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080077 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080078
Dianne Hackborn7299c412010-03-04 18:41:49 -080079 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050080 private boolean mCharging = false;
Lucas Dupin53c6e292018-07-12 18:42:53 -070081 private boolean mPowerSave = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080082 private int mDefaultUiModeType;
83 private boolean mCarModeKeepsScreenOn;
84 private boolean mDeskModeKeepsScreenOn;
85 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040086 private boolean mWatch;
Zak Cohen1a705732017-01-09 12:54:34 -080087 private boolean mVrHeadset;
Dianne Hackborn7299c412010-03-04 18:41:49 -080088 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070089 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080090
keunyounga7710492015-09-23 11:33:58 -070091 // flag set by resource, whether to enable Car dock launch when starting car mode.
92 private boolean mEnableCarDockLaunch = true;
93 // flag set by resource, whether to lock UI mode to the default one or not.
94 private boolean mUiModeLocked = false;
95 // flag set by resource, whether to night mode change for normal all or not.
96 private boolean mNightModeLocked = false;
97
Adam Lesinski182f73f2013-12-05 16:48:06 -080098 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080099 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800100 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800101
Dianne Hackborn7299c412010-03-04 18:41:49 -0800102 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800103 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100104
Adam Lesinski182f73f2013-12-05 16:48:06 -0800105 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800106
Adam Lesinski182f73f2013-12-05 16:48:06 -0800107 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800108 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800109 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700110
Adam Lesinski182f73f2013-12-05 16:48:06 -0800111 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800112
Felipe Lemeff9ec382018-09-24 11:07:56 -0700113 private final LocalService mLocalService = new LocalService();
114
Jeff Brownb880d882014-02-10 19:47:07 -0800115 public UiModeManagerService(Context context) {
116 super(context);
117 }
118
Adam Lesinski182f73f2013-12-05 16:48:06 -0800119 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700120 Intent intent = new Intent(Intent.ACTION_MAIN);
121 intent.addCategory(category);
122 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
123 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
124 return intent;
125 }
John Spurlock960779d2012-05-29 14:37:05 -0400126
Dianne Hackborn7299c412010-03-04 18:41:49 -0800127 // The broadcast receiver which receives the result of the ordered broadcast sent when
128 // the dock state changes. The original ordered broadcast is sent with an initial result
129 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
130 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
131 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
132 @Override
133 public void onReceive(Context context, Intent intent) {
134 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400135 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400136 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400137 + ": canceled: " + getResultCode());
138 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800139 return;
140 }
141
Jeff Brown62c82e42012-09-26 01:30:41 -0700142 final int enableFlags = intent.getIntExtra("enableFlags", 0);
143 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800144 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700145 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800146 }
147 }
148 };
149
Dianne Hackborn7299c412010-03-04 18:41:49 -0800150 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
151 @Override
152 public void onReceive(Context context, Intent intent) {
153 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
154 Intent.EXTRA_DOCK_STATE_UNDOCKED);
155 updateDockState(state);
156 }
157 };
158
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500159 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
160 @Override
161 public void onReceive(Context context, Intent intent) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700162 switch (intent.getAction()) {
163 case Intent.ACTION_BATTERY_CHANGED:
164 mCharging = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
165 break;
166 case PowerManager.ACTION_POWER_SAVE_MODE_CHANGING:
167 mPowerSave = intent.getBooleanExtra(PowerManager.EXTRA_POWER_SAVE_MODE, false);
168 break;
169 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500170 synchronized (mLock) {
171 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700172 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500173 }
174 }
175 }
176 };
177
Adam Lesinski182f73f2013-12-05 16:48:06 -0800178 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700179 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700180 public void onTwilightStateChanged(@Nullable TwilightState state) {
181 synchronized (mLock) {
182 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
183 updateComputedNightModeLocked();
184 updateLocked(0, 0);
185 }
186 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700187 }
188 };
189
Zak Cohen1a705732017-01-09 12:54:34 -0800190 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
191 @Override
192 public void onVrStateChanged(boolean enabled) {
193 synchronized (mLock) {
194 mVrHeadset = enabled;
195 if (mSystemReady) {
196 updateLocked(0, 0);
197 }
198 }
199 }
200 };
201
Adam Lesinski182f73f2013-12-05 16:48:06 -0800202 @Override
203 public void onStart() {
204 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800205
Adam Lesinski182f73f2013-12-05 16:48:06 -0800206 final PowerManager powerManager =
207 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
208 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800209
Adam Lesinski182f73f2013-12-05 16:48:06 -0800210 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800211 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Lucas Dupin53c6e292018-07-12 18:42:53 -0700212 IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
213 batteryFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING);
214 context.registerReceiver(mBatteryReceiver, batteryFilter);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500215
Dianne Hackborn7299c412010-03-04 18:41:49 -0800216 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500217
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800218 final Resources res = context.getResources();
219 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800220 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800221 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500222 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800223 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500224 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700225 mEnableCarDockLaunch = res.getBoolean(
226 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
227 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
228 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700229
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800230 final PackageManager pm = context.getPackageManager();
231 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
232 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
233 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
234
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700235 updateNightModeFromSettings(context, res, UserHandle.getCallingUserId());
Jeff Brown2416e092012-08-21 22:12:20 -0700236
Adam Lesinski05199e82015-03-19 14:37:11 -0700237 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800238 SystemServerInitThreadPool.get().submit(() -> {
239 synchronized (mLock) {
240 updateConfigurationLocked();
241 sendConfigurationLocked();
242 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800243
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800244 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800245 publishBinderService(Context.UI_MODE_SERVICE, mService);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700246 publishLocalService(UiModeManagerInternal.class, mLocalService);
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700247
248 IntentFilter filter = new IntentFilter();
249 filter.addAction(Intent.ACTION_USER_SWITCHED);
250 context.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
251 }
252
253 /**
254 * Updates the night mode setting in Settings.Global and returns if the value was successfully
255 * changed.
256 * @param context A valid context
257 * @param res A valid resource object
258 * @param userId The user to update the setting for
259 * @return True if the new value is different from the old value. False otherwise.
260 */
261 private boolean updateNightModeFromSettings(Context context, Resources res, int userId) {
262 final int defaultNightMode = res.getInteger(
263 com.android.internal.R.integer.config_defaultNightMode);
264 int oldNightMode = mNightMode;
265 mNightMode = Settings.Secure.getIntForUser(context.getContentResolver(),
266 Settings.Secure.UI_NIGHT_MODE, defaultNightMode, userId);
267
268 // false if night mode stayed the same, true otherwise.
269 return !(oldNightMode == mNightMode);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800270 }
271
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500272 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800273 @Override
274 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700275 if (isUiModeLocked()) {
276 Slog.e(TAG, "enableCarMode while UI mode is locked");
277 return;
278 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800279 final long ident = Binder.clearCallingIdentity();
280 try {
281 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700282 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800283 if (mSystemReady) {
284 updateLocked(flags, 0);
285 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700286 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800287 } finally {
288 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500289 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800290 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800291
Adam Lesinski182f73f2013-12-05 16:48:06 -0800292 @Override
293 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700294 if (isUiModeLocked()) {
295 Slog.e(TAG, "disableCarMode while UI mode is locked");
296 return;
297 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800298 final long ident = Binder.clearCallingIdentity();
299 try {
300 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700301 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800302 if (mSystemReady) {
303 updateLocked(0, flags);
304 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700305 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800306 } finally {
307 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500308 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700309 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100310
Adam Lesinski182f73f2013-12-05 16:48:06 -0800311 @Override
312 public int getCurrentModeType() {
313 final long ident = Binder.clearCallingIdentity();
314 try {
315 synchronized (mLock) {
316 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700317 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800318 } finally {
319 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800320 }
321 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100322
Adam Lesinski182f73f2013-12-05 16:48:06 -0800323 @Override
324 public void setNightMode(int mode) {
Lucas Dupinc5aaf452018-09-20 16:10:07 -0700325 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
keunyounga7710492015-09-23 11:33:58 -0700326 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
327 != PackageManager.PERMISSION_GRANTED)) {
Lucas Dupinc5aaf452018-09-20 16:10:07 -0700328 Slog.e(TAG, "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
keunyounga7710492015-09-23 11:33:58 -0700329 return;
330 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800331 switch (mode) {
332 case UiModeManager.MODE_NIGHT_NO:
333 case UiModeManager.MODE_NIGHT_YES:
334 case UiModeManager.MODE_NIGHT_AUTO:
335 break;
336 default:
337 throw new IllegalArgumentException("Unknown mode: " + mode);
338 }
339
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700340 final int user = UserHandle.getCallingUserId();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800341 final long ident = Binder.clearCallingIdentity();
342 try {
343 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800344 if (mNightMode != mode) {
Winsonadc69672019-01-28 14:56:12 -0800345 // Only persist setting if not transient night mode or not in car mode
346 if (!shouldTransientNightWhenInCarMode() || !mCarModeEnabled) {
347 Settings.Secure.putIntForUser(getContext().getContentResolver(),
348 Settings.Secure.UI_NIGHT_MODE, mode, user);
349 }
350
Adam Lesinski182f73f2013-12-05 16:48:06 -0800351 mNightMode = mode;
352 updateLocked(0, 0);
353 }
354 }
355 } finally {
356 Binder.restoreCallingIdentity(ident);
357 }
358 }
359
360 @Override
361 public int getNightMode() {
362 synchronized (mLock) {
363 return mNightMode;
364 }
365 }
366
367 @Override
keunyounga7710492015-09-23 11:33:58 -0700368 public boolean isUiModeLocked() {
369 synchronized (mLock) {
370 return mUiModeLocked;
371 }
372 }
373
374 @Override
375 public boolean isNightModeLocked() {
376 synchronized (mLock) {
377 return mNightModeLocked;
378 }
379 }
380
381 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500382 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
383 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
384 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
385 }
386
387 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800388 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600389 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800390 dumpImpl(pw);
391 }
392 };
393
394 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700395 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800396 pw.println("Current UI Mode Service state:");
397 pw.print(" mDockState="); pw.print(mDockState);
398 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700399 pw.print(" mNightMode="); pw.print(mNightMode); pw.print(" (");
400 pw.print(Shell.nightModeToStr(mNightMode)); pw.print(") ");
keunyounga7710492015-09-23 11:33:58 -0700401 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800402 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700403 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700404 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
405 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800406 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700407 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800408 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
409 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
410 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700411 if (mTwilightManager != null) {
412 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700413 pw.print(" mTwilightService.getLastTwilightState()=");
414 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700415 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700416 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800417 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100418
Adam Lesinski182f73f2013-12-05 16:48:06 -0800419 @Override
420 public void onBootPhase(int phase) {
421 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
422 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700423 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800424 mSystemReady = true;
425 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
426 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800427 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800428 updateLocked(0, 0);
429 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800430 }
431 }
432
Winsonadc69672019-01-28 14:56:12 -0800433 // Night mode settings in car mode are only persisted below Q.
434 // When targeting Q, changes are not saved and night mode will be re-read
435 // from settings when exiting car mode.
436 private boolean shouldTransientNightWhenInCarMode() {
437 int uid = Binder.getCallingUid();
438 PackageManager packageManager = getContext().getPackageManager();
439 String[] packagesForUid = packageManager.getPackagesForUid(uid);
440 if (packagesForUid == null || packagesForUid.length == 0) {
441 return false;
442 }
443
444 try {
445 ApplicationInfo appInfo = packageManager.getApplicationInfoAsUser(
446 packagesForUid[0], 0, uid);
447
448 return appInfo.targetSdkVersion >= Build.VERSION_CODES.Q;
449 } catch (PackageManager.NameNotFoundException ignored) {
450 }
451
452 return false;
453 }
454
keunyoung1d0a7cc2014-07-28 13:12:50 -0700455 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800456 if (mCarModeEnabled != enabled) {
457 mCarModeEnabled = enabled;
Winsonadc69672019-01-28 14:56:12 -0800458
459 // When transient night mode and exiting car mode, restore night mode from settings
460 if (shouldTransientNightWhenInCarMode() && !mCarModeEnabled) {
461 Context context = getContext();
462 updateNightModeFromSettings(context,
463 context.getResources(),
464 UserHandle.getCallingUserId());
465 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800466 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700467 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800468 }
469
Jeff Brown487bb6e2012-10-11 13:35:42 -0700470 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800471 synchronized (mLock) {
472 if (newState != mDockState) {
473 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700474 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800475 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700476 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800477 }
478 }
479 }
480 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500481
Jeff Brown487bb6e2012-10-11 13:35:42 -0700482 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400483 switch (state) {
484 case Intent.EXTRA_DOCK_STATE_DESK:
485 case Intent.EXTRA_DOCK_STATE_LE_DESK:
486 case Intent.EXTRA_DOCK_STATE_HE_DESK:
487 return true;
488 default:
489 return false;
490 }
491 }
492
Jeff Brown487bb6e2012-10-11 13:35:42 -0700493 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400494 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700495 if (mUiModeLocked) {
496 // no-op, keeps default one
497 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400498 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
499 } else if (mWatch) {
500 uiMode = Configuration.UI_MODE_TYPE_WATCH;
501 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800502 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400503 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800504 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800505 } else if (mVrHeadset) {
506 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800507 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800508
509 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700510 if (mTwilightManager != null) {
511 mTwilightManager.registerListener(mTwilightListener, mHandler);
512 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800513 updateComputedNightModeLocked();
514 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
515 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800516 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700517 if (mTwilightManager != null) {
518 mTwilightManager.unregisterListener(mTwilightListener);
519 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800520 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400521 }
522
Winsonadc69672019-01-28 14:56:12 -0800523 // Override night mode in power save mode if not transient night mode or not in car mode
524 boolean shouldOverrideNight = !mCarModeEnabled || !shouldTransientNightWhenInCarMode();
525 if (mPowerSave && shouldOverrideNight) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700526 uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
527 uiMode |= Configuration.UI_MODE_NIGHT_YES;
528 }
529
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400530 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400531 Slog.d(TAG,
532 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400533 + "; mCarMode=" + mCarModeEnabled
534 + "; mNightMode=" + mNightMode
535 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800536 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100537
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800538 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700539 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700540 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700541 }
542 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100543
Jeff Brown487bb6e2012-10-11 13:35:42 -0700544 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700545 if (mSetUiMode != mConfiguration.uiMode) {
546 mSetUiMode = mConfiguration.uiMode;
547
548 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700549 ActivityTaskManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700550 } catch (RemoteException e) {
551 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800552 }
553 }
554 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100555
Adam Lesinski182f73f2013-12-05 16:48:06 -0800556 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700557 String action = null;
558 String oldAction = null;
559 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
560 adjustStatusBarCarModeLocked();
561 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
562 } else if (isDeskDockState(mLastBroadcastState)) {
563 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
564 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100565
Jeff Brown487bb6e2012-10-11 13:35:42 -0700566 if (mCarModeEnabled) {
567 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100568 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700569 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700570 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700571 }
572 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
573 action = UiModeManager.ACTION_ENTER_CAR_MODE;
574 }
575 } else if (isDeskDockState(mDockState)) {
576 if (!isDeskDockState(mLastBroadcastState)) {
577 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700578 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700579 }
580 mLastBroadcastState = mDockState;
581 action = UiModeManager.ACTION_ENTER_DESK_MODE;
582 }
583 } else {
584 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
585 action = oldAction;
586 }
587
588 if (action != null) {
589 if (LOG) {
590 Slog.v(TAG, String.format(
591 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
592 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800593 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100594
Jeff Brown487bb6e2012-10-11 13:35:42 -0700595 // Send the ordered broadcast; the result receiver will receive after all
596 // broadcasts have been sent. If any broadcast receiver changes the result
597 // code from the initial value of RESULT_OK, then the result receiver will
598 // not launch the corresponding dock application. This gives apps a chance
599 // to override the behavior and stay in their app even when the device is
600 // placed into a dock.
601 Intent intent = new Intent(action);
602 intent.putExtra("enableFlags", enableFlags);
603 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700604 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800605 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700606 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100607
Jeff Brown487bb6e2012-10-11 13:35:42 -0700608 // Attempting to make this transition a little more clean, we are going
609 // to hold off on doing a configuration change until we have finished
610 // the broadcast and started the home activity.
611 mHoldingConfiguration = true;
612 updateConfigurationLocked();
613 } else {
614 String category = null;
615 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700616 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700617 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
618 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800619 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400620 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700621 if (ENABLE_LAUNCH_DESK_DOCK_APP
622 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
623 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800624 }
625 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700626 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
627 category = Intent.CATEGORY_HOME;
628 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800629 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100630
Jeff Brown487bb6e2012-10-11 13:35:42 -0700631 if (LOG) {
632 Slog.v(TAG, "updateLocked: null action, mDockState="
633 + mDockState +", category=" + category);
634 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400635
Jeff Brown487bb6e2012-10-11 13:35:42 -0700636 sendConfigurationAndStartDreamOrDockAppLocked(category);
637 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700638
Jeff Brown487bb6e2012-10-11 13:35:42 -0700639 // keep screen on when charging and in car mode
640 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700641 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700642 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700643 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
644 if (keepScreenOn != mWakeLock.isHeld()) {
645 if (keepScreenOn) {
646 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700647 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700648 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800649 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800650 }
651 }
652
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700653 private void sendForegroundBroadcastToAllUsers(String action) {
654 getContext().sendBroadcastAsUser(new Intent(action)
655 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
656 }
657
Jeff Brown62c82e42012-09-26 01:30:41 -0700658 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
659 // Launch a dock activity
660 String category = null;
661 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
662 // Only launch car home when car mode is enabled and the caller
663 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700664 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700665 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
666 category = Intent.CATEGORY_CAR_DOCK;
667 }
668 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
669 // Only launch car home when desk mode is enabled and the caller
670 // has asked us to switch to it. Currently re-using the car
671 // mode flag since we don't have a formal API for "desk mode".
672 if (ENABLE_LAUNCH_DESK_DOCK_APP
673 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
674 category = Intent.CATEGORY_DESK_DOCK;
675 }
676 } else {
677 // Launch the standard home app if requested.
678 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
679 category = Intent.CATEGORY_HOME;
680 }
681 }
682
683 if (LOG) {
684 Slog.v(TAG, String.format(
685 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
686 + "category=%s",
687 action, enableFlags, disableFlags, category));
688 }
689
690 sendConfigurationAndStartDreamOrDockAppLocked(category);
691 }
692
693 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
694 // Update the configuration but don't send it yet.
695 mHoldingConfiguration = false;
696 updateConfigurationLocked();
697
698 // Start the dock app, if there is one.
699 boolean dockAppStarted = false;
700 if (category != null) {
701 // Now we are going to be careful about switching the
702 // configuration and starting the activity -- we need to
703 // do this in a specific order under control of the
704 // activity manager, to do it cleanly. So compute the
705 // new config, but don't set it yet, and let the
706 // activity manager take care of both the start and config
707 // change.
708 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800709 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700710 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700711 int result = ActivityTaskManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800712 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700713 mConfiguration, null, UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -0700714 if (ActivityManager.isStartResultSuccessful(result)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700715 dockAppStarted = true;
716 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
717 Slog.e(TAG, "Could not start dock app: " + homeIntent
718 + ", startActivityWithConfig result " + result);
719 }
720 } catch (RemoteException ex) {
721 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700722 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700723 }
724 }
725
726 // Send the new configuration.
727 sendConfigurationLocked();
728
729 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700730 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800731 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700732 }
733 }
734
Dianne Hackborn7299c412010-03-04 18:41:49 -0800735 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800736 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800737 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700738 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800739 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800740 }
741
Joe Onorato089de882010-04-12 08:18:45 -0700742 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800743 // features of the status bar; these are ORed together to form the
744 // active disabled list. So if (for example) the device is locked and
745 // the status bar should be totally disabled, the calls below will
746 // have no effect until the device is unlocked.
747 if (mStatusBarManager != null) {
748 mStatusBarManager.disable(mCarModeEnabled
749 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
750 : StatusBarManager.DISABLE_NONE);
751 }
752
753 if (mNotificationManager == null) {
754 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800755 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800756 }
757
758 if (mNotificationManager != null) {
759 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800760 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800761
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500762 Notification.Builder n =
763 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400764 .setSmallIcon(R.drawable.stat_notify_car_mode)
765 .setDefaults(Notification.DEFAULT_LIGHTS)
766 .setOngoing(true)
767 .setWhen(0)
768 .setColor(context.getColor(
769 com.android.internal.R.color.system_notification_accent_color))
770 .setContentTitle(
771 context.getString(R.string.car_mode_disable_notification_title))
772 .setContentText(
773 context.getString(R.string.car_mode_disable_notification_message))
774 .setContentIntent(
775 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
776 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700777 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400778 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800779 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700780 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400781 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800782 }
783 }
784 }
785
Jeff Brown2416e092012-08-21 22:12:20 -0700786 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700787 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700788 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700789 if (state != null) {
790 mComputedNightMode = state.isNight();
791 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800792 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800793 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100794
Zak Cohen1a705732017-01-09 12:54:34 -0800795 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800796 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
797 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800798 try {
799 if (vrManager != null) {
800 vrManager.registerListener(mVrStateCallbacks);
801 }
802 } catch (RemoteException e) {
803 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
804 }
805 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100806
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500807 /**
808 * Handles "adb shell" commands.
809 */
810 private static class Shell extends ShellCommand {
811 public static final String NIGHT_MODE_STR_YES = "yes";
812 public static final String NIGHT_MODE_STR_NO = "no";
813 public static final String NIGHT_MODE_STR_AUTO = "auto";
814 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
815 private final IUiModeManager mInterface;
816
817 Shell(IUiModeManager iface) {
818 mInterface = iface;
819 }
820
821 @Override
822 public void onHelp() {
823 final PrintWriter pw = getOutPrintWriter();
824 pw.println("UiModeManager service (uimode) commands:");
825 pw.println(" help");
826 pw.println(" Print this help text.");
827 pw.println(" night [yes|no|auto]");
828 pw.println(" Set or read night mode.");
829 }
830
831 @Override
832 public int onCommand(String cmd) {
833 if (cmd == null) {
834 return handleDefaultCommands(cmd);
835 }
836
837 try {
838 switch (cmd) {
839 case "night":
840 return handleNightMode();
841 default:
842 return handleDefaultCommands(cmd);
843 }
844 } catch (RemoteException e) {
845 final PrintWriter err = getErrPrintWriter();
846 err.println("Remote exception: " + e);
847 }
848 return -1;
849 }
850
851 private int handleNightMode() throws RemoteException {
852 final PrintWriter err = getErrPrintWriter();
853 final String modeStr = getNextArg();
854 if (modeStr == null) {
855 printCurrentNightMode();
856 return 0;
857 }
858
859 final int mode = strToNightMode(modeStr);
860 if (mode >= 0) {
861 mInterface.setNightMode(mode);
862 printCurrentNightMode();
863 return 0;
864 } else {
865 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
866 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
867 return -1;
868 }
869 }
870
871 private void printCurrentNightMode() throws RemoteException {
872 final PrintWriter pw = getOutPrintWriter();
873 final int currMode = mInterface.getNightMode();
874 final String currModeStr = nightModeToStr(currMode);
875 pw.println("Night mode: " + currModeStr);
876 }
877
878 private static String nightModeToStr(int mode) {
879 switch (mode) {
880 case UiModeManager.MODE_NIGHT_YES:
881 return NIGHT_MODE_STR_YES;
882 case UiModeManager.MODE_NIGHT_NO:
883 return NIGHT_MODE_STR_NO;
884 case UiModeManager.MODE_NIGHT_AUTO:
885 return NIGHT_MODE_STR_AUTO;
886 default:
887 return NIGHT_MODE_STR_UNKNOWN;
888 }
889 }
890
891 private static int strToNightMode(String modeStr) {
892 switch (modeStr) {
893 case NIGHT_MODE_STR_YES:
894 return UiModeManager.MODE_NIGHT_YES;
895 case NIGHT_MODE_STR_NO:
896 return UiModeManager.MODE_NIGHT_NO;
897 case NIGHT_MODE_STR_AUTO:
898 return UiModeManager.MODE_NIGHT_AUTO;
899 default:
900 return -1;
901 }
902 }
903 }
Felipe Lemeff9ec382018-09-24 11:07:56 -0700904
905 public final class LocalService extends UiModeManagerInternal {
906
907 @Override
908 public boolean isNightMode() {
909 synchronized (mLock) {
910 final boolean isIt = (mConfiguration.uiMode & Configuration.UI_MODE_NIGHT_YES) != 0;
911 if (LOG) {
912 Slog.d(TAG,
913 "LocalService.isNightMode(): mNightMode=" + mNightMode
914 + "; mComputedNightMode=" + mComputedNightMode
915 + "; uiMode=" + mConfiguration.uiMode
916 + "; isIt=" + isIt);
917 }
918 return isIt;
919 }
920 }
921 }
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700922
923 private final class UserSwitchedReceiver extends BroadcastReceiver {
924 @Override
925 public void onReceive(Context context, Intent intent) {
926 synchronized (mLock) {
927 final int currentId = intent.getIntExtra(
928 Intent.EXTRA_USER_HANDLE, UserHandle.USER_SYSTEM);
929 // only update if the value is actually changed
930 if (updateNightModeFromSettings(context, context.getResources(), currentId)) {
931 updateLocked(0, 0);
932 }
933 }
934 }
935 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800936}