blob: 4a7df8fa6463361b1a80002b162614b934127ce0 [file] [log] [blame]
Mike Lockwood24236072010-06-23 17:36:36 -04001/*
2 * Copyright (C) 2010 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
19import android.content.ContentResolver;
20import android.content.Context;
21import android.content.Intent;
22import android.hardware.Usb;
23import android.net.Uri;
24import android.os.Handler;
25import android.os.Message;
26import android.os.UEventObserver;
Mike Lockwooda3156052010-11-20 12:28:27 -050027import android.provider.Ptp;
Mike Lockwood24236072010-06-23 17:36:36 -040028import android.provider.Settings;
29import android.util.Log;
30import android.util.Slog;
31
32import java.io.File;
33import java.io.FileNotFoundException;
34import java.io.FileReader;
35import java.util.ArrayList;
36
37/**
38 * <p>UsbObserver monitors for changes to USB state.
39 */
40class UsbObserver extends UEventObserver {
41 private static final String TAG = UsbObserver.class.getSimpleName();
42 private static final boolean LOG = false;
43
44 private static final String USB_CONFIGURATION_MATCH = "DEVPATH=/devices/virtual/switch/usb_configuration";
45 private static final String USB_FUNCTIONS_MATCH = "DEVPATH=/devices/virtual/usb_composite/";
46 private static final String USB_CONFIGURATION_PATH = "/sys/class/switch/usb_configuration/state";
47 private static final String USB_COMPOSITE_CLASS_PATH = "/sys/class/usb_composite";
48
49 private static final int MSG_UPDATE = 0;
50
51 private int mUsbConfig = 0;
52 private int mPreviousUsbConfig = 0;
53
54 // lists of enabled and disabled USB functions
55 private final ArrayList<String> mEnabledFunctions = new ArrayList<String>();
56 private final ArrayList<String> mDisabledFunctions = new ArrayList<String>();
57
58 private boolean mSystemReady;
59
60 private final Context mContext;
61
62 private PowerManagerService mPowerManager;
63
64 public UsbObserver(Context context) {
65 mContext = context;
66 init(); // set initial status
67
68 startObserving(USB_CONFIGURATION_MATCH);
69 startObserving(USB_FUNCTIONS_MATCH);
70 }
71
72 @Override
73 public void onUEvent(UEventObserver.UEvent event) {
74 if (Log.isLoggable(TAG, Log.VERBOSE)) {
75 Slog.v(TAG, "USB UEVENT: " + event.toString());
76 }
77
78 synchronized (this) {
79 String switchState = event.get("SWITCH_STATE");
80 if (switchState != null) {
81 try {
82 int newConfig = Integer.parseInt(switchState);
83 if (newConfig != mUsbConfig) {
84 mPreviousUsbConfig = mUsbConfig;
85 mUsbConfig = newConfig;
86 // trigger an Intent broadcast
87 if (mSystemReady) {
88 update();
89 }
90 }
91 } catch (NumberFormatException e) {
92 Slog.e(TAG, "Could not parse switch state from event " + event);
93 }
94 } else {
95 String function = event.get("FUNCTION");
96 String enabledStr = event.get("ENABLED");
97 if (function != null && enabledStr != null) {
98 // Note: we do not broadcast a change when a function is enabled or disabled.
99 // We just record the state change for the next broadcast.
100 boolean enabled = "1".equals(enabledStr);
101 if (enabled) {
102 if (!mEnabledFunctions.contains(function)) {
103 mEnabledFunctions.add(function);
104 }
105 mDisabledFunctions.remove(function);
106 } else {
107 if (!mDisabledFunctions.contains(function)) {
108 mDisabledFunctions.add(function);
109 }
110 mEnabledFunctions.remove(function);
111 }
112 }
113 }
114 }
115 }
116 private final void init() {
117 char[] buffer = new char[1024];
118
119 try {
120 FileReader file = new FileReader(USB_CONFIGURATION_PATH);
121 int len = file.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700122 file.close();
Mike Lockwood24236072010-06-23 17:36:36 -0400123 mPreviousUsbConfig = mUsbConfig = Integer.valueOf((new String(buffer, 0, len)).trim());
124
125 } catch (FileNotFoundException e) {
126 Slog.w(TAG, "This kernel does not have USB configuration switch support");
127 } catch (Exception e) {
128 Slog.e(TAG, "" , e);
129 }
130
131 try {
132 File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles();
133 for (int i = 0; i < files.length; i++) {
134 File file = new File(files[i], "enable");
135 FileReader reader = new FileReader(file);
136 int len = reader.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700137 reader.close();
Mike Lockwood24236072010-06-23 17:36:36 -0400138 int value = Integer.valueOf((new String(buffer, 0, len)).trim());
139 String functionName = files[i].getName();
140 if (value == 1) {
141 mEnabledFunctions.add(functionName);
142 } else {
143 mDisabledFunctions.add(functionName);
144 }
145 }
146 } catch (FileNotFoundException e) {
147 Slog.w(TAG, "This kernel does not have USB composite class support");
148 } catch (Exception e) {
149 Slog.e(TAG, "" , e);
150 }
151 }
152
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400153 private native void monitorUsbHostBus();
154
155 // called from JNI in monitorUsbHostBus()
156 private void usbCameraAdded(int deviceID) {
157 Intent intent = new Intent(Usb.ACTION_USB_CAMERA_ATTACHED,
Mike Lockwooda3156052010-11-20 12:28:27 -0500158 Ptp.Device.getContentUri(deviceID));
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400159 Log.d(TAG, "usbCameraAdded, sending " + intent);
160 mContext.sendBroadcast(intent);
161 }
162
163 // called from JNI in monitorUsbHostBus()
164 private void usbCameraRemoved(int deviceID) {
165 Intent intent = new Intent(Usb.ACTION_USB_CAMERA_DETACHED,
Mike Lockwooda3156052010-11-20 12:28:27 -0500166 Ptp.Device.getContentUri(deviceID));
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400167 Log.d(TAG, "usbCameraRemoved, sending " + intent);
168 mContext.sendBroadcast(intent);
169 }
170
171 private void initHostSupport() {
172 // Create a thread to call into native code to wait for USB host events.
173 // This thread will call us back on usbCameraAdded and usbCameraRemoved.
174 Runnable runnable = new Runnable() {
175 public void run() {
176 monitorUsbHostBus();
177 }
178 };
179 new Thread(null, runnable, "UsbObserver host thread").start();
180 }
181
Mike Lockwood24236072010-06-23 17:36:36 -0400182 void systemReady() {
183 synchronized (this) {
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400184 if (mContext.getResources().getBoolean(
185 com.android.internal.R.bool.config_hasUsbHostSupport)) {
186 // start monitoring for connected USB devices
187 initHostSupport();
188 }
189
Mike Lockwood24236072010-06-23 17:36:36 -0400190 update();
191 mSystemReady = true;
192 }
193 }
194
195 private final void update() {
196 mHandler.sendEmptyMessage(MSG_UPDATE);
197 }
198
199 private final Handler mHandler = new Handler() {
Mike Lockwood709981e2010-06-28 09:58:58 -0400200 private void addEnabledFunctions(Intent intent) {
201 // include state of all USB functions in our extras
202 for (int i = 0; i < mEnabledFunctions.size(); i++) {
203 intent.putExtra(mEnabledFunctions.get(i), Usb.USB_FUNCTION_ENABLED);
204 }
205 for (int i = 0; i < mDisabledFunctions.size(); i++) {
206 intent.putExtra(mDisabledFunctions.get(i), Usb.USB_FUNCTION_DISABLED);
207 }
208 }
209
Mike Lockwood24236072010-06-23 17:36:36 -0400210 @Override
211 public void handleMessage(Message msg) {
212 switch (msg.what) {
213 case MSG_UPDATE:
214 synchronized (this) {
215 final ContentResolver cr = mContext.getContentResolver();
216
217 if (Settings.Secure.getInt(cr,
218 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
219 Slog.i(TAG, "Device not provisioned, skipping USB broadcast");
220 return;
221 }
222 // Send an Intent containing connected/disconnected state
223 // and the enabled/disabled state of all USB functions
224 Intent intent;
Mike Lockwood709981e2010-06-28 09:58:58 -0400225 boolean usbConnected = (mUsbConfig != 0);
226 if (usbConnected) {
Mike Lockwood24236072010-06-23 17:36:36 -0400227 intent = new Intent(Usb.ACTION_USB_CONNECTED);
Mike Lockwood709981e2010-06-28 09:58:58 -0400228 addEnabledFunctions(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400229 } else {
230 intent = new Intent(Usb.ACTION_USB_DISCONNECTED);
231 }
Mike Lockwood709981e2010-06-28 09:58:58 -0400232 mContext.sendBroadcast(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400233
Mike Lockwood709981e2010-06-28 09:58:58 -0400234 // send a sticky broadcast for clients interested in both connect and disconnect
235 intent = new Intent(Usb.ACTION_USB_STATE);
236 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
237 intent.putExtra(Usb.USB_CONNECTED, usbConnected);
238 addEnabledFunctions(intent);
239 mContext.sendStickyBroadcast(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400240 }
241 break;
242 }
243 }
244 };
245}