blob: d1af2b0229ef7d44af95c5b22ae0eff123c7b7e4 [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;
Dianne Hackbornbe87e2f2012-09-28 16:31:34 -070042import android.service.dreams.DreamService;
Jeff Brown62c82e42012-09-26 01:30:41 -070043import android.service.dreams.IDreamManager;
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;
Jeff Brown2416e092012-08-21 22:12:20 -070051import com.android.server.TwilightService.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080052
53class UiModeManagerService extends IUiModeManager.Stub {
54 private static final String TAG = UiModeManager.class.getSimpleName();
55 private static final boolean LOG = false;
56
Daniel Sandler11ddf532011-11-16 11:10:03 -080057 // Enable launching of applications when entering the dock.
58 private static final boolean ENABLE_LAUNCH_CAR_DOCK_APP = true;
John Spurlock960779d2012-05-29 14:37:05 -040059 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080060
Jeff Brown62c82e42012-09-26 01:30:41 -070061 private static final int DEFAULT_SCREENSAVER_ENABLED = 1;
62 private static final int DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK = 1;
63
Dianne Hackborn7299c412010-03-04 18:41:49 -080064 private final Context mContext;
Jeff Brown2416e092012-08-21 22:12:20 -070065 private final TwilightService mTwilightService;
66 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080067
68 final Object mLock = new Object();
Bernd Holzheyba9ab182010-03-12 09:30:29 +010069
Dianne Hackborn7299c412010-03-04 18:41:49 -080070 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
71 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010072
Dianne Hackborn7299c412010-03-04 18:41:49 -080073 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
74 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050075 private boolean mCharging = false;
Joe Onorato44fcb832011-12-14 20:59:30 -080076 private final int mDefaultUiModeType;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050077 private final boolean mCarModeKeepsScreenOn;
78 private final boolean mDeskModeKeepsScreenOn;
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -070079 private final boolean mTelevision;
Dianne Hackborn7299c412010-03-04 18:41:49 -080080
81 private boolean mComputedNightMode;
82 private int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080083 private int mSetUiMode = 0;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010084
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080085 private boolean mHoldingConfiguration = false;
Dianne Hackborn7299c412010-03-04 18:41:49 -080086 private Configuration mConfiguration = new Configuration();
Bernd Holzheyba9ab182010-03-12 09:30:29 +010087
Dianne Hackborn7299c412010-03-04 18:41:49 -080088 private boolean mSystemReady;
89
90 private NotificationManager mNotificationManager;
91
Dianne Hackborn7299c412010-03-04 18:41:49 -080092 private StatusBarManager mStatusBarManager;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050093 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -080094
Dianne Hackbornf5c5d222010-04-09 13:14:48 -070095 static Intent buildHomeIntent(String category) {
96 Intent intent = new Intent(Intent.ACTION_MAIN);
97 intent.addCategory(category);
98 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
99 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
100 return intent;
101 }
John Spurlock960779d2012-05-29 14:37:05 -0400102
Dianne Hackborn7299c412010-03-04 18:41:49 -0800103 // The broadcast receiver which receives the result of the ordered broadcast sent when
104 // the dock state changes. The original ordered broadcast is sent with an initial result
105 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
106 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
107 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
108 @Override
109 public void onReceive(Context context, Intent intent) {
110 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400111 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400112 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400113 + ": canceled: " + getResultCode());
114 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800115 return;
116 }
117
Jeff Brown62c82e42012-09-26 01:30:41 -0700118 final int enableFlags = intent.getIntExtra("enableFlags", 0);
119 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800120 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700121 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800122 }
123 }
124 };
125
Dianne Hackborn7299c412010-03-04 18:41:49 -0800126 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
127 @Override
128 public void onReceive(Context context, Intent intent) {
129 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
130 Intent.EXTRA_DOCK_STATE_UNDOCKED);
131 updateDockState(state);
132 }
133 };
134
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500135 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
136 @Override
137 public void onReceive(Context context, Intent intent) {
138 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
139 synchronized (mLock) {
140 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700141 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500142 }
143 }
144 }
145 };
146
Jeff Brown2416e092012-08-21 22:12:20 -0700147 private final TwilightService.TwilightListener mTwilightListener =
148 new TwilightService.TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700149 @Override
Jeff Brown2416e092012-08-21 22:12:20 -0700150 public void onTwilightStateChanged() {
151 updateTwilight();
Dianne Hackborn57f45032010-06-17 15:49:33 -0700152 }
153 };
154
Jeff Brown2416e092012-08-21 22:12:20 -0700155 public UiModeManagerService(Context context, TwilightService twilight) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800156 mContext = context;
Jeff Brown2416e092012-08-21 22:12:20 -0700157 mTwilightService = twilight;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800158
159 ServiceManager.addService(Context.UI_MODE_SERVICE, this);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100160
Dianne Hackborn7299c412010-03-04 18:41:49 -0800161 mContext.registerReceiver(mDockModeReceiver,
162 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500163 mContext.registerReceiver(mBatteryReceiver,
164 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
165
166 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
167 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800168
169 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500170
Joe Onorato44fcb832011-12-14 20:59:30 -0800171 mDefaultUiModeType = context.getResources().getInteger(
172 com.android.internal.R.integer.config_defaultUiModeType);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500173 mCarModeKeepsScreenOn = (context.getResources().getInteger(
174 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
175 mDeskModeKeepsScreenOn = (context.getResources().getInteger(
176 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700177 mTelevision = context.getPackageManager().hasSystemFeature(
178 PackageManager.FEATURE_TELEVISION);
179
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700180 mNightMode = Settings.Secure.getInt(mContext.getContentResolver(),
181 Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
Jeff Brown2416e092012-08-21 22:12:20 -0700182
183 mTwilightService.registerListener(mTwilightListener, mHandler);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800184 }
185
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700186 public void disableCarMode(int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800187 synchronized (mLock) {
188 setCarModeLocked(false);
Mike Lockwood924e1642010-03-05 11:56:53 -0500189 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700190 updateLocked(0, flags);
Mike Lockwood924e1642010-03-05 11:56:53 -0500191 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800192 }
193 }
194
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700195 public void enableCarMode(int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800196 synchronized (mLock) {
197 setCarModeLocked(true);
Mike Lockwood924e1642010-03-05 11:56:53 -0500198 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700199 updateLocked(flags, 0);
Mike Lockwood924e1642010-03-05 11:56:53 -0500200 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800201 }
202 }
203
204 public int getCurrentModeType() {
205 synchronized (mLock) {
206 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
207 }
208 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100209
Dianne Hackborn7299c412010-03-04 18:41:49 -0800210 public void setNightMode(int mode) throws RemoteException {
211 synchronized (mLock) {
212 switch (mode) {
213 case UiModeManager.MODE_NIGHT_NO:
214 case UiModeManager.MODE_NIGHT_YES:
215 case UiModeManager.MODE_NIGHT_AUTO:
216 break;
217 default:
218 throw new IllegalArgumentException("Unknown mode: " + mode);
219 }
220 if (!isDoingNightMode()) {
221 return;
222 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100223
Dianne Hackborn7299c412010-03-04 18:41:49 -0800224 if (mNightMode != mode) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700225 long ident = Binder.clearCallingIdentity();
226 Settings.Secure.putInt(mContext.getContentResolver(),
227 Settings.Secure.UI_NIGHT_MODE, mode);
228 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800229 mNightMode = mode;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700230 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800231 }
232 }
233 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100234
Dianne Hackborn7299c412010-03-04 18:41:49 -0800235 public int getNightMode() throws RemoteException {
236 return mNightMode;
237 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100238
Dianne Hackborn7299c412010-03-04 18:41:49 -0800239 void systemReady() {
240 synchronized (mLock) {
241 mSystemReady = true;
242 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
Jeff Brown2416e092012-08-21 22:12:20 -0700243 updateComputedNightModeLocked();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700244 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800245 }
246 }
247
248 boolean isDoingNightMode() {
249 return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
250 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100251
Dianne Hackborn7299c412010-03-04 18:41:49 -0800252 void setCarModeLocked(boolean enabled) {
253 if (mCarModeEnabled != enabled) {
254 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800255 }
256 }
257
258 void updateDockState(int newState) {
259 synchronized (mLock) {
260 if (newState != mDockState) {
261 mDockState = newState;
Mike Lockwood924e1642010-03-05 11:56:53 -0500262 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800263 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700264 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800265 }
266 }
267 }
268 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500269
Daniel Sandler69a1da42011-11-04 15:08:30 -0400270 final static boolean isDeskDockState(int state) {
271 switch (state) {
272 case Intent.EXTRA_DOCK_STATE_DESK:
273 case Intent.EXTRA_DOCK_STATE_LE_DESK:
274 case Intent.EXTRA_DOCK_STATE_HE_DESK:
275 return true;
276 default:
277 return false;
278 }
279 }
280
Jeff Brown62c82e42012-09-26 01:30:41 -0700281 final void updateConfigurationLocked() {
282 int uiMode = mTelevision ? Configuration.UI_MODE_TYPE_TELEVISION : mDefaultUiModeType;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800283 if (mCarModeEnabled) {
284 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400285 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800286 uiMode = Configuration.UI_MODE_TYPE_DESK;
287 }
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700288 if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800289 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700290 updateComputedNightModeLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800291 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
292 : Configuration.UI_MODE_NIGHT_NO;
293 } else {
294 uiMode |= mNightMode << 4;
295 }
296 } else {
297 // Disabling the car mode clears the night mode.
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400298 uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO;
299 }
300
301 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400302 Slog.d(TAG,
303 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400304 + "; mCarMode=" + mCarModeEnabled
305 + "; mNightMode=" + mNightMode
306 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800307 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100308
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800309 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700310 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700311 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700312 }
313 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100314
Jeff Brown62c82e42012-09-26 01:30:41 -0700315 final void sendConfigurationLocked() {
316 if (mSetUiMode != mConfiguration.uiMode) {
317 mSetUiMode = mConfiguration.uiMode;
318
319 try {
320 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
321 } catch (RemoteException e) {
322 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800323 }
324 }
325 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100326
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700327 final void updateLocked(int enableFlags, int disableFlags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800328 long ident = Binder.clearCallingIdentity();
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100329
Dianne Hackborn7299c412010-03-04 18:41:49 -0800330 try {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800331 String action = null;
332 String oldAction = null;
333 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100334 adjustStatusBarCarModeLocked();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800335 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400336 } else if (isDeskDockState(mLastBroadcastState)) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800337 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
338 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100339
Dianne Hackborn7299c412010-03-04 18:41:49 -0800340 if (mCarModeEnabled) {
341 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
342 adjustStatusBarCarModeLocked();
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100343
Dianne Hackborn7299c412010-03-04 18:41:49 -0800344 if (oldAction != null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700345 mContext.sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800346 }
347 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
348 action = UiModeManager.ACTION_ENTER_CAR_MODE;
349 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400350 } else if (isDeskDockState(mDockState)) {
351 if (!isDeskDockState(mLastBroadcastState)) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800352 if (oldAction != null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700353 mContext.sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800354 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400355 mLastBroadcastState = mDockState;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800356 action = UiModeManager.ACTION_ENTER_DESK_MODE;
357 }
358 } else {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800359 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
360 action = oldAction;
361 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100362
Dianne Hackborn7299c412010-03-04 18:41:49 -0800363 if (action != null) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400364 if (LOG) {
365 Slog.v(TAG, String.format(
366 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
367 action, enableFlags, disableFlags));
368 }
369
Dianne Hackborn7299c412010-03-04 18:41:49 -0800370 // Send the ordered broadcast; the result receiver will receive after all
371 // broadcasts have been sent. If any broadcast receiver changes the result
372 // code from the initial value of RESULT_OK, then the result receiver will
373 // not launch the corresponding dock application. This gives apps a chance
374 // to override the behavior and stay in their app even when the device is
375 // placed into a dock.
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700376 Intent intent = new Intent(action);
377 intent.putExtra("enableFlags", enableFlags);
378 intent.putExtra("disableFlags", disableFlags);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700379 mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800380 mResultReceiver, null, Activity.RESULT_OK, null, null);
Jeff Brown62c82e42012-09-26 01:30:41 -0700381
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800382 // Attempting to make this transition a little more clean, we are going
383 // to hold off on doing a configuration change until we have finished
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700384 // the broadcast and started the home activity.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800385 mHoldingConfiguration = true;
Jeff Brown62c82e42012-09-26 01:30:41 -0700386 updateConfigurationLocked();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700387 } else {
Jeff Brown62c82e42012-09-26 01:30:41 -0700388 String category = null;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700389 if (mCarModeEnabled) {
Daniel Sandler11ddf532011-11-16 11:10:03 -0800390 if (ENABLE_LAUNCH_CAR_DOCK_APP
Jeff Brown62c82e42012-09-26 01:30:41 -0700391 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
392 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700393 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400394 } else if (isDeskDockState(mDockState)) {
Daniel Sandler11ddf532011-11-16 11:10:03 -0800395 if (ENABLE_LAUNCH_DESK_DOCK_APP
Jeff Brown62c82e42012-09-26 01:30:41 -0700396 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
397 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700398 }
399 } else {
Jeff Brown62c82e42012-09-26 01:30:41 -0700400 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
401 category = Intent.CATEGORY_HOME;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700402 }
403 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400404
405 if (LOG) {
406 Slog.v(TAG, "updateLocked: null action, mDockState="
Jeff Brown62c82e42012-09-26 01:30:41 -0700407 + mDockState +", category=" + category);
Daniel Sandler69a1da42011-11-04 15:08:30 -0400408 }
409
Jeff Brown62c82e42012-09-26 01:30:41 -0700410 sendConfigurationAndStartDreamOrDockAppLocked(category);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800411 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100412
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500413 // keep screen on when charging and in car mode
414 boolean keepScreenOn = mCharging &&
415 ((mCarModeEnabled && mCarModeKeepsScreenOn) ||
416 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
417 if (keepScreenOn != mWakeLock.isHeld()) {
418 if (keepScreenOn) {
419 mWakeLock.acquire();
420 } else {
421 mWakeLock.release();
422 }
423 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800424 } finally {
425 Binder.restoreCallingIdentity(ident);
426 }
427 }
428
Jeff Brown62c82e42012-09-26 01:30:41 -0700429 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
430 // Launch a dock activity
431 String category = null;
432 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
433 // Only launch car home when car mode is enabled and the caller
434 // has asked us to switch to it.
435 if (ENABLE_LAUNCH_CAR_DOCK_APP
436 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
437 category = Intent.CATEGORY_CAR_DOCK;
438 }
439 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
440 // Only launch car home when desk mode is enabled and the caller
441 // has asked us to switch to it. Currently re-using the car
442 // mode flag since we don't have a formal API for "desk mode".
443 if (ENABLE_LAUNCH_DESK_DOCK_APP
444 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
445 category = Intent.CATEGORY_DESK_DOCK;
446 }
447 } else {
448 // Launch the standard home app if requested.
449 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
450 category = Intent.CATEGORY_HOME;
451 }
452 }
453
454 if (LOG) {
455 Slog.v(TAG, String.format(
456 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
457 + "category=%s",
458 action, enableFlags, disableFlags, category));
459 }
460
461 sendConfigurationAndStartDreamOrDockAppLocked(category);
462 }
463
464 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
465 // Update the configuration but don't send it yet.
466 mHoldingConfiguration = false;
467 updateConfigurationLocked();
468
469 // Start the dock app, if there is one.
470 boolean dockAppStarted = false;
471 if (category != null) {
472 // Now we are going to be careful about switching the
473 // configuration and starting the activity -- we need to
474 // do this in a specific order under control of the
475 // activity manager, to do it cleanly. So compute the
476 // new config, but don't set it yet, and let the
477 // activity manager take care of both the start and config
478 // change.
479 Intent homeIntent = buildHomeIntent(category);
480 try {
481 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
482 null, homeIntent, null, null, null, 0, 0,
483 mConfiguration, null, UserHandle.USER_CURRENT);
484 if (result >= ActivityManager.START_SUCCESS) {
485 dockAppStarted = true;
486 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
487 Slog.e(TAG, "Could not start dock app: " + homeIntent
488 + ", startActivityWithConfig result " + result);
489 }
490 } catch (RemoteException ex) {
491 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
492 }
493 }
494
495 // Send the new configuration.
496 sendConfigurationLocked();
497
498 // If we did not start a dock app, then start dreaming if supported.
Jeff Brownf9d40f42012-09-26 18:57:48 -0700499 if (category != null && !dockAppStarted
500 && isScreenSaverEnabled() && isScreenSaverActivatedOnDock()) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700501 Slog.i(TAG, "Activating dream while docked.");
502 try {
503 IDreamManager dreamManagerService = IDreamManager.Stub.asInterface(
Dianne Hackbornbe87e2f2012-09-28 16:31:34 -0700504 ServiceManager.getService(DreamService.DREAM_SERVICE));
Jeff Brown62c82e42012-09-26 01:30:41 -0700505 dreamManagerService.dream();
506 } catch (RemoteException ex) {
507 Slog.e(TAG, "Could not start dream when docked.", ex);
508 }
509 }
510 }
511
512 private boolean isScreenSaverEnabled() {
513 return Settings.Secure.getIntForUser(mContext.getContentResolver(),
514 Settings.Secure.SCREENSAVER_ENABLED, DEFAULT_SCREENSAVER_ENABLED,
515 UserHandle.USER_CURRENT) != 0;
516 }
517
518 private boolean isScreenSaverActivatedOnDock() {
519 return Settings.Secure.getIntForUser(mContext.getContentResolver(),
520 Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
521 DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK, UserHandle.USER_CURRENT) != 0;
522 }
523
Dianne Hackborn7299c412010-03-04 18:41:49 -0800524 private void adjustStatusBarCarModeLocked() {
525 if (mStatusBarManager == null) {
526 mStatusBarManager = (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE);
527 }
528
Joe Onorato089de882010-04-12 08:18:45 -0700529 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800530 // features of the status bar; these are ORed together to form the
531 // active disabled list. So if (for example) the device is locked and
532 // the status bar should be totally disabled, the calls below will
533 // have no effect until the device is unlocked.
534 if (mStatusBarManager != null) {
535 mStatusBarManager.disable(mCarModeEnabled
536 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
537 : StatusBarManager.DISABLE_NONE);
538 }
539
540 if (mNotificationManager == null) {
541 mNotificationManager = (NotificationManager)
542 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
543 }
544
545 if (mNotificationManager != null) {
546 if (mCarModeEnabled) {
547 Intent carModeOffIntent = new Intent(mContext, DisableCarModeActivity.class);
548
549 Notification n = new Notification();
550 n.icon = R.drawable.stat_notify_car_mode;
551 n.defaults = Notification.DEFAULT_LIGHTS;
552 n.flags = Notification.FLAG_ONGOING_EVENT;
553 n.when = 0;
554 n.setLatestEventInfo(
555 mContext,
556 mContext.getString(R.string.car_mode_disable_notification_title),
557 mContext.getString(R.string.car_mode_disable_notification_message),
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700558 PendingIntent.getActivityAsUser(mContext, 0, carModeOffIntent, 0,
559 null, UserHandle.CURRENT));
560 mNotificationManager.notifyAsUser(null,
561 R.string.car_mode_disable_notification_title, n, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800562 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700563 mNotificationManager.cancelAsUser(null,
564 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800565 }
566 }
567 }
568
Jeff Brown2416e092012-08-21 22:12:20 -0700569 private void updateTwilight() {
570 synchronized (mLock) {
571 if (isDoingNightMode() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
572 updateComputedNightModeLocked();
573 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800574 }
575 }
Jeff Brown2416e092012-08-21 22:12:20 -0700576 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800577
Jeff Brown2416e092012-08-21 22:12:20 -0700578 private void updateComputedNightModeLocked() {
579 TwilightState state = mTwilightService.getCurrentState();
580 if (state != null) {
581 mComputedNightMode = state.isNight();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800582 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800583 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100584
Dianne Hackborn7299c412010-03-04 18:41:49 -0800585 @Override
586 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
587 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
588 != PackageManager.PERMISSION_GRANTED) {
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100589
Dianne Hackborn7299c412010-03-04 18:41:49 -0800590 pw.println("Permission Denial: can't dump uimode service from from pid="
591 + Binder.getCallingPid()
592 + ", uid=" + Binder.getCallingUid());
593 return;
594 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100595
Dianne Hackborn7299c412010-03-04 18:41:49 -0800596 synchronized (mLock) {
597 pw.println("Current UI Mode Service state:");
598 pw.print(" mDockState="); pw.print(mDockState);
599 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
600 pw.print(" mNightMode="); pw.print(mNightMode);
601 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
602 pw.print(" mComputedNightMode="); pw.println(mComputedNightMode);
603 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800604 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
605 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800606 pw.print(" mSystemReady="); pw.println(mSystemReady);
Jeff Brown2416e092012-08-21 22:12:20 -0700607 pw.print(" mTwilightService.getCurrentState()=");
608 pw.println(mTwilightService.getCurrentState());
Dianne Hackborn7299c412010-03-04 18:41:49 -0800609 }
610 }
611}