blob: 5538e72703e9148b7c1608e1d2d1069d99b8a698 [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
233 final int defaultNightMode = res.getInteger(
234 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800235 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800236 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700237
Adam Lesinski05199e82015-03-19 14:37:11 -0700238 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800239 SystemServerInitThreadPool.get().submit(() -> {
240 synchronized (mLock) {
241 updateConfigurationLocked();
242 sendConfigurationLocked();
243 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800244
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800245 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800246 publishBinderService(Context.UI_MODE_SERVICE, mService);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700247 publishLocalService(UiModeManagerInternal.class, mLocalService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800248 }
249
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500250 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800251 @Override
252 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700253 if (isUiModeLocked()) {
254 Slog.e(TAG, "enableCarMode while UI mode is locked");
255 return;
256 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800257 final long ident = Binder.clearCallingIdentity();
258 try {
259 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700260 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800261 if (mSystemReady) {
262 updateLocked(flags, 0);
263 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700264 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800265 } finally {
266 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500267 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800268 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800269
Adam Lesinski182f73f2013-12-05 16:48:06 -0800270 @Override
271 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700272 if (isUiModeLocked()) {
273 Slog.e(TAG, "disableCarMode while UI mode is locked");
274 return;
275 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800276 final long ident = Binder.clearCallingIdentity();
277 try {
278 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700279 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800280 if (mSystemReady) {
281 updateLocked(0, flags);
282 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700283 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800284 } finally {
285 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500286 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700287 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100288
Adam Lesinski182f73f2013-12-05 16:48:06 -0800289 @Override
290 public int getCurrentModeType() {
291 final long ident = Binder.clearCallingIdentity();
292 try {
293 synchronized (mLock) {
294 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700295 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800296 } finally {
297 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800298 }
299 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100300
Adam Lesinski182f73f2013-12-05 16:48:06 -0800301 @Override
302 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700303 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
304 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
305 != PackageManager.PERMISSION_GRANTED)) {
306 Slog.e(TAG,
307 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
308 return;
309 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800310 switch (mode) {
311 case UiModeManager.MODE_NIGHT_NO:
312 case UiModeManager.MODE_NIGHT_YES:
313 case UiModeManager.MODE_NIGHT_AUTO:
314 break;
315 default:
316 throw new IllegalArgumentException("Unknown mode: " + mode);
317 }
318
319 final long ident = Binder.clearCallingIdentity();
320 try {
321 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800322 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800323 Settings.Secure.putInt(getContext().getContentResolver(),
324 Settings.Secure.UI_NIGHT_MODE, mode);
325 mNightMode = mode;
326 updateLocked(0, 0);
327 }
328 }
329 } finally {
330 Binder.restoreCallingIdentity(ident);
331 }
332 }
333
334 @Override
335 public int getNightMode() {
336 synchronized (mLock) {
337 return mNightMode;
338 }
339 }
340
341 @Override
keunyounga7710492015-09-23 11:33:58 -0700342 public boolean isUiModeLocked() {
343 synchronized (mLock) {
344 return mUiModeLocked;
345 }
346 }
347
348 @Override
349 public boolean isNightModeLocked() {
350 synchronized (mLock) {
351 return mNightModeLocked;
352 }
353 }
354
355 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500356 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
357 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
358 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
359 }
360
361 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800362 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600363 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800364 dumpImpl(pw);
365 }
366 };
367
368 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700369 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800370 pw.println("Current UI Mode Service state:");
371 pw.print(" mDockState="); pw.print(mDockState);
372 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
Felipe Lemeff9ec382018-09-24 11:07:56 -0700373 pw.print(" mNightMode="); pw.print(mNightMode); pw.print(" (");
374 pw.print(Shell.nightModeToStr(mNightMode)); pw.print(") ");
keunyounga7710492015-09-23 11:33:58 -0700375 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800376 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700377 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700378 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
379 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800380 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700381 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800382 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
383 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
384 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700385 if (mTwilightManager != null) {
386 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700387 pw.print(" mTwilightService.getLastTwilightState()=");
388 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700389 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700390 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800391 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100392
Adam Lesinski182f73f2013-12-05 16:48:06 -0800393 @Override
394 public void onBootPhase(int phase) {
395 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
396 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700397 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800398 mSystemReady = true;
399 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
400 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800401 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800402 updateLocked(0, 0);
403 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800404 }
405 }
406
keunyoung1d0a7cc2014-07-28 13:12:50 -0700407 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800408 if (mCarModeEnabled != enabled) {
409 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800410 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700411 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800412 }
413
Jeff Brown487bb6e2012-10-11 13:35:42 -0700414 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800415 synchronized (mLock) {
416 if (newState != mDockState) {
417 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700418 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800419 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700420 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800421 }
422 }
423 }
424 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500425
Jeff Brown487bb6e2012-10-11 13:35:42 -0700426 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400427 switch (state) {
428 case Intent.EXTRA_DOCK_STATE_DESK:
429 case Intent.EXTRA_DOCK_STATE_LE_DESK:
430 case Intent.EXTRA_DOCK_STATE_HE_DESK:
431 return true;
432 default:
433 return false;
434 }
435 }
436
Jeff Brown487bb6e2012-10-11 13:35:42 -0700437 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400438 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700439 if (mUiModeLocked) {
440 // no-op, keeps default one
441 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400442 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
443 } else if (mWatch) {
444 uiMode = Configuration.UI_MODE_TYPE_WATCH;
445 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800446 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400447 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800448 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800449 } else if (mVrHeadset) {
450 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800451 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800452
453 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700454 if (mTwilightManager != null) {
455 mTwilightManager.registerListener(mTwilightListener, mHandler);
456 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800457 updateComputedNightModeLocked();
458 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
459 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800460 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700461 if (mTwilightManager != null) {
462 mTwilightManager.unregisterListener(mTwilightListener);
463 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800464 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400465 }
466
Lucas Dupinb1f0c762018-09-18 10:33:38 -0700467 if (mPowerSave) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700468 uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
469 uiMode |= Configuration.UI_MODE_NIGHT_YES;
470 }
471
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400472 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400473 Slog.d(TAG,
474 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400475 + "; mCarMode=" + mCarModeEnabled
476 + "; mNightMode=" + mNightMode
477 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800478 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100479
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800480 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700481 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700482 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700483 }
484 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100485
Jeff Brown487bb6e2012-10-11 13:35:42 -0700486 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700487 if (mSetUiMode != mConfiguration.uiMode) {
488 mSetUiMode = mConfiguration.uiMode;
489
490 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700491 ActivityTaskManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700492 } catch (RemoteException e) {
493 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800494 }
495 }
496 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100497
Adam Lesinski182f73f2013-12-05 16:48:06 -0800498 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700499 String action = null;
500 String oldAction = null;
501 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
502 adjustStatusBarCarModeLocked();
503 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
504 } else if (isDeskDockState(mLastBroadcastState)) {
505 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
506 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100507
Jeff Brown487bb6e2012-10-11 13:35:42 -0700508 if (mCarModeEnabled) {
509 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100510 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700511 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700512 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700513 }
514 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
515 action = UiModeManager.ACTION_ENTER_CAR_MODE;
516 }
517 } else if (isDeskDockState(mDockState)) {
518 if (!isDeskDockState(mLastBroadcastState)) {
519 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700520 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700521 }
522 mLastBroadcastState = mDockState;
523 action = UiModeManager.ACTION_ENTER_DESK_MODE;
524 }
525 } else {
526 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
527 action = oldAction;
528 }
529
530 if (action != null) {
531 if (LOG) {
532 Slog.v(TAG, String.format(
533 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
534 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800535 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100536
Jeff Brown487bb6e2012-10-11 13:35:42 -0700537 // Send the ordered broadcast; the result receiver will receive after all
538 // broadcasts have been sent. If any broadcast receiver changes the result
539 // code from the initial value of RESULT_OK, then the result receiver will
540 // not launch the corresponding dock application. This gives apps a chance
541 // to override the behavior and stay in their app even when the device is
542 // placed into a dock.
543 Intent intent = new Intent(action);
544 intent.putExtra("enableFlags", enableFlags);
545 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700546 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800547 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700548 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100549
Jeff Brown487bb6e2012-10-11 13:35:42 -0700550 // Attempting to make this transition a little more clean, we are going
551 // to hold off on doing a configuration change until we have finished
552 // the broadcast and started the home activity.
553 mHoldingConfiguration = true;
554 updateConfigurationLocked();
555 } else {
556 String category = null;
557 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700558 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700559 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
560 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800561 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400562 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700563 if (ENABLE_LAUNCH_DESK_DOCK_APP
564 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
565 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800566 }
567 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700568 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
569 category = Intent.CATEGORY_HOME;
570 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800571 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100572
Jeff Brown487bb6e2012-10-11 13:35:42 -0700573 if (LOG) {
574 Slog.v(TAG, "updateLocked: null action, mDockState="
575 + mDockState +", category=" + category);
576 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400577
Jeff Brown487bb6e2012-10-11 13:35:42 -0700578 sendConfigurationAndStartDreamOrDockAppLocked(category);
579 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700580
Jeff Brown487bb6e2012-10-11 13:35:42 -0700581 // keep screen on when charging and in car mode
582 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700583 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700584 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700585 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
586 if (keepScreenOn != mWakeLock.isHeld()) {
587 if (keepScreenOn) {
588 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700589 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700590 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800591 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800592 }
593 }
594
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700595 private void sendForegroundBroadcastToAllUsers(String action) {
596 getContext().sendBroadcastAsUser(new Intent(action)
597 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
598 }
599
Jeff Brown62c82e42012-09-26 01:30:41 -0700600 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
601 // Launch a dock activity
602 String category = null;
603 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
604 // Only launch car home when car mode is enabled and the caller
605 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700606 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700607 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
608 category = Intent.CATEGORY_CAR_DOCK;
609 }
610 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
611 // Only launch car home when desk mode is enabled and the caller
612 // has asked us to switch to it. Currently re-using the car
613 // mode flag since we don't have a formal API for "desk mode".
614 if (ENABLE_LAUNCH_DESK_DOCK_APP
615 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
616 category = Intent.CATEGORY_DESK_DOCK;
617 }
618 } else {
619 // Launch the standard home app if requested.
620 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
621 category = Intent.CATEGORY_HOME;
622 }
623 }
624
625 if (LOG) {
626 Slog.v(TAG, String.format(
627 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
628 + "category=%s",
629 action, enableFlags, disableFlags, category));
630 }
631
632 sendConfigurationAndStartDreamOrDockAppLocked(category);
633 }
634
635 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
636 // Update the configuration but don't send it yet.
637 mHoldingConfiguration = false;
638 updateConfigurationLocked();
639
640 // Start the dock app, if there is one.
641 boolean dockAppStarted = false;
642 if (category != null) {
643 // Now we are going to be careful about switching the
644 // configuration and starting the activity -- we need to
645 // do this in a specific order under control of the
646 // activity manager, to do it cleanly. So compute the
647 // new config, but don't set it yet, and let the
648 // activity manager take care of both the start and config
649 // change.
650 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800651 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700652 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700653 int result = ActivityTaskManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800654 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700655 mConfiguration, null, UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -0700656 if (ActivityManager.isStartResultSuccessful(result)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700657 dockAppStarted = true;
658 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
659 Slog.e(TAG, "Could not start dock app: " + homeIntent
660 + ", startActivityWithConfig result " + result);
661 }
662 } catch (RemoteException ex) {
663 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700664 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700665 }
666 }
667
668 // Send the new configuration.
669 sendConfigurationLocked();
670
671 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700672 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800673 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700674 }
675 }
676
Dianne Hackborn7299c412010-03-04 18:41:49 -0800677 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800678 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800679 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700680 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800681 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800682 }
683
Joe Onorato089de882010-04-12 08:18:45 -0700684 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800685 // features of the status bar; these are ORed together to form the
686 // active disabled list. So if (for example) the device is locked and
687 // the status bar should be totally disabled, the calls below will
688 // have no effect until the device is unlocked.
689 if (mStatusBarManager != null) {
690 mStatusBarManager.disable(mCarModeEnabled
691 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
692 : StatusBarManager.DISABLE_NONE);
693 }
694
695 if (mNotificationManager == null) {
696 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800697 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800698 }
699
700 if (mNotificationManager != null) {
701 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800702 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800703
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500704 Notification.Builder n =
705 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400706 .setSmallIcon(R.drawable.stat_notify_car_mode)
707 .setDefaults(Notification.DEFAULT_LIGHTS)
708 .setOngoing(true)
709 .setWhen(0)
710 .setColor(context.getColor(
711 com.android.internal.R.color.system_notification_accent_color))
712 .setContentTitle(
713 context.getString(R.string.car_mode_disable_notification_title))
714 .setContentText(
715 context.getString(R.string.car_mode_disable_notification_message))
716 .setContentIntent(
717 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
718 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700719 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400720 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800721 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700722 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400723 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800724 }
725 }
726 }
727
Jeff Brown2416e092012-08-21 22:12:20 -0700728 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700729 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700730 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700731 if (state != null) {
732 mComputedNightMode = state.isNight();
733 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800734 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800735 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100736
Zak Cohen1a705732017-01-09 12:54:34 -0800737 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800738 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
739 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800740 try {
741 if (vrManager != null) {
742 vrManager.registerListener(mVrStateCallbacks);
743 }
744 } catch (RemoteException e) {
745 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
746 }
747 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100748
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500749 /**
750 * Handles "adb shell" commands.
751 */
752 private static class Shell extends ShellCommand {
753 public static final String NIGHT_MODE_STR_YES = "yes";
754 public static final String NIGHT_MODE_STR_NO = "no";
755 public static final String NIGHT_MODE_STR_AUTO = "auto";
756 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
757 private final IUiModeManager mInterface;
758
759 Shell(IUiModeManager iface) {
760 mInterface = iface;
761 }
762
763 @Override
764 public void onHelp() {
765 final PrintWriter pw = getOutPrintWriter();
766 pw.println("UiModeManager service (uimode) commands:");
767 pw.println(" help");
768 pw.println(" Print this help text.");
769 pw.println(" night [yes|no|auto]");
770 pw.println(" Set or read night mode.");
771 }
772
773 @Override
774 public int onCommand(String cmd) {
775 if (cmd == null) {
776 return handleDefaultCommands(cmd);
777 }
778
779 try {
780 switch (cmd) {
781 case "night":
782 return handleNightMode();
783 default:
784 return handleDefaultCommands(cmd);
785 }
786 } catch (RemoteException e) {
787 final PrintWriter err = getErrPrintWriter();
788 err.println("Remote exception: " + e);
789 }
790 return -1;
791 }
792
793 private int handleNightMode() throws RemoteException {
794 final PrintWriter err = getErrPrintWriter();
795 final String modeStr = getNextArg();
796 if (modeStr == null) {
797 printCurrentNightMode();
798 return 0;
799 }
800
801 final int mode = strToNightMode(modeStr);
802 if (mode >= 0) {
803 mInterface.setNightMode(mode);
804 printCurrentNightMode();
805 return 0;
806 } else {
807 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
808 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
809 return -1;
810 }
811 }
812
813 private void printCurrentNightMode() throws RemoteException {
814 final PrintWriter pw = getOutPrintWriter();
815 final int currMode = mInterface.getNightMode();
816 final String currModeStr = nightModeToStr(currMode);
817 pw.println("Night mode: " + currModeStr);
818 }
819
820 private static String nightModeToStr(int mode) {
821 switch (mode) {
822 case UiModeManager.MODE_NIGHT_YES:
823 return NIGHT_MODE_STR_YES;
824 case UiModeManager.MODE_NIGHT_NO:
825 return NIGHT_MODE_STR_NO;
826 case UiModeManager.MODE_NIGHT_AUTO:
827 return NIGHT_MODE_STR_AUTO;
828 default:
829 return NIGHT_MODE_STR_UNKNOWN;
830 }
831 }
832
833 private static int strToNightMode(String modeStr) {
834 switch (modeStr) {
835 case NIGHT_MODE_STR_YES:
836 return UiModeManager.MODE_NIGHT_YES;
837 case NIGHT_MODE_STR_NO:
838 return UiModeManager.MODE_NIGHT_NO;
839 case NIGHT_MODE_STR_AUTO:
840 return UiModeManager.MODE_NIGHT_AUTO;
841 default:
842 return -1;
843 }
844 }
845 }
Felipe Lemeff9ec382018-09-24 11:07:56 -0700846
847 public final class LocalService extends UiModeManagerInternal {
848
849 @Override
850 public boolean isNightMode() {
851 synchronized (mLock) {
852 final boolean isIt = (mConfiguration.uiMode & Configuration.UI_MODE_NIGHT_YES) != 0;
853 if (LOG) {
854 Slog.d(TAG,
855 "LocalService.isNightMode(): mNightMode=" + mNightMode
856 + "; mComputedNightMode=" + mComputedNightMode
857 + "; uiMode=" + mConfiguration.uiMode
858 + "; isIt=" + isIt);
859 }
860 return isIt;
861 }
862 }
863 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800864}