blob: 60195b9e577ba653fa2ae56358d55c902abe7950 [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 Lockwood9092ab42009-09-16 13:01:32 -040019import android.content.ActivityNotFoundException;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050020import android.content.Context;
21import android.content.Intent;
22import android.os.Handler;
23import android.os.Message;
Ken Schultzf02c0742009-09-10 18:37:37 -050024import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050025import android.os.UEventObserver;
26import android.util.Log;
27
28import java.io.FileReader;
29import java.io.FileNotFoundException;
30
31/**
32 * <p>DockObserver monitors for a docking station.
33 */
34class DockObserver extends UEventObserver {
35 private static final String TAG = DockObserver.class.getSimpleName();
36 private static final boolean LOG = false;
37
38 private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock";
39 private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state";
40
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070041 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
42 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050043
44 private final Context mContext;
45
Ken Schultzf02c0742009-09-10 18:37:37 -050046 private PowerManagerService mPowerManager;
47
48 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050049 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -050050 mPowerManager = pm;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050051 init(); // set initial status
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070052 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050053 }
54
55 @Override
56 public void onUEvent(UEventObserver.UEvent event) {
57 if (Log.isLoggable(TAG, Log.VERBOSE)) {
58 Log.v(TAG, "Dock UEVENT: " + event.toString());
59 }
60
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070061 synchronized (this) {
62 try {
63 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
64 if (newState != mDockState) {
65 mDockState = newState;
66 if (mSystemReady) {
67 update();
68 }
69 }
70 } catch (NumberFormatException e) {
71 Log.e(TAG, "Could not parse switch state from event " + event);
72 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050073 }
74 }
75
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070076 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050077 char[] buffer = new char[1024];
78
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050079 try {
80 FileReader file = new FileReader(DOCK_STATE_PATH);
81 int len = file.read(buffer, 0, 1024);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070082 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050083
84 } catch (FileNotFoundException e) {
85 Log.w(TAG, "This kernel does not have dock station support");
86 } catch (Exception e) {
87 Log.e(TAG, "" , e);
88 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050089 }
90
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070091 void systemReady() {
92 synchronized (this) {
93 // don't bother broadcasting undocked here
94 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
95 update();
96 }
97 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050098 }
99 }
100
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700101 private final void update() {
102 mHandler.sendEmptyMessage(0);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500103 }
104
105 private final Handler mHandler = new Handler() {
106 @Override
107 public void handleMessage(Message msg) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700108 synchronized (this) {
109 Log.d(TAG, "Broadcasting dock state " + mDockState);
110 // Pack up the values and broadcast them to everyone
Ken Schultzf02c0742009-09-10 18:37:37 -0500111 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), false, true);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700112 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
113 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
114 mContext.sendStickyBroadcast(intent);
Mike Lockwood9092ab42009-09-16 13:01:32 -0400115
116 // Launch a dock activity
117 String category;
118 switch (mDockState) {
119 case Intent.EXTRA_DOCK_STATE_CAR:
120 category = Intent.CATEGORY_CAR_DOCK;
121 break;
122 case Intent.EXTRA_DOCK_STATE_DESK:
123 category = Intent.CATEGORY_DESK_DOCK;
124 break;
125 default:
126 category = null;
127 break;
128 }
129 if (category != null) {
130 intent = new Intent(Intent.ACTION_MAIN);
131 intent.addCategory(category);
132 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
133 try {
134 mContext.startActivity(intent);
135 } catch (ActivityNotFoundException e) {
136 Log.w(TAG, e.getCause());
137 }
138 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500139 }
140 }
141 };
142}