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