Skip to main content
Samsung Developer Program

Retrieving the Previously Generated List of Purchased Items

Service apps can retrieve a previously generated list of items that the current user has purchased from the cache. The following diagram illustrates the query.

OpenIABLibrary_Integration_GettingCachedListofUserPurchasedItems_Diagram.png

Getting the locally cached list of user-purchased items
  1. In the Trivial Drive app UI, tap QUERY CACHED.
  2. This button invokes the onQueryCachedButtonClicked() of MainActivity.java. It starts the process of displaying a list of purchased items with faster response through client caching. onQueryCachedButtonClicked() calls queryInventoryAsync() of OpenIabHelper.java, with an extra parameter true to specify getting the locally cached list.
MainActivity.java
public void onQueryCachedButtonClicked(View arg0) {
    if (setupDone == null) {
        complain("Billing Setup is not completed yet");
        return;
    }
    
    if (!setupDone) {
        complain("Billing Setup failed");
        return;
    }
    
    if(!mHelper.subscriptionsSupported()) {
        complain("Subscriptions not supported on your device yet. Sorry!");
        return;
    }
    
    setWaitScreen(true);
    Log.d(TAG, "Querying Cached Inventory");
    ArrayList<String> itemSkus = new ArrayList<String>();
    itemSkus.add(InAppConfig.SKU_GAS);
    itemSkus.add(InAppConfig.SKU_INFINITE_GAS);
    itemSkus.add(InAppConfig.SKU_PREMIUM);
    itemSkus.add(InAppConfig.SKU_AUTO_SUPPLY);
    mHelper.queryInventoryAsync(true, itemSkus, new IabHelper.QueryInventoryFinishedListener() {
        @Override
        public void onQueryInventoryFinished(IabResult iabResult, Inventory inventory) {
            setWaitScreen(false);
            if (iabResult.isFailure()) {
                complain("Query inventory failed");
            } else {
                Log.d(TAG, "Query inventory was successful.");
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("PURCHASES: \n");
                List<String> purchases = inventory.getAllOwnedSkus();
                Log.d(TAG, "Query inventory was successful. size " + purchases.size() )'
                for (int i = 0; i < purchases.size(); i++) {
                    stringBuilder.append(purchases.get(i) + "\n");
                }
                alert(stringBuilder.toString());
            }
        }
    }, true);
}
  1. queryInventoryAsync() queries the purchased SKUs.
OpenIabHelper.java
public void queryInventoryAsync(final boolean querySkuDetails, 
                    @Nullable final List<String> moreSkus,
                    @NotNull final IabHelper.QueryInventoryFinishedListener listener, Object... extraParams) {
    queryInventoryAsync(querySkuDetails, moreSkus, null, listener, extraParams);
}
OpenIabHelper.java
public void queryInventoryAsync(final boolean querySkuDetails, 
                    @Nullable final List<String> moreItemSkus,
                    @Nullable final List<String> moreSubsSkus,
                    @NotNull final IabHelper.QueryInventoryFinishedListener listener, final Object... extraParams) {
    Logger.d("Entered queryInventoryAsync");
    checkSetupDone("queryInventory");
    if (listener == null) {
        throw new IllegalArgumentException("Inventory listener must not be null");
    }
    new Thread(new Runnable() {
        public void run() {
            IabResult result;
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreItemSkus, moreSubsSkus, extraParams);
                result = new IabResult(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            } catch (IabException exception) {
                result = exception.getResult() 
                Logger.e("queryInventoryAsync() Error : ", exception);
            }
            
            final IabResult result_f = result;
            final Inventory inv_f = inv;
            handler.post(new Runnable() {
                public void run() {
                    if (setupState == SETUP_RESULT_SUCCESSFUL) {
                        listener.onQueryInventoryFinished(result_f, inv_f);
                    }
                }
            });
        }
    }).start();
}
  1. The results of the query are returned in a list of SkuDetails via IabHelper.QueryInventoryFinishedListener().
MainActivity.java
public void onQueryInventoryFinished(IabResult iabResult, Inventory inventory) {
    setWaitScreen(false);
    if (iabResult.isFailure()) {
        complain("Query inventory failed");
    } else {
        Log.d(TAG, "Query inventory was successful.");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("PURCHASES: \n");
        List<String> purchases = inventory.getAllOwnedSkus();
        for (int i = 0; i < purchases.size(); i++) {
            stringBuilder.append(purchases.get(i) + "\n");
        }
        alert(stringBuilder.toString());
    
    }
}; 
  • Was this article helpful?