blob: ad693d00248d49136af191689607e401cdef40ce [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;
Adam Lesinski182f73f2013-12-05 16:48:06 -080037import android.os.IBinder;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050038import android.os.PowerManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080039import android.os.RemoteException;
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;
Adam Lesinski182f73f2013-12-05 16:48:06 -080050import com.android.server.twilight.TwilightListener;
51import com.android.server.twilight.TwilightManager;
52import com.android.server.twilight.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080053
Adam Lesinski182f73f2013-12-05 16:48:06 -080054final class UiModeManagerService extends SystemService {
Dianne Hackborn7299c412010-03-04 18:41:49 -080055 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
Dianne Hackborn7299c412010-03-04 18:41:49 -080062 final Object mLock = new Object();
Dianne Hackborn7299c412010-03-04 18:41:49 -080063 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010064
Adam Lesinski182f73f2013-12-05 16:48:06 -080065 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
66 int mNightMode = UiModeManager.MODE_NIGHT_NO;
67
Dianne Hackborn7299c412010-03-04 18:41:49 -080068 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050069 private boolean mCharging = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080070 private int mDefaultUiModeType;
71 private boolean mCarModeKeepsScreenOn;
72 private boolean mDeskModeKeepsScreenOn;
73 private boolean mTelevision;
Dianne Hackborn7299c412010-03-04 18:41:49 -080074 private boolean mComputedNightMode;
Adam Lesinski182f73f2013-12-05 16:48:06 -080075
76 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080077 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080078 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080079
Dianne Hackborn7299c412010-03-04 18:41:49 -080080 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -080081 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010082
Adam Lesinski182f73f2013-12-05 16:48:06 -080083 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080084
Adam Lesinski182f73f2013-12-05 16:48:06 -080085 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080086 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080087 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -070088
Adam Lesinski182f73f2013-12-05 16:48:06 -080089 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -080090
Jeff Brownb880d882014-02-10 19:47:07 -080091 public UiModeManagerService(Context context) {
92 super(context);
93 }
94
Adam Lesinski182f73f2013-12-05 16:48:06 -080095 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -070096 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
Adam Lesinski182f73f2013-12-05 16:48:06 -0800147 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700148 @Override
Jeff Brown2416e092012-08-21 22:12:20 -0700149 public void onTwilightStateChanged() {
150 updateTwilight();
Dianne Hackborn57f45032010-06-17 15:49:33 -0700151 }
152 };
153
Adam Lesinski182f73f2013-12-05 16:48:06 -0800154 @Override
155 public void onStart() {
156 final Context context = getContext();
157 mTwilightManager = getLocalService(TwilightManager.class);
158 final PowerManager powerManager =
159 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
160 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800161
Adam Lesinski182f73f2013-12-05 16:48:06 -0800162 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800163 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800164 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500165 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
166
Dianne Hackborn7299c412010-03-04 18:41:49 -0800167 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
Adam Lesinski182f73f2013-12-05 16:48:06 -0800178 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700179 Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
Jeff Brown2416e092012-08-21 22:12:20 -0700180
Adam Lesinski182f73f2013-12-05 16:48:06 -0800181 mTwilightManager.registerListener(mTwilightListener, mHandler);
182
183 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800184 }
185
Adam Lesinski182f73f2013-12-05 16:48:06 -0800186 private final IBinder mService = new IUiModeManager.Stub() {
187 @Override
188 public void enableCarMode(int flags) {
189 final long ident = Binder.clearCallingIdentity();
190 try {
191 synchronized (mLock) {
192 setCarModeLocked(true);
193 if (mSystemReady) {
194 updateLocked(flags, 0);
195 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700196 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800197 } finally {
198 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500199 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800200 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800201
Adam Lesinski182f73f2013-12-05 16:48:06 -0800202 @Override
203 public void disableCarMode(int flags) {
204 final long ident = Binder.clearCallingIdentity();
205 try {
206 synchronized (mLock) {
207 setCarModeLocked(false);
208 if (mSystemReady) {
209 updateLocked(0, flags);
210 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700211 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800212 } finally {
213 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500214 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700215 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100216
Adam Lesinski182f73f2013-12-05 16:48:06 -0800217 @Override
218 public int getCurrentModeType() {
219 final long ident = Binder.clearCallingIdentity();
220 try {
221 synchronized (mLock) {
222 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700223 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800224 } finally {
225 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800226 }
227 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100228
Adam Lesinski182f73f2013-12-05 16:48:06 -0800229 @Override
230 public void setNightMode(int mode) {
231 switch (mode) {
232 case UiModeManager.MODE_NIGHT_NO:
233 case UiModeManager.MODE_NIGHT_YES:
234 case UiModeManager.MODE_NIGHT_AUTO:
235 break;
236 default:
237 throw new IllegalArgumentException("Unknown mode: " + mode);
238 }
239
240 final long ident = Binder.clearCallingIdentity();
241 try {
242 synchronized (mLock) {
243 if (isDoingNightModeLocked() && mNightMode != mode) {
244 Settings.Secure.putInt(getContext().getContentResolver(),
245 Settings.Secure.UI_NIGHT_MODE, mode);
246 mNightMode = mode;
247 updateLocked(0, 0);
248 }
249 }
250 } finally {
251 Binder.restoreCallingIdentity(ident);
252 }
253 }
254
255 @Override
256 public int getNightMode() {
257 synchronized (mLock) {
258 return mNightMode;
259 }
260 }
261
262 @Override
263 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
264 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
265 != PackageManager.PERMISSION_GRANTED) {
266
267 pw.println("Permission Denial: can't dump uimode service from from pid="
268 + Binder.getCallingPid()
269 + ", uid=" + Binder.getCallingUid());
270 return;
271 }
272
273 dumpImpl(pw);
274 }
275 };
276
277 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700278 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800279 pw.println("Current UI Mode Service state:");
280 pw.print(" mDockState="); pw.print(mDockState);
281 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
282 pw.print(" mNightMode="); pw.print(mNightMode);
283 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
284 pw.print(" mComputedNightMode="); pw.println(mComputedNightMode);
285 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
286 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
287 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
288 pw.print(" mSystemReady="); pw.println(mSystemReady);
289 pw.print(" mTwilightService.getCurrentState()=");
290 pw.println(mTwilightManager.getCurrentState());
Jeff Brown487bb6e2012-10-11 13:35:42 -0700291 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800292 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100293
Adam Lesinski182f73f2013-12-05 16:48:06 -0800294 @Override
295 public void onBootPhase(int phase) {
296 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
297 synchronized (mLock) {
298 mSystemReady = true;
299 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
300 updateComputedNightModeLocked();
301 updateLocked(0, 0);
302 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800303 }
304 }
305
Adam Lesinski182f73f2013-12-05 16:48:06 -0800306 boolean isDoingNightModeLocked() {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800307 return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
308 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100309
Adam Lesinski182f73f2013-12-05 16:48:06 -0800310 void setCarModeLocked(boolean enabled) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800311 if (mCarModeEnabled != enabled) {
312 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800313 }
314 }
315
Jeff Brown487bb6e2012-10-11 13:35:42 -0700316 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800317 synchronized (mLock) {
318 if (newState != mDockState) {
319 mDockState = newState;
Mike Lockwood924e1642010-03-05 11:56:53 -0500320 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800321 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700322 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800323 }
324 }
325 }
326 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500327
Jeff Brown487bb6e2012-10-11 13:35:42 -0700328 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400329 switch (state) {
330 case Intent.EXTRA_DOCK_STATE_DESK:
331 case Intent.EXTRA_DOCK_STATE_LE_DESK:
332 case Intent.EXTRA_DOCK_STATE_HE_DESK:
333 return true;
334 default:
335 return false;
336 }
337 }
338
Jeff Brown487bb6e2012-10-11 13:35:42 -0700339 private void updateConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700340 int uiMode = mTelevision ? Configuration.UI_MODE_TYPE_TELEVISION : mDefaultUiModeType;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800341 if (mCarModeEnabled) {
342 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400343 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800344 uiMode = Configuration.UI_MODE_TYPE_DESK;
345 }
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700346 if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800347 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700348 updateComputedNightModeLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800349 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
350 : Configuration.UI_MODE_NIGHT_NO;
351 } else {
352 uiMode |= mNightMode << 4;
353 }
354 } else {
355 // Disabling the car mode clears the night mode.
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400356 uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO;
357 }
358
359 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400360 Slog.d(TAG,
361 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400362 + "; mCarMode=" + mCarModeEnabled
363 + "; mNightMode=" + mNightMode
364 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800365 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100366
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800367 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700368 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700369 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700370 }
371 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100372
Jeff Brown487bb6e2012-10-11 13:35:42 -0700373 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700374 if (mSetUiMode != mConfiguration.uiMode) {
375 mSetUiMode = mConfiguration.uiMode;
376
377 try {
378 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
379 } catch (RemoteException e) {
380 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800381 }
382 }
383 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100384
Adam Lesinski182f73f2013-12-05 16:48:06 -0800385 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700386 String action = null;
387 String oldAction = null;
388 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
389 adjustStatusBarCarModeLocked();
390 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
391 } else if (isDeskDockState(mLastBroadcastState)) {
392 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
393 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100394
Jeff Brown487bb6e2012-10-11 13:35:42 -0700395 if (mCarModeEnabled) {
396 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100397 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700398
399 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800400 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700401 }
402 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
403 action = UiModeManager.ACTION_ENTER_CAR_MODE;
404 }
405 } else if (isDeskDockState(mDockState)) {
406 if (!isDeskDockState(mLastBroadcastState)) {
407 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800408 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700409 }
410 mLastBroadcastState = mDockState;
411 action = UiModeManager.ACTION_ENTER_DESK_MODE;
412 }
413 } else {
414 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
415 action = oldAction;
416 }
417
418 if (action != null) {
419 if (LOG) {
420 Slog.v(TAG, String.format(
421 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
422 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800423 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100424
Jeff Brown487bb6e2012-10-11 13:35:42 -0700425 // Send the ordered broadcast; the result receiver will receive after all
426 // broadcasts have been sent. If any broadcast receiver changes the result
427 // code from the initial value of RESULT_OK, then the result receiver will
428 // not launch the corresponding dock application. This gives apps a chance
429 // to override the behavior and stay in their app even when the device is
430 // placed into a dock.
431 Intent intent = new Intent(action);
432 intent.putExtra("enableFlags", enableFlags);
433 intent.putExtra("disableFlags", disableFlags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800434 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700435 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100436
Jeff Brown487bb6e2012-10-11 13:35:42 -0700437 // Attempting to make this transition a little more clean, we are going
438 // to hold off on doing a configuration change until we have finished
439 // the broadcast and started the home activity.
440 mHoldingConfiguration = true;
441 updateConfigurationLocked();
442 } else {
443 String category = null;
444 if (mCarModeEnabled) {
445 if (ENABLE_LAUNCH_CAR_DOCK_APP
446 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
447 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800448 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400449 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700450 if (ENABLE_LAUNCH_DESK_DOCK_APP
451 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
452 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800453 }
454 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700455 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
456 category = Intent.CATEGORY_HOME;
457 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800458 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100459
Jeff Brown487bb6e2012-10-11 13:35:42 -0700460 if (LOG) {
461 Slog.v(TAG, "updateLocked: null action, mDockState="
462 + mDockState +", category=" + category);
463 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400464
Jeff Brown487bb6e2012-10-11 13:35:42 -0700465 sendConfigurationAndStartDreamOrDockAppLocked(category);
466 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700467
Jeff Brown487bb6e2012-10-11 13:35:42 -0700468 // keep screen on when charging and in car mode
469 boolean keepScreenOn = mCharging &&
470 ((mCarModeEnabled && mCarModeKeepsScreenOn) ||
471 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
472 if (keepScreenOn != mWakeLock.isHeld()) {
473 if (keepScreenOn) {
474 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700475 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700476 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800477 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800478 }
479 }
480
Jeff Brown62c82e42012-09-26 01:30:41 -0700481 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
482 // Launch a dock activity
483 String category = null;
484 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
485 // Only launch car home when car mode is enabled and the caller
486 // has asked us to switch to it.
487 if (ENABLE_LAUNCH_CAR_DOCK_APP
488 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
489 category = Intent.CATEGORY_CAR_DOCK;
490 }
491 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
492 // Only launch car home when desk mode is enabled and the caller
493 // has asked us to switch to it. Currently re-using the car
494 // mode flag since we don't have a formal API for "desk mode".
495 if (ENABLE_LAUNCH_DESK_DOCK_APP
496 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
497 category = Intent.CATEGORY_DESK_DOCK;
498 }
499 } else {
500 // Launch the standard home app if requested.
501 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
502 category = Intent.CATEGORY_HOME;
503 }
504 }
505
506 if (LOG) {
507 Slog.v(TAG, String.format(
508 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
509 + "category=%s",
510 action, enableFlags, disableFlags, category));
511 }
512
513 sendConfigurationAndStartDreamOrDockAppLocked(category);
514 }
515
516 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
517 // Update the configuration but don't send it yet.
518 mHoldingConfiguration = false;
519 updateConfigurationLocked();
520
521 // Start the dock app, if there is one.
522 boolean dockAppStarted = false;
523 if (category != null) {
524 // Now we are going to be careful about switching the
525 // configuration and starting the activity -- we need to
526 // do this in a specific order under control of the
527 // activity manager, to do it cleanly. So compute the
528 // new config, but don't set it yet, and let the
529 // activity manager take care of both the start and config
530 // change.
531 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800532 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700533 try {
534 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800535 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700536 mConfiguration, null, UserHandle.USER_CURRENT);
537 if (result >= ActivityManager.START_SUCCESS) {
538 dockAppStarted = true;
539 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
540 Slog.e(TAG, "Could not start dock app: " + homeIntent
541 + ", startActivityWithConfig result " + result);
542 }
543 } catch (RemoteException ex) {
544 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700545 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700546 }
547 }
548
549 // Send the new configuration.
550 sendConfigurationLocked();
551
552 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700553 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800554 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700555 }
556 }
557
Dianne Hackborn7299c412010-03-04 18:41:49 -0800558 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800559 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800560 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700561 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800562 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800563 }
564
Joe Onorato089de882010-04-12 08:18:45 -0700565 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800566 // features of the status bar; these are ORed together to form the
567 // active disabled list. So if (for example) the device is locked and
568 // the status bar should be totally disabled, the calls below will
569 // have no effect until the device is unlocked.
570 if (mStatusBarManager != null) {
571 mStatusBarManager.disable(mCarModeEnabled
572 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
573 : StatusBarManager.DISABLE_NONE);
574 }
575
576 if (mNotificationManager == null) {
577 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800578 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800579 }
580
581 if (mNotificationManager != null) {
582 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800583 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800584
585 Notification n = new Notification();
586 n.icon = R.drawable.stat_notify_car_mode;
587 n.defaults = Notification.DEFAULT_LIGHTS;
588 n.flags = Notification.FLAG_ONGOING_EVENT;
589 n.when = 0;
590 n.setLatestEventInfo(
Adam Lesinski182f73f2013-12-05 16:48:06 -0800591 context,
592 context.getString(R.string.car_mode_disable_notification_title),
593 context.getString(R.string.car_mode_disable_notification_message),
594 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700595 null, UserHandle.CURRENT));
596 mNotificationManager.notifyAsUser(null,
597 R.string.car_mode_disable_notification_title, n, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800598 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700599 mNotificationManager.cancelAsUser(null,
600 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800601 }
602 }
603 }
604
Adam Lesinski182f73f2013-12-05 16:48:06 -0800605 void updateTwilight() {
Jeff Brown2416e092012-08-21 22:12:20 -0700606 synchronized (mLock) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700607 if (isDoingNightModeLocked() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700608 updateComputedNightModeLocked();
609 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800610 }
611 }
Jeff Brown2416e092012-08-21 22:12:20 -0700612 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800613
Jeff Brown2416e092012-08-21 22:12:20 -0700614 private void updateComputedNightModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800615 TwilightState state = mTwilightManager.getCurrentState();
Jeff Brown2416e092012-08-21 22:12:20 -0700616 if (state != null) {
617 mComputedNightMode = state.isNight();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800618 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800619 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100620
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100621
Dianne Hackborn7299c412010-03-04 18:41:49 -0800622}