Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [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 | |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 20 | import android.app.ActivityManagerNative; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 21 | import android.app.AlarmManager; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 22 | import android.app.IActivityManager; |
| 23 | import android.app.IUiModeManager; |
Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 24 | import android.app.KeyguardManager; |
Tobias Haamel | 154f7a1 | 2010-02-17 11:56:39 -0800 | [diff] [blame] | 25 | import android.app.Notification; |
| 26 | import android.app.NotificationManager; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 27 | import android.app.PendingIntent; |
Tobias Haamel | 154f7a1 | 2010-02-17 11:56:39 -0800 | [diff] [blame] | 28 | import android.app.StatusBarManager; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 29 | import android.bluetooth.BluetoothAdapter; |
| 30 | import android.bluetooth.BluetoothDevice; |
Mike Lockwood | 9092ab4 | 2009-09-16 13:01:32 -0400 | [diff] [blame] | 31 | import android.content.ActivityNotFoundException; |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 32 | import android.content.BroadcastReceiver; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 33 | import android.content.ContentResolver; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 36 | import android.content.IntentFilter; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 37 | import android.content.res.Configuration; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 38 | import android.location.Criteria; |
| 39 | import android.location.Location; |
| 40 | import android.location.LocationListener; |
| 41 | import android.location.LocationManager; |
| 42 | import android.location.LocationProvider; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 43 | import android.os.Binder; |
Daniel Sandler | ec2c88d | 2010-02-20 01:04:57 -0500 | [diff] [blame^] | 44 | import android.media.AudioManager; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 45 | import android.media.Ringtone; |
| 46 | import android.media.RingtoneManager; |
| 47 | import android.net.Uri; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 48 | import android.os.Bundle; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 49 | import android.os.Handler; |
| 50 | import android.os.Message; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 51 | import android.os.RemoteException; |
| 52 | import android.os.ServiceManager; |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 53 | import android.os.SystemClock; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 54 | import android.os.UEventObserver; |
Dianne Hackborn | 4949334 | 2009-10-02 10:44:41 -0700 | [diff] [blame] | 55 | import android.provider.Settings; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 56 | import android.server.BluetoothService; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 57 | import android.text.format.DateUtils; |
| 58 | import android.text.format.Time; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 59 | import android.util.Log; |
| 60 | |
Tobias Haamel | 154f7a1 | 2010-02-17 11:56:39 -0800 | [diff] [blame] | 61 | import com.android.internal.R; |
| 62 | import com.android.internal.app.DisableCarModeActivity; |
Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 63 | import com.android.internal.widget.LockPatternUtils; |
| 64 | |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 65 | import java.io.FileNotFoundException; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 66 | import java.io.FileReader; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * <p>DockObserver monitors for a docking station. |
| 70 | */ |
| 71 | class DockObserver extends UEventObserver { |
| 72 | private static final String TAG = DockObserver.class.getSimpleName(); |
| 73 | private static final boolean LOG = false; |
| 74 | |
| 75 | private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock"; |
| 76 | private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state"; |
| 77 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 78 | private static final String KEY_LAST_UPDATE_INTERVAL = "LAST_UPDATE_INTERVAL"; |
| 79 | |
| 80 | private static final int MSG_DOCK_STATE = 0; |
| 81 | private static final int MSG_UPDATE_TWILIGHT = 1; |
| 82 | private static final int MSG_ENABLE_LOCATION_UPDATES = 2; |
| 83 | |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 84 | public static final int MODE_NIGHT_AUTO = Configuration.UI_MODE_NIGHT_MASK >> 4; |
| 85 | public static final int MODE_NIGHT_NO = Configuration.UI_MODE_NIGHT_NO >> 4; |
| 86 | public static final int MODE_NIGHT_YES = Configuration.UI_MODE_NIGHT_YES >> 4; |
| 87 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 88 | private static final long LOCATION_UPDATE_MS = 30 * DateUtils.MINUTE_IN_MILLIS; |
| 89 | private static final float LOCATION_UPDATE_DISTANCE_METER = 1000 * 20; |
| 90 | private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MIN = 5000; |
| 91 | private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MAX = 5 * DateUtils.MINUTE_IN_MILLIS; |
| 92 | // velocity for estimating a potential movement: 150km/h |
| 93 | private static final float MAX_VELOCITY_M_MS = 150 / 3600; |
| 94 | private static final double FACTOR_GMT_OFFSET_LONGITUDE = 1000.0 * 360.0 / DateUtils.DAY_IN_MILLIS; |
| 95 | |
| 96 | private static final String ACTION_UPDATE_NIGHT_MODE = "com.android.server.action.UPDATE_NIGHT_MODE"; |
| 97 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 98 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 99 | private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 100 | |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 101 | private int mNightMode = MODE_NIGHT_NO; |
| 102 | private boolean mCarModeEnabled = false; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 103 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 104 | private boolean mSystemReady; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 105 | |
| 106 | private final Context mContext; |
| 107 | |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 108 | private PowerManagerService mPowerManager; |
Tobias Haamel | 154f7a1 | 2010-02-17 11:56:39 -0800 | [diff] [blame] | 109 | private NotificationManager mNotificationManager; |
Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 110 | |
| 111 | private KeyguardManager.KeyguardLock mKeyguardLock; |
| 112 | private boolean mKeyguardDisabled; |
| 113 | private LockPatternUtils mLockPatternUtils; |
| 114 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 115 | private AlarmManager mAlarmManager; |
| 116 | |
| 117 | private LocationManager mLocationManager; |
| 118 | private Location mLocation; |
| 119 | private StatusBarManager mStatusBarManager; |
Daniel Sandler | a0430a1 | 2010-02-11 23:35:49 -0500 | [diff] [blame] | 120 | |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 121 | // The broadcast receiver which receives the result of the ordered broadcast sent when |
| 122 | // the dock state changes. The original ordered broadcast is sent with an initial result |
| 123 | // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g., |
| 124 | // to RESULT_CANCELED, then the intent to start a dock app will not be sent. |
| 125 | private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() { |
| 126 | @Override |
| 127 | public void onReceive(Context context, Intent intent) { |
| 128 | if (getResultCode() != Activity.RESULT_OK) { |
| 129 | return; |
| 130 | } |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 131 | |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 132 | // Launch a dock activity |
| 133 | String category; |
Tobias Haamel | 154f7a1 | 2010-02-17 11:56:39 -0800 | [diff] [blame] | 134 | if (mCarModeEnabled) { |
| 135 | // Only launch car home when car mode is enabled. |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 136 | category = Intent.CATEGORY_CAR_DOCK; |
| 137 | } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) { |
| 138 | category = Intent.CATEGORY_DESK_DOCK; |
| 139 | } else { |
| 140 | category = null; |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 141 | } |
| 142 | if (category != null) { |
| 143 | intent = new Intent(Intent.ACTION_MAIN); |
| 144 | intent.addCategory(category); |
Dianne Hackborn | 9bfb707 | 2009-09-22 11:37:40 -0700 | [diff] [blame] | 145 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 146 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 147 | try { |
| 148 | mContext.startActivity(intent); |
| 149 | } catch (ActivityNotFoundException e) { |
| 150 | Log.w(TAG, e.getCause()); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | }; |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 155 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 156 | private final BroadcastReceiver mTwilightUpdateReceiver = new BroadcastReceiver() { |
| 157 | @Override |
| 158 | public void onReceive(Context context, Intent intent) { |
| 159 | if (mCarModeEnabled && mNightMode == MODE_NIGHT_AUTO) { |
| 160 | mHandler.sendEmptyMessage(MSG_UPDATE_TWILIGHT); |
| 161 | } |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | private final LocationListener mLocationListener = new LocationListener() { |
| 166 | |
| 167 | public void onLocationChanged(Location location) { |
| 168 | updateLocation(location); |
| 169 | } |
| 170 | |
| 171 | public void onProviderDisabled(String provider) { |
| 172 | } |
| 173 | |
| 174 | public void onProviderEnabled(String provider) { |
| 175 | } |
| 176 | |
| 177 | public void onStatusChanged(String provider, int status, Bundle extras) { |
| 178 | // If the network location is no longer available check for a GPS fix |
| 179 | // and try to update the location. |
| 180 | if (provider == LocationManager.NETWORK_PROVIDER && |
| 181 | status != LocationProvider.AVAILABLE) { |
| 182 | updateLocation(mLocation); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private void updateLocation(Location location) { |
| 187 | location = DockObserver.chooseBestLocation(location, |
| 188 | mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)); |
| 189 | if (hasMoved(location)) { |
| 190 | synchronized (this) { |
| 191 | mLocation = location; |
| 192 | } |
| 193 | if (mCarModeEnabled && mNightMode == MODE_NIGHT_AUTO) { |
| 194 | mHandler.sendEmptyMessage(MSG_UPDATE_TWILIGHT); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * The user has moved if the accuracy circles of the two locations |
| 201 | * don't overlap. |
| 202 | */ |
| 203 | private boolean hasMoved(Location location) { |
| 204 | if (location == null) { |
| 205 | return false; |
| 206 | } |
| 207 | if (mLocation == null) { |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | /* if new location is older than the current one, the devices hasn't |
| 212 | * moved. |
| 213 | */ |
| 214 | if (location.getTime() < mLocation.getTime()) { |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | /* Get the distance between the two points */ |
| 219 | float distance = mLocation.distanceTo(location); |
| 220 | |
| 221 | /* Get the total accuracy radius for both locations */ |
| 222 | float totalAccuracy = mLocation.getAccuracy() + location.getAccuracy(); |
| 223 | |
| 224 | /* If the distance is greater than the combined accuracy of the two |
| 225 | * points then they can't overlap and hence the user has moved. |
| 226 | */ |
| 227 | return distance > totalAccuracy; |
| 228 | } |
| 229 | }; |
| 230 | |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 231 | public DockObserver(Context context, PowerManagerService pm) { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 232 | mContext = context; |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 233 | mPowerManager = pm; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 234 | mLockPatternUtils = new LockPatternUtils(context); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 235 | init(); // set initial status |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 236 | |
| 237 | ServiceManager.addService("uimode", mBinder); |
| 238 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 239 | mAlarmManager = |
| 240 | (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 241 | mLocationManager = |
| 242 | (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE); |
| 243 | mContext.registerReceiver(mTwilightUpdateReceiver, |
| 244 | new IntentFilter(ACTION_UPDATE_NIGHT_MODE)); |
| 245 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 246 | startObserving(DOCK_UEVENT_MATCH); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void onUEvent(UEventObserver.UEvent event) { |
| 251 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 252 | Log.v(TAG, "Dock UEVENT: " + event.toString()); |
| 253 | } |
| 254 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 255 | synchronized (this) { |
| 256 | try { |
| 257 | int newState = Integer.parseInt(event.get("SWITCH_STATE")); |
| 258 | if (newState != mDockState) { |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 259 | mPreviousDockState = mDockState; |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 260 | mDockState = newState; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 261 | boolean carModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR; |
| 262 | if (mCarModeEnabled != carModeEnabled) { |
| 263 | try { |
| 264 | setCarMode(carModeEnabled); |
| 265 | } catch (RemoteException e1) { |
| 266 | Log.w(TAG, "Unable to change car mode.", e1); |
| 267 | } |
| 268 | } |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 269 | if (mSystemReady) { |
Mike Lockwood | 1d06992 | 2009-11-11 18:09:25 -0500 | [diff] [blame] | 270 | // Don't force screen on when undocking from the desk dock. |
| 271 | // The change in power state will do this anyway. |
| 272 | // FIXME - we should be configurable. |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 273 | if (mPreviousDockState != Intent.EXTRA_DOCK_STATE_DESK || |
| 274 | mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
Mike Lockwood | 1d06992 | 2009-11-11 18:09:25 -0500 | [diff] [blame] | 275 | mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), |
| 276 | false, true); |
| 277 | } |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 278 | update(); |
| 279 | } |
| 280 | } |
| 281 | } catch (NumberFormatException e) { |
| 282 | Log.e(TAG, "Could not parse switch state from event " + event); |
| 283 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 287 | private final void init() { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 288 | char[] buffer = new char[1024]; |
| 289 | |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 290 | try { |
| 291 | FileReader file = new FileReader(DOCK_STATE_PATH); |
| 292 | int len = file.read(buffer, 0, 1024); |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 293 | mPreviousDockState = mDockState = Integer.valueOf((new String(buffer, 0, len)).trim()); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 294 | |
| 295 | } catch (FileNotFoundException e) { |
| 296 | Log.w(TAG, "This kernel does not have dock station support"); |
| 297 | } catch (Exception e) { |
| 298 | Log.e(TAG, "" , e); |
| 299 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 300 | } |
| 301 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 302 | void systemReady() { |
| 303 | synchronized (this) { |
Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 304 | KeyguardManager keyguardManager = |
| 305 | (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE); |
| 306 | mKeyguardLock = keyguardManager.newKeyguardLock(TAG); |
| 307 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 308 | // don't bother broadcasting undocked here |
| 309 | if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 310 | update(); |
| 311 | } |
| 312 | mSystemReady = true; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 313 | mHandler.sendEmptyMessage(MSG_ENABLE_LOCATION_UPDATES); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 317 | private final void update() { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 318 | mHandler.sendEmptyMessage(MSG_DOCK_STATE); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | private final Handler mHandler = new Handler() { |
| 322 | @Override |
| 323 | public void handleMessage(Message msg) { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 324 | switch (msg.what) { |
| 325 | case MSG_DOCK_STATE: |
| 326 | synchronized (this) { |
| 327 | Log.i(TAG, "Dock state changed: " + mDockState); |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 328 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 329 | final ContentResolver cr = mContext.getContentResolver(); |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 330 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 331 | if (Settings.Secure.getInt(cr, |
| 332 | Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { |
| 333 | Log.i(TAG, "Device not provisioned, skipping dock broadcast"); |
| 334 | return; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 335 | } |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 336 | // Pack up the values and broadcast them to everyone |
| 337 | Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); |
| 338 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 339 | if (mCarModeEnabled && mDockState != Intent.EXTRA_DOCK_STATE_CAR) { |
| 340 | // Pretend to be in DOCK_STATE_CAR. |
| 341 | intent.putExtra(Intent.EXTRA_DOCK_STATE, Intent.EXTRA_DOCK_STATE_CAR); |
Tobias Haamel | 154f7a1 | 2010-02-17 11:56:39 -0800 | [diff] [blame] | 342 | } else if (!mCarModeEnabled && mDockState == Intent.EXTRA_DOCK_STATE_CAR) { |
| 343 | // Pretend to be in DOCK_STATE_UNDOCKED. |
| 344 | intent.putExtra(Intent.EXTRA_DOCK_STATE, Intent.EXTRA_DOCK_STATE_UNDOCKED); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 345 | } else { |
| 346 | intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState); |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 347 | } |
Tobias Haamel | 154f7a1 | 2010-02-17 11:56:39 -0800 | [diff] [blame] | 348 | intent.putExtra(Intent.EXTRA_PHYSICAL_DOCK_STATE, mDockState); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 349 | intent.putExtra(Intent.EXTRA_CAR_MODE_ENABLED, mCarModeEnabled); |
| 350 | |
| 351 | // Check if this is Bluetooth Dock |
| 352 | String address = BluetoothService.readDockBluetoothAddress(); |
| 353 | if (address != null) |
| 354 | intent.putExtra(BluetoothDevice.EXTRA_DEVICE, |
| 355 | BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address)); |
| 356 | |
| 357 | // User feedback to confirm dock connection. Particularly |
| 358 | // useful for flaky contact pins... |
| 359 | if (Settings.System.getInt(cr, |
| 360 | Settings.System.DOCK_SOUNDS_ENABLED, 1) == 1) |
| 361 | { |
| 362 | String whichSound = null; |
| 363 | if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 364 | if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_DESK) { |
| 365 | whichSound = Settings.System.DESK_UNDOCK_SOUND; |
| 366 | } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) { |
| 367 | whichSound = Settings.System.CAR_UNDOCK_SOUND; |
| 368 | } |
| 369 | } else { |
| 370 | if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) { |
| 371 | whichSound = Settings.System.DESK_DOCK_SOUND; |
| 372 | } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) { |
| 373 | whichSound = Settings.System.CAR_DOCK_SOUND; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | if (whichSound != null) { |
| 378 | final String soundPath = Settings.System.getString(cr, whichSound); |
| 379 | if (soundPath != null) { |
| 380 | final Uri soundUri = Uri.parse("file://" + soundPath); |
| 381 | if (soundUri != null) { |
| 382 | final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri); |
Daniel Sandler | ec2c88d | 2010-02-20 01:04:57 -0500 | [diff] [blame^] | 383 | if (sfx != null) { |
| 384 | sfx.setStreamType(AudioManager.STREAM_SYSTEM); |
| 385 | sfx.play(); |
| 386 | } |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Send the ordered broadcast; the result receiver will receive after all |
| 393 | // broadcasts have been sent. If any broadcast receiver changes the result |
| 394 | // code from the initial value of RESULT_OK, then the result receiver will |
| 395 | // not launch the corresponding dock application. This gives apps a chance |
| 396 | // to override the behavior and stay in their app even when the device is |
| 397 | // placed into a dock. |
| 398 | mContext.sendStickyOrderedBroadcast( |
| 399 | intent, mResultReceiver, null, Activity.RESULT_OK, null, null); |
| 400 | |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 401 | } |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 402 | break; |
| 403 | case MSG_UPDATE_TWILIGHT: |
| 404 | synchronized (this) { |
| 405 | if (mCarModeEnabled && mLocation != null && mNightMode == MODE_NIGHT_AUTO) { |
| 406 | try { |
| 407 | DockObserver.this.updateTwilight(); |
| 408 | } catch (RemoteException e) { |
| 409 | Log.w(TAG, "Unable to change night mode.", e); |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 413 | break; |
| 414 | case MSG_ENABLE_LOCATION_UPDATES: |
| 415 | if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { |
| 416 | mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, |
| 417 | LOCATION_UPDATE_MS, LOCATION_UPDATE_DISTANCE_METER, mLocationListener); |
| 418 | retrieveLocation(); |
| 419 | if (mLocation != null) { |
| 420 | try { |
| 421 | DockObserver.this.updateTwilight(); |
| 422 | } catch (RemoteException e) { |
| 423 | Log.w(TAG, "Unable to change night mode.", e); |
| 424 | } |
| 425 | } |
| 426 | } else { |
| 427 | long interval = msg.getData().getLong(KEY_LAST_UPDATE_INTERVAL); |
| 428 | interval *= 1.5; |
| 429 | if (interval == 0) { |
| 430 | interval = LOCATION_UPDATE_ENABLE_INTERVAL_MIN; |
| 431 | } else if (interval > LOCATION_UPDATE_ENABLE_INTERVAL_MAX) { |
| 432 | interval = LOCATION_UPDATE_ENABLE_INTERVAL_MAX; |
| 433 | } |
| 434 | Bundle bundle = new Bundle(); |
| 435 | bundle.putLong(KEY_LAST_UPDATE_INTERVAL, interval); |
| 436 | Message newMsg = mHandler.obtainMessage(MSG_ENABLE_LOCATION_UPDATES); |
| 437 | newMsg.setData(bundle); |
| 438 | mHandler.sendMessageDelayed(newMsg, interval); |
| 439 | } |
| 440 | break; |
| 441 | } |
| 442 | } |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 443 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 444 | private void retrieveLocation() { |
| 445 | final Location gpsLocation = |
| 446 | mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
| 447 | Location location; |
| 448 | Criteria criteria = new Criteria(); |
| 449 | criteria.setSpeedRequired(false); |
| 450 | criteria.setAltitudeRequired(false); |
| 451 | criteria.setBearingRequired(false); |
| 452 | final String bestProvider = mLocationManager.getBestProvider(criteria, true); |
| 453 | if (LocationManager.GPS_PROVIDER.equals(bestProvider)) { |
| 454 | location = gpsLocation; |
| 455 | } else { |
| 456 | location = DockObserver.chooseBestLocation(gpsLocation, |
| 457 | mLocationManager.getLastKnownLocation(bestProvider)); |
| 458 | } |
| 459 | // In the case there is no location available (e.g. GPS fix or network location |
| 460 | // is not available yet), the longitude of the location is estimated using the timezone, |
| 461 | // latitude and accuracy are set to get a good average. |
| 462 | if (location == null) { |
| 463 | Time currentTime = new Time(); |
| 464 | currentTime.set(System.currentTimeMillis()); |
| 465 | double lngOffset = FACTOR_GMT_OFFSET_LONGITUDE * currentTime.gmtoff |
| 466 | - (currentTime.isDst > 0 ? 3600 : 0); |
| 467 | location = new Location("fake"); |
| 468 | location.setLongitude(lngOffset); |
| 469 | location.setLatitude(59.95); |
| 470 | location.setAccuracy(417000.0f); |
| 471 | location.setTime(System.currentTimeMillis()); |
| 472 | } |
| 473 | synchronized (this) { |
| 474 | mLocation = location; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | }; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 478 | |
Tobias Haamel | 154f7a1 | 2010-02-17 11:56:39 -0800 | [diff] [blame] | 479 | private void adjustStatusBarCarMode() { |
| 480 | if (mStatusBarManager == null) { |
| 481 | mStatusBarManager = (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE); |
| 482 | } |
| 483 | |
| 484 | // Fear not: StatusBarService manages a list of requests to disable |
| 485 | // features of the status bar; these are ORed together to form the |
| 486 | // active disabled list. So if (for example) the device is locked and |
| 487 | // the status bar should be totally disabled, the calls below will |
| 488 | // have no effect until the device is unlocked. |
| 489 | if (mStatusBarManager != null) { |
| 490 | long ident = Binder.clearCallingIdentity(); |
| 491 | mStatusBarManager.disable(mCarModeEnabled |
| 492 | ? StatusBarManager.DISABLE_NOTIFICATION_TICKER |
| 493 | : StatusBarManager.DISABLE_NONE); |
| 494 | Binder.restoreCallingIdentity(ident); |
| 495 | } |
| 496 | |
| 497 | if (mNotificationManager == null) { |
| 498 | mNotificationManager = (NotificationManager) |
| 499 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 500 | } |
| 501 | |
| 502 | if (mNotificationManager != null) { |
| 503 | long ident = Binder.clearCallingIdentity(); |
| 504 | if (mCarModeEnabled) { |
| 505 | Intent carModeOffIntent = new Intent(mContext, DisableCarModeActivity.class); |
| 506 | |
| 507 | Notification n = new Notification(); |
| 508 | n.icon = R.drawable.stat_notify_car_mode; |
| 509 | n.defaults = Notification.DEFAULT_LIGHTS; |
| 510 | n.flags = Notification.FLAG_ONGOING_EVENT; |
| 511 | n.when = 0; |
| 512 | n.setLatestEventInfo( |
| 513 | mContext, |
| 514 | mContext.getString(R.string.car_mode_disable_notification_title), |
| 515 | mContext.getString(R.string.car_mode_disable_notification_message), |
| 516 | PendingIntent.getActivity(mContext, 0, carModeOffIntent, 0)); |
| 517 | mNotificationManager.notify(0, n); |
| 518 | } else { |
| 519 | mNotificationManager.cancel(0); |
| 520 | } |
| 521 | Binder.restoreCallingIdentity(ident); |
| 522 | } |
| 523 | } |
| 524 | |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 525 | private void setCarMode(boolean enabled) throws RemoteException { |
| 526 | mCarModeEnabled = enabled; |
| 527 | if (enabled) { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 528 | if (mNightMode == MODE_NIGHT_AUTO) { |
| 529 | updateTwilight(); |
| 530 | } else { |
| 531 | setMode(Configuration.UI_MODE_TYPE_CAR, mNightMode << 4); |
| 532 | } |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 533 | } else { |
| 534 | // Disabling the car mode clears the night mode. |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 535 | setMode(Configuration.UI_MODE_TYPE_NORMAL, |
| 536 | Configuration.UI_MODE_NIGHT_UNDEFINED); |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 537 | } |
Tobias Haamel | 154f7a1 | 2010-02-17 11:56:39 -0800 | [diff] [blame] | 538 | adjustStatusBarCarMode(); |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | private void setMode(int modeType, int modeNight) throws RemoteException { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 542 | long ident = Binder.clearCallingIdentity(); |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 543 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 544 | Configuration config = am.getConfiguration(); |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 545 | if (config.uiMode != (modeType | modeNight)) { |
| 546 | config.uiMode = modeType | modeNight; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 547 | am.updateConfiguration(config); |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 548 | } |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 549 | Binder.restoreCallingIdentity(ident); |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | private void setNightMode(int mode) throws RemoteException { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 553 | if (mNightMode != mode) { |
| 554 | mNightMode = mode; |
| 555 | switch (mode) { |
| 556 | case MODE_NIGHT_NO: |
| 557 | case MODE_NIGHT_YES: |
| 558 | setMode(Configuration.UI_MODE_TYPE_CAR, mode << 4); |
| 559 | break; |
| 560 | case MODE_NIGHT_AUTO: |
| 561 | long ident = Binder.clearCallingIdentity(); |
| 562 | updateTwilight(); |
| 563 | Binder.restoreCallingIdentity(ident); |
| 564 | break; |
| 565 | default: |
| 566 | setMode(Configuration.UI_MODE_TYPE_CAR, MODE_NIGHT_NO << 4); |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | private void updateTwilight() throws RemoteException { |
| 573 | synchronized (this) { |
| 574 | if (mLocation == null) { |
| 575 | return; |
| 576 | } |
| 577 | final long currentTime = System.currentTimeMillis(); |
| 578 | int nightMode; |
| 579 | // calculate current twilight |
| 580 | TwilightCalculator tw = new TwilightCalculator(); |
| 581 | tw.calculateTwilight(currentTime, |
| 582 | mLocation.getLatitude(), mLocation.getLongitude()); |
| 583 | if (tw.mState == TwilightCalculator.DAY) { |
| 584 | nightMode = MODE_NIGHT_NO; |
| 585 | } else { |
| 586 | nightMode = MODE_NIGHT_YES; |
| 587 | } |
| 588 | |
| 589 | // schedule next update |
| 590 | final int mLastTwilightState = tw.mState; |
| 591 | // add some extra time to be on the save side. |
| 592 | long nextUpdate = DateUtils.MINUTE_IN_MILLIS; |
| 593 | if (currentTime > tw.mSunset) { |
| 594 | // next update should be on the following day |
| 595 | tw.calculateTwilight(currentTime |
| 596 | + DateUtils.DAY_IN_MILLIS, mLocation.getLatitude(), |
| 597 | mLocation.getLongitude()); |
| 598 | } |
| 599 | |
| 600 | if (mLastTwilightState == TwilightCalculator.NIGHT) { |
| 601 | nextUpdate += tw.mSunrise; |
| 602 | } else { |
| 603 | nextUpdate += tw.mSunset; |
| 604 | } |
| 605 | |
| 606 | Intent updateIntent = new Intent(ACTION_UPDATE_NIGHT_MODE); |
| 607 | PendingIntent pendingIntent = |
| 608 | PendingIntent.getBroadcast(mContext, 0, updateIntent, 0); |
| 609 | mAlarmManager.cancel(pendingIntent); |
| 610 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdate, pendingIntent); |
| 611 | |
| 612 | // set current mode |
| 613 | setMode(Configuration.UI_MODE_TYPE_CAR, nightMode << 4); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Check which of two locations is better by comparing the distance a device |
| 619 | * could have cover since the last timestamp of the location. |
| 620 | * |
| 621 | * @param location first location |
| 622 | * @param otherLocation second location |
| 623 | * @return one of the two locations |
| 624 | */ |
| 625 | protected static Location chooseBestLocation(Location location, Location otherLocation) { |
| 626 | if (location == null) { |
| 627 | return otherLocation; |
| 628 | } |
| 629 | if (otherLocation == null) { |
| 630 | return location; |
| 631 | } |
| 632 | final long currentTime = System.currentTimeMillis(); |
| 633 | float gpsPotentialMove = MAX_VELOCITY_M_MS * (currentTime - location.getTime()) |
| 634 | + location.getAccuracy(); |
| 635 | float otherPotentialMove = MAX_VELOCITY_M_MS * (currentTime - otherLocation.getTime()) |
| 636 | + otherLocation.getAccuracy(); |
| 637 | if (gpsPotentialMove < otherPotentialMove) { |
| 638 | return location; |
| 639 | } else { |
| 640 | return otherLocation; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Wrapper class implementing the IUiModeManager interface. |
| 646 | */ |
| 647 | private final IUiModeManager.Stub mBinder = new IUiModeManager.Stub() { |
| 648 | |
| 649 | public void disableCarMode() throws RemoteException { |
| 650 | if (mCarModeEnabled) { |
| 651 | setCarMode(false); |
| 652 | update(); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | public void enableCarMode() throws RemoteException { |
| 657 | mContext.enforceCallingOrSelfPermission( |
| 658 | android.Manifest.permission.ENABLE_CAR_MODE, |
| 659 | "Need ENABLE_CAR_MODE permission"); |
| 660 | if (!mCarModeEnabled) { |
| 661 | setCarMode(true); |
| 662 | update(); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | public void setNightMode(int mode) throws RemoteException { |
| 667 | if (mCarModeEnabled) { |
| 668 | DockObserver.this.setNightMode(mode); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | public int getNightMode() throws RemoteException { |
| 673 | return mNightMode; |
| 674 | } |
| 675 | }; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 676 | } |