1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 08:26:10 +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:
David A. Velasco 2016-12-15 13:54:07 +01:00
parent 7b88fb4e6c
commit 455cabb72b
3 changed files with 129 additions and 55 deletions

View File

@ -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);
}
}

View File

@ -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;

View File

@ -25,56 +25,60 @@ package com.owncloud.android.lib.common;
public class OwnCloudClientManagerFactory { 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;
private static OwnCloudClientManager sDefaultSingleton; private static OwnCloudClientManager sDefaultSingleton;
private static String sUserAgent; private static String sUserAgent;
public static OwnCloudClientManager newDefaultOwnCloudClientManager() { public static OwnCloudClientManager newDefaultOwnCloudClientManager() {
return newOwnCloudClientManager(sDefaultPolicy); return newOwnCloudClientManager(sDefaultPolicy);
} }
public static OwnCloudClientManager newOwnCloudClientManager(Policy policy) { public static OwnCloudClientManager newOwnCloudClientManager(Policy policy) {
switch (policy) { switch (policy) {
case ALWAYS_NEW_CLIENT: case ALWAYS_NEW_CLIENT:
return new SimpleFactoryManager(); return new SimpleFactoryManager();
case SINGLE_SESSION_PER_ACCOUNT: case SINGLE_SESSION_PER_ACCOUNT:
return new SingleSessionManager(); return new SingleSessionManager();
default: case SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING:
throw new IllegalArgumentException("Unknown policy"); return new DynamicSessionManager();
}
} default:
throw new IllegalArgumentException("Unknown policy");
}
}
public static OwnCloudClientManager getDefaultSingleton() { public static OwnCloudClientManager getDefaultSingleton() {
if (sDefaultSingleton == null) { if (sDefaultSingleton == null) {
sDefaultSingleton = newDefaultOwnCloudClientManager(); sDefaultSingleton = newDefaultOwnCloudClientManager();
} }
return sDefaultSingleton; return sDefaultSingleton;
} }
public static Policy getDefaultPolicy() { public static Policy getDefaultPolicy() {
return sDefaultPolicy; return sDefaultPolicy;
} }
public static void setDefaultPolicy(Policy policy) { public static void setDefaultPolicy(Policy policy) {
if (policy == null) { if (policy == null) {
throw new IllegalArgumentException("Default policy cannot be NULL"); throw new IllegalArgumentException("Default policy cannot be NULL");
} }
if (defaultSingletonMustBeUpdated(policy)) { if (defaultSingletonMustBeUpdated(policy)) {
sDefaultSingleton = null; sDefaultSingleton = null;
} }
sDefaultPolicy = policy; sDefaultPolicy = policy;
} }
public static void setUserAgent(String userAgent){ public static void setUserAgent(String userAgent) {
sUserAgent = userAgent; sUserAgent = userAgent;
} }
@ -82,19 +86,19 @@ public class OwnCloudClientManagerFactory {
return sUserAgent; return sUserAgent;
} }
private static boolean defaultSingletonMustBeUpdated(Policy policy) { private static boolean defaultSingletonMustBeUpdated(Policy policy) {
if (sDefaultSingleton == null) { if (sDefaultSingleton == null) {
return false; return false;
} }
if (policy == Policy.ALWAYS_NEW_CLIENT && if (policy == Policy.ALWAYS_NEW_CLIENT &&
!(sDefaultSingleton instanceof SimpleFactoryManager)) { !(sDefaultSingleton instanceof SimpleFactoryManager)) {
return true; return true;
} }
if (policy == Policy.SINGLE_SESSION_PER_ACCOUNT && if (policy == Policy.SINGLE_SESSION_PER_ACCOUNT &&
!(sDefaultSingleton instanceof SingleSessionManager)) { !(sDefaultSingleton instanceof SingleSessionManager)) {
return true; return true;
} }
return false; return false;
} }
} }