blob: e4f4687326863264e91043d498b45ee405aae36a [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;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050061import com.android.internal.notification.SystemNotificationChannels;
Jason Monk97a06a12016-11-09 15:53:30 -050062import com.android.server.power.ShutdownThread;
Adam Lesinski182f73f2013-12-05 16:48:06 -080063import com.android.server.twilight.TwilightListener;
64import com.android.server.twilight.TwilightManager;
65import com.android.server.twilight.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080066
Adam Lesinski182f73f2013-12-05 16:48:06 -080067final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080068 private static final String TAG = UiModeManager.class.getSimpleName();
69 private static final boolean LOG = false;
70
Daniel Sandler11ddf532011-11-16 11:10:03 -080071 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040072 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080073
Dianne Hackborn7299c412010-03-04 18:41:49 -080074 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080075 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010076
Adam Lesinski182f73f2013-12-05 16:48:06 -080077 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080078 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080079
Dianne Hackborn7299c412010-03-04 18:41:49 -080080 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050081 private boolean mCharging = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080082 private int mDefaultUiModeType;
83 private boolean mCarModeKeepsScreenOn;
84 private boolean mDeskModeKeepsScreenOn;
85 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040086 private boolean mWatch;
Zak Cohen1a705732017-01-09 12:54:34 -080087 private boolean mVrHeadset;
Dianne Hackborn7299c412010-03-04 18:41:49 -080088 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070089 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080090
keunyounga7710492015-09-23 11:33:58 -070091 // flag set by resource, whether to enable Car dock launch when starting car mode.
92 private boolean mEnableCarDockLaunch = true;
93 // flag set by resource, whether to lock UI mode to the default one or not.
94 private boolean mUiModeLocked = false;
95 // flag set by resource, whether to night mode change for normal all or not.
96 private boolean mNightModeLocked = false;
97
Adam Lesinski182f73f2013-12-05 16:48:06 -080098 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080099 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800100 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800101
Dianne Hackborn7299c412010-03-04 18:41:49 -0800102 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800103 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100104
Adam Lesinski182f73f2013-12-05 16:48:06 -0800105 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800106
Adam Lesinski182f73f2013-12-05 16:48:06 -0800107 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800108 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800109 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700110
Adam Lesinski182f73f2013-12-05 16:48:06 -0800111 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800112
Jeff Brownb880d882014-02-10 19:47:07 -0800113 public UiModeManagerService(Context context) {
114 super(context);
115 }
116
Adam Lesinski182f73f2013-12-05 16:48:06 -0800117 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700118 Intent intent = new Intent(Intent.ACTION_MAIN);
119 intent.addCategory(category);
120 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
121 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
122 return intent;
123 }
John Spurlock960779d2012-05-29 14:37:05 -0400124
Dianne Hackborn7299c412010-03-04 18:41:49 -0800125 // The broadcast receiver which receives the result of the ordered broadcast sent when
126 // the dock state changes. The original ordered broadcast is sent with an initial result
127 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
128 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
129 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
130 @Override
131 public void onReceive(Context context, Intent intent) {
132 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400133 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400134 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400135 + ": canceled: " + getResultCode());
136 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800137 return;
138 }
139
Jeff Brown62c82e42012-09-26 01:30:41 -0700140 final int enableFlags = intent.getIntExtra("enableFlags", 0);
141 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800142 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700143 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800144 }
145 }
146 };
147
Dianne Hackborn7299c412010-03-04 18:41:49 -0800148 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
149 @Override
150 public void onReceive(Context context, Intent intent) {
151 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
152 Intent.EXTRA_DOCK_STATE_UNDOCKED);
153 updateDockState(state);
154 }
155 };
156
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500157 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
158 @Override
159 public void onReceive(Context context, Intent intent) {
160 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
161 synchronized (mLock) {
162 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700163 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500164 }
165 }
166 }
167 };
168
Adam Lesinski182f73f2013-12-05 16:48:06 -0800169 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700170 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700171 public void onTwilightStateChanged(@Nullable TwilightState state) {
172 synchronized (mLock) {
173 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
174 updateComputedNightModeLocked();
175 updateLocked(0, 0);
176 }
177 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700178 }
179 };
180
Zak Cohen1a705732017-01-09 12:54:34 -0800181 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
182 @Override
183 public void onVrStateChanged(boolean enabled) {
184 synchronized (mLock) {
185 mVrHeadset = enabled;
186 if (mSystemReady) {
187 updateLocked(0, 0);
188 }
189 }
190 }
191 };
192
Adam Lesinski182f73f2013-12-05 16:48:06 -0800193 @Override
194 public void onStart() {
195 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800196
Adam Lesinski182f73f2013-12-05 16:48:06 -0800197 final PowerManager powerManager =
198 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
199 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800200
Adam Lesinski182f73f2013-12-05 16:48:06 -0800201 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800202 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800203 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500204 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
205
Dianne Hackborn7299c412010-03-04 18:41:49 -0800206 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500207
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800208 final Resources res = context.getResources();
209 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800210 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800211 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500212 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800213 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500214 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700215 mEnableCarDockLaunch = res.getBoolean(
216 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
217 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
218 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700219
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800220 final PackageManager pm = context.getPackageManager();
221 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
222 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
223 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
224
225 final int defaultNightMode = res.getInteger(
226 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800227 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800228 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700229
Adam Lesinski05199e82015-03-19 14:37:11 -0700230 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800231 SystemServerInitThreadPool.get().submit(() -> {
232 synchronized (mLock) {
233 updateConfigurationLocked();
234 sendConfigurationLocked();
235 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800236
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800237 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800238 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800239 }
240
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500241 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800242 @Override
243 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700244 if (isUiModeLocked()) {
245 Slog.e(TAG, "enableCarMode while UI mode is locked");
246 return;
247 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800248 final long ident = Binder.clearCallingIdentity();
249 try {
250 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700251 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800252 if (mSystemReady) {
253 updateLocked(flags, 0);
254 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700255 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800256 } finally {
257 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500258 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800259 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800260
Adam Lesinski182f73f2013-12-05 16:48:06 -0800261 @Override
262 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700263 if (isUiModeLocked()) {
264 Slog.e(TAG, "disableCarMode while UI mode is locked");
265 return;
266 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800267 final long ident = Binder.clearCallingIdentity();
268 try {
269 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700270 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800271 if (mSystemReady) {
272 updateLocked(0, flags);
273 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700274 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800275 } finally {
276 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500277 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700278 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100279
Adam Lesinski182f73f2013-12-05 16:48:06 -0800280 @Override
281 public int getCurrentModeType() {
282 final long ident = Binder.clearCallingIdentity();
283 try {
284 synchronized (mLock) {
285 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700286 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800287 } finally {
288 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800289 }
290 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100291
Adam Lesinski182f73f2013-12-05 16:48:06 -0800292 @Override
293 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700294 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
295 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
296 != PackageManager.PERMISSION_GRANTED)) {
297 Slog.e(TAG,
298 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
299 return;
300 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800301 switch (mode) {
302 case UiModeManager.MODE_NIGHT_NO:
303 case UiModeManager.MODE_NIGHT_YES:
304 case UiModeManager.MODE_NIGHT_AUTO:
305 break;
306 default:
307 throw new IllegalArgumentException("Unknown mode: " + mode);
308 }
309
310 final long ident = Binder.clearCallingIdentity();
311 try {
312 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800313 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800314 Settings.Secure.putInt(getContext().getContentResolver(),
315 Settings.Secure.UI_NIGHT_MODE, mode);
316 mNightMode = mode;
317 updateLocked(0, 0);
318 }
319 }
320 } finally {
321 Binder.restoreCallingIdentity(ident);
322 }
323 }
324
325 @Override
326 public int getNightMode() {
327 synchronized (mLock) {
328 return mNightMode;
329 }
330 }
331
332 @Override
keunyounga7710492015-09-23 11:33:58 -0700333 public boolean isUiModeLocked() {
334 synchronized (mLock) {
335 return mUiModeLocked;
336 }
337 }
338
339 @Override
340 public boolean isNightModeLocked() {
341 synchronized (mLock) {
342 return mNightModeLocked;
343 }
344 }
345
346 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500347 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
348 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
349 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
350 }
351
352 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800353 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
354 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
355 != PackageManager.PERMISSION_GRANTED) {
356
357 pw.println("Permission Denial: can't dump uimode service from from pid="
358 + Binder.getCallingPid()
359 + ", uid=" + Binder.getCallingUid());
360 return;
361 }
362
363 dumpImpl(pw);
364 }
365 };
366
367 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700368 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800369 pw.println("Current UI Mode Service state:");
370 pw.print(" mDockState="); pw.print(mDockState);
371 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
372 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700373 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800374 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700375 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700376 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
377 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800378 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700379 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800380 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
381 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
382 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700383 if (mTwilightManager != null) {
384 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700385 pw.print(" mTwilightService.getLastTwilightState()=");
386 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700387 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700388 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800389 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100390
Adam Lesinski182f73f2013-12-05 16:48:06 -0800391 @Override
392 public void onBootPhase(int phase) {
393 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
394 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700395 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800396 mSystemReady = true;
397 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
398 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800399 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800400 updateLocked(0, 0);
401 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800402 }
403 }
404
keunyoung1d0a7cc2014-07-28 13:12:50 -0700405 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800406 if (mCarModeEnabled != enabled) {
407 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800408 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700409 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800410 }
411
Jeff Brown487bb6e2012-10-11 13:35:42 -0700412 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800413 synchronized (mLock) {
414 if (newState != mDockState) {
415 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700416 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800417 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700418 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800419 }
420 }
421 }
422 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500423
Jeff Brown487bb6e2012-10-11 13:35:42 -0700424 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400425 switch (state) {
426 case Intent.EXTRA_DOCK_STATE_DESK:
427 case Intent.EXTRA_DOCK_STATE_LE_DESK:
428 case Intent.EXTRA_DOCK_STATE_HE_DESK:
429 return true;
430 default:
431 return false;
432 }
433 }
434
Jeff Brown487bb6e2012-10-11 13:35:42 -0700435 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400436 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700437 if (mUiModeLocked) {
438 // no-op, keeps default one
439 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400440 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
441 } else if (mWatch) {
442 uiMode = Configuration.UI_MODE_TYPE_WATCH;
443 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800444 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400445 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800446 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800447 } else if (mVrHeadset) {
448 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800449 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800450
451 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700452 if (mTwilightManager != null) {
453 mTwilightManager.registerListener(mTwilightListener, mHandler);
454 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800455 updateComputedNightModeLocked();
456 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
457 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800458 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700459 if (mTwilightManager != null) {
460 mTwilightManager.unregisterListener(mTwilightListener);
461 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800462 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400463 }
464
465 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400466 Slog.d(TAG,
467 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400468 + "; mCarMode=" + mCarModeEnabled
469 + "; mNightMode=" + mNightMode
470 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800471 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100472
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800473 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700474 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700475 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700476 }
477 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100478
Jeff Brown487bb6e2012-10-11 13:35:42 -0700479 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700480 if (mSetUiMode != mConfiguration.uiMode) {
481 mSetUiMode = mConfiguration.uiMode;
482
483 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800484 ActivityManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700485 } catch (RemoteException e) {
486 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800487 }
488 }
489 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100490
Adam Lesinski182f73f2013-12-05 16:48:06 -0800491 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700492 String action = null;
493 String oldAction = null;
494 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
495 adjustStatusBarCarModeLocked();
496 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
497 } else if (isDeskDockState(mLastBroadcastState)) {
498 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
499 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100500
Jeff Brown487bb6e2012-10-11 13:35:42 -0700501 if (mCarModeEnabled) {
502 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100503 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700504 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700505 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700506 }
507 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
508 action = UiModeManager.ACTION_ENTER_CAR_MODE;
509 }
510 } else if (isDeskDockState(mDockState)) {
511 if (!isDeskDockState(mLastBroadcastState)) {
512 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700513 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700514 }
515 mLastBroadcastState = mDockState;
516 action = UiModeManager.ACTION_ENTER_DESK_MODE;
517 }
518 } else {
519 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
520 action = oldAction;
521 }
522
523 if (action != null) {
524 if (LOG) {
525 Slog.v(TAG, String.format(
526 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
527 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800528 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100529
Jeff Brown487bb6e2012-10-11 13:35:42 -0700530 // Send the ordered broadcast; the result receiver will receive after all
531 // broadcasts have been sent. If any broadcast receiver changes the result
532 // code from the initial value of RESULT_OK, then the result receiver will
533 // not launch the corresponding dock application. This gives apps a chance
534 // to override the behavior and stay in their app even when the device is
535 // placed into a dock.
536 Intent intent = new Intent(action);
537 intent.putExtra("enableFlags", enableFlags);
538 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700539 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800540 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700541 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100542
Jeff Brown487bb6e2012-10-11 13:35:42 -0700543 // Attempting to make this transition a little more clean, we are going
544 // to hold off on doing a configuration change until we have finished
545 // the broadcast and started the home activity.
546 mHoldingConfiguration = true;
547 updateConfigurationLocked();
548 } else {
549 String category = null;
550 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700551 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700552 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
553 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800554 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400555 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700556 if (ENABLE_LAUNCH_DESK_DOCK_APP
557 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
558 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800559 }
560 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700561 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
562 category = Intent.CATEGORY_HOME;
563 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800564 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100565
Jeff Brown487bb6e2012-10-11 13:35:42 -0700566 if (LOG) {
567 Slog.v(TAG, "updateLocked: null action, mDockState="
568 + mDockState +", category=" + category);
569 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400570
Jeff Brown487bb6e2012-10-11 13:35:42 -0700571 sendConfigurationAndStartDreamOrDockAppLocked(category);
572 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700573
Jeff Brown487bb6e2012-10-11 13:35:42 -0700574 // keep screen on when charging and in car mode
575 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700576 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700577 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700578 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
579 if (keepScreenOn != mWakeLock.isHeld()) {
580 if (keepScreenOn) {
581 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700582 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700583 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800584 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800585 }
586 }
587
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700588 private void sendForegroundBroadcastToAllUsers(String action) {
589 getContext().sendBroadcastAsUser(new Intent(action)
590 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
591 }
592
Jeff Brown62c82e42012-09-26 01:30:41 -0700593 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
594 // Launch a dock activity
595 String category = null;
596 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
597 // Only launch car home when car mode is enabled and the caller
598 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700599 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700600 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
601 category = Intent.CATEGORY_CAR_DOCK;
602 }
603 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
604 // Only launch car home when desk mode is enabled and the caller
605 // has asked us to switch to it. Currently re-using the car
606 // mode flag since we don't have a formal API for "desk mode".
607 if (ENABLE_LAUNCH_DESK_DOCK_APP
608 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
609 category = Intent.CATEGORY_DESK_DOCK;
610 }
611 } else {
612 // Launch the standard home app if requested.
613 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
614 category = Intent.CATEGORY_HOME;
615 }
616 }
617
618 if (LOG) {
619 Slog.v(TAG, String.format(
620 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
621 + "category=%s",
622 action, enableFlags, disableFlags, category));
623 }
624
625 sendConfigurationAndStartDreamOrDockAppLocked(category);
626 }
627
628 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
629 // Update the configuration but don't send it yet.
630 mHoldingConfiguration = false;
631 updateConfigurationLocked();
632
633 // Start the dock app, if there is one.
634 boolean dockAppStarted = false;
635 if (category != null) {
636 // Now we are going to be careful about switching the
637 // configuration and starting the activity -- we need to
638 // do this in a specific order under control of the
639 // activity manager, to do it cleanly. So compute the
640 // new config, but don't set it yet, and let the
641 // activity manager take care of both the start and config
642 // change.
643 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800644 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700645 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800646 int result = ActivityManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800647 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700648 mConfiguration, null, UserHandle.USER_CURRENT);
649 if (result >= ActivityManager.START_SUCCESS) {
650 dockAppStarted = true;
651 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
652 Slog.e(TAG, "Could not start dock app: " + homeIntent
653 + ", startActivityWithConfig result " + result);
654 }
655 } catch (RemoteException ex) {
656 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700657 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700658 }
659 }
660
661 // Send the new configuration.
662 sendConfigurationLocked();
663
664 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700665 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800666 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700667 }
668 }
669
Dianne Hackborn7299c412010-03-04 18:41:49 -0800670 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800671 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800672 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700673 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800674 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800675 }
676
Joe Onorato089de882010-04-12 08:18:45 -0700677 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800678 // features of the status bar; these are ORed together to form the
679 // active disabled list. So if (for example) the device is locked and
680 // the status bar should be totally disabled, the calls below will
681 // have no effect until the device is unlocked.
682 if (mStatusBarManager != null) {
683 mStatusBarManager.disable(mCarModeEnabled
684 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
685 : StatusBarManager.DISABLE_NONE);
686 }
687
688 if (mNotificationManager == null) {
689 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800690 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800691 }
692
693 if (mNotificationManager != null) {
694 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800695 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800696
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500697 Notification.Builder n =
698 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400699 .setSmallIcon(R.drawable.stat_notify_car_mode)
700 .setDefaults(Notification.DEFAULT_LIGHTS)
701 .setOngoing(true)
702 .setWhen(0)
703 .setColor(context.getColor(
704 com.android.internal.R.color.system_notification_accent_color))
705 .setContentTitle(
706 context.getString(R.string.car_mode_disable_notification_title))
707 .setContentText(
708 context.getString(R.string.car_mode_disable_notification_message))
709 .setContentIntent(
710 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
711 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700712 mNotificationManager.notifyAsUser(null,
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400713 R.string.car_mode_disable_notification_title, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800714 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700715 mNotificationManager.cancelAsUser(null,
716 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800717 }
718 }
719 }
720
Jeff Brown2416e092012-08-21 22:12:20 -0700721 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700722 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700723 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700724 if (state != null) {
725 mComputedNightMode = state.isNight();
726 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800727 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800728 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100729
Zak Cohen1a705732017-01-09 12:54:34 -0800730 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800731 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
732 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800733 try {
734 if (vrManager != null) {
735 vrManager.registerListener(mVrStateCallbacks);
736 }
737 } catch (RemoteException e) {
738 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
739 }
740 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100741
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500742 /**
743 * Handles "adb shell" commands.
744 */
745 private static class Shell extends ShellCommand {
746 public static final String NIGHT_MODE_STR_YES = "yes";
747 public static final String NIGHT_MODE_STR_NO = "no";
748 public static final String NIGHT_MODE_STR_AUTO = "auto";
749 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
750 private final IUiModeManager mInterface;
751
752 Shell(IUiModeManager iface) {
753 mInterface = iface;
754 }
755
756 @Override
757 public void onHelp() {
758 final PrintWriter pw = getOutPrintWriter();
759 pw.println("UiModeManager service (uimode) commands:");
760 pw.println(" help");
761 pw.println(" Print this help text.");
762 pw.println(" night [yes|no|auto]");
763 pw.println(" Set or read night mode.");
764 }
765
766 @Override
767 public int onCommand(String cmd) {
768 if (cmd == null) {
769 return handleDefaultCommands(cmd);
770 }
771
772 try {
773 switch (cmd) {
774 case "night":
775 return handleNightMode();
776 default:
777 return handleDefaultCommands(cmd);
778 }
779 } catch (RemoteException e) {
780 final PrintWriter err = getErrPrintWriter();
781 err.println("Remote exception: " + e);
782 }
783 return -1;
784 }
785
786 private int handleNightMode() throws RemoteException {
787 final PrintWriter err = getErrPrintWriter();
788 final String modeStr = getNextArg();
789 if (modeStr == null) {
790 printCurrentNightMode();
791 return 0;
792 }
793
794 final int mode = strToNightMode(modeStr);
795 if (mode >= 0) {
796 mInterface.setNightMode(mode);
797 printCurrentNightMode();
798 return 0;
799 } else {
800 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
801 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
802 return -1;
803 }
804 }
805
806 private void printCurrentNightMode() throws RemoteException {
807 final PrintWriter pw = getOutPrintWriter();
808 final int currMode = mInterface.getNightMode();
809 final String currModeStr = nightModeToStr(currMode);
810 pw.println("Night mode: " + currModeStr);
811 }
812
813 private static String nightModeToStr(int mode) {
814 switch (mode) {
815 case UiModeManager.MODE_NIGHT_YES:
816 return NIGHT_MODE_STR_YES;
817 case UiModeManager.MODE_NIGHT_NO:
818 return NIGHT_MODE_STR_NO;
819 case UiModeManager.MODE_NIGHT_AUTO:
820 return NIGHT_MODE_STR_AUTO;
821 default:
822 return NIGHT_MODE_STR_UNKNOWN;
823 }
824 }
825
826 private static int strToNightMode(String modeStr) {
827 switch (modeStr) {
828 case NIGHT_MODE_STR_YES:
829 return UiModeManager.MODE_NIGHT_YES;
830 case NIGHT_MODE_STR_NO:
831 return UiModeManager.MODE_NIGHT_NO;
832 case NIGHT_MODE_STR_AUTO:
833 return UiModeManager.MODE_NIGHT_AUTO;
834 default:
835 return -1;
836 }
837 }
838 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800839}