blob: de912dcc13320db1cc80af465b2f0d3d4c63d234 [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
Adam Lesinski182f73f2013-12-05 16:48:06 -080091 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -070092 Intent intent = new Intent(Intent.ACTION_MAIN);
93 intent.addCategory(category);
94 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
95 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
96 return intent;
97 }
John Spurlock960779d2012-05-29 14:37:05 -040098
Dianne Hackborn7299c412010-03-04 18:41:49 -080099 // The broadcast receiver which receives the result of the ordered broadcast sent when
100 // the dock state changes. The original ordered broadcast is sent with an initial result
101 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
102 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
103 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
104 @Override
105 public void onReceive(Context context, Intent intent) {
106 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400107 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400108 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400109 + ": canceled: " + getResultCode());
110 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800111 return;
112 }
113
Jeff Brown62c82e42012-09-26 01:30:41 -0700114 final int enableFlags = intent.getIntExtra("enableFlags", 0);
115 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800116 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700117 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800118 }
119 }
120 };
121
Dianne Hackborn7299c412010-03-04 18:41:49 -0800122 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
123 @Override
124 public void onReceive(Context context, Intent intent) {
125 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
126 Intent.EXTRA_DOCK_STATE_UNDOCKED);
127 updateDockState(state);
128 }
129 };
130
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500131 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
132 @Override
133 public void onReceive(Context context, Intent intent) {
134 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
135 synchronized (mLock) {
136 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700137 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500138 }
139 }
140 }
141 };
142
Adam Lesinski182f73f2013-12-05 16:48:06 -0800143 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700144 @Override
Jeff Brown2416e092012-08-21 22:12:20 -0700145 public void onTwilightStateChanged() {
146 updateTwilight();
Dianne Hackborn57f45032010-06-17 15:49:33 -0700147 }
148 };
149
Adam Lesinski182f73f2013-12-05 16:48:06 -0800150 @Override
151 public void onStart() {
152 final Context context = getContext();
153 mTwilightManager = getLocalService(TwilightManager.class);
154 final PowerManager powerManager =
155 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
156 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800157
Adam Lesinski182f73f2013-12-05 16:48:06 -0800158 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800159 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800160 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500161 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
162
Dianne Hackborn7299c412010-03-04 18:41:49 -0800163 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500164
Joe Onorato44fcb832011-12-14 20:59:30 -0800165 mDefaultUiModeType = context.getResources().getInteger(
166 com.android.internal.R.integer.config_defaultUiModeType);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500167 mCarModeKeepsScreenOn = (context.getResources().getInteger(
168 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
169 mDeskModeKeepsScreenOn = (context.getResources().getInteger(
170 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700171 mTelevision = context.getPackageManager().hasSystemFeature(
172 PackageManager.FEATURE_TELEVISION);
173
Adam Lesinski182f73f2013-12-05 16:48:06 -0800174 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700175 Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
Jeff Brown2416e092012-08-21 22:12:20 -0700176
Adam Lesinski182f73f2013-12-05 16:48:06 -0800177 mTwilightManager.registerListener(mTwilightListener, mHandler);
178
179 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800180 }
181
Adam Lesinski182f73f2013-12-05 16:48:06 -0800182 private final IBinder mService = new IUiModeManager.Stub() {
183 @Override
184 public void enableCarMode(int flags) {
185 final long ident = Binder.clearCallingIdentity();
186 try {
187 synchronized (mLock) {
188 setCarModeLocked(true);
189 if (mSystemReady) {
190 updateLocked(flags, 0);
191 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700192 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800193 } finally {
194 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500195 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800196 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800197
Adam Lesinski182f73f2013-12-05 16:48:06 -0800198 @Override
199 public void disableCarMode(int flags) {
200 final long ident = Binder.clearCallingIdentity();
201 try {
202 synchronized (mLock) {
203 setCarModeLocked(false);
204 if (mSystemReady) {
205 updateLocked(0, flags);
206 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700207 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800208 } finally {
209 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500210 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700211 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100212
Adam Lesinski182f73f2013-12-05 16:48:06 -0800213 @Override
214 public int getCurrentModeType() {
215 final long ident = Binder.clearCallingIdentity();
216 try {
217 synchronized (mLock) {
218 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700219 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800220 } finally {
221 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800222 }
223 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100224
Adam Lesinski182f73f2013-12-05 16:48:06 -0800225 @Override
226 public void setNightMode(int mode) {
227 switch (mode) {
228 case UiModeManager.MODE_NIGHT_NO:
229 case UiModeManager.MODE_NIGHT_YES:
230 case UiModeManager.MODE_NIGHT_AUTO:
231 break;
232 default:
233 throw new IllegalArgumentException("Unknown mode: " + mode);
234 }
235
236 final long ident = Binder.clearCallingIdentity();
237 try {
238 synchronized (mLock) {
239 if (isDoingNightModeLocked() && mNightMode != mode) {
240 Settings.Secure.putInt(getContext().getContentResolver(),
241 Settings.Secure.UI_NIGHT_MODE, mode);
242 mNightMode = mode;
243 updateLocked(0, 0);
244 }
245 }
246 } finally {
247 Binder.restoreCallingIdentity(ident);
248 }
249 }
250
251 @Override
252 public int getNightMode() {
253 synchronized (mLock) {
254 return mNightMode;
255 }
256 }
257
258 @Override
259 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
260 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
261 != PackageManager.PERMISSION_GRANTED) {
262
263 pw.println("Permission Denial: can't dump uimode service from from pid="
264 + Binder.getCallingPid()
265 + ", uid=" + Binder.getCallingUid());
266 return;
267 }
268
269 dumpImpl(pw);
270 }
271 };
272
273 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700274 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800275 pw.println("Current UI Mode Service state:");
276 pw.print(" mDockState="); pw.print(mDockState);
277 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
278 pw.print(" mNightMode="); pw.print(mNightMode);
279 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
280 pw.print(" mComputedNightMode="); pw.println(mComputedNightMode);
281 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
282 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
283 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
284 pw.print(" mSystemReady="); pw.println(mSystemReady);
285 pw.print(" mTwilightService.getCurrentState()=");
286 pw.println(mTwilightManager.getCurrentState());
Jeff Brown487bb6e2012-10-11 13:35:42 -0700287 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800288 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100289
Adam Lesinski182f73f2013-12-05 16:48:06 -0800290 @Override
291 public void onBootPhase(int phase) {
292 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
293 synchronized (mLock) {
294 mSystemReady = true;
295 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
296 updateComputedNightModeLocked();
297 updateLocked(0, 0);
298 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800299 }
300 }
301
Adam Lesinski182f73f2013-12-05 16:48:06 -0800302 boolean isDoingNightModeLocked() {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800303 return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
304 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100305
Adam Lesinski182f73f2013-12-05 16:48:06 -0800306 void setCarModeLocked(boolean enabled) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800307 if (mCarModeEnabled != enabled) {
308 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800309 }
310 }
311
Jeff Brown487bb6e2012-10-11 13:35:42 -0700312 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800313 synchronized (mLock) {
314 if (newState != mDockState) {
315 mDockState = newState;
Mike Lockwood924e1642010-03-05 11:56:53 -0500316 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800317 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700318 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800319 }
320 }
321 }
322 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500323
Jeff Brown487bb6e2012-10-11 13:35:42 -0700324 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400325 switch (state) {
326 case Intent.EXTRA_DOCK_STATE_DESK:
327 case Intent.EXTRA_DOCK_STATE_LE_DESK:
328 case Intent.EXTRA_DOCK_STATE_HE_DESK:
329 return true;
330 default:
331 return false;
332 }
333 }
334
Jeff Brown487bb6e2012-10-11 13:35:42 -0700335 private void updateConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700336 int uiMode = mTelevision ? Configuration.UI_MODE_TYPE_TELEVISION : mDefaultUiModeType;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800337 if (mCarModeEnabled) {
338 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400339 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800340 uiMode = Configuration.UI_MODE_TYPE_DESK;
341 }
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700342 if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800343 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700344 updateComputedNightModeLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800345 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
346 : Configuration.UI_MODE_NIGHT_NO;
347 } else {
348 uiMode |= mNightMode << 4;
349 }
350 } else {
351 // Disabling the car mode clears the night mode.
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400352 uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO;
353 }
354
355 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400356 Slog.d(TAG,
357 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400358 + "; mCarMode=" + mCarModeEnabled
359 + "; mNightMode=" + mNightMode
360 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800361 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100362
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800363 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700364 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700365 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700366 }
367 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100368
Jeff Brown487bb6e2012-10-11 13:35:42 -0700369 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700370 if (mSetUiMode != mConfiguration.uiMode) {
371 mSetUiMode = mConfiguration.uiMode;
372
373 try {
374 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
375 } catch (RemoteException e) {
376 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800377 }
378 }
379 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100380
Adam Lesinski182f73f2013-12-05 16:48:06 -0800381 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700382 String action = null;
383 String oldAction = null;
384 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
385 adjustStatusBarCarModeLocked();
386 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
387 } else if (isDeskDockState(mLastBroadcastState)) {
388 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
389 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100390
Jeff Brown487bb6e2012-10-11 13:35:42 -0700391 if (mCarModeEnabled) {
392 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100393 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700394
395 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800396 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700397 }
398 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
399 action = UiModeManager.ACTION_ENTER_CAR_MODE;
400 }
401 } else if (isDeskDockState(mDockState)) {
402 if (!isDeskDockState(mLastBroadcastState)) {
403 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800404 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700405 }
406 mLastBroadcastState = mDockState;
407 action = UiModeManager.ACTION_ENTER_DESK_MODE;
408 }
409 } else {
410 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
411 action = oldAction;
412 }
413
414 if (action != null) {
415 if (LOG) {
416 Slog.v(TAG, String.format(
417 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
418 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800419 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100420
Jeff Brown487bb6e2012-10-11 13:35:42 -0700421 // Send the ordered broadcast; the result receiver will receive after all
422 // broadcasts have been sent. If any broadcast receiver changes the result
423 // code from the initial value of RESULT_OK, then the result receiver will
424 // not launch the corresponding dock application. This gives apps a chance
425 // to override the behavior and stay in their app even when the device is
426 // placed into a dock.
427 Intent intent = new Intent(action);
428 intent.putExtra("enableFlags", enableFlags);
429 intent.putExtra("disableFlags", disableFlags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800430 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700431 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100432
Jeff Brown487bb6e2012-10-11 13:35:42 -0700433 // Attempting to make this transition a little more clean, we are going
434 // to hold off on doing a configuration change until we have finished
435 // the broadcast and started the home activity.
436 mHoldingConfiguration = true;
437 updateConfigurationLocked();
438 } else {
439 String category = null;
440 if (mCarModeEnabled) {
441 if (ENABLE_LAUNCH_CAR_DOCK_APP
442 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
443 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800444 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400445 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700446 if (ENABLE_LAUNCH_DESK_DOCK_APP
447 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
448 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800449 }
450 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700451 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
452 category = Intent.CATEGORY_HOME;
453 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800454 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100455
Jeff Brown487bb6e2012-10-11 13:35:42 -0700456 if (LOG) {
457 Slog.v(TAG, "updateLocked: null action, mDockState="
458 + mDockState +", category=" + category);
459 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400460
Jeff Brown487bb6e2012-10-11 13:35:42 -0700461 sendConfigurationAndStartDreamOrDockAppLocked(category);
462 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700463
Jeff Brown487bb6e2012-10-11 13:35:42 -0700464 // keep screen on when charging and in car mode
465 boolean keepScreenOn = mCharging &&
466 ((mCarModeEnabled && mCarModeKeepsScreenOn) ||
467 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
468 if (keepScreenOn != mWakeLock.isHeld()) {
469 if (keepScreenOn) {
470 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700471 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700472 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800473 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800474 }
475 }
476
Jeff Brown62c82e42012-09-26 01:30:41 -0700477 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
478 // Launch a dock activity
479 String category = null;
480 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
481 // Only launch car home when car mode is enabled and the caller
482 // has asked us to switch to it.
483 if (ENABLE_LAUNCH_CAR_DOCK_APP
484 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
485 category = Intent.CATEGORY_CAR_DOCK;
486 }
487 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
488 // Only launch car home when desk mode is enabled and the caller
489 // has asked us to switch to it. Currently re-using the car
490 // mode flag since we don't have a formal API for "desk mode".
491 if (ENABLE_LAUNCH_DESK_DOCK_APP
492 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
493 category = Intent.CATEGORY_DESK_DOCK;
494 }
495 } else {
496 // Launch the standard home app if requested.
497 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
498 category = Intent.CATEGORY_HOME;
499 }
500 }
501
502 if (LOG) {
503 Slog.v(TAG, String.format(
504 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
505 + "category=%s",
506 action, enableFlags, disableFlags, category));
507 }
508
509 sendConfigurationAndStartDreamOrDockAppLocked(category);
510 }
511
512 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
513 // Update the configuration but don't send it yet.
514 mHoldingConfiguration = false;
515 updateConfigurationLocked();
516
517 // Start the dock app, if there is one.
518 boolean dockAppStarted = false;
519 if (category != null) {
520 // Now we are going to be careful about switching the
521 // configuration and starting the activity -- we need to
522 // do this in a specific order under control of the
523 // activity manager, to do it cleanly. So compute the
524 // new config, but don't set it yet, and let the
525 // activity manager take care of both the start and config
526 // change.
527 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800528 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700529 try {
530 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800531 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700532 mConfiguration, null, UserHandle.USER_CURRENT);
533 if (result >= ActivityManager.START_SUCCESS) {
534 dockAppStarted = true;
535 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
536 Slog.e(TAG, "Could not start dock app: " + homeIntent
537 + ", startActivityWithConfig result " + result);
538 }
539 } catch (RemoteException ex) {
540 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700541 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700542 }
543 }
544
545 // Send the new configuration.
546 sendConfigurationLocked();
547
548 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700549 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800550 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700551 }
552 }
553
Dianne Hackborn7299c412010-03-04 18:41:49 -0800554 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800555 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800556 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700557 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800558 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800559 }
560
Joe Onorato089de882010-04-12 08:18:45 -0700561 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800562 // features of the status bar; these are ORed together to form the
563 // active disabled list. So if (for example) the device is locked and
564 // the status bar should be totally disabled, the calls below will
565 // have no effect until the device is unlocked.
566 if (mStatusBarManager != null) {
567 mStatusBarManager.disable(mCarModeEnabled
568 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
569 : StatusBarManager.DISABLE_NONE);
570 }
571
572 if (mNotificationManager == null) {
573 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800574 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800575 }
576
577 if (mNotificationManager != null) {
578 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800579 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800580
581 Notification n = new Notification();
582 n.icon = R.drawable.stat_notify_car_mode;
583 n.defaults = Notification.DEFAULT_LIGHTS;
584 n.flags = Notification.FLAG_ONGOING_EVENT;
585 n.when = 0;
586 n.setLatestEventInfo(
Adam Lesinski182f73f2013-12-05 16:48:06 -0800587 context,
588 context.getString(R.string.car_mode_disable_notification_title),
589 context.getString(R.string.car_mode_disable_notification_message),
590 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700591 null, UserHandle.CURRENT));
592 mNotificationManager.notifyAsUser(null,
593 R.string.car_mode_disable_notification_title, n, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800594 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700595 mNotificationManager.cancelAsUser(null,
596 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800597 }
598 }
599 }
600
Adam Lesinski182f73f2013-12-05 16:48:06 -0800601 void updateTwilight() {
Jeff Brown2416e092012-08-21 22:12:20 -0700602 synchronized (mLock) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700603 if (isDoingNightModeLocked() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700604 updateComputedNightModeLocked();
605 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800606 }
607 }
Jeff Brown2416e092012-08-21 22:12:20 -0700608 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800609
Jeff Brown2416e092012-08-21 22:12:20 -0700610 private void updateComputedNightModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800611 TwilightState state = mTwilightManager.getCurrentState();
Jeff Brown2416e092012-08-21 22:12:20 -0700612 if (state != null) {
613 mComputedNightMode = state.isNight();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800614 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800615 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100616
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100617
Dianne Hackborn7299c412010-03-04 18:41:49 -0800618}