diff --git a/app/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java b/app/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java index 943fa2fa9e..2bcc027631 100644 --- a/app/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java +++ b/app/src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java @@ -113,25 +113,78 @@ public class UserAccountManagerImpl implements UserAccountManager { @Override public boolean exists(Account account) { - Account[] nextcloudAccounts = getAccounts(); + try { + if (account == null) { + Log_OC.d(TAG, "account is null"); + return false; + } + + Account[] nextcloudAccounts = getAccounts(); + if (nextcloudAccounts.length == 0) { + Log_OC.d(TAG, "nextcloudAccounts are empty"); + return false; + } + + if (account.name.isEmpty()) { + Log_OC.d(TAG, "account name is empty"); + return false; + } - if (account != null && account.name != null) { int lastAtPos = account.name.lastIndexOf('@'); + if (lastAtPos == -1) { + Log_OC.d(TAG, "lastAtPos cannot be found"); + return false; + } + + boolean isLastAtPosInBoundsForHostAndPort = lastAtPos + 1 < account.name.length(); + if (!isLastAtPosInBoundsForHostAndPort) { + Log_OC.d(TAG, "lastAtPos not in bounds"); + return false; + } + String hostAndPort = account.name.substring(lastAtPos + 1); + String username = account.name.substring(0, lastAtPos); + if (hostAndPort.isEmpty() || username.isEmpty()) { + Log_OC.d(TAG, "hostAndPort or username is empty"); + return false; + } + String otherHostAndPort; String otherUsername; + for (Account otherAccount : nextcloudAccounts) { + // Skip null accounts or accounts with null names + if (otherAccount == null || otherAccount.name.isEmpty()) { + continue; + } + lastAtPos = otherAccount.name.lastIndexOf('@'); + + // Skip invalid account names + if (lastAtPos == -1) { + continue; + } + + boolean isLastAtPosInBoundsForOtherHostAndPort = lastAtPos + 1 < otherAccount.name.length(); + if (!isLastAtPosInBoundsForOtherHostAndPort) { + continue; + } otherHostAndPort = otherAccount.name.substring(lastAtPos + 1); + otherUsername = otherAccount.name.substring(0, lastAtPos); + if (otherHostAndPort.equals(hostAndPort) && otherUsername.equalsIgnoreCase(username)) { return true; } } + + return false; + } catch (Exception e) { + Log_OC.d(TAG, "Exception caught at UserAccountManagerImpl.exists(): " + e); + return false; } - return false; } @Override