blob: 738535955e6c7b9ba710dd817106539ebddc0314 [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;
23import android.os.UEventObserver;
24import android.util.Log;
25
26import java.io.FileReader;
27import java.io.FileNotFoundException;
28
29/**
30 * <p>DockObserver monitors for a docking station.
31 */
32class DockObserver extends UEventObserver {
33 private static final String TAG = DockObserver.class.getSimpleName();
34 private static final boolean LOG = false;
35
36 private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock";
37 private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state";
38
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070039 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
40 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050041
42 private final Context mContext;
43
44 public DockObserver(Context context) {
45 mContext = context;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050046 init(); // set initial status
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070047 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050048 }
49
50 @Override
51 public void onUEvent(UEventObserver.UEvent event) {
52 if (Log.isLoggable(TAG, Log.VERBOSE)) {
53 Log.v(TAG, "Dock UEVENT: " + event.toString());
54 }
55
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070056 synchronized (this) {
57 try {
58 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
59 if (newState != mDockState) {
60 mDockState = newState;
61 if (mSystemReady) {
62 update();
63 }
64 }
65 } catch (NumberFormatException e) {
66 Log.e(TAG, "Could not parse switch state from event " + event);
67 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050068 }
69 }
70
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070071 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050072 char[] buffer = new char[1024];
73
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050074 try {
75 FileReader file = new FileReader(DOCK_STATE_PATH);
76 int len = file.read(buffer, 0, 1024);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070077 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050078
79 } catch (FileNotFoundException e) {
80 Log.w(TAG, "This kernel does not have dock station support");
81 } catch (Exception e) {
82 Log.e(TAG, "" , e);
83 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050084 }
85
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070086 void systemReady() {
87 synchronized (this) {
88 // don't bother broadcasting undocked here
89 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
90 update();
91 }
92 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050093 }
94 }
95
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070096 private final void update() {
97 mHandler.sendEmptyMessage(0);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050098 }
99
100 private final Handler mHandler = new Handler() {
101 @Override
102 public void handleMessage(Message msg) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700103 synchronized (this) {
104 Log.d(TAG, "Broadcasting dock state " + mDockState);
105 // Pack up the values and broadcast them to everyone
106 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
107 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
108 mContext.sendStickyBroadcast(intent);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500109 }
110 }
111 };
112}