@simplysm/capacitor-plugin-usb-storage 13.0.99 → 14.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +1 -0
- package/android/src/main/java/kr/co/simplysm/capacitor/usbstorage/UsbStoragePlugin.kt +269 -0
- package/dist/UsbStorage.d.ts +19 -19
- package/dist/UsbStorage.js +59 -57
- package/dist/UsbStorage.js.map +1 -6
- package/dist/UsbStoragePlugin.js +2 -1
- package/dist/UsbStoragePlugin.js.map +1 -6
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -6
- package/dist/web/UsbStorageWeb.d.ts +3 -3
- package/dist/web/UsbStorageWeb.js +72 -75
- package/dist/web/UsbStorageWeb.js.map +1 -6
- package/dist/web/VirtualUsbStorage.js +36 -39
- package/dist/web/VirtualUsbStorage.js.map +1 -6
- package/package.json +7 -7
- package/src/UsbStorage.ts +19 -19
- package/src/index.ts +1 -1
- package/src/web/UsbStorageWeb.ts +3 -3
- package/README.md +0 -95
- package/android/src/main/java/kr/co/simplysm/capacitor/usbstorage/UsbStoragePlugin.java +0 -278
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
package kr.co.simplysm.capacitor.usbstorage;
|
|
2
|
-
|
|
3
|
-
import android.app.PendingIntent;
|
|
4
|
-
import android.content.BroadcastReceiver;
|
|
5
|
-
import android.content.Context;
|
|
6
|
-
import android.content.Intent;
|
|
7
|
-
import android.content.IntentFilter;
|
|
8
|
-
import android.hardware.usb.UsbDevice;
|
|
9
|
-
import android.hardware.usb.UsbManager;
|
|
10
|
-
import android.os.Build;
|
|
11
|
-
import android.util.Base64;
|
|
12
|
-
import android.util.Log;
|
|
13
|
-
|
|
14
|
-
import com.getcapacitor.JSArray;
|
|
15
|
-
import com.getcapacitor.JSObject;
|
|
16
|
-
import com.getcapacitor.Plugin;
|
|
17
|
-
import com.getcapacitor.PluginCall;
|
|
18
|
-
import com.getcapacitor.PluginMethod;
|
|
19
|
-
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
20
|
-
|
|
21
|
-
import java.nio.ByteBuffer;
|
|
22
|
-
import java.util.Arrays;
|
|
23
|
-
import java.util.Optional;
|
|
24
|
-
|
|
25
|
-
import me.jahnen.libaums.core.UsbMassStorageDevice;
|
|
26
|
-
import me.jahnen.libaums.core.fs.FileSystem;
|
|
27
|
-
import me.jahnen.libaums.core.fs.UsbFile;
|
|
28
|
-
import me.jahnen.libaums.core.fs.UsbFileInputStream;
|
|
29
|
-
|
|
30
|
-
@CapacitorPlugin(name = "UsbStorage")
|
|
31
|
-
public class UsbStoragePlugin extends Plugin {
|
|
32
|
-
|
|
33
|
-
private static final String TAG = "UsbStoragePlugin";
|
|
34
|
-
private static final String ACTION_USB_PERMISSION = "kr.co.simplysm.capacitor.usbstorage.USB_PERMISSION";
|
|
35
|
-
private static final long MAX_FILE_SIZE = 100L * 1024 * 1024; // 100MB
|
|
36
|
-
|
|
37
|
-
@PluginMethod
|
|
38
|
-
public void getDevices(PluginCall call) {
|
|
39
|
-
try {
|
|
40
|
-
UsbMassStorageDevice[] devices = UsbMassStorageDevice.getMassStorageDevices(getContext());
|
|
41
|
-
|
|
42
|
-
JSArray result = new JSArray();
|
|
43
|
-
for (UsbMassStorageDevice device : devices) {
|
|
44
|
-
UsbDevice usbDevice = device.getUsbDevice();
|
|
45
|
-
|
|
46
|
-
JSObject deviceObj = new JSObject();
|
|
47
|
-
deviceObj.put("deviceName", usbDevice.getDeviceName());
|
|
48
|
-
deviceObj.put("manufacturerName", usbDevice.getManufacturerName());
|
|
49
|
-
deviceObj.put("productName", usbDevice.getProductName());
|
|
50
|
-
deviceObj.put("vendorId", usbDevice.getVendorId());
|
|
51
|
-
deviceObj.put("productId", usbDevice.getProductId());
|
|
52
|
-
result.put(deviceObj);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
JSObject ret = new JSObject();
|
|
56
|
-
ret.put("devices", result);
|
|
57
|
-
call.resolve(ret);
|
|
58
|
-
} catch (Exception e) {
|
|
59
|
-
Log.e(TAG, "getDevices failed", e);
|
|
60
|
-
call.reject("getDevices failed: " + e.getMessage());
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
@PluginMethod
|
|
65
|
-
public void requestPermissions(PluginCall call) {
|
|
66
|
-
Integer vendorId = call.getInt("vendorId");
|
|
67
|
-
Integer productId = call.getInt("productId");
|
|
68
|
-
|
|
69
|
-
if (vendorId == null || productId == null) {
|
|
70
|
-
call.reject("vendorId and productId are required");
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
try {
|
|
75
|
-
UsbMassStorageDevice device = getDevice(vendorId, productId);
|
|
76
|
-
UsbDevice usbDevice = device.getUsbDevice();
|
|
77
|
-
|
|
78
|
-
UsbManager usbManager = (UsbManager) getContext().getSystemService(Context.USB_SERVICE);
|
|
79
|
-
if (usbManager.hasPermission(usbDevice)) {
|
|
80
|
-
JSObject ret = new JSObject();
|
|
81
|
-
ret.put("granted", true);
|
|
82
|
-
call.resolve(ret);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
BroadcastReceiver receiver = new BroadcastReceiver() {
|
|
87
|
-
@Override
|
|
88
|
-
public void onReceive(Context context, Intent intent) {
|
|
89
|
-
try {
|
|
90
|
-
context.unregisterReceiver(this);
|
|
91
|
-
boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
|
|
92
|
-
JSObject ret = new JSObject();
|
|
93
|
-
ret.put("granted", granted);
|
|
94
|
-
call.resolve(ret);
|
|
95
|
-
} catch (Exception e) {
|
|
96
|
-
Log.e(TAG, "requestPermission callback failed", e);
|
|
97
|
-
call.reject("requestPermission failed: " + e.getMessage());
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
|
|
103
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
104
|
-
getContext().registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED);
|
|
105
|
-
} else {
|
|
106
|
-
getContext().registerReceiver(receiver, filter);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
|
|
110
|
-
? PendingIntent.FLAG_MUTABLE
|
|
111
|
-
: 0;
|
|
112
|
-
PendingIntent permissionIntent = PendingIntent.getBroadcast(
|
|
113
|
-
getContext(), 0, new Intent(ACTION_USB_PERMISSION), flags
|
|
114
|
-
);
|
|
115
|
-
usbManager.requestPermission(usbDevice, permissionIntent);
|
|
116
|
-
} catch (Exception e) {
|
|
117
|
-
Log.e(TAG, "requestPermission failed", e);
|
|
118
|
-
call.reject("requestPermission failed: " + e.getMessage());
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
@PluginMethod
|
|
123
|
-
public void checkPermissions(PluginCall call) {
|
|
124
|
-
Integer vendorId = call.getInt("vendorId");
|
|
125
|
-
Integer productId = call.getInt("productId");
|
|
126
|
-
|
|
127
|
-
if (vendorId == null || productId == null) {
|
|
128
|
-
call.reject("vendorId and productId are required");
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
try {
|
|
133
|
-
UsbMassStorageDevice device = getDevice(vendorId, productId);
|
|
134
|
-
UsbManager usbManager = (UsbManager) getContext().getSystemService(Context.USB_SERVICE);
|
|
135
|
-
boolean hasPermission = usbManager.hasPermission(device.getUsbDevice());
|
|
136
|
-
|
|
137
|
-
JSObject ret = new JSObject();
|
|
138
|
-
ret.put("granted", hasPermission);
|
|
139
|
-
call.resolve(ret);
|
|
140
|
-
} catch (Exception e) {
|
|
141
|
-
Log.e(TAG, "hasPermission failed", e);
|
|
142
|
-
call.reject("hasPermission failed: " + e.getMessage());
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
@PluginMethod
|
|
147
|
-
public void readdir(PluginCall call) {
|
|
148
|
-
Integer vendorId = call.getInt("vendorId");
|
|
149
|
-
Integer productId = call.getInt("productId");
|
|
150
|
-
String path = call.getString("path");
|
|
151
|
-
|
|
152
|
-
if (vendorId == null || productId == null || path == null) {
|
|
153
|
-
call.reject("vendorId, productId, and path are required");
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
try {
|
|
158
|
-
UsbMassStorageDevice device = getDevice(vendorId, productId);
|
|
159
|
-
|
|
160
|
-
UsbManager usbManager = (UsbManager) getContext().getSystemService(Context.USB_SERVICE);
|
|
161
|
-
if (!usbManager.hasPermission(device.getUsbDevice())) {
|
|
162
|
-
call.reject("No permission for this USB device");
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
device.init();
|
|
167
|
-
try {
|
|
168
|
-
FileSystem fs = device.getPartitions().get(0).getFileSystem();
|
|
169
|
-
UsbFile root = fs.getRootDirectory();
|
|
170
|
-
UsbFile dir = root.search(path);
|
|
171
|
-
|
|
172
|
-
if (dir == null || !dir.isDirectory()) {
|
|
173
|
-
call.reject("Directory not found: " + path);
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
UsbFile[] files = dir.listFiles();
|
|
178
|
-
|
|
179
|
-
JSArray result = new JSArray();
|
|
180
|
-
for (UsbFile file : files) {
|
|
181
|
-
JSObject info = new JSObject();
|
|
182
|
-
info.put("name", file.getName());
|
|
183
|
-
info.put("isDirectory", file.isDirectory());
|
|
184
|
-
result.put(info);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
JSObject ret = new JSObject();
|
|
188
|
-
ret.put("files", result);
|
|
189
|
-
call.resolve(ret);
|
|
190
|
-
} finally {
|
|
191
|
-
device.close();
|
|
192
|
-
}
|
|
193
|
-
} catch (Exception e) {
|
|
194
|
-
Log.e(TAG, "readdir failed", e);
|
|
195
|
-
call.reject("readdir failed: " + e.getMessage());
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
@PluginMethod
|
|
200
|
-
public void readFile(PluginCall call) {
|
|
201
|
-
Integer vendorId = call.getInt("vendorId");
|
|
202
|
-
Integer productId = call.getInt("productId");
|
|
203
|
-
String path = call.getString("path");
|
|
204
|
-
|
|
205
|
-
if (vendorId == null || productId == null || path == null) {
|
|
206
|
-
call.reject("vendorId, productId, and path are required");
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
try {
|
|
211
|
-
UsbMassStorageDevice device = getDevice(vendorId, productId);
|
|
212
|
-
|
|
213
|
-
UsbManager usbManager = (UsbManager) getContext().getSystemService(Context.USB_SERVICE);
|
|
214
|
-
if (!usbManager.hasPermission(device.getUsbDevice())) {
|
|
215
|
-
call.reject("No permission for this USB device");
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
device.init();
|
|
220
|
-
try {
|
|
221
|
-
FileSystem fs = device.getPartitions().get(0).getFileSystem();
|
|
222
|
-
UsbFile root = fs.getRootDirectory();
|
|
223
|
-
UsbFile usbFile = root.search(path);
|
|
224
|
-
|
|
225
|
-
if (usbFile == null) {
|
|
226
|
-
JSObject ret = new JSObject();
|
|
227
|
-
ret.put("data", (String) null);
|
|
228
|
-
call.resolve(ret);
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (usbFile.isDirectory()) {
|
|
233
|
-
call.reject("Path is a directory: " + path);
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
long fileLength = usbFile.getLength();
|
|
238
|
-
if (fileLength > MAX_FILE_SIZE) {
|
|
239
|
-
call.reject("File too large: " + fileLength + " bytes (max " + MAX_FILE_SIZE + ")");
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
ByteBuffer buffer = ByteBuffer.allocate((int) fileLength);
|
|
243
|
-
|
|
244
|
-
UsbFileInputStream inputStream = new UsbFileInputStream(usbFile);
|
|
245
|
-
byte[] tmpBuf = new byte[fs.getChunkSize()];
|
|
246
|
-
int count;
|
|
247
|
-
while ((count = inputStream.read(tmpBuf)) != -1) {
|
|
248
|
-
buffer.put(tmpBuf, 0, count);
|
|
249
|
-
}
|
|
250
|
-
inputStream.close();
|
|
251
|
-
|
|
252
|
-
String base64Data = Base64.encodeToString(buffer.array(), Base64.NO_WRAP);
|
|
253
|
-
|
|
254
|
-
JSObject ret = new JSObject();
|
|
255
|
-
ret.put("data", base64Data);
|
|
256
|
-
call.resolve(ret);
|
|
257
|
-
} finally {
|
|
258
|
-
device.close();
|
|
259
|
-
}
|
|
260
|
-
} catch (Exception e) {
|
|
261
|
-
Log.e(TAG, "readFile failed", e);
|
|
262
|
-
call.reject("readFile failed: " + e.getMessage());
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
private UsbMassStorageDevice getDevice(int vendorId, int productId) throws Exception {
|
|
267
|
-
UsbMassStorageDevice[] devices = UsbMassStorageDevice.getMassStorageDevices(getContext());
|
|
268
|
-
Optional<UsbMassStorageDevice> optDevice = Arrays.stream(devices).filter((tmpDevice) -> {
|
|
269
|
-
UsbDevice tmpUsbDevice = tmpDevice.getUsbDevice();
|
|
270
|
-
return tmpUsbDevice.getVendorId() == vendorId && tmpUsbDevice.getProductId() == productId;
|
|
271
|
-
}).findFirst();
|
|
272
|
-
|
|
273
|
-
if (!optDevice.isPresent()) {
|
|
274
|
-
throw new Exception("USB device not found: vendorId=" + vendorId + ", productId=" + productId);
|
|
275
|
-
}
|
|
276
|
-
return optDevice.get();
|
|
277
|
-
}
|
|
278
|
-
}
|