summaryrefslogtreecommitdiffstats
path: root/libs
diff options
context:
space:
mode:
authorMike Lockwood <lockwood@android.com>2011-03-09 15:47:10 -0800
committerAndroid (Google) Code Review <android-gerrit@google.com>2011-03-09 15:47:10 -0800
commit564f12ebe409bead467eee04dfd719e97367e520 (patch)
treeee566495df013526c9163e0e8ef6fe9fd6e61bfc /libs
parent37c2e3e55641e91dd0ec3dbd296bc6bc7c7641cf (diff)
parentb966b9d9e882835691e5adda292d89dd704df71c (diff)
downloadframeworks_base-564f12ebe409bead467eee04dfd719e97367e520.zip
frameworks_base-564f12ebe409bead467eee04dfd719e97367e520.tar.gz
frameworks_base-564f12ebe409bead467eee04dfd719e97367e520.tar.bz2
Merge changes Ifd78cca3,Ieadb86fe,If5b44ebd into honeycomb-mr1
* changes: MTP: Convert date created and modified values from seconds to milliseconds Update USB accessory compatibility library to support new requestPermission API UsbService: Don't require permissions for UsbManager.getCurrentAccessory()
Diffstat (limited to 'libs')
-rw-r--r--libs/usb/src/com/android/future/usb/UsbManager.java66
-rw-r--r--libs/usb/tests/AccessoryChat/src/com/android/accessorychat/AccessoryChat.java77
2 files changed, 122 insertions, 21 deletions
diff --git a/libs/usb/src/com/android/future/usb/UsbManager.java b/libs/usb/src/com/android/future/usb/UsbManager.java
index f74b291..33eb3ee 100644
--- a/libs/usb/src/com/android/future/usb/UsbManager.java
+++ b/libs/usb/src/com/android/future/usb/UsbManager.java
@@ -17,6 +17,7 @@
package com.android.future.usb;
+import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.IUsbManager;
@@ -55,28 +56,39 @@ public class UsbManager {
public static final String ACTION_USB_ACCESSORY_DETACHED =
"android.hardware.usb.action.USB_ACCESSORY_DETACHED";
+ /**
+ * Name of extra added to the {@link android.app.PendingIntent}
+ * passed into {#requestPermission} or {#requestPermission}
+ * containing a boolean value indicating whether the user granted permission or not.
+ */
+ public static final String EXTRA_PERMISSION_GRANTED = "permission";
+
+ private final Context mContext;
private final IUsbManager mService;
- private UsbManager(IUsbManager service) {
+ private UsbManager(Context context, IUsbManager service) {
+ mContext = context;
mService = service;
}
/**
* Returns a new instance of this class.
*
+ * @param context the caller's {@link android.content.Context}
* @return UsbManager instance.
*/
- public static UsbManager getInstance() {
+ public static UsbManager getInstance(Context context) {
IBinder b = ServiceManager.getService(Context.USB_SERVICE);
- return new UsbManager(IUsbManager.Stub.asInterface(b));
+ return new UsbManager(context, IUsbManager.Stub.asInterface(b));
}
/**
* Returns the {@link com.google.android.usb.UsbAccessory} for
* a {@link #ACTION_USB_ACCESSORY_ATTACHED} or {@link #ACTION_USB_ACCESSORY_ATTACHED}
- * broadcast Intent
+ * broadcast Intent. This can also be used to retrieve the accessory from the result
+ * of a call to {#requestPermission}.
*
- * @return UsbAccessory for the broadcast.
+ * @return UsbAccessory for the intent.
*/
public static UsbAccessory getAccessory(Intent intent) {
android.hardware.usb.UsbAccessory accessory =
@@ -124,4 +136,48 @@ public class UsbManager {
return null;
}
}
+
+ /**
+ * Returns true if the caller has permission to access the accessory.
+ * Permission might have been granted temporarily via
+ * {@link #requestPermission(android.hardware.usb.UsbAccessory} or
+ * by the user choosing the caller as the default application for the accessory.
+ *
+ * @param accessory to check permissions for
+ * @return true if caller has permission
+ */
+ public boolean hasPermission(UsbAccessory accessory) {
+ try {
+ return mService.hasAccessoryPermission(new android.hardware.usb.UsbAccessory(
+ accessory.getManufacturer(),accessory.getModel(),
+ accessory.getType(), accessory.getVersion()));
+ } catch (RemoteException e) {
+ Log.e(TAG, "RemoteException in hasPermission", e);
+ return false;
+ }
+ }
+
+ /**
+ * Requests temporary permission for the given package to access the accessory.
+ * This may result in a system dialog being displayed to the user
+ * if permission had not already been granted.
+ * Success or failure is returned via the {@link android.app.PendingIntent} pi.
+ * The boolean extra {@link #EXTRA_PERMISSION_GRANTED} will be attached to the
+ * PendingIntent to indicate success or failure.
+ * If successful, this grants the caller permission to access the device only
+ * until the device is disconnected.
+ *
+ * @param accessory to request permissions for
+ * @param pi PendingIntent for returning result
+ */
+ public void requestPermission(UsbAccessory accessory, PendingIntent pi) {
+ try {
+ mService.requestAccessoryPermission(new android.hardware.usb.UsbAccessory(
+ accessory.getManufacturer(),accessory.getModel(),
+ accessory.getType(), accessory.getVersion()),
+ mContext.getPackageName(), pi);
+ } catch (RemoteException e) {
+ Log.e(TAG, "RemoteException in requestPermission", e);
+ }
+ }
}
diff --git a/libs/usb/tests/AccessoryChat/src/com/android/accessorychat/AccessoryChat.java b/libs/usb/tests/AccessoryChat/src/com/android/accessorychat/AccessoryChat.java
index 5cf02c7..f9a5bf4 100644
--- a/libs/usb/tests/AccessoryChat/src/com/android/accessorychat/AccessoryChat.java
+++ b/libs/usb/tests/AccessoryChat/src/com/android/accessorychat/AccessoryChat.java
@@ -17,9 +17,11 @@
package com.android.accessorychat;
import android.app.Activity;
+import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
+import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
@@ -42,18 +44,47 @@ import java.io.IOException;
public class AccessoryChat extends Activity implements Runnable, TextView.OnEditorActionListener {
private static final String TAG = "AccessoryChat";
- TextView mLog;
- EditText mEditText;
- ParcelFileDescriptor mFileDescriptor;
- FileInputStream mInputStream;
- FileOutputStream mOutputStream;
+
+ private static final String ACTION_USB_PERMISSION =
+ "com.android.accessorychat.action.USB_PERMISSION";
+
+ private TextView mLog;
+ private EditText mEditText;
+ private ParcelFileDescriptor mFileDescriptor;
+ private FileInputStream mInputStream;
+ private FileOutputStream mOutputStream;
+ private UsbManager mUsbManager;
+ private PendingIntent mPermissionIntent;
+ private boolean mPermissionRequestPending;
private static final int MESSAGE_LOG = 1;
+ private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (ACTION_USB_PERMISSION.equals(intent.getAction())) {
+ synchronized (this) {
+ UsbAccessory accessory = UsbManager.getAccessory(intent);
+ if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
+ openAccessory(accessory);
+ } else {
+ Log.d(TAG, "permission denied for accessory " + accessory);
+ }
+ mPermissionRequestPending = false;
+ }
+ }
+ }
+ };
+
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ mUsbManager = UsbManager.getInstance(this);
+ mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
+ IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
+ registerReceiver(mUsbReceiver, filter);
+
setContentView(R.layout.accessory_chat);
mLog = (TextView)findViewById(R.id.log);
mEditText = (EditText)findViewById(R.id.message);
@@ -66,21 +97,20 @@ public class AccessoryChat extends Activity implements Runnable, TextView.OnEdit
Intent intent = getIntent();
Log.d(TAG, "intent: " + intent);
- UsbManager manager = UsbManager.getInstance();
- UsbAccessory[] accessories = manager.getAccessoryList();
+ UsbAccessory[] accessories = mUsbManager.getAccessoryList();
UsbAccessory accessory = (accessories == null ? null : accessories[0]);
if (accessory != null) {
- mFileDescriptor = manager.openAccessory(accessory);
- if (mFileDescriptor != null) {
- FileDescriptor fd = mFileDescriptor.getFileDescriptor();
- mInputStream = new FileInputStream(fd);
- mOutputStream = new FileOutputStream(fd);
- Thread thread = new Thread(null, this, "AccessoryChat");
- thread.start();
+ if (mUsbManager.hasPermission(accessory)) {
+ openAccessory(accessory);
} else {
- Log.d(TAG, "openAccessory fail");
+ synchronized (mUsbReceiver) {
+ if (!mPermissionRequestPending) {
+ mUsbManager.requestPermission(accessory, mPermissionIntent);
+ mPermissionRequestPending = true;
+ }
+ }
}
- } else {
+ } else {
Log.d(TAG, "mAccessory is null");
}
}
@@ -100,9 +130,24 @@ public class AccessoryChat extends Activity implements Runnable, TextView.OnEdit
@Override
public void onDestroy() {
+ unregisterReceiver(mUsbReceiver);
super.onDestroy();
}
+ private void openAccessory(UsbAccessory accessory) {
+ mFileDescriptor = mUsbManager.openAccessory(accessory);
+ if (mFileDescriptor != null) {
+ FileDescriptor fd = mFileDescriptor.getFileDescriptor();
+ mInputStream = new FileInputStream(fd);
+ mOutputStream = new FileOutputStream(fd);
+ Thread thread = new Thread(null, this, "AccessoryChat");
+ thread.start();
+ Log.d(TAG, "openAccessory succeeded");
+ } else {
+ Log.d(TAG, "openAccessory fail");
+ }
+ }
+
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE && mOutputStream != null) {
try {