Skip to content

Commit 3333f8a

Browse files
mikeandroidAndroid (Google) Code Review
authored andcommitted
Merge "DO NOT MERGE UsbManager: squashed commit of:" into gingerbread
2 parents f7b99b3 + 7916432 commit 3333f8a

File tree

9 files changed

+145
-73
lines changed

9 files changed

+145
-73
lines changed

core/java/android/hardware/Usb.java renamed to core/java/android/hardware/UsbManager.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717

1818
package android.hardware;
1919

20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.IOException;
23+
2024
/**
2125
* Class for accessing USB state information.
2226
* @hide
2327
*/
24-
public class Usb {
28+
public class UsbManager {
2529
/**
2630
* Broadcast Action: A broadcast for USB connected events.
2731
*
@@ -96,4 +100,30 @@ public class Usb {
96100
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast
97101
*/
98102
public static final String USB_FUNCTION_DISABLED = "disabled";
103+
104+
private static File getFunctionEnableFile(String function) {
105+
return new File("/sys/class/usb_composite/" + function + "/enable");
106+
}
107+
108+
/**
109+
* Returns true if the specified USB function is supported by the kernel.
110+
* Note that a USB function maybe supported but disabled.
111+
*/
112+
public static boolean isFunctionSupported(String function) {
113+
return getFunctionEnableFile(function).exists();
114+
}
115+
116+
/**
117+
* Returns true if the specified USB function is currently enabled.
118+
*/
119+
public static boolean isFunctionEnabled(String function) {
120+
try {
121+
FileInputStream stream = new FileInputStream(getFunctionEnableFile(function));
122+
boolean enabled = (stream.read() == '1');
123+
stream.close();
124+
return enabled;
125+
} catch (IOException e) {
126+
return false;
127+
}
128+
}
99129
}

packages/SystemUI/src/com/android/systemui/usb/UsbStorageActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import android.content.pm.ApplicationInfo;
3131
import android.content.pm.PackageManager;
3232
import android.content.pm.PackageManager.NameNotFoundException;
33-
import android.hardware.Usb;
33+
import android.hardware.UsbManager;
3434
import android.os.Bundle;
3535
import android.os.Environment;
3636
import android.os.Handler;
@@ -83,7 +83,7 @@ public class UsbStorageActivity extends Activity
8383
private BroadcastReceiver mUsbStateReceiver = new BroadcastReceiver() {
8484
@Override
8585
public void onReceive(Context context, Intent intent) {
86-
if (intent.getAction().equals(Usb.ACTION_USB_STATE)) {
86+
if (intent.getAction().equals(UsbManager.ACTION_USB_STATE)) {
8787
handleUsbStateChanged(intent);
8888
}
8989
}
@@ -175,7 +175,7 @@ protected void onResume() {
175175
super.onResume();
176176

177177
mStorageManager.registerListener(mStorageListener);
178-
registerReceiver(mUsbStateReceiver, new IntentFilter(Usb.ACTION_USB_STATE));
178+
registerReceiver(mUsbStateReceiver, new IntentFilter(UsbManager.ACTION_USB_STATE));
179179
try {
180180
mAsyncStorageHandler.post(new Runnable() {
181181
@Override
@@ -199,7 +199,7 @@ protected void onPause() {
199199
}
200200

201201
private void handleUsbStateChanged(Intent intent) {
202-
boolean connected = intent.getExtras().getBoolean(Usb.USB_CONNECTED);
202+
boolean connected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
203203
if (!connected) {
204204
// It was disconnected from the plug, so finish
205205
finish();

services/java/com/android/server/NotificationManagerService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import android.content.pm.PackageManager.NameNotFoundException;
3939
import android.content.res.Resources;
4040
import android.database.ContentObserver;
41-
import android.hardware.Usb;
41+
import android.hardware.UsbManager;
4242
import android.media.AudioManager;
4343
import android.net.Uri;
4444
import android.os.BatteryManager;
@@ -352,13 +352,13 @@ public void onReceive(Context context, Intent intent) {
352352
mBatteryFull = batteryFull;
353353
updateLights();
354354
}
355-
} else if (action.equals(Usb.ACTION_USB_STATE)) {
355+
} else if (action.equals(UsbManager.ACTION_USB_STATE)) {
356356
Bundle extras = intent.getExtras();
357-
boolean usbConnected = extras.getBoolean(Usb.USB_CONNECTED);
358-
boolean adbEnabled = (Usb.USB_FUNCTION_ENABLED.equals(
359-
extras.getString(Usb.USB_FUNCTION_ADB)));
357+
boolean usbConnected = extras.getBoolean(UsbManager.USB_CONNECTED);
358+
boolean adbEnabled = (UsbManager.USB_FUNCTION_ENABLED.equals(
359+
extras.getString(UsbManager.USB_FUNCTION_ADB)));
360360
updateAdbNotification(usbConnected && adbEnabled);
361-
} else if (action.equals(Usb.ACTION_USB_DISCONNECTED)) {
361+
} else if (action.equals(UsbManager.ACTION_USB_DISCONNECTED)) {
362362
updateAdbNotification(false);
363363
} else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
364364
|| action.equals(Intent.ACTION_PACKAGE_RESTARTED)
@@ -464,7 +464,7 @@ public void update() {
464464
// register for battery changed notifications
465465
IntentFilter filter = new IntentFilter();
466466
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
467-
filter.addAction(Usb.ACTION_USB_STATE);
467+
filter.addAction(UsbManager.ACTION_USB_STATE);
468468
filter.addAction(Intent.ACTION_SCREEN_ON);
469469
filter.addAction(Intent.ACTION_SCREEN_OFF);
470470
filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);

services/java/com/android/server/SystemServer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void run() {
122122
BluetoothA2dpService bluetoothA2dp = null;
123123
HeadsetObserver headset = null;
124124
DockObserver dock = null;
125-
UsbObserver usb = null;
125+
UsbService usb = null;
126126
UiModeManagerService uiMode = null;
127127
RecognitionManagerService recognition = null;
128128
ThrottleService throttle = null;
@@ -399,9 +399,9 @@ public void run() {
399399
try {
400400
Slog.i(TAG, "USB Observer");
401401
// Listen for USB changes
402-
usb = new UsbObserver(context);
402+
usb = new UsbService(context);
403403
} catch (Throwable e) {
404-
Slog.e(TAG, "Failure starting UsbObserver", e);
404+
Slog.e(TAG, "Failure starting UsbService", e);
405405
}
406406

407407
try {
@@ -493,7 +493,7 @@ public void run() {
493493
final BatteryService batteryF = battery;
494494
final ConnectivityService connectivityF = connectivity;
495495
final DockObserver dockF = dock;
496-
final UsbObserver usbF = usb;
496+
final UsbService usbF = usb;
497497
final ThrottleService throttleF = throttle;
498498
final UiModeManagerService uiModeF = uiMode;
499499
final AppWidgetService appWidgetF = appWidget;

services/java/com/android/server/UsbObserver.java renamed to services/java/com/android/server/UsbService.java

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import android.content.ContentResolver;
2020
import android.content.Context;
2121
import android.content.Intent;
22-
import android.hardware.Usb;
22+
import android.hardware.UsbManager;
2323
import android.net.Uri;
2424
import android.os.Handler;
2525
import android.os.Message;
@@ -34,10 +34,10 @@
3434
import java.util.ArrayList;
3535

3636
/**
37-
* <p>UsbObserver monitors for changes to USB state.
37+
* <p>UsbService monitors for changes to USB state.
3838
*/
39-
class UsbObserver extends UEventObserver {
40-
private static final String TAG = UsbObserver.class.getSimpleName();
39+
class UsbService {
40+
private static final String TAG = UsbService.class.getSimpleName();
4141
private static final boolean LOG = false;
4242

4343
private static final String USB_CONFIGURATION_MATCH = "DEVPATH=/devices/virtual/switch/usb_configuration";
@@ -60,58 +60,61 @@ class UsbObserver extends UEventObserver {
6060

6161
private PowerManagerService mPowerManager;
6262

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-
}
63+
private final UEventObserver mUEventObserver = new UEventObserver() {
64+
@Override
65+
public void onUEvent(UEventObserver.UEvent event) {
66+
if (Log.isLoggable(TAG, Log.VERBOSE)) {
67+
Slog.v(TAG, "USB UEVENT: " + event.toString());
68+
}
7669

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();
70+
synchronized (this) {
71+
String switchState = event.get("SWITCH_STATE");
72+
if (switchState != null) {
73+
try {
74+
int newConfig = Integer.parseInt(switchState);
75+
if (newConfig != mUsbConfig) {
76+
mPreviousUsbConfig = mUsbConfig;
77+
mUsbConfig = newConfig;
78+
// trigger an Intent broadcast
79+
if (mSystemReady) {
80+
update();
81+
}
8882
}
83+
} catch (NumberFormatException e) {
84+
Slog.e(TAG, "Could not parse switch state from event " + event);
8985
}
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);
86+
} else {
87+
String function = event.get("FUNCTION");
88+
String enabledStr = event.get("ENABLED");
89+
if (function != null && enabledStr != null) {
90+
// Note: we do not broadcast a change when a function is enabled or disabled.
91+
// We just record the state change for the next broadcast.
92+
boolean enabled = "1".equals(enabledStr);
93+
if (enabled) {
94+
if (!mEnabledFunctions.contains(function)) {
95+
mEnabledFunctions.add(function);
96+
}
97+
mDisabledFunctions.remove(function);
98+
} else {
99+
if (!mDisabledFunctions.contains(function)) {
100+
mDisabledFunctions.add(function);
101+
}
102+
mEnabledFunctions.remove(function);
108103
}
109-
mEnabledFunctions.remove(function);
110104
}
111105
}
112106
}
113107
}
108+
};
109+
110+
public UsbService(Context context) {
111+
mContext = context;
112+
init(); // set initial status
113+
114+
mUEventObserver.startObserving(USB_CONFIGURATION_MATCH);
115+
mUEventObserver.startObserving(USB_FUNCTIONS_MATCH);
114116
}
117+
115118
private final void init() {
116119
char[] buffer = new char[1024];
117120

@@ -162,10 +165,10 @@ private final void update() {
162165
private void addEnabledFunctions(Intent intent) {
163166
// include state of all USB functions in our extras
164167
for (int i = 0; i < mEnabledFunctions.size(); i++) {
165-
intent.putExtra(mEnabledFunctions.get(i), Usb.USB_FUNCTION_ENABLED);
168+
intent.putExtra(mEnabledFunctions.get(i), UsbManager.USB_FUNCTION_ENABLED);
166169
}
167170
for (int i = 0; i < mDisabledFunctions.size(); i++) {
168-
intent.putExtra(mDisabledFunctions.get(i), Usb.USB_FUNCTION_DISABLED);
171+
intent.putExtra(mDisabledFunctions.get(i), UsbManager.USB_FUNCTION_DISABLED);
169172
}
170173
}
171174

@@ -186,17 +189,17 @@ public void handleMessage(Message msg) {
186189
Intent intent;
187190
boolean usbConnected = (mUsbConfig != 0);
188191
if (usbConnected) {
189-
intent = new Intent(Usb.ACTION_USB_CONNECTED);
192+
intent = new Intent(UsbManager.ACTION_USB_CONNECTED);
190193
addEnabledFunctions(intent);
191194
} else {
192-
intent = new Intent(Usb.ACTION_USB_DISCONNECTED);
195+
intent = new Intent(UsbManager.ACTION_USB_DISCONNECTED);
193196
}
194197
mContext.sendBroadcast(intent);
195198

196199
// send a sticky broadcast for clients interested in both connect and disconnect
197-
intent = new Intent(Usb.ACTION_USB_STATE);
200+
intent = new Intent(UsbManager.ACTION_USB_STATE);
198201
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
199-
intent.putExtra(Usb.USB_CONNECTED, usbConnected);
202+
intent.putExtra(UsbManager.USB_CONNECTED, usbConnected);
200203
addEnabledFunctions(intent);
201204
mContext.sendStickyBroadcast(intent);
202205
}

services/java/com/android/server/connectivity/Tethering.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import android.content.IntentFilter;
2727
import android.content.pm.PackageManager;
2828
import android.content.res.Resources;
29-
import android.hardware.Usb;
29+
import android.hardware.UsbManager;
3030
import android.net.ConnectivityManager;
3131
import android.net.InterfaceConfiguration;
3232
import android.net.IConnectivityManager;
@@ -143,7 +143,7 @@ public Tethering(Context context, Looper looper) {
143143

144144
mStateReceiver = new StateReceiver();
145145
IntentFilter filter = new IntentFilter();
146-
filter.addAction(Usb.ACTION_USB_STATE);
146+
filter.addAction(UsbManager.ACTION_USB_STATE);
147147
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
148148
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
149149
mContext.registerReceiver(mStateReceiver, filter);
@@ -440,10 +440,10 @@ public void handleMessage(Message msg) {
440440
private class StateReceiver extends BroadcastReceiver {
441441
public void onReceive(Context content, Intent intent) {
442442
String action = intent.getAction();
443-
if (action.equals(Usb.ACTION_USB_STATE)) {
443+
if (action.equals(UsbManager.ACTION_USB_STATE)) {
444444
// process connect events immediately, but delay handling disconnects
445445
// to debounce USB configuration changes
446-
boolean connected = intent.getExtras().getBoolean(Usb.USB_CONNECTED);
446+
boolean connected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
447447
Message msg = Message.obtain(mUsbHandler, USB_STATE_CHANGE,
448448
(connected ? USB_CONNECTED : USB_DISCONNECTED), 0);
449449
mUsbHandler.removeMessages(USB_STATE_CHANGE);

services/jni/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LOCAL_SRC_FILES:= \
88
com_android_server_LightsService.cpp \
99
com_android_server_PowerManagerService.cpp \
1010
com_android_server_SystemServer.cpp \
11+
com_android_server_UsbService.cpp \
1112
com_android_server_VibratorService.cpp \
1213
com_android_server_location_GpsLocationProvider.cpp \
1314
onload.cpp
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2009 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+
17+
#define LOG_TAG "UsbService"
18+
#include "utils/Log.h"
19+
20+
#include "jni.h"
21+
#include "JNIHelp.h"
22+
#include "android_runtime/AndroidRuntime.h"
23+
#include "utils/Vector.h"
24+
25+
#include <stdio.h>
26+
27+
namespace android
28+
{
29+
30+
int register_android_server_UsbService(JNIEnv *env)
31+
{
32+
33+
return 0;
34+
}
35+
36+
};

0 commit comments

Comments
 (0)