From 3dabeb93deb11b09c3950d743245e135aed31369 Mon Sep 17 00:00:00 2001 From: deeppandya Date: Tue, 19 Sep 2023 17:20:31 -0400 Subject: [PATCH] Refactor end connection to InAppPurchaseWrapper only --- .../chrome/browser/app/BraveActivity.java | 8 +- .../BraveTabbedAppMenuPropertiesDelegate.java | 1 - .../settings/BraveMainPreferencesBase.java | 10 +- .../browser/settings/BraveVpnPreferences.java | 21 +- .../activities/BraveVpnParentActivity.java | 18 +- .../vpn/activities/BraveVpnPlansActivity.java | 31 +-- .../activities/BraveVpnProfileActivity.java | 46 +---- .../vpn/billing/InAppPurchaseWrapper.java | 181 +++++++++++------- .../browser/vpn/utils/BraveVpnUtils.java | 20 +- android/java/res/values/brave_styles.xml | 2 + 10 files changed, 147 insertions(+), 191 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/app/BraveActivity.java b/android/java/org/chromium/chrome/browser/app/BraveActivity.java index e96185e95991..9c68af7201e0 100644 --- a/android/java/org/chromium/chrome/browser/app/BraveActivity.java +++ b/android/java/org/chromium/chrome/browser/app/BraveActivity.java @@ -392,13 +392,7 @@ public boolean onMenuOrKeyboardAction(int id, boolean fromMenu) { BraveVpnUtils.showProgressDialog(BraveActivity.this, getResources().getString(R.string.vpn_connect_text)); if (BraveVpnPrefUtils.isSubscriptionPurchase()) { - MutableLiveData _billingConnectionState = - new MutableLiveData(); - LiveData billingConnectionState = _billingConnectionState; - InAppPurchaseWrapper.getInstance().startBillingServiceConnection( - BraveActivity.this, _billingConnectionState); - LiveDataUtil.observeOnce(billingConnectionState, - isConnected -> { verifySubscription(); }); + verifySubscription(); } else { BraveVpnUtils.dismissProgressDialog(); BraveVpnUtils.openBraveVpnPlansActivity(BraveActivity.this); diff --git a/android/java/org/chromium/chrome/browser/appmenu/BraveTabbedAppMenuPropertiesDelegate.java b/android/java/org/chromium/chrome/browser/appmenu/BraveTabbedAppMenuPropertiesDelegate.java index fcb3eedd9ee2..f94fd7a22668 100644 --- a/android/java/org/chromium/chrome/browser/appmenu/BraveTabbedAppMenuPropertiesDelegate.java +++ b/android/java/org/chromium/chrome/browser/appmenu/BraveTabbedAppMenuPropertiesDelegate.java @@ -46,7 +46,6 @@ import org.chromium.chrome.browser.ui.appmenu.AppMenuDelegate; import org.chromium.chrome.browser.ui.appmenu.AppMenuHandler; import org.chromium.chrome.browser.ui.messages.snackbar.SnackbarManager; -import org.chromium.chrome.browser.vpn.billing.InAppPurchaseWrapper; import org.chromium.chrome.browser.vpn.utils.BraveVpnProfileUtils; import org.chromium.chrome.browser.vpn.utils.BraveVpnUtils; import org.chromium.chrome.features.start_surface.StartSurface; diff --git a/android/java/org/chromium/chrome/browser/settings/BraveMainPreferencesBase.java b/android/java/org/chromium/chrome/browser/settings/BraveMainPreferencesBase.java index 0939ce3da505..2289b5970b59 100644 --- a/android/java/org/chromium/chrome/browser/settings/BraveMainPreferencesBase.java +++ b/android/java/org/chromium/chrome/browser/settings/BraveMainPreferencesBase.java @@ -33,7 +33,6 @@ import org.chromium.chrome.browser.rate.BraveRateDialogFragment; import org.chromium.chrome.browser.search_engines.TemplateUrlServiceFactory; import org.chromium.chrome.browser.toolbar.bottom.BottomToolbarConfiguration; -import org.chromium.chrome.browser.vpn.billing.InAppPurchaseWrapper; import org.chromium.chrome.browser.vpn.utils.BraveVpnPrefUtils; import org.chromium.chrome.browser.vpn.utils.BraveVpnUtils; import org.chromium.chrome.browser.widget.quickactionsearchandbookmark.utils.BraveSearchWidgetUtils; @@ -278,10 +277,11 @@ private void rearrangePreferenceOrders() { preference.setOrder(++generalOrder); } - if (BraveSearchWidgetUtils.isRequestPinAppWidgetSupported()) + if (BraveSearchWidgetUtils.isRequestPinAppWidgetSupported()) { findPreference(PREF_HOME_SCREEN_WIDGET).setOrder(++generalOrder); - else + } else { removePreferenceIfPresent(PREF_HOME_SCREEN_WIDGET); + } findPreference(PREF_PASSWORDS).setOrder(++generalOrder); findPreference(PREF_SYNC).setOrder(++generalOrder); @@ -431,9 +431,9 @@ public boolean onPreferenceClick(Preference preference) { // TODO(simonhong): Make this static public with proper class. private int dp2px(int dp) { - final float DP_PER_INCH_MDPI = 160f; + final float dpPerInchMdpi = 160f; DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); - float px = dp * (metrics.densityDpi / DP_PER_INCH_MDPI); + float px = dp * (metrics.densityDpi / dpPerInchMdpi); return Math.round(px); } diff --git a/android/java/org/chromium/chrome/browser/settings/BraveVpnPreferences.java b/android/java/org/chromium/chrome/browser/settings/BraveVpnPreferences.java index cd6dc63465eb..e9546dde3cd0 100644 --- a/android/java/org/chromium/chrome/browser/settings/BraveVpnPreferences.java +++ b/android/java/org/chromium/chrome/browser/settings/BraveVpnPreferences.java @@ -120,13 +120,7 @@ public boolean onPreferenceClick(Preference preference) { BraveVpnUtils.showProgressDialog( getActivity(), getResources().getString(R.string.vpn_connect_text)); if (BraveVpnPrefUtils.isSubscriptionPurchase()) { - MutableLiveData _billingConnectionState = - new MutableLiveData(); - LiveData billingConnectionState = _billingConnectionState; - InAppPurchaseWrapper.getInstance().startBillingServiceConnection( - getActivity(), _billingConnectionState); - LiveDataUtil.observeOnce(billingConnectionState, - isConnected -> { verifyPurchase(true); }); + verifyPurchase(true); } else { BraveVpnUtils.openBraveVpnPlansActivity(getActivity()); BraveVpnUtils.dismissProgressDialog(); @@ -257,12 +251,7 @@ public void onResume() { mBraveVpnPrefModel = new BraveVpnPrefModel(); BraveVpnNativeWorker.getInstance().getSubscriberCredentialV12(); } else { - MutableLiveData _billingConnectionState = new MutableLiveData(); - LiveData billingConnectionState = _billingConnectionState; - InAppPurchaseWrapper.getInstance().startBillingServiceConnection( - getActivity(), _billingConnectionState); - LiveDataUtil.observeOnce( - billingConnectionState, isConnected -> { verifyPurchase(false); }); + verifyPurchase(false); } } else if (BraveVpnUtils.mUpdateProfileAfterSplitTunnel) { BraveVpnUtils.mUpdateProfileAfterSplitTunnel = false; @@ -588,10 +577,4 @@ private void resetConfiguration() { } }, INVALIDATE_CREDENTIAL_TIMER_COUNT); } - - @Override - public void onDestroy() { - InAppPurchaseWrapper.getInstance().endConnection(); - super.onDestroy(); - } } diff --git a/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnParentActivity.java b/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnParentActivity.java index 229ce088da29..731b2847fd98 100644 --- a/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnParentActivity.java +++ b/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnParentActivity.java @@ -19,11 +19,8 @@ import com.wireguard.crypto.KeyPair; import org.chromium.base.Log; -import org.chromium.base.supplier.OneshotSupplier; -import org.chromium.base.supplier.OneshotSupplierImpl; import org.chromium.chrome.R; import org.chromium.chrome.browser.init.AsyncInitializationActivity; -import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.util.LiveDataUtil; import org.chromium.chrome.browser.vpn.BraveVpnNativeWorker; import org.chromium.chrome.browser.vpn.BraveVpnObserver; @@ -42,28 +39,20 @@ public abstract class BraveVpnParentActivity private static final String TAG = "BraveVPN"; public boolean mIsVerification; protected BraveVpnPrefModel mBraveVpnPrefModel; - private final OneshotSupplierImpl mProfileSupplier; abstract void showRestoreMenu(boolean shouldShowRestore); abstract void updateProfileView(); - public BraveVpnParentActivity() { - mProfileSupplier = new OneshotSupplierImpl<>(); - } - // Pass @{code ActivityResultRegistry} reference explicitly to avoid crash // https://github.com/brave/brave-browser/issues/31882 ActivityResultLauncher mIntentActivityResultLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), getActivityResultRegistry(), result -> { BraveVpnUtils.dismissProgressDialog(); - Log.e("InAppPurchaseWrapper", "StartActivityForResult"); if (result.getResultCode() == RESULT_OK) { - Log.e("InAppPurchaseWrapper", "RESULT_OK"); BraveVpnProfileUtils.getInstance().startVpn(BraveVpnParentActivity.this); BraveVpnUtils.showVpnConfirmDialog(this); } else if (result.getResultCode() == RESULT_CANCELED) { - Log.e("InAppPurchaseWrapper", "RESULT_CANCELED"); if (BraveVpnProfileUtils.getInstance().isVPNRunning(this)) { BraveVpnUtils.showVpnAlwaysOnErrorDialog(this); } else { @@ -77,7 +66,6 @@ public BraveVpnParentActivity() { @Override public void finishNativeInitialization() { super.finishNativeInitialization(); - mProfileSupplier.set(Profile.getLastUsedRegularProfile()); } protected void verifySubscription() { @@ -98,6 +86,8 @@ protected void verifySubscription() { if (!mIsVerification) { BraveVpnApiResponseUtils.queryPurchaseFailed( BraveVpnParentActivity.this); + } else { + showRestoreMenu(false); } BraveVpnUtils.dismissProgressDialog(); } @@ -216,8 +206,4 @@ public void run() { } }.start(); } - - public OneshotSupplier getProfileSupplier() { - return mProfileSupplier; - } } diff --git a/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnPlansActivity.java b/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnPlansActivity.java index 1ce81df352da..829eb21154a3 100644 --- a/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnPlansActivity.java +++ b/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnPlansActivity.java @@ -6,7 +6,6 @@ package org.chromium.chrome.browser.vpn.activities; import android.graphics.Paint; -import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -16,14 +15,11 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.widget.Toolbar; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; import androidx.viewpager.widget.ViewPager; import com.google.android.material.tabs.TabLayout; import org.chromium.chrome.R; -import org.chromium.chrome.browser.firstrun.BraveFirstRunFlowSequencer; import org.chromium.chrome.browser.util.LiveDataUtil; import org.chromium.chrome.browser.vpn.BraveVpnNativeWorker; import org.chromium.chrome.browser.vpn.adapters.BraveVpnPlanPagerAdapter; @@ -31,7 +27,6 @@ import org.chromium.chrome.browser.vpn.utils.BraveVpnUtils; public class BraveVpnPlansActivity extends BraveVpnParentActivity { - private BraveFirstRunFlowSequencer mFirstRunFlowSequencer; private ProgressBar mMonthlyPlanProgress; private ProgressBar mYearlyPlanProgress; private LinearLayout mPlanLayout; @@ -43,9 +38,6 @@ public class BraveVpnPlansActivity extends BraveVpnParentActivity { private LinearLayout mYearlySelectorLayout; private TextView mYearlySubscriptionAmountText; - private MutableLiveData _billingConnectionState = new MutableLiveData(); - private LiveData billingConnectionState = _billingConnectionState; - @Override public void onResumeWithNative() { super.onResumeWithNative(); @@ -71,6 +63,7 @@ private void initializeViews() { actionBar.setTitle(getResources().getString(R.string.brave_vpn)); mMonthlyPlanProgress = findViewById(R.id.monthly_plan_progress); + mYearlyPlanProgress = findViewById(R.id.yearly_plan_progress); mPlanLayout = findViewById(R.id.plan_layout); @@ -95,19 +88,9 @@ private void initializeViews() { mYearlySelectorLayout = findViewById(R.id.yearly_selector_layout); } - @Override - public void onDestroy() { - InAppPurchaseWrapper.getInstance().endConnection(); - super.onDestroy(); - } - @Override public void finishNativeInitialization() { super.finishNativeInitialization(); - InAppPurchaseWrapper.getInstance().startBillingServiceConnection( - BraveVpnPlansActivity.this, _billingConnectionState); - LiveDataUtil.observeOnce(billingConnectionState, isConnected -> { - if (isConnected) { // Check for an active subscription to show restore BraveVpnUtils.showProgressDialog(BraveVpnPlansActivity.this, getResources().getString(R.string.vpn_connect_text)); @@ -116,7 +99,6 @@ public void finishNativeInitialization() { // Set up monthly subscription mMonthlyPlanProgress.setVisibility(View.VISIBLE); - InAppPurchaseWrapper.getInstance().queryProductDetailsAsync(); LiveDataUtil.observeOnce( InAppPurchaseWrapper.getInstance().getMonthlyProductDetails(), monthlyProductDetails -> { @@ -226,8 +208,6 @@ public void run() { }); } }); - } - }); } @Override @@ -262,13 +242,7 @@ public boolean onOptionsItemSelected(MenuItem item) { @Override protected void triggerLayoutInflation() { - mFirstRunFlowSequencer = new BraveFirstRunFlowSequencer(this, getProfileSupplier()) { - @Override - public void onFlowIsKnown(Bundle freProperties) { - initializeViews(); - } - }; - mFirstRunFlowSequencer.start(); + initializeViews(); onInitialLayoutInflationComplete(); } @@ -280,6 +254,7 @@ public boolean shouldStartGpuProcess() { @Override public void showRestoreMenu(boolean shouldShowRestore) { this.mShouldShowRestoreMenu = shouldShowRestore; + InAppPurchaseWrapper.getInstance().queryProductDetailsAsync(); invalidateOptionsMenu(); } diff --git a/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnProfileActivity.java b/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnProfileActivity.java index 4d856e97b82b..ac2e5f09f485 100644 --- a/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnProfileActivity.java +++ b/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnProfileActivity.java @@ -7,7 +7,6 @@ package org.chromium.chrome.browser.vpn.activities; -import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.widget.Button; @@ -17,19 +16,13 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.widget.Toolbar; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; import org.chromium.chrome.R; -import org.chromium.chrome.browser.firstrun.BraveFirstRunFlowSequencer; -import org.chromium.chrome.browser.util.LiveDataUtil; import org.chromium.chrome.browser.vpn.BraveVpnNativeWorker; -import org.chromium.chrome.browser.vpn.billing.InAppPurchaseWrapper; import org.chromium.chrome.browser.vpn.models.BraveVpnPrefModel; import org.chromium.chrome.browser.vpn.utils.BraveVpnUtils; public class BraveVpnProfileActivity extends BraveVpnParentActivity { - private BraveFirstRunFlowSequencer mFirstRunFlowSequencer; private TextView mProfileTitle; private TextView mProfileText; private Button mInstallVpnButton; @@ -72,18 +65,12 @@ private void initializeViews() { public void onClick(View v) { BraveVpnUtils.showProgressDialog(BraveVpnProfileActivity.this, getResources().getString(R.string.vpn_connect_text)); - MutableLiveData _billingConnectionState = new MutableLiveData(); - LiveData billingConnectionState = _billingConnectionState; - InAppPurchaseWrapper.getInstance().startBillingServiceConnection( - BraveVpnProfileActivity.this, _billingConnectionState); - LiveDataUtil.observeOnce(billingConnectionState, isConnected -> { - if (BraveVpnNativeWorker.getInstance().isPurchasedUser()) { - mBraveVpnPrefModel = new BraveVpnPrefModel(); - BraveVpnNativeWorker.getInstance().getSubscriberCredentialV12(); - } else { - verifySubscription(); - } - }); + if (BraveVpnNativeWorker.getInstance().isPurchasedUser()) { + mBraveVpnPrefModel = new BraveVpnPrefModel(); + BraveVpnNativeWorker.getInstance().getSubscriberCredentialV12(); + } else { + verifySubscription(); + } } }); @@ -101,12 +88,7 @@ public void finishNativeInitialization() { super.finishNativeInitialization(); if (getIntent() != null && getIntent().getBooleanExtra(BraveVpnUtils.VERIFY_CREDENTIALS_FAILED, false)) { - MutableLiveData _billingConnectionState = new MutableLiveData(); - LiveData billingConnectionState = _billingConnectionState; - InAppPurchaseWrapper.getInstance().startBillingServiceConnection( - BraveVpnProfileActivity.this, _billingConnectionState); - LiveDataUtil.observeOnce( - billingConnectionState, isConnected -> { verifySubscription(); }); + verifySubscription(); } } @@ -120,13 +102,7 @@ public boolean onOptionsItemSelected(MenuItem item) { @Override protected void triggerLayoutInflation() { - mFirstRunFlowSequencer = new BraveFirstRunFlowSequencer(this, getProfileSupplier()) { - @Override - public void onFlowIsKnown(Bundle freProperties) { - initializeViews(); - } - }; - mFirstRunFlowSequencer.start(); + initializeViews(); onInitialLayoutInflationComplete(); } @@ -145,10 +121,4 @@ public void updateProfileView() { mInstallVpnButton.setText(getResources().getString(R.string.accept_connection_request)); mContactSupportButton.setVisibility(View.GONE); } - - @Override - public void onDestroy() { - InAppPurchaseWrapper.getInstance().endConnection(); - super.onDestroy(); - } } 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 10bbb0d2ac96..fc8dc5f85768 100644 --- a/android/java/org/chromium/chrome/browser/vpn/billing/InAppPurchaseWrapper.java +++ b/android/java/org/chromium/chrome/browser/vpn/billing/InAppPurchaseWrapper.java @@ -26,7 +26,9 @@ import org.chromium.base.ContextUtils; import org.chromium.base.Log; import org.chromium.chrome.R; +import org.chromium.chrome.browser.app.BraveActivity; import org.chromium.chrome.browser.util.BraveConstants; +import org.chromium.chrome.browser.util.LiveDataUtil; import org.chromium.chrome.browser.vpn.utils.BraveVpnPrefUtils; import org.chromium.chrome.browser.vpn.utils.BraveVpnUtils; @@ -86,13 +88,16 @@ public boolean isMonthlySubscription(String productId) { || productId.equals(RELEASE_MONTHLY_SUBSCRIPTION); } - public void startBillingServiceConnection( - Context context, MutableLiveData billingClientConnectionState) { - Log.e(TAG, " after startBillingServiceConnection"); + private void startBillingServiceConnection( + MutableLiveData billingClientConnectionState) { + Context context = ContextUtils.getApplicationContext(); if (!BraveVpnUtils.isVpnFeatureSupported(context)) { return; } - Log.e(TAG, " after startBillingServiceConnection"); + + // End existing connection if any before we start another connection + endConnection(); + mBillingClient = BillingClient.newBuilder(context) .enablePendingPurchases() .setListener(getPurchasesUpdatedListener(context)) @@ -105,14 +110,14 @@ public void startBillingServiceConnection( public void onBillingServiceDisconnected() { mRetryCount++; if (mRetryCount <= 3) { - startBillingServiceConnection(context, billingClientConnectionState); + endConnection(); + startBillingServiceConnection(billingClientConnectionState); } } @Override public void onBillingSetupFinished(@NonNull BillingResult billingResult) { boolean isConnectionEstablished = billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK; - Log.e(TAG, "onBillingSetupFinished"); if (billingClientConnectionState != null) { billingClientConnectionState.postValue(isConnectionEstablished); } @@ -138,10 +143,10 @@ public void onBillingSetupFinished(@NonNull BillingResult billingResult) { } } - public void endConnection() { + private void endConnection() { if (mBillingClient != null) { - Log.e(TAG, "endConnection"); mBillingClient.endConnection(); + mBillingClient = null; } } @@ -171,45 +176,73 @@ public void queryProductDetailsAsync() { QueryProductDetailsParams queryProductDetailsParams = QueryProductDetailsParams.newBuilder().setProductList(products).build(); - mBillingClient.queryProductDetailsAsync( - queryProductDetailsParams, (billingResult, productDetailsList) -> { - if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { - for (ProductDetails productDetail : productDetailsList) { - productDetails.put(productDetail.getProductId(), productDetail); - } - setMonthlyProductDetails( - productDetails.get(getProductId(SubscriptionType.MONTHLY))); - setYearlyProductDetails( - productDetails.get(getProductId(SubscriptionType.YEARLY))); - } else { - Log.e(TAG, - "queryProductDetailsAsync failed" - + billingResult.getDebugMessage()); - BraveVpnUtils.showToast(billingResult.getDebugMessage()); - } - }); + MutableLiveData _billingConnectionState = new MutableLiveData(); + LiveData billingConnectionState = _billingConnectionState; + startBillingServiceConnection(_billingConnectionState); + LiveDataUtil.observeOnce(billingConnectionState, isConnected -> { + if (isConnected) { + mBillingClient.queryProductDetailsAsync( + queryProductDetailsParams, (billingResult, productDetailsList) -> { + // End connection after getting the product details + endConnection(); + + if (billingResult.getResponseCode() + == BillingClient.BillingResponseCode.OK) { + for (ProductDetails productDetail : productDetailsList) { + productDetails.put(productDetail.getProductId(), productDetail); + } + setMonthlyProductDetails( + productDetails.get(getProductId(SubscriptionType.MONTHLY))); + setYearlyProductDetails( + productDetails.get(getProductId(SubscriptionType.YEARLY))); + } else { + Log.e(TAG, + "queryProductDetailsAsync failed" + + billingResult.getDebugMessage()); + BraveVpnUtils.showToast(billingResult.getDebugMessage()); + } + }); + } + }); } public void queryPurchases(MutableLiveData mutableActivePurchases) { - mBillingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder() - .setProductType(BillingClient.ProductType.SUBS) - .build(), - (billingResult, purchases) -> { - PurchaseModel activePurchaseModel = null; - if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { - for (Purchase purchase : purchases) { - if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) { - activePurchaseModel = new PurchaseModel(purchase.getPurchaseToken(), - purchase.getProducts().get(0).toString(), purchase); - break; + MutableLiveData _billingConnectionState = new MutableLiveData(); + LiveData billingConnectionState = _billingConnectionState; + startBillingServiceConnection(_billingConnectionState); + LiveDataUtil.observeOnce(billingConnectionState, isConnected -> { + if (isConnected) { + mBillingClient.queryPurchasesAsync( + QueryPurchasesParams.newBuilder() + .setProductType(BillingClient.ProductType.SUBS) + .build(), + (billingResult, purchases) -> { + // End connection after getting purchases + endConnection(); + + PurchaseModel activePurchaseModel = null; + if (billingResult.getResponseCode() + == BillingClient.BillingResponseCode.OK) { + for (Purchase purchase : purchases) { + if (purchase.getPurchaseState() + == Purchase.PurchaseState.PURCHASED) { + activePurchaseModel = new PurchaseModel( + purchase.getPurchaseToken(), + purchase.getProducts().get(0).toString(), purchase); + break; + } + } + } else { + Log.e(TAG, + "queryPurchases failed" + billingResult.getDebugMessage()); + BraveVpnUtils.showToast(billingResult.getDebugMessage()); } - } - } else { - Log.e(TAG, "queryPurchases failed" + billingResult.getDebugMessage()); - BraveVpnUtils.showToast(billingResult.getDebugMessage()); - } - mutableActivePurchases.postValue(activePurchaseModel); - }); + mutableActivePurchases.postValue(activePurchaseModel); + }); + } else { + mutableActivePurchases.postValue(null); + } + }); } public void initiatePurchase(Activity activity, ProductDetails productDetails) { @@ -224,12 +257,19 @@ public void initiatePurchase(Activity activity, ProductDetails productDetails) { BillingFlowParams.newBuilder() .setProductDetailsParamsList(productDetailsParamsList) .build(); - - BillingResult billingResult = mBillingClient.launchBillingFlow(activity, billingFlowParams); + MutableLiveData _billingConnectionState = new MutableLiveData(); + LiveData billingConnectionState = _billingConnectionState; + startBillingServiceConnection(_billingConnectionState); + LiveDataUtil.observeOnce(billingConnectionState, isConnected -> { + if (isConnected) { + BillingResult billingResult = + mBillingClient.launchBillingFlow(activity, billingFlowParams); + } + }); } public void processPurchases(Context context, Purchase activePurchase) { - Log.e(TAG, "processPurchases"); + // Log.e(TAG, "processPurchases"); acknowledgePurchase(context, activePurchase); } @@ -238,37 +278,46 @@ private void acknowledgePurchase(Context context, Purchase purchase) { AcknowledgePurchaseParams.newBuilder() .setPurchaseToken(purchase.getPurchaseToken()) .build(); - Log.e(TAG, " before acknowledgePurchase"); if (!purchase.isAcknowledged()) { - mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, billingResult -> { - Log.e(TAG, "acknowledgePurchase"); - if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { - Log.e(TAG, "acknowledgePurchase : BillingClient.BillingResponseCode.OK"); - BraveVpnPrefUtils.setSubscriptionPurchase(true); - BraveVpnUtils.openBraveVpnProfileActivity(context); - BraveVpnUtils.showToast( - context.getResources().getString(R.string.subscription_consumed)); - } else { - BraveVpnUtils.showToast( - context.getResources().getString(R.string.fail_to_aknowledge)); + MutableLiveData _billingConnectionState = new MutableLiveData(); + LiveData billingConnectionState = _billingConnectionState; + startBillingServiceConnection(_billingConnectionState); + LiveDataUtil.observeOnce(billingConnectionState, isConnected -> { + if (isConnected) { + mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, billingResult -> { + // End connection after getting the resposne of the purchase aknowledgment + endConnection(); + BraveActivity activity = null; + try { + activity = BraveActivity.getBraveActivity(); + } catch (BraveActivity.BraveActivityNotFoundException e) { + Log.e(TAG, "acknowledgePurchase " + e); + } + if (billingResult.getResponseCode() + == BillingClient.BillingResponseCode.OK) { + BraveVpnPrefUtils.setSubscriptionPurchase(true); + if (activity != null) { + BraveVpnUtils.openBraveVpnProfileActivity(activity); + BraveVpnUtils.showToast(activity.getResources().getString( + R.string.subscription_consumed)); + } + } else { + BraveVpnUtils.showToast( + context.getResources().getString(R.string.fail_to_aknowledge)); + } + }); } }); } else { - Log.e(TAG, " after acknowledgePurchase"); BraveVpnPrefUtils.setSubscriptionPurchase(true); } } private PurchasesUpdatedListener getPurchasesUpdatedListener(Context context) { return (billingResult, purchases) -> { - Log.e(TAG, "getPurchasesUpdatedListener"); - Log.e(TAG, - "getPurchasesUpdatedListener : billingResult.getResponseCode() : " - + billingResult.getResponseCode()); + endConnection(); if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { - Log.e(TAG, "getPurchasesUpdatedListener 1"); if (purchases != null) { - Log.e(TAG, "getPurchasesUpdatedListener 2"); mRetryCount = 0; for (Purchase purchase : purchases) { if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) { @@ -283,7 +332,7 @@ private PurchasesUpdatedListener getPurchasesUpdatedListener(Context context) { } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_DISCONNECTED && mRetryCount < 5) { - startBillingServiceConnection(context, null); + startBillingServiceConnection(null); mRetryCount++; } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) { diff --git a/android/java/org/chromium/chrome/browser/vpn/utils/BraveVpnUtils.java b/android/java/org/chromium/chrome/browser/vpn/utils/BraveVpnUtils.java index e9031b9af88c..2ceddb98e8f5 100644 --- a/android/java/org/chromium/chrome/browser/vpn/utils/BraveVpnUtils.java +++ b/android/java/org/chromium/chrome/browser/vpn/utils/BraveVpnUtils.java @@ -11,7 +11,6 @@ import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; -import android.os.Build; import android.text.TextUtils; import android.util.Pair; @@ -31,7 +30,6 @@ import org.chromium.chrome.browser.vpn.activities.BraveVpnPlansActivity; import org.chromium.chrome.browser.vpn.activities.BraveVpnProfileActivity; import org.chromium.chrome.browser.vpn.activities.BraveVpnSupportActivity; -import org.chromium.chrome.browser.vpn.billing.InAppPurchaseWrapper; import org.chromium.chrome.browser.vpn.fragments.BraveVpnAlwaysOnErrorDialogFragment; import org.chromium.chrome.browser.vpn.fragments.BraveVpnConfirmDialogFragment; import org.chromium.chrome.browser.vpn.models.BraveVpnServerRegion; @@ -74,24 +72,24 @@ public static void openBraveVpnPlansActivity(Activity activity) { activity.startActivity(braveVpnPlanIntent); } - public static void openBraveVpnProfileActivity(Context context) { - Intent braveVpnProfileIntent = new Intent(context, BraveVpnProfileActivity.class); + public static void openBraveVpnProfileActivity(Activity activity) { + Intent braveVpnProfileIntent = new Intent(activity, BraveVpnProfileActivity.class); braveVpnProfileIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); braveVpnProfileIntent.setAction(Intent.ACTION_VIEW); - context.startActivity(braveVpnProfileIntent); + activity.startActivity(braveVpnProfileIntent); } - public static void openBraveVpnSupportActivity(Context context) { - Intent braveVpnSupportIntent = new Intent(context, BraveVpnSupportActivity.class); + public static void openBraveVpnSupportActivity(Activity activity) { + Intent braveVpnSupportIntent = new Intent(activity, BraveVpnSupportActivity.class); braveVpnSupportIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); braveVpnSupportIntent.setAction(Intent.ACTION_VIEW); - context.startActivity(braveVpnSupportIntent); + activity.startActivity(braveVpnSupportIntent); } - public static void openSplitTunnelActivity(Context context) { - Intent braveVpnSupportIntent = new Intent(context, SplitTunnelActivity.class); + public static void openSplitTunnelActivity(Activity activity) { + Intent braveVpnSupportIntent = new Intent(activity, SplitTunnelActivity.class); braveVpnSupportIntent.setAction(Intent.ACTION_VIEW); - context.startActivity(braveVpnSupportIntent); + activity.startActivity(braveVpnSupportIntent); } public static void showProgressDialog(Activity activity, String message) { diff --git a/android/java/res/values/brave_styles.xml b/android/java/res/values/brave_styles.xml index 492ae590de18..189710f8d73b 100644 --- a/android/java/res/values/brave_styles.xml +++ b/android/java/res/values/brave_styles.xml @@ -246,6 +246,8 @@ @android:color/transparent @null @null + @android:color/transparent + @android:color/transparent