Search in sources :

Example 1 with IabResult

use of com.onesignal.example.iap.IabResult in project OneSignal-Android-SDK by OneSignal.

the class IabHelper method startSetup.

/**
     * Starts the setup process. This will start up the setup process asynchronously.
     * You will be notified through the listener when the setup process is complete.
     * This method is safe to call from a UI thread.
     *
     * @param listener The listener to notify when the setup process is complete.
     */
public void startSetup(final OnIabSetupFinishedListener listener) {
    // If already set up, can't do it again.
    checkNotDisposed();
    if (mSetupDone)
        throw new IllegalStateException("IAB helper is already set up.");
    // Connection to IAB service
    logDebug("Starting in-app billing setup.");
    mServiceConn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            logDebug("Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (mDisposed)
                return;
            logDebug("Billing service connected.");
            mService = IInAppBillingService.Stub.asInterface(service);
            String packageName = mContext.getPackageName();
            try {
                logDebug("Checking for in-app billing 3 support.");
                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != BILLING_RESPONSE_RESULT_OK) {
                    if (listener != null)
                        listener.onIabSetupFinished(new IabResult(response, "Error checking for billing v3 support."));
                    // if in-app purchases aren't supported, neither are subscriptions.
                    mSubscriptionsSupported = false;
                    return;
                }
                logDebug("In-app billing version 3 supported for " + packageName);
                // check for v3 subscriptions support
                response = mService.isBillingSupported(3, packageName, ITEM_TYPE_SUBS);
                if (response == BILLING_RESPONSE_RESULT_OK) {
                    logDebug("Subscriptions AVAILABLE.");
                    mSubscriptionsSupported = true;
                } else {
                    logDebug("Subscriptions NOT AVAILABLE. Response: " + response);
                }
                mSetupDone = true;
            } catch (RemoteException e) {
                if (listener != null) {
                    listener.onIabSetupFinished(new IabResult(IABHELPER_REMOTE_EXCEPTION, "RemoteException while setting up in-app billing."));
                }
                e.printStackTrace();
                return;
            }
            if (listener != null) {
                listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
            }
        }
    };
    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
        // service available to handle that Intent
        mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else {
        // no service available to handle that Intent
        if (listener != null) {
            listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE, "Billing service unavailable on device."));
        }
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) ComponentName(android.content.ComponentName) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IabResult(com.onesignal.example.iap.IabResult) RemoteException(android.os.RemoteException)

Example 2 with IabResult

use of com.onesignal.example.iap.IabResult in project OneSignal-Android-SDK by OneSignal.

the class IabHelper method consumeAsyncInternal.

void consumeAsyncInternal(final List<Purchase> purchases, final OnConsumeFinishedListener singleListener, final OnConsumeMultiFinishedListener multiListener) {
    final Handler handler = new Handler();
    flagStartAsync("consume");
    (new Thread(new Runnable() {

        public void run() {
            final List<IabResult> results = new ArrayList<IabResult>();
            for (Purchase purchase : purchases) {
                try {
                    consume(purchase);
                    results.add(new IabResult(BILLING_RESPONSE_RESULT_OK, "Successful consume of sku " + purchase.getSku()));
                } catch (IabException ex) {
                    results.add(ex.getResult());
                }
            }
            flagEndAsync();
            if (!mDisposed && singleListener != null) {
                handler.post(new Runnable() {

                    public void run() {
                        singleListener.onConsumeFinished(purchases.get(0), results.get(0));
                    }
                });
            }
            if (!mDisposed && multiListener != null) {
                handler.post(new Runnable() {

                    public void run() {
                        multiListener.onConsumeMultiFinished(purchases, results);
                    }
                });
            }
        }
    })).start();
}
Also used : Purchase(com.onesignal.example.iap.Purchase) Handler(android.os.Handler) ArrayList(java.util.ArrayList) List(java.util.List) IabException(com.onesignal.example.iap.IabException) IabResult(com.onesignal.example.iap.IabResult)

Example 3 with IabResult

use of com.onesignal.example.iap.IabResult in project OneSignal-Android-SDK by OneSignal.

the class IabHelper method queryInventoryAsync.

