blob: 6f2f2c402f430e1b5088aacade30785256edcb86 [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;
Jason Monk97a06a12016-11-09 15:53:30 -050061import com.android.server.power.ShutdownThread;
Adam Lesinski182f73f2013-12-05 16:48:06 -080062import com.android.server.twilight.TwilightListener;
63import com.android.server.twilight.TwilightManager;
64import com.android.server.twilight.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080065
Adam Lesinski182f73f2013-12-05 16:48:06 -080066final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080067 private static final String TAG = UiModeManager.class.getSimpleName();
68 private static final boolean LOG = false;
69
Daniel Sandler11ddf532011-11-16 11:10:03 -080070 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040071 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080072
Dianne Hackborn7299c412010-03-04 18:41:49 -080073 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080074 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010075
Adam Lesinski182f73f2013-12-05 16:48:06 -080076 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080077 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080078
Dianne Hackborn7299c412010-03-04 18:41:49 -080079 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050080 private boolean mCharging = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080081 private int mDefaultUiModeType;
82 private boolean mCarModeKeepsScreenOn;
83 private boolean mDeskModeKeepsScreenOn;
84 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040085 private boolean mWatch;
Zak Cohen1a705732017-01-09 12:54:34 -080086 private boolean mVrHeadset;
Dianne Hackborn7299c412010-03-04 18:41:49 -080087 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070088 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080089
keunyounga7710492015-09-23 11:33:58 -070090 // flag set by resource, whether to enable Car dock launch when starting car mode.
91 private boolean mEnableCarDockLaunch = true;
92 // flag set by resource, whether to lock UI mode to the default one or not.
93 private boolean mUiModeLocked = false;
94 // flag set by resource, whether to night mode change for normal all or not.
95 private boolean mNightModeLocked = false;
96
Adam Lesinski182f73f2013-12-05 16:48:06 -080097 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080098 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080099 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800100
Dianne Hackborn7299c412010-03-04 18:41:49 -0800101 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800102 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100103
Adam Lesinski182f73f2013-12-05 16:48:06 -0800104 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800105
Adam Lesinski182f73f2013-12-05 16:48:06 -0800106 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800107 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800108 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700109
Adam Lesinski182f73f2013-12-05 16:48:06 -0800110 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800111
Jeff Brownb880d882014-02-10 19:47:07 -0800112 public UiModeManagerService(Context context) {
113 super(context);
114 }
115
Adam Lesinski182f73f2013-12-05 16:48:06 -0800116 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700117 Intent intent = new Intent(Intent.ACTION_MAIN);
118 intent.addCategory(category);
119 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
120 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
121 return intent;
122 }
John Spurlock960779d2012-05-29 14:37:05 -0400123
Dianne Hackborn7299c412010-03-04 18:41:49 -0800124 // The broadcast receiver which receives the result of the ordered broadcast sent when
125 // the dock state changes. The original ordered broadcast is sent with an initial result
126 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
127 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
128 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
129 @Override
130 public void onReceive(Context context, Intent intent) {
131 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400132 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400133 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400134 + ": canceled: " + getResultCode());
135 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800136 return;
137 }
138
Jeff Brown62c82e42012-09-26 01:30:41 -0700139 final int enableFlags = intent.getIntExtra("enableFlags", 0);
140 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800141 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700142 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800143 }
144 }
145 };
146
Dianne Hackborn7299c412010-03-04 18:41:49 -0800147 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
148 @Override
149 public void onReceive(Context context, Intent intent) {
150 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
151 Intent.EXTRA_DOCK_STATE_UNDOCKED);
152 updateDockState(state);
153 }
154 };
155
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500156 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
157 @Override
158 public void onReceive(Context context, Intent intent) {
159 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
160 synchronized (mLock) {
161 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700162 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500163 }
164 }
165 }
166 };
167
Adam Lesinski182f73f2013-12-05 16:48:06 -0800168 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700169 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700170 public void onTwilightStateChanged(@Nullable TwilightState state) {
171 synchronized (mLock) {
172 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
173 updateComputedNightModeLocked();
174 updateLocked(0, 0);
175 }
176 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700177 }
178 };
179
Zak Cohen1a705732017-01-09 12:54:34 -0800180 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
181 @Override
182 public void onVrStateChanged(boolean enabled) {
183 synchronized (mLock) {
184 mVrHeadset = enabled;
185 if (mSystemReady) {
186 updateLocked(0, 0);
187 }
188 }
189 }
190 };
191
Adam Lesinski182f73f2013-12-05 16:48:06 -0800192 @Override
193 public void onStart() {
194 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800195
Adam Lesinski182f73f2013-12-05 16:48:06 -0800196 final PowerManager powerManager =
197 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
198 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800199
Adam Lesinski182f73f2013-12-05 16:48:06 -0800200 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800201 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800202 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500203 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
204
Dianne Hackborn7299c412010-03-04 18:41:49 -0800205 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500206
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800207 final Resources res = context.getResources();
208 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800209 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800210 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500211 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800212 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500213 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700214 mEnableCarDockLaunch = res.getBoolean(
215 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
216 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
217 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700218
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800219 final PackageManager pm = context.getPackageManager();
220 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
221 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
222 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
223
224 final int defaultNightMode = res.getInteger(
225 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800226 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800227 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700228
Adam Lesinski05199e82015-03-19 14:37:11 -0700229 // Update the initial, static configurations.
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800230 SystemServerInitThreadPool.get().submit(() -> {
231 synchronized (mLock) {
232 updateConfigurationLocked();
233 sendConfigurationLocked();
234 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800235
Fyodor Kupolove29a5a12016-12-16 16:14:17 -0800236 }, TAG + ".onStart");
Adam Lesinski182f73f2013-12-05 16:48:06 -0800237 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800238 }
239
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500240 private final IUiModeManager.Stub mService = new IUiModeManager.Stub() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800241 @Override
242 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700243 if (isUiModeLocked()) {
244 Slog.e(TAG, "enableCarMode while UI mode is locked");
245 return;
246 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800247 final long ident = Binder.clearCallingIdentity();
248 try {
249 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700250 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800251 if (mSystemReady) {
252 updateLocked(flags, 0);
253 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700254 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800255 } finally {
256 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500257 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800258 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800259
Adam Lesinski182f73f2013-12-05 16:48:06 -0800260 @Override
261 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700262 if (isUiModeLocked()) {
263 Slog.e(TAG, "disableCarMode while UI mode is locked");
264 return;
265 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800266 final long ident = Binder.clearCallingIdentity();
267 try {
268 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700269 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800270 if (mSystemReady) {
271 updateLocked(0, flags);
272 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700273 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800274 } finally {
275 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500276 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700277 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100278
Adam Lesinski182f73f2013-12-05 16:48:06 -0800279 @Override
280 public int getCurrentModeType() {
281 final long ident = Binder.clearCallingIdentity();
282 try {
283 synchronized (mLock) {
284 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700285 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800286 } finally {
287 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800288 }
289 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100290
Adam Lesinski182f73f2013-12-05 16:48:06 -0800291 @Override
292 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700293 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
294 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
295 != PackageManager.PERMISSION_GRANTED)) {
296 Slog.e(TAG,
297 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
298 return;
299 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800300 switch (mode) {
301 case UiModeManager.MODE_NIGHT_NO:
302 case UiModeManager.MODE_NIGHT_YES:
303 case UiModeManager.MODE_NIGHT_AUTO:
304 break;
305 default:
306 throw new IllegalArgumentException("Unknown mode: " + mode);
307 }
308
309 final long ident = Binder.clearCallingIdentity();
310 try {
311 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800312 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800313 Settings.Secure.putInt(getContext().getContentResolver(),
314 Settings.Secure.UI_NIGHT_MODE, mode);
315 mNightMode = mode;
316 updateLocked(0, 0);
317 }
318 }
319 } finally {
320 Binder.restoreCallingIdentity(ident);
321 }
322 }
323
324 @Override
Jason Monk97a06a12016-11-09 15:53:30 -0500325 public void setTheme(String theme) {
326 if (getContext().checkCallingOrSelfPermission(
327 android.Manifest.permission.MODIFY_THEME_OVERLAY)
328 != PackageManager.PERMISSION_GRANTED) {
329 Slog.e(TAG, "setTheme requires MODIFY_THEME_OVERLAY permission");
330 return;
331 }
332 SystemProperties.set("persist.vendor.overlay.theme", theme);
333 mHandler.post(() -> ShutdownThread.reboot(getContext(),
334 PowerManager.SHUTDOWN_USER_REQUESTED, false));
335 }
336
337 @Override
338 public String getTheme() {
339 if (getContext().checkCallingOrSelfPermission(
340 android.Manifest.permission.MODIFY_THEME_OVERLAY)
341 != PackageManager.PERMISSION_GRANTED) {
342 Slog.e(TAG, "setTheme requires MODIFY_THEME_OVERLAY permission");
343 return null;
344 }
345 return SystemProperties.get("persist.vendor.overlay.theme");
346 }
347
348 @Override
Jason Monk524fb402017-01-25 10:33:31 -0500349 public String[] getAvailableThemes() {
350 if (getContext().checkCallingOrSelfPermission(
351 android.Manifest.permission.MODIFY_THEME_OVERLAY)
352 != PackageManager.PERMISSION_GRANTED) {
353 Slog.e(TAG, "getAvailableThemes requires MODIFY_THEME_OVERLAY permission");
354 return null;
355 }
356 String def = SystemProperties.get("ro.boot.vendor.overlay.theme");
357 if (TextUtils.isEmpty(def)) {
358 def = null;
359 }
360 String[] fileList = new File("/vendor/overlay").list();
361 if (fileList == null) return new String[0];
362 ArrayList<String> options = new ArrayList(fileList.length + 1);
363 Collections.addAll(options, fileList);
364 if (!options.contains(def)) {
365 options.add(0, def);
366 }
367 return options.toArray(new String[options.size()]);
368 }
369
370 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800371 public int getNightMode() {
372 synchronized (mLock) {
373 return mNightMode;
374 }
375 }
376
377 @Override
keunyounga7710492015-09-23 11:33:58 -0700378 public boolean isUiModeLocked() {
379 synchronized (mLock) {
380 return mUiModeLocked;
381 }
382 }
383
384 @Override
385 public boolean isNightModeLocked() {
386 synchronized (mLock) {
387 return mNightModeLocked;
388 }
389 }
390
391 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500392 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
393 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
394 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
395 }
396
397 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800398 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
399 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
400 != PackageManager.PERMISSION_GRANTED) {
401
402 pw.println("Permission Denial: can't dump uimode service from from pid="
403 + Binder.getCallingPid()
404 + ", uid=" + Binder.getCallingUid());
405 return;
406 }
407
408 dumpImpl(pw);
409 }
410 };
411
412 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700413 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800414 pw.println("Current UI Mode Service state:");
415 pw.print(" mDockState="); pw.print(mDockState);
416 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
417 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700418 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800419 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700420 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700421 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
422 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800423 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700424 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800425 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
426 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
427 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700428 if (mTwilightManager != null) {
429 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700430 pw.print(" mTwilightService.getLastTwilightState()=");
431 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700432 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700433 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800434 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100435
Adam Lesinski182f73f2013-12-05 16:48:06 -0800436 @Override
437 public void onBootPhase(int phase) {
438 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
439 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700440 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800441 mSystemReady = true;
442 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
443 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800444 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800445 updateLocked(0, 0);
446 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800447 }
448 }
449
keunyoung1d0a7cc2014-07-28 13:12:50 -0700450 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800451 if (mCarModeEnabled != enabled) {
452 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800453 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700454 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800455 }
456
Jeff Brown487bb6e2012-10-11 13:35:42 -0700457 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800458 synchronized (mLock) {
459 if (newState != mDockState) {
460 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700461 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800462 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700463 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800464 }
465 }
466 }
467 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500468
Jeff Brown487bb6e2012-10-11 13:35:42 -0700469 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400470 switch (state) {
471 case Intent.EXTRA_DOCK_STATE_DESK:
472 case Intent.EXTRA_DOCK_STATE_LE_DESK:
473 case Intent.EXTRA_DOCK_STATE_HE_DESK:
474 return true;
475 default:
476 return false;
477 }
478 }
479
Jeff Brown487bb6e2012-10-11 13:35:42 -0700480 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400481 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700482 if (mUiModeLocked) {
483 // no-op, keeps default one
484 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400485 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
486 } else if (mWatch) {
487 uiMode = Configuration.UI_MODE_TYPE_WATCH;
488 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800489 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400490 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800491 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800492 } else if (mVrHeadset) {
493 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800494 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800495
496 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700497 if (mTwilightManager != null) {
498 mTwilightManager.registerListener(mTwilightListener, mHandler);
499 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800500 updateComputedNightModeLocked();
501 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
502 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800503 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700504 if (mTwilightManager != null) {
505 mTwilightManager.unregisterListener(mTwilightListener);
506 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800507 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400508 }
509
510 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400511 Slog.d(TAG,
512 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400513 + "; mCarMode=" + mCarModeEnabled
514 + "; mNightMode=" + mNightMode
515 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800516 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100517
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800518 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700519 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700520 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700521 }
522 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100523
Jeff Brown487bb6e2012-10-11 13:35:42 -0700524 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700525 if (mSetUiMode != mConfiguration.uiMode) {
526 mSetUiMode = mConfiguration.uiMode;
527
528 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800529 ActivityManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700530 } catch (RemoteException e) {
531 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800532 }
533 }
534 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100535
Adam Lesinski182f73f2013-12-05 16:48:06 -0800536 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700537 String action = null;
538 String oldAction = null;
539 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
540 adjustStatusBarCarModeLocked();
541 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
542 } else if (isDeskDockState(mLastBroadcastState)) {
543 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
544 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100545
Jeff Brown487bb6e2012-10-11 13:35:42 -0700546 if (mCarModeEnabled) {
547 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100548 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700549 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700550 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700551 }
552 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
553 action = UiModeManager.ACTION_ENTER_CAR_MODE;
554 }
555 } else if (isDeskDockState(mDockState)) {
556 if (!isDeskDockState(mLastBroadcastState)) {
557 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700558 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700559 }
560 mLastBroadcastState = mDockState;
561 action = UiModeManager.ACTION_ENTER_DESK_MODE;
562 }
563 } else {
564 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
565 action = oldAction;
566 }
567
568 if (action != null) {
569 if (LOG) {
570 Slog.v(TAG, String.format(
571 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
572 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800573 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100574
Jeff Brown487bb6e2012-10-11 13:35:42 -0700575 // Send the ordered broadcast; the result receiver will receive after all
576 // broadcasts have been sent. If any broadcast receiver changes the result
577 // code from the initial value of RESULT_OK, then the result receiver will
578 // not launch the corresponding dock application. This gives apps a chance
579 // to override the behavior and stay in their app even when the device is
580 // placed into a dock.
581 Intent intent = new Intent(action);
582 intent.putExtra("enableFlags", enableFlags);
583 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700584 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800585 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700586 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100587
Jeff Brown487bb6e2012-10-11 13:35:42 -0700588 // Attempting to make this transition a little more clean, we are going
589 // to hold off on doing a configuration change until we have finished
590 // the broadcast and started the home activity.
591 mHoldingConfiguration = true;
592 updateConfigurationLocked();
593 } else {
594 String category = null;
595 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700596 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700597 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
598 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800599 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400600 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700601 if (ENABLE_LAUNCH_DESK_DOCK_APP
602 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
603 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800604 }
605 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700606 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
607 category = Intent.CATEGORY_HOME;
608 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800609 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100610
Jeff Brown487bb6e2012-10-11 13:35:42 -0700611 if (LOG) {
612 Slog.v(TAG, "updateLocked: null action, mDockState="
613 + mDockState +", category=" + category);
614 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400615
Jeff Brown487bb6e2012-10-11 13:35:42 -0700616 sendConfigurationAndStartDreamOrDockAppLocked(category);
617 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700618
Jeff Brown487bb6e2012-10-11 13:35:42 -0700619 // keep screen on when charging and in car mode
620 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700621 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700622 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700623 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
624 if (keepScreenOn != mWakeLock.isHeld()) {
625 if (keepScreenOn) {
626 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700627 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700628 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800629 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800630 }
631 }
632
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700633 private void sendForegroundBroadcastToAllUsers(String action) {
634 getContext().sendBroadcastAsUser(new Intent(action)
635 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
636 }
637
Jeff Brown62c82e42012-09-26 01:30:41 -0700638 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
639 // Launch a dock activity
640 String category = null;
641 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
642 // Only launch car home when car mode is enabled and the caller
643 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700644 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700645 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
646 category = Intent.CATEGORY_CAR_DOCK;
647 }
648 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
649 // Only launch car home when desk mode is enabled and the caller
650 // has asked us to switch to it. Currently re-using the car
651 // mode flag since we don't have a formal API for "desk mode".
652 if (ENABLE_LAUNCH_DESK_DOCK_APP
653 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
654 category = Intent.CATEGORY_DESK_DOCK;
655 }
656 } else {
657 // Launch the standard home app if requested.
658 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
659 category = Intent.CATEGORY_HOME;
660 }
661 }
662
663 if (LOG) {
664 Slog.v(TAG, String.format(
665 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
666 + "category=%s",
667 action, enableFlags, disableFlags, category));
668 }
669
670 sendConfigurationAndStartDreamOrDockAppLocked(category);
671 }
672
673 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
674 // Update the configuration but don't send it yet.
675 mHoldingConfiguration = false;
676 updateConfigurationLocked();
677
678 // Start the dock app, if there is one.
679 boolean dockAppStarted = false;
680 if (category != null) {
681 // Now we are going to be careful about switching the
682 // configuration and starting the activity -- we need to
683 // do this in a specific order under control of the
684 // activity manager, to do it cleanly. So compute the
685 // new config, but don't set it yet, and let the
686 // activity manager take care of both the start and config
687 // change.
688 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800689 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700690 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800691 int result = ActivityManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800692 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700693 mConfiguration, null, UserHandle.USER_CURRENT);
694 if (result >= ActivityManager.START_SUCCESS) {
695 dockAppStarted = true;
696 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
697 Slog.e(TAG, "Could not start dock app: " + homeIntent
698 + ", startActivityWithConfig result " + result);
699 }
700 } catch (RemoteException ex) {
701 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700702 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700703 }
704 }
705
706 // Send the new configuration.
707 sendConfigurationLocked();
708
709 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700710 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800711 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700712 }
713 }
714
Dianne Hackborn7299c412010-03-04 18:41:49 -0800715 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800716 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800717 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700718 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800719 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800720 }
721
Joe Onorato089de882010-04-12 08:18:45 -0700722 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800723 // features of the status bar; these are ORed together to form the
724 // active disabled list. So if (for example) the device is locked and
725 // the status bar should be totally disabled, the calls below will
726 // have no effect until the device is unlocked.
727 if (mStatusBarManager != null) {
728 mStatusBarManager.disable(mCarModeEnabled
729 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
730 : StatusBarManager.DISABLE_NONE);
731 }
732
733 if (mNotificationManager == null) {
734 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800735 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800736 }
737
738 if (mNotificationManager != null) {
739 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800740 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800741
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400742 Notification.Builder n = new Notification.Builder(context)
743 .setSmallIcon(R.drawable.stat_notify_car_mode)
744 .setDefaults(Notification.DEFAULT_LIGHTS)
745 .setOngoing(true)
746 .setWhen(0)
747 .setColor(context.getColor(
748 com.android.internal.R.color.system_notification_accent_color))
749 .setContentTitle(
750 context.getString(R.string.car_mode_disable_notification_title))
751 .setContentText(
752 context.getString(R.string.car_mode_disable_notification_message))
753 .setContentIntent(
754 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
755 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700756 mNotificationManager.notifyAsUser(null,
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400757 R.string.car_mode_disable_notification_title, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800758 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700759 mNotificationManager.cancelAsUser(null,
760 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800761 }
762 }
763 }
764
Jeff Brown2416e092012-08-21 22:12:20 -0700765 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700766 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700767 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700768 if (state != null) {
769 mComputedNightMode = state.isNight();
770 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800771 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800772 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100773
Zak Cohen1a705732017-01-09 12:54:34 -0800774 private void registerVrStateListener() {
775 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService("vrmanager"));
776 try {
777 if (vrManager != null) {
778 vrManager.registerListener(mVrStateCallbacks);
779 }
780 } catch (RemoteException e) {
781 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
782 }
783 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100784
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500785 /**
786 * Handles "adb shell" commands.
787 */
788 private static class Shell extends ShellCommand {
789 public static final String NIGHT_MODE_STR_YES = "yes";
790 public static final String NIGHT_MODE_STR_NO = "no";
791 public static final String NIGHT_MODE_STR_AUTO = "auto";
792 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
793 private final IUiModeManager mInterface;
794
795 Shell(IUiModeManager iface) {
796 mInterface = iface;
797 }
798
799 @Override
800 public void onHelp() {
801 final PrintWriter pw = getOutPrintWriter();
802 pw.println("UiModeManager service (uimode) commands:");
803 pw.println(" help");
804 pw.println(" Print this help text.");
805 pw.println(" night [yes|no|auto]");
806 pw.println(" Set or read night mode.");
807 }
808
809 @Override
810 public int onCommand(String cmd) {
811 if (cmd == null) {
812 return handleDefaultCommands(cmd);
813 }
814
815 try {
816 switch (cmd) {
817 case "night":
818 return handleNightMode();
819 default:
820 return handleDefaultCommands(cmd);
821 }
822 } catch (RemoteException e) {
823 final PrintWriter err = getErrPrintWriter();
824 err.println("Remote exception: " + e);
825 }
826 return -1;
827 }
828
829 private int handleNightMode() throws RemoteException {
830 final PrintWriter err = getErrPrintWriter();
831 final String modeStr = getNextArg();
832 if (modeStr == null) {
833 printCurrentNightMode();
834 return 0;
835 }
836
837 final int mode = strToNightMode(modeStr);
838 if (mode >= 0) {
839 mInterface.setNightMode(mode);
840 printCurrentNightMode();
841 return 0;
842 } else {
843 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
844 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
845 return -1;
846 }
847 }
848
849 private void printCurrentNightMode() throws RemoteException {
850 final PrintWriter pw = getOutPrintWriter();
851 final int currMode = mInterface.getNightMode();
852 final String currModeStr = nightModeToStr(currMode);
853 pw.println("Night mode: " + currModeStr);
854 }
855
856 private static String nightModeToStr(int mode) {
857 switch (mode) {
858 case UiModeManager.MODE_NIGHT_YES:
859 return NIGHT_MODE_STR_YES;
860 case UiModeManager.MODE_NIGHT_NO:
861 return NIGHT_MODE_STR_NO;
862 case UiModeManager.MODE_NIGHT_AUTO:
863 return NIGHT_MODE_STR_AUTO;
864 default:
865 return NIGHT_MODE_STR_UNKNOWN;
866 }
867 }
868
869 private static int strToNightMode(String modeStr) {
870 switch (modeStr) {
871 case NIGHT_MODE_STR_YES:
872 return UiModeManager.MODE_NIGHT_YES;
873 case NIGHT_MODE_STR_NO:
874 return UiModeManager.MODE_NIGHT_NO;
875 case NIGHT_MODE_STR_AUTO:
876 return UiModeManager.MODE_NIGHT_AUTO;
877 default:
878 return -1;
879 }
880 }
881 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800882}