am 3333f8af: Merge "DO NOT MERGE UsbManager: squashed commit of:" into gingerbread

* commit '3333f8afb2d1eae8bfdb41fa87f86836ad8702f7':
  DO NOT MERGE UsbManager: squashed commit of:
This commit is contained in:
Mike Lockwood
2011-02-28 13:17:58 -08:00
committed by Android Git Automerger
9 changed files with 145 additions and 73 deletions

View File

@ -17,11 +17,15 @@
package android.hardware;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* Class for accessing USB state information.
* @hide
*/
public class Usb {
public class UsbManager {
/**
* Broadcast Action: A broadcast for USB connected events.
*
@ -96,4 +100,30 @@ public class Usb {
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast
*/
public static final String USB_FUNCTION_DISABLED = "disabled";
private static File getFunctionEnableFile(String function) {
return new File("/sys/class/usb_composite/" + function + "/enable");
}
/**
* Returns true if the specified USB function is supported by the kernel.
* Note that a USB function maybe supported but disabled.
*/
public static boolean isFunctionSupported(String function) {
return getFunctionEnableFile(function).exists();
}
/**
* Returns true if the specified USB function is currently enabled.
*/
public static boolean isFunctionEnabled(String function) {
try {
FileInputStream stream = new FileInputStream(getFunctionEnableFile(function));
boolean enabled = (stream.read() == '1');
stream.close();
return enabled;
} catch (IOException e) {
return false;
}
}
}

View File

@ -30,7 +30,7 @@ import android.content.DialogInterface.OnCancelListener;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.hardware.Usb;
import android.hardware.UsbManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
@ -83,7 +83,7 @@ public class UsbStorageActivity extends Activity
private BroadcastReceiver mUsbStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Usb.ACTION_USB_STATE)) {
if (intent.getAction().equals(UsbManager.ACTION_USB_STATE)) {
handleUsbStateChanged(intent);
}
}
@ -175,7 +175,7 @@ public class UsbStorageActivity extends Activity
super.onResume();
mStorageManager.registerListener(mStorageListener);
registerReceiver(mUsbStateReceiver, new IntentFilter(Usb.ACTION_USB_STATE));
registerReceiver(mUsbStateReceiver, new IntentFilter(UsbManager.ACTION_USB_STATE));
try {
mAsyncStorageHandler.post(new Runnable() {
@Override
@ -199,7 +199,7 @@ public class UsbStorageActivity extends Activity
}
private void handleUsbStateChanged(Intent intent) {
boolean connected = intent.getExtras().getBoolean(Usb.USB_CONNECTED);
boolean connected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
if (!connected) {
// It was disconnected from the plug, so finish
finish();

View File

@ -38,7 +38,7 @@ import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.hardware.Usb;
import android.hardware.UsbManager;
import android.media.AudioManager;
import android.net.Uri;
import android.os.BatteryManager;
@ -352,13 +352,13 @@ public class NotificationManagerService extends INotificationManager.Stub
mBatteryFull = batteryFull;
updateLights();
}
} else if (action.equals(Usb.ACTION_USB_STATE)) {
} else if (action.equals(UsbManager.ACTION_USB_STATE)) {
Bundle extras = intent.getExtras();
boolean usbConnected = extras.getBoolean(Usb.USB_CONNECTED);
boolean adbEnabled = (Usb.USB_FUNCTION_ENABLED.equals(
extras.getString(Usb.USB_FUNCTION_ADB)));
boolean usbConnected = extras.getBoolean(UsbManager.USB_CONNECTED);
boolean adbEnabled = (UsbManager.USB_FUNCTION_ENABLED.equals(
extras.getString(UsbManager.USB_FUNCTION_ADB)));
updateAdbNotification(usbConnected && adbEnabled);
} else if (action.equals(Usb.ACTION_USB_DISCONNECTED)) {
} else if (action.equals(UsbManager.ACTION_USB_DISCONNECTED)) {
updateAdbNotification(false);
} else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
|| action.equals(Intent.ACTION_PACKAGE_RESTARTED)
@ -475,7 +475,7 @@ public class NotificationManagerService extends INotificationManager.Stub
// register for battery changed notifications
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(Usb.ACTION_USB_STATE);
filter.addAction(UsbManager.ACTION_USB_STATE);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);

View File

@ -122,7 +122,7 @@ class ServerThread extends Thread {
BluetoothA2dpService bluetoothA2dp = null;
HeadsetObserver headset = null;
DockObserver dock = null;
UsbObserver usb = null;
UsbService usb = null;
UiModeManagerService uiMode = null;
RecognitionManagerService recognition = null;
ThrottleService throttle = null;
@ -399,9 +399,9 @@ class ServerThread extends Thread {
try {
Slog.i(TAG, "USB Observer");
// Listen for USB changes
usb = new UsbObserver(context);
usb = new UsbService(context);
} catch (Throwable e) {
Slog.e(TAG, "Failure starting UsbObserver", e);
Slog.e(TAG, "Failure starting UsbService", e);
}
try {
@ -493,7 +493,7 @@ class ServerThread extends Thread {
final BatteryService batteryF = battery;
final ConnectivityService connectivityF = connectivity;
final DockObserver dockF = dock;
final UsbObserver usbF = usb;
final UsbService usbF = usb;
final ThrottleService throttleF = throttle;
final UiModeManagerService uiModeF = uiMode;
final AppWidgetService appWidgetF = appWidget;

View File

@ -19,7 +19,7 @@ package com.android.server;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.hardware.Usb;
import android.hardware.UsbManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
@ -34,10 +34,10 @@ import java.io.FileReader;
import java.util.ArrayList;
/**
* <p>UsbObserver monitors for changes to USB state.
* <p>UsbService monitors for changes to USB state.
*/
class UsbObserver extends UEventObserver {
private static final String TAG = UsbObserver.class.getSimpleName();
class UsbService {
private static final String TAG = UsbService.class.getSimpleName();
private static final boolean LOG = false;
private static final String USB_CONFIGURATION_MATCH = "DEVPATH=/devices/virtual/switch/usb_configuration";
@ -60,58 +60,61 @@ class UsbObserver extends UEventObserver {
private PowerManagerService mPowerManager;
public UsbObserver(Context context) {
mContext = context;
init(); // set initial status
private final UEventObserver mUEventObserver = new UEventObserver() {
@Override
public void onUEvent(UEventObserver.UEvent event) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Slog.v(TAG, "USB UEVENT: " + event.toString());
}
startObserving(USB_CONFIGURATION_MATCH);
startObserving(USB_FUNCTIONS_MATCH);
}
@Override
public void onUEvent(UEventObserver.UEvent event) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Slog.v(TAG, "USB UEVENT: " + event.toString());
}
synchronized (this) {
String switchState = event.get("SWITCH_STATE");
if (switchState != null) {
try {
int newConfig = Integer.parseInt(switchState);
if (newConfig != mUsbConfig) {
mPreviousUsbConfig = mUsbConfig;
mUsbConfig = newConfig;
// trigger an Intent broadcast
if (mSystemReady) {
update();
synchronized (this) {
String switchState = event.get("SWITCH_STATE");
if (switchState != null) {
try {
int newConfig = Integer.parseInt(switchState);
if (newConfig != mUsbConfig) {
mPreviousUsbConfig = mUsbConfig;
mUsbConfig = newConfig;
// trigger an Intent broadcast
if (mSystemReady) {
update();
}
}
} catch (NumberFormatException e) {
Slog.e(TAG, "Could not parse switch state from event " + event);
}
} catch (NumberFormatException e) {
Slog.e(TAG, "Could not parse switch state from event " + event);
}
} else {
String function = event.get("FUNCTION");
String enabledStr = event.get("ENABLED");
if (function != null && enabledStr != null) {
// Note: we do not broadcast a change when a function is enabled or disabled.
// We just record the state change for the next broadcast.
boolean enabled = "1".equals(enabledStr);
if (enabled) {
if (!mEnabledFunctions.contains(function)) {
mEnabledFunctions.add(function);
} else {
String function = event.get("FUNCTION");
String enabledStr = event.get("ENABLED");
if (function != null && enabledStr != null) {
// Note: we do not broadcast a change when a function is enabled or disabled.
// We just record the state change for the next broadcast.
boolean enabled = "1".equals(enabledStr);
if (enabled) {
if (!mEnabledFunctions.contains(function)) {
mEnabledFunctions.add(function);
}
mDisabledFunctions.remove(function);
} else {
if (!mDisabledFunctions.contains(function)) {
mDisabledFunctions.add(function);
}
mEnabledFunctions.remove(function);
}
mDisabledFunctions.remove(function);
} else {
if (!mDisabledFunctions.contains(function)) {
mDisabledFunctions.add(function);
}
mEnabledFunctions.remove(function);
}
}
}
}
};
public UsbService(Context context) {
mContext = context;
init(); // set initial status
mUEventObserver.startObserving(USB_CONFIGURATION_MATCH);
mUEventObserver.startObserving(USB_FUNCTIONS_MATCH);
}
private final void init() {
char[] buffer = new char[1024];
@ -162,10 +165,10 @@ class UsbObserver extends UEventObserver {
private void addEnabledFunctions(Intent intent) {
// include state of all USB functions in our extras
for (int i = 0; i < mEnabledFunctions.size(); i++) {
intent.putExtra(mEnabledFunctions.get(i), Usb.USB_FUNCTION_ENABLED);
intent.putExtra(mEnabledFunctions.get(i), UsbManager.USB_FUNCTION_ENABLED);
}
for (int i = 0; i < mDisabledFunctions.size(); i++) {
intent.putExtra(mDisabledFunctions.get(i), Usb.USB_FUNCTION_DISABLED);
intent.putExtra(mDisabledFunctions.get(i), UsbManager.USB_FUNCTION_DISABLED);
}
}
@ -186,17 +189,17 @@ class UsbObserver extends UEventObserver {
Intent intent;
boolean usbConnected = (mUsbConfig != 0);
if (usbConnected) {
intent = new Intent(Usb.ACTION_USB_CONNECTED);
intent = new Intent(UsbManager.ACTION_USB_CONNECTED);
addEnabledFunctions(intent);
} else {
intent = new Intent(Usb.ACTION_USB_DISCONNECTED);
intent = new Intent(UsbManager.ACTION_USB_DISCONNECTED);
}
mContext.sendBroadcast(intent);
// send a sticky broadcast for clients interested in both connect and disconnect
intent = new Intent(Usb.ACTION_USB_STATE);
intent = new Intent(UsbManager.ACTION_USB_STATE);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
intent.putExtra(Usb.USB_CONNECTED, usbConnected);
intent.putExtra(UsbManager.USB_CONNECTED, usbConnected);
addEnabledFunctions(intent);
mContext.sendStickyBroadcast(intent);
}

View File

@ -26,7 +26,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.hardware.Usb;
import android.hardware.UsbManager;
import android.net.ConnectivityManager;
import android.net.InterfaceConfiguration;
import android.net.IConnectivityManager;
@ -143,7 +143,7 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
mStateReceiver = new StateReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Usb.ACTION_USB_STATE);
filter.addAction(UsbManager.ACTION_USB_STATE);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
mContext.registerReceiver(mStateReceiver, filter);
@ -440,10 +440,10 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
private class StateReceiver extends BroadcastReceiver {
public void onReceive(Context content, Intent intent) {
String action = intent.getAction();
if (action.equals(Usb.ACTION_USB_STATE)) {
if (action.equals(UsbManager.ACTION_USB_STATE)) {
// process connect events immediately, but delay handling disconnects
// to debounce USB configuration changes
boolean connected = intent.getExtras().getBoolean(Usb.USB_CONNECTED);
boolean connected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
Message msg = Message.obtain(mUsbHandler, USB_STATE_CHANGE,
(connected ? USB_CONNECTED : USB_DISCONNECTED), 0);
mUsbHandler.removeMessages(USB_STATE_CHANGE);

View File

@ -8,6 +8,7 @@ LOCAL_SRC_FILES:= \
com_android_server_LightsService.cpp \
com_android_server_PowerManagerService.cpp \
com_android_server_SystemServer.cpp \
com_android_server_UsbService.cpp \
com_android_server_VibratorService.cpp \
com_android_server_location_GpsLocationProvider.cpp \
onload.cpp

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "UsbService"
#include "utils/Log.h"
#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include "utils/Vector.h"
#include <stdio.h>
namespace android
{
int register_android_server_UsbService(JNIEnv *env)
{
return 0;
}
};

View File

@ -9,6 +9,7 @@ int register_android_server_BatteryService(JNIEnv* env);
int register_android_server_InputManager(JNIEnv* env);
int register_android_server_LightsService(JNIEnv* env);
int register_android_server_PowerManagerService(JNIEnv* env);
int register_android_server_UsbService(JNIEnv* env);
int register_android_server_VibratorService(JNIEnv* env);
int register_android_server_SystemServer(JNIEnv* env);
int register_android_server_location_GpsLocationProvider(JNIEnv* env);
@ -32,6 +33,7 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
register_android_server_LightsService(env);
register_android_server_AlarmManagerService(env);
register_android_server_BatteryService(env);
register_android_server_UsbService(env);
register_android_server_VibratorService(env);
register_android_server_SystemServer(env);
register_android_server_location_GpsLocationProvider(env);