Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow background registration and adds check for Activity nullability…
… in a more specific method.
  • Loading branch information
juliocbcotta committed Aug 27, 2019
commit cdaea1525fdf099e4f60efab8a1ea9df85a83655
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
public class InAppPurchasePlugin implements MethodCallHandler {
private static final String TAG = "InAppPurchasePlugin";
private @Nullable BillingClient billingClient;
private final Activity activity;
private final Context context;
private final Registrar registrar;
private final Context applicationContext;
private final MethodChannel channel;

@VisibleForTesting
Expand Down Expand Up @@ -67,18 +67,14 @@ static final class MethodNames {

/** Plugin registration. */
public static void registerWith(Registrar registrar) {
final Activity activity = registrar.activity();
if (activity == null) {
return;
}
final MethodChannel channel =
new MethodChannel(registrar.messenger(), "plugins.flutter.io/in_app_purchase");
channel.setMethodCallHandler(new InAppPurchasePlugin(registrar.context(), activity, channel));
channel.setMethodCallHandler(new InAppPurchasePlugin(registrar, channel));
}

public InAppPurchasePlugin(Context context, Activity activity, MethodChannel channel) {
this.context = context;
this.activity = activity;
public InAppPurchasePlugin(Registrar registrar, MethodChannel channel) {
this.applicationContext = registrar.context().getApplicationContext();
this.registrar = registrar;
this.channel = channel;
}

Expand Down Expand Up @@ -120,13 +116,13 @@ public void onMethodCall(MethodCall call, Result result) {
/*package*/ InAppPurchasePlugin(@Nullable BillingClient billingClient, MethodChannel channel) {
this.billingClient = billingClient;
this.channel = channel;
this.context = null;
this.activity = null;
this.applicationContext = null;
this.registrar = null;
}

private void startConnection(final int handle, final Result result) {
if (billingClient == null) {
billingClient = buildBillingClient(context, channel);
billingClient = buildBillingClient(applicationContext, channel);
}

billingClient.startConnection(
Expand Down Expand Up @@ -204,6 +200,15 @@ private void launchBillingFlow(String sku, @Nullable String accountId, Result re
null);
return;
}
final Activity activity = registrar.activity();

if (activity == null) {
result.error(
"UNAVAILABLE",
"Details for sku " + sku + " are not available. This method requires to be run with the app in foreground.",
null);
return;
}

BillingFlowParams.Builder paramsBuilder =
BillingFlowParams.newBuilder().setSkuDetails(skuDetails);
Expand Down