blob: d6a42f67041ac420de929c4ec12fcb64ba85192b [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;
21import android.app.AlarmManager;
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;
34import android.location.Criteria;
35import android.location.Location;
36import android.location.LocationListener;
37import android.location.LocationManager;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050038import android.os.BatteryManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080039import android.os.Binder;
40import android.os.Bundle;
41import android.os.Handler;
42import android.os.Message;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050043import android.os.PowerManager;
Dianne Hackborn7299c412010-03-04 18:41:49 -080044import android.os.RemoteException;
45import android.os.ServiceManager;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -070046import android.provider.Settings;
Dianne Hackborn7299c412010-03-04 18:41:49 -080047import android.text.format.DateUtils;
48import android.text.format.Time;
49import android.util.Slog;
50
51import java.io.FileDescriptor;
52import java.io.PrintWriter;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010053import java.util.Iterator;
Dianne Hackborn7299c412010-03-04 18:41:49 -080054
55import com.android.internal.R;
56import com.android.internal.app.DisableCarModeActivity;
57
58class UiModeManagerService extends IUiModeManager.Stub {
59 private static final String TAG = UiModeManager.class.getSimpleName();
60 private static final boolean LOG = false;
61
62 private static final String KEY_LAST_UPDATE_INTERVAL = "LAST_UPDATE_INTERVAL";
63
64 private static final int MSG_UPDATE_TWILIGHT = 0;
65 private static final int MSG_ENABLE_LOCATION_UPDATES = 1;
66
67 private static final long LOCATION_UPDATE_MS = 30 * DateUtils.MINUTE_IN_MILLIS;
68 private static final float LOCATION_UPDATE_DISTANCE_METER = 1000 * 20;
69 private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MIN = 5000;
70 private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MAX = 5 * DateUtils.MINUTE_IN_MILLIS;
71 private static final double FACTOR_GMT_OFFSET_LONGITUDE = 1000.0 * 360.0 / DateUtils.DAY_IN_MILLIS;
72
73 private static final String ACTION_UPDATE_NIGHT_MODE = "com.android.server.action.UPDATE_NIGHT_MODE";
74
75 private final Context mContext;
76
77 final Object mLock = new Object();
Bernd Holzheyba9ab182010-03-12 09:30:29 +010078
Dianne Hackborn7299c412010-03-04 18:41:49 -080079 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
80 private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010081
Dianne Hackborn7299c412010-03-04 18:41:49 -080082 private int mNightMode = UiModeManager.MODE_NIGHT_NO;
83 private boolean mCarModeEnabled = false;
Mike Lockwoode29db6a2010-03-05 13:45:51 -050084 private boolean mCharging = false;
85 private final boolean mCarModeKeepsScreenOn;
86 private final boolean mDeskModeKeepsScreenOn;
Dianne Hackborn7299c412010-03-04 18:41:49 -080087
88 private boolean mComputedNightMode;
89 private int mCurUiMode = 0;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080090 private int mSetUiMode = 0;
Bernd Holzheyba9ab182010-03-12 09:30:29 +010091
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080092 private boolean mHoldingConfiguration = false;
Dianne Hackborn7299c412010-03-04 18:41:49 -080093 private Configuration mConfiguration = new Configuration();
Bernd Holzheyba9ab182010-03-12 09:30:29 +010094
Dianne Hackborn7299c412010-03-04 18:41:49 -080095 private boolean mSystemReady;
96
97 private NotificationManager mNotificationManager;
98
99 private AlarmManager mAlarmManager;
100
101 private LocationManager mLocationManager;
102 private Location mLocation;
103 private StatusBarManager mStatusBarManager;
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500104 private final PowerManager.WakeLock mWakeLock;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800105
106 // The broadcast receiver which receives the result of the ordered broadcast sent when
107 // the dock state changes. The original ordered broadcast is sent with an initial result
108 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
109 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
110 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
111 @Override
112 public void onReceive(Context context, Intent intent) {
113 if (getResultCode() != Activity.RESULT_OK) {
114 return;
115 }
116
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800117 synchronized (mLock) {
118 // Launch a dock activity
119 String category;
120 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) {
121 // Only launch car home when car mode is enabled.
122 category = Intent.CATEGORY_CAR_DOCK;
123 } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(intent.getAction())) {
124 category = Intent.CATEGORY_DESK_DOCK;
125 } else {
126 category = null;
127 }
128 if (category != null) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700129 // This is the new activity that will serve as home while
130 // we are in care mode.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800131 intent = new Intent(Intent.ACTION_MAIN);
132 intent.addCategory(category);
133 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
134 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700135
136 // Now we are going to be careful about switching the
137 // configuration and starting the activity -- we need to
138 // do this in a specific order under control of the
139 // activity manager, to do it cleanly. So compute the
140 // new config, but don't set it yet, and let the
141 // activity manager take care of both the start and config
142 // change.
143 Configuration newConfig = null;
144 if (mHoldingConfiguration) {
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700145 mHoldingConfiguration = false;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700146 updateConfigurationLocked(false);
147 newConfig = mConfiguration;
148 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800149 try {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700150 ActivityManagerNative.getDefault().startActivityWithConfig(
151 null, intent, null, null, 0, null, null, 0, false, false,
152 newConfig);
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700153 mHoldingConfiguration = false;
154 } catch (RemoteException e) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800155 Slog.w(TAG, e.getCause());
156 }
157 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100158
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800159 if (mHoldingConfiguration) {
160 mHoldingConfiguration = false;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700161 updateConfigurationLocked(true);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800162 }
163 }
164 }
165 };
166
167 private final BroadcastReceiver mTwilightUpdateReceiver = new BroadcastReceiver() {
168 @Override
169 public void onReceive(Context context, Intent intent) {
170 if (isDoingNightMode() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
171 mHandler.sendEmptyMessage(MSG_UPDATE_TWILIGHT);
172 }
173 }
174 };
175
176 private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() {
177 @Override
178 public void onReceive(Context context, Intent intent) {
179 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
180 Intent.EXTRA_DOCK_STATE_UNDOCKED);
181 updateDockState(state);
182 }
183 };
184
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500185 private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
186 @Override
187 public void onReceive(Context context, Intent intent) {
188 mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
189 synchronized (mLock) {
190 if (mSystemReady) {
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700191 updateLocked(0);
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500192 }
193 }
194 }
195 };
196
Dianne Hackborn7299c412010-03-04 18:41:49 -0800197 // A LocationListener to initialize the network location provider. The location updates
198 // are handled through the passive location provider.
199 private final LocationListener mEmptyLocationListener = new LocationListener() {
200 public void onLocationChanged(Location location) {
201 }
202
203 public void onProviderDisabled(String provider) {
204 }
205
206 public void onProviderEnabled(String provider) {
207 }
208
209 public void onStatusChanged(String provider, int status, Bundle extras) {
210 }
211 };
212
213 private final LocationListener mLocationListener = new LocationListener() {
214
215 public void onLocationChanged(Location location) {
216 final boolean hasMoved = hasMoved(location);
217 final boolean hasBetterAccuracy = mLocation == null
218 || location.getAccuracy() < mLocation.getAccuracy();
219 if (hasMoved || hasBetterAccuracy) {
220 synchronized (mLock) {
221 mLocation = location;
222 if (hasMoved && isDoingNightMode()
223 && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
224 mHandler.sendEmptyMessage(MSG_UPDATE_TWILIGHT);
225 }
226 }
227 }
228 }
229
230 public void onProviderDisabled(String provider) {
231 }
232
233 public void onProviderEnabled(String provider) {
234 }
235
236 public void onStatusChanged(String provider, int status, Bundle extras) {
237 }
238
239 /*
240 * The user has moved if the accuracy circles of the two locations
241 * don't overlap.
242 */
243 private boolean hasMoved(Location location) {
244 if (location == null) {
245 return false;
246 }
247 if (mLocation == null) {
248 return true;
249 }
250
251 /* if new location is older than the current one, the devices hasn't
252 * moved.
253 */
254 if (location.getTime() < mLocation.getTime()) {
255 return false;
256 }
257
258 /* Get the distance between the two points */
259 float distance = mLocation.distanceTo(location);
260
261 /* Get the total accuracy radius for both locations */
262 float totalAccuracy = mLocation.getAccuracy() + location.getAccuracy();
263
264 /* If the distance is greater than the combined accuracy of the two
265 * points then they can't overlap and hence the user has moved.
266 */
267 return distance >= totalAccuracy;
268 }
269 };
270
271 public UiModeManagerService(Context context) {
272 mContext = context;
273
274 ServiceManager.addService(Context.UI_MODE_SERVICE, this);
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100275
Dianne Hackborn7299c412010-03-04 18:41:49 -0800276 mAlarmManager =
277 (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
278 mLocationManager =
279 (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
280 mContext.registerReceiver(mTwilightUpdateReceiver,
281 new IntentFilter(ACTION_UPDATE_NIGHT_MODE));
282 mContext.registerReceiver(mDockModeReceiver,
283 new IntentFilter(Intent.ACTION_DOCK_EVENT));
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500284 mContext.registerReceiver(mBatteryReceiver,
285 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
286
287 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
288 mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800289
290 mConfiguration.setToDefaults();
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500291
292 mCarModeKeepsScreenOn = (context.getResources().getInteger(
293 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
294 mDeskModeKeepsScreenOn = (context.getResources().getInteger(
295 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700296
297 mNightMode = Settings.Secure.getInt(mContext.getContentResolver(),
298 Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800299 }
300
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700301 public void disableCarMode(int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800302 synchronized (mLock) {
303 setCarModeLocked(false);
Mike Lockwood924e1642010-03-05 11:56:53 -0500304 if (mSystemReady) {
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700305 updateLocked(flags);
Mike Lockwood924e1642010-03-05 11:56:53 -0500306 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800307 }
308 }
309
310 public void enableCarMode() {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800311 synchronized (mLock) {
312 setCarModeLocked(true);
Mike Lockwood924e1642010-03-05 11:56:53 -0500313 if (mSystemReady) {
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700314 updateLocked(0);
Mike Lockwood924e1642010-03-05 11:56:53 -0500315 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800316 }
317 }
318
319 public int getCurrentModeType() {
320 synchronized (mLock) {
321 return mCurUiMode & Configuration.UI_MODE_TYPE_MASK;
322 }
323 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100324
Dianne Hackborn7299c412010-03-04 18:41:49 -0800325 public void setNightMode(int mode) throws RemoteException {
326 synchronized (mLock) {
327 switch (mode) {
328 case UiModeManager.MODE_NIGHT_NO:
329 case UiModeManager.MODE_NIGHT_YES:
330 case UiModeManager.MODE_NIGHT_AUTO:
331 break;
332 default:
333 throw new IllegalArgumentException("Unknown mode: " + mode);
334 }
335 if (!isDoingNightMode()) {
336 return;
337 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100338
Dianne Hackborn7299c412010-03-04 18:41:49 -0800339 if (mNightMode != mode) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700340 long ident = Binder.clearCallingIdentity();
341 Settings.Secure.putInt(mContext.getContentResolver(),
342 Settings.Secure.UI_NIGHT_MODE, mode);
343 Binder.restoreCallingIdentity(ident);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800344 mNightMode = mode;
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700345 updateLocked(0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800346 }
347 }
348 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100349
Dianne Hackborn7299c412010-03-04 18:41:49 -0800350 public int getNightMode() throws RemoteException {
351 return mNightMode;
352 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100353
Dianne Hackborn7299c412010-03-04 18:41:49 -0800354 void systemReady() {
355 synchronized (mLock) {
356 mSystemReady = true;
357 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700358 updateLocked(0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800359 mHandler.sendEmptyMessage(MSG_ENABLE_LOCATION_UPDATES);
360 }
361 }
362
363 boolean isDoingNightMode() {
364 return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
365 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100366
Dianne Hackborn7299c412010-03-04 18:41:49 -0800367 void setCarModeLocked(boolean enabled) {
368 if (mCarModeEnabled != enabled) {
369 mCarModeEnabled = enabled;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800370 }
371 }
372
373 void updateDockState(int newState) {
374 synchronized (mLock) {
375 if (newState != mDockState) {
376 mDockState = newState;
Mike Lockwood924e1642010-03-05 11:56:53 -0500377 setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800378 if (mSystemReady) {
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700379 updateLocked(0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800380 }
381 }
382 }
383 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500384
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700385 final void updateConfigurationLocked(boolean sendIt) {
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400386 int uiMode = Configuration.UI_MODE_TYPE_NORMAL;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800387 if (mCarModeEnabled) {
388 uiMode = Configuration.UI_MODE_TYPE_CAR;
389 } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) {
390 uiMode = Configuration.UI_MODE_TYPE_DESK;
391 }
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700392 if (mCarModeEnabled) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800393 if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
394 updateTwilightLocked();
395 uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
396 : Configuration.UI_MODE_NIGHT_NO;
397 } else {
398 uiMode |= mNightMode << 4;
399 }
400 } else {
401 // Disabling the car mode clears the night mode.
Daniel Sandler8daf2a42010-04-02 10:15:09 -0400402 uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO;
403 }
404
405 if (LOG) {
406 Slog.d(TAG,
407 "updateConfigurationLocked: mDockState=" + mDockState
408 + "; mCarMode=" + mCarModeEnabled
409 + "; mNightMode=" + mNightMode
410 + "; uiMode=" + uiMode);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800411 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100412
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800413 mCurUiMode = uiMode;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100414
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800415 if (!mHoldingConfiguration && uiMode != mSetUiMode) {
416 mSetUiMode = uiMode;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700417 mConfiguration.uiMode = uiMode;
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100418
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -0700419 if (sendIt) {
420 try {
421 ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
422 } catch (RemoteException e) {
423 Slog.w(TAG, "Failure communicating with activity manager", e);
424 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800425 }
426 }
427 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100428
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700429 final void updateLocked(int flags) {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800430 long ident = Binder.clearCallingIdentity();
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100431
Dianne Hackborn7299c412010-03-04 18:41:49 -0800432 try {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800433 String action = null;
434 String oldAction = null;
435 if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
Tobias Haamel780b2602010-03-15 12:54:45 +0100436 adjustStatusBarCarModeLocked();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800437 oldAction = UiModeManager.ACTION_EXIT_CAR_MODE;
438 } else if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_DESK) {
439 oldAction = UiModeManager.ACTION_EXIT_DESK_MODE;
440 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100441
Dianne Hackborn7299c412010-03-04 18:41:49 -0800442 if (mCarModeEnabled) {
443 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
444 adjustStatusBarCarModeLocked();
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100445
Dianne Hackborn7299c412010-03-04 18:41:49 -0800446 if (oldAction != null) {
447 mContext.sendBroadcast(new Intent(oldAction));
448 }
449 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
450 action = UiModeManager.ACTION_ENTER_CAR_MODE;
451 }
452 } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) {
453 if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_DESK) {
454 if (oldAction != null) {
455 mContext.sendBroadcast(new Intent(oldAction));
456 }
457 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_DESK;
458 action = UiModeManager.ACTION_ENTER_DESK_MODE;
459 }
460 } else {
Dianne Hackborn7299c412010-03-04 18:41:49 -0800461 mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
462 action = oldAction;
463 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100464
Dianne Hackborn7299c412010-03-04 18:41:49 -0800465 if (action != null) {
466 // Send the ordered broadcast; the result receiver will receive after all
467 // broadcasts have been sent. If any broadcast receiver changes the result
468 // code from the initial value of RESULT_OK, then the result receiver will
469 // not launch the corresponding dock application. This gives apps a chance
470 // to override the behavior and stay in their app even when the device is
471 // placed into a dock.
472 mContext.sendOrderedBroadcast(new Intent(action), null,
473 mResultReceiver, null, Activity.RESULT_OK, null, null);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800474 // Attempting to make this transition a little more clean, we are going
475 // to hold off on doing a configuration change until we have finished
476 // the broacast and started the home activity.
477 mHoldingConfiguration = true;
Dianne Hackborn7299c412010-03-04 18:41:49 -0800478 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100479
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700480 if (oldAction != null && (flags&UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
481 // We are exiting the special mode, and have been asked to return
482 // to the main home screen while doing so. To keep this clean, we
483 // have the activity manager switch the configuration for us at the
484 // same time as the switch.
485 try {
486 Intent intent = new Intent(Intent.ACTION_MAIN);
487 intent.addCategory(Intent.CATEGORY_HOME);
488 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborne2522462010-03-29 18:41:30 -0700489 mHoldingConfiguration = false;
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700490 updateConfigurationLocked(false);
491 ActivityManagerNative.getDefault().startActivityWithConfig(
492 null, intent, null, null, 0, null, null, 0, false, false,
493 mConfiguration);
494 } catch (RemoteException e) {
495 Slog.w(TAG, e.getCause());
496 }
497 } else {
498 updateConfigurationLocked(true);
499 }
Mike Lockwoode29db6a2010-03-05 13:45:51 -0500500
501 // keep screen on when charging and in car mode
502 boolean keepScreenOn = mCharging &&
503 ((mCarModeEnabled && mCarModeKeepsScreenOn) ||
504 (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
505 if (keepScreenOn != mWakeLock.isHeld()) {
506 if (keepScreenOn) {
507 mWakeLock.acquire();
508 } else {
509 mWakeLock.release();
510 }
511 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800512 } finally {
513 Binder.restoreCallingIdentity(ident);
514 }
515 }
516
517 private void adjustStatusBarCarModeLocked() {
518 if (mStatusBarManager == null) {
519 mStatusBarManager = (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE);
520 }
521
522 // Fear not: StatusBarService manages a list of requests to disable
523 // features of the status bar; these are ORed together to form the
524 // active disabled list. So if (for example) the device is locked and
525 // the status bar should be totally disabled, the calls below will
526 // have no effect until the device is unlocked.
527 if (mStatusBarManager != null) {
528 mStatusBarManager.disable(mCarModeEnabled
529 ? StatusBarManager.DISABLE_NOTIFICATION_TICKER
530 : StatusBarManager.DISABLE_NONE);
531 }
532
533 if (mNotificationManager == null) {
534 mNotificationManager = (NotificationManager)
535 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
536 }
537
538 if (mNotificationManager != null) {
539 if (mCarModeEnabled) {
540 Intent carModeOffIntent = new Intent(mContext, DisableCarModeActivity.class);
541
542 Notification n = new Notification();
543 n.icon = R.drawable.stat_notify_car_mode;
544 n.defaults = Notification.DEFAULT_LIGHTS;
545 n.flags = Notification.FLAG_ONGOING_EVENT;
546 n.when = 0;
547 n.setLatestEventInfo(
548 mContext,
549 mContext.getString(R.string.car_mode_disable_notification_title),
550 mContext.getString(R.string.car_mode_disable_notification_message),
551 PendingIntent.getActivity(mContext, 0, carModeOffIntent, 0));
552 mNotificationManager.notify(0, n);
553 } else {
554 mNotificationManager.cancel(0);
555 }
556 }
557 }
558
559 private final Handler mHandler = new Handler() {
560
561 boolean mPassiveListenerEnabled;
562 boolean mNetworkListenerEnabled;
563
564 @Override
565 public void handleMessage(Message msg) {
566 switch (msg.what) {
567 case MSG_UPDATE_TWILIGHT:
568 synchronized (mLock) {
569 if (isDoingNightMode() && mLocation != null
570 && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
571 updateTwilightLocked();
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700572 updateLocked(0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800573 }
574 }
575 break;
576 case MSG_ENABLE_LOCATION_UPDATES:
577 // enable network provider to receive at least location updates for a given
578 // distance.
579 boolean networkLocationEnabled;
580 try {
581 networkLocationEnabled =
582 mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
583 } catch (Exception e) {
584 // we may get IllegalArgumentException if network location provider
585 // does not exist or is not yet installed.
586 networkLocationEnabled = false;
587 }
588 if (!mNetworkListenerEnabled && networkLocationEnabled) {
589 mNetworkListenerEnabled = true;
590 mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
591 LOCATION_UPDATE_MS, 0, mEmptyLocationListener);
592
593 if (mLocation == null) {
594 retrieveLocation();
595 }
596 synchronized (mLock) {
597 if (isDoingNightMode() && mLocation != null
598 && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
599 updateTwilightLocked();
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700600 updateLocked(0);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800601 }
602 }
603 }
604 // enable passive provider to receive updates from location fixes (gps
605 // and network).
606 boolean passiveLocationEnabled;
607 try {
608 passiveLocationEnabled =
609 mLocationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER);
610 } catch (Exception e) {
611 // we may get IllegalArgumentException if passive location provider
612 // does not exist or is not yet installed.
613 passiveLocationEnabled = false;
614 }
615 if (!mPassiveListenerEnabled && passiveLocationEnabled) {
616 mPassiveListenerEnabled = true;
617 mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,
618 0, LOCATION_UPDATE_DISTANCE_METER , mLocationListener);
619 }
620 if (!(mNetworkListenerEnabled && mPassiveListenerEnabled)) {
621 long interval = msg.getData().getLong(KEY_LAST_UPDATE_INTERVAL);
622 interval *= 1.5;
623 if (interval == 0) {
624 interval = LOCATION_UPDATE_ENABLE_INTERVAL_MIN;
625 } else if (interval > LOCATION_UPDATE_ENABLE_INTERVAL_MAX) {
626 interval = LOCATION_UPDATE_ENABLE_INTERVAL_MAX;
627 }
628 Bundle bundle = new Bundle();
629 bundle.putLong(KEY_LAST_UPDATE_INTERVAL, interval);
630 Message newMsg = mHandler.obtainMessage(MSG_ENABLE_LOCATION_UPDATES);
631 newMsg.setData(bundle);
632 mHandler.sendMessageDelayed(newMsg, interval);
633 }
634 break;
635 }
636 }
637
638 private void retrieveLocation() {
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100639 Location location = null;
640 final Iterator<String> providers =
641 mLocationManager.getProviders(new Criteria(), true).iterator();
642 while (providers.hasNext()) {
643 final Location lastKnownLocation =
644 mLocationManager.getLastKnownLocation(providers.next());
645 // pick the most recent location
646 if (location == null || (lastKnownLocation != null &&
647 location.getTime() < lastKnownLocation.getTime())) {
648 location = lastKnownLocation;
649 }
650 }
Dianne Hackborn7299c412010-03-04 18:41:49 -0800651 // In the case there is no location available (e.g. GPS fix or network location
652 // is not available yet), the longitude of the location is estimated using the timezone,
653 // latitude and accuracy are set to get a good average.
654 if (location == null) {
655 Time currentTime = new Time();
656 currentTime.set(System.currentTimeMillis());
657 double lngOffset = FACTOR_GMT_OFFSET_LONGITUDE *
658 (currentTime.gmtoff - (currentTime.isDst > 0 ? 3600 : 0));
659 location = new Location("fake");
660 location.setLongitude(lngOffset);
661 location.setLatitude(0);
662 location.setAccuracy(417000.0f);
663 location.setTime(System.currentTimeMillis());
664 }
665 synchronized (mLock) {
666 mLocation = location;
667 }
668 }
669 };
670
671 void updateTwilightLocked() {
672 if (mLocation == null) {
673 return;
674 }
675 final long currentTime = System.currentTimeMillis();
676 boolean nightMode;
677 // calculate current twilight
678 TwilightCalculator tw = new TwilightCalculator();
679 tw.calculateTwilight(currentTime,
680 mLocation.getLatitude(), mLocation.getLongitude());
681 if (tw.mState == TwilightCalculator.DAY) {
682 nightMode = false;
683 } else {
684 nightMode = true;
685 }
686
687 // schedule next update
688 long nextUpdate = 0;
689 if (tw.mSunrise == -1 || tw.mSunset == -1) {
690 // In the case the day or night never ends the update is scheduled 12 hours later.
691 nextUpdate = currentTime + 12 * DateUtils.HOUR_IN_MILLIS;
692 } else {
693 final int mLastTwilightState = tw.mState;
694 // add some extra time to be on the save side.
695 nextUpdate += DateUtils.MINUTE_IN_MILLIS;
696 if (currentTime > tw.mSunset) {
697 // next update should be on the following day
698 tw.calculateTwilight(currentTime
699 + DateUtils.DAY_IN_MILLIS, mLocation.getLatitude(),
700 mLocation.getLongitude());
701 }
702
703 if (mLastTwilightState == TwilightCalculator.NIGHT) {
704 nextUpdate += tw.mSunrise;
705 } else {
706 nextUpdate += tw.mSunset;
707 }
708 }
709
710 Intent updateIntent = new Intent(ACTION_UPDATE_NIGHT_MODE);
711 PendingIntent pendingIntent =
712 PendingIntent.getBroadcast(mContext, 0, updateIntent, 0);
713 mAlarmManager.cancel(pendingIntent);
714 mAlarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdate, pendingIntent);
715
716 mComputedNightMode = nightMode;
717 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100718
Dianne Hackborn7299c412010-03-04 18:41:49 -0800719 @Override
720 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
721 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
722 != PackageManager.PERMISSION_GRANTED) {
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100723
Dianne Hackborn7299c412010-03-04 18:41:49 -0800724 pw.println("Permission Denial: can't dump uimode service from from pid="
725 + Binder.getCallingPid()
726 + ", uid=" + Binder.getCallingUid());
727 return;
728 }
Bernd Holzheyba9ab182010-03-12 09:30:29 +0100729
Dianne Hackborn7299c412010-03-04 18:41:49 -0800730 synchronized (mLock) {
731 pw.println("Current UI Mode Service state:");
732 pw.print(" mDockState="); pw.print(mDockState);
733 pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState);
734 pw.print(" mNightMode="); pw.print(mNightMode);
735 pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled);
736 pw.print(" mComputedNightMode="); pw.println(mComputedNightMode);
737 pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode));
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800738 pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
739 pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800740 pw.print(" mSystemReady="); pw.println(mSystemReady);
741 if (mLocation != null) {
742 pw.print(" mLocation="); pw.println(mLocation);
743 }
744 }
745 }
746}