Created
April 22, 2020 06:01
-
-
Save yehgdotnet/fef2f953170179a6637d4f02348d023f to your computer and use it in GitHub Desktop.
Android: secure AppStore Launch (aka protocol takover check)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# any apps that registers market:// could be part of user selection list | |
# only google play ID is allowed | |
# need to check for such application existence then launch the intent. | |
public static void secureAppStoreLaunch(Context context) { | |
// you can also use BuildConfig.APPLICATION_ID | |
String appId = context.getPackageName(); | |
Intent rateIntent = new Intent(Intent.ACTION_VIEW, | |
Uri.parse("market://details?id=" + appId)); | |
boolean marketFound = false; | |
// find all applications able to handle our rateIntent | |
final List otherApps = context.getPackageManager() | |
.queryIntentActivities(rateIntent, 0); | |
for (ResolveInfo otherApp: otherApps) { | |
// look for Google Play application | |
if (otherApp.activityInfo.applicationInfo.packageName | |
.equals("com.android.vending")) { | |
ActivityInfo otherAppActivity = otherApp.activityInfo; | |
ComponentName componentName = new ComponentName( | |
otherAppActivity.applicationInfo.packageName, | |
otherAppActivity.name | |
); | |
// make sure it does NOT open in the stack of your activity | |
rateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
// task reparenting if needed | |
rateIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); | |
// if the Google Play was already open in a search result | |
// this make sure it still go to the app page you requested | |
rateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
// this make sure only the Google Play app is allowed to | |
// intercept the intent | |
rateIntent.setComponent(componentName); | |
context.startActivity(rateIntent); | |
marketFound = true; | |
break; | |
} | |
} | |
// if GP not present on device, open web browser | |
if (!marketFound) { | |
Intent webIntent = new Intent(Intent.ACTION_VIEW, | |
Uri.parse("https://play.google.com/store/apps/details?id="+appId)); | |
context.startActivity(webIntent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment