blob: 6b5366a39a4376dfead9b99548af4e585182a261 [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;
Dianne Hackborn7299c412010-03-04 18:41:49 -080022import android.app.IUiModeManager;
23import android.app.Notification;
24import android.app.NotificationManager;
25import android.app.PendingIntent;
26import android.app.StatusBarManager;
27import android.app.UiModeManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080028import android.content.BroadcastReceiver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
32import android.content.pm.PackageManager;
33import android.content.res.Configuration;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080034import android.content.res.Resources;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050035import android.os.BatteryManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080036import android.os.Binder;
Dianne Hackborn7299c412010-03-04 18:41:49 -080037import android.os.Handler;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050038import android.os.PowerManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080039import android.os.RemoteException;
Alan Viveretteba0d98f2017-01-30 10:36:54 -050040import android.os.ResultReceiver;
Zak Cohen1a705732017-01-09 12:54:34 -080041import android.os.ServiceManager;
Alan Viveretteba0d98f2017-01-30 10:36:54 -050042import android.os.ShellCallback;
43import android.os.ShellCommand;
Jason Monk97a06a12016-11-09 15:53:30 -050044import android.os.SystemProperties;
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;
Jason Monk524fb402017-01-25 10:33:31 -050050import android.text.TextUtils;
Dianne Hackborn7299c412010-03-04 18:41:49 -080051import android.util.Slog;
52
Jason Monk524fb402017-01-25 10:33:31 -050053import java.io.File;
Dianne Hackborn7299c412010-03-04 18:41:49 -080054import java.io.FileDescriptor;
55import java.io.PrintWriter;
Jason Monk524fb402017-01-25 10:33:31 -050056import java.util.ArrayList;
57import java.util.Collections;
Dianne Hackborn7299c412010-03-04 18:41:49 -080058
59import com.android.internal.R;
60import com.android.internal.app.DisableCarModeActivity;
Chris Wren282cfef2017-03-27 15:01:44 -040061import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050062import com.android.internal.notification.SystemNotificationChannels;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060063import com.android.internal.util.DumpUtils;
Jason Monk97a06a12016-11-09 15:53:30 -050064import com.android.server.power.ShutdownThread;
Adam Lesinski182f73f2013-12-05 16:48:06 -080065import com.android.server.twilight.TwilightListener;
66import com.android.server.twilight.TwilightManager;
67import com.android.server.twilight.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080068
Adam Lesinski182f73f2013-12-05 16:48:06 -080069final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080070 private static final String TAG = UiModeManager.class.getSimpleName();
71 private static final boolean LOG = false;
72
Daniel Sandler11ddf532011-11-16 11:10:03 -080073 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040074 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080075
Dianne Hackborn7299c412010-03-04 18:41:49 -080076 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080077 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010078
Adam Lesinski182f73f2013-12-05 16:48:06 -080079 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080080 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080081
Dianne Hackborn7299c412010-03-04 18:41:49 -080082 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050083 private boolean mCharging = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080084 private int mDefaultUiModeType;
85 private boolean mCarModeKeepsScreenOn;
86 private boolean mDeskModeKeepsScreenOn;
87 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040088 private boolean mWatch;
Zak Cohen1a705732017-01-09 12:54:34 -080089 private boolean mVrHeadset;
Dianne Hackborn7299c412010-03-04 18:41:49 -080090 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070091 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080092
keunyounga7710492015-09-23 11:33:58 -070093 // flag set by resource, whether to enable Car dock launch when starting car mode.
94 private boolean mEnableCarDockLaunch = true;
95 // flag set by resource, whether to lock UI mode to the default one or not.
96 private boolean mUiModeLocked = false;
97 // flag set by resource, whether to night mode change for normal all or not.
98 private boolean mNightModeLocked = false;
99
Adam Lesinski182f73f2013-12-05 16:48:06 -0800100 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800101 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800102 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800103
Dianne Hackborn7299c412010-03-04 18:41:49 -0800104 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800105 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100106
Adam Lesinski182f73f2013-12-05 16:48:06 -0800107 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800108
Adam Lesinski182f73f2013-12-05 16:48:06 -0800109 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800110 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800111 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700112
Adam Lesinski182f73f2013-12-05 16:48:06 -0800113 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800114
Jeff Brownb880d882014-02-10 19:47:07 -0800115 public UiModeManagerService(Context context) {
116 super(context);
117 }
118
Adam Lesinski182f73f2013-12-05 16:48:06 -0800119 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700120 Intent intent = new Intent(Intent.ACTION_MAIN);
121 intent.addCategory(category);
122 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
123 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
124 return intent;
125 }
John Spurlock960779d2012-05-29 14:37:05 -0400126
Dianne Hackborn7299c412010-03-04 18:41:49 -0800127 // The broadcast receiver which receives the result of the ordered broadcast sent when
128 // the dock state changes. The original ordered broadcast is sent with an initial result
129 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
130 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
131 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
132 @Override
133 public void onReceive(Context context, Intent intent) {
134 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400135 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400136 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400137 + ": canceled: " + getResultCode());
138 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800139 return;
140 }
141
Jeff Brown62c82e42012-09-26 01:30:41 -0700142 final int enableFlags = intent.getIntExtra("enableFlags", 0);
143 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800144 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700145 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800146 }
147 }
148 };
149
Dianne Hackborn7299c412010-03-04 18:41:49 -0800150 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
151 @Override
152 public void onReceive(Context context, Intent intent) {
153 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
154 Intent.EXTRA_DOCK_STATE_UNDOCKED);
155 updateDockState(state);
156 }
157 };
158
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500159 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
160 @Override
161 public void onReceive(Context context, Intent intent) {
162 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
163 synchronized (mLock) {
164 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700165 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500166 }
167 }
168 }
169 };
170
Adam Lesinski182f73f2013-12-05 16:48:06 -0800171 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700172 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700173 public void onTwilightStateChanged(@Nullable TwilightState state) {
174 synchronized (mLock) {
175 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
176 updateComputedNightModeLocked();
177 updateLocked(0, 0);
178 }
179 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700180 }
181 };
182
Zak Cohen1a705732017-01-09 12:54:34 -0800183 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
184 @Override
185 public void onVrStateChanged(boolean enabled) {
186 synchronized (mLock) {
187 mVrHeadset = enabled;
188 if (mSystemReady) {
189 updateLocked(0, 0);
190 }
191 }
192 }
193 };
194
Adam Lesinski182f73f2013-12-05 16:48:06 -0800195 @Override
196 public void onStart() {
197 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800198
Adam Lesinski182f73f2013-12-05 16:48:06 -0800199 final PowerManager powerManager =
200 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
201 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800202
Adam Lesinski182f73f2013-12-05 16:48:06 -0800203 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800204 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800205 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500206 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
207
Dianne Hackborn7299c412010-03-04 18:41:49 -0800208 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500209
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800210 final Resources res = context.getResources();
211 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800212 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800213 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500214 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800215 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500216 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700217 mEnableCarDockLaunch = res.getBoolean(
218 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
219 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
220 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700221
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800222 final PackageManager pm = context.getPackageManager();
223 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
224 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
225 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
226
227 final int defaultNightMode = res.getInteger(
228 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800229 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800230 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700231
Adam Lesinski05199e82015-03-19 14:37:11 -0700232 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800233 SystemServerInitThreadPool.get().submit(() -> {
234 synchronized (mLock) {
235 updateConfigurationLocked();
236 sendConfigurationLocked();
237 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800238
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800239 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800240 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800241 }
242
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500243 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800244 @Override
245 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700246 if (isUiModeLocked()) {
247 Slog.e(TAG, "enableCarMode while UI mode is locked");
248 return;
249 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800250 final long ident = Binder.clearCallingIdentity();
251 try {
252 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700253 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800254 if (mSystemReady) {
255 updateLocked(flags, 0);
256 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700257 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800258 } finally {
259 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500260 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800261 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800262
Adam Lesinski182f73f2013-12-05 16:48:06 -0800263 @Override
264 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700265 if (isUiModeLocked()) {
266 Slog.e(TAG, "disableCarMode while UI mode is locked");
267 return;
268 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800269 final long ident = Binder.clearCallingIdentity();
270 try {
271 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700272 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800273 if (mSystemReady) {
274 updateLocked(0, flags);
275 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700276 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800277 } finally {
278 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500279 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700280 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100281
Adam Lesinski182f73f2013-12-05 16:48:06 -0800282 @Override
283 public int getCurrentModeType() {
284 final long ident = Binder.clearCallingIdentity();
285 try {
286 synchronized (mLock) {
287 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700288 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800289 } finally {
290 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800291 }
292 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100293
Adam Lesinski182f73f2013-12-05 16:48:06 -0800294 @Override
295 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700296 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
297 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
298 != PackageManager.PERMISSION_GRANTED)) {
299 Slog.e(TAG,
300 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
301 return;
302 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800303 switch (mode) {
304 case UiModeManager.MODE_NIGHT_NO:
305 case UiModeManager.MODE_NIGHT_YES:
306 case UiModeManager.MODE_NIGHT_AUTO:
307 break;
308 default:
309 throw new IllegalArgumentException("Unknown mode: " + mode);
310 }
311
312 final long ident = Binder.clearCallingIdentity();
313 try {
314 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800315 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800316 Settings.Secure.putInt(getContext().getContentResolver(),
317 Settings.Secure.UI_NIGHT_MODE, mode);
318 mNightMode = mode;
319 updateLocked(0, 0);
320 }
321 }
322 } finally {
323 Binder.restoreCallingIdentity(ident);
324 }
325 }
326
327 @Override
328 public int getNightMode() {
329 synchronized (mLock) {
330 return mNightMode;
331 }
332 }
333
334 @Override
keunyounga7710492015-09-23 11:33:58 -0700335 public boolean isUiModeLocked() {
336 synchronized (mLock) {
337 return mUiModeLocked;
338 }
339 }
340
341 @Override
342 public boolean isNightModeLocked() {
343 synchronized (mLock) {
344 return mNightModeLocked;
345 }
346 }
347
348 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500349 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
350 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
351 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
352 }
353
354 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800355 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600356 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800357 dumpImpl(pw);
358 }
359 };
360
361 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700362 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800363 pw.println("Current UI Mode Service state:");
364 pw.print(" mDockState="); pw.print(mDockState);
365 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
366 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700367 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800368 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700369 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700370 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
371 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800372 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700373 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800374 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
375 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
376 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700377 if (mTwilightManager != null) {
378 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700379 pw.print(" mTwilightService.getLastTwilightState()=");
380 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700381 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700382 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800383 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100384
Adam Lesinski182f73f2013-12-05 16:48:06 -0800385 @Override
386 public void onBootPhase(int phase) {
387 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
388 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700389 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800390 mSystemReady = true;
391 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
392 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800393 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800394 updateLocked(0, 0);
395 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800396 }
397 }
398
keunyoung1d0a7cc2014-07-28 13:12:50 -0700399 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800400 if (mCarModeEnabled != enabled) {
401 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800402 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700403 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800404 }
405
Jeff Brown487bb6e2012-10-11 13:35:42 -0700406 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800407 synchronized (mLock) {
408 if (newState != mDockState) {
409 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700410 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800411 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700412 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800413 }
414 }
415 }
416 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500417
Jeff Brown487bb6e2012-10-11 13:35:42 -0700418 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400419 switch (state) {
420 case Intent.EXTRA_DOCK_STATE_DESK:
421 case Intent.EXTRA_DOCK_STATE_LE_DESK:
422 case Intent.EXTRA_DOCK_STATE_HE_DESK:
423 return true;
424 default:
425 return false;
426 }
427 }
428
Jeff Brown487bb6e2012-10-11 13:35:42 -0700429 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400430 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700431 if (mUiModeLocked) {
432 // no-op, keeps default one
433 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400434 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
435 } else if (mWatch) {
436 uiMode = Configuration.UI_MODE_TYPE_WATCH;
437 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800438 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400439 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800440 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800441 } else if (mVrHeadset) {
442 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800443 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800444
445 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700446 if (mTwilightManager != null) {
447 mTwilightManager.registerListener(mTwilightListener, mHandler);
448 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800449 updateComputedNightModeLocked();
450 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
451 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800452 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700453 if (mTwilightManager != null) {
454 mTwilightManager.unregisterListener(mTwilightListener);
455 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800456 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400457 }
458
459 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400460 Slog.d(TAG,
461 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400462 + "; mCarMode=" + mCarModeEnabled
463 + "; mNightMode=" + mNightMode
464 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800465 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100466
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800467 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700468 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700469 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700470 }
471 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100472
Jeff Brown487bb6e2012-10-11 13:35:42 -0700473 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700474 if (mSetUiMode != mConfiguration.uiMode) {
475 mSetUiMode = mConfiguration.uiMode;
476
477 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800478 ActivityManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700479 } catch (RemoteException e) {
480 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800481 }
482 }
483 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100484
Adam Lesinski182f73f2013-12-05 16:48:06 -0800485 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700486 String action = null;
487 String oldAction = null;
488 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
489 adjustStatusBarCarModeLocked();
490 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
491 } else if (isDeskDockState(mLastBroadcastState)) {
492 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
493 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100494
Jeff Brown487bb6e2012-10-11 13:35:42 -0700495 if (mCarModeEnabled) {
496 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100497 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700498 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700499 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700500 }
501 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
502 action = UiModeManager.ACTION_ENTER_CAR_MODE;
503 }
504 } else if (isDeskDockState(mDockState)) {
505 if (!isDeskDockState(mLastBroadcastState)) {
506 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700507 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700508 }
509 mLastBroadcastState = mDockState;
510 action = UiModeManager.ACTION_ENTER_DESK_MODE;
511 }
512 } else {
513 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
514 action = oldAction;
515 }
516
517 if (action != null) {
518 if (LOG) {
519 Slog.v(TAG, String.format(
520 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
521 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800522 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100523
Jeff Brown487bb6e2012-10-11 13:35:42 -0700524 // Send the ordered broadcast; the result receiver will receive after all
525 // broadcasts have been sent. If any broadcast receiver changes the result
526 // code from the initial value of RESULT_OK, then the result receiver will
527 // not launch the corresponding dock application. This gives apps a chance
528 // to override the behavior and stay in their app even when the device is
529 // placed into a dock.
530 Intent intent = new Intent(action);
531 intent.putExtra("enableFlags", enableFlags);
532 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700533 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800534 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700535 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100536
Jeff Brown487bb6e2012-10-11 13:35:42 -0700537 // Attempting to make this transition a little more clean, we are going
538 // to hold off on doing a configuration change until we have finished
539 // the broadcast and started the home activity.
540 mHoldingConfiguration = true;
541 updateConfigurationLocked();
542 } else {
543 String category = null;
544 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700545 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700546 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
547 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800548 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400549 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700550 if (ENABLE_LAUNCH_DESK_DOCK_APP
551 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
552 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800553 }
554 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700555 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
556 category = Intent.CATEGORY_HOME;
557 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800558 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100559
Jeff Brown487bb6e2012-10-11 13:35:42 -0700560 if (LOG) {
561 Slog.v(TAG, "updateLocked: null action, mDockState="
562 + mDockState +", category=" + category);
563 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400564
Jeff Brown487bb6e2012-10-11 13:35:42 -0700565 sendConfigurationAndStartDreamOrDockAppLocked(category);
566 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700567
Jeff Brown487bb6e2012-10-11 13:35:42 -0700568 // keep screen on when charging and in car mode
569 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700570 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700571 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700572 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
573 if (keepScreenOn != mWakeLock.isHeld()) {
574 if (keepScreenOn) {
575 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700576 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700577 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800578 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800579 }
580 }
581
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700582 private void sendForegroundBroadcastToAllUsers(String action) {
583 getContext().sendBroadcastAsUser(new Intent(action)
584 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
585 }
586
Jeff Brown62c82e42012-09-26 01:30:41 -0700587 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
588 // Launch a dock activity
589 String category = null;
590 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
591 // Only launch car home when car mode is enabled and the caller
592 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700593 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700594 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
595 category = Intent.CATEGORY_CAR_DOCK;
596 }
597 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
598 // Only launch car home when desk mode is enabled and the caller
599 // has asked us to switch to it. Currently re-using the car
600 // mode flag since we don't have a formal API for "desk mode".
601 if (ENABLE_LAUNCH_DESK_DOCK_APP
602 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
603 category = Intent.CATEGORY_DESK_DOCK;
604 }
605 } else {
606 // Launch the standard home app if requested.
607 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
608 category = Intent.CATEGORY_HOME;
609 }
610 }
611
612 if (LOG) {
613 Slog.v(TAG, String.format(
614 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
615 + "category=%s",
616 action, enableFlags, disableFlags, category));
617 }
618
619 sendConfigurationAndStartDreamOrDockAppLocked(category);
620 }
621
622 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
623 // Update the configuration but don't send it yet.
624 mHoldingConfiguration = false;
625 updateConfigurationLocked();
626
627 // Start the dock app, if there is one.
628 boolean dockAppStarted = false;
629 if (category != null) {
630 // Now we are going to be careful about switching the
631 // configuration and starting the activity -- we need to
632 // do this in a specific order under control of the
633 // activity manager, to do it cleanly. So compute the
634 // new config, but don't set it yet, and let the
635 // activity manager take care of both the start and config
636 // change.
637 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800638 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700639 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800640 int result = ActivityManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800641 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700642 mConfiguration, null, UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -0700643 if (ActivityManager.isStartResultSuccessful(result)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700644 dockAppStarted = true;
645 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
646 Slog.e(TAG, "Could not start dock app: " + homeIntent
647 + ", startActivityWithConfig result " + result);
648 }
649 } catch (RemoteException ex) {
650 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700651 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700652 }
653 }
654
655 // Send the new configuration.
656 sendConfigurationLocked();
657
658 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700659 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800660 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700661 }
662 }
663
Dianne Hackborn7299c412010-03-04 18:41:49 -0800664 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800665 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800666 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700667 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800668 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800669 }
670
Joe Onorato089de882010-04-12 08:18:45 -0700671 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800672 // features of the status bar; these are ORed together to form the
673 // active disabled list. So if (for example) the device is locked and
674 // the status bar should be totally disabled, the calls below will
675 // have no effect until the device is unlocked.
676 if (mStatusBarManager != null) {
677 mStatusBarManager.disable(mCarModeEnabled
678 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
679 : StatusBarManager.DISABLE_NONE);
680 }
681
682 if (mNotificationManager == null) {
683 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800684 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800685 }
686
687 if (mNotificationManager != null) {
688 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800689 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800690
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500691 Notification.Builder n =
692 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400693 .setSmallIcon(R.drawable.stat_notify_car_mode)
694 .setDefaults(Notification.DEFAULT_LIGHTS)
695 .setOngoing(true)
696 .setWhen(0)
697 .setColor(context.getColor(
698 com.android.internal.R.color.system_notification_accent_color))
699 .setContentTitle(
700 context.getString(R.string.car_mode_disable_notification_title))
701 .setContentText(
702 context.getString(R.string.car_mode_disable_notification_message))
703 .setContentIntent(
704 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
705 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700706 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400707 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800708 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700709 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400710 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800711 }
712 }
713 }
714
Jeff Brown2416e092012-08-21 22:12:20 -0700715 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700716 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700717 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700718 if (state != null) {
719 mComputedNightMode = state.isNight();
720 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800721 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800722 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100723
Zak Cohen1a705732017-01-09 12:54:34 -0800724 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800725 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
726 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800727 try {
728 if (vrManager != null) {
729 vrManager.registerListener(mVrStateCallbacks);
730 }
731 } catch (RemoteException e) {
732 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
733 }
734 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100735
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500736 /**
737 * Handles "adb shell" commands.
738 */
739 private static class Shell extends ShellCommand {
740 public static final String NIGHT_MODE_STR_YES = "yes";
741 public static final String NIGHT_MODE_STR_NO = "no";
742 public static final String NIGHT_MODE_STR_AUTO = "auto";
743 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
744 private final IUiModeManager mInterface;
745
746 Shell(IUiModeManager iface) {
747 mInterface = iface;
748 }
749
750 @Override
751 public void onHelp() {
752 final PrintWriter pw = getOutPrintWriter();
753 pw.println("UiModeManager service (uimode) commands:");
754 pw.println(" help");
755 pw.println(" Print this help text.");
756 pw.println(" night [yes|no|auto]");
757 pw.println(" Set or read night mode.");
758 }
759
760 @Override
761 public int onCommand(String cmd) {
762 if (cmd == null) {
763 return handleDefaultCommands(cmd);
764 }
765
766 try {
767 switch (cmd) {
768 case "night":
769 return handleNightMode();
770 default:
771 return handleDefaultCommands(cmd);
772 }
773 } catch (RemoteException e) {
774 final PrintWriter err = getErrPrintWriter();
775 err.println("Remote exception: " + e);
776 }
777 return -1;
778 }
779
780 private int handleNightMode() throws RemoteException {
781 final PrintWriter err = getErrPrintWriter();
782 final String modeStr = getNextArg();
783 if (modeStr == null) {
784 printCurrentNightMode();
785 return 0;
786 }
787
788 final int mode = strToNightMode(modeStr);
789 if (mode >= 0) {
790 mInterface.setNightMode(mode);
791 printCurrentNightMode();
792 return 0;
793 } else {
794 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
795 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
796 return -1;
797 }
798 }
799
800 private void printCurrentNightMode() throws RemoteException {
801 final PrintWriter pw = getOutPrintWriter();
802 final int currMode = mInterface.getNightMode();
803 final String currModeStr = nightModeToStr(currMode);
804 pw.println("Night mode: " + currModeStr);
805 }
806
807 private static String nightModeToStr(int mode) {
808 switch (mode) {
809 case UiModeManager.MODE_NIGHT_YES:
810 return NIGHT_MODE_STR_YES;
811 case UiModeManager.MODE_NIGHT_NO:
812 return NIGHT_MODE_STR_NO;
813 case UiModeManager.MODE_NIGHT_AUTO:
814 return NIGHT_MODE_STR_AUTO;
815 default:
816 return NIGHT_MODE_STR_UNKNOWN;
817 }
818 }
819
820 private static int strToNightMode(String modeStr) {
821 switch (modeStr) {
822 case NIGHT_MODE_STR_YES:
823 return UiModeManager.MODE_NIGHT_YES;
824 case NIGHT_MODE_STR_NO:
825 return UiModeManager.MODE_NIGHT_NO;
826 case NIGHT_MODE_STR_AUTO:
827 return UiModeManager.MODE_NIGHT_AUTO;
828 default:
829 return -1;
830 }
831 }
832 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800833}