Search in sources :

Example 1 with IInAppBillingService

use of com.android.vending.billing.IInAppBillingService in project smooth-app-bar-layout by henrytao-me.

the class BaseActivity method requestItemsForPurchase.

private void requestItemsForPurchase(final AsyncCallback<List<PurchaseItem>> callback) {
    if (mPurchaseItems != null && mPurchaseItems.size() > 0) {
        if (callback != null) {
            callback.onSuccess(mPurchaseItems);
        }
        return;
    }
    new AsyncTask<IInAppBillingService, Void, AsyncResult<List<PurchaseItem>>>() {

        @Override
        protected AsyncResult<List<PurchaseItem>> doInBackground(IInAppBillingService... params) {
            List<PurchaseItem> result = new ArrayList<>();
            Throwable exception = null;
            IInAppBillingService billingService = params[0];
            if (billingService == null) {
                exception = new Exception("Unknow");
            } else {
                ArrayList<String> skuList = new ArrayList<>(Arrays.asList(DONATE_ITEMS));
                Bundle querySkus = new Bundle();
                querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
                try {
                    Bundle skuDetails = billingService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
                    int response = skuDetails.getInt("RESPONSE_CODE");
                    if (response == 0) {
                        ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
                        PurchaseItem purchaseItem;
                        for (String item : responseList) {
                            purchaseItem = new PurchaseItem(new JSONObject(item));
                            if (purchaseItem.isValid()) {
                                result.add(purchaseItem);
                            }
                        }
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                    exception = e;
                } catch (JSONException e) {
                    e.printStackTrace();
                    exception = e;
                }
            }
            return new AsyncResult<>(result, exception);
        }

        @Override
        protected void onPostExecute(AsyncResult<List<PurchaseItem>> result) {
            if (!isFinishing() && callback != null) {
                Throwable error = result.getError();
                if (error == null && (result.getResult() == null || result.getResult().size() == 0)) {
                    error = new Exception("Unknow");
                }
                if (error != null) {
                    callback.onError(error);
                } else {
                    mPurchaseItems = result.getResult();
                    Collections.sort(mPurchaseItems, new Comparator<PurchaseItem>() {

                        @Override
                        public int compare(PurchaseItem lhs, PurchaseItem rhs) {
                            return (int) ((lhs.getPriceAmountMicros() - rhs.getPriceAmountMicros()) / 1000);
                        }
                    });
                    callback.onSuccess(mPurchaseItems);
                }
            }
        }
    }.execute(mBillingService);
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) RemoteException(android.os.RemoteException) JSONException(org.json.JSONException) Comparator(java.util.Comparator) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) IInAppBillingService(com.android.vending.billing.IInAppBillingService) RemoteException(android.os.RemoteException)

Aggregations

Bundle (android.os.Bundle)1 RemoteException (android.os.RemoteException)1 IInAppBillingService (com.android.vending.billing.IInAppBillingService)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1