blob: f089de1135bc9412eb57de65f3ec73d670ffc3c8 [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) {
114 mDockState = newState;
115 if (mSystemReady) {
116 update();
117 }
118 }
119 } catch (NumberFormatException e) {
120 Log.e(TAG, "Could not parse switch state from event " + event);
121 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500122 }
123 }
124
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700125 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500126 char[] buffer = new char[1024];
127
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500128 try {
129 FileReader file = new FileReader(DOCK_STATE_PATH);
130 int len = file.read(buffer, 0, 1024);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700131 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500132
133 } catch (FileNotFoundException e) {
134 Log.w(TAG, "This kernel does not have dock station support");
135 } catch (Exception e) {
136 Log.e(TAG, "" , e);
137 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500138 }
139
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700140 void systemReady() {
141 synchronized (this) {
Mike Lockwood733fdf32009-09-28 19:08:53 -0400142 KeyguardManager keyguardManager =
143 (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
144 mKeyguardLock = keyguardManager.newKeyguardLock(TAG);
145
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700146 // don't bother broadcasting undocked here
147 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
148 update();
149 }
150 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500151 }
152 }
153
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700154 private final void update() {
155 mHandler.sendEmptyMessage(0);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500156 }
157
158 private final Handler mHandler = new Handler() {
159 @Override
160 public void handleMessage(Message msg) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700161 synchronized (this) {
Dianne Hackborn49493342009-10-02 10:44:41 -0700162 Log.i(TAG, "Dock state changed: " + mDockState);
163 if (Settings.Secure.getInt(mContext.getContentResolver(),
164 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
165 Log.i(TAG, "Device not provisioned, skipping dock broadcast");
166 return;
167 }
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700168 // Pack up the values and broadcast them to everyone
Ken Schultzf02c0742009-09-10 18:37:37 -0500169 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), false, true);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700170 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
171 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
Mike LeBeau1f6c7e62009-09-19 18:06:52 -0700172
173 // Send the ordered broadcast; the result receiver will receive after all
174 // broadcasts have been sent. If any broadcast receiver changes the result
175 // code from the initial value of RESULT_OK, then the result receiver will
176 // not launch the corresponding dock application. This gives apps a chance
177 // to override the behavior and stay in their app even when the device is
178 // placed into a dock.
179 mContext.sendStickyOrderedBroadcast(
180 intent, mResultReceiver, null, Activity.RESULT_OK, null, null);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500181 }
182 }
183 };
184}