/** * Makes {@link Inventory} to load SKU details for the given list of <var>skus</var>. As * SKU identifier is unique only in product <var>product</var> id must be also provided to * this method. * @param product product * @param skus list of SKU identifiers for which SKU details should be loaded * @return this request */ @Nonnull public Request loadSkus(@Nonnull String product, @Nonnull List<String> skus) { for (String sku : skus) { loadSkus(product, sku); } return this; }
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ButterKnife.bind(this, view); mItem.setText(AD_FREE); mBuyConsume.setOnClickListener(this); mCheckout.loadInventory(Inventory.Request.create().loadAllPurchases(), new InventoryCallback()); }
/** * Creates an empty load request. Only {@link Product#supported} flag is set in * {@link #load(Request, Callback)} for all available for billing products. * @return empty request * @see ProductTypes */ @Nonnull public static Request create() { return new Request(); }
private void loadSkus(@Nonnull BillingRequests requests, @Nonnull final Product product) { final List<String> skuIds = mTask.getRequest().getSkus(product.id); if (skuIds.isEmpty()) { Billing.warning("There are no SKUs for \"" + product.id + "\" product. No SKU information will be loaded"); synchronized (mLock) { countDown(); } return; } requests.getSkus(product.id, skuIds, synchronizedListener(new RequestListener<Skus>() { @Override public void onSuccess(@Nonnull Skus skus) { product.setSkus(skus.list); countDown(); } @Override public void onError(int response, @Nonnull Exception e) { countDown(); } })); } }
@Test public void testShouldLoadPurchasesWhenProductsBecameSupported() throws Exception { when(mService.isBillingSupported(eq(3), anyString(), eq(IN_APP))).thenReturn(BILLING_UNAVAILABLE); when(mService.isBillingSupported(eq(3), anyString(), eq(SUBSCRIPTION))).thenReturn(BILLING_UNAVAILABLE); when(mService.getPurchases(anyInt(), anyString(), anyString(), isNull(String.class))).thenReturn(newPurchasesBundle(0, false)); mCheckout.start(); final AwaitingCallback c1 = new AwaitingCallback(); mCheckout.loadInventory(Inventory.Request.create().loadAllPurchases(), c1); c1.waitWhileLoading(); assertFalse(c1.mProducts.get(IN_APP).supported); assertFalse(c1.mProducts.get(SUBSCRIPTION).supported); assertTrue(c1.mProducts.get(IN_APP).getPurchases().isEmpty()); when(mService.isBillingSupported(eq(3), anyString(), eq(IN_APP))).thenReturn(OK); when(mService.isBillingSupported(eq(3), anyString(), eq(SUBSCRIPTION))).thenReturn(OK); final AwaitingCallback c2 = new AwaitingCallback(); mCheckout.loadInventory(Inventory.Request.create().loadAllPurchases(), c2); c2.waitWhileLoading(); assertTrue(c2.mProducts.get(IN_APP).supported); assertTrue(c2.mProducts.get(SUBSCRIPTION).supported); assertTrue(c2.mProducts.get(IN_APP).getPurchases().size() == 1); }
@Test public void testIsLoadedWithEmptySkusList() throws Exception { populatePurchases(); populateSkus(); final Inventory.Request request = Inventory.Request.create() .loadAllPurchases() .loadSkus(IN_APP, "in_app_01") .loadSkus(SUBSCRIPTION, "sub_01"); final Checkout checkout = Checkout.forApplication(mBilling); final CheckoutInventory inventory = new CheckoutInventory(checkout); final TestCallback listener = new TestCallback(); checkout.start(); inventory.load(request, listener); Tests.waitWhileLoading(inventory); final Inventory.Product app = listener.mProducts.get(IN_APP); Assert.assertTrue(app.getSkus().isEmpty()); Assert.assertFalse(app.getPurchases().isEmpty()); final Inventory.Product sub = listener.mProducts.get(SUBSCRIPTION); Assert.assertTrue(sub.getSkus().isEmpty()); Assert.assertFalse(sub.getPurchases().isEmpty()); }
@Override public void onReady(@Nonnull BillingRequests requests, @Nonnull String productId, boolean billingSupported) { final Product product = new Product(productId, billingSupported); synchronized (mLock) { countDown(); mProducts.add(product); if (!mTask.isCancelled() && product.supported && mTask.getRequest().shouldLoadPurchases(productId)) { loadPurchases(requests, product); } else { countDown(1); } if (!mTask.isCancelled() && product.supported && mTask.getRequest().shouldLoadSkus(productId)) { loadSkus(requests, product); } else { countDown(1); } } }
@Nonnull private Inventory.Products loadProducts(@Nonnull Inventory.Request request, @Nonnull SQLiteDatabase db) { final Inventory.Products result = new Inventory.Products(); for (String productId : ProductTypes.ALL) { final Inventory.Product product = new Inventory.Product(productId, true); final List<String> skus = request.getSkus(productId); if (!skus.isEmpty()) { product.setPurchases(loadPurchases(skus, db)); } else { Billing.warning("There are no SKUs for \"" + product.id + "\" product. No purchase information will be loaded"); } result.add(product); } return result; }
@Test public void testShouldContinueAfterListenerException() throws Exception { populatePurchases(); final Inventory.Request request = Inventory.Request.create() .loadSkus(IN_APP, "in_app_01") .loadSkus(SUBSCRIPTION, "sub_01"); final Checkout checkout = Checkout.forApplication(mBilling); final CrashingCallback listener = new CrashingCallback(); final CheckoutInventory inventory = new CheckoutInventory(checkout); checkout.start(); inventory.load(request, listener); Tests.waitWhileLoading(inventory); Assert.assertTrue(listener.exceptionThrown); }
@Test public void testShouldLoadSeveralTasksAsynchronously() throws Exception { populatePurchases(); populateSkus(); mCheckout.start(); final CountingCallback c = new CountingCallback(); mInventory.load(Inventory.Request.create().loadPurchases(IN_APP), c); mInventory.load(Inventory.Request.create().loadPurchases(SUBSCRIPTION), c); mInventory.load(Inventory.Request.create().loadSkus(SUBSCRIPTION, asList("sub1", "sub2", "sub3", "sub4")), c); mInventory.load(Inventory.Request.create().loadSkus(IN_APP, asList("1", "2", "3", "4", "6")), c); mInventory.load(mRequest, c); Tests.waitWhileLoading(mInventory); assertEquals(5, c.mCount); }
private void reloadInventory() { final Inventory.Request request = Inventory.Request.create(); request.loadPurchases(SUBSCRIPTION); request.loadSkus(SUBSCRIPTION, SKUS); mCheckout.loadInventory(request, new Inventory.Callback() { @Override public void onLoaded(@Nonnull Inventory.Products products) { for (Inventory.Callback callback : mInventoryCallbacks) { callback.onLoaded(products); } } }); }
@Nonnull Request copy() { final Request copy = new Request(); copy.mSkus.putAll(mSkus); copy.mProducts.addAll(mProducts); return copy; }
/** * Same as {@link #loadSkus(String, List)}. */ @Nonnull public Request loadSkus(@Nonnull String product, @Nonnull String... skus) { Check.isTrue(skus.length > 0, "No SKUs listed, can't load them"); return loadSkus(product, Arrays.asList(skus)); }
public Task(@Nonnull Request request, @Nonnull Callback callback) { mRequest = request.copy(); mCallback = callback; }
@Test public void testShouldLoadSkus() throws Exception { populateSkus(); mCheckout.start(); final TestCallback c1 = new TestCallback(); mInventory.load(Inventory.Request.create().loadSkus(SUBSCRIPTION, asList("subX", "sub2", "sub3")), c1); final TestCallback c2 = new TestCallback(); mInventory.load(Inventory.Request.create().loadSkus(IN_APP, asList("1", "2", "5")), c2); Tests.waitWhileLoading(mInventory); assertEquals(2, c1.mProducts.get(SUBSCRIPTION).getSkus().size()); assertEquals(2, c2.mProducts.get(IN_APP).getSkus().size()); }
@Before public void setUp() throws Exception { mBilling = newBilling(); mRequest = Inventory.Request.create() .loadAllPurchases() .loadSkus(IN_APP, asList("1", "2", "3", "4", "6")) .loadSkus(SUBSCRIPTION, asList("sub1", "sub2", "sub3", "sub4")); mCheckout = Checkout.forApplication(mBilling); mInventory = newInventory(mCheckout); }
@Before public void setUp() throws Exception { mFailingCache = new FailingCache(); mBilling = newBilling(); mRequest = Inventory.Request.create() .loadAllPurchases() .loadSkus(IN_APP, asList("1", "2", "3", "4", "6")) .loadSkus(SUBSCRIPTION, asList("sub1", "sub2", "sub3", "sub4")); mCheckout = Checkout.forApplication(mBilling); }
@Test public void testShouldLoadSkus() throws Exception { populateSkus(); mCheckout.start(); final TestCallback c1 = new TestCallback(); mInventory.load(Inventory.Request.create().loadSkus(SUBSCRIPTION, asList("subX", "sub2", "sub3")), c1); final TestCallback c2 = new TestCallback(); mInventory.load(Inventory.Request.create().loadSkus(IN_APP, asList("1", "2", "5")), c2); Tests.waitWhileLoading(mInventory); assertEquals(0, c1.mProducts.get(SUBSCRIPTION).getSkus().size()); assertEquals(2, c2.mProducts.get(IN_APP).getSkus().size()); }
private void reloadInventory() { final Inventory.Request request = Inventory.Request.create(); // load purchase info request.loadAllPurchases(); // load SKU details request.loadSkus(ProductTypes.IN_APP, getInAppSkus()); mCheckout.loadInventory(request, mInventoryCallback); }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_banner); ButterKnife.bind(this); mAd.setOnClickListener(this); final Billing billing = CheckoutApplication.get(this).getBilling(); mCheckout = Checkout.forActivity(this, billing); mCheckout.start(); mCheckout.loadInventory(Inventory.Request.create().loadAllPurchases(), new InventoryCallback()); }