Added: Add support to get termux app package context with code classloader for plugin usage

This commit is contained in:
agnostic-apollo 2022-06-01 00:06:05 +05:00
parent 231ecff5f0
commit 980bf8f0ae
2 changed files with 29 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -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.
*