From 743661ca906fc9523c81c69a6bea982aa5130d43 Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Mon, 6 Jun 2016 17:31:20 +0200 Subject: [PATCH] Added field 'displayName' to OwnCloudAccount, and refactoring load of account from storage --- .../android/lib/common/OwnCloudAccount.java | 98 ++++++++----------- .../common/OwnCloudAccountStorageManager.java | 48 +++++++++ .../lib/common/accounts/AccountUtils.java | 6 ++ .../common/operations/RemoteOperation.java | 6 +- 4 files changed, 97 insertions(+), 61 deletions(-) create mode 100644 src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java diff --git a/src/com/owncloud/android/lib/common/OwnCloudAccount.java b/src/com/owncloud/android/lib/common/OwnCloudAccount.java index aa34f2e3..2026ee3e 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudAccount.java +++ b/src/com/owncloud/android/lib/common/OwnCloudAccount.java @@ -46,6 +46,8 @@ public class OwnCloudAccount { private Uri mBaseUri; private OwnCloudCredentials mCredentials; + + private String mDisplayName; private String mSavedAccountName; @@ -53,25 +55,44 @@ public class OwnCloudAccount { /** - * 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"); } - - if (context == null) { - throw new IllegalArgumentException("Parameter 'context' 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); } + } - 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; + } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java b/src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java new file mode 100644 index 00000000..ce533d0b --- /dev/null +++ b/src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java @@ -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; + } + +} diff --git a/src/com/owncloud/android/lib/common/accounts/AccountUtils.java b/src/com/owncloud/android/lib/common/accounts/AccountUtils.java index a13225ce..8b020ab9 100644 --- a/src/com/owncloud/android/lib/common/accounts/AccountUtils.java +++ b/src/com/owncloud/android/lib/common/accounts/AccountUtils.java @@ -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"; + } } diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index 242f614a..6cc67d56 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -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); }