blob: 0b67ad81b379ec964fe6048aedbc84b769508a17 [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.
60 private static final boolean ENABLE_LAUNCH_CAR_DOCK_APP = true;
John Spurlock960779d2012-05-29 14:37:05 -040061 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080062
Dianne Hackborn7299c412010-03-04 18:41:49 -080063 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080064 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010065
Adam Lesinski182f73f2013-12-05 16:48:06 -080066 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Alan Viverette4cc1e9e2015-02-12 11:01:06 -080067 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
Adam Lesinski182f73f2013-12-05 16:48:06 -080068
Dianne Hackborn7299c412010-03-04 18:41:49 -080069 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050070 private boolean mCharging = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080071 private int mDefaultUiModeType;
72 private boolean mCarModeKeepsScreenOn;
73 private boolean mDeskModeKeepsScreenOn;
74 private boolean mTelevision;
John Spurlock6c191292014-04-03 16:37:27 -040075 private boolean mWatch;
Dianne Hackborn7299c412010-03-04 18:41:49 -080076 private boolean mComputedNightMode;
keunyoung1d0a7cc2014-07-28 13:12:50 -070077 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080078
79 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080080 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080081 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080082
Dianne Hackborn7299c412010-03-04 18:41:49 -080083 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -080084 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010085
Adam Lesinski182f73f2013-12-05 16:48:06 -080086 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080087
Adam Lesinski182f73f2013-12-05 16:48:06 -080088 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080089 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080090 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -070091
Adam Lesinski182f73f2013-12-05 16:48:06 -080092 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -080093
Jeff Brownb880d882014-02-10 19:47:07 -080094 public UiModeManagerService(Context context) {
95 super(context);
96 }
97
Adam Lesinski182f73f2013-12-05 16:48:06 -080098 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -070099 Intent intent = new Intent(Intent.ACTION_MAIN);
100 intent.addCategory(category);
101 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
102 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
103 return intent;
104 }
John Spurlock960779d2012-05-29 14:37:05 -0400105
Dianne Hackborn7299c412010-03-04 18:41:49 -0800106 // The broadcast receiver which receives the result of the ordered broadcast sent when
107 // the dock state changes. The original ordered broadcast is sent with an initial result
108 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
109 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
110 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
111 @Override
112 public void onReceive(Context context, Intent intent) {
113 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400114 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400115 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400116 + ": canceled: " + getResultCode());
117 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800118 return;
119 }
120
Jeff Brown62c82e42012-09-26 01:30:41 -0700121 final int enableFlags = intent.getIntExtra("enableFlags", 0);
122 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800123 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700124 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800125 }
126 }
127 };
128
Dianne Hackborn7299c412010-03-04 18:41:49 -0800129 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
130 @Override
131 public void onReceive(Context context, Intent intent) {
132 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
133 Intent.EXTRA_DOCK_STATE_UNDOCKED);
134 updateDockState(state);
135 }
136 };
137
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500138 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
139 @Override
140 public void onReceive(Context context, Intent intent) {
141 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
142 synchronized (mLock) {
143 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700144 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500145 }
146 }
147 }
148 };
149
Adam Lesinski182f73f2013-12-05 16:48:06 -0800150 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700151 @Override
Jeff Brown2416e092012-08-21 22:12:20 -0700152 public void onTwilightStateChanged() {
153 updateTwilight();
Dianne Hackborn57f45032010-06-17 15:49:33 -0700154 }
155 };
156
Adam Lesinski182f73f2013-12-05 16:48:06 -0800157 @Override
158 public void onStart() {
159 final Context context = getContext();
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800160
Adam Lesinski182f73f2013-12-05 16:48:06 -0800161 final PowerManager powerManager =
162 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
163 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800164
Adam Lesinski182f73f2013-12-05 16:48:06 -0800165 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800166 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800167 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500168 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
169
Dianne Hackborn7299c412010-03-04 18:41:49 -0800170 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500171
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800172 final Resources res = context.getResources();
173 mDefaultUiModeType = res.getInteger(
Joe Onorato44fcb832011-12-14 20:59:30 -0800174 com.android.internal.R.integer.config_defaultUiModeType);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800175 mCarModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500176 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800177 mDeskModeKeepsScreenOn = (res.getInteger(
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500178 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700179
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800180 final PackageManager pm = context.getPackageManager();
181 mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
182 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
183 mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
184
185 final int defaultNightMode = res.getInteger(
186 com.android.internal.R.integer.config_defaultNightMode);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800187 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800188 Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
Jeff Brown2416e092012-08-21 22:12:20 -0700189
Adam Lesinski05199e82015-03-19 14:37:11 -0700190 // Update the initial, static configurations.
191 synchronized (this) {
192 updateConfigurationLocked();
193 sendConfigurationLocked();
194 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800195
196 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800197 }
198
Adam Lesinski182f73f2013-12-05 16:48:06 -0800199 private final IBinder mService = new IUiModeManager.Stub() {
200 @Override
201 public void enableCarMode(int flags) {
202 final long ident = Binder.clearCallingIdentity();
203 try {
204 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700205 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800206 if (mSystemReady) {
207 updateLocked(flags, 0);
208 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700209 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800210 } finally {
211 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500212 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800213 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800214
Adam Lesinski182f73f2013-12-05 16:48:06 -0800215 @Override
216 public void disableCarMode(int flags) {
217 final long ident = Binder.clearCallingIdentity();
218 try {
219 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700220 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800221 if (mSystemReady) {
222 updateLocked(0, flags);
223 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700224 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800225 } finally {
226 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500227 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700228 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100229
Adam Lesinski182f73f2013-12-05 16:48:06 -0800230 @Override
231 public int getCurrentModeType() {
232 final long ident = Binder.clearCallingIdentity();
233 try {
234 synchronized (mLock) {
235 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700236 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800237 } finally {
238 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800239 }
240 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100241
Adam Lesinski182f73f2013-12-05 16:48:06 -0800242 @Override
243 public void setNightMode(int mode) {
244 switch (mode) {
245 case UiModeManager.MODE_NIGHT_NO:
246 case UiModeManager.MODE_NIGHT_YES:
247 case UiModeManager.MODE_NIGHT_AUTO:
248 break;
249 default:
250 throw new IllegalArgumentException("Unknown mode: " + mode);
251 }
252
253 final long ident = Binder.clearCallingIdentity();
254 try {
255 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800256 if (mNightMode != mode) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800257 Settings.Secure.putInt(getContext().getContentResolver(),
258 Settings.Secure.UI_NIGHT_MODE, mode);
259 mNightMode = mode;
260 updateLocked(0, 0);
261 }
262 }
263 } finally {
264 Binder.restoreCallingIdentity(ident);
265 }
266 }
267
268 @Override
269 public int getNightMode() {
270 synchronized (mLock) {
271 return mNightMode;
272 }
273 }
274
275 @Override
276 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
277 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
278 != PackageManager.PERMISSION_GRANTED) {
279
280 pw.println("Permission Denial: can't dump uimode service from from pid="
281 + Binder.getCallingPid()
282 + ", uid=" + Binder.getCallingUid());
283 return;
284 }
285
286 dumpImpl(pw);
287 }
288 };
289
290 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700291 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800292 pw.println("Current UI Mode Service state:");
293 pw.print(" mDockState="); pw.print(mDockState);
294 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
295 pw.print(" mNightMode="); pw.print(mNightMode);
296 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700297 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
298 pw.print(" mCarModeEnableFlags="); pw.println(mCarModeEnableFlags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800299 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
300 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
301 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
302 pw.print(" mSystemReady="); pw.println(mSystemReady);
Adam Lesinski05199e82015-03-19 14:37:11 -0700303 if (mTwilightManager != null) {
304 // We may not have a TwilightManager.
305 pw.print(" mTwilightService.getCurrentState()=");
306 pw.println(mTwilightManager.getCurrentState());
307 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700308 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800309 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100310
Adam Lesinski182f73f2013-12-05 16:48:06 -0800311 @Override
312 public void onBootPhase(int phase) {
313 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
314 synchronized (mLock) {
Adam Lesinski05199e82015-03-19 14:37:11 -0700315 mTwilightManager = getLocalService(TwilightManager.class);
316 if (mTwilightManager != null) {
317 mTwilightManager.registerListener(mTwilightListener, mHandler);
318 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800319 mSystemReady = true;
320 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
321 updateComputedNightModeLocked();
322 updateLocked(0, 0);
323 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800324 }
325 }
326
keunyoung1d0a7cc2014-07-28 13:12:50 -0700327 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800328 if (mCarModeEnabled != enabled) {
329 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800330 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700331 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800332 }
333
Jeff Brown487bb6e2012-10-11 13:35:42 -0700334 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800335 synchronized (mLock) {
336 if (newState != mDockState) {
337 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700338 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800339 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700340 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800341 }
342 }
343 }
344 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500345
Jeff Brown487bb6e2012-10-11 13:35:42 -0700346 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400347 switch (state) {
348 case Intent.EXTRA_DOCK_STATE_DESK:
349 case Intent.EXTRA_DOCK_STATE_LE_DESK:
350 case Intent.EXTRA_DOCK_STATE_HE_DESK:
351 return true;
352 default:
353 return false;
354 }
355 }
356
Jeff Brown487bb6e2012-10-11 13:35:42 -0700357 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400358 int uiMode = mDefaultUiModeType;
359 if (mTelevision) {
360 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
361 } else if (mWatch) {
362 uiMode = Configuration.UI_MODE_TYPE_WATCH;
363 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800364 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400365 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800366 uiMode = Configuration.UI_MODE_TYPE_DESK;
367 }
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800368
369 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
370 updateComputedNightModeLocked();
371 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
372 : Configuration.UI_MODE_NIGHT_NO;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800373 } else {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800374 uiMode |= mNightMode << 4;
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400375 }
376
377 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400378 Slog.d(TAG,
379 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400380 + "; mCarMode=" + mCarModeEnabled
381 + "; mNightMode=" + mNightMode
382 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800383 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100384
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800385 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700386 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700387 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700388 }
389 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100390
Jeff Brown487bb6e2012-10-11 13:35:42 -0700391 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700392 if (mSetUiMode != mConfiguration.uiMode) {
393 mSetUiMode = mConfiguration.uiMode;
394
395 try {
396 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
397 } catch (RemoteException e) {
398 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800399 }
400 }
401 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100402
Adam Lesinski182f73f2013-12-05 16:48:06 -0800403 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700404 String action = null;
405 String oldAction = null;
406 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
407 adjustStatusBarCarModeLocked();
408 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
409 } else if (isDeskDockState(mLastBroadcastState)) {
410 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
411 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100412
Jeff Brown487bb6e2012-10-11 13:35:42 -0700413 if (mCarModeEnabled) {
414 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100415 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700416
417 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800418 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700419 }
420 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
421 action = UiModeManager.ACTION_ENTER_CAR_MODE;
422 }
423 } else if (isDeskDockState(mDockState)) {
424 if (!isDeskDockState(mLastBroadcastState)) {
425 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800426 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700427 }
428 mLastBroadcastState = mDockState;
429 action = UiModeManager.ACTION_ENTER_DESK_MODE;
430 }
431 } else {
432 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
433 action = oldAction;
434 }
435
436 if (action != null) {
437 if (LOG) {
438 Slog.v(TAG, String.format(
439 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
440 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800441 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100442
Jeff Brown487bb6e2012-10-11 13:35:42 -0700443 // Send the ordered broadcast; the result receiver will receive after all
444 // broadcasts have been sent. If any broadcast receiver changes the result
445 // code from the initial value of RESULT_OK, then the result receiver will
446 // not launch the corresponding dock application. This gives apps a chance
447 // to override the behavior and stay in their app even when the device is
448 // placed into a dock.
449 Intent intent = new Intent(action);
450 intent.putExtra("enableFlags", enableFlags);
451 intent.putExtra("disableFlags", disableFlags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800452 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700453 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100454
Jeff Brown487bb6e2012-10-11 13:35:42 -0700455 // Attempting to make this transition a little more clean, we are going
456 // to hold off on doing a configuration change until we have finished
457 // the broadcast and started the home activity.
458 mHoldingConfiguration = true;
459 updateConfigurationLocked();
460 } else {
461 String category = null;
462 if (mCarModeEnabled) {
463 if (ENABLE_LAUNCH_CAR_DOCK_APP
464 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
465 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800466 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400467 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700468 if (ENABLE_LAUNCH_DESK_DOCK_APP
469 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
470 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800471 }
472 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700473 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
474 category = Intent.CATEGORY_HOME;
475 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800476 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100477
Jeff Brown487bb6e2012-10-11 13:35:42 -0700478 if (LOG) {
479 Slog.v(TAG, "updateLocked: null action, mDockState="
480 + mDockState +", category=" + category);
481 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400482
Jeff Brown487bb6e2012-10-11 13:35:42 -0700483 sendConfigurationAndStartDreamOrDockAppLocked(category);
484 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700485
Jeff Brown487bb6e2012-10-11 13:35:42 -0700486 // keep screen on when charging and in car mode
487 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700488 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
keunyoungc093bf22014-08-11 18:51:15 -0700489 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700490 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
491 if (keepScreenOn != mWakeLock.isHeld()) {
492 if (keepScreenOn) {
493 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700494 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700495 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800496 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800497 }
498 }
499
Jeff Brown62c82e42012-09-26 01:30:41 -0700500 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
501 // Launch a dock activity
502 String category = null;
503 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
504 // Only launch car home when car mode is enabled and the caller
505 // has asked us to switch to it.
506 if (ENABLE_LAUNCH_CAR_DOCK_APP
507 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
508 category = Intent.CATEGORY_CAR_DOCK;
509 }
510 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
511 // Only launch car home when desk mode is enabled and the caller
512 // has asked us to switch to it. Currently re-using the car
513 // mode flag since we don't have a formal API for "desk mode".
514 if (ENABLE_LAUNCH_DESK_DOCK_APP
515 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
516 category = Intent.CATEGORY_DESK_DOCK;
517 }
518 } else {
519 // Launch the standard home app if requested.
520 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
521 category = Intent.CATEGORY_HOME;
522 }
523 }
524
525 if (LOG) {
526 Slog.v(TAG, String.format(
527 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
528 + "category=%s",
529 action, enableFlags, disableFlags, category));
530 }
531
532 sendConfigurationAndStartDreamOrDockAppLocked(category);
533 }
534
535 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
536 // Update the configuration but don't send it yet.
537 mHoldingConfiguration = false;
538 updateConfigurationLocked();
539
540 // Start the dock app, if there is one.
541 boolean dockAppStarted = false;
542 if (category != null) {
543 // Now we are going to be careful about switching the
544 // configuration and starting the activity -- we need to
545 // do this in a specific order under control of the
546 // activity manager, to do it cleanly. So compute the
547 // new config, but don't set it yet, and let the
548 // activity manager take care of both the start and config
549 // change.
550 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800551 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700552 try {
553 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800554 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700555 mConfiguration, null, UserHandle.USER_CURRENT);
556 if (result >= ActivityManager.START_SUCCESS) {
557 dockAppStarted = true;
558 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
559 Slog.e(TAG, "Could not start dock app: " + homeIntent
560 + ", startActivityWithConfig result " + result);
561 }
562 } catch (RemoteException ex) {
563 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700564 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700565 }
566 }
567
568 // Send the new configuration.
569 sendConfigurationLocked();
570
571 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700572 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800573 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700574 }
575 }
576
Dianne Hackborn7299c412010-03-04 18:41:49 -0800577 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800578 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800579 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700580 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800581 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800582 }
583
Joe Onorato089de882010-04-12 08:18:45 -0700584 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800585 // features of the status bar; these are ORed together to form the
586 // active disabled list. So if (for example) the device is locked and
587 // the status bar should be totally disabled, the calls below will
588 // have no effect until the device is unlocked.
589 if (mStatusBarManager != null) {
590 mStatusBarManager.disable(mCarModeEnabled
591 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
592 : StatusBarManager.DISABLE_NONE);
593 }
594
595 if (mNotificationManager == null) {
596 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800597 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800598 }
599
600 if (mNotificationManager != null) {
601 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800602 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800603
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400604 Notification.Builder n = new Notification.Builder(context)
605 .setSmallIcon(R.drawable.stat_notify_car_mode)
606 .setDefaults(Notification.DEFAULT_LIGHTS)
607 .setOngoing(true)
608 .setWhen(0)
609 .setColor(context.getColor(
610 com.android.internal.R.color.system_notification_accent_color))
611 .setContentTitle(
612 context.getString(R.string.car_mode_disable_notification_title))
613 .setContentText(
614 context.getString(R.string.car_mode_disable_notification_message))
615 .setContentIntent(
616 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
617 null, UserHandle.CURRENT));
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700618 mNotificationManager.notifyAsUser(null,
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400619 R.string.car_mode_disable_notification_title, n.build(), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800620 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700621 mNotificationManager.cancelAsUser(null,
622 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800623 }
624 }
625 }
626
Adam Lesinski182f73f2013-12-05 16:48:06 -0800627 void updateTwilight() {
Jeff Brown2416e092012-08-21 22:12:20 -0700628 synchronized (mLock) {
Alan Viverette4cc1e9e2015-02-12 11:01:06 -0800629 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700630 updateComputedNightModeLocked();
631 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800632 }
633 }
Jeff Brown2416e092012-08-21 22:12:20 -0700634 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800635
Jeff Brown2416e092012-08-21 22:12:20 -0700636 private void updateComputedNightModeLocked() {
Adam Lesinski05199e82015-03-19 14:37:11 -0700637 if (mTwilightManager != null) {
638 TwilightState state = mTwilightManager.getCurrentState();
639 if (state != null) {
640 mComputedNightMode = state.isNight();
641 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800642 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800643 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100644
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100645
Dianne Hackborn7299c412010-03-04 18:41:49 -0800646}