blob: cb03255923cf6aa1ccd4e47ac7fa0494a898e9ad [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;
Jason Monk97a06a12016-11-09 15:53:30 -050045import android.os.SystemProperties;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070046import android.os.UserHandle;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070047import android.provider.Settings;
Jeff Brown11159e92012-10-11 15:58:37 -070048import android.service.dreams.Sandman;
Zak Cohen1a705732017-01-09 12:54:34 -080049import android.service.vr.IVrManager;
50import android.service.vr.IVrStateCallbacks;
Jason Monk524fb402017-01-25 10:33:31 -050051import android.text.TextUtils;
Dianne Hackborn7299c412010-03-04 18:41:49 -080052import android.util.Slog;
53
Jason Monk524fb402017-01-25 10:33:31 -050054import java.io.File;
Dianne Hackborn7299c412010-03-04 18:41:49 -080055import java.io.FileDescriptor;
56import java.io.PrintWriter;
Jason Monk524fb402017-01-25 10:33:31 -050057import java.util.ArrayList;
58import java.util.Collections;
Dianne Hackborn7299c412010-03-04 18:41:49 -080059
60import com.android.internal.R;
61import com.android.internal.app.DisableCarModeActivity;
Chris Wren282cfef2017-03-27 15:01:44 -040062import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050063import com.android.internal.notification.SystemNotificationChannels;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060064import com.android.internal.util.DumpUtils;
Jason Monk97a06a12016-11-09 15:53:30 -050065import com.android.server.power.ShutdownThread;
Adam Lesinski182f73f2013-12-05 16:48:06 -080066import com.android.server.twilight.TwilightListener;
67import com.android.server.twilight.TwilightManager;
68import com.android.server.twilight.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080069
Adam Lesinski182f73f2013-12-05 16:48:06 -080070final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080071 private static final String TAG = UiModeManager.class.getSimpleName();
72 private static final boolean LOG = false;
73
Daniel Sandler11ddf532011-11-16 11:10:03 -080074 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040075 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080076
Dianne Hackborn7299c412010-03-04 18:41:49 -080077 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080078 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010079
Adam Lesinski182f73f2013-12-05 16:48:06 -080080 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080081 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080082
Dianne Hackborn7299c412010-03-04 18:41:49 -080083 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050084 private boolean mCharging = false;
Lucas Dupin53c6e292018-07-12 18:42:53 -070085 private boolean mPowerSave = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080086 private int mDefaultUiModeType;
87 private boolean mCarModeKeepsScreenOn;
88 private boolean mDeskModeKeepsScreenOn;
89 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040090 private boolean mWatch;
Zak Cohen1a705732017-01-09 12:54:34 -080091 private boolean mVrHeadset;
Dianne Hackborn7299c412010-03-04 18:41:49 -080092 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070093 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080094
keunyounga7710492015-09-23 11:33:58 -070095 // flag set by resource, whether to enable Car dock launch when starting car mode.
96 private boolean mEnableCarDockLaunch = true;
97 // flag set by resource, whether to lock UI mode to the default one or not.
98 private boolean mUiModeLocked = false;
99 // flag set by resource, whether to night mode change for normal all or not.
100 private boolean mNightModeLocked = false;
101
Adam Lesinski182f73f2013-12-05 16:48:06 -0800102 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800103 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800104 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800105
Dianne Hackborn7299c412010-03-04 18:41:49 -0800106 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800107 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100108
Adam Lesinski182f73f2013-12-05 16:48:06 -0800109 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800110
Adam Lesinski182f73f2013-12-05 16:48:06 -0800111 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800112 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800113 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700114
Adam Lesinski182f73f2013-12-05 16:48:06 -0800115 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800116
Jeff Brownb880d882014-02-10 19:47:07 -0800117 public UiModeManagerService(Context context) {
118 super(context);
119 }
120
Adam Lesinski182f73f2013-12-05 16:48:06 -0800121 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700122 Intent intent = new Intent(Intent.ACTION_MAIN);
123 intent.addCategory(category);
124 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
125 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
126 return intent;
127 }
John Spurlock960779d2012-05-29 14:37:05 -0400128
Dianne Hackborn7299c412010-03-04 18:41:49 -0800129 // The broadcast receiver which receives the result of the ordered broadcast sent when
130 // the dock state changes. The original ordered broadcast is sent with an initial result
131 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
132 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
133 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
134 @Override
135 public void onReceive(Context context, Intent intent) {
136 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400137 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400138 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400139 + ": canceled: " + getResultCode());
140 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800141 return;
142 }
143
Jeff Brown62c82e42012-09-26 01:30:41 -0700144 final int enableFlags = intent.getIntExtra("enableFlags", 0);
145 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800146 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700147 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800148 }
149 }
150 };
151
Dianne Hackborn7299c412010-03-04 18:41:49 -0800152 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
153 @Override
154 public void onReceive(Context context, Intent intent) {
155 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
156 Intent.EXTRA_DOCK_STATE_UNDOCKED);
157 updateDockState(state);
158 }
159 };
160
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500161 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
162 @Override
163 public void onReceive(Context context, Intent intent) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700164 switch (intent.getAction()) {
165 case Intent.ACTION_BATTERY_CHANGED:
166 mCharging = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
167 break;
168 case PowerManager.ACTION_POWER_SAVE_MODE_CHANGING:
169 mPowerSave = intent.getBooleanExtra(PowerManager.EXTRA_POWER_SAVE_MODE, false);
170 break;
171 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500172 synchronized (mLock) {
173 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700174 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500175 }
176 }
177 }
178 };
179
Adam Lesinski182f73f2013-12-05 16:48:06 -0800180 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700181 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700182 public void onTwilightStateChanged(@Nullable TwilightState state) {
183 synchronized (mLock) {
184 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
185 updateComputedNightModeLocked();
186 updateLocked(0, 0);
187 }
188 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700189 }
190 };
191
Zak Cohen1a705732017-01-09 12:54:34 -0800192 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
193 @Override
194 public void onVrStateChanged(boolean enabled) {
195 synchronized (mLock) {
196 mVrHeadset = enabled;
197 if (mSystemReady) {
198 updateLocked(0, 0);
199 }
200 }
201 }
202 };
203
Adam Lesinski182f73f2013-12-05 16:48:06 -0800204 @Override
205 public void onStart() {
206 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800207
Adam Lesinski182f73f2013-12-05 16:48:06 -0800208 final PowerManager powerManager =
209 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
210 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800211
Adam Lesinski182f73f2013-12-05 16:48:06 -0800212 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800213 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Lucas Dupin53c6e292018-07-12 18:42:53 -0700214 IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
215 batteryFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING);
216 context.registerReceiver(mBatteryReceiver, batteryFilter);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500217
Dianne Hackborn7299c412010-03-04 18:41:49 -0800218 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500219
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800220 final Resources res = context.getResources();
221 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800222 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800223 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500224 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800225 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500226 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700227 mEnableCarDockLaunch = res.getBoolean(
228 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
229 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
230 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700231
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800232 final PackageManager pm = context.getPackageManager();
233 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
234 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
235 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
236
237 final int defaultNightMode = res.getInteger(
238 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800239 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800240 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700241
Adam Lesinski05199e82015-03-19 14:37:11 -0700242 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800243 SystemServerInitThreadPool.get().submit(() -> {
244 synchronized (mLock) {
245 updateConfigurationLocked();
246 sendConfigurationLocked();
247 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800248
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800249 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800250 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800251 }
252
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500253 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800254 @Override
255 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700256 if (isUiModeLocked()) {
257 Slog.e(TAG, "enableCarMode while UI mode is locked");
258 return;
259 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800260 final long ident = Binder.clearCallingIdentity();
261 try {
262 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700263 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800264 if (mSystemReady) {
265 updateLocked(flags, 0);
266 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700267 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800268 } finally {
269 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500270 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800271 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800272
Adam Lesinski182f73f2013-12-05 16:48:06 -0800273 @Override
274 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700275 if (isUiModeLocked()) {
276 Slog.e(TAG, "disableCarMode while UI mode is locked");
277 return;
278 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800279 final long ident = Binder.clearCallingIdentity();
280 try {
281 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700282 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800283 if (mSystemReady) {
284 updateLocked(0, flags);
285 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700286 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800287 } finally {
288 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500289 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700290 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100291
Adam Lesinski182f73f2013-12-05 16:48:06 -0800292 @Override
293 public int getCurrentModeType() {
294 final long ident = Binder.clearCallingIdentity();
295 try {
296 synchronized (mLock) {
297 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700298 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800299 } finally {
300 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800301 }
302 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100303
Adam Lesinski182f73f2013-12-05 16:48:06 -0800304 @Override
305 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700306 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
307 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
308 != PackageManager.PERMISSION_GRANTED)) {
309 Slog.e(TAG,
310 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
311 return;
312 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800313 switch (mode) {
314 case UiModeManager.MODE_NIGHT_NO:
315 case UiModeManager.MODE_NIGHT_YES:
316 case UiModeManager.MODE_NIGHT_AUTO:
317 break;
318 default:
319 throw new IllegalArgumentException("Unknown mode: " + mode);
320 }
321
322 final long ident = Binder.clearCallingIdentity();
323 try {
324 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800325 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800326 Settings.Secure.putInt(getContext().getContentResolver(),
327 Settings.Secure.UI_NIGHT_MODE, mode);
328 mNightMode = mode;
329 updateLocked(0, 0);
330 }
331 }
332 } finally {
333 Binder.restoreCallingIdentity(ident);
334 }
335 }
336
337 @Override
338 public int getNightMode() {
339 synchronized (mLock) {
340 return mNightMode;
341 }
342 }
343
344 @Override
keunyounga7710492015-09-23 11:33:58 -0700345 public boolean isUiModeLocked() {
346 synchronized (mLock) {
347 return mUiModeLocked;
348 }
349 }
350
351 @Override
352 public boolean isNightModeLocked() {
353 synchronized (mLock) {
354 return mNightModeLocked;
355 }
356 }
357
358 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500359 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
360 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
361 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
362 }
363
364 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800365 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600366 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800367 dumpImpl(pw);
368 }
369 };
370
371 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700372 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800373 pw.println("Current UI Mode Service state:");
374 pw.print(" mDockState="); pw.print(mDockState);
375 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
376 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700377 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800378 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700379 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700380 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
381 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800382 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700383 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800384 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
385 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
386 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700387 if (mTwilightManager != null) {
388 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700389 pw.print(" mTwilightService.getLastTwilightState()=");
390 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700391 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700392 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800393 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100394
Adam Lesinski182f73f2013-12-05 16:48:06 -0800395 @Override
396 public void onBootPhase(int phase) {
397 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
398 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700399 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800400 mSystemReady = true;
401 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
402 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800403 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800404 updateLocked(0, 0);
405 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800406 }
407 }
408
keunyoung1d0a7cc2014-07-28 13:12:50 -0700409 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800410 if (mCarModeEnabled != enabled) {
411 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800412 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700413 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800414 }
415
Jeff Brown487bb6e2012-10-11 13:35:42 -0700416 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800417 synchronized (mLock) {
418 if (newState != mDockState) {
419 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700420 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800421 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700422 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800423 }
424 }
425 }
426 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500427
Jeff Brown487bb6e2012-10-11 13:35:42 -0700428 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400429 switch (state) {
430 case Intent.EXTRA_DOCK_STATE_DESK:
431 case Intent.EXTRA_DOCK_STATE_LE_DESK:
432 case Intent.EXTRA_DOCK_STATE_HE_DESK:
433 return true;
434 default:
435 return false;
436 }
437 }
438
Jeff Brown487bb6e2012-10-11 13:35:42 -0700439 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400440 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700441 if (mUiModeLocked) {
442 // no-op, keeps default one
443 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400444 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
445 } else if (mWatch) {
446 uiMode = Configuration.UI_MODE_TYPE_WATCH;
447 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800448 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400449 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800450 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800451 } else if (mVrHeadset) {
452 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800453 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800454
455 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700456 if (mTwilightManager != null) {
457 mTwilightManager.registerListener(mTwilightListener, mHandler);
458 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800459 updateComputedNightModeLocked();
460 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
461 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800462 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700463 if (mTwilightManager != null) {
464 mTwilightManager.unregisterListener(mTwilightListener);
465 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800466 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400467 }
468
Lucas Dupin53c6e292018-07-12 18:42:53 -0700469 if (mPowerSave && !mNightModeLocked) {
470 uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
471 uiMode |= Configuration.UI_MODE_NIGHT_YES;
472 }
473
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400474 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400475 Slog.d(TAG,
476 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400477 + "; mCarMode=" + mCarModeEnabled
478 + "; mNightMode=" + mNightMode
479 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800480 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100481
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800482 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700483 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700484 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700485 }
486 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100487
Jeff Brown487bb6e2012-10-11 13:35:42 -0700488 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700489 if (mSetUiMode != mConfiguration.uiMode) {
490 mSetUiMode = mConfiguration.uiMode;
491
492 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700493 ActivityTaskManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700494 } catch (RemoteException e) {
495 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800496 }
497 }
498 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100499
Adam Lesinski182f73f2013-12-05 16:48:06 -0800500 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700501 String action = null;
502 String oldAction = null;
503 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
504 adjustStatusBarCarModeLocked();
505 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
506 } else if (isDeskDockState(mLastBroadcastState)) {
507 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
508 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100509
Jeff Brown487bb6e2012-10-11 13:35:42 -0700510 if (mCarModeEnabled) {
511 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100512 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700513 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700514 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700515 }
516 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
517 action = UiModeManager.ACTION_ENTER_CAR_MODE;
518 }
519 } else if (isDeskDockState(mDockState)) {
520 if (!isDeskDockState(mLastBroadcastState)) {
521 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700522 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700523 }
524 mLastBroadcastState = mDockState;
525 action = UiModeManager.ACTION_ENTER_DESK_MODE;
526 }
527 } else {
528 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
529 action = oldAction;
530 }
531
532 if (action != null) {
533 if (LOG) {
534 Slog.v(TAG, String.format(
535 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
536 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800537 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100538
Jeff Brown487bb6e2012-10-11 13:35:42 -0700539 // Send the ordered broadcast; the result receiver will receive after all
540 // broadcasts have been sent. If any broadcast receiver changes the result
541 // code from the initial value of RESULT_OK, then the result receiver will
542 // not launch the corresponding dock application. This gives apps a chance
543 // to override the behavior and stay in their app even when the device is
544 // placed into a dock.
545 Intent intent = new Intent(action);
546 intent.putExtra("enableFlags", enableFlags);
547 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700548 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800549 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700550 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100551
Jeff Brown487bb6e2012-10-11 13:35:42 -0700552 // Attempting to make this transition a little more clean, we are going
553 // to hold off on doing a configuration change until we have finished
554 // the broadcast and started the home activity.
555 mHoldingConfiguration = true;
556 updateConfigurationLocked();
557 } else {
558 String category = null;
559 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700560 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700561 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
562 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800563 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400564 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700565 if (ENABLE_LAUNCH_DESK_DOCK_APP
566 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
567 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800568 }
569 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700570 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
571 category = Intent.CATEGORY_HOME;
572 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800573 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100574
Jeff Brown487bb6e2012-10-11 13:35:42 -0700575 if (LOG) {
576 Slog.v(TAG, "updateLocked: null action, mDockState="
577 + mDockState +", category=" + category);
578 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400579
Jeff Brown487bb6e2012-10-11 13:35:42 -0700580 sendConfigurationAndStartDreamOrDockAppLocked(category);
581 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700582
Jeff Brown487bb6e2012-10-11 13:35:42 -0700583 // keep screen on when charging and in car mode
584 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700585 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700586 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700587 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
588 if (keepScreenOn != mWakeLock.isHeld()) {
589 if (keepScreenOn) {
590 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700591 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700592 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800593 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800594 }
595 }
596
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700597 private void sendForegroundBroadcastToAllUsers(String action) {
598 getContext().sendBroadcastAsUser(new Intent(action)
599 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
600 }
601
Jeff Brown62c82e42012-09-26 01:30:41 -0700602 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
603 // Launch a dock activity
604 String category = null;
605 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
606 // Only launch car home when car mode is enabled and the caller
607 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700608 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700609 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
610 category = Intent.CATEGORY_CAR_DOCK;
611 }
612 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
613 // Only launch car home when desk mode is enabled and the caller
614 // has asked us to switch to it. Currently re-using the car
615 // mode flag since we don't have a formal API for "desk mode".
616 if (ENABLE_LAUNCH_DESK_DOCK_APP
617 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
618 category = Intent.CATEGORY_DESK_DOCK;
619 }
620 } else {
621 // Launch the standard home app if requested.
622 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
623 category = Intent.CATEGORY_HOME;
624 }
625 }
626
627 if (LOG) {
628 Slog.v(TAG, String.format(
629 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
630 + "category=%s",
631 action, enableFlags, disableFlags, category));
632 }
633
634 sendConfigurationAndStartDreamOrDockAppLocked(category);
635 }
636
637 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
638 // Update the configuration but don't send it yet.
639 mHoldingConfiguration = false;
640 updateConfigurationLocked();
641
642 // Start the dock app, if there is one.
643 boolean dockAppStarted = false;
644 if (category != null) {
645 // Now we are going to be careful about switching the
646 // configuration and starting the activity -- we need to
647 // do this in a specific order under control of the
648 // activity manager, to do it cleanly. So compute the
649 // new config, but don't set it yet, and let the
650 // activity manager take care of both the start and config
651 // change.
652 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800653 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700654 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700655 int result = ActivityTaskManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800656 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700657 mConfiguration, null, UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -0700658 if (ActivityManager.isStartResultSuccessful(result)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700659 dockAppStarted = true;
660 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
661 Slog.e(TAG, "Could not start dock app: " + homeIntent
662 + ", startActivityWithConfig result " + result);
663 }
664 } catch (RemoteException ex) {
665 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700666 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700667 }
668 }
669
670 // Send the new configuration.
671 sendConfigurationLocked();
672
673 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700674 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800675 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700676 }
677 }
678
Dianne Hackborn7299c412010-03-04 18:41:49 -0800679 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800680 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800681 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700682 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800683 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800684 }
685
Joe Onorato089de882010-04-12 08:18:45 -0700686 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800687 // features of the status bar; these are ORed together to form the
688 // active disabled list. So if (for example) the device is locked and
689 // the status bar should be totally disabled, the calls below will
690 // have no effect until the device is unlocked.
691 if (mStatusBarManager != null) {
692 mStatusBarManager.disable(mCarModeEnabled
693 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
694 : StatusBarManager.DISABLE_NONE);
695 }
696
697 if (mNotificationManager == null) {
698 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800699 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800700 }
701
702 if (mNotificationManager != null) {
703 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800704 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800705
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500706 Notification.Builder n =
707 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400708 .setSmallIcon(R.drawable.stat_notify_car_mode)
709 .setDefaults(Notification.DEFAULT_LIGHTS)
710 .setOngoing(true)
711 .setWhen(0)
712 .setColor(context.getColor(
713 com.android.internal.R.color.system_notification_accent_color))
714 .setContentTitle(
715 context.getString(R.string.car_mode_disable_notification_title))
716 .setContentText(
717 context.getString(R.string.car_mode_disable_notification_message))
718 .setContentIntent(
719 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
720 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700721 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400722 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800723 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700724 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400725 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800726 }
727 }
728 }
729
Jeff Brown2416e092012-08-21 22:12:20 -0700730 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700731 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700732 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700733 if (state != null) {
734 mComputedNightMode = state.isNight();
735 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800736 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800737 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100738
Zak Cohen1a705732017-01-09 12:54:34 -0800739 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800740 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
741 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800742 try {
743 if (vrManager != null) {
744 vrManager.registerListener(mVrStateCallbacks);
745 }
746 } catch (RemoteException e) {
747 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
748 }
749 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100750
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500751 /**
752 * Handles "adb shell" commands.
753 */
754 private static class Shell extends ShellCommand {
755 public static final String NIGHT_MODE_STR_YES = "yes";
756 public static final String NIGHT_MODE_STR_NO = "no";
757 public static final String NIGHT_MODE_STR_AUTO = "auto";
758 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
759 private final IUiModeManager mInterface;
760
761 Shell(IUiModeManager iface) {
762 mInterface = iface;
763 }
764
765 @Override
766 public void onHelp() {
767 final PrintWriter pw = getOutPrintWriter();
768 pw.println("UiModeManager service (uimode) commands:");
769 pw.println(" help");
770 pw.println(" Print this help text.");
771 pw.println(" night [yes|no|auto]");
772 pw.println(" Set or read night mode.");
773 }
774
775 @Override
776 public int onCommand(String cmd) {
777 if (cmd == null) {
778 return handleDefaultCommands(cmd);
779 }
780
781 try {
782 switch (cmd) {
783 case "night":
784 return handleNightMode();
785 default:
786 return handleDefaultCommands(cmd);
787 }
788 } catch (RemoteException e) {
789 final PrintWriter err = getErrPrintWriter();
790 err.println("Remote exception: " + e);
791 }
792 return -1;
793 }
794
795 private int handleNightMode() throws RemoteException {
796 final PrintWriter err = getErrPrintWriter();
797 final String modeStr = getNextArg();
798 if (modeStr == null) {
799 printCurrentNightMode();
800 return 0;
801 }
802
803 final int mode = strToNightMode(modeStr);
804 if (mode >= 0) {
805 mInterface.setNightMode(mode);
806 printCurrentNightMode();
807 return 0;
808 } else {
809 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
810 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
811 return -1;
812 }
813 }
814
815 private void printCurrentNightMode() throws RemoteException {
816 final PrintWriter pw = getOutPrintWriter();
817 final int currMode = mInterface.getNightMode();
818 final String currModeStr = nightModeToStr(currMode);
819 pw.println("Night mode: " + currModeStr);
820 }
821
822 private static String nightModeToStr(int mode) {
823 switch (mode) {
824 case UiModeManager.MODE_NIGHT_YES:
825 return NIGHT_MODE_STR_YES;
826 case UiModeManager.MODE_NIGHT_NO:
827 return NIGHT_MODE_STR_NO;
828 case UiModeManager.MODE_NIGHT_AUTO:
829 return NIGHT_MODE_STR_AUTO;
830 default:
831 return NIGHT_MODE_STR_UNKNOWN;
832 }
833 }
834
835 private static int strToNightMode(String modeStr) {
836 switch (modeStr) {
837 case NIGHT_MODE_STR_YES:
838 return UiModeManager.MODE_NIGHT_YES;
839 case NIGHT_MODE_STR_NO:
840 return UiModeManager.MODE_NIGHT_NO;
841 case NIGHT_MODE_STR_AUTO:
842 return UiModeManager.MODE_NIGHT_AUTO;
843 default:
844 return -1;
845 }
846 }
847 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800848}