Skip to content

Instantly share code, notes, and snippets.

@yccheok
Last active March 6, 2019 10:35
Show Gist options
  • Select an option

  • Save yccheok/6a6fa42aecba892c9376a04b76b048d3 to your computer and use it in GitHub Desktop.

Select an option

Save yccheok/6a6fa42aecba892c9376a04b76b048d3 to your computer and use it in GitHub Desktop.
// Minimize the execution scope of new user thread.
// Most code still run in UI thread, except isBillingSupported.
// Ensure user thread doesn't deal with member variable.
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
BillingHelper.logVerbose(TAG, "Billing service connected.");
mService = IInAppBillingService.Stub.asInterface(service);
String packageName = mApplicationContext.getPackageName();
executeAsync(() -> {
try {
IInAppBillingService _service = mService;
if (_service == null) {
return;
}
int _responseForSubs = BillingResponse.BILLING_UNAVAILABLE;
int _responseFoInApp = BillingResponse.BILLING_UNAVAILABLE;
// Determine the highest supported level for Subs.
int _highestLevelSupportedForSubs = 0;
// Determine the highest supported level for InApp.
int _highestLevelSupportedForInApp = 0;
for (int apiVersion = MAX_IAP_VERSION; apiVersion >= MIN_IAP_VERSION; apiVersion--) {
_responseForSubs = _service.isBillingSupported(apiVersion, packageName, SkuType.SUBS);
if (_responseForSubs == BillingResponse.OK) {
_highestLevelSupportedForSubs = apiVersion;
break;
}
}
for (int apiVersion = MAX_IAP_VERSION; apiVersion >= MIN_IAP_VERSION; apiVersion--) {
_responseFoInApp = _service.isBillingSupported(apiVersion, packageName, SkuType.INAPP);
if (_responseFoInApp == BillingResponse.OK) {
_highestLevelSupportedForInApp = apiVersion;
break;
}
}
final int highestLevelSupportedForSubs = _highestLevelSupportedForSubs;
final int highestLevelSupportedForInApp = _highestLevelSupportedForInApp;
final int responseForSubs = _responseForSubs;
final int responseFoInApp = _responseFoInApp;
// Now, switch back to UI thread, to minimize possible side effect.
postToUiThread(() -> {
mSubscriptionUpdateSupported = highestLevelSupportedForSubs >= 5;
mSubscriptionsSupported = highestLevelSupportedForSubs >= 3;
if (highestLevelSupportedForSubs < MIN_IAP_VERSION) {
BillingHelper.logVerbose(
TAG, "In-app billing API does not support subscription on this device.");
}
mIABv8Supported = highestLevelSupportedForInApp >= 8;
mIABv6Supported = highestLevelSupportedForInApp >= 6;
if (highestLevelSupportedForInApp < MIN_IAP_VERSION) {
BillingHelper.logWarn(
TAG, "In-app billing API version 3 is not supported on this device.");
}
if (responseForSubs == BillingResponse.OK && responseFoInApp == BillingResponse.OK) {
mClientState = ClientState.CONNECTED;
} else {
mClientState = ClientState.DISCONNECTED;
mService = null;
}
// Pick bad response if any.
mListener.onBillingSetupFinished(responseForSubs != BillingResponse.OK ? responseForSubs : responseFoInApp);
});
} catch (final RemoteException e) {
postToUiThread(() -> {
BillingHelper.logWarn(TAG, "RemoteException while setting up in-app billing" + e);
mClientState = ClientState.DISCONNECTED;
mService = null;
mListener.onBillingSetupFinished(BillingResponse.SERVICE_DISCONNECTED);
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment