Added: Add TERMUX_API_VERSION to termux shell environment

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
```
This commit is contained in:
agnostic-apollo
2021-09-08 11:24:11 +05:00
parent 7b10a35f24
commit 0cf3cef7de
2 changed files with 27 additions and 0 deletions

View File

@@ -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;
}
@@ -52,6 +54,9 @@ public class TermuxShellUtils {
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);
@@ -180,6 +185,16 @@ public class TermuxShellUtils {
}
}
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);
}
}
}

View File

@@ -132,6 +132,18 @@ public class TermuxUtils {
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.