diff options
Diffstat (limited to 'core/java/android')
-rw-r--r-- | core/java/android/app/ContextImpl.java | 16 | ||||
-rw-r--r-- | core/java/android/content/Context.java | 11 | ||||
-rw-r--r-- | core/java/android/content/pm/PackageManager.java | 7 | ||||
-rw-r--r-- | core/java/android/hardware/UsbManager.java | 115 | ||||
-rw-r--r-- | core/java/android/hardware/usb/IUsbManager.aidl | 47 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbAccessory.aidl | 19 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbAccessory.java | 145 | ||||
-rw-r--r-- | core/java/android/hardware/usb/UsbManager.java | 239 | ||||
-rw-r--r-- | core/java/android/util/SparseArray.java | 11 |
9 files changed, 495 insertions, 115 deletions
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index 50ec34f..fb9335b 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -62,6 +62,8 @@ import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.hardware.SensorManager; +import android.hardware.usb.IUsbManager; +import android.hardware.usb.UsbManager; import android.location.ILocationManager; import android.location.LocationManager; import android.media.AudioManager; @@ -191,6 +193,7 @@ class ContextImpl extends Context { private SearchManager mSearchManager = null; private SensorManager mSensorManager = null; private StorageManager mStorageManager = null; + private UsbManager mUsbManager = null; private Vibrator mVibrator = null; private LayoutInflater mLayoutInflater = null; private StatusBarManager mStatusBarManager = null; @@ -954,6 +957,8 @@ class ContextImpl extends Context { return getSensorManager(); } else if (STORAGE_SERVICE.equals(name)) { return getStorageManager(); + } else if (USB_SERVICE.equals(name)) { + return getUsbManager(); } else if (VIBRATOR_SERVICE.equals(name)) { return getVibrator(); } else if (STATUS_BAR_SERVICE.equals(name)) { @@ -1148,6 +1153,17 @@ class ContextImpl extends Context { return mStorageManager; } + private UsbManager getUsbManager() { + synchronized (mSync) { + if (mUsbManager == null) { + IBinder b = ServiceManager.getService(USB_SERVICE); + IUsbManager service = IUsbManager.Stub.asInterface(b); + mUsbManager = new UsbManager(service); + } + } + return mUsbManager; + } + private Vibrator getVibrator() { synchronized (mSync) { if (mVibrator == null) { diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index 85c29b8..ec2d640 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -1567,6 +1567,17 @@ public abstract class Context { public static final String SIP_SERVICE = "sip"; /** + * Use with {@link #getSystemService} to retrieve a {@link + * android.hardware.usb.UsbManager} for access to USB devices (as a USB host) + * and for controlling this device's behavior as a USB device. + * + * @see #getSystemService + * @see android.harware.usb.UsbManager + * @hide + */ + public static final String USB_SERVICE = "usb"; + + /** * Determine whether the given permission is allowed for a particular * process and user ID running in the system. * diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java index 922f8cd..a779925 100644 --- a/core/java/android/content/pm/PackageManager.java +++ b/core/java/android/content/pm/PackageManager.java @@ -785,6 +785,13 @@ public abstract class PackageManager { /** * Feature for {@link #getSystemAvailableFeatures} and + * {@link #hasSystemFeature}: The device supports connecting to USB accessories. + * @hide + */ + public static final String FEATURE_USB_ACCESSORY = "android.hardware.usb.accessory"; + + /** + * Feature for {@link #getSystemAvailableFeatures} and * {@link #hasSystemFeature}: The SIP API is enabled on the device. */ @SdkConstant(SdkConstantType.FEATURE) diff --git a/core/java/android/hardware/UsbManager.java b/core/java/android/hardware/UsbManager.java deleted file mode 100644 index 18790d2..0000000 --- a/core/java/android/hardware/UsbManager.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (C) 2010 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. - */ - - -package android.hardware; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; - -/** - * Class for accessing USB state information. - * @hide - */ -public class UsbManager { - /** - * Broadcast Action: A sticky broadcast for USB state change events. - * - * This is a sticky broadcast for clients that includes USB connected/disconnected state, - * the USB configuration that is currently set and a bundle containing name/value pairs - * with the names of the functions and a value of either {@link #USB_FUNCTION_ENABLED} - * or {@link #USB_FUNCTION_DISABLED}. - * Possible USB function names include {@link #USB_FUNCTION_MASS_STORAGE}, - * {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS} and {@link #USB_FUNCTION_MTP}. - */ - public static final String ACTION_USB_STATE = - "android.hardware.action.USB_STATE"; - - /** - * Boolean extra indicating whether USB is connected or disconnected. - * Used in extras for the {@link #ACTION_USB_STATE} broadcast. - */ - public static final String USB_CONNECTED = "connected"; - - /** - * Integer extra containing currently set USB configuration. - * Used in extras for the {@link #ACTION_USB_STATE} broadcast. - */ - public static final String USB_CONFIGURATION = "configuration"; - - /** - * Name of the USB mass storage USB function. - * Used in extras for the {@link #ACTION_USB_STATE} broadcast - */ - public static final String USB_FUNCTION_MASS_STORAGE = "mass_storage"; - - /** - * Name of the adb USB function. - * Used in extras for the {@link #ACTION_USB_STATE} broadcast - */ - public static final String USB_FUNCTION_ADB = "adb"; - - /** - * Name of the RNDIS ethernet USB function. - * Used in extras for the {@link #ACTION_USB_STATE} broadcast - */ - public static final String USB_FUNCTION_RNDIS = "rndis"; - - /** - * Name of the MTP USB function. - * Used in extras for the {@link #ACTION_USB_STATE} broadcast - */ - public static final String USB_FUNCTION_MTP = "mtp"; - - /** - * Value indicating that a USB function is enabled. - * Used in extras for the {@link #ACTION_USB_STATE} broadcast - */ - public static final String USB_FUNCTION_ENABLED = "enabled"; - - /** - * Value indicating that a USB function is disabled. - * Used in extras for the {@link #ACTION_USB_STATE} 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; - } - } -} diff --git a/core/java/android/hardware/usb/IUsbManager.aidl b/core/java/android/hardware/usb/IUsbManager.aidl new file mode 100644 index 0000000..9be7e77 --- /dev/null +++ b/core/java/android/hardware/usb/IUsbManager.aidl @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2010 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. + */ + +package android.hardware.usb; + +import android.hardware.usb.UsbAccessory; +import android.os.Bundle; +import android.os.ParcelFileDescriptor; + +/** @hide */ +interface IUsbManager +{ + /* Returns the currently attached USB accessory */ + UsbAccessory getCurrentAccessory(); + + /* Returns a file descriptor for communicating with the USB accessory. + * This file descriptor can be used with standard Java file operations. + */ + ParcelFileDescriptor openAccessory(in UsbAccessory accessory); + + /* Sets the default package for a USB accessory + * (or clears it if the package name is null) + */ + void setAccessoryPackage(in UsbAccessory accessory, String packageName); + + /* Grants permission for the given UID to access the accessory */ + void grantAccessoryPermission(in UsbAccessory accessory, int uid); + + /* Returns true if the USB manager has default preferences or permissions for the package */ + boolean hasDefaults(String packageName, int uid); + + /* Clears default preferences and permissions for the package */ + oneway void clearDefaults(String packageName, int uid); +} diff --git a/core/java/android/hardware/usb/UsbAccessory.aidl b/core/java/android/hardware/usb/UsbAccessory.aidl new file mode 100644 index 0000000..1c15f1c --- /dev/null +++ b/core/java/android/hardware/usb/UsbAccessory.aidl @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2011, 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. + */ + +package android.hardware.usb; + +parcelable UsbAccessory; diff --git a/core/java/android/hardware/usb/UsbAccessory.java b/core/java/android/hardware/usb/UsbAccessory.java new file mode 100644 index 0000000..a7e953d --- /dev/null +++ b/core/java/android/hardware/usb/UsbAccessory.java @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2011 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. + */ + +package android.hardware.usb; + +import android.os.Bundle; +import android.os.Parcel; +import android.os.Parcelable; +import android.util.Log; + +/** + * A class representing a USB accessory. + * @hide + */ +public class UsbAccessory implements Parcelable { + + private static final String TAG = "UsbAccessory"; + + private final String mManufacturer; + private final String mModel; + private final String mType; + private final String mVersion; + + /** + * UsbAccessory should only be instantiated by UsbService implementation + * @hide + */ + public UsbAccessory(String manufacturer, String model, String type, String version) { + mManufacturer = manufacturer; + mModel = model; + mType = type; + mVersion = version; + } + + /** + * UsbAccessory should only be instantiated by UsbService implementation + * @hide + */ + public UsbAccessory(String[] strings) { + mManufacturer = strings[0]; + mModel = strings[1]; + mType = strings[2]; + mVersion = strings[3]; + } + + /** + * Returns the manufacturer of the accessory. + * + * @return the accessory manufacturer + */ + public String getManufacturer() { + return mManufacturer; + } + + /** + * Returns the model name of the accessory. + * + * @return the accessory model + */ + public String getModel() { + return mModel; + } + + /** + * Returns the type of the accessory. + * + * @return the accessory type + */ + public String getType() { + return mType; + } + + /** + * Returns the version of the accessory. + * + * @return the accessory version + */ + public String getVersion() { + return mVersion; + } + + private static boolean compare(String s1, String s2) { + if (s1 == null) return (s2 == null); + return s1.equals(s2); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof UsbAccessory) { + UsbAccessory accessory = (UsbAccessory)obj; + return (compare(mManufacturer, accessory.getManufacturer()) && + compare(mModel, accessory.getModel()) && + compare(mType, accessory.getType()) && + compare(mVersion, accessory.getVersion())); + } + return false; + } + + @Override + public String toString() { + return "UsbAccessory[mManufacturer=" + mManufacturer + + ", mModel=" + mModel + + ", mType=" + mType + + ", mVersion=" + mVersion + "]"; + } + + public static final Parcelable.Creator<UsbAccessory> CREATOR = + new Parcelable.Creator<UsbAccessory>() { + public UsbAccessory createFromParcel(Parcel in) { + String manufacturer = in.readString(); + String model = in.readString(); + String type = in.readString(); + String version = in.readString(); + return new UsbAccessory(manufacturer, model, type, version); + } + + public UsbAccessory[] newArray(int size) { + return new UsbAccessory[size]; + } + }; + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel parcel, int flags) { + parcel.writeString(mManufacturer); + parcel.writeString(mModel); + parcel.writeString(mType); + parcel.writeString(mVersion); + } +} diff --git a/core/java/android/hardware/usb/UsbManager.java b/core/java/android/hardware/usb/UsbManager.java new file mode 100644 index 0000000..a350274 --- /dev/null +++ b/core/java/android/hardware/usb/UsbManager.java @@ -0,0 +1,239 @@ +/* + * Copyright (C) 2010 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. + */ + + +package android.hardware.usb; + +import android.os.Bundle; +import android.os.ParcelFileDescriptor; +import android.os.RemoteException; +import android.util.Log; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.HashMap; + +/** + * This class allows you to access the state of USB. + * + * <p>You can obtain an instance of this class by calling + * {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}. + * + * {@samplecode + * UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); + * } + * @hide + */ +public class UsbManager { + private static final String TAG = "UsbManager"; + + /** + * Broadcast Action: A sticky broadcast for USB state change events when in device mode. + * + * This is a sticky broadcast for clients that includes USB connected/disconnected state, + * <ul> + * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected. + * <li> {@link #USB_CONFIGURATION} a Bundle containing name/value pairs where the name + * is the name of a USB function and the value is either {@link #USB_FUNCTION_ENABLED} + * or {@link #USB_FUNCTION_DISABLED}. The possible function names include + * {@link #USB_FUNCTION_MASS_STORAGE}, {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS}, + * {@link #USB_FUNCTION_MTP} and {@link #USB_FUNCTION_ACCESSORY}. + * </ul> + */ + public static final String ACTION_USB_STATE = + "android.hardware.usb.action.USB_STATE"; + + /** + * Broadcast Action: A broadcast for USB accessory attached event. + * + * This intent is sent when a USB accessory is attached. + * <ul> + * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory} + * for the attached accessory + * </ul> + */ + public static final String ACTION_USB_ACCESSORY_ATTACHED = + "android.hardware.usb.action.USB_ACCESSORY_ATTACHED"; + + /** + * Broadcast Action: A broadcast for USB accessory detached event. + * + * This intent is sent when a USB accessory is detached. + * <ul> + * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory} + * for the attached accessory that was detached + * </ul> + */ + public static final String ACTION_USB_ACCESSORY_DETACHED = + "android.hardware.usb.action.USB_ACCESSORY_DETACHED"; + + /** + * Boolean extra indicating whether USB is connected or disconnected. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast. + */ + public static final String USB_CONNECTED = "connected"; + + /** + * Integer extra containing currently set USB configuration. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast. + */ + public static final String USB_CONFIGURATION = "configuration"; + + /** + * Name of the USB mass storage USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_MASS_STORAGE = "mass_storage"; + + /** + * Name of the adb USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_ADB = "adb"; + + /** + * Name of the RNDIS ethernet USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_RNDIS = "rndis"; + + /** + * Name of the MTP USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_MTP = "mtp"; + + /** + * Name of the Accessory USB function. + * Used in extras for the {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_ACCESSORY = "accessory"; + + /** + * Value indicating that a USB function is enabled. + * Used in {@link #USB_CONFIGURATION} extras bundle for the + * {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_ENABLED = "enabled"; + + /** + * Value indicating that a USB function is disabled. + * Used in {@link #USB_CONFIGURATION} extras bundle for the + * {@link #ACTION_USB_STATE} broadcast + */ + public static final String USB_FUNCTION_DISABLED = "disabled"; + + /** + * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} and + * {@link #ACTION_USB_ACCESSORY_DETACHED} broadcasts + * containing the UsbAccessory object for the accessory. + */ + public static final String EXTRA_ACCESSORY = "accessory"; + + private IUsbManager mService; + + /** + * {@hide} + */ + public UsbManager(IUsbManager service) { + mService = service; + } + + /** + * Returns a list of currently attached USB accessories. + * (in the current implementation there can be at most one) + * + * @return list of USB accessories, or null if none are attached. + */ + public UsbAccessory[] getAccessoryList() { + try { + UsbAccessory accessory = mService.getCurrentAccessory(); + if (accessory == null) { + return null; + } else { + return new UsbAccessory[] { accessory }; + } + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in getAccessoryList" , e); + return null; + } + } + + /** + * Opens a file descriptor for reading and writing data to the USB accessory. + * + * @param accessory the USB accessory to open + * @return file descriptor, or null if the accessor could not be opened. + */ + public ParcelFileDescriptor openAccessory(UsbAccessory accessory) { + try { + return mService.openAccessory(accessory); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in openAccessory" , e); + return null; + } + } + + 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. + * + * @param function name of the USB function + * @return true if the USB function is supported. + */ + public static boolean isFunctionSupported(String function) { + return getFunctionEnableFile(function).exists(); + } + + /** + * Returns true if the specified USB function is currently enabled. + * + * @param function name of the USB function + * @return true if the USB function is 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; + } + } + + /** + * Enables or disables a USB function. + * + * @hide + */ + public static boolean setFunctionEnabled(String function, boolean enable) { + try { + FileOutputStream stream = new FileOutputStream(getFunctionEnableFile(function)); + stream.write(enable ? '1' : '0'); + stream.close(); + return true; + } catch (IOException e) { + return false; + } + } +} diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java index 1c8b330..4c13460 100644 --- a/core/java/android/util/SparseArray.java +++ b/core/java/android/util/SparseArray.java @@ -90,6 +90,17 @@ public class SparseArray<E> { delete(key); } + /** + * Removes the mapping at the specified index. + * @hide + */ + public void removeAt(int index) { + if (mValues[index] != DELETED) { + mValues[index] = DELETED; + mGarbage = true; + } + } + private void gc() { // Log.e("SparseArray", "gc start with " + mSize); |