Skip to content

Commit

Permalink
Changed!: Change TERMUX_IS_DEBUG_BUILD env variable name to TERMUX_IS…
Browse files Browse the repository at this point in the history
…_DEBUGGABLE_BUILD and change GITHUB_DEBUG_BUILD release type to just GITHUB

This is being done since github release artifacts may be converted to non-debuggable if felt appropriate in future or at least is a more appropriate name. Signing keys can stay same as per commit/push builds. Currently, no changes are planned, just future proofing. The `TERMUX_IS_DEBUGGABLE_BUILD` env variable could be used to differentiate if needed.

Will also check if Termux app is installed and not disabled and will calculate APK signature only when needed since its a slightly expensive operation.

This commit breaks da07826.
  • Loading branch information
agnostic-apollo committed Sep 8, 2021
1 parent e36c529 commit 7b10a35
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static int getTargetSDKForPackage(@NonNull final Context context) {
* @param context The {@link Context} for the package.
* @return Returns the {@code versionName}. This will be {@code null} if an exception is raised.
*/
public static Boolean isAppForPackageADebugBuild(@NonNull final Context context) {
public static Boolean isAppForPackageADebuggableBuild(@NonNull final Context context) {
return ( 0 != ( context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
}

Expand Down Expand Up @@ -275,4 +275,47 @@ public static String getPackagePID(final Context context, String packageName) {
return null;
}

/**
* Check if app is installed and enabled. This can be used by external apps that don't
* share `sharedUserId` with the an app.
*
* If your third-party app is targeting sdk `30` (android `11`), then it needs to add package
* name to the `queries` element or request `QUERY_ALL_PACKAGES` permission in its
* `AndroidManifest.xml`. Otherwise it will get `PackageSetting{...... package_name/......} BLOCKED`
* errors in `logcat` and `RUN_COMMAND` won't work.
* Check [package-visibility](https://developer.android.com/training/basics/intents/package-visibility#package-name),
* `QUERY_ALL_PACKAGES` [googleplay policy](https://support.google.com/googleplay/android-developer/answer/10158779
* and this [article](https://medium.com/androiddevelopers/working-with-package-visibility-dc252829de2d) for more info.
*
* {@code
* <manifest
* <queries>
* <package android:name="package_name" />
* </queries>
* </manifest>
* }
*
* @param context The context for operations.
* @return Returns {@code errmsg} if {@code packageName} is not installed or disabled, otherwise {@code null}.
*/
public static String isAppInstalled(@NonNull final Context context, String appName, String packageName) {
String errmsg = null;

PackageManager packageManager = context.getPackageManager();

ApplicationInfo applicationInfo;
try {
applicationInfo = packageManager.getApplicationInfo(packageName, 0);
} catch (final PackageManager.NameNotFoundException e) {
applicationInfo = null;
}
boolean isAppEnabled = (applicationInfo != null && applicationInfo.enabled);

// If app is not installed or is disabled
if (!isAppEnabled)
errmsg = context.getString(R.string.error_app_not_installed_or_disabled_warning, appName, packageName);

return errmsg;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
public class TermuxShellUtils {

public static String TERMUX_VERSION_NAME;
public static String TERMUX_IS_DEBUG_BUILD;
public static String TERMUX_APK_RELEASE;
public static String TERMUX_IS_DEBUGGABLE_BUILD;
public static String TERMUX_APP_PID;
public static String TERMUX_APK_RELEASE;

public static String getDefaultWorkingDirectoryPath() {
return TermuxConstants.TERMUX_HOME_DIR_PATH;
Expand All @@ -45,12 +45,12 @@ public static String[] buildEnvironment(Context currentPackageContext, boolean i

if (TERMUX_VERSION_NAME != null)
environment.add("TERMUX_VERSION=" + TERMUX_VERSION_NAME);
if (TERMUX_IS_DEBUG_BUILD != null)
environment.add("TERMUX_IS_DEBUG_BUILD=" + TERMUX_IS_DEBUG_BUILD);
if (TERMUX_APK_RELEASE != null)
environment.add("TERMUX_APK_RELEASE=" + TERMUX_APK_RELEASE);
if (TERMUX_IS_DEBUGGABLE_BUILD != null)
environment.add("TERMUX_IS_DEBUGGABLE_BUILD=" + TERMUX_IS_DEBUGGABLE_BUILD);
if (TERMUX_APP_PID != null)
environment.add("TERMUX_APP_PID=" + TERMUX_APP_PID);
if (TERMUX_APK_RELEASE != null)
environment.add("TERMUX_APK_RELEASE=" + TERMUX_APK_RELEASE);

environment.add("TERM=xterm-256color");
environment.add("COLORTERM=truecolor");
Expand Down Expand Up @@ -156,20 +156,30 @@ public static void clearTermuxTMPDIR(boolean onlyIfExists) {
}

public static void loadTermuxEnvVariables(Context currentPackageContext) {
TERMUX_VERSION_NAME = TERMUX_IS_DEBUG_BUILD = TERMUX_APK_RELEASE = TERMUX_APP_PID = null;

// This function may be called by a different package like a plugin, so we get version for Termux package via its context
Context termuxPackageContext = TermuxUtils.getTermuxPackageContext(currentPackageContext);
if (termuxPackageContext != null) {
TERMUX_VERSION_NAME = PackageUtils.getVersionNameForPackage(termuxPackageContext);
TERMUX_IS_DEBUG_BUILD = PackageUtils.isAppForPackageADebugBuild(termuxPackageContext) ? "1" : "0";

String signingCertificateSHA256Digest = PackageUtils.getSigningCertificateSHA256DigestForPackage(termuxPackageContext);
if (signingCertificateSHA256Digest != null)
TERMUX_APK_RELEASE = TermuxUtils.getAPKRelease(signingCertificateSHA256Digest).replaceAll("[^a-zA-Z]", "_").toUpperCase();

TERMUX_APP_PID = TermuxUtils.getTermuxAppPID(currentPackageContext);
String termuxAPKReleaseOld = TERMUX_APK_RELEASE;
TERMUX_VERSION_NAME = TERMUX_IS_DEBUGGABLE_BUILD = TERMUX_APP_PID = TERMUX_APK_RELEASE = null;

// Check if Termux app is installed and not disabled
if (TermuxUtils.isTermuxAppInstalled(currentPackageContext) == null) {
// This function may be called by a different package like a plugin, so we get version for Termux package via its context
Context termuxPackageContext = TermuxUtils.getTermuxPackageContext(currentPackageContext);
if (termuxPackageContext != null) {
TERMUX_VERSION_NAME = PackageUtils.getVersionNameForPackage(termuxPackageContext);
TERMUX_IS_DEBUGGABLE_BUILD = PackageUtils.isAppForPackageADebuggableBuild(termuxPackageContext) ? "1" : "0";

TERMUX_APP_PID = TermuxUtils.getTermuxAppPID(currentPackageContext);

// Getting APK signature is a slightly expensive operation, so do it only when needed
if (termuxAPKReleaseOld == null) {
String signingCertificateSHA256Digest = PackageUtils.getSigningCertificateSHA256DigestForPackage(termuxPackageContext);
if (signingCertificateSHA256Digest != null)
TERMUX_APK_RELEASE = TermuxUtils.getAPKRelease(signingCertificateSHA256Digest).replaceAll("[^a-zA-Z]", "_").toUpperCase();
} else {
TERMUX_APK_RELEASE = termuxAPKReleaseOld;
}
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static String getAppInfoMarkdownString(@NonNull final Context context) {
AndroidUtils.appendPropertyToMarkdown(markdownString,"VERSION_NAME", PackageUtils.getVersionNameForPackage(context));
AndroidUtils.appendPropertyToMarkdown(markdownString,"VERSION_CODE", PackageUtils.getVersionCodeForPackage(context));
AndroidUtils.appendPropertyToMarkdown(markdownString,"TARGET_SDK", PackageUtils.getTargetSDKForPackage(context));
AndroidUtils.appendPropertyToMarkdown(markdownString,"IS_DEBUG_BUILD", PackageUtils.isAppForPackageADebugBuild(context));
AndroidUtils.appendPropertyToMarkdown(markdownString,"IS_DEBUGGABLE_BUILD", PackageUtils.isAppForPackageADebuggableBuild(context));

if (PackageUtils.isAppInstalledOnExternalStorage(context)) {
AndroidUtils.appendPropertyToMarkdown(markdownString,"IS_INSTALLED_ON_EXTERNAL_STORAGE", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.List;

/*
* Version: v0.29.0
* Version: v0.30.0
*
* Changelog
*
Expand Down Expand Up @@ -198,6 +198,11 @@
* `ACTION_WIDGET_ITEM_CLICKED`, `ACTION_REFRESH_WIDGET`, `EXTRA_FILE_CLICKED`.
* - Changed naming convention of `TERMUX_FLOAT_APP.TERMUX_FLOAT_SERVICE.ACTION_*`.
* - Fixed wrong path set for `TERMUX_SHORTCUT_SCRIPTS_DIR_PATH`.
*
* - 0.30.0 (2021-09-08)
* - Changed `APK_RELEASE_GITHUB_DEBUG_BUILD`to `APK_RELEASE_GITHUB` and
* `APK_RELEASE_GITHUB_DEBUG_BUILD_SIGNING_CERTIFICATE_SHA256_DIGEST` to
* `APK_RELEASE_GITHUB_SIGNING_CERTIFICATE_SHA256_DIGEST`.
*/

/**
Expand Down Expand Up @@ -397,11 +402,11 @@ public final class TermuxConstants {
/** F-Droid APK release signing certificate SHA-256 digest */
public static final String APK_RELEASE_FDROID_SIGNING_CERTIFICATE_SHA256_DIGEST = "228FB2CFE90831C1499EC3CCAF61E96E8E1CE70766B9474672CE427334D41C42"; // Default: "228FB2CFE90831C1499EC3CCAF61E96E8E1CE70766B9474672CE427334D41C42"

/** Github Debug Build APK release */
public static final String APK_RELEASE_GITHUB_DEBUG_BUILD = "Github Debug Build"; // Default: "Github Debug Build"
/** Github APK release */
public static final String APK_RELEASE_GITHUB = "Github"; // Default: "Github"

/** Github Debug Build APK release signing certificate SHA-256 digest */
public static final String APK_RELEASE_GITHUB_DEBUG_BUILD_SIGNING_CERTIFICATE_SHA256_DIGEST = "B6DA01480EEFD5FBF2CD3771B8D1021EC791304BDD6C4BF41D3FAABAD48EE5E1"; // Default: "B6DA01480EEFD5FBF2CD3771B8D1021EC791304BDD6C4BF41D3FAABAD48EE5E1"
/** Github APK release signing certificate SHA-256 digest */
public static final String APK_RELEASE_GITHUB_SIGNING_CERTIFICATE_SHA256_DIGEST = "B6DA01480EEFD5FBF2CD3771B8D1021EC791304BDD6C4BF41D3FAABAD48EE5E1"; // Default: "B6DA01480EEFD5FBF2CD3771B8D1021EC791304BDD6C4BF41D3FAABAD48EE5E1"

/** Google Play Store APK release */
public static final String APK_RELEASE_GOOGLE_PLAYSTORE = "Google Play Store"; // Default: "Google Play Store"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,12 @@ public static Context getTermuxWidgetPackageContext(@NonNull Context context) {
* </manifest>
* }
*
* @param currentPackageContext The context of current package.
* @return Returns {@code errmsg} if termux package is not installed or disabled, otherwise {@code null}.
* @param context The context for operations.
* @return Returns {@code errmsg} if {@link TermuxConstants#TERMUX_PACKAGE_NAME} is not installed
* or disabled, otherwise {@code null}.
*/
public static String isTermuxAppInstalled(@NonNull final Context currentPackageContext) {
String errmsg = null;

PackageManager packageManager = currentPackageContext.getPackageManager();

ApplicationInfo applicationInfo;
try {
applicationInfo = packageManager.getApplicationInfo(TermuxConstants.TERMUX_PACKAGE_NAME, 0);
} catch (final PackageManager.NameNotFoundException e) {
applicationInfo = null;
}
boolean termuxAppEnabled = (applicationInfo != null && applicationInfo.enabled);

// If Termux app is not installed or is disabled
if (!termuxAppEnabled)
errmsg = currentPackageContext.getString(R.string.error_termux_app_not_installed_or_disabled_warning);

return errmsg;
public static String isTermuxAppInstalled(@NonNull final Context context) {
return PackageUtils.isAppInstalled(context, TermuxConstants.TERMUX_APP_NAME, TermuxConstants.TERMUX_PACKAGE_NAME);
}

/**
Expand Down Expand Up @@ -524,8 +509,8 @@ public static String getAPKRelease(String signingCertificateSHA256Digest) {
switch (signingCertificateSHA256Digest.toUpperCase()) {
case TermuxConstants.APK_RELEASE_FDROID_SIGNING_CERTIFICATE_SHA256_DIGEST:
return TermuxConstants.APK_RELEASE_FDROID;
case TermuxConstants.APK_RELEASE_GITHUB_DEBUG_BUILD_SIGNING_CERTIFICATE_SHA256_DIGEST:
return TermuxConstants.APK_RELEASE_GITHUB_DEBUG_BUILD;
case TermuxConstants.APK_RELEASE_GITHUB_SIGNING_CERTIFICATE_SHA256_DIGEST:
return TermuxConstants.APK_RELEASE_GITHUB;
case TermuxConstants.APK_RELEASE_GOOGLE_PLAYSTORE_SIGNING_CERTIFICATE_SHA256_DIGEST:
return TermuxConstants.APK_RELEASE_GOOGLE_PLAYSTORE;
default:
Expand Down
2 changes: 1 addition & 1 deletion termux-shared/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


<!-- PackageUtils -->
<string name="error_app_not_installed_or_disabled_warning">The %1$s (%2$s) app is not installed or is disabled."</string>
<string name="error_get_package_context_failed_title">Failed To Get Package Context</string>
<string name="error_get_package_context_failed_message">Failed to get package context for the \"%1$s\" package.
This may be because the app package is not installed or it has different APK signature from the current app.
Expand Down Expand Up @@ -74,7 +75,6 @@
even result in **temporary or permanent** ban. Check %1$s/wiki/Hacking for details.</string>

<string name="msg_termux_app_required_by_app">The &TERMUX_APP_NAME; is required by the %1$s app to run termux commands."</string>
<string name="error_termux_app_not_installed_or_disabled_warning">The &TERMUX_APP_NAME; app is not installed or is disabled."</string>
<string name="error_termux_app_package_context_not_accessible">The &TERMUX_APP_NAME; app (package context) is not accessible."</string>
<string name="error_termux_prefix_dir_path_not_accessible">The &TERMUX_APP_NAME; app $PREFIX directory is not accessible by the %1$s app.
This may be because you have not installed or setup &TERMUX_APP_NAME; app or
Expand Down

0 comments on commit 7b10a35

Please sign in to comment.