From 30b706e783c7835123ce92513451d7ff4c0a1839 Mon Sep 17 00:00:00 2001 From: Pavneet-Sing Date: Sat, 5 Mar 2022 04:09:32 +0530 Subject: [PATCH] feat(wallet): add reset transaction & nonce setting --- android/brave_java_resources.gni | 1 + android/brave_java_sources.gni | 1 + ...alletResetTxHistoryAndNoncePreference.java | 132 ++++++++++++++++++ ...et_preference_confirmation_text_layout.xml | 24 ++++ .../java/res/xml/brave_wallet_preferences.xml | 6 +- .../android/strings/android_brave_strings.grd | 9 ++ 6 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 android/java/org/chromium/chrome/browser/settings/BraveWalletResetTxHistoryAndNoncePreference.java create mode 100644 android/java/res/layout/brave_wallet_preference_confirmation_text_layout.xml diff --git a/android/brave_java_resources.gni b/android/brave_java_resources.gni index 784f4ec5de45..fd7a32f1228c 100644 --- a/android/brave_java_resources.gni +++ b/android/brave_java_resources.gni @@ -848,6 +848,7 @@ brave_java_resources = [ "java/res/layout/brave_wallet_edit_gas.xml", "java/res/layout/brave_wallet_network_item.xml", "java/res/layout/brave_wallet_networks_preference_layout.xml", + "java/res/layout/brave_wallet_preference_confirmation_text_layout.xml", "java/res/layout/brave_wallet_reset_content.xml", "java/res/layout/brave_webrtc_policy_preference.xml", "java/res/layout/bre_bottom_banner.xml", diff --git a/android/brave_java_sources.gni b/android/brave_java_sources.gni index 48bc531bd0fa..6cd10c27a4b7 100644 --- a/android/brave_java_sources.gni +++ b/android/brave_java_sources.gni @@ -239,6 +239,7 @@ brave_java_sources = [ "../../brave/android/java/org/chromium/chrome/browser/settings/BraveWalletNetworksPreferenceFragment.java", "../../brave/android/java/org/chromium/chrome/browser/settings/BraveWalletPreferences.java", "../../brave/android/java/org/chromium/chrome/browser/settings/BraveWalletResetPreference.java", + "../../brave/android/java/org/chromium/chrome/browser/settings/BraveWalletResetTxHistoryAndNoncePreference.java", "../../brave/android/java/org/chromium/chrome/browser/settings/BraveWebrtcPolicyPreference.java", "../../brave/android/java/org/chromium/chrome/browser/settings/BraveWebrtcPolicyPreferencesFragment.java", "../../brave/android/java/org/chromium/chrome/browser/settings/NetworkListBaseAdapter.java", diff --git a/android/java/org/chromium/chrome/browser/settings/BraveWalletResetTxHistoryAndNoncePreference.java b/android/java/org/chromium/chrome/browser/settings/BraveWalletResetTxHistoryAndNoncePreference.java new file mode 100644 index 000000000000..fad0f19b7062 --- /dev/null +++ b/android/java/org/chromium/chrome/browser/settings/BraveWalletResetTxHistoryAndNoncePreference.java @@ -0,0 +1,132 @@ +/* Copyright (c) 2022 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.chromium.chrome.browser.settings; + +import android.content.Context; +import android.content.DialogInterface; +import android.text.Editable; +import android.text.TextUtils; +import android.text.TextWatcher; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; + +import androidx.appcompat.app.AlertDialog; +import androidx.preference.Preference; + +import org.chromium.brave_wallet.mojom.TxService; +import org.chromium.chrome.R; +import org.chromium.chrome.browser.crypto_wallet.TxServiceFactory; +import org.chromium.mojo.bindings.ConnectionErrorHandler; +import org.chromium.mojo.system.MojoException; +import org.chromium.ui.KeyboardVisibilityDelegate; + +/** + * The preference used to reset transaction and nonce history in Brave Wallet. + */ +public class BraveWalletResetTxHistoryAndNoncePreference + extends Preference implements Preference.OnPreferenceClickListener, ConnectionErrorHandler { + private String TAG = "BraveWalletResetTxHistoryAndNoncePreference"; + + + private TxService mTxService; + private final String mConfirmationPhrase; + + public BraveWalletResetTxHistoryAndNoncePreference(Context context, AttributeSet attrs) { + super(context, attrs); + + setOnPreferenceClickListener(this); + mConfirmationPhrase = + getContext().getResources().getString(R.string.brave_wallet_reset_settings_confirmation_phrase); + initTxService(); + } + + @Override + public boolean onPreferenceClick(Preference preference) { + showBraveWalletResetTxHistoryAndNonceDialog(); + return true; + } + + @Override + public void onDetached() { + super.onDetached(); + if (mTxService != null) { + mTxService.close(); + } + } + + private void showBraveWalletResetTxHistoryAndNonceDialog() { + LayoutInflater inflater = + (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View view = inflater.inflate(R.layout.brave_wallet_preference_confirmation_text_layout, null); + TextView textView = view.findViewById(R.id.brave_wallet_preference_confirmation_dialog_tv_message); + textView.setText(getContext().getResources().getString(R.string.brave_wallet_clear_tx_and_nonce_dialog_confirmation, mConfirmationPhrase)); + final EditText input = view.findViewById(R.id.brave_wallet_preference_confirmation_dialog_edittext); + + DialogInterface.OnClickListener onClickListener = (dialog, button) -> { + if (button == AlertDialog.BUTTON_POSITIVE) { + mTxService.reset(); + } else { + dialog.dismiss(); + } + }; + + AlertDialog.Builder alert = + new AlertDialog.Builder(getContext(), R.style.Theme_Chromium_AlertDialog); + AlertDialog alertDialog = + alert.setTitle(R.string.brave_wallet_clear_tx_and_nonce_setting_title) + .setView(view) + .setPositiveButton(R.string.brave_wallet_confirm_text, onClickListener) + .setNegativeButton(R.string.cancel, onClickListener) + .create(); + alertDialog.getDelegate().setHandleNativeActionModesEnabled(false); + alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialog) { + KeyboardVisibilityDelegate.getInstance().showKeyboard(input); + } + }); + alertDialog.show(); + final Button okButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); + okButton.setEnabled(false); + + input.addTextChangedListener(new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + // Disable ok button if input is invalid + String inputText = s.toString().trim(); + + okButton.setEnabled(TextUtils.equals(inputText, mConfirmationPhrase)); + } + }); + } + + @Override + public void onConnectionError(MojoException e) { + mTxService.close(); + mTxService = null; + initTxService(); + } + + private void initTxService() { + if (mTxService != null) { + return; + } + + mTxService = TxServiceFactory.getInstance().getTxService(this); + } +} diff --git a/android/java/res/layout/brave_wallet_preference_confirmation_text_layout.xml b/android/java/res/layout/brave_wallet_preference_confirmation_text_layout.xml new file mode 100644 index 000000000000..577db89035b0 --- /dev/null +++ b/android/java/res/layout/brave_wallet_preference_confirmation_text_layout.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + diff --git a/android/java/res/xml/brave_wallet_preferences.xml b/android/java/res/xml/brave_wallet_preferences.xml index 59fced452e79..7e187d63bfd1 100644 --- a/android/java/res/xml/brave_wallet_preferences.xml +++ b/android/java/res/xml/brave_wallet_preferences.xml @@ -16,10 +16,14 @@ + + diff --git a/browser/ui/android/strings/android_brave_strings.grd b/browser/ui/android/strings/android_brave_strings.grd index c5808b895f27..f40a45a1a6bc 100644 --- a/browser/ui/android/strings/android_brave_strings.grd +++ b/browser/ui/android/strings/android_brave_strings.grd @@ -2204,6 +2204,15 @@ Are you sure you want to do this? Yes + + Clear transaction & nonce info + + + Clearing transactions may be useful for developers or when clearing state on a local server + + + Are you sure you want to reset transaction and nonce information? This option is mostly used by developers running a local test server. Type "%1$sYes" to confirm. + Confirm