Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- USBInfoAPI.class
- public class USBInfoAPI extends AppCompatActivity
- {
- TextView textInfo;
- TextView textInfoInterface;
- TextView textEndPoint;
- Spinner spInterface;
- ArrayList<String> listInterface;
- ArrayList<UsbInterface> listUsbInterface;
- ArrayAdapter<String> adapterInterface;
- Spinner spEndPoint;
- ArrayList<String> listEndPoint;
- ArrayList<UsbEndpoint> listUsbEndpoint;
- ArrayAdapter<String> adapterEndpoint;
- UsbDevice deviceFound = null;
- private static final String ACTION_USB_PERMISSION =
- "com.android.example.USB_PERMISSION";
- PendingIntent mPermissionIntent;
- UsbInterface usbInterface;
- UsbDeviceConnection usbDeviceConnection;
- private int SETTINGS_ACTION = 1;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
- boolean keep_screen = sharedPrefs.getBoolean("Keep screen on",false);
- String theme = sharedPrefs.getString("Choose Theme","Light");
- if(keep_screen)
- {
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- }
- if(theme != null && theme.equals("Light"))
- {
- setTheme(R.style.AppTheme);
- //setTheme(R.style.Theme_light);
- //Toast.makeText(AutoOTG.this,"Light theme",Toast.LENGTH_SHORT).show();
- }
- else
- {
- setTheme(R.style.AppTheme_AppBarOverlay);
- //Toast.makeText(AutoOTG.this,"Dark theme",Toast.LENGTH_SHORT).show();
- }
- super.onCreate(savedInstanceState);
- setContentView(R.layout.usbinfoapi);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- getSupportActionBar().setTitle("USB Info");
- if(getSupportActionBar() != null)
- {
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- getSupportActionBar().setDisplayShowHomeEnabled(true);
- }
- spInterface = (Spinner)findViewById(R.id.spinnerinterface);
- spEndPoint = (Spinner)findViewById(R.id.spinnerendpoint);
- textInfo = (TextView) findViewById(R.id.info);
- textInfoInterface = (TextView)findViewById(R.id.infointerface);
- textEndPoint = (TextView)findViewById(R.id.infoendpoint);
- //register the broadcast receiver
- mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
- IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
- registerReceiver(mUsbReceiver, filter);
- registerReceiver(mUsbDeviceReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));
- registerReceiver(mUsbDeviceReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
- connectUsb();
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem menuItem)
- {
- if(menuItem.getItemId() == android.R.id.home)
- finish();
- return super.onOptionsItemSelected(menuItem);
- }
- @Override
- protected void onDestroy() {
- releaseUsb();
- unregisterReceiver(mUsbReceiver);
- unregisterReceiver(mUsbDeviceReceiver);
- super.onDestroy();
- }
- private void connectUsb(){
- Toast.makeText(USBInfoAPI.this,
- "connectUsb()",
- Toast.LENGTH_LONG).show();
- //textStatus.setText("connectUsb()");
- checkDeviceInfo();
- if(deviceFound != null){
- doRawDescriptors();
- }
- }
- private void releaseUsb(){
- Toast.makeText(USBInfoAPI.this,
- "releaseUsb()",
- Toast.LENGTH_LONG).show();
- //textStatus.setText("releaseUsb()");
- if(usbDeviceConnection != null){
- if(usbInterface != null){
- usbDeviceConnection.releaseInterface(usbInterface);
- usbInterface = null;
- }
- usbDeviceConnection.close();
- usbDeviceConnection = null;
- }
- deviceFound = null;
- }
- private void doRawDescriptors(){
- UsbDevice deviceToRead = deviceFound;
- UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
- Boolean permitToRead = manager.hasPermission(deviceToRead);
- if(permitToRead){
- }else{
- manager.requestPermission(deviceToRead, mPermissionIntent);
- Toast.makeText(USBInfoAPI.this,
- "Permission: " + permitToRead,
- Toast.LENGTH_LONG).show();
- //textStatus.setText("Permission: " + permitToRead);
- }
- }
- private final BroadcastReceiver mUsbReceiver =
- new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (ACTION_USB_PERMISSION.equals(action)) {
- Toast.makeText(USBInfoAPI.this,
- "ACTION_USB_PERMISSION",
- Toast.LENGTH_LONG).show();
- //textStatus.setText("ACTION_USB_PERMISSION");
- synchronized (this) {
- UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
- if(device != null){
- // doReadRawDescriptors(device);
- }
- }
- else {
- Toast.makeText(USBInfoAPI.this,
- "permission denied for device " + device,
- Toast.LENGTH_LONG).show();
- //textStatus.setText("permission denied for device " + device);
- }
- }
- }
- }
- };
- private final BroadcastReceiver mUsbDeviceReceiver =
- new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
- deviceFound = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- Toast.makeText(USBInfoAPI.this,
- "ACTION_USB_DEVICE_ATTACHED: \n" +
- deviceFound.toString(),
- Toast.LENGTH_LONG).show();
- /*textStatus.setText("ACTION_USB_DEVICE_ATTACHED: \n" +
- deviceFound.toString());*/
- checkDeviceInfoSkipDeviceSearching();
- doRawDescriptors();
- }else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
- UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- Toast.makeText(USBInfoAPI.this,
- "ACTION_USB_DEVICE_DETACHED: \n" +
- device.toString(),
- Toast.LENGTH_LONG).show();
- /*textStatus.setText("ACTION_USB_DEVICE_DETACHED: \n" +
- device.toString());*/
- if(device!=null){
- if(device == deviceFound){
- releaseUsb();
- }
- }
- textInfo.setText("");
- textInfoInterface.setText("");
- textEndPoint.setText("");
- listInterface.clear();
- listUsbInterface.clear();
- adapterInterface.notifyDataSetChanged();
- listEndPoint.clear();
- listUsbEndpoint.clear();
- adapterEndpoint.notifyDataSetChanged();
- }
- }
- };
- private void checkDeviceInfo() {
- deviceFound = null;
- UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
- HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
- Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
- while (deviceIterator.hasNext()) {
- UsbDevice device = deviceIterator.next();
- //if(device.getVendorId()==targetVendorID){
- // if(device.getProductId()==targetProductID){
- deviceFound = device;
- // }
- // }
- }
- textInfo.setText("");
- textInfoInterface.setText("");
- textEndPoint.setText("");
- if(deviceFound==null){
- Toast.makeText(USBInfoAPI.this,
- "device not found",
- Toast.LENGTH_LONG).show();
- //textStatus.setText("device not found");
- }else{
- String i = deviceFound.toString() + "\n" +
- "DevicePath: " + deviceFound.getDeviceName() + "\n" +
- "DeviceClass: " + deviceFound.getDeviceClass() + " - "
- + translateDeviceClass(deviceFound.getDeviceClass()) + "\n" +
- "DeviceSubClass: " + deviceFound.getDeviceSubclass() + "\n" +
- "DeviceID: " + deviceFound.getDeviceId() + "\n" +
- "VendorID: " + deviceFound.getVendorId() + "\n" +
- "Vendor Name" + deviceFound.getDeviceName() + "\n" +
- "ProductID: " + deviceFound.getProductId() + "\n" +
- "Product Name" + deviceFound.getManufacturerName() + "\n" +
- "InterfaceCount: " + deviceFound.getInterfaceCount();
- textInfo.setText(i);
- checkUsbDevicve(deviceFound);
- }
- }
- private void checkDeviceInfoSkipDeviceSearching() {
- //called when ACTION_USB_DEVICE_ATTACHED,
- //device already found, skip device searching
- textInfo.setText("");
- textInfoInterface.setText("");
- textEndPoint.setText("");
- String i = deviceFound.toString() + "\n" +
- "DevicePath: " + deviceFound.getDeviceName() + "\n" +
- "DeviceClass: " + deviceFound.getDeviceClass() + " - "
- + translateDeviceClass(deviceFound.getDeviceClass()) + "\n" +
- "DeviceSubClass: " + deviceFound.getDeviceSubclass() + "\n" +
- "DeviceID: " + deviceFound.getDeviceId() + "\n" +
- "VendorID: " + deviceFound.getVendorId() + "\n" +
- "Vendor Name" + deviceFound.getDeviceName() + "\n" +
- "ProductID: " + deviceFound.getProductId() + "\n" +
- "Product Name" + deviceFound.getManufacturerName() + "\n" +
- "InterfaceCount: " + deviceFound.getInterfaceCount();
- textInfo.setText(i);
- checkUsbDevicve(deviceFound);
- }
- OnItemSelectedListener deviceOnItemSelectedListener =
- new OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> parent,
- View view, int position, long id) {
- UsbDevice device = deviceFound;
- String i = device.toString() + "\n" +
- "DevicePath: " + deviceFound.getDeviceName() + "\n" +
- "DeviceClass: " + deviceFound.getDeviceClass() + " - "
- + translateDeviceClass(deviceFound.getDeviceClass()) + "\n" +
- "DeviceSubClass: " + deviceFound.getDeviceSubclass() + "\n" +
- "DeviceID: " + deviceFound.getDeviceId() + "\n" +
- "VendorID: " + deviceFound.getVendorId() + "\n" +
- "Vendor Name" + deviceFound.getDeviceName() + "\n" +
- "ProductID: " + deviceFound.getProductId() + "\n" +
- "Product Name" + deviceFound.getManufacturerName() + "\n" +
- "InterfaceCount: " + deviceFound.getInterfaceCount();
- textInfo.setText(i);
- checkUsbDevicve(device);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {}
- };
- private void checkUsbDevicve(UsbDevice d) {
- listInterface = new ArrayList<String>();
- listUsbInterface = new ArrayList<UsbInterface>();
- for(int i=0; i<d.getInterfaceCount(); i++){
- UsbInterface usbif = d.getInterface(i);
- listInterface.add(usbif.toString());
- listUsbInterface.add(usbif);
- }
- adapterInterface = new ArrayAdapter<String>(this,
- android.R.layout.simple_spinner_item, listInterface);
- adapterInterface.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spInterface.setAdapter(adapterInterface);
- spInterface.setOnItemSelectedListener(interfaceOnItemSelectedListener);
- }
- OnItemSelectedListener interfaceOnItemSelectedListener =
- new OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> parent,
- View view, int position, long id) {
- UsbInterface selectedUsbIf = listUsbInterface.get(position);
- String sUsbIf = "\n" + selectedUsbIf.toString() + "\n"
- + "Id: " + selectedUsbIf.getId() + "\n"
- + "InterfaceClass: " + selectedUsbIf.getInterfaceClass() + "\n"
- + "InterfaceProtocol: " + selectedUsbIf.getInterfaceProtocol() + "\n"
- + "InterfaceSubclass: " + selectedUsbIf.getInterfaceSubclass() + "\n"
- + "EndpointCount: " + selectedUsbIf.getEndpointCount();
- textInfoInterface.setText(sUsbIf);
- checkUsbInterface(selectedUsbIf);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {}
- };
- private void checkUsbInterface(UsbInterface uif) {
- listEndPoint = new ArrayList<String>();
- listUsbEndpoint = new ArrayList<UsbEndpoint>();
- for(int i=0; i<uif.getEndpointCount(); i++){
- UsbEndpoint usbEndpoint = uif.getEndpoint(i);
- listEndPoint.add(usbEndpoint.toString());
- listUsbEndpoint.add(usbEndpoint);
- }
- adapterEndpoint = new ArrayAdapter<String>(this,
- android.R.layout.simple_spinner_item, listEndPoint);
- adapterEndpoint.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spEndPoint.setAdapter(adapterEndpoint);
- spEndPoint.setOnItemSelectedListener(endpointOnItemSelectedListener);
- }
- OnItemSelectedListener endpointOnItemSelectedListener =
- new OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> parent,
- View view, int position, long id) {
- UsbEndpoint selectedEndpoint = listUsbEndpoint.get(position);
- String sEndpoint = "\n" + selectedEndpoint.toString() + "\n"
- + translateEndpointType(selectedEndpoint.getType());
- textEndPoint.setText(sEndpoint);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {}
- };
- private String translateEndpointType(int type){
- switch(type){
- case UsbConstants.USB_ENDPOINT_XFER_CONTROL:
- return "USB_ENDPOINT_XFER_CONTROL (endpoint zero)";
- case UsbConstants.USB_ENDPOINT_XFER_ISOC:
- return "USB_ENDPOINT_XFER_ISOC (isochronous endpoint)";
- case UsbConstants.USB_ENDPOINT_XFER_BULK :
- return "USB_ENDPOINT_XFER_BULK (bulk endpoint)";
- case UsbConstants.USB_ENDPOINT_XFER_INT:
- return "USB_ENDPOINT_XFER_INT (interrupt endpoint)";
- default:
- return "unknown";
- }
- }
- private String translateDeviceClass(int deviceClass){
- switch(deviceClass){
- case UsbConstants.USB_CLASS_APP_SPEC:
- return "Application specific USB class";
- case UsbConstants.USB_CLASS_AUDIO:
- return "USB class for audio devices";
- case UsbConstants.USB_CLASS_CDC_DATA:
- return "USB class for CDC devices (communications device class)";
- case UsbConstants.USB_CLASS_COMM:
- return "USB class for communication devices";
- case UsbConstants.USB_CLASS_CONTENT_SEC:
- return "USB class for content security devices";
- case UsbConstants.USB_CLASS_CSCID:
- return "USB class for content smart card devices";
- case UsbConstants.USB_CLASS_HID:
- return "USB class for human interface devices (for example, mice and keyboards)";
- case UsbConstants.USB_CLASS_HUB:
- return "USB class for USB hubs";
- case UsbConstants.USB_CLASS_MASS_STORAGE:
- return "USB class for mass storage devices";
- case UsbConstants.USB_CLASS_MISC:
- return "USB class for wireless miscellaneous devices";
- case UsbConstants.USB_CLASS_PER_INTERFACE:
- return "USB class indicating that the class is determined on a per-interface basis";
- case UsbConstants.USB_CLASS_PHYSICA:
- return "USB class for physical devices";
- case UsbConstants.USB_CLASS_PRINTER:
- return "USB class for printers";
- case UsbConstants.USB_CLASS_STILL_IMAGE:
- return "USB class for still image devices (digital cameras)";
- case UsbConstants.USB_CLASS_VENDOR_SPEC:
- return "Vendor specific USB class";
- case UsbConstants.USB_CLASS_VIDEO:
- return "USB class for video devices";
- case UsbConstants.USB_CLASS_WIRELESS_CONTROLLER:
- return "USB class for wireless controller devices";
- default: return "Unknown USB class!";
- }
- }
- }
- USBInfoLinux.class
- public class USBInfoLinux extends AppCompatActivity
- {
- public static final String DEVICE_PATH_KEY = "devicePath";
- private static final String[] fieldList = new String[]{ "bDeviceClass", "bDeviceSubClass", "idVendor", "manufacturer",
- "idProduct", "product", "version", "speed", "bDeviceProtocol", "bMaxPower", "serial" };
- private TextView textView1,textView2;
- private static class InfoField
- {
- String field;
- String value;
- InfoField(String field, String value)
- {
- this.field = field;
- this.value = value;
- }
- @Override
- public String toString()
- {
- return field+": "+value;
- }
- }
- private static String getValue(String path)
- {
- File file = new File(path);
- if (!file.isDirectory())
- {
- try
- {
- FileReader fileReader = new FileReader(file);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String line = bufferedReader.readLine();
- bufferedReader.close();
- return line;
- }
- catch (IOException e)
- {
- e.printStackTrace();
- return null;
- }
- }
- return null;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
- boolean keep_screen = sharedPrefs.getBoolean("Keep screen on",false);
- String theme = sharedPrefs.getString("Choose Theme","Light");
- if(keep_screen)
- {
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- }
- if(theme != null && theme.equals("Light"))
- {
- setTheme(R.style.AppTheme);
- //setTheme(R.style.Theme_light);
- //Toast.makeText(AutoOTG.this,"Light theme",Toast.LENGTH_SHORT).show();
- }
- else
- {
- setTheme(R.style.AppTheme_AppBarOverlay);
- //Toast.makeText(AutoOTG.this,"Dark theme",Toast.LENGTH_SHORT).show();
- }
- super.onCreate(savedInstanceState);
- setContentView(R.layout.usbinfolinux);
- textView1 = (TextView) findViewById(R.id.textView3);
- textView2 = (TextView) findViewById(R.id.textView4);
- Intent intent = getIntent();
- if (intent.getExtras() == null)
- finish();
- String devicePath = intent.getExtras().getString(DEVICE_PATH_KEY);
- try
- {
- String path = devicePath + "/" + "idVendor";
- FileReader fileReader = new FileReader(path);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String line = bufferedReader.readLine();
- bufferedReader.close();
- String path1 = devicePath + "/" + "bDeviceClass";
- FileReader fileReader1 = new FileReader(path1);
- BufferedReader bufferedReader1 = new BufferedReader(fileReader1);
- String deviceclass = bufferedReader1.readLine();
- bufferedReader1.close();
- String path2 = devicePath + "/" + "bDeviceSubClass";
- FileReader fileReader2 = new FileReader(path2);
- BufferedReader bufferedReader2 = new BufferedReader(fileReader2);
- String devicesubclass = bufferedReader2.readLine();
- bufferedReader2.close();
- String path3 = devicePath + "/" + "idVendor";
- FileReader fileReader3 = new FileReader(path3);
- BufferedReader bufferedReader3 = new BufferedReader(fileReader3);
- String idVendor = bufferedReader3.readLine();
- bufferedReader3.close();
- String path4 = devicePath + "/" + "manufacturer";
- FileReader fileReader4 = new FileReader(path4);
- BufferedReader bufferedReader4 = new BufferedReader(fileReader4);
- String manufacturer = bufferedReader4.readLine();
- bufferedReader4.close();
- String path5 = devicePath + "/" + "idProduct";
- FileReader fileReader5 = new FileReader(path5);
- BufferedReader bufferedReader5 = new BufferedReader(fileReader5);
- String idProduct = bufferedReader5.readLine();
- bufferedReader5.close();
- String path6 = devicePath + "/" + "product";
- FileReader fileReader6 = new FileReader(path6);
- BufferedReader bufferedReader6 = new BufferedReader(fileReader6);
- String product = bufferedReader6.readLine();
- bufferedReader6.close();
- textView1.setText("Path : " + devicePath + "\n" + "Device Class :" + deviceclass + "\n" +
- "Device SubClass :" + devicesubclass + "\n" + "Vendor ID :" + idVendor + "\n" +
- "Vendor Name :" + manufacturer + "\n" + "Product ID :" + idProduct + "\n" +
- "Product Name :" + product);
- String path7 = devicePath + "/" + "version";
- FileReader fileReader7 = new FileReader(path7);
- BufferedReader bufferedReader7 = new BufferedReader(fileReader7);
- String version = bufferedReader7.readLine();
- bufferedReader7.close();
- String path8 = devicePath + "/" + "speed";
- FileReader fileReader8 = new FileReader(path8);
- BufferedReader bufferedReader8 = new BufferedReader(fileReader8);
- String speed = bufferedReader8.readLine();
- bufferedReader8.close();
- String path9 = devicePath + "/" + "bDeviceProtocol";
- FileReader fileReader9 = new FileReader(path9);
- BufferedReader bufferedReader9 = new BufferedReader(fileReader9);
- String deviceprotocol = bufferedReader9.readLine();
- bufferedReader9.close();
- String path10 = devicePath + "/" + "bMaxPower";
- FileReader fileReader10 = new FileReader(path10);
- BufferedReader bufferedReader10 = new BufferedReader(fileReader10);
- String maximumpower = bufferedReader10.readLine();
- bufferedReader10.close();
- String path11 = devicePath + "/" + "serial";
- FileReader fileReader11 = new FileReader(path11);
- BufferedReader bufferedReader11 = new BufferedReader(fileReader11);
- String serial = bufferedReader11.readLine();
- bufferedReader11.close();
- textView2.setText("USB Version : " + version + "\n" + "Speed :" + speed + "\n" +
- "Protocol :" + deviceprotocol + "\n" + "Maximum Power :" + maximumpower + "\n" +
- "Serial Number :" + serial);
- }
- catch(IOException ie)
- {
- ie.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment