mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-08 00:16:09 +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;
|
||||
}
|
||||
|
||||
public Account getSavedAccount() {
|
||||
return mSavedAccount;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
if (mDisplayName != null && mDisplayName.length() > 0) {
|
||||
return mDisplayName;
|
||||
|
@ -24,57 +24,61 @@
|
||||
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 enum Policy {
|
||||
ALWAYS_NEW_CLIENT,
|
||||
SINGLE_SESSION_PER_ACCOUNT,
|
||||
SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING
|
||||
}
|
||||
|
||||
private static Policy sDefaultPolicy = Policy.ALWAYS_NEW_CLIENT;
|
||||
|
||||
private static OwnCloudClientManager sDefaultSingleton;
|
||||
|
||||
private static String sUserAgent;
|
||||
|
||||
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 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();
|
||||
|
||||
case SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING:
|
||||
return new DynamicSessionManager();
|
||||
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown policy");
|
||||
}
|
||||
}
|
||||
|
||||
public static OwnCloudClientManager getDefaultSingleton() {
|
||||
if (sDefaultSingleton == null) {
|
||||
sDefaultSingleton = newDefaultOwnCloudClientManager();
|
||||
}
|
||||
return sDefaultSingleton;
|
||||
}
|
||||
|
||||
public static Policy getDefaultPolicy() {
|
||||
return sDefaultPolicy;
|
||||
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;
|
||||
if (policy == null) {
|
||||
throw new IllegalArgumentException("Default policy cannot be NULL");
|
||||
}
|
||||
if (defaultSingletonMustBeUpdated(policy)) {
|
||||
sDefaultSingleton = null;
|
||||
}
|
||||
sDefaultPolicy = policy;
|
||||
}
|
||||
|
||||
public static void setUserAgent(String userAgent){
|
||||
public static void setUserAgent(String userAgent) {
|
||||
sUserAgent = userAgent;
|
||||
}
|
||||
|
||||
@ -82,19 +86,19 @@ public class OwnCloudClientManagerFactory {
|
||||
return sUserAgent;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user