com.vorlonsoft.android.rate.AppInformation Maven / Gradle / Ivy
Show all versions of androidrate Show documentation
package com.vorlonsoft.android.rate;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import static com.vorlonsoft.android.rate.Constants.Utils.TAG;
/**
* AppInformation Class - app information class of the AndroidRate library, thread-safe and a
* fast singleton implementation.
*
* @since 1.2.1
* @version 1.2.1
* @author Alexander Savin
*/
final class AppInformation {
/** The {@link AppInformation} singleton object.
*/
private static volatile AppInformation singleton = null;
/** The versionCode and the versionCodeMajor combined together as a single long value.
*/
private final long appLongVersionCode;
/** The name of app's package.
*/
private final String appPackageName;
/** The version name of app's package.
*/
private final String appVersionName;
/** The icon associated with an app.
*/
private final Drawable appIcon;
/**
* Constructor of AppInformation class.
*
* @param appLongVersionCode the versionCode and the versionCodeMajor combined together as a single long value
* @param appPackageName the name of app's package
* @param appVersionName the version name of app's package
* @param appIcon the icon associated with an app
*/
private AppInformation(final long appLongVersionCode,
@NonNull final String appPackageName,
@NonNull final String appVersionName,
@Nullable final Drawable appIcon) {
this.appLongVersionCode = appLongVersionCode;
this.appPackageName = appPackageName;
this.appVersionName = appVersionName;
this.appIcon = appIcon;
}
/**
* Creates the {@link AppInformation} singleton object.
*
* @param context context
* @return the {@link AppInformation} singleton object
*/
@NonNull
static AppInformation getInstance(@NonNull final Context context) {
if (singleton == null) {
final long appLongVersionCode;
final String appPackageName = context.getPackageName();
final String appVersionName;
Drawable appIcon;
PackageInfo packageInfo;
PackageManager pm = context.getPackageManager();
try {
appIcon = pm.getApplicationIcon(appPackageName);
} catch (PackageManager.NameNotFoundException e) {
Log.i(TAG, "Failed to get app icon", e);
appIcon = null;
}
try {
packageInfo = pm.getPackageInfo(appPackageName, 0);
} catch (PackageManager.NameNotFoundException e) {
Log.i(TAG, "Failed to get app package info", e);
packageInfo = null;
}
if (packageInfo != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
appLongVersionCode = ((long) packageInfo.versionCode) & 0b11111111111111111111111111111111L;
} else {
appLongVersionCode = packageInfo.getLongVersionCode();
}
appVersionName = packageInfo.versionName;
} else {
appLongVersionCode = 0L;
appVersionName = "";
}
synchronized (AppInformation.class) {
if (singleton == null) {
singleton = new AppInformation(appLongVersionCode, appPackageName, appVersionName, appIcon);
}
}
}
return singleton;
}
/**
* Returns the version number of app's package, as specified by the <manifest> tag's
* {@link android.R.styleable#AndroidManifest_versionCode versionCode} attribute.
*
* @return the version number of app's package
* @see #getAppVersionCodeMajor()
* @see #getAppLongVersionCode()
*/
@SuppressWarnings({"WeakerAccess", "JavadocReference", "unused"})
int getAppVersionCode() {
return (int) (appLongVersionCode & 0b11111111111111111111111111111111L);
}
/**
* Returns the major version number of app's package, as specified by the <manifest> tag's
* {@link android.R.styleable#AndroidManifest_versionCodeMajor versionCodeMajor} attribute.
*
* @return the major version number of app's package, 0 if API < 28
* @see #getAppVersionCode()
* @see #getAppLongVersionCode()
*/
@SuppressWarnings({"WeakerAccess", "JavadocReference", "unused"})
int getAppVersionCodeMajor() {
return (int) (appLongVersionCode >>> 32);
}
/**
* Returns {@link android.R.styleable#AndroidManifest_versionCode versionCode} and
* {@link android.R.styleable#AndroidManifest_versionCodeMajor versionCodeMajor} combined
* together as a single long value.
* The {@link android.R.styleable#AndroidManifest_versionCodeMajor versionCodeMajor} is
* placed in the upper 32 bits.
*
* @return versionCode and versionCodeMajor combined together as a single long value
* @see #getAppVersionCode()
* @see #getAppVersionCodeMajor()
*/
@SuppressWarnings({"WeakerAccess", "JavadocReference"})
long getAppLongVersionCode() {
return appLongVersionCode;
}
/**
* Returns the name of app's package.
*
* @return the name of app's package
*/
String getAppPackageName() {
return appPackageName;
}
/**
* Returns the version name of app's package, as specified by the <manifest> tag's
* {@link android.R.styleable#AndroidManifest_versionName versionName} attribute.
*
* @return the version name of app's package
*/
@SuppressWarnings("JavadocReference")
String getAppVersionName() {
return appVersionName;
}
/**
* Returns the icon associated with an application.
*
* @return the image of the icon, or the default application icon if it could not be found.
* Returns null if the resources for the application could not be loaded.
*/
@SuppressWarnings("unused")
@Nullable
Drawable getAppIcon() {
return appIcon;
}
}