diff --git a/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java b/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java index 7d0bf6fd9d39..940250ed3b04 100644 --- a/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java +++ b/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java @@ -18,7 +18,7 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.CountDownTimer; +import android.os.Handler; import android.text.Html; import android.text.Spannable; import android.text.SpannableStringBuilder; @@ -118,9 +118,13 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Timer; +import java.util.TimerTask; import java.util.TreeMap; -public class BraveNewTabPageLayout extends NewTabPageLayout { +public class BraveNewTabPageLayout + extends NewTabPageLayout implements CryptoWidgetBottomSheetDialogFragment + .CryptoWidgetBottomSheetDialogDismissListener { private static final String TAG = "BraveNewTabPageView"; private static final String BRAVE_BINANCE = "https://brave.com/binance/"; private static final String BRAVE_REF_URL = "https://brave.com/r/"; @@ -157,7 +161,7 @@ public class BraveNewTabPageLayout extends NewTabPageLayout { private BinanceNativeWorker mBinanceNativeWorker; private CryptoWidgetBottomSheetDialogFragment cryptoWidgetBottomSheetDialogFragment; - private CountDownTimer countDownTimer; + private Timer countDownTimer; private List widgetList = new ArrayList(); public static final int NTP_WIDGET_STACK_CODE = 3333; @@ -324,6 +328,9 @@ public void onClick(View view) { cancelTimer(); cryptoWidgetBottomSheetDialogFragment = new CryptoWidgetBottomSheetDialogFragment(); + cryptoWidgetBottomSheetDialogFragment + .setCryptoWidgetBottomSheetDialogDismissListener( + BraveNewTabPageLayout.this); cryptoWidgetBottomSheetDialogFragment.show( ((BraveActivity) mActivity).getSupportFragmentManager(), CryptoWidgetBottomSheetDialogFragment.TAG_FRAGMENT); @@ -879,7 +886,6 @@ public void onMenuDisconnect() { private BinanceObserver mBinanaceObserver = new BinanceObserver() { @Override public void OnGetAccessToken(boolean isSuccess) { - Log.e("NTP", "OnGetAccessToken : " + isSuccess); BinanceWidgetManager.getInstance().setUserAuthenticationForBinance(isSuccess); if (isSuccess) { mBinanceNativeWorker.getAccountBalances(); @@ -919,23 +925,31 @@ public void OnGetAccountBalances(String jsonBalances, boolean isSuccess) { // start timer function public void startTimer() { - countDownTimer = new CountDownTimer(30000, 1000) { - @Override - public void onTick(long millisUntilFinished) {} - @Override - public void onFinish() { - if (BinanceWidgetManager.getInstance().isUserAuthenticatedForBinance()) { - mBinanceNativeWorker.getAccountBalances(); + if (countDownTimer == null) { + countDownTimer = new Timer(); + final Handler handler = new Handler(); + countDownTimer.scheduleAtFixedRate(new TimerTask() { + public void run() { + handler.post(new Runnable() { + public void run() { + if (BinanceWidgetManager.getInstance() + .isUserAuthenticatedForBinance()) { + mBinanceNativeWorker.getAccountBalances(); + } + } + }); } - if ((BraveActivity) mActivity != null) startTimer(); - } - }; - countDownTimer.start(); + }, 0, 30000); + } } // cancel timer public void cancelTimer() { - if (countDownTimer != null) countDownTimer.cancel(); + if (countDownTimer != null) { + countDownTimer.cancel(); + countDownTimer.purge(); + countDownTimer = null; + } } public void openWidgetStack() { @@ -946,7 +960,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); fm.beginTransaction().remove(this).commit(); if (requestCode == NTP_WIDGET_STACK_CODE) { - Log.e("NTP", "Inside result"); showWidgets(); } } @@ -978,4 +991,9 @@ public void onTileCountChanged() { mTopsiteErrorMessage.setVisibility(View.VISIBLE); } } + + @Override + public void onCryptoWidgetBottomSheetDialogDismiss() { + startTimer(); + } } diff --git a/android/java/org/chromium/chrome/browser/widget/crypto/binance/BinanceBuyFragment.java b/android/java/org/chromium/chrome/browser/widget/crypto/binance/BinanceBuyFragment.java index 2979683016f7..8bfdb2315ff0 100644 --- a/android/java/org/chromium/chrome/browser/widget/crypto/binance/BinanceBuyFragment.java +++ b/android/java/org/chromium/chrome/browser/widget/crypto/binance/BinanceBuyFragment.java @@ -67,10 +67,19 @@ public class BinanceBuyFragment extends Fragment { private static final String US = ".us"; private static final String COM = ".com"; + private CryptoWidgetBottomSheetDialogFragment + .BinanceBottomSheetListener mBinanceBottomSheetListener; + public BinanceBuyFragment() { // Required empty public constructor } + public void setBinanceBottomSheetListener( + CryptoWidgetBottomSheetDialogFragment + .BinanceBottomSheetListener binanceBottomSheetListener) { + mBinanceBottomSheetListener = binanceBottomSheetListener; + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -194,15 +203,7 @@ private void setFiatSpinner(List fiatSpinnerList) { } private void dismissBinanceBottomSheet() { - FragmentManager fm = getParentFragmentManager(); - CryptoWidgetBottomSheetDialogFragment fragment = - (CryptoWidgetBottomSheetDialogFragment) fm.findFragmentByTag( - CryptoWidgetBottomSheetDialogFragment.TAG_FRAGMENT); - FragmentTransaction transaction = fm.beginTransaction(); - - if (fragment != null) { - fragment.dismiss(); - } + mBinanceBottomSheetListener.dismissBottomSheetDialog(); } private BinanceObserver mBinanaceObserver = new BinanceObserver() { diff --git a/android/java/org/chromium/chrome/browser/widget/crypto/binance/BinanceConvertFragment.java b/android/java/org/chromium/chrome/browser/widget/crypto/binance/BinanceConvertFragment.java index 475f0852f6d6..a4f0771eed06 100644 --- a/android/java/org/chromium/chrome/browser/widget/crypto/binance/BinanceConvertFragment.java +++ b/android/java/org/chromium/chrome/browser/widget/crypto/binance/BinanceConvertFragment.java @@ -251,18 +251,6 @@ public void onNothingSelected(AdapterView parentView) {} } } - private void dismissBinanceBottomSheet() { - FragmentManager fm = getParentFragmentManager(); - CryptoWidgetBottomSheetDialogFragment fragment = - (CryptoWidgetBottomSheetDialogFragment) fm.findFragmentByTag( - CryptoWidgetBottomSheetDialogFragment.TAG_FRAGMENT); - FragmentTransaction transaction = fm.beginTransaction(); - - if (fragment != null) { - fragment.dismiss(); - } - } - private BinanceObserver mBinanaceObserver = new BinanceObserver() { @Override public void OnGetConvertQuote( diff --git a/android/java/org/chromium/chrome/browser/widget/crypto/binance/CryptoWidgetBottomSheetDialogFragment.java b/android/java/org/chromium/chrome/browser/widget/crypto/binance/CryptoWidgetBottomSheetDialogFragment.java index e85a4577436c..b761263baf05 100644 --- a/android/java/org/chromium/chrome/browser/widget/crypto/binance/CryptoWidgetBottomSheetDialogFragment.java +++ b/android/java/org/chromium/chrome/browser/widget/crypto/binance/CryptoWidgetBottomSheetDialogFragment.java @@ -5,6 +5,7 @@ package org.chromium.chrome.browser.widget.crypto.binance; +import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -35,9 +36,23 @@ public class CryptoWidgetBottomSheetDialogFragment extends BottomSheetDialogFrag final public static String TAG_FRAGMENT = "CRYPTO_WIDGET_FRAG"; private ViewPager viewPager; private CryptoWidgetTabAdapter adapter; + private CryptoWidgetBottomSheetDialogDismissListener + mCryptoWidgetBottomSheetDialogDismissListener; public interface BinanceBottomSheetListener { void onContinue(); + void dismissBottomSheetDialog(); + } + + public interface CryptoWidgetBottomSheetDialogDismissListener { + void onCryptoWidgetBottomSheetDialogDismiss(); + } + + public void setCryptoWidgetBottomSheetDialogDismissListener( + CryptoWidgetBottomSheetDialogDismissListener + cryptoWidgetBottomSheetDialogDismissListener) { + this.mCryptoWidgetBottomSheetDialogDismissListener = + cryptoWidgetBottomSheetDialogDismissListener; } @Nullable @@ -64,11 +79,22 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat tabLayout.setupWithViewPager(viewPager); } + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + mCryptoWidgetBottomSheetDialogDismissListener.onCryptoWidgetBottomSheetDialogDismiss(); + } + private BinanceBottomSheetListener binanceBottomSheetListener = new BinanceBottomSheetListener() { @Override public void onContinue() { if (viewPager != null && adapter != null) viewPager.setAdapter(adapter); } + + @Override + public void dismissBottomSheetDialog() { + dismiss(); + } }; } \ No newline at end of file diff --git a/android/java/org/chromium/chrome/browser/widget/crypto/binance/CryptoWidgetTabAdapter.java b/android/java/org/chromium/chrome/browser/widget/crypto/binance/CryptoWidgetTabAdapter.java index 4bb5fb9ada69..303561d5545d 100644 --- a/android/java/org/chromium/chrome/browser/widget/crypto/binance/CryptoWidgetTabAdapter.java +++ b/android/java/org/chromium/chrome/browser/widget/crypto/binance/CryptoWidgetTabAdapter.java @@ -52,6 +52,8 @@ public Fragment getItem(int position) { ((BinanceConvertFragment)fragment).setBinanceBottomSheetListener(mBinanceBottomSheetListener); } else if (position == 3) { fragment = new BinanceBuyFragment(); + ((BinanceBuyFragment) fragment) + .setBinanceBottomSheetListener(mBinanceBottomSheetListener); } return fragment; }