Search in sources :

Example 1 with UserProfile

use of com.owncloud.android.datamodel.UserProfile 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)

Aggregations

Account (android.accounts.Account)1 AccountManager (android.accounts.AccountManager)1 UserProfile (com.owncloud.android.datamodel.UserProfile)1 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)1 GetRemoteUserAvatarOperation (com.owncloud.android.lib.resources.users.GetRemoteUserAvatarOperation)1 GetRemoteUserInfoOperation (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation)1 UserInfo (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo)1 ArrayList (java.util.ArrayList)1