/** * Same as {@link #start(Listener)} but with no initial request listener. */ public void start() { start(null); }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_history); ButterKnife.bind(this); final Adapter adapter = new Adapter(); mRecycler.setLayoutManager(new LinearLayoutManager(this)); mRecycler.setAdapter(adapter); final Billing billing = CheckoutApplication.get(this).getBilling(); mCheckout = Checkout.forActivity(this, billing); mCheckout.start(); mCheckout.whenReady(new HistoryLoader(adapter)); }
@Override protected void onDestroy() { mCheckout.stop(); super.onDestroy(); }
@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); }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_static); ButterKnife.bind(this); mBuy.setOnClickListener(this); mSkus.setAdapter(new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, new String[]{"android.test.purchased", "android.test.canceled", "android.test.refunded", "android.test.item_unavailable"})); mSkus.setSelection(0); Billing.setLogger(Billing.newMainThreadLogger(new BillingLogger(mConsole))); final Billing billing = CheckoutApplication.get(this).getBilling(); mCheckout = Checkout.forActivity(this, billing); mCheckout.start(); }
@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); }
@Override public void run() { Check.isTrue(Thread.holdsLock(mLock), "Must be synchronized"); // for each product we wait for: // 1. onReady to be called // 2. loadPurchased to be finished // 3. loadSkus to be finished mCount = ProductTypes.ALL.size() * 3; mCheckout.whenReady(this); }
@Before public void setUp() throws Exception { Billing billing = newBilling(); billing.connect(); mService = ((TestServiceConnector) billing.getConnector()).mService; when(mService.isBillingSupported(eq(3), anyString(), eq(IN_APP))).thenReturn(OK); when(mService.isBillingSupported(eq(3), anyString(), eq(SUBSCRIPTION))).thenReturn(OK); mCheckout = Checkout.forApplication(billing); }
@Override public void run() { if (RobotmediaDatabase.exists(mCheckout.getContext())) { mBackground.execute(new Loader()); } else { onLoaded(RobotmediaDatabase.toInventoryProducts(ProductTypes.ALL)); } }
@Nonnull public static Checkout forApplication(@Nonnull Billing billing) { return new Checkout(null, billing); }
@Override public void onAttach(Context context) { super.onAttach(context); final Activity activity = (Activity) context; final Billing billing = CheckoutApplication.get(activity).getBilling(); mCheckout = Checkout.forUi(new MyIntentStarter(this), this, billing); }
/** * Creates an {@link Inventory} object related to this {@link Checkout} instance. The created * {@link Inventory} will use a fall-back {@link Inventory} if it is returned from * {@link Billing.Configuration#getFallbackInventory(Checkout, Executor)} method. * * @return inventory */ @Nonnull public Inventory makeInventory() { Check.isMainThread(); synchronized (mLock) { checkIsNotStopped(); } final Inventory inventory; final Inventory fallbackInventory = mBilling.getConfiguration().getFallbackInventory(this, mOnLoadExecutor); if (fallbackInventory == null) { inventory = new CheckoutInventory(this); } else { inventory = new FallingBackInventory(this, fallbackInventory); } return inventory; }
@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 protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_skus); ButterKnife.bind(this); final Adapter adapter = new Adapter(); mInventoryCallback = new InventoryCallback(adapter); mRecycler.setLayoutManager(new LinearLayoutManager(this)); mRecycler.setAdapter(adapter); final Billing billing = CheckoutApplication.get(this).getBilling(); mCheckout = Checkout.forActivity(this, billing); mCheckout.start(); reloadInventory(); }
/** * Starts this {@link Checkout} and sends an initial request that checks whether billing is * supported for each product available in the Billing API. * * @param listener initial request listener */ public void start(@Nullable final Listener listener) { Check.isMainThread(); synchronized (mLock) { Check.isFalse(mState == State.STARTED, "Already started"); Check.isNull(mRequests, "Already started"); mState = State.STARTED; mBilling.onCheckoutStarted(); mRequests = mBilling.getRequests(mTag); } whenReady(listener == null ? new EmptyListener() {} : listener); }
@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); }
@Override public void run() { final Context context = mCheckout.getContext(); final RobotmediaDatabase database = new RobotmediaDatabase(context); final Products products = database.load(mTask.getRequest()); onLoaded(products); } }
@Nonnull public static Checkout forService(@Nonnull Service service, @Nonnull Billing billing) { return new Checkout(service, billing); }
@Test public void testAllProductsShouldBeSupported() throws Exception { final AwaitingListener l = new AwaitingListener(); mCheckout.start(l); l.waitWhileLoading(); verify(l.mListener, times(2)).onReady(any(BillingRequests.class), anyString(), eq(true)); verify(l.mListener, never()).onReady(any(BillingRequests.class), anyString(), eq(false)); verify(l.mListener).onReady(any(BillingRequests.class)); }
@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()); }