blob: c2b1b965888a918fc0a57103d7fc220d7fd8aadb [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;
Mike Lockwood9092ab42009-09-16 13:01:32 -040021import android.content.ActivityNotFoundException;
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070022import android.content.BroadcastReceiver;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050023import android.content.Context;
24import android.content.Intent;
25import android.os.Handler;
26import android.os.Message;
Ken Schultzf02c0742009-09-10 18:37:37 -050027import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050028import android.os.UEventObserver;
Dianne Hackborn49493342009-10-02 10:44:41 -070029import android.provider.Settings;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050030import android.util.Log;
31
Mike Lockwood733fdf32009-09-28 19:08:53 -040032import com.android.internal.widget.LockPatternUtils;
33
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050034import java.io.FileReader;
35import java.io.FileNotFoundException;
36
37/**
38 * <p>DockObserver monitors for a docking station.
39 */
40class DockObserver extends UEventObserver {
41 private static final String TAG = DockObserver.class.getSimpleName();
42 private static final boolean LOG = false;
43
44 private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock";
45 private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state";
46
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070047 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
48 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050049
50 private final Context mContext;
51
Ken Schultzf02c0742009-09-10 18:37:37 -050052 private PowerManagerService mPowerManager;
Mike Lockwood733fdf32009-09-28 19:08:53 -040053
54 private KeyguardManager.KeyguardLock mKeyguardLock;
55 private boolean mKeyguardDisabled;
56 private LockPatternUtils mLockPatternUtils;
57
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070058 // The broadcast receiver which receives the result of the ordered broadcast sent when
59 // the dock state changes. The original ordered broadcast is sent with an initial result
60 // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g.,
61 // to RESULT_CANCELED, then the intent to start a dock app will not be sent.
62 private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
63 @Override
64 public void onReceive(Context context, Intent intent) {
65 if (getResultCode() != Activity.RESULT_OK) {
66 return;
67 }
68
69 // Launch a dock activity
70 String category;
71 switch (mDockState) {
72 case Intent.EXTRA_DOCK_STATE_CAR:
73 category = Intent.CATEGORY_CAR_DOCK;
74 break;
75 case Intent.EXTRA_DOCK_STATE_DESK:
76 category = Intent.CATEGORY_DESK_DOCK;
77 break;
78 default:
79 category = null;
80 break;
81 }
82 if (category != null) {
83 intent = new Intent(Intent.ACTION_MAIN);
84 intent.addCategory(category);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070085 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
86 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070087 try {
88 mContext.startActivity(intent);
89 } catch (ActivityNotFoundException e) {
90 Log.w(TAG, e.getCause());
91 }
92 }
93 }
94 };
Ken Schultzf02c0742009-09-10 18:37:37 -050095
96 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050097 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -050098 mPowerManager = pm;
Mike Lockwood733fdf32009-09-28 19:08:53 -040099 mLockPatternUtils = new LockPatternUtils(context.getContentResolver());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500100 init(); // set initial status
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700101 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500102 }
103
104 @Override
105 public void onUEvent(UEventObserver.UEvent event) {
106 if (Log.isLoggable(TAG, Log.VERBOSE)) {
107 Log.v(TAG, "Dock UEVENT: " + event.toString());
108 }
109
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700110 synchronized (this) {
111 try {
112 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
113 if (newState != mDockState) {
Mike Lockwood1d069922009-11-11 18:09:25 -0500114 int oldState = mDockState;
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700115 mDockState = newState;
116 if (mSystemReady) {
Mike Lockwood1d069922009-11-11 18:09:25 -0500117 // Don't force screen on when undocking from the desk dock.
118 // The change in power state will do this anyway.
119 // FIXME - we should be configurable.
120 if (oldState != Intent.EXTRA_DOCK_STATE_DESK ||
121 newState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
122 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(),
123 false, true);
124 }
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700125 update();
126 }
127 }
128 } catch (NumberFormatException e) {
129 Log.e(TAG, "Could not parse switch state from event " + event);
130 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500131 }
132 }
133
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700134 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500135 char[] buffer = new char[1024];
136
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500137 try {
138 FileReader file = new FileReader(DOCK_STATE_PATH);
139 int len = file.read(buffer, 0, 1024);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700140 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500141
142 } catch (FileNotFoundException e) {
143 Log.w(TAG, "This kernel does not have dock station support");
144 } catch (Exception e) {
145 Log.e(TAG, "" , e);
146 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500147 }
148
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700149 void systemReady() {
150 synchronized (this) {
Mike Lockwood733fdf32009-09-28 19:08:53 -0400151 KeyguardManager keyguardManager =
152 (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
153 mKeyguardLock = keyguardManager.newKeyguardLock(TAG);
154
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700155 // don't bother broadcasting undocked here
156 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
157 update();
158 }
159 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500160 }
161 }
162
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700163 private final void update() {
164 mHandler.sendEmptyMessage(0);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500165 }
166
167 private final Handler mHandler = new Handler() {
168 @Override
169 public void handleMessage(Message msg) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700170 synchronized (this) {
Dianne Hackborn49493342009-10-02 10:44:41 -0700171 Log.i(TAG, "Dock state changed: " + mDockState);
172 if (Settings.Secure.getInt(mContext.getContentResolver(),
173 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
174 Log.i(TAG, "Device not provisioned, skipping dock broadcast");
175 return;
176 }
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700177 // Pack up the values and broadcast them to everyone
178 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
179 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
Mike LeBeau1f6c7e62009-09-19 18:06:52 -0700180
181 // Send the ordered broadcast; the result receiver will receive after all
182 // broadcasts have been sent. If any broadcast receiver changes the result
183 // code from the initial value of RESULT_OK, then the result receiver will
184 // not launch the corresponding dock application. This gives apps a chance
185 // to override the behavior and stay in their app even when the device is
186 // placed into a dock.
187 mContext.sendStickyOrderedBroadcast(
188 intent, mResultReceiver, null, Activity.RESULT_OK, null, null);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500189 }
190 }
191 };
192}