Java Code Examples for android.hardware.usb.UsbManager#ACTION_USB_DEVICE_ATTACHED

The following examples show how to use android.hardware.usb.UsbManager#ACTION_USB_DEVICE_ATTACHED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NsBroadcastReceiver.java    From ns-usbloader-mobile with GNU General Public License v3.0 6 votes vote down vote up
@Override
public synchronized void onReceive(Context context, Intent intent) {
    if (intent.getAction() == null)
        return;
    switch (intent.getAction()) {
        case UsbManager.ACTION_USB_DEVICE_ATTACHED: {
            showNotification(context, intent.getParcelableExtra(UsbManager.EXTRA_DEVICE));
            break;
        }
        case UsbManager.ACTION_USB_DEVICE_DETACHED: {
            hideNotification(context);
            break;
        }
        case NsConstants.SERVICE_TRANSFER_TASK_FINISHED_INTENT:
            String issues = intent.getStringExtra("ISSUES");
            if (issues != null) {
                Toast.makeText(context, context.getString(R.string.transfers_service_stopped) + " " + issues, Toast.LENGTH_LONG).show();
                break;
            }
            Toast.makeText(context, context.getString(R.string.transfers_service_stopped), Toast.LENGTH_SHORT).show();
            break;
    }
}
 
Example 2
Source File: UsbDeviceManager.java    From yubikit-android with Apache License 2.0 6 votes vote down vote up
/**
 * Registers receiver on usb connection event
 * @param usbConfiguration contains information if device manager also registers receiver on permissions grant from user
 */
public void enable(final UsbConfiguration usbConfiguration) {
    disable();
    configuration = usbConfiguration;
    isPermissionRequired = usbConfiguration.isHandlePermissions();

    receiver = new UsbBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    context.registerReceiver(receiver, intentFilter);

    List<UsbDevice> devices = findDevices();
    for (UsbDevice device : devices) {
        checkPermissions(device, isPermissionRequired);
    }
}
 
Example 3
Source File: SetupWizardUsbDeviceListFragment.java    From talkback with Apache License 2.0 6 votes vote down vote up
@Override
public void onStart() {
  super.onStart();
  HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
  ListView listView = getRootView().findViewById(R.id.connected_usb_devices_list);
  listView.setAdapter(usbArrayAdapter);
  for (UsbDevice device : deviceList.values()) {
    usbArrayAdapter.add(getDisplayableUsbDeviceName(device));
  }
  usbArrayAdapter.notifyDataSetChanged();
  if (usbArrayAdapter.getCount() == 0) {
    updateScreenBasedOnIfUsbDeviceIsConnected(false);
  }

  IntentFilter intent = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
  intent.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
  getActivity().registerReceiver(broadcastReceiver, intent);
}
 
Example 4
Source File: MainActivity.java    From ns-usbloader-mobile with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction() == null)
        return;
    switch (intent.getAction()) {
        case UsbManager.ACTION_USB_DEVICE_ATTACHED:
            usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
            if (usbManager == null)
                break;  // ???
            if (usbManager.hasPermission(usbDevice))
                isUsbDeviceAccessible = true;
            else {
                isUsbDeviceAccessible = false;
                usbManager.requestPermission(usbDevice, PendingIntent.getBroadcast(context, 0, new Intent(NsConstants.REQUEST_NS_ACCESS_INTENT), 0));
            }
            break;
        case NsConstants.REQUEST_NS_ACCESS_INTENT:
            isUsbDeviceAccessible = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
            break;
        case UsbManager.ACTION_USB_DEVICE_DETACHED:
            usbDevice = null;
            isUsbDeviceAccessible = false;
            stopService(new Intent(context, CommunicationsService.class));
            break;
        case NsConstants.SERVICE_TRANSFER_TASK_FINISHED_INTENT:
            ArrayList<NSPElement> nspElements = intent.getParcelableArrayListExtra(NsConstants.SERVICE_CONTENT_NSP_LIST);
            if (nspElements == null)
                break;
            for (int i=0; i < mDataset.size(); i++){
                for (NSPElement receivedNSPe : nspElements)
                    if (receivedNSPe.getFilename().equals(mDataset.get(i).getFilename()))
                        mDataset.get(i).setStatus(receivedNSPe.getStatus());
            }
            mAdapter.notifyDataSetChanged();
            blockUI(false);
            break;
    }
}
 
Example 5
Source File: MediaMountedReceiver.java    From edslite with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) 
{
	Logger.debug("MediaMountedReceiver");
	LocationsManagerBase lm = _lm;
	if(lm == null)
		return;
	String mountPath = intent.getDataString();
       ExternalStorageLocation loc = mountPath != null ? new ExternalStorageLocation(context, "ext storage", mountPath, null) : null;
	try
	{
		Thread.sleep(3000);
	}
	catch (InterruptedException e)
	{
		e.printStackTrace();
	}
	switch (intent.getAction())
	{
		case UsbManager.ACTION_USB_DEVICE_ATTACHED:
			Logger.debug("ACTION_USB_DEVICE_ATTACHED");
			lm.updateDeviceLocations();
			LocationsManager.broadcastLocationAdded(context, loc);
			break;
		case Intent.ACTION_MEDIA_MOUNTED:
			Logger.debug("ACTION_MEDIA_MOUNTED");
			lm.updateDeviceLocations();
			LocationsManager.broadcastLocationAdded(context, loc);
			break;
		case Intent.ACTION_MEDIA_UNMOUNTED:
			Logger.debug("ACTION_MEDIA_UNMOUNTED");
			lm.updateDeviceLocations();
			LocationsManager.broadcastLocationRemoved(context, loc);
			break;
		case Intent.ACTION_MEDIA_REMOVED:
			Logger.debug("ACTION_MEDIA_REMOVED");
			lm.updateDeviceLocations();
			LocationsManager.broadcastLocationRemoved(context, loc);
			break;
		case UsbManager.ACTION_USB_DEVICE_DETACHED:
			Logger.debug("ACTION_USB_DEVICE_DETACHED");
			lm.updateDeviceLocations();
			LocationsManager.broadcastLocationRemoved(context, loc);
			break;
	}
}
 
Example 6
Source File: MainActivity.java    From astrobee_android with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    final UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    if (device == null)
        return;

    switch (action) {
    case ACTION_USB_PERMISSION:
        boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
        if (!granted) {
            Log.e(TAG, "permission denied for device: " + device);
            setState(STATE_ERROR, R.string.error_no_permission);
            return;
        }

        performUsbPermissionCallback(device);
        break;

    case UsbManager.ACTION_USB_DEVICE_ATTACHED:
        if (!isRoyaleDevice(device)) return;

        if (mConn != null) {
            Log.i(TAG, "Another picoflexx attached");
            return;
        }

        if (mUsbManager.hasPermission(device)) {
            performUsbPermissionCallback(device);
            return;
        }

        mUsbManager.requestPermission(device, mUsbPi);
        break;

    case UsbManager.ACTION_USB_ACCESSORY_DETACHED:
        if (!isRoyaleDevice(device))
            return;

        Log.i(TAG, "did the device disappear beneath us?");
        break;
    }
}