blob: 546e5f8d0242b43101522f0512971e8245c24fb7 [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 Lockwoodda39f0e2010-07-27 18:44:30 -040027import android.provider.Mtp;
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);
122 mPreviousUsbConfig = mUsbConfig = Integer.valueOf((new String(buffer, 0, len)).trim());
123
124 } catch (FileNotFoundException e) {
125 Slog.w(TAG, "This kernel does not have USB configuration switch support");
126 } catch (Exception e) {
127 Slog.e(TAG, "" , e);
128 }
129
130 try {
131 File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles();
132 for (int i = 0; i < files.length; i++) {
133 File file = new File(files[i], "enable");
134 FileReader reader = new FileReader(file);
135 int len = reader.read(buffer, 0, 1024);
136 int value = Integer.valueOf((new String(buffer, 0, len)).trim());
137 String functionName = files[i].getName();
138 if (value == 1) {
139 mEnabledFunctions.add(functionName);
140 } else {
141 mDisabledFunctions.add(functionName);
142 }
143 }
144 } catch (FileNotFoundException e) {
145 Slog.w(TAG, "This kernel does not have USB composite class support");
146 } catch (Exception e) {
147 Slog.e(TAG, "" , e);
148 }
149 }
150
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400151 private native void monitorUsbHostBus();
152
153 // called from JNI in monitorUsbHostBus()
154 private void usbCameraAdded(int deviceID) {
155 Intent intent = new Intent(Usb.ACTION_USB_CAMERA_ATTACHED,
156 Mtp.Device.getContentUri(deviceID));
157 Log.d(TAG, "usbCameraAdded, sending " + intent);
158 mContext.sendBroadcast(intent);
159 }
160
161 // called from JNI in monitorUsbHostBus()
162 private void usbCameraRemoved(int deviceID) {
163 Intent intent = new Intent(Usb.ACTION_USB_CAMERA_DETACHED,
164 Mtp.Device.getContentUri(deviceID));
165 Log.d(TAG, "usbCameraRemoved, sending " + intent);
166 mContext.sendBroadcast(intent);
167 }
168
169 private void initHostSupport() {
170 // Create a thread to call into native code to wait for USB host events.
171 // This thread will call us back on usbCameraAdded and usbCameraRemoved.
172 Runnable runnable = new Runnable() {
173 public void run() {
174 monitorUsbHostBus();
175 }
176 };
177 new Thread(null, runnable, "UsbObserver host thread").start();
178 }
179
Mike Lockwood24236072010-06-23 17:36:36 -0400180 void systemReady() {
181 synchronized (this) {
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400182 if (mContext.getResources().getBoolean(
183 com.android.internal.R.bool.config_hasUsbHostSupport)) {
184 // start monitoring for connected USB devices
185 initHostSupport();
186 }
187
Mike Lockwood24236072010-06-23 17:36:36 -0400188 update();
189 mSystemReady = true;
190 }
191 }
192
193 private final void update() {
194 mHandler.sendEmptyMessage(MSG_UPDATE);
195 }
196
197 private final Handler mHandler = new Handler() {
Mike Lockwood709981e2010-06-28 09:58:58 -0400198 private void addEnabledFunctions(Intent intent) {
199 // include state of all USB functions in our extras
200 for (int i = 0; i < mEnabledFunctions.size(); i++) {
201 intent.putExtra(mEnabledFunctions.get(i), Usb.USB_FUNCTION_ENABLED);
202 }
203 for (int i = 0; i < mDisabledFunctions.size(); i++) {
204 intent.putExtra(mDisabledFunctions.get(i), Usb.USB_FUNCTION_DISABLED);
205 }
206 }
207
Mike Lockwood24236072010-06-23 17:36:36 -0400208 @Override
209 public void handleMessage(Message msg) {
210 switch (msg.what) {
211 case MSG_UPDATE:
212 synchronized (this) {
213 final ContentResolver cr = mContext.getContentResolver();
214
215 if (Settings.Secure.getInt(cr,
216 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
217 Slog.i(TAG, "Device not provisioned, skipping USB broadcast");
218 return;
219 }
220 // Send an Intent containing connected/disconnected state
221 // and the enabled/disabled state of all USB functions
222 Intent intent;
Mike Lockwood709981e2010-06-28 09:58:58 -0400223 boolean usbConnected = (mUsbConfig != 0);
224 if (usbConnected) {
Mike Lockwood24236072010-06-23 17:36:36 -0400225 intent = new Intent(Usb.ACTION_USB_CONNECTED);
Mike Lockwood709981e2010-06-28 09:58:58 -0400226 addEnabledFunctions(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400227 } else {
228 intent = new Intent(Usb.ACTION_USB_DISCONNECTED);
229 }
Mike Lockwood709981e2010-06-28 09:58:58 -0400230 mContext.sendBroadcast(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400231
Mike Lockwood709981e2010-06-28 09:58:58 -0400232 // send a sticky broadcast for clients interested in both connect and disconnect
233 intent = new Intent(Usb.ACTION_USB_STATE);
234 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
235 intent.putExtra(Usb.USB_CONNECTED, usbConnected);
236 addEnabledFunctions(intent);
237 mContext.sendStickyBroadcast(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400238 }
239 break;
240 }
241 }
242 };
243}