use of com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation in project android by nextcloud.
the class DrawerActivity method getAndDisplayUserQuota.
/**
* Retrieves and shows the user quota if available
*/
private void getAndDisplayUserQuota() {
// set user space information
Thread t = new Thread(() -> {
final User user = accountManager.getUser();
if (user.isAnonymous()) {
return;
}
final Context context = MainApp.getAppContext();
NextcloudClient nextcloudClient = null;
try {
nextcloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().getNextcloudClientFor(user.toOwnCloudAccount(), context);
} catch (OperationCanceledException | AuthenticatorException | IOException e) {
Log_OC.e(this, "Error retrieving user quota", e);
}
if (nextcloudClient == null) {
return;
}
RemoteOperationResult<UserInfo> result = new GetUserInfoRemoteOperation().execute(nextcloudClient);
if (result.isSuccess() && result.getResultData() != null) {
final UserInfo userInfo = result.getResultData();
final Quota quota = userInfo.getQuota();
if (quota != null) {
final long used = quota.getUsed();
final long total = quota.getTotal();
final int relative = (int) Math.ceil(quota.getRelative());
final long quotaValue = quota.getQuota();
runOnUiThread(() -> {
if (quotaValue > 0 || quotaValue == GetUserInfoRemoteOperation.SPACE_UNLIMITED || quotaValue == GetUserInfoRemoteOperation.QUOTA_LIMIT_INFO_NOT_AVAILABLE) {
/*
* show quota in case
* it is available and calculated (> 0) or
* in case of legacy servers (==QUOTA_LIMIT_INFO_NOT_AVAILABLE)
*/
setQuotaInformation(used, total, relative, quotaValue);
} else {
/*
* quotaValue < 0 means special cases like
* {@link RemoteGetUserQuotaOperation.SPACE_NOT_COMPUTED},
* {@link RemoteGetUserQuotaOperation.SPACE_UNKNOWN} or
* {@link RemoteGetUserQuotaOperation.SPACE_UNLIMITED}
* thus don't display any quota information.
*/
showQuota(false);
}
});
}
}
});
t.start();
}
use of com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation in project android by nextcloud.
the class AuthenticatorAsyncTask method doInBackground.
@Override
protected RemoteOperationResult<UserInfo> doInBackground(Object... params) {
RemoteOperationResult<UserInfo> result;
if (params != null && params.length == 2 && mWeakContext.get() != null) {
String url = (String) params[0];
Context context = mWeakContext.get();
OwnCloudCredentials credentials = (OwnCloudCredentials) params[1];
// Client
Uri uri = Uri.parse(url);
NextcloudClient nextcloudClient = OwnCloudClientFactory.createNextcloudClient(uri, credentials.getUsername(), credentials.toOkHttpCredentials(), context, true);
// Operation - get display name
RemoteOperationResult<UserInfo> userInfoResult = new GetUserInfoRemoteOperation().execute(nextcloudClient);
// Operation - try credentials
if (userInfoResult.isSuccess()) {
OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, context, true);
client.setUserId(userInfoResult.getResultData().getId());
client.setCredentials(credentials);
ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(ROOT_PATH, SUCCESS_IF_ABSENT);
result = operation.execute(client);
if (operation.wasRedirected()) {
RedirectionPath redirectionPath = operation.getRedirectionPath();
String permanentLocation = redirectionPath.getLastPermanentLocation();
result.setLastPermanentLocation(permanentLocation);
}
result.setResultData(userInfoResult.getResultData());
} else {
result = userInfoResult;
}
} else {
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
}
return result;
}
use of com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation in project android by nextcloud.
the class UserInfoActivity method fetchAndSetData.
private void fetchAndSetData() {
Thread t = new Thread(() -> {
NextcloudClient nextcloudClient;
try {
nextcloudClient = OwnCloudClientFactory.createNextcloudClient(user.toPlatformAccount(), this);
} catch (AccountUtils.AccountNotFoundException e) {
Log_OC.e(this, "Error retrieving user info", e);
return;
}
RemoteOperationResult<UserInfo> result = new GetUserInfoRemoteOperation().execute(nextcloudClient);
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
if (result.isSuccess() && result.getResultData() != null) {
userInfo = result.getResultData();
runOnUiThread(() -> populateUserInfoUi(userInfo));
} else {
// show error
runOnUiThread(() -> setErrorMessageForMultiList(getString(R.string.user_information_retrieval_error), result.getLogMessage(), R.drawable.ic_list_empty_error));
Log_OC.d(TAG, result.getLogMessage());
}
}
});
t.start();
}
use of com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation in project android by nextcloud.
the class UserAccountManagerImpl method migrateUserId.
public boolean migrateUserId() {
Account[] ocAccounts = accountManager.getAccountsByType(MainApp.getAccountType(context));
String userId;
String displayName;
GetUserInfoRemoteOperation remoteUserNameOperation = new GetUserInfoRemoteOperation();
int failed = 0;
for (Account account : ocAccounts) {
String storedUserId = accountManager.getUserData(account, com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
if (!TextUtils.isEmpty(storedUserId)) {
continue;
}
// add userId
try {
OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
NextcloudClient nextcloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().getNextcloudClientFor(ocAccount, context);
RemoteOperationResult<UserInfo> result = remoteUserNameOperation.execute(nextcloudClient);
if (result.isSuccess()) {
UserInfo userInfo = result.getResultData();
userId = userInfo.getId();
displayName = userInfo.getDisplayName();
} else {
// skip account, try it next time
Log_OC.e(TAG, "Error while getting username for account: " + account.name);
failed++;
continue;
}
} catch (Exception e) {
Log_OC.e(TAG, "Error while getting username: " + e.getMessage());
failed++;
continue;
}
accountManager.setUserData(account, com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_DISPLAY_NAME, displayName);
accountManager.setUserData(account, com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID, userId);
}
return failed == 0;
}
use of com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation in project android by nextcloud.
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
public RemoteOperationResult<UserInfo> run(NextcloudClient client) {
// get display name
RemoteOperationResult<UserInfo> result = new GetUserInfoRemoteOperation().execute(client);
if (result.isSuccess()) {
// store display name with account data
AccountManager accountManager = AccountManager.get(MainApp.getAppContext());
UserInfo userInfo = result.getResultData();
Account storedAccount = getStorageManager().getUser().toPlatformAccount();
accountManager.setUserData(storedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName());
}
return result;
}
Aggregations