blob: 742a7d8c802e5f1cdf65924152769a8c45556af9 [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;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080024import android.bluetooth.BluetoothAdapter;
25import android.bluetooth.BluetoothDevice;
Mike Lockwood9092ab42009-09-16 13:01:32 -040026import android.content.ActivityNotFoundException;
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070027import android.content.BroadcastReceiver;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050028import android.content.Context;
29import android.content.Intent;
Tobias Haamel27b28b32010-02-09 23:09:17 +010030import android.content.res.Configuration;
31import android.os.Binder;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050032import android.os.Handler;
33import android.os.Message;
Tobias Haamel27b28b32010-02-09 23:09:17 +010034import android.os.RemoteException;
35import android.os.ServiceManager;
Ken Schultzf02c0742009-09-10 18:37:37 -050036import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050037import android.os.UEventObserver;
Dianne Hackborn49493342009-10-02 10:44:41 -070038import android.provider.Settings;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080039import android.server.BluetoothService;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050040import android.util.Log;
41
Mike Lockwood733fdf32009-09-28 19:08:53 -040042import com.android.internal.widget.LockPatternUtils;
43
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050044import java.io.FileNotFoundException;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080045import java.io.FileReader;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050046
47/**
48 * <p>DockObserver monitors for a docking station.
49 */
50class 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 Haamel27b28b32010-02-09 23:09:17 +010057 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 Lockwoodd0e82ce2009-08-27 16:19:07 -070061 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Tobias Haamel27b28b32010-02-09 23:09:17 +010062 private int mNightMode = MODE_NIGHT_NO;
63 private boolean mCarModeEnabled = false;
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070064 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050065
66 private final Context mContext;
67
Ken Schultzf02c0742009-09-10 18:37:37 -050068 private PowerManagerService mPowerManager;
Mike Lockwood733fdf32009-09-28 19:08:53 -040069
70 private KeyguardManager.KeyguardLock mKeyguardLock;
71 private boolean mKeyguardDisabled;
72 private LockPatternUtils mLockPatternUtils;
73
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070074 // 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 Ganesh3fbf7b62009-12-02 17:28:38 -080084
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070085 // Launch a dock activity
86 String category;
Tobias Haamel27b28b32010-02-09 23:09:17 +010087 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 LeBeau1f6c7e62009-09-19 18:06:52 -070093 }
94 if (category != null) {
95 intent = new Intent(Intent.ACTION_MAIN);
96 intent.addCategory(category);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070097 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
98 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070099 try {
100 mContext.startActivity(intent);
101 } catch (ActivityNotFoundException e) {
102 Log.w(TAG, e.getCause());
103 }
104 }
105 }
106 };
Ken Schultzf02c0742009-09-10 18:37:37 -0500107
108 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500109 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -0500110 mPowerManager = pm;
Jim Miller31f90b62010-01-20 13:35:20 -0800111 mLockPatternUtils = new LockPatternUtils(context);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500112 init(); // set initial status
Tobias Haamel27b28b32010-02-09 23:09:17 +0100113
114 ServiceManager.addService("uimode", mBinder);
115
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700116 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500117 }
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 Lockwoodd0e82ce2009-08-27 16:19:07 -0700125 synchronized (this) {
126 try {
127 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
128 if (newState != mDockState) {
Mike Lockwood1d069922009-11-11 18:09:25 -0500129 int oldState = mDockState;
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700130 mDockState = newState;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100131 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 Lockwoodd0e82ce2009-08-27 16:19:07 -0700139 if (mSystemReady) {
Mike Lockwood1d069922009-11-11 18:09:25 -0500140 // 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 Lockwoodd0e82ce2009-08-27 16:19:07 -0700148 update();
149 }
150 }
151 } catch (NumberFormatException e) {
152 Log.e(TAG, "Could not parse switch state from event " + event);
153 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500154 }
155 }
156
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700157 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500158 char[] buffer = new char[1024];
159
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500160 try {
161 FileReader file = new FileReader(DOCK_STATE_PATH);
162 int len = file.read(buffer, 0, 1024);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700163 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500164
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 Murphyc9f4eaf2009-08-12 15:15:43 -0500170 }
171
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700172 void systemReady() {
173 synchronized (this) {
Mike Lockwood733fdf32009-09-28 19:08:53 -0400174 KeyguardManager keyguardManager =
175 (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
176 mKeyguardLock = keyguardManager.newKeyguardLock(TAG);
177
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700178 // don't bother broadcasting undocked here
179 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
180 update();
181 }
182 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500183 }
184 }
185
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700186 private final void update() {
187 mHandler.sendEmptyMessage(0);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500188 }
189
190 private final Handler mHandler = new Handler() {
191 @Override
192 public void handleMessage(Message msg) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700193 synchronized (this) {
Dianne Hackborn49493342009-10-02 10:44:41 -0700194 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 Lockwoodd0e82ce2009-08-27 16:19:07 -0700200 // Pack up the values and broadcast them to everyone
201 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800202 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Tobias Haamel27b28b32010-02-09 23:09:17 +0100203 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 Ganesh3fbf7b62009-12-02 17:28:38 -0800210
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 LeBeau1f6c7e62009-09-19 18:06:52 -0700217 // 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 Murphyc9f4eaf2009-08-12 15:15:43 -0500225 }
226 }
227 };
Tobias Haamel27b28b32010-02-09 23:09:17 +0100228
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 Murphyc9f4eaf2009-08-12 15:15:43 -0500300}