blob: c90736876ee8807fdff0941e08795525b0fd0fd5 [file] [log] [blame]
Dan Murphyc9f4eaf2009-08-12 15:15:43 -05001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070019import android.app.Activity;
Tobias Haamel27b28b32010-02-09 23:09:17 +010020import android.app.ActivityManagerNative;
21import android.app.IActivityManager;
22import android.app.IUiModeManager;
Mike Lockwood733fdf32009-09-28 19:08:53 -040023import android.app.KeyguardManager;
Daniel Sandlera0430a12010-02-11 23:35:49 -050024import android.app.StatusBarManager;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080025import android.bluetooth.BluetoothAdapter;
26import android.bluetooth.BluetoothDevice;
Mike Lockwood9092ab42009-09-16 13:01:32 -040027import android.content.ActivityNotFoundException;
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070028import android.content.BroadcastReceiver;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050029import android.content.Context;
30import android.content.Intent;
Tobias Haamel27b28b32010-02-09 23:09:17 +010031import android.content.res.Configuration;
32import android.os.Binder;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050033import android.os.Handler;
34import android.os.Message;
Tobias Haamel27b28b32010-02-09 23:09:17 +010035import android.os.RemoteException;
36import android.os.ServiceManager;
Ken Schultzf02c0742009-09-10 18:37:37 -050037import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050038import android.os.UEventObserver;
Dianne Hackborn49493342009-10-02 10:44:41 -070039import android.provider.Settings;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080040import android.server.BluetoothService;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050041import android.util.Log;
42
Mike Lockwood733fdf32009-09-28 19:08:53 -040043import com.android.internal.widget.LockPatternUtils;
44
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050045import java.io.FileNotFoundException;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080046import java.io.FileReader;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050047
48/**
49 * <p>DockObserver monitors for a docking station.
50 */
51class 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 Haamel27b28b32010-02-09 23:09:17 +010058 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 Lockwoodd0e82ce2009-08-27 16:19:07 -070062 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Tobias Haamel27b28b32010-02-09 23:09:17 +010063 private int mNightMode = MODE_NIGHT_NO;
64 private boolean mCarModeEnabled = false;
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070065 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050066
67 private final Context mContext;
68
Ken Schultzf02c0742009-09-10 18:37:37 -050069 private PowerManagerService mPowerManager;
Mike Lockwood733fdf32009-09-28 19:08:53 -040070
71 private KeyguardManager.KeyguardLock mKeyguardLock;
72 private boolean mKeyguardDisabled;
73 private LockPatternUtils mLockPatternUtils;
74
Daniel Sandlera0430a12010-02-11 23:35:49 -050075 private StatusBarManager mStatusBarManager;
76
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070077 // 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 Ganesh3fbf7b62009-12-02 17:28:38 -080087
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070088 // Launch a dock activity
89 String category;
Tobias Haamel27b28b32010-02-09 23:09:17 +010090 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 LeBeau1f6c7e62009-09-19 18:06:52 -070096 }
97 if (category != null) {
98 intent = new Intent(Intent.ACTION_MAIN);
99 intent.addCategory(category);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700100 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
101 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Mike LeBeau1f6c7e62009-09-19 18:06:52 -0700102 try {
103 mContext.startActivity(intent);
104 } catch (ActivityNotFoundException e) {
105 Log.w(TAG, e.getCause());
106 }
107 }
108 }
109 };
Ken Schultzf02c0742009-09-10 18:37:37 -0500110
111 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500112 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -0500113 mPowerManager = pm;
Jim Miller31f90b62010-01-20 13:35:20 -0800114 mLockPatternUtils = new LockPatternUtils(context);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500115 init(); // set initial status
Tobias Haamel27b28b32010-02-09 23:09:17 +0100116
117 ServiceManager.addService("uimode", mBinder);
118
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700119 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500120 }
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 Lockwoodd0e82ce2009-08-27 16:19:07 -0700128 synchronized (this) {
129 try {
130 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
131 if (newState != mDockState) {
Mike Lockwood1d069922009-11-11 18:09:25 -0500132 int oldState = mDockState;
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700133 mDockState = newState;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100134 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 Lockwoodd0e82ce2009-08-27 16:19:07 -0700142 if (mSystemReady) {
Mike Lockwood1d069922009-11-11 18:09:25 -0500143 // 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 Lockwoodd0e82ce2009-08-27 16:19:07 -0700151 update();
152 }
153 }
154 } catch (NumberFormatException e) {
155 Log.e(TAG, "Could not parse switch state from event " + event);
156 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500157 }
158 }
159
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700160 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500161 char[] buffer = new char[1024];
162
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500163 try {
164 FileReader file = new FileReader(DOCK_STATE_PATH);
165 int len = file.read(buffer, 0, 1024);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700166 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500167
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 Murphyc9f4eaf2009-08-12 15:15:43 -0500173 }
174
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700175 void systemReady() {
176 synchronized (this) {
Mike Lockwood733fdf32009-09-28 19:08:53 -0400177 KeyguardManager keyguardManager =
178 (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
179 mKeyguardLock = keyguardManager.newKeyguardLock(TAG);
180
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700181 // don't bother broadcasting undocked here
182 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
183 update();
184 }
185 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500186 }
187 }
188
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700189 private final void update() {
190 mHandler.sendEmptyMessage(0);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500191 }
192
193 private final Handler mHandler = new Handler() {
194 @Override
195 public void handleMessage(Message msg) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700196 synchronized (this) {
Dianne Hackborn49493342009-10-02 10:44:41 -0700197 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 Lockwoodd0e82ce2009-08-27 16:19:07 -0700203 // Pack up the values and broadcast them to everyone
204 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800205 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100206 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 Ganesh3fbf7b62009-12-02 17:28:38 -0800213
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 LeBeau1f6c7e62009-09-19 18:06:52 -0700220 // 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 Murphyc9f4eaf2009-08-12 15:15:43 -0500228 }
229 }
230 };
Tobias Haamel27b28b32010-02-09 23:09:17 +0100231
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 Sandlera0430a12010-02-11 23:35:49 -0500240
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 Haamel27b28b32010-02-09 23:09:17 +0100255 }
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 Murphyc9f4eaf2009-08-12 15:15:43 -0500318}