blob: 227e2a2ac3e4205d25dd391e3a79459dfc63fa7c [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;
Jason Monk97a06a12016-11-09 15:53:30 -050063import com.android.server.power.ShutdownThread;
Adam Lesinski182f73f2013-12-05 16:48:06 -080064import com.android.server.twilight.TwilightListener;
65import com.android.server.twilight.TwilightManager;
66import com.android.server.twilight.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080067
Adam Lesinski182f73f2013-12-05 16:48:06 -080068final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080069 private static final String TAG = UiModeManager.class.getSimpleName();
70 private static final boolean LOG = false;
71
Daniel Sandler11ddf532011-11-16 11:10:03 -080072 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040073 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080074
Dianne Hackborn7299c412010-03-04 18:41:49 -080075 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080076 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010077
Adam Lesinski182f73f2013-12-05 16:48:06 -080078 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080079 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080080
Dianne Hackborn7299c412010-03-04 18:41:49 -080081 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050082 private boolean mCharging = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080083 private int mDefaultUiModeType;
84 private boolean mCarModeKeepsScreenOn;
85 private boolean mDeskModeKeepsScreenOn;
86 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040087 private boolean mWatch;
Zak Cohen1a705732017-01-09 12:54:34 -080088 private boolean mVrHeadset;
Dianne Hackborn7299c412010-03-04 18:41:49 -080089 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070090 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080091
keunyounga7710492015-09-23 11:33:58 -070092 // flag set by resource, whether to enable Car dock launch when starting car mode.
93 private boolean mEnableCarDockLaunch = true;
94 // flag set by resource, whether to lock UI mode to the default one or not.
95 private boolean mUiModeLocked = false;
96 // flag set by resource, whether to night mode change for normal all or not.
97 private boolean mNightModeLocked = false;
98
Adam Lesinski182f73f2013-12-05 16:48:06 -080099 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800100 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800101 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800102
Dianne Hackborn7299c412010-03-04 18:41:49 -0800103 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800104 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100105
Adam Lesinski182f73f2013-12-05 16:48:06 -0800106 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800107
Adam Lesinski182f73f2013-12-05 16:48:06 -0800108 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800109 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800110 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700111
Adam Lesinski182f73f2013-12-05 16:48:06 -0800112 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800113
Jeff Brownb880d882014-02-10 19:47:07 -0800114 public UiModeManagerService(Context context) {
115 super(context);
116 }
117
Adam Lesinski182f73f2013-12-05 16:48:06 -0800118 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700119 Intent intent = new Intent(Intent.ACTION_MAIN);
120 intent.addCategory(category);
121 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
122 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
123 return intent;
124 }
John Spurlock960779d2012-05-29 14:37:05 -0400125
Dianne Hackborn7299c412010-03-04 18:41:49 -0800126 // The broadcast receiver which receives the result of the ordered broadcast sent when
127 // the dock state changes. The original ordered broadcast is sent with an initial result
128 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
129 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
130 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
131 @Override
132 public void onReceive(Context context, Intent intent) {
133 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400134 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400135 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400136 + ": canceled: " + getResultCode());
137 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800138 return;
139 }
140
Jeff Brown62c82e42012-09-26 01:30:41 -0700141 final int enableFlags = intent.getIntExtra("enableFlags", 0);
142 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800143 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700144 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800145 }
146 }
147 };
148
Dianne Hackborn7299c412010-03-04 18:41:49 -0800149 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
150 @Override
151 public void onReceive(Context context, Intent intent) {
152 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
153 Intent.EXTRA_DOCK_STATE_UNDOCKED);
154 updateDockState(state);
155 }
156 };
157
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500158 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
159 @Override
160 public void onReceive(Context context, Intent intent) {
161 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
162 synchronized (mLock) {
163 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700164 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500165 }
166 }
167 }
168 };
169
Adam Lesinski182f73f2013-12-05 16:48:06 -0800170 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700171 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700172 public void onTwilightStateChanged(@Nullable TwilightState state) {
173 synchronized (mLock) {
174 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
175 updateComputedNightModeLocked();
176 updateLocked(0, 0);
177 }
178 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700179 }
180 };
181
Zak Cohen1a705732017-01-09 12:54:34 -0800182 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
183 @Override
184 public void onVrStateChanged(boolean enabled) {
185 synchronized (mLock) {
186 mVrHeadset = enabled;
187 if (mSystemReady) {
188 updateLocked(0, 0);
189 }
190 }
191 }
192 };
193
Adam Lesinski182f73f2013-12-05 16:48:06 -0800194 @Override
195 public void onStart() {
196 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800197
Adam Lesinski182f73f2013-12-05 16:48:06 -0800198 final PowerManager powerManager =
199 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
200 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800201
Adam Lesinski182f73f2013-12-05 16:48:06 -0800202 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800203 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800204 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500205 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
206
Dianne Hackborn7299c412010-03-04 18:41:49 -0800207 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500208
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800209 final Resources res = context.getResources();
210 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800211 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800212 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500213 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800214 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500215 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700216 mEnableCarDockLaunch = res.getBoolean(
217 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
218 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
219 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700220
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800221 final PackageManager pm = context.getPackageManager();
222 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
223 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
224 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
225
226 final int defaultNightMode = res.getInteger(
227 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800228 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800229 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700230
Adam Lesinski05199e82015-03-19 14:37:11 -0700231 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800232 SystemServerInitThreadPool.get().submit(() -> {
233 synchronized (mLock) {
234 updateConfigurationLocked();
235 sendConfigurationLocked();
236 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800237
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800238 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800239 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800240 }
241
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500242 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800243 @Override
244 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700245 if (isUiModeLocked()) {
246 Slog.e(TAG, "enableCarMode while UI mode is locked");
247 return;
248 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800249 final long ident = Binder.clearCallingIdentity();
250 try {
251 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700252 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800253 if (mSystemReady) {
254 updateLocked(flags, 0);
255 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700256 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800257 } finally {
258 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500259 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800260 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800261
Adam Lesinski182f73f2013-12-05 16:48:06 -0800262 @Override
263 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700264 if (isUiModeLocked()) {
265 Slog.e(TAG, "disableCarMode while UI mode is locked");
266 return;
267 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800268 final long ident = Binder.clearCallingIdentity();
269 try {
270 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700271 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800272 if (mSystemReady) {
273 updateLocked(0, flags);
274 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700275 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800276 } finally {
277 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500278 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700279 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100280
Adam Lesinski182f73f2013-12-05 16:48:06 -0800281 @Override
282 public int getCurrentModeType() {
283 final long ident = Binder.clearCallingIdentity();
284 try {
285 synchronized (mLock) {
286 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700287 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800288 } finally {
289 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800290 }
291 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100292
Adam Lesinski182f73f2013-12-05 16:48:06 -0800293 @Override
294 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700295 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
296 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
297 != PackageManager.PERMISSION_GRANTED)) {
298 Slog.e(TAG,
299 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
300 return;
301 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800302 switch (mode) {
303 case UiModeManager.MODE_NIGHT_NO:
304 case UiModeManager.MODE_NIGHT_YES:
305 case UiModeManager.MODE_NIGHT_AUTO:
306 break;
307 default:
308 throw new IllegalArgumentException("Unknown mode: " + mode);
309 }
310
311 final long ident = Binder.clearCallingIdentity();
312 try {
313 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800314 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800315 Settings.Secure.putInt(getContext().getContentResolver(),
316 Settings.Secure.UI_NIGHT_MODE, mode);
317 mNightMode = mode;
318 updateLocked(0, 0);
319 }
320 }
321 } finally {
322 Binder.restoreCallingIdentity(ident);
323 }
324 }
325
326 @Override
327 public int getNightMode() {
328 synchronized (mLock) {
329 return mNightMode;
330 }
331 }
332
333 @Override
keunyounga7710492015-09-23 11:33:58 -0700334 public boolean isUiModeLocked() {
335 synchronized (mLock) {
336 return mUiModeLocked;
337 }
338 }
339
340 @Override
341 public boolean isNightModeLocked() {
342 synchronized (mLock) {
343 return mNightModeLocked;
344 }
345 }
346
347 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500348 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
349 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
350 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
351 }
352
353 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800354 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
355 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
356 != PackageManager.PERMISSION_GRANTED) {
357
358 pw.println("Permission Denial: can't dump uimode service from from pid="
359 + Binder.getCallingPid()
360 + ", uid=" + Binder.getCallingUid());
361 return;
362 }
363
364 dumpImpl(pw);
365 }
366 };
367
368 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700369 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800370 pw.println("Current UI Mode Service state:");
371 pw.print(" mDockState="); pw.print(mDockState);
372 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
373 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700374 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800375 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700376 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700377 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
378 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800379 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700380 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800381 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
382 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
383 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700384 if (mTwilightManager != null) {
385 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700386 pw.print(" mTwilightService.getLastTwilightState()=");
387 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700388 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700389 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800390 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100391
Adam Lesinski182f73f2013-12-05 16:48:06 -0800392 @Override
393 public void onBootPhase(int phase) {
394 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
395 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700396 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800397 mSystemReady = true;
398 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
399 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800400 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800401 updateLocked(0, 0);
402 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800403 }
404 }
405
keunyoung1d0a7cc2014-07-28 13:12:50 -0700406 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800407 if (mCarModeEnabled != enabled) {
408 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800409 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700410 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800411 }
412
Jeff Brown487bb6e2012-10-11 13:35:42 -0700413 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800414 synchronized (mLock) {
415 if (newState != mDockState) {
416 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700417 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800418 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700419 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800420 }
421 }
422 }
423 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500424
Jeff Brown487bb6e2012-10-11 13:35:42 -0700425 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400426 switch (state) {
427 case Intent.EXTRA_DOCK_STATE_DESK:
428 case Intent.EXTRA_DOCK_STATE_LE_DESK:
429 case Intent.EXTRA_DOCK_STATE_HE_DESK:
430 return true;
431 default:
432 return false;
433 }
434 }
435
Jeff Brown487bb6e2012-10-11 13:35:42 -0700436 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400437 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700438 if (mUiModeLocked) {
439 // no-op, keeps default one
440 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400441 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
442 } else if (mWatch) {
443 uiMode = Configuration.UI_MODE_TYPE_WATCH;
444 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800445 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400446 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800447 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800448 } else if (mVrHeadset) {
449 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800450 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800451
452 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700453 if (mTwilightManager != null) {
454 mTwilightManager.registerListener(mTwilightListener, mHandler);
455 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800456 updateComputedNightModeLocked();
457 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
458 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800459 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700460 if (mTwilightManager != null) {
461 mTwilightManager.unregisterListener(mTwilightListener);
462 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800463 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400464 }
465
466 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400467 Slog.d(TAG,
468 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400469 + "; mCarMode=" + mCarModeEnabled
470 + "; mNightMode=" + mNightMode
471 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800472 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100473
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800474 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700475 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700476 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700477 }
478 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100479
Jeff Brown487bb6e2012-10-11 13:35:42 -0700480 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700481 if (mSetUiMode != mConfiguration.uiMode) {
482 mSetUiMode = mConfiguration.uiMode;
483
484 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800485 ActivityManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700486 } catch (RemoteException e) {
487 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800488 }
489 }
490 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100491
Adam Lesinski182f73f2013-12-05 16:48:06 -0800492 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700493 String action = null;
494 String oldAction = null;
495 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
496 adjustStatusBarCarModeLocked();
497 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
498 } else if (isDeskDockState(mLastBroadcastState)) {
499 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
500 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100501
Jeff Brown487bb6e2012-10-11 13:35:42 -0700502 if (mCarModeEnabled) {
503 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100504 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700505 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700506 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700507 }
508 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
509 action = UiModeManager.ACTION_ENTER_CAR_MODE;
510 }
511 } else if (isDeskDockState(mDockState)) {
512 if (!isDeskDockState(mLastBroadcastState)) {
513 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700514 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700515 }
516 mLastBroadcastState = mDockState;
517 action = UiModeManager.ACTION_ENTER_DESK_MODE;
518 }
519 } else {
520 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
521 action = oldAction;
522 }
523
524 if (action != null) {
525 if (LOG) {
526 Slog.v(TAG, String.format(
527 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
528 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800529 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100530
Jeff Brown487bb6e2012-10-11 13:35:42 -0700531 // Send the ordered broadcast; the result receiver will receive after all
532 // broadcasts have been sent. If any broadcast receiver changes the result
533 // code from the initial value of RESULT_OK, then the result receiver will
534 // not launch the corresponding dock application. This gives apps a chance
535 // to override the behavior and stay in their app even when the device is
536 // placed into a dock.
537 Intent intent = new Intent(action);
538 intent.putExtra("enableFlags", enableFlags);
539 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700540 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800541 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700542 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100543
Jeff Brown487bb6e2012-10-11 13:35:42 -0700544 // Attempting to make this transition a little more clean, we are going
545 // to hold off on doing a configuration change until we have finished
546 // the broadcast and started the home activity.
547 mHoldingConfiguration = true;
548 updateConfigurationLocked();
549 } else {
550 String category = null;
551 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700552 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700553 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
554 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800555 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400556 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700557 if (ENABLE_LAUNCH_DESK_DOCK_APP
558 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
559 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800560 }
561 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700562 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
563 category = Intent.CATEGORY_HOME;
564 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800565 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100566
Jeff Brown487bb6e2012-10-11 13:35:42 -0700567 if (LOG) {
568 Slog.v(TAG, "updateLocked: null action, mDockState="
569 + mDockState +", category=" + category);
570 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400571
Jeff Brown487bb6e2012-10-11 13:35:42 -0700572 sendConfigurationAndStartDreamOrDockAppLocked(category);
573 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700574
Jeff Brown487bb6e2012-10-11 13:35:42 -0700575 // keep screen on when charging and in car mode
576 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700577 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700578 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700579 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
580 if (keepScreenOn != mWakeLock.isHeld()) {
581 if (keepScreenOn) {
582 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700583 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700584 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800585 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800586 }
587 }
588
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700589 private void sendForegroundBroadcastToAllUsers(String action) {
590 getContext().sendBroadcastAsUser(new Intent(action)
591 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
592 }
593
Jeff Brown62c82e42012-09-26 01:30:41 -0700594 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
595 // Launch a dock activity
596 String category = null;
597 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
598 // Only launch car home when car mode is enabled and the caller
599 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700600 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700601 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
602 category = Intent.CATEGORY_CAR_DOCK;
603 }
604 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
605 // Only launch car home when desk mode is enabled and the caller
606 // has asked us to switch to it. Currently re-using the car
607 // mode flag since we don't have a formal API for "desk mode".
608 if (ENABLE_LAUNCH_DESK_DOCK_APP
609 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
610 category = Intent.CATEGORY_DESK_DOCK;
611 }
612 } else {
613 // Launch the standard home app if requested.
614 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
615 category = Intent.CATEGORY_HOME;
616 }
617 }
618
619 if (LOG) {
620 Slog.v(TAG, String.format(
621 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
622 + "category=%s",
623 action, enableFlags, disableFlags, category));
624 }
625
626 sendConfigurationAndStartDreamOrDockAppLocked(category);
627 }
628
629 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
630 // Update the configuration but don't send it yet.
631 mHoldingConfiguration = false;
632 updateConfigurationLocked();
633
634 // Start the dock app, if there is one.
635 boolean dockAppStarted = false;
636 if (category != null) {
637 // Now we are going to be careful about switching the
638 // configuration and starting the activity -- we need to
639 // do this in a specific order under control of the
640 // activity manager, to do it cleanly. So compute the
641 // new config, but don't set it yet, and let the
642 // activity manager take care of both the start and config
643 // change.
644 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800645 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700646 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800647 int result = ActivityManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800648 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700649 mConfiguration, null, UserHandle.USER_CURRENT);
650 if (result >= ActivityManager.START_SUCCESS) {
651 dockAppStarted = true;
652 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
653 Slog.e(TAG, "Could not start dock app: " + homeIntent
654 + ", startActivityWithConfig result " + result);
655 }
656 } catch (RemoteException ex) {
657 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700658 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700659 }
660 }
661
662 // Send the new configuration.
663 sendConfigurationLocked();
664
665 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700666 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800667 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700668 }
669 }
670
Dianne Hackborn7299c412010-03-04 18:41:49 -0800671 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800672 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800673 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700674 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800675 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800676 }
677
Joe Onorato089de882010-04-12 08:18:45 -0700678 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800679 // features of the status bar; these are ORed together to form the
680 // active disabled list. So if (for example) the device is locked and
681 // the status bar should be totally disabled, the calls below will
682 // have no effect until the device is unlocked.
683 if (mStatusBarManager != null) {
684 mStatusBarManager.disable(mCarModeEnabled
685 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
686 : StatusBarManager.DISABLE_NONE);
687 }
688
689 if (mNotificationManager == null) {
690 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800691 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800692 }
693
694 if (mNotificationManager != null) {
695 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800696 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800697
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500698 Notification.Builder n =
699 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400700 .setSmallIcon(R.drawable.stat_notify_car_mode)
701 .setDefaults(Notification.DEFAULT_LIGHTS)
702 .setOngoing(true)
703 .setWhen(0)
704 .setColor(context.getColor(
705 com.android.internal.R.color.system_notification_accent_color))
706 .setContentTitle(
707 context.getString(R.string.car_mode_disable_notification_title))
708 .setContentText(
709 context.getString(R.string.car_mode_disable_notification_message))
710 .setContentIntent(
711 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
712 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700713 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400714 SystemMessage.NOTE_CAR_MODE_DISABLE, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800715 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700716 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -0400717 SystemMessage.NOTE_CAR_MODE_DISABLE, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800718 }
719 }
720 }
721
Jeff Brown2416e092012-08-21 22:12:20 -0700722 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700723 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700724 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700725 if (state != null) {
726 mComputedNightMode = state.isNight();
727 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800728 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800729 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100730
Zak Cohen1a705732017-01-09 12:54:34 -0800731 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800732 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
733 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800734 try {
735 if (vrManager != null) {
736 vrManager.registerListener(mVrStateCallbacks);
737 }
738 } catch (RemoteException e) {
739 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
740 }
741 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100742
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500743 /**
744 * Handles "adb shell" commands.
745 */
746 private static class Shell extends ShellCommand {
747 public static final String NIGHT_MODE_STR_YES = "yes";
748 public static final String NIGHT_MODE_STR_NO = "no";
749 public static final String NIGHT_MODE_STR_AUTO = "auto";
750 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
751 private final IUiModeManager mInterface;
752
753 Shell(IUiModeManager iface) {
754 mInterface = iface;
755 }
756
757 @Override
758 public void onHelp() {
759 final PrintWriter pw = getOutPrintWriter();
760 pw.println("UiModeManager service (uimode) commands:");
761 pw.println(" help");
762 pw.println(" Print this help text.");
763 pw.println(" night [yes|no|auto]");
764 pw.println(" Set or read night mode.");
765 }
766
767 @Override
768 public int onCommand(String cmd) {
769 if (cmd == null) {
770 return handleDefaultCommands(cmd);
771 }
772
773 try {
774 switch (cmd) {
775 case "night":
776 return handleNightMode();
777 default:
778 return handleDefaultCommands(cmd);
779 }
780 } catch (RemoteException e) {
781 final PrintWriter err = getErrPrintWriter();
782 err.println("Remote exception: " + e);
783 }
784 return -1;
785 }
786
787 private int handleNightMode() throws RemoteException {
788 final PrintWriter err = getErrPrintWriter();
789 final String modeStr = getNextArg();
790 if (modeStr == null) {
791 printCurrentNightMode();
792 return 0;
793 }
794
795 final int mode = strToNightMode(modeStr);
796 if (mode >= 0) {
797 mInterface.setNightMode(mode);
798 printCurrentNightMode();
799 return 0;
800 } else {
801 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
802 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
803 return -1;
804 }
805 }
806
807 private void printCurrentNightMode() throws RemoteException {
808 final PrintWriter pw = getOutPrintWriter();
809 final int currMode = mInterface.getNightMode();
810 final String currModeStr = nightModeToStr(currMode);
811 pw.println("Night mode: " + currModeStr);
812 }
813
814 private static String nightModeToStr(int mode) {
815 switch (mode) {
816 case UiModeManager.MODE_NIGHT_YES:
817 return NIGHT_MODE_STR_YES;
818 case UiModeManager.MODE_NIGHT_NO:
819 return NIGHT_MODE_STR_NO;
820 case UiModeManager.MODE_NIGHT_AUTO:
821 return NIGHT_MODE_STR_AUTO;
822 default:
823 return NIGHT_MODE_STR_UNKNOWN;
824 }
825 }
826
827 private static int strToNightMode(String modeStr) {
828 switch (modeStr) {
829 case NIGHT_MODE_STR_YES:
830 return UiModeManager.MODE_NIGHT_YES;
831 case NIGHT_MODE_STR_NO:
832 return UiModeManager.MODE_NIGHT_NO;
833 case NIGHT_MODE_STR_AUTO:
834 return UiModeManager.MODE_NIGHT_AUTO;
835 default:
836 return -1;
837 }
838 }
839 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800840}