Example usage for android.hardware.usb UsbManager ACTION_USB_DEVICE_DETACHED

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

Introduction

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

Prototype

String ACTION_USB_DEVICE_DETACHED

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

Click Source Link

Document

Broadcast Action: A broadcast for USB device detached event.

Usage

From source file:com.dslr.dashboard.PtpService.java

@Override
public void onCreate() {
    Log.d(TAG, "onCreate");
    super.onCreate();

    mGpsHelper = new GpsLocationHelper(this);

    IntentFilter usbDetachedFilter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbDeviceDetached, usbDetachedFilter);

    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbPermissionReceiver, filter);

    // get the shared preferences
    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    // create the ptpdevice
    mPtpDevice = new PtpDevice(this);

    try {//from   w  w w  .j  a v  a2 s.  co m
        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    } catch (Exception e) {
        Log.d(TAG, "UsbManager not available: " + e.getMessage());
    }

}

From source file:com.physicaroid.pocketduino.cordova.PocketDuino.java

/**
 * USB???//from   w w  w  .  jav  a  2  s  . co m
 */
private void listenUsbDevice(CallbackContext callbackContext) {
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    this.usbCallbackContext = callbackContext;
    if (this.mUsbReceiver == null) {
        this.mUsbReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String handlerName = null;
                String action = intent.getAction();
                if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                    // plugin result of USB attached
                    handlerName = HANDLER_PREFIX + "." + "attached";
                }
                if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                    // plugin result of USB detached
                    handlerName = HANDLER_PREFIX + "." + "detached";
                }
                try {
                    String json = "{\"handlerName\":" + handlerName + " }";
                    JSONObject parameter = new JSONObject(json);
                    PluginResult dataResult = new PluginResult(PluginResult.Status.OK, parameter);
                    dataResult.setKeepCallback(true);
                    usbCallbackContext.sendPluginResult(dataResult);
                } catch (JSONException e) {
                    Log.e(POCKETDUINO, "Exception: " + e.getMessage());
                    usbCallbackContext.error(e.toString());
                }
            }
        };
        webView.getContext().registerReceiver(this.mUsbReceiver, intentFilter);
    }
}

From source file:fr.bmartel.android.fadecandy.service.FadecandyService.java

/**
 * Initialize broadcast receiver to receive USB ATTACHED/DETACHED events.
 *///from w w  w  . j ava  2 s .c  o  m
