1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-22 23:36:28 +00:00
android-library/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java
2014-06-17 14:12:30 +02:00

68 lines
1.7 KiB
Java

package com.owncloud.android.lib.common;
public class OwnCloudClientManagerFactory {
public static enum Policy {
ALWAYS_NEW_CLIENT,
SINGLE_SESSION_PER_ACCOUNT
}
private static Policy sDefaultPolicy = Policy.ALWAYS_NEW_CLIENT;
private static OwnCloudClientManager sDefaultSingleton;
public static OwnCloudClientManager newDefaultOwnCloudClientManager() {
return newOwnCloudClientManager(sDefaultPolicy);
}
public static OwnCloudClientManager newOwnCloudClientManager(Policy policy) {
switch (policy) {
case ALWAYS_NEW_CLIENT:
return new SimpleFactoryManager();
case SINGLE_SESSION_PER_ACCOUNT:
return new SingleSessionManager();
default:
throw new IllegalArgumentException("Unknown policy");
}
}
public static OwnCloudClientManager getDefaultSingleton() {
if (sDefaultSingleton == null) {
sDefaultSingleton = newDefaultOwnCloudClientManager();
}
return sDefaultSingleton;
}
public static Policy getDefaultPolicy() {
return sDefaultPolicy;
}
public static void setDefaultPolicy(Policy policy) {
if (policy == null) {
throw new IllegalArgumentException("Default policy cannot be NULL");
}
if (defaultSingletonMustBeUpdated(policy)) {
sDefaultSingleton = null;
}
sDefaultPolicy = policy;
}
private static boolean defaultSingletonMustBeUpdated(Policy policy) {
if (sDefaultSingleton == null) {
return false;
}
if (policy == Policy.ALWAYS_NEW_CLIENT &&
!(sDefaultSingleton instanceof SimpleFactoryManager)) {
return true;
}
if (policy == Policy.SINGLE_SESSION_PER_ACCOUNT &&
!(sDefaultSingleton instanceof SingleSessionManager)) {
return true;
}
return false;
}
}