blob: 94f699fccd651a1141f4e4d4423eee4e73809229 [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(
Tim Kilbourn07229682014-03-14 13:44:12 -0700176 PackageManager.FEATURE_TELEVISION) ||
177 context.getPackageManager().hasSystemFeature(
178 PackageManager.FEATURE_LEANBACK);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700179
Adam Lesinski182f73f2013-12-05 16:48:06 -0800180 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700181 Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
Jeff Brown2416e092012-08-21 22:12:20 -0700182
Adam Lesinski182f73f2013-12-05 16:48:06 -0800183 mTwilightManager.registerListener(mTwilightListener, mHandler);
184
185 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800186 }
187
Adam Lesinski182f73f2013-12-05 16:48:06 -0800188 private final IBinder mService = new IUiModeManager.Stub() {
189 @Override
190 public void enableCarMode(int flags) {
191 final long ident = Binder.clearCallingIdentity();
192 try {
193 synchronized (mLock) {
194 setCarModeLocked(true);
195 if (mSystemReady) {
196 updateLocked(flags, 0);
197 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700198 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800199 } finally {
200 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500201 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800202 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800203
Adam Lesinski182f73f2013-12-05 16:48:06 -0800204 @Override
205 public void disableCarMode(int flags) {
206 final long ident = Binder.clearCallingIdentity();
207 try {
208 synchronized (mLock) {
209 setCarModeLocked(false);
210 if (mSystemReady) {
211 updateLocked(0, flags);
212 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700213 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800214 } finally {
215 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500216 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700217 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100218
Adam Lesinski182f73f2013-12-05 16:48:06 -0800219 @Override
220 public int getCurrentModeType() {
221 final long ident = Binder.clearCallingIdentity();
222 try {
223 synchronized (mLock) {
224 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700225 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800226 } finally {
227 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800228 }
229 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100230
Adam Lesinski182f73f2013-12-05 16:48:06 -0800231 @Override
232 public void setNightMode(int mode) {
233 switch (mode) {
234 case UiModeManager.MODE_NIGHT_NO:
235 case UiModeManager.MODE_NIGHT_YES:
236 case UiModeManager.MODE_NIGHT_AUTO:
237 break;
238 default:
239 throw new IllegalArgumentException("Unknown mode: " + mode);
240 }
241
242 final long ident = Binder.clearCallingIdentity();
243 try {
244 synchronized (mLock) {
245 if (isDoingNightModeLocked() && mNightMode != mode) {
246 Settings.Secure.putInt(getContext().getContentResolver(),
247 Settings.Secure.UI_NIGHT_MODE, mode);
248 mNightMode = mode;
249 updateLocked(0, 0);
250 }
251 }
252 } finally {
253 Binder.restoreCallingIdentity(ident);
254 }
255 }
256
257 @Override
258 public int getNightMode() {
259 synchronized (mLock) {
260 return mNightMode;
261 }
262 }
263
264 @Override
265 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
266 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
267 != PackageManager.PERMISSION_GRANTED) {
268
269 pw.println("Permission Denial: can't dump uimode service from from pid="
270 + Binder.getCallingPid()
271 + ", uid=" + Binder.getCallingUid());
272 return;
273 }
274
275 dumpImpl(pw);
276 }
277 };
278
279 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700280 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800281 pw.println("Current UI Mode Service state:");
282 pw.print(" mDockState="); pw.print(mDockState);
283 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
284 pw.print(" mNightMode="); pw.print(mNightMode);
285 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
286 pw.print(" mComputedNightMode="); pw.println(mComputedNightMode);
287 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
288 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
289 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
290 pw.print(" mSystemReady="); pw.println(mSystemReady);
291 pw.print(" mTwilightService.getCurrentState()=");
292 pw.println(mTwilightManager.getCurrentState());
Jeff Brown487bb6e2012-10-11 13:35:42 -0700293 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800294 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100295
Adam Lesinski182f73f2013-12-05 16:48:06 -0800296 @Override
297 public void onBootPhase(int phase) {
298 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
299 synchronized (mLock) {
300 mSystemReady = true;
301 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
302 updateComputedNightModeLocked();
303 updateLocked(0, 0);
304 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800305 }
306 }
307
Adam Lesinski182f73f2013-12-05 16:48:06 -0800308 boolean isDoingNightModeLocked() {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800309 return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
310 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100311
Adam Lesinski182f73f2013-12-05 16:48:06 -0800312 void setCarModeLocked(boolean enabled) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800313 if (mCarModeEnabled != enabled) {
314 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800315 }
316 }
317
Jeff Brown487bb6e2012-10-11 13:35:42 -0700318 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800319 synchronized (mLock) {
320 if (newState != mDockState) {
321 mDockState = newState;
Mike Lockwood924e1642010-03-05 11:56:53 -0500322 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800323 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700324 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800325 }
326 }
327 }
328 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500329
Jeff Brown487bb6e2012-10-11 13:35:42 -0700330 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400331 switch (state) {
332 case Intent.EXTRA_DOCK_STATE_DESK:
333 case Intent.EXTRA_DOCK_STATE_LE_DESK:
334 case Intent.EXTRA_DOCK_STATE_HE_DESK:
335 return true;
336 default:
337 return false;
338 }
339 }
340
Jeff Brown487bb6e2012-10-11 13:35:42 -0700341 private void updateConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700342 int uiMode = mTelevision ? Configuration.UI_MODE_TYPE_TELEVISION : mDefaultUiModeType;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800343 if (mCarModeEnabled) {
344 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400345 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800346 uiMode = Configuration.UI_MODE_TYPE_DESK;
347 }
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700348 if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800349 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700350 updateComputedNightModeLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800351 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
352 : Configuration.UI_MODE_NIGHT_NO;
353 } else {
354 uiMode |= mNightMode << 4;
355 }
356 } else {
357 // Disabling the car mode clears the night mode.
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400358 uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO;
359 }
360
361 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400362 Slog.d(TAG,
363 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400364 + "; mCarMode=" + mCarModeEnabled
365 + "; mNightMode=" + mNightMode
366 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800367 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100368
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800369 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700370 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700371 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700372 }
373 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100374
Jeff Brown487bb6e2012-10-11 13:35:42 -0700375 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700376 if (mSetUiMode != mConfiguration.uiMode) {
377 mSetUiMode = mConfiguration.uiMode;
378
379 try {
380 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
381 } catch (RemoteException e) {
382 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800383 }
384 }
385 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100386
Adam Lesinski182f73f2013-12-05 16:48:06 -0800387 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700388 String action = null;
389 String oldAction = null;
390 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
391 adjustStatusBarCarModeLocked();
392 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
393 } else if (isDeskDockState(mLastBroadcastState)) {
394 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
395 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100396
Jeff Brown487bb6e2012-10-11 13:35:42 -0700397 if (mCarModeEnabled) {
398 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100399 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700400
401 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800402 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700403 }
404 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
405 action = UiModeManager.ACTION_ENTER_CAR_MODE;
406 }
407 } else if (isDeskDockState(mDockState)) {
408 if (!isDeskDockState(mLastBroadcastState)) {
409 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800410 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700411 }
412 mLastBroadcastState = mDockState;
413 action = UiModeManager.ACTION_ENTER_DESK_MODE;
414 }
415 } else {
416 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
417 action = oldAction;
418 }
419
420 if (action != null) {
421 if (LOG) {
422 Slog.v(TAG, String.format(
423 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
424 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800425 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100426
Jeff Brown487bb6e2012-10-11 13:35:42 -0700427 // Send the ordered broadcast; the result receiver will receive after all
428 // broadcasts have been sent. If any broadcast receiver changes the result
429 // code from the initial value of RESULT_OK, then the result receiver will
430 // not launch the corresponding dock application. This gives apps a chance
431 // to override the behavior and stay in their app even when the device is
432 // placed into a dock.
433 Intent intent = new Intent(action);
434 intent.putExtra("enableFlags", enableFlags);
435 intent.putExtra("disableFlags", disableFlags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800436 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700437 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100438
Jeff Brown487bb6e2012-10-11 13:35:42 -0700439 // Attempting to make this transition a little more clean, we are going
440 // to hold off on doing a configuration change until we have finished
441 // the broadcast and started the home activity.
442 mHoldingConfiguration = true;
443 updateConfigurationLocked();
444 } else {
445 String category = null;
446 if (mCarModeEnabled) {
447 if (ENABLE_LAUNCH_CAR_DOCK_APP
448 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
449 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800450 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400451 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700452 if (ENABLE_LAUNCH_DESK_DOCK_APP
453 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
454 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800455 }
456 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700457 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
458 category = Intent.CATEGORY_HOME;
459 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800460 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100461
Jeff Brown487bb6e2012-10-11 13:35:42 -0700462 if (LOG) {
463 Slog.v(TAG, "updateLocked: null action, mDockState="
464 + mDockState +", category=" + category);
465 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400466
Jeff Brown487bb6e2012-10-11 13:35:42 -0700467 sendConfigurationAndStartDreamOrDockAppLocked(category);
468 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700469
Jeff Brown487bb6e2012-10-11 13:35:42 -0700470 // keep screen on when charging and in car mode
471 boolean keepScreenOn = mCharging &&
472 ((mCarModeEnabled && mCarModeKeepsScreenOn) ||
473 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
474 if (keepScreenOn != mWakeLock.isHeld()) {
475 if (keepScreenOn) {
476 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700477 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700478 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800479 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800480 }
481 }
482
Jeff Brown62c82e42012-09-26 01:30:41 -0700483 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
484 // Launch a dock activity
485 String category = null;
486 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
487 // Only launch car home when car mode is enabled and the caller
488 // has asked us to switch to it.
489 if (ENABLE_LAUNCH_CAR_DOCK_APP
490 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
491 category = Intent.CATEGORY_CAR_DOCK;
492 }
493 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
494 // Only launch car home when desk mode is enabled and the caller
495 // has asked us to switch to it. Currently re-using the car
496 // mode flag since we don't have a formal API for "desk mode".
497 if (ENABLE_LAUNCH_DESK_DOCK_APP
498 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
499 category = Intent.CATEGORY_DESK_DOCK;
500 }
501 } else {
502 // Launch the standard home app if requested.
503 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
504 category = Intent.CATEGORY_HOME;
505 }
506 }
507
508 if (LOG) {
509 Slog.v(TAG, String.format(
510 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
511 + "category=%s",
512 action, enableFlags, disableFlags, category));
513 }
514
515 sendConfigurationAndStartDreamOrDockAppLocked(category);
516 }
517
518 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
519 // Update the configuration but don't send it yet.
520 mHoldingConfiguration = false;
521 updateConfigurationLocked();
522
523 // Start the dock app, if there is one.
524 boolean dockAppStarted = false;
525 if (category != null) {
526 // Now we are going to be careful about switching the
527 // configuration and starting the activity -- we need to
528 // do this in a specific order under control of the
529 // activity manager, to do it cleanly. So compute the
530 // new config, but don't set it yet, and let the
531 // activity manager take care of both the start and config
532 // change.
533 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800534 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700535 try {
536 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800537 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700538 mConfiguration, null, UserHandle.USER_CURRENT);
539 if (result >= ActivityManager.START_SUCCESS) {
540 dockAppStarted = true;
541 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
542 Slog.e(TAG, "Could not start dock app: " + homeIntent
543 + ", startActivityWithConfig result " + result);
544 }
545 } catch (RemoteException ex) {
546 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700547 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700548 }
549 }
550
551 // Send the new configuration.
552 sendConfigurationLocked();
553
554 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700555 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800556 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700557 }
558 }
559
Dianne Hackborn7299c412010-03-04 18:41:49 -0800560 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800561 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800562 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700563 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800564 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800565 }
566
Joe Onorato089de882010-04-12 08:18:45 -0700567 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800568 // features of the status bar; these are ORed together to form the
569 // active disabled list. So if (for example) the device is locked and
570 // the status bar should be totally disabled, the calls below will
571 // have no effect until the device is unlocked.
572 if (mStatusBarManager != null) {
573 mStatusBarManager.disable(mCarModeEnabled
574 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
575 : StatusBarManager.DISABLE_NONE);
576 }
577
578 if (mNotificationManager == null) {
579 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800580 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800581 }
582
583 if (mNotificationManager != null) {
584 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800585 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800586
587 Notification n = new Notification();
588 n.icon = R.drawable.stat_notify_car_mode;
589 n.defaults = Notification.DEFAULT_LIGHTS;
590 n.flags = Notification.FLAG_ONGOING_EVENT;
591 n.when = 0;
592 n.setLatestEventInfo(
Adam Lesinski182f73f2013-12-05 16:48:06 -0800593 context,
594 context.getString(R.string.car_mode_disable_notification_title),
595 context.getString(R.string.car_mode_disable_notification_message),
596 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700597 null, UserHandle.CURRENT));
598 mNotificationManager.notifyAsUser(null,
599 R.string.car_mode_disable_notification_title, n, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800600 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700601 mNotificationManager.cancelAsUser(null,
602 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800603 }
604 }
605 }
606
Adam Lesinski182f73f2013-12-05 16:48:06 -0800607 void updateTwilight() {
Jeff Brown2416e092012-08-21 22:12:20 -0700608 synchronized (mLock) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700609 if (isDoingNightModeLocked() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700610 updateComputedNightModeLocked();
611 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800612 }
613 }
Jeff Brown2416e092012-08-21 22:12:20 -0700614 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800615
Jeff Brown2416e092012-08-21 22:12:20 -0700616 private void updateComputedNightModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800617 TwilightState state = mTwilightManager.getCurrentState();
Jeff Brown2416e092012-08-21 22:12:20 -0700618 if (state != null) {
619 mComputedNightMode = state.isNight();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800620 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800621 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100622
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100623
Dianne Hackborn7299c412010-03-04 18:41:49 -0800624}