private void initBroadcastReceiver() {

    IntentFilter filter = new IntentFilter();
    filter.addAction(ACTION_USB_PERMISSION);
    filter.addAction(UsbEventReceiverActivity.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    filter.addAction(FadecandyService.ACTION_EXIT);
    registerReceiver(receiver, filter);
}

From source file:com.grupohqh.carservices.operator.ManipulateCarActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manipulate_car);

    if (getIntent().getExtras().containsKey("userId"))
        userId = getIntent().getExtras().getInt("userId");
    if (getIntent().getExtras().containsKey("carOwnerId"))
        carOwnerId = getIntent().getExtras().getInt("carOwnerId");

    CARBRAND_URL = getString(R.string.base_url) + getString(R.string.carbrands_url);
    CARLINE_URL = getString(R.string.base_url) + getString(R.string.carmodels_url);
    SAVECAR_URL = getString(R.string.base_url) + "savecar/";
    SAVEPHOTO_URL = getString(R.string.base_url) + "savephoto/";
    hasCamera = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);

    etTagNumber = (EditText) findViewById(R.id.etTagNumber);
    etUsernameOwner = (EditText) findViewById(R.id.etUsernameOwner);
    etCarBrand = (EditText) findViewById(R.id.etCarBrand);
    etCarModel = (EditText) findViewById(R.id.etCarModel);
    etCarYear = (EditText) findViewById(R.id.etCarYear);
    etColor = (EditText) findViewById(R.id.etCarColor);
    etSerialNumber = (EditText) findViewById(R.id.etSerialNumber);
    etLicensePlate = (EditText) findViewById(R.id.etLicensePlate);
    etKM = (EditText) findViewById(R.id.etKm);
    imgCar = (ImageView) findViewById(R.id.imgCar);
    spCarBrand = (Spinner) findViewById(R.id.spCarBrand);
    spCarModel = (Spinner) findViewById(R.id.spCarModel);
    btnSaveCar = (Button) findViewById(R.id.btnSaveCar);
    btnTakePicture = (Button) findViewById(R.id.btnTakePicture);
    bar = (ProgressBar) findViewById(R.id.progressBar);
    viewUsername = findViewById(R.id.viewUsername);

    if (savedInstanceState != null) {
        bm = savedInstanceState.getParcelable("bm");
        if (bm != null)
            imgCar.setImageBitmap(bm);//w  ww. j  av a 2s .  c  om
    }

    bar.setVisibility(View.GONE);
    etCarBrand.setVisibility(View.GONE);
    etCarModel.setVisibility(View.GONE);
    viewUsername.setVisibility(carOwnerId == 0 ? View.VISIBLE : View.GONE);

    btnTakePicture.setEnabled(hasCamera);
    btnTakePicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(ManipulateCarActivity.this)));
            startActivityForResult(intent, CAPTURE_IMAGE);
        }
    });

    btnSaveCar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (validate())
                new SaveCarAsyncTask().execute(SAVECAR_URL);
        }
    });

    spCarBrand.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0)
                ((TextView) parent.getChildAt(position)).setTextColor(Color.GRAY);
            if (mapBrands.get(brands.get(position)) == otherId) {
                etCarBrand.setVisibility(View.VISIBLE);
                etCarModel.setVisibility(View.VISIBLE);
                spCarModel.setVisibility(View.GONE);
                etCarBrand.requestFocus();
            } else {
                etCarBrand.setVisibility(View.GONE);
                etCarModel.setVisibility(View.GONE);
                spCarModel.setVisibility(View.VISIBLE);
                new LoadCarModelsAsyncTask().execute(CARLINE_URL + mapBrands.get(brands.get(position)) + "/");
                reload = true;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    spCarModel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0)
                ((TextView) parent.getChildAt(position)).setTextColor(Color.GRAY);
            if (mapModels.get(models.get(position)) == otherId) {
                etCarModel.setVisibility(View.VISIBLE);
                etCarModel.requestFocus();
            } else {
                etCarModel.setVisibility(View.GONE);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    if (useMiniMe) {
        manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        usbCommunication = UsbCommunication.newInstance();

        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); // will intercept by system
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        filter.addAction(ACTION_USB_PERMISSION);
        registerReceiver(usbReceiver, filter);

        etEPC = (EditText) findViewById(R.id.etEPC);
        btnReadTAG = (Button) findViewById(R.id.btnReadTAG);
        txtStatus = (TextView) findViewById(R.id.txtStatus);
        btnReadTAG.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etEPC.setText("");
                if (txtStatus.getText().toString().equals("conectado")) {
                    readTag();
                } else {
                    Toast.makeText(getBaseContext(), "dispositivo " + txtStatus.getText(), Toast.LENGTH_LONG)
                            .show();
                }
            }
        });
    }

    etKM.addTextChangedListener(new NumberTextWatcher(etKM));
}

From source file:com.nps.micro.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    usbDisconnectedBroadcastReceiver = new UsbDisconnectedBroadcastReceiver(this);
    registerReceiver(usbDisconnectedBroadcastReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));

    initUsbService();/*from   www. j a v  a 2s.co  m*/
}

From source file:jp.ksksue.app.terminal.AndroidUSBSerialMonitorLite.java

/** Called when the activity is first created. */
@Override//w w  w .  jav  a  2  s  . c  om
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* FIXME : How to check that there is a title bar menu or not.
            // Should not set a Window.FEATURE_NO_TITLE on Honeycomb because a user cannot see menu button.
            if(isICSorHigher) {
    if(!getWindow().hasFeature(Window.FEATURE_ACTION_BAR)) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }
            }
    */
    setContentView(R.layout.main);

    mSvText = (ScrollView) findViewById(R.id.svText);
    mTvSerial = (TextView) findViewById(R.id.tvSerial);
    btWrite = (Button) findViewById(R.id.btWrite);
    btWrite.setEnabled(false);
    etWrite = (EditText) findViewById(R.id.etWrite);
    etWrite.setEnabled(false);
    etWrite.setHint("CR : \\r, LF : \\n, bin : \\u0000");

    if (SHOW_DEBUG) {
        Log.d(TAG, "New FTDriver");
    }

    // get service
    mSerial = new Physicaloid(this);

    if (SHOW_DEBUG) {
        Log.d(TAG, "New instance : " + mSerial);
    }
    // listen for new devices
    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbReceiver, filter);

    if (SHOW_DEBUG) {
        Log.d(TAG, "FTDriver beginning");
    }

    openUsbSerial();

    etWrite.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_ENTER) {
                writeDataToSerial();
                return true;
            }
            return false;
        }
    });
    // ---------------------------------------------------------------------------------------
    // Write Button
    // ---------------------------------------------------------------------------------------
    if (!USE_WRITE_BUTTON_FOR_DEBUG) {
        btWrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                writeDataToSerial();
            }
        });
    } else {
        // Write test button for debug
        btWrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strWrite = "";
                for (int i = 0; i < 3000; ++i) {
                    strWrite = strWrite + " " + Integer.toString(i);
                }
                if (SHOW_DEBUG) {
                    Log.d(TAG, "FTDriver Write(" + strWrite.length() + ") : " + strWrite);
                }
                mSerial.write(strWrite.getBytes(), strWrite.length());
            }
        });
    } // end of if(SHOW_WRITE_TEST_BUTTON)

    btnLoad = (Button) findViewById(R.id.btnLoad);
    btnLoad.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            SimpleFileDialog simpleFileDialog = new SimpleFileDialog(AndroidUSBSerialMonitorLite.this,
                    "FileOpen", new SimpleFileDialog.SimpleFileDialogListener() {
                        @Override
                        public void onChosenDir(String chosenDir) {
                            File file = null;
                            FileInputStream fileInputStream = null;
                            byte[] data;

                            try {
                                file = new File(chosenDir);
                                if (file.isDirectory()) {
                                    throw new IOException("not file");
                                }

                                fileInputStream = new FileInputStream(file);
                                data = new byte[(int) file.length()];
                                fileInputStream.read(data);

                                String dataText = new String(data, "UTF-8");
                                parseJSON(dataText);

                            } catch (IOException e) {
                                Toast.makeText(AndroidUSBSerialMonitorLite.this, e.getMessage(),
                                        Toast.LENGTH_SHORT).show();
                            } finally {
                                if (fileInputStream != null) {
                                    try {
                                        fileInputStream.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    });
            simpleFileDialog.chooseFile_or_Dir();
        }
    });

    btnPlay = (Button) findViewById(R.id.btnPlay);
    btnPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startIrBeaconPulse();
        }
    });

    btnStop = (Button) findViewById(R.id.btnStop);
    btnStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            stopIrBeaconPulse();
        }
    });

    btnPlay.setEnabled(false);
    btnStop.setEnabled(false);
}

From source file:com.rp.podemu.MainActivity.java

@Override
public void onResume() {
    super.onResume();

    try {//w w w .j av  a  2  s . c o m
        loadPreferences();
        start_service(null);

        iF = new PodEmuIntentFilter();
        iF.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        iF.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        //            iF.addDataScheme("content");
        //            iF.addDataAuthority(ctrlAppProcessName, null);

        registerReceiver(mReceiver, iF);

        PodEmuLog.debug("onResume done");
    } catch (Exception e) {
        PodEmuLog.printStackTrace(e);
        throw e;
    }

}

From source file:com.rp.podemu.PodEmuService.java

@Override
public void onCreate() {
    super.onCreate();

    try {//from   www.j av  a  2  s  .  c om
        serialInterfaceBuilder = new SerialInterfaceBuilder();

        //SharedPreferences sharedPref = this.getSharedPreferences("PODEMU_PREFS", Context.MODE_PRIVATE);
        //String ctrlAppProcessName = sharedPref.getString("ControlledAppProcessName", "unknown app");
        iF = new PodEmuIntentFilter();
        iF.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        iF.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        registerReceiver(mReceiver, iF);
        reloadBaudRate();
    } catch (Exception e) {
        PodEmuLog.printStackTrace(e);
        throw e;
    }

}

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

@Override
public void onResume() {
    super.onResume();
    registerReceiver(mUsbAttachedReceiver, new IntentFilter(ACTION_USB_PERMISSION));
    registerReceiver(mUsbDetachedReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
}

From source file:com.github.mjdev.libaums.usbfileman.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    serviceIntent = new Intent(this, UsbFileHttpServerService.class);

    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.listview);

    listView.setOnItemClickListener(this);
    registerForContextMenu(listView);//from ww w .  j av a 2  s.  c o  m

    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(usbReceiver, filter);
    discoverDevice();
}