blob: 07e8f182ed70ad919017756dacaca14ef9387ef0 [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 Hackbornf5c5d222010-04-09 13:14:48 -070028import android.content.ActivityNotFoundException;
Dianne Hackborn7299c412010-03-04 18:41:49 -080029import android.content.BroadcastReceiver;
30import android.content.Context;
31import android.content.Intent;
32import android.content.IntentFilter;
33import android.content.pm.PackageManager;
34import android.content.res.Configuration;
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;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050038import android.os.PowerManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080039import android.os.RemoteException;
40import android.os.ServiceManager;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070041import android.os.UserHandle;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070042import android.provider.Settings;
Jeff Brown62c82e42012-09-26 01:30:41 -070043import android.service.dreams.Dream;
44import android.service.dreams.IDreamManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080045import android.util.Slog;
46
47import java.io.FileDescriptor;
48import java.io.PrintWriter;
49
50import com.android.internal.R;
51import com.android.internal.app.DisableCarModeActivity;
Jeff Brown2416e092012-08-21 22:12:20 -070052import com.android.server.TwilightService.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080053
54class UiModeManagerService extends IUiModeManager.Stub {
55 private static final String TAG = UiModeManager.class.getSimpleName();
56 private static final boolean LOG = false;
57
Daniel Sandler11ddf532011-11-16 11:10:03 -080058 // Enable launching of applications when entering the dock.
59 private static final boolean ENABLE_LAUNCH_CAR_DOCK_APP = true;
John Spurlock960779d2012-05-29 14:37:05 -040060 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080061
Jeff Brown62c82e42012-09-26 01:30:41 -070062 private static final int DEFAULT_SCREENSAVER_ENABLED = 1;
63 private static final int DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK = 1;
64
Dianne Hackborn7299c412010-03-04 18:41:49 -080065 private final Context mContext;
Jeff Brown2416e092012-08-21 22:12:20 -070066 private final TwilightService mTwilightService;
67 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080068
69 final Object mLock = new Object();
Bernd Holzheyba9ab182010-03-12 09:30:29 +010070
Dianne Hackborn7299c412010-03-04 18:41:49 -080071 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
72 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010073
Dianne Hackborn7299c412010-03-04 18:41:49 -080074 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
75 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050076 private boolean mCharging = false;
Joe Onorato44fcb832011-12-14 20:59:30 -080077 private final int mDefaultUiModeType;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050078 private final boolean mCarModeKeepsScreenOn;
79 private final boolean mDeskModeKeepsScreenOn;
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -070080 private final boolean mTelevision;
Dianne Hackborn7299c412010-03-04 18:41:49 -080081
82 private boolean mComputedNightMode;
83 private int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080084 private int mSetUiMode = 0;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010085
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080086 private boolean mHoldingConfiguration = false;
Dianne Hackborn7299c412010-03-04 18:41:49 -080087 private Configuration mConfiguration = new Configuration();
Bernd Holzheyba9ab182010-03-12 09:30:29 +010088
Dianne Hackborn7299c412010-03-04 18:41:49 -080089 private boolean mSystemReady;
90
91 private NotificationManager mNotificationManager;
92
Dianne Hackborn7299c412010-03-04 18:41:49 -080093 private StatusBarManager mStatusBarManager;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050094 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -080095
Dianne Hackbornf5c5d222010-04-09 13:14:48 -070096 static Intent buildHomeIntent(String category) {
97 Intent intent = new Intent(Intent.ACTION_MAIN);
98 intent.addCategory(category);
99 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
100 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
101 return intent;
102 }
John Spurlock960779d2012-05-29 14:37:05 -0400103
Dianne Hackborn7299c412010-03-04 18:41:49 -0800104 // The broadcast receiver which receives the result of the ordered broadcast sent when
105 // the dock state changes. The original ordered broadcast is sent with an initial result
106 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
107 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
108 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
109 @Override
110 public void onReceive(Context context, Intent intent) {
111 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400112 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400113 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400114 + ": canceled: " + getResultCode());
115 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800116 return;
117 }
118
Jeff Brown62c82e42012-09-26 01:30:41 -0700119 final int enableFlags = intent.getIntExtra("enableFlags", 0);
120 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800121 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700122 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800123 }
124 }
125 };
126
Dianne Hackborn7299c412010-03-04 18:41:49 -0800127 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
128 @Override
129 public void onReceive(Context context, Intent intent) {
130 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
131 Intent.EXTRA_DOCK_STATE_UNDOCKED);
132 updateDockState(state);
133 }
134 };
135
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500136 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
137 @Override
138 public void onReceive(Context context, Intent intent) {
139 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
140 synchronized (mLock) {
141 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700142 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500143 }
144 }
145 }
146 };
147
Jeff Brown2416e092012-08-21 22:12:20 -0700148 private final TwilightService.TwilightListener mTwilightListener =
149 new TwilightService.TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700150 @Override
Jeff Brown2416e092012-08-21 22:12:20 -0700151 public void onTwilightStateChanged() {
152 updateTwilight();
Dianne Hackborn57f45032010-06-17 15:49:33 -0700153 }
154 };
155
Jeff Brown2416e092012-08-21 22:12:20 -0700156 public UiModeManagerService(Context context, TwilightService twilight) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800157 mContext = context;
Jeff Brown2416e092012-08-21 22:12:20 -0700158 mTwilightService = twilight;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800159
160 ServiceManager.addService(Context.UI_MODE_SERVICE, this);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100161
Dianne Hackborn7299c412010-03-04 18:41:49 -0800162 mContext.registerReceiver(mDockModeReceiver,
163 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500164 mContext.registerReceiver(mBatteryReceiver,
165 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
166
167 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
168 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800169
170 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500171
Joe Onorato44fcb832011-12-14 20:59:30 -0800172 mDefaultUiModeType = context.getResources().getInteger(
173 com.android.internal.R.integer.config_defaultUiModeType);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500174 mCarModeKeepsScreenOn = (context.getResources().getInteger(
175 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
176 mDeskModeKeepsScreenOn = (context.getResources().getInteger(
177 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700178 mTelevision = context.getPackageManager().hasSystemFeature(
179 PackageManager.FEATURE_TELEVISION);
180
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700181 mNightMode = Settings.Secure.getInt(mContext.getContentResolver(),
182 Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
Jeff Brown2416e092012-08-21 22:12:20 -0700183
184 mTwilightService.registerListener(mTwilightListener, mHandler);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800185 }
186
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700187 public void disableCarMode(int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800188 synchronized (mLock) {
189 setCarModeLocked(false);
Mike Lockwood924e1642010-03-05 11:56:53 -0500190 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700191 updateLocked(0, flags);
Mike Lockwood924e1642010-03-05 11:56:53 -0500192 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800193 }
194 }
195
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700196 public void enableCarMode(int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800197 synchronized (mLock) {
198 setCarModeLocked(true);
Mike Lockwood924e1642010-03-05 11:56:53 -0500199 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700200 updateLocked(flags, 0);
Mike Lockwood924e1642010-03-05 11:56:53 -0500201 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800202 }
203 }
204
205 public int getCurrentModeType() {
206 synchronized (mLock) {
207 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
208 }
209 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100210
Dianne Hackborn7299c412010-03-04 18:41:49 -0800211 public void setNightMode(int mode) throws RemoteException {
212 synchronized (mLock) {
213 switch (mode) {
214 case UiModeManager.MODE_NIGHT_NO:
215 case UiModeManager.MODE_NIGHT_YES:
216 case UiModeManager.MODE_NIGHT_AUTO:
217 break;
218 default:
219 throw new IllegalArgumentException("Unknown mode: " + mode);
220 }
221 if (!isDoingNightMode()) {
222 return;
223 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100224
Dianne Hackborn7299c412010-03-04 18:41:49 -0800225 if (mNightMode != mode) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700226 long ident = Binder.clearCallingIdentity();
227 Settings.Secure.putInt(mContext.getContentResolver(),
228 Settings.Secure.UI_NIGHT_MODE, mode);
229 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800230 mNightMode = mode;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700231 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800232 }
233 }
234 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100235
Dianne Hackborn7299c412010-03-04 18:41:49 -0800236 public int getNightMode() throws RemoteException {
237 return mNightMode;
238 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100239
Dianne Hackborn7299c412010-03-04 18:41:49 -0800240 void systemReady() {
241 synchronized (mLock) {
242 mSystemReady = true;
243 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
Jeff Brown2416e092012-08-21 22:12:20 -0700244 updateComputedNightModeLocked();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700245 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800246 }
247 }
248
249 boolean isDoingNightMode() {
250 return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
251 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100252
Dianne Hackborn7299c412010-03-04 18:41:49 -0800253 void setCarModeLocked(boolean enabled) {
254 if (mCarModeEnabled != enabled) {
255 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800256 }
257 }
258
259 void updateDockState(int newState) {
260 synchronized (mLock) {
261 if (newState != mDockState) {
262 mDockState = newState;
Mike Lockwood924e1642010-03-05 11:56:53 -0500263 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800264 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700265 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800266 }
267 }
268 }
269 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500270
Daniel Sandler69a1da42011-11-04 15:08:30 -0400271 final static boolean isDeskDockState(int state) {
272 switch (state) {
273 case Intent.EXTRA_DOCK_STATE_DESK:
274 case Intent.EXTRA_DOCK_STATE_LE_DESK:
275 case Intent.EXTRA_DOCK_STATE_HE_DESK:
276 return true;
277 default:
278 return false;
279 }
280 }
281
Jeff Brown62c82e42012-09-26 01:30:41 -0700282 final void updateConfigurationLocked() {
283 int uiMode = mTelevision ? Configuration.UI_MODE_TYPE_TELEVISION : mDefaultUiModeType;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800284 if (mCarModeEnabled) {
285 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400286 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800287 uiMode = Configuration.UI_MODE_TYPE_DESK;
288 }
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700289 if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800290 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700291 updateComputedNightModeLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800292 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
293 : Configuration.UI_MODE_NIGHT_NO;
294 } else {
295 uiMode |= mNightMode << 4;
296 }
297 } else {
298 // Disabling the car mode clears the night mode.
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400299 uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO;
300 }
301
302 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400303 Slog.d(TAG,
304 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400305 + "; mCarMode=" + mCarModeEnabled
306 + "; mNightMode=" + mNightMode
307 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800308 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100309
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800310 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700311 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700312 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700313 }
314 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100315
Jeff Brown62c82e42012-09-26 01:30:41 -0700316 final void sendConfigurationLocked() {
317 if (mSetUiMode != mConfiguration.uiMode) {
318 mSetUiMode = mConfiguration.uiMode;
319
320 try {
321 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
322 } catch (RemoteException e) {
323 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800324 }
325 }
326 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100327
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700328 final void updateLocked(int enableFlags, int disableFlags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800329 long ident = Binder.clearCallingIdentity();
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100330
Dianne Hackborn7299c412010-03-04 18:41:49 -0800331 try {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800332 String action = null;
333 String oldAction = null;
334 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100335 adjustStatusBarCarModeLocked();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800336 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400337 } else if (isDeskDockState(mLastBroadcastState)) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800338 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
339 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100340
Dianne Hackborn7299c412010-03-04 18:41:49 -0800341 if (mCarModeEnabled) {
342 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
343 adjustStatusBarCarModeLocked();
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100344
Dianne Hackborn7299c412010-03-04 18:41:49 -0800345 if (oldAction != null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700346 mContext.sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800347 }
348 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
349 action = UiModeManager.ACTION_ENTER_CAR_MODE;
350 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400351 } else if (isDeskDockState(mDockState)) {
352 if (!isDeskDockState(mLastBroadcastState)) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800353 if (oldAction != null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700354 mContext.sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800355 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400356 mLastBroadcastState = mDockState;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800357 action = UiModeManager.ACTION_ENTER_DESK_MODE;
358 }
359 } else {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800360 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
361 action = oldAction;
362 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100363
Dianne Hackborn7299c412010-03-04 18:41:49 -0800364 if (action != null) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400365 if (LOG) {
366 Slog.v(TAG, String.format(
367 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
368 action, enableFlags, disableFlags));
369 }
370
Dianne Hackborn7299c412010-03-04 18:41:49 -0800371 // Send the ordered broadcast; the result receiver will receive after all
372 // broadcasts have been sent. If any broadcast receiver changes the result
373 // code from the initial value of RESULT_OK, then the result receiver will
374 // not launch the corresponding dock application. This gives apps a chance
375 // to override the behavior and stay in their app even when the device is
376 // placed into a dock.
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700377 Intent intent = new Intent(action);
378 intent.putExtra("enableFlags", enableFlags);
379 intent.putExtra("disableFlags", disableFlags);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700380 mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800381 mResultReceiver, null, Activity.RESULT_OK, null, null);
Jeff Brown62c82e42012-09-26 01:30:41 -0700382
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800383 // Attempting to make this transition a little more clean, we are going
384 // to hold off on doing a configuration change until we have finished
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700385 // the broadcast and started the home activity.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800386 mHoldingConfiguration = true;
Jeff Brown62c82e42012-09-26 01:30:41 -0700387 updateConfigurationLocked();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700388 } else {
Jeff Brown62c82e42012-09-26 01:30:41 -0700389 String category = null;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700390 if (mCarModeEnabled) {
Daniel Sandler11ddf532011-11-16 11:10:03 -0800391 if (ENABLE_LAUNCH_CAR_DOCK_APP
Jeff Brown62c82e42012-09-26 01:30:41 -0700392 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
393 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700394 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400395 } else if (isDeskDockState(mDockState)) {
Daniel Sandler11ddf532011-11-16 11:10:03 -0800396 if (ENABLE_LAUNCH_DESK_DOCK_APP
Jeff Brown62c82e42012-09-26 01:30:41 -0700397 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
398 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700399 }
400 } else {
Jeff Brown62c82e42012-09-26 01:30:41 -0700401 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
402 category = Intent.CATEGORY_HOME;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700403 }
404 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400405
406 if (LOG) {
407 Slog.v(TAG, "updateLocked: null action, mDockState="
Jeff Brown62c82e42012-09-26 01:30:41 -0700408 + mDockState +", category=" + category);
Daniel Sandler69a1da42011-11-04 15:08:30 -0400409 }
410
Jeff Brown62c82e42012-09-26 01:30:41 -0700411 sendConfigurationAndStartDreamOrDockAppLocked(category);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800412 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100413
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500414 // keep screen on when charging and in car mode
415 boolean keepScreenOn = mCharging &&
416 ((mCarModeEnabled && mCarModeKeepsScreenOn) ||
417 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
418 if (keepScreenOn != mWakeLock.isHeld()) {
419 if (keepScreenOn) {
420 mWakeLock.acquire();
421 } else {
422 mWakeLock.release();
423 }
424 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800425 } finally {
426 Binder.restoreCallingIdentity(ident);
427 }
428 }
429
Jeff Brown62c82e42012-09-26 01:30:41 -0700430 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
431 // Launch a dock activity
432 String category = null;
433 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
434 // Only launch car home when car mode is enabled and the caller
435 // has asked us to switch to it.
436 if (ENABLE_LAUNCH_CAR_DOCK_APP
437 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
438 category = Intent.CATEGORY_CAR_DOCK;
439 }
440 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
441 // Only launch car home when desk mode is enabled and the caller
442 // has asked us to switch to it. Currently re-using the car
443 // mode flag since we don't have a formal API for "desk mode".
444 if (ENABLE_LAUNCH_DESK_DOCK_APP
445 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
446 category = Intent.CATEGORY_DESK_DOCK;
447 }
448 } else {
449 // Launch the standard home app if requested.
450 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
451 category = Intent.CATEGORY_HOME;
452 }
453 }
454
455 if (LOG) {
456 Slog.v(TAG, String.format(
457 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
458 + "category=%s",
459 action, enableFlags, disableFlags, category));
460 }
461
462 sendConfigurationAndStartDreamOrDockAppLocked(category);
463 }
464
465 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
466 // Update the configuration but don't send it yet.
467 mHoldingConfiguration = false;
468 updateConfigurationLocked();
469
470 // Start the dock app, if there is one.
471 boolean dockAppStarted = false;
472 if (category != null) {
473 // Now we are going to be careful about switching the
474 // configuration and starting the activity -- we need to
475 // do this in a specific order under control of the
476 // activity manager, to do it cleanly. So compute the
477 // new config, but don't set it yet, and let the
478 // activity manager take care of both the start and config
479 // change.
480 Intent homeIntent = buildHomeIntent(category);
481 try {
482 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
483 null, homeIntent, null, null, null, 0, 0,
484 mConfiguration, null, UserHandle.USER_CURRENT);
485 if (result >= ActivityManager.START_SUCCESS) {
486 dockAppStarted = true;
487 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
488 Slog.e(TAG, "Could not start dock app: " + homeIntent
489 + ", startActivityWithConfig result " + result);
490 }
491 } catch (RemoteException ex) {
492 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
493 }
494 }
495
496 // Send the new configuration.
497 sendConfigurationLocked();
498
499 // If we did not start a dock app, then start dreaming if supported.
500 if (!dockAppStarted && isScreenSaverEnabled() && isScreenSaverActivatedOnDock()) {
501 Slog.i(TAG, "Activating dream while docked.");
502 try {
503 IDreamManager dreamManagerService = IDreamManager.Stub.asInterface(
504 ServiceManager.getService(Dream.DREAM_SERVICE));
505 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}