blob: 77ddf3b76e858033b52371099053377ae18c0736 [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;
27import android.provider.Settings;
28import android.util.Log;
29import android.util.Slog;
30
31import java.io.File;
32import java.io.FileNotFoundException;
33import java.io.FileReader;
34import java.util.ArrayList;
35
36/**
Mike Lockwood770126a2010-12-09 22:30:37 -080037 * <p>UsbService monitors for changes to USB state.
Mike Lockwood24236072010-06-23 17:36:36 -040038 */
Mike Lockwood770126a2010-12-09 22:30:37 -080039class UsbService {
40 private static final String TAG = UsbService.class.getSimpleName();
Mike Lockwood24236072010-06-23 17:36:36 -040041 private static final boolean LOG = false;
42
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080043 private static final String USB_CONNECTED_MATCH =
44 "DEVPATH=/devices/virtual/switch/usb_connected";
45 private static final String USB_CONFIGURATION_MATCH =
46 "DEVPATH=/devices/virtual/switch/usb_configuration";
47 private static final String USB_FUNCTIONS_MATCH =
48 "DEVPATH=/devices/virtual/usb_composite/";
49 private static final String USB_CONNECTED_PATH =
50 "/sys/class/switch/usb_connected/state";
51 private static final String USB_CONFIGURATION_PATH =
52 "/sys/class/switch/usb_configuration/state";
53 private static final String USB_COMPOSITE_CLASS_PATH =
54 "/sys/class/usb_composite";
Mike Lockwood24236072010-06-23 17:36:36 -040055
56 private static final int MSG_UPDATE = 0;
57
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080058 // Delay for debouncing USB disconnects.
59 // We often get rapid connect/disconnect events when enabling USB functions,
60 // which need debouncing.
61 private static final int UPDATE_DELAY = 1000;
62
63 // current connected and configuration state
64 private int mConnected;
65 private int mConfiguration;
66
67 // last broadcasted connected and configuration state
68 private int mLastConnected = -1;
69 private int mLastConfiguration = -1;
Mike Lockwood24236072010-06-23 17:36:36 -040070
71 // lists of enabled and disabled USB functions
72 private final ArrayList<String> mEnabledFunctions = new ArrayList<String>();
73 private final ArrayList<String> mDisabledFunctions = new ArrayList<String>();
74
75 private boolean mSystemReady;
76
77 private final Context mContext;
78
Mike Lockwood770126a2010-12-09 22:30:37 -080079 private final UEventObserver mUEventObserver = new UEventObserver() {
80 @Override
81 public void onUEvent(UEventObserver.UEvent event) {
82 if (Log.isLoggable(TAG, Log.VERBOSE)) {
83 Slog.v(TAG, "USB UEVENT: " + event.toString());
84 }
Mike Lockwood24236072010-06-23 17:36:36 -040085
Mike Lockwood770126a2010-12-09 22:30:37 -080086 synchronized (this) {
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080087 String name = event.get("SWITCH_NAME");
88 String state = event.get("SWITCH_STATE");
89 if (name != null && state != null) {
Mike Lockwood770126a2010-12-09 22:30:37 -080090 try {
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080091 int intState = Integer.parseInt(state);
92 if ("usb_connected".equals(name)) {
93 mConnected = intState;
Mike Lockwood770126a2010-12-09 22:30:37 -080094 // trigger an Intent broadcast
95 if (mSystemReady) {
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080096 // debounce disconnects
97 update(mConnected == 0);
98 }
99 } else if ("usb_configuration".equals(name)) {
100 mConfiguration = intState;
101 // trigger an Intent broadcast
102 if (mSystemReady) {
103 update(mConnected == 0);
Mike Lockwood770126a2010-12-09 22:30:37 -0800104 }
Mike Lockwood24236072010-06-23 17:36:36 -0400105 }
Mike Lockwood770126a2010-12-09 22:30:37 -0800106 } catch (NumberFormatException e) {
107 Slog.e(TAG, "Could not parse switch state from event " + event);
Mike Lockwood24236072010-06-23 17:36:36 -0400108 }
Mike Lockwood770126a2010-12-09 22:30:37 -0800109 } else {
110 String function = event.get("FUNCTION");
111 String enabledStr = event.get("ENABLED");
112 if (function != null && enabledStr != null) {
113 // Note: we do not broadcast a change when a function is enabled or disabled.
114 // We just record the state change for the next broadcast.
115 boolean enabled = "1".equals(enabledStr);
116 if (enabled) {
117 if (!mEnabledFunctions.contains(function)) {
118 mEnabledFunctions.add(function);
119 }
120 mDisabledFunctions.remove(function);
121 } else {
122 if (!mDisabledFunctions.contains(function)) {
123 mDisabledFunctions.add(function);
124 }
125 mEnabledFunctions.remove(function);
Mike Lockwood24236072010-06-23 17:36:36 -0400126 }
Mike Lockwood24236072010-06-23 17:36:36 -0400127 }
128 }
129 }
130 }
Mike Lockwood770126a2010-12-09 22:30:37 -0800131 };
132
133 public UsbService(Context context) {
134 mContext = context;
135 init(); // set initial status
136
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800137 mUEventObserver.startObserving(USB_CONNECTED_MATCH);
Mike Lockwood770126a2010-12-09 22:30:37 -0800138 mUEventObserver.startObserving(USB_CONFIGURATION_MATCH);
139 mUEventObserver.startObserving(USB_FUNCTIONS_MATCH);
Mike Lockwood24236072010-06-23 17:36:36 -0400140 }
Mike Lockwood770126a2010-12-09 22:30:37 -0800141
Mike Lockwood24236072010-06-23 17:36:36 -0400142 private final void init() {
143 char[] buffer = new char[1024];
144
145 try {
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800146 FileReader file = new FileReader(USB_CONNECTED_PATH);
Mike Lockwood24236072010-06-23 17:36:36 -0400147 int len = file.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700148 file.close();
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800149 mConnected = Integer.valueOf((new String(buffer, 0, len)).trim());
150
151 file = new FileReader(USB_CONFIGURATION_PATH);
152 len = file.read(buffer, 0, 1024);
153 file.close();
154 mConfiguration = Integer.valueOf((new String(buffer, 0, len)).trim());
Mike Lockwood24236072010-06-23 17:36:36 -0400155
156 } catch (FileNotFoundException e) {
157 Slog.w(TAG, "This kernel does not have USB configuration switch support");
158 } catch (Exception e) {
159 Slog.e(TAG, "" , e);
160 }
161
162 try {
163 File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles();
164 for (int i = 0; i < files.length; i++) {
165 File file = new File(files[i], "enable");
166 FileReader reader = new FileReader(file);
167 int len = reader.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700168 reader.close();
Mike Lockwood24236072010-06-23 17:36:36 -0400169 int value = Integer.valueOf((new String(buffer, 0, len)).trim());
170 String functionName = files[i].getName();
171 if (value == 1) {
172 mEnabledFunctions.add(functionName);
173 } else {
174 mDisabledFunctions.add(functionName);
175 }
176 }
177 } catch (FileNotFoundException e) {
178 Slog.w(TAG, "This kernel does not have USB composite class support");
179 } catch (Exception e) {
180 Slog.e(TAG, "" , e);
181 }
182 }
183
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400184 private void initHostSupport() {
Mike Lockwood264f6cd2011-01-06 10:38:10 -0500185 // temporarily disabled
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400186 }
187
Mike Lockwood24236072010-06-23 17:36:36 -0400188 void systemReady() {
189 synchronized (this) {
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400190 if (mContext.getResources().getBoolean(
191 com.android.internal.R.bool.config_hasUsbHostSupport)) {
192 // start monitoring for connected USB devices
193 initHostSupport();
194 }
195
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800196 update(false);
Mike Lockwood24236072010-06-23 17:36:36 -0400197 mSystemReady = true;
198 }
199 }
200
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800201 private final void update(boolean delayed) {
202 mHandler.removeMessages(MSG_UPDATE);
203 mHandler.sendEmptyMessageDelayed(MSG_UPDATE, delayed ? UPDATE_DELAY : 0);
Mike Lockwood24236072010-06-23 17:36:36 -0400204 }
205
206 private final Handler mHandler = new Handler() {
Mike Lockwood709981e2010-06-28 09:58:58 -0400207 private void addEnabledFunctions(Intent intent) {
208 // include state of all USB functions in our extras
209 for (int i = 0; i < mEnabledFunctions.size(); i++) {
Mike Lockwood770126a2010-12-09 22:30:37 -0800210 intent.putExtra(mEnabledFunctions.get(i), UsbManager.USB_FUNCTION_ENABLED);
Mike Lockwood709981e2010-06-28 09:58:58 -0400211 }
212 for (int i = 0; i < mDisabledFunctions.size(); i++) {
Mike Lockwood770126a2010-12-09 22:30:37 -0800213 intent.putExtra(mDisabledFunctions.get(i), UsbManager.USB_FUNCTION_DISABLED);
Mike Lockwood709981e2010-06-28 09:58:58 -0400214 }
215 }
216
Mike Lockwood24236072010-06-23 17:36:36 -0400217 @Override
218 public void handleMessage(Message msg) {
219 switch (msg.what) {
220 case MSG_UPDATE:
221 synchronized (this) {
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800222 if (mConnected != mLastConnected || mConfiguration != mLastConfiguration) {
Mike Lockwood24236072010-06-23 17:36:36 -0400223
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800224 final ContentResolver cr = mContext.getContentResolver();
225 if (Settings.Secure.getInt(cr,
226 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
227 Slog.i(TAG, "Device not provisioned, skipping USB broadcast");
228 return;
229 }
230
231 mLastConnected = mConnected;
232 mLastConfiguration = mConfiguration;
233
234 // send a sticky broadcast containing current USB state
235 Intent intent = new Intent(UsbManager.ACTION_USB_STATE);
236 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
237 intent.putExtra(UsbManager.USB_CONNECTED, mConnected != 0);
238 intent.putExtra(UsbManager.USB_CONFIGURATION, mConfiguration);
Mike Lockwood709981e2010-06-28 09:58:58 -0400239 addEnabledFunctions(intent);
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800240 mContext.sendStickyBroadcast(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400241 }
Mike Lockwood24236072010-06-23 17:36:36 -0400242 }
243 break;
244 }
245 }
246 };
247}