mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 10:27:45 +00:00 
			
		
		
		
	Add new OwnCloudClientManagerFactory implementation to get OwnCloudClient instances that keep session or not depending on the server version
This commit is contained in:
		
							parent
							
								
									7b88fb4e6c
								
							
						
					
					
						commit
						455cabb72b
					
				| @ -0,0 +1,66 @@ | |||||||
|  | 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.resources.status.OwnCloudVersion; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Dynamic implementation of {@link OwnCloudClientManager}. | ||||||
|  |  * | ||||||
|  |  * Wraps instances of {@link SingleSessionManager} and {@link SimpleFactoryManager} and delegates on one | ||||||
|  |  * or the other depending on the known version of the server corresponding to the {@link OwnCloudAccount} | ||||||
|  |  * | ||||||
|  |  * @author David A. Velasco | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public class DynamicSessionManager implements OwnCloudClientManager { | ||||||
|  | 
 | ||||||
|  |     private SimpleFactoryManager mSimpleFactoryManager = new SimpleFactoryManager(); | ||||||
|  | 
 | ||||||
|  |     private SingleSessionManager mSingleSessionManager = new SingleSessionManager(); | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) | ||||||
|  |         throws AccountUtils.AccountNotFoundException, | ||||||
|  |                 OperationCanceledException, AuthenticatorException, IOException { | ||||||
|  | 
 | ||||||
|  |         OwnCloudVersion ownCloudVersion = null; | ||||||
|  |         if (account.getSavedAccount() != null) { | ||||||
|  |             ownCloudVersion = AccountUtils.getServerVersionForAccount( | ||||||
|  |                 account.getSavedAccount(), context | ||||||
|  |             ); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (ownCloudVersion !=  null && ownCloudVersion.isSessionMonitoringSupported()) { | ||||||
|  |             return mSingleSessionManager.getClientFor(account, context); | ||||||
|  |         } else { | ||||||
|  |             return mSimpleFactoryManager.getClientFor(account, context); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public OwnCloudClient removeClientFor(OwnCloudAccount account) { | ||||||
|  |         OwnCloudClient clientRemoved = mSimpleFactoryManager.removeClientFor(account); | ||||||
|  |         OwnCloudClient clientRemoved2 = mSingleSessionManager.removeClientFor(account); | ||||||
|  |         if (clientRemoved2 != null) { | ||||||
|  |             return clientRemoved2; | ||||||
|  |         } else { | ||||||
|  |             return clientRemoved; | ||||||
|  |         } | ||||||
|  |         // clientRemoved and clientRemoved2 should not be != null at the same time | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void saveAllClients(Context context, String accountType) | ||||||
|  |         throws AccountUtils.AccountNotFoundException, | ||||||
|  |                 AuthenticatorException, IOException, OperationCanceledException { | ||||||
|  |         mSimpleFactoryManager.saveAllClients(context, accountType); | ||||||
|  |         mSingleSessionManager.saveAllClients(context, accountType); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -139,6 +139,10 @@ public class OwnCloudAccount { | |||||||
|     	return mSavedAccountName; |     	return mSavedAccountName; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public Account getSavedAccount() { | ||||||
|  |         return mSavedAccount; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public String getDisplayName() { |     public String getDisplayName() { | ||||||
|         if (mDisplayName != null && mDisplayName.length() > 0) { |         if (mDisplayName != null && mDisplayName.length() > 0) { | ||||||
|             return mDisplayName; |             return mDisplayName; | ||||||
|  | |||||||
| @ -27,7 +27,8 @@ public class OwnCloudClientManagerFactory { | |||||||
| 
 | 
 | ||||||
|     public static enum Policy { |     public static enum Policy { | ||||||
|         ALWAYS_NEW_CLIENT, |         ALWAYS_NEW_CLIENT, | ||||||
| 		SINGLE_SESSION_PER_ACCOUNT |         SINGLE_SESSION_PER_ACCOUNT, | ||||||
|  |         SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private static Policy sDefaultPolicy = Policy.ALWAYS_NEW_CLIENT; |     private static Policy sDefaultPolicy = Policy.ALWAYS_NEW_CLIENT; | ||||||
| @ -48,6 +49,9 @@ public class OwnCloudClientManagerFactory { | |||||||
|             case SINGLE_SESSION_PER_ACCOUNT: |             case SINGLE_SESSION_PER_ACCOUNT: | ||||||
|                 return new SingleSessionManager(); |                 return new SingleSessionManager(); | ||||||
| 
 | 
 | ||||||
|  |             case SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING: | ||||||
|  |                 return new DynamicSessionManager(); | ||||||
|  | 
 | ||||||
|             default: |             default: | ||||||
|                 throw new IllegalArgumentException("Unknown policy"); |                 throw new IllegalArgumentException("Unknown policy"); | ||||||
|         } |         } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user