Skip to content

Commit

Permalink
Added: Add TERMUX_API_VERSION to termux shell environment
Browse files Browse the repository at this point in the history
This can be used to check if `Termux:API` is installed and enabled for cases where users try to run `termux-api` commands and it hangs. The check can be added to start of each `termux-api` script during build time by replacing a placeholder with `sed`.

```
if dpkg --compare-versions "$TERMUX_VERSION" ge 0.118 && [ -z "$TERMUX_API_VERSION" ]; then
echo "The Termux:API app is not installed or enabled which is required by termux-api commands to work." 1>&2
exit 1
fi

current_user="$(id -un)"
termux_user="$(stat -c "%U" "/data/data/com.termux/files/usr")"
if [ "$current_user" != "$termux_user" ]; then
echo "The termux-api commands must be run as the termux user \"$termux_user\" instead of as \"$current_user\"." 1>&2
echo "Trying to run with \"su $termux_user -c termux-api-command\" will fail as well." 1>&2
exit 1
fi
```
  • Loading branch information
agnostic-apollo committed Sep 8, 2021
1 parent 7b10a35 commit 0cf3cef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class TermuxShellUtils {
public static String TERMUX_APP_PID;
public static String TERMUX_APK_RELEASE;

public static String TERMUX_API_VERSION_NAME;

public static String getDefaultWorkingDirectoryPath() {
return TermuxConstants.TERMUX_HOME_DIR_PATH;
}
Expand Down Expand Up @@ -52,6 +54,9 @@ public static String[] buildEnvironment(Context currentPackageContext, boolean i
if (TERMUX_APK_RELEASE != null)
environment.add("TERMUX_APK_RELEASE=" + TERMUX_APK_RELEASE);

if (TERMUX_API_VERSION_NAME != null)
environment.add("TERMUX_API_VERSION=" + TERMUX_API_VERSION_NAME);

environment.add("TERM=xterm-256color");
environment.add("COLORTERM=truecolor");
environment.add("HOME=" + TermuxConstants.TERMUX_HOME_DIR_PATH);
Expand Down Expand Up @@ -180,6 +185,16 @@ public static void loadTermuxEnvVariables(Context currentPackageContext) {
}
}


TERMUX_API_VERSION_NAME = null;

// Check if Termux:API app is installed and not disabled
if (TermuxUtils.isTermuxAPIAppInstalled(currentPackageContext) == null) {
// This function may be called by a different package like a plugin, so we get version for Termux:API package via its context
Context termuxAPIPackageContext = TermuxUtils.getTermuxAPIPackageContext(currentPackageContext);
if (termuxAPIPackageContext != null)
TERMUX_API_VERSION_NAME = PackageUtils.getVersionNameForPackage(termuxAPIPackageContext);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ public static String isTermuxAppInstalled(@NonNull final Context context) {
return PackageUtils.isAppInstalled(context, TermuxConstants.TERMUX_APP_NAME, TermuxConstants.TERMUX_PACKAGE_NAME);
}

/**
* Check if Termux:API app is installed and enabled. This can be used by external apps that don't
* share `sharedUserId` with the Termux:API app.
*
* @param context The context for operations.
* @return Returns {@code errmsg} if {@link TermuxConstants#TERMUX_API_PACKAGE_NAME} is not installed
* or disabled, otherwise {@code null}.
*/
public static String isTermuxAPIAppInstalled(@NonNull final Context context) {
return PackageUtils.isAppInstalled(context, TermuxConstants.TERMUX_API_APP_NAME, TermuxConstants.TERMUX_API_PACKAGE_NAME);
}

/**
* Check if Termux app is installed and accessible. This can only be used by apps that share
* `sharedUserId` with the Termux app.
Expand Down

0 comments on commit 0cf3cef

Please sign in to comment.