Simplify account import

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2018-01-01 17:03:54 +01:00
parent 1880a9f481
commit 2111ef247a
3 changed files with 39 additions and 65 deletions

View File

@ -12,6 +12,9 @@
android:glEsVersion="0x00020000" android:glEsVersion="0x00020000"
android:required="true"/> android:required="true"/>
<uses-permission
android:name="android.permission.AUTHENTICATE_ACCOUNTS"
android:maxSdkVersion="22"/>
<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.CAMERA"/>
<uses-permission <uses-permission

View File

@ -25,15 +25,12 @@ package com.nextcloud.talk.controllers;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.accounts.OperationCanceledException;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -48,7 +45,6 @@ import com.nextcloud.talk.controllers.base.BaseController;
import com.nextcloud.talk.models.ImportAccount; import com.nextcloud.talk.models.ImportAccount;
import com.nextcloud.talk.persistence.entities.UserEntity; import com.nextcloud.talk.persistence.entities.UserEntity;
import com.nextcloud.talk.utils.AccountUtils; import com.nextcloud.talk.utils.AccountUtils;
import com.nextcloud.talk.utils.ErrorMessageHolder;
import com.nextcloud.talk.utils.bundle.BundleBuilder; import com.nextcloud.talk.utils.bundle.BundleBuilder;
import com.nextcloud.talk.utils.bundle.BundleKeys; import com.nextcloud.talk.utils.bundle.BundleKeys;
import com.nextcloud.talk.utils.database.user.UserUtils; import com.nextcloud.talk.utils.database.user.UserUtils;
@ -181,7 +177,7 @@ public class SwitchAccountController extends BaseController {
ImportAccount importAccount; ImportAccount importAccount;
for (Object accountObject : AccountUtils.findAccounts(userUtils.getUsers())) { for (Object accountObject : AccountUtils.findAccounts(userUtils.getUsers())) {
account = (Account) accountObject; account = (Account) accountObject;
importAccount = AccountUtils.getInformationFromAccount(account, null); importAccount = AccountUtils.getInformationFromAccount(account);
participant = new Participant(); participant = new Participant();
participant.setName(importAccount.getUsername()); participant.setName(importAccount.getUsername());
@ -217,35 +213,16 @@ public class SwitchAccountController extends BaseController {
private void getAuthTokenForAccount(Account account) { private void getAuthTokenForAccount(Account account) {
final AccountManager accMgr = AccountManager.get(getActivity()); final AccountManager accMgr = AccountManager.get(getActivity());
String authTokenType = getResources().getString(R.string.nc_import_account_type) + ".password"; ImportAccount importAccount = AccountUtils.getInformationFromAccount(account);
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()));
final Handler handler = new Handler();
accMgr.getAuthToken(account, authTokenType, true,
future -> {
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 @Override

View File

@ -27,8 +27,6 @@ import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import com.nextcloud.talk.R; import com.nextcloud.talk.R;
@ -46,7 +44,7 @@ public class AccountUtils {
public static List<Account> findAccounts(List<UserEntity> userEntitiesList) { public static List<Account> findAccounts(List<UserEntity> userEntitiesList) {
Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext(); Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
final AccountManager accMgr = AccountManager.get(context); 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<Account> accountsAvailable = new ArrayList<>(); List<Account> accountsAvailable = new ArrayList<>();
ImportAccount importAccount; ImportAccount importAccount;
@ -54,35 +52,32 @@ public class AccountUtils {
boolean accountFound; boolean accountFound;
for (Account account : accounts) { for (Account account : accounts) {
accountFound = false; 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; return accountsAvailable;
@ -101,16 +96,15 @@ public class AccountUtils {
return appName; return appName;
} }
public static ImportAccount getInformationFromAccount(Account account, @Nullable Bundle data) { public static ImportAccount getInformationFromAccount(Account account) {
int lastAtPos = account.name.lastIndexOf("@"); int lastAtPos = account.name.lastIndexOf("@");
String urlString = account.name.substring(lastAtPos + 1); String urlString = account.name.substring(lastAtPos + 1);
String username = account.name.substring(0, lastAtPos); String username = account.name.substring(0, lastAtPos);
String password = null; Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
final AccountManager accMgr = AccountManager.get(context);
if (data != null) { String password = accMgr.getPassword(account);
password = data.getString(AccountManager.KEY_AUTHTOKEN);
}
if (urlString.endsWith("/")) { if (urlString.endsWith("/")) {
urlString = urlString.substring(0, urlString.length() - 1); urlString = urlString.substring(0, urlString.length() - 1);