/**
     * Asynchronous wrapper for inventory query. This will perform an inventory
     * query as described in {@link #queryInventory}, but will do so asynchronously
     * and call back the specified listener upon completion. This method is safe to
     * call from a UI thread.
     *
     * @param querySkuDetails as in {@link #queryInventory}
     * @param moreSkus as in {@link #queryInventory}
     * @param listener The listener to notify when the refresh operation completes.
     */
public void queryInventoryAsync(final boolean querySkuDetails, final List<String> moreSkus, final QueryInventoryFinishedListener listener) {
    final Handler handler = new Handler();
    checkNotDisposed();
    checkSetupDone("queryInventory");
    flagStartAsync("refresh inventory");
    (new Thread(new Runnable() {

        public void run() {
            IabResult result = new IabResult(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreSkus);
            } catch (IabException ex) {
                result = ex.getResult();
            }
            flagEndAsync();
            final IabResult result_f = result;
            final Inventory inv_f = inv;
            if (!mDisposed && listener != null) {
                handler.post(new Runnable() {

                    public void run() {
                        listener.onQueryInventoryFinished(result_f, inv_f);
                    }
                });
            }
        }
    })).start();
}
Also used : Handler(android.os.Handler) IabException(com.onesignal.example.iap.IabException) IabResult(com.onesignal.example.iap.IabResult) Inventory(com.onesignal.example.iap.Inventory)

Example 4 with IabResult

use of com.onesignal.example.iap.IabResult in project OneSignal-Android-SDK by OneSignal.

the class IabHelper method handleActivityResult.

/**
     * Handles an activity result that's part of the purchase flow in in-app billing. If you
     * are calling {@link #launchPurchaseFlow}, then you must call this method from your
     * Activity's {@link android.app.Activity@onActivityResult} method. This method
     * MUST be called from the UI thread of the Activity.
     *
     * @param requestCode The requestCode as you received it.
     * @param resultCode The resultCode as you received it.
     * @param data The data (Intent) as you received it.
     * @return Returns true if the result was related to a purchase flow and was handled;
     *     false if the result was not related to a purchase, in which case you should
     *     handle it normally.
     */
public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
    IabResult result;
    if (requestCode != mRequestCode)
        return false;
    checkNotDisposed();
    checkSetupDone("handleActivityResult");
    // end of async purchase operation that started on launchPurchaseFlow
    flagEndAsync();
    if (data == null) {
        logError("Null data in IAB activity result.");
        result = new IabResult(IABHELPER_BAD_RESPONSE, "Null data in IAB result");
        if (mPurchaseListener != null)
            mPurchaseListener.onIabPurchaseFinished(result, null);
        return true;
    }
    int responseCode = getResponseCodeFromIntent(data);
    String purchaseData = data.getStringExtra(RESPONSE_INAPP_PURCHASE_DATA);
    String dataSignature = data.getStringExtra(RESPONSE_INAPP_SIGNATURE);
    if (resultCode == Activity.RESULT_OK && responseCode == BILLING_RESPONSE_RESULT_OK) {
        logDebug("Successful resultcode from purchase activity.");
        logDebug("Purchase data: " + purchaseData);
        logDebug("Data signature: " + dataSignature);
        logDebug("Extras: " + data.getExtras());
        logDebug("Expected item type: " + mPurchasingItemType);
        if (purchaseData == null || dataSignature == null) {
            logError("BUG: either purchaseData or dataSignature is null.");
            logDebug("Extras: " + data.getExtras().toString());
            result = new IabResult(IABHELPER_UNKNOWN_ERROR, "IAB returned null purchaseData or dataSignature");
            if (mPurchaseListener != null)
                mPurchaseListener.onIabPurchaseFinished(result, null);
            return true;
        }
        Purchase purchase = null;
        try {
            purchase = new Purchase(mPurchasingItemType, purchaseData, dataSignature);
            String sku = purchase.getSku();
            // Verify signature
            if (!Security.verifyPurchase(mSignatureBase64, purchaseData, dataSignature)) {
                logError("Purchase signature verification FAILED for sku " + sku);
                result = new IabResult(IABHELPER_VERIFICATION_FAILED, "Signature verification failed for sku " + sku);
                if (mPurchaseListener != null)
                    mPurchaseListener.onIabPurchaseFinished(result, purchase);
                return true;
            }
            logDebug("Purchase signature successfully verified.");
        } catch (JSONException e) {
            logError("Failed to parse purchase data.");
            e.printStackTrace();
            result = new IabResult(IABHELPER_BAD_RESPONSE, "Failed to parse purchase data.");
            if (mPurchaseListener != null)
                mPurchaseListener.onIabPurchaseFinished(result, null);
            return true;
        }
        if (mPurchaseListener != null) {
            mPurchaseListener.onIabPurchaseFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Success"), purchase);
        }
    } else if (resultCode == Activity.RESULT_OK) {
        // result code was OK, but in-app billing response was not OK.
        logDebug("Result code was OK but in-app billing response was not OK: " + getResponseDesc(responseCode));
        if (mPurchaseListener != null) {
            result = new IabResult(responseCode, "Problem purchashing item.");
            mPurchaseListener.onIabPurchaseFinished(result, null);
        }
    } else if (resultCode == Activity.RESULT_CANCELED) {
        logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_USER_CANCELLED, "User canceled.");
        if (mPurchaseListener != null)
            mPurchaseListener.onIabPurchaseFinished(result, null);
    } else {
        logError("Purchase failed. Result code: " + Integer.toString(resultCode) + ". Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_UNKNOWN_PURCHASE_RESPONSE, "Unknown purchase response.");
        if (mPurchaseListener != null)
            mPurchaseListener.onIabPurchaseFinished(result, null);
    }
    return true;
}
Also used : Purchase(com.onesignal.example.iap.Purchase) JSONException(org.json.JSONException) IabResult(com.onesignal.example.iap.IabResult)

Example 5 with IabResult

use of com.onesignal.example.iap.IabResult in project OneSignal-Android-SDK by OneSignal.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.e("tet", Locale.getDefault().getLanguage());
    OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.InAppAlert);
    OneSignal.sendTag("test3", "test7");
    //OneSignal.setSubscription(false);
    OneSignal.syncHashedEmail("test@onesignal.com");
    //        OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
    //            @Override
    //            public void idsAvailable(String userId, String registrationId) {
    //                Log.i("OneSignal Example:", "UserID: " + userId + ", RegId: " + (registrationId != null ? registrationId : "null"));
    //
    //                try {
    //                    OneSignal.postNotification(new JSONObject("{'contents': {'en':'Test Message'}, 'include_player_ids': ['" + userId + "']}"), null);
    //                    //OneSignal.postNotification(new JSONObject("{'contents': {'en':'Test Message'}, 'include_player_ids': ['" + "86480bb0-ef9a-11e4-8cf1-000c29917011', '2def6d7a-4395-11e4-890a-000c2940e62c" + "']}"), null);
    //                } catch (JSONException e) {
    //                    e.printStackTrace();
    //                }
    //            }
    //        });
    // compute your public key and store it in base64EncodedPublicKey
    mHelper = new IabHelper(this, "sdafsfds");
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {

        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                Log.d("OneSignalExample", "Problem setting up In-app Billing: " + result);
            }
        // Hooray, IAB is fully set up!
        }
    });
}
Also used : IabResult(com.onesignal.example.iap.IabResult) IabHelper(com.onesignal.example.iap.IabHelper)

Aggregations

IabResult (com.onesignal.example.iap.IabResult)6 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 Handler (android.os.Handler)2 RemoteException (android.os.RemoteException)2 IabException (com.onesignal.example.iap.IabException)2 Purchase (com.onesignal.example.iap.Purchase)2 ComponentName (android.content.ComponentName)1 SendIntentException (android.content.IntentSender.SendIntentException)1 ServiceConnection (android.content.ServiceConnection)1 Bundle (android.os.Bundle)1 IBinder (android.os.IBinder)1 IabHelper (com.onesignal.example.iap.IabHelper)1 Inventory (com.onesignal.example.iap.Inventory)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JSONException (org.json.JSONException)1