blob: 30c25e0ba8672e6072185e84927fed38ff00b179 [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
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050019import android.content.Context;
20import android.content.Intent;
21import android.os.Handler;
22import android.os.Message;
Ken Schultzf02c0742009-09-10 18:37:37 -050023import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050024import android.os.UEventObserver;
25import android.util.Log;
26
27import java.io.FileReader;
28import java.io.FileNotFoundException;
29
30/**
31 * <p>DockObserver monitors for a docking station.
32 */
33class DockObserver extends UEventObserver {
34 private static final String TAG = DockObserver.class.getSimpleName();
35 private static final boolean LOG = false;
36
37 private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock";
38 private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state";
39
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070040 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
41 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050042
43 private final Context mContext;
44
Ken Schultzf02c0742009-09-10 18:37:37 -050045 private PowerManagerService mPowerManager;
46
47 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050048 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -050049 mPowerManager = pm;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050050 init(); // set initial status
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070051 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050052 }
53
54 @Override
55 public void onUEvent(UEventObserver.UEvent event) {
56 if (Log.isLoggable(TAG, Log.VERBOSE)) {
57 Log.v(TAG, "Dock UEVENT: " + event.toString());
58 }
59
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070060 synchronized (this) {
61 try {
62 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
63 if (newState != mDockState) {
64 mDockState = newState;
65 if (mSystemReady) {
66 update();
67 }
68 }
69 } catch (NumberFormatException e) {
70 Log.e(TAG, "Could not parse switch state from event " + event);
71 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050072 }
73 }
74
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070075 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050076 char[] buffer = new char[1024];
77
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050078 try {
79 FileReader file = new FileReader(DOCK_STATE_PATH);
80 int len = file.read(buffer, 0, 1024);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070081 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050082
83 } catch (FileNotFoundException e) {
84 Log.w(TAG, "This kernel does not have dock station support");
85 } catch (Exception e) {
86 Log.e(TAG, "" , e);
87 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050088 }
89
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070090 void systemReady() {
91 synchronized (this) {
92 // don't bother broadcasting undocked here
93 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
94 update();
95 }
96 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050097 }
98 }
99
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700100 private final void update() {
101 mHandler.sendEmptyMessage(0);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500102 }
103
104 private final Handler mHandler = new Handler() {
105 @Override
106 public void handleMessage(Message msg) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700107 synchronized (this) {
108 Log.d(TAG, "Broadcasting dock state " + mDockState);
109 // Pack up the values and broadcast them to everyone
Ken Schultzf02c0742009-09-10 18:37:37 -0500110 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), false, true);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700111 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
112 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
113 mContext.sendStickyBroadcast(intent);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500114 }
115 }
116 };
117}