Changed!: Change TERMUX_IS_DEBUG_BUILD env variable name to TERMUX_IS_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 da07826a.
This commit is contained in:
agnostic-apollo
2021-09-08 10:59:56 +05:00
parent e36c5294db
commit 7b10a35f24
6 changed files with 89 additions and 46 deletions

View File

@@ -130,7 +130,7 @@ public class PackageUtils {
* @param context The {@link Context} for the package. * @param context The {@link Context} for the package.
* @return Returns the {@code versionName}. This will be {@code null} if an exception is raised. * @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 ) ); return ( 0 != ( context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
} }
@@ -275,4 +275,47 @@ public class PackageUtils {
return null; 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;
}
} }

View File

@@ -21,9 +21,9 @@ import java.util.List;
public class TermuxShellUtils { public class TermuxShellUtils {
public static String TERMUX_VERSION_NAME; public static String TERMUX_VERSION_NAME;
public static String TERMUX_IS_DEBUG_BUILD; public static String TERMUX_IS_DEBUGGABLE_BUILD;
public static String TERMUX_APK_RELEASE;
public static String TERMUX_APP_PID; public static String TERMUX_APP_PID;
public static String TERMUX_APK_RELEASE;
public static String getDefaultWorkingDirectoryPath() { public static String getDefaultWorkingDirectoryPath() {
return TermuxConstants.TERMUX_HOME_DIR_PATH; return TermuxConstants.TERMUX_HOME_DIR_PATH;
@@ -45,12 +45,12 @@ public class TermuxShellUtils {
if (TERMUX_VERSION_NAME != null) if (TERMUX_VERSION_NAME != null)
environment.add("TERMUX_VERSION=" + TERMUX_VERSION_NAME); environment.add("TERMUX_VERSION=" + TERMUX_VERSION_NAME);
if (TERMUX_IS_DEBUG_BUILD != null) if (TERMUX_IS_DEBUGGABLE_BUILD != null)
environment.add("TERMUX_IS_DEBUG_BUILD=" + TERMUX_IS_DEBUG_BUILD); environment.add("TERMUX_IS_DEBUGGABLE_BUILD=" + TERMUX_IS_DEBUGGABLE_BUILD);
if (TERMUX_APK_RELEASE != null)
environment.add("TERMUX_APK_RELEASE=" + TERMUX_APK_RELEASE);
if (TERMUX_APP_PID != null) if (TERMUX_APP_PID != null)
environment.add("TERMUX_APP_PID=" + TERMUX_APP_PID); 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("TERM=xterm-256color");
environment.add("COLORTERM=truecolor"); environment.add("COLORTERM=truecolor");
@@ -156,20 +156,30 @@ public class TermuxShellUtils {
} }
public static void loadTermuxEnvVariables(Context currentPackageContext) { public static void loadTermuxEnvVariables(Context currentPackageContext) {
TERMUX_VERSION_NAME = TERMUX_IS_DEBUG_BUILD = TERMUX_APK_RELEASE = TERMUX_APP_PID = null; String termuxAPKReleaseOld = TERMUX_APK_RELEASE;
TERMUX_VERSION_NAME = TERMUX_IS_DEBUGGABLE_BUILD = TERMUX_APP_PID = TERMUX_APK_RELEASE = null;
// This function may be called by a different package like a plugin, so we get version for Termux package via its context // Check if Termux app is installed and not disabled
Context termuxPackageContext = TermuxUtils.getTermuxPackageContext(currentPackageContext); if (TermuxUtils.isTermuxAppInstalled(currentPackageContext) == null) {
if (termuxPackageContext != null) { // This function may be called by a different package like a plugin, so we get version for Termux package via its context
TERMUX_VERSION_NAME = PackageUtils.getVersionNameForPackage(termuxPackageContext); Context termuxPackageContext = TermuxUtils.getTermuxPackageContext(currentPackageContext);
TERMUX_IS_DEBUG_BUILD = PackageUtils.isAppForPackageADebugBuild(termuxPackageContext) ? "1" : "0"; if (termuxPackageContext != null) {
TERMUX_VERSION_NAME = PackageUtils.getVersionNameForPackage(termuxPackageContext);
TERMUX_IS_DEBUGGABLE_BUILD = PackageUtils.isAppForPackageADebuggableBuild(termuxPackageContext) ? "1" : "0";
String signingCertificateSHA256Digest = PackageUtils.getSigningCertificateSHA256DigestForPackage(termuxPackageContext); TERMUX_APP_PID = TermuxUtils.getTermuxAppPID(currentPackageContext);
if (signingCertificateSHA256Digest != null)
TERMUX_APK_RELEASE = TermuxUtils.getAPKRelease(signingCertificateSHA256Digest).replaceAll("[^a-zA-Z]", "_").toUpperCase();
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;
}
}
} }
} }
} }

View File

@@ -38,7 +38,7 @@ public class AndroidUtils {
AndroidUtils.appendPropertyToMarkdown(markdownString,"VERSION_NAME", PackageUtils.getVersionNameForPackage(context)); AndroidUtils.appendPropertyToMarkdown(markdownString,"VERSION_NAME", PackageUtils.getVersionNameForPackage(context));
AndroidUtils.appendPropertyToMarkdown(markdownString,"VERSION_CODE", PackageUtils.getVersionCodeForPackage(context)); AndroidUtils.appendPropertyToMarkdown(markdownString,"VERSION_CODE", PackageUtils.getVersionCodeForPackage(context));
AndroidUtils.appendPropertyToMarkdown(markdownString,"TARGET_SDK", PackageUtils.getTargetSDKForPackage(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)) { if (PackageUtils.isAppInstalledOnExternalStorage(context)) {
AndroidUtils.appendPropertyToMarkdown(markdownString,"IS_INSTALLED_ON_EXTERNAL_STORAGE", true); AndroidUtils.appendPropertyToMarkdown(markdownString,"IS_INSTALLED_ON_EXTERNAL_STORAGE", true);

View File

@@ -12,7 +12,7 @@ import java.util.IllegalFormatException;
import java.util.List; import java.util.List;
/* /*
* Version: v0.29.0 * Version: v0.30.0
* *
* Changelog * Changelog
* *
@@ -198,6 +198,11 @@ import java.util.List;
* `ACTION_WIDGET_ITEM_CLICKED`, `ACTION_REFRESH_WIDGET`, `EXTRA_FILE_CLICKED`. * `ACTION_WIDGET_ITEM_CLICKED`, `ACTION_REFRESH_WIDGET`, `EXTRA_FILE_CLICKED`.
* - Changed naming convention of `TERMUX_FLOAT_APP.TERMUX_FLOAT_SERVICE.ACTION_*`. * - Changed naming convention of `TERMUX_FLOAT_APP.TERMUX_FLOAT_SERVICE.ACTION_*`.
* - Fixed wrong path set for `TERMUX_SHORTCUT_SCRIPTS_DIR_PATH`. * - 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`.
*/ */
/** /**
@@ -397,11 +402,11 @@ public final class TermuxConstants {
/** F-Droid APK release signing certificate SHA-256 digest */ /** F-Droid APK release signing certificate SHA-256 digest */
public static final String APK_RELEASE_FDROID_SIGNING_CERTIFICATE_SHA256_DIGEST = "228FB2CFE90831C1499EC3CCAF61E96E8E1CE70766B9474672CE427334D41C42"; // Default: "228FB2CFE90831C1499EC3CCAF61E96E8E1CE70766B9474672CE427334D41C42" public static final String APK_RELEASE_FDROID_SIGNING_CERTIFICATE_SHA256_DIGEST = "228FB2CFE90831C1499EC3CCAF61E96E8E1CE70766B9474672CE427334D41C42"; // Default: "228FB2CFE90831C1499EC3CCAF61E96E8E1CE70766B9474672CE427334D41C42"
/** Github Debug Build APK release */ /** Github APK release */
public static final String APK_RELEASE_GITHUB_DEBUG_BUILD = "Github Debug Build"; // Default: "Github Debug Build" public static final String APK_RELEASE_GITHUB = "Github"; // Default: "Github"
/** Github Debug Build APK release signing certificate SHA-256 digest */ /** Github APK release signing certificate SHA-256 digest */
public static final String APK_RELEASE_GITHUB_DEBUG_BUILD_SIGNING_CERTIFICATE_SHA256_DIGEST = "B6DA01480EEFD5FBF2CD3771B8D1021EC791304BDD6C4BF41D3FAABAD48EE5E1"; // Default: "B6DA01480EEFD5FBF2CD3771B8D1021EC791304BDD6C4BF41D3FAABAD48EE5E1" public static final String APK_RELEASE_GITHUB_SIGNING_CERTIFICATE_SHA256_DIGEST = "B6DA01480EEFD5FBF2CD3771B8D1021EC791304BDD6C4BF41D3FAABAD48EE5E1"; // Default: "B6DA01480EEFD5FBF2CD3771B8D1021EC791304BDD6C4BF41D3FAABAD48EE5E1"
/** Google Play Store APK release */ /** Google Play Store APK release */
public static final String APK_RELEASE_GOOGLE_PLAYSTORE = "Google Play Store"; // Default: "Google Play Store" public static final String APK_RELEASE_GOOGLE_PLAYSTORE = "Google Play Store"; // Default: "Google Play Store"

View File

@@ -124,27 +124,12 @@ public class TermuxUtils {
* </manifest> * </manifest>
* } * }
* *
* @param currentPackageContext The context of current package. * @param context The context for operations.
* @return Returns {@code errmsg} if termux package is not installed or disabled, otherwise {@code null}. * @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) { public static String isTermuxAppInstalled(@NonNull final Context context) {
String errmsg = null; return PackageUtils.isAppInstalled(context, TermuxConstants.TERMUX_APP_NAME, TermuxConstants.TERMUX_PACKAGE_NAME);
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;
} }
/** /**
@@ -524,8 +509,8 @@ public class TermuxUtils {
switch (signingCertificateSHA256Digest.toUpperCase()) { switch (signingCertificateSHA256Digest.toUpperCase()) {
case TermuxConstants.APK_RELEASE_FDROID_SIGNING_CERTIFICATE_SHA256_DIGEST: case TermuxConstants.APK_RELEASE_FDROID_SIGNING_CERTIFICATE_SHA256_DIGEST:
return TermuxConstants.APK_RELEASE_FDROID; return TermuxConstants.APK_RELEASE_FDROID;
case TermuxConstants.APK_RELEASE_GITHUB_DEBUG_BUILD_SIGNING_CERTIFICATE_SHA256_DIGEST: case TermuxConstants.APK_RELEASE_GITHUB_SIGNING_CERTIFICATE_SHA256_DIGEST:
return TermuxConstants.APK_RELEASE_GITHUB_DEBUG_BUILD; return TermuxConstants.APK_RELEASE_GITHUB;
case TermuxConstants.APK_RELEASE_GOOGLE_PLAYSTORE_SIGNING_CERTIFICATE_SHA256_DIGEST: case TermuxConstants.APK_RELEASE_GOOGLE_PLAYSTORE_SIGNING_CERTIFICATE_SHA256_DIGEST:
return TermuxConstants.APK_RELEASE_GOOGLE_PLAYSTORE; return TermuxConstants.APK_RELEASE_GOOGLE_PLAYSTORE;
default: default:

View File

@@ -19,6 +19,7 @@
<!-- PackageUtils --> <!-- 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_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. <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. This may be because the app package is not installed or it has different APK signature from the current app.
@@ -74,7 +75,6 @@
even result in **temporary or permanent** ban. Check %1$s/wiki/Hacking for details.</string> 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="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_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. <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 This may be because you have not installed or setup &TERMUX_APP_NAME; app or