Skip to content

Commit

Permalink
Issue 251. Created new factory constructors that allow for late-init …
Browse files Browse the repository at this point in the history
…of play services. (#252)

New initialize() method calls bindPlayServices().

Updated README.md.
  • Loading branch information
autonomousapps authored and serggl committed Apr 3, 2017
1 parent e37f7c2 commit 134d925
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class SomeActivity extends Activity implements BillingProcessor.IBillingH
setContentView(R.layout.activity_main);

bp = new BillingProcessor(this, "YOUR LICENSE KEY FROM GOOGLE PLAY CONSOLE HERE", this);
// or bp = BillingProcessor.newBillingProcessor(this, "YOUR LICENSE KEY FROM GOOGLE PLAY CONSOLE HERE", this);
// See below on why this is a useful alternative
}

// IBillingHandler implementation
Expand Down Expand Up @@ -104,6 +106,13 @@ bp.subscribe(YOUR_ACTIVITY, "YOUR SUBSCRIPTION ID FROM GOOGLE PLAY CONSOLE HERE"
}
```

### Instantiating a `BillingProcessor` with late initialization
The basic `new BillingProcessor(...)` actually binds to Play Services inside the constructor. This can, very rarely, lead to a race condition where Play Services are bound and `onBillingInitialized()` is called before the constructor finishes, and can lead to NPEs. To avoid this, we have the following:
```java
bp = BillingProcessor.newBillingProcessor(this, "YOUR LICENSE KEY FROM GOOGLE PLAY CONSOLE HERE", this); // doesn't bind
bp.initialize(); // binds
```

## Testing In-app Billing

Here is a [complete guide](https://developer.android.com/google/play/billing/billing_testing.html).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,38 @@ public void onServiceConnected(ComponentName name, IBinder service)
}
};

/**
* Returns a new {@link BillingProcessor}, without immediately binding to Play Services. If you use
* this factory, then you must call {@link #initialize()} afterwards.
*/
public static BillingProcessor newBillingProcessor(Context context, String licenseKey, IBillingHandler handler)
{
return newBillingProcessor(context, licenseKey, null, handler);
}

/**
* Returns a new {@link BillingProcessor}, without immediately binding to Play Services. If you use
* this factory, then you must call {@link #initialize()} afterwards.
*/
public static BillingProcessor newBillingProcessor(Context context, String licenseKey, String merchantId,
IBillingHandler handler)
{
return new BillingProcessor(context, licenseKey, merchantId, handler, false);
}

public BillingProcessor(Context context, String licenseKey, IBillingHandler handler)
{
this(context, licenseKey, null, handler);
}

public BillingProcessor(Context context, String licenseKey, String merchantId,
IBillingHandler handler)
{
this(context, licenseKey, merchantId, handler, true);
}

private BillingProcessor(Context context, String licenseKey, String merchantId, IBillingHandler handler,
boolean bindImmediately)
{
super(context.getApplicationContext());
signatureBase64 = licenseKey;
Expand All @@ -140,6 +165,18 @@ public BillingProcessor(Context context, String licenseKey, String merchantId,
cachedProducts = new BillingCache(getContext(), MANAGED_PRODUCTS_CACHE_KEY);
cachedSubscriptions = new BillingCache(getContext(), SUBSCRIPTIONS_CACHE_KEY);
developerMerchantId = merchantId;
if (bindImmediately)
{
bindPlayServices();
}
}

/**
* Binds to Play Services. When complete, caller will be notified via
* {@link IBillingHandler#onBillingInitialized()}.
*/
public void initialize()
{
bindPlayServices();
}

Expand Down

0 comments on commit 134d925

Please sign in to comment.