From 7eaef45e7dc3b10652dc3ea4d4c82b8adf55dbdd Mon Sep 17 00:00:00 2001 From: deeppandya Date: Tue, 19 Sep 2023 19:12:11 -0400 Subject: [PATCH] Update retry logic for new api --- .../vpn/billing/InAppPurchaseWrapper.java | 78 +++++++++++++------ 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/vpn/billing/InAppPurchaseWrapper.java b/android/java/org/chromium/chrome/browser/vpn/billing/InAppPurchaseWrapper.java index fc8dc5f85768..55d1b8fd43ef 100644 --- a/android/java/org/chromium/chrome/browser/vpn/billing/InAppPurchaseWrapper.java +++ b/android/java/org/chromium/chrome/browser/vpn/billing/InAppPurchaseWrapper.java @@ -45,7 +45,6 @@ public class InAppPurchaseWrapper { public static final String RELEASE_MONTHLY_SUBSCRIPTION = "brave.vpn.monthly"; public static final String RELEASE_YEARLY_SUBSCRIPTION = "brave.vpn.yearly"; private BillingClient mBillingClient; - private int mRetryCount; private static volatile InAppPurchaseWrapper sInAppPurchaseWrapper; private static Object sMutex = new Object(); @@ -105,26 +104,20 @@ private void startBillingServiceConnection( if (!mBillingClient.isReady()) { try { mBillingClient.startConnection(new BillingClientStateListener() { - private int mRetryCount; @Override public void onBillingServiceDisconnected() { - mRetryCount++; - if (mRetryCount <= 3) { - endConnection(); - startBillingServiceConnection(billingClientConnectionState); - } + retryBillingServiceConnection(billingClientConnectionState); } @Override public void onBillingSetupFinished(@NonNull BillingResult billingResult) { - boolean isConnectionEstablished = billingResult.getResponseCode() - == BillingClient.BillingResponseCode.OK; - if (billingClientConnectionState != null) { - billingClientConnectionState.postValue(isConnectionEstablished); - } - if (isConnectionEstablished) { - mRetryCount = 0; + if (billingResult.getResponseCode() + == BillingClient.BillingResponseCode.OK) { + if (billingClientConnectionState != null) { + billingClientConnectionState.postValue(true); + } } else { BraveVpnUtils.showToast(billingResult.getDebugMessage()); + retryBillingServiceConnection(billingClientConnectionState); } } }); @@ -269,7 +262,6 @@ public void initiatePurchase(Activity activity, ProductDetails productDetails) { } public void processPurchases(Context context, Purchase activePurchase) { - // Log.e(TAG, "processPurchases"); acknowledgePurchase(context, activePurchase); } @@ -291,7 +283,7 @@ private void acknowledgePurchase(Context context, Purchase purchase) { try { activity = BraveActivity.getBraveActivity(); } catch (BraveActivity.BraveActivityNotFoundException e) { - Log.e(TAG, "acknowledgePurchase " + e); + Log.e(TAG, "acknowledgePurchase " + e.getMessage()); } if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { @@ -318,7 +310,6 @@ private PurchasesUpdatedListener getPurchasesUpdatedListener(Context context) { endConnection(); if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { if (purchases != null) { - mRetryCount = 0; for (Purchase purchase : purchases) { if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) { processPurchases(context, purchase); @@ -329,11 +320,6 @@ private PurchasesUpdatedListener getPurchasesUpdatedListener(Context context) { == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) { BraveVpnUtils.showToast( context.getResources().getString(R.string.already_subscribed)); - } else if (billingResult.getResponseCode() - == BillingClient.BillingResponseCode.SERVICE_DISCONNECTED - && mRetryCount < 5) { - startBillingServiceConnection(null); - mRetryCount++; } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) { BraveVpnUtils.showToast( @@ -344,4 +330,52 @@ private PurchasesUpdatedListener getPurchasesUpdatedListener(Context context) { } }; } + + private int maxTries; + private int tries; + private boolean isConnectionEstablished; + private void retryBillingServiceConnection( + MutableLiveData billingClientConnectionState) { + maxTries = 3; + tries = 1; + isConnectionEstablished = false; + do { + try { + // End existing connection if any before we start another connection + endConnection(); + + Context context = ContextUtils.getApplicationContext(); + + mBillingClient = BillingClient.newBuilder(context) + .enablePendingPurchases() + .setListener(getPurchasesUpdatedListener(context)) + .build(); + + mBillingClient.startConnection(new BillingClientStateListener() { + @Override + public void onBillingServiceDisconnected() { + if (tries == maxTries && billingClientConnectionState != null) { + billingClientConnectionState.postValue(false); + } + } + @Override + public void onBillingSetupFinished(@NonNull BillingResult billingResult) { + if (billingResult.getResponseCode() + == BillingClient.BillingResponseCode.OK) { + isConnectionEstablished = true; + if (billingClientConnectionState != null) { + billingClientConnectionState.postValue(true); + } + } else { + BraveVpnUtils.showToast(billingResult.getDebugMessage()); + } + } + }); + } catch (Exception ex) { + Log.e(TAG, "retryBillingServiceConnection " + ex.getMessage()); + } finally { + tries++; + } + } while (tries <= maxTries && !isConnectionEstablished); + } }