blob: 6ea6fb7c9c0376ae342133451735c0d4ab03db2c [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;
Adam Lesinski182f73f2013-12-05 16:48:06 -080038import android.os.IBinder;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050039import android.os.PowerManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080040import android.os.RemoteException;
Jason Monk97a06a12016-11-09 15:53:30 -050041import android.os.SystemProperties;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070042import android.os.UserHandle;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070043import android.provider.Settings;
Jeff Brown11159e92012-10-11 15:58:37 -070044import android.service.dreams.Sandman;
Dianne Hackborn7299c412010-03-04 18:41:49 -080045import android.util.Slog;
Jason Monk97a06a12016-11-09 15:53:30 -050046import android.view.WindowManagerInternal;
47import android.view.WindowManagerPolicy;
Dianne Hackborn7299c412010-03-04 18:41:49 -080048
49import java.io.FileDescriptor;
50import java.io.PrintWriter;
51
52import com.android.internal.R;
53import com.android.internal.app.DisableCarModeActivity;
Jason Monk97a06a12016-11-09 15:53:30 -050054import com.android.server.power.ShutdownThread;
Adam Lesinski182f73f2013-12-05 16:48:06 -080055import com.android.server.twilight.TwilightListener;
56import com.android.server.twilight.TwilightManager;
57import com.android.server.twilight.TwilightState;
Jason Monk97a06a12016-11-09 15:53:30 -050058import com.android.server.wm.WindowManagerService;
Dianne Hackborn7299c412010-03-04 18:41:49 -080059
Adam Lesinski182f73f2013-12-05 16:48:06 -080060final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080061 private static final String TAG = UiModeManager.class.getSimpleName();
62 private static final boolean LOG = false;
63
Daniel Sandler11ddf532011-11-16 11:10:03 -080064 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040065 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080066
Dianne Hackborn7299c412010-03-04 18:41:49 -080067 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080068 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010069
Adam Lesinski182f73f2013-12-05 16:48:06 -080070 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080071 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080072
Dianne Hackborn7299c412010-03-04 18:41:49 -080073 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050074 private boolean mCharging = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080075 private int mDefaultUiModeType;
76 private boolean mCarModeKeepsScreenOn;
77 private boolean mDeskModeKeepsScreenOn;
78 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040079 private boolean mWatch;
Dianne Hackborn7299c412010-03-04 18:41:49 -080080 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070081 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080082
keunyounga7710492015-09-23 11:33:58 -070083 // flag set by resource, whether to enable Car dock launch when starting car mode.
84 private boolean mEnableCarDockLaunch = true;
85 // flag set by resource, whether to lock UI mode to the default one or not.
86 private boolean mUiModeLocked = false;
87 // flag set by resource, whether to night mode change for normal all or not.
88 private boolean mNightModeLocked = false;
89
Adam Lesinski182f73f2013-12-05 16:48:06 -080090 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080091 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080092 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080093
Dianne Hackborn7299c412010-03-04 18:41:49 -080094 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -080095 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010096
Adam Lesinski182f73f2013-12-05 16:48:06 -080097 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080098
Adam Lesinski182f73f2013-12-05 16:48:06 -080099 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800100 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800101 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -0700102
Adam Lesinski182f73f2013-12-05 16:48:06 -0800103 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800104
Jeff Brownb880d882014-02-10 19:47:07 -0800105 public UiModeManagerService(Context context) {
106 super(context);
107 }
108
Adam Lesinski182f73f2013-12-05 16:48:06 -0800109 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700110 Intent intent = new Intent(Intent.ACTION_MAIN);
111 intent.addCategory(category);
112 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
113 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
114 return intent;
115 }
John Spurlock960779d2012-05-29 14:37:05 -0400116
Dianne Hackborn7299c412010-03-04 18:41:49 -0800117 // The broadcast receiver which receives the result of the ordered broadcast sent when
118 // the dock state changes. The original ordered broadcast is sent with an initial result
119 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
120 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
121 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
122 @Override
123 public void onReceive(Context context, Intent intent) {
124 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400125 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400126 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400127 + ": canceled: " + getResultCode());
128 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800129 return;
130 }
131
Jeff Brown62c82e42012-09-26 01:30:41 -0700132 final int enableFlags = intent.getIntExtra("enableFlags", 0);
133 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800134 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700135 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800136 }
137 }
138 };
139
Dianne Hackborn7299c412010-03-04 18:41:49 -0800140 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
141 @Override
142 public void onReceive(Context context, Intent intent) {
143 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
144 Intent.EXTRA_DOCK_STATE_UNDOCKED);
145 updateDockState(state);
146 }
147 };
148
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500149 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
150 @Override
151 public void onReceive(Context context, Intent intent) {
152 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
153 synchronized (mLock) {
154 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700155 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500156 }
157 }
158 }
159 };
160
Adam Lesinski182f73f2013-12-05 16:48:06 -0800161 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700162 @Override
Justin Klaassen908b86c2016-08-08 09:18:42 -0700163 public void onTwilightStateChanged(@Nullable TwilightState state) {
164 synchronized (mLock) {
165 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
166 updateComputedNightModeLocked();
167 updateLocked(0, 0);
168 }
169 }
Dianne Hackborn57f45032010-06-17 15:49:33 -0700170 }
171 };
172
Adam Lesinski182f73f2013-12-05 16:48:06 -0800173 @Override
174 public void onStart() {
175 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800176
Adam Lesinski182f73f2013-12-05 16:48:06 -0800177 final PowerManager powerManager =
178 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
179 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800180
Adam Lesinski182f73f2013-12-05 16:48:06 -0800181 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800182 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800183 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500184 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
185
Dianne Hackborn7299c412010-03-04 18:41:49 -0800186 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500187
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800188 final Resources res = context.getResources();
189 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800190 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800191 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500192 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800193 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500194 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700195 mEnableCarDockLaunch = res.getBoolean(
196 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
197 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
198 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700199
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800200 final PackageManager pm = context.getPackageManager();
201 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
202 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
203 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
204
205 final int defaultNightMode = res.getInteger(
206 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800207 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800208 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700209
Adam Lesinski05199e82015-03-19 14:37:11 -0700210 // Update the initial, static configurations.
211 synchronized (this) {
212 updateConfigurationLocked();
213 sendConfigurationLocked();
214 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800215
216 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800217 }
218
Adam Lesinski182f73f2013-12-05 16:48:06 -0800219 private final IBinder mService = new IUiModeManager.Stub() {
220 @Override
221 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700222 if (isUiModeLocked()) {
223 Slog.e(TAG, "enableCarMode while UI mode is locked");
224 return;
225 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800226 final long ident = Binder.clearCallingIdentity();
227 try {
228 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700229 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800230 if (mSystemReady) {
231 updateLocked(flags, 0);
232 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700233 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800234 } finally {
235 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500236 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800237 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800238
Adam Lesinski182f73f2013-12-05 16:48:06 -0800239 @Override
240 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700241 if (isUiModeLocked()) {
242 Slog.e(TAG, "disableCarMode while UI mode is locked");
243 return;
244 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800245 final long ident = Binder.clearCallingIdentity();
246 try {
247 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700248 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800249 if (mSystemReady) {
250 updateLocked(0, flags);
251 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700252 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800253 } finally {
254 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500255 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700256 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100257
Adam Lesinski182f73f2013-12-05 16:48:06 -0800258 @Override
259 public int getCurrentModeType() {
260 final long ident = Binder.clearCallingIdentity();
261 try {
262 synchronized (mLock) {
263 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700264 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800265 } finally {
266 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800267 }
268 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100269
Adam Lesinski182f73f2013-12-05 16:48:06 -0800270 @Override
271 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700272 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
273 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
274 != PackageManager.PERMISSION_GRANTED)) {
275 Slog.e(TAG,
276 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
277 return;
278 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800279 switch (mode) {
280 case UiModeManager.MODE_NIGHT_NO:
281 case UiModeManager.MODE_NIGHT_YES:
282 case UiModeManager.MODE_NIGHT_AUTO:
283 break;
284 default:
285 throw new IllegalArgumentException("Unknown mode: " + mode);
286 }
287
288 final long ident = Binder.clearCallingIdentity();
289 try {
290 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800291 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800292 Settings.Secure.putInt(getContext().getContentResolver(),
293 Settings.Secure.UI_NIGHT_MODE, mode);
294 mNightMode = mode;
295 updateLocked(0, 0);
296 }
297 }
298 } finally {
299 Binder.restoreCallingIdentity(ident);
300 }
301 }
302
303 @Override
Jason Monk97a06a12016-11-09 15:53:30 -0500304 public void setTheme(String theme) {
305 if (getContext().checkCallingOrSelfPermission(
306 android.Manifest.permission.MODIFY_THEME_OVERLAY)
307 != PackageManager.PERMISSION_GRANTED) {
308 Slog.e(TAG, "setTheme requires MODIFY_THEME_OVERLAY permission");
309 return;
310 }
311 SystemProperties.set("persist.vendor.overlay.theme", theme);
312 mHandler.post(() -> ShutdownThread.reboot(getContext(),
313 PowerManager.SHUTDOWN_USER_REQUESTED, false));
314 }
315
316 @Override
317 public String getTheme() {
318 if (getContext().checkCallingOrSelfPermission(
319 android.Manifest.permission.MODIFY_THEME_OVERLAY)
320 != PackageManager.PERMISSION_GRANTED) {
321 Slog.e(TAG, "setTheme requires MODIFY_THEME_OVERLAY permission");
322 return null;
323 }
324 return SystemProperties.get("persist.vendor.overlay.theme");
325 }
326
327 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800328 public int getNightMode() {
329 synchronized (mLock) {
330 return mNightMode;
331 }
332 }
333
334 @Override
keunyounga7710492015-09-23 11:33:58 -0700335 public boolean isUiModeLocked() {
336 synchronized (mLock) {
337 return mUiModeLocked;
338 }
339 }
340
341 @Override
342 public boolean isNightModeLocked() {
343 synchronized (mLock) {
344 return mNightModeLocked;
345 }
346 }
347
348 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800349 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
350 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
351 != PackageManager.PERMISSION_GRANTED) {
352
353 pw.println("Permission Denial: can't dump uimode service from from pid="
354 + Binder.getCallingPid()
355 + ", uid=" + Binder.getCallingUid());
356 return;
357 }
358
359 dumpImpl(pw);
360 }
361 };
362
363 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700364 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800365 pw.println("Current UI Mode Service state:");
366 pw.print(" mDockState="); pw.print(mDockState);
367 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
368 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700369 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800370 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700371 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700372 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
373 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800374 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700375 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800376 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
377 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
378 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700379 if (mTwilightManager != null) {
380 // We may not have a TwilightManager.
Justin Klaassen908b86c2016-08-08 09:18:42 -0700381 pw.print(" mTwilightService.getLastTwilightState()=");
382 pw.println(mTwilightManager.getLastTwilightState());
Adam Lesinski05199e82015-03-19 14:37:11 -0700383 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700384 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800385 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100386
Adam Lesinski182f73f2013-12-05 16:48:06 -0800387 @Override
388 public void onBootPhase(int phase) {
389 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
390 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700391 mTwilightManager = getLocalService(TwilightManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800392 mSystemReady = true;
393 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
394 updateComputedNightModeLocked();
395 updateLocked(0, 0);
396 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800397 }
398 }
399
keunyoung1d0a7cc2014-07-28 13:12:50 -0700400 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800401 if (mCarModeEnabled != enabled) {
402 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800403 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700404 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800405 }
406
Jeff Brown487bb6e2012-10-11 13:35:42 -0700407 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800408 synchronized (mLock) {
409 if (newState != mDockState) {
410 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700411 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800412 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700413 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800414 }
415 }
416 }
417 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500418
Jeff Brown487bb6e2012-10-11 13:35:42 -0700419 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400420 switch (state) {
421 case Intent.EXTRA_DOCK_STATE_DESK:
422 case Intent.EXTRA_DOCK_STATE_LE_DESK:
423 case Intent.EXTRA_DOCK_STATE_HE_DESK:
424 return true;
425 default:
426 return false;
427 }
428 }
429
Jeff Brown487bb6e2012-10-11 13:35:42 -0700430 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400431 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700432 if (mUiModeLocked) {
433 // no-op, keeps default one
434 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400435 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
436 } else if (mWatch) {
437 uiMode = Configuration.UI_MODE_TYPE_WATCH;
438 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800439 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400440 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800441 uiMode = Configuration.UI_MODE_TYPE_DESK;
442 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800443
444 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700445 if (mTwilightManager != null) {
446 mTwilightManager.registerListener(mTwilightListener, mHandler);
447 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800448 updateComputedNightModeLocked();
449 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
450 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800451 } else {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700452 if (mTwilightManager != null) {
453 mTwilightManager.unregisterListener(mTwilightListener);
454 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800455 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400456 }
457
458 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400459 Slog.d(TAG,
460 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400461 + "; mCarMode=" + mCarModeEnabled
462 + "; mNightMode=" + mNightMode
463 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800464 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100465
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800466 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700467 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700468 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700469 }
470 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100471
Jeff Brown487bb6e2012-10-11 13:35:42 -0700472 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700473 if (mSetUiMode != mConfiguration.uiMode) {
474 mSetUiMode = mConfiguration.uiMode;
475
476 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800477 ActivityManager.getService().updateConfiguration(mConfiguration);
Jeff Brown62c82e42012-09-26 01:30:41 -0700478 } catch (RemoteException e) {
479 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800480 }
481 }
482 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100483
Adam Lesinski182f73f2013-12-05 16:48:06 -0800484 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700485 String action = null;
486 String oldAction = null;
487 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
488 adjustStatusBarCarModeLocked();
489 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
490 } else if (isDeskDockState(mLastBroadcastState)) {
491 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
492 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100493
Jeff Brown487bb6e2012-10-11 13:35:42 -0700494 if (mCarModeEnabled) {
495 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100496 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700497 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700498 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700499 }
500 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
501 action = UiModeManager.ACTION_ENTER_CAR_MODE;
502 }
503 } else if (isDeskDockState(mDockState)) {
504 if (!isDeskDockState(mLastBroadcastState)) {
505 if (oldAction != null) {
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700506 sendForegroundBroadcastToAllUsers(oldAction);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700507 }
508 mLastBroadcastState = mDockState;
509 action = UiModeManager.ACTION_ENTER_DESK_MODE;
510 }
511 } else {
512 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
513 action = oldAction;
514 }
515
516 if (action != null) {
517 if (LOG) {
518 Slog.v(TAG, String.format(
519 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
520 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800521 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100522
Jeff Brown487bb6e2012-10-11 13:35:42 -0700523 // Send the ordered broadcast; the result receiver will receive after all
524 // broadcasts have been sent. If any broadcast receiver changes the result
525 // code from the initial value of RESULT_OK, then the result receiver will
526 // not launch the corresponding dock application. This gives apps a chance
527 // to override the behavior and stay in their app even when the device is
528 // placed into a dock.
529 Intent intent = new Intent(action);
530 intent.putExtra("enableFlags", enableFlags);
531 intent.putExtra("disableFlags", disableFlags);
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700532 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800533 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700534 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100535
Jeff Brown487bb6e2012-10-11 13:35:42 -0700536 // Attempting to make this transition a little more clean, we are going
537 // to hold off on doing a configuration change until we have finished
538 // the broadcast and started the home activity.
539 mHoldingConfiguration = true;
540 updateConfigurationLocked();
541 } else {
542 String category = null;
543 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700544 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700545 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
546 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800547 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400548 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700549 if (ENABLE_LAUNCH_DESK_DOCK_APP
550 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
551 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800552 }
553 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700554 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
555 category = Intent.CATEGORY_HOME;
556 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800557 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100558
Jeff Brown487bb6e2012-10-11 13:35:42 -0700559 if (LOG) {
560 Slog.v(TAG, "updateLocked: null action, mDockState="
561 + mDockState +", category=" + category);
562 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400563
Jeff Brown487bb6e2012-10-11 13:35:42 -0700564 sendConfigurationAndStartDreamOrDockAppLocked(category);
565 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700566
Jeff Brown487bb6e2012-10-11 13:35:42 -0700567 // keep screen on when charging and in car mode
568 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700569 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700570 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700571 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
572 if (keepScreenOn != mWakeLock.isHeld()) {
573 if (keepScreenOn) {
574 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700575 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700576 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800577 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800578 }
579 }
580
Adrian Roos2fbf4d52016-10-20 14:40:51 -0700581 private void sendForegroundBroadcastToAllUsers(String action) {
582 getContext().sendBroadcastAsUser(new Intent(action)
583 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND), UserHandle.ALL);
584 }
585
Jeff Brown62c82e42012-09-26 01:30:41 -0700586 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
587 // Launch a dock activity
588 String category = null;
589 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
590 // Only launch car home when car mode is enabled and the caller
591 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700592 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700593 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
594 category = Intent.CATEGORY_CAR_DOCK;
595 }
596 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
597 // Only launch car home when desk mode is enabled and the caller
598 // has asked us to switch to it. Currently re-using the car
599 // mode flag since we don't have a formal API for "desk mode".
600 if (ENABLE_LAUNCH_DESK_DOCK_APP
601 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
602 category = Intent.CATEGORY_DESK_DOCK;
603 }
604 } else {
605 // Launch the standard home app if requested.
606 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
607 category = Intent.CATEGORY_HOME;
608 }
609 }
610
611 if (LOG) {
612 Slog.v(TAG, String.format(
613 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
614 + "category=%s",
615 action, enableFlags, disableFlags, category));
616 }
617
618 sendConfigurationAndStartDreamOrDockAppLocked(category);
619 }
620
621 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
622 // Update the configuration but don't send it yet.
623 mHoldingConfiguration = false;
624 updateConfigurationLocked();
625
626 // Start the dock app, if there is one.
627 boolean dockAppStarted = false;
628 if (category != null) {
629 // Now we are going to be careful about switching the
630 // configuration and starting the activity -- we need to
631 // do this in a specific order under control of the
632 // activity manager, to do it cleanly. So compute the
633 // new config, but don't set it yet, and let the
634 // activity manager take care of both the start and config
635 // change.
636 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800637 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700638 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800639 int result = ActivityManager.getService().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800640 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700641 mConfiguration, null, UserHandle.USER_CURRENT);
642 if (result >= ActivityManager.START_SUCCESS) {
643 dockAppStarted = true;
644 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
645 Slog.e(TAG, "Could not start dock app: " + homeIntent
646 + ", startActivityWithConfig result " + result);
647 }
648 } catch (RemoteException ex) {
649 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700650 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700651 }
652 }
653
654 // Send the new configuration.
655 sendConfigurationLocked();
656
657 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700658 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800659 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700660 }
661 }
662
Dianne Hackborn7299c412010-03-04 18:41:49 -0800663 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800664 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800665 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700666 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800667 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800668 }
669
Joe Onorato089de882010-04-12 08:18:45 -0700670 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800671 // features of the status bar; these are ORed together to form the
672 // active disabled list. So if (for example) the device is locked and
673 // the status bar should be totally disabled, the calls below will
674 // have no effect until the device is unlocked.
675 if (mStatusBarManager != null) {
676 mStatusBarManager.disable(mCarModeEnabled
677 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
678 : StatusBarManager.DISABLE_NONE);
679 }
680
681 if (mNotificationManager == null) {
682 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800683 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800684 }
685
686 if (mNotificationManager != null) {
687 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800688 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800689
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400690 Notification.Builder n = new Notification.Builder(context)
691 .setSmallIcon(R.drawable.stat_notify_car_mode)
692 .setDefaults(Notification.DEFAULT_LIGHTS)
693 .setOngoing(true)
694 .setWhen(0)
695 .setColor(context.getColor(
696 com.android.internal.R.color.system_notification_accent_color))
697 .setContentTitle(
698 context.getString(R.string.car_mode_disable_notification_title))
699 .setContentText(
700 context.getString(R.string.car_mode_disable_notification_message))
701 .setContentIntent(
702 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
703 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700704 mNotificationManager.notifyAsUser(null,
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400705 R.string.car_mode_disable_notification_title, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800706 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700707 mNotificationManager.cancelAsUser(null,
708 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800709 }
710 }
711 }
712
Jeff Brown2416e092012-08-21 22:12:20 -0700713 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700714 if (mTwilightManager != null) {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700715 TwilightState state = mTwilightManager.getLastTwilightState();
Adam Lesinski05199e82015-03-19 14:37:11 -0700716 if (state != null) {
717 mComputedNightMode = state.isNight();
718 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800719 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800720 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100721
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100722
Dianne Hackborn7299c412010-03-04 18:41:49 -0800723}