Skip to content

Commit

Permalink
Fixed: Use /system/bin/am for broadcast commands on Android >= 14
Browse files Browse the repository at this point in the history
… instead of TermuxAm

Using TermuxAm for broadcast commands will trigger the `Sending broadcast <action> with resultTo requires resultToApp` exception and will hang the app_process command forever. Reproducible on Android 14 revision 8 AVD.

Related comment #9 (comment)
  • Loading branch information
agnostic-apollo committed Aug 30, 2023
1 parent 598a9c0 commit 8761289
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
39 changes: 39 additions & 0 deletions am-libexec-packaged
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,45 @@

AM_APK_PATH="@TERMUX_PREFIX@/libexec/termux-am/am.apk"

is_int() {
case "$1" in
''|*[!0-9]*) return 1;;
*) return 0;;
esac
}

# If sdk version is not exported by app, then get value with getprop instead.
if ! is_int "$ANDROID__BUILD_VERSION_SDK"; then
ANDROID__BUILD_VERSION_SDK="$(getprop "ro.build.version.sdk")"
if ! is_int "$ANDROID__BUILD_VERSION_SDK"; then
echo "Failed to get android build version sdk with getprop" 1>&2
exit 1
fi
fi

# Do not use TermuxAm for broadcast commands on Android >= 14 since it
# will trigger the `Sending broadcast <action> with resultTo requires resultToApp`
# exception in logcat by ActivityManagerService and will hang the
# app_process command forever since no result callback is received
# by TermuxAm `IIntentReceiver.performReceive()`.
# - https://github.com/termux/TermuxAm/issues/9#issuecomment-1649867810
if [ "$ANDROID__BUILD_VERSION_SDK" -ge 34 ] && [ "$1" = "broadcast" ]; then
is_int "$TERMUX__USER_ID" || TERMUX__USER_ID=0
shift 1 # Remove the first `broadcast` argument

# Use a subshell with specified redirection to prevent
# `cmd: Failure calling service activity` error on Android >= 8
# due to selinux restrictions where the `system_server` source
# domain does not have access to `untrusted_app_all_devpts`
# `pty` devices when a source transition is made from `untrusted_app*`
# domain when `/system/bin/am` is executed.
# - https://github.com/termux/termux-packages/discussions/8292#discussioncomment-5102555
output="$(/system/bin/am broadcast --user "$TERMUX__USER_ID" "$@" 2>&1 </dev/null)"
exit_code=$?
echo "$output"
exit $exit_code
fi

# If apk file is writable and current effective user is not root (0),
# system (1000) and shell (2000), then remove write bit from apk
# permissions for current used for Android >= 14 since it will trigger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.IIntentReceiver;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;

Expand Down Expand Up @@ -95,6 +96,11 @@ public void testStartActivity() throws Exception {

@Test
public void testBroadcastIntent() throws Exception {
// Do not run on Android `>= 14` since it will trigger the
// `Sending broadcast <action> with resultTo requires resultToApp` exception in logcat by
// ActivityManagerService and will hang the test forever
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) return;

final CountDownLatch latch = new CountDownLatch(1);
final Intent[] outIntent = new Intent[1];
final String[] outData = new String[1];
Expand Down

0 comments on commit 8761289

Please sign in to comment.