blob: 626869718580972f799785deba650e00e34e231c [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.ActivityManagerNative;
Dianne Hackborn7299c412010-03-04 18:41:49 -080023import android.app.IUiModeManager;
24import android.app.Notification;
25import android.app.NotificationManager;
26import android.app.PendingIntent;
27import android.app.StatusBarManager;
28import android.app.UiModeManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080029import android.content.BroadcastReceiver;
30import android.content.Context;
31import android.content.Intent;
32import android.content.IntentFilter;
33import android.content.pm.PackageManager;
34import android.content.res.Configuration;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080035import android.content.res.Resources;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050036import android.os.BatteryManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080037import android.os.Binder;
Dianne Hackborn7299c412010-03-04 18:41:49 -080038import android.os.Handler;
Adam Lesinski182f73f2013-12-05 16:48:06 -080039import android.os.IBinder;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050040import android.os.PowerManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080041import android.os.RemoteException;
Jason Monk97a06a12016-11-09 15:53:30 -050042import android.os.SystemProperties;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070043import android.os.UserHandle;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070044import android.provider.Settings;
Jeff Brown11159e92012-10-11 15:58:37 -070045import android.service.dreams.Sandman;
Dianne Hackborn7299c412010-03-04 18:41:49 -080046import android.util.Slog;
Jason Monk97a06a12016-11-09 15:53:30 -050047import android.view.WindowManagerInternal;
48import android.view.WindowManagerPolicy;
Dianne Hackborn7299c412010-03-04 18:41:49 -080049
50import java.io.FileDescriptor;
51import java.io.PrintWriter;
52
53import com.android.internal.R;
54import com.android.internal.app.DisableCarModeActivity;
Jason Monk97a06a12016-11-09 15:53:30 -050055import com.android.server.power.ShutdownThread;
Adam Lesinski182f73f2013-12-05 16:48:06 -080056import com.android.server.twilight.TwilightListener;
57import com.android.server.twilight.TwilightManager;
58import com.android.server.twilight.TwilightState;
Jason Monk97a06a12016-11-09 15:53:30 -050059import com.android.server.wm.WindowManagerService;
Dianne Hackborn7299c412010-03-04 18:41:49 -080060
Adam Lesinski182f73f2013-12-05 16:48:06 -080061final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080062 private static final String TAG = UiModeManager.class.getSimpleName();
63 private static final boolean LOG = false;
64
Daniel Sandler11ddf532011-11-16 11:10:03 -080065 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040066 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080067
Dianne Hackborn7299c412010-03-04 18:41:49 -080068 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080069 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010070
Adam Lesinski182f73f2013-12-05 16:48:06 -080071 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080072 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080073
Dianne Hackborn7299c412010-03-04 18:41:49 -080074 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050075 private boolean mCharging = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080076 private int mDefaultUiModeType;
77 private boolean mCarModeKeepsScreenOn;
78 private boolean mDeskModeKeepsScreenOn;
79 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040080 private boolean mWatch;
Dianne Hackborn7299c412010-03-04 18:41:49 -080081 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070082 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080083
keunyounga7710492015-09-23 11:33:58 -070084 // flag set by resource, whether to enable Car dock launch when starting car mode.
85 private boolean mEnableCarDockLaunch = true;
86 // flag set by resource, whether to lock UI mode to the default one or not.
87 private boolean mUiModeLocked = false;
88 // flag set by resource, whether to night mode change for normal all or not.
89 private boolean mNightModeLocked = false;
90
Adam Lesinski182f73f2013-12-05 16:48:06 -080091 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080092 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080093 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080094
Dianne Hackborn7299c412010-03-04 18:41:49 -080095 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -080096 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010097
Adam Lesinski182f73f2013-12-05 16:48:06 -080098 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080099
Adam Lesinski182f73f2013-12-05 16:48:06 -0800100 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800101 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800102 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700103
Adam Lesinski182f73f2013-12-05 16:48:06 -0800104 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800105
Jeff Brownb880d882014-02-10 19:47:07 -0800106 public UiModeManagerService(Context context) {
107 super(context);
108 }
109
Adam Lesinski182f73f2013-12-05 16:48:06 -0800110 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700111 Intent intent = new Intent(Intent.ACTION_MAIN);
112 intent.addCategory(category);
113 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
114 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
115 return intent;
116 }
John Spurlock960779d2012-05-29 14:37:05 -0400117
Dianne Hackborn7299c412010-03-04 18:41:49 -0800118 // The broadcast receiver which receives the result of the ordered broadcast sent when
119 // the dock state changes. The original ordered broadcast is sent with an initial result
120 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
121 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
122 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
123 @Override
124 public void onReceive(Context context, Intent intent) {
125 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400126 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400127 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400128 + ": canceled: " + getResultCode());
129 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800130 return;
131 }
132
Jeff Brown62c82e42012-09-26 01:30:41 -0700133 final int enableFlags = intent.getIntExtra("enableFlags", 0);
134 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800135 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700136 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800137 }
138 }
139 };
140
Dianne Hackborn7299c412010-03-04 18:41:49 -0800141 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
142 @Override
143 public void onReceive(Context context, Intent intent) {
144 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
145 Intent.EXTRA_DOCK_STATE_UNDOCKED);
146 updateDockState(state);
147 }
148 };
149
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500150 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
151 @Override
152 public void onReceive(Context context, Intent intent) {
153 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
154 synchronized (mLock) {
155 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700156 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500157 }
158 }
159 }
160 };
161
Adam Lesinski182f73f2013-12-05 16:48:06 -0800162 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700163 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700164 public void onTwilightStateChanged(@Nullable TwilightState state) {
165 synchronized (mLock) {
166 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
167 updateComputedNightModeLocked();
168 updateLocked(0, 0);
169 }
170 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700171 }
172 };
173
Adam Lesinski182f73f2013-12-05 16:48:06 -0800174 @Override
175 public void onStart() {
176 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800177
Adam Lesinski182f73f2013-12-05 16:48:06 -0800178 final PowerManager powerManager =
179 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
180 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800181
Adam Lesinski182f73f2013-12-05 16:48:06 -0800182 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800183 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800184 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500185 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
186
Dianne Hackborn7299c412010-03-04 18:41:49 -0800187 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500188
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800189 final Resources res = context.getResources();
190 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800191 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800192 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500193 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800194 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500195 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700196 mEnableCarDockLaunch = res.getBoolean(
197 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
198 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
199 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700200
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800201 final PackageManager pm = context.getPackageManager();
202 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
203 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
204 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
205
206 final int defaultNightMode = res.getInteger(
207 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800208 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800209 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700210
Adam Lesinski05199e82015-03-19 14:37:11 -0700211 // Update the initial, static configurations.
212 synchronized (this) {
213 updateConfigurationLocked();
214 sendConfigurationLocked();
215 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800216
217 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800218 }
219
Adam Lesinski182f73f2013-12-05 16:48:06 -0800220 private final IBinder mService = new IUiModeManager.Stub() {
221 @Override
222 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700223 if (isUiModeLocked()) {
224 Slog.e(TAG, "enableCarMode while UI mode is locked");
225 return;
226 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800227 final long ident = Binder.clearCallingIdentity();
228 try {
229 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700230 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800231 if (mSystemReady) {
232 updateLocked(flags, 0);
233 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700234 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800235 } finally {
236 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500237 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800238 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800239
Adam Lesinski182f73f2013-12-05 16:48:06 -0800240 @Override
241 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700242 if (isUiModeLocked()) {
243 Slog.e(TAG, "disableCarMode while UI mode is locked");
244 return;
245 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800246 final long ident = Binder.clearCallingIdentity();
247 try {
248 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700249 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800250 if (mSystemReady) {
251 updateLocked(0, flags);
252 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700253 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800254 } finally {
255 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500256 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700257 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100258
Adam Lesinski182f73f2013-12-05 16:48:06 -0800259 @Override
260 public int getCurrentModeType() {
261 final long ident = Binder.clearCallingIdentity();
262 try {
263 synchronized (mLock) {
264 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700265 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800266 } finally {
267 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800268 }
269 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100270
Adam Lesinski182f73f2013-12-05 16:48:06 -0800271 @Override
272 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700273 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
274 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
275 != PackageManager.PERMISSION_GRANTED)) {
276 Slog.e(TAG,
277 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
278 return;
279 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800280 switch (mode) {
281 case UiModeManager.MODE_NIGHT_NO:
282 case UiModeManager.MODE_NIGHT_YES:
283 case UiModeManager.MODE_NIGHT_AUTO:
284 break;
285 default:
286 throw new IllegalArgumentException("Unknown mode: " + mode);
287 }
288
289 final long ident = Binder.clearCallingIdentity();
290 try {
291 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800292 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800293 Settings.Secure.putInt(getContext().getContentResolver(),
294 Settings.Secure.UI_NIGHT_MODE, mode);
295 mNightMode = mode;
296 updateLocked(0, 0);
297 }
298 }
299 } finally {
300 Binder.restoreCallingIdentity(ident);
301 }
302 }
303
304 @Override
Jason Monk97a06a12016-11-09 15:53:30 -0500305 public void setTheme(String theme) {
306 if (getContext().checkCallingOrSelfPermission(
307 android.Manifest.permission.MODIFY_THEME_OVERLAY)
308 != PackageManager.PERMISSION_GRANTED) {
309 Slog.e(TAG, "setTheme requires MODIFY_THEME_OVERLAY permission");
310 return;
311 }
312 SystemProperties.set("persist.vendor.overlay.theme", theme);
313 mHandler.post(() -> ShutdownThread.reboot(getContext(),
314 PowerManager.SHUTDOWN_USER_REQUESTED, false));
315 }
316
317 @Override
318 public String getTheme() {
319 if (getContext().checkCallingOrSelfPermission(
320 android.Manifest.permission.MODIFY_THEME_OVERLAY)
321 != PackageManager.PERMISSION_GRANTED) {
322 Slog.e(TAG, "setTheme requires MODIFY_THEME_OVERLAY permission");
323 return null;
324 }
325 return SystemProperties.get("persist.vendor.overlay.theme");
326 }
327
328 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800329 public int getNightMode() {
330 synchronized (mLock) {
331 return mNightMode;
332 }
333 }
334
335 @Override
keunyounga7710492015-09-23 11:33:58 -0700336 public boolean isUiModeLocked() {
337 synchronized (mLock) {
338 return mUiModeLocked;
339 }
340 }
341
342 @Override
343 public boolean isNightModeLocked() {
344 synchronized (mLock) {
345 return mNightModeLocked;
346 }
347 }
348
349 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800350 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
351 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
352 != PackageManager.PERMISSION_GRANTED) {
353
354 pw.println("Permission Denial: can't dump uimode service from from pid="
355 + Binder.getCallingPid()
356 + ", uid=" + Binder.getCallingUid());
357 return;
358 }
359
360 dumpImpl(pw);
361 }
362 };
363
364 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700365 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800366 pw.println("Current UI Mode Service state:");
367 pw.print(" mDockState="); pw.print(mDockState);
368 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
369 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700370 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800371 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700372 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700373 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
374 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800375 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700376 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800377 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
378 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
379 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700380 if (mTwilightManager != null) {
381 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700382 pw.print(" mTwilightService.getLastTwilightState()=");
383 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700384 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700385 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800386 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100387
Adam Lesinski182f73f2013-12-05 16:48:06 -0800388 @Override
389 public void onBootPhase(int phase) {
390 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
391 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700392 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800393 mSystemReady = true;
394 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
395 updateComputedNightModeLocked();
396 updateLocked(0, 0);
397 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800398 }
399 }
400
keunyoung1d0a7cc2014-07-28 13:12:50 -0700401 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800402 if (mCarModeEnabled != enabled) {
403 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800404 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700405 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800406 }
407
Jeff Brown487bb6e2012-10-11 13:35:42 -0700408 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800409 synchronized (mLock) {
410 if (newState != mDockState) {
411 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700412 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800413 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700414 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800415 }
416 }
417 }
418 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500419
Jeff Brown487bb6e2012-10-11 13:35:42 -0700420 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400421 switch (state) {
422 case Intent.EXTRA_DOCK_STATE_DESK:
423 case Intent.EXTRA_DOCK_STATE_LE_DESK:
424 case Intent.EXTRA_DOCK_STATE_HE_DESK:
425 return true;
426 default:
427 return false;
428 }
429 }
430
Jeff Brown487bb6e2012-10-11 13:35:42 -0700431 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400432 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700433 if (mUiModeLocked) {
434 // no-op, keeps default one
435 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400436 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
437 } else if (mWatch) {
438 uiMode = Configuration.UI_MODE_TYPE_WATCH;
439 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800440 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400441 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800442 uiMode = Configuration.UI_MODE_TYPE_DESK;
443 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800444
445 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700446 if (mTwilightManager != null) {
447 mTwilightManager.registerListener(mTwilightListener, mHandler);
448 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800449 updateComputedNightModeLocked();
450 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
451 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800452 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700453 if (mTwilightManager != null) {
454 mTwilightManager.unregisterListener(mTwilightListener);
455 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800456 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400457 }
458
459 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400460 Slog.d(TAG,
461 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400462 + "; mCarMode=" + mCarModeEnabled
463 + "; mNightMode=" + mNightMode
464 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800465 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100466
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800467 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700468 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700469 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700470 }
471 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100472
Jeff Brown487bb6e2012-10-11 13:35:42 -0700473 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700474 if (mSetUiMode != mConfiguration.uiMode) {
475 mSetUiMode = mConfiguration.uiMode;
476
477 try {
478 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
479 } catch (RemoteException e) {
480 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800481 }
482 }
483 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100484
Adam Lesinski182f73f2013-12-05 16:48:06 -0800485 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700486 String action = null;
487 String oldAction = null;
488 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
489 adjustStatusBarCarModeLocked();
490 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
491 } else if (isDeskDockState(mLastBroadcastState)) {
492 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
493 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100494
Jeff Brown487bb6e2012-10-11 13:35:42 -0700495 if (mCarModeEnabled) {
496 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100497 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700498 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700499 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700500 }
501 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
502 action = UiModeManager.ACTION_ENTER_CAR_MODE;
503 }
504 } else if (isDeskDockState(mDockState)) {
505 if (!isDeskDockState(mLastBroadcastState)) {
506 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700507 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700508 }
509 mLastBroadcastState = mDockState;
510 action = UiModeManager.ACTION_ENTER_DESK_MODE;
511 }
512 } else {
513 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
514 action = oldAction;
515 }
516
517 if (action != null) {
518 if (LOG) {
519 Slog.v(TAG, String.format(
520 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
521 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800522 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100523
Jeff Brown487bb6e2012-10-11 13:35:42 -0700524 // Send the ordered broadcast; the result receiver will receive after all
525 // broadcasts have been sent. If any broadcast receiver changes the result
526 // code from the initial value of RESULT_OK, then the result receiver will
527 // not launch the corresponding dock application. This gives apps a chance
528 // to override the behavior and stay in their app even when the device is
529 // placed into a dock.
530 Intent intent = new Intent(action);
531 intent.putExtra("enableFlags", enableFlags);
532 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700533 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800534 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700535 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100536
Jeff Brown487bb6e2012-10-11 13:35:42 -0700537 // Attempting to make this transition a little more clean, we are going
538 // to hold off on doing a configuration change until we have finished
539 // the broadcast and started the home activity.
540 mHoldingConfiguration = true;
541 updateConfigurationLocked();
542 } else {
543 String category = null;
544 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700545 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700546 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
547 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800548 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400549 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700550 if (ENABLE_LAUNCH_DESK_DOCK_APP
551 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
552 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800553 }
554 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700555 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
556 category = Intent.CATEGORY_HOME;
557 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800558 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100559
Jeff Brown487bb6e2012-10-11 13:35:42 -0700560 if (LOG) {
561 Slog.v(TAG, "updateLocked: null action, mDockState="
562 + mDockState +", category=" + category);
563 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400564
Jeff Brown487bb6e2012-10-11 13:35:42 -0700565 sendConfigurationAndStartDreamOrDockAppLocked(category);
566 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700567
Jeff Brown487bb6e2012-10-11 13:35:42 -0700568 // keep screen on when charging and in car mode
569 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700570 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700571 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700572 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
573 if (keepScreenOn != mWakeLock.isHeld()) {
574 if (keepScreenOn) {
575 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700576 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700577 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800578 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800579 }
580 }
581
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700582 private void sendForegroundBroadcastToAllUsers(String action) {
583 getContext().sendBroadcastAsUser(new Intent(action)
584 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
585 }
586
Jeff Brown62c82e42012-09-26 01:30:41 -0700587 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
588 // Launch a dock activity
589 String category = null;
590 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
591 // Only launch car home when car mode is enabled and the caller
592 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700593 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700594 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
595 category = Intent.CATEGORY_CAR_DOCK;
596 }
597 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
598 // Only launch car home when desk mode is enabled and the caller
599 // has asked us to switch to it. Currently re-using the car
600 // mode flag since we don't have a formal API for "desk mode".
601 if (ENABLE_LAUNCH_DESK_DOCK_APP
602 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
603 category = Intent.CATEGORY_DESK_DOCK;
604 }
605 } else {
606 // Launch the standard home app if requested.
607 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
608 category = Intent.CATEGORY_HOME;
609 }
610 }
611
612 if (LOG) {
613 Slog.v(TAG, String.format(
614 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
615 + "category=%s",
616 action, enableFlags, disableFlags, category));
617 }
618
619 sendConfigurationAndStartDreamOrDockAppLocked(category);
620 }
621
622 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
623 // Update the configuration but don't send it yet.
624 mHoldingConfiguration = false;
625 updateConfigurationLocked();
626
627 // Start the dock app, if there is one.
628 boolean dockAppStarted = false;
629 if (category != null) {
630 // Now we are going to be careful about switching the
631 // configuration and starting the activity -- we need to
632 // do this in a specific order under control of the
633 // activity manager, to do it cleanly. So compute the
634 // new config, but don't set it yet, and let the
635 // activity manager take care of both the start and config
636 // change.
637 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800638 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700639 try {
640 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800641 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700642 mConfiguration, null, UserHandle.USER_CURRENT);
643 if (result >= ActivityManager.START_SUCCESS) {
644 dockAppStarted = true;
645 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
646 Slog.e(TAG, "Could not start dock app: " + homeIntent
647 + ", startActivityWithConfig result " + result);
648 }
649 } catch (RemoteException ex) {
650 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700651 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700652 }
653 }
654
655 // Send the new configuration.
656 sendConfigurationLocked();
657
658 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700659 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800660 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700661 }
662 }
663
Dianne Hackborn7299c412010-03-04 18:41:49 -0800664 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800665 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800666 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700667 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800668 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800669 }
670
Joe Onorato089de882010-04-12 08:18:45 -0700671 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800672 // features of the status bar; these are ORed together to form the
673 // active disabled list. So if (for example) the device is locked and
674 // the status bar should be totally disabled, the calls below will
675 // have no effect until the device is unlocked.
676 if (mStatusBarManager != null) {
677 mStatusBarManager.disable(mCarModeEnabled
678 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
679 : StatusBarManager.DISABLE_NONE);
680 }
681
682 if (mNotificationManager == null) {
683 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800684 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800685 }
686
687 if (mNotificationManager != null) {
688 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800689 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800690
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400691 Notification.Builder n = new Notification.Builder(context)
692 .setSmallIcon(R.drawable.stat_notify_car_mode)
693 .setDefaults(Notification.DEFAULT_LIGHTS)
694 .setOngoing(true)
695 .setWhen(0)
696 .setColor(context.getColor(
697 com.android.internal.R.color.system_notification_accent_color))
698 .setContentTitle(
699 context.getString(R.string.car_mode_disable_notification_title))
700 .setContentText(
701 context.getString(R.string.car_mode_disable_notification_message))
702 .setContentIntent(
703 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
704 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700705 mNotificationManager.notifyAsUser(null,
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400706 R.string.car_mode_disable_notification_title, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800707 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700708 mNotificationManager.cancelAsUser(null,
709 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800710 }
711 }
712 }
713
Jeff Brown2416e092012-08-21 22:12:20 -0700714 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700715 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700716 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700717 if (state != null) {
718 mComputedNightMode = state.isNight();
719 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800720 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800721 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100722
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100723
Dianne Hackborn7299c412010-03-04 18:41:49 -0800724}