Skip to content

Commit

Permalink
Update retry logic for new api
Browse files Browse the repository at this point in the history
  • Loading branch information
deeppandya committed Sep 19, 2023
1 parent 3dabeb9 commit 7eaef45
Showing 1 changed file with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
});
Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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(
Expand All @@ -344,4 +330,52 @@ private PurchasesUpdatedListener getPurchasesUpdatedListener(Context context) {
}
};
}

private int maxTries;
private int tries;
private boolean isConnectionEstablished;
private void retryBillingServiceConnection(
MutableLiveData<Boolean> 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);
}
}

0 comments on commit 7eaef45

Please sign in to comment.