Example usage for android.app Activity startIntentSenderForResult

List of usage examples for android.app Activity startIntentSenderForResult

Introduction

In this page you can find the example usage for android.app Activity startIntentSenderForResult.

Prototype

public void startIntentSenderForResult(IntentSender intent, int requestCode, @Nullable Intent fillInIntent,
        int flagsMask, int flagsValues, int extraFlags) throws IntentSender.SendIntentException 

Source Link

Document

Same as calling #startIntentSenderForResult(IntentSender,int,Intent,int,int,int,Bundle) with no options.

Usage

From source file:com.evolup.test.IAPTest01.util.IabHelper.java

/**
 * Initiate the UI flow for an in-app purchase. Call this method to initiate an in-app purchase,
 * which will involve bringing up the Google Play screen. The calling activity will be paused while
 * the user interacts with Google Play, and the result will be delivered via the activity's
 * {@link android.app.Activity#onActivityResult} method, at which point you must call
 * this object's {@link #handleActivityResult} method to continue the purchase flow. This method
 * MUST be called from the UI thread of the Activity.
 *
 * @param act The calling activity.// w w  w .  j a v  a  2  s  . c  o m
 * @param sku The sku of the item to purchase.
 * @param requestCode A request code (to differentiate from other responses --
 *     as in {@link android.app.Activity#startActivityForResult}).
 * @param listener The listener to notify when the purchase process finishes
 * @param extraData Extra data (developer payload), which will be returned with the purchase data
 *     when the purchase completes. This extra data will be permanently bound to that purchase
 *     and will always be returned when the purchase is queried.
 */
