blob: 710a0ba34d4f533f9d026623976a5c6d30a3fdda [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;
Kweku Adams4db6a3c2019-02-04 16:06:13 -080042import android.os.PowerManager.ServiceType;
43import android.os.PowerManagerInternal;
Dianne Hackborn7299c412010-03-04 18:41:49 -080044import android.os.RemoteException;
Alan Viveretteba0d98f2017-01-30 10:36:54 -050045import android.os.ResultReceiver;
Zak Cohen1a705732017-01-09 12:54:34 -080046import android.os.ServiceManager;
Alan Viveretteba0d98f2017-01-30 10:36:54 -050047import android.os.ShellCallback;
48import android.os.ShellCommand;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070049import android.os.UserHandle;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070050import android.provider.Settings;
Jeff Brown11159e92012-10-11 15:58:37 -070051import android.service.dreams.Sandman;
Zak Cohen1a705732017-01-09 12:54:34 -080052import android.service.vr.IVrManager;
53import android.service.vr.IVrStateCallbacks;
Dianne Hackborn7299c412010-03-04 18:41:49 -080054import android.util.Slog;
55
Dianne Hackborn7299c412010-03-04 18:41:49 -080056import com.android.internal.R;
57import com.android.internal.app.DisableCarModeActivity;
Chris Wren282cfef2017-03-27 15:01:44 -040058import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050059import com.android.internal.notification.SystemNotificationChannels;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060060import com.android.internal.util.DumpUtils;
Adam Lesinski182f73f2013-12-05 16:48:06 -080061import com.android.server.twilight.TwilightListener;
62import com.android.server.twilight.TwilightManager;
63import com.android.server.twilight.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080064
Lucas Dupinb1f0c762018-09-18 10:33:38 -070065import java.io.FileDescriptor;
66import java.io.PrintWriter;
67
Adam Lesinski182f73f2013-12-05 16:48:06 -080068final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080069 private static final String TAG = UiModeManager.class.getSimpleName();
70 private static final boolean LOG = false;
71
Daniel Sandler11ddf532011-11-16 11:10:03 -080072 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040073 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080074
Dianne Hackborn7299c412010-03-04 18:41:49 -080075 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080076 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010077
Adam Lesinski182f73f2013-12-05 16:48:06 -080078 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080079 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080080
Dianne Hackborn7299c412010-03-04 18:41:49 -080081 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050082 private boolean mCharging = false;
Lucas Dupin53c6e292018-07-12 18:42:53 -070083 private boolean mPowerSave = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080084 private int mDefaultUiModeType;
85 private boolean mCarModeKeepsScreenOn;
86 private boolean mDeskModeKeepsScreenOn;
87 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040088 private boolean mWatch;
Zak Cohen1a705732017-01-09 12:54:34 -080089 private boolean mVrHeadset;
Dianne Hackborn7299c412010-03-04 18:41:49 -080090 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070091 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080092
keunyounga7710492015-09-23 11:33:58 -070093 // flag set by resource, whether to enable Car dock launch when starting car mode.
94 private boolean mEnableCarDockLaunch = true;
95 // flag set by resource, whether to lock UI mode to the default one or not.
96 private boolean mUiModeLocked = false;
97 // flag set by resource, whether to night mode change for normal all or not.
98 private boolean mNightModeLocked = false;
99
Adam Lesinski182f73f2013-12-05 16:48:06 -0800100 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800101 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800102 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800103
Dianne Hackborn7299c412010-03-04 18:41:49 -0800104 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800105 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100106
Adam Lesinski182f73f2013-12-05 16:48:06 -0800107 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800108
Adam Lesinski182f73f2013-12-05 16:48:06 -0800109 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800110 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800111 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700112
Adam Lesinski182f73f2013-12-05 16:48:06 -0800113 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800114
Felipe Lemeff9ec382018-09-24 11:07:56 -0700115 private final LocalService mLocalService = new LocalService();
116
Jeff Brownb880d882014-02-10 19:47:07 -0800117 public UiModeManagerService(Context context) {
118 super(context);
119 }
120
Adam Lesinski182f73f2013-12-05 16:48:06 -0800121 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700122 Intent intent = new Intent(Intent.ACTION_MAIN);
123 intent.addCategory(category);
124 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
125 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
126 return intent;
127 }
John Spurlock960779d2012-05-29 14:37:05 -0400128
Dianne Hackborn7299c412010-03-04 18:41:49 -0800129 // The broadcast receiver which receives the result of the ordered broadcast sent when
130 // the dock state changes. The original ordered broadcast is sent with an initial result
131 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
132 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
133 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
134 @Override
135 public void onReceive(Context context, Intent intent) {
136 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400137 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400138 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400139 + ": canceled: " + getResultCode());
140 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800141 return;
142 }
143
Jeff Brown62c82e42012-09-26 01:30:41 -0700144 final int enableFlags = intent.getIntExtra("enableFlags", 0);
145 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800146 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700147 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800148 }
149 }
150 };
151
Dianne Hackborn7299c412010-03-04 18:41:49 -0800152 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
153 @Override
154 public void onReceive(Context context, Intent intent) {
155 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
156 Intent.EXTRA_DOCK_STATE_UNDOCKED);
157 updateDockState(state);
158 }
159 };
160
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500161 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
162 @Override
163 public void onReceive(Context context, Intent intent) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700164 switch (intent.getAction()) {
165 case Intent.ACTION_BATTERY_CHANGED:
166 mCharging = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
167 break;
Lucas Dupin53c6e292018-07-12 18:42:53 -0700168 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500169 synchronized (mLock) {
170 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700171 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500172 }
173 }
174 }
175 };
176
Adam Lesinski182f73f2013-12-05 16:48:06 -0800177 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700178 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700179 public void onTwilightStateChanged(@Nullable TwilightState state) {
180 synchronized (mLock) {
181 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
182 updateComputedNightModeLocked();
183 updateLocked(0, 0);
184 }
185 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700186 }
187 };
188
Zak Cohen1a705732017-01-09 12:54:34 -0800189 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
190 @Override
191 public void onVrStateChanged(boolean enabled) {
192 synchronized (mLock) {
193 mVrHeadset = enabled;
194 if (mSystemReady) {
195 updateLocked(0, 0);
196 }
197 }
198 }
199 };
200
Adam Lesinski182f73f2013-12-05 16:48:06 -0800201 @Override
202 public void onStart() {
203 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800204
Adam Lesinski182f73f2013-12-05 16:48:06 -0800205 final PowerManager powerManager =
206 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
207 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800208
Adam Lesinski182f73f2013-12-05 16:48:06 -0800209 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800210 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Lucas Dupin53c6e292018-07-12 18:42:53 -0700211 IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Lucas Dupin53c6e292018-07-12 18:42:53 -0700212 context.registerReceiver(mBatteryReceiver, batteryFilter);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500213
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800214 PowerManagerInternal localPowerManager =
215 LocalServices.getService(PowerManagerInternal.class);
216 mPowerSave = localPowerManager.getLowPowerState(ServiceType.NIGHT_MODE).batterySaverEnabled;
217 localPowerManager.registerLowPowerModeObserver(ServiceType.NIGHT_MODE,
218 state -> {
219 synchronized (mLock) {
220 if (mPowerSave == state.batterySaverEnabled) {
221 return;
222 }
223 mPowerSave = state.batterySaverEnabled;
224 if (mSystemReady) {
225 updateLocked(0, 0);
226 }
227 }
228 });
229
Dianne Hackborn7299c412010-03-04 18:41:49 -0800230 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500231
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800232 final Resources res = context.getResources();
233 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800234 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800235 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500236 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800237 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500238 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700239 mEnableCarDockLaunch = res.getBoolean(
240 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
241 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
242 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700243
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800244 final PackageManager pm = context.getPackageManager();
245 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
246 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
247 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
248
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700249 updateNightModeFromSettings(context, res, UserHandle.getCallingUserId());
Jeff Brown2416e092012-08-21 22:12:20 -0700250
Adam Lesinski05199e82015-03-19 14:37:11 -0700251 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800252 SystemServerInitThreadPool.get().submit(() -> {
253 synchronized (mLock) {
254 updateConfigurationLocked();
255 sendConfigurationLocked();
256 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800257
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800258 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800259 publishBinderService(Context.UI_MODE_SERVICE, mService);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700260 publishLocalService(UiModeManagerInternal.class, mLocalService);
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700261
262 IntentFilter filter = new IntentFilter();
263 filter.addAction(Intent.ACTION_USER_SWITCHED);
264 context.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
265 }
266
267 /**
268 * Updates the night mode setting in Settings.Global and returns if the value was successfully
269 * changed.
270 * @param context A valid context
271 * @param res A valid resource object
272 * @param userId The user to update the setting for
273 * @return True if the new value is different from the old value. False otherwise.
274 */
275 private boolean updateNightModeFromSettings(Context context, Resources res, int userId) {
276 final int defaultNightMode = res.getInteger(
277 com.android.internal.R.integer.config_defaultNightMode);
278 int oldNightMode = mNightMode;
279 mNightMode = Settings.Secure.getIntForUser(context.getContentResolver(),
280 Settings.Secure.UI_NIGHT_MODE, defaultNightMode, userId);
281
282 // false if night mode stayed the same, true otherwise.
283 return !(oldNightMode == mNightMode);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800284 }
285
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500286 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800287 @Override
288 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700289 if (isUiModeLocked()) {
290 Slog.e(TAG, "enableCarMode while UI mode is locked");
291 return;
292 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800293 final long ident = Binder.clearCallingIdentity();
294 try {
295 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700296 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800297 if (mSystemReady) {
298 updateLocked(flags, 0);
299 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700300 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800301 } finally {
302 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500303 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800304 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800305
Adam Lesinski182f73f2013-12-05 16:48:06 -0800306 @Override
307 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700308 if (isUiModeLocked()) {
309 Slog.e(TAG, "disableCarMode while UI mode is locked");
310 return;
311 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800312 final long ident = Binder.clearCallingIdentity();
313 try {
314 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700315 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800316 if (mSystemReady) {
317 updateLocked(0, flags);
318 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700319 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800320 } finally {
321 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500322 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700323 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100324
Adam Lesinski182f73f2013-12-05 16:48:06 -0800325 @Override
326 public int getCurrentModeType() {
327 final long ident = Binder.clearCallingIdentity();
328 try {
329 synchronized (mLock) {
330 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700331 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800332 } finally {
333 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800334 }
335 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100336
Adam Lesinski182f73f2013-12-05 16:48:06 -0800337 @Override
338 public void setNightMode(int mode) {
Lucas Dupinc5aaf452018-09-20 16:10:07 -0700339 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
keunyounga7710492015-09-23 11:33:58 -0700340 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
341 != PackageManager.PERMISSION_GRANTED)) {
Lucas Dupinc5aaf452018-09-20 16:10:07 -0700342 Slog.e(TAG, "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
keunyounga7710492015-09-23 11:33:58 -0700343 return;
344 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800345 switch (mode) {
346 case UiModeManager.MODE_NIGHT_NO:
347 case UiModeManager.MODE_NIGHT_YES:
348 case UiModeManager.MODE_NIGHT_AUTO:
349 break;
350 default:
351 throw new IllegalArgumentException("Unknown mode: " + mode);
352 }
353
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700354 final int user = UserHandle.getCallingUserId();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800355 final long ident = Binder.clearCallingIdentity();
356 try {
357 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800358 if (mNightMode != mode) {
Winsonadc69672019-01-28 14:56:12 -0800359 // Only persist setting if not transient night mode or not in car mode
360 if (!shouldTransientNightWhenInCarMode() || !mCarModeEnabled) {
361 Settings.Secure.putIntForUser(getContext().getContentResolver(),
362 Settings.Secure.UI_NIGHT_MODE, mode, user);
363 }
364
Adam Lesinski182f73f2013-12-05 16:48:06 -0800365 mNightMode = mode;
366 updateLocked(0, 0);
367 }
368 }
369 } finally {
370 Binder.restoreCallingIdentity(ident);
371 }
372 }
373
374 @Override
375 public int getNightMode() {
376 synchronized (mLock) {
377 return mNightMode;
378 }
379 }
380
381 @Override
keunyounga7710492015-09-23 11:33:58 -0700382 public boolean isUiModeLocked() {
383 synchronized (mLock) {
384 return mUiModeLocked;
385 }
386 }
387
388 @Override
389 public boolean isNightModeLocked() {
390 synchronized (mLock) {
391 return mNightModeLocked;
392 }
393 }
394
395 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500396 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
397 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
398 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
399 }
400
401 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800402 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600403 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800404 dumpImpl(pw);
405 }
406 };
407
408 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700409 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800410 pw.println("Current UI Mode Service state:");
411 pw.print(" mDockState="); pw.print(mDockState);
412 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700413 pw.print(" mNightMode="); pw.print(mNightMode); pw.print(" (");
414 pw.print(Shell.nightModeToStr(mNightMode)); pw.print(") ");
keunyounga7710492015-09-23 11:33:58 -0700415 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800416 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700417 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700418 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
419 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800420 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700421 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800422 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
423 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
424 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700425 if (mTwilightManager != null) {
426 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700427 pw.print(" mTwilightService.getLastTwilightState()=");
428 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700429 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700430 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800431 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100432
Adam Lesinski182f73f2013-12-05 16:48:06 -0800433 @Override
434 public void onBootPhase(int phase) {
435 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
436 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700437 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800438 mSystemReady = true;
439 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
440 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800441 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800442 updateLocked(0, 0);
443 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800444 }
445 }
446
Winsonadc69672019-01-28 14:56:12 -0800447 // Night mode settings in car mode are only persisted below Q.
448 // When targeting Q, changes are not saved and night mode will be re-read
449 // from settings when exiting car mode.
450 private boolean shouldTransientNightWhenInCarMode() {
451 int uid = Binder.getCallingUid();
452 PackageManager packageManager = getContext().getPackageManager();
453 String[] packagesForUid = packageManager.getPackagesForUid(uid);
454 if (packagesForUid == null || packagesForUid.length == 0) {
455 return false;
456 }
457
458 try {
459 ApplicationInfo appInfo = packageManager.getApplicationInfoAsUser(
460 packagesForUid[0], 0, uid);
461
462 return appInfo.targetSdkVersion >= Build.VERSION_CODES.Q;
463 } catch (PackageManager.NameNotFoundException ignored) {
464 }
465
466 return false;
467 }
468
keunyoung1d0a7cc2014-07-28 13:12:50 -0700469 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800470 if (mCarModeEnabled != enabled) {
471 mCarModeEnabled = enabled;
Winsonadc69672019-01-28 14:56:12 -0800472
473 // When transient night mode and exiting car mode, restore night mode from settings
474 if (shouldTransientNightWhenInCarMode() && !mCarModeEnabled) {
475 Context context = getContext();
476 updateNightModeFromSettings(context,
477 context.getResources(),
478 UserHandle.getCallingUserId());
479 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800480 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700481 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800482 }
483
Jeff Brown487bb6e2012-10-11 13:35:42 -0700484 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800485 synchronized (mLock) {
486 if (newState != mDockState) {
487 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700488 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800489 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700490 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800491 }
492 }
493 }
494 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500495
Jeff Brown487bb6e2012-10-11 13:35:42 -0700496 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400497 switch (state) {
498 case Intent.EXTRA_DOCK_STATE_DESK:
499 case Intent.EXTRA_DOCK_STATE_LE_DESK:
500 case Intent.EXTRA_DOCK_STATE_HE_DESK:
501 return true;
502 default:
503 return false;
504 }
505 }
506
Jeff Brown487bb6e2012-10-11 13:35:42 -0700507 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400508 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700509 if (mUiModeLocked) {
510 // no-op, keeps default one
511 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400512 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
513 } else if (mWatch) {
514 uiMode = Configuration.UI_MODE_TYPE_WATCH;
515 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800516 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400517 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800518 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800519 } else if (mVrHeadset) {
520 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800521 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800522
523 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700524 if (mTwilightManager != null) {
525 mTwilightManager.registerListener(mTwilightListener, mHandler);
526 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800527 updateComputedNightModeLocked();
528 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
529 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800530 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700531 if (mTwilightManager != null) {
532 mTwilightManager.unregisterListener(mTwilightListener);
533 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800534 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400535 }
536
Winsonadc69672019-01-28 14:56:12 -0800537 // Override night mode in power save mode if not transient night mode or not in car mode
538 boolean shouldOverrideNight = !mCarModeEnabled || !shouldTransientNightWhenInCarMode();
539 if (mPowerSave && shouldOverrideNight) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700540 uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
541 uiMode |= Configuration.UI_MODE_NIGHT_YES;
542 }
543
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400544 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400545 Slog.d(TAG,
546 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400547 + "; mCarMode=" + mCarModeEnabled
548 + "; mNightMode=" + mNightMode
549 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800550 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100551
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800552 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700553 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700554 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700555 }
556 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100557
Jeff Brown487bb6e2012-10-11 13:35:42 -0700558 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700559 if (mSetUiMode != mConfiguration.uiMode) {
560 mSetUiMode = mConfiguration.uiMode;
561
562 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700563 ActivityTaskManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700564 } catch (RemoteException e) {
565 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800566 }
567 }
568 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100569
Adam Lesinski182f73f2013-12-05 16:48:06 -0800570 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700571 String action = null;
572 String oldAction = null;
573 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
574 adjustStatusBarCarModeLocked();
575 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
576 } else if (isDeskDockState(mLastBroadcastState)) {
577 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
578 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100579
Jeff Brown487bb6e2012-10-11 13:35:42 -0700580 if (mCarModeEnabled) {
581 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100582 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700583 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700584 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700585 }
586 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
587 action = UiModeManager.ACTION_ENTER_CAR_MODE;
588 }
589 } else if (isDeskDockState(mDockState)) {
590 if (!isDeskDockState(mLastBroadcastState)) {
591 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700592 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700593 }
594 mLastBroadcastState = mDockState;
595 action = UiModeManager.ACTION_ENTER_DESK_MODE;
596 }
597 } else {
598 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
599 action = oldAction;
600 }
601
602 if (action != null) {
603 if (LOG) {
604 Slog.v(TAG, String.format(
605 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
606 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800607 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100608
Jeff Brown487bb6e2012-10-11 13:35:42 -0700609 // Send the ordered broadcast; the result receiver will receive after all
610 // broadcasts have been sent. If any broadcast receiver changes the result
611 // code from the initial value of RESULT_OK, then the result receiver will
612 // not launch the corresponding dock application. This gives apps a chance
613 // to override the behavior and stay in their app even when the device is
614 // placed into a dock.
615 Intent intent = new Intent(action);
616 intent.putExtra("enableFlags", enableFlags);
617 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700618 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800619 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700620 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100621
Jeff Brown487bb6e2012-10-11 13:35:42 -0700622 // Attempting to make this transition a little more clean, we are going
623 // to hold off on doing a configuration change until we have finished
624 // the broadcast and started the home activity.
625 mHoldingConfiguration = true;
626 updateConfigurationLocked();
627 } else {
628 String category = null;
629 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700630 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700631 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
632 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800633 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400634 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700635 if (ENABLE_LAUNCH_DESK_DOCK_APP
636 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
637 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800638 }
639 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700640 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
641 category = Intent.CATEGORY_HOME;
642 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800643 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100644
Jeff Brown487bb6e2012-10-11 13:35:42 -0700645 if (LOG) {
646 Slog.v(TAG, "updateLocked: null action, mDockState="
647 + mDockState +", category=" + category);
648 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400649
Jeff Brown487bb6e2012-10-11 13:35:42 -0700650 sendConfigurationAndStartDreamOrDockAppLocked(category);
651 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700652
Jeff Brown487bb6e2012-10-11 13:35:42 -0700653 // keep screen on when charging and in car mode
654 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700655 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700656 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700657 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
658 if (keepScreenOn != mWakeLock.isHeld()) {
659 if (keepScreenOn) {
660 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700661 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700662 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800663 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800664 }
665 }
666
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700667 private void sendForegroundBroadcastToAllUsers(String action) {
668 getContext().sendBroadcastAsUser(new Intent(action)
669 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
670 }
671
Jeff Brown62c82e42012-09-26 01:30:41 -0700672 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
673 // Launch a dock activity
674 String category = null;
675 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
676 // Only launch car home when car mode is enabled and the caller
677 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700678 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700679 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
680 category = Intent.CATEGORY_CAR_DOCK;
681 }
682 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
683 // Only launch car home when desk mode is enabled and the caller
684 // has asked us to switch to it. Currently re-using the car
685 // mode flag since we don't have a formal API for "desk mode".
686 if (ENABLE_LAUNCH_DESK_DOCK_APP
687 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
688 category = Intent.CATEGORY_DESK_DOCK;
689 }
690 } else {
691 // Launch the standard home app if requested.
692 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
693 category = Intent.CATEGORY_HOME;
694 }
695 }
696
697 if (LOG) {
698 Slog.v(TAG, String.format(
699 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
700 + "category=%s",
701 action, enableFlags, disableFlags, category));
702 }
703
704 sendConfigurationAndStartDreamOrDockAppLocked(category);
705 }
706
707 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
708 // Update the configuration but don't send it yet.
709 mHoldingConfiguration = false;
710 updateConfigurationLocked();
711
712 // Start the dock app, if there is one.
713 boolean dockAppStarted = false;
714 if (category != null) {
715 // Now we are going to be careful about switching the
716 // configuration and starting the activity -- we need to
717 // do this in a specific order under control of the
718 // activity manager, to do it cleanly. So compute the
719 // new config, but don't set it yet, and let the
720 // activity manager take care of both the start and config
721 // change.
722 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800723 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700724 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700725 int result = ActivityTaskManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800726 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700727 mConfiguration, null, UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -0700728 if (ActivityManager.isStartResultSuccessful(result)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700729 dockAppStarted = true;
730 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
731 Slog.e(TAG, "Could not start dock app: " + homeIntent
732 + ", startActivityWithConfig result " + result);
733 }
734 } catch (RemoteException ex) {
735 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700736 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700737 }
738 }
739
740 // Send the new configuration.
741 sendConfigurationLocked();
742
743 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700744 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800745 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700746 }
747 }
748
Dianne Hackborn7299c412010-03-04 18:41:49 -0800749 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800750 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800751 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700752 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800753 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800754 }
755
Joe Onorato089de882010-04-12 08:18:45 -0700756 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800757 // features of the status bar; these are ORed together to form the
758 // active disabled list. So if (for example) the device is locked and
759 // the status bar should be totally disabled, the calls below will
760 // have no effect until the device is unlocked.
761 if (mStatusBarManager != null) {
762 mStatusBarManager.disable(mCarModeEnabled
763 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
764 : StatusBarManager.DISABLE_NONE);
765 }
766
767 if (mNotificationManager == null) {
768 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800769 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800770 }
771
772 if (mNotificationManager != null) {
773 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800774 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800775
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500776 Notification.Builder n =
777 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400778 .setSmallIcon(R.drawable.stat_notify_car_mode)
779 .setDefaults(Notification.DEFAULT_LIGHTS)
780 .setOngoing(true)
781 .setWhen(0)
782 .setColor(context.getColor(
783 com.android.internal.R.color.system_notification_accent_color))
784 .setContentTitle(
785 context.getString(R.string.car_mode_disable_notification_title))
786 .setContentText(
787 context.getString(R.string.car_mode_disable_notification_message))
788 .setContentIntent(
789 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
790 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700791 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400792 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800793 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700794 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400795 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800796 }
797 }
798 }
799
Jeff Brown2416e092012-08-21 22:12:20 -0700800 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700801 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700802 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700803 if (state != null) {
804 mComputedNightMode = state.isNight();
805 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800806 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800807 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100808
Zak Cohen1a705732017-01-09 12:54:34 -0800809 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800810 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
811 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800812 try {
813 if (vrManager != null) {
814 vrManager.registerListener(mVrStateCallbacks);
815 }
816 } catch (RemoteException e) {
817 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
818 }
819 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100820
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500821 /**
822 * Handles "adb shell" commands.
823 */
824 private static class Shell extends ShellCommand {
825 public static final String NIGHT_MODE_STR_YES = "yes";
826 public static final String NIGHT_MODE_STR_NO = "no";
827 public static final String NIGHT_MODE_STR_AUTO = "auto";
828 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
829 private final IUiModeManager mInterface;
830
831 Shell(IUiModeManager iface) {
832 mInterface = iface;
833 }
834
835 @Override
836 public void onHelp() {
837 final PrintWriter pw = getOutPrintWriter();
838 pw.println("UiModeManager service (uimode) commands:");
839 pw.println(" help");
840 pw.println(" Print this help text.");
841 pw.println(" night [yes|no|auto]");
842 pw.println(" Set or read night mode.");
843 }
844
845 @Override
846 public int onCommand(String cmd) {
847 if (cmd == null) {
848 return handleDefaultCommands(cmd);
849 }
850
851 try {
852 switch (cmd) {
853 case "night":
854 return handleNightMode();
855 default:
856 return handleDefaultCommands(cmd);
857 }
858 } catch (RemoteException e) {
859 final PrintWriter err = getErrPrintWriter();
860 err.println("Remote exception: " + e);
861 }
862 return -1;
863 }
864
865 private int handleNightMode() throws RemoteException {
866 final PrintWriter err = getErrPrintWriter();
867 final String modeStr = getNextArg();
868 if (modeStr == null) {
869 printCurrentNightMode();
870 return 0;
871 }
872
873 final int mode = strToNightMode(modeStr);
874 if (mode >= 0) {
875 mInterface.setNightMode(mode);
876 printCurrentNightMode();
877 return 0;
878 } else {
879 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
880 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
881 return -1;
882 }
883 }
884
885 private void printCurrentNightMode() throws RemoteException {
886 final PrintWriter pw = getOutPrintWriter();
887 final int currMode = mInterface.getNightMode();
888 final String currModeStr = nightModeToStr(currMode);
889 pw.println("Night mode: " + currModeStr);
890 }
891
892 private static String nightModeToStr(int mode) {
893 switch (mode) {
894 case UiModeManager.MODE_NIGHT_YES:
895 return NIGHT_MODE_STR_YES;
896 case UiModeManager.MODE_NIGHT_NO:
897 return NIGHT_MODE_STR_NO;
898 case UiModeManager.MODE_NIGHT_AUTO:
899 return NIGHT_MODE_STR_AUTO;
900 default:
901 return NIGHT_MODE_STR_UNKNOWN;
902 }
903 }
904
905 private static int strToNightMode(String modeStr) {
906 switch (modeStr) {
907 case NIGHT_MODE_STR_YES:
908 return UiModeManager.MODE_NIGHT_YES;
909 case NIGHT_MODE_STR_NO:
910 return UiModeManager.MODE_NIGHT_NO;
911 case NIGHT_MODE_STR_AUTO:
912 return UiModeManager.MODE_NIGHT_AUTO;
913 default:
914 return -1;
915 }
916 }
917 }
Felipe Lemeff9ec382018-09-24 11:07:56 -0700918
919 public final class LocalService extends UiModeManagerInternal {
920
921 @Override
922 public boolean isNightMode() {
923 synchronized (mLock) {
924 final boolean isIt = (mConfiguration.uiMode & Configuration.UI_MODE_NIGHT_YES) != 0;
925 if (LOG) {
926 Slog.d(TAG,
927 "LocalService.isNightMode(): mNightMode=" + mNightMode
928 + "; mComputedNightMode=" + mComputedNightMode
929 + "; uiMode=" + mConfiguration.uiMode
930 + "; isIt=" + isIt);
931 }
932 return isIt;
933 }
934 }
935 }
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700936
937 private final class UserSwitchedReceiver extends BroadcastReceiver {
938 @Override
939 public void onReceive(Context context, Intent intent) {
940 synchronized (mLock) {
941 final int currentId = intent.getIntExtra(
942 Intent.EXTRA_USER_HANDLE, UserHandle.USER_SYSTEM);
943 // only update if the value is actually changed
944 if (updateNightModeFromSettings(context, context.getResources(), currentId)) {
945 updateLocked(0, 0);
946 }
947 }
948 }
949 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800950}