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.
    • 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
                                              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 ComponentName getComponentName​(@NonNull
                                                     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
                                                         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
                                                         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
                                                      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
                                                      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
                                                      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
                                                      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 Drawable getAppIcon​(@Nullable
                                          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 Drawable getAppIcon​(@Nullable
                                          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)
      • isSystemApp

        public static boolean isSystemApp​(@Nullable
                                          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.