blob: c29fc7f753a145d1606bca48b4ceb35e5339530e [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;
Adam Lesinski182f73f2013-12-05 16:48:06 -080085 private int mDefaultUiModeType;
86 private boolean mCarModeKeepsScreenOn;
87 private boolean mDeskModeKeepsScreenOn;
88 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040089 private boolean mWatch;
Zak Cohen1a705732017-01-09 12:54:34 -080090 private boolean mVrHeadset;
Dianne Hackborn7299c412010-03-04 18:41:49 -080091 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070092 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080093
keunyounga7710492015-09-23 11:33:58 -070094 // flag set by resource, whether to enable Car dock launch when starting car mode.
95 private boolean mEnableCarDockLaunch = true;
96 // flag set by resource, whether to lock UI mode to the default one or not.
97 private boolean mUiModeLocked = false;
98 // flag set by resource, whether to night mode change for normal all or not.
99 private boolean mNightModeLocked = false;
100
Adam Lesinski182f73f2013-12-05 16:48:06 -0800101 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800102 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800103 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800104
Dianne Hackborn7299c412010-03-04 18:41:49 -0800105 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800106 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100107
Adam Lesinski182f73f2013-12-05 16:48:06 -0800108 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800109
Adam Lesinski182f73f2013-12-05 16:48:06 -0800110 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800111 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800112 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700113
Adam Lesinski182f73f2013-12-05 16:48:06 -0800114 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800115
Jeff Brownb880d882014-02-10 19:47:07 -0800116 public UiModeManagerService(Context context) {
117 super(context);
118 }
119
Adam Lesinski182f73f2013-12-05 16:48:06 -0800120 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700121 Intent intent = new Intent(Intent.ACTION_MAIN);
122 intent.addCategory(category);
123 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
124 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
125 return intent;
126 }
John Spurlock960779d2012-05-29 14:37:05 -0400127
Dianne Hackborn7299c412010-03-04 18:41:49 -0800128 // The broadcast receiver which receives the result of the ordered broadcast sent when
129 // the dock state changes. The original ordered broadcast is sent with an initial result
130 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
131 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
132 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
133 @Override
134 public void onReceive(Context context, Intent intent) {
135 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400136 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400137 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400138 + ": canceled: " + getResultCode());
139 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800140 return;
141 }
142
Jeff Brown62c82e42012-09-26 01:30:41 -0700143 final int enableFlags = intent.getIntExtra("enableFlags", 0);
144 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800145 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700146 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800147 }
148 }
149 };
150
Dianne Hackborn7299c412010-03-04 18:41:49 -0800151 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
152 @Override
153 public void onReceive(Context context, Intent intent) {
154 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
155 Intent.EXTRA_DOCK_STATE_UNDOCKED);
156 updateDockState(state);
157 }
158 };
159
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500160 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
161 @Override
162 public void onReceive(Context context, Intent intent) {
163 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
164 synchronized (mLock) {
165 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700166 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500167 }
168 }
169 }
170 };
171
Adam Lesinski182f73f2013-12-05 16:48:06 -0800172 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700173 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700174 public void onTwilightStateChanged(@Nullable TwilightState state) {
175 synchronized (mLock) {
176 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
177 updateComputedNightModeLocked();
178 updateLocked(0, 0);
179 }
180 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700181 }
182 };
183
Zak Cohen1a705732017-01-09 12:54:34 -0800184 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
185 @Override
186 public void onVrStateChanged(boolean enabled) {
187 synchronized (mLock) {
188 mVrHeadset = enabled;
189 if (mSystemReady) {
190 updateLocked(0, 0);
191 }
192 }
193 }
194 };
195
Adam Lesinski182f73f2013-12-05 16:48:06 -0800196 @Override
197 public void onStart() {
198 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800199
Adam Lesinski182f73f2013-12-05 16:48:06 -0800200 final PowerManager powerManager =
201 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
202 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800203
Adam Lesinski182f73f2013-12-05 16:48:06 -0800204 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800205 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800206 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500207 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
208
Dianne Hackborn7299c412010-03-04 18:41:49 -0800209 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500210
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800211 final Resources res = context.getResources();
212 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800213 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800214 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500215 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800216 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500217 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700218 mEnableCarDockLaunch = res.getBoolean(
219 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
220 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
221 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700222
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800223 final PackageManager pm = context.getPackageManager();
224 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
225 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
226 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
227
228 final int defaultNightMode = res.getInteger(
229 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800230 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800231 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700232
Adam Lesinski05199e82015-03-19 14:37:11 -0700233 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800234 SystemServerInitThreadPool.get().submit(() -> {
235 synchronized (mLock) {
236 updateConfigurationLocked();
237 sendConfigurationLocked();
238 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800239
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800240 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800241 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800242 }
243
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500244 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800245 @Override
246 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700247 if (isUiModeLocked()) {
248 Slog.e(TAG, "enableCarMode while UI mode is locked");
249 return;
250 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800251 final long ident = Binder.clearCallingIdentity();
252 try {
253 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700254 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800255 if (mSystemReady) {
256 updateLocked(flags, 0);
257 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700258 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800259 } finally {
260 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500261 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800262 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800263
Adam Lesinski182f73f2013-12-05 16:48:06 -0800264 @Override
265 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700266 if (isUiModeLocked()) {
267 Slog.e(TAG, "disableCarMode while UI mode is locked");
268 return;
269 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800270 final long ident = Binder.clearCallingIdentity();
271 try {
272 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700273 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800274 if (mSystemReady) {
275 updateLocked(0, flags);
276 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700277 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800278 } finally {
279 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500280 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700281 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100282
Adam Lesinski182f73f2013-12-05 16:48:06 -0800283 @Override
284 public int getCurrentModeType() {
285 final long ident = Binder.clearCallingIdentity();
286 try {
287 synchronized (mLock) {
288 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700289 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800290 } finally {
291 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800292 }
293 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100294
Adam Lesinski182f73f2013-12-05 16:48:06 -0800295 @Override
296 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700297 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
298 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
299 != PackageManager.PERMISSION_GRANTED)) {
300 Slog.e(TAG,
301 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
302 return;
303 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800304 switch (mode) {
305 case UiModeManager.MODE_NIGHT_NO:
306 case UiModeManager.MODE_NIGHT_YES:
307 case UiModeManager.MODE_NIGHT_AUTO:
308 break;
309 default:
310 throw new IllegalArgumentException("Unknown mode: " + mode);
311 }
312
313 final long ident = Binder.clearCallingIdentity();
314 try {
315 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800316 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800317 Settings.Secure.putInt(getContext().getContentResolver(),
318 Settings.Secure.UI_NIGHT_MODE, mode);
319 mNightMode = mode;
320 updateLocked(0, 0);
321 }
322 }
323 } finally {
324 Binder.restoreCallingIdentity(ident);
325 }
326 }
327
328 @Override
329 public int getNightMode() {
330 synchronized (mLock) {
331 return mNightMode;
332 }
333 }
334
335 @Override
keunyounga7710492015-09-23 11:33:58 -0700336 public boolean isUiModeLocked() {
337 synchronized (mLock) {
338 return mUiModeLocked;
339 }
340 }
341
342 @Override
343 public boolean isNightModeLocked() {
344 synchronized (mLock) {
345 return mNightModeLocked;
346 }
347 }
348
349 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500350 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
351 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
352 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
353 }
354
355 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800356 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600357 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800358 dumpImpl(pw);
359 }
360 };
361
362 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700363 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800364 pw.println("Current UI Mode Service state:");
365 pw.print(" mDockState="); pw.print(mDockState);
366 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
367 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700368 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800369 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700370 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700371 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
372 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800373 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700374 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800375 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
376 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
377 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700378 if (mTwilightManager != null) {
379 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700380 pw.print(" mTwilightService.getLastTwilightState()=");
381 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700382 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700383 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800384 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100385
Adam Lesinski182f73f2013-12-05 16:48:06 -0800386 @Override
387 public void onBootPhase(int phase) {
388 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
389 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700390 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800391 mSystemReady = true;
392 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
393 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800394 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800395 updateLocked(0, 0);
396 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800397 }
398 }
399
keunyoung1d0a7cc2014-07-28 13:12:50 -0700400 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800401 if (mCarModeEnabled != enabled) {
402 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800403 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700404 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800405 }
406
Jeff Brown487bb6e2012-10-11 13:35:42 -0700407 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800408 synchronized (mLock) {
409 if (newState != mDockState) {
410 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700411 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800412 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700413 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800414 }
415 }
416 }
417 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500418
Jeff Brown487bb6e2012-10-11 13:35:42 -0700419 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400420 switch (state) {
421 case Intent.EXTRA_DOCK_STATE_DESK:
422 case Intent.EXTRA_DOCK_STATE_LE_DESK:
423 case Intent.EXTRA_DOCK_STATE_HE_DESK:
424 return true;
425 default:
426 return false;
427 }
428 }
429
Jeff Brown487bb6e2012-10-11 13:35:42 -0700430 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400431 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700432 if (mUiModeLocked) {
433 // no-op, keeps default one
434 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400435 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
436 } else if (mWatch) {
437 uiMode = Configuration.UI_MODE_TYPE_WATCH;
438 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800439 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400440 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800441 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800442 } else if (mVrHeadset) {
443 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800444 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800445
446 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700447 if (mTwilightManager != null) {
448 mTwilightManager.registerListener(mTwilightListener, mHandler);
449 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800450 updateComputedNightModeLocked();
451 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
452 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800453 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700454 if (mTwilightManager != null) {
455 mTwilightManager.unregisterListener(mTwilightListener);
456 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800457 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400458 }
459
460 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400461 Slog.d(TAG,
462 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400463 + "; mCarMode=" + mCarModeEnabled
464 + "; mNightMode=" + mNightMode
465 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800466 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100467
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800468 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700469 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700470 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700471 }
472 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100473
Jeff Brown487bb6e2012-10-11 13:35:42 -0700474 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700475 if (mSetUiMode != mConfiguration.uiMode) {
476 mSetUiMode = mConfiguration.uiMode;
477
478 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700479 ActivityTaskManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700480 } catch (RemoteException e) {
481 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800482 }
483 }
484 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100485
Adam Lesinski182f73f2013-12-05 16:48:06 -0800486 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700487 String action = null;
488 String oldAction = null;
489 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
490 adjustStatusBarCarModeLocked();
491 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
492 } else if (isDeskDockState(mLastBroadcastState)) {
493 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
494 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100495
Jeff Brown487bb6e2012-10-11 13:35:42 -0700496 if (mCarModeEnabled) {
497 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100498 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700499 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700500 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700501 }
502 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
503 action = UiModeManager.ACTION_ENTER_CAR_MODE;
504 }
505 } else if (isDeskDockState(mDockState)) {
506 if (!isDeskDockState(mLastBroadcastState)) {
507 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700508 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700509 }
510 mLastBroadcastState = mDockState;
511 action = UiModeManager.ACTION_ENTER_DESK_MODE;
512 }
513 } else {
514 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
515 action = oldAction;
516 }
517
518 if (action != null) {
519 if (LOG) {
520 Slog.v(TAG, String.format(
521 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
522 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800523 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100524
Jeff Brown487bb6e2012-10-11 13:35:42 -0700525 // Send the ordered broadcast; the result receiver will receive after all
526 // broadcasts have been sent. If any broadcast receiver changes the result
527 // code from the initial value of RESULT_OK, then the result receiver will
528 // not launch the corresponding dock application. This gives apps a chance
529 // to override the behavior and stay in their app even when the device is
530 // placed into a dock.
531 Intent intent = new Intent(action);
532 intent.putExtra("enableFlags", enableFlags);
533 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700534 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800535 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700536 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100537
Jeff Brown487bb6e2012-10-11 13:35:42 -0700538 // Attempting to make this transition a little more clean, we are going
539 // to hold off on doing a configuration change until we have finished
540 // the broadcast and started the home activity.
541 mHoldingConfiguration = true;
542 updateConfigurationLocked();
543 } else {
544 String category = null;
545 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700546 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700547 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
548 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800549 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400550 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700551 if (ENABLE_LAUNCH_DESK_DOCK_APP
552 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
553 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800554 }
555 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700556 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
557 category = Intent.CATEGORY_HOME;
558 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800559 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100560
Jeff Brown487bb6e2012-10-11 13:35:42 -0700561 if (LOG) {
562 Slog.v(TAG, "updateLocked: null action, mDockState="
563 + mDockState +", category=" + category);
564 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400565
Jeff Brown487bb6e2012-10-11 13:35:42 -0700566 sendConfigurationAndStartDreamOrDockAppLocked(category);
567 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700568
Jeff Brown487bb6e2012-10-11 13:35:42 -0700569 // keep screen on when charging and in car mode
570 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700571 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700572 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700573 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
574 if (keepScreenOn != mWakeLock.isHeld()) {
575 if (keepScreenOn) {
576 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700577 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700578 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800579 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800580 }
581 }
582
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700583 private void sendForegroundBroadcastToAllUsers(String action) {
584 getContext().sendBroadcastAsUser(new Intent(action)
585 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
586 }
587
Jeff Brown62c82e42012-09-26 01:30:41 -0700588 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
589 // Launch a dock activity
590 String category = null;
591 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
592 // Only launch car home when car mode is enabled and the caller
593 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700594 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700595 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
596 category = Intent.CATEGORY_CAR_DOCK;
597 }
598 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
599 // Only launch car home when desk mode is enabled and the caller
600 // has asked us to switch to it. Currently re-using the car
601 // mode flag since we don't have a formal API for "desk mode".
602 if (ENABLE_LAUNCH_DESK_DOCK_APP
603 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
604 category = Intent.CATEGORY_DESK_DOCK;
605 }
606 } else {
607 // Launch the standard home app if requested.
608 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
609 category = Intent.CATEGORY_HOME;
610 }
611 }
612
613 if (LOG) {
614 Slog.v(TAG, String.format(
615 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
616 + "category=%s",
617 action, enableFlags, disableFlags, category));
618 }
619
620 sendConfigurationAndStartDreamOrDockAppLocked(category);
621 }
622
623 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
624 // Update the configuration but don't send it yet.
625 mHoldingConfiguration = false;
626 updateConfigurationLocked();
627
628 // Start the dock app, if there is one.
629 boolean dockAppStarted = false;
630 if (category != null) {
631 // Now we are going to be careful about switching the
632 // configuration and starting the activity -- we need to
633 // do this in a specific order under control of the
634 // activity manager, to do it cleanly. So compute the
635 // new config, but don't set it yet, and let the
636 // activity manager take care of both the start and config
637 // change.
638 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800639 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700640 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700641 int result = ActivityTaskManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800642 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700643 mConfiguration, null, UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -0700644 if (ActivityManager.isStartResultSuccessful(result)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700645 dockAppStarted = true;
646 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
647 Slog.e(TAG, "Could not start dock app: " + homeIntent
648 + ", startActivityWithConfig result " + result);
649 }
650 } catch (RemoteException ex) {
651 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700652 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700653 }
654 }
655
656 // Send the new configuration.
657 sendConfigurationLocked();
658
659 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700660 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800661 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700662 }
663 }
664
Dianne Hackborn7299c412010-03-04 18:41:49 -0800665 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800666 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800667 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700668 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800669 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800670 }
671
Joe Onorato089de882010-04-12 08:18:45 -0700672 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800673 // features of the status bar; these are ORed together to form the
674 // active disabled list. So if (for example) the device is locked and
675 // the status bar should be totally disabled, the calls below will
676 // have no effect until the device is unlocked.
677 if (mStatusBarManager != null) {
678 mStatusBarManager.disable(mCarModeEnabled
679 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
680 : StatusBarManager.DISABLE_NONE);
681 }
682
683 if (mNotificationManager == null) {
684 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800685 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800686 }
687
688 if (mNotificationManager != null) {
689 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800690 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800691
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500692 Notification.Builder n =
693 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400694 .setSmallIcon(R.drawable.stat_notify_car_mode)
695 .setDefaults(Notification.DEFAULT_LIGHTS)
696 .setOngoing(true)
697 .setWhen(0)
698 .setColor(context.getColor(
699 com.android.internal.R.color.system_notification_accent_color))
700 .setContentTitle(
701 context.getString(R.string.car_mode_disable_notification_title))
702 .setContentText(
703 context.getString(R.string.car_mode_disable_notification_message))
704 .setContentIntent(
705 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
706 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700707 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400708 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800709 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700710 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400711 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800712 }
713 }
714 }
715
Jeff Brown2416e092012-08-21 22:12:20 -0700716 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700717 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700718 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700719 if (state != null) {
720 mComputedNightMode = state.isNight();
721 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800722 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800723 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100724
Zak Cohen1a705732017-01-09 12:54:34 -0800725 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800726 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
727 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800728 try {
729 if (vrManager != null) {
730 vrManager.registerListener(mVrStateCallbacks);
731 }
732 } catch (RemoteException e) {
733 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
734 }
735 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100736
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500737 /**
738 * Handles "adb shell" commands.
739 */
740 private static class Shell extends ShellCommand {
741 public static final String NIGHT_MODE_STR_YES = "yes";
742 public static final String NIGHT_MODE_STR_NO = "no";
743 public static final String NIGHT_MODE_STR_AUTO = "auto";
744 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
745 private final IUiModeManager mInterface;
746
747 Shell(IUiModeManager iface) {
748 mInterface = iface;
749 }
750
751 @Override
752 public void onHelp() {
753 final PrintWriter pw = getOutPrintWriter();
754 pw.println("UiModeManager service (uimode) commands:");
755 pw.println(" help");
756 pw.println(" Print this help text.");
757 pw.println(" night [yes|no|auto]");
758 pw.println(" Set or read night mode.");
759 }
760
761 @Override
762 public int onCommand(String cmd) {
763 if (cmd == null) {
764 return handleDefaultCommands(cmd);
765 }
766
767 try {
768 switch (cmd) {
769 case "night":
770 return handleNightMode();
771 default:
772 return handleDefaultCommands(cmd);
773 }
774 } catch (RemoteException e) {
775 final PrintWriter err = getErrPrintWriter();
776 err.println("Remote exception: " + e);
777 }
778 return -1;
779 }
780
781 private int handleNightMode() throws RemoteException {
782 final PrintWriter err = getErrPrintWriter();
783 final String modeStr = getNextArg();
784 if (modeStr == null) {
785 printCurrentNightMode();
786 return 0;
787 }
788
789 final int mode = strToNightMode(modeStr);
790 if (mode >= 0) {
791 mInterface.setNightMode(mode);
792 printCurrentNightMode();
793 return 0;
794 } else {
795 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
796 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
797 return -1;
798 }
799 }
800
801 private void printCurrentNightMode() throws RemoteException {
802 final PrintWriter pw = getOutPrintWriter();
803 final int currMode = mInterface.getNightMode();
804 final String currModeStr = nightModeToStr(currMode);
805 pw.println("Night mode: " + currModeStr);
806 }
807
808 private static String nightModeToStr(int mode) {
809 switch (mode) {
810 case UiModeManager.MODE_NIGHT_YES:
811 return NIGHT_MODE_STR_YES;
812 case UiModeManager.MODE_NIGHT_NO:
813 return NIGHT_MODE_STR_NO;
814 case UiModeManager.MODE_NIGHT_AUTO:
815 return NIGHT_MODE_STR_AUTO;
816 default:
817 return NIGHT_MODE_STR_UNKNOWN;
818 }
819 }
820
821 private static int strToNightMode(String modeStr) {
822 switch (modeStr) {
823 case NIGHT_MODE_STR_YES:
824 return UiModeManager.MODE_NIGHT_YES;
825 case NIGHT_MODE_STR_NO:
826 return UiModeManager.MODE_NIGHT_NO;
827 case NIGHT_MODE_STR_AUTO:
828 return UiModeManager.MODE_NIGHT_AUTO;
829 default:
830 return -1;
831 }
832 }
833 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800834}