Android Open Source - DroidBilling Query Inventory Task






From Project

Back to project page DroidBilling.

License

The source code is released under:

MIT License

If you think the Android project DroidBilling listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.techery.droid.billings.tasks;
//from ww  w .  ja  va2 s . com
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;

import com.techery.droid.billings.BillingConfig;
import com.techery.droid.billings.Constants;
import com.techery.droid.billings.events.QueryInventoryErrorEvent;
import com.techery.droid.billings.events.QueryInventoryFinishedEvent;
import com.techery.droid.billings.models.BillingFeatureSupportingResult;
import com.techery.droid.billings.models.IabException;
import com.techery.droid.billings.models.IabResult;
import com.techery.droid.billings.models.Inventory;
import com.techery.droid.billings.models.Purchase;
import com.techery.droid.billings.models.SkuDetails;
import com.techery.droid.billings.tasks.BillingTask;
import com.techery.droid.billings.utils.BillingSecurity;

import org.json.JSONException;

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

import javax.inject.Inject;

public class QueryInventoryTask extends BillingTask {
    private final List<String> moreSkus;
    private final boolean querySkuDetails;

    @Inject
    BillingSecurity billingSecurity;

    @Inject
    BillingConfig billingConfig;

    @Inject
    BillingFeatureSupportingResult supportingResult;

    public QueryInventoryTask(List<String> moreSkus, boolean querySkuDetails) {
        this.moreSkus = moreSkus;
        this.querySkuDetails = querySkuDetails;
    }

    protected Inventory queryInventory(boolean querySkuDetails, List<String> moreSkus) throws IabException {
        return queryInventory(querySkuDetails, moreSkus, null);
    }

    protected Inventory queryInventory(boolean querySkuDetails, List<String> moreItemSkus,
                                    List<String> moreSubsSkus) throws IabException {
        try {
            Inventory inv = new Inventory();
            int r = queryPurchases(inv, Constants.ITEM_TYPE_INAPP);
            if (r != Constants.BILLING_RESPONSE_RESULT.OK) {
                throw new IabException(r, "Error refreshing inventory (querying owned items).");
            }

            if (querySkuDetails) {
                r = querySkuDetails(Constants.ITEM_TYPE_INAPP, inv, moreItemSkus);
                if (r != Constants.BILLING_RESPONSE_RESULT.OK) {
                    throw new IabException(r, "Error refreshing inventory (querying prices of items).");
                }
            }

            if (this.supportingResult.isSubscriptionSupported()) {
                r = queryPurchases(inv, Constants.ITEM_TYPE_SUBS);
                if (r != Constants.BILLING_RESPONSE_RESULT.OK) {
                    throw new IabException(r, "Error refreshing inventory (querying owned subscriptions).");
                }

                if (querySkuDetails) {
                    r = querySkuDetails(Constants.ITEM_TYPE_SUBS, inv, moreItemSkus);
                    if (r != Constants.BILLING_RESPONSE_RESULT.OK) {
                        throw new IabException(r, "Error refreshing inventory (querying prices of subscriptions).");
                    }
                }
            }

            return inv;
        } catch (RemoteException e) {
            throw new IabException(Constants.IABHELPER_REMOTE_EXCEPTION, "Remote exception while refreshing inventory.", e);
        } catch (JSONException e) {
            throw new IabException(Constants.IABHELPER_BAD_RESPONSE, "Error parsing JSON response while refreshing inventory.", e);
        }
    }

    int querySkuDetails(String itemType, Inventory inv, List<String> moreSkus)
            throws RemoteException, JSONException {
        ArrayList<String> skuList = new ArrayList<String>();
        skuList.addAll(inv.getAllOwnedSkus(itemType));
        if (moreSkus != null) {
            for (String sku : moreSkus) {
                if (!skuList.contains(sku)) {
                    skuList.add(sku);
                }
            }
        }

        if (skuList.size() == 0) {
            return Constants.BILLING_RESPONSE_RESULT.OK;
        }

        Bundle querySkus = new Bundle();
        querySkus.putStringArrayList(Constants.GET_SKU_DETAILS_ITEM_LIST, skuList);
        Bundle skuDetails = getService().getSkuDetails(3, context.getPackageName(),
                itemType, querySkus);

        if (!skuDetails.containsKey(Constants.RESPONSE_GET_SKU_DETAILS_LIST)) {
            int response = this.responseHelper.getResponseCodeFromBundle(skuDetails);
            if (response != Constants.BILLING_RESPONSE_RESULT.OK) {
                return response;
            } else {
                return Constants.IABHELPER_BAD_RESPONSE;
            }
        }

        ArrayList<String> responseList = skuDetails.getStringArrayList(
                Constants.RESPONSE_GET_SKU_DETAILS_LIST);

        for (String thisResponse : responseList) {
            SkuDetails d = new SkuDetails(itemType, thisResponse);
            inv.addSkuDetails(d);
        }
        return Constants.BILLING_RESPONSE_RESULT.OK;
    }

    int queryPurchases(Inventory inv, String itemType) throws JSONException, RemoteException {
        boolean verificationFailed = false;
        String continueToken = null;

        do {
            Bundle ownedItems = getService().getPurchases(3, context.getPackageName(),
                    itemType, continueToken);

            int response = this.responseHelper.getResponseCodeFromBundle(ownedItems);
            if (response != Constants.BILLING_RESPONSE_RESULT.OK) {
                return response;
            }
            if (!ownedItems.containsKey(Constants.RESPONSE_INAPP_ITEM_LIST)
                    || !ownedItems.containsKey(Constants.RESPONSE_INAPP_PURCHASE_DATA_LIST)
                    || !ownedItems.containsKey(Constants.RESPONSE_INAPP_SIGNATURE_LIST)) {
                return Constants.IABHELPER_BAD_RESPONSE;
            }

            ArrayList<String> ownedSkus = ownedItems.getStringArrayList(
                    Constants.RESPONSE_INAPP_ITEM_LIST);
            ArrayList<String> purchaseDataList = ownedItems.getStringArrayList(
                    Constants.RESPONSE_INAPP_PURCHASE_DATA_LIST);
            ArrayList<String> signatureList = ownedItems.getStringArrayList(
                    Constants.RESPONSE_INAPP_SIGNATURE_LIST);

            for (int i = 0; i < purchaseDataList.size(); ++i) {
                String purchaseData = purchaseDataList.get(i);
                String signature = signatureList.get(i);
                String sku = ownedSkus.get(i);
                if (this.billingSecurity.verifyPurchase(this.billingConfig.getSignatureBase64(), purchaseData, signature)) {
                    Purchase purchase = new Purchase(itemType, purchaseData, signature);
                    inv.addPurchase(purchase);
                } else {
                    verificationFailed = true;
                }
            }

            continueToken = ownedItems.getString(Constants.INAPP_CONTINUATION_TOKEN);
        } while (!TextUtils.isEmpty(continueToken));

        return verificationFailed ? Constants.IABHELPER_VERIFICATION_FAILED : Constants.BILLING_RESPONSE_RESULT.OK;
    }

    @Override
    public void run() {
        IabResult result = new IabResult(Constants.BILLING_RESPONSE_RESULT.OK, "Inventory refresh successful.");
        Inventory inv = null;
        try {
            inv = queryInventory(querySkuDetails, moreSkus);
        } catch (IabException ex) {
            result = ex.getResult();
        }

        if (result.isSuccess()) {
            bus.post(new QueryInventoryFinishedEvent(result, inv));
        } else {
            bus.post(new QueryInventoryErrorEvent(result, inv));
        }
    }
}




Java Source Code List

com.techery.droid.billings.AbstractController.java
com.techery.droid.billings.BillingConfig.java
com.techery.droid.billings.BillingInitializationController.java
com.techery.droid.billings.BillingManager.java
com.techery.droid.billings.BillingProcessController.java
com.techery.droid.billings.Constants.java
com.techery.droid.billings.annotations.Billing.java
com.techery.droid.billings.events.IabEvent.java
com.techery.droid.billings.events.IabSetupErrorEvent.java
com.techery.droid.billings.events.IabSetupFinishedEvent.java
com.techery.droid.billings.events.OnConsumeErrorEvent.java
com.techery.droid.billings.events.OnConsumeEvent.java
com.techery.droid.billings.events.OnConsumeFinishedEvent.java
com.techery.droid.billings.events.OnConsumeMultiFinishedEvent.java
com.techery.droid.billings.events.OnIabPurchaseErrorEvent.java
com.techery.droid.billings.events.OnIabPurchaseEvent.java
com.techery.droid.billings.events.OnIabPurchaseFinishedEvent.java
com.techery.droid.billings.events.QueryInventoryErrorEvent.java
com.techery.droid.billings.events.QueryInventoryEvent.java
com.techery.droid.billings.events.QueryInventoryFinishedEvent.java
com.techery.droid.billings.events.UninitializedHelperAccess.java
com.techery.droid.billings.models.BillableItem.java
com.techery.droid.billings.models.BillingFeatureSupportingResult.java
com.techery.droid.billings.models.ConsumableItem.java
com.techery.droid.billings.models.IabException.java
com.techery.droid.billings.models.IabResult.java
com.techery.droid.billings.models.Inventory.java
com.techery.droid.billings.models.PurchaseResult.java
com.techery.droid.billings.models.Purchase.java
com.techery.droid.billings.models.SkuDetails.java
com.techery.droid.billings.models.Subscription.java
com.techery.droid.billings.modules.BillingModule.java
com.techery.droid.billings.modules.BillingServiceModule.java
com.techery.droid.billings.tasks.BillingTask.java
com.techery.droid.billings.tasks.ConsumeTask.java
com.techery.droid.billings.tasks.QueryInventoryTask.java
com.techery.droid.billings.utils.Base64DecoderException.java
com.techery.droid.billings.utils.Base64.java
com.techery.droid.billings.utils.BillingSecurity.java
com.techery.droid.billings.utils.BillingSupportingChecker.java
com.techery.droid.billings.utils.ResponseHelper.java