Search in sources :

Example 1 with GetRemoteUserInfoOperation

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

the class AuthenticatorAsyncTask method doInBackground.

@Override
protected RemoteOperationResult doInBackground(Object... params) {
    RemoteOperationResult result;
    if (params != null && params.length == 2) {
        String url = (String) params[0];
        OwnCloudCredentials credentials = (OwnCloudCredentials) params[1];
        // Client
        Uri uri = Uri.parse(url);
        OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
        client.setCredentials(credentials);
        // Operation - try credentials
        ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(REMOTE_PATH, mContext, SUCCESS_IF_ABSENT);
        result = operation.execute(client);
        String targetUrlAfterPermanentRedirection = null;
        if (operation.wasRedirected()) {
            RedirectionPath redirectionPath = operation.getRedirectionPath();
            targetUrlAfterPermanentRedirection = redirectionPath.getLastPermanentLocation();
        }
        // Operation - get display name
        if (result.isSuccess()) {
            GetRemoteUserInfoOperation remoteUserNameOperation = new GetRemoteUserInfoOperation();
            if (targetUrlAfterPermanentRedirection != null) {
                // we can't assume that any subpath of the domain is correctly redirected; ugly stuff
                client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(AccountUtils.trimWebdavSuffix(targetUrlAfterPermanentRedirection)), mContext, true);
                client.setCredentials(credentials);
            }
            result = remoteUserNameOperation.execute(client);
        }
        // let the caller knows what is real URL that should be accessed for the account
        // being authenticated if the initial URL is being redirected permanently (HTTP code 301)
        result.setLastPermanentLocation(targetUrlAfterPermanentRedirection);
    } else {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
    }
    return result;
}
Also used : RedirectionPath(com.owncloud.android.lib.common.network.RedirectionPath) GetRemoteUserInfoOperation(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Uri(android.net.Uri) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 2 with GetRemoteUserInfoOperation

use of com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation 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 GetRemoteUserInfoOperation

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

the class OperationsService method newOperation.

/**
     * Creates a new operation, as described by operationIntent.
     * 
     * TODO - move to ServiceHandler (probably)
     * 
     * @param operationIntent       Intent describing a new operation to queue and execute.
     * @return                      Pair with the new operation object and the information about its
     *                              target server.
     */
private Pair<Target, RemoteOperation> newOperation(Intent operationIntent) {
    RemoteOperation operation = null;
    Target target = null;
    try {
        if (!operationIntent.hasExtra(EXTRA_ACCOUNT) && !operationIntent.hasExtra(EXTRA_SERVER_URL)) {
            Log_OC.e(TAG, "Not enough information provided in intent");
        } else {
            Account account = operationIntent.getParcelableExtra(EXTRA_ACCOUNT);
            String serverUrl = operationIntent.getStringExtra(EXTRA_SERVER_URL);
            String cookie = operationIntent.getStringExtra(EXTRA_COOKIE);
            target = new Target(account, (serverUrl == null) ? null : Uri.parse(serverUrl), cookie);
            String action = operationIntent.getAction();
            if (action.equals(ACTION_CREATE_SHARE_VIA_LINK)) {
                // Create public share via link
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                String password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
                if (remotePath.length() > 0) {
                    operation = new CreateShareViaLinkOperation(remotePath, password);
                }
            } else if (ACTION_UPDATE_SHARE.equals(action)) {
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                long shareId = operationIntent.getLongExtra(EXTRA_SHARE_ID, -1);
                if (remotePath != null && remotePath.length() > 0) {
                    operation = new UpdateShareViaLinkOperation(remotePath);
                    String password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
                    ((UpdateShareViaLinkOperation) operation).setPassword(password);
                    long expirationDate = operationIntent.getLongExtra(EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS, 0);
                    ((UpdateShareViaLinkOperation) operation).setExpirationDate(expirationDate);
                    if (operationIntent.hasExtra(EXTRA_SHARE_PUBLIC_UPLOAD)) {
                        ((UpdateShareViaLinkOperation) operation).setPublicUpload(operationIntent.getBooleanExtra(EXTRA_SHARE_PUBLIC_UPLOAD, false));
                    }
                } else if (shareId > 0) {
                    operation = new UpdateSharePermissionsOperation(shareId);
                    int permissions = operationIntent.getIntExtra(EXTRA_SHARE_PERMISSIONS, 1);
                    ((UpdateSharePermissionsOperation) operation).setPermissions(permissions);
                }
            } else if (action.equals(ACTION_CREATE_SHARE_WITH_SHAREE)) {
                // Create private share with user or group
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                String shareeName = operationIntent.getStringExtra(EXTRA_SHARE_WITH);
                ShareType shareType = (ShareType) operationIntent.getSerializableExtra(EXTRA_SHARE_TYPE);
                int permissions = operationIntent.getIntExtra(EXTRA_SHARE_PERMISSIONS, -1);
                if (remotePath.length() > 0) {
                    operation = new CreateShareWithShareeOperation(remotePath, shareeName, shareType, permissions);
                }
            } else if (action.equals(ACTION_UNSHARE)) {
                // Unshare file
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                ShareType shareType = (ShareType) operationIntent.getSerializableExtra(EXTRA_SHARE_TYPE);
                String shareWith = operationIntent.getStringExtra(EXTRA_SHARE_WITH);
                if (remotePath.length() > 0) {
                    operation = new UnshareOperation(remotePath, shareType, shareWith, OperationsService.this);
                }
            } else if (action.equals(ACTION_GET_SERVER_INFO)) {
                // check OC server and get basic information from it
                operation = new GetServerInfoOperation(serverUrl, OperationsService.this);
            } else if (action.equals(ACTION_OAUTH2_GET_ACCESS_TOKEN)) {
                /// GET ACCESS TOKEN to the OAuth server
                String oauth2QueryParameters = operationIntent.getStringExtra(EXTRA_OAUTH2_QUERY_PARAMETERS);
                operation = new OAuth2GetAccessToken(getString(R.string.oauth2_client_id), getString(R.string.oauth2_redirect_uri), getString(R.string.oauth2_grant_type), oauth2QueryParameters);
            } else if (action.equals(ACTION_GET_USER_NAME)) {
                // Get User Name
                operation = new GetRemoteUserInfoOperation();
            } else if (action.equals(ACTION_RENAME)) {
                // Rename file or folder
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                String newName = operationIntent.getStringExtra(EXTRA_NEWNAME);
                operation = new RenameFileOperation(remotePath, newName);
            } else if (action.equals(ACTION_REMOVE)) {
                // Remove file or folder
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                boolean onlyLocalCopy = operationIntent.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL, false);
                operation = new RemoveFileOperation(remotePath, onlyLocalCopy);
            } else if (action.equals(ACTION_CREATE_FOLDER)) {
                // Create Folder
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                boolean createFullPath = operationIntent.getBooleanExtra(EXTRA_CREATE_FULL_PATH, true);
                operation = new CreateFolderOperation(remotePath, createFullPath);
            } else if (action.equals(ACTION_SYNC_FILE)) {
                // Sync file
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                operation = new SynchronizeFileOperation(remotePath, account, getApplicationContext());
            } else if (action.equals(ACTION_SYNC_FOLDER)) {
                // Sync folder (all its descendant files are sync'ed)
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                boolean pushOnly = operationIntent.getBooleanExtra(EXTRA_PUSH_ONLY, false);
                boolean syncContentOfRegularFiles = operationIntent.getBooleanExtra(EXTRA_SYNC_REGULAR_FILES, false);
                operation = new SynchronizeFolderOperation(// TODO remove this dependency from construction time
                this, remotePath, account, // TODO remove this dependency from construction time
                System.currentTimeMillis(), pushOnly, false, syncContentOfRegularFiles);
            } else if (action.equals(ACTION_MOVE_FILE)) {
                // Move file/folder
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                String newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
                operation = new MoveFileOperation(remotePath, newParentPath, account);
            } else if (action.equals(ACTION_COPY_FILE)) {
                // Copy file/folder
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                String newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
                operation = new CopyFileOperation(remotePath, newParentPath, account);
            } else if (action.equals(ACTION_CHECK_CURRENT_CREDENTIALS)) {
                // Check validity of currently stored credentials for a given account
                operation = new CheckCurrentCredentialsOperation(account);
            }
        }
    } catch (IllegalArgumentException e) {
        Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
        operation = null;
    }
    if (operation != null) {
        return new Pair<Target, RemoteOperation>(target, operation);
    } else {
        return null;
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) UnshareOperation(com.owncloud.android.operations.UnshareOperation) RemoveFileOperation(com.owncloud.android.operations.RemoveFileOperation) RenameFileOperation(com.owncloud.android.operations.RenameFileOperation) CopyFileOperation(com.owncloud.android.operations.CopyFileOperation) SynchronizeFolderOperation(com.owncloud.android.operations.SynchronizeFolderOperation) UpdateShareViaLinkOperation(com.owncloud.android.operations.UpdateShareViaLinkOperation) Pair(android.util.Pair) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) GetRemoteUserInfoOperation(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation) CreateShareWithShareeOperation(com.owncloud.android.operations.CreateShareWithShareeOperation) CheckCurrentCredentialsOperation(com.owncloud.android.operations.CheckCurrentCredentialsOperation) CreateShareViaLinkOperation(com.owncloud.android.operations.CreateShareViaLinkOperation) CreateFolderOperation(com.owncloud.android.operations.CreateFolderOperation) OAuth2GetAccessToken(com.owncloud.android.operations.OAuth2GetAccessToken) MoveFileOperation(com.owncloud.android.operations.MoveFileOperation) GetServerInfoOperation(com.owncloud.android.operations.GetServerInfoOperation) UpdateSharePermissionsOperation(com.owncloud.android.operations.UpdateSharePermissionsOperation) ShareType(com.owncloud.android.lib.resources.shares.ShareType) SynchronizeFileOperation(com.owncloud.android.operations.SynchronizeFileOperation)

Aggregations

GetRemoteUserInfoOperation (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation)3 Account (android.accounts.Account)2 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)2 AccountManager (android.accounts.AccountManager)1 Uri (android.net.Uri)1 Pair (android.util.Pair)1 UserProfile (com.owncloud.android.datamodel.UserProfile)1 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)1 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)1 OwnCloudCredentials (com.owncloud.android.lib.common.OwnCloudCredentials)1 RedirectionPath (com.owncloud.android.lib.common.network.RedirectionPath)1 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)1 ExistenceCheckRemoteOperation (com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation)1 ShareType (com.owncloud.android.lib.resources.shares.ShareType)1 GetRemoteUserAvatarOperation (com.owncloud.android.lib.resources.users.GetRemoteUserAvatarOperation)1 UserInfo (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo)1 CheckCurrentCredentialsOperation (com.owncloud.android.operations.CheckCurrentCredentialsOperation)1 CopyFileOperation (com.owncloud.android.operations.CopyFileOperation)1 CreateFolderOperation (com.owncloud.android.operations.CreateFolderOperation)1 CreateShareViaLinkOperation (com.owncloud.android.operations.CreateShareViaLinkOperation)1