blob: 363e93e6af1a9e2aa1cd774b40ca18a870eef1a2 [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;
Mike Lockwood733fdf32009-09-28 19:08:53 -040020import android.app.KeyguardManager;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080021import android.bluetooth.BluetoothAdapter;
22import android.bluetooth.BluetoothDevice;
Mike Lockwood9092ab42009-09-16 13:01:32 -040023import android.content.ActivityNotFoundException;
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070024import android.content.BroadcastReceiver;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050025import android.content.Context;
26import android.content.Intent;
27import android.os.Handler;
28import android.os.Message;
Ken Schultzf02c0742009-09-10 18:37:37 -050029import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050030import android.os.UEventObserver;
Dianne Hackborn49493342009-10-02 10:44:41 -070031import android.provider.Settings;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080032import android.server.BluetoothService;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050033import android.util.Log;
34
Mike Lockwood733fdf32009-09-28 19:08:53 -040035import com.android.internal.widget.LockPatternUtils;
36
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050037import java.io.FileNotFoundException;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080038import java.io.FileReader;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050039
40/**
41 * <p>DockObserver monitors for a docking station.
42 */
43class DockObserver extends UEventObserver {
44 private static final String TAG = DockObserver.class.getSimpleName();
45 private static final boolean LOG = false;
46
47 private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock";
48 private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state";
49
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070050 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
51 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050052
53 private final Context mContext;
54
Ken Schultzf02c0742009-09-10 18:37:37 -050055 private PowerManagerService mPowerManager;
Mike Lockwood733fdf32009-09-28 19:08:53 -040056
57 private KeyguardManager.KeyguardLock mKeyguardLock;
58 private boolean mKeyguardDisabled;
59 private LockPatternUtils mLockPatternUtils;
60
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070061 // The broadcast receiver which receives the result of the ordered broadcast sent when
62 // the dock state changes. The original ordered broadcast is sent with an initial result
63 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
64 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
65 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
66 @Override
67 public void onReceive(Context context, Intent intent) {
68 if (getResultCode() != Activity.RESULT_OK) {
69 return;
70 }
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080071
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070072 // Launch a dock activity
73 String category;
74 switch (mDockState) {
75 case Intent.EXTRA_DOCK_STATE_CAR:
76 category = Intent.CATEGORY_CAR_DOCK;
77 break;
78 case Intent.EXTRA_DOCK_STATE_DESK:
79 category = Intent.CATEGORY_DESK_DOCK;
80 break;
81 default:
82 category = null;
83 break;
84 }
85 if (category != null) {
86 intent = new Intent(Intent.ACTION_MAIN);
87 intent.addCategory(category);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070088 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
89 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070090 try {
91 mContext.startActivity(intent);
92 } catch (ActivityNotFoundException e) {
93 Log.w(TAG, e.getCause());
94 }
95 }
96 }
97 };
Ken Schultzf02c0742009-09-10 18:37:37 -050098
99 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500100 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -0500101 mPowerManager = pm;
Jim Miller31f90b62010-01-20 13:35:20 -0800102 mLockPatternUtils = new LockPatternUtils(context);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500103 init(); // set initial status
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700104 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500105 }
106
107 @Override
108 public void onUEvent(UEventObserver.UEvent event) {
109 if (Log.isLoggable(TAG, Log.VERBOSE)) {
110 Log.v(TAG, "Dock UEVENT: " + event.toString());
111 }
112
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700113 synchronized (this) {
114 try {
115 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
116 if (newState != mDockState) {
Mike Lockwood1d069922009-11-11 18:09:25 -0500117 int oldState = mDockState;
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700118 mDockState = newState;
119 if (mSystemReady) {
Mike Lockwood1d069922009-11-11 18:09:25 -0500120 // Don't force screen on when undocking from the desk dock.
121 // The change in power state will do this anyway.
122 // FIXME - we should be configurable.
123 if (oldState != Intent.EXTRA_DOCK_STATE_DESK ||
124 newState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
125 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(),
126 false, true);
127 }
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700128 update();
129 }
130 }
131 } catch (NumberFormatException e) {
132 Log.e(TAG, "Could not parse switch state from event " + event);
133 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500134 }
135 }
136
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700137 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500138 char[] buffer = new char[1024];
139
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500140 try {
141 FileReader file = new FileReader(DOCK_STATE_PATH);
142 int len = file.read(buffer, 0, 1024);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700143 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500144
145 } catch (FileNotFoundException e) {
146 Log.w(TAG, "This kernel does not have dock station support");
147 } catch (Exception e) {
148 Log.e(TAG, "" , e);
149 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500150 }
151
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700152 void systemReady() {
153 synchronized (this) {
Mike Lockwood733fdf32009-09-28 19:08:53 -0400154 KeyguardManager keyguardManager =
155 (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
156 mKeyguardLock = keyguardManager.newKeyguardLock(TAG);
157
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700158 // don't bother broadcasting undocked here
159 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
160 update();
161 }
162 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500163 }
164 }
165
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700166 private final void update() {
167 mHandler.sendEmptyMessage(0);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500168 }
169
170 private final Handler mHandler = new Handler() {
171 @Override
172 public void handleMessage(Message msg) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700173 synchronized (this) {
Dianne Hackborn49493342009-10-02 10:44:41 -0700174 Log.i(TAG, "Dock state changed: " + mDockState);
175 if (Settings.Secure.getInt(mContext.getContentResolver(),
176 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
177 Log.i(TAG, "Device not provisioned, skipping dock broadcast");
178 return;
179 }
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700180 // Pack up the values and broadcast them to everyone
181 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800182 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700183 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -0800184
185 // Check if this is Bluetooth Dock
186 String address = BluetoothService.readDockBluetoothAddress();
187 if (address != null)
188 intent.putExtra(BluetoothDevice.EXTRA_DEVICE,
189 BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address));
190
Mike LeBeau1f6c7e62009-09-19 18:06:52 -0700191 // Send the ordered broadcast; the result receiver will receive after all
192 // broadcasts have been sent. If any broadcast receiver changes the result
193 // code from the initial value of RESULT_OK, then the result receiver will
194 // not launch the corresponding dock application. This gives apps a chance
195 // to override the behavior and stay in their app even when the device is
196 // placed into a dock.
197 mContext.sendStickyOrderedBroadcast(
198 intent, mResultReceiver, null, Activity.RESULT_OK, null, null);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500199 }
200 }
201 };
202}