diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 46a679c06..e2c49dcf3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,9 @@ android:glEsVersion="0x00020000" android:required="true"/> + { - try { - ImportAccount importAccount = AccountUtils.getInformationFromAccount(account, future - .getResult()); - BundleBuilder bundleBuilder = new BundleBuilder(new Bundle()); - bundleBuilder.putString(BundleKeys.KEY_USERNAME, importAccount.getUsername()); - bundleBuilder.putString(BundleKeys.KEY_TOKEN, importAccount.getToken()); - bundleBuilder.putString(BundleKeys.KEY_BASE_URL, importAccount.getBaseUrl()); - bundleBuilder.putBoolean(BundleKeys.KEY_IS_ACCOUNT_IMPORT, true); - getRouter().pushController(RouterTransaction.with(new AccountVerificationController - (bundleBuilder.build())).pushChangeHandler(new HorizontalChangeHandler()) - .popChangeHandler(new HorizontalChangeHandler())); - } catch (OperationCanceledException e) { - Log.e(TAG, "Access was denied"); - ErrorMessageHolder.getInstance().setMessageType( - ErrorMessageHolder.ErrorMessageType.FAILED_TO_IMPORT_ACCOUNT); - new Handler().post(() -> getRouter().popToRoot()); - } catch (Exception e) { - Log.e(TAG, "Something went wrong while accessing token"); - ErrorMessageHolder.getInstance().setMessageType( - ErrorMessageHolder.ErrorMessageType.FAILED_TO_IMPORT_ACCOUNT); - new Handler().post(() -> getRouter().popToRoot()); - } - }, handler - ); } @Override diff --git a/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.java b/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.java index 479144943..e19c6a4fe 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.java @@ -27,8 +27,6 @@ import android.accounts.Account; import android.accounts.AccountManager; import android.content.Context; import android.content.pm.PackageManager; -import android.os.Bundle; -import android.support.annotation.Nullable; import android.util.Log; import com.nextcloud.talk.R; @@ -46,7 +44,7 @@ public class AccountUtils { public static List findAccounts(List userEntitiesList) { Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext(); final AccountManager accMgr = AccountManager.get(context); - final Account[] accounts = accMgr.getAccounts(); + final Account[] accounts = accMgr.getAccountsByType(context.getString(R.string.nc_import_account_type)); List accountsAvailable = new ArrayList<>(); ImportAccount importAccount; @@ -54,35 +52,32 @@ public class AccountUtils { boolean accountFound; for (Account account : accounts) { accountFound = false; - String accountType = account.type.intern(); - - if (context.getResources().getString(R.string.nc_import_account_type).equals(accountType)) { - for (int i = 0; i < userEntitiesList.size(); i++) { - internalUserEntity = userEntitiesList.get(i); - importAccount = getInformationFromAccount(account, null); - if (importAccount.getBaseUrl().startsWith("http://") || - importAccount.getBaseUrl().startsWith("https://")) { - if (internalUserEntity.getUsername().equals(importAccount.getUsername()) && - internalUserEntity.getBaseUrl().equals(importAccount.getBaseUrl())) { - accountFound = true; - break; - } - } else { - if (internalUserEntity.getUsername().equals(importAccount.getUsername()) && - (internalUserEntity.getBaseUrl().equals("http://" + importAccount.getBaseUrl()) || - internalUserEntity.getBaseUrl().equals("https://" + - importAccount.getBaseUrl()))) { - accountFound = true; - break; - } + for (int i = 0; i < userEntitiesList.size(); i++) { + internalUserEntity = userEntitiesList.get(i); + importAccount = getInformationFromAccount(account); + if (importAccount.getBaseUrl().startsWith("http://") || + importAccount.getBaseUrl().startsWith("https://")) { + if (internalUserEntity.getUsername().equals(importAccount.getUsername()) && + internalUserEntity.getBaseUrl().equals(importAccount.getBaseUrl())) { + accountFound = true; + break; + } + } else { + if (internalUserEntity.getUsername().equals(importAccount.getUsername()) && + (internalUserEntity.getBaseUrl().equals("http://" + importAccount.getBaseUrl()) || + internalUserEntity.getBaseUrl().equals("https://" + + importAccount.getBaseUrl()))) { + accountFound = true; + break; } - } - if (!accountFound) { - accountsAvailable.add(account); } } + + if (!accountFound) { + accountsAvailable.add(account); + } } return accountsAvailable; @@ -101,16 +96,15 @@ public class AccountUtils { return appName; } - public static ImportAccount getInformationFromAccount(Account account, @Nullable Bundle data) { + public static ImportAccount getInformationFromAccount(Account account) { int lastAtPos = account.name.lastIndexOf("@"); String urlString = account.name.substring(lastAtPos + 1); String username = account.name.substring(0, lastAtPos); - String password = null; + Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext(); + final AccountManager accMgr = AccountManager.get(context); - if (data != null) { - password = data.getString(AccountManager.KEY_AUTHTOKEN); - } + String password = accMgr.getPassword(account); if (urlString.endsWith("/")) { urlString = urlString.substring(0, urlString.length() - 1);