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; package android.hardware;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/** /**
* Class for accessing USB state information. * Class for accessing USB state information.
* @hide * @hide
*/ */
public class Usb { public class UsbManager {
/** /**
* Broadcast Action: A broadcast for USB connected events. * 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 * Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast
*/ */
public static final String USB_FUNCTION_DISABLED = "disabled"; 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.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.hardware.Usb; import android.hardware.UsbManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
@ -83,7 +83,7 @@ public class UsbStorageActivity extends Activity
private BroadcastReceiver mUsbStateReceiver = new BroadcastReceiver() { private BroadcastReceiver mUsbStateReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { 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); handleUsbStateChanged(intent);
} }
} }
@ -175,7 +175,7 @@ public class UsbStorageActivity extends Activity
super.onResume(); super.onResume();
mStorageManager.registerListener(mStorageListener); mStorageManager.registerListener(mStorageListener);
registerReceiver(mUsbStateReceiver, new IntentFilter(Usb.ACTION_USB_STATE)); registerReceiver(mUsbStateReceiver, new IntentFilter(UsbManager.ACTION_USB_STATE));
try { try {
mAsyncStorageHandler.post(new Runnable() { mAsyncStorageHandler.post(new Runnable() {
@Override @Override
@ -199,7 +199,7 @@ public class UsbStorageActivity extends Activity
} }
private void handleUsbStateChanged(Intent intent) { private void handleUsbStateChanged(Intent intent) {
boolean connected = intent.getExtras().getBoolean(Usb.USB_CONNECTED); boolean connected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
if (!connected) { if (!connected) {
// It was disconnected from the plug, so finish // It was disconnected from the plug, so finish
finish(); finish();

View File

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

View File

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

View File

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

View File

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

View File

@ -8,6 +8,7 @@ LOCAL_SRC_FILES:= \
com_android_server_LightsService.cpp \ com_android_server_LightsService.cpp \
com_android_server_PowerManagerService.cpp \ com_android_server_PowerManagerService.cpp \
com_android_server_SystemServer.cpp \ com_android_server_SystemServer.cpp \
com_android_server_UsbService.cpp \
com_android_server_VibratorService.cpp \ com_android_server_VibratorService.cpp \
com_android_server_location_GpsLocationProvider.cpp \ com_android_server_location_GpsLocationProvider.cpp \
onload.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_InputManager(JNIEnv* env);
int register_android_server_LightsService(JNIEnv* env); int register_android_server_LightsService(JNIEnv* env);
int register_android_server_PowerManagerService(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_VibratorService(JNIEnv* env);
int register_android_server_SystemServer(JNIEnv* env); int register_android_server_SystemServer(JNIEnv* env);
int register_android_server_location_GpsLocationProvider(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_LightsService(env);
register_android_server_AlarmManagerService(env); register_android_server_AlarmManagerService(env);
register_android_server_BatteryService(env); register_android_server_BatteryService(env);
register_android_server_UsbService(env);
register_android_server_VibratorService(env); register_android_server_VibratorService(env);
register_android_server_SystemServer(env); register_android_server_SystemServer(env);
register_android_server_location_GpsLocationProvider(env); register_android_server_location_GpsLocationProvider(env);