1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 16:36:13 +00:00

Load of credentials from AccountManager is refactored out from constructor

This commit is contained in:
David A. Velasco 2015-02-09 15:27:44 +01:00
parent 2f178c9c34
commit f5fe254c09
4 changed files with 88 additions and 38 deletions

View File

@ -42,7 +42,41 @@ public class OwnCloudAccount {
private String mSavedAccountName;
private Account mSavedAccount;
/**
* Constructor for already saved OC accounts.
*
* Do not use for anonymous credentials.
*/
public OwnCloudAccount(Account savedAccount, Context context) throws AccountNotFoundException {
if (savedAccount == null) {
throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null");
}
mSavedAccount = savedAccount;
mSavedAccountName = savedAccount.name;
mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, mSavedAccount));
mCredentials = null;
}
/**
* Method for deferred load of account attributes from AccountManager
*
* @param context
* @throws AccountNotFoundException
* @throws AuthenticatorException
* @throws IOException
* @throws OperationCanceledException
*/
public void loadCredentials(Context context)
throws AccountNotFoundException, AuthenticatorException,
IOException, OperationCanceledException {
mCredentials = AccountUtils.getCredentialsForAccount(context, mSavedAccount);
}
/*
public OwnCloudAccount(Account savedAccount, Context context)
throws AccountNotFoundException, AuthenticatorException,
IOException, OperationCanceledException {
@ -61,12 +95,19 @@ public class OwnCloudAccount {
mCredentials = OwnCloudCredentialsFactory.getAnonymousCredentials();
}
}
*/
/**
* Constructor for non yet saved OC accounts.
*
* @param baseUri URI to the OC server to get access to.
* @param credentials Credentials to authenticate in the server. NULL is valid for anonymous credentials.
*/
public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) {
if (baseUri == null) {
throw new IllegalArgumentException("Parameter 'baseUri' cannot be null");
}
mSavedAccount = null;
mSavedAccountName = null;
mBaseUri = baseUri;
mCredentials = credentials != null ?
@ -80,7 +121,7 @@ public class OwnCloudAccount {
public boolean isAnonymous() {
return (mCredentials == null);
}
} // TODO no more
public Uri getBaseUri() {
return mBaseUri;

View File

@ -42,7 +42,8 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce
public interface OwnCloudClientManager {
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context);
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException;
public OwnCloudClient removeClientFor(OwnCloudAccount account);

View File

@ -25,31 +25,41 @@
package com.owncloud.android.lib.common;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
import com.owncloud.android.lib.common.utils.Log_OC;
import java.io.IOException;
public class SimpleFactoryManager implements OwnCloudClientManager {
private static final String TAG = SimpleFactoryManager.class.getSimpleName();
@Override
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) {
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : ");
OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(
account.getBaseUri(),
context.getApplicationContext(),
true);
Log_OC.d(TAG, " new client {" +
Log_OC.v(TAG, " new client {" +
(account.getName() != null ?
account.getName() :
AccountUtils.buildAccountName(
account.getBaseUri(),
account.getCredentials().getAuthToken())) +
", " + client.hashCode() + "}");
AccountUtils.buildAccountName(account.getBaseUri(), "")
) + ", " + client.hashCode() + "}");
if (account.getCredentials() == null) {
account.loadCredentials(context);
}
client.setCredentials(account.getCredentials());
return client;
}

View File

@ -62,7 +62,9 @@ public class SingleSessionManager implements OwnCloudClientManager {
@Override
public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context) {
public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : ");
if (account == null) {
throw new IllegalArgumentException("Cannot get an OwnCloudClient for a null account");
@ -70,9 +72,12 @@ public class SingleSessionManager implements OwnCloudClientManager {
OwnCloudClient client = null;
String accountName = account.getName();
String sessionName = AccountUtils.buildAccountName(
String sessionName = account.getCredentials() == null ? "" :
AccountUtils.buildAccountName (
account.getBaseUri(),
account.getCredentials().getAuthToken());
account.getCredentials().getAuthToken()
)
;
if (accountName != null) {
client = mClientsWithKnownUsername.get(accountName);
@ -82,6 +87,7 @@ public class SingleSessionManager implements OwnCloudClientManager {
if (accountName != null) {
client = mClientsWithUnknownUsername.remove(sessionName);
if (client != null) {
// TODO REMOVE THIS LOG
Log_OC.d(TAG, " reusing client {" + sessionName + ", " +
client.hashCode() + "}");
mClientsWithKnownUsername.put(accountName, client);
@ -105,10 +111,9 @@ public class SingleSessionManager implements OwnCloudClientManager {
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
// enable cookie tracking
// Restore Cookies ??
AccountUtils.restoreCookies(accountName, client, context);
account.loadCredentials(context);
client.setCredentials(account.getCredentials());
if (accountName != null) {
mClientsWithKnownUsername.put(accountName, client);
@ -116,10 +121,12 @@ public class SingleSessionManager implements OwnCloudClientManager {
} else {
mClientsWithUnknownUsername.put(sessionName, client);
// TODO REMOVE THIS LOG
Log_OC.d(TAG, " new client {" + sessionName + ", " + client.hashCode() + "}");
}
} else {
if (!reusingKnown) {
// TODO REMOVE THIS LOG
Log_OC.d(TAG, " reusing client {" + sessionName + ", " + client.hashCode() + "}");
}
keepCredentialsUpdated(account, client);
@ -149,17 +156,8 @@ public class SingleSessionManager implements OwnCloudClientManager {
}
}
String sessionName = AccountUtils.buildAccountName(
account.getBaseUri(),
account.getCredentials().getAuthToken());
client = mClientsWithUnknownUsername.remove(sessionName);
if (client != null) {
Log_OC.d(TAG, "Removed client {" + sessionName + ", " + client.hashCode() + "}");
return client;
}
Log_OC.d(TAG, "No client tracked for {" + sessionName + "}");
mClientsWithUnknownUsername.clear();
Log_OC.d(TAG, "No client removed");
return null;
}