Skip to content

Commit

Permalink
[in_app_review] check installer package and show warning if unrecognised
Browse files Browse the repository at this point in the history
  • Loading branch information
britannio committed Jul 29, 2024
1 parent 0f55238 commit d39d86f
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import com.google.android.play.core.review.ReviewManager;
import com.google.android.play.core.review.ReviewManagerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
Expand Down Expand Up @@ -101,6 +105,10 @@ private void isAvailable(final Result result) {
return;
}

if (!appInstalledBySupportedStore()) {
invalidStoreWarning();
}

final boolean playStoreAndPlayServicesAvailable = isPlayStoreInstalled() && isPlayServicesAvailable();
final boolean lollipopOrLater = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;

Expand Down Expand Up @@ -149,6 +157,10 @@ private void requestReview(final Result result) {
Log.i(TAG, "requestReview: called");
if (noContextOrActivity(result)) return;

if (!appInstalledBySupportedStore()) {
invalidStoreWarning();
}

final ReviewManager manager = ReviewManagerFactory.create(context);

if (reviewInfo != null) {
Expand Down Expand Up @@ -184,7 +196,7 @@ private void launchReviewFlow(final Result result, ReviewManager manager, Review
private boolean isPlayStoreInstalled() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.getPackageManager().getPackageInfo("com.android.vending", PackageManager.PackageInfoFlags.of(0));
context.getPackageManager().getPackageInfo("com.android.vending", PackageManager.PackageInfoFlags.of(0));
} else {
context.getPackageManager().getPackageInfo("com.android.vending", 0);
}
Expand All @@ -206,6 +218,17 @@ private boolean isPlayServicesAvailable() {
return true;
}

private boolean appInstalledBySupportedStore() {
final List<String> validInstallers = new ArrayList<>(Arrays.asList("com.android.vending"));
final String installer = context.getPackageManager().getInstallerPackageName(context.getPackageName());
Log.i(TAG, "appInstalledBySupportedStore: installer: " + installer);
return installer != null && validInstallers.contains(installer);
}

private void invalidStoreWarning() {
Log.w(TAG, "The app should be installed by the Play Store to test in_app_review. See https://pub.dev/packages/in_app_review#testing-read-carefully for more information.");
}


private void openStoreListing(Result result) {
Log.i(TAG, "openStoreListing: called");
Expand Down

0 comments on commit d39d86f

Please sign in to comment.