blob: 1b1e6ad6e096a469b3577a1b87a3f96237d48bb0 [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;
33import android.content.pm.PackageManager;
34import android.content.res.Configuration;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080035import android.content.res.Resources;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050036import android.os.BatteryManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080037import android.os.Binder;
Dianne Hackborn7299c412010-03-04 18:41:49 -080038import android.os.Handler;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050039import android.os.PowerManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080040import android.os.RemoteException;
Alan Viveretteba0d98f2017-01-30 10:36:54 -050041import android.os.ResultReceiver;
Zak Cohen1a705732017-01-09 12:54:34 -080042import android.os.ServiceManager;
Alan Viveretteba0d98f2017-01-30 10:36:54 -050043import android.os.ShellCallback;
44import android.os.ShellCommand;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070045import android.os.UserHandle;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070046import android.provider.Settings;
Jeff Brown11159e92012-10-11 15:58:37 -070047import android.service.dreams.Sandman;
Zak Cohen1a705732017-01-09 12:54:34 -080048import android.service.vr.IVrManager;
49import android.service.vr.IVrStateCallbacks;
Dianne Hackborn7299c412010-03-04 18:41:49 -080050import android.util.Slog;
51
Dianne Hackborn7299c412010-03-04 18:41:49 -080052import com.android.internal.R;
53import com.android.internal.app.DisableCarModeActivity;
Chris Wren282cfef2017-03-27 15:01:44 -040054import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050055import com.android.internal.notification.SystemNotificationChannels;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060056import com.android.internal.util.DumpUtils;
Adam Lesinski182f73f2013-12-05 16:48:06 -080057import com.android.server.twilight.TwilightListener;
58import com.android.server.twilight.TwilightManager;
59import com.android.server.twilight.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080060
Lucas Dupinb1f0c762018-09-18 10:33:38 -070061import java.io.FileDescriptor;
62import java.io.PrintWriter;
63
Adam Lesinski182f73f2013-12-05 16:48:06 -080064final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080065 private static final String TAG = UiModeManager.class.getSimpleName();
66 private static final boolean LOG = false;
67
Daniel Sandler11ddf532011-11-16 11:10:03 -080068 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040069 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080070
Dianne Hackborn7299c412010-03-04 18:41:49 -080071 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080072 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010073
Adam Lesinski182f73f2013-12-05 16:48:06 -080074 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080075 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080076
Dianne Hackborn7299c412010-03-04 18:41:49 -080077 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050078 private boolean mCharging = false;
Lucas Dupin53c6e292018-07-12 18:42:53 -070079 private boolean mPowerSave = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080080 private int mDefaultUiModeType;
81 private boolean mCarModeKeepsScreenOn;
82 private boolean mDeskModeKeepsScreenOn;
83 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040084 private boolean mWatch;
Zak Cohen1a705732017-01-09 12:54:34 -080085 private boolean mVrHeadset;
Dianne Hackborn7299c412010-03-04 18:41:49 -080086 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070087 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080088
keunyounga7710492015-09-23 11:33:58 -070089 // flag set by resource, whether to enable Car dock launch when starting car mode.
90 private boolean mEnableCarDockLaunch = true;
91 // flag set by resource, whether to lock UI mode to the default one or not.
92 private boolean mUiModeLocked = false;
93 // flag set by resource, whether to night mode change for normal all or not.
94 private boolean mNightModeLocked = false;
95
Adam Lesinski182f73f2013-12-05 16:48:06 -080096 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080097 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080098 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080099
Dianne Hackborn7299c412010-03-04 18:41:49 -0800100 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800101 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100102
Adam Lesinski182f73f2013-12-05 16:48:06 -0800103 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800104
Adam Lesinski182f73f2013-12-05 16:48:06 -0800105 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800106 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800107 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700108
Adam Lesinski182f73f2013-12-05 16:48:06 -0800109 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800110
Felipe Lemeff9ec382018-09-24 11:07:56 -0700111 private final LocalService mLocalService = new LocalService();
112
Jeff Brownb880d882014-02-10 19:47:07 -0800113 public UiModeManagerService(Context context) {
114 super(context);
115 }
116
Adam Lesinski182f73f2013-12-05 16:48:06 -0800117 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700118 Intent intent = new Intent(Intent.ACTION_MAIN);
119 intent.addCategory(category);
120 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
121 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
122 return intent;
123 }
John Spurlock960779d2012-05-29 14:37:05 -0400124
Dianne Hackborn7299c412010-03-04 18:41:49 -0800125 // The broadcast receiver which receives the result of the ordered broadcast sent when
126 // the dock state changes. The original ordered broadcast is sent with an initial result
127 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
128 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
129 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
130 @Override
131 public void onReceive(Context context, Intent intent) {
132 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400133 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400134 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400135 + ": canceled: " + getResultCode());
136 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800137 return;
138 }
139
Jeff Brown62c82e42012-09-26 01:30:41 -0700140 final int enableFlags = intent.getIntExtra("enableFlags", 0);
141 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800142 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700143 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800144 }
145 }
146 };
147
Dianne Hackborn7299c412010-03-04 18:41:49 -0800148 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
149 @Override
150 public void onReceive(Context context, Intent intent) {
151 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
152 Intent.EXTRA_DOCK_STATE_UNDOCKED);
153 updateDockState(state);
154 }
155 };
156
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500157 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
158 @Override
159 public void onReceive(Context context, Intent intent) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700160 switch (intent.getAction()) {
161 case Intent.ACTION_BATTERY_CHANGED:
162 mCharging = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
163 break;
164 case PowerManager.ACTION_POWER_SAVE_MODE_CHANGING:
165 mPowerSave = intent.getBooleanExtra(PowerManager.EXTRA_POWER_SAVE_MODE, false);
166 break;
167 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500168 synchronized (mLock) {
169 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700170 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500171 }
172 }
173 }
174 };
175
Adam Lesinski182f73f2013-12-05 16:48:06 -0800176 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700177 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700178 public void onTwilightStateChanged(@Nullable TwilightState state) {
179 synchronized (mLock) {
180 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
181 updateComputedNightModeLocked();
182 updateLocked(0, 0);
183 }
184 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700185 }
186 };
187
Zak Cohen1a705732017-01-09 12:54:34 -0800188 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
189 @Override
190 public void onVrStateChanged(boolean enabled) {
191 synchronized (mLock) {
192 mVrHeadset = enabled;
193 if (mSystemReady) {
194 updateLocked(0, 0);
195 }
196 }
197 }
198 };
199
Adam Lesinski182f73f2013-12-05 16:48:06 -0800200 @Override
201 public void onStart() {
202 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800203
Adam Lesinski182f73f2013-12-05 16:48:06 -0800204 final PowerManager powerManager =
205 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
206 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800207
Adam Lesinski182f73f2013-12-05 16:48:06 -0800208 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800209 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Lucas Dupin53c6e292018-07-12 18:42:53 -0700210 IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
211 batteryFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING);
212 context.registerReceiver(mBatteryReceiver, batteryFilter);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500213
Dianne Hackborn7299c412010-03-04 18:41:49 -0800214 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500215
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800216 final Resources res = context.getResources();
217 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800218 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800219 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500220 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800221 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500222 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700223 mEnableCarDockLaunch = res.getBoolean(
224 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
225 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
226 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700227
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800228 final PackageManager pm = context.getPackageManager();
229 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
230 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
231 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
232
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700233 updateNightModeFromSettings(context, res, UserHandle.getCallingUserId());
Jeff Brown2416e092012-08-21 22:12:20 -0700234
Adam Lesinski05199e82015-03-19 14:37:11 -0700235 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800236 SystemServerInitThreadPool.get().submit(() -> {
237 synchronized (mLock) {
238 updateConfigurationLocked();
239 sendConfigurationLocked();
240 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800241
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800242 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800243 publishBinderService(Context.UI_MODE_SERVICE, mService);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700244 publishLocalService(UiModeManagerInternal.class, mLocalService);
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700245
246 IntentFilter filter = new IntentFilter();
247 filter.addAction(Intent.ACTION_USER_SWITCHED);
248 context.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
249 }
250
251 /**
252 * Updates the night mode setting in Settings.Global and returns if the value was successfully
253 * changed.
254 * @param context A valid context
255 * @param res A valid resource object
256 * @param userId The user to update the setting for
257 * @return True if the new value is different from the old value. False otherwise.
258 */
259 private boolean updateNightModeFromSettings(Context context, Resources res, int userId) {
260 final int defaultNightMode = res.getInteger(
261 com.android.internal.R.integer.config_defaultNightMode);
262 int oldNightMode = mNightMode;
263 mNightMode = Settings.Secure.getIntForUser(context.getContentResolver(),
264 Settings.Secure.UI_NIGHT_MODE, defaultNightMode, userId);
265
266 // false if night mode stayed the same, true otherwise.
267 return !(oldNightMode == mNightMode);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800268 }
269
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500270 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800271 @Override
272 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700273 if (isUiModeLocked()) {
274 Slog.e(TAG, "enableCarMode while UI mode is locked");
275 return;
276 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800277 final long ident = Binder.clearCallingIdentity();
278 try {
279 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700280 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800281 if (mSystemReady) {
282 updateLocked(flags, 0);
283 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700284 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800285 } finally {
286 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500287 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800288 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800289
Adam Lesinski182f73f2013-12-05 16:48:06 -0800290 @Override
291 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700292 if (isUiModeLocked()) {
293 Slog.e(TAG, "disableCarMode while UI mode is locked");
294 return;
295 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800296 final long ident = Binder.clearCallingIdentity();
297 try {
298 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700299 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800300 if (mSystemReady) {
301 updateLocked(0, flags);
302 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700303 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800304 } finally {
305 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500306 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700307 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100308
Adam Lesinski182f73f2013-12-05 16:48:06 -0800309 @Override
310 public int getCurrentModeType() {
311 final long ident = Binder.clearCallingIdentity();
312 try {
313 synchronized (mLock) {
314 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700315 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800316 } finally {
317 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800318 }
319 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100320
Adam Lesinski182f73f2013-12-05 16:48:06 -0800321 @Override
322 public void setNightMode(int mode) {
Lucas Dupinc5aaf452018-09-20 16:10:07 -0700323 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
keunyounga7710492015-09-23 11:33:58 -0700324 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
325 != PackageManager.PERMISSION_GRANTED)) {
Lucas Dupinc5aaf452018-09-20 16:10:07 -0700326 Slog.e(TAG, "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
keunyounga7710492015-09-23 11:33:58 -0700327 return;
328 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800329 switch (mode) {
330 case UiModeManager.MODE_NIGHT_NO:
331 case UiModeManager.MODE_NIGHT_YES:
332 case UiModeManager.MODE_NIGHT_AUTO:
333 break;
334 default:
335 throw new IllegalArgumentException("Unknown mode: " + mode);
336 }
337
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700338 final int user = UserHandle.getCallingUserId();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800339 final long ident = Binder.clearCallingIdentity();
340 try {
341 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800342 if (mNightMode != mode) {
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700343 Settings.Secure.putIntForUser(getContext().getContentResolver(),
344 Settings.Secure.UI_NIGHT_MODE, mode, user);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800345 mNightMode = mode;
346 updateLocked(0, 0);
347 }
348 }
349 } finally {
350 Binder.restoreCallingIdentity(ident);
351 }
352 }
353
354 @Override
355 public int getNightMode() {
356 synchronized (mLock) {
357 return mNightMode;
358 }
359 }
360
361 @Override
keunyounga7710492015-09-23 11:33:58 -0700362 public boolean isUiModeLocked() {
363 synchronized (mLock) {
364 return mUiModeLocked;
365 }
366 }
367
368 @Override
369 public boolean isNightModeLocked() {
370 synchronized (mLock) {
371 return mNightModeLocked;
372 }
373 }
374
375 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500376 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
377 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
378 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
379 }
380
381 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800382 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600383 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800384 dumpImpl(pw);
385 }
386 };
387
388 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700389 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800390 pw.println("Current UI Mode Service state:");
391 pw.print(" mDockState="); pw.print(mDockState);
392 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700393 pw.print(" mNightMode="); pw.print(mNightMode); pw.print(" (");
394 pw.print(Shell.nightModeToStr(mNightMode)); pw.print(") ");
keunyounga7710492015-09-23 11:33:58 -0700395 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800396 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700397 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700398 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
399 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800400 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700401 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800402 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
403 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
404 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700405 if (mTwilightManager != null) {
406 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700407 pw.print(" mTwilightService.getLastTwilightState()=");
408 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700409 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700410 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800411 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100412
Adam Lesinski182f73f2013-12-05 16:48:06 -0800413 @Override
414 public void onBootPhase(int phase) {
415 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
416 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700417 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800418 mSystemReady = true;
419 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
420 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800421 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800422 updateLocked(0, 0);
423 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800424 }
425 }
426
keunyoung1d0a7cc2014-07-28 13:12:50 -0700427 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800428 if (mCarModeEnabled != enabled) {
429 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800430 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700431 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800432 }
433
Jeff Brown487bb6e2012-10-11 13:35:42 -0700434 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800435 synchronized (mLock) {
436 if (newState != mDockState) {
437 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700438 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800439 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700440 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800441 }
442 }
443 }
444 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500445
Jeff Brown487bb6e2012-10-11 13:35:42 -0700446 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400447 switch (state) {
448 case Intent.EXTRA_DOCK_STATE_DESK:
449 case Intent.EXTRA_DOCK_STATE_LE_DESK:
450 case Intent.EXTRA_DOCK_STATE_HE_DESK:
451 return true;
452 default:
453 return false;
454 }
455 }
456
Jeff Brown487bb6e2012-10-11 13:35:42 -0700457 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400458 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700459 if (mUiModeLocked) {
460 // no-op, keeps default one
461 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400462 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
463 } else if (mWatch) {
464 uiMode = Configuration.UI_MODE_TYPE_WATCH;
465 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800466 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400467 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800468 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800469 } else if (mVrHeadset) {
470 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800471 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800472
473 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700474 if (mTwilightManager != null) {
475 mTwilightManager.registerListener(mTwilightListener, mHandler);
476 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800477 updateComputedNightModeLocked();
478 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
479 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800480 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700481 if (mTwilightManager != null) {
482 mTwilightManager.unregisterListener(mTwilightListener);
483 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800484 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400485 }
486
Lucas Dupinb1f0c762018-09-18 10:33:38 -0700487 if (mPowerSave) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700488 uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
489 uiMode |= Configuration.UI_MODE_NIGHT_YES;
490 }
491
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400492 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400493 Slog.d(TAG,
494 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400495 + "; mCarMode=" + mCarModeEnabled
496 + "; mNightMode=" + mNightMode
497 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800498 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100499
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800500 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700501 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700502 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700503 }
504 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100505
Jeff Brown487bb6e2012-10-11 13:35:42 -0700506 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700507 if (mSetUiMode != mConfiguration.uiMode) {
508 mSetUiMode = mConfiguration.uiMode;
509
510 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700511 ActivityTaskManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700512 } catch (RemoteException e) {
513 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800514 }
515 }
516 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100517
Adam Lesinski182f73f2013-12-05 16:48:06 -0800518 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700519 String action = null;
520 String oldAction = null;
521 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
522 adjustStatusBarCarModeLocked();
523 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
524 } else if (isDeskDockState(mLastBroadcastState)) {
525 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
526 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100527
Jeff Brown487bb6e2012-10-11 13:35:42 -0700528 if (mCarModeEnabled) {
529 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100530 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700531 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700532 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700533 }
534 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
535 action = UiModeManager.ACTION_ENTER_CAR_MODE;
536 }
537 } else if (isDeskDockState(mDockState)) {
538 if (!isDeskDockState(mLastBroadcastState)) {
539 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700540 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700541 }
542 mLastBroadcastState = mDockState;
543 action = UiModeManager.ACTION_ENTER_DESK_MODE;
544 }
545 } else {
546 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
547 action = oldAction;
548 }
549
550 if (action != null) {
551 if (LOG) {
552 Slog.v(TAG, String.format(
553 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
554 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800555 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100556
Jeff Brown487bb6e2012-10-11 13:35:42 -0700557 // Send the ordered broadcast; the result receiver will receive after all
558 // broadcasts have been sent. If any broadcast receiver changes the result
559 // code from the initial value of RESULT_OK, then the result receiver will
560 // not launch the corresponding dock application. This gives apps a chance
561 // to override the behavior and stay in their app even when the device is
562 // placed into a dock.
563 Intent intent = new Intent(action);
564 intent.putExtra("enableFlags", enableFlags);
565 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700566 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800567 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700568 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100569
Jeff Brown487bb6e2012-10-11 13:35:42 -0700570 // Attempting to make this transition a little more clean, we are going
571 // to hold off on doing a configuration change until we have finished
572 // the broadcast and started the home activity.
573 mHoldingConfiguration = true;
574 updateConfigurationLocked();
575 } else {
576 String category = null;
577 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700578 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700579 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
580 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800581 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400582 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700583 if (ENABLE_LAUNCH_DESK_DOCK_APP
584 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
585 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800586 }
587 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700588 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
589 category = Intent.CATEGORY_HOME;
590 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800591 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100592
Jeff Brown487bb6e2012-10-11 13:35:42 -0700593 if (LOG) {
594 Slog.v(TAG, "updateLocked: null action, mDockState="
595 + mDockState +", category=" + category);
596 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400597
Jeff Brown487bb6e2012-10-11 13:35:42 -0700598 sendConfigurationAndStartDreamOrDockAppLocked(category);
599 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700600
Jeff Brown487bb6e2012-10-11 13:35:42 -0700601 // keep screen on when charging and in car mode
602 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700603 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700604 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700605 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
606 if (keepScreenOn != mWakeLock.isHeld()) {
607 if (keepScreenOn) {
608 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700609 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700610 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800611 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800612 }
613 }
614
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700615 private void sendForegroundBroadcastToAllUsers(String action) {
616 getContext().sendBroadcastAsUser(new Intent(action)
617 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
618 }
619
Jeff Brown62c82e42012-09-26 01:30:41 -0700620 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
621 // Launch a dock activity
622 String category = null;
623 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
624 // Only launch car home when car mode is enabled and the caller
625 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700626 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700627 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
628 category = Intent.CATEGORY_CAR_DOCK;
629 }
630 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
631 // Only launch car home when desk mode is enabled and the caller
632 // has asked us to switch to it. Currently re-using the car
633 // mode flag since we don't have a formal API for "desk mode".
634 if (ENABLE_LAUNCH_DESK_DOCK_APP
635 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
636 category = Intent.CATEGORY_DESK_DOCK;
637 }
638 } else {
639 // Launch the standard home app if requested.
640 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
641 category = Intent.CATEGORY_HOME;
642 }
643 }
644
645 if (LOG) {
646 Slog.v(TAG, String.format(
647 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
648 + "category=%s",
649 action, enableFlags, disableFlags, category));
650 }
651
652 sendConfigurationAndStartDreamOrDockAppLocked(category);
653 }
654
655 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
656 // Update the configuration but don't send it yet.
657 mHoldingConfiguration = false;
658 updateConfigurationLocked();
659
660 // Start the dock app, if there is one.
661 boolean dockAppStarted = false;
662 if (category != null) {
663 // Now we are going to be careful about switching the
664 // configuration and starting the activity -- we need to
665 // do this in a specific order under control of the
666 // activity manager, to do it cleanly. So compute the
667 // new config, but don't set it yet, and let the
668 // activity manager take care of both the start and config
669 // change.
670 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800671 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700672 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700673 int result = ActivityTaskManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800674 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700675 mConfiguration, null, UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -0700676 if (ActivityManager.isStartResultSuccessful(result)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700677 dockAppStarted = true;
678 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
679 Slog.e(TAG, "Could not start dock app: " + homeIntent
680 + ", startActivityWithConfig result " + result);
681 }
682 } catch (RemoteException ex) {
683 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700684 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700685 }
686 }
687
688 // Send the new configuration.
689 sendConfigurationLocked();
690
691 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700692 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800693 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700694 }
695 }
696
Dianne Hackborn7299c412010-03-04 18:41:49 -0800697 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800698 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800699 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700700 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800701 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800702 }
703
Joe Onorato089de882010-04-12 08:18:45 -0700704 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800705 // features of the status bar; these are ORed together to form the
706 // active disabled list. So if (for example) the device is locked and
707 // the status bar should be totally disabled, the calls below will
708 // have no effect until the device is unlocked.
709 if (mStatusBarManager != null) {
710 mStatusBarManager.disable(mCarModeEnabled
711 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
712 : StatusBarManager.DISABLE_NONE);
713 }
714
715 if (mNotificationManager == null) {
716 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800717 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800718 }
719
720 if (mNotificationManager != null) {
721 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800722 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800723
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500724 Notification.Builder n =
725 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400726 .setSmallIcon(R.drawable.stat_notify_car_mode)
727 .setDefaults(Notification.DEFAULT_LIGHTS)
728 .setOngoing(true)
729 .setWhen(0)
730 .setColor(context.getColor(
731 com.android.internal.R.color.system_notification_accent_color))
732 .setContentTitle(
733 context.getString(R.string.car_mode_disable_notification_title))
734 .setContentText(
735 context.getString(R.string.car_mode_disable_notification_message))
736 .setContentIntent(
737 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
738 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700739 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400740 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800741 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700742 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400743 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800744 }
745 }
746 }
747
Jeff Brown2416e092012-08-21 22:12:20 -0700748 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700749 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700750 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700751 if (state != null) {
752 mComputedNightMode = state.isNight();
753 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800754 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800755 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100756
Zak Cohen1a705732017-01-09 12:54:34 -0800757 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800758 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
759 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800760 try {
761 if (vrManager != null) {
762 vrManager.registerListener(mVrStateCallbacks);
763 }
764 } catch (RemoteException e) {
765 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
766 }
767 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100768
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500769 /**
770 * Handles "adb shell" commands.
771 */
772 private static class Shell extends ShellCommand {
773 public static final String NIGHT_MODE_STR_YES = "yes";
774 public static final String NIGHT_MODE_STR_NO = "no";
775 public static final String NIGHT_MODE_STR_AUTO = "auto";
776 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
777 private final IUiModeManager mInterface;
778
779 Shell(IUiModeManager iface) {
780 mInterface = iface;
781 }
782
783 @Override
784 public void onHelp() {
785 final PrintWriter pw = getOutPrintWriter();
786 pw.println("UiModeManager service (uimode) commands:");
787 pw.println(" help");
788 pw.println(" Print this help text.");
789 pw.println(" night [yes|no|auto]");
790 pw.println(" Set or read night mode.");
791 }
792
793 @Override
794 public int onCommand(String cmd) {
795 if (cmd == null) {
796 return handleDefaultCommands(cmd);
797 }
798
799 try {
800 switch (cmd) {
801 case "night":
802 return handleNightMode();
803 default:
804 return handleDefaultCommands(cmd);
805 }
806 } catch (RemoteException e) {
807 final PrintWriter err = getErrPrintWriter();
808 err.println("Remote exception: " + e);
809 }
810 return -1;
811 }
812
813 private int handleNightMode() throws RemoteException {
814 final PrintWriter err = getErrPrintWriter();
815 final String modeStr = getNextArg();
816 if (modeStr == null) {
817 printCurrentNightMode();
818 return 0;
819 }
820
821 final int mode = strToNightMode(modeStr);
822 if (mode >= 0) {
823 mInterface.setNightMode(mode);
824 printCurrentNightMode();
825 return 0;
826 } else {
827 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
828 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
829 return -1;
830 }
831 }
832
833 private void printCurrentNightMode() throws RemoteException {
834 final PrintWriter pw = getOutPrintWriter();
835 final int currMode = mInterface.getNightMode();
836 final String currModeStr = nightModeToStr(currMode);
837 pw.println("Night mode: " + currModeStr);
838 }
839
840 private static String nightModeToStr(int mode) {
841 switch (mode) {
842 case UiModeManager.MODE_NIGHT_YES:
843 return NIGHT_MODE_STR_YES;
844 case UiModeManager.MODE_NIGHT_NO:
845 return NIGHT_MODE_STR_NO;
846 case UiModeManager.MODE_NIGHT_AUTO:
847 return NIGHT_MODE_STR_AUTO;
848 default:
849 return NIGHT_MODE_STR_UNKNOWN;
850 }
851 }
852
853 private static int strToNightMode(String modeStr) {
854 switch (modeStr) {
855 case NIGHT_MODE_STR_YES:
856 return UiModeManager.MODE_NIGHT_YES;
857 case NIGHT_MODE_STR_NO:
858 return UiModeManager.MODE_NIGHT_NO;
859 case NIGHT_MODE_STR_AUTO:
860 return UiModeManager.MODE_NIGHT_AUTO;
861 default:
862 return -1;
863 }
864 }
865 }
Felipe Lemeff9ec382018-09-24 11:07:56 -0700866
867 public final class LocalService extends UiModeManagerInternal {
868
869 @Override
870 public boolean isNightMode() {
871 synchronized (mLock) {
872 final boolean isIt = (mConfiguration.uiMode & Configuration.UI_MODE_NIGHT_YES) != 0;
873 if (LOG) {
874 Slog.d(TAG,
875 "LocalService.isNightMode(): mNightMode=" + mNightMode
876 + "; mComputedNightMode=" + mComputedNightMode
877 + "; uiMode=" + mConfiguration.uiMode
878 + "; isIt=" + isIt);
879 }
880 return isIt;
881 }
882 }
883 }
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700884
885 private final class UserSwitchedReceiver extends BroadcastReceiver {
886 @Override
887 public void onReceive(Context context, Intent intent) {
888 synchronized (mLock) {
889 final int currentId = intent.getIntExtra(
890 Intent.EXTRA_USER_HANDLE, UserHandle.USER_SYSTEM);
891 // only update if the value is actually changed
892 if (updateNightModeFromSettings(context, context.getResources(), currentId)) {
893 updateLocked(0, 0);
894 }
895 }
896 }
897 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800898}