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