Skip to content

Commit 0cf3cef

Browse files
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 ```
1 parent 7b10a35 commit 0cf3cef

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

termux-shared/src/main/java/com/termux/shared/shell/TermuxShellUtils.java

+15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class TermuxShellUtils {
2525
public static String TERMUX_APP_PID;
2626
public static String TERMUX_APK_RELEASE;
2727

28+
public static String TERMUX_API_VERSION_NAME;
29+
2830
public static String getDefaultWorkingDirectoryPath() {
2931
return TermuxConstants.TERMUX_HOME_DIR_PATH;
3032
}
@@ -52,6 +54,9 @@ public static String[] buildEnvironment(Context currentPackageContext, boolean i
5254
if (TERMUX_APK_RELEASE != null)
5355
environment.add("TERMUX_APK_RELEASE=" + TERMUX_APK_RELEASE);
5456

57+
if (TERMUX_API_VERSION_NAME != null)
58+
environment.add("TERMUX_API_VERSION=" + TERMUX_API_VERSION_NAME);
59+
5560
environment.add("TERM=xterm-256color");
5661
environment.add("COLORTERM=truecolor");
5762
environment.add("HOME=" + TermuxConstants.TERMUX_HOME_DIR_PATH);
@@ -180,6 +185,16 @@ public static void loadTermuxEnvVariables(Context currentPackageContext) {
180185
}
181186
}
182187

188+
189+
TERMUX_API_VERSION_NAME = null;
190+
191+
// Check if Termux:API app is installed and not disabled
192+
if (TermuxUtils.isTermuxAPIAppInstalled(currentPackageContext) == null) {
193+
// This function may be called by a different package like a plugin, so we get version for Termux:API package via its context
194+
Context termuxAPIPackageContext = TermuxUtils.getTermuxAPIPackageContext(currentPackageContext);
195+
if (termuxAPIPackageContext != null)
196+
TERMUX_API_VERSION_NAME = PackageUtils.getVersionNameForPackage(termuxAPIPackageContext);
197+
}
183198
}
184199

185200
}

termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java

+12
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ public static String isTermuxAppInstalled(@NonNull final Context context) {
132132
return PackageUtils.isAppInstalled(context, TermuxConstants.TERMUX_APP_NAME, TermuxConstants.TERMUX_PACKAGE_NAME);
133133
}
134134

135+
/**
136+
* Check if Termux:API app is installed and enabled. This can be used by external apps that don't
137+
* share `sharedUserId` with the Termux:API app.
138+
*
139+
* @param context The context for operations.
140+
* @return Returns {@code errmsg} if {@link TermuxConstants#TERMUX_API_PACKAGE_NAME} is not installed
141+
* or disabled, otherwise {@code null}.
142+
*/
143+
public static String isTermuxAPIAppInstalled(@NonNull final Context context) {
144+
return PackageUtils.isAppInstalled(context, TermuxConstants.TERMUX_API_APP_NAME, TermuxConstants.TERMUX_API_PACKAGE_NAME);
145+
}
146+
135147
/**
136148
* Check if Termux app is installed and accessible. This can only be used by apps that share
137149
* `sharedUserId` with the Termux app.

0 commit comments

Comments
 (0)