Skip to content

Commit

Permalink
Refactor end connection to InAppPurchaseWrapper only
Browse files Browse the repository at this point in the history
  • Loading branch information
deeppandya committed Sep 19, 2023
1 parent 1a480af commit 3dabeb9
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> _billingConnectionState =
new MutableLiveData();
LiveData<Boolean> billingConnectionState = _billingConnectionState;
InAppPurchaseWrapper.getInstance().startBillingServiceConnection(
BraveActivity.this, _billingConnectionState);
LiveDataUtil.observeOnce(billingConnectionState,
isConnected -> { verifySubscription(); });
verifySubscription();
} else {
BraveVpnUtils.dismissProgressDialog();
BraveVpnUtils.openBraveVpnPlansActivity(BraveActivity.this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,7 @@ public boolean onPreferenceClick(Preference preference) {
BraveVpnUtils.showProgressDialog(
getActivity(), getResources().getString(R.string.vpn_connect_text));
if (BraveVpnPrefUtils.isSubscriptionPurchase()) {
MutableLiveData<Boolean> _billingConnectionState =
new MutableLiveData();
LiveData<Boolean> billingConnectionState = _billingConnectionState;
InAppPurchaseWrapper.getInstance().startBillingServiceConnection(
getActivity(), _billingConnectionState);
LiveDataUtil.observeOnce(billingConnectionState,
isConnected -> { verifyPurchase(true); });
verifyPurchase(true);
} else {
BraveVpnUtils.openBraveVpnPlansActivity(getActivity());
BraveVpnUtils.dismissProgressDialog();
Expand Down Expand Up @@ -257,12 +251,7 @@ public void onResume() {
mBraveVpnPrefModel = new BraveVpnPrefModel();
BraveVpnNativeWorker.getInstance().getSubscriberCredentialV12();
} else {
MutableLiveData<Boolean> _billingConnectionState = new MutableLiveData();
LiveData<Boolean> billingConnectionState = _billingConnectionState;
InAppPurchaseWrapper.getInstance().startBillingServiceConnection(
getActivity(), _billingConnectionState);
LiveDataUtil.observeOnce(
billingConnectionState, isConnected -> { verifyPurchase(false); });
verifyPurchase(false);
}
} else if (BraveVpnUtils.mUpdateProfileAfterSplitTunnel) {
BraveVpnUtils.mUpdateProfileAfterSplitTunnel = false;
Expand Down Expand Up @@ -588,10 +577,4 @@ 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 @@ -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;
Expand All @@ -42,28 +39,20 @@ public abstract class BraveVpnParentActivity
private static final String TAG = "BraveVPN";
public boolean mIsVerification;
protected BraveVpnPrefModel mBraveVpnPrefModel;
private final OneshotSupplierImpl<Profile> 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<Intent> 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 {
Expand All @@ -77,7 +66,6 @@ public BraveVpnParentActivity() {
@Override
public void finishNativeInitialization() {
super.finishNativeInitialization();
mProfileSupplier.set(Profile.getLastUsedRegularProfile());
}

protected void verifySubscription() {
Expand All @@ -98,6 +86,8 @@ protected void verifySubscription() {
if (!mIsVerification) {
BraveVpnApiResponseUtils.queryPurchaseFailed(
BraveVpnParentActivity.this);
} else {
showRestoreMenu(false);
}
BraveVpnUtils.dismissProgressDialog();
}
Expand Down Expand Up @@ -216,8 +206,4 @@ public void run() {
}
}.start();
}

public OneshotSupplier<Profile> getProfileSupplier() {
return mProfileSupplier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,22 +15,18 @@

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;
import org.chromium.chrome.browser.vpn.billing.InAppPurchaseWrapper;
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;
Expand All @@ -43,9 +38,6 @@ public class BraveVpnPlansActivity extends BraveVpnParentActivity {
private LinearLayout mYearlySelectorLayout;
private TextView mYearlySubscriptionAmountText;

private MutableLiveData<Boolean> _billingConnectionState = new MutableLiveData();
private LiveData<Boolean> billingConnectionState = _billingConnectionState;

@Override
public void onResumeWithNative() {
super.onResumeWithNative();
Expand All @@ -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);

Expand All @@ -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));
Expand All @@ -116,7 +99,6 @@ public void finishNativeInitialization() {

// Set up monthly subscription
mMonthlyPlanProgress.setVisibility(View.VISIBLE);
InAppPurchaseWrapper.getInstance().queryProductDetailsAsync();
LiveDataUtil.observeOnce(
InAppPurchaseWrapper.getInstance().getMonthlyProductDetails(),
monthlyProductDetails -> {
Expand Down Expand Up @@ -226,8 +208,6 @@ public void run() {
});
}
});
}
});
}

@Override
Expand Down Expand Up @@ -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();
}

Expand All @@ -280,6 +254,7 @@ public boolean shouldStartGpuProcess() {
@Override
public void showRestoreMenu(boolean shouldShowRestore) {
this.mShouldShowRestoreMenu = shouldShowRestore;
InAppPurchaseWrapper.getInstance().queryProductDetailsAsync();
invalidateOptionsMenu();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -72,18 +65,12 @@ private void initializeViews() {
public void onClick(View v) {
BraveVpnUtils.showProgressDialog(BraveVpnProfileActivity.this,
getResources().getString(R.string.vpn_connect_text));
MutableLiveData<Boolean> _billingConnectionState = new MutableLiveData();
LiveData<Boolean> 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();
}
}
});

Expand All @@ -101,12 +88,7 @@ public void finishNativeInitialization() {
super.finishNativeInitialization();
if (getIntent() != null
&& getIntent().getBooleanExtra(BraveVpnUtils.VERIFY_CREDENTIALS_FAILED, false)) {
MutableLiveData<Boolean> _billingConnectionState = new MutableLiveData();
LiveData<Boolean> billingConnectionState = _billingConnectionState;
InAppPurchaseWrapper.getInstance().startBillingServiceConnection(
BraveVpnProfileActivity.this, _billingConnectionState);
LiveDataUtil.observeOnce(
billingConnectionState, isConnected -> { verifySubscription(); });
verifySubscription();
}
}

Expand All @@ -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();
}

Expand All @@ -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();
}
}
Loading

0 comments on commit 3dabeb9

Please sign in to comment.