Skip to content

Commit 7b10a35

Browse files
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 da07826.
1 parent e36c529 commit 7b10a35

File tree

6 files changed

+92
-49
lines changed

6 files changed

+92
-49
lines changed

termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java

+44-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static int getTargetSDKForPackage(@NonNull final Context context) {
130130
* @param context The {@link Context} for the package.
131131
* @return Returns the {@code versionName}. This will be {@code null} if an exception is raised.
132132
*/
133-
public static Boolean isAppForPackageADebugBuild(@NonNull final Context context) {
133+
public static Boolean isAppForPackageADebuggableBuild(@NonNull final Context context) {
134134
return ( 0 != ( context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
135135
}
136136

@@ -275,4 +275,47 @@ public static String getPackagePID(final Context context, String packageName) {
275275
return null;
276276
}
277277

278+
/**
279+
* Check if app is installed and enabled. This can be used by external apps that don't
280+
* share `sharedUserId` with the an app.
281+
*
282+
* If your third-party app is targeting sdk `30` (android `11`), then it needs to add package
283+
* name to the `queries` element or request `QUERY_ALL_PACKAGES` permission in its
284+
* `AndroidManifest.xml`. Otherwise it will get `PackageSetting{...... package_name/......} BLOCKED`
285+
* errors in `logcat` and `RUN_COMMAND` won't work.
286+
* Check [package-visibility](https://developer.android.com/training/basics/intents/package-visibility#package-name),
287+
* `QUERY_ALL_PACKAGES` [googleplay policy](https://support.google.com/googleplay/android-developer/answer/10158779
288+
* and this [article](https://medium.com/androiddevelopers/working-with-package-visibility-dc252829de2d) for more info.
289+
*
290+
* {@code
291+
* <manifest
292+
* <queries>
293+
* <package android:name="package_name" />
294+
* </queries>
295+
* </manifest>
296+
* }
297+
*
298+
* @param context The context for operations.
299+
* @return Returns {@code errmsg} if {@code packageName} is not installed or disabled, otherwise {@code null}.
300+
*/
301+
public static String isAppInstalled(@NonNull final Context context, String appName, String packageName) {
302+
String errmsg = null;
303+
304+
PackageManager packageManager = context.getPackageManager();
305+
306+
ApplicationInfo applicationInfo;
307+
try {
308+
applicationInfo = packageManager.getApplicationInfo(packageName, 0);
309+
} catch (final PackageManager.NameNotFoundException e) {
310+
applicationInfo = null;
311+
}
312+
boolean isAppEnabled = (applicationInfo != null && applicationInfo.enabled);
313+
314+
// If app is not installed or is disabled
315+
if (!isAppEnabled)
316+
errmsg = context.getString(R.string.error_app_not_installed_or_disabled_warning, appName, packageName);
317+
318+
return errmsg;
319+
}
320+
278321
}

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

+29-19
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
public class TermuxShellUtils {
2222

2323
public static String TERMUX_VERSION_NAME;
24-
public static String TERMUX_IS_DEBUG_BUILD;
25-
public static String TERMUX_APK_RELEASE;
24+
public static String TERMUX_IS_DEBUGGABLE_BUILD;
2625
public static String TERMUX_APP_PID;
26+
public static String TERMUX_APK_RELEASE;
2727

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

4646
if (TERMUX_VERSION_NAME != null)
4747
environment.add("TERMUX_VERSION=" + TERMUX_VERSION_NAME);
48-
if (TERMUX_IS_DEBUG_BUILD != null)
49-
environment.add("TERMUX_IS_DEBUG_BUILD=" + TERMUX_IS_DEBUG_BUILD);
50-
if (TERMUX_APK_RELEASE != null)
51-
environment.add("TERMUX_APK_RELEASE=" + TERMUX_APK_RELEASE);
48+
if (TERMUX_IS_DEBUGGABLE_BUILD != null)
49+
environment.add("TERMUX_IS_DEBUGGABLE_BUILD=" + TERMUX_IS_DEBUGGABLE_BUILD);
5250
if (TERMUX_APP_PID != null)
5351
environment.add("TERMUX_APP_PID=" + TERMUX_APP_PID);
52+
if (TERMUX_APK_RELEASE != null)
53+
environment.add("TERMUX_APK_RELEASE=" + TERMUX_APK_RELEASE);
5454

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

158158
public static void loadTermuxEnvVariables(Context currentPackageContext) {
159-
TERMUX_VERSION_NAME = TERMUX_IS_DEBUG_BUILD = TERMUX_APK_RELEASE = TERMUX_APP_PID = null;
160-
161-
// This function may be called by a different package like a plugin, so we get version for Termux package via its context
162-
Context termuxPackageContext = TermuxUtils.getTermuxPackageContext(currentPackageContext);
163-
if (termuxPackageContext != null) {
164-
TERMUX_VERSION_NAME = PackageUtils.getVersionNameForPackage(termuxPackageContext);
165-
TERMUX_IS_DEBUG_BUILD = PackageUtils.isAppForPackageADebugBuild(termuxPackageContext) ? "1" : "0";
166-
167-
String signingCertificateSHA256Digest = PackageUtils.getSigningCertificateSHA256DigestForPackage(termuxPackageContext);
168-
if (signingCertificateSHA256Digest != null)
169-
TERMUX_APK_RELEASE = TermuxUtils.getAPKRelease(signingCertificateSHA256Digest).replaceAll("[^a-zA-Z]", "_").toUpperCase();
170-
171-
TERMUX_APP_PID = TermuxUtils.getTermuxAppPID(currentPackageContext);
159+
String termuxAPKReleaseOld = TERMUX_APK_RELEASE;
160+
TERMUX_VERSION_NAME = TERMUX_IS_DEBUGGABLE_BUILD = TERMUX_APP_PID = TERMUX_APK_RELEASE = null;
161+
162+
// Check if Termux app is installed and not disabled
163+
if (TermuxUtils.isTermuxAppInstalled(currentPackageContext) == null) {
164+
// This function may be called by a different package like a plugin, so we get version for Termux package via its context
165+
Context termuxPackageContext = TermuxUtils.getTermuxPackageContext(currentPackageContext);
166+
if (termuxPackageContext != null) {
167+
TERMUX_VERSION_NAME = PackageUtils.getVersionNameForPackage(termuxPackageContext);
168+
TERMUX_IS_DEBUGGABLE_BUILD = PackageUtils.isAppForPackageADebuggableBuild(termuxPackageContext) ? "1" : "0";
169+
170+
TERMUX_APP_PID = TermuxUtils.getTermuxAppPID(currentPackageContext);
171+
172+
// Getting APK signature is a slightly expensive operation, so do it only when needed
173+
if (termuxAPKReleaseOld == null) {
174+
String signingCertificateSHA256Digest = PackageUtils.getSigningCertificateSHA256DigestForPackage(termuxPackageContext);
175+
if (signingCertificateSHA256Digest != null)
176+
TERMUX_APK_RELEASE = TermuxUtils.getAPKRelease(signingCertificateSHA256Digest).replaceAll("[^a-zA-Z]", "_").toUpperCase();
177+
} else {
178+
TERMUX_APK_RELEASE = termuxAPKReleaseOld;
179+
}
180+
}
172181
}
182+
173183
}
174184

175185
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static String getAppInfoMarkdownString(@NonNull final Context context) {
3838
AndroidUtils.appendPropertyToMarkdown(markdownString,"VERSION_NAME", PackageUtils.getVersionNameForPackage(context));
3939
AndroidUtils.appendPropertyToMarkdown(markdownString,"VERSION_CODE", PackageUtils.getVersionCodeForPackage(context));
4040
AndroidUtils.appendPropertyToMarkdown(markdownString,"TARGET_SDK", PackageUtils.getTargetSDKForPackage(context));
41-
AndroidUtils.appendPropertyToMarkdown(markdownString,"IS_DEBUG_BUILD", PackageUtils.isAppForPackageADebugBuild(context));
41+
AndroidUtils.appendPropertyToMarkdown(markdownString,"IS_DEBUGGABLE_BUILD", PackageUtils.isAppForPackageADebuggableBuild(context));
4242

4343
if (PackageUtils.isAppInstalledOnExternalStorage(context)) {
4444
AndroidUtils.appendPropertyToMarkdown(markdownString,"IS_INSTALLED_ON_EXTERNAL_STORAGE", true);

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.List;
1313

1414
/*
15-
* Version: v0.29.0
15+
* Version: v0.30.0
1616
*
1717
* Changelog
1818
*
@@ -198,6 +198,11 @@
198198
* `ACTION_WIDGET_ITEM_CLICKED`, `ACTION_REFRESH_WIDGET`, `EXTRA_FILE_CLICKED`.
199199
* - Changed naming convention of `TERMUX_FLOAT_APP.TERMUX_FLOAT_SERVICE.ACTION_*`.
200200
* - Fixed wrong path set for `TERMUX_SHORTCUT_SCRIPTS_DIR_PATH`.
201+
*
202+
* - 0.30.0 (2021-09-08)
203+
* - Changed `APK_RELEASE_GITHUB_DEBUG_BUILD`to `APK_RELEASE_GITHUB` and
204+
* `APK_RELEASE_GITHUB_DEBUG_BUILD_SIGNING_CERTIFICATE_SHA256_DIGEST` to
205+
* `APK_RELEASE_GITHUB_SIGNING_CERTIFICATE_SHA256_DIGEST`.
201206
*/
202207

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

400-
/** Github Debug Build APK release */
401-
public static final String APK_RELEASE_GITHUB_DEBUG_BUILD = "Github Debug Build"; // Default: "Github Debug Build"
405+
/** Github APK release */
406+
public static final String APK_RELEASE_GITHUB = "Github"; // Default: "Github"
402407

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

406411
/** Google Play Store APK release */
407412
public static final String APK_RELEASE_GOOGLE_PLAYSTORE = "Google Play Store"; // Default: "Google Play Store"

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

+7-22
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,12 @@ public static Context getTermuxWidgetPackageContext(@NonNull Context context) {
124124
* </manifest>
125125
* }
126126
*
127-
* @param currentPackageContext The context of current package.
128-
* @return Returns {@code errmsg} if termux package is not installed or disabled, otherwise {@code null}.
127+
* @param context The context for operations.
128+
* @return Returns {@code errmsg} if {@link TermuxConstants#TERMUX_PACKAGE_NAME} is not installed
129+
* or disabled, otherwise {@code null}.
129130
*/
130-
public static String isTermuxAppInstalled(@NonNull final Context currentPackageContext) {
131-
String errmsg = null;
132-
133-
PackageManager packageManager = currentPackageContext.getPackageManager();
134-
135-
ApplicationInfo applicationInfo;
136-
try {
137-
applicationInfo = packageManager.getApplicationInfo(TermuxConstants.TERMUX_PACKAGE_NAME, 0);
138-
} catch (final PackageManager.NameNotFoundException e) {
139-
applicationInfo = null;
140-
}
141-
boolean termuxAppEnabled = (applicationInfo != null && applicationInfo.enabled);
142-
143-
// If Termux app is not installed or is disabled
144-
if (!termuxAppEnabled)
145-
errmsg = currentPackageContext.getString(R.string.error_termux_app_not_installed_or_disabled_warning);
146-
147-
return errmsg;
131+
public static String isTermuxAppInstalled(@NonNull final Context context) {
132+
return PackageUtils.isAppInstalled(context, TermuxConstants.TERMUX_APP_NAME, TermuxConstants.TERMUX_PACKAGE_NAME);
148133
}
149134

150135
/**
@@ -524,8 +509,8 @@ public static String getAPKRelease(String signingCertificateSHA256Digest) {
524509
switch (signingCertificateSHA256Digest.toUpperCase()) {
525510
case TermuxConstants.APK_RELEASE_FDROID_SIGNING_CERTIFICATE_SHA256_DIGEST:
526511
return TermuxConstants.APK_RELEASE_FDROID;
527-
case TermuxConstants.APK_RELEASE_GITHUB_DEBUG_BUILD_SIGNING_CERTIFICATE_SHA256_DIGEST:
528-
return TermuxConstants.APK_RELEASE_GITHUB_DEBUG_BUILD;
512+
case TermuxConstants.APK_RELEASE_GITHUB_SIGNING_CERTIFICATE_SHA256_DIGEST:
513+
return TermuxConstants.APK_RELEASE_GITHUB;
529514
case TermuxConstants.APK_RELEASE_GOOGLE_PLAYSTORE_SIGNING_CERTIFICATE_SHA256_DIGEST:
530515
return TermuxConstants.APK_RELEASE_GOOGLE_PLAYSTORE;
531516
default:

termux-shared/src/main/res/values/strings.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

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

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

0 commit comments

Comments
 (0)