private int runWhenConnected(@Nonnull Request request, @Nullable Object tag) { return runWhenConnected(request, null, tag); }
@Override public int getPurchaseHistory(@Nonnull String product, @Nullable String continuationToken, @Nullable Bundle extraParams, @Nonnull RequestListener<Purchases> listener) { Check.isNotEmpty(product); return runWhenConnected(new GetPurchaseHistoryRequest(product, continuationToken, extraParams), wrapListener(listener), mTag); }
@Override public int isBillingSupported(@Nonnull String product, int apiVersion, @Nonnull Bundle extraParams, @Nonnull RequestListener<Object> listener) { Check.isNotEmpty(product); return runWhenConnected(new BillingSupportedRequest(product, apiVersion, extraParams), wrapListener(listener), mTag); }
@Override public int purchase(@Nonnull String product, @Nonnull String sku, @Nullable String payload, @Nullable Bundle extraParams, @Nonnull PurchaseFlow purchaseFlow) { Check.isNotEmpty(product); Check.isNotEmpty(sku); return runWhenConnected(new PurchaseRequest(product, sku, payload, extraParams), wrapListener(purchaseFlow), mTag); }
@Override public int isBillingSupported(@Nonnull String product, int apiVersion, @Nonnull RequestListener<Object> listener) { Check.isNotEmpty(product); return runWhenConnected(new BillingSupportedRequest(product, apiVersion, null), wrapListener(listener), mTag); }
@Override public int purchase(@Nonnull String product, @Nonnull String sku, @Nullable String payload, @Nonnull PurchaseFlow purchaseFlow) { Check.isNotEmpty(product); Check.isNotEmpty(sku); return runWhenConnected(new PurchaseRequest(product, sku, payload), wrapListener(purchaseFlow), mTag); }
@Override public int getPurchases(@Nonnull final String product, @Nullable final String continuationToken, @Nonnull RequestListener<Purchases> listener) { Check.isNotEmpty(product); return runWhenConnected(new GetPurchasesRequest(product, continuationToken, mConfiguration.getPurchaseVerifier()), wrapListener(listener), mTag); }
@Override public int changeSubscription(@Nonnull List<String> oldSkus, @Nonnull String newSku, @Nullable String payload, @Nonnull PurchaseFlow purchaseFlow) { Check.isNotEmpty(oldSkus); Check.isNotEmpty(newSku); return runWhenConnected( new ChangePurchaseRequest(ProductTypes.SUBSCRIPTION, oldSkus, newSku, payload), wrapListener(purchaseFlow), mTag); }
@Override public int consume(@Nonnull String token, @Nonnull RequestListener<Object> listener) { Check.isNotEmpty(token); return runWhenConnected(new ConsumePurchaseRequest(token), wrapListener(listener), mTag); }
@Override public int getSkus(@Nonnull String product, @Nonnull List<String> skus, @Nonnull RequestListener<Skus> listener) { Check.isNotEmpty(product); Check.isNotEmpty(skus); return runWhenConnected(new GetSkuDetailsRequest(product, skus), wrapListener(listener), mTag); }
@Override public int getWholePurchaseHistory(@Nonnull String product, @Nullable Bundle extraParams, @Nonnull RequestListener<Purchases> listener) { Check.isNotEmpty(product); final GetPurchaseHistoryRequest request = new GetPurchaseHistoryRequest(product, null, extraParams); return runWhenConnected(request, wrapListener(new GetWholePurchaseHistoryListener(request, listener)), mTag); }
@Override public int isPurchased(@Nonnull final String product, @Nonnull final String sku, @Nonnull final RequestListener<Boolean> listener) { Check.isNotEmpty(sku); final IsPurchasedListener isPurchasedListener = new IsPurchasedListener(sku, listener); final GetPurchasesRequest request = new GetPurchasesRequest(product, null, mConfiguration.getPurchaseVerifier()); isPurchasedListener.mRequest = request; return runWhenConnected(request, wrapListener(isPurchasedListener), mTag); }
@Override public int getAllPurchases(@Nonnull String product, @Nonnull RequestListener<Purchases> listener) { Check.isNotEmpty(product); final GetPurchasesRequest request = new GetPurchasesRequest(product, null, mConfiguration.getPurchaseVerifier()); return runWhenConnected(request, wrapListener(new GetAllPurchasesListener(request, listener)), mTag); }
@Test public void testShouldCancelRequests() throws Exception { final int REQUESTS = 10; final Billing b = Tests.newBilling(false); final CountDownLatch latch = new CountDownLatch(REQUESTS / 2); final RequestListener l = new CountDownListener(latch); final List<Integer> requestIds = new ArrayList<Integer>(); for (int i = 0; i < REQUESTS; i++) { requestIds.add(b.runWhenConnected(new SleepingRequest(100), l, null)); } Thread.sleep(100 * (REQUESTS / 2 - 1)); for (int i = REQUESTS / 2; i < REQUESTS; i++) { b.cancel(requestIds.get(i)); } assertTrue(latch.await(1, SECONDS)); }
@Test public void testShouldRunAllRequests() throws Exception { final int REQUESTS = 100; final int SLEEP = 10; final Billing b = Tests.newBilling(false); b.setMainThread(Tests.sameThreadExecutor()); final AsyncServiceConnector c = new AsyncServiceConnector(b); b.setConnector(c); final CountDownLatch latch = new CountDownLatch(REQUESTS); final RequestListener l = new CountDownListener(latch); for (int i = 0; i < REQUESTS; i++) { if (i % 10 == 0) { if (mRandom.nextBoolean()) { b.connect(); } else { // connector is called directly in order to avoid cancelling the pending // requests c.disconnect(); } } b.runWhenConnected(new SleepingRequest(mRandom.nextInt(SLEEP)), l, null); } b.connect(); assertTrue(latch.await(SLEEP * REQUESTS, TimeUnit.MILLISECONDS)); }