Skip to content

Commit

Permalink
[in_app_purchase] Mostly convert to Android Pigeon (flutter#6262)
Browse files Browse the repository at this point in the history
Replaces manual method channels with Pigeon.

This replaces all of the method calls in both direction with Pigeon calls, and converts all the top-level objects used for params and returns to Pigeon objects. However, because of the significant object graph, in order to somewhat limit the scope of this PR I made a cut at that layer in the object graph, with nested objects still using the existing JSON serialization. In a follow-up PR, those will be converted to typed Pigeon objects as well, completing the transition.

Unfortunately a significant amount of JSON code can't be removed yet even though it's now unused by the plugin, because it's part of the API of the public wrappers, so clients may be using it. Once all the JSON code is unused, we could `@Deprecated` all of it, and then could do a follow-up breaking change later to remove it.

Most of flutter#117910
  • Loading branch information
stuartmorgan authored Apr 3, 2024
1 parent f4790cf commit 0e848fa
Show file tree
Hide file tree
Showing 28 changed files with 5,090 additions and 2,240 deletions.
4 changes: 4 additions & 0 deletions packages/in_app_purchase/in_app_purchase_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.2+1

* Converts internal platform communication to Pigeon.

## 0.3.2

* Adds UserChoiceBilling APIs to platform addition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import androidx.annotation.Nullable;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.UserChoiceBillingListener;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.inapppurchase.Messages.PlatformBillingChoiceMode;

/** Responsible for creating a {@link BillingClient} object. */
interface BillingClientFactory {
Expand All @@ -18,14 +18,14 @@ interface BillingClientFactory {
* Creates and returns a {@link BillingClient}.
*
* @param context The context used to create the {@link BillingClient}.
* @param channel The method channel used to create the {@link BillingClient}.
* @param callbackApi The callback API to be used by the {@link BillingClient}.
* @param billingChoiceMode Enables the ability to offer alternative billing or Google Play
* billing.
* @return The {@link BillingClient} object that is created.
*/
BillingClient createBillingClient(
@NonNull Context context,
@NonNull MethodChannel channel,
int billingChoiceMode,
@NonNull Messages.InAppPurchaseCallbackApi callbackApi,
PlatformBillingChoiceMode billingChoiceMode,
@Nullable UserChoiceBillingListener userChoiceBillingListener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.UserChoiceBillingListener;
import io.flutter.Log;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.inapppurchase.MethodCallHandlerImpl.BillingChoiceMode;
import io.flutter.plugins.inapppurchase.Messages.PlatformBillingChoiceMode;

/** The implementation for {@link BillingClientFactory} for the plugin. */
final class BillingClientFactoryImpl implements BillingClientFactory {

@Override
public BillingClient createBillingClient(
@NonNull Context context,
@NonNull MethodChannel channel,
int billingChoiceMode,
@NonNull Messages.InAppPurchaseCallbackApi callbackApi,
PlatformBillingChoiceMode billingChoiceMode,
@Nullable UserChoiceBillingListener userChoiceBillingListener) {
BillingClient.Builder builder = BillingClient.newBuilder(context).enablePendingPurchases();
switch (billingChoiceMode) {
case BillingChoiceMode.ALTERNATIVE_BILLING_ONLY:
case ALTERNATIVE_BILLING_ONLY:
// https://developer.android.com/google/play/billing/alternative/alternative-billing-without-user-choice-in-app
builder.enableAlternativeBillingOnly();
break;
case BillingChoiceMode.USER_CHOICE_BILLING:
case USER_CHOICE_BILLING:
if (userChoiceBillingListener != null) {
// https://developer.android.com/google/play/billing/alternative/alternative-billing-with-user-choice-in-app
builder.enableUserChoiceBilling(userChoiceBillingListener);
Expand All @@ -38,7 +37,7 @@ public BillingClient createBillingClient(
"userChoiceBillingListener null when USER_CHOICE_BILLING set. Defaulting to PLAY_BILLING_ONLY");
}
break;
case BillingChoiceMode.PLAY_BILLING_ONLY:
case PLAY_BILLING_ONLY:
// Do nothing.
break;
default:
Expand All @@ -47,6 +46,6 @@ public BillingClient createBillingClient(
"Unknown BillingChoiceMode " + billingChoiceMode + ", Defaulting to PLAY_BILLING_ONLY");
break;
}
return builder.setListener(new PluginPurchaseListener(channel)).build();
return builder.setListener(new PluginPurchaseListener(callbackApi)).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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;

/** Wraps a {@link BillingClient} instance and responds to Dart calls for it. */
public class InAppPurchasePlugin implements FlutterPlugin, ActivityAware {
Expand All @@ -25,7 +24,6 @@ public class InAppPurchasePlugin implements FlutterPlugin, ActivityAware {
// code owner of this package.
static final String PROXY_VALUE = "io.flutter.plugins.inapppurchase";

private MethodChannel methodChannel;
private MethodCallHandlerImpl methodCallHandler;

/** Plugin registration. */
Expand All @@ -45,7 +43,7 @@ public void onAttachedToEngine(@NonNull FlutterPlugin.FlutterPluginBinding bindi

@Override
public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) {
teardownMethodChannel();
teardownMethodChannel(binding.getBinaryMessenger());
}

@Override
Expand All @@ -71,16 +69,15 @@ public void onDetachedFromActivityForConfigChanges() {
}

private void setUpMethodChannel(BinaryMessenger messenger, Context context) {
methodChannel = new MethodChannel(messenger, "plugins.flutter.io/in_app_purchase");
Messages.InAppPurchaseCallbackApi handler = new Messages.InAppPurchaseCallbackApi(messenger);
methodCallHandler =
new MethodCallHandlerImpl(
/*activity=*/ null, context, methodChannel, new BillingClientFactoryImpl());
methodChannel.setMethodCallHandler(methodCallHandler);
/*activity=*/ null, context, handler, new BillingClientFactoryImpl());
Messages.InAppPurchaseApi.setUp(messenger, methodCallHandler);
}

private void teardownMethodChannel() {
methodChannel.setMethodCallHandler(null);
methodChannel = null;
private void teardownMethodChannel(BinaryMessenger messenger) {
Messages.InAppPurchaseApi.setUp(messenger, null);
methodCallHandler = null;
}

Expand Down
Loading

0 comments on commit 0e848fa

Please sign in to comment.