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;
}
Aggregations