From 743661ca906fc9523c81c69a6bea982aa5130d43 Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Mon, 6 Jun 2016 17:31:20 +0200 Subject: [PATCH 1/6] Added field 'displayName' to OwnCloudAccount, and refactoring load of account from storage --- .../android/lib/common/OwnCloudAccount.java | 98 ++++++++----------- .../common/OwnCloudAccountStorageManager.java | 48 +++++++++ .../lib/common/accounts/AccountUtils.java | 6 ++ .../common/operations/RemoteOperation.java | 6 +- 4 files changed, 97 insertions(+), 61 deletions(-) create mode 100644 src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java diff --git a/src/com/owncloud/android/lib/common/OwnCloudAccount.java b/src/com/owncloud/android/lib/common/OwnCloudAccount.java index aa34f2e3..2026ee3e 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudAccount.java +++ b/src/com/owncloud/android/lib/common/OwnCloudAccount.java @@ -46,6 +46,8 @@ public class OwnCloudAccount { private Uri mBaseUri; private OwnCloudCredentials mCredentials; + + private String mDisplayName; private String mSavedAccountName; @@ -53,25 +55,44 @@ public class OwnCloudAccount { /** - * Constructor for already saved OC accounts. + * Full constructor. * - * Do not use for anonymous credentials. + * @param baseUri URI to the OC server to get access to. + * @param credentials Credentials to authenticate in the server. NULL is valid for anonymous credentials. */ - public OwnCloudAccount(Account savedAccount, Context context) throws AccountNotFoundException { - if (savedAccount == null) { - throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null"); + public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) { + if (baseUri == null) { + throw new IllegalArgumentException("Parameter 'baseUri' cannot be null"); } - - if (context == null) { - throw new IllegalArgumentException("Parameter 'context' cannot be null"); + mSavedAccount = null; + mSavedAccountName = null; + mBaseUri = baseUri; + mCredentials = credentials != null ? + credentials : OwnCloudCredentialsFactory.getAnonymousCredentials(); + String username = mCredentials.getUsername(); + if (username != null) { + mSavedAccountName = AccountUtils.buildAccountName(mBaseUri, username); } + } - mSavedAccount = savedAccount; - mSavedAccountName = savedAccount.name; - mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, mSavedAccount)); + + /** + * Partial constructor. + * + * Load of credentials is delayed. + * @param baseUri URI to the OC server to get access to. + */ + public OwnCloudAccount(Uri baseUri) { + if (baseUri == null) { + throw new IllegalArgumentException("Parameter 'baseUri' cannot be null"); + } + mSavedAccount = null; + mSavedAccountName = null; + mBaseUri = baseUri; mCredentials = null; } + /** * Method for deferred load of account attributes from AccountManager * @@ -94,53 +115,6 @@ public class OwnCloudAccount { } } - /* - public OwnCloudAccount(Account savedAccount, Context context) - throws AccountNotFoundException, AuthenticatorException, - IOException, OperationCanceledException { - - if (savedAccount == null) { - throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null"); - } - if (context == null) { - throw new IllegalArgumentException("Parameter 'context' cannot be null"); - } - - mSavedAccountName = savedAccount.name; - mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, savedAccount)); - mCredentials = AccountUtils.getCredentialsForAccount(context, savedAccount); - if (mCredentials == null) { - mCredentials = OwnCloudCredentialsFactory.getAnonymousCredentials(); - } - } - */ - - /** - * Constructor for non yet saved OC accounts. - * - * @param baseUri URI to the OC server to get access to. - * @param credentials Credentials to authenticate in the server. NULL is valid for anonymous credentials. - */ - public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) { - if (baseUri == null) { - throw new IllegalArgumentException("Parameter 'baseUri' cannot be null"); - } - mSavedAccount = null; - mSavedAccountName = null; - mBaseUri = baseUri; - mCredentials = credentials != null ? - credentials : OwnCloudCredentialsFactory.getAnonymousCredentials(); - String username = mCredentials.getUsername(); - if (username != null) { - mSavedAccountName = AccountUtils.buildAccountName(mBaseUri, username); - } - } - - - public boolean isAnonymous() { - return (mCredentials == null); - } // TODO no more - public Uri getBaseUri() { return mBaseUri; } @@ -153,5 +127,11 @@ public class OwnCloudAccount { return mSavedAccountName; } - + public String getDisplayName() { + return mDisplayName; + } + + public void setDisplayName(String displayName) { + mDisplayName = displayName; + } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java b/src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java new file mode 100644 index 00000000..ce533d0b --- /dev/null +++ b/src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java @@ -0,0 +1,48 @@ +package com.owncloud.android.lib.common; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.content.Context; +import android.net.Uri; + +import com.owncloud.android.lib.common.accounts.AccountUtils; + +import java.io.IOException; + +/** + * OwnCloud Account + * + * @author David A. Velasco + */ +public class OwnCloudAccountStorageManager { + + /** + * Constructor for already saved OC accounts. + * + * Do not use for anonymous credentials. + */ + public static OwnCloudAccount getOwnCloudAccount(Account savedAccount, Context context) + throws AccountUtils.AccountNotFoundException { + + if (savedAccount == null) { + throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null"); + } + + if (context == null) { + throw new IllegalArgumentException("Parameter 'context' cannot be null"); + } + + OwnCloudAccount account = new OwnCloudAccount( + Uri.parse(AccountUtils.getBaseUrlForAccount(context, savedAccount)) + ); + + AccountManager ama = AccountManager.get(context.getApplicationContext()); + String displayName = ama.getUserData(savedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME); + if (displayName != null && displayName.length() > 0) { + account.setDisplayName(displayName); + } + + return account; + } + +} diff --git a/src/com/owncloud/android/lib/common/accounts/AccountUtils.java b/src/com/owncloud/android/lib/common/accounts/AccountUtils.java index a13225ce..8b020ab9 100644 --- a/src/com/owncloud/android/lib/common/accounts/AccountUtils.java +++ b/src/com/owncloud/android/lib/common/accounts/AccountUtils.java @@ -379,6 +379,12 @@ public class AccountUtils { * OC account version */ public static final String KEY_OC_ACCOUNT_VERSION = "oc_account_version"; + + /** + * User's display name + */ + public static final String KEY_DISPLAY_NAME = "oc_display_name"; + } } diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index 242f614a..6cc67d56 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -32,6 +32,7 @@ import android.content.Context; import android.os.Handler; import com.owncloud.android.lib.common.OwnCloudAccount; +import com.owncloud.android.lib.common.OwnCloudAccountStorageManager; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClientFactory; import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; @@ -108,7 +109,7 @@ public abstract class RemoteOperation implements Runnable { mAccount = account; mContext = context.getApplicationContext(); try { - OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); + OwnCloudAccount ocAccount = OwnCloudAccountStorageManager.getOwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). getClientFor(ocAccount, mContext); } catch (Exception e) { @@ -277,7 +278,8 @@ public abstract class RemoteOperation implements Runnable { mAccount, mContext, mCallerActivity); } else { /** EOF DEPRECATED */ - OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); + OwnCloudAccount ocAccount = OwnCloudAccountStorageManager. + getOwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). getClientFor(ocAccount, mContext); } From afe65bdc3f3bdbc0491eb91c028600ceec12003f Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Mon, 6 Jun 2016 18:26:55 +0200 Subject: [PATCH 2/6] Return user name when display name is unknown --- src/com/owncloud/android/lib/common/OwnCloudAccount.java | 8 +++++++- .../lib/resources/users/GetRemoteUserNameOperation.java | 8 ++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/com/owncloud/android/lib/common/OwnCloudAccount.java b/src/com/owncloud/android/lib/common/OwnCloudAccount.java index 2026ee3e..b41349c7 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudAccount.java +++ b/src/com/owncloud/android/lib/common/OwnCloudAccount.java @@ -128,7 +128,13 @@ public class OwnCloudAccount { } public String getDisplayName() { - return mDisplayName; + if (mDisplayName != null && mDisplayName.length() > 0) { + return mDisplayName; + } else if (mCredentials != null) { + return mCredentials.getUsername(); + } else { + return "NONE"; + } } public void setDisplayName(String displayName) { diff --git a/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java b/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java index e9676e70..f5e7221f 100644 --- a/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java +++ b/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java @@ -59,10 +59,10 @@ public class GetRemoteUserNameOperation extends RemoteOperation { private static final String NODE_DISPLAY_NAME= "display-name"; private static final String NODE_EMAIL= "email"; - private String mUserName; + private String mDisplayName; - public String getUserName() { - return mUserName; + public String getDisplayName() { + return mDisplayName; } @@ -98,7 +98,7 @@ public class GetRemoteUserNameOperation extends RemoteOperation { ArrayList data = new ArrayList(); data.add(displayName); result.setData(data); - mUserName = displayName; + mDisplayName = displayName; Log_OC.d(TAG, "*** Parsed user information: " + id + " - " + displayName + " - " + email); From 06f27a7c796dcec57f1705ca8f59fa340c589135 Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Thu, 9 Jun 2016 10:54:08 +0200 Subject: [PATCH 3/6] Undo buggy refactoring; too much to do in that sense to get it works --- .../android/lib/common/OwnCloudAccount.java | 51 +++++++++++-------- .../common/OwnCloudAccountStorageManager.java | 48 ----------------- .../common/operations/RemoteOperation.java | 6 +-- 3 files changed, 32 insertions(+), 73 deletions(-) delete mode 100644 src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java diff --git a/src/com/owncloud/android/lib/common/OwnCloudAccount.java b/src/com/owncloud/android/lib/common/OwnCloudAccount.java index b41349c7..7b37efcf 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudAccount.java +++ b/src/com/owncloud/android/lib/common/OwnCloudAccount.java @@ -31,6 +31,7 @@ import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; import android.accounts.Account; +import android.accounts.AccountManager; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; import android.content.Context; @@ -55,7 +56,35 @@ public class OwnCloudAccount { /** - * Full constructor. + * Constructor for already saved OC accounts. + * + * Do not use for anonymous credentials. + */ + public OwnCloudAccount(Account savedAccount, Context context) throws AccountNotFoundException { + if (savedAccount == null) { + throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null"); + } + + if (context == null) { + throw new IllegalArgumentException("Parameter 'context' cannot be null"); + } + + mSavedAccount = savedAccount; + mSavedAccountName = savedAccount.name; + mCredentials = null; // load of credentials is delayed + + AccountManager ama = AccountManager.get(context.getApplicationContext()); + String baseUrl = ama.getUserData(mSavedAccount, AccountUtils.Constants.KEY_OC_BASE_URL); + if (baseUrl == null ) { + throw new AccountNotFoundException(mSavedAccount, "Account not found", null); + } + mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, mSavedAccount)); + mDisplayName = ama.getUserData(mSavedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME); + } + + + /** + * Constructor for non yet saved OC accounts. * * @param baseUri URI to the OC server to get access to. * @param credentials Credentials to authenticate in the server. NULL is valid for anonymous credentials. @@ -76,23 +105,6 @@ public class OwnCloudAccount { } - /** - * Partial constructor. - * - * Load of credentials is delayed. - * @param baseUri URI to the OC server to get access to. - */ - public OwnCloudAccount(Uri baseUri) { - if (baseUri == null) { - throw new IllegalArgumentException("Parameter 'baseUri' cannot be null"); - } - mSavedAccount = null; - mSavedAccountName = null; - mBaseUri = baseUri; - mCredentials = null; - } - - /** * Method for deferred load of account attributes from AccountManager * @@ -137,7 +149,4 @@ public class OwnCloudAccount { } } - public void setDisplayName(String displayName) { - mDisplayName = displayName; - } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java b/src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java deleted file mode 100644 index ce533d0b..00000000 --- a/src/com/owncloud/android/lib/common/OwnCloudAccountStorageManager.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.owncloud.android.lib.common; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.content.Context; -import android.net.Uri; - -import com.owncloud.android.lib.common.accounts.AccountUtils; - -import java.io.IOException; - -/** - * OwnCloud Account - * - * @author David A. Velasco - */ -public class OwnCloudAccountStorageManager { - - /** - * Constructor for already saved OC accounts. - * - * Do not use for anonymous credentials. - */ - public static OwnCloudAccount getOwnCloudAccount(Account savedAccount, Context context) - throws AccountUtils.AccountNotFoundException { - - if (savedAccount == null) { - throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null"); - } - - if (context == null) { - throw new IllegalArgumentException("Parameter 'context' cannot be null"); - } - - OwnCloudAccount account = new OwnCloudAccount( - Uri.parse(AccountUtils.getBaseUrlForAccount(context, savedAccount)) - ); - - AccountManager ama = AccountManager.get(context.getApplicationContext()); - String displayName = ama.getUserData(savedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME); - if (displayName != null && displayName.length() > 0) { - account.setDisplayName(displayName); - } - - return account; - } - -} diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index 6cc67d56..242f614a 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -32,7 +32,6 @@ import android.content.Context; import android.os.Handler; import com.owncloud.android.lib.common.OwnCloudAccount; -import com.owncloud.android.lib.common.OwnCloudAccountStorageManager; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClientFactory; import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; @@ -109,7 +108,7 @@ public abstract class RemoteOperation implements Runnable { mAccount = account; mContext = context.getApplicationContext(); try { - OwnCloudAccount ocAccount = OwnCloudAccountStorageManager.getOwnCloudAccount(mAccount, mContext); + OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). getClientFor(ocAccount, mContext); } catch (Exception e) { @@ -278,8 +277,7 @@ public abstract class RemoteOperation implements Runnable { mAccount, mContext, mCallerActivity); } else { /** EOF DEPRECATED */ - OwnCloudAccount ocAccount = OwnCloudAccountStorageManager. - getOwnCloudAccount(mAccount, mContext); + OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). getClientFor(ocAccount, mContext); } From 75667c67f4455217c1843d576f6c471280c409f4 Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Mon, 13 Jun 2016 13:40:05 +0200 Subject: [PATCH 4/6] Renamed GetRemoteUserNameOperation to GetRemoteUserInfoOperation --- .../users/GetRemoteUserInfoOperation.java | 129 +++++++++++++++++ .../users/GetRemoteUserNameOperation.java | 130 ------------------ 2 files changed, 129 insertions(+), 130 deletions(-) create mode 100644 src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java delete mode 100644 src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java diff --git a/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java b/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java new file mode 100644 index 00000000..c726a6ce --- /dev/null +++ b/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java @@ -0,0 +1,129 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2015 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.resources.users; + +import java.util.ArrayList; + +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.GetMethod; +import org.json.JSONObject; + +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.operations.RemoteOperation; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.common.utils.Log_OC; + + +/** + * Gets information (id, display name, and e-mail address) about the user logged in. + * + * @author masensio + * @author David A. Velasco + */ + +public class GetRemoteUserInfoOperation extends RemoteOperation { + + private static final String TAG = GetRemoteUserInfoOperation.class.getSimpleName(); + + // OCS Route + private static final String OCS_ROUTE = "/index.php/ocs/cloud/user?format=json"; + + // JSON Node names + private static final String NODE_OCS = "ocs"; + private static final String NODE_DATA = "data"; + private static final String NODE_ID = "id"; + private static final String NODE_DISPLAY_NAME = "display-name"; + private static final String NODE_EMAIL = "email"; + + public GetRemoteUserInfoOperation() { + } + + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result = null; + int status = -1; + GetMethod get = null; + + //Get the user + try { + get = new GetMethod(client.getBaseUri() + OCS_ROUTE); + get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + status = client.executeMethod(get); + if (isSuccess(status)) { + String response = get.getResponseBodyAsString(); + Log_OC.d(TAG, "Successful response: " + response); + + // Parse the response + JSONObject respJSON = new JSONObject(response); + JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); + JSONObject respData = respOCS.getJSONObject(NODE_DATA); + + UserInfo userInfo = new UserInfo(); + userInfo.mId = respData.getString(NODE_ID); + userInfo.mDisplayName = respData.getString(NODE_DISPLAY_NAME); + userInfo.mEmail = respData.getString(NODE_EMAIL); + + // Result + result = new RemoteOperationResult(true, status, get.getResponseHeaders()); + // Username in result.data + ArrayList data = new ArrayList(); + data.add(userInfo); + result.setData(data); + + } else { + result = new RemoteOperationResult(false, status, get.getResponseHeaders()); + String response = get.getResponseBodyAsString(); + Log_OC.e(TAG, "Failed response while getting user information "); + if (response != null) { + Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response); + } else { + Log_OC.e(TAG, "*** status code: " + status); + } + } + } catch (Exception e) { + result = new RemoteOperationResult(e); + Log_OC.e(TAG, "Exception while getting OC user information", e); + + } finally { + if (get != null) { + get.releaseConnection(); + } + } + + return result; + } + + private boolean isSuccess(int status) { + return (status == HttpStatus.SC_OK); + } + + + public static class UserInfo { + public String mId = ""; + public String mDisplayName = ""; + public String mEmail = ""; + } + +} diff --git a/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java b/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java deleted file mode 100644 index f5e7221f..00000000 --- a/src/com/owncloud/android/lib/resources/users/GetRemoteUserNameOperation.java +++ /dev/null @@ -1,130 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2015 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.resources.users; - -import java.util.ArrayList; - -import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.GetMethod; -import org.json.JSONObject; - -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; - - -/** - * @author masensio - * - * Get the UserName for a SAML connection, from a JSON with the format: - * id - * display-name - * email - */ - -public class GetRemoteUserNameOperation extends RemoteOperation { - - private static final String TAG = GetRemoteUserNameOperation.class.getSimpleName(); - - // OCS Route - private static final String OCS_ROUTE ="/index.php/ocs/cloud/user?format=json"; - - // JSON Node names - private static final String NODE_OCS = "ocs"; - private static final String NODE_DATA = "data"; - private static final String NODE_ID = "id"; - private static final String NODE_DISPLAY_NAME= "display-name"; - private static final String NODE_EMAIL= "email"; - - private String mDisplayName; - - public String getDisplayName() { - return mDisplayName; - } - - - public GetRemoteUserNameOperation() { - } - - @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result = null; - int status = -1; - GetMethod get = null; - - //Get the user - try { - get = new GetMethod(client.getBaseUri() + OCS_ROUTE); - get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - status = client.executeMethod(get); - if(isSuccess(status)) { - String response = get.getResponseBodyAsString(); - Log_OC.d(TAG, "Successful response: " + response); - - // Parse the response - JSONObject respJSON = new JSONObject(response); - JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); - JSONObject respData = respOCS.getJSONObject(NODE_DATA); - String id = respData.getString(NODE_ID); - String displayName = respData.getString(NODE_DISPLAY_NAME); - String email = respData.getString(NODE_EMAIL); - - // Result - result = new RemoteOperationResult(true, status, get.getResponseHeaders()); - // Username in result.data - ArrayList data = new ArrayList(); - data.add(displayName); - result.setData(data); - mDisplayName = displayName; - - Log_OC.d(TAG, "*** Parsed user information: " + id + " - " + displayName + " - " + email); - - } else { - result = new RemoteOperationResult(false, status, get.getResponseHeaders()); - String response = get.getResponseBodyAsString(); - Log_OC.e(TAG, "Failed response while getting user information "); - if (response != null) { - Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response); - } else { - Log_OC.e(TAG, "*** status code: " + status); - } - } - } catch (Exception e) { - result = new RemoteOperationResult(e); - Log_OC.e(TAG, "Exception while getting OC user information", e); - - } finally { - get.releaseConnection(); - } - - return result; - } - - private boolean isSuccess(int status) { - return (status == HttpStatus.SC_OK); - } - -} From 4d80ca096dd68d718d68517d9ae52510007f8582 Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Wed, 15 Jun 2016 10:21:20 +0200 Subject: [PATCH 5/6] Removed annoying NONE value from OwnCloudAccount#getDisplayName --- src/com/owncloud/android/lib/common/OwnCloudAccount.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/owncloud/android/lib/common/OwnCloudAccount.java b/src/com/owncloud/android/lib/common/OwnCloudAccount.java index 7b37efcf..03965061 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudAccount.java +++ b/src/com/owncloud/android/lib/common/OwnCloudAccount.java @@ -145,7 +145,7 @@ public class OwnCloudAccount { } else if (mCredentials != null) { return mCredentials.getUsername(); } else { - return "NONE"; + return null; } } From f679e55933d93cfb992ab32fb332adc6929233ac Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Thu, 16 Jun 2016 12:14:47 +0200 Subject: [PATCH 6/6] Updated OwnCloudAccount#getDisplayName with better fallbacks --- src/com/owncloud/android/lib/common/OwnCloudAccount.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/com/owncloud/android/lib/common/OwnCloudAccount.java b/src/com/owncloud/android/lib/common/OwnCloudAccount.java index 03965061..60fb2e7f 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudAccount.java +++ b/src/com/owncloud/android/lib/common/OwnCloudAccount.java @@ -144,6 +144,8 @@ public class OwnCloudAccount { return mDisplayName; } else if (mCredentials != null) { return mCredentials.getUsername(); + } else if (mSavedAccount != null) { + return AccountUtils.getUsernameForAccount(mSavedAccount); } else { return null; }