mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-11-03 20:08:00 +00:00 
			
		
		
		
	Added field 'displayName' to OwnCloudAccount, and refactoring load of account from storage
This commit is contained in:
		
							parent
							
								
									48f35c14b4
								
							
						
					
					
						commit
						743661ca90
					
				@ -46,6 +46,8 @@ public class OwnCloudAccount {
 | 
				
			|||||||
    private Uri mBaseUri; 
 | 
					    private Uri mBaseUri; 
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    private OwnCloudCredentials mCredentials;
 | 
					    private OwnCloudCredentials mCredentials;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private String mDisplayName;
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    private String mSavedAccountName;
 | 
					    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 {
 | 
					    public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) {
 | 
				
			||||||
        if (savedAccount == null) {
 | 
					        if (baseUri == null) {
 | 
				
			||||||
            throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null");
 | 
					            throw new IllegalArgumentException("Parameter 'baseUri' cannot be null");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        mSavedAccount = null;
 | 
				
			||||||
        if (context == null) {
 | 
					        mSavedAccountName = null;
 | 
				
			||||||
            throw new IllegalArgumentException("Parameter 'context' cannot be 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;
 | 
					        mCredentials = null;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Method for deferred load of account attributes from AccountManager
 | 
					     * 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() {
 | 
					    public Uri getBaseUri() {
 | 
				
			||||||
        return mBaseUri;
 | 
					        return mBaseUri;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -153,5 +127,11 @@ public class OwnCloudAccount {
 | 
				
			|||||||
    	return mSavedAccountName;
 | 
					    	return mSavedAccountName;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    
 | 
					    public String getDisplayName() {
 | 
				
			||||||
 | 
					        return mDisplayName;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void setDisplayName(String displayName) {
 | 
				
			||||||
 | 
					        mDisplayName = displayName;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -379,6 +379,12 @@ public class AccountUtils {
 | 
				
			|||||||
         * OC account version
 | 
					         * OC account version
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
        public static final String KEY_OC_ACCOUNT_VERSION = "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";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -32,6 +32,7 @@ import android.content.Context;
 | 
				
			|||||||
import android.os.Handler;
 | 
					import android.os.Handler;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.owncloud.android.lib.common.OwnCloudAccount;
 | 
					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.OwnCloudClient;
 | 
				
			||||||
import com.owncloud.android.lib.common.OwnCloudClientFactory;
 | 
					import com.owncloud.android.lib.common.OwnCloudClientFactory;
 | 
				
			||||||
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 | 
					import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 | 
				
			||||||
@ -108,7 +109,7 @@ public abstract class RemoteOperation implements Runnable {
 | 
				
			|||||||
        mAccount = account;
 | 
					        mAccount = account;
 | 
				
			||||||
        mContext = context.getApplicationContext();
 | 
					        mContext = context.getApplicationContext();
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
        	OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
 | 
					        	OwnCloudAccount ocAccount = OwnCloudAccountStorageManager.getOwnCloudAccount(mAccount, mContext);
 | 
				
			||||||
            mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
 | 
					            mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
 | 
				
			||||||
            		getClientFor(ocAccount, mContext);
 | 
					            		getClientFor(ocAccount, mContext);
 | 
				
			||||||
        } catch (Exception e) {
 | 
					        } catch (Exception e) {
 | 
				
			||||||
@ -277,7 +278,8 @@ public abstract class RemoteOperation implements Runnable {
 | 
				
			|||||||
                            		mAccount, mContext, mCallerActivity);
 | 
					                            		mAccount, mContext, mCallerActivity);
 | 
				
			||||||
                        } else {
 | 
					                        } else {
 | 
				
			||||||
                        /** EOF DEPRECATED */
 | 
					                        /** EOF DEPRECATED */
 | 
				
			||||||
                        	OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
 | 
					                        	OwnCloudAccount ocAccount = OwnCloudAccountStorageManager.
 | 
				
			||||||
 | 
					                                    getOwnCloudAccount(mAccount, mContext);
 | 
				
			||||||
                            mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
 | 
					                            mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
 | 
				
			||||||
                            		getClientFor(ocAccount, mContext);
 | 
					                            		getClientFor(ocAccount, mContext);
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user