public void launchPurchaseFlow(Activity act, String sku, int requestCode,
        OnIabPurchaseFinishedListener listener, String extraData) {
    checkSetupDone("launchPurchaseFlow");
    flagStartAsync("launchPurchaseFlow");
    IabResult result;

    try {
        logDebug("Constructing buy intent for " + sku);
        Bundle buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, ITEM_TYPE_INAPP,
                extraData);
        int response = getResponseCodeFromBundle(buyIntentBundle);
        if (response != BILLING_RESPONSE_RESULT_OK) {
            logError("Unable to buy item, Error response: " + getResponseDesc(response));

            result = new IabResult(response, "Unable to buy item");
            if (listener != null)
                listener.onIabPurchaseFinished(result, null);
        }

        PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        logDebug("Launching buy intent for " + sku + ". Request code: " + requestCode);
        mRequestCode = requestCode;
        mPurchaseListener = listener;
        act.startIntentSenderForResult(pendingIntent.getIntentSender(), requestCode, new Intent(),
                Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
    } catch (SendIntentException e) {
        logError("SendIntentException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
        if (listener != null)
            listener.onIabPurchaseFinished(result, null);
    } catch (RemoteException e) {
        logError("RemoteException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IABHELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
        if (listener != null)
            listener.onIabPurchaseFinished(result, null);
    }
}

From source file:uk.co.pilllogger.billing.IabHelper.java

/**
 * Initiate the UI flow for an in-app purchase. Call this method to initiate an in-app purchase,
 * which will involve bringing up the Google Play screen. The calling activity will be paused while
 * the user interacts with Google Play, and the result will be delivered via the activity's
 * {@link android.app.Activity#onActivityResult} method, at which point you must call
 * this object's {@link #handleActivityResult} method to continue the purchase flow. This method
 * MUST be called from the UI thread of the Activity.
 *
 * @param act The calling activity.// ww  w  . ja  va 2s  .  co m
 * @param sku The sku of the item to purchase.
 * @param requestCode A request code (to differentiate from other responses --
 *     as in {@link android.app.Activity#startActivityForResult}).
 * @param listener The listener to notify when the purchase process finishes
 * @param extraData Extra data (developer payload), which will be returned with the purchase data
 *     when the purchase completes. This extra data will be permanently bound to that purchase
 *     and will always be returned when the purchase is queried.
 */
public void launchPurchaseFlow(Activity act, String sku, int requestCode,
        OnIabPurchaseFinishedListener listener, String extraData) {
    checkSetupDone("launchPurchaseFlow");
    flagStartAsync("launchPurchaseFlow");
    IabResult result;

    try {
        logDebug("Constructing buy intent for " + sku);
        Bundle buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, ITEM_TYPE_INAPP,
                extraData);
        int response = getResponseCodeFromBundle(buyIntentBundle);
        if (response != BILLING_RESPONSE_RESULT_OK) {
            logError("Unable to buy item, Error response: " + getResponseDesc(response));

            result = new IabResult(response, "Unable to buy item");
            if (listener != null)
                listener.onIabPurchaseFinished(result, null);
        } else {
            PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
            logDebug("Launching buy intent for " + sku + ". Request code: " + requestCode);
            mRequestCode = requestCode;
            mPurchaseListener = listener;
            act.startIntentSenderForResult(pendingIntent.getIntentSender(), requestCode, new Intent(),
                    Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
        }
    } catch (SendIntentException e) {
        logError("SendIntentException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
        if (listener != null)
            listener.onIabPurchaseFinished(result, null);
    } catch (RemoteException e) {
        logError("RemoteException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IABHELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
        if (listener != null)
            listener.onIabPurchaseFinished(result, null);
    }
}

From source file:com.soomla.store.billing.nokia.NokiaIabHelper.java

/**
 * See parent/*from   w w w  .j  av a  2s  .  c  o  m*/
 */
@Override
protected void launchPurchaseFlowInner(Activity act, String sku, String extraData) {
    IabResult result;

    try {
        SoomlaUtils.LogDebug(TAG, "Constructing buy intent for " + sku + ", item type: " + ITEM_TYPE_INAPP);
        Bundle buyIntentBundle = mService.getBuyIntent(3, SoomlaApp.getAppContext().getPackageName(), sku,
                ITEM_TYPE_INAPP, extraData);
        buyIntentBundle.putString("PURCHASE_SKU", sku);
        int response = getResponseCodeFromBundle(buyIntentBundle);
        if (response != IabResult.BILLING_RESPONSE_RESULT_OK) {
            SoomlaUtils.LogError(TAG,
                    "Unable to buy item, Error response: " + IabResult.getResponseDesc(response));

            IabPurchase failPurchase = new IabPurchase(ITEM_TYPE_INAPP, "{\"productId\":" + sku + "}", "");
            result = new IabResult(response, "Unable to buy item");
            purchaseFailed(result, failPurchase);
            act.finish();
            return;
        }

        PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        SoomlaUtils.LogDebug(TAG, "Launching buy intent for " + sku + ". Request code: " + RC_REQUEST);
        mPurchasingItemSku = sku;
        mPurchasingItemType = ITEM_TYPE_INAPP;

        act.startIntentSenderForResult(pendingIntent.getIntentSender(), RC_REQUEST, new Intent(),
                Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
    } catch (SendIntentException e) {
        SoomlaUtils.LogError(TAG, "SendIntentException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IabResult.IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
        purchaseFailed(result, null);
    } catch (RemoteException e) {
        SoomlaUtils.LogError(TAG, "RemoteException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IabResult.IABHELPER_REMOTE_EXCEPTION,
                "Remote exception while starting purchase flow");
        purchaseFailed(result, null);
    } catch (JSONException e) {
        SoomlaUtils.LogError(TAG, "Failed to generate failing purchase.");
        e.printStackTrace();

        result = new IabResult(IabResult.IABHELPER_BAD_RESPONSE, "Failed to generate failing purchase.");
        purchaseFailed(result, null);
    }

}

From source file:com.soomla.store.billing.google.GoogleIabHelper.java

/**
 * See parent/*from   w w  w .j a v a 2  s .co m*/
 */
@Override
protected void launchPurchaseFlowInner(Activity act, String sku, String extraData) {
    IabResult result;

    try {
        SoomlaUtils.LogDebug(TAG, "Constructing buy intent for " + sku + ", item type: " + ITEM_TYPE_INAPP);
        Bundle buyIntentBundle = mService.getBuyIntent(3, SoomlaApp.getAppContext().getPackageName(), sku,
                ITEM_TYPE_INAPP, extraData);
        buyIntentBundle.putString("PURCHASE_SKU", sku);
        int response = getResponseCodeFromBundle(buyIntentBundle);
        if (response != IabResult.BILLING_RESPONSE_RESULT_OK) {
            SoomlaUtils.LogError(TAG,
                    "Unable to buy item, Error response: " + IabResult.getResponseDesc(response));

            IabPurchase failPurchase = new IabPurchase(ITEM_TYPE_INAPP, "{\"productId\":" + sku + "}", null);
            result = new IabResult(response, "Unable to buy item");
            purchaseFailed(result, failPurchase);
            act.finish();
            return;
        }

        PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        SoomlaUtils.LogDebug(TAG, "Launching buy intent for " + sku + ". Request code: " + RC_REQUEST);
        mPurchasingItemSku = sku;
        mPurchasingItemType = ITEM_TYPE_INAPP;

        act.startIntentSenderForResult(pendingIntent.getIntentSender(), RC_REQUEST, new Intent(),
                Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
    } catch (SendIntentException e) {
        SoomlaUtils.LogError(TAG, "SendIntentException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IabResult.IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
        purchaseFailed(result, null);
    } catch (RemoteException e) {
        SoomlaUtils.LogError(TAG, "RemoteException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IabResult.IABHELPER_REMOTE_EXCEPTION,
                "Remote exception while starting purchase flow");
        purchaseFailed(result, null);
    } catch (JSONException e) {
        SoomlaUtils.LogError(TAG, "Failed to generate failing purchase.");
        e.printStackTrace();

        result = new IabResult(IabResult.IABHELPER_BAD_RESPONSE, "Failed to generate failing purchase.");
        purchaseFailed(result, null);
    }

}

From source file:ir.aarani.bazaar.billing.BillingProcessor.java

private boolean purchase(Activity activity, List<String> oldProductIds, String productId, String purchaseType,
        String developerPayload) {
    if (!isInitialized() || TextUtils.isEmpty(productId) || TextUtils.isEmpty(purchaseType))
        return false;
    try {/*  w  ww. j  a  v  a 2s .  c om*/
        String purchasePayload = purchaseType + ":" + productId;
        if (!purchaseType.equals(Constants.PRODUCT_TYPE_SUBSCRIPTION)) {
            purchasePayload += ":" + UUID.randomUUID().toString();
        }
        if (developerPayload != null) {
            purchasePayload += ":" + developerPayload;
        }
        savePurchasePayload(purchasePayload);
        Bundle bundle;
        if (oldProductIds != null && purchaseType.equals(Constants.PRODUCT_TYPE_SUBSCRIPTION))
            bundle = billingService.getBuyIntentToReplaceSkus(Constants.GOOGLE_API_SUBSCRIPTION_CHANGE_VERSION,
                    contextPackageName, oldProductIds, productId, purchaseType, purchasePayload);
        else
            bundle = billingService.getBuyIntent(Constants.GOOGLE_API_VERSION, contextPackageName, productId,
                    purchaseType, purchasePayload);

        if (bundle != null) {
            int response = bundle.getInt(Constants.RESPONSE_CODE);
            if (response == Constants.BILLING_RESPONSE_RESULT_OK) {
                PendingIntent pendingIntent = bundle.getParcelable(Constants.BUY_INTENT);
                if (activity != null && pendingIntent != null)
                    activity.startIntentSenderForResult(pendingIntent.getIntentSender(),
                            PURCHASE_FLOW_REQUEST_CODE, new Intent(), 0, 0, 0);
                else if (eventHandler != null)
                    eventHandler.onBillingError(Constants.BILLING_ERROR_LOST_CONTEXT, null);
            } else if (response == Constants.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) {
                if (!isPurchased(productId) && !isSubscribed(productId))
                    loadOwnedPurchasesFromGoogle();
                TransactionDetails details = getPurchaseTransactionDetails(productId);
                if (!checkMerchant(details)) {
                    Log.i(LOG_TAG, "Invalid or tampered merchant id!");
                    if (eventHandler != null)
                        eventHandler.onBillingError(Constants.BILLING_ERROR_INVALID_MERCHANT_ID, null);
                    return false;
                }
                if (eventHandler != null) {
                    if (details == null)
                        details = getSubscriptionTransactionDetails(productId);
                    eventHandler.onProductPurchased(productId, details);
                }
            } else if (eventHandler != null)
                eventHandler.onBillingError(Constants.BILLING_ERROR_FAILED_TO_INITIALIZE_PURCHASE, null);
        }
        return true;
    } catch (Exception e) {
        Log.e(LOG_TAG, "Error in purchase", e);
        if (eventHandler != null)
            eventHandler.onBillingError(Constants.BILLING_ERROR_OTHER_ERROR, e);
    }
    return false;
}

From source file:com.kreggysoft.Billing.util.IabHelper.java

/**
 * Initiate the UI flow for an in-app purchase. Call this method to initiate
 * an in-app purchase, which will involve bringing up the Google Play
 * screen. The calling activity will be paused while the user interacts with
 * Google Play, and the result will be delivered via the activity's
 * {@link android.app.Activity#onActivityResult} method, at which point you
 * must call this object's {@link #handleActivityResult} method to continue
 * the purchase flow. This method MUST be called from the UI thread of the
 * Activity.//www .  ja  v a  2s.  c o m
 * 
 * @param act
 *            The calling activity.
 * @param sku
 *            The sku of the item to purchase.
 * @param requestCode
 *            A request code (to differentiate from other responses -- as in
 *            {@link android.app.Activity#startActivityForResult}).
 * @param listener
 *            The listener to notify when the purchase process finishes
 * @param extraData
 *            Extra data (developer payload), which will be returned with
 *            the purchase data when the purchase completes. This extra data
 *            will be permanently bound to that purchase and will always be
 *            returned when the purchase is queried.
 */
public void launchPurchaseFlow(Activity act, String sku, int requestCode,
        OnIabPurchaseFinishedListener listener, String extraData) {
    checkSetupDone("launchPurchaseFlow");
    flagStartAsync("launchPurchaseFlow");
    IabResult result;

    try {
        logDebug("Constructing buy intent for " + sku);
        Bundle buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, ITEM_TYPE_INAPP,
                extraData);
        int response = getResponseCodeFromBundle(buyIntentBundle);
        PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        if (response != BILLING_RESPONSE_RESULT_OK) {
            logError("Unable to buy item, Error response: " + getResponseDesc(response));

            result = new IabResult(response, "Unable to buy item");
            if (listener != null)
                listener.onIabPurchaseFinished(result, null);

        }
        if (pendingIntent == null) {
            flagEndAsync();
            return;
        }

        logDebug("Launching buy intent for " + sku + ". Request code: " + requestCode);
        mRequestCode = requestCode;
        mPurchaseListener = listener;
        // if (pendingIntent!=null)
        act.startIntentSenderForResult(pendingIntent.getIntentSender(), requestCode, new Intent(),
                Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
    } catch (SendIntentException e) {
        logError("SendIntentException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
        if (listener != null)
            listener.onIabPurchaseFinished(result, null);
    } catch (RemoteException e) {
        logError("RemoteException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IABHELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
        if (listener != null)
            listener.onIabPurchaseFinished(result, null);
    }
}

From source file:mc.inappbilling.v3.InAppBillingHelper.java

/**
 * Initiate the UI flow for an in-app purchase. Call this method to initiate an in-app purchase, which will involve
 * bringing up the Google Play screen. The calling activity will be paused while the user interacts with Google Play, and
 * the result will be delivered via the activity's {@link android.app.Activity#onActivityResult} method, at which point
 * you must call this object's {@link #handleActivityResult} method to continue the purchase flow. This method MUST be
 * called from the UI thread of the Activity.
 * /*  w w  w  .j a  va 2  s .  c  o  m*/
 * @param act
 *            The calling activity.
 * @param sku
 *            The sku of the item to purchase.
 * @param requestCode
 *            A request code (to differentiate from other responses -- as in
 *            {@link android.app.Activity#startActivityForResult}).
 * @param listener
 *            The listener to notify when the purchase process finishes
 * @param extraData
 *            Extra data (developer payload), which will be returned with the purchase data when the purchase completes.
 *            This extra data will be permanently bound to that purchase and will always be returned when the purchase is
 *            queried.
 */
public void launchPurchaseFlow(Activity act, String sku, int requestCode,
        OnIabPurchaseFinishedListener listener, String extraData) {
    checkSetupDone("launchPurchaseFlow");
    flagStartAsync("launchPurchaseFlow");
    InAppBillingResult result;

    try {
        logDebug("Constructing buy intent for " + sku);
        Bundle buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, ITEM_TYPE_INAPP,
                extraData);
        int response = getResponseCodeFromBundle(buyIntentBundle);
        if (response != BILLING_RESPONSE_RESULT_OK) {
            logError("Unable to buy item, Error response: " + getResponseDesc(response));

            result = new InAppBillingResult(response, "Unable to buy item");
            if (listener != null) {
                listener.onIabPurchaseFinished(result, null);
            }
            return;
        }

        PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        logDebug("Launching buy intent for " + sku + ". Request code: " + requestCode);
        mRequestCode = requestCode;
        mPurchaseListener = listener;
        if (pendingIntent == null) {
            logError("RESPONSE_BUY_INTENT(pendingIntent) is null!");
            return;
        }
        act.startIntentSenderForResult(pendingIntent.getIntentSender(), requestCode, new Intent(),
                Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
    } catch (SendIntentException e) {
        logError("SendIntentException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new InAppBillingResult(IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
        if (listener != null)
            listener.onIabPurchaseFinished(result, null);
    } catch (RemoteException e) {
        logError("RemoteException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new InAppBillingResult(IABHELPER_REMOTE_EXCEPTION,
                "Remote exception while starting purchase flow");
        if (listener != null)
            listener.onIabPurchaseFinished(result, null);
    }
}

From source file:br.com.cpb.esperanca.iab.IabHelper.java

/**
 * Initiate the UI flow for an in-app purchase. Call this method to initiate an in-app purchase,
 * which will involve bringing up the Google Play screen. The calling activity will be paused while
 * the user interacts with Google Play, and the result will be delivered via the activity's
 * {@link android.app.Activity#onActivityResult} method, at which point you must call
 * this object's {@link #handleActivityResult} method to continue the purchase flow. This method
 * MUST be called from the UI thread of the Activity.
 * /*from w ww .j  a  v  a 2s .  c  o m*/
 * @param act
 *            The calling activity.
 * @param sku
 *            The sku of the item to purchase.
 * @param requestCode
 *            A request code (to differentiate from other responses --
 *            as in {@link android.app.Activity#startActivityForResult}).
 * @param listener
 *            The listener to notify when the purchase process finishes
 * @param extraData
 *            Extra data (developer payload), which will be returned with the purchase data
 *            when the purchase completes. This extra data will be permanently bound to that purchase
 *            and will always be returned when the purchase is queried.
 */
public void launchPurchaseFlow(Activity act, String sku, int requestCode,
        OnIabPurchaseFinishedListener listener, String extraData) {
    checkSetupDone("launchPurchaseFlow");
    flagStartAsync("launchPurchaseFlow");
    IabResult result;

    try {
        //            String purchaseToken = "inapp:" + act.getPackageName() + ":android.test.purchased";
        //            mService.consumePurchase(3, act.getPackageName(), purchaseToken);

        logDebug("Constructing buy intent for " + sku);
        Bundle buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, ITEM_TYPE_INAPP,
                extraData);
        int response = getResponseCodeFromBundle(buyIntentBundle);
        if (response != BILLING_RESPONSE_RESULT_OK) {
            logError("Unable to buy item, Error response: " + getResponseDesc(response));

            result = new IabResult(response, "Unable to buy item");
            if (listener != null) {
                listener.onIabPurchaseFinished(result, null);
            }
            return;
        }

        PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        logDebug("Launching buy intent for " + sku + ". Request code: " + requestCode);
        mRequestCode = requestCode;
        mPurchaseListener = listener;
        act.startIntentSenderForResult(pendingIntent.getIntentSender(), requestCode, new Intent(),
                Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
    } catch (SendIntentException e) {
        logError("SendIntentException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
        if (listener != null) {
            listener.onIabPurchaseFinished(result, null);
        }
    } catch (RemoteException e) {
        logError("RemoteException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IABHELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
        if (listener != null) {
            listener.onIabPurchaseFinished(result, null);
        }
    }
}

From source file:com.mobileobservinglog.support.billing.DonationBillingHandler.java

/**
 * Initiate the UI flow for an in-app purchase. Call this method to initiate an in-app purchase,
 * which will involve bringing up the Google Play screen. The calling activity will be paused while
 * the user interacts with Google Play, and the result will be delivered via the activity's
 * {@link android.app.Activity#onActivityResult} method, at which point you must call
 * this object's {@link #handleActivityResult} method to continue the purchase flow. This method
 * MUST be called from the UI thread of the Activity.
 *
 * @param act The calling activity./*from www.j  a v a2s  . co  m*/
 * @param sku The sku of the item to purchase.
 * @param requestCode A request code (to differentiate from other responses --
 *     as in {@link android.app.Activity#startActivityForResult}).
 * @param listener The listener to notify when the purchase process finishes
 * @param extraData Extra data (developer payload), which will be returned with the purchase data
 *     when the purchase completes. This extra data will be permanently bound to that purchase
 *     and will always be returned when the purchase is queried.
 */
public void launchPurchaseFlow(Activity act, String sku, int requestCode, OnPurchaseFinishedListener listener,
        String extraData) {
    Log.d("InAppPurchase", "Purchase Flow started -- handler");
    BillingHandlerResult result;
    purchaseListener = listener;
    try {
        checkSetupDone("launchPurchaseFlow");
    } catch (IllegalStateException e) {
        Log.d("InAppPurchase", "Setup was not done -- handler");
        result = new BillingHandlerResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                "Setup was not finished yet");
        if (listener != null)
            listener.onPurchaseFinished(result, null);
        return;
    }
    flagStartAsync("launchPurchaseFlow");

    try {
        Log.d("InAppPurchase", "Starting the purchase try -- handler");
        Bundle buyIntentBundle = service.getBuyIntent(3, context.getPackageName(), sku, ITEM_TYPE_INAPP,
                extraData);
        int response = getResponseCodeFromBundle(buyIntentBundle);
        if (response != BILLING_RESPONSE_RESULT_OK) {
            Log.d("InAppPurchase", "The response code from the bundle was not OK -- handler");

            //In case we have a managed item that needs to be consumed. Bit of a hack. But works for now
            if (response == BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) {
                String packageName = context.getPackageName();
                service.consumePurchase(3, packageName, "inapp:" + packageName + ":" + sku);
                buyIntentBundle = service.getBuyIntent(3, packageName, sku, ITEM_TYPE_INAPP, extraData);
            }
        }

        PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        this.requestCode = requestCode;
        Log.d("InAppPurchase", "About to start the intent sender for result -- handler");
        if (pendingIntent != null) {
            act.startIntentSenderForResult(pendingIntent.getIntentSender(), requestCode, new Intent(),
                    Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
            Log.d("InAppPurchase", "Done with the intent sender for result -- handler");
        } else {
            result = new BillingHandlerResult(BILLINGHELPER_SEND_INTENT_FAILED,
                    "Failed to send intent. We had a null pending intent");
            if (purchaseListener != null)
                purchaseListener.onPurchaseFinished(result, null);
        }
    } catch (SendIntentException e) {
        Log.d("InAppPurchase", "Caught a SendIntentException -- handler");
        e.printStackTrace();

        result = new BillingHandlerResult(BILLINGHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
        if (purchaseListener != null)
            purchaseListener.onPurchaseFinished(result, null);
    } catch (RemoteException e) {
        Log.d("InAppPurchase", "Caught RemoteException -- handler");
        e.printStackTrace();

        result = new BillingHandlerResult(BILLINGHELPER_REMOTE_EXCEPTION,
                "Remote exception while starting purchase flow");
        if (purchaseListener != null)
            purchaseListener.onPurchaseFinished(result, null);
    }
}

From source file:org.onepf.life2.oms.appstore.googleUtils.IabHelper.java

/**
 * Initiate the UI flow for an in-app purchase. Call this method to initiate an in-app purchase,
 * which will involve bringing up the Google Play screen. The calling activity will be paused while
 * the user interacts with Google Play, and the result will be delivered via the activity's
 * {@link android.app.Activity#onActivityResult} method, at which point you must call
 * this object's {@link #handleActivityResult} method to continue the purchase flow. This method
 * MUST be called from the UI thread of the Activity.
 *
 * @param act         The calling activity.
 * @param sku         The sku of the item to purchase.
 * @param itemType    indicates if it's a product or a subscription (ITEM_TYPE_INAPP or ITEM_TYPE_SUBS)
 * @param requestCode A request code (to differentiate from other responses --
 *                    as in {@link android.app.Activity#startActivityForResult}).
 * @param listener    The listener to notify when the purchase process finishes
 * @param extraData   Extra data (developer payload), which will be returned with the purchase data
 *                    when the purchase completes. This extra data will be permanently bound to that purchase
 *                    and will always be returned when the purchase is queried.
 *//* w w w . j av a  2  s .  c  o m*/
public void launchPurchaseFlow(Activity act, String sku, String itemType, int requestCode,
        OnIabPurchaseFinishedListener listener, String extraData) {
    checkSetupDone("launchPurchaseFlow");
    flagStartAsync("launchPurchaseFlow");
    IabResult result;

    if (itemType.equals(ITEM_TYPE_SUBS) && !mSubscriptionsSupported) {
        IabResult r = new IabResult(IABHELPER_SUBSCRIPTIONS_NOT_AVAILABLE, "Subscriptions are not available.");
        if (listener != null)
            listener.onIabPurchaseFinished(r, null);
        return;
    }

    try {
        logDebug("Constructing buy intent for " + sku + ", item type: " + itemType);
        Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), sku, itemType, extraData);
        int response = getResponseCodeFromBundle(buyIntentBundle);
        if (response != BILLING_RESPONSE_RESULT_OK) {
            logError("Unable to buy item, Error response: " + getResponseDesc(response));

            result = new IabResult(response, "Unable to buy item");
            if (listener != null)
                listener.onIabPurchaseFinished(result, null);
            return;
        }

        PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        logDebug("Launching buy intent for " + sku + ". Request code: " + requestCode);
        mRequestCode = requestCode;
        mPurchaseListener = listener;
        mPurchasingItemType = itemType;
        act.startIntentSenderForResult(pendingIntent.getIntentSender(), requestCode, new Intent(),
                Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
    } catch (SendIntentException e) {
        logError("SendIntentException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
        if (listener != null)
            listener.onIabPurchaseFinished(result, null);
    } catch (RemoteException e) {
        logError("RemoteException while launching purchase flow for sku " + sku);
        e.printStackTrace();

        result = new IabResult(IABHELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
        if (listener != null)
            listener.onIabPurchaseFinished(result, null);
    }
}