Example usage for android.hardware.usb UsbManager EXTRA_PERMISSION_GRANTED

List of usage examples for android.hardware.usb UsbManager EXTRA_PERMISSION_GRANTED

Introduction

In this page you can find the example usage for android.hardware.usb UsbManager EXTRA_PERMISSION_GRANTED.

Prototype

String EXTRA_PERMISSION_GRANTED

To view the source code for android.hardware.usb UsbManager EXTRA_PERMISSION_GRANTED.

Click Source Link

Document

Name of extra added to the android.app.PendingIntent passed into #requestPermission(UsbDevice,PendingIntent) or #requestPermission(UsbAccessory,PendingIntent) containing a boolean value indicating whether the user granted permission or not.

Usage

From source file:com.ekumen.tangobot.application.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

    // Load raw resources
    for (Pair<Integer, String> ip : mResourcesToLoad) {
        mOpenedResources.add(new ParameterLoaderNode.Resource(
                getResources().openRawResource(ip.first.intValue()), ip.second));
    }//  ww w  . j a v a  2s .c o  m

    mSharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    // UI
    initializeUI();

    // USB handling code
    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    mUsbPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    mUsbAttachedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mLog.info("Received USB Intent");
            if (intent.getAction() == ACTION_USB_PERMISSION
                    && intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                onDeviceReady((UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE));
            }
        }
    };
    mUsbDetachedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mLog.info("Received USB disconnection Intent");
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            onDeviceDetached(device);
        }
    };
}

From source file:org.chromium.ChromeUsb.java

private void openDevice(CordovaArgs args, JSONObject params, final CallbackContext callbackContext)
        throws JSONException, UsbError {
    // First recover the device object from Id.
    int devId = params.getInt("device");
    ConnectedDevice dev = null;//  w  w  w.ja v  a  2  s . c om
    int vid = -1, pid = -1;
    {
        HashMap<String, UsbDevice> devices = mUsbManager.getDeviceList();
        UsbDevice usbDev = null;
        for (UsbDevice d : devices.values()) {
            if (d.getDeviceId() == devId) {
                usbDev = d;
                break;
            }
        }
        if (usbDev != null) {
            if (mUsbReceiver == null) {
                mUsbReceiver = new BroadcastReceiver() {
                    public void onReceive(Context context, Intent intent) {
                        String action = intent.getAction();
                        if (ACTION_USB_PERMISSION.equals(action)) {
                            synchronized (this) {
                                UsbDevice device = (UsbDevice) intent
                                        .getParcelableExtra(UsbManager.EXTRA_DEVICE);

                                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                                    if (device != null) {
                                        UsbDeviceConnection usbConn = mUsbManager.openDevice(device);
                                        if (usbConn == null) {
                                            throw new UsbError(
                                                    "UsbManager.openDevice returned null opening " + device);
                                        }
                                        ConnectedDevice dev = new RealDevice(device, usbConn);
                                        int vid = device.getVendorId();
                                        int pid = device.getProductId();

                                        if (dev == null || vid < 0 || pid < 0) {
                                            throw new UsbError("Unknown device ID: " + device);
                                        }
                                        int handle = mNextConnectionId++;
                                        mConnections.put(handle, dev);
                                        JSONObject jsonHandle = new JSONObject();
                                        try {
                                            jsonHandle.put("handle", handle);
                                            jsonHandle.put("vendorId", vid);
                                            jsonHandle.put("productId", pid);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        callbackContext.success(jsonHandle);
                                    }
                                } else {
                                    Log.d(TAG, "permission denied for device " + device);
                                }
                            }
                        }
                    }
                };

                IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
                webView.getContext().registerReceiver(mUsbReceiver, filter);
            }

            mUsbManager.requestPermission(usbDev, mPermissionIntent);
        } else if (devId == FakeDevice.ID) {
            dev = new FakeDevice();
            vid = FakeDevice.VID;
            pid = FakeDevice.PID;

            if (dev == null || vid < 0 || pid < 0) {
                throw new UsbError("Unknown device ID: " + devId);
            }
            int handle = mNextConnectionId++;
            mConnections.put(handle, dev);
            JSONObject jsonHandle = new JSONObject();
            jsonHandle.put("handle", handle);
            jsonHandle.put("vendorId", vid);
            jsonHandle.put("productId", pid);
            callbackContext.success(jsonHandle);
        }
    }
}