Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet): add reset transaction & nonce setting #12493

Merged
merged 1 commit into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/brave_java_resources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--* 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/. -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
style="@style/AlertDialogContent">

<TextView
android:id="@+id/brave_wallet_preference_confirmation_dialog_tv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<org.chromium.components.browser_ui.widget.text.AlertDialogEditText
android:id="@+id/brave_wallet_preference_confirmation_dialog_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions" />

</LinearLayout>

6 changes: 5 additions & 1 deletion android/java/res/xml/brave_wallet_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
<Preference
android:fragment="org.chromium.chrome.browser.settings.BraveWalletNetworksPreferenceFragment"
android:key="pref_brave_wallet_networks"
android:order="1"
android:title="@string/brave_wallet_networks_title"
android:summary="@string/brave_wallet_networks_summary" />

<org.chromium.chrome.browser.settings.BraveWalletResetTxHistoryAndNoncePreference
android:key="pref_brave_wallet_tx_history_and_nonce_reset"
android:title="@string/brave_wallet_clear_tx_and_nonce_setting_title"
android:summary="@string/brave_wallet_clear_tx_and_nonce_setting_desc"/>

<org.chromium.chrome.browser.settings.BraveWalletResetPreference
android:key="pref_brave_wallet_reset"
android:title="@string/brave_wallet_reset_settings_option" />
Expand Down
9 changes: 9 additions & 0 deletions browser/ui/android/strings/android_brave_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,15 @@ Are you sure you want to do this?
<message name="IDS_BRAVE_WALLET_RESET_SETTINGS_CONFIRMATION_PHRASE" desc="The phrase users should type to reset Brave Wallet">
Yes
</message>
<message name="IDS_BRAVE_WALLET_CLEAR_TX_AND_NONCE_SETTING_TITLE" desc="The title of a button that will reset transaction and nonce information. As in to erase the users transaction history and reset nonce value starting from 0x0">
Clear transaction &amp; nonce info
</message>
<message name="IDS_BRAVE_WALLET_CLEAR_TX_AND_NONCE_SETTING_DESC" desc="The footer message below the button to reset transaction and nonce">
Clearing transactions may be useful for developers or when clearing state on a local server
</message>
<message name="IDS_BRAVE_WALLET_CLEAR_TX_AND_NONCE_DIALOG_CONFIRMATION" desc="Confirmation dialog description for transaction and nonce reset.">
Are you sure you want to reset transaction and nonce information? This option is mostly used by developers running a local test server. Type "<ph name="CONFIRMATION_PHRASE">%1$s<ex>Yes</ex></ph>" to confirm.
</message>
<message name="IDS_BRAVE_WALLET_CONFIRM_TEXT" desc="Text for Brave Wallet confirm button text.">
Confirm
</message>
Expand Down