Procházet zdrojové kódy

Merge pull request #1709 from owncloud/1639_show_display_name_instead_of_login_name

Show display name instead of login name
David A. Velasco před 9 roky
rodič
revize
c14d049aa6

+ 1 - 1
owncloud-android-library

@@ -1 +1 @@
-Subproject commit 48f35c14b4a1ce82c9e09c37aaa918bbfbb98315
+Subproject commit 0ce18350cfdf246ecad91aab8345c5141c12339c

+ 15 - 2
src/com/owncloud/android/authentication/AuthenticatorActivity.java

@@ -79,7 +79,8 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
-import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation;
+import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
+import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo;
 import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod;
 import com.owncloud.android.operations.GetServerInfoOperation;
 import com.owncloud.android.operations.OAuth2GetAccessToken;
@@ -1030,7 +1031,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         } else if (operation instanceof OAuth2GetAccessToken) {
             onGetOAuthAccessTokenFinish(result);
 
-        } else if (operation instanceof GetRemoteUserNameOperation) {
+        } else if (operation instanceof GetRemoteUserInfoOperation) {
             onGetUserNameFinish(result);
         }
 
@@ -1594,6 +1595,18 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             mAccountMgr.setUserData(
                     mAccount, Constants.KEY_OC_BASE_URL,   mServerInfo.mBaseUrl
             );
+            if (authResult.getData() != null) {
+                try {
+                    UserInfo userInfo = (UserInfo) authResult.getData().get(0);
+                    mAccountMgr.setUserData(
+                        mAccount, Constants.KEY_DISPLAY_NAME, userInfo.mDisplayName
+                    );
+                } catch (ClassCastException c) {
+                    Log_OC.w(TAG, "Couldn't get display name for " + username);
+                }
+            } else {
+                Log_OC.w(TAG, "Couldn't get display name for " + username);
+            }
 
             if (isSaml) {
                 mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE"); 

+ 9 - 3
src/com/owncloud/android/authentication/AuthenticatorAsyncTask.java

@@ -30,6 +30,7 @@ import com.owncloud.android.lib.common.OwnCloudCredentials;
 import com.owncloud.android.lib.common.network.RedirectionPath;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
+import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
 
 import java.lang.ref.WeakReference;
 
@@ -44,11 +45,10 @@ public class AuthenticatorAsyncTask  extends AsyncTask<Object, Void, RemoteOpera
 
     private Context mContext;
     private final WeakReference<OnAuthenticatorTaskListener> mListener;
-    protected Activity mActivity;
 
     public AuthenticatorAsyncTask(Activity activity) {
         mContext = activity.getApplicationContext();
-        mListener = new WeakReference<OnAuthenticatorTaskListener>((OnAuthenticatorTaskListener)activity);
+        mListener = new WeakReference<>((OnAuthenticatorTaskListener)activity);
     }
 
     @Override
@@ -64,7 +64,7 @@ public class AuthenticatorAsyncTask  extends AsyncTask<Object, Void, RemoteOpera
             OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
             client.setCredentials(credentials);
 
-            // Operation
+            // Operation - try credentials
             ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(
                     REMOTE_PATH,
                     mContext,
@@ -78,6 +78,12 @@ public class AuthenticatorAsyncTask  extends AsyncTask<Object, Void, RemoteOpera
                 result.setLastPermanentLocation(permanentLocation);
             }
 
+            // Operation - get display name
+            if (result.isSuccess()) {
+                GetRemoteUserInfoOperation remoteUserNameOperation = new GetRemoteUserInfoOperation();
+                result = remoteUserNameOperation.execute(client);
+            }
+
         } else {
             result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
         }

+ 4 - 2
src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

@@ -168,8 +168,10 @@ public class ThumbnailsCacheManager {
 
             try {
                 if (mAccount != null) {
-                    OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount,
-                            MainApp.getAppContext());
+                    OwnCloudAccount ocAccount = new OwnCloudAccount(
+                            mAccount,
+                            MainApp.getAppContext()
+                    );
                     mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
                             getClientFor(ocAccount, MainApp.getAppContext());
                 }

+ 4 - 1
src/com/owncloud/android/files/services/FileDownloader.java

@@ -411,7 +411,10 @@ public class FileDownloader extends Service
 
                     // always get client from client manager, to get fresh credentials in case
                     // of update
-                    OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
+                    OwnCloudAccount ocAccount = new OwnCloudAccount(
+                            mCurrentAccount,
+                            this
+                    );
                     mDownloadClient = OwnCloudClientManagerFactory.getDefaultSingleton().
                             getClientFor(ocAccount, this);
 

+ 4 - 1
src/com/owncloud/android/files/services/FileUploader.java

@@ -907,7 +907,10 @@ public class FileUploader extends Service
                 }   // else, reuse storage manager from previous operation
 
                 // always get client from client manager, to get fresh credentials in case of update
-                OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
+                OwnCloudAccount ocAccount = new OwnCloudAccount(
+                        mCurrentAccount,
+                        this
+                );
                 mUploadClient = OwnCloudClientManagerFactory.getDefaultSingleton().
                         getClientFor(ocAccount, this);
 

+ 72 - 0
src/com/owncloud/android/operations/GetUserProfileOperation.java

@@ -0,0 +1,72 @@
+/**
+ *   ownCloud Android client application
+ *
+ *   @author David A. Velasco
+ *   Copyright (C) 2016 ownCloud Inc.
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+package com.owncloud.android.operations;
+
+import android.accounts.Account;
+import android.accounts.AccountManager;
+
+import com.owncloud.android.MainApp;
+import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.accounts.AccountUtils;
+import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
+import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo;
+import com.owncloud.android.operations.common.SyncOperation;
+
+/**
+ * Get and save user's profile from the server.
+ *
+ * Currently only retrieves the display name.
+ */
+public class GetUserProfileOperation extends SyncOperation {
+
+    /**
+     * Performs the operation.
+     *
+     * Target user account is implicit in 'client'.
+     *
+     * Stored account is implicit in {@link #getStorageManager()}.
+     *
+     * @return      Result of the operation. If successful, includes an instance of
+     *              {@link String} with the display name retrieved from the server.
+     *              Call {@link RemoteOperationResult#getData()}.get(0) to get it.
+     */
+    @Override
+    protected RemoteOperationResult run(OwnCloudClient client) {
+
+        // get display name
+        GetRemoteUserInfoOperation getDisplayName = new GetRemoteUserInfoOperation();
+        RemoteOperationResult result = getDisplayName.execute(client);
+
+        if (result.isSuccess()) {
+            // store display name with account data
+            AccountManager accountManager = AccountManager.get(MainApp.getAppContext());
+            UserInfo userInfo = (UserInfo) result.getData().get(0);
+            Account storedAccount = getStorageManager().getAccount();
+            accountManager.setUserData(
+                storedAccount,
+                AccountUtils.Constants.KEY_DISPLAY_NAME,
+                userInfo.mDisplayName
+            );
+        }
+        return result;
+    }
+
+}

+ 13 - 4
src/com/owncloud/android/operations/RefreshFolderOperation.java

@@ -187,7 +187,7 @@ public class RefreshFolderOperation extends RemoteOperation {
         
         if (OCFile.ROOT_PATH.equals(mLocalFolder.getRemotePath()) && !mSyncFullAccount) {
             updateOCVersion(client);
-
+            updateUserProfile();
         }
         
         result = checkForChanges(client);
@@ -226,7 +226,6 @@ public class RefreshFolderOperation extends RemoteOperation {
         
     }
 
-
     private void updateOCVersion(OwnCloudClient client) {
         UpdateOCVersionOperation update = new UpdateOCVersionOperation(mAccount, mContext);
         RemoteOperationResult result = update.execute(client);
@@ -235,14 +234,24 @@ public class RefreshFolderOperation extends RemoteOperation {
 
             // Update Capabilities for this account
             if (update.getOCVersion().isVersionWithCapabilitiesAPI()) {
-                updateCapabilities(client);
+                updateCapabilities();
             } else {
                 Log_OC.d(TAG, "Capabilities API disabled");
             }
         }
     }
 
-    private void updateCapabilities(OwnCloudClient client){
+    private void updateUserProfile() {
+        GetUserProfileOperation update = new GetUserProfileOperation();
+        RemoteOperationResult result = update.execute(mStorageManager, mContext);
+        if (!result.isSuccess()) {
+            Log_OC.w(TAG, "Couldn't update user profile from server");
+        } else {
+            Log_OC.i(TAG, "Got display name: " + result.getData().get(0));
+        }
+    }
+
+    private void updateCapabilities(){
         GetCapabilitiesOperarion getCapabilities = new GetCapabilitiesOperarion();
         RemoteOperationResult  result = getCapabilities.execute(mStorageManager,mContext);
         if (!result.isSuccess()){

+ 3 - 4
src/com/owncloud/android/services/OperationsService.java

@@ -51,7 +51,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
-import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation;
+import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
 import com.owncloud.android.operations.CheckCurrentCredentialsOperation;
 import com.owncloud.android.operations.CopyFileOperation;
 import com.owncloud.android.operations.CreateFolderOperation;
@@ -447,8 +447,7 @@ public class OperationsService extends Service {
                     if (mLastTarget == null || !mLastTarget.equals(next.first)) {
                         mLastTarget = next.first;
                         if (mLastTarget.mAccount != null) {
-                            OwnCloudAccount ocAccount = new OwnCloudAccount(mLastTarget.mAccount,
-                                    mService);
+                            OwnCloudAccount ocAccount = new OwnCloudAccount(mLastTarget.mAccount, mService);
                             mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().
                                     getClientFor(ocAccount, mService);
 
@@ -643,7 +642,7 @@ public class OperationsService extends Service {
 
                 } else if (action.equals(ACTION_GET_USER_NAME)) {
                     // Get User Name
-                    operation = new GetRemoteUserNameOperation();
+                    operation = new GetRemoteUserInfoOperation();
                     
                 } else if (action.equals(ACTION_RENAME)) {
                     // Rename file or folder

+ 0 - 2
src/com/owncloud/android/syncadapter/AbstractOwnCloudSyncAdapter.java

@@ -25,7 +25,6 @@ package com.owncloud.android.syncadapter;
 import java.io.IOException;
 
 import com.owncloud.android.datamodel.FileDataStorageManager;
-import com.owncloud.android.lib.common.accounts.AccountUtils;
 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudClient;
@@ -100,7 +99,6 @@ public abstract class AbstractOwnCloudSyncAdapter extends
 
     protected void initClientForCurrentAccount() throws OperationCanceledException,
             AuthenticatorException, IOException, AccountNotFoundException {
-        AccountUtils.constructFullURLForAccount(getContext(), account);
         OwnCloudAccount ocAccount = new OwnCloudAccount(account, getContext());
         mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
                 getClientFor(ocAccount, getContext());

+ 11 - 4
src/com/owncloud/android/ui/activity/FileActivity.java

@@ -456,8 +456,16 @@ public class FileActivity extends AppCompatActivity
     protected void setUsernameInDrawer(View navigationDrawerLayout, Account account) {
         if (navigationDrawerLayout != null && account != null) {
             TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username);
-            int lastAtPos = account.name.lastIndexOf("@");
-            username.setText(account.name.substring(0, lastAtPos));
+            try {
+                OwnCloudAccount oca = new OwnCloudAccount(account, this);
+                username.setText(oca.getDisplayName());
+
+            } catch (Exception e) {
+                Log_OC.w(TAG, "Couldn't read display name of account; using account name instead");
+
+                int lastAtPos = account.name.lastIndexOf("@");
+                username.setText(account.name.substring(0, lastAtPos));
+            }
         }
     }
 
@@ -826,8 +834,7 @@ public class FileActivity extends AppCompatActivity
                 account = getAccount();
             }
             OwnCloudClient client;
-            OwnCloudAccount ocAccount =
-                    new OwnCloudAccount(account, context);
+            OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
             client = (OwnCloudClientManagerFactory.getDefaultSingleton().
                     removeClientFor(ocAccount));
             if (client != null) {

+ 15 - 9
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -977,22 +977,28 @@ public class FileDisplayActivity extends HookActivity
                                         .equals(event));
 
                         if (RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.
-                            equals(event) &&/// TODO refactor and make common
+                            equals(event)) {
 
-                            synchResult != null && !synchResult.isSuccess()) {
+                            if (synchResult != null && !synchResult.isSuccess()) {
+                                /// TODO refactor and make common
 
-                            if(ResultCode.UNAUTHORIZED.equals(synchResult.getCode()) ||
-                                (synchResult.isException() && synchResult.getException()
-                                    instanceof AuthenticatorException)) {
+                                if (ResultCode.UNAUTHORIZED.equals(synchResult.getCode()) ||
+                                    (synchResult.isException() && synchResult.getException()
+                                        instanceof AuthenticatorException)) {
 
-                                requestCredentialsUpdate(context);
+                                    requestCredentialsUpdate(context);
 
-                            } else if(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED.equals(
-                                synchResult.getCode())) {
+                                } else if (RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED.equals(
+                                    synchResult.getCode())) {
+
+                                    showUntrustedCertDialog(synchResult);
+                                }
 
-                                showUntrustedCertDialog(synchResult);
                             }
 
+                            if (synchFolderRemotePath.equals(OCFile.ROOT_PATH)) {
+                                setUsernameInDrawer(mDrawerLayout, getAccount());
+                            }
                         }
 
                     }

+ 16 - 3
src/com/owncloud/android/ui/activity/Preferences.java

@@ -74,6 +74,7 @@ import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.FileOperationsHelper;
 import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;
+import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.services.OperationsService;
 import com.owncloud.android.ui.RadioButtonPreference;
@@ -744,12 +745,24 @@ public class Preferences extends PreferenceActivity
                     null);
         }
         else {
-
+            OwnCloudAccount oca;
             for (Account a : accounts) {
                 RadioButtonPreference accountPreference = new RadioButtonPreference(this);
                 accountPreference.setKey(a.name);
-                // Handle internationalized domain names
-                accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
+                try {
+                    oca = new OwnCloudAccount(a, this);
+                    accountPreference.setTitle(
+                        oca.getDisplayName() + " @ " +
+                        DisplayUtils.convertIdn(a.name.substring(a.name.lastIndexOf("@") + 1), false)
+                    );
+                } catch (Exception e) {
+                    Log_OC.w(
+                        TAG,
+                        "Account not found right after being read :\\ ; using account name instead of display name"
+                    );
+                    // Handle internationalized domain names
+                    accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
+                }
                 mAccountsPrefCategory.addPreference(accountPreference);
 
                 // Check the current account that is being used

+ 19 - 6
src/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java

@@ -59,6 +59,7 @@ import com.owncloud.android.authentication.AccountAuthenticator;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.db.PreferenceManager;
 import com.owncloud.android.files.services.FileUploader;
+import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
@@ -265,14 +266,26 @@ public class ReceiveExternalFilesActivity extends FileActivity
             });
             return builder.create();
         case DIALOG_MULTIPLE_ACCOUNT:
-            CharSequence ac[] = new CharSequence[
-                    mAccountManager.getAccountsByType(MainApp.getAccountType()).length];
-            for (int i = 0; i < ac.length; ++i) {
-                ac[i] = DisplayUtils.convertIdn(
-                        mAccountManager.getAccountsByType(MainApp.getAccountType())[i].name, false);
+            Account accounts[] = mAccountManager.getAccountsByType(MainApp.getAccountType());
+            CharSequence dialogItems[] = new CharSequence[accounts.length];
+            OwnCloudAccount oca;
+            for (int i = 0; i < dialogItems.length; ++i) {
+                try {
+                    oca = new OwnCloudAccount(accounts[i], this);
+                    dialogItems[i] =
+                        oca.getDisplayName() + " @ " +
+                        DisplayUtils.convertIdn(
+                            accounts[i].name.substring(accounts[i].name.lastIndexOf("@") + 1),
+                            false
+                        );
+
+                } catch (Exception e) {
+                    Log_OC.w(TAG, "Couldn't read display name of account; using account name instead");
+                    dialogItems[i] = DisplayUtils.convertIdn(accounts[i].name, false);
+                }
             }
             builder.setTitle(R.string.common_choose_account);
-            builder.setItems(ac, new OnClickListener() {
+            builder.setItems(dialogItems, new OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int which) {
                     setAccount(mAccountManager.getAccountsByType(MainApp.getAccountType())[which]);

+ 14 - 1
src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java

@@ -19,6 +19,7 @@
  */
 package com.owncloud.android.ui.adapter;
 
+import android.accounts.Account;
 import android.content.Context;
 import android.database.DataSetObserver;
 import android.graphics.Bitmap;
@@ -36,6 +37,7 @@ import android.widget.TextView;
 import android.widget.Toast;
 
 import com.owncloud.android.R;
+import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.datamodel.UploadsStorageManager;
@@ -43,6 +45,7 @@ import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus;
 import com.owncloud.android.db.OCUpload;
 import com.owncloud.android.db.UploadResult;
 import com.owncloud.android.files.services.FileUploader;
+import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.activity.FileActivity;
@@ -238,7 +241,17 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
             uploadDateTextView.setText(dateString);
 
             TextView accountNameTextView = (TextView) view.findViewById(R.id.upload_account);
-            accountNameTextView.setText(upload.getAccountName());
+            try {
+                Account account = AccountUtils.getOwnCloudAccountByName(mParentActivity, upload.getAccountName());
+                OwnCloudAccount oca = new OwnCloudAccount(account, mParentActivity);
+                accountNameTextView.setText(
+                    oca.getDisplayName() + " @ " +
+                    DisplayUtils.convertIdn(account.name.substring(account.name.lastIndexOf("@") + 1), false)
+                );
+            } catch (Exception e) {
+                Log_OC.w(TAG, "Couldn't get display name for account, using old style");
+                accountNameTextView.setText(upload.getAccountName());
+            }
 
             TextView statusTextView = (TextView) view.findViewById(R.id.upload_status);
 

+ 4 - 2
src/com/owncloud/android/utils/GetShareWithUsersAsyncTask.java

@@ -64,8 +64,10 @@ public class GetShareWithUsersAsyncTask extends AsyncTask<Object, Void, Pair<Rem
             try {
                 // Get shares request
                 operation = new GetSharesForFileOperation(file.getRemotePath(), false, false);
-                OwnCloudAccount ocAccount = new OwnCloudAccount(account,
-                        MainApp.getAppContext());
+                OwnCloudAccount ocAccount = new OwnCloudAccount(
+                        account,
+                        MainApp.getAppContext()
+                );
                 OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().
                         getClientFor(ocAccount, MainApp.getAppContext());
                 result = operation.execute(client, fileDataStorageManager);