Class DynamicPackageUtils


  • public class DynamicPackageUtils
    extends java.lang.Object
    Helper class to get package or app related information.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ID_NULL
      The null resource ID.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static android.graphics.drawable.Drawable getActivityIcon​(android.content.Context context, android.content.ComponentName componentName)
      Load activity icon from the given component name.
      static int getActivityIconRes​(android.content.Context context, android.content.ComponentName componentName)
      Load activity icon resource from the given component name.
      static android.graphics.drawable.Drawable getAppIcon​(android.content.Context context)
      Load application icon from the given context.
      static android.graphics.drawable.Drawable getAppIcon​(android.content.Context context, java.lang.String packageName)
      Load application icon from the given package.
      static java.lang.CharSequence getAppLabel​(android.content.Context context)
      Get application label from the given context.
      static java.lang.CharSequence getAppLabel​(android.content.Context context, java.lang.String packageName)
      Get application label from the given package.
      static android.content.ComponentName getComponentName​(android.content.Context context)
      Get component name from the given context.
      static java.lang.String getVersionCode​(android.content.Context context)
      Get package version code from the given context.
      static java.lang.String getVersionCode​(android.content.Context context, java.lang.String packageName)
      Get package version code from the given package.
      static java.lang.String getVersionName​(android.content.Context context)
      Get package version name from the given context.
      static java.lang.String getVersionName​(android.content.Context context, java.lang.String packageName)
      Get package version name from the given package.
      static boolean isPackageExists​(android.content.Context context, java.lang.String packageName)
      Checks if a given package name exits.
      static boolean isSystemApp​(android.content.pm.ApplicationInfo applicationInfo)
      Detects if the given application info is a system app or not.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ID_NULL

        @AnyRes
        public static final int ID_NULL
        The null resource ID. This denotes an invalid resource ID that is returned by the system when a resource is not found or the value is set to @null in XML.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DynamicPackageUtils

        public DynamicPackageUtils()
    • Method Detail

      • isPackageExists

        public static boolean isPackageExists​(@Nullable
                                              android.content.Context context,
                                              @Nullable
                                              java.lang.String packageName)
        Checks if a given package name exits.
        Parameters:
        context - The context to get the package manager.
        packageName - The package name to be checked.
        Returns:
        true if the given package name exits.
      • getComponentName

        @NonNull
        public static android.content.ComponentName getComponentName​(@NonNull
                                                                     android.content.Context context)
        Get component name from the given context.
        Parameters:
        context - The context to build the component.
        Returns:
        The component name from the given context.
        See Also:
        ComponentName(String, String)
      • getAppLabel

        @Nullable
        public static java.lang.CharSequence getAppLabel​(@Nullable
                                                         android.content.Context context,
                                                         @Nullable
                                                         java.lang.String packageName)
        Get application label from the given package.
        Parameters:
        context - The context to get the package manager.
        packageName - The package name of the app to get its label.
        Returns:
        The application label or name.
        See Also:
        PackageItemInfo.loadLabel(PackageManager)
      • getAppLabel

        @Nullable
        public static java.lang.CharSequence getAppLabel​(@Nullable
                                                         android.content.Context context)
        Get application label from the given context.
        Parameters:
        context - The context to get the package manager.
        Returns:
        The application label or name.
        See Also:
        PackageItemInfo.loadLabel(PackageManager)
      • getVersionCode

        @Nullable
        public static java.lang.String getVersionCode​(@Nullable
                                                      android.content.Context context,
                                                      @Nullable
                                                      java.lang.String packageName)
        Get package version code from the given package.
        Parameters:
        context - The context to get the package manager.
        packageName - The package name to get its version code.
        Returns:
        The package version code.
        See Also:
        PackageInfo.versionCode
      • getVersionCode

        @Nullable
        public static java.lang.String getVersionCode​(@Nullable
                                                      android.content.Context context)
        Get package version code from the given context.
        Parameters:
        context - The context to get the package manager.
        Returns:
        The package version code.
        See Also:
        PackageInfo.versionCode
      • getVersionName

        @Nullable
        public static java.lang.String getVersionName​(@Nullable
                                                      android.content.Context context,
                                                      @Nullable
                                                      java.lang.String packageName)
        Get package version name from the given package.
        Parameters:
        context - The context to get the package manager.
        packageName - The package name to get its version name.
        Returns:
        The package version name.
        See Also:
        PackageInfo.versionName
      • getVersionName

        @Nullable
        public static java.lang.String getVersionName​(@Nullable
                                                      android.content.Context context)
        Get package version name from the given context.
        Parameters:
        context - The context to get the package manager.
        Returns:
        The package version name.
        See Also:
        PackageInfo.versionName
      • getAppIcon

        @Nullable
        public static android.graphics.drawable.Drawable getAppIcon​(@Nullable
                                                                    android.content.Context context,
                                                                    @Nullable
                                                                    java.lang.String packageName)
        Load application icon from the given package.
        Parameters:
        context - The context to get the package manager.
        packageName - The package name of the app to load its icon.
        Returns:
        The application icon drawable.
        See Also:
        PackageItemInfo.loadIcon(PackageManager)
      • getAppIcon

        @Nullable
        public static android.graphics.drawable.Drawable getAppIcon​(@Nullable
                                                                    android.content.Context context)
        Load application icon from the given context.
        Parameters:
        context - The context to get the package manager.
        Returns:
        The application icon drawable.
        See Also:
        PackageItemInfo.loadIcon(PackageManager)
      • getActivityIcon

        @Nullable
        public static android.graphics.drawable.Drawable getActivityIcon​(@Nullable
                                                                         android.content.Context context,
                                                                         @Nullable
                                                                         android.content.ComponentName componentName)
        Load activity icon from the given component name.
        Parameters:
        context - The context to get the package manager.
        componentName - The component name to be used.
        Returns:
        The activity icon drawable.
        See Also:
        PackageManager.getActivityIcon(ComponentName), PackageManager.getDefaultActivityIcon()
      • getActivityIconRes

        @DrawableRes
        public static int getActivityIconRes​(@Nullable
                                             android.content.Context context,
                                             @Nullable
                                             android.content.ComponentName componentName)
        Load activity icon resource from the given component name.
        Parameters:
        context - The context to get the package manager.
        componentName - The component name to be used.
        Returns:
        The activity icon resource.
        See Also:
        PackageManager.getActivityIcon(ComponentName), PackageManager.getDefaultActivityIcon()
      • isSystemApp

        public static boolean isSystemApp​(@Nullable
                                          android.content.pm.ApplicationInfo applicationInfo)
        Detects if the given application info is a system app or not.
        Parameters:
        applicationInfo - The application info of the package.
        Returns:
        true if the associated package is a system app.