Skip to content

Commit

Permalink
Explicitly end connection for billing client
Browse files Browse the repository at this point in the history
  • Loading branch information
deeppandya committed Sep 19, 2023
1 parent d07e6ba commit 1a480af
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -590,4 +588,10 @@ private void resetConfiguration() {
}
}, INVALIDATE_CREDENTIAL_TIMER_COUNT);
}

@Override
public void onDestroy() {
InAppPurchaseWrapper.getInstance().endConnection();
super.onDestroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Boolean> 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))
Expand All @@ -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());
}
}
});
Expand All @@ -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) {
Expand Down Expand Up @@ -189,6 +185,7 @@ public void queryProductDetailsAsync() {
Log.e(TAG,
"queryProductDetailsAsync failed"
+ billingResult.getDebugMessage());
BraveVpnUtils.showToast(billingResult.getDebugMessage());
}
});
}
Expand All @@ -209,6 +206,7 @@ public void queryPurchases(MutableLiveData<PurchaseModel> mutableActivePurchases
}
} else {
Log.e(TAG, "queryPurchases failed" + billingResult.getDebugMessage());
BraveVpnUtils.showToast(billingResult.getDebugMessage());
}
mutableActivePurchases.postValue(activePurchaseModel);
});
Expand All @@ -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);
}

Expand All @@ -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(
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1a480af

Please sign in to comment.