use of com.nextcloud.common.NextcloudClient 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.nextcloud.common.NextcloudClient 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.nextcloud.common.NextcloudClient 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.nextcloud.common.NextcloudClient in project android by nextcloud.
the class RefreshFolderOperation method updatePredefinedStatus.
private void updatePredefinedStatus(ArbitraryDataProvider arbitraryDataProvider) {
NextcloudClient client;
try {
client = OwnCloudClientFactory.createNextcloudClient(user.toPlatformAccount(), mContext);
} catch (AccountUtils.AccountNotFoundException | NullPointerException e) {
Log_OC.e(this, "Update of predefined status not possible!");
return;
}
RemoteOperationResult<ArrayList<PredefinedStatus>> result = new GetPredefinedStatusesRemoteOperation().execute(client);
if (result.isSuccess()) {
ArrayList<PredefinedStatus> predefinedStatuses = result.getResultData();
String json = new Gson().toJson(predefinedStatuses);
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(), ArbitraryDataProvider.PREDEFINED_STATUS, json);
} else {
arbitraryDataProvider.deleteKeyForAccount(user.getAccountName(), ArbitraryDataProvider.PREDEFINED_STATUS);
}
}
use of com.nextcloud.common.NextcloudClient 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;
}
Aggregations