Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive In-App Billing on Android

Edward Dale
November 27, 2015

Reactive In-App Billing on Android

Presented at DevFest Greece 2015

Edward Dale

November 27, 2015
Tweet

More Decks by Edward Dale

Other Decks in Technology

Transcript

  1. Motivation • Our app uses RxJava for async operations •

    Except In-app billing • Crashes in IAB code
  2. In-App Billing • Google Play service that lets you sell

    digital content from inside your applications • Google Play handles all checkout details • Any application that you publish through Google Play can implement In-app Billing • Other stores (Amazon, etc) have similar APIs http://developer.android.com/google/play/billing/index.html
  3. package com.android.vending.billing; interface IInAppBillingService { . . Bundle getBuyIntent(int apiVersion,

    String packageName, String sku, String type, String developerPayload); . AIDL
  4. Interprocess Communication • Supports primitives, Parcelables, and some Collection classes

    • Parcelables must be available to both processes • Synchronous • Multithreading must be dealt with • Exceptions aren’t propogated http://developer.android.com/guide/components/aidl.html
  5. How some people do it Your App Google Play App

    Google Play Server IPC Magic IabHelper Java
  6. IabHelper • “Sample” code from Google • Not versioned or

    part of a library • ~1,000 lines of code • Big API • 20 public methods • 5 interfaces • Buggy
  7. public Inventory queryInventory(boolean querySkuDetails, List<String> moreSkus) throws IabException public Inventory

    queryInventory(boolean querySkuDetails, List<String> moreItemSkus, List<String> moreSubsSkus) throws IabException public void queryInventoryAsync(boolean querySkuDetails, List<String> moreSkus, 
 QueryInventoryFinishedListener listener) public void queryInventoryAsync(QueryInventoryFinishedListener listener) public void queryInventoryAsync(boolean querySkuDetails,
 QueryInventoryFinishedListener listener) Query
  8. public void launchPurchaseFlow(Activity act, String sku, int requestCode, OnIabPurchaseFinishedListener listener)

    public void launchPurchaseFlow(Activity act, String sku, int requestCode, OnIabPurchaseFinishedListener listener, String extraData) public void launchSubscriptionPurchaseFlow(Activity act, String sku, int requestCode, 
 OnIabPurchaseFinishedListener listener) public void launchSubscriptionPurchaseFlow(Activity act, String sku, int requestCode, 
 OnIabPurchaseFinishedListener listener, String extraData) public void launchPurchaseFlow(Activity act, String sku, String itemType, int requestCode, OnIabPurchaseFinishedListener listener, String extraData) Purchase
  9. public void launchPurchaseFlow( Activity act, 
 String sku, 
 String

    itemType, 
 int requestCode,
 OnIabPurchaseFinishedListener listener,
 String extraData ) Purchase
  10. void consume(Purchase itemInfo) throws IabException public void consumeAsync(Purchase purchase, OnConsumeFinishedListener

    listener) public void consumeAsync(List<Purchase> purchases, OnConsumeMultiFinishedListener listener) Consume
  11. public boolean handleActivityResult(int requestCode, 
 int resultCode, Intent data) public

    void enableDebugLogging(boolean enable, String tag) public void enableDebugLogging(boolean enable) public void startSetup(OnIabSetupFinishedListener listener) public void dispose() Misc
  12. RxJava • A library for composing asynchronous and event-based programs

    by using observable sequences • Parameterized concurrency • Uniform, composable interface https://github.com/ReactiveX/RxJava
  13. Going Reactive • Remember “The ideal Billing API”? • query(…)

    • purchase(…) • consume(…) • (Not much else)
  14. Observable<Inventory> queryInventory(
 boolean querySkuDetails, 
 List<String> moreItemSkus, 
 List<String> moreSubsSkus)

    Observable<Purchase> consume(List<Purchase> toConsume) Observable<Purchase> launchPurchaseFlow(
 Activity activity,
 SkuDetails sku,
 int requestCode,
 String extraData) Going Reactive
  15. Observable<Purchase> consume(List<Purchase> toConsume) • sync/async versions of the method •

    Listener to be notified of the result • Checked exceptions • Single/multiple product consumption What’s Missing?
  16. final Observable<Purchase> consumeObs = billingService.consume(purchases); consumeObs.subscribe(new Action1<Purchase>() { @Override public

    void call(Purchase purchase) { // Successful consume } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { // Show error message } }); Using the API
  17. Additional Benefits Your App Google Play App Google Play Server

    IPC Magic Play Billing Service RxJava Amazon Billing Service Amazon Server Magic Billing Service Polymorphism
  18. Observable<Inventory> queryInventory(
 boolean querySkuDetails, 
 List<String> moreItemSkus, 
 List<String> moreSubsSkus)

    Observable<Purchase> consume(List<Purchase> toConsume) Observable<Purchase> launchPurchaseFlow(
 Activity activity,
 SkuDetails sku,
 int requestCode,
 String extraData) Reactive In-App Billing Coming soon
 as Open Source library