blob: 6f713cdfebbe0e0844c7dc18a9be73da9ccd39aa [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
19import android.app.Activity;
Jeff Brown62c82e42012-09-26 01:30:41 -070020import android.app.ActivityManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080021import android.app.ActivityManagerNative;
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;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070041import android.os.UserHandle;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070042import android.provider.Settings;
Jeff Brown11159e92012-10-11 15:58:37 -070043import android.service.dreams.Sandman;
Dianne Hackborn7299c412010-03-04 18:41:49 -080044import android.util.Slog;
45
46import java.io.FileDescriptor;
47import java.io.PrintWriter;
48
49import com.android.internal.R;
50import com.android.internal.app.DisableCarModeActivity;
Adam Lesinski182f73f2013-12-05 16:48:06 -080051import com.android.server.twilight.TwilightListener;
52import com.android.server.twilight.TwilightManager;
53import com.android.server.twilight.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080054
Adam Lesinski182f73f2013-12-05 16:48:06 -080055final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080056 private static final String TAG = UiModeManager.class.getSimpleName();
57 private static final boolean LOG = false;
58
Daniel Sandler11ddf532011-11-16 11:10:03 -080059 // Enable launching of applications when entering the dock.
John Spurlock960779d2012-05-29 14:37:05 -040060 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080061
Dianne Hackborn7299c412010-03-04 18:41:49 -080062 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080063 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010064
Adam Lesinski182f73f2013-12-05 16:48:06 -080065 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080066 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080067
Dianne Hackborn7299c412010-03-04 18:41:49 -080068 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050069 private boolean mCharging = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080070 private int mDefaultUiModeType;
71 private boolean mCarModeKeepsScreenOn;
72 private boolean mDeskModeKeepsScreenOn;
73 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040074 private boolean mWatch;
Dianne Hackborn7299c412010-03-04 18:41:49 -080075 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070076 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080077
keunyounga7710492015-09-23 11:33:58 -070078 // flag set by resource, whether to enable Car dock launch when starting car mode.
79 private boolean mEnableCarDockLaunch = true;
80 // flag set by resource, whether to lock UI mode to the default one or not.
81 private boolean mUiModeLocked = false;
82 // flag set by resource, whether to night mode change for normal all or not.
83 private boolean mNightModeLocked = false;
84
Adam Lesinski182f73f2013-12-05 16:48:06 -080085 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080086 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080087 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080088
Dianne Hackborn7299c412010-03-04 18:41:49 -080089 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -080090 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010091
Adam Lesinski182f73f2013-12-05 16:48:06 -080092 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080093
Adam Lesinski182f73f2013-12-05 16:48:06 -080094 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080095 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080096 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -070097
Adam Lesinski182f73f2013-12-05 16:48:06 -080098 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -080099
Jeff Brownb880d882014-02-10 19:47:07 -0800100 public UiModeManagerService(Context context) {
101 super(context);
102 }
103
Adam Lesinski182f73f2013-12-05 16:48:06 -0800104 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700105 Intent intent = new Intent(Intent.ACTION_MAIN);
106 intent.addCategory(category);
107 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
108 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
109 return intent;
110 }
John Spurlock960779d2012-05-29 14:37:05 -0400111
Dianne Hackborn7299c412010-03-04 18:41:49 -0800112 // The broadcast receiver which receives the result of the ordered broadcast sent when
113 // the dock state changes. The original ordered broadcast is sent with an initial result
114 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
115 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
116 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
117 @Override
118 public void onReceive(Context context, Intent intent) {
119 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400120 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400121 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400122 + ": canceled: " + getResultCode());
123 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800124 return;
125 }
126
Jeff Brown62c82e42012-09-26 01:30:41 -0700127 final int enableFlags = intent.getIntExtra("enableFlags", 0);
128 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800129 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700130 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800131 }
132 }
133 };
134
Dianne Hackborn7299c412010-03-04 18:41:49 -0800135 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
136 @Override
137 public void onReceive(Context context, Intent intent) {
138 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
139 Intent.EXTRA_DOCK_STATE_UNDOCKED);
140 updateDockState(state);
141 }
142 };
143
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500144 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
145 @Override
146 public void onReceive(Context context, Intent intent) {
147 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
148 synchronized (mLock) {
149 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700150 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500151 }
152 }
153 }
154 };
155
Adam Lesinski182f73f2013-12-05 16:48:06 -0800156 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700157 @Override
Jeff Brown2416e092012-08-21 22:12:20 -0700158 public void onTwilightStateChanged() {
159 updateTwilight();
Dianne Hackborn57f45032010-06-17 15:49:33 -0700160 }
161 };
162
Adam Lesinski182f73f2013-12-05 16:48:06 -0800163 @Override
164 public void onStart() {
165 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800166
Adam Lesinski182f73f2013-12-05 16:48:06 -0800167 final PowerManager powerManager =
168 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
169 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800170
Adam Lesinski182f73f2013-12-05 16:48:06 -0800171 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800172 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800173 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500174 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
175
Dianne Hackborn7299c412010-03-04 18:41:49 -0800176 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500177
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800178 final Resources res = context.getResources();
179 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800180 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800181 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500182 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800183 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500184 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
keunyounga7710492015-09-23 11:33:58 -0700185 mEnableCarDockLaunch = res.getBoolean(
186 com.android.internal.R.bool.config_enableCarDockHomeLaunch);
187 mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
188 mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700189
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800190 final PackageManager pm = context.getPackageManager();
191 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
192 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
193 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
194
195 final int defaultNightMode = res.getInteger(
196 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800197 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800198 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700199
Adam Lesinski05199e82015-03-19 14:37:11 -0700200 // Update the initial, static configurations.
201 synchronized (this) {
202 updateConfigurationLocked();
203 sendConfigurationLocked();
204 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800205
206 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800207 }
208
Adam Lesinski182f73f2013-12-05 16:48:06 -0800209 private final IBinder mService = new IUiModeManager.Stub() {
210 @Override
211 public void enableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700212 if (isUiModeLocked()) {
213 Slog.e(TAG, "enableCarMode while UI mode is locked");
214 return;
215 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800216 final long ident = Binder.clearCallingIdentity();
217 try {
218 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700219 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800220 if (mSystemReady) {
221 updateLocked(flags, 0);
222 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700223 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800224 } finally {
225 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500226 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800227 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800228
Adam Lesinski182f73f2013-12-05 16:48:06 -0800229 @Override
230 public void disableCarMode(int flags) {
keunyounga7710492015-09-23 11:33:58 -0700231 if (isUiModeLocked()) {
232 Slog.e(TAG, "disableCarMode while UI mode is locked");
233 return;
234 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800235 final long ident = Binder.clearCallingIdentity();
236 try {
237 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700238 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800239 if (mSystemReady) {
240 updateLocked(0, flags);
241 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700242 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800243 } finally {
244 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500245 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700246 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100247
Adam Lesinski182f73f2013-12-05 16:48:06 -0800248 @Override
249 public int getCurrentModeType() {
250 final long ident = Binder.clearCallingIdentity();
251 try {
252 synchronized (mLock) {
253 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700254 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800255 } finally {
256 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800257 }
258 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100259
Adam Lesinski182f73f2013-12-05 16:48:06 -0800260 @Override
261 public void setNightMode(int mode) {
keunyounga7710492015-09-23 11:33:58 -0700262 if (isNightModeLocked() && (getContext().checkCallingOrSelfPermission(
263 android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
264 != PackageManager.PERMISSION_GRANTED)) {
265 Slog.e(TAG,
266 "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
267 return;
268 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800269 switch (mode) {
270 case UiModeManager.MODE_NIGHT_NO:
271 case UiModeManager.MODE_NIGHT_YES:
272 case UiModeManager.MODE_NIGHT_AUTO:
273 break;
274 default:
275 throw new IllegalArgumentException("Unknown mode: " + mode);
276 }
277
278 final long ident = Binder.clearCallingIdentity();
279 try {
280 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800281 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800282 Settings.Secure.putInt(getContext().getContentResolver(),
283 Settings.Secure.UI_NIGHT_MODE, mode);
284 mNightMode = mode;
285 updateLocked(0, 0);
286 }
287 }
288 } finally {
289 Binder.restoreCallingIdentity(ident);
290 }
291 }
292
293 @Override
294 public int getNightMode() {
295 synchronized (mLock) {
296 return mNightMode;
297 }
298 }
299
300 @Override
keunyounga7710492015-09-23 11:33:58 -0700301 public boolean isUiModeLocked() {
302 synchronized (mLock) {
303 return mUiModeLocked;
304 }
305 }
306
307 @Override
308 public boolean isNightModeLocked() {
309 synchronized (mLock) {
310 return mNightModeLocked;
311 }
312 }
313
314 @Override
Adam Lesinski182f73f2013-12-05 16:48:06 -0800315 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
316 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
317 != PackageManager.PERMISSION_GRANTED) {
318
319 pw.println("Permission Denial: can't dump uimode service from from pid="
320 + Binder.getCallingPid()
321 + ", uid=" + Binder.getCallingUid());
322 return;
323 }
324
325 dumpImpl(pw);
326 }
327 };
328
329 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700330 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800331 pw.println("Current UI Mode Service state:");
332 pw.print(" mDockState="); pw.print(mDockState);
333 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
334 pw.print(" mNightMode="); pw.print(mNightMode);
keunyounga7710492015-09-23 11:33:58 -0700335 pw.print(" mNightModeLocked="); pw.print(mNightModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800336 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700337 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
keunyounga7710492015-09-23 11:33:58 -0700338 pw.print(" mCarModeEnableFlags="); pw.print(mCarModeEnableFlags);
339 pw.print(" mEnableCarDockLaunch="); pw.println(mEnableCarDockLaunch);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800340 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
keunyounga7710492015-09-23 11:33:58 -0700341 pw.print(" mUiModeLocked="); pw.print(mUiModeLocked);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800342 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
343 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
344 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700345 if (mTwilightManager != null) {
346 // We may not have a TwilightManager.
347 pw.print(" mTwilightService.getCurrentState()=");
348 pw.println(mTwilightManager.getCurrentState());
349 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700350 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800351 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100352
Adam Lesinski182f73f2013-12-05 16:48:06 -0800353 @Override
354 public void onBootPhase(int phase) {
355 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
356 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700357 mTwilightManager = getLocalService(TwilightManager.class);
358 if (mTwilightManager != null) {
359 mTwilightManager.registerListener(mTwilightListener, mHandler);
360 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800361 mSystemReady = true;
362 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
363 updateComputedNightModeLocked();
364 updateLocked(0, 0);
365 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800366 }
367 }
368
keunyoung1d0a7cc2014-07-28 13:12:50 -0700369 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800370 if (mCarModeEnabled != enabled) {
371 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800372 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700373 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800374 }
375
Jeff Brown487bb6e2012-10-11 13:35:42 -0700376 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800377 synchronized (mLock) {
378 if (newState != mDockState) {
379 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700380 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800381 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700382 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800383 }
384 }
385 }
386 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500387
Jeff Brown487bb6e2012-10-11 13:35:42 -0700388 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400389 switch (state) {
390 case Intent.EXTRA_DOCK_STATE_DESK:
391 case Intent.EXTRA_DOCK_STATE_LE_DESK:
392 case Intent.EXTRA_DOCK_STATE_HE_DESK:
393 return true;
394 default:
395 return false;
396 }
397 }
398
Jeff Brown487bb6e2012-10-11 13:35:42 -0700399 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400400 int uiMode = mDefaultUiModeType;
keunyounga7710492015-09-23 11:33:58 -0700401 if (mUiModeLocked) {
402 // no-op, keeps default one
403 } else if (mTelevision) {
John Spurlock6c191292014-04-03 16:37:27 -0400404 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
405 } else if (mWatch) {
406 uiMode = Configuration.UI_MODE_TYPE_WATCH;
407 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800408 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400409 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800410 uiMode = Configuration.UI_MODE_TYPE_DESK;
411 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800412
413 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
414 updateComputedNightModeLocked();
415 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
416 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800417 } else {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800418 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400419 }
420
421 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400422 Slog.d(TAG,
423 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400424 + "; mCarMode=" + mCarModeEnabled
425 + "; mNightMode=" + mNightMode
426 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800427 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100428
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800429 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700430 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700431 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700432 }
433 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100434
Jeff Brown487bb6e2012-10-11 13:35:42 -0700435 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700436 if (mSetUiMode != mConfiguration.uiMode) {
437 mSetUiMode = mConfiguration.uiMode;
438
439 try {
440 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
441 } catch (RemoteException e) {
442 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800443 }
444 }
445 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100446
Adam Lesinski182f73f2013-12-05 16:48:06 -0800447 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700448 String action = null;
449 String oldAction = null;
450 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
451 adjustStatusBarCarModeLocked();
452 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
453 } else if (isDeskDockState(mLastBroadcastState)) {
454 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
455 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100456
Jeff Brown487bb6e2012-10-11 13:35:42 -0700457 if (mCarModeEnabled) {
458 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100459 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700460
461 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800462 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700463 }
464 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
465 action = UiModeManager.ACTION_ENTER_CAR_MODE;
466 }
467 } else if (isDeskDockState(mDockState)) {
468 if (!isDeskDockState(mLastBroadcastState)) {
469 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800470 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700471 }
472 mLastBroadcastState = mDockState;
473 action = UiModeManager.ACTION_ENTER_DESK_MODE;
474 }
475 } else {
476 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
477 action = oldAction;
478 }
479
480 if (action != null) {
481 if (LOG) {
482 Slog.v(TAG, String.format(
483 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
484 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800485 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100486
Jeff Brown487bb6e2012-10-11 13:35:42 -0700487 // Send the ordered broadcast; the result receiver will receive after all
488 // broadcasts have been sent. If any broadcast receiver changes the result
489 // code from the initial value of RESULT_OK, then the result receiver will
490 // not launch the corresponding dock application. This gives apps a chance
491 // to override the behavior and stay in their app even when the device is
492 // placed into a dock.
493 Intent intent = new Intent(action);
494 intent.putExtra("enableFlags", enableFlags);
495 intent.putExtra("disableFlags", disableFlags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800496 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700497 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100498
Jeff Brown487bb6e2012-10-11 13:35:42 -0700499 // Attempting to make this transition a little more clean, we are going
500 // to hold off on doing a configuration change until we have finished
501 // the broadcast and started the home activity.
502 mHoldingConfiguration = true;
503 updateConfigurationLocked();
504 } else {
505 String category = null;
506 if (mCarModeEnabled) {
keunyounga7710492015-09-23 11:33:58 -0700507 if (mEnableCarDockLaunch
Jeff Brown487bb6e2012-10-11 13:35:42 -0700508 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
509 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800510 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400511 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700512 if (ENABLE_LAUNCH_DESK_DOCK_APP
513 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
514 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800515 }
516 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700517 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
518 category = Intent.CATEGORY_HOME;
519 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800520 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100521
Jeff Brown487bb6e2012-10-11 13:35:42 -0700522 if (LOG) {
523 Slog.v(TAG, "updateLocked: null action, mDockState="
524 + mDockState +", category=" + category);
525 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400526
Jeff Brown487bb6e2012-10-11 13:35:42 -0700527 sendConfigurationAndStartDreamOrDockAppLocked(category);
528 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700529
Jeff Brown487bb6e2012-10-11 13:35:42 -0700530 // keep screen on when charging and in car mode
531 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700532 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700533 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700534 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
535 if (keepScreenOn != mWakeLock.isHeld()) {
536 if (keepScreenOn) {
537 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700538 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700539 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800540 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800541 }
542 }
543
Jeff Brown62c82e42012-09-26 01:30:41 -0700544 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
545 // Launch a dock activity
546 String category = null;
547 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
548 // Only launch car home when car mode is enabled and the caller
549 // has asked us to switch to it.
keunyounga7710492015-09-23 11:33:58 -0700550 if (mEnableCarDockLaunch
Jeff Brown62c82e42012-09-26 01:30:41 -0700551 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
552 category = Intent.CATEGORY_CAR_DOCK;
553 }
554 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
555 // Only launch car home when desk mode is enabled and the caller
556 // has asked us to switch to it. Currently re-using the car
557 // mode flag since we don't have a formal API for "desk mode".
558 if (ENABLE_LAUNCH_DESK_DOCK_APP
559 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
560 category = Intent.CATEGORY_DESK_DOCK;
561 }
562 } else {
563 // Launch the standard home app if requested.
564 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
565 category = Intent.CATEGORY_HOME;
566 }
567 }
568
569 if (LOG) {
570 Slog.v(TAG, String.format(
571 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
572 + "category=%s",
573 action, enableFlags, disableFlags, category));
574 }
575
576 sendConfigurationAndStartDreamOrDockAppLocked(category);
577 }
578
579 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
580 // Update the configuration but don't send it yet.
581 mHoldingConfiguration = false;
582 updateConfigurationLocked();
583
584 // Start the dock app, if there is one.
585 boolean dockAppStarted = false;
586 if (category != null) {
587 // Now we are going to be careful about switching the
588 // configuration and starting the activity -- we need to
589 // do this in a specific order under control of the
590 // activity manager, to do it cleanly. So compute the
591 // new config, but don't set it yet, and let the
592 // activity manager take care of both the start and config
593 // change.
594 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800595 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700596 try {
597 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800598 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700599 mConfiguration, null, UserHandle.USER_CURRENT);
600 if (result >= ActivityManager.START_SUCCESS) {
601 dockAppStarted = true;
602 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
603 Slog.e(TAG, "Could not start dock app: " + homeIntent
604 + ", startActivityWithConfig result " + result);
605 }
606 } catch (RemoteException ex) {
607 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700608 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700609 }
610 }
611
612 // Send the new configuration.
613 sendConfigurationLocked();
614
615 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700616 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800617 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700618 }
619 }
620
Dianne Hackborn7299c412010-03-04 18:41:49 -0800621 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800622 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800623 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700624 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800625 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800626 }
627
Joe Onorato089de882010-04-12 08:18:45 -0700628 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800629 // features of the status bar; these are ORed together to form the
630 // active disabled list. So if (for example) the device is locked and
631 // the status bar should be totally disabled, the calls below will
632 // have no effect until the device is unlocked.
633 if (mStatusBarManager != null) {
634 mStatusBarManager.disable(mCarModeEnabled
635 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
636 : StatusBarManager.DISABLE_NONE);
637 }
638
639 if (mNotificationManager == null) {
640 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800641 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800642 }
643
644 if (mNotificationManager != null) {
645 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800646 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800647
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400648 Notification.Builder n = new Notification.Builder(context)
649 .setSmallIcon(R.drawable.stat_notify_car_mode)
650 .setDefaults(Notification.DEFAULT_LIGHTS)
651 .setOngoing(true)
652 .setWhen(0)
653 .setColor(context.getColor(
654 com.android.internal.R.color.system_notification_accent_color))
655 .setContentTitle(
656 context.getString(R.string.car_mode_disable_notification_title))
657 .setContentText(
658 context.getString(R.string.car_mode_disable_notification_message))
659 .setContentIntent(
660 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
661 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700662 mNotificationManager.notifyAsUser(null,
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400663 R.string.car_mode_disable_notification_title, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800664 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700665 mNotificationManager.cancelAsUser(null,
666 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800667 }
668 }
669 }
670
Adam Lesinski182f73f2013-12-05 16:48:06 -0800671 void updateTwilight() {
Jeff Brown2416e092012-08-21 22:12:20 -0700672 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800673 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700674 updateComputedNightModeLocked();
675 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800676 }
677 }
Jeff Brown2416e092012-08-21 22:12:20 -0700678 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800679
Jeff Brown2416e092012-08-21 22:12:20 -0700680 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700681 if (mTwilightManager != null) {
682 TwilightState state = mTwilightManager.getCurrentState();
683 if (state != null) {
684 mComputedNightMode = state.isNight();
685 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800686 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800687 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100688
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100689
Dianne Hackborn7299c412010-03-04 18:41:49 -0800690}