mirror of
https://github.com/nix-community/nix-on-droid-app.git
synced 2025-12-16 05:51:15 +01:00
The termux plugins should use this library instead of hardcoding "com.termux" values in their source code. The library can be included as a dependency by plugins and third party apps by including the following line in the build.gradle where x.xxx is the version number, once its published. `implementation 'com.termux:termux-shared:x.xxx'` The `TermuxConstants` class has been updated to `v0.17.0`, `TermuxPreferenceConstants` to `v0.9.0` and `TermuxPropertyConstants` to `v0.6.0`. Check their Changelog sections for info on changes. Some typos and redundant code has also been fixed.
110 lines
4 KiB
Java
110 lines
4 KiB
Java
package com.termux.shared.packages;
|
|
|
|
import android.content.Context;
|
|
import android.content.pm.ApplicationInfo;
|
|
import android.content.pm.PackageInfo;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import com.termux.shared.logger.Logger;
|
|
|
|
public class PackageUtils {
|
|
|
|
/**
|
|
* Get the {@link Context} for the package name.
|
|
*
|
|
* @param context The {@link Context} to use to get the {@link Context} of the {@code packageName}.
|
|
* @return Returns the {@link Context}. This will {@code null} if an exception is raised.
|
|
*/
|
|
public static Context getContextForPackage(@NonNull final Context context, String packageName) {
|
|
try {
|
|
return context.createPackageContext(packageName, Context.CONTEXT_RESTRICTED);
|
|
} catch (Exception e) {
|
|
Logger.logStackTraceWithMessage("Failed to get \"" + packageName + "\" package context.", e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the {@link PackageInfo} for the package associated with the {@code context}.
|
|
*
|
|
* @param context The {@link Context} for the package.
|
|
* @return Returns the {@link PackageInfo}. This will be {@code null} if an exception is raised.
|
|
*/
|
|
public static PackageInfo getPackageInfoForPackage(@NonNull final Context context) {
|
|
try {
|
|
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
|
} catch (final Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the app name for the package associated with the {@code context}.
|
|
*
|
|
* @param context The {@link Context} for the package.
|
|
* @return Returns the {@code android:name} attribute.
|
|
*/
|
|
public static String getAppNameForPackage(@NonNull final Context context) {
|
|
return context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
|
|
}
|
|
|
|
/**
|
|
* Get the package name for the package associated with the {@code context}.
|
|
*
|
|
* @param context The {@link Context} for the package.
|
|
* @return Returns the package name.
|
|
*/
|
|
public static String getPackageNameForPackage(@NonNull final Context context) {
|
|
return context.getApplicationInfo().packageName;
|
|
}
|
|
|
|
/**
|
|
* Get the {@code targetSdkVersion} for the package associated with the {@code context}.
|
|
*
|
|
* @param context The {@link Context} for the package.
|
|
* @return Returns the {@code targetSdkVersion}.
|
|
*/
|
|
public static int getTargetSDKForPackage(@NonNull final Context context) {
|
|
return context.getApplicationInfo().targetSdkVersion;
|
|
}
|
|
|
|
/**
|
|
* Get the {@code versionName} for the package associated with the {@code context}.
|
|
*
|
|
* @param context The {@link Context} for the package.
|
|
* @return Returns the {@code versionName}. This will be {@code null} if an exception is raised.
|
|
*/
|
|
public static Boolean isAppForPackageADebugBuild(@NonNull final Context context) {
|
|
return ( 0 != ( context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
|
|
}
|
|
|
|
/**
|
|
* Get the {@code versionCode} for the package associated with the {@code context}.
|
|
*
|
|
* @param context The {@link Context} for the package.
|
|
* @return Returns the {@code versionCode}. This will be {@code null} if an exception is raised.
|
|
*/
|
|
public static Integer getVersionCodeForPackage(@NonNull final Context context) {
|
|
try {
|
|
return getPackageInfoForPackage(context).versionCode;
|
|
} catch (final Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the {@code versionName} for the package associated with the {@code context}.
|
|
*
|
|
* @param context The {@link Context} for the package.
|
|
* @return Returns the {@code versionName}. This will be {@code null} if an exception is raised.
|
|
*/
|
|
public static String getVersionNameForPackage(@NonNull final Context context) {
|
|
try {
|
|
return getPackageInfoForPackage(context).versionName;
|
|
} catch (final Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
}
|