From 5130a93ef576f0c75d852840355672551fea2e9c Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Fri, 13 Jun 2014 15:08:20 +0200 Subject: [PATCH] Updated OwnCloudClientManager#removeClient for new type OWnCloudAccount --- .../lib/common/OwnCloudClientManager.java | 14 +- .../lib/common/SimpleFactoryManager.java | 88 ++++++------ .../lib/common/SingleSessionManager.java | 130 ++++++------------ 3 files changed, 83 insertions(+), 149 deletions(-) diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java index 25aa4639..e54ae191 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java @@ -26,7 +26,6 @@ package com.owncloud.android.lib.common; import java.io.IOException; -import android.accounts.Account; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; import android.content.Context; @@ -45,21 +44,10 @@ public interface OwnCloudClientManager { public OwnCloudClient getClientFor(OwnCloudAccount account, Context context); - /* - public OwnCloudClient getClientFor(Account savedAccount, Context context) - throws AccountNotFoundException, AuthenticatorException, - IOException, OperationCanceledException; - - public OwnCloudClient getClientFor( - Uri serverBaseUri, OwnCloudCredentials credentials, Context context); - */ + public OwnCloudClient removeClientFor(OwnCloudAccount account); public void saveAllClients(Context context, String accountType) throws AccountNotFoundException, AuthenticatorException, IOException, OperationCanceledException; - public OwnCloudClient removeClientFor(Account account, Context context) - throws AccountNotFoundException, AuthenticatorException, - IOException, OperationCanceledException; - } diff --git a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java index 3f63e959..94918ec6 100644 --- a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java +++ b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java @@ -1,14 +1,34 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + package com.owncloud.android.lib.common; -import android.accounts.Account; -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; -import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; +import android.content.Context; +import android.util.Log; public class SimpleFactoryManager implements OwnCloudClientManager { @@ -22,51 +42,27 @@ public class SimpleFactoryManager implements OwnCloudClientManager { context.getApplicationContext(), true); - Log.d(TAG, " new client " + client.hashCode()); + Log.d(TAG, " new client {" + + (account.getName() != null ? + account.getName() : + AccountUtils.buildAccountName( + account.getBaseUri(), + account.getCredentials().getAuthToken())) + + ", " + client.hashCode() + "}"); + client.setCredentials(account.getCredentials()); return client; } - /* @Override - public OwnCloudClient getClientFor(Account savedAccount, Context context) - throws OperationCanceledException, AuthenticatorException, AccountNotFoundException, - IOException { - 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, - context.getApplicationContext(), - true); - - client.setCredentials(credentials); - Log.d(TAG, " new client " + client.hashCode()); - return client; - } - */ - - @Override - public void saveAllClients(Context context, String accountType) { - // TODO Auto-generated method stub - - } - - @Override - public OwnCloudClient removeClientFor(Account account, Context context) { + public OwnCloudClient removeClientFor(OwnCloudAccount account) { + // nothing to do - not taking care of tracking instances! return null; } + @Override + public void saveAllClients(Context context, String accountType) { + // nothing to do - not taking care of tracking instances! + } + } diff --git a/src/com/owncloud/android/lib/common/SingleSessionManager.java b/src/com/owncloud/android/lib/common/SingleSessionManager.java index 221e1609..1dec9441 100644 --- a/src/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/src/com/owncloud/android/lib/common/SingleSessionManager.java @@ -46,16 +46,12 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce * * TODO check multithreading safety * - * TODO better mapping? - * * @author David A. Velasco * @author masensio */ public class SingleSessionManager implements OwnCloudClientManager { - //private static final String TAG = SingleSessionManager.class.getSimpleName(); - private static final String TAG = SingleSessionManager.class.getSimpleName(); private Map> mClientsPerServer = @@ -130,96 +126,41 @@ public class SingleSessionManager implements OwnCloudClientManager { return client; } - /* - @Override - public synchronized OwnCloudClient getClientFor(Account savedAccount, Context context) - throws OperationCanceledException, AuthenticatorException, AccountNotFoundException, - IOException { - - Uri serverBaseUri = - Uri.parse(AccountUtils.getBaseUrlForAccount(context, savedAccount)); - - OwnCloudCredentials credentials = - AccountUtils.getCredentialsForAccount(context, savedAccount); - - OwnCloudClient client = getClientFor(serverBaseUri, credentials, context); - - // Restore Cookies ?? - AccountUtils.restoreCookies(savedAccount, client, context); - - return client; - - } - */ - - /* - @Override - public synchronized OwnCloudClient getClientFor( - Uri serverBaseUri, OwnCloudCredentials credentials, Context context) { - - Map clientsPerAccount = - mClientsPerServer.get(serverBaseUri.toString()); - - if (clientsPerAccount == null) { - clientsPerAccount = new HashMap(); - mClientsPerServer.put( - serverBaseUri.toString(), - clientsPerAccount); - } - - if (credentials == null) { - credentials = OwnCloudCredentialsFactory.getAnonymousCredentials(); - } - - /// TODO - CRITERIA FOR MATCH OF KEYS!!! - OwnCloudClient client = clientsPerAccount.get(credentials); - if (client == null) { - client = OwnCloudClientFactory.createOwnCloudClient( - serverBaseUri, - context.getApplicationContext(), - true); - - // Restore Cookies - String accountName = AccountUtils.buildAccountName(serverBaseUri, credentials.getUsername()); - AccountUtils.restoreCookies(accountName, client, context); - - client.setCredentials(credentials); - clientsPerAccount.put(credentials, client); - - } - - return client; - } - */ - private void keepCredentialsUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) { - OwnCloudCredentials recentCredentials = account.getCredentials(); - if (!recentCredentials.getAuthToken().equals( - reusedClient.getCredentials().getAuthToken())) { - reusedClient.setCredentials(recentCredentials); - } + @Override + public OwnCloudClient removeClientFor(OwnCloudAccount account) { + + if (account == null) { + return null; + } + + OwnCloudClient client = null; + String accountName = account.getName(); + if (accountName != null) { + client = mClientsWithKnownUsername.remove(accountName); + if (client != null) { + Log.d(TAG, "Removed client {" + accountName + ", " + client.hashCode() + "}"); + return client; + } else { + Log.d(TAG, "No client tracked for {" + accountName + "}"); + } + } + + String sessionName = AccountUtils.buildAccountName( + account.getBaseUri(), + account.getCredentials().getAuthToken()); + client = mClientsWithUnknownUsername.remove(sessionName); + if (client != null) { + Log.d(TAG, "Removed client {" + sessionName + ", " + client.hashCode() + "}"); + return client; + } + Log.d(TAG, "No client tracked for {" + sessionName + "}"); + + Log.d(TAG, "No client removed"); + return null; } - @Override - public synchronized OwnCloudClient removeClientFor(Account savedAccount, Context context) - throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException { - - Uri serverBaseUri = - Uri.parse(AccountUtils.getBaseUrlForAccount(context, savedAccount)); - - Map clientsPerAccount = - mClientsPerServer.get(serverBaseUri.toString()); - - if (clientsPerAccount != null) { - OwnCloudCredentials credentials = - AccountUtils.getCredentialsForAccount(context, savedAccount); - - return clientsPerAccount.remove(credentials); - } - return null; - } - @Override public synchronized void saveAllClients(Context context, String accountType) @@ -253,5 +194,14 @@ public class SingleSessionManager implements OwnCloudClientManager { } + + private void keepCredentialsUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) { + OwnCloudCredentials recentCredentials = account.getCredentials(); + if (!recentCredentials.getAuthToken().equals( + reusedClient.getCredentials().getAuthToken())) { + reusedClient.setCredentials(recentCredentials); + } + + } }