blob: b90731fa74d6771dfa5f1ae8a7b276408aa9f7e0 [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;
Mike Lockwood770126a2010-12-09 22:30:37 -080022import android.hardware.UsbManager;
Mike Lockwood24236072010-06-23 17:36:36 -040023import 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/**
Mike Lockwood770126a2010-12-09 22:30:37 -080038 * <p>UsbService monitors for changes to USB state.
Mike Lockwood24236072010-06-23 17:36:36 -040039 */
Mike Lockwood770126a2010-12-09 22:30:37 -080040class UsbService {
41 private static final String TAG = UsbService.class.getSimpleName();
Mike Lockwood24236072010-06-23 17:36:36 -040042 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
Mike Lockwood770126a2010-12-09 22:30:37 -080064 private final UEventObserver mUEventObserver = new UEventObserver() {
65 @Override
66 public void onUEvent(UEventObserver.UEvent event) {
67 if (Log.isLoggable(TAG, Log.VERBOSE)) {
68 Slog.v(TAG, "USB UEVENT: " + event.toString());
69 }
Mike Lockwood24236072010-06-23 17:36:36 -040070
Mike Lockwood770126a2010-12-09 22:30:37 -080071 synchronized (this) {
72 String switchState = event.get("SWITCH_STATE");
73 if (switchState != null) {
74 try {
75 int newConfig = Integer.parseInt(switchState);
76 if (newConfig != mUsbConfig) {
77 mPreviousUsbConfig = mUsbConfig;
78 mUsbConfig = newConfig;
79 // trigger an Intent broadcast
80 if (mSystemReady) {
81 update();
82 }
Mike Lockwood24236072010-06-23 17:36:36 -040083 }
Mike Lockwood770126a2010-12-09 22:30:37 -080084 } catch (NumberFormatException e) {
85 Slog.e(TAG, "Could not parse switch state from event " + event);
Mike Lockwood24236072010-06-23 17:36:36 -040086 }
Mike Lockwood770126a2010-12-09 22:30:37 -080087 } else {
88 String function = event.get("FUNCTION");
89 String enabledStr = event.get("ENABLED");
90 if (function != null && enabledStr != null) {
91 // Note: we do not broadcast a change when a function is enabled or disabled.
92 // We just record the state change for the next broadcast.
93 boolean enabled = "1".equals(enabledStr);
94 if (enabled) {
95 if (!mEnabledFunctions.contains(function)) {
96 mEnabledFunctions.add(function);
97 }
98 mDisabledFunctions.remove(function);
99 } else {
100 if (!mDisabledFunctions.contains(function)) {
101 mDisabledFunctions.add(function);
102 }
103 mEnabledFunctions.remove(function);
Mike Lockwood24236072010-06-23 17:36:36 -0400104 }
Mike Lockwood24236072010-06-23 17:36:36 -0400105 }
106 }
107 }
108 }
Mike Lockwood770126a2010-12-09 22:30:37 -0800109 };
110
111 public UsbService(Context context) {
112 mContext = context;
113 init(); // set initial status
114
115 mUEventObserver.startObserving(USB_CONFIGURATION_MATCH);
116 mUEventObserver.startObserving(USB_FUNCTIONS_MATCH);
Mike Lockwood24236072010-06-23 17:36:36 -0400117 }
Mike Lockwood770126a2010-12-09 22:30:37 -0800118
Mike Lockwood24236072010-06-23 17:36:36 -0400119 private final void init() {
120 char[] buffer = new char[1024];
121
122 try {
123 FileReader file = new FileReader(USB_CONFIGURATION_PATH);
124 int len = file.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700125 file.close();
Mike Lockwood24236072010-06-23 17:36:36 -0400126 mPreviousUsbConfig = mUsbConfig = Integer.valueOf((new String(buffer, 0, len)).trim());
127
128 } catch (FileNotFoundException e) {
129 Slog.w(TAG, "This kernel does not have USB configuration switch support");
130 } catch (Exception e) {
131 Slog.e(TAG, "" , e);
132 }
133
134 try {
135 File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles();
136 for (int i = 0; i < files.length; i++) {
137 File file = new File(files[i], "enable");
138 FileReader reader = new FileReader(file);
139 int len = reader.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700140 reader.close();
Mike Lockwood24236072010-06-23 17:36:36 -0400141 int value = Integer.valueOf((new String(buffer, 0, len)).trim());
142 String functionName = files[i].getName();
143 if (value == 1) {
144 mEnabledFunctions.add(functionName);
145 } else {
146 mDisabledFunctions.add(functionName);
147 }
148 }
149 } catch (FileNotFoundException e) {
150 Slog.w(TAG, "This kernel does not have USB composite class support");
151 } catch (Exception e) {
152 Slog.e(TAG, "" , e);
153 }
154 }
155
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400156 private native void monitorUsbHostBus();
157
158 // called from JNI in monitorUsbHostBus()
159 private void usbCameraAdded(int deviceID) {
Mike Lockwood770126a2010-12-09 22:30:37 -0800160 Intent intent = new Intent(UsbManager.ACTION_USB_CAMERA_ATTACHED,
Mike Lockwooda3156052010-11-20 12:28:27 -0500161 Ptp.Device.getContentUri(deviceID));
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400162 Log.d(TAG, "usbCameraAdded, sending " + intent);
163 mContext.sendBroadcast(intent);
164 }
165
166 // called from JNI in monitorUsbHostBus()
167 private void usbCameraRemoved(int deviceID) {
Mike Lockwood770126a2010-12-09 22:30:37 -0800168 Intent intent = new Intent(UsbManager.ACTION_USB_CAMERA_DETACHED,
Mike Lockwooda3156052010-11-20 12:28:27 -0500169 Ptp.Device.getContentUri(deviceID));
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400170 Log.d(TAG, "usbCameraRemoved, sending " + intent);
171 mContext.sendBroadcast(intent);
172 }
173
174 private void initHostSupport() {
175 // Create a thread to call into native code to wait for USB host events.
176 // This thread will call us back on usbCameraAdded and usbCameraRemoved.
177 Runnable runnable = new Runnable() {
178 public void run() {
179 monitorUsbHostBus();
180 }
181 };
Mike Lockwood770126a2010-12-09 22:30:37 -0800182 new Thread(null, runnable, "UsbService host thread").start();
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400183 }
184
Mike Lockwood24236072010-06-23 17:36:36 -0400185 void systemReady() {
186 synchronized (this) {
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400187 if (mContext.getResources().getBoolean(
188 com.android.internal.R.bool.config_hasUsbHostSupport)) {
189 // start monitoring for connected USB devices
190 initHostSupport();
191 }
192
Mike Lockwood24236072010-06-23 17:36:36 -0400193 update();
194 mSystemReady = true;
195 }
196 }
197
198 private final void update() {
199 mHandler.sendEmptyMessage(MSG_UPDATE);
200 }
201
202 private final Handler mHandler = new Handler() {
Mike Lockwood709981e2010-06-28 09:58:58 -0400203 private void addEnabledFunctions(Intent intent) {
204 // include state of all USB functions in our extras
205 for (int i = 0; i < mEnabledFunctions.size(); i++) {
Mike Lockwood770126a2010-12-09 22:30:37 -0800206 intent.putExtra(mEnabledFunctions.get(i), UsbManager.USB_FUNCTION_ENABLED);
Mike Lockwood709981e2010-06-28 09:58:58 -0400207 }
208 for (int i = 0; i < mDisabledFunctions.size(); i++) {
Mike Lockwood770126a2010-12-09 22:30:37 -0800209 intent.putExtra(mDisabledFunctions.get(i), UsbManager.USB_FUNCTION_DISABLED);
Mike Lockwood709981e2010-06-28 09:58:58 -0400210 }
211 }
212
Mike Lockwood24236072010-06-23 17:36:36 -0400213 @Override
214 public void handleMessage(Message msg) {
215 switch (msg.what) {
216 case MSG_UPDATE:
217 synchronized (this) {
218 final ContentResolver cr = mContext.getContentResolver();
219
220 if (Settings.Secure.getInt(cr,
221 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
222 Slog.i(TAG, "Device not provisioned, skipping USB broadcast");
223 return;
224 }
225 // Send an Intent containing connected/disconnected state
226 // and the enabled/disabled state of all USB functions
227 Intent intent;
Mike Lockwood709981e2010-06-28 09:58:58 -0400228 boolean usbConnected = (mUsbConfig != 0);
229 if (usbConnected) {
Mike Lockwood770126a2010-12-09 22:30:37 -0800230 intent = new Intent(UsbManager.ACTION_USB_CONNECTED);
Mike Lockwood709981e2010-06-28 09:58:58 -0400231 addEnabledFunctions(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400232 } else {
Mike Lockwood770126a2010-12-09 22:30:37 -0800233 intent = new Intent(UsbManager.ACTION_USB_DISCONNECTED);
Mike Lockwood24236072010-06-23 17:36:36 -0400234 }
Mike Lockwood709981e2010-06-28 09:58:58 -0400235 mContext.sendBroadcast(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400236
Mike Lockwood709981e2010-06-28 09:58:58 -0400237 // send a sticky broadcast for clients interested in both connect and disconnect
Mike Lockwood770126a2010-12-09 22:30:37 -0800238 intent = new Intent(UsbManager.ACTION_USB_STATE);
Mike Lockwood709981e2010-06-28 09:58:58 -0400239 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Mike Lockwood770126a2010-12-09 22:30:37 -0800240 intent.putExtra(UsbManager.USB_CONNECTED, usbConnected);
Mike Lockwood709981e2010-06-28 09:58:58 -0400241 addEnabledFunctions(intent);
242 mContext.sendStickyBroadcast(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400243 }
244 break;
245 }
246 }
247 };
248}