blob: f59edc7f83e32e6d70da7062c925d78b14d3c553 [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;
John Spurlock6c191292014-04-03 16:37:27 -040074 private boolean mWatch;
Dianne Hackborn7299c412010-03-04 18:41:49 -080075 private boolean mComputedNightMode;
Adam Lesinski182f73f2013-12-05 16:48:06 -080076
77 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080078 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080079 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080080
Dianne Hackborn7299c412010-03-04 18:41:49 -080081 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -080082 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010083
Adam Lesinski182f73f2013-12-05 16:48:06 -080084 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080085
Adam Lesinski182f73f2013-12-05 16:48:06 -080086 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080087 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080088 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -070089
Adam Lesinski182f73f2013-12-05 16:48:06 -080090 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -080091
Jeff Brownb880d882014-02-10 19:47:07 -080092 public UiModeManagerService(Context context) {
93 super(context);
94 }
95
Adam Lesinski182f73f2013-12-05 16:48:06 -080096 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -070097 Intent intent = new Intent(Intent.ACTION_MAIN);
98 intent.addCategory(category);
99 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
100 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
101 return intent;
102 }
John Spurlock960779d2012-05-29 14:37:05 -0400103
Dianne Hackborn7299c412010-03-04 18:41:49 -0800104 // The broadcast receiver which receives the result of the ordered broadcast sent when
105 // the dock state changes. The original ordered broadcast is sent with an initial result
106 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
107 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
108 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
109 @Override
110 public void onReceive(Context context, Intent intent) {
111 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400112 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400113 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400114 + ": canceled: " + getResultCode());
115 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800116 return;
117 }
118
Jeff Brown62c82e42012-09-26 01:30:41 -0700119 final int enableFlags = intent.getIntExtra("enableFlags", 0);
120 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800121 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700122 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800123 }
124 }
125 };
126
Dianne Hackborn7299c412010-03-04 18:41:49 -0800127 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
128 @Override
129 public void onReceive(Context context, Intent intent) {
130 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
131 Intent.EXTRA_DOCK_STATE_UNDOCKED);
132 updateDockState(state);
133 }
134 };
135
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500136 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
137 @Override
138 public void onReceive(Context context, Intent intent) {
139 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
140 synchronized (mLock) {
141 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700142 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500143 }
144 }
145 }
146 };
147
Adam Lesinski182f73f2013-12-05 16:48:06 -0800148 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700149 @Override
Jeff Brown2416e092012-08-21 22:12:20 -0700150 public void onTwilightStateChanged() {
151 updateTwilight();
Dianne Hackborn57f45032010-06-17 15:49:33 -0700152 }
153 };
154
Adam Lesinski182f73f2013-12-05 16:48:06 -0800155 @Override
156 public void onStart() {
157 final Context context = getContext();
158 mTwilightManager = getLocalService(TwilightManager.class);
159 final PowerManager powerManager =
160 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
161 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800162
Adam Lesinski182f73f2013-12-05 16:48:06 -0800163 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800164 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800165 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500166 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
167
Dianne Hackborn7299c412010-03-04 18:41:49 -0800168 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500169
Joe Onorato44fcb832011-12-14 20:59:30 -0800170 mDefaultUiModeType = context.getResources().getInteger(
171 com.android.internal.R.integer.config_defaultUiModeType);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500172 mCarModeKeepsScreenOn = (context.getResources().getInteger(
173 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
174 mDeskModeKeepsScreenOn = (context.getResources().getInteger(
175 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700176 mTelevision = context.getPackageManager().hasSystemFeature(
Tim Kilbourn07229682014-03-14 13:44:12 -0700177 PackageManager.FEATURE_TELEVISION) ||
178 context.getPackageManager().hasSystemFeature(
179 PackageManager.FEATURE_LEANBACK);
John Spurlock6c191292014-04-03 16:37:27 -0400180 mWatch = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700181
Adam Lesinski182f73f2013-12-05 16:48:06 -0800182 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700183 Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
Jeff Brown2416e092012-08-21 22:12:20 -0700184
Adam Lesinski182f73f2013-12-05 16:48:06 -0800185 mTwilightManager.registerListener(mTwilightListener, mHandler);
186
187 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800188 }
189
Adam Lesinski182f73f2013-12-05 16:48:06 -0800190 private final IBinder mService = new IUiModeManager.Stub() {
191 @Override
192 public void enableCarMode(int flags) {
193 final long ident = Binder.clearCallingIdentity();
194 try {
195 synchronized (mLock) {
196 setCarModeLocked(true);
197 if (mSystemReady) {
198 updateLocked(flags, 0);
199 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700200 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800201 } finally {
202 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500203 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800204 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800205
Adam Lesinski182f73f2013-12-05 16:48:06 -0800206 @Override
207 public void disableCarMode(int flags) {
208 final long ident = Binder.clearCallingIdentity();
209 try {
210 synchronized (mLock) {
211 setCarModeLocked(false);
212 if (mSystemReady) {
213 updateLocked(0, flags);
214 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700215 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800216 } finally {
217 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500218 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700219 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100220
Adam Lesinski182f73f2013-12-05 16:48:06 -0800221 @Override
222 public int getCurrentModeType() {
223 final long ident = Binder.clearCallingIdentity();
224 try {
225 synchronized (mLock) {
226 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700227 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800228 } finally {
229 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800230 }
231 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100232
Adam Lesinski182f73f2013-12-05 16:48:06 -0800233 @Override
234 public void setNightMode(int mode) {
235 switch (mode) {
236 case UiModeManager.MODE_NIGHT_NO:
237 case UiModeManager.MODE_NIGHT_YES:
238 case UiModeManager.MODE_NIGHT_AUTO:
239 break;
240 default:
241 throw new IllegalArgumentException("Unknown mode: " + mode);
242 }
243
244 final long ident = Binder.clearCallingIdentity();
245 try {
246 synchronized (mLock) {
247 if (isDoingNightModeLocked() && mNightMode != mode) {
248 Settings.Secure.putInt(getContext().getContentResolver(),
249 Settings.Secure.UI_NIGHT_MODE, mode);
250 mNightMode = mode;
251 updateLocked(0, 0);
252 }
253 }
254 } finally {
255 Binder.restoreCallingIdentity(ident);
256 }
257 }
258
259 @Override
260 public int getNightMode() {
261 synchronized (mLock) {
262 return mNightMode;
263 }
264 }
265
266 @Override
267 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
268 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
269 != PackageManager.PERMISSION_GRANTED) {
270
271 pw.println("Permission Denial: can't dump uimode service from from pid="
272 + Binder.getCallingPid()
273 + ", uid=" + Binder.getCallingUid());
274 return;
275 }
276
277 dumpImpl(pw);
278 }
279 };
280
281 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700282 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800283 pw.println("Current UI Mode Service state:");
284 pw.print(" mDockState="); pw.print(mDockState);
285 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
286 pw.print(" mNightMode="); pw.print(mNightMode);
287 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
288 pw.print(" mComputedNightMode="); pw.println(mComputedNightMode);
289 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
290 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
291 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
292 pw.print(" mSystemReady="); pw.println(mSystemReady);
293 pw.print(" mTwilightService.getCurrentState()=");
294 pw.println(mTwilightManager.getCurrentState());
Jeff Brown487bb6e2012-10-11 13:35:42 -0700295 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800296 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100297
Adam Lesinski182f73f2013-12-05 16:48:06 -0800298 @Override
299 public void onBootPhase(int phase) {
300 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
301 synchronized (mLock) {
302 mSystemReady = true;
303 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
304 updateComputedNightModeLocked();
305 updateLocked(0, 0);
306 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800307 }
308 }
309
Adam Lesinski182f73f2013-12-05 16:48:06 -0800310 boolean isDoingNightModeLocked() {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800311 return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
312 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100313
Adam Lesinski182f73f2013-12-05 16:48:06 -0800314 void setCarModeLocked(boolean enabled) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800315 if (mCarModeEnabled != enabled) {
316 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800317 }
318 }
319
Jeff Brown487bb6e2012-10-11 13:35:42 -0700320 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800321 synchronized (mLock) {
322 if (newState != mDockState) {
323 mDockState = newState;
Mike Lockwood924e1642010-03-05 11:56:53 -0500324 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800325 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700326 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800327 }
328 }
329 }
330 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500331
Jeff Brown487bb6e2012-10-11 13:35:42 -0700332 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400333 switch (state) {
334 case Intent.EXTRA_DOCK_STATE_DESK:
335 case Intent.EXTRA_DOCK_STATE_LE_DESK:
336 case Intent.EXTRA_DOCK_STATE_HE_DESK:
337 return true;
338 default:
339 return false;
340 }
341 }
342
Jeff Brown487bb6e2012-10-11 13:35:42 -0700343 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400344 int uiMode = mDefaultUiModeType;
345 if (mTelevision) {
346 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
347 } else if (mWatch) {
348 uiMode = Configuration.UI_MODE_TYPE_WATCH;
349 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800350 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400351 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800352 uiMode = Configuration.UI_MODE_TYPE_DESK;
353 }
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700354 if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800355 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700356 updateComputedNightModeLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800357 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
358 : Configuration.UI_MODE_NIGHT_NO;
359 } else {
360 uiMode |= mNightMode << 4;
361 }
362 } else {
363 // Disabling the car mode clears the night mode.
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400364 uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO;
365 }
366
367 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400368 Slog.d(TAG,
369 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400370 + "; mCarMode=" + mCarModeEnabled
371 + "; mNightMode=" + mNightMode
372 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800373 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100374
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800375 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700376 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700377 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700378 }
379 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100380
Jeff Brown487bb6e2012-10-11 13:35:42 -0700381 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700382 if (mSetUiMode != mConfiguration.uiMode) {
383 mSetUiMode = mConfiguration.uiMode;
384
385 try {
386 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
387 } catch (RemoteException e) {
388 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800389 }
390 }
391 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100392
Adam Lesinski182f73f2013-12-05 16:48:06 -0800393 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700394 String action = null;
395 String oldAction = null;
396 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
397 adjustStatusBarCarModeLocked();
398 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
399 } else if (isDeskDockState(mLastBroadcastState)) {
400 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
401 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100402
Jeff Brown487bb6e2012-10-11 13:35:42 -0700403 if (mCarModeEnabled) {
404 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100405 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700406
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 = Intent.EXTRA_DOCK_STATE_CAR;
411 action = UiModeManager.ACTION_ENTER_CAR_MODE;
412 }
413 } else if (isDeskDockState(mDockState)) {
414 if (!isDeskDockState(mLastBroadcastState)) {
415 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800416 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700417 }
418 mLastBroadcastState = mDockState;
419 action = UiModeManager.ACTION_ENTER_DESK_MODE;
420 }
421 } else {
422 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
423 action = oldAction;
424 }
425
426 if (action != null) {
427 if (LOG) {
428 Slog.v(TAG, String.format(
429 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
430 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800431 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100432
Jeff Brown487bb6e2012-10-11 13:35:42 -0700433 // Send the ordered broadcast; the result receiver will receive after all
434 // broadcasts have been sent. If any broadcast receiver changes the result
435 // code from the initial value of RESULT_OK, then the result receiver will
436 // not launch the corresponding dock application. This gives apps a chance
437 // to override the behavior and stay in their app even when the device is
438 // placed into a dock.
439 Intent intent = new Intent(action);
440 intent.putExtra("enableFlags", enableFlags);
441 intent.putExtra("disableFlags", disableFlags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800442 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700443 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100444
Jeff Brown487bb6e2012-10-11 13:35:42 -0700445 // Attempting to make this transition a little more clean, we are going
446 // to hold off on doing a configuration change until we have finished
447 // the broadcast and started the home activity.
448 mHoldingConfiguration = true;
449 updateConfigurationLocked();
450 } else {
451 String category = null;
452 if (mCarModeEnabled) {
453 if (ENABLE_LAUNCH_CAR_DOCK_APP
454 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
455 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800456 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400457 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700458 if (ENABLE_LAUNCH_DESK_DOCK_APP
459 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
460 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800461 }
462 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700463 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
464 category = Intent.CATEGORY_HOME;
465 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800466 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100467
Jeff Brown487bb6e2012-10-11 13:35:42 -0700468 if (LOG) {
469 Slog.v(TAG, "updateLocked: null action, mDockState="
470 + mDockState +", category=" + category);
471 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400472
Jeff Brown487bb6e2012-10-11 13:35:42 -0700473 sendConfigurationAndStartDreamOrDockAppLocked(category);
474 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700475
Jeff Brown487bb6e2012-10-11 13:35:42 -0700476 // keep screen on when charging and in car mode
477 boolean keepScreenOn = mCharging &&
478 ((mCarModeEnabled && mCarModeKeepsScreenOn) ||
479 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
480 if (keepScreenOn != mWakeLock.isHeld()) {
481 if (keepScreenOn) {
482 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700483 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700484 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800485 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800486 }
487 }
488
Jeff Brown62c82e42012-09-26 01:30:41 -0700489 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
490 // Launch a dock activity
491 String category = null;
492 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
493 // Only launch car home when car mode is enabled and the caller
494 // has asked us to switch to it.
495 if (ENABLE_LAUNCH_CAR_DOCK_APP
496 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
497 category = Intent.CATEGORY_CAR_DOCK;
498 }
499 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
500 // Only launch car home when desk mode is enabled and the caller
501 // has asked us to switch to it. Currently re-using the car
502 // mode flag since we don't have a formal API for "desk mode".
503 if (ENABLE_LAUNCH_DESK_DOCK_APP
504 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
505 category = Intent.CATEGORY_DESK_DOCK;
506 }
507 } else {
508 // Launch the standard home app if requested.
509 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
510 category = Intent.CATEGORY_HOME;
511 }
512 }
513
514 if (LOG) {
515 Slog.v(TAG, String.format(
516 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
517 + "category=%s",
518 action, enableFlags, disableFlags, category));
519 }
520
521 sendConfigurationAndStartDreamOrDockAppLocked(category);
522 }
523
524 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
525 // Update the configuration but don't send it yet.
526 mHoldingConfiguration = false;
527 updateConfigurationLocked();
528
529 // Start the dock app, if there is one.
530 boolean dockAppStarted = false;
531 if (category != null) {
532 // Now we are going to be careful about switching the
533 // configuration and starting the activity -- we need to
534 // do this in a specific order under control of the
535 // activity manager, to do it cleanly. So compute the
536 // new config, but don't set it yet, and let the
537 // activity manager take care of both the start and config
538 // change.
539 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800540 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700541 try {
542 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800543 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700544 mConfiguration, null, UserHandle.USER_CURRENT);
545 if (result >= ActivityManager.START_SUCCESS) {
546 dockAppStarted = true;
547 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
548 Slog.e(TAG, "Could not start dock app: " + homeIntent
549 + ", startActivityWithConfig result " + result);
550 }
551 } catch (RemoteException ex) {
552 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700553 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700554 }
555 }
556
557 // Send the new configuration.
558 sendConfigurationLocked();
559
560 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700561 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800562 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700563 }
564 }
565
Dianne Hackborn7299c412010-03-04 18:41:49 -0800566 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800567 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800568 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700569 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800570 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800571 }
572
Joe Onorato089de882010-04-12 08:18:45 -0700573 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800574 // features of the status bar; these are ORed together to form the
575 // active disabled list. So if (for example) the device is locked and
576 // the status bar should be totally disabled, the calls below will
577 // have no effect until the device is unlocked.
578 if (mStatusBarManager != null) {
579 mStatusBarManager.disable(mCarModeEnabled
580 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
581 : StatusBarManager.DISABLE_NONE);
582 }
583
584 if (mNotificationManager == null) {
585 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800586 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800587 }
588
589 if (mNotificationManager != null) {
590 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800591 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800592
593 Notification n = new Notification();
594 n.icon = R.drawable.stat_notify_car_mode;
595 n.defaults = Notification.DEFAULT_LIGHTS;
596 n.flags = Notification.FLAG_ONGOING_EVENT;
597 n.when = 0;
598 n.setLatestEventInfo(
Adam Lesinski182f73f2013-12-05 16:48:06 -0800599 context,
600 context.getString(R.string.car_mode_disable_notification_title),
601 context.getString(R.string.car_mode_disable_notification_message),
602 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700603 null, UserHandle.CURRENT));
604 mNotificationManager.notifyAsUser(null,
605 R.string.car_mode_disable_notification_title, n, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800606 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700607 mNotificationManager.cancelAsUser(null,
608 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800609 }
610 }
611 }
612
Adam Lesinski182f73f2013-12-05 16:48:06 -0800613 void updateTwilight() {
Jeff Brown2416e092012-08-21 22:12:20 -0700614 synchronized (mLock) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700615 if (isDoingNightModeLocked() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700616 updateComputedNightModeLocked();
617 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800618 }
619 }
Jeff Brown2416e092012-08-21 22:12:20 -0700620 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800621
Jeff Brown2416e092012-08-21 22:12:20 -0700622 private void updateComputedNightModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800623 TwilightState state = mTwilightManager.getCurrentState();
Jeff Brown2416e092012-08-21 22:12:20 -0700624 if (state != null) {
625 mComputedNightMode = state.isNight();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800626 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800627 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100628
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100629
Dianne Hackborn7299c412010-03-04 18:41:49 -0800630}