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

The following examples show how to use android.hardware.usb.UsbManager#ACTION_USB_DEVICE_DETACHED . 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: UsbActivity.java    From sample-usbenum with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_usb);

    mStatusView = (TextView) findViewById(R.id.text_status);
    mResultView = (TextView) findViewById(R.id.text_result);

    mUsbManager = getSystemService(UsbManager.class);

    // Detach events are sent as a system-wide broadcast
    IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbReceiver, filter);

    handleIntent(getIntent());
}
 
Example 3
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 4
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;
	}
}