Fix BillingManager crash

This commit is contained in:
max-klaus 2019-08-20 10:45:05 +03:00
parent 4353251067
commit 8235f31f7d
2 changed files with 51 additions and 20 deletions

View file

@ -54,7 +54,7 @@ public class InAppPurchaseHelper {
// Debug tag, for logging // Debug tag, for logging
private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(InAppPurchaseHelper.class); private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(InAppPurchaseHelper.class);
private static final String TAG = InAppPurchaseHelper.class.getSimpleName(); private static final String TAG = InAppPurchaseHelper.class.getSimpleName();
private boolean mDebugLog = true; private boolean mDebugLog = false;
public static final long SUBSCRIPTION_HOLDING_TIME_MSEC = 1000 * 60 * 60 * 24 * 3; // 3 days public static final long SUBSCRIPTION_HOLDING_TIME_MSEC = 1000 * 60 * 60 * 24 * 3; // 3 days
@ -190,6 +190,10 @@ public class InAppPurchaseHelper {
return false; return false;
} }
private BillingManager getBillingManager() {
return billingManager;
}
private void exec(final @NonNull InAppPurchaseTaskType taskType, final @NonNull InAppRunnable runnable) { private void exec(final @NonNull InAppPurchaseTaskType taskType, final @NonNull InAppRunnable runnable) {
if (isDeveloperVersion || !Version.isGooglePlayEnabled(ctx)) { if (isDeveloperVersion || !Version.isGooglePlayEnabled(ctx)) {
return; return;
@ -218,16 +222,17 @@ public class InAppPurchaseHelper {
public void onBillingClientSetupFinished() { public void onBillingClientSetupFinished() {
logDebug("Setup finished."); logDebug("Setup finished.");
if (!billingManager.isIsServiceConnected()) { BillingManager billingManager = getBillingManager();
// Oh noes, there was a problem. // Have we been disposed of in the meantime? If so, quit.
//complain("Problem setting up in-app billing: " + result); if (billingManager == null) {
notifyError(taskType, billingManager.getBillingClientResponseMessage());
stop(true); stop(true);
return; return;
} }
// Have we been disposed of in the meantime? If so, quit. if (!billingManager.isServiceConnected()) {
if (billingManager == null) { // Oh noes, there was a problem.
//complain("Problem setting up in-app billing: " + result);
notifyError(taskType, billingManager.getBillingClientResponseMessage());
stop(true); stop(true);
return; return;
} }
@ -242,6 +247,7 @@ public class InAppPurchaseHelper {
@Override @Override
public void onPurchasesUpdated(final List<Purchase> purchases) { public void onPurchasesUpdated(final List<Purchase> purchases) {
BillingManager billingManager = getBillingManager();
// Have we been disposed of in the meantime? If so, quit. // Have we been disposed of in the meantime? If so, quit.
if (billingManager == null) { if (billingManager == null) {
stop(true); stop(true);
@ -275,6 +281,7 @@ public class InAppPurchaseHelper {
skuSubscriptions.add(p.getSku()); skuSubscriptions.add(p.getSku());
} }
BillingManager billingManager = getBillingManager();
// Have we been disposed of in the meantime? If so, quit. // Have we been disposed of in the meantime? If so, quit.
if (billingManager == null) { if (billingManager == null) {
stop(true); stop(true);
@ -340,7 +347,12 @@ public class InAppPurchaseHelper {
if (skuDetails == null) { if (skuDetails == null) {
throw new IllegalArgumentException("Cannot find sku details"); throw new IllegalArgumentException("Cannot find sku details");
} }
billingManager.initiatePurchaseFlow(activity, skuDetails); BillingManager billingManager = getBillingManager();
if (billingManager != null) {
billingManager.initiatePurchaseFlow(activity, skuDetails);
} else {
throw new IllegalStateException("BillingManager disposed");
}
return false; return false;
} catch (Exception e) { } catch (Exception e) {
complain("Cannot launch full version purchase!"); complain("Cannot launch full version purchase!");
@ -369,7 +381,12 @@ public class InAppPurchaseHelper {
if (skuDetails == null) { if (skuDetails == null) {
throw new IllegalArgumentException("Cannot find sku details"); throw new IllegalArgumentException("Cannot find sku details");
} }
billingManager.initiatePurchaseFlow(activity, skuDetails); BillingManager billingManager = getBillingManager();
if (billingManager != null) {
billingManager.initiatePurchaseFlow(activity, skuDetails);
} else {
throw new IllegalStateException("BillingManager disposed");
}
return false; return false;
} catch (Exception e) { } catch (Exception e) {
complain("Cannot launch depth contours purchase!"); complain("Cannot launch depth contours purchase!");
@ -400,7 +417,7 @@ public class InAppPurchaseHelper {
@Nullable @Nullable
private Purchase getPurchase(@NonNull String sku) { private Purchase getPurchase(@NonNull String sku) {
BillingManager billingManager = this.billingManager; BillingManager billingManager = getBillingManager();
if (billingManager != null) { if (billingManager != null) {
List<Purchase> purchases = billingManager.getPurchases(); List<Purchase> purchases = billingManager.getPurchases();
if (purchases != null) { if (purchases != null) {
@ -420,9 +437,12 @@ public class InAppPurchaseHelper {
@NonNull @NonNull
private List<String> getAllOwnedSubscriptionSkus() { private List<String> getAllOwnedSubscriptionSkus() {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
for (Purchase p : billingManager.getPurchases()) { BillingManager billingManager = getBillingManager();
if (getInAppPurchases().getInAppSubscriptionBySku(p.getSku()) != null) { if (billingManager != null) {
result.add(p.getSku()); for (Purchase p : billingManager.getPurchases()) {
if (getInAppPurchases().getInAppSubscriptionBySku(p.getSku()) != null) {
result.add(p.getSku());
}
} }
} }
return result; return result;
@ -434,7 +454,7 @@ public class InAppPurchaseHelper {
logDebug("Query sku details finished."); logDebug("Query sku details finished.");
// Have we been disposed of in the meantime? If so, quit. // Have we been disposed of in the meantime? If so, quit.
if (billingManager == null) { if (getBillingManager() == null) {
stop(true); stop(true);
return; return;
} }
@ -727,8 +747,13 @@ public class InAppPurchaseHelper {
Activity a = activity.get(); Activity a = activity.get();
SkuDetails skuDetails = getSkuDetails(sku); SkuDetails skuDetails = getSkuDetails(sku);
if (a != null && skuDetails != null) { if (a != null && skuDetails != null) {
billingManager.setPayload(payload); BillingManager billingManager = getBillingManager();
billingManager.initiatePurchaseFlow(a, skuDetails); if (billingManager != null) {
billingManager.setPayload(payload);
billingManager.initiatePurchaseFlow(a, skuDetails);
} else {
throw new IllegalStateException("BillingManager disposed");
}
return false; return false;
} else { } else {
stop(true); stop(true);
@ -793,7 +818,12 @@ public class InAppPurchaseHelper {
public boolean run(InAppPurchaseHelper helper) { public boolean run(InAppPurchaseHelper helper) {
logDebug("Setup successful. Querying inventory."); logDebug("Setup successful. Querying inventory.");
try { try {
billingManager.queryPurchases(); BillingManager billingManager = getBillingManager();
if (billingManager != null) {
billingManager.queryPurchases();
} else {
throw new IllegalStateException("BillingManager disposed");
}
return false; return false;
} catch (Exception e) { } catch (Exception e) {
logError("queryInventoryAsync Error", e); logError("queryInventoryAsync Error", e);
@ -820,7 +850,7 @@ public class InAppPurchaseHelper {
logDebug("Purchase finished: " + purchase); logDebug("Purchase finished: " + purchase);
// if we were disposed of in the meantime, quit. // if we were disposed of in the meantime, quit.
if (billingManager == null) { if (getBillingManager() == null) {
stop(true); stop(true);
return; return;
} }
@ -886,6 +916,7 @@ public class InAppPurchaseHelper {
private void stop(boolean taskDone) { private void stop(boolean taskDone) {
logDebug("Destroying helper."); logDebug("Destroying helper.");
BillingManager billingManager = getBillingManager();
if (billingManager != null) { if (billingManager != null) {
if (taskDone) { if (taskDone) {
processingTask = false; processingTask = false;
@ -893,7 +924,7 @@ public class InAppPurchaseHelper {
if (!processingTask) { if (!processingTask) {
activeTask = null; activeTask = null;
billingManager.destroy(); billingManager.destroy();
billingManager = null; this.billingManager = null;
} }
} else { } else {
processingTask = false; processingTask = false;

View file

@ -226,7 +226,7 @@ public class BillingManager implements PurchasesUpdatedListener {
executeServiceRequest(consumeRequest); executeServiceRequest(consumeRequest);
} }
public boolean isIsServiceConnected() { public boolean isServiceConnected() {
return mIsServiceConnected; return mIsServiceConnected;
} }