Commit 15d5fb6
Changed|Fixed(UsbAPI): Add
- Fix `termux-usb` command hanging for a long time after permission request was granted/denied on Android `14`. This was being caused by `BroadcastReceiver.goAsync()` call for `TermuxApiReceiver` in `ResultReturner.returnData()`. If that is called, then the permission request `BroadcastReceiver.onReceive()` registered by `UsbAPI` is not called immediately or may hang indefinitely. Removing the `goAsync` fixes the issue, but that would result in `TermuxApiReceiver.onReceive()` processing time to be reduced as per Android `BroadcastReceiver` limits, and longer processing time would result in exceptions. To fix this, the `UsbService` has been added that will handle all the UsbAPI actions, including requesting and waiting for permission. Using a service will also not have limits on processing times.
- Use explicit intent, otherwise permission request intent will be blocked if intent is mutable and app uses `targetSdkVersion` `>= 34`, or following exception will be logged to logcat if app uses `targetSdkVersion` `< 34`.
- https://developer.android.com/about/versions/14/behavior-changes-14#safer-intents
```
android.app.StackTrace: New mutable implicit PendingIntent: pkg=com.termux.api, action=com.termux.api.USB_PERMISSION, featureId=null. This will be blocked once the app targets U+ for security reasons.
at android.app.PendingIntent.checkPendingIntent(PendingIntent.java:465)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:753)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:740)
```
- Use mutable intent, otherwise permission request intent will be blocked if app uses `targetSdkVersion` `>= 31` and following exception may be logged to logcat.
- https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability
```
java.lang.IllegalArgumentException: com.termux.api: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.PendingIntent.checkPendingIntent(PendingIntent.java:450)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:753)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:740)
at com.termux.api.apis.UsbAPI.requestPermission(UsbAPI.java:139)
```
- Specify flag to not export receiver, otherwise permission request intent will be blocked if app uses `targetSdkVersion` `>= 34`.
- https://developer.android.com/about/versions/14/behavior-changes-14#runtime-receivers-exported
- Use a single queue for `permission` and `open` device requests. Additionally, use a `30s` timeout, and if user does not grant/deny permission within the timeout, then abort request with `Permission request timeout.` error. Pressing the permission dialog `OK` button after `30s` will be ignored. However, on Android `>= 14`, the Termux:API app process may get frozen after showing the permission dialog, and the timeout will not occur. To fix that, either disable `cached_apps_freezer`, or send an intent to Termux:API app process to unfreeze it, like with `termux-api-start` with a 30s delay before running the actual open request command, like with following commands.
- #638 (comment)
```
( { sleep 30 && termux-api-start; } &)
termux-usb -r -e echo "<device>"
```
- The following messages/errors have been updated for consistency. Error handling for exit code `1` in `termux-usb` script will be updated accordingly.
- open|permission: `No such device` -> `No such device.`
- permission: `yes` -> `Permission granted.`
- permission: `no` -> `Permission denied.`
- open: `No permission` -> `Permission denied.`
- open: `Failed to open device` -> `Open device failed.`
Co-authored-by: @agnostic-apollo <agnosticapollo@gmail.com>
Co-authored-by: @9hm2 <m.horvath020@gmail.com>
Closes #675UsbService to process usb api requests and fix multiple exceptions for Android 12+ and hang up for Android 14+1 parent d5da717 commit 15d5fb6
File tree
3 files changed
+292
-122
lines changed- app/src/main
- java/com/termux/api
- apis
3 files changed
+292
-122
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
212 | 215 | | |
213 | 216 | | |
214 | 217 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
242 | 242 | | |
243 | 243 | | |
244 | 244 | | |
245 | | - | |
| 245 | + | |
246 | 246 | | |
247 | 247 | | |
248 | 248 | | |
| |||
0 commit comments