Skip to content

Commit dd94c3b

Browse files
committed
Fixed(UsbAPI): Implement product/vendor ID support
Implement support for options added in API package: termux/termux-api-package@7531d57
1 parent 760c177 commit dd94c3b

File tree

1 file changed

+55
-33
lines changed

1 file changed

+55
-33
lines changed

app/src/main/java/com/termux/api/apis/UsbAPI.java

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.concurrent.atomic.AtomicReference;
3030

3131
import androidx.annotation.NonNull;
32+
import androidx.annotation.Nullable;
3233

3334
public class UsbAPI {
3435

@@ -111,9 +112,8 @@ public void onDestroy() {
111112
}
112113

113114

114-
115115
protected void runListAction(Intent intent) {
116-
Logger.logVerbose(LOG_TAG,"Running 'list' usb devices action");
116+
Logger.logVerbose(LOG_TAG, "Running 'list' usb devices action");
117117

118118
ResultReturner.returnData(this, intent, new ResultReturner.ResultJsonWriter() {
119119
@Override
@@ -134,60 +134,64 @@ protected void listDevices(JsonWriter out) throws IOException {
134134
}
135135

136136

137-
138137
protected void runPermissionAction(Intent intent) {
139138
mThreadPoolExecutor.submit(() -> {
140139
String deviceName = intent.getStringExtra("device");
140+
String vendorId = intent.getStringExtra("vendorId");
141+
String productId = intent.getStringExtra("productId");
142+
String device = (deviceName != null) ? deviceName : vendorId + ":" + productId;
141143

142-
Logger.logVerbose(LOG_TAG,"Running 'permission' action for device \"" + deviceName + "\"");
144+
Logger.logVerbose(LOG_TAG, "Running 'permission' action for device \"" + device + "\"");
143145

144-
UsbDevice device = getDevice(intent, deviceName);
145-
if (device == null) return;
146+
UsbDevice usbDevice = getDevice(intent, deviceName, vendorId, productId);
147+
if (usbDevice == null) return;
146148

147-
int status = checkAndRequestUsbDevicePermission(intent, device);
149+
int status = checkAndRequestUsbDevicePermission(intent, usbDevice);
148150
ResultReturner.returnData(this, intent, out -> {
149151
if (status == 0) {
150-
Logger.logVerbose(LOG_TAG, "Permission granted for device \"" + device.getDeviceName() + "\"");
151-
out.append("Permission granted.\n" );
152+
Logger.logVerbose(LOG_TAG, "Permission granted for device \"" + usbDevice.getDeviceName() + "\"");
153+
out.append("Permission granted.\n");
152154
} else if (status == 1) {
153-
Logger.logVerbose(LOG_TAG, "Permission denied for device \"" + device.getDeviceName() + "\"");
154-
out.append("Permission denied.\n" );
155+
Logger.logVerbose(LOG_TAG, "Permission denied for device \"" + usbDevice.getDeviceName() + "\"");
156+
out.append("Permission denied.\n");
155157
} else if (status == -1) {
156-
out.append("Permission request timeout.\n" );
158+
out.append("Permission request timeout.\n");
157159
}
158160
});
159161
});
160162
}
161163

162164

163-
164165
protected void runOpenAction(Intent intent) {
165166
mThreadPoolExecutor.submit(() -> {
166167
String deviceName = intent.getStringExtra("device");
168+
String vendorId = intent.getStringExtra("vendorId");
169+
String productId = intent.getStringExtra("productId");
170+
String device = (deviceName != null) ? deviceName : vendorId + ":" + productId;
167171

168-
Logger.logVerbose(LOG_TAG,"Running 'open' action for device \"" + deviceName + "\"");
172+
Logger.logVerbose(LOG_TAG, "Running 'open' action for device \"" + device + "\"");
169173

170-
UsbDevice device = getDevice(intent, deviceName);
171-
if (device == null) return;
174+
UsbDevice usbDevice = getDevice(intent, deviceName, vendorId, productId);
175+
if (usbDevice == null) return;
172176

173-
int status = checkAndRequestUsbDevicePermission(intent, device);
177+
int status = checkAndRequestUsbDevicePermission(intent, usbDevice);
174178
ResultReturner.returnData(this, intent, new ResultReturner.WithAncillaryFd() {
175179
@Override
176180
public void writeResult(PrintWriter out) {
177181
if (status == 0) {
178-
int fd = open(device);
182+
int fd = open(usbDevice);
179183
if (fd < 0) {
180-
Logger.logVerbose(LOG_TAG, "Failed to open device \"" + device.getDeviceName() + "\": " + fd);
184+
Logger.logVerbose(LOG_TAG, "Failed to open device \"" + usbDevice.getDeviceName() + "\": " + fd);
181185
out.append("Open device failed.\n");
182186
} else {
183-
Logger.logVerbose(LOG_TAG, "Open device \"" + device.getDeviceName() + "\" successful");
187+
Logger.logVerbose(LOG_TAG, "Open device \"" + usbDevice.getDeviceName() + "\" successful");
184188
this.sendFd(out, fd);
185189
}
186190
} else if (status == 1) {
187-
Logger.logVerbose(LOG_TAG, "Permission denied to open device \"" + device.getDeviceName() + "\"");
188-
out.append("Permission denied.\n" );
191+
Logger.logVerbose(LOG_TAG, "Permission denied to open device \"" + usbDevice.getDeviceName() + "\"");
192+
out.append("Permission denied.\n");
189193
} else if (status == -1) {
190-
out.append("Permission request timeout.\n" );
194+
out.append("Permission request timeout.\n");
191195
}
192196
}
193197
});
@@ -211,20 +215,38 @@ protected int open(@NonNull UsbDevice device) {
211215
}
212216

213217

214-
215-
protected UsbDevice getDevice(Intent intent, String deviceName) {
218+
protected UsbDevice getDevice(Intent intent, @Nullable String deviceName, @Nullable String vendorId, @Nullable String productId) {
216219
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
217220

218-
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
219-
UsbDevice device = deviceList.get(deviceName);
220-
if (device == null) {
221-
Logger.logVerbose(LOG_TAG, "Failed to find device \"" + deviceName + "\"");
222-
ResultReturner.returnData(this, intent, out -> out.append("No such device.\n"));
221+
if (deviceName != null) {
222+
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
223+
UsbDevice usbDevice = deviceList.get(deviceName);
224+
if (usbDevice != null) return usbDevice;
223225
}
224226

225-
return device;
226-
}
227+
if (vendorId != null && productId != null) {
228+
// If we do not strip the 0x, it freezes at Integer.parseInt
229+
vendorId = vendorId.startsWith("0x") ? vendorId.substring(2) : vendorId;
230+
productId = productId.startsWith("0x") ? productId.substring(2) : productId;
231+
// UsbDevice returns int, not hex, correct for that.
232+
int iVendorId = Integer.parseInt(vendorId, 16);
233+
int iProductId = Integer.parseInt(productId, 16);
234+
for (UsbDevice usbDevice : usbManager.getDeviceList().values()) {
235+
if (usbDevice.getVendorId() == iVendorId && usbDevice.getProductId() == iProductId) {
236+
if (deviceName != null && !(deviceName.equals(usbDevice.getDeviceName()))) {
237+
Logger.logVerbose(LOG_TAG, "Device \"" + deviceName + "\" mismatch with " + vendorId + ":" + productId);
238+
ResultReturner.returnData(this, intent, out -> out.append("No such device.\n"));
239+
return null;
240+
}
241+
return usbDevice;
242+
}
243+
}
244+
}
227245

246+
Logger.logVerbose(LOG_TAG, "No intent extras were passed for device name or vendor and product id.");
247+
ResultReturner.returnData(this, intent, out -> out.append("No such device.\n"));
248+
return null;
249+
}
228250

229251

230252
protected boolean checkUsbDevicePermission(@NonNull UsbDevice device) {
@@ -239,7 +261,7 @@ protected int checkAndRequestUsbDevicePermission(Intent intent, @NonNull UsbDevi
239261
return 0;
240262
}
241263

242-
if(!intent.getBooleanExtra("request", false)) {
264+
if (!intent.getBooleanExtra("request", false)) {
243265
return 1;
244266
}
245267

0 commit comments

Comments
 (0)