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);
}
Aggregations