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

Added field 'displayName' to OwnCloudAccount, and refactoring load of account from storage

This commit is contained in:
David A. Velasco 2016-06-06 17:31:20 +02:00
parent 48f35c14b4
commit 743661ca90
4 changed files with 97 additions and 61 deletions

View File

@ -47,31 +47,52 @@ public class OwnCloudAccount {
private OwnCloudCredentials mCredentials;
private String mDisplayName;
private String mSavedAccountName;
private Account mSavedAccount;
/**
* Constructor for already saved OC accounts.
* Full constructor.
*
* Do not use for anonymous credentials.
* @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(Account savedAccount, Context context) throws AccountNotFoundException {
if (savedAccount == null) {
throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null");
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 ?
credentials : OwnCloudCredentialsFactory.getAnonymousCredentials();
String username = mCredentials.getUsername();
if (username != null) {
mSavedAccountName = AccountUtils.buildAccountName(mBaseUri, username);
}
}
if (context == null) {
throw new IllegalArgumentException("Parameter 'context' cannot be null");
}
mSavedAccount = savedAccount;
mSavedAccountName = savedAccount.name;
mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, mSavedAccount));
/**
* Partial constructor.
*
* Load of credentials is delayed.
* @param baseUri URI to the OC server to get access to.
*/
public OwnCloudAccount(Uri baseUri) {
if (baseUri == null) {
throw new IllegalArgumentException("Parameter 'baseUri' cannot be null");
}
mSavedAccount = null;
mSavedAccountName = null;
mBaseUri = baseUri;
mCredentials = null;
}
/**
* Method for deferred load of account attributes from AccountManager
*
@ -94,53 +115,6 @@ public class OwnCloudAccount {
}
}
/*
public OwnCloudAccount(Account savedAccount, Context context)
throws AccountNotFoundException, AuthenticatorException,
IOException, OperationCanceledException {
if (savedAccount == null) {
throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null");
}
if (context == null) {
throw new IllegalArgumentException("Parameter 'context' cannot be null");
}
mSavedAccountName = savedAccount.name;
mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, savedAccount));
mCredentials = AccountUtils.getCredentialsForAccount(context, savedAccount);
if (mCredentials == null) {
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 ?
credentials : OwnCloudCredentialsFactory.getAnonymousCredentials();
String username = mCredentials.getUsername();
if (username != null) {
mSavedAccountName = AccountUtils.buildAccountName(mBaseUri, username);
}
}
public boolean isAnonymous() {
return (mCredentials == null);
} // TODO no more
public Uri getBaseUri() {
return mBaseUri;
}
@ -153,5 +127,11 @@ public class OwnCloudAccount {
return mSavedAccountName;
}
public String getDisplayName() {
return mDisplayName;
}
public void setDisplayName(String displayName) {
mDisplayName = displayName;
}
}

View File

@ -0,0 +1,48 @@
package com.owncloud.android.lib.common;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.net.Uri;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import java.io.IOException;
/**
* OwnCloud Account
*
* @author David A. Velasco
*/
public class OwnCloudAccountStorageManager {
/**
* Constructor for already saved OC accounts.
*
* Do not use for anonymous credentials.
*/
public static OwnCloudAccount getOwnCloudAccount(Account savedAccount, Context context)
throws AccountUtils.AccountNotFoundException {
if (savedAccount == null) {
throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null");
}
if (context == null) {
throw new IllegalArgumentException("Parameter 'context' cannot be null");
}
OwnCloudAccount account = new OwnCloudAccount(
Uri.parse(AccountUtils.getBaseUrlForAccount(context, savedAccount))
);
AccountManager ama = AccountManager.get(context.getApplicationContext());
String displayName = ama.getUserData(savedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME);
if (displayName != null && displayName.length() > 0) {
account.setDisplayName(displayName);
}
return account;
}
}

View File

@ -379,6 +379,12 @@ public class AccountUtils {
* OC account version
*/
public static final String KEY_OC_ACCOUNT_VERSION = "oc_account_version";
/**
* User's display name
*/
public static final String KEY_DISPLAY_NAME = "oc_display_name";
}
}

View File

@ -32,6 +32,7 @@ import android.content.Context;
import android.os.Handler;
import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudAccountStorageManager;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
@ -108,7 +109,7 @@ public abstract class RemoteOperation implements Runnable {
mAccount = account;
mContext = context.getApplicationContext();
try {
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
OwnCloudAccount ocAccount = OwnCloudAccountStorageManager.getOwnCloudAccount(mAccount, mContext);
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, mContext);
} catch (Exception e) {
@ -277,7 +278,8 @@ public abstract class RemoteOperation implements Runnable {
mAccount, mContext, mCallerActivity);
} else {
/** EOF DEPRECATED */
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
OwnCloudAccount ocAccount = OwnCloudAccountStorageManager.
getOwnCloudAccount(mAccount, mContext);
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, mContext);
}