blob: 5115fdecd1bf0415efd17e286aed6604993582ca [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
Jason Monk97a06a12016-11-09 15:53:30 -0500326 public void setTheme(String theme) {
327 if (getContext().checkCallingOrSelfPermission(
328 android.Manifest.permission.MODIFY_THEME_OVERLAY)
329 != PackageManager.PERMISSION_GRANTED) {
330 Slog.e(TAG, "setTheme requires MODIFY_THEME_OVERLAY permission");
331 return;
332 }
333 SystemProperties.set("persist.vendor.overlay.theme", theme);
334 mHandler.post(() -> ShutdownThread.reboot(getContext(),
335 PowerManager.SHUTDOWN_USER_REQUESTED, false));
336 }
337
338 @Override
339 public String getTheme() {
340 if (getContext().checkCallingOrSelfPermission(
341 android.Manifest.permission.MODIFY_THEME_OVERLAY)
342 != PackageManager.PERMISSION_GRANTED) {
343 Slog.e(TAG, "setTheme requires MODIFY_THEME_OVERLAY permission");
344 return null;
345 }
346 return SystemProperties.get("persist.vendor.overlay.theme");
347 }
348
349 @Override
Jason Monk524fb402017-01-25 10:33:31 -0500350 public String[] getAvailableThemes() {
351 if (getContext().checkCallingOrSelfPermission(
352 android.Manifest.permission.MODIFY_THEME_OVERLAY)
353 != PackageManager.PERMISSION_GRANTED) {
354 Slog.e(TAG, "getAvailableThemes requires MODIFY_THEME_OVERLAY permission");
355 return null;
356 }
357 String def = SystemProperties.get("ro.boot.vendor.overlay.theme");
358 if (TextUtils.isEmpty(def)) {
359 def = null;
360 }
361 String[] fileList = new File("/vendor/overlay").list();
362 if (fileList == null) return new String[0];
363 ArrayList<String> options = new ArrayList(fileList.length + 1);
364 Collections.addAll(options, fileList);
365 if (!options.contains(def)) {
366 options.add(0, def);
367 }
368 return options.toArray(new String[options.size()]);
369 }
370
371 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800372 public int getNightMode() {
373 synchronized (mLock) {
374 return mNightMode;
375 }
376 }
377
378 @Override
keunyounga7710492015-09-23 11:33:58 -0700379 public boolean isUiModeLocked() {
380 synchronized (mLock) {
381 return mUiModeLocked;
382 }
383 }
384
385 @Override
386 public boolean isNightModeLocked() {
387 synchronized (mLock) {
388 return mNightModeLocked;
389 }
390 }
391
392 @Override
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500393 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
394 String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
395 new Shell(mService).exec(mService, in, out, err, args, callback, resultReceiver);
396 }
397
398 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800399 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
400 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
401 != PackageManager.PERMISSION_GRANTED) {
402
403 pw.println("Permission Denial: can't dump uimode service from from pid="
404 + Binder.getCallingPid()
405 + ", uid=" + Binder.getCallingUid());
406 return;
407 }
408
409 dumpImpl(pw);
410 }
411 };
412
413 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700414 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800415 pw.println("Current UI Mode Service state:");
416 pw.print(" mDockState="); pw.print(mDockState);
417 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
418 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700419 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800420 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700421 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700422 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
423 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800424 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700425 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800426 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
427 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
428 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700429 if (mTwilightManager != null) {
430 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700431 pw.print(" mTwilightService.getLastTwilightState()=");
432 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700433 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700434 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800435 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100436
Adam Lesinski182f73f2013-12-05 16:48:06 -0800437 @Override
438 public void onBootPhase(int phase) {
439 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
440 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700441 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800442 mSystemReady = true;
443 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
444 updateComputedNightModeLocked();
Zak Cohen1a705732017-01-09 12:54:34 -0800445 registerVrStateListener();
Adam Lesinski182f73f2013-12-05 16:48:06 -0800446 updateLocked(0, 0);
447 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800448 }
449 }
450
keunyoung1d0a7cc2014-07-28 13:12:50 -0700451 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800452 if (mCarModeEnabled != enabled) {
453 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800454 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700455 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800456 }
457
Jeff Brown487bb6e2012-10-11 13:35:42 -0700458 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800459 synchronized (mLock) {
460 if (newState != mDockState) {
461 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700462 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800463 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700464 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800465 }
466 }
467 }
468 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500469
Jeff Brown487bb6e2012-10-11 13:35:42 -0700470 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400471 switch (state) {
472 case Intent.EXTRA_DOCK_STATE_DESK:
473 case Intent.EXTRA_DOCK_STATE_LE_DESK:
474 case Intent.EXTRA_DOCK_STATE_HE_DESK:
475 return true;
476 default:
477 return false;
478 }
479 }
480
Jeff Brown487bb6e2012-10-11 13:35:42 -0700481 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400482 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700483 if (mUiModeLocked) {
484 // no-op, keeps default one
485 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400486 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
487 } else if (mWatch) {
488 uiMode = Configuration.UI_MODE_TYPE_WATCH;
489 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800490 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400491 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800492 uiMode = Configuration.UI_MODE_TYPE_DESK;
Zak Cohen1a705732017-01-09 12:54:34 -0800493 } else if (mVrHeadset) {
494 uiMode = Configuration.UI_MODE_TYPE_VR_HEADSET;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800495 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800496
497 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700498 if (mTwilightManager != null) {
499 mTwilightManager.registerListener(mTwilightListener, mHandler);
500 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800501 updateComputedNightModeLocked();
502 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
503 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800504 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700505 if (mTwilightManager != null) {
506 mTwilightManager.unregisterListener(mTwilightListener);
507 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800508 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400509 }
510
511 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400512 Slog.d(TAG,
513 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400514 + "; mCarMode=" + mCarModeEnabled
515 + "; mNightMode=" + mNightMode
516 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800517 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100518
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800519 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700520 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700521 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700522 }
523 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100524
Jeff Brown487bb6e2012-10-11 13:35:42 -0700525 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700526 if (mSetUiMode != mConfiguration.uiMode) {
527 mSetUiMode = mConfiguration.uiMode;
528
529 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800530 ActivityManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700531 } catch (RemoteException e) {
532 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800533 }
534 }
535 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100536
Adam Lesinski182f73f2013-12-05 16:48:06 -0800537 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700538 String action = null;
539 String oldAction = null;
540 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
541 adjustStatusBarCarModeLocked();
542 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
543 } else if (isDeskDockState(mLastBroadcastState)) {
544 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
545 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100546
Jeff Brown487bb6e2012-10-11 13:35:42 -0700547 if (mCarModeEnabled) {
548 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100549 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700550 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700551 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700552 }
553 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
554 action = UiModeManager.ACTION_ENTER_CAR_MODE;
555 }
556 } else if (isDeskDockState(mDockState)) {
557 if (!isDeskDockState(mLastBroadcastState)) {
558 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700559 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700560 }
561 mLastBroadcastState = mDockState;
562 action = UiModeManager.ACTION_ENTER_DESK_MODE;
563 }
564 } else {
565 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
566 action = oldAction;
567 }
568
569 if (action != null) {
570 if (LOG) {
571 Slog.v(TAG, String.format(
572 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
573 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800574 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100575
Jeff Brown487bb6e2012-10-11 13:35:42 -0700576 // Send the ordered broadcast; the result receiver will receive after all
577 // broadcasts have been sent. If any broadcast receiver changes the result
578 // code from the initial value of RESULT_OK, then the result receiver will
579 // not launch the corresponding dock application. This gives apps a chance
580 // to override the behavior and stay in their app even when the device is
581 // placed into a dock.
582 Intent intent = new Intent(action);
583 intent.putExtra("enableFlags", enableFlags);
584 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700585 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800586 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700587 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100588
Jeff Brown487bb6e2012-10-11 13:35:42 -0700589 // Attempting to make this transition a little more clean, we are going
590 // to hold off on doing a configuration change until we have finished
591 // the broadcast and started the home activity.
592 mHoldingConfiguration = true;
593 updateConfigurationLocked();
594 } else {
595 String category = null;
596 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700597 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700598 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
599 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800600 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400601 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700602 if (ENABLE_LAUNCH_DESK_DOCK_APP
603 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
604 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800605 }
606 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700607 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
608 category = Intent.CATEGORY_HOME;
609 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800610 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100611
Jeff Brown487bb6e2012-10-11 13:35:42 -0700612 if (LOG) {
613 Slog.v(TAG, "updateLocked: null action, mDockState="
614 + mDockState +", category=" + category);
615 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400616
Jeff Brown487bb6e2012-10-11 13:35:42 -0700617 sendConfigurationAndStartDreamOrDockAppLocked(category);
618 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700619
Jeff Brown487bb6e2012-10-11 13:35:42 -0700620 // keep screen on when charging and in car mode
621 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700622 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700623 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700624 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
625 if (keepScreenOn != mWakeLock.isHeld()) {
626 if (keepScreenOn) {
627 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700628 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700629 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800630 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800631 }
632 }
633
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700634 private void sendForegroundBroadcastToAllUsers(String action) {
635 getContext().sendBroadcastAsUser(new Intent(action)
636 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
637 }
638
Jeff Brown62c82e42012-09-26 01:30:41 -0700639 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
640 // Launch a dock activity
641 String category = null;
642 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
643 // Only launch car home when car mode is enabled and the caller
644 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700645 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700646 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
647 category = Intent.CATEGORY_CAR_DOCK;
648 }
649 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
650 // Only launch car home when desk mode is enabled and the caller
651 // has asked us to switch to it. Currently re-using the car
652 // mode flag since we don't have a formal API for "desk mode".
653 if (ENABLE_LAUNCH_DESK_DOCK_APP
654 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
655 category = Intent.CATEGORY_DESK_DOCK;
656 }
657 } else {
658 // Launch the standard home app if requested.
659 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
660 category = Intent.CATEGORY_HOME;
661 }
662 }
663
664 if (LOG) {
665 Slog.v(TAG, String.format(
666 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
667 + "category=%s",
668 action, enableFlags, disableFlags, category));
669 }
670
671 sendConfigurationAndStartDreamOrDockAppLocked(category);
672 }
673
674 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
675 // Update the configuration but don't send it yet.
676 mHoldingConfiguration = false;
677 updateConfigurationLocked();
678
679 // Start the dock app, if there is one.
680 boolean dockAppStarted = false;
681 if (category != null) {
682 // Now we are going to be careful about switching the
683 // configuration and starting the activity -- we need to
684 // do this in a specific order under control of the
685 // activity manager, to do it cleanly. So compute the
686 // new config, but don't set it yet, and let the
687 // activity manager take care of both the start and config
688 // change.
689 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800690 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700691 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800692 int result = ActivityManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800693 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700694 mConfiguration, null, UserHandle.USER_CURRENT);
695 if (result >= ActivityManager.START_SUCCESS) {
696 dockAppStarted = true;
697 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
698 Slog.e(TAG, "Could not start dock app: " + homeIntent
699 + ", startActivityWithConfig result " + result);
700 }
701 } catch (RemoteException ex) {
702 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700703 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700704 }
705 }
706
707 // Send the new configuration.
708 sendConfigurationLocked();
709
710 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700711 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800712 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700713 }
714 }
715
Dianne Hackborn7299c412010-03-04 18:41:49 -0800716 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800717 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800718 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700719 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800720 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800721 }
722
Joe Onorato089de882010-04-12 08:18:45 -0700723 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800724 // features of the status bar; these are ORed together to form the
725 // active disabled list. So if (for example) the device is locked and
726 // the status bar should be totally disabled, the calls below will
727 // have no effect until the device is unlocked.
728 if (mStatusBarManager != null) {
729 mStatusBarManager.disable(mCarModeEnabled
730 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
731 : StatusBarManager.DISABLE_NONE);
732 }
733
734 if (mNotificationManager == null) {
735 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800736 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800737 }
738
739 if (mNotificationManager != null) {
740 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800741 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800742
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500743 Notification.Builder n =
744 new Notification.Builder(context, SystemNotificationChannels.CAR_MODE)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400745 .setSmallIcon(R.drawable.stat_notify_car_mode)
746 .setDefaults(Notification.DEFAULT_LIGHTS)
747 .setOngoing(true)
748 .setWhen(0)
749 .setColor(context.getColor(
750 com.android.internal.R.color.system_notification_accent_color))
751 .setContentTitle(
752 context.getString(R.string.car_mode_disable_notification_title))
753 .setContentText(
754 context.getString(R.string.car_mode_disable_notification_message))
755 .setContentIntent(
756 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
757 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700758 mNotificationManager.notifyAsUser(null,
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400759 R.string.car_mode_disable_notification_title, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800760 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700761 mNotificationManager.cancelAsUser(null,
762 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800763 }
764 }
765 }
766
Jeff Brown2416e092012-08-21 22:12:20 -0700767 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700768 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700769 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700770 if (state != null) {
771 mComputedNightMode = state.isNight();
772 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800773 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800774 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100775
Zak Cohen1a705732017-01-09 12:54:34 -0800776 private void registerVrStateListener() {
Craig Donner8deb67c2017-02-07 18:10:32 -0800777 IVrManager vrManager = IVrManager.Stub.asInterface(ServiceManager.getService(
778 Context.VR_SERVICE));
Zak Cohen1a705732017-01-09 12:54:34 -0800779 try {
780 if (vrManager != null) {
781 vrManager.registerListener(mVrStateCallbacks);
782 }
783 } catch (RemoteException e) {
784 Slog.e(TAG, "Failed to register VR mode state listener: " + e);
785 }
786 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100787
Alan Viveretteba0d98f2017-01-30 10:36:54 -0500788 /**
789 * Handles "adb shell" commands.
790 */
791 private static class Shell extends ShellCommand {
792 public static final String NIGHT_MODE_STR_YES = "yes";
793 public static final String NIGHT_MODE_STR_NO = "no";
794 public static final String NIGHT_MODE_STR_AUTO = "auto";
795 public static final String NIGHT_MODE_STR_UNKNOWN = "unknown";
796 private final IUiModeManager mInterface;
797
798 Shell(IUiModeManager iface) {
799 mInterface = iface;
800 }
801
802 @Override
803 public void onHelp() {
804 final PrintWriter pw = getOutPrintWriter();
805 pw.println("UiModeManager service (uimode) commands:");
806 pw.println(" help");
807 pw.println(" Print this help text.");
808 pw.println(" night [yes|no|auto]");
809 pw.println(" Set or read night mode.");
810 }
811
812 @Override
813 public int onCommand(String cmd) {
814 if (cmd == null) {
815 return handleDefaultCommands(cmd);
816 }
817
818 try {
819 switch (cmd) {
820 case "night":
821 return handleNightMode();
822 default:
823 return handleDefaultCommands(cmd);
824 }
825 } catch (RemoteException e) {
826 final PrintWriter err = getErrPrintWriter();
827 err.println("Remote exception: " + e);
828 }
829 return -1;
830 }
831
832 private int handleNightMode() throws RemoteException {
833 final PrintWriter err = getErrPrintWriter();
834 final String modeStr = getNextArg();
835 if (modeStr == null) {
836 printCurrentNightMode();
837 return 0;
838 }
839
840 final int mode = strToNightMode(modeStr);
841 if (mode >= 0) {
842 mInterface.setNightMode(mode);
843 printCurrentNightMode();
844 return 0;
845 } else {
846 err.println("Error: mode must be '" + NIGHT_MODE_STR_YES + "', '"
847 + NIGHT_MODE_STR_NO + "', or '" + NIGHT_MODE_STR_AUTO + "'");
848 return -1;
849 }
850 }
851
852 private void printCurrentNightMode() throws RemoteException {
853 final PrintWriter pw = getOutPrintWriter();
854 final int currMode = mInterface.getNightMode();
855 final String currModeStr = nightModeToStr(currMode);
856 pw.println("Night mode: " + currModeStr);
857 }
858
859 private static String nightModeToStr(int mode) {
860 switch (mode) {
861 case UiModeManager.MODE_NIGHT_YES:
862 return NIGHT_MODE_STR_YES;
863 case UiModeManager.MODE_NIGHT_NO:
864 return NIGHT_MODE_STR_NO;
865 case UiModeManager.MODE_NIGHT_AUTO:
866 return NIGHT_MODE_STR_AUTO;
867 default:
868 return NIGHT_MODE_STR_UNKNOWN;
869 }
870 }
871
872 private static int strToNightMode(String modeStr) {
873 switch (modeStr) {
874 case NIGHT_MODE_STR_YES:
875 return UiModeManager.MODE_NIGHT_YES;
876 case NIGHT_MODE_STR_NO:
877 return UiModeManager.MODE_NIGHT_NO;
878 case NIGHT_MODE_STR_AUTO:
879 return UiModeManager.MODE_NIGHT_AUTO;
880 default:
881 return -1;
882 }
883 }
884 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800885}