Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 564f12eb authored by Mike Lockwood's avatar Mike Lockwood Committed by Android (Google) Code Review
Browse files

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()
parents 37c2e3e5 b966b9d9
Loading
Loading
Loading
Loading
+61 −5
Original line number Diff line number Diff line
@@ -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);
        }
    }
}
+61 −16
Original line number Diff line number Diff line
@@ -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,19 +97,18 @@ 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 {
            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 {
+2 −2
Original line number Diff line number Diff line
@@ -311,9 +311,9 @@ android_mtp_MtpDevice_get_object_info(JNIEnv *env, jobject thiz, jint objectID)
    if (objectInfo->mName)
        env->SetObjectField(info, field_objectInfo_name, env->NewStringUTF(objectInfo->mName));
    if (objectInfo->mDateCreated)
        env->SetLongField(info, field_objectInfo_dateCreated, objectInfo->mDateCreated);
        env->SetLongField(info, field_objectInfo_dateCreated, objectInfo->mDateCreated * 1000LL);
    if (objectInfo->mDateModified)
        env->SetLongField(info, field_objectInfo_dateModified, objectInfo->mDateModified);
        env->SetLongField(info, field_objectInfo_dateModified, objectInfo->mDateModified * 1000LL);
    if (objectInfo->mKeywords)
        env->SetObjectField(info, field_objectInfo_keywords,
            env->NewStringUTF(objectInfo->mKeywords));
+1 −4
Original line number Diff line number Diff line
@@ -423,11 +423,8 @@ public class UsbService extends IUsbManager.Stub {

    /* returns the currently attached USB accessory (device mode) */
    public UsbAccessory getCurrentAccessory() {
        synchronized (mLock) {
            mDeviceManager.checkPermission(mCurrentAccessory);
        return mCurrentAccessory;
    }
    }

    /* opens the currently attached USB accessory (device mode) */
    public ParcelFileDescriptor openAccessory(UsbAccessory accessory) {