blob: 2bd039f1d1bd375d3f64ee50dfc009d57968b484 [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
39 private int mDockState;
40 private boolean mPendingIntent;
41
42 private final Context mContext;
43
44 public DockObserver(Context context) {
45 mContext = context;
46
47 startObserving(DOCK_UEVENT_MATCH);
48
49 init(); // set initial status
50 }
51
52 @Override
53 public void onUEvent(UEventObserver.UEvent event) {
54 if (Log.isLoggable(TAG, Log.VERBOSE)) {
55 Log.v(TAG, "Dock UEVENT: " + event.toString());
56 }
57
58 try {
59 update(Integer.parseInt(event.get("SWITCH_STATE")));
60 } catch (NumberFormatException e) {
61 Log.e(TAG, "Could not parse switch state from event " + event);
62 }
63 }
64
65 private synchronized final void init() {
66 char[] buffer = new char[1024];
67
68 int newState = mDockState;
69 try {
70 FileReader file = new FileReader(DOCK_STATE_PATH);
71 int len = file.read(buffer, 0, 1024);
72 newState = Integer.valueOf((new String(buffer, 0, len)).trim());
73
74 } catch (FileNotFoundException e) {
75 Log.w(TAG, "This kernel does not have dock station support");
76 } catch (Exception e) {
77 Log.e(TAG, "" , e);
78 }
79
80 update(newState);
81 }
82
83 private synchronized final void update(int newState) {
84 if (newState != mDockState) {
85 mDockState = newState;
86
87 mPendingIntent = true;
88 mHandler.sendEmptyMessage(0);
89 }
90 }
91
92 private synchronized final void sendIntent() {
Mike Lockwoode4465e22009-08-24 13:51:01 -070093 Log.d(TAG, "Broadcasting dock state " + mDockState);
94
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050095 // Pack up the values and broadcast them to everyone
96 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050097 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
Mike Lockwoode4465e22009-08-24 13:51:01 -070098 mContext.sendStickyBroadcast(intent);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050099 }
100
101 private final Handler mHandler = new Handler() {
102 @Override
103 public void handleMessage(Message msg) {
104 if (mPendingIntent) {
105 sendIntent();
106 mPendingIntent = false;
107 }
108 }
109 };
110}