blob: f436286a1e40889bb1044d4c46d2714c3f89a941 [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
Jeff Brownb880d882014-02-10 19:47:07 -0800111 public UiModeManagerService(Context context) {
112 super(context);
113 }
114
Adam Lesinski182f73f2013-12-05 16:48:06 -0800115 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700116 Intent intent = new Intent(Intent.ACTION_MAIN);
117 intent.addCategory(category);
118 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
119 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
120 return intent;
121 }
John Spurlock960779d2012-05-29 14:37:05 -0400122
Dianne Hackborn7299c412010-03-04 18:41:49 -0800123 // The broadcast receiver which receives the result of the ordered broadcast sent when
124 // the dock state changes. The original ordered broadcast is sent with an initial result
125 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
126 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
127 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
128 @Override
129 public void onReceive(Context context, Intent intent) {
130 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400131 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400132 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400133 + ": canceled: " + getResultCode());
134 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800135 return;
136 }
137
Jeff Brown62c82e42012-09-26 01:30:41 -0700138 final int enableFlags = intent.getIntExtra("enableFlags", 0);
139 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800140 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700141 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800142 }
143 }
144 };
145
Dianne Hackborn7299c412010-03-04 18:41:49 -0800146 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
147 @Override
148 public void onReceive(Context context, Intent intent) {
149 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
150 Intent.EXTRA_DOCK_STATE_UNDOCKED);
151 updateDockState(state);
152 }
153 };
154
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500155 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
156 @Override
157 public void onReceive(Context context, Intent intent) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700158 switch (intent.getAction()) {
159 case Intent.ACTION_BATTERY_CHANGED:
160 mCharging = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
161 break;
162 case PowerManager.ACTION_POWER_SAVE_MODE_CHANGING:
163 mPowerSave = intent.getBooleanExtra(PowerManager.EXTRA_POWER_SAVE_MODE, false);
164 break;
165 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500166 synchronized (mLock) {
167 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700168 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500169 }
170 }
171 }
172 };
173
Adam Lesinski182f73f2013-12-05 16:48:06 -0800174 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700175 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700176 public void onTwilightStateChanged(@Nullable TwilightState state) {
177 synchronized (mLock) {
178 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
179 updateComputedNightModeLocked();
180 updateLocked(0, 0);
181 }
182 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700183 }
184 };
185
Zak Cohen1a705732017-01-09 12:54:34 -0800186 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
187 @Override
188 public void onVrStateChanged(boolean enabled) {
189 synchronized (mLock) {
190 mVrHeadset = enabled;
191 if (mSystemReady) {
192 updateLocked(0, 0);
193 }
194 }
195 }
196 };
197
Adam Lesinski182f73f2013-12-05 16:48:06 -0800198 @Override
199 public void onStart() {
200 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800201
Adam Lesinski182f73f2013-12-05 16:48:06 -0800202 final PowerManager powerManager =
203 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
204 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800205
Adam Lesinski182f73f2013-12-05 16:48:06 -0800206 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800207 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Lucas Dupin53c6e292018-07-12 18:42:53 -0700208 IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
209 batteryFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING);
210 context.registerReceiver(mBatteryReceiver, batteryFilter);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500211
Dianne Hackborn7299c412010-03-04 18:41:49 -0800212 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500213
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800214 final Resources res = context.getResources();
215 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800216 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800217 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500218 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800219 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500220 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700221 mEnableCarDockLaunch = res.getBoolean(
222 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
223 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
224 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700225
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800226 final PackageManager pm = context.getPackageManager();
227 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
228 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
229 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
230
231 final int defaultNightMode = res.getInteger(
232 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800233 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800234 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700235
Adam Lesinski05199e82015-03-19 14:37:11 -0700236 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800237 SystemServerInitThreadPool.get().submit(() -> {
238 synchronized (mLock) {
239 updateConfigurationLocked();
240 sendConfigurationLocked();
241 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800242
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800243 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800244 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800245 }
246
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500247 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800248 @Override
249 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700250 if (isUiModeLocked()) {
251 Slog.e(TAG, "enableCarMode while UI mode is locked");
252 return;
253 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800254 final long ident = Binder.clearCallingIdentity();
255 try {
256 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700257 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800258 if (mSystemReady) {
259 updateLocked(flags, 0);
260 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700261 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800262 } finally {
263 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500264 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800265 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800266
Adam Lesinski182f73f2013-12-05 16:48:06 -0800267 @Override
268 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700269 if (isUiModeLocked()) {
270 Slog.e(TAG, "disableCarMode while UI mode is locked");
271 return;
272 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800273 final long ident = Binder.clearCallingIdentity();
274 try {
275 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700276 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800277 if (mSystemReady) {
278 updateLocked(0, flags);
279 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700280 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800281 } finally {
282 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500283 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700284 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100285
Adam Lesinski182f73f2013-12-05 16:48:06 -0800286 @Override
287 public int getCurrentModeType() {
288 final long ident = Binder.clearCallingIdentity();
289 try {
290 synchronized (mLock) {
291 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700292 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800293 } finally {
294 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800295 }
296 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100297
Adam Lesinski182f73f2013-12-05 16:48:06 -0800298 @Override
299 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700300 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
301 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
302 != PackageManager.PERMISSION_GRANTED)) {
303 Slog.e(TAG,
304 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
305 return;
306 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800307 switch (mode) {
308 case UiModeManager.MODE_NIGHT_NO:
309 case UiModeManager.MODE_NIGHT_YES:
310 case UiModeManager.MODE_NIGHT_AUTO:
311 break;
312 default:
313 throw new IllegalArgumentException("Unknown mode: " + mode);
314 }
315
316 final long ident = Binder.clearCallingIdentity();
317 try {
318 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800319 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800320 Settings.Secure.putInt(getContext().getContentResolver(),
321 Settings.Secure.UI_NIGHT_MODE, mode);
322 mNightMode = mode;
323 updateLocked(0, 0);
324 }
325 }
326 } finally {
327 Binder.restoreCallingIdentity(ident);
328 }
329 }
330
331 @Override
332 public int getNightMode() {
333 synchronized (mLock) {
334 return mNightMode;
335 }
336 }
337
338 @Override
keunyounga7710492015-09-23 11:33:58 -0700339 public boolean isUiModeLocked() {
340 synchronized (mLock) {
341 return mUiModeLocked;
342 }
343 }
344
345 @Override
346 public boolean isNightModeLocked() {
347 synchronized (mLock) {
348 return mNightModeLocked;
349 }
350 }
351
352 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500353 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
354 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
355 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
356 }
357
358 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800359 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600360 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800361 dumpImpl(pw);
362 }
363 };
364
365 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700366 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800367 pw.println("Current UI Mode Service state:");
368 pw.print(" mDockState="); pw.print(mDockState);
369 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
370 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700371 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800372 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700373 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700374 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
375 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800376 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700377 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800378 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
379 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
380 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700381 if (mTwilightManager != null) {
382 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700383 pw.print(" mTwilightService.getLastTwilightState()=");
384 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700385 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700386 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800387 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100388
Adam Lesinski182f73f2013-12-05 16:48:06 -0800389 @Override
390 public void onBootPhase(int phase) {
391 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
392 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700393 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800394 mSystemReady = true;
395 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
396 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800397 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800398 updateLocked(0, 0);
399 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800400 }
401 }
402
keunyoung1d0a7cc2014-07-28 13:12:50 -0700403 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800404 if (mCarModeEnabled != enabled) {
405 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800406 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700407 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800408 }
409
Jeff Brown487bb6e2012-10-11 13:35:42 -0700410 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800411 synchronized (mLock) {
412 if (newState != mDockState) {
413 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700414 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800415 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700416 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800417 }
418 }
419 }
420 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500421
Jeff Brown487bb6e2012-10-11 13:35:42 -0700422 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400423 switch (state) {
424 case Intent.EXTRA_DOCK_STATE_DESK:
425 case Intent.EXTRA_DOCK_STATE_LE_DESK:
426 case Intent.EXTRA_DOCK_STATE_HE_DESK:
427 return true;
428 default:
429 return false;
430 }
431 }
432
Jeff Brown487bb6e2012-10-11 13:35:42 -0700433 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400434 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700435 if (mUiModeLocked) {
436 // no-op, keeps default one
437 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400438 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
439 } else if (mWatch) {
440 uiMode = Configuration.UI_MODE_TYPE_WATCH;
441 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800442 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400443 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800444 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800445 } else if (mVrHeadset) {
446 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800447 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800448
449 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700450 if (mTwilightManager != null) {
451 mTwilightManager.registerListener(mTwilightListener, mHandler);
452 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800453 updateComputedNightModeLocked();
454 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
455 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800456 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700457 if (mTwilightManager != null) {
458 mTwilightManager.unregisterListener(mTwilightListener);
459 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800460 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400461 }
462
Lucas Dupinb1f0c762018-09-18 10:33:38 -0700463 if (mPowerSave) {
Lucas Dupin53c6e292018-07-12 18:42:53 -0700464 uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
465 uiMode |= Configuration.UI_MODE_NIGHT_YES;
466 }
467
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400468 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400469 Slog.d(TAG,
470 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400471 + "; mCarMode=" + mCarModeEnabled
472 + "; mNightMode=" + mNightMode
473 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800474 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100475
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800476 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700477 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700478 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700479 }
480 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100481
Jeff Brown487bb6e2012-10-11 13:35:42 -0700482 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700483 if (mSetUiMode != mConfiguration.uiMode) {
484 mSetUiMode = mConfiguration.uiMode;
485
486 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700487 ActivityTaskManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700488 } catch (RemoteException e) {
489 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800490 }
491 }
492 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100493
Adam Lesinski182f73f2013-12-05 16:48:06 -0800494 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700495 String action = null;
496 String oldAction = null;
497 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
498 adjustStatusBarCarModeLocked();
499 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
500 } else if (isDeskDockState(mLastBroadcastState)) {
501 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
502 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100503
Jeff Brown487bb6e2012-10-11 13:35:42 -0700504 if (mCarModeEnabled) {
505 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100506 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700507 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700508 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700509 }
510 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
511 action = UiModeManager.ACTION_ENTER_CAR_MODE;
512 }
513 } else if (isDeskDockState(mDockState)) {
514 if (!isDeskDockState(mLastBroadcastState)) {
515 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700516 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700517 }
518 mLastBroadcastState = mDockState;
519 action = UiModeManager.ACTION_ENTER_DESK_MODE;
520 }
521 } else {
522 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
523 action = oldAction;
524 }
525
526 if (action != null) {
527 if (LOG) {
528 Slog.v(TAG, String.format(
529 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
530 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800531 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100532
Jeff Brown487bb6e2012-10-11 13:35:42 -0700533 // Send the ordered broadcast; the result receiver will receive after all
534 // broadcasts have been sent. If any broadcast receiver changes the result
535 // code from the initial value of RESULT_OK, then the result receiver will
536 // not launch the corresponding dock application. This gives apps a chance
537 // to override the behavior and stay in their app even when the device is
538 // placed into a dock.
539 Intent intent = new Intent(action);
540 intent.putExtra("enableFlags", enableFlags);
541 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700542 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800543 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700544 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100545
Jeff Brown487bb6e2012-10-11 13:35:42 -0700546 // Attempting to make this transition a little more clean, we are going
547 // to hold off on doing a configuration change until we have finished
548 // the broadcast and started the home activity.
549 mHoldingConfiguration = true;
550 updateConfigurationLocked();
551 } else {
552 String category = null;
553 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700554 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700555 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
556 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800557 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400558 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700559 if (ENABLE_LAUNCH_DESK_DOCK_APP
560 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
561 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800562 }
563 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700564 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
565 category = Intent.CATEGORY_HOME;
566 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800567 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100568
Jeff Brown487bb6e2012-10-11 13:35:42 -0700569 if (LOG) {
570 Slog.v(TAG, "updateLocked: null action, mDockState="
571 + mDockState +", category=" + category);
572 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400573
Jeff Brown487bb6e2012-10-11 13:35:42 -0700574 sendConfigurationAndStartDreamOrDockAppLocked(category);
575 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700576
Jeff Brown487bb6e2012-10-11 13:35:42 -0700577 // keep screen on when charging and in car mode
578 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700579 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700580 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700581 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
582 if (keepScreenOn != mWakeLock.isHeld()) {
583 if (keepScreenOn) {
584 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700585 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700586 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800587 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800588 }
589 }
590
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700591 private void sendForegroundBroadcastToAllUsers(String action) {
592 getContext().sendBroadcastAsUser(new Intent(action)
593 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
594 }
595
Jeff Brown62c82e42012-09-26 01:30:41 -0700596 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
597 // Launch a dock activity
598 String category = null;
599 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
600 // Only launch car home when car mode is enabled and the caller
601 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700602 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700603 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
604 category = Intent.CATEGORY_CAR_DOCK;
605 }
606 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
607 // Only launch car home when desk mode is enabled and the caller
608 // has asked us to switch to it. Currently re-using the car
609 // mode flag since we don't have a formal API for "desk mode".
610 if (ENABLE_LAUNCH_DESK_DOCK_APP
611 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
612 category = Intent.CATEGORY_DESK_DOCK;
613 }
614 } else {
615 // Launch the standard home app if requested.
616 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
617 category = Intent.CATEGORY_HOME;
618 }
619 }
620
621 if (LOG) {
622 Slog.v(TAG, String.format(
623 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
624 + "category=%s",
625 action, enableFlags, disableFlags, category));
626 }
627
628 sendConfigurationAndStartDreamOrDockAppLocked(category);
629 }
630
631 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
632 // Update the configuration but don't send it yet.
633 mHoldingConfiguration = false;
634 updateConfigurationLocked();
635
636 // Start the dock app, if there is one.
637 boolean dockAppStarted = false;
638 if (category != null) {
639 // Now we are going to be careful about switching the
640 // configuration and starting the activity -- we need to
641 // do this in a specific order under control of the
642 // activity manager, to do it cleanly. So compute the
643 // new config, but don't set it yet, and let the
644 // activity manager take care of both the start and config
645 // change.
646 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800647 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700648 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700649 int result = ActivityTaskManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800650 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700651 mConfiguration, null, UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -0700652 if (ActivityManager.isStartResultSuccessful(result)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700653 dockAppStarted = true;
654 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
655 Slog.e(TAG, "Could not start dock app: " + homeIntent
656 + ", startActivityWithConfig result " + result);
657 }
658 } catch (RemoteException ex) {
659 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700660 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700661 }
662 }
663
664 // Send the new configuration.
665 sendConfigurationLocked();
666
667 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700668 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800669 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700670 }
671 }
672
Dianne Hackborn7299c412010-03-04 18:41:49 -0800673 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800674 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800675 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700676 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800677 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800678 }
679
Joe Onorato089de882010-04-12 08:18:45 -0700680 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800681 // features of the status bar; these are ORed together to form the
682 // active disabled list. So if (for example) the device is locked and
683 // the status bar should be totally disabled, the calls below will
684 // have no effect until the device is unlocked.
685 if (mStatusBarManager != null) {
686 mStatusBarManager.disable(mCarModeEnabled
687 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
688 : StatusBarManager.DISABLE_NONE);
689 }
690
691 if (mNotificationManager == null) {
692 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800693 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800694 }
695
696 if (mNotificationManager != null) {
697 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800698 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800699
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500700 Notification.Builder n =
701 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400702 .setSmallIcon(R.drawable.stat_notify_car_mode)
703 .setDefaults(Notification.DEFAULT_LIGHTS)
704 .setOngoing(true)
705 .setWhen(0)
706 .setColor(context.getColor(
707 com.android.internal.R.color.system_notification_accent_color))
708 .setContentTitle(
709 context.getString(R.string.car_mode_disable_notification_title))
710 .setContentText(
711 context.getString(R.string.car_mode_disable_notification_message))
712 .setContentIntent(
713 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
714 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700715 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400716 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800717 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700718 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400719 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800720 }
721 }
722 }
723
Jeff Brown2416e092012-08-21 22:12:20 -0700724 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700725 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700726 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700727 if (state != null) {
728 mComputedNightMode = state.isNight();
729 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800730 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800731 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100732
Zak Cohen1a705732017-01-09 12:54:34 -0800733 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800734 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
735 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800736 try {
737 if (vrManager != null) {
738 vrManager.registerListener(mVrStateCallbacks);
739 }
740 } catch (RemoteException e) {
741 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
742 }
743 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100744
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500745 /**
746 * Handles "adb shell" commands.
747 */
748 private static class Shell extends ShellCommand {
749 public static final String NIGHT_MODE_STR_YES = "yes";
750 public static final String NIGHT_MODE_STR_NO = "no";
751 public static final String NIGHT_MODE_STR_AUTO = "auto";
752 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
753 private final IUiModeManager mInterface;
754
755 Shell(IUiModeManager iface) {
756 mInterface = iface;
757 }
758
759 @Override
760 public void onHelp() {
761 final PrintWriter pw = getOutPrintWriter();
762 pw.println("UiModeManager service (uimode) commands:");
763 pw.println(" help");
764 pw.println(" Print this help text.");
765 pw.println(" night [yes|no|auto]");
766 pw.println(" Set or read night mode.");
767 }
768
769 @Override
770 public int onCommand(String cmd) {
771 if (cmd == null) {
772 return handleDefaultCommands(cmd);
773 }
774
775 try {
776 switch (cmd) {
777 case "night":
778 return handleNightMode();
779 default:
780 return handleDefaultCommands(cmd);
781 }
782 } catch (RemoteException e) {
783 final PrintWriter err = getErrPrintWriter();
784 err.println("Remote exception: " + e);
785 }
786 return -1;
787 }
788
789 private int handleNightMode() throws RemoteException {
790 final PrintWriter err = getErrPrintWriter();
791 final String modeStr = getNextArg();
792 if (modeStr == null) {
793 printCurrentNightMode();
794 return 0;
795 }
796
797 final int mode = strToNightMode(modeStr);
798 if (mode >= 0) {
799 mInterface.setNightMode(mode);
800 printCurrentNightMode();
801 return 0;
802 } else {
803 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
804 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
805 return -1;
806 }
807 }
808
809 private void printCurrentNightMode() throws RemoteException {
810 final PrintWriter pw = getOutPrintWriter();
811 final int currMode = mInterface.getNightMode();
812 final String currModeStr = nightModeToStr(currMode);
813 pw.println("Night mode: " + currModeStr);
814 }
815
816 private static String nightModeToStr(int mode) {
817 switch (mode) {
818 case UiModeManager.MODE_NIGHT_YES:
819 return NIGHT_MODE_STR_YES;
820 case UiModeManager.MODE_NIGHT_NO:
821 return NIGHT_MODE_STR_NO;
822 case UiModeManager.MODE_NIGHT_AUTO:
823 return NIGHT_MODE_STR_AUTO;
824 default:
825 return NIGHT_MODE_STR_UNKNOWN;
826 }
827 }
828
829 private static int strToNightMode(String modeStr) {
830 switch (modeStr) {
831 case NIGHT_MODE_STR_YES:
832 return UiModeManager.MODE_NIGHT_YES;
833 case NIGHT_MODE_STR_NO:
834 return UiModeManager.MODE_NIGHT_NO;
835 case NIGHT_MODE_STR_AUTO:
836 return UiModeManager.MODE_NIGHT_AUTO;
837 default:
838 return -1;
839 }
840 }
841 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800842}