blob: 4c6b772ee8ab371f2dad393fd80fa855c68d3c20 [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;
keunyoung1d0a7cc2014-07-28 13:12:50 -070076 private int mCarModeEnableFlags;
Adam Lesinski182f73f2013-12-05 16:48:06 -080077
78 int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080079 private int mSetUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080080 private boolean mHoldingConfiguration = false;
Adam Lesinski182f73f2013-12-05 16:48:06 -080081
Dianne Hackborn7299c412010-03-04 18:41:49 -080082 private Configuration mConfiguration = new Configuration();
Adam Lesinski182f73f2013-12-05 16:48:06 -080083 boolean mSystemReady;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010084
Adam Lesinski182f73f2013-12-05 16:48:06 -080085 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080086
Adam Lesinski182f73f2013-12-05 16:48:06 -080087 private TwilightManager mTwilightManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080088 private NotificationManager mNotificationManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080089 private StatusBarManager mStatusBarManager;
Jeff Brown9fca9e92012-10-05 14:42:56 -070090
Adam Lesinski182f73f2013-12-05 16:48:06 -080091 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -080092
Jeff Brownb880d882014-02-10 19:47:07 -080093 public UiModeManagerService(Context context) {
94 super(context);
95 }
96
Adam Lesinski182f73f2013-12-05 16:48:06 -080097 private static Intent buildHomeIntent(String category) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -070098 Intent intent = new Intent(Intent.ACTION_MAIN);
99 intent.addCategory(category);
100 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
101 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
102 return intent;
103 }
John Spurlock960779d2012-05-29 14:37:05 -0400104
Dianne Hackborn7299c412010-03-04 18:41:49 -0800105 // The broadcast receiver which receives the result of the ordered broadcast sent when
106 // the dock state changes. The original ordered broadcast is sent with an initial result
107 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
108 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
109 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
110 @Override
111 public void onReceive(Context context, Intent intent) {
112 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400113 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400114 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400115 + ": canceled: " + getResultCode());
116 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800117 return;
118 }
119
Jeff Brown62c82e42012-09-26 01:30:41 -0700120 final int enableFlags = intent.getIntExtra("enableFlags", 0);
121 final int disableFlags = intent.getIntExtra("disableFlags", 0);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800122 synchronized (mLock) {
Jeff Brown62c82e42012-09-26 01:30:41 -0700123 updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800124 }
125 }
126 };
127
Dianne Hackborn7299c412010-03-04 18:41:49 -0800128 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
129 @Override
130 public void onReceive(Context context, Intent intent) {
131 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
132 Intent.EXTRA_DOCK_STATE_UNDOCKED);
133 updateDockState(state);
134 }
135 };
136
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500137 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
138 @Override
139 public void onReceive(Context context, Intent intent) {
140 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
141 synchronized (mLock) {
142 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700143 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500144 }
145 }
146 }
147 };
148
Adam Lesinski182f73f2013-12-05 16:48:06 -0800149 private final TwilightListener mTwilightListener = new TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700150 @Override
Jeff Brown2416e092012-08-21 22:12:20 -0700151 public void onTwilightStateChanged() {
152 updateTwilight();
Dianne Hackborn57f45032010-06-17 15:49:33 -0700153 }
154 };
155
Adam Lesinski182f73f2013-12-05 16:48:06 -0800156 @Override
157 public void onStart() {
158 final Context context = getContext();
159 mTwilightManager = getLocalService(TwilightManager.class);
160 final PowerManager powerManager =
161 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
162 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800163
Adam Lesinski182f73f2013-12-05 16:48:06 -0800164 context.registerReceiver(mDockModeReceiver,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800165 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Adam Lesinski182f73f2013-12-05 16:48:06 -0800166 context.registerReceiver(mBatteryReceiver,
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500167 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
168
Dianne Hackborn7299c412010-03-04 18:41:49 -0800169 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500170
Joe Onorato44fcb832011-12-14 20:59:30 -0800171 mDefaultUiModeType = context.getResources().getInteger(
172 com.android.internal.R.integer.config_defaultUiModeType);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500173 mCarModeKeepsScreenOn = (context.getResources().getInteger(
174 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
175 mDeskModeKeepsScreenOn = (context.getResources().getInteger(
176 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700177 mTelevision = context.getPackageManager().hasSystemFeature(
Tim Kilbourn07229682014-03-14 13:44:12 -0700178 PackageManager.FEATURE_TELEVISION) ||
179 context.getPackageManager().hasSystemFeature(
180 PackageManager.FEATURE_LEANBACK);
John Spurlock6c191292014-04-03 16:37:27 -0400181 mWatch = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700182
Adam Lesinski182f73f2013-12-05 16:48:06 -0800183 mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700184 Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
Jeff Brown2416e092012-08-21 22:12:20 -0700185
Adam Lesinski182f73f2013-12-05 16:48:06 -0800186 mTwilightManager.registerListener(mTwilightListener, mHandler);
187
188 publishBinderService(Context.UI_MODE_SERVICE, mService);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800189 }
190
Adam Lesinski182f73f2013-12-05 16:48:06 -0800191 private final IBinder mService = new IUiModeManager.Stub() {
192 @Override
193 public void enableCarMode(int flags) {
194 final long ident = Binder.clearCallingIdentity();
195 try {
196 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700197 setCarModeLocked(true, flags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800198 if (mSystemReady) {
199 updateLocked(flags, 0);
200 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700201 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800202 } finally {
203 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500204 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800205 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800206
Adam Lesinski182f73f2013-12-05 16:48:06 -0800207 @Override
208 public void disableCarMode(int flags) {
209 final long ident = Binder.clearCallingIdentity();
210 try {
211 synchronized (mLock) {
keunyoung1d0a7cc2014-07-28 13:12:50 -0700212 setCarModeLocked(false, 0);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800213 if (mSystemReady) {
214 updateLocked(0, flags);
215 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700216 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800217 } finally {
218 Binder.restoreCallingIdentity(ident);
Mike Lockwood924e1642010-03-05 11:56:53 -0500219 }
Jeff Brown487bb6e2012-10-11 13:35:42 -0700220 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100221
Adam Lesinski182f73f2013-12-05 16:48:06 -0800222 @Override
223 public int getCurrentModeType() {
224 final long ident = Binder.clearCallingIdentity();
225 try {
226 synchronized (mLock) {
227 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
Jeff Brown487bb6e2012-10-11 13:35:42 -0700228 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800229 } finally {
230 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800231 }
232 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100233
Adam Lesinski182f73f2013-12-05 16:48:06 -0800234 @Override
235 public void setNightMode(int mode) {
236 switch (mode) {
237 case UiModeManager.MODE_NIGHT_NO:
238 case UiModeManager.MODE_NIGHT_YES:
239 case UiModeManager.MODE_NIGHT_AUTO:
240 break;
241 default:
242 throw new IllegalArgumentException("Unknown mode: " + mode);
243 }
244
245 final long ident = Binder.clearCallingIdentity();
246 try {
247 synchronized (mLock) {
248 if (isDoingNightModeLocked() && mNightMode != mode) {
249 Settings.Secure.putInt(getContext().getContentResolver(),
250 Settings.Secure.UI_NIGHT_MODE, mode);
251 mNightMode = mode;
252 updateLocked(0, 0);
253 }
254 }
255 } finally {
256 Binder.restoreCallingIdentity(ident);
257 }
258 }
259
260 @Override
261 public int getNightMode() {
262 synchronized (mLock) {
263 return mNightMode;
264 }
265 }
266
267 @Override
268 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
269 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
270 != PackageManager.PERMISSION_GRANTED) {
271
272 pw.println("Permission Denial: can't dump uimode service from from pid="
273 + Binder.getCallingPid()
274 + ", uid=" + Binder.getCallingUid());
275 return;
276 }
277
278 dumpImpl(pw);
279 }
280 };
281
282 void dumpImpl(PrintWriter pw) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700283 synchronized (mLock) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800284 pw.println("Current UI Mode Service state:");
285 pw.print(" mDockState="); pw.print(mDockState);
286 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
287 pw.print(" mNightMode="); pw.print(mNightMode);
288 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
keunyoung1d0a7cc2014-07-28 13:12:50 -0700289 pw.print(" mComputedNightMode="); pw.print(mComputedNightMode);
290 pw.print(" mCarModeEnableFlags="); pw.println(mCarModeEnableFlags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800291 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
292 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
293 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
294 pw.print(" mSystemReady="); pw.println(mSystemReady);
295 pw.print(" mTwilightService.getCurrentState()=");
296 pw.println(mTwilightManager.getCurrentState());
Jeff Brown487bb6e2012-10-11 13:35:42 -0700297 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800298 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100299
Adam Lesinski182f73f2013-12-05 16:48:06 -0800300 @Override
301 public void onBootPhase(int phase) {
302 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
303 synchronized (mLock) {
304 mSystemReady = true;
305 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
306 updateComputedNightModeLocked();
307 updateLocked(0, 0);
308 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800309 }
310 }
311
Adam Lesinski182f73f2013-12-05 16:48:06 -0800312 boolean isDoingNightModeLocked() {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800313 return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
314 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100315
keunyoung1d0a7cc2014-07-28 13:12:50 -0700316 void setCarModeLocked(boolean enabled, int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800317 if (mCarModeEnabled != enabled) {
318 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800319 }
keunyoung1d0a7cc2014-07-28 13:12:50 -0700320 mCarModeEnableFlags = flags;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800321 }
322
Jeff Brown487bb6e2012-10-11 13:35:42 -0700323 private void updateDockState(int newState) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800324 synchronized (mLock) {
325 if (newState != mDockState) {
326 mDockState = newState;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700327 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800328 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700329 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800330 }
331 }
332 }
333 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500334
Jeff Brown487bb6e2012-10-11 13:35:42 -0700335 private static boolean isDeskDockState(int state) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400336 switch (state) {
337 case Intent.EXTRA_DOCK_STATE_DESK:
338 case Intent.EXTRA_DOCK_STATE_LE_DESK:
339 case Intent.EXTRA_DOCK_STATE_HE_DESK:
340 return true;
341 default:
342 return false;
343 }
344 }
345
Jeff Brown487bb6e2012-10-11 13:35:42 -0700346 private void updateConfigurationLocked() {
John Spurlock6c191292014-04-03 16:37:27 -0400347 int uiMode = mDefaultUiModeType;
348 if (mTelevision) {
349 uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
350 } else if (mWatch) {
351 uiMode = Configuration.UI_MODE_TYPE_WATCH;
352 } else if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800353 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400354 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800355 uiMode = Configuration.UI_MODE_TYPE_DESK;
356 }
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700357 if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800358 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700359 updateComputedNightModeLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800360 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
361 : Configuration.UI_MODE_NIGHT_NO;
362 } else {
363 uiMode |= mNightMode << 4;
364 }
365 } else {
366 // Disabling the car mode clears the night mode.
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400367 uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO;
368 }
369
370 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400371 Slog.d(TAG,
372 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400373 + "; mCarMode=" + mCarModeEnabled
374 + "; mNightMode=" + mNightMode
375 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800376 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100377
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800378 mCurUiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700379 if (!mHoldingConfiguration) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700380 mConfiguration.uiMode = uiMode;
Jeff Brown62c82e42012-09-26 01:30:41 -0700381 }
382 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100383
Jeff Brown487bb6e2012-10-11 13:35:42 -0700384 private void sendConfigurationLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700385 if (mSetUiMode != mConfiguration.uiMode) {
386 mSetUiMode = mConfiguration.uiMode;
387
388 try {
389 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
390 } catch (RemoteException e) {
391 Slog.w(TAG, "Failure communicating with activity manager", e);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800392 }
393 }
394 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100395
Adam Lesinski182f73f2013-12-05 16:48:06 -0800396 void updateLocked(int enableFlags, int disableFlags) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700397 String action = null;
398 String oldAction = null;
399 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
400 adjustStatusBarCarModeLocked();
401 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
402 } else if (isDeskDockState(mLastBroadcastState)) {
403 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
404 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100405
Jeff Brown487bb6e2012-10-11 13:35:42 -0700406 if (mCarModeEnabled) {
407 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100408 adjustStatusBarCarModeLocked();
Jeff Brown487bb6e2012-10-11 13:35:42 -0700409
410 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800411 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700412 }
413 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
414 action = UiModeManager.ACTION_ENTER_CAR_MODE;
415 }
416 } else if (isDeskDockState(mDockState)) {
417 if (!isDeskDockState(mLastBroadcastState)) {
418 if (oldAction != null) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800419 getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Jeff Brown487bb6e2012-10-11 13:35:42 -0700420 }
421 mLastBroadcastState = mDockState;
422 action = UiModeManager.ACTION_ENTER_DESK_MODE;
423 }
424 } else {
425 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
426 action = oldAction;
427 }
428
429 if (action != null) {
430 if (LOG) {
431 Slog.v(TAG, String.format(
432 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
433 action, enableFlags, disableFlags));
Dianne Hackborn7299c412010-03-04 18:41:49 -0800434 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100435
Jeff Brown487bb6e2012-10-11 13:35:42 -0700436 // Send the ordered broadcast; the result receiver will receive after all
437 // broadcasts have been sent. If any broadcast receiver changes the result
438 // code from the initial value of RESULT_OK, then the result receiver will
439 // not launch the corresponding dock application. This gives apps a chance
440 // to override the behavior and stay in their app even when the device is
441 // placed into a dock.
442 Intent intent = new Intent(action);
443 intent.putExtra("enableFlags", enableFlags);
444 intent.putExtra("disableFlags", disableFlags);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800445 getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Jeff Brown487bb6e2012-10-11 13:35:42 -0700446 mResultReceiver, null, Activity.RESULT_OK, null, null);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100447
Jeff Brown487bb6e2012-10-11 13:35:42 -0700448 // Attempting to make this transition a little more clean, we are going
449 // to hold off on doing a configuration change until we have finished
450 // the broadcast and started the home activity.
451 mHoldingConfiguration = true;
452 updateConfigurationLocked();
453 } else {
454 String category = null;
455 if (mCarModeEnabled) {
456 if (ENABLE_LAUNCH_CAR_DOCK_APP
457 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
458 category = Intent.CATEGORY_CAR_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800459 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400460 } else if (isDeskDockState(mDockState)) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700461 if (ENABLE_LAUNCH_DESK_DOCK_APP
462 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
463 category = Intent.CATEGORY_DESK_DOCK;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800464 }
465 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700466 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
467 category = Intent.CATEGORY_HOME;
468 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800469 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100470
Jeff Brown487bb6e2012-10-11 13:35:42 -0700471 if (LOG) {
472 Slog.v(TAG, "updateLocked: null action, mDockState="
473 + mDockState +", category=" + category);
474 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400475
Jeff Brown487bb6e2012-10-11 13:35:42 -0700476 sendConfigurationAndStartDreamOrDockAppLocked(category);
477 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700478
Jeff Brown487bb6e2012-10-11 13:35:42 -0700479 // keep screen on when charging and in car mode
480 boolean keepScreenOn = mCharging &&
keunyoung1d0a7cc2014-07-28 13:12:50 -0700481 ((mCarModeEnabled && mCarModeKeepsScreenOn &&
482 (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_NO_WAKE_LOCK) == 0) ||
Jeff Brown487bb6e2012-10-11 13:35:42 -0700483 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
484 if (keepScreenOn != mWakeLock.isHeld()) {
485 if (keepScreenOn) {
486 mWakeLock.acquire();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700487 } else {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700488 mWakeLock.release();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800489 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800490 }
491 }
492
Jeff Brown62c82e42012-09-26 01:30:41 -0700493 private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
494 // Launch a dock activity
495 String category = null;
496 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
497 // Only launch car home when car mode is enabled and the caller
498 // has asked us to switch to it.
499 if (ENABLE_LAUNCH_CAR_DOCK_APP
500 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
501 category = Intent.CATEGORY_CAR_DOCK;
502 }
503 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
504 // Only launch car home when desk mode is enabled and the caller
505 // has asked us to switch to it. Currently re-using the car
506 // mode flag since we don't have a formal API for "desk mode".
507 if (ENABLE_LAUNCH_DESK_DOCK_APP
508 && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
509 category = Intent.CATEGORY_DESK_DOCK;
510 }
511 } else {
512 // Launch the standard home app if requested.
513 if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
514 category = Intent.CATEGORY_HOME;
515 }
516 }
517
518 if (LOG) {
519 Slog.v(TAG, String.format(
520 "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
521 + "category=%s",
522 action, enableFlags, disableFlags, category));
523 }
524
525 sendConfigurationAndStartDreamOrDockAppLocked(category);
526 }
527
528 private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
529 // Update the configuration but don't send it yet.
530 mHoldingConfiguration = false;
531 updateConfigurationLocked();
532
533 // Start the dock app, if there is one.
534 boolean dockAppStarted = false;
535 if (category != null) {
536 // Now we are going to be careful about switching the
537 // configuration and starting the activity -- we need to
538 // do this in a specific order under control of the
539 // activity manager, to do it cleanly. So compute the
540 // new config, but don't set it yet, and let the
541 // activity manager take care of both the start and config
542 // change.
543 Intent homeIntent = buildHomeIntent(category);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800544 if (Sandman.shouldStartDockApp(getContext(), homeIntent)) {
Jeff Brown11159e92012-10-11 15:58:37 -0700545 try {
546 int result = ActivityManagerNative.getDefault().startActivityWithConfig(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800547 null, null, homeIntent, null, null, null, 0, 0,
Jeff Brown11159e92012-10-11 15:58:37 -0700548 mConfiguration, null, UserHandle.USER_CURRENT);
549 if (result >= ActivityManager.START_SUCCESS) {
550 dockAppStarted = true;
551 } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
552 Slog.e(TAG, "Could not start dock app: " + homeIntent
553 + ", startActivityWithConfig result " + result);
554 }
555 } catch (RemoteException ex) {
556 Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
Jeff Brown62c82e42012-09-26 01:30:41 -0700557 }
Jeff Brown62c82e42012-09-26 01:30:41 -0700558 }
559 }
560
561 // Send the new configuration.
562 sendConfigurationLocked();
563
564 // If we did not start a dock app, then start dreaming if supported.
Jeff Brown11159e92012-10-11 15:58:37 -0700565 if (category != null && !dockAppStarted) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800566 Sandman.startDreamWhenDockedIfAppropriate(getContext());
Jeff Brown62c82e42012-09-26 01:30:41 -0700567 }
568 }
569
Dianne Hackborn7299c412010-03-04 18:41:49 -0800570 private void adjustStatusBarCarModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800571 final Context context = getContext();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800572 if (mStatusBarManager == null) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700573 mStatusBarManager = (StatusBarManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800574 context.getSystemService(Context.STATUS_BAR_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800575 }
576
Joe Onorato089de882010-04-12 08:18:45 -0700577 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800578 // features of the status bar; these are ORed together to form the
579 // active disabled list. So if (for example) the device is locked and
580 // the status bar should be totally disabled, the calls below will
581 // have no effect until the device is unlocked.
582 if (mStatusBarManager != null) {
583 mStatusBarManager.disable(mCarModeEnabled
584 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
585 : StatusBarManager.DISABLE_NONE);
586 }
587
588 if (mNotificationManager == null) {
589 mNotificationManager = (NotificationManager)
Adam Lesinski182f73f2013-12-05 16:48:06 -0800590 context.getSystemService(Context.NOTIFICATION_SERVICE);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800591 }
592
593 if (mNotificationManager != null) {
594 if (mCarModeEnabled) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800595 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800596
597 Notification n = new Notification();
598 n.icon = R.drawable.stat_notify_car_mode;
599 n.defaults = Notification.DEFAULT_LIGHTS;
600 n.flags = Notification.FLAG_ONGOING_EVENT;
601 n.when = 0;
602 n.setLatestEventInfo(
Adam Lesinski182f73f2013-12-05 16:48:06 -0800603 context,
604 context.getString(R.string.car_mode_disable_notification_title),
605 context.getString(R.string.car_mode_disable_notification_message),
606 PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700607 null, UserHandle.CURRENT));
608 mNotificationManager.notifyAsUser(null,
609 R.string.car_mode_disable_notification_title, n, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800610 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700611 mNotificationManager.cancelAsUser(null,
612 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800613 }
614 }
615 }
616
Adam Lesinski182f73f2013-12-05 16:48:06 -0800617 void updateTwilight() {
Jeff Brown2416e092012-08-21 22:12:20 -0700618 synchronized (mLock) {
Jeff Brown487bb6e2012-10-11 13:35:42 -0700619 if (isDoingNightModeLocked() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700620 updateComputedNightModeLocked();
621 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800622 }
623 }
Jeff Brown2416e092012-08-21 22:12:20 -0700624 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800625
Jeff Brown2416e092012-08-21 22:12:20 -0700626 private void updateComputedNightModeLocked() {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800627 TwilightState state = mTwilightManager.getCurrentState();
Jeff Brown2416e092012-08-21 22:12:20 -0700628 if (state != null) {
629 mComputedNightMode = state.isNight();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800630 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800631 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100632
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100633
Dianne Hackborn7299c412010-03-04 18:41:49 -0800634}