Change add button behavior; Set default account name

This commit is contained in:
wchen342
2021-11-26 20:58:51 +02:00
parent 268b415869
commit a528d73989
3 changed files with 63 additions and 20 deletions
@@ -8,7 +8,9 @@ package org.chromium.chrome.browser.crypto_wallet.activities;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
@@ -18,6 +20,7 @@ import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import org.chromium.base.Log;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.KeyringController;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.crypto_wallet.KeyringControllerFactory;
@@ -42,6 +45,7 @@ public class AddAccountActivity
private boolean mIsImported;
private EditText mPrivateKeyControl;
private KeyringController mKeyringController;
private EditText mAddAccountText;
private static final int FILE_PICKER_REQUEST_CODE = 1;
@Override
@@ -59,12 +63,30 @@ public class AddAccountActivity
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(getResources().getString(R.string.add_account));
EditText addAccountText = findViewById(R.id.add_account_text);
final Button btnAdd = findViewById(R.id.btn_add);
btnAdd.setEnabled(false);
mAddAccountText = findViewById(R.id.add_account_text);
mAddAccountText.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 add button if input is empty
String inputText = s.toString().trim();
btnAdd.setEnabled(!TextUtils.isEmpty(inputText));
}
});
mPrivateKeyControl = findViewById(R.id.import_account_text);
EditText importAccountPasswordText = findViewById(R.id.import_account_password_text);
Button btnAdd = findViewById(R.id.btn_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@@ -72,29 +94,29 @@ public class AddAccountActivity
if (mIsUpdate) {
if (mIsImported) {
mKeyringController.setDefaultKeyringImportedAccountName(
mAddress, addAccountText.getText().toString(), result -> {
mAddress, mAddAccountText.getText().toString(), result -> {
if (result) {
Intent returnIntent = new Intent();
returnIntent.putExtra(Utils.NAME,
addAccountText.getText().toString());
mAddAccountText.getText().toString());
setResult(Activity.RESULT_OK, returnIntent);
finish();
} else {
addAccountText.setError(
mAddAccountText.setError(
getString(R.string.account_update_failed));
}
});
} else {
mKeyringController.setDefaultKeyringDerivedAccountName(
mAddress, addAccountText.getText().toString(), result -> {
mAddress, mAddAccountText.getText().toString(), result -> {
if (result) {
Intent returnIntent = new Intent();
returnIntent.putExtra(Utils.NAME,
addAccountText.getText().toString());
mAddAccountText.getText().toString());
setResult(Activity.RESULT_OK, returnIntent);
finish();
} else {
addAccountText.setError(
mAddAccountText.setError(
getString(R.string.account_update_failed));
}
});
@@ -102,37 +124,37 @@ public class AddAccountActivity
} else if (!TextUtils.isEmpty(mPrivateKeyControl.getText().toString())) {
if (Utils.isJSONValid(mPrivateKeyControl.getText().toString())) {
mKeyringController.importAccountFromJson(
addAccountText.getText().toString(),
mAddAccountText.getText().toString(),
importAccountPasswordText.getText().toString(),
mPrivateKeyControl.getText().toString(), (result, address) -> {
if (result) {
setResult(Activity.RESULT_OK);
finish();
} else {
addAccountText.setError(
mAddAccountText.setError(
getString(R.string.account_name_empty_error));
}
});
} else {
mKeyringController.importAccount(addAccountText.getText().toString(),
mKeyringController.importAccount(mAddAccountText.getText().toString(),
mPrivateKeyControl.getText().toString(), (result, address) -> {
if (result) {
setResult(Activity.RESULT_OK);
finish();
} else {
addAccountText.setError(
mAddAccountText.setError(
getString(R.string.password_error));
}
});
}
} else {
mKeyringController.addAccount(
addAccountText.getText().toString(), result -> {
mAddAccountText.getText().toString(), result -> {
if (result) {
setResult(Activity.RESULT_OK);
finish();
} else {
addAccountText.setError(
mAddAccountText.setError(
getString(R.string.account_name_empty_error));
}
});
@@ -155,7 +177,7 @@ public class AddAccountActivity
if (!TextUtils.isEmpty(mAddress) && !TextUtils.isEmpty(mName)) {
btnAdd.setText(getResources().getString(R.string.update));
addAccountText.setText(mName);
mAddAccountText.setText(mName);
getSupportActionBar().setTitle(getResources().getString(R.string.update_account));
findViewById(R.id.import_account_layout).setVisibility(View.GONE);
findViewById(R.id.import_account_title).setVisibility(View.GONE);
@@ -204,6 +226,15 @@ public class AddAccountActivity
public void finishNativeInitialization() {
super.finishNativeInitialization();
InitKeyringController();
if (mKeyringController != null) {
mKeyringController.getDefaultKeyringInfo(keyringInfo -> {
if (keyringInfo != null) {
AccountInfo[] accountInfos = keyringInfo.accountInfos;
mAddAccountText.setText(getString(
R.string.new_account_prefix, String.valueOf(accountInfos.length + 1)));
}
});
}
}
@Override
@@ -1,6 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="12dp">
<solid android:color="@color/brave_action_color"/>
<corners android:radius="48dp"/>
</shape>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="false">
<shape android:shape="rectangle" android:padding="12dp">
<solid android:color="@color/modern_grey_50"/>
<corners android:radius="48dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle" android:padding="12dp">
<solid android:color="@color/brave_action_color"/>
<corners android:radius="48dp"/>
</shape>
</item>
</selector>
@@ -1827,6 +1827,9 @@ Are you sure you want to do this?
<message name="IDS_ADD_ACCOUNT_DIALOG_TEXT" desc="Brave Wallet onboarding text">
Buy, trade, or hold crypto. If you have an existing crypto account you may add it now.
</message> -->
<message name="IDS_NEW_ACCOUNT_PREFIX" desc="Brave Wallet new account default name prefix">
Account <ph name="ACCOUNT_NUMBER">%1$s<ex>2</ex></ph>
</message>
<message name="IDS_ADD_ACCOUNT" desc="Brave Wallet account dialog button text">
Add Account
</message>