Search in sources :

Example 1 with UserInfo

use of com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo in project android by owncloud.

the class AuthenticatorActivity method createAccount.

/**
     * Creates a new account through the Account Authenticator that started this activity. 
     * 
     * This makes the account permanent.
     * 
     * TODO Decide how to name the OAuth accounts
     */
private boolean createAccount(RemoteOperationResult authResult) {
    /// create and save new ownCloud account
    boolean isOAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType);
    boolean isSaml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType);
    String lastPermanentLocation = authResult.getLastPermanentLocation();
    if (lastPermanentLocation != null) {
        mServerInfo.mBaseUrl = AccountUtils.trimWebdavSuffix(lastPermanentLocation);
    }
    Uri uri = Uri.parse(mServerInfo.mBaseUrl);
    String username = mUsernameInput.getText().toString().trim();
    if (isOAuth) {
        username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();
    }
    String accountName = com.owncloud.android.lib.common.accounts.AccountUtils.buildAccountName(uri, username);
    Account newAccount = new Account(accountName, MainApp.getAccountType());
    if (AccountUtils.exists(newAccount, getApplicationContext())) {
        // fail - not a new account, but an existing one; disallow
        RemoteOperationResult result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_NEW);
        updateAuthStatusIconAndText(result);
        showAuthStatus();
        Log_OC.d(TAG, result.getLogMessage());
        return false;
    } else {
        mAccount = newAccount;
        if (isOAuth || isSaml) {
            // with external authorizations, the password is never input in the app
            mAccountMgr.addAccountExplicitly(mAccount, "", null);
        } else {
            mAccountMgr.addAccountExplicitly(mAccount, mPasswordInput.getText().toString(), null);
        }
        // include account version with the new account
        mAccountMgr.setUserData(mAccount, Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(AccountUtils.ACCOUNT_VERSION));
        /// add the new account as default in preferences, if there is none already
        Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
        if (defaultAccount == null) {
            SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
            editor.putString("select_oc_account", accountName);
            editor.commit();
        }
        /// prepare result to return to the Authenticator
        //  TODO check again what the Authenticator makes with it; probably has the same 
        //  effect as addAccountExplicitly, but it's not well done
        final Intent intent = new Intent();
        intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, MainApp.getAccountType());
        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
        intent.putExtra(AccountManager.KEY_USERDATA, username);
        if (isOAuth || isSaml) {
            mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);
        }
        /// add user data to the new account; TODO probably can be done in the last parameter 
        //      addAccountExplicitly, or in KEY_USERDATA
        mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION, mServerInfo.mVersion.getVersion());
        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");
        } else if (isOAuth) {
            mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE");
        }
        setAccountAuthenticatorResult(intent.getExtras());
        setResult(RESULT_OK, intent);
        return true;
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) SharedPreferences(android.content.SharedPreferences) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) Intent(android.content.Intent) UserInfo(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo) Uri(android.net.Uri)

Example 2 with UserInfo

use of com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo in project android by owncloud.

the class GetUserProfileOperation method run.

/**
     * 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) {
    UserProfile userProfile = null;
    RemoteOperationResult result = null;
    try {
        /// get display name
        GetRemoteUserInfoOperation getDisplayName = new GetRemoteUserInfoOperation();
        RemoteOperationResult remoteResult = getDisplayName.execute(client);
        if (remoteResult.isSuccess()) {
            // store display name with account data
            AccountManager accountManager = AccountManager.get(MainApp.getAppContext());
            UserInfo userInfo = (UserInfo) remoteResult.getData().get(0);
            Account storedAccount = getStorageManager().getAccount();
            accountManager.setUserData(storedAccount, // keep also there, for the moment
            AccountUtils.Constants.KEY_DISPLAY_NAME, userInfo.mDisplayName);
            // map user info into UserProfile instance
            userProfile = new UserProfile(storedAccount.name, userInfo.mId, userInfo.mDisplayName, userInfo.mEmail);
            /// get avatar (optional for success)
            int dimension = getAvatarDimension();
            UserProfile.UserAvatar currentUserAvatar = getUserProfilesRepository().getAvatar(storedAccount.name);
            GetRemoteUserAvatarOperation getAvatarOperation = new GetRemoteUserAvatarOperation(dimension, (currentUserAvatar == null) ? "" : currentUserAvatar.getEtag());
            remoteResult = getAvatarOperation.execute(client);
            if (remoteResult.isSuccess()) {
                GetRemoteUserAvatarOperation.ResultData avatar = (GetRemoteUserAvatarOperation.ResultData) remoteResult.getData().get(0);
                //
                byte[] avatarData = avatar.getAvatarData();
                String avatarKey = ThumbnailsCacheManager.addAvatarToCache(storedAccount.name, avatarData, dimension);
                UserProfile.UserAvatar userAvatar = new UserProfile.UserAvatar(avatarKey, avatar.getMimeType(), avatar.getEtag());
                userProfile.setAvatar(userAvatar);
            } else if (remoteResult.getCode().equals(RemoteOperationResult.ResultCode.FILE_NOT_FOUND)) {
                Log_OC.i(TAG, "No avatar available, removing cached copy");
                getUserProfilesRepository().deleteAvatar(storedAccount.name);
                ThumbnailsCacheManager.removeAvatarFromCache(storedAccount.name);
            }
            // others are ignored, including 304 (not modified), so the avatar is only stored
            // if changed in the server :D
            /// store userProfile
            getUserProfilesRepository().update(userProfile);
            result = new RemoteOperationResult(RemoteOperationResult.ResultCode.OK);
            ArrayList<Object> data = new ArrayList<>();
            data.add(userProfile);
            result.setData(data);
        } else {
            result = remoteResult;
        }
    } catch (Exception e) {
        Log_OC.e(TAG, "Exception while getting user profile: ", e);
        result = new RemoteOperationResult(e);
    }
    return result;
}
Also used : Account(android.accounts.Account) UserProfile(com.owncloud.android.datamodel.UserProfile) GetRemoteUserInfoOperation(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArrayList(java.util.ArrayList) UserInfo(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo) AccountManager(android.accounts.AccountManager) GetRemoteUserAvatarOperation(com.owncloud.android.lib.resources.users.GetRemoteUserAvatarOperation)

Example 3 with UserInfo

use of com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo in project android by owncloud.

the class AuthenticatorActivity method onGetUserNameFinish.

private void onGetUserNameFinish(RemoteOperationResult result) {
    mWaitingForOpId = Long.MAX_VALUE;
    if (result.isSuccess()) {
        boolean success = false;
        String username = ((UserInfo) result.getData().get(0)).mDisplayName;
        if (mAction == ACTION_CREATE) {
            mUsernameInput.setText(username);
            success = createAccount(result);
        } else {
            if (!mUsernameInput.getText().toString().trim().equals(username)) {
                // fail - not a new account, but an existing one; disallow
                result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME);
                mAuthToken = "";
                updateAuthStatusIconAndText(result);
                showAuthStatus();
                Log_OC.d(TAG, result.getLogMessage());
            } else {
                try {
                    updateAccountAuthentication();
                    success = true;
                } catch (AccountNotFoundException e) {
                    Log_OC.e(TAG, "Account " + mAccount + " was removed!", e);
                    Toast.makeText(this, R.string.auth_account_does_not_exist, Toast.LENGTH_SHORT).show();
                    // don't use a Snackbar, finishing right now
                    finish();
                }
            }
        }
        if (success)
            finish();
    } else {
        int failedStatusText = result.getCode() == ResultCode.SERVICE_UNAVAILABLE ? R.string.service_unavailable : R.string.auth_fail_get_user_name;
        updateFailedAuthStatusIconAndText(failedStatusText);
        showAuthStatus();
        Log_OC.e(TAG, "Access to user name failed: " + result.getLogMessage());
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UserInfo(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo) AccountNotFoundException(com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)3 UserInfo (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo)3 Account (android.accounts.Account)2 AccountManager (android.accounts.AccountManager)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Uri (android.net.Uri)1 UserProfile (com.owncloud.android.datamodel.UserProfile)1 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)1 AccountNotFoundException (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException)1 GetRemoteUserAvatarOperation (com.owncloud.android.lib.resources.users.GetRemoteUserAvatarOperation)1 GetRemoteUserInfoOperation (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation)1 ArrayList (java.util.ArrayList)1