diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java b/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java index 78ac9979..f8a9a2f3 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java @@ -8,6 +8,8 @@ public class OwnCloudClientManagerFactory { } public final static Policy DEFAULT_POLICY = Policy.ALWAYS_NEW_CLIENT; + + private static OwnCloudClientManager mDefaultSingleton; public static OwnCloudClientManager newDefaultOwnCloudClientManager() { return newOwnCloudClientManager(DEFAULT_POLICY); @@ -26,5 +28,11 @@ public class OwnCloudClientManagerFactory { } } - + public static OwnCloudClientManager getDefaultSingleton() { + if (mDefaultSingleton == null) { + mDefaultSingleton = newDefaultOwnCloudClientManager(); + } + return mDefaultSingleton; + } + } diff --git a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java index 03839539..5ced7cc5 100644 --- a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java +++ b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java @@ -7,19 +7,23 @@ import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; import android.content.Context; import android.net.Uri; +import android.util.Log; import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; public class SimpleFactoryManager implements OwnCloudClientManager { - + private static final String TAG = OwnCloudClientManager.class.getSimpleName(); + @Override public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) { + Log.d(TAG, "getClientFor(OwnCloudAccount ... : "); OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( account.getBaseUri(), context.getApplicationContext(), true); + Log.d(TAG, " new client " + client.hashCode()); client.setCredentials(account.getCredentials()); return client; } @@ -29,15 +33,20 @@ public class SimpleFactoryManager implements OwnCloudClientManager { public OwnCloudClient getClientFor(Account savedAccount, Context context) throws OperationCanceledException, AuthenticatorException, AccountNotFoundException, IOException { - - return OwnCloudClientFactory.createOwnCloudClient( + Log.d(TAG, "getClientFor(Account ... : "); + + OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( savedAccount, context.getApplicationContext()); + + Log.d(TAG, " new client " + client.hashCode()); + return client; } @Override public OwnCloudClient getClientFor(Uri serverBaseUri, OwnCloudCredentials credentials, Context context) { + Log.d(TAG, "getClientFor(Uri ... : "); OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( serverBaseUri, @@ -45,6 +54,7 @@ public class SimpleFactoryManager implements OwnCloudClientManager { true); client.setCredentials(credentials); + Log.d(TAG, " new client " + client.hashCode()); return client; } diff --git a/src/com/owncloud/android/lib/common/SingleSessionManager.java b/src/com/owncloud/android/lib/common/SingleSessionManager.java index 17226e5c..99eb5daf 100644 --- a/src/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/src/com/owncloud/android/lib/common/SingleSessionManager.java @@ -71,16 +71,9 @@ public class SingleSessionManager implements OwnCloudClientManager { new HashMap(); - public static OwnCloudClientManager getInstance() { - if (mInstance == null) { - mInstance = new SingleSessionManager(); - } - return mInstance; - } - - @Override public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context) { + Log.d(TAG, "getClientFor(OwnCloudAccount ... : "); if (account == null) { throw new IllegalArgumentException("Cannot get an OwnCloudClient for a null account"); } @@ -92,17 +85,21 @@ public class SingleSessionManager implements OwnCloudClientManager { account.getCredentials().getAuthToken()); if (accountName != null) { - client = mClientsWithKnownUsername.get(account.getName()); + client = mClientsWithKnownUsername.get(accountName); } if (client == null) { if (accountName != null) { client = mClientsWithUnknownUsername.remove(sessionName); if (client != null) { + Log.d(TAG, " reusing client {" + sessionName + ", " + client.hashCode() + "}"); mClientsWithKnownUsername.put(accountName, client); + Log.d(TAG, " moved client to {" + accountName + ", " + client.hashCode() + "}"); } } else { client = mClientsWithUnknownUsername.get(sessionName); } + } else { + Log.d(TAG, " reusing client {" + accountName + ", " + client.hashCode() + "}"); } if (client == null) { @@ -114,9 +111,14 @@ public class SingleSessionManager implements OwnCloudClientManager { client.setCredentials(account.getCredentials()); if (accountName != null) { mClientsWithKnownUsername.put(accountName, client); + Log.d(TAG, " new client {" + accountName + ", " + client.hashCode() + "}"); + } else { mClientsWithUnknownUsername.put(sessionName, client); + Log.d(TAG, " new client {" + sessionName + ", " + client.hashCode() + "}"); } + } else { + Log.d(TAG, " reusing client {" + sessionName + ", " + client.hashCode() + "}"); } return client; diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index fa89b24b..942be396 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -28,6 +28,7 @@ import java.io.IOException; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClientFactory; +import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; import com.owncloud.android.lib.common.OwnCloudCredentials; import com.owncloud.android.lib.common.SingleSessionManager; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; @@ -104,7 +105,8 @@ public abstract class RemoteOperation implements Runnable { mAccount = account; mContext = context.getApplicationContext(); try { - mClient = SingleSessionManager.getInstance().getClientFor(mAccount, mContext); + mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). + getClientFor(mAccount, mContext); } catch (Exception e) { Log.e(TAG, "Error while trying to access to " + mAccount.name, e); return new RemoteOperationResult(e); @@ -251,7 +253,7 @@ public abstract class RemoteOperation implements Runnable { mAccount, mContext, mCallerActivity); } else { /** EOF DEPRECATED */ - mClient = SingleSessionManager.getInstance(). + mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). getClientFor(mAccount, mContext); } } else {