Skip to content

Commit

Permalink
updated to v2 embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardosilva-asaas committed Jun 3, 2020
1 parent a85487e commit 11c9dcd
Showing 1 changed file with 111 additions and 74 deletions.
185 changes: 111 additions & 74 deletions android/src/main/java/com/iyaffle/launchreview/LaunchReviewPlugin.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.iyaffle.launchreview;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.PluginRegistry.Registrar;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
Expand All @@ -14,85 +19,117 @@
import android.content.pm.ActivityInfo;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.util.List;
/**
* LaunchReviewPlugin
*/
public class LaunchReviewPlugin implements MethodCallHandler {

private final Registrar mRegistrar;

private LaunchReviewPlugin(Registrar registrar) {
this.mRegistrar = registrar;
}

/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "launch_review");
LaunchReviewPlugin instance = new LaunchReviewPlugin(registrar);
channel.setMethodCallHandler(instance);
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("launch")) {
String appId = call.argument("android_id");

if (appId == null) {
appId = mRegistrar.activity().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<ResolveInfo> otherApps = mRegistrar.activity().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);
Toast.makeText(mRegistrar.activity(), "Please Rate Application", Toast.LENGTH_SHORT).show();

mRegistrar.activity().startActivity(rateIntent);
marketFound = true;
break;
public class LaunchReviewPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware {
Activity activity;

private static LaunchReviewPlugin register(LaunchReviewPlugin plugin, BinaryMessenger messenger, Activity activity) {
final MethodChannel channel = new MethodChannel(messenger, "launch_review");
plugin.activity = activity;
channel.setMethodCallHandler(plugin);
return plugin;
}

/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
register(new LaunchReviewPlugin(), registrar.messenger(), registrar.activity());
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("launch")) {
String appId = call.argument("android_id");

if (appId == null) {
appId = activity.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<ResolveInfo> otherApps = activity.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);
Toast.makeText(activity, "Please Rate Application", Toast.LENGTH_SHORT).show();

activity.startActivity(rateIntent);
marketFound = true;
break;

}
}

// if GP not present on device, open web browser
if (!marketFound) {
try {
activity.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + appId)));
} catch (ActivityNotFoundException e) {
activity.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=" + appId)));
}
}
result.success(null);
} else {
result.notImplemented();
}
}

// if GP not present on device, open web browser
if (!marketFound) {
try {
mRegistrar.activity().startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + appId)));
} catch (ActivityNotFoundException e) {
mRegistrar.activity().startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=" + appId)));
}
}
result.success(null);
} else {
result.notImplemented();
}
}

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
register(this, binding.getBinaryMessenger(), null);
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {

}

@Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
activity = binding.getActivity();
}

@Override
public void onDetachedFromActivityForConfigChanges() {

}

@Override
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

}

@Override
public void onDetachedFromActivity() {

}
}

0 comments on commit 11c9dcd

Please sign in to comment.