blob: 0e456f15fea0d0e38c5a8465bdd015d7460bdcee [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;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050034import android.os.BatteryManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080035import android.os.Binder;
Dianne Hackborn7299c412010-03-04 18:41:49 -080036import android.os.Handler;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050037import android.os.PowerManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080038import android.os.RemoteException;
39import android.os.ServiceManager;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070040import android.os.UserHandle;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070041import android.provider.Settings;
Jeff Brown11159e92012-10-11 15:58:37 -070042import android.service.dreams.Sandman;
Dianne Hackborn7299c412010-03-04 18:41:49 -080043import android.util.Slog;
44
45import java.io.FileDescriptor;
46import java.io.PrintWriter;
47
48import com.android.internal.R;
49import com.android.internal.app.DisableCarModeActivity;
Jeff Brown2416e092012-08-21 22:12:20 -070050import com.android.server.TwilightService.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080051
Jeff Brown487bb6e2012-10-11 13:35:42 -070052final class UiModeManagerService extends IUiModeManager.Stub {
Dianne Hackborn7299c412010-03-04 18:41:49 -080053 private static final String TAG = UiModeManager.class.getSimpleName();
54 private static final boolean LOG = false;
55
Daniel Sandler11ddf532011-11-16 11:10:03 -080056 // Enable launching of applications when entering the dock.
57 private static final boolean ENABLE_LAUNCH_CAR_DOCK_APP = true;
John Spurlock960779d2012-05-29 14:37:05 -040058 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080059
Dianne Hackborn7299c412010-03-04 18:41:49 -080060 private final Context mContext;
Jeff Brown2416e092012-08-21 22:12:20 -070061 private final TwilightService mTwilightService;
62 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080063
64 final Object mLock = new Object();
Bernd Holzheyba9ab182010-03-12 09:30:29 +010065
Dianne Hackborn7299c412010-03-04 18:41:49 -080066 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
67 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010068
Dianne Hackborn7299c412010-03-04 18:41:49 -080069 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
70 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050071 private boolean mCharging = false;
Joe Onorato44fcb832011-12-14 20:59:30 -080072 private final int mDefaultUiModeType;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050073 private final boolean mCarModeKeepsScreenOn;
74 private final boolean mDeskModeKeepsScreenOn;
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -070075 private final boolean mTelevision;
Dianne Hackborn7299c412010-03-04 18:41:49 -080076
77 private boolean mComputedNightMode;
78 private int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080079 private int mSetUiMode = 0;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010080
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080081 private boolean mHoldingConfiguration = false;
Dianne Hackborn7299c412010-03-04 18:41:49 -080082 private Configuration mConfiguration = new Configuration();
Bernd Holzheyba9ab182010-03-12 09:30:29 +010083
Dianne Hackborn7299c412010-03-04 18:41:49 -080084 private boolean mSystemReady;
85
86 private NotificationManager mNotificationManager;
87
Dianne Hackborn7299c412010-03-04 18:41:49 -080088 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -070089
90 private final PowerManager mPowerManager;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050091 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -080092
Dianne Hackbornf5c5d222010-04-09 13:14:48 -070093 static Intent buildHomeIntent(String category) {
94 Intent intent = new Intent(Intent.ACTION_MAIN);
95 intent.addCategory(category);
96 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
97 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
98 return intent;
99 }
John Spurlock960779d2012-05-29 14:37:05 -0400100
Dianne Hackborn7299c412010-03-04 18:41:49 -0800101 // The broadcast receiver which receives the result of the ordered broadcast sent when
102 // the dock state changes. The original ordered broadcast is sent with an initial result
103 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
104 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
105 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
106 @Override
107 public void onReceive(Context context, Intent intent) {
108 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400109 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400110 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400111 + ": canceled: " + getResultCode());
112 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800113 return;
114 }
115
Jeff Brown62c82e42012-09-26 01:30:41 -0700116 final int enableFlags = intent.getIntExtra("enableFlags", 0);
117 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800118 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700119 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800120 }
121 }
122 };
123
Dianne Hackborn7299c412010-03-04 18:41:49 -0800124 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
125 @Override
126 public void onReceive(Context context, Intent intent) {
127 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
128 Intent.EXTRA_DOCK_STATE_UNDOCKED);
129 updateDockState(state);
130 }
131 };
132
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500133 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
134 @Override
135 public void onReceive(Context context, Intent intent) {
136 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
137 synchronized (mLock) {
138 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700139 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500140 }
141 }
142 }
143 };
144
Jeff Brown2416e092012-08-21 22:12:20 -0700145 private final TwilightService.TwilightListener mTwilightListener =
146 new TwilightService.TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700147 @Override
Jeff Brown2416e092012-08-21 22:12:20 -0700148 public void onTwilightStateChanged() {
149 updateTwilight();
Dianne Hackborn57f45032010-06-17 15:49:33 -0700150 }
151 };
152
Jeff Brown2416e092012-08-21 22:12:20 -0700153 public UiModeManagerService(Context context, TwilightService twilight) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800154 mContext = context;
Jeff Brown2416e092012-08-21 22:12:20 -0700155 mTwilightService = twilight;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800156
157 ServiceManager.addService(Context.UI_MODE_SERVICE, this);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100158
Dianne Hackborn7299c412010-03-04 18:41:49 -0800159 mContext.registerReceiver(mDockModeReceiver,
160 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500161 mContext.registerReceiver(mBatteryReceiver,
162 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
163
Jeff Brown9fca9e92012-10-05 14:42:56 -0700164 mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
165 mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800166
167 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500168
Joe Onorato44fcb832011-12-14 20:59:30 -0800169 mDefaultUiModeType = context.getResources().getInteger(
170 com.android.internal.R.integer.config_defaultUiModeType);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500171 mCarModeKeepsScreenOn = (context.getResources().getInteger(
172 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
173 mDeskModeKeepsScreenOn = (context.getResources().getInteger(
174 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700175 mTelevision = context.getPackageManager().hasSystemFeature(
176 PackageManager.FEATURE_TELEVISION);
177
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700178 mNightMode = Settings.Secure.getInt(mContext.getContentResolver(),
179 Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
Jeff Brown2416e092012-08-21 22:12:20 -0700180
181 mTwilightService.registerListener(mTwilightListener, mHandler);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800182 }
183
Jeff Brown487bb6e2012-10-11 13:35:42 -0700184 @Override // Binder call
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700185 public void disableCarMode(int flags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700186 final long ident = Binder.clearCallingIdentity();
187 try {
188 synchronized (mLock) {
189 setCarModeLocked(false);
190 if (mSystemReady) {
191 updateLocked(0, flags);
192 }
Mike Lockwood924e1642010-03-05 11:56:53 -0500193 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700194 } finally {
195 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800196 }
197 }
198
Jeff Brown487bb6e2012-10-11 13:35:42 -0700199 @Override // Binder call
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700200 public void enableCarMode(int flags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700201 final long ident = Binder.clearCallingIdentity();
202 try {
203 synchronized (mLock) {
204 setCarModeLocked(true);
205 if (mSystemReady) {
206 updateLocked(flags, 0);
207 }
Mike Lockwood924e1642010-03-05 11:56:53 -0500208 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700209 } finally {
210 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800211 }
212 }
213
Jeff Brown487bb6e2012-10-11 13:35:42 -0700214 @Override // Binder call
Dianne Hackborn7299c412010-03-04 18:41:49 -0800215 public int getCurrentModeType() {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700216 final long ident = Binder.clearCallingIdentity();
217 try {
218 synchronized (mLock) {
219 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
220 }
221 } finally {
222 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800223 }
224 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100225
Jeff Brown487bb6e2012-10-11 13:35:42 -0700226 @Override // Binder call
227 public void setNightMode(int mode) {
228 switch (mode) {
229 case UiModeManager.MODE_NIGHT_NO:
230 case UiModeManager.MODE_NIGHT_YES:
231 case UiModeManager.MODE_NIGHT_AUTO:
232 break;
233 default:
234 throw new IllegalArgumentException("Unknown mode: " + mode);
235 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100236
Jeff Brown487bb6e2012-10-11 13:35:42 -0700237 final long ident = Binder.clearCallingIdentity();
238 try {
239 synchronized (mLock) {
240 if (isDoingNightModeLocked() && mNightMode != mode) {
241 Settings.Secure.putInt(mContext.getContentResolver(),
242 Settings.Secure.UI_NIGHT_MODE, mode);
243 mNightMode = mode;
244 updateLocked(0, 0);
245 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800246 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700247 } finally {
248 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800249 }
250 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100251
Jeff Brown487bb6e2012-10-11 13:35:42 -0700252 @Override // Binder call
253 public int getNightMode() {
254 synchronized (mLock) {
255 return mNightMode;
256 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800257 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100258
Dianne Hackborn7299c412010-03-04 18:41:49 -0800259 void systemReady() {
260 synchronized (mLock) {
261 mSystemReady = true;
262 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
Jeff Brown2416e092012-08-21 22:12:20 -0700263 updateComputedNightModeLocked();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700264 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800265 }
266 }
267
Jeff Brown487bb6e2012-10-11 13:35:42 -0700268 private boolean isDoingNightModeLocked() {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800269 return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
270 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100271
Jeff Brown487bb6e2012-10-11 13:35:42 -0700272 private void setCarModeLocked(boolean enabled) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800273 if (mCarModeEnabled != enabled) {
274 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800275 }
276 }
277
Jeff Brown487bb6e2012-10-11 13:35:42 -0700278 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800279 synchronized (mLock) {
280 if (newState != mDockState) {
281 mDockState = newState;
Mike Lockwood924e1642010-03-05 11:56:53 -0500282 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800283 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700284 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800285 }
286 }
287 }
288 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500289
Jeff Brown487bb6e2012-10-11 13:35:42 -0700290 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400291 switch (state) {
292 case Intent.EXTRA_DOCK_STATE_DESK:
293 case Intent.EXTRA_DOCK_STATE_LE_DESK:
294 case Intent.EXTRA_DOCK_STATE_HE_DESK:
295 return true;
296 default:
297 return false;
298 }
299 }
300
Jeff Brown487bb6e2012-10-11 13:35:42 -0700301 private void updateConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700302 int uiMode = mTelevision ? Configuration.UI_MODE_TYPE_TELEVISION : mDefaultUiModeType;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800303 if (mCarModeEnabled) {
304 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400305 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800306 uiMode = Configuration.UI_MODE_TYPE_DESK;
307 }
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700308 if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800309 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700310 updateComputedNightModeLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800311 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
312 : Configuration.UI_MODE_NIGHT_NO;
313 } else {
314 uiMode |= mNightMode << 4;
315 }
316 } else {
317 // Disabling the car mode clears the night mode.
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400318 uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO;
319 }
320
321 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400322 Slog.d(TAG,
323 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400324 + "; mCarMode=" + mCarModeEnabled
325 + "; mNightMode=" + mNightMode
326 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800327 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100328
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800329 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700330 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700331 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700332 }
333 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100334
Jeff Brown487bb6e2012-10-11 13:35:42 -0700335 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700336 if (mSetUiMode != mConfiguration.uiMode) {
337 mSetUiMode = mConfiguration.uiMode;
338
339 try {
340 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
341 } catch (RemoteException e) {
342 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800343 }
344 }
345 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100346
Jeff Brown487bb6e2012-10-11 13:35:42 -0700347 private void updateLocked(int enableFlags, int disableFlags) {
348 String action = null;
349 String oldAction = null;
350 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
351 adjustStatusBarCarModeLocked();
352 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
353 } else if (isDeskDockState(mLastBroadcastState)) {
354 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
355 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100356
Jeff Brown487bb6e2012-10-11 13:35:42 -0700357 if (mCarModeEnabled) {
358 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100359 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700360
361 if (oldAction != null) {
362 mContext.sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
363 }
364 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
365 action = UiModeManager.ACTION_ENTER_CAR_MODE;
366 }
367 } else if (isDeskDockState(mDockState)) {
368 if (!isDeskDockState(mLastBroadcastState)) {
369 if (oldAction != null) {
370 mContext.sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
371 }
372 mLastBroadcastState = mDockState;
373 action = UiModeManager.ACTION_ENTER_DESK_MODE;
374 }
375 } else {
376 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
377 action = oldAction;
378 }
379
380 if (action != null) {
381 if (LOG) {
382 Slog.v(TAG, String.format(
383 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
384 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800385 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100386
Jeff Brown487bb6e2012-10-11 13:35:42 -0700387 // Send the ordered broadcast; the result receiver will receive after all
388 // broadcasts have been sent. If any broadcast receiver changes the result
389 // code from the initial value of RESULT_OK, then the result receiver will
390 // not launch the corresponding dock application. This gives apps a chance
391 // to override the behavior and stay in their app even when the device is
392 // placed into a dock.
393 Intent intent = new Intent(action);
394 intent.putExtra("enableFlags", enableFlags);
395 intent.putExtra("disableFlags", disableFlags);
396 mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
397 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100398
Jeff Brown487bb6e2012-10-11 13:35:42 -0700399 // Attempting to make this transition a little more clean, we are going
400 // to hold off on doing a configuration change until we have finished
401 // the broadcast and started the home activity.
402 mHoldingConfiguration = true;
403 updateConfigurationLocked();
404 } else {
405 String category = null;
406 if (mCarModeEnabled) {
407 if (ENABLE_LAUNCH_CAR_DOCK_APP
408 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
409 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800410 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400411 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700412 if (ENABLE_LAUNCH_DESK_DOCK_APP
413 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
414 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800415 }
416 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700417 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
418 category = Intent.CATEGORY_HOME;
419 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800420 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100421
Jeff Brown487bb6e2012-10-11 13:35:42 -0700422 if (LOG) {
423 Slog.v(TAG, "updateLocked: null action, mDockState="
424 + mDockState +", category=" + category);
425 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400426
Jeff Brown487bb6e2012-10-11 13:35:42 -0700427 sendConfigurationAndStartDreamOrDockAppLocked(category);
428 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700429
Jeff Brown487bb6e2012-10-11 13:35:42 -0700430 // keep screen on when charging and in car mode
431 boolean keepScreenOn = mCharging &&
432 ((mCarModeEnabled && mCarModeKeepsScreenOn) ||
433 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
434 if (keepScreenOn != mWakeLock.isHeld()) {
435 if (keepScreenOn) {
436 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700437 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700438 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800439 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800440 }
441 }
442
Jeff Brown62c82e42012-09-26 01:30:41 -0700443 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
444 // Launch a dock activity
445 String category = null;
446 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
447 // Only launch car home when car mode is enabled and the caller
448 // has asked us to switch to it.
449 if (ENABLE_LAUNCH_CAR_DOCK_APP
450 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
451 category = Intent.CATEGORY_CAR_DOCK;
452 }
453 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
454 // Only launch car home when desk mode is enabled and the caller
455 // has asked us to switch to it. Currently re-using the car
456 // mode flag since we don't have a formal API for "desk mode".
457 if (ENABLE_LAUNCH_DESK_DOCK_APP
458 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
459 category = Intent.CATEGORY_DESK_DOCK;
460 }
461 } else {
462 // Launch the standard home app if requested.
463 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
464 category = Intent.CATEGORY_HOME;
465 }
466 }
467
468 if (LOG) {
469 Slog.v(TAG, String.format(
470 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
471 + "category=%s",
472 action, enableFlags, disableFlags, category));
473 }
474
475 sendConfigurationAndStartDreamOrDockAppLocked(category);
476 }
477
478 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
479 // Update the configuration but don't send it yet.
480 mHoldingConfiguration = false;
481 updateConfigurationLocked();
482
483 // Start the dock app, if there is one.
484 boolean dockAppStarted = false;
485 if (category != null) {
486 // Now we are going to be careful about switching the
487 // configuration and starting the activity -- we need to
488 // do this in a specific order under control of the
489 // activity manager, to do it cleanly. So compute the
490 // new config, but don't set it yet, and let the
491 // activity manager take care of both the start and config
492 // change.
493 Intent homeIntent = buildHomeIntent(category);
Jeff Brown11159e92012-10-11 15:58:37 -0700494 if (Sandman.shouldStartDockApp(mContext, homeIntent)) {
495 try {
496 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
497 null, homeIntent, null, null, null, 0, 0,
498 mConfiguration, null, UserHandle.USER_CURRENT);
499 if (result >= ActivityManager.START_SUCCESS) {
500 dockAppStarted = true;
501 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
502 Slog.e(TAG, "Could not start dock app: " + homeIntent
503 + ", startActivityWithConfig result " + result);
504 }
505 } catch (RemoteException ex) {
506 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700507 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700508 }
509 }
510
511 // Send the new configuration.
512 sendConfigurationLocked();
513
514 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700515 if (category != null && !dockAppStarted) {
516 Sandman.startDreamWhenDockedIfAppropriate(mContext);
Jeff Brown62c82e42012-09-26 01:30:41 -0700517 }
518 }
519
Dianne Hackborn7299c412010-03-04 18:41:49 -0800520 private void adjustStatusBarCarModeLocked() {
521 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700522 mStatusBarManager = (StatusBarManager)
523 mContext.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800524 }
525
Joe Onorato089de882010-04-12 08:18:45 -0700526 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800527 // features of the status bar; these are ORed together to form the
528 // active disabled list. So if (for example) the device is locked and
529 // the status bar should be totally disabled, the calls below will
530 // have no effect until the device is unlocked.
531 if (mStatusBarManager != null) {
532 mStatusBarManager.disable(mCarModeEnabled
533 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
534 : StatusBarManager.DISABLE_NONE);
535 }
536
537 if (mNotificationManager == null) {
538 mNotificationManager = (NotificationManager)
539 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
540 }
541
542 if (mNotificationManager != null) {
543 if (mCarModeEnabled) {
544 Intent carModeOffIntent = new Intent(mContext, DisableCarModeActivity.class);
545
546 Notification n = new Notification();
547 n.icon = R.drawable.stat_notify_car_mode;
548 n.defaults = Notification.DEFAULT_LIGHTS;
549 n.flags = Notification.FLAG_ONGOING_EVENT;
550 n.when = 0;
551 n.setLatestEventInfo(
552 mContext,
553 mContext.getString(R.string.car_mode_disable_notification_title),
554 mContext.getString(R.string.car_mode_disable_notification_message),
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700555 PendingIntent.getActivityAsUser(mContext, 0, carModeOffIntent, 0,
556 null, UserHandle.CURRENT));
557 mNotificationManager.notifyAsUser(null,
558 R.string.car_mode_disable_notification_title, n, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800559 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700560 mNotificationManager.cancelAsUser(null,
561 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800562 }
563 }
564 }
565
Jeff Brown2416e092012-08-21 22:12:20 -0700566 private void updateTwilight() {
567 synchronized (mLock) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700568 if (isDoingNightModeLocked() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700569 updateComputedNightModeLocked();
570 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800571 }
572 }
Jeff Brown2416e092012-08-21 22:12:20 -0700573 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800574
Jeff Brown2416e092012-08-21 22:12:20 -0700575 private void updateComputedNightModeLocked() {
576 TwilightState state = mTwilightService.getCurrentState();
577 if (state != null) {
578 mComputedNightMode = state.isNight();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800579 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800580 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100581
Dianne Hackborn7299c412010-03-04 18:41:49 -0800582 @Override
583 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
584 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
585 != PackageManager.PERMISSION_GRANTED) {
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100586
Dianne Hackborn7299c412010-03-04 18:41:49 -0800587 pw.println("Permission Denial: can't dump uimode service from from pid="
588 + Binder.getCallingPid()
589 + ", uid=" + Binder.getCallingUid());
590 return;
591 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100592
Dianne Hackborn7299c412010-03-04 18:41:49 -0800593 synchronized (mLock) {
594 pw.println("Current UI Mode Service state:");
595 pw.print(" mDockState="); pw.print(mDockState);
596 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
597 pw.print(" mNightMode="); pw.print(mNightMode);
598 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
599 pw.print(" mComputedNightMode="); pw.println(mComputedNightMode);
600 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800601 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
602 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800603 pw.print(" mSystemReady="); pw.println(mSystemReady);
Jeff Brown2416e092012-08-21 22:12:20 -0700604 pw.print(" mTwilightService.getCurrentState()=");
605 pw.println(mTwilightService.getCurrentState());
Dianne Hackborn7299c412010-03-04 18:41:49 -0800606 }
607 }
608}