Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.ActivityManagerNative; |
| 21 | import android.app.AlarmManager; |
| 22 | import android.app.IActivityManager; |
| 23 | import android.app.IUiModeManager; |
| 24 | import android.app.Notification; |
| 25 | import android.app.NotificationManager; |
| 26 | import android.app.PendingIntent; |
| 27 | import android.app.StatusBarManager; |
| 28 | import android.app.UiModeManager; |
| 29 | import android.content.ActivityNotFoundException; |
| 30 | import android.content.BroadcastReceiver; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
| 33 | import android.content.IntentFilter; |
| 34 | import android.content.pm.PackageManager; |
| 35 | import android.content.res.Configuration; |
| 36 | import android.location.Criteria; |
| 37 | import android.location.Location; |
| 38 | import android.location.LocationListener; |
| 39 | import android.location.LocationManager; |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 40 | import android.os.BatteryManager; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 41 | import android.os.Binder; |
| 42 | import android.os.Bundle; |
| 43 | import android.os.Handler; |
| 44 | import android.os.Message; |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 45 | import android.os.PowerManager; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 46 | import android.os.RemoteException; |
| 47 | import android.os.ServiceManager; |
| 48 | import android.text.format.DateUtils; |
| 49 | import android.text.format.Time; |
| 50 | import android.util.Slog; |
| 51 | |
| 52 | import java.io.FileDescriptor; |
| 53 | import java.io.PrintWriter; |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 54 | import java.util.Iterator; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 55 | |
| 56 | import com.android.internal.R; |
| 57 | import com.android.internal.app.DisableCarModeActivity; |
| 58 | |
| 59 | class UiModeManagerService extends IUiModeManager.Stub { |
| 60 | private static final String TAG = UiModeManager.class.getSimpleName(); |
| 61 | private static final boolean LOG = false; |
| 62 | |
| 63 | private static final String KEY_LAST_UPDATE_INTERVAL = "LAST_UPDATE_INTERVAL"; |
| 64 | |
| 65 | private static final int MSG_UPDATE_TWILIGHT = 0; |
| 66 | private static final int MSG_ENABLE_LOCATION_UPDATES = 1; |
| 67 | |
| 68 | private static final long LOCATION_UPDATE_MS = 30 * DateUtils.MINUTE_IN_MILLIS; |
| 69 | private static final float LOCATION_UPDATE_DISTANCE_METER = 1000 * 20; |
| 70 | private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MIN = 5000; |
| 71 | private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MAX = 5 * DateUtils.MINUTE_IN_MILLIS; |
| 72 | private static final double FACTOR_GMT_OFFSET_LONGITUDE = 1000.0 * 360.0 / DateUtils.DAY_IN_MILLIS; |
| 73 | |
| 74 | private static final String ACTION_UPDATE_NIGHT_MODE = "com.android.server.action.UPDATE_NIGHT_MODE"; |
| 75 | |
| 76 | private final Context mContext; |
| 77 | |
| 78 | final Object mLock = new Object(); |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 79 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 80 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 81 | private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 82 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 83 | private int mNightMode = UiModeManager.MODE_NIGHT_NO; |
| 84 | private boolean mCarModeEnabled = false; |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 85 | private boolean mCharging = false; |
| 86 | private final boolean mCarModeKeepsScreenOn; |
| 87 | private final boolean mDeskModeKeepsScreenOn; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 88 | |
| 89 | private boolean mComputedNightMode; |
| 90 | private int mCurUiMode = 0; |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 91 | private int mSetUiMode = 0; |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 92 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 93 | private boolean mHoldingConfiguration = false; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 94 | private Configuration mConfiguration = new Configuration(); |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 95 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 96 | private boolean mSystemReady; |
| 97 | |
| 98 | private NotificationManager mNotificationManager; |
| 99 | |
| 100 | private AlarmManager mAlarmManager; |
| 101 | |
| 102 | private LocationManager mLocationManager; |
| 103 | private Location mLocation; |
| 104 | private StatusBarManager mStatusBarManager; |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 105 | private final PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 106 | |
| 107 | // The broadcast receiver which receives the result of the ordered broadcast sent when |
| 108 | // the dock state changes. The original ordered broadcast is sent with an initial result |
| 109 | // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g., |
| 110 | // to RESULT_CANCELED, then the intent to start a dock app will not be sent. |
| 111 | private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() { |
| 112 | @Override |
| 113 | public void onReceive(Context context, Intent intent) { |
| 114 | if (getResultCode() != Activity.RESULT_OK) { |
| 115 | return; |
| 116 | } |
| 117 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 118 | synchronized (mLock) { |
| 119 | // Launch a dock activity |
| 120 | String category; |
| 121 | if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) { |
| 122 | // Only launch car home when car mode is enabled. |
| 123 | category = Intent.CATEGORY_CAR_DOCK; |
| 124 | } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(intent.getAction())) { |
| 125 | category = Intent.CATEGORY_DESK_DOCK; |
| 126 | } else { |
| 127 | category = null; |
| 128 | } |
| 129 | if (category != null) { |
| 130 | intent = new Intent(Intent.ACTION_MAIN); |
| 131 | intent.addCategory(category); |
| 132 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 133 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 134 | try { |
| 135 | mContext.startActivity(intent); |
| 136 | } catch (ActivityNotFoundException e) { |
| 137 | Slog.w(TAG, e.getCause()); |
| 138 | } |
| 139 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 140 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 141 | if (mHoldingConfiguration) { |
| 142 | mHoldingConfiguration = false; |
| 143 | updateConfigurationLocked(); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | }; |
| 148 | |
| 149 | private final BroadcastReceiver mTwilightUpdateReceiver = new BroadcastReceiver() { |
| 150 | @Override |
| 151 | public void onReceive(Context context, Intent intent) { |
| 152 | if (isDoingNightMode() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) { |
| 153 | mHandler.sendEmptyMessage(MSG_UPDATE_TWILIGHT); |
| 154 | } |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() { |
| 159 | @Override |
| 160 | public void onReceive(Context context, Intent intent) { |
| 161 | int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 162 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 163 | updateDockState(state); |
| 164 | } |
| 165 | }; |
| 166 | |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 167 | private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() { |
| 168 | @Override |
| 169 | public void onReceive(Context context, Intent intent) { |
| 170 | mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0); |
| 171 | synchronized (mLock) { |
| 172 | if (mSystemReady) { |
| 173 | updateLocked(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | }; |
| 178 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 179 | // A LocationListener to initialize the network location provider. The location updates |
| 180 | // are handled through the passive location provider. |
| 181 | private final LocationListener mEmptyLocationListener = new LocationListener() { |
| 182 | public void onLocationChanged(Location location) { |
| 183 | } |
| 184 | |
| 185 | public void onProviderDisabled(String provider) { |
| 186 | } |
| 187 | |
| 188 | public void onProviderEnabled(String provider) { |
| 189 | } |
| 190 | |
| 191 | public void onStatusChanged(String provider, int status, Bundle extras) { |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | private final LocationListener mLocationListener = new LocationListener() { |
| 196 | |
| 197 | public void onLocationChanged(Location location) { |
| 198 | final boolean hasMoved = hasMoved(location); |
| 199 | final boolean hasBetterAccuracy = mLocation == null |
| 200 | || location.getAccuracy() < mLocation.getAccuracy(); |
| 201 | if (hasMoved || hasBetterAccuracy) { |
| 202 | synchronized (mLock) { |
| 203 | mLocation = location; |
| 204 | if (hasMoved && isDoingNightMode() |
| 205 | && mNightMode == UiModeManager.MODE_NIGHT_AUTO) { |
| 206 | mHandler.sendEmptyMessage(MSG_UPDATE_TWILIGHT); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | public void onProviderDisabled(String provider) { |
| 213 | } |
| 214 | |
| 215 | public void onProviderEnabled(String provider) { |
| 216 | } |
| 217 | |
| 218 | public void onStatusChanged(String provider, int status, Bundle extras) { |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * The user has moved if the accuracy circles of the two locations |
| 223 | * don't overlap. |
| 224 | */ |
| 225 | private boolean hasMoved(Location location) { |
| 226 | if (location == null) { |
| 227 | return false; |
| 228 | } |
| 229 | if (mLocation == null) { |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | /* if new location is older than the current one, the devices hasn't |
| 234 | * moved. |
| 235 | */ |
| 236 | if (location.getTime() < mLocation.getTime()) { |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | /* Get the distance between the two points */ |
| 241 | float distance = mLocation.distanceTo(location); |
| 242 | |
| 243 | /* Get the total accuracy radius for both locations */ |
| 244 | float totalAccuracy = mLocation.getAccuracy() + location.getAccuracy(); |
| 245 | |
| 246 | /* If the distance is greater than the combined accuracy of the two |
| 247 | * points then they can't overlap and hence the user has moved. |
| 248 | */ |
| 249 | return distance >= totalAccuracy; |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | public UiModeManagerService(Context context) { |
| 254 | mContext = context; |
| 255 | |
| 256 | ServiceManager.addService(Context.UI_MODE_SERVICE, this); |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 257 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 258 | mAlarmManager = |
| 259 | (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 260 | mLocationManager = |
| 261 | (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE); |
| 262 | mContext.registerReceiver(mTwilightUpdateReceiver, |
| 263 | new IntentFilter(ACTION_UPDATE_NIGHT_MODE)); |
| 264 | mContext.registerReceiver(mDockModeReceiver, |
| 265 | new IntentFilter(Intent.ACTION_DOCK_EVENT)); |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 266 | mContext.registerReceiver(mBatteryReceiver, |
| 267 | new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); |
| 268 | |
| 269 | PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 270 | mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 271 | |
| 272 | mConfiguration.setToDefaults(); |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 273 | |
| 274 | mCarModeKeepsScreenOn = (context.getResources().getInteger( |
| 275 | com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1); |
| 276 | mDeskModeKeepsScreenOn = (context.getResources().getInteger( |
| 277 | com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | public void disableCarMode() { |
| 281 | synchronized (mLock) { |
| 282 | setCarModeLocked(false); |
Mike Lockwood | 924e164 | 2010-03-05 11:56:53 -0500 | [diff] [blame] | 283 | if (mSystemReady) { |
| 284 | updateLocked(); |
| 285 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | public void enableCarMode() { |
| 290 | mContext.enforceCallingOrSelfPermission( |
| 291 | android.Manifest.permission.ENABLE_CAR_MODE, |
| 292 | "Need ENABLE_CAR_MODE permission"); |
| 293 | synchronized (mLock) { |
| 294 | setCarModeLocked(true); |
Mike Lockwood | 924e164 | 2010-03-05 11:56:53 -0500 | [diff] [blame] | 295 | if (mSystemReady) { |
| 296 | updateLocked(); |
| 297 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | public int getCurrentModeType() { |
| 302 | synchronized (mLock) { |
| 303 | return mCurUiMode & Configuration.UI_MODE_TYPE_MASK; |
| 304 | } |
| 305 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 306 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 307 | public void setNightMode(int mode) throws RemoteException { |
| 308 | synchronized (mLock) { |
| 309 | switch (mode) { |
| 310 | case UiModeManager.MODE_NIGHT_NO: |
| 311 | case UiModeManager.MODE_NIGHT_YES: |
| 312 | case UiModeManager.MODE_NIGHT_AUTO: |
| 313 | break; |
| 314 | default: |
| 315 | throw new IllegalArgumentException("Unknown mode: " + mode); |
| 316 | } |
| 317 | if (!isDoingNightMode()) { |
| 318 | return; |
| 319 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 320 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 321 | if (mNightMode != mode) { |
| 322 | mNightMode = mode; |
| 323 | updateLocked(); |
| 324 | } |
| 325 | } |
| 326 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 327 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 328 | public int getNightMode() throws RemoteException { |
| 329 | return mNightMode; |
| 330 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 331 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 332 | void systemReady() { |
| 333 | synchronized (mLock) { |
| 334 | mSystemReady = true; |
| 335 | mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR; |
| 336 | updateLocked(); |
| 337 | mHandler.sendEmptyMessage(MSG_ENABLE_LOCATION_UPDATES); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | boolean isDoingNightMode() { |
| 342 | return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 343 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 344 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 345 | void setCarModeLocked(boolean enabled) { |
| 346 | if (mCarModeEnabled != enabled) { |
| 347 | mCarModeEnabled = enabled; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | void updateDockState(int newState) { |
| 352 | synchronized (mLock) { |
| 353 | if (newState != mDockState) { |
| 354 | mDockState = newState; |
Mike Lockwood | 924e164 | 2010-03-05 11:56:53 -0500 | [diff] [blame] | 355 | setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 356 | if (mSystemReady) { |
| 357 | updateLocked(); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 362 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 363 | final void updateConfigurationLocked() { |
| 364 | int uiMode = 0; |
| 365 | if (mCarModeEnabled) { |
| 366 | uiMode = Configuration.UI_MODE_TYPE_CAR; |
| 367 | } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) { |
| 368 | uiMode = Configuration.UI_MODE_TYPE_DESK; |
| 369 | } |
| 370 | if (uiMode != 0) { |
| 371 | if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) { |
| 372 | updateTwilightLocked(); |
| 373 | uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES |
| 374 | : Configuration.UI_MODE_NIGHT_NO; |
| 375 | } else { |
| 376 | uiMode |= mNightMode << 4; |
| 377 | } |
| 378 | } else { |
| 379 | // Disabling the car mode clears the night mode. |
| 380 | uiMode = Configuration.UI_MODE_TYPE_NORMAL | |
| 381 | Configuration.UI_MODE_NIGHT_NO; |
| 382 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 383 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 384 | mCurUiMode = uiMode; |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 385 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 386 | if (!mHoldingConfiguration && uiMode != mSetUiMode) { |
| 387 | mSetUiMode = uiMode; |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 388 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 389 | try { |
| 390 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 391 | mConfiguration.uiMode = uiMode; |
| 392 | am.updateConfiguration(mConfiguration); |
| 393 | } catch (RemoteException e) { |
| 394 | Slog.w(TAG, "Failure communicating with activity manager", e); |
| 395 | } |
| 396 | } |
| 397 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 398 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 399 | final void updateLocked() { |
| 400 | long ident = Binder.clearCallingIdentity(); |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 401 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 402 | try { |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 403 | String action = null; |
| 404 | String oldAction = null; |
| 405 | if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) { |
Tobias Haamel | 780b260 | 2010-03-15 12:54:45 +0100 | [diff] [blame] | 406 | adjustStatusBarCarModeLocked(); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 407 | oldAction = UiModeManager.ACTION_EXIT_CAR_MODE; |
| 408 | } else if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_DESK) { |
| 409 | oldAction = UiModeManager.ACTION_EXIT_DESK_MODE; |
| 410 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 411 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 412 | if (mCarModeEnabled) { |
| 413 | if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) { |
| 414 | adjustStatusBarCarModeLocked(); |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 415 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 416 | if (oldAction != null) { |
| 417 | mContext.sendBroadcast(new Intent(oldAction)); |
| 418 | } |
| 419 | mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR; |
| 420 | action = UiModeManager.ACTION_ENTER_CAR_MODE; |
| 421 | } |
| 422 | } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) { |
| 423 | if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_DESK) { |
| 424 | if (oldAction != null) { |
| 425 | mContext.sendBroadcast(new Intent(oldAction)); |
| 426 | } |
| 427 | mLastBroadcastState = Intent.EXTRA_DOCK_STATE_DESK; |
| 428 | action = UiModeManager.ACTION_ENTER_DESK_MODE; |
| 429 | } |
| 430 | } else { |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 431 | mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 432 | action = oldAction; |
| 433 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 434 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 435 | if (action != null) { |
| 436 | // 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 | mContext.sendOrderedBroadcast(new Intent(action), null, |
| 443 | mResultReceiver, null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 444 | // Attempting to make this transition a little more clean, we are going |
| 445 | // to hold off on doing a configuration change until we have finished |
| 446 | // the broacast and started the home activity. |
| 447 | mHoldingConfiguration = true; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 448 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 449 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 450 | updateConfigurationLocked(); |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 451 | |
| 452 | // keep screen on when charging and in car mode |
| 453 | boolean keepScreenOn = mCharging && |
| 454 | ((mCarModeEnabled && mCarModeKeepsScreenOn) || |
| 455 | (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn)); |
| 456 | if (keepScreenOn != mWakeLock.isHeld()) { |
| 457 | if (keepScreenOn) { |
| 458 | mWakeLock.acquire(); |
| 459 | } else { |
| 460 | mWakeLock.release(); |
| 461 | } |
| 462 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 463 | } finally { |
| 464 | Binder.restoreCallingIdentity(ident); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | private void adjustStatusBarCarModeLocked() { |
| 469 | if (mStatusBarManager == null) { |
| 470 | mStatusBarManager = (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE); |
| 471 | } |
| 472 | |
| 473 | // Fear not: StatusBarService manages a list of requests to disable |
| 474 | // features of the status bar; these are ORed together to form the |
| 475 | // active disabled list. So if (for example) the device is locked and |
| 476 | // the status bar should be totally disabled, the calls below will |
| 477 | // have no effect until the device is unlocked. |
| 478 | if (mStatusBarManager != null) { |
| 479 | mStatusBarManager.disable(mCarModeEnabled |
| 480 | ? StatusBarManager.DISABLE_NOTIFICATION_TICKER |
| 481 | : StatusBarManager.DISABLE_NONE); |
| 482 | } |
| 483 | |
| 484 | if (mNotificationManager == null) { |
| 485 | mNotificationManager = (NotificationManager) |
| 486 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 487 | } |
| 488 | |
| 489 | if (mNotificationManager != null) { |
| 490 | if (mCarModeEnabled) { |
| 491 | Intent carModeOffIntent = new Intent(mContext, DisableCarModeActivity.class); |
| 492 | |
| 493 | Notification n = new Notification(); |
| 494 | n.icon = R.drawable.stat_notify_car_mode; |
| 495 | n.defaults = Notification.DEFAULT_LIGHTS; |
| 496 | n.flags = Notification.FLAG_ONGOING_EVENT; |
| 497 | n.when = 0; |
| 498 | n.setLatestEventInfo( |
| 499 | mContext, |
| 500 | mContext.getString(R.string.car_mode_disable_notification_title), |
| 501 | mContext.getString(R.string.car_mode_disable_notification_message), |
| 502 | PendingIntent.getActivity(mContext, 0, carModeOffIntent, 0)); |
| 503 | mNotificationManager.notify(0, n); |
| 504 | } else { |
| 505 | mNotificationManager.cancel(0); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | private final Handler mHandler = new Handler() { |
| 511 | |
| 512 | boolean mPassiveListenerEnabled; |
| 513 | boolean mNetworkListenerEnabled; |
| 514 | |
| 515 | @Override |
| 516 | public void handleMessage(Message msg) { |
| 517 | switch (msg.what) { |
| 518 | case MSG_UPDATE_TWILIGHT: |
| 519 | synchronized (mLock) { |
| 520 | if (isDoingNightMode() && mLocation != null |
| 521 | && mNightMode == UiModeManager.MODE_NIGHT_AUTO) { |
| 522 | updateTwilightLocked(); |
| 523 | updateLocked(); |
| 524 | } |
| 525 | } |
| 526 | break; |
| 527 | case MSG_ENABLE_LOCATION_UPDATES: |
| 528 | // enable network provider to receive at least location updates for a given |
| 529 | // distance. |
| 530 | boolean networkLocationEnabled; |
| 531 | try { |
| 532 | networkLocationEnabled = |
| 533 | mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); |
| 534 | } catch (Exception e) { |
| 535 | // we may get IllegalArgumentException if network location provider |
| 536 | // does not exist or is not yet installed. |
| 537 | networkLocationEnabled = false; |
| 538 | } |
| 539 | if (!mNetworkListenerEnabled && networkLocationEnabled) { |
| 540 | mNetworkListenerEnabled = true; |
| 541 | mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, |
| 542 | LOCATION_UPDATE_MS, 0, mEmptyLocationListener); |
| 543 | |
| 544 | if (mLocation == null) { |
| 545 | retrieveLocation(); |
| 546 | } |
| 547 | synchronized (mLock) { |
| 548 | if (isDoingNightMode() && mLocation != null |
| 549 | && mNightMode == UiModeManager.MODE_NIGHT_AUTO) { |
| 550 | updateTwilightLocked(); |
| 551 | updateLocked(); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | // enable passive provider to receive updates from location fixes (gps |
| 556 | // and network). |
| 557 | boolean passiveLocationEnabled; |
| 558 | try { |
| 559 | passiveLocationEnabled = |
| 560 | mLocationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER); |
| 561 | } catch (Exception e) { |
| 562 | // we may get IllegalArgumentException if passive location provider |
| 563 | // does not exist or is not yet installed. |
| 564 | passiveLocationEnabled = false; |
| 565 | } |
| 566 | if (!mPassiveListenerEnabled && passiveLocationEnabled) { |
| 567 | mPassiveListenerEnabled = true; |
| 568 | mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, |
| 569 | 0, LOCATION_UPDATE_DISTANCE_METER , mLocationListener); |
| 570 | } |
| 571 | if (!(mNetworkListenerEnabled && mPassiveListenerEnabled)) { |
| 572 | long interval = msg.getData().getLong(KEY_LAST_UPDATE_INTERVAL); |
| 573 | interval *= 1.5; |
| 574 | if (interval == 0) { |
| 575 | interval = LOCATION_UPDATE_ENABLE_INTERVAL_MIN; |
| 576 | } else if (interval > LOCATION_UPDATE_ENABLE_INTERVAL_MAX) { |
| 577 | interval = LOCATION_UPDATE_ENABLE_INTERVAL_MAX; |
| 578 | } |
| 579 | Bundle bundle = new Bundle(); |
| 580 | bundle.putLong(KEY_LAST_UPDATE_INTERVAL, interval); |
| 581 | Message newMsg = mHandler.obtainMessage(MSG_ENABLE_LOCATION_UPDATES); |
| 582 | newMsg.setData(bundle); |
| 583 | mHandler.sendMessageDelayed(newMsg, interval); |
| 584 | } |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | private void retrieveLocation() { |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 590 | Location location = null; |
| 591 | final Iterator<String> providers = |
| 592 | mLocationManager.getProviders(new Criteria(), true).iterator(); |
| 593 | while (providers.hasNext()) { |
| 594 | final Location lastKnownLocation = |
| 595 | mLocationManager.getLastKnownLocation(providers.next()); |
| 596 | // pick the most recent location |
| 597 | if (location == null || (lastKnownLocation != null && |
| 598 | location.getTime() < lastKnownLocation.getTime())) { |
| 599 | location = lastKnownLocation; |
| 600 | } |
| 601 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 602 | // In the case there is no location available (e.g. GPS fix or network location |
| 603 | // is not available yet), the longitude of the location is estimated using the timezone, |
| 604 | // latitude and accuracy are set to get a good average. |
| 605 | if (location == null) { |
| 606 | Time currentTime = new Time(); |
| 607 | currentTime.set(System.currentTimeMillis()); |
| 608 | double lngOffset = FACTOR_GMT_OFFSET_LONGITUDE * |
| 609 | (currentTime.gmtoff - (currentTime.isDst > 0 ? 3600 : 0)); |
| 610 | location = new Location("fake"); |
| 611 | location.setLongitude(lngOffset); |
| 612 | location.setLatitude(0); |
| 613 | location.setAccuracy(417000.0f); |
| 614 | location.setTime(System.currentTimeMillis()); |
| 615 | } |
| 616 | synchronized (mLock) { |
| 617 | mLocation = location; |
| 618 | } |
| 619 | } |
| 620 | }; |
| 621 | |
| 622 | void updateTwilightLocked() { |
| 623 | if (mLocation == null) { |
| 624 | return; |
| 625 | } |
| 626 | final long currentTime = System.currentTimeMillis(); |
| 627 | boolean nightMode; |
| 628 | // calculate current twilight |
| 629 | TwilightCalculator tw = new TwilightCalculator(); |
| 630 | tw.calculateTwilight(currentTime, |
| 631 | mLocation.getLatitude(), mLocation.getLongitude()); |
| 632 | if (tw.mState == TwilightCalculator.DAY) { |
| 633 | nightMode = false; |
| 634 | } else { |
| 635 | nightMode = true; |
| 636 | } |
| 637 | |
| 638 | // schedule next update |
| 639 | long nextUpdate = 0; |
| 640 | if (tw.mSunrise == -1 || tw.mSunset == -1) { |
| 641 | // In the case the day or night never ends the update is scheduled 12 hours later. |
| 642 | nextUpdate = currentTime + 12 * DateUtils.HOUR_IN_MILLIS; |
| 643 | } else { |
| 644 | final int mLastTwilightState = tw.mState; |
| 645 | // add some extra time to be on the save side. |
| 646 | nextUpdate += DateUtils.MINUTE_IN_MILLIS; |
| 647 | if (currentTime > tw.mSunset) { |
| 648 | // next update should be on the following day |
| 649 | tw.calculateTwilight(currentTime |
| 650 | + DateUtils.DAY_IN_MILLIS, mLocation.getLatitude(), |
| 651 | mLocation.getLongitude()); |
| 652 | } |
| 653 | |
| 654 | if (mLastTwilightState == TwilightCalculator.NIGHT) { |
| 655 | nextUpdate += tw.mSunrise; |
| 656 | } else { |
| 657 | nextUpdate += tw.mSunset; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | Intent updateIntent = new Intent(ACTION_UPDATE_NIGHT_MODE); |
| 662 | PendingIntent pendingIntent = |
| 663 | PendingIntent.getBroadcast(mContext, 0, updateIntent, 0); |
| 664 | mAlarmManager.cancel(pendingIntent); |
| 665 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdate, pendingIntent); |
| 666 | |
| 667 | mComputedNightMode = nightMode; |
| 668 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 669 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 670 | @Override |
| 671 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 672 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 673 | != PackageManager.PERMISSION_GRANTED) { |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 674 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 675 | pw.println("Permission Denial: can't dump uimode service from from pid=" |
| 676 | + Binder.getCallingPid() |
| 677 | + ", uid=" + Binder.getCallingUid()); |
| 678 | return; |
| 679 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 680 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 681 | synchronized (mLock) { |
| 682 | pw.println("Current UI Mode Service state:"); |
| 683 | pw.print(" mDockState="); pw.print(mDockState); |
| 684 | pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState); |
| 685 | pw.print(" mNightMode="); pw.print(mNightMode); |
| 686 | pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled); |
| 687 | pw.print(" mComputedNightMode="); pw.println(mComputedNightMode); |
| 688 | pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode)); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 689 | pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode)); |
| 690 | pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 691 | pw.print(" mSystemReady="); pw.println(mSystemReady); |
| 692 | if (mLocation != null) { |
| 693 | pw.print(" mLocation="); pw.println(mLocation); |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | } |