Helper class for getting host information. Keep it as simple as possible.
| 14 | * Helper class for getting host information. Keep it as simple as possible. |
| 15 | */ |
| 16 | public class HostInfo { |
| 17 | |
| 18 | private static Application sHostApplication = null; |
| 19 | private static PackageInfo sHostPackageInfo = null; |
| 20 | private static String sHostAppPackageName = null; |
| 21 | private static int sHostVersionCode = 0; |
| 22 | private static long sHostLongVersionCode = 0; |
| 23 | private static String sHostAppVersionName = null; |
| 24 | private static String sHostAppName = null; |
| 25 | |
| 26 | private HostInfo() { |
| 27 | throw new AssertionError("No instance for you!"); |
| 28 | } |
| 29 | |
| 30 | public static void setHostApplication(@NonNull Application app) { |
| 31 | Objects.requireNonNull(app, "app"); |
| 32 | sHostApplication = app; |
| 33 | try { |
| 34 | sHostPackageInfo = app.getPackageManager().getPackageInfo(app.getPackageName(), PackageManager.GET_META_DATA); |
| 35 | } catch (PackageManager.NameNotFoundException e) { |
| 36 | // should not happen, we must be installed |
| 37 | throw new IllegalStateException(e); |
| 38 | } |
| 39 | sHostAppPackageName = app.getPackageName(); |
| 40 | sHostVersionCode = sHostPackageInfo.versionCode; |
| 41 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { |
| 42 | sHostLongVersionCode = sHostPackageInfo.getLongVersionCode(); |
| 43 | } else { |
| 44 | sHostLongVersionCode = sHostVersionCode; |
| 45 | } |
| 46 | sHostAppVersionName = sHostPackageInfo.versionName; |
| 47 | sHostAppName = sHostPackageInfo.applicationInfo.loadLabel(app.getPackageManager()).toString(); |
| 48 | } |
| 49 | |
| 50 | @NonNull |
| 51 | public static Application getApplication() { |
| 52 | return sHostApplication; |
| 53 | } |
| 54 | |
| 55 | @NonNull |
| 56 | public static String getPackageName() { |
| 57 | return sHostAppPackageName; |
| 58 | } |
| 59 | |
| 60 | @NonNull |
| 61 | public static String getAppName() { |
| 62 | return sHostAppName; |
| 63 | } |
| 64 | |
| 65 | @NonNull |
| 66 | public static String getVersionName() { |
| 67 | return sHostAppVersionName; |
| 68 | } |
| 69 | |
| 70 | @NonNull |
| 71 | public static PackageInfo getPackageInfo() { |
| 72 | return sHostPackageInfo; |
| 73 | } |
nothing calls this directly
no outgoing calls
no test coverage detected