Skip to content

Commit

Permalink
Merge pull request #7151 from brave/binance_widget_update_android
Browse files Browse the repository at this point in the history
Binance widget update android
  • Loading branch information
deeppandya authored Nov 16, 2020
2 parents 336cdf2 + ba18d95 commit cafacb9
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public class BraveNewTabPageLayout extends NewTabPageLayout {
private TextView mTopsiteErrorMessage;

private BinanceNativeWorker mBinanceNativeWorker;
private CryptoWidgetBottomSheetDialogFragment cryptoWidgetBottomSheetDialogFragment;
private CountDownTimer countDownTimer;
private List<NTPWidgetItem> widgetList = new ArrayList<NTPWidgetItem>();
public static final int NTP_WIDGET_STACK_CODE = 3333;
Expand Down Expand Up @@ -261,8 +262,7 @@ public void onClick(View v) {
public void onClick(View view) {
if (BinanceWidgetManager.getInstance().isUserAuthenticatedForBinance()) {
cancelTimer();
CryptoWidgetBottomSheetDialogFragment
cryptoWidgetBottomSheetDialogFragment =
cryptoWidgetBottomSheetDialogFragment =
new CryptoWidgetBottomSheetDialogFragment();
cryptoWidgetBottomSheetDialogFragment.show(
((BraveActivity) mActivity).getSupportFragmentManager(),
Expand Down Expand Up @@ -796,6 +796,9 @@ public void OnGetAccessToken(boolean isSuccess) {
public void OnGetAccountBalances(String jsonBalances, boolean isSuccess) {
if (!isSuccess) {
BinanceWidgetManager.getInstance().setUserAuthenticationForBinance(isSuccess);
if (cryptoWidgetBottomSheetDialogFragment != null) {
cryptoWidgetBottomSheetDialogFragment.dismiss();
}
}
try {
BinanceWidgetManager.binanceAccountBalance =
Expand Down Expand Up @@ -828,6 +831,9 @@ public void OnGetConvertQuote(
@Override
public void OnRevokeToken(boolean isSuccess) {
BinanceWidgetManager.getInstance().setUserAuthenticationForBinance(!isSuccess);
if (cryptoWidgetBottomSheetDialogFragment != null) {
cryptoWidgetBottomSheetDialogFragment.dismiss();
}
// Reset binance widget to connect page
showWidgets();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.chromium.base.ContextUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ntp.widget.NTPWidgetItem;
import org.chromium.chrome.browser.widget.crypto.binance.BinanceNativeWorker;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -95,7 +96,8 @@ public List<String> getUsedWidgets() {
if (getFavoritesWidget() != -1) {
usedWidgetMap.put(getFavoritesWidget(), PREF_FAVORITES);
}
if (getBinanceWidget() != -1) {
if (getBinanceWidget() != -1
&& BinanceNativeWorker.getInstance().IsSupportedRegion()) {
usedWidgetMap.put(getBinanceWidget(), PREF_BINANCE);
}
return new ArrayList<String>(usedWidgetMap.values());
Expand All @@ -109,7 +111,8 @@ public List<String> getAvailableWidgets() {
if (getFavoritesWidget() == -1) {
availableWidgets.add(PREF_FAVORITES);
}
if (getBinanceWidget() == -1) {
if (getBinanceWidget() == -1
&& BinanceNativeWorker.getInstance().IsSupportedRegion()) {
availableWidgets.add(PREF_BINANCE);
}
return availableWidgets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,20 @@ public class BinanceConvertFragment extends Fragment {
private TextView binanceConvertTitle;
private LinearLayout convertLayout;
private LinearLayout errorLayout;
private LinearLayout successLayout;
private EditText amountEditText;

private LinearLayout confirmLayout;
private TextView convertCurrencyText;
private TextView convertFeeText;
private TextView convertBalanceText;
private CountDownTimer countDownTimer;
private ProgressBar binanceWidgetProgress;
private TextView successText;

private double convertAmount;
private String convertedAmount;
private CryptoWidgetBottomSheetDialogFragment.BinanceBottomSheetListener mBinanceBottomSheetListener;

private static final String ZERO_BALANCE = "0.000000";

Expand Down Expand Up @@ -108,10 +115,14 @@ public void onDestroyView() {
super.onDestroyView();
}

public void setBinanceBottomSheetListener(CryptoWidgetBottomSheetDialogFragment.BinanceBottomSheetListener binanceBottomSheetListener) {
mBinanceBottomSheetListener = binanceBottomSheetListener;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
EditText amountEditText = view.findViewById(R.id.amount_edittext);
amountEditText = view.findViewById(R.id.amount_edittext);
binanceConvertTitle = view.findViewById(R.id.binance_convert_title);

convertLayout = view.findViewById(R.id.convert_layout);
Expand All @@ -129,6 +140,17 @@ public void onClick(View view) {
}
});

successLayout = view.findViewById(R.id.success_layout);
successText = view.findViewById(R.id.success_message_text);
Button continueButton = view.findViewById(R.id.btn_continue);
continueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
amountEditText.getText().clear();
mBinanceBottomSheetListener.onContinue();
}
});

confirmLayout = view.findViewById(R.id.confirm_convert_layout);
convertCurrencyText = view.findViewById(R.id.convert_currency_text);
convertFeeText = view.findViewById(R.id.convert_fee_text);
Expand All @@ -140,6 +162,7 @@ public void onClick(View view) {
@Override
public void onClick(View view) {
cancelTimer();
amountEditText.getText().clear();
convertLayout.setVisibility(View.VISIBLE);
confirmLayout.setVisibility(View.GONE);
}
Expand All @@ -149,9 +172,9 @@ public void onClick(View view) {
convertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE))
.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
double convertAmount = !TextUtils.isEmpty(amountEditText.getText().toString())
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(amountEditText.getWindowToken(), 0);
convertAmount = !TextUtils.isEmpty(amountEditText.getText().toString())
? Double.valueOf(amountEditText.getText().toString())
: 0.0;
double availableBalance =
Expand Down Expand Up @@ -222,21 +245,18 @@ private void dismissBinanceBottomSheet() {
}

private BinanceObserver mBinanaceObserver = new BinanceObserver() {
@Override
public void OnGetAccessToken(boolean isSuccess){};

@Override
public void OnGetAccountBalances(String jsonBalances, boolean isSuccess){};

@Override
public void OnGetConvertQuote(
String quoteId, String quotePrice, String totalFee, String totalAmount) {
convertLayout.setVisibility(View.GONE);
confirmLayout.setVisibility(View.VISIBLE);
convertedAmount = !TextUtils.isEmpty(totalAmount) ? String.format(
Locale.getDefault(), "%.6f", Double.parseDouble(totalAmount))
: ZERO_BALANCE;
convertCurrencyText.setText(String.format(
getActivity().getResources().getString(R.string.convert_stat_text),
!TextUtils.isEmpty(quotePrice) ? String.format(
Locale.getDefault(), "%.6f", Double.parseDouble(quotePrice))
convertAmount != 0.0 ? String.format(
Locale.getDefault(), "%.6f", convertAmount)
: ZERO_BALANCE,
selectedCrypto1));
convertFeeText.setText(String.format(
Expand All @@ -247,9 +267,7 @@ public void OnGetConvertQuote(
selectedCrypto1));
convertBalanceText.setText(String.format(
getActivity().getResources().getString(R.string.convert_stat_text),
!TextUtils.isEmpty(totalAmount) ? String.format(
Locale.getDefault(), "%.6f", Double.parseDouble(totalAmount))
: ZERO_BALANCE,
convertedAmount,
selectedCrypto2.getAsset()));
startTimer();

Expand All @@ -262,21 +280,17 @@ public void onClick(View view) {
});
};

@Override
public void OnGetCoinNetworks(String jsonNetworks){};

@Override
public void OnGetDepositInfo(
String depositAddress, String depositeTag, boolean isSuccess){};

@Override
public void OnConfirmConvert(boolean isSuccess, String message) {
if (!isSuccess) {
Toast.makeText(getActivity(), R.string.conversion_failed, Toast.LENGTH_LONG).show();
} else {
confirmLayout.setVisibility(View.GONE);
successLayout.setVisibility(View.VISIBLE);
if (successText != null) successText.setText(String.format(getActivity().getResources().getString(R.string.convert_success), convertAmount, selectedCrypto1,
convertedAmount, selectedCrypto2.getAsset()));
}
cancelTimer();
convertLayout.setVisibility(View.VISIBLE);
confirmLayout.setVisibility(View.GONE);
};

@Override
Expand Down Expand Up @@ -312,9 +326,6 @@ public void OnGetConvertAssets(String jsonAssets) {
Log.e("NTP", e.getMessage());
}
};

@Override
public void OnRevokeToken(boolean isSuccess){};
};

private void setCryptoSpinner(String key) {
Expand Down Expand Up @@ -363,7 +374,7 @@ private void setTitle() {

// start timer function
public void startTimer() {
countDownTimer = new CountDownTimer(60000, 1000) {
countDownTimer = new CountDownTimer(30000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
if (confirmButton != null) {
Expand All @@ -376,6 +387,7 @@ public void onTick(long millisUntilFinished) {
@Override
public void onFinish() {
cancelTimer();
amountEditText.getText().clear();
convertLayout.setVisibility(View.VISIBLE);
confirmLayout.setVisibility(View.GONE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public void getAccessToken() {
}
}

public boolean IsSupportedRegion() {
synchronized(lock) {
return nativeIsSupportedRegion(mNativeBinanceNativeWorker);
}
}

@CalledByNative
private void setNativePtr(long nativePtr) {
assert mNativeBinanceNativeWorker == 0;
Expand Down Expand Up @@ -175,6 +181,7 @@ public void revokeToken() {
private native void nativeDestroy(long nativeBinanceNativeWorker);
private native String nativeGetOAuthClientUrl(long nativeBinanceNativeWorker);
private native void nativeGetAccessToken(long nativeBinanceNativeWorker);
private native boolean nativeIsSupportedRegion(long nativeBinanceNativeWorker);
private native void nativeSetAuthToken(long nativeBinanceNativeWorker, String authToken);
private native void nativeGetAccountBalances(long nativeBinanceNativeWorker);
private native void nativeGetConvertQuote(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class BinanceSummaryFragment extends Fragment {
private BinanceNativeWorker mBinanceNativeWorker;
private LinearLayout summaryLayout;
private ProgressBar binanceCoinsProgress;
private TextView binanceBalanceText;
private TextView binanceBtcText;
private TextView binanceUSDBalanceText;

private static final String ZERO_BALANCE = "0.000000";
private static final String ZERO_USD_BALANCE = "0.00";
Expand Down Expand Up @@ -72,37 +75,40 @@ public void onDestroyView() {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView binanceBalanceText = view.findViewById(R.id.binance_balance_text);
TextView binanceBtcText = view.findViewById(R.id.binance_btc_text);
TextView binanceUSDBalanceText = view.findViewById(R.id.binance_usd_balance_text);
mBinanceNativeWorker.getAccountBalances();
binanceBalanceText = view.findViewById(R.id.binance_balance_text);
binanceBtcText = view.findViewById(R.id.binance_btc_text);
binanceUSDBalanceText = view.findViewById(R.id.binance_usd_balance_text);

binanceCoinsProgress = view.findViewById(R.id.binance_coins_progress);

if (BinanceWidgetManager.binanceAccountBalance != null) {
binanceBalanceText.setText(String.format(Locale.getDefault(), "%.6f",
BinanceWidgetManager.binanceAccountBalance.getTotalBTC()));
binanceBtcText.setText(BTC);
binanceUSDBalanceText.setText(
String.format(getActivity().getResources().getString(R.string.usd_balance),
String.format(Locale.getDefault(), "%.2f",
BinanceWidgetManager.binanceAccountBalance.getTotalUSD())));
}
summaryLayout = view.findViewById(R.id.summary_layout);
binanceCoinsProgress.setVisibility(View.VISIBLE);
summaryLayout.setVisibility(View.GONE);
mBinanceNativeWorker.getCoinNetworks();
}

private BinanceObserver mBinanaceObserver = new BinanceObserver() {
@Override
public void OnGetAccessToken(boolean isSuccess){};

@Override
public void OnGetAccountBalances(String jsonBalances, boolean isSuccess){};

@Override
public void OnGetConvertQuote(
String quoteId, String quotePrice, String totalFee, String totalAmount){};
public void OnGetAccountBalances(String jsonBalances, boolean isSuccess) {
if (!isSuccess) {
BinanceWidgetManager.getInstance().setUserAuthenticationForBinance(isSuccess);
} else {
try {
BinanceWidgetManager.binanceAccountBalance =
new BinanceAccountBalance(jsonBalances);
binanceBalanceText.setText(String.format(Locale.getDefault(), "%.6f",
BinanceWidgetManager.binanceAccountBalance.getTotalBTC()));
binanceBtcText.setText(BTC);
binanceUSDBalanceText.setText(
String.format(getActivity().getResources().getString(R.string.usd_balance),
String.format(Locale.getDefault(), "%.2f",
BinanceWidgetManager.binanceAccountBalance.getTotalUSD())));
} catch (JSONException e) {
Log.e("NTP", e.getMessage());
}
mBinanceNativeWorker.getCoinNetworks();
}
};

@Override
public void OnGetCoinNetworks(String jsonNetworks) {
Expand Down Expand Up @@ -165,18 +171,5 @@ public void OnGetCoinNetworks(String jsonNetworks) {
Log.e("NTP", e.getMessage());
}
};

@Override
public void OnGetDepositInfo(
String depositAddress, String depositeTag, boolean isSuccess){};

@Override
public void OnConfirmConvert(boolean isSuccess, String message){};

@Override
public void OnGetConvertAssets(String jsonAssets){};

@Override
public void OnRevokeToken(boolean isSuccess){};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@
import java.util.ArrayList;
import java.util.List;

public class CryptoWidgetBottomSheetDialogFragment extends BottomSheetDialogFragment {
public class CryptoWidgetBottomSheetDialogFragment extends BottomSheetDialogFragment{
final public static String TAG_FRAGMENT = "CRYPTO_WIDGET_FRAG";
private ViewPager viewPager;
private CryptoWidgetTabAdapter adapter;

public interface BinanceBottomSheetListener {
void onContinue();
}

@Nullable
@Override
Expand All @@ -47,13 +53,22 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager viewPager = view.findViewById(R.id.viewpager);
viewPager = view.findViewById(R.id.viewpager);
TabLayout tabLayout = view.findViewById(R.id.tablayout);

assert getFragmentManager() != null;
CryptoWidgetTabAdapter adapter = new CryptoWidgetTabAdapter(getChildFragmentManager(),
adapter = new CryptoWidgetTabAdapter(getChildFragmentManager(),
FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
adapter.setBinanceBottomSheetListener(binanceBottomSheetListener);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}

private BinanceBottomSheetListener binanceBottomSheetListener =
new BinanceBottomSheetListener() {
@Override
public void onContinue() {
if (viewPager != null && adapter != null) viewPager.setAdapter(adapter);
}
};
}
Loading

0 comments on commit cafacb9

Please sign in to comment.