Skip to main content
Samsung Developer Program

Generating and Retrieving a List of Purchased Items

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

OpenIABLibrary_Integration_GeneratingAndGettingaListofUser-PurchasedItems_Diagram.png

Generating and getting a list of user-purchased items
  1. In the Trivial Drive app UI, click QUERY INVENTORY.
  2. This button invokes the onQueryInventoryButtonClicked() of MainActivity.java. onQueryInventoryButtonClicked() starts the process to display a list of purchased items. This calls queryInventoryAsync() of OpenIabHelper.java.
MainActivity.java
public void onQueryInventoryButtonClicked(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 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();
                for (int i = 0; i < purchases.size(); i++) {
                    stringBuilder.append(purchases.get(i) + "\n");
                }
                alert(stringBuilder.toString());
            }
        }
    });
}
  1. queryInventoryAsync() queries the purchased SKUs.
OpenIabHelper.java
public void queryInventoryAsync(final boolean querySkuDetails, 
                    @NotNull IabHelper.QueryInventoryFinishedListener listener, Ojbect... extraParams) {
    queryInventoryAsync(querySkuDetails, null, listener, extraParams);
}
OpenIabHelper.java
public void queryInventoryAsync(final boolean querySkuDetails, 
                    @NotNullable 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");
        // noinspection ConstantConditions
    if (listener == null) {
        throw new IllegalArgumentException("Inventory listener must be not 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?