From 1a480afcb9454d619ef8bc8f71a3a3c89bc54460 Mon Sep 17 00:00:00 2001 From: deeppandya Date: Tue, 19 Sep 2023 11:15:25 -0400 Subject: [PATCH] Explicitly end connection for billing client --- .../chrome/browser/app/BraveActivity.java | 9 ++-- .../BraveTabbedAppMenuPropertiesDelegate.java | 5 +- .../settings/BraveMainPreferencesBase.java | 6 +-- .../browser/settings/BraveVpnPreferences.java | 8 +++- .../activities/BraveVpnParentActivity.java | 3 ++ .../vpn/activities/BraveVpnPlansActivity.java | 6 +++ .../activities/BraveVpnProfileActivity.java | 6 +++ .../vpn/billing/InAppPurchaseWrapper.java | 47 +++++++++++-------- .../browser/vpn/utils/BraveVpnUtils.java | 32 +++++++++---- 9 files changed, 78 insertions(+), 44 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/app/BraveActivity.java b/android/java/org/chromium/chrome/browser/app/BraveActivity.java index 01671cd76266..e96185e95991 100644 --- a/android/java/org/chromium/chrome/browser/app/BraveActivity.java +++ b/android/java/org/chromium/chrome/browser/app/BraveActivity.java @@ -294,11 +294,8 @@ public BraveActivity() {} public void onResumeWithNative() { super.onResumeWithNative(); BraveActivityJni.get().restartStatsUpdater(); - if (BraveVpnUtils.isBraveVpnFeatureEnable()) { - InAppPurchaseWrapper.getInstance().startBillingServiceConnection( - BraveActivity.this, null); + if (BraveVpnUtils.isVpnFeatureSupported(BraveActivity.this)) { BraveVpnNativeWorker.getInstance().addObserver(this); - BraveVpnUtils.reportBackgroundUsageP3A(); } Profile profile = getCurrentTabModel().getProfile(); @@ -331,7 +328,7 @@ public void onResumeWithNative() { @Override public void onPauseWithNative() { - if (BraveVpnUtils.isBraveVpnFeatureEnable()) { + if (BraveVpnUtils.isVpnFeatureSupported(BraveActivity.this)) { BraveVpnNativeWorker.getInstance().removeObserver(this); } Profile profile = getCurrentTabModel().getProfile(); @@ -1141,7 +1138,7 @@ private void checkForVpnCallout() { String countryCode = Locale.getDefault().getCountry(); if (!countryCode.equals(BraveConstants.INDIA_COUNTRY_CODE) - && BraveVpnUtils.isBraveVpnFeatureEnable()) { + && BraveVpnUtils.isVpnFeatureSupported(BraveActivity.this)) { if (BraveVpnPrefUtils.shouldShowCallout() && !BraveVpnPrefUtils.isSubscriptionPurchase() && (SharedPreferencesManager.getInstance().readInt( BravePreferenceKeys.BRAVE_APP_OPEN_COUNT) diff --git a/android/java/org/chromium/chrome/browser/appmenu/BraveTabbedAppMenuPropertiesDelegate.java b/android/java/org/chromium/chrome/browser/appmenu/BraveTabbedAppMenuPropertiesDelegate.java index ea241df26b26..fcb3eedd9ee2 100644 --- a/android/java/org/chromium/chrome/browser/appmenu/BraveTabbedAppMenuPropertiesDelegate.java +++ b/android/java/org/chromium/chrome/browser/appmenu/BraveTabbedAppMenuPropertiesDelegate.java @@ -90,8 +90,7 @@ public void prepareMenu(Menu menu, AppMenuHandler handler) { mMenu = menu; - if (BraveVpnUtils.isBraveVpnFeatureEnable() - && InAppPurchaseWrapper.getInstance().isSubscriptionSupported()) { + if (BraveVpnUtils.isVpnFeatureSupported(mContext)) { SubMenu vpnSubMenu = menu.findItem(R.id.request_brave_vpn_row_menu_id).getSubMenu(); MenuItem braveVpnSubMenuItem = vpnSubMenu.findItem(R.id.request_brave_vpn_id); if (shouldShowIconBeforeItem()) { @@ -257,7 +256,7 @@ public void onMenuDismissed() { mMenu.removeItem(R.id.brave_playlist_id); mMenu.removeItem(R.id.brave_speedreader_id); mMenu.removeItem(R.id.exit_id); - if (BraveVpnUtils.isBraveVpnFeatureEnable()) { + if (BraveVpnUtils.isVpnFeatureSupported(mContext)) { mMenu.removeItem(R.id.request_brave_vpn_row_menu_id); } } diff --git a/android/java/org/chromium/chrome/browser/settings/BraveMainPreferencesBase.java b/android/java/org/chromium/chrome/browser/settings/BraveMainPreferencesBase.java index 5858dafbfe68..0939ce3da505 100644 --- a/android/java/org/chromium/chrome/browser/settings/BraveMainPreferencesBase.java +++ b/android/java/org/chromium/chrome/browser/settings/BraveMainPreferencesBase.java @@ -235,8 +235,7 @@ private void rearrangePreferenceOrders() { int firstSectionOrder = 0; if (BraveVpnPrefUtils.shouldShowCallout() && !BraveVpnPrefUtils.isSubscriptionPurchase() - && BraveVpnUtils.isBraveVpnFeatureEnable() - && InAppPurchaseWrapper.getInstance().isSubscriptionSupported()) { + && BraveVpnUtils.isVpnFeatureSupported(getActivity())) { if (getActivity() != null && mVpnCalloutPreference == null) { mVpnCalloutPreference = new VpnCalloutPreference(getActivity()); } @@ -264,8 +263,7 @@ private void rearrangePreferenceOrders() { removePreferenceIfPresent(PREF_BRAVE_PLAYLIST); } - if (BraveVpnUtils.isBraveVpnFeatureEnable() - && InAppPurchaseWrapper.getInstance().isSubscriptionSupported()) { + if (BraveVpnUtils.isVpnFeatureSupported(getActivity())) { findPreference(PREF_BRAVE_VPN).setOrder(++firstSectionOrder); } else { removePreferenceIfPresent(PREF_BRAVE_VPN); diff --git a/android/java/org/chromium/chrome/browser/settings/BraveVpnPreferences.java b/android/java/org/chromium/chrome/browser/settings/BraveVpnPreferences.java index abb2643c5097..cd6dc63465eb 100644 --- a/android/java/org/chromium/chrome/browser/settings/BraveVpnPreferences.java +++ b/android/java/org/chromium/chrome/browser/settings/BraveVpnPreferences.java @@ -94,8 +94,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { getActivity().setTitle(R.string.brave_firewall_vpn); SettingsUtils.addPreferencesFromResource(this, R.xml.brave_vpn_preferences); - InAppPurchaseWrapper.getInstance().startBillingServiceConnection(getActivity(), null); - mVpnSwitch = (ChromeSwitchPreference) findPreference(PREF_VPN_SWITCH); mVpnSwitch.setChecked( BraveVpnProfileUtils.getInstance().isBraveVPNConnected(getActivity())); @@ -590,4 +588,10 @@ 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 c1a460884376..229ce088da29 100644 --- a/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnParentActivity.java +++ b/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnParentActivity.java @@ -57,10 +57,13 @@ public BraveVpnParentActivity() { 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 { 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 d093213205d8..1ce81df352da 100644 --- a/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnPlansActivity.java +++ b/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnPlansActivity.java @@ -95,6 +95,12 @@ 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(); 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 a447e2446c1f..4d856e97b82b 100644 --- a/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnProfileActivity.java +++ b/android/java/org/chromium/chrome/browser/vpn/activities/BraveVpnProfileActivity.java @@ -145,4 +145,10 @@ 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 9d163a7e185e..10bbb0d2ac96 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,6 @@ import org.chromium.base.ContextUtils; import org.chromium.base.Log; import org.chromium.chrome.R; -import org.chromium.chrome.browser.BraveRewardsNativeWorker; import org.chromium.chrome.browser.util.BraveConstants; import org.chromium.chrome.browser.vpn.utils.BraveVpnPrefUtils; import org.chromium.chrome.browser.vpn.utils.BraveVpnUtils; @@ -87,15 +86,13 @@ public boolean isMonthlySubscription(String productId) { || productId.equals(RELEASE_MONTHLY_SUBSCRIPTION); } - public boolean isBillingClientReady() { - return mBillingClient != null && mBillingClient.isReady(); - } - public void startBillingServiceConnection( Context context, MutableLiveData billingClientConnectionState) { - if (!BraveVpnUtils.isBraveVpnFeatureEnable()) { + Log.e(TAG, " after startBillingServiceConnection"); + if (!BraveVpnUtils.isVpnFeatureSupported(context)) { return; } + Log.e(TAG, " after startBillingServiceConnection"); mBillingClient = BillingClient.newBuilder(context) .enablePendingPurchases() .setListener(getPurchasesUpdatedListener(context)) @@ -113,12 +110,16 @@ public void onBillingServiceDisconnected() { } @Override public void onBillingSetupFinished(@NonNull BillingResult billingResult) { - if (billingResult.getResponseCode() - == BillingClient.BillingResponseCode.OK) { + boolean isConnectionEstablished = billingResult.getResponseCode() + == BillingClient.BillingResponseCode.OK; + Log.e(TAG, "onBillingSetupFinished"); + if (billingClientConnectionState != null) { + billingClientConnectionState.postValue(isConnectionEstablished); + } + if (isConnectionEstablished) { mRetryCount = 0; - if (billingClientConnectionState != null) { - billingClientConnectionState.postValue(true); - } + } else { + BraveVpnUtils.showToast(billingResult.getDebugMessage()); } } }); @@ -137,16 +138,11 @@ public void onBillingSetupFinished(@NonNull BillingResult billingResult) { } } - public boolean isSubscriptionSupported() { + public void endConnection() { if (mBillingClient != null) { - BillingResult result = - mBillingClient.isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS); - BraveRewardsNativeWorker braveRewardsNativeWorker = - BraveRewardsNativeWorker.getInstance(); - return (result.getResponseCode() == BillingClient.BillingResponseCode.OK - && braveRewardsNativeWorker != null && braveRewardsNativeWorker.IsSupported()); + Log.e(TAG, "endConnection"); + mBillingClient.endConnection(); } - return false; } public String getProductId(SubscriptionType subscriptionType) { @@ -189,6 +185,7 @@ public void queryProductDetailsAsync() { Log.e(TAG, "queryProductDetailsAsync failed" + billingResult.getDebugMessage()); + BraveVpnUtils.showToast(billingResult.getDebugMessage()); } }); } @@ -209,6 +206,7 @@ public void queryPurchases(MutableLiveData mutableActivePurchases } } else { Log.e(TAG, "queryPurchases failed" + billingResult.getDebugMessage()); + BraveVpnUtils.showToast(billingResult.getDebugMessage()); } mutableActivePurchases.postValue(activePurchaseModel); }); @@ -231,6 +229,7 @@ public void initiatePurchase(Activity activity, ProductDetails productDetails) { } public void processPurchases(Context context, Purchase activePurchase) { + Log.e(TAG, "processPurchases"); acknowledgePurchase(context, activePurchase); } @@ -239,9 +238,12 @@ 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( @@ -252,14 +254,21 @@ private void acknowledgePurchase(Context context, Purchase purchase) { } }); } 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()); 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) { 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 8a2c7028279a..e9031b9af88c 100644 --- a/android/java/org/chromium/chrome/browser/vpn/utils/BraveVpnUtils.java +++ b/android/java/org/chromium/chrome/browser/vpn/utils/BraveVpnUtils.java @@ -25,17 +25,20 @@ import org.chromium.base.ContextUtils; import org.chromium.base.Log; +import org.chromium.chrome.browser.BraveRewardsNativeWorker; import org.chromium.chrome.browser.util.BraveConstants; import org.chromium.chrome.browser.vpn.BraveVpnNativeWorker; 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; import org.chromium.chrome.browser.vpn.models.BraveVpnWireguardProfileCredentials; import org.chromium.chrome.browser.vpn.split_tunnel.SplitTunnelActivity; import org.chromium.chrome.browser.vpn.wireguard.WireguardConfigUtils; +import org.chromium.gms.ChromiumPlayServicesAvailability; import org.chromium.ui.widget.Toast; import java.util.ArrayList; @@ -59,16 +62,6 @@ public class BraveVpnUtils { public static String selectedServerRegion; private static ProgressDialog sProgressDialog; - public static boolean isBraveVpnFeatureEnable() { - if ((ContextUtils.getApplicationContext().getPackageName().equals( - BraveConstants.BRAVE_PRODUCTION_PACKAGE_NAME) - || BraveVpnPrefUtils.isBraveVpnFeatureEnabled()) - && Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) { - return true; - } - return false; - } - public static String getBraveAccountUrl() { return BraveVpnPrefUtils.isLinkSubscriptionOnStaging() ? BRAVE_ACCOUNT_STAGING_PAGE_URL : BRAVE_ACCOUNT_PROD_PAGE_URL; @@ -281,4 +274,23 @@ public static void showToast(String message) { Context context = ContextUtils.getApplicationContext(); Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } + + private static boolean isRegionSupported() { + BraveRewardsNativeWorker braveRewardsNativeWorker = BraveRewardsNativeWorker.getInstance(); + return (braveRewardsNativeWorker != null && braveRewardsNativeWorker.IsSupported()); + } + + private static boolean isBraveVpnFeatureEnable() { + if ((ContextUtils.getApplicationContext().getPackageName().equals( + BraveConstants.BRAVE_PRODUCTION_PACKAGE_NAME) + || BraveVpnPrefUtils.isBraveVpnFeatureEnabled())) { + return true; + } + return false; + } + + public static boolean isVpnFeatureSupported(Context context) { + return isBraveVpnFeatureEnable() && isRegionSupported() + && ChromiumPlayServicesAvailability.isGooglePlayServicesAvailable(context); + } }