blob: d08fe9b3964b068412f1d3bfab405ee0cad1e765 [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;
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/**
37 * <p>UsbObserver monitors for changes to USB state.
38 */
39class UsbObserver extends UEventObserver {
40 private static final String TAG = UsbObserver.class.getSimpleName();
41 private static final boolean LOG = false;
42
43 private static final String USB_CONFIGURATION_MATCH = "DEVPATH=/devices/virtual/switch/usb_configuration";
44 private static final String USB_FUNCTIONS_MATCH = "DEVPATH=/devices/virtual/usb_composite/";
45 private static final String USB_CONFIGURATION_PATH = "/sys/class/switch/usb_configuration/state";
46 private static final String USB_COMPOSITE_CLASS_PATH = "/sys/class/usb_composite";
47
48 private static final int MSG_UPDATE = 0;
49
50 private int mUsbConfig = 0;
51 private int mPreviousUsbConfig = 0;
52
53 // lists of enabled and disabled USB functions
54 private final ArrayList<String> mEnabledFunctions = new ArrayList<String>();
55 private final ArrayList<String> mDisabledFunctions = new ArrayList<String>();
56
57 private boolean mSystemReady;
58
59 private final Context mContext;
60
61 private PowerManagerService mPowerManager;
62
63 public UsbObserver(Context context) {
64 mContext = context;
65 init(); // set initial status
66
67 startObserving(USB_CONFIGURATION_MATCH);
68 startObserving(USB_FUNCTIONS_MATCH);
69 }
70
71 @Override
72 public void onUEvent(UEventObserver.UEvent event) {
73 if (Log.isLoggable(TAG, Log.VERBOSE)) {
74 Slog.v(TAG, "USB UEVENT: " + event.toString());
75 }
76
77 synchronized (this) {
78 String switchState = event.get("SWITCH_STATE");
79 if (switchState != null) {
80 try {
81 int newConfig = Integer.parseInt(switchState);
82 if (newConfig != mUsbConfig) {
83 mPreviousUsbConfig = mUsbConfig;
84 mUsbConfig = newConfig;
85 // trigger an Intent broadcast
86 if (mSystemReady) {
87 update();
88 }
89 }
90 } catch (NumberFormatException e) {
91 Slog.e(TAG, "Could not parse switch state from event " + event);
92 }
93 } else {
94 String function = event.get("FUNCTION");
95 String enabledStr = event.get("ENABLED");
96 if (function != null && enabledStr != null) {
97 // Note: we do not broadcast a change when a function is enabled or disabled.
98 // We just record the state change for the next broadcast.
99 boolean enabled = "1".equals(enabledStr);
100 if (enabled) {
101 if (!mEnabledFunctions.contains(function)) {
102 mEnabledFunctions.add(function);
103 }
104 mDisabledFunctions.remove(function);
105 } else {
106 if (!mDisabledFunctions.contains(function)) {
107 mDisabledFunctions.add(function);
108 }
109 mEnabledFunctions.remove(function);
110 }
111 }
112 }
113 }
114 }
115 private final void init() {
116 char[] buffer = new char[1024];
117
118 try {
119 FileReader file = new FileReader(USB_CONFIGURATION_PATH);
120 int len = file.read(buffer, 0, 1024);
121 mPreviousUsbConfig = mUsbConfig = Integer.valueOf((new String(buffer, 0, len)).trim());
122
123 } catch (FileNotFoundException e) {
124 Slog.w(TAG, "This kernel does not have USB configuration switch support");
125 } catch (Exception e) {
126 Slog.e(TAG, "" , e);
127 }
128
129 try {
130 File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles();
131 for (int i = 0; i < files.length; i++) {
132 File file = new File(files[i], "enable");
133 FileReader reader = new FileReader(file);
134 int len = reader.read(buffer, 0, 1024);
135 int value = Integer.valueOf((new String(buffer, 0, len)).trim());
136 String functionName = files[i].getName();
137 if (value == 1) {
138 mEnabledFunctions.add(functionName);
139 } else {
140 mDisabledFunctions.add(functionName);
141 }
142 }
143 } catch (FileNotFoundException e) {
144 Slog.w(TAG, "This kernel does not have USB composite class support");
145 } catch (Exception e) {
146 Slog.e(TAG, "" , e);
147 }
148 }
149
150 void systemReady() {
151 synchronized (this) {
152 update();
153 mSystemReady = true;
154 }
155 }
156
157 private final void update() {
158 mHandler.sendEmptyMessage(MSG_UPDATE);
159 }
160
161 private final Handler mHandler = new Handler() {
Mike Lockwood709981e2010-06-28 09:58:58 -0400162 private void addEnabledFunctions(Intent intent) {
163 // include state of all USB functions in our extras
164 for (int i = 0; i < mEnabledFunctions.size(); i++) {
165 intent.putExtra(mEnabledFunctions.get(i), Usb.USB_FUNCTION_ENABLED);
166 }
167 for (int i = 0; i < mDisabledFunctions.size(); i++) {
168 intent.putExtra(mDisabledFunctions.get(i), Usb.USB_FUNCTION_DISABLED);
169 }
170 }
171
Mike Lockwood24236072010-06-23 17:36:36 -0400172 @Override
173 public void handleMessage(Message msg) {
174 switch (msg.what) {
175 case MSG_UPDATE:
176 synchronized (this) {
177 final ContentResolver cr = mContext.getContentResolver();
178
179 if (Settings.Secure.getInt(cr,
180 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
181 Slog.i(TAG, "Device not provisioned, skipping USB broadcast");
182 return;
183 }
184 // Send an Intent containing connected/disconnected state
185 // and the enabled/disabled state of all USB functions
186 Intent intent;
Mike Lockwood709981e2010-06-28 09:58:58 -0400187 boolean usbConnected = (mUsbConfig != 0);
188 if (usbConnected) {
Mike Lockwood24236072010-06-23 17:36:36 -0400189 intent = new Intent(Usb.ACTION_USB_CONNECTED);
Mike Lockwood709981e2010-06-28 09:58:58 -0400190 addEnabledFunctions(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400191 } else {
192 intent = new Intent(Usb.ACTION_USB_DISCONNECTED);
193 }
Mike Lockwood709981e2010-06-28 09:58:58 -0400194 mContext.sendBroadcast(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400195
Mike Lockwood709981e2010-06-28 09:58:58 -0400196 // send a sticky broadcast for clients interested in both connect and disconnect
197 intent = new Intent(Usb.ACTION_USB_STATE);
198 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
199 intent.putExtra(Usb.USB_CONNECTED, usbConnected);
200 addEnabledFunctions(intent);
201 mContext.sendStickyBroadcast(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400202 }
203 break;
204 }
205 }
206 };
207}