mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-07 19:14:04 +08:00
Added: Add support to get termux app package context with code classloader for plugin usage
This commit is contained in:
@@ -31,7 +31,7 @@ public class PackageUtils {
|
|||||||
private static final String LOG_TAG = "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 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 packageName The package name whose {@link Context} to get.
|
||||||
@@ -39,10 +39,23 @@ public class PackageUtils {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static Context getContextForPackage(@NonNull final Context context, String packageName) {
|
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 {
|
try {
|
||||||
return context.createPackageContext(packageName, Context.CONTEXT_RESTRICTED);
|
return context.createPackageContext(packageName, flags);
|
||||||
} catch (Exception e) {
|
} 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;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -50,7 +50,8 @@ public class TermuxUtils {
|
|||||||
private static final String LOG_TAG = "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.
|
* @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.
|
* @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);
|
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.
|
* Get the {@link Context} for {@link TermuxConstants#TERMUX_API_PACKAGE_NAME} package.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user