diff --git a/termux-shared/src/main/java/com/termux/shared/android/PackageUtils.java b/termux-shared/src/main/java/com/termux/shared/android/PackageUtils.java index 4447a921..ded3c01f 100644 --- a/termux-shared/src/main/java/com/termux/shared/android/PackageUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/android/PackageUtils.java @@ -31,7 +31,7 @@ public class PackageUtils { private static final String LOG_TAG = "PackageUtils"; /** - * Get the {@link Context} for the package name. + * Get the {@link Context} for the package name with {@link Context#CONTEXT_RESTRICTED} flags. * * @param context The {@link Context} to use to get the {@link Context} of the {@code packageName}. * @param packageName The package name whose {@link Context} to get. @@ -39,10 +39,23 @@ public class PackageUtils { */ @Nullable public static Context getContextForPackage(@NonNull final Context context, String packageName) { + return getContextForPackage(context, packageName, Context.CONTEXT_RESTRICTED); + } + + /** + * Get the {@link Context} for the package name. + * + * @param context The {@link Context} to use to get the {@link Context} of the {@code packageName}. + * @param packageName The package name whose {@link Context} to get. + * @param flags The flags for {@link Context} type. + * @return Returns the {@link Context}. This will {@code null} if an exception is raised. + */ + @Nullable + public static Context getContextForPackage(@NonNull final Context context, String packageName, int flags) { try { - return context.createPackageContext(packageName, Context.CONTEXT_RESTRICTED); + return context.createPackageContext(packageName, flags); } catch (Exception e) { - Logger.logVerbose(LOG_TAG, "Failed to get \"" + packageName + "\" package context: " + e.getMessage()); + Logger.logVerbose(LOG_TAG, "Failed to get \"" + packageName + "\" package context with flags " + flags + ": " + e.getMessage()); return null; } } 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 cd6bb8d3..cc60b1cf 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 @@ -50,7 +50,8 @@ public class TermuxUtils { private static final String LOG_TAG = "TermuxUtils"; /** - * Get the {@link Context} for {@link TermuxConstants#TERMUX_PACKAGE_NAME} package. + * Get the {@link Context} for {@link TermuxConstants#TERMUX_PACKAGE_NAME} package with the + * {@link Context#CONTEXT_RESTRICTED} flag. * * @param context The {@link Context} to use to get the {@link Context} of the package. * @return Returns the {@link Context}. This will {@code null} if an exception is raised. @@ -59,6 +60,17 @@ public class TermuxUtils { return PackageUtils.getContextForPackage(context, TermuxConstants.TERMUX_PACKAGE_NAME); } + /** + * Get the {@link Context} for {@link TermuxConstants#TERMUX_PACKAGE_NAME} package with the + * {@link Context#CONTEXT_INCLUDE_CODE} flag. + * + * @param context The {@link Context} to use to get the {@link Context} of the package. + * @return Returns the {@link Context}. This will {@code null} if an exception is raised. + */ + public static Context getTermuxPackageContextWithCode(@NonNull Context context) { + return PackageUtils.getContextForPackage(context, TermuxConstants.TERMUX_PACKAGE_NAME, Context.CONTEXT_INCLUDE_CODE); + } + /** * Get the {@link Context} for {@link TermuxConstants#TERMUX_API_PACKAGE_NAME} package. *