| 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; |
| 21 | import android.app.IActivityManager; |
| 22 | import android.app.IUiModeManager; |
| Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 23 | import android.app.KeyguardManager; |
| Daniel Sandler | a0430a1 | 2010-02-11 23:35:49 -0500 | [diff] [blame^] | 24 | import android.app.StatusBarManager; |
| Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 25 | import android.bluetooth.BluetoothAdapter; |
| 26 | import android.bluetooth.BluetoothDevice; |
| Mike Lockwood | 9092ab4 | 2009-09-16 13:01:32 -0400 | [diff] [blame] | 27 | import android.content.ActivityNotFoundException; |
| Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 31 | import android.content.res.Configuration; |
| 32 | import android.os.Binder; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 33 | import android.os.Handler; |
| 34 | import android.os.Message; |
| Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 35 | import android.os.RemoteException; |
| 36 | import android.os.ServiceManager; |
| Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 37 | import android.os.SystemClock; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 38 | import android.os.UEventObserver; |
| Dianne Hackborn | 4949334 | 2009-10-02 10:44:41 -0700 | [diff] [blame] | 39 | import android.provider.Settings; |
| Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 40 | import android.server.BluetoothService; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 41 | import android.util.Log; |
| 42 | |
| Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 43 | import com.android.internal.widget.LockPatternUtils; |
| 44 | |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 45 | import java.io.FileNotFoundException; |
| Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 46 | import java.io.FileReader; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * <p>DockObserver monitors for a docking station. |
| 50 | */ |
| 51 | class DockObserver extends UEventObserver { |
| 52 | private static final String TAG = DockObserver.class.getSimpleName(); |
| 53 | private static final boolean LOG = false; |
| 54 | |
| 55 | private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock"; |
| 56 | private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state"; |
| 57 | |
| Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 58 | public static final int MODE_NIGHT_AUTO = Configuration.UI_MODE_NIGHT_MASK >> 4; |
| 59 | public static final int MODE_NIGHT_NO = Configuration.UI_MODE_NIGHT_NO >> 4; |
| 60 | public static final int MODE_NIGHT_YES = Configuration.UI_MODE_NIGHT_YES >> 4; |
| 61 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 62 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 63 | private int mNightMode = MODE_NIGHT_NO; |
| 64 | private boolean mCarModeEnabled = false; |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 65 | private boolean mSystemReady; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 66 | |
| 67 | private final Context mContext; |
| 68 | |
| Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 69 | private PowerManagerService mPowerManager; |
| Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 70 | |
| 71 | private KeyguardManager.KeyguardLock mKeyguardLock; |
| 72 | private boolean mKeyguardDisabled; |
| 73 | private LockPatternUtils mLockPatternUtils; |
| 74 | |
| Daniel Sandler | a0430a1 | 2010-02-11 23:35:49 -0500 | [diff] [blame^] | 75 | private StatusBarManager mStatusBarManager; |
| 76 | |
| Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 77 | // The broadcast receiver which receives the result of the ordered broadcast sent when |
| 78 | // the dock state changes. The original ordered broadcast is sent with an initial result |
| 79 | // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g., |
| 80 | // to RESULT_CANCELED, then the intent to start a dock app will not be sent. |
| 81 | private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() { |
| 82 | @Override |
| 83 | public void onReceive(Context context, Intent intent) { |
| 84 | if (getResultCode() != Activity.RESULT_OK) { |
| 85 | return; |
| 86 | } |
| Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 87 | |
| Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 88 | // Launch a dock activity |
| 89 | String category; |
| Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 90 | if (mCarModeEnabled || mDockState == Intent.EXTRA_DOCK_STATE_CAR) { |
| 91 | category = Intent.CATEGORY_CAR_DOCK; |
| 92 | } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) { |
| 93 | category = Intent.CATEGORY_DESK_DOCK; |
| 94 | } else { |
| 95 | category = null; |
| Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 96 | } |
| 97 | if (category != null) { |
| 98 | intent = new Intent(Intent.ACTION_MAIN); |
| 99 | intent.addCategory(category); |
| Dianne Hackborn | 9bfb707 | 2009-09-22 11:37:40 -0700 | [diff] [blame] | 100 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 101 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 102 | try { |
| 103 | mContext.startActivity(intent); |
| 104 | } catch (ActivityNotFoundException e) { |
| 105 | Log.w(TAG, e.getCause()); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | }; |
| Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 110 | |
| 111 | public DockObserver(Context context, PowerManagerService pm) { |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 112 | mContext = context; |
| Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 113 | mPowerManager = pm; |
| Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 114 | mLockPatternUtils = new LockPatternUtils(context); |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 115 | init(); // set initial status |
| Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 116 | |
| 117 | ServiceManager.addService("uimode", mBinder); |
| 118 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 119 | startObserving(DOCK_UEVENT_MATCH); |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void onUEvent(UEventObserver.UEvent event) { |
| 124 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 125 | Log.v(TAG, "Dock UEVENT: " + event.toString()); |
| 126 | } |
| 127 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 128 | synchronized (this) { |
| 129 | try { |
| 130 | int newState = Integer.parseInt(event.get("SWITCH_STATE")); |
| 131 | if (newState != mDockState) { |
| Mike Lockwood | 1d06992 | 2009-11-11 18:09:25 -0500 | [diff] [blame] | 132 | int oldState = mDockState; |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 133 | mDockState = newState; |
| Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 134 | boolean carModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR; |
| 135 | if (mCarModeEnabled != carModeEnabled) { |
| 136 | try { |
| 137 | setCarMode(carModeEnabled); |
| 138 | } catch (RemoteException e1) { |
| 139 | Log.w(TAG, "Unable to change car mode.", e1); |
| 140 | } |
| 141 | } |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 142 | if (mSystemReady) { |
| Mike Lockwood | 1d06992 | 2009-11-11 18:09:25 -0500 | [diff] [blame] | 143 | // Don't force screen on when undocking from the desk dock. |
| 144 | // The change in power state will do this anyway. |
| 145 | // FIXME - we should be configurable. |
| 146 | if (oldState != Intent.EXTRA_DOCK_STATE_DESK || |
| 147 | newState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 148 | mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), |
| 149 | false, true); |
| 150 | } |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 151 | update(); |
| 152 | } |
| 153 | } |
| 154 | } catch (NumberFormatException e) { |
| 155 | Log.e(TAG, "Could not parse switch state from event " + event); |
| 156 | } |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 160 | private final void init() { |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 161 | char[] buffer = new char[1024]; |
| 162 | |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 163 | try { |
| 164 | FileReader file = new FileReader(DOCK_STATE_PATH); |
| 165 | int len = file.read(buffer, 0, 1024); |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 166 | mDockState = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 167 | |
| 168 | } catch (FileNotFoundException e) { |
| 169 | Log.w(TAG, "This kernel does not have dock station support"); |
| 170 | } catch (Exception e) { |
| 171 | Log.e(TAG, "" , e); |
| 172 | } |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 175 | void systemReady() { |
| 176 | synchronized (this) { |
| Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 177 | KeyguardManager keyguardManager = |
| 178 | (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE); |
| 179 | mKeyguardLock = keyguardManager.newKeyguardLock(TAG); |
| 180 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 181 | // don't bother broadcasting undocked here |
| 182 | if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 183 | update(); |
| 184 | } |
| 185 | mSystemReady = true; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 189 | private final void update() { |
| 190 | mHandler.sendEmptyMessage(0); |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | private final Handler mHandler = new Handler() { |
| 194 | @Override |
| 195 | public void handleMessage(Message msg) { |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 196 | synchronized (this) { |
| Dianne Hackborn | 4949334 | 2009-10-02 10:44:41 -0700 | [diff] [blame] | 197 | Log.i(TAG, "Dock state changed: " + mDockState); |
| 198 | if (Settings.Secure.getInt(mContext.getContentResolver(), |
| 199 | Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { |
| 200 | Log.i(TAG, "Device not provisioned, skipping dock broadcast"); |
| 201 | return; |
| 202 | } |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 203 | // Pack up the values and broadcast them to everyone |
| 204 | Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); |
| Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 205 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 206 | if (mCarModeEnabled && mDockState != Intent.EXTRA_DOCK_STATE_CAR) { |
| 207 | // Pretend to be in DOCK_STATE_CAR. |
| 208 | intent.putExtra(Intent.EXTRA_DOCK_STATE, Intent.EXTRA_DOCK_STATE_CAR); |
| 209 | } else { |
| 210 | intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState); |
| 211 | } |
| 212 | intent.putExtra(Intent.EXTRA_CAR_MODE_ENABLED, mCarModeEnabled); |
| Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 213 | |
| 214 | // Check if this is Bluetooth Dock |
| 215 | String address = BluetoothService.readDockBluetoothAddress(); |
| 216 | if (address != null) |
| 217 | intent.putExtra(BluetoothDevice.EXTRA_DEVICE, |
| 218 | BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address)); |
| 219 | |
| Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 220 | // Send the ordered broadcast; the result receiver will receive after all |
| 221 | // broadcasts have been sent. If any broadcast receiver changes the result |
| 222 | // code from the initial value of RESULT_OK, then the result receiver will |
| 223 | // not launch the corresponding dock application. This gives apps a chance |
| 224 | // to override the behavior and stay in their app even when the device is |
| 225 | // placed into a dock. |
| 226 | mContext.sendStickyOrderedBroadcast( |
| 227 | intent, mResultReceiver, null, Activity.RESULT_OK, null, null); |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | }; |
| Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 231 | |
| 232 | private void setCarMode(boolean enabled) throws RemoteException { |
| 233 | mCarModeEnabled = enabled; |
| 234 | if (enabled) { |
| 235 | setMode(Configuration.UI_MODE_TYPE_CAR, mNightMode); |
| 236 | } else { |
| 237 | // Disabling the car mode clears the night mode. |
| 238 | setMode(Configuration.UI_MODE_TYPE_NORMAL, MODE_NIGHT_NO); |
| 239 | } |
| Daniel Sandler | a0430a1 | 2010-02-11 23:35:49 -0500 | [diff] [blame^] | 240 | |
| 241 | if (mStatusBarManager == null) { |
| 242 | mStatusBarManager = (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE); |
| 243 | } |
| 244 | |
| 245 | // Fear not: StatusBarService manages a list of requests to disable |
| 246 | // features of the status bar; these are ORed together to form the |
| 247 | // active disabled list. So if (for example) the device is locked and |
| 248 | // the status bar should be totally disabled, the calls below will |
| 249 | // have no effect until the device is unlocked. |
| 250 | if (mStatusBarManager != null) { |
| 251 | mStatusBarManager.disable(enabled |
| 252 | ? StatusBarManager.DISABLE_NOTIFICATION_TICKER |
| 253 | : StatusBarManager.DISABLE_NONE); |
| 254 | } |
| Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | private void setMode(int modeType, int modeNight) throws RemoteException { |
| 258 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 259 | Configuration config = am.getConfiguration(); |
| 260 | |
| 261 | if (config.uiMode != (modeType | modeNight)) { |
| 262 | config.uiMode = modeType | modeNight; |
| 263 | long ident = Binder.clearCallingIdentity(); |
| 264 | am.updateConfiguration(config); |
| 265 | Binder.restoreCallingIdentity(ident); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private void setNightMode(int mode) throws RemoteException { |
| 270 | mNightMode = mode; |
| 271 | switch (mode) { |
| 272 | case MODE_NIGHT_NO: |
| 273 | case MODE_NIGHT_YES: |
| 274 | setMode(Configuration.UI_MODE_TYPE_CAR, mode << 4); |
| 275 | break; |
| 276 | case MODE_NIGHT_AUTO: |
| 277 | // FIXME: not yet supported, this functionality will be |
| 278 | // added in a separate change. |
| 279 | break; |
| 280 | default: |
| 281 | setMode(Configuration.UI_MODE_TYPE_CAR, MODE_NIGHT_NO << 4); |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Wrapper class implementing the IUiModeManager interface. |
| 288 | */ |
| 289 | private final IUiModeManager.Stub mBinder = new IUiModeManager.Stub() { |
| 290 | |
| 291 | public void disableCarMode() throws RemoteException { |
| 292 | if (mCarModeEnabled) { |
| 293 | setCarMode(false); |
| 294 | update(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | public void enableCarMode() throws RemoteException { |
| 299 | mContext.enforceCallingOrSelfPermission( |
| 300 | android.Manifest.permission.ENABLE_CAR_MODE, |
| 301 | "Need ENABLE_CAR_MODE permission"); |
| 302 | if (!mCarModeEnabled) { |
| 303 | setCarMode(true); |
| 304 | update(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | public void setNightMode(int mode) throws RemoteException { |
| 309 | if (mCarModeEnabled) { |
| 310 | DockObserver.this.setNightMode(mode); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | public int getNightMode() throws RemoteException { |
| 315 | return mNightMode; |
| 316 | } |
| 317 | }; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 318 | } |