(Wallet) Improve account name generation (#34831)
This commit is contained in:
@@ -16,70 +16,62 @@ import org.chromium.base.Log;
|
||||
import org.chromium.brave_wallet.mojom.AccountId;
|
||||
import org.chromium.brave_wallet.mojom.AccountInfo;
|
||||
import org.chromium.brave_wallet.mojom.CoinType;
|
||||
import org.chromium.build.annotations.NullMarked;
|
||||
import org.chromium.chrome.R;
|
||||
import org.chromium.chrome.browser.app.BraveActivity;
|
||||
import org.chromium.chrome.browser.util.TabUtils;
|
||||
import org.chromium.mojo_base.mojom.TimeDelta;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@NullMarked
|
||||
public class WalletUtils {
|
||||
private static final String ACCOUNT_INFO = "accountInfo";
|
||||
private static final String TAG = "WalletUtils";
|
||||
|
||||
private static String getNewAccountPrefixForCoin(@CoinType.EnumType int coinType) {
|
||||
private static String getNewAccountPrefixForCoin(@CoinType.EnumType final int coinType) {
|
||||
switch (coinType) {
|
||||
case CoinType.ETH:
|
||||
case CoinType.ETH -> {
|
||||
return "Ethereum";
|
||||
case CoinType.SOL:
|
||||
}
|
||||
case CoinType.SOL -> {
|
||||
return "Solana";
|
||||
case CoinType.FIL:
|
||||
}
|
||||
case CoinType.FIL -> {
|
||||
return "Filecoin";
|
||||
case CoinType.BTC:
|
||||
}
|
||||
case CoinType.BTC -> {
|
||||
return "Bitcoin";
|
||||
}
|
||||
assert false;
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String timeDeltaToDateString(TimeDelta timeDelta) {
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a", Locale.getDefault());
|
||||
return dateFormat.format(new Date(timeDelta.microseconds / 1000));
|
||||
}
|
||||
|
||||
@SuppressWarnings("NoStreams")
|
||||
public static String generateUniqueAccountName(
|
||||
@CoinType.EnumType int coinType, AccountInfo[] accountInfos) {
|
||||
Context context = ContextUtils.getApplicationContext();
|
||||
if (context == null) {
|
||||
Log.w(TAG, "Application context was null");
|
||||
return "";
|
||||
}
|
||||
Set<String> allNames =
|
||||
Arrays.stream(accountInfos)
|
||||
.map(acc -> acc.name)
|
||||
.collect(Collectors.toCollection(HashSet::new));
|
||||
|
||||
for (int number = 1; number < 1000; ++number) {
|
||||
String accountName =
|
||||
context.getString(
|
||||
R.string.new_account_prefix,
|
||||
getNewAccountPrefixForCoin(coinType),
|
||||
String.valueOf(number));
|
||||
|
||||
if (!allNames.contains(accountName)) {
|
||||
return accountName;
|
||||
}
|
||||
default -> {
|
||||
assert false;
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a unique account name following the Desktop strategy: count existing accounts of
|
||||
* the same coin type and use {@code count + 1} as the account number. This ensures the
|
||||
* suggested name always increments based on total accounts, even if some were renamed.
|
||||
*
|
||||
* <p>Desktop implementation: {@code suggestNewAccountName} in {@code
|
||||
* components/brave_wallet_ui/utils/address-utils.ts}.
|
||||
*/
|
||||
@SuppressWarnings("NoStreams")
|
||||
public static String generateUniqueAccountName(
|
||||
@CoinType.EnumType final int coinType, final AccountInfo[] accountInfos) {
|
||||
final Context context = ContextUtils.getApplicationContext();
|
||||
final int sameCoinAccountCount =
|
||||
(int)
|
||||
Arrays.stream(accountInfos)
|
||||
.filter(acc -> acc.accountId.coin == coinType)
|
||||
.count();
|
||||
|
||||
return context.getString(
|
||||
R.string.new_account_prefix,
|
||||
getNewAccountPrefixForCoin(coinType),
|
||||
String.valueOf(sameCoinAccountCount + 1));
|
||||
}
|
||||
|
||||
public static boolean accountIdsEqual(AccountId left, AccountId right) {
|
||||
@@ -95,15 +87,6 @@ public class WalletUtils {
|
||||
return accountIdsEqual(left.accountId, right.accountId);
|
||||
}
|
||||
|
||||
public static void addAccountInfoToIntent(
|
||||
@NonNull final Intent intent, @NonNull final AccountInfo accountInfo) {
|
||||
ByteBuffer bb = accountInfo.serialize();
|
||||
byte[] bytes = new byte[bb.remaining()];
|
||||
bb.get(bytes);
|
||||
|
||||
intent.putExtra(ACCOUNT_INFO, bytes);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static AccountInfo getAccountInfoFromIntent(@NonNull final Intent intent) {
|
||||
byte[] bytes = intent.getByteArrayExtra(ACCOUNT_INFO);
|
||||
@@ -145,7 +128,7 @@ public class WalletUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void openWalletHelpCenter(Context context) {
|
||||
public static void openWalletHelpCenter(@Nullable Context context) {
|
||||
if (context == null) return;
|
||||
TabUtils.openUrlInCustomTab(context, WalletConstants.WALLET_HELP_CENTER);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user