Android Open Source - DroidBilling Consume 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.j a  v  a 2  s. c o m*/
import android.os.RemoteException;

import com.techery.droid.billings.Constants;
import com.techery.droid.billings.events.OnConsumeFinishedEvent;
import com.techery.droid.billings.events.OnConsumeMultiFinishedEvent;
import com.techery.droid.billings.models.ConsumableItem;
import com.techery.droid.billings.models.IabException;
import com.techery.droid.billings.models.IabResult;
import com.techery.droid.billings.models.Purchase;

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

public class ConsumeTask extends BillingTask {
    private final List<ConsumableItem> items;
    private final boolean isSingleQuery;

    public ConsumeTask(List<ConsumableItem> items) {
        this.items = items;
        this.isSingleQuery = false;
    }

    public ConsumeTask(ConsumableItem purchase) {
        List<ConsumableItem> purchases = new ArrayList<ConsumableItem>();
        purchases.add(purchase);
        this.items = purchases;
        this.isSingleQuery = true;
    }

    private void consume(Purchase itemInfo) throws IabException {
        if (!itemInfo.getItemType().equals(Constants.ITEM_TYPE_INAPP)) {
            throw new IabException(Constants.IABHELPER_INVALID_CONSUMPTION,
                    "Items of type '" + itemInfo.getItemType() + "' can't be consumed.");
        }

        try {
            String token = itemInfo.getToken();
            String sku = itemInfo.getSku();
            if (token == null || token.equals("")) {
                throw new IabException(Constants.IABHELPER_MISSING_TOKEN, "PurchaseInfo is missing token for sku: "
                        + sku + " " + itemInfo);
            }

            int response = getService().consumePurchase(3, context.getPackageName(), token);
            if (response != Constants.BILLING_RESPONSE_RESULT.OK) {
                throw new IabException(response, "Error consuming sku " + sku);
            }

        } catch (RemoteException e) {
            throw new IabException(Constants.IABHELPER_REMOTE_EXCEPTION, "Remote exception while consuming. PurchaseInfo: " + itemInfo, e);
        }
    }

    @Override
    public void run() {
        final List<IabResult> results = new ArrayList<IabResult>();
        for (ConsumableItem item : this.items) {
            try {
                consume(item.getPurchase());
                item.markAsConsumed();
                results.add(new IabResult(Constants.BILLING_RESPONSE_RESULT.OK, "Successful consume of sku " + item.getSku()));
            } catch (IabException ex) {
                results.add(ex.getResult());
            }
        }

        if (this.isSingleQuery) {
            this.bus.post(new OnConsumeFinishedEvent(this.items.get(0), results.get(0)));
        } else {
            this.bus.post(new OnConsumeMultiFinishedEvent(this.items, results));
        }
    }
}




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