// Copyright 2020 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.androidbrowserhelper.playbilling.digitalgoods;

import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;

import com.google.androidbrowserhelper.playbilling.provider.BillingWrapper;
import com.google.androidbrowserhelper.playbilling.provider.BillingWrapperFactory;
import com.google.androidbrowserhelper.trusted.ExtraCommandHandler;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.browser.trusted.TrustedWebActivityCallbackRemote;

public class DigitalGoodsRequestHandler implements ExtraCommandHandler {
    private final BillingWrapper mWrapper;
    private final BillingWrapper.Listener mListener = (result, token) -> { };

    public DigitalGoodsRequestHandler(Context context) {
        mWrapper = new ConnectedBillingWrapper(BillingWrapperFactory.get(context, mListener));
    }

    @NonNull
    @Override
    public Bundle handleExtraCommand(Context context, String commandName, Bundle args,
            @Nullable TrustedWebActivityCallbackRemote callback) {
        DigitalGoodsCallback wrappedCallback = (callbackName, callbackArgs) -> {
            try {
                callback.runExtraCallback(callbackName, callbackArgs);
            } catch (RemoteException e) {
                // The remote app crashed/got shut down, not much we can do.
            }
        };

        boolean success = handle(commandName, args, wrappedCallback);

        Bundle bundle = new Bundle();
        bundle.putBoolean(EXTRA_COMMAND_SUCCESS, success);
        return bundle;
    }

    public boolean handle(@NonNull String commandName, @NonNull Bundle args,
            @Nullable DigitalGoodsCallback callback) {
        switch (commandName) {
            case GetDetailsCall.COMMAND_NAME:
                GetDetailsCall getDetailsCall = GetDetailsCall.create(args, callback);
                if (getDetailsCall == null) break;
                getDetailsCall.call(mWrapper);
                return true;
            case AcknowledgeCall.COMMAND_NAME:
                AcknowledgeCall acknowledgeCall = AcknowledgeCall.create(args, callback);
                if (acknowledgeCall == null) break;
                acknowledgeCall.call(mWrapper);
                return true;
            case ListPurchasesCall.COMMAND_NAME:
                ListPurchasesCall listPurchasesCall = ListPurchasesCall.create(callback);
                if (listPurchasesCall == null) break;
                listPurchasesCall.call(mWrapper);
                return true;
        }

        Logging.logUnknownCommand(commandName);
        return false;
    }
}

@@ -1205,16 +1205,655 @@
 ndler %7B%0A
+    /**%0A     * A version number used to track the communication format between the TWA shell and the%0A     * browser.%0A     *%0A     * Version 1 of this API corresponds to DGAPI v1 and v2.0 (there were no changes in%0A     * shell/browser communication between DGAPI v1 and v2.0). However, we didn't put the API%0A     * version into the Bundle at that point, so v1 is implied by this extra missing from the%0A     * returned Bundle.%0A     *%0A     * Version 2 corresponds to DGAPI v2.1.%0A     */%0A    private static final String EXTRA_DIGITAL_GOODS_API_VERSION = %22digital_goods_api_version%22;%0A    private static final int DIGITAL_GOODS_API_VERSION = 2;%0A%0A
     priv
@@ -2785,24 +2785,107 @@
 , success);%0A
+        bundle.putInt(EXTRA_DIGITAL_GOODS_API_VERSION, DIGITAL_GOODS_API_VERSION);%0A
         retu

Add version code to Digital Goods communications.