blob: 6b57fcd3145099805f18d651ef520a76a24ed33e [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;
Kweku Adams4db6a3c2019-02-04 16:06:13 -080040import android.os.PowerManager.ServiceType;
41import android.os.PowerManagerInternal;
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;
Lucas Dupin53c6e292018-07-12 18:42:53 -0700166 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500167 synchronized (mLock) {
168 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700169 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500170 }
171 }
172 }
173 };
174
Adam Lesinski182f73f2013-12-05 16:48:06 -0800175 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700176 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700177 public void onTwilightStateChanged(@Nullable TwilightState state) {
178 synchronized (mLock) {
179 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
180 updateComputedNightModeLocked();
181 updateLocked(0, 0);
182 }
183 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700184 }
185 };
186
Zak Cohen1a705732017-01-09 12:54:34 -0800187 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
188 @Override
189 public void onVrStateChanged(boolean enabled) {
190 synchronized (mLock) {
191 mVrHeadset = enabled;
192 if (mSystemReady) {
193 updateLocked(0, 0);
194 }
195 }
196 }
197 };
198
Adam Lesinski182f73f2013-12-05 16:48:06 -0800199 @Override
200 public void onStart() {
201 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800202
Adam Lesinski182f73f2013-12-05 16:48:06 -0800203 final PowerManager powerManager =
204 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
205 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800206
Adam Lesinski182f73f2013-12-05 16:48:06 -0800207 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800208 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Lucas Dupin53c6e292018-07-12 18:42:53 -0700209 IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Lucas Dupin53c6e292018-07-12 18:42:53 -0700210 context.registerReceiver(mBatteryReceiver, batteryFilter);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500211
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800212 PowerManagerInternal localPowerManager =
213 LocalServices.getService(PowerManagerInternal.class);
214 mPowerSave = localPowerManager.getLowPowerState(ServiceType.NIGHT_MODE).batterySaverEnabled;
215 localPowerManager.registerLowPowerModeObserver(ServiceType.NIGHT_MODE,
216 state -> {
217 synchronized (mLock) {
218 if (mPowerSave == state.batterySaverEnabled) {
219 return;
220 }
221 mPowerSave = state.batterySaverEnabled;
222 if (mSystemReady) {
223 updateLocked(0, 0);
224 }
225 }
226 });
227
Dianne Hackborn7299c412010-03-04 18:41:49 -0800228 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500229
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800230 final Resources res = context.getResources();
231 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800232 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800233 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500234 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800235 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500236 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700237 mEnableCarDockLaunch = res.getBoolean(
238 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
239 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
240 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700241
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800242 final PackageManager pm = context.getPackageManager();
243 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
244 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
245 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
246
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700247 updateNightModeFromSettings(context, res, UserHandle.getCallingUserId());
Jeff Brown2416e092012-08-21 22:12:20 -0700248
Adam Lesinski05199e82015-03-19 14:37:11 -0700249 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800250 SystemServerInitThreadPool.get().submit(() -> {
251 synchronized (mLock) {
252 updateConfigurationLocked();
253 sendConfigurationLocked();
254 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800255
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800256 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800257 publishBinderService(Context.UI_MODE_SERVICE, mService);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700258 publishLocalService(UiModeManagerInternal.class, mLocalService);
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700259
260 IntentFilter filter = new IntentFilter();
261 filter.addAction(Intent.ACTION_USER_SWITCHED);
262 context.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler);
263 }
264
265 /**
266 * Updates the night mode setting in Settings.Global and returns if the value was successfully
267 * changed.
268 * @param context A valid context
269 * @param res A valid resource object
270 * @param userId The user to update the setting for
271 * @return True if the new value is different from the old value. False otherwise.
272 */
273 private boolean updateNightModeFromSettings(Context context, Resources res, int userId) {
274 final int defaultNightMode = res.getInteger(
275 com.android.internal.R.integer.config_defaultNightMode);
276 int oldNightMode = mNightMode;
277 mNightMode = Settings.Secure.getIntForUser(context.getContentResolver(),
278 Settings.Secure.UI_NIGHT_MODE, defaultNightMode, userId);
279
280 // false if night mode stayed the same, true otherwise.
281 return !(oldNightMode == mNightMode);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800282 }
283
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500284 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800285 @Override
286 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700287 if (isUiModeLocked()) {
288 Slog.e(TAG, "enableCarMode while UI mode is locked");
289 return;
290 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800291 final long ident = Binder.clearCallingIdentity();
292 try {
293 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700294 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800295 if (mSystemReady) {
296 updateLocked(flags, 0);
297 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700298 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800299 } finally {
300 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500301 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800302 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800303
Adam Lesinski182f73f2013-12-05 16:48:06 -0800304 @Override
305 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700306 if (isUiModeLocked()) {
307 Slog.e(TAG, "disableCarMode while UI mode is locked");
308 return;
309 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800310 final long ident = Binder.clearCallingIdentity();
311 try {
312 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700313 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800314 if (mSystemReady) {
315 updateLocked(0, flags);
316 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700317 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800318 } finally {
319 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500320 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700321 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100322
Adam Lesinski182f73f2013-12-05 16:48:06 -0800323 @Override
324 public int getCurrentModeType() {
325 final long ident = Binder.clearCallingIdentity();
326 try {
327 synchronized (mLock) {
328 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700329 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800330 } finally {
331 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800332 }
333 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100334
Adam Lesinski182f73f2013-12-05 16:48:06 -0800335 @Override
336 public void setNightMode(int mode) {
Lucas Dupinc5aaf452018-09-20 16:10:07 -0700337 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
keunyounga7710492015-09-23 11:33:58 -0700338 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
339 != PackageManager.PERMISSION_GRANTED)) {
Lucas Dupinc5aaf452018-09-20 16:10:07 -0700340 Slog.e(TAG, "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
keunyounga7710492015-09-23 11:33:58 -0700341 return;
342 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800343 switch (mode) {
344 case UiModeManager.MODE_NIGHT_NO:
345 case UiModeManager.MODE_NIGHT_YES:
346 case UiModeManager.MODE_NIGHT_AUTO:
347 break;
348 default:
349 throw new IllegalArgumentException("Unknown mode: " + mode);
350 }
351
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700352 final int user = UserHandle.getCallingUserId();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800353 final long ident = Binder.clearCallingIdentity();
354 try {
355 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800356 if (mNightMode != mode) {
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700357 Settings.Secure.putIntForUser(getContext().getContentResolver(),
358 Settings.Secure.UI_NIGHT_MODE, mode, user);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800359 mNightMode = mode;
360 updateLocked(0, 0);
361 }
362 }
363 } finally {
364 Binder.restoreCallingIdentity(ident);
365 }
366 }
367
368 @Override
369 public int getNightMode() {
370 synchronized (mLock) {
371 return mNightMode;
372 }
373 }
374
375 @Override
keunyounga7710492015-09-23 11:33:58 -0700376 public boolean isUiModeLocked() {
377 synchronized (mLock) {
378 return mUiModeLocked;
379 }
380 }
381
382 @Override
383 public boolean isNightModeLocked() {
384 synchronized (mLock) {
385 return mNightModeLocked;
386 }
387 }
388
389 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500390 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
391 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
392 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
393 }
394
395 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800396 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600397 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800398 dumpImpl(pw);
399 }
400 };
401
402 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700403 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800404 pw.println("Current UI Mode Service state:");
405 pw.print(" mDockState="); pw.print(mDockState);
406 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700407 pw.print(" mNightMode="); pw.print(mNightMode); pw.print(" (");
408 pw.print(Shell.nightModeToStr(mNightMode)); pw.print(") ");
keunyounga7710492015-09-23 11:33:58 -0700409 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800410 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700411 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700412 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
413 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800414 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700415 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800416 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
417 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
418 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700419 if (mTwilightManager != null) {
420 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700421 pw.print(" mTwilightService.getLastTwilightState()=");
422 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700423 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700424 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800425 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100426
Adam Lesinski182f73f2013-12-05 16:48:06 -0800427 @Override
428 public void onBootPhase(int phase) {
429 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
430 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700431 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800432 mSystemReady = true;
433 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
434 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800435 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800436 updateLocked(0, 0);
437 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800438 }
439 }
440
keunyoung1d0a7cc2014-07-28 13:12:50 -0700441 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800442 if (mCarModeEnabled != enabled) {
443 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800444 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700445 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800446 }
447
Jeff Brown487bb6e2012-10-11 13:35:42 -0700448 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800449 synchronized (mLock) {
450 if (newState != mDockState) {
451 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700452 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800453 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700454 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800455 }
456 }
457 }
458 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500459
Jeff Brown487bb6e2012-10-11 13:35:42 -0700460 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400461 switch (state) {
462 case Intent.EXTRA_DOCK_STATE_DESK:
463 case Intent.EXTRA_DOCK_STATE_LE_DESK:
464 case Intent.EXTRA_DOCK_STATE_HE_DESK:
465 return true;
466 default:
467 return false;
468 }
469 }
470
Jeff Brown487bb6e2012-10-11 13:35:42 -0700471 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400472 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700473 if (mUiModeLocked) {
474 // no-op, keeps default one
475 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400476 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
477 } else if (mWatch) {
478 uiMode = Configuration.UI_MODE_TYPE_WATCH;
479 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800480 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400481 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800482 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800483 } else if (mVrHeadset) {
484 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800485 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800486
487 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700488 if (mTwilightManager != null) {
489 mTwilightManager.registerListener(mTwilightListener, mHandler);
490 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800491 updateComputedNightModeLocked();
492 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
493 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800494 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700495 if (mTwilightManager != null) {
496 mTwilightManager.unregisterListener(mTwilightListener);
497 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800498 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400499 }
500
Lucas Dupinb1f0c762018-09-18 10:33:38 -0700501 if (mPowerSave) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700502 uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
503 uiMode |= Configuration.UI_MODE_NIGHT_YES;
504 }
505
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400506 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400507 Slog.d(TAG,
508 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400509 + "; mCarMode=" + mCarModeEnabled
510 + "; mNightMode=" + mNightMode
511 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800512 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100513
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800514 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700515 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700516 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700517 }
518 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100519
Jeff Brown487bb6e2012-10-11 13:35:42 -0700520 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700521 if (mSetUiMode != mConfiguration.uiMode) {
522 mSetUiMode = mConfiguration.uiMode;
523
524 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700525 ActivityTaskManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700526 } catch (RemoteException e) {
527 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800528 }
529 }
530 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100531
Adam Lesinski182f73f2013-12-05 16:48:06 -0800532 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700533 String action = null;
534 String oldAction = null;
535 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
536 adjustStatusBarCarModeLocked();
537 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
538 } else if (isDeskDockState(mLastBroadcastState)) {
539 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
540 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100541
Jeff Brown487bb6e2012-10-11 13:35:42 -0700542 if (mCarModeEnabled) {
543 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100544 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700545 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700546 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700547 }
548 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
549 action = UiModeManager.ACTION_ENTER_CAR_MODE;
550 }
551 } else if (isDeskDockState(mDockState)) {
552 if (!isDeskDockState(mLastBroadcastState)) {
553 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700554 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700555 }
556 mLastBroadcastState = mDockState;
557 action = UiModeManager.ACTION_ENTER_DESK_MODE;
558 }
559 } else {
560 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
561 action = oldAction;
562 }
563
564 if (action != null) {
565 if (LOG) {
566 Slog.v(TAG, String.format(
567 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
568 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800569 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100570
Jeff Brown487bb6e2012-10-11 13:35:42 -0700571 // Send the ordered broadcast; the result receiver will receive after all
572 // broadcasts have been sent. If any broadcast receiver changes the result
573 // code from the initial value of RESULT_OK, then the result receiver will
574 // not launch the corresponding dock application. This gives apps a chance
575 // to override the behavior and stay in their app even when the device is
576 // placed into a dock.
577 Intent intent = new Intent(action);
578 intent.putExtra("enableFlags", enableFlags);
579 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700580 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800581 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700582 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100583
Jeff Brown487bb6e2012-10-11 13:35:42 -0700584 // Attempting to make this transition a little more clean, we are going
585 // to hold off on doing a configuration change until we have finished
586 // the broadcast and started the home activity.
587 mHoldingConfiguration = true;
588 updateConfigurationLocked();
589 } else {
590 String category = null;
591 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700592 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700593 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
594 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800595 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400596 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700597 if (ENABLE_LAUNCH_DESK_DOCK_APP
598 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
599 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800600 }
601 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700602 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
603 category = Intent.CATEGORY_HOME;
604 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800605 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100606
Jeff Brown487bb6e2012-10-11 13:35:42 -0700607 if (LOG) {
608 Slog.v(TAG, "updateLocked: null action, mDockState="
609 + mDockState +", category=" + category);
610 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400611
Jeff Brown487bb6e2012-10-11 13:35:42 -0700612 sendConfigurationAndStartDreamOrDockAppLocked(category);
613 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700614
Jeff Brown487bb6e2012-10-11 13:35:42 -0700615 // keep screen on when charging and in car mode
616 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700617 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700618 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700619 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
620 if (keepScreenOn != mWakeLock.isHeld()) {
621 if (keepScreenOn) {
622 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700623 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700624 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800625 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800626 }
627 }
628
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700629 private void sendForegroundBroadcastToAllUsers(String action) {
630 getContext().sendBroadcastAsUser(new Intent(action)
631 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
632 }
633
Jeff Brown62c82e42012-09-26 01:30:41 -0700634 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
635 // Launch a dock activity
636 String category = null;
637 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
638 // Only launch car home when car mode is enabled and the caller
639 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700640 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700641 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
642 category = Intent.CATEGORY_CAR_DOCK;
643 }
644 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
645 // Only launch car home when desk mode is enabled and the caller
646 // has asked us to switch to it. Currently re-using the car
647 // mode flag since we don't have a formal API for "desk mode".
648 if (ENABLE_LAUNCH_DESK_DOCK_APP
649 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
650 category = Intent.CATEGORY_DESK_DOCK;
651 }
652 } else {
653 // Launch the standard home app if requested.
654 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
655 category = Intent.CATEGORY_HOME;
656 }
657 }
658
659 if (LOG) {
660 Slog.v(TAG, String.format(
661 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
662 + "category=%s",
663 action, enableFlags, disableFlags, category));
664 }
665
666 sendConfigurationAndStartDreamOrDockAppLocked(category);
667 }
668
669 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
670 // Update the configuration but don't send it yet.
671 mHoldingConfiguration = false;
672 updateConfigurationLocked();
673
674 // Start the dock app, if there is one.
675 boolean dockAppStarted = false;
676 if (category != null) {
677 // Now we are going to be careful about switching the
678 // configuration and starting the activity -- we need to
679 // do this in a specific order under control of the
680 // activity manager, to do it cleanly. So compute the
681 // new config, but don't set it yet, and let the
682 // activity manager take care of both the start and config
683 // change.
684 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800685 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700686 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700687 int result = ActivityTaskManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800688 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700689 mConfiguration, null, UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -0700690 if (ActivityManager.isStartResultSuccessful(result)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700691 dockAppStarted = true;
692 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
693 Slog.e(TAG, "Could not start dock app: " + homeIntent
694 + ", startActivityWithConfig result " + result);
695 }
696 } catch (RemoteException ex) {
697 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700698 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700699 }
700 }
701
702 // Send the new configuration.
703 sendConfigurationLocked();
704
705 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700706 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800707 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700708 }
709 }
710
Dianne Hackborn7299c412010-03-04 18:41:49 -0800711 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800712 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800713 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700714 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800715 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800716 }
717
Joe Onorato089de882010-04-12 08:18:45 -0700718 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800719 // features of the status bar; these are ORed together to form the
720 // active disabled list. So if (for example) the device is locked and
721 // the status bar should be totally disabled, the calls below will
722 // have no effect until the device is unlocked.
723 if (mStatusBarManager != null) {
724 mStatusBarManager.disable(mCarModeEnabled
725 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
726 : StatusBarManager.DISABLE_NONE);
727 }
728
729 if (mNotificationManager == null) {
730 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800731 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800732 }
733
734 if (mNotificationManager != null) {
735 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800736 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800737
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500738 Notification.Builder n =
739 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400740 .setSmallIcon(R.drawable.stat_notify_car_mode)
741 .setDefaults(Notification.DEFAULT_LIGHTS)
742 .setOngoing(true)
743 .setWhen(0)
744 .setColor(context.getColor(
745 com.android.internal.R.color.system_notification_accent_color))
746 .setContentTitle(
747 context.getString(R.string.car_mode_disable_notification_title))
748 .setContentText(
749 context.getString(R.string.car_mode_disable_notification_message))
750 .setContentIntent(
751 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
752 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700753 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400754 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800755 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700756 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400757 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800758 }
759 }
760 }
761
Jeff Brown2416e092012-08-21 22:12:20 -0700762 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700763 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700764 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700765 if (state != null) {
766 mComputedNightMode = state.isNight();
767 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800768 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800769 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100770
Zak Cohen1a705732017-01-09 12:54:34 -0800771 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800772 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
773 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800774 try {
775 if (vrManager != null) {
776 vrManager.registerListener(mVrStateCallbacks);
777 }
778 } catch (RemoteException e) {
779 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
780 }
781 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100782
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500783 /**
784 * Handles "adb shell" commands.
785 */
786 private static class Shell extends ShellCommand {
787 public static final String NIGHT_MODE_STR_YES = "yes";
788 public static final String NIGHT_MODE_STR_NO = "no";
789 public static final String NIGHT_MODE_STR_AUTO = "auto";
790 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
791 private final IUiModeManager mInterface;
792
793 Shell(IUiModeManager iface) {
794 mInterface = iface;
795 }
796
797 @Override
798 public void onHelp() {
799 final PrintWriter pw = getOutPrintWriter();
800 pw.println("UiModeManager service (uimode) commands:");
801 pw.println(" help");
802 pw.println(" Print this help text.");
803 pw.println(" night [yes|no|auto]");
804 pw.println(" Set or read night mode.");
805 }
806
807 @Override
808 public int onCommand(String cmd) {
809 if (cmd == null) {
810 return handleDefaultCommands(cmd);
811 }
812
813 try {
814 switch (cmd) {
815 case "night":
816 return handleNightMode();
817 default:
818 return handleDefaultCommands(cmd);
819 }
820 } catch (RemoteException e) {
821 final PrintWriter err = getErrPrintWriter();
822 err.println("Remote exception: " + e);
823 }
824 return -1;
825 }
826
827 private int handleNightMode() throws RemoteException {
828 final PrintWriter err = getErrPrintWriter();
829 final String modeStr = getNextArg();
830 if (modeStr == null) {
831 printCurrentNightMode();
832 return 0;
833 }
834
835 final int mode = strToNightMode(modeStr);
836 if (mode >= 0) {
837 mInterface.setNightMode(mode);
838 printCurrentNightMode();
839 return 0;
840 } else {
841 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
842 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
843 return -1;
844 }
845 }
846
847 private void printCurrentNightMode() throws RemoteException {
848 final PrintWriter pw = getOutPrintWriter();
849 final int currMode = mInterface.getNightMode();
850 final String currModeStr = nightModeToStr(currMode);
851 pw.println("Night mode: " + currModeStr);
852 }
853
854 private static String nightModeToStr(int mode) {
855 switch (mode) {
856 case UiModeManager.MODE_NIGHT_YES:
857 return NIGHT_MODE_STR_YES;
858 case UiModeManager.MODE_NIGHT_NO:
859 return NIGHT_MODE_STR_NO;
860 case UiModeManager.MODE_NIGHT_AUTO:
861 return NIGHT_MODE_STR_AUTO;
862 default:
863 return NIGHT_MODE_STR_UNKNOWN;
864 }
865 }
866
867 private static int strToNightMode(String modeStr) {
868 switch (modeStr) {
869 case NIGHT_MODE_STR_YES:
870 return UiModeManager.MODE_NIGHT_YES;
871 case NIGHT_MODE_STR_NO:
872 return UiModeManager.MODE_NIGHT_NO;
873 case NIGHT_MODE_STR_AUTO:
874 return UiModeManager.MODE_NIGHT_AUTO;
875 default:
876 return -1;
877 }
878 }
879 }
Felipe Lemeff9ec382018-09-24 11:07:56 -0700880
881 public final class LocalService extends UiModeManagerInternal {
882
883 @Override
884 public boolean isNightMode() {
885 synchronized (mLock) {
886 final boolean isIt = (mConfiguration.uiMode & Configuration.UI_MODE_NIGHT_YES) != 0;
887 if (LOG) {
888 Slog.d(TAG,
889 "LocalService.isNightMode(): mNightMode=" + mNightMode
890 + "; mComputedNightMode=" + mComputedNightMode
891 + "; uiMode=" + mConfiguration.uiMode
892 + "; isIt=" + isIt);
893 }
894 return isIt;
895 }
896 }
897 }
Salvador Martinez2f792ef2018-09-26 17:33:51 -0700898
899 private final class UserSwitchedReceiver extends BroadcastReceiver {
900 @Override
901 public void onReceive(Context context, Intent intent) {
902 synchronized (mLock) {
903 final int currentId = intent.getIntExtra(
904 Intent.EXTRA_USER_HANDLE, UserHandle.USER_SYSTEM);
905 // only update if the value is actually changed
906 if (updateNightModeFromSettings(context, context.getResources(), currentId)) {
907 updateLocked(0, 0);
908 }
909 }
910 }
911 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800912}