mirror of
https://github.com/danog/termux-api.git
synced 2024-12-02 09:17:50 +01:00
termux-usb: add UsbClose
This commit is contained in:
parent
e7b53001e7
commit
0303cf47b0
@ -174,6 +174,9 @@ public class TermuxApiReceiver extends BroadcastReceiver {
|
|||||||
case "Torch":
|
case "Torch":
|
||||||
TorchAPI.onReceive(this, context, intent);
|
TorchAPI.onReceive(this, context, intent);
|
||||||
break;
|
break;
|
||||||
|
case "UsbClose":
|
||||||
|
UsbAPI.onReceiveUsbClose(this, context, intent);
|
||||||
|
break;
|
||||||
case "UsbInfo":
|
case "UsbInfo":
|
||||||
UsbAPI.onReceiveUsbInfo(this, context, intent);
|
UsbAPI.onReceiveUsbInfo(this, context, intent);
|
||||||
break;
|
break;
|
||||||
|
@ -9,6 +9,7 @@ import android.hardware.usb.UsbConstants;
|
|||||||
import android.hardware.usb.UsbDevice;
|
import android.hardware.usb.UsbDevice;
|
||||||
import android.hardware.usb.UsbDeviceConnection;
|
import android.hardware.usb.UsbDeviceConnection;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
|
import android.os.SystemClock;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.text.format.Formatter;
|
import android.text.format.Formatter;
|
||||||
import android.util.JsonWriter;
|
import android.util.JsonWriter;
|
||||||
@ -60,30 +61,36 @@ public class UsbAPI {
|
|||||||
UsbDevice device;
|
UsbDevice device;
|
||||||
UsbDeviceConnection connection = null;
|
UsbDeviceConnection connection = null;
|
||||||
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
||||||
|
|
||||||
|
Integer vendorId = intent.getIntExtra("vendorid", -1);
|
||||||
|
Integer productId = intent.getIntExtra("productid", -1);
|
||||||
out.beginArray();
|
out.beginArray();
|
||||||
out.beginObject();
|
out.beginObject();
|
||||||
out.name("vendorId").value(vendorId);
|
|
||||||
out.name("productId").value(productId);
|
out.name("vendorid").value(intent.getIntExtra("vendorid", -1));
|
||||||
out.endObject();
|
out.name("productid").value(intent.getIntExtra("productid", -1));
|
||||||
|
|
||||||
while (deviceIterator.hasNext()) {
|
while (deviceIterator.hasNext()) {
|
||||||
device = deviceIterator.next();
|
device = deviceIterator.next();
|
||||||
//out.beginObject().name("vendor_id").value(device.getVendorId()).endObject();
|
if ((vendorId == 0 || Integer.toHexString(device.getVendorId()) == Integer.toString(vendorId)) && (productId == 0 || Integer.toHexString(device.getProductId()) == Integer.toString(productId))) {
|
||||||
|
|
||||||
if ((vendorId == 0 || device.getVendorId() == vendorId) && (productId == 0 || device.getProductId() == productId)) {
|
|
||||||
if (! manager.hasPermission(device)) {
|
if (! manager.hasPermission(device)) {
|
||||||
manager.requestPermission(device, mPermissionIntent);
|
manager.requestPermission(device, mPermissionIntent);
|
||||||
}
|
}
|
||||||
connection = manager.openDevice(device);
|
SystemClock.sleep(3000);
|
||||||
out.beginObject().name("fd").value(connection.getFileDescriptor()).endObject();
|
if (manager.hasPermission(device)) {
|
||||||
|
connection = manager.openDevice(device);
|
||||||
|
if (connection != null) {
|
||||||
|
out.name("fd").value(connection.getFileDescriptor());
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connection == null) {
|
if (connection == null) {
|
||||||
out.beginObject().name("Connection").value("failed").endObject();
|
// out.beginObject().name("Connection").value("failed").endObject();
|
||||||
} else {
|
} else {
|
||||||
out.beginObject().name("Connection").value("succeeded").endObject();
|
// out.beginObject().name("Connection").value("succeeded").endObject();
|
||||||
}
|
}
|
||||||
//manager.RequestPermission(device, mPermissionIntent);
|
//manager.RequestPermission(device, mPermissionIntent);
|
||||||
//bool hasPermision = manager.HasPermission(device);
|
//bool hasPermision = manager.HasPermission(device);
|
||||||
@ -91,11 +98,41 @@ public class UsbAPI {
|
|||||||
//UsbEndpoint endpoint = intf.getEndpoint(0);
|
//UsbEndpoint endpoint = intf.getEndpoint(0);
|
||||||
//permissionIntent = PendingIntent.getBroadcast(0, new Intent(ACTION_USB_PERMISSION), 0);
|
//permissionIntent = PendingIntent.getBroadcast(0, new Intent(ACTION_USB_PERMISSION), 0);
|
||||||
//usbManager.requestPermission(device, permissionIntent);
|
//usbManager.requestPermission(device, permissionIntent);
|
||||||
|
out.endObject();
|
||||||
out.endArray();
|
out.endArray();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void onReceiveUsbClose(TermuxApiReceiver apiReceiver, final Context context, final Intent intent) {
|
||||||
|
ResultReturner.returnData(apiReceiver, intent, new ResultReturner.ResultJsonWriter() {
|
||||||
|
@Override
|
||||||
|
public void writeJson(JsonWriter out) throws Exception {
|
||||||
|
UsbManager manager = (UsbManager) context.getApplicationContext().getSystemService(Context.USB_SERVICE);
|
||||||
|
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
|
||||||
|
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
|
||||||
|
UsbDevice device;
|
||||||
|
UsbDeviceConnection connection = null;
|
||||||
|
|
||||||
|
Integer vendorId = intent.getIntExtra("vendorid", -1);
|
||||||
|
Integer productId = intent.getIntExtra("productid", -1);
|
||||||
|
|
||||||
|
while (deviceIterator.hasNext()) {
|
||||||
|
device = deviceIterator.next();
|
||||||
|
if ((vendorId == 0 || device.getVendorId() == vendorId) && (productId == 0 || device.getProductId() == productId)) {
|
||||||
|
if (manager.hasPermission(device)) {
|
||||||
|
connection = manager.openDevice(device);
|
||||||
|
if (connection != null) {
|
||||||
|
out.beginObject().name("fd").value(connection.getFileDescriptor()).endObject();
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
static String translateDeviceClass(int usbClass){
|
static String translateDeviceClass(int usbClass){
|
||||||
switch(usbClass){
|
switch(usbClass){
|
||||||
case UsbConstants.USB_CLASS_APP_SPEC:
|
case UsbConstants.USB_CLASS_APP_SPEC:
|
||||||
|
Loading…
Reference in New Issue
Block a user