diff --git a/termux-shared/src/main/java/com/termux/shared/shell/TermuxShellUtils.java b/termux-shared/src/main/java/com/termux/shared/shell/TermuxShellUtils.java index aa9ba9e94a..c41ca55c46 100644 --- a/termux-shared/src/main/java/com/termux/shared/shell/TermuxShellUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/shell/TermuxShellUtils.java @@ -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 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); @@ -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); + } } } diff --git a/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java b/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java index 414f22bc35..0b5fbf82e4 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java @@ -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.