mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-30 09:57:39 +00:00 
			
		
		
		
	Added OwnCloudAccount class and method OwnCloudSessionManager#gertClientFor(OwnCloudAccount, ...) to ease the reuse of OwnCloudClient instances after creating accounts with authentication based in external authentication providers
This commit is contained in:
		
							parent
							
								
									a42f6b5d6d
								
							
						
					
					
						commit
						e069a8cb9f
					
				
							
								
								
									
										95
									
								
								src/com/owncloud/android/lib/common/OwnCloudAccount.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								src/com/owncloud/android/lib/common/OwnCloudAccount.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,95 @@ | |||||||
|  | /* ownCloud Android client application | ||||||
|  |  *   Copyright (C) 2014 ownCloud Inc. | ||||||
|  |  * | ||||||
|  |  *   This program is free software: you can redistribute it and/or modify | ||||||
|  |  *   it under the terms of the GNU General Public License version 2, | ||||||
|  |  *   as published by the Free Software Foundation. | ||||||
|  |  * | ||||||
|  |  *   This program is distributed in the hope that it will be useful, | ||||||
|  |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
|  |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||||
|  |  *   GNU General Public License for more details. | ||||||
|  |  * | ||||||
|  |  *   You should have received a copy of the GNU General Public License | ||||||
|  |  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | package com.owncloud.android.lib.common; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | 
 | ||||||
|  | import com.owncloud.android.lib.common.accounts.AccountUtils; | ||||||
|  | import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; | ||||||
|  | 
 | ||||||
|  | import android.accounts.Account; | ||||||
|  | import android.accounts.AuthenticatorException; | ||||||
|  | import android.accounts.OperationCanceledException; | ||||||
|  | import android.content.Context; | ||||||
|  | import android.net.Uri; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * OwnCloud Account | ||||||
|  |  *  | ||||||
|  |  * @author David A. Velasco | ||||||
|  |  */ | ||||||
|  | public class OwnCloudAccount { | ||||||
|  | 
 | ||||||
|  |     private Uri mBaseUri;  | ||||||
|  |      | ||||||
|  |     private OwnCloudCredentials mCredentials; | ||||||
|  |      | ||||||
|  |     private String mSavedAccountName; | ||||||
|  |      | ||||||
|  |      | ||||||
|  |     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); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |      | ||||||
|  |     public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) { | ||||||
|  |         if (baseUri == null) { | ||||||
|  |             throw new IllegalArgumentException("Parameter 'baseUri' cannot be null"); | ||||||
|  |         } | ||||||
|  |         mSavedAccountName = null; | ||||||
|  |         mBaseUri = baseUri; | ||||||
|  |         mCredentials = credentials != null ?  | ||||||
|  |         		credentials : OwnCloudCredentialsFactory.getAnonymousCredentials(); | ||||||
|  |         String username = credentials.getUsername(); | ||||||
|  |         if (username != null) { | ||||||
|  |         	mSavedAccountName = AccountUtils.buildAccountName(mBaseUri, username); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | 
 | ||||||
|  | 	public boolean isAnonymous() { | ||||||
|  |         return (mCredentials == null); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     public Uri getBaseUri() { | ||||||
|  |         return mBaseUri; | ||||||
|  |     } | ||||||
|  |              | ||||||
|  |     public OwnCloudCredentials getCredentials() { | ||||||
|  |         return mCredentials; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     public String getName() { | ||||||
|  |     	return mSavedAccountName; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |      | ||||||
|  | } | ||||||
| @ -30,6 +30,11 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { | |||||||
| 		); | 		); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	@Override | ||||||
|  | 	public String getUsername() { | ||||||
|  | 		return mUsername; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	public String getAuthToken() { | 	public String getAuthToken() { | ||||||
| 		return mPassword; | 		return mPassword; | ||||||
|  | |||||||
| @ -32,6 +32,12 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials { | |||||||
| 		); | 		); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	@Override | ||||||
|  | 	public String getUsername() { | ||||||
|  | 		// its unknown | ||||||
|  | 		return null; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
| 	@Override | 	@Override | ||||||
| 	public String getAuthToken() { | 	public String getAuthToken() { | ||||||
| 		return mAccessToken; | 		return mAccessToken; | ||||||
|  | |||||||
| @ -63,4 +63,6 @@ public interface OwnCloudClientManager { | |||||||
|     		throws AccountNotFoundException, AuthenticatorException,  |     		throws AccountNotFoundException, AuthenticatorException,  | ||||||
|     		IOException, OperationCanceledException; |     		IOException, OperationCanceledException; | ||||||
| 
 | 
 | ||||||
|  | 	public OwnCloudClient getClientFor(OwnCloudAccount account, Context context); | ||||||
|  |      | ||||||
| } | } | ||||||
|  | |||||||
| @ -21,6 +21,8 @@ public interface OwnCloudCredentials { | |||||||
| 
 | 
 | ||||||
| 	public void applyTo(OwnCloudClient ownCloudClient); | 	public void applyTo(OwnCloudClient ownCloudClient); | ||||||
| 
 | 
 | ||||||
|  | 	public String getUsername(); | ||||||
|  | 	 | ||||||
| 	public String getAuthToken(); | 	public String getAuthToken(); | ||||||
| 	 | 	 | ||||||
| 	public boolean authTokenExpires(); | 	public boolean authTokenExpires(); | ||||||
|  | |||||||
| @ -42,6 +42,12 @@ public class OwnCloudCredentialsFactory { | |||||||
| 		public boolean authTokenExpires() { | 		public boolean authTokenExpires() { | ||||||
| 			return false; | 			return false; | ||||||
| 		} | 		} | ||||||
|  | 
 | ||||||
|  | 		@Override | ||||||
|  | 		public String getUsername() { | ||||||
|  | 			// no user name | ||||||
|  | 			return null; | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -39,6 +39,12 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials { | |||||||
|         } |         } | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	@Override | ||||||
|  | 	public String getUsername() { | ||||||
|  | 		// its unknown | ||||||
|  | 		return null; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
| 	@Override | 	@Override | ||||||
| 	public String getAuthToken() { | 	public String getAuthToken() { | ||||||
| 		return mSessionCookie; | 		return mSessionCookie; | ||||||
|  | |||||||
| @ -13,6 +13,18 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce | |||||||
| public class SimpleFactoryManager implements OwnCloudClientManager { | public class SimpleFactoryManager implements OwnCloudClientManager { | ||||||
| 
 | 
 | ||||||
|      |      | ||||||
|  | 	@Override | ||||||
|  | 	public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) { | ||||||
|  | 		OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( | ||||||
|  | 				account.getBaseUri(),  | ||||||
|  | 				context.getApplicationContext(), | ||||||
|  | 				true); | ||||||
|  | 
 | ||||||
|  | 		client.setCredentials(account.getCredentials()); | ||||||
|  | 		return client; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	 | ||||||
| 	@Override | 	@Override | ||||||
| 	public OwnCloudClient getClientFor(Account savedAccount, Context context) | 	public OwnCloudClient getClientFor(Account savedAccount, Context context) | ||||||
| 			throws OperationCanceledException, AuthenticatorException, AccountNotFoundException, | 			throws OperationCanceledException, AuthenticatorException, AccountNotFoundException, | ||||||
|  | |||||||
| @ -64,12 +64,62 @@ public class SingleSessionManager implements OwnCloudClientManager { | |||||||
|     private Map<String, Map<OwnCloudCredentials, OwnCloudClient>> mClientsPerServer =  |     private Map<String, Map<OwnCloudCredentials, OwnCloudClient>> mClientsPerServer =  | ||||||
|             new HashMap<String, Map<OwnCloudCredentials, OwnCloudClient>>(); |             new HashMap<String, Map<OwnCloudCredentials, OwnCloudClient>>(); | ||||||
|      |      | ||||||
|  |     private Map<String, OwnCloudClient> mClientsWithKnownUsername =  | ||||||
|  |     		new HashMap<String, OwnCloudClient>(); | ||||||
|  |      | ||||||
|  |     private Map<String, OwnCloudClient> mClientsWithUnknownUsername =  | ||||||
|  |     		new HashMap<String, OwnCloudClient>(); | ||||||
|  |      | ||||||
|      |      | ||||||
|     public static OwnCloudClientManager getInstance() { |     public static OwnCloudClientManager getInstance() { | ||||||
|     	if (mInstance == null) { |     	if (mInstance == null) { | ||||||
|     		mInstance = new SingleSessionManager(); |     		mInstance = new SingleSessionManager(); | ||||||
|     	} |     	} | ||||||
|     	return mInstance ; |     	return mInstance; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context) { | ||||||
|  |     	if (account == null) { | ||||||
|  |     		throw new IllegalArgumentException("Cannot get an OwnCloudClient for a null account"); | ||||||
|  |     	} | ||||||
|  | 
 | ||||||
|  |     	OwnCloudClient client = null; | ||||||
|  |     	String accountName = account.getName(); | ||||||
|  |     	String sessionName = AccountUtils.buildAccountName( | ||||||
|  |     			account.getBaseUri(),  | ||||||
|  |     			account.getCredentials().getAuthToken()); | ||||||
|  |     	 | ||||||
|  |     	if (accountName != null) { | ||||||
|  |     		client = mClientsWithKnownUsername.get(account.getName()); | ||||||
|  |     	} | ||||||
|  |     	if (client == null) { | ||||||
|  |     		if (accountName != null) { | ||||||
|  |     			client = mClientsWithUnknownUsername.remove(sessionName); | ||||||
|  |     			if (client != null) { | ||||||
|  |     				mClientsWithKnownUsername.put(accountName, client); | ||||||
|  |     			} | ||||||
|  |     		} else { | ||||||
|  |         		client = mClientsWithUnknownUsername.get(sessionName); | ||||||
|  |     		} | ||||||
|  |     	} | ||||||
|  |     	 | ||||||
|  |     	if (client == null) { | ||||||
|  |     		// no client to reuse - create a new one | ||||||
|  |     		client = OwnCloudClientFactory.createOwnCloudClient( | ||||||
|  |     				account.getBaseUri(),  | ||||||
|  |     				context.getApplicationContext(),  | ||||||
|  |     				true); | ||||||
|  |     		client.setCredentials(account.getCredentials()); | ||||||
|  |     		if (accountName != null) { | ||||||
|  |     			mClientsWithKnownUsername.put(accountName, client); | ||||||
|  |     		} else { | ||||||
|  |     			mClientsWithUnknownUsername.put(sessionName, client); | ||||||
|  |     		} | ||||||
|  |     	} | ||||||
|  |     	 | ||||||
|  |     	return client; | ||||||
|     } |     } | ||||||
|      |      | ||||||
|      |      | ||||||
|  | |||||||
| @ -37,6 +37,7 @@ import android.accounts.AccountsException; | |||||||
| import android.accounts.AuthenticatorException; | import android.accounts.AuthenticatorException; | ||||||
| import android.accounts.OperationCanceledException; | import android.accounts.OperationCanceledException; | ||||||
| import android.content.Context; | import android.content.Context; | ||||||
|  | import android.net.Uri; | ||||||
| 
 | 
 | ||||||
| public class AccountUtils { | public class AccountUtils { | ||||||
|     public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php"; |     public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php"; | ||||||
| @ -187,6 +188,15 @@ public class AccountUtils { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	 | 	 | ||||||
|  |     public static String buildAccountName(Uri serverBaseUrl, String username) { | ||||||
|  |         String accountName = username + "@" + serverBaseUrl.getHost(); | ||||||
|  |         if (serverBaseUrl.getPort() >= 0) { | ||||||
|  |             accountName += ":" + serverBaseUrl.getPort(); | ||||||
|  |         } | ||||||
|  |         return accountName; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |      | ||||||
|     public static class AccountNotFoundException extends AccountsException { |     public static class AccountNotFoundException extends AccountsException { | ||||||
|          |          | ||||||
| 		/** Generated - should be refreshed every time the class changes!! */ | 		/** Generated - should be refreshed every time the class changes!! */ | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user