Skip to content

Instantly share code, notes, and snippets.

@vinitraj10
Last active January 24, 2020 12:38
Show Gist options
  • Select an option

  • Save vinitraj10/954a3a95253528f6b99ce530eacc5edb to your computer and use it in GitHub Desktop.

Select an option

Save vinitraj10/954a3a95253528f6b99ce530eacc5edb to your computer and use it in GitHub Desktop.
// method to check whether chrome is installed in user's android device or
// not,also to check the version of the chrom app to better handle TWA.
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
public static boolean isValidChrome(Context context) {
PackageInfo pInfo;
ApplicationInfo applicationInfo;
try {
pInfo = context.getPackageManager().getPackageInfo("com.android.chrome", 0);
} catch (PackageManager.NameNotFoundException e) {
//chrome is not installed on the device
return false;
}
if (pInfo != null) {
//Chrome has versions like 68.0.3440.91, we need to find the major version
//using the first dot we find in the string
int firstDotIndex = pInfo.versionName.indexOf(".");
//take only the number before the first dot excluding the dot itself
String majorVersion = pInfo.versionName.substring(0, firstDotIndex);
try {
applicationInfo = context.getPackageManager().getApplicationInfo("com.android.chrome", 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
if(applicationInfo.enabled) {
return Integer.parseInt(majorVersion) >= 72;
}
return false;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment