package com.google.androidbrowserhelper.playbilling.digitalgoods;

import android.app.Activity;

import com.android.billingclient.api.AcknowledgePurchaseResponseListener;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.ConsumeResponseListener;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.google.androidbrowserhelper.playbilling.provider.BillingWrapper;

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

/**
 * A wrapper around {@link BillingWrapper} that ensures it is connected before calling
 * {@link #querySkuDetails}, {@link #acknowledge} or {@link #consume}.
 */
public class ConnectedBillingWrapper implements BillingWrapper {
    private final BillingWrapper mInner;

    private static final int NOT_CONNECTED = 0;
    private static final int CONNECTING = 1;
    private static final int CONNECTED = 2;
    private int mState = NOT_CONNECTED;

    private interface Callback {
        void run();
    }

    private final List<Callback> mPendingCallbacks = new ArrayList<>();

    public ConnectedBillingWrapper(BillingWrapper mInner) {
        this.mInner = mInner;
    }

    private void execute(Callback callback) {
        if (mState == CONNECTED) {
            callback.run();
            return;
        }

        mPendingCallbacks.add(callback);

        if (mState == CONNECTING) return;

        mState = CONNECTING;
        mInner.connect(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                mState = CONNECTED;

                for (Callback callback : mPendingCallbacks) {
                    callback.run();
                }

                mPendingCallbacks.clear();
            }

            @Override
            public void onBillingServiceDisconnected() {
                mState = NOT_CONNECTED;
            }
        });
    }

    @Override
    public void connect(BillingClientStateListener callback) {
        execute(() -> {});
    }

    @Override
    public void querySkuDetails(List<String> skus, SkuDetailsResponseListener callback) {
        execute(() -> mInner.querySkuDetails(skus, callback));
    }

    @Override
    public void acknowledge(String token, AcknowledgePurchaseResponseListener callback) {
        execute(() -> mInner.acknowledge(token, callback));
    }

    @Override
    public void consume(String token, ConsumeResponseListener callback) {
        execute(() -> mInner.consume(token, callback));
    }

    @Override
    public boolean launchPaymentFlow(Activity activity, SkuDetails sku) {
        throw new IllegalStateException(
                "EnsuredConnectionBillingWrapper doesn't handle launch Payment flow");
    }
}

@@ -1,8 +1,618 @@
+// Copyright 2020 Google Inc. All Rights Reserved.%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//     http://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A%0A
 package 
@@ -1651,68 +1651,8 @@
 D;%0A%0A
-    private interface Callback %7B%0A        void run();%0A    %7D%0A%0A
     
@@ -1670,24 +1670,24 @@
 al List%3C
-Callback
+Runnable
 %3E mPendi
@@ -1842,24 +1842,24 @@
 execute(
-Callback
+Runnable
  callbac
@@ -2287,24 +2287,24 @@
    for (
-Callback
+Runnable
  callbac

Use Runnable