blob: 85a6e4150f296037d371f7634789948d84e56494 [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;
20import android.app.ActivityManagerNative;
Dianne Hackborn7299c412010-03-04 18:41:49 -080021import android.app.IUiModeManager;
22import android.app.Notification;
23import android.app.NotificationManager;
24import android.app.PendingIntent;
25import android.app.StatusBarManager;
26import android.app.UiModeManager;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -070027import android.content.ActivityNotFoundException;
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;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050037import android.os.PowerManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080038import android.os.RemoteException;
39import android.os.ServiceManager;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070040import android.os.UserHandle;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070041import android.provider.Settings;
Dianne Hackborn7299c412010-03-04 18:41:49 -080042import android.util.Slog;
43
44import java.io.FileDescriptor;
45import java.io.PrintWriter;
46
47import com.android.internal.R;
48import com.android.internal.app.DisableCarModeActivity;
Jeff Brown2416e092012-08-21 22:12:20 -070049import com.android.server.TwilightService.TwilightState;
Dianne Hackborn7299c412010-03-04 18:41:49 -080050
51class UiModeManagerService extends IUiModeManager.Stub {
52 private static final String TAG = UiModeManager.class.getSimpleName();
53 private static final boolean LOG = false;
54
Daniel Sandler11ddf532011-11-16 11:10:03 -080055 // Enable launching of applications when entering the dock.
56 private static final boolean ENABLE_LAUNCH_CAR_DOCK_APP = true;
John Spurlock960779d2012-05-29 14:37:05 -040057 private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
Daniel Sandler11ddf532011-11-16 11:10:03 -080058
Dianne Hackborn7299c412010-03-04 18:41:49 -080059 private final Context mContext;
Jeff Brown2416e092012-08-21 22:12:20 -070060 private final TwilightService mTwilightService;
61 private final Handler mHandler = new Handler();
Dianne Hackborn7299c412010-03-04 18:41:49 -080062
63 final Object mLock = new Object();
Bernd Holzheyba9ab182010-03-12 09:30:29 +010064
Dianne Hackborn7299c412010-03-04 18:41:49 -080065 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
66 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010067
Dianne Hackborn7299c412010-03-04 18:41:49 -080068 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
69 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050070 private boolean mCharging = false;
Joe Onorato44fcb832011-12-14 20:59:30 -080071 private final int mDefaultUiModeType;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050072 private final boolean mCarModeKeepsScreenOn;
73 private final boolean mDeskModeKeepsScreenOn;
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -070074 private final boolean mTelevision;
Dianne Hackborn7299c412010-03-04 18:41:49 -080075
76 private boolean mComputedNightMode;
77 private int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080078 private int mSetUiMode = 0;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010079
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080080 private boolean mHoldingConfiguration = false;
Dianne Hackborn7299c412010-03-04 18:41:49 -080081 private Configuration mConfiguration = new Configuration();
Bernd Holzheyba9ab182010-03-12 09:30:29 +010082
Dianne Hackborn7299c412010-03-04 18:41:49 -080083 private boolean mSystemReady;
84
85 private NotificationManager mNotificationManager;
86
Dianne Hackborn7299c412010-03-04 18:41:49 -080087 private StatusBarManager mStatusBarManager;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050088 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -080089
Dianne Hackbornf5c5d222010-04-09 13:14:48 -070090 static Intent buildHomeIntent(String category) {
91 Intent intent = new Intent(Intent.ACTION_MAIN);
92 intent.addCategory(category);
93 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
94 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
95 return intent;
96 }
John Spurlock960779d2012-05-29 14:37:05 -040097
Dianne Hackborn7299c412010-03-04 18:41:49 -080098 // The broadcast receiver which receives the result of the ordered broadcast sent when
99 // the dock state changes. The original ordered broadcast is sent with an initial result
100 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
101 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
102 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
103 @Override
104 public void onReceive(Context context, Intent intent) {
105 if (getResultCode() != Activity.RESULT_OK) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400106 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400107 Slog.v(TAG, "Handling broadcast result for action " + intent.getAction()
Daniel Sandler69a1da42011-11-04 15:08:30 -0400108 + ": canceled: " + getResultCode());
109 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800110 return;
111 }
112
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700113 final int enableFlags = intent.getIntExtra("enableFlags", 0);
114 final int disableFlags = intent.getIntExtra("disableFlags", 0);
John Spurlock960779d2012-05-29 14:37:05 -0400115
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800116 synchronized (mLock) {
117 // Launch a dock activity
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700118 String category = null;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800119 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700120 // Only launch car home when car mode is enabled and the caller
121 // has asked us to switch to it.
Daniel Sandler11ddf532011-11-16 11:10:03 -0800122 if (ENABLE_LAUNCH_CAR_DOCK_APP
123 && (enableFlags&UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700124 category = Intent.CATEGORY_CAR_DOCK;
125 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800126 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(intent.getAction())) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700127 // Only launch car home when desk mode is enabled and the caller
128 // has asked us to switch to it. Currently re-using the car
129 // mode flag since we don't have a formal API for "desk mode".
Daniel Sandler11ddf532011-11-16 11:10:03 -0800130 if (ENABLE_LAUNCH_DESK_DOCK_APP
131 && (enableFlags&UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700132 category = Intent.CATEGORY_DESK_DOCK;
133 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800134 } else {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700135 // Launch the standard home app if requested.
136 if ((disableFlags&UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
137 category = Intent.CATEGORY_HOME;
138 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800139 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400140
141 if (LOG) {
142 Slog.v(TAG, String.format(
John Spurlock960779d2012-05-29 14:37:05 -0400143 "Handling broadcast result for action %s: enable=0x%08x disable=0x%08x category=%s",
Daniel Sandler69a1da42011-11-04 15:08:30 -0400144 intent.getAction(), enableFlags, disableFlags, category));
145 }
John Spurlock960779d2012-05-29 14:37:05 -0400146
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800147 if (category != null) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700148 // This is the new activity that will serve as home while
149 // we are in care mode.
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700150 Intent homeIntent = buildHomeIntent(category);
John Spurlock960779d2012-05-29 14:37:05 -0400151
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700152 // Now we are going to be careful about switching the
153 // configuration and starting the activity -- we need to
154 // do this in a specific order under control of the
155 // activity manager, to do it cleanly. So compute the
156 // new config, but don't set it yet, and let the
157 // activity manager take care of both the start and config
158 // change.
159 Configuration newConfig = null;
160 if (mHoldingConfiguration) {
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700161 mHoldingConfiguration = false;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700162 updateConfigurationLocked(false);
163 newConfig = mConfiguration;
164 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800165 try {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700166 ActivityManagerNative.getDefault().startActivityWithConfig(
Dianne Hackborna4972e92012-03-14 10:38:05 -0700167 null, homeIntent, null, null, null, 0, 0,
Dianne Hackborn41203752012-08-31 14:05:51 -0700168 newConfig, null, UserHandle.USER_CURRENT);
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700169 mHoldingConfiguration = false;
170 } catch (RemoteException e) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800171 Slog.w(TAG, e.getCause());
172 }
173 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100174
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800175 if (mHoldingConfiguration) {
176 mHoldingConfiguration = false;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700177 updateConfigurationLocked(true);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800178 }
179 }
180 }
181 };
182
Dianne Hackborn7299c412010-03-04 18:41:49 -0800183 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
184 @Override
185 public void onReceive(Context context, Intent intent) {
186 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
187 Intent.EXTRA_DOCK_STATE_UNDOCKED);
188 updateDockState(state);
189 }
190 };
191
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500192 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
193 @Override
194 public void onReceive(Context context, Intent intent) {
195 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
196 synchronized (mLock) {
197 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700198 updateLocked(0, 0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500199 }
200 }
201 }
202 };
203
Jeff Brown2416e092012-08-21 22:12:20 -0700204 private final TwilightService.TwilightListener mTwilightListener =
205 new TwilightService.TwilightListener() {
Dianne Hackborn57f45032010-06-17 15:49:33 -0700206 @Override
Jeff Brown2416e092012-08-21 22:12:20 -0700207 public void onTwilightStateChanged() {
208 updateTwilight();
Dianne Hackborn57f45032010-06-17 15:49:33 -0700209 }
210 };
211
Jeff Brown2416e092012-08-21 22:12:20 -0700212 public UiModeManagerService(Context context, TwilightService twilight) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800213 mContext = context;
Jeff Brown2416e092012-08-21 22:12:20 -0700214 mTwilightService = twilight;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800215
216 ServiceManager.addService(Context.UI_MODE_SERVICE, this);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100217
Dianne Hackborn7299c412010-03-04 18:41:49 -0800218 mContext.registerReceiver(mDockModeReceiver,
219 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500220 mContext.registerReceiver(mBatteryReceiver,
221 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
222
223 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
224 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800225
226 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500227
Joe Onorato44fcb832011-12-14 20:59:30 -0800228 mDefaultUiModeType = context.getResources().getInteger(
229 com.android.internal.R.integer.config_defaultUiModeType);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500230 mCarModeKeepsScreenOn = (context.getResources().getInteger(
231 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
232 mDeskModeKeepsScreenOn = (context.getResources().getInteger(
233 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700234 mTelevision = context.getPackageManager().hasSystemFeature(
235 PackageManager.FEATURE_TELEVISION);
236
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700237 mNightMode = Settings.Secure.getInt(mContext.getContentResolver(),
238 Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
Jeff Brown2416e092012-08-21 22:12:20 -0700239
240 mTwilightService.registerListener(mTwilightListener, mHandler);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800241 }
242
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700243 public void disableCarMode(int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800244 synchronized (mLock) {
245 setCarModeLocked(false);
Mike Lockwood924e1642010-03-05 11:56:53 -0500246 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700247 updateLocked(0, flags);
Mike Lockwood924e1642010-03-05 11:56:53 -0500248 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800249 }
250 }
251
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700252 public void enableCarMode(int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800253 synchronized (mLock) {
254 setCarModeLocked(true);
Mike Lockwood924e1642010-03-05 11:56:53 -0500255 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700256 updateLocked(flags, 0);
Mike Lockwood924e1642010-03-05 11:56:53 -0500257 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800258 }
259 }
260
261 public int getCurrentModeType() {
262 synchronized (mLock) {
263 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
264 }
265 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100266
Dianne Hackborn7299c412010-03-04 18:41:49 -0800267 public void setNightMode(int mode) throws RemoteException {
268 synchronized (mLock) {
269 switch (mode) {
270 case UiModeManager.MODE_NIGHT_NO:
271 case UiModeManager.MODE_NIGHT_YES:
272 case UiModeManager.MODE_NIGHT_AUTO:
273 break;
274 default:
275 throw new IllegalArgumentException("Unknown mode: " + mode);
276 }
277 if (!isDoingNightMode()) {
278 return;
279 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100280
Dianne Hackborn7299c412010-03-04 18:41:49 -0800281 if (mNightMode != mode) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700282 long ident = Binder.clearCallingIdentity();
283 Settings.Secure.putInt(mContext.getContentResolver(),
284 Settings.Secure.UI_NIGHT_MODE, mode);
285 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800286 mNightMode = mode;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700287 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800288 }
289 }
290 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100291
Dianne Hackborn7299c412010-03-04 18:41:49 -0800292 public int getNightMode() throws RemoteException {
293 return mNightMode;
294 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100295
Dianne Hackborn7299c412010-03-04 18:41:49 -0800296 void systemReady() {
297 synchronized (mLock) {
298 mSystemReady = true;
299 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
Jeff Brown2416e092012-08-21 22:12:20 -0700300 updateComputedNightModeLocked();
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700301 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800302 }
303 }
304
305 boolean isDoingNightMode() {
306 return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
307 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100308
Dianne Hackborn7299c412010-03-04 18:41:49 -0800309 void setCarModeLocked(boolean enabled) {
310 if (mCarModeEnabled != enabled) {
311 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800312 }
313 }
314
315 void updateDockState(int newState) {
316 synchronized (mLock) {
317 if (newState != mDockState) {
318 mDockState = newState;
Mike Lockwood924e1642010-03-05 11:56:53 -0500319 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800320 if (mSystemReady) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700321 updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800322 }
323 }
324 }
325 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500326
Daniel Sandler69a1da42011-11-04 15:08:30 -0400327 final static boolean isDeskDockState(int state) {
328 switch (state) {
329 case Intent.EXTRA_DOCK_STATE_DESK:
330 case Intent.EXTRA_DOCK_STATE_LE_DESK:
331 case Intent.EXTRA_DOCK_STATE_HE_DESK:
332 return true;
333 default:
334 return false;
335 }
336 }
337
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700338 final void updateConfigurationLocked(boolean sendIt) {
Dianne Hackborn0cf2c8a2012-05-17 17:29:49 -0700339 int uiMode = mTelevision ? Configuration.UI_MODE_TYPE_TELEVISION
340 : mDefaultUiModeType;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800341 if (mCarModeEnabled) {
342 uiMode = Configuration.UI_MODE_TYPE_CAR;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400343 } else if (isDeskDockState(mDockState)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800344 uiMode = Configuration.UI_MODE_TYPE_DESK;
345 }
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700346 if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800347 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
Jeff Brown2416e092012-08-21 22:12:20 -0700348 updateComputedNightModeLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800349 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
350 : Configuration.UI_MODE_NIGHT_NO;
351 } else {
352 uiMode |= mNightMode << 4;
353 }
354 } else {
355 // Disabling the car mode clears the night mode.
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400356 uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO;
357 }
358
359 if (LOG) {
John Spurlock960779d2012-05-29 14:37:05 -0400360 Slog.d(TAG,
361 "updateConfigurationLocked: mDockState=" + mDockState
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400362 + "; mCarMode=" + mCarModeEnabled
363 + "; mNightMode=" + mNightMode
364 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800365 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100366
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800367 mCurUiMode = uiMode;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100368
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800369 if (!mHoldingConfiguration && uiMode != mSetUiMode) {
370 mSetUiMode = uiMode;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700371 mConfiguration.uiMode = uiMode;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100372
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700373 if (sendIt) {
374 try {
375 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
376 } catch (RemoteException e) {
377 Slog.w(TAG, "Failure communicating with activity manager", e);
378 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800379 }
380 }
381 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100382
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700383 final void updateLocked(int enableFlags, int disableFlags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800384 long ident = Binder.clearCallingIdentity();
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100385
Dianne Hackborn7299c412010-03-04 18:41:49 -0800386 try {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800387 String action = null;
388 String oldAction = null;
389 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100390 adjustStatusBarCarModeLocked();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800391 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
Daniel Sandler69a1da42011-11-04 15:08:30 -0400392 } else if (isDeskDockState(mLastBroadcastState)) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800393 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
394 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100395
Dianne Hackborn7299c412010-03-04 18:41:49 -0800396 if (mCarModeEnabled) {
397 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
398 adjustStatusBarCarModeLocked();
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100399
Dianne Hackborn7299c412010-03-04 18:41:49 -0800400 if (oldAction != null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700401 mContext.sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800402 }
403 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
404 action = UiModeManager.ACTION_ENTER_CAR_MODE;
405 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400406 } else if (isDeskDockState(mDockState)) {
407 if (!isDeskDockState(mLastBroadcastState)) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800408 if (oldAction != null) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700409 mContext.sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800410 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400411 mLastBroadcastState = mDockState;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800412 action = UiModeManager.ACTION_ENTER_DESK_MODE;
413 }
414 } else {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800415 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
416 action = oldAction;
417 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100418
Dianne Hackborn7299c412010-03-04 18:41:49 -0800419 if (action != null) {
Daniel Sandler69a1da42011-11-04 15:08:30 -0400420 if (LOG) {
421 Slog.v(TAG, String.format(
422 "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x",
423 action, enableFlags, disableFlags));
424 }
425
Dianne Hackborn7299c412010-03-04 18:41:49 -0800426 // Send the ordered broadcast; the result receiver will receive after all
427 // broadcasts have been sent. If any broadcast receiver changes the result
428 // code from the initial value of RESULT_OK, then the result receiver will
429 // not launch the corresponding dock application. This gives apps a chance
430 // to override the behavior and stay in their app even when the device is
431 // placed into a dock.
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700432 Intent intent = new Intent(action);
433 intent.putExtra("enableFlags", enableFlags);
434 intent.putExtra("disableFlags", disableFlags);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700435 mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
Dianne Hackborn7299c412010-03-04 18:41:49 -0800436 mResultReceiver, null, Activity.RESULT_OK, null, null);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800437 // Attempting to make this transition a little more clean, we are going
438 // to hold off on doing a configuration change until we have finished
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700439 // the broadcast and started the home activity.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800440 mHoldingConfiguration = true;
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700441 } else {
442 Intent homeIntent = null;
443 if (mCarModeEnabled) {
Daniel Sandler11ddf532011-11-16 11:10:03 -0800444 if (ENABLE_LAUNCH_CAR_DOCK_APP
445 && (enableFlags&UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700446 homeIntent = buildHomeIntent(Intent.CATEGORY_CAR_DOCK);
447 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400448 } else if (isDeskDockState(mDockState)) {
Daniel Sandler11ddf532011-11-16 11:10:03 -0800449 if (ENABLE_LAUNCH_DESK_DOCK_APP
450 && (enableFlags&UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700451 homeIntent = buildHomeIntent(Intent.CATEGORY_DESK_DOCK);
452 }
453 } else {
454 if ((disableFlags&UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
455 homeIntent = buildHomeIntent(Intent.CATEGORY_HOME);
456 }
457 }
Daniel Sandler69a1da42011-11-04 15:08:30 -0400458
459 if (LOG) {
460 Slog.v(TAG, "updateLocked: null action, mDockState="
461 + mDockState +", firing homeIntent: " + homeIntent);
462 }
463
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700464 if (homeIntent != null) {
465 try {
Dianne Hackbornd8883992012-09-07 15:58:52 -0700466 mContext.startActivityAsUser(homeIntent, UserHandle.CURRENT);
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700467 } catch (ActivityNotFoundException e) {
468 }
469 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800470 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100471
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700472 updateConfigurationLocked(true);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500473
474 // keep screen on when charging and in car mode
475 boolean keepScreenOn = mCharging &&
476 ((mCarModeEnabled && mCarModeKeepsScreenOn) ||
477 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
478 if (keepScreenOn != mWakeLock.isHeld()) {
479 if (keepScreenOn) {
480 mWakeLock.acquire();
481 } else {
482 mWakeLock.release();
483 }
484 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800485 } finally {
486 Binder.restoreCallingIdentity(ident);
487 }
488 }
489
490 private void adjustStatusBarCarModeLocked() {
491 if (mStatusBarManager == null) {
492 mStatusBarManager = (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE);
493 }
494
Joe Onorato089de882010-04-12 08:18:45 -0700495 // Fear not: StatusBarManagerService manages a list of requests to disable
Dianne Hackborn7299c412010-03-04 18:41:49 -0800496 // features of the status bar; these are ORed together to form the
497 // active disabled list. So if (for example) the device is locked and
498 // the status bar should be totally disabled, the calls below will
499 // have no effect until the device is unlocked.
500 if (mStatusBarManager != null) {
501 mStatusBarManager.disable(mCarModeEnabled
502 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
503 : StatusBarManager.DISABLE_NONE);
504 }
505
506 if (mNotificationManager == null) {
507 mNotificationManager = (NotificationManager)
508 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
509 }
510
511 if (mNotificationManager != null) {
512 if (mCarModeEnabled) {
513 Intent carModeOffIntent = new Intent(mContext, DisableCarModeActivity.class);
514
515 Notification n = new Notification();
516 n.icon = R.drawable.stat_notify_car_mode;
517 n.defaults = Notification.DEFAULT_LIGHTS;
518 n.flags = Notification.FLAG_ONGOING_EVENT;
519 n.when = 0;
520 n.setLatestEventInfo(
521 mContext,
522 mContext.getString(R.string.car_mode_disable_notification_title),
523 mContext.getString(R.string.car_mode_disable_notification_message),
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700524 PendingIntent.getActivityAsUser(mContext, 0, carModeOffIntent, 0,
525 null, UserHandle.CURRENT));
526 mNotificationManager.notifyAsUser(null,
527 R.string.car_mode_disable_notification_title, n, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800528 } else {
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700529 mNotificationManager.cancelAsUser(null,
530 R.string.car_mode_disable_notification_title, UserHandle.ALL);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800531 }
532 }
533 }
534
Jeff Brown2416e092012-08-21 22:12:20 -0700535 private void updateTwilight() {
536 synchronized (mLock) {
537 if (isDoingNightMode() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
538 updateComputedNightModeLocked();
539 updateLocked(0, 0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800540 }
541 }
Jeff Brown2416e092012-08-21 22:12:20 -0700542 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800543
Jeff Brown2416e092012-08-21 22:12:20 -0700544 private void updateComputedNightModeLocked() {
545 TwilightState state = mTwilightService.getCurrentState();
546 if (state != null) {
547 mComputedNightMode = state.isNight();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800548 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800549 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100550
Dianne Hackborn7299c412010-03-04 18:41:49 -0800551 @Override
552 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
553 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
554 != PackageManager.PERMISSION_GRANTED) {
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100555
Dianne Hackborn7299c412010-03-04 18:41:49 -0800556 pw.println("Permission Denial: can't dump uimode service from from pid="
557 + Binder.getCallingPid()
558 + ", uid=" + Binder.getCallingUid());
559 return;
560 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100561
Dianne Hackborn7299c412010-03-04 18:41:49 -0800562 synchronized (mLock) {
563 pw.println("Current UI Mode Service state:");
564 pw.print(" mDockState="); pw.print(mDockState);
565 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
566 pw.print(" mNightMode="); pw.print(mNightMode);
567 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
568 pw.print(" mComputedNightMode="); pw.println(mComputedNightMode);
569 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800570 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
571 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800572 pw.print(" mSystemReady="); pw.println(mSystemReady);
Jeff Brown2416e092012-08-21 22:12:20 -0700573 pw.print(" mTwilightService.getCurrentState()=");
574 pw.println(mTwilightService.getCurrentState());
Dianne Hackborn7299c412010-03-04 18:41:49 -0800575 }
576 }
577}