Android Open Source - DroidBilling Billing Initialization Controller






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;
//from   w  w w .j  ava2s  .com
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

import com.android.vending.billing.IInAppBillingService;
import com.techery.droid.billings.AbstractController;
import com.techery.droid.billings.events.IabSetupErrorEvent;
import com.techery.droid.billings.events.IabSetupFinishedEvent;
import com.techery.droid.billings.models.BillingFeatureSupportingResult;
import com.techery.droid.billings.models.IabResult;
import com.techery.droid.billings.modules.BillingServiceModule;
import com.techery.droid.billings.utils.BillingSupportingChecker;

import javax.inject.Inject;

import dagger.ObjectGraph;

public class BillingInitializationController extends AbstractController {
    private ServiceConnection serviceConnection;
    private BillingProcessController billingProcessController;

    @Inject
    BillingSupportingChecker billingSupportingChecker;

    public BillingInitializationController(ObjectGraph objectGraph) {
        super(objectGraph);
    }

    public void start() {
        buildServiceConnection();
        bindServiceConnection();
    }

    public void stop() {
        if (serviceConnection != null) {
            if (context != null) {
                context.unbindService(serviceConnection);
            }
        }

        serviceConnection = null;
        billingProcessController = null;
    }

    private void bindServiceConnection() {
        Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");

        if (!context.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
            context.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
        } else {
            bus.post(new IabSetupErrorEvent(new IabResult(Constants.BILLING_RESPONSE_RESULT.BILLING_UNAVAILABLE, "Billing service unavailable on device.")));
        }
    }

    private void buildServiceConnection() {
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName name) {
                billingProcessController = null;
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                setupService(IInAppBillingService.Stub.asInterface(service));
            }
        };
    }

    private void setupService(IInAppBillingService iInAppBillingService) {
        try {
            BillingFeatureSupportingResult supportingResult = this.billingSupportingChecker.getFeaturesSupporting(iInAppBillingService);

            if (supportingResult.isBillingSupportingResult()) {
                setupHelper(iInAppBillingService, supportingResult);
            } else {
                bus.post(new IabSetupErrorEvent(new IabResult(supportingResult.getBillingSupportingResult(), "Error checking for billing v3 support.")));
            }

        } catch (RemoteException e) {
            bus.post(new IabSetupErrorEvent(new IabResult(Constants.IABHELPER_REMOTE_EXCEPTION, "RemoteException while setting up in-app billing.")));
            return;
        }
    }

    private void setupHelper(IInAppBillingService iInAppBillingService, BillingFeatureSupportingResult supportingResult) {
        IabResult result = new IabResult(Constants.BILLING_RESPONSE_RESULT.OK, "Setup successful.");

        ObjectGraph billingServiceGraph = this.objectGraph.plus(new BillingServiceModule(supportingResult, iInAppBillingService));

        this.billingProcessController = new BillingProcessController(billingServiceGraph);

        bus.post(new IabSetupFinishedEvent(result));
    }

    public BillingProcessController getBillingProcessController() {
        return billingProcessController;
    }
}




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