Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6be71552 authored by Lukas Reschke's avatar Lukas Reschke Committed by GitHub
Browse files

Merge pull request #3 from nextcloud/displayName

Get full name of account
parents eb6206b4 d6e18614
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ buildscript {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.android.tools.build:gradle:2.1.2'
    }
}

+46 −49
Original line number Diff line number Diff line
@@ -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;
@@ -47,6 +48,8 @@ public class OwnCloudAccount {
    
    private OwnCloudCredentials mCredentials;

    private String mDisplayName;
    
    private String mSavedAccountName;

    private Account mSavedAccount;
@@ -68,52 +71,17 @@ public class OwnCloudAccount {

        mSavedAccount = savedAccount;
        mSavedAccountName = savedAccount.name;
        mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, mSavedAccount));
        mCredentials = null;
    }

    /**
     * Method for deferred load of account attributes from AccountManager
     *
     * @param context
     * @throws AccountNotFoundException
     * @throws AuthenticatorException
     * @throws IOException
     * @throws OperationCanceledException
     */
    public void loadCredentials(Context context)
        throws AccountNotFoundException, AuthenticatorException,
                IOException, OperationCanceledException {

        if (context == null) {
            throw new IllegalArgumentException("Parameter 'context' cannot be null");
        }
        mCredentials = null;    // load of credentials is delayed

		if (mSavedAccount != null) {
			mCredentials = AccountUtils.getCredentialsForAccount(context, mSavedAccount);
		}
	}

    /*
    public OwnCloudAccount(Account savedAccount, Context context)
    		throws AccountNotFoundException, AuthenticatorException, 
    		IOException, OperationCanceledException {
    	
    	if (savedAccount == null) {
    		throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null");
        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);
        }
    	if (context == null) {
    		throw new IllegalArgumentException("Parameter 'context' cannot be null");
        mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, mSavedAccount));
        mDisplayName = ama.getUserData(mSavedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME);
    }

    	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.
@@ -137,9 +105,27 @@ public class OwnCloudAccount {
    }


	public boolean isAnonymous() {
        return (mCredentials == null);
    }   // TODO no more
    /**
     * Method for deferred load of account attributes from AccountManager
     *
     * @param context
     * @throws AccountNotFoundException
     * @throws AuthenticatorException
     * @throws IOException
     * @throws OperationCanceledException
     */
    public void loadCredentials(Context context)
        throws AccountNotFoundException, AuthenticatorException,
                IOException, OperationCanceledException {

        if (context == null) {
            throw new IllegalArgumentException("Parameter 'context' cannot be null");
        }

		if (mSavedAccount != null) {
			mCredentials = AccountUtils.getCredentialsForAccount(context, mSavedAccount);
		}
	}

    public Uri getBaseUri() {
        return mBaseUri;
@@ -153,5 +139,16 @@ public class OwnCloudAccount {
    	return mSavedAccountName;
    }

    public String getDisplayName() {
        if (mDisplayName != null && mDisplayName.length() > 0) {
            return mDisplayName;
        } else if (mCredentials != null) {
            return mCredentials.getUsername();
        } else if (mSavedAccount != null) {
            return AccountUtils.getUsernameForAccount(mSavedAccount);
        } else {
            return null;
        }
    }

}
 No newline at end of file
+6 −0
Original line number Diff line number Diff line
@@ -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";

    }

}
+129 −0
Original line number Diff line number Diff line
@@ -37,17 +37,15 @@ import com.owncloud.android.lib.common.utils.Log_OC;


/**
 * @author masensio
 * Gets information (id, display name, and e-mail address) about the user logged in.
 *
 * Get the UserName for a SAML connection, from a JSON with the format:
 * 		id
 * 		display-name
 * 		email
 * @author masensio
 * @author David A. Velasco
 */

public class GetRemoteUserNameOperation extends RemoteOperation {
public class GetRemoteUserInfoOperation extends RemoteOperation {

	private static final String TAG = GetRemoteUserNameOperation.class.getSimpleName();
    private static final String TAG = GetRemoteUserInfoOperation.class.getSimpleName();

    // OCS Route
    private static final String OCS_ROUTE = "/index.php/ocs/cloud/user?format=json";
@@ -59,14 +57,7 @@ public class GetRemoteUserNameOperation extends RemoteOperation {
    private static final String NODE_DISPLAY_NAME = "display-name";
    private static final String NODE_EMAIL = "email";

	private String mUserName;

	public String getUserName() {
		return mUserName;
	}

	
	public GetRemoteUserNameOperation() {
    public GetRemoteUserInfoOperation() {
    }

    @Override
@@ -88,19 +79,18 @@ public class GetRemoteUserNameOperation extends RemoteOperation {
                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);

                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<Object> data = new ArrayList<Object>();
                 data.add(displayName);
                data.add(userInfo);
                result.setData(data);
				 mUserName =  displayName;
				 
				 Log_OC.d(TAG, "*** Parsed user information: " + id + " - " + displayName + " - " + email);

            } else {
                result = new RemoteOperationResult(false, status, get.getResponseHeaders());
@@ -117,8 +107,10 @@ public class GetRemoteUserNameOperation extends RemoteOperation {
            Log_OC.e(TAG, "Exception while getting OC user information", e);

        } finally {
            if (get != null) {
                get.releaseConnection();
            }
        }

        return result;
    }
@@ -127,4 +119,11 @@ public class GetRemoteUserNameOperation extends RemoteOperation {
        return (status == HttpStatus.SC_OK);
    }


    public static class UserInfo {
        public String mId = "";
        public String mDisplayName = "";
        public String mEmail = "";
    }

}