这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit 15d5fb6

Browse files
9hm2agnostic-apollo
authored andcommitted
Changed|Fixed(UsbAPI): Add UsbService to process usb api requests and fix multiple exceptions for Android 12+ and hang up for Android 14+
- 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 #675
1 parent d5da717 commit 15d5fb6

File tree

3 files changed

+292
-122
lines changed

3 files changed

+292
-122
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@
209209
<service android:name=".apis.WallpaperAPI$WallpaperService"
210210
android:exported="false" />
211211

212+
<service android:name=".apis.UsbAPI$UsbService"
213+
android:exported="false" />
214+
212215
</application>
213216

214217
</manifest>

app/src/main/java/com/termux/api/TermuxApiReceiver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private void doWork(Context context, Intent intent) {
242242
TorchAPI.onReceive(this, context, intent);
243243
break;
244244
case "Usb":
245-
UsbAPI.onReceive(this, context, intent);
245+
UsbAPI.onReceive(context, intent);
246246
break;
247247
case "Vibrate":
248248
VibrateAPI.onReceive(this, context, intent);

0 commit comments

Comments
 (0)