Search in sources :

Example 1 with GetUserInfoRemoteOperation

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();
}
Also used : Context(android.content.Context) User(com.nextcloud.client.account.User) NextcloudClient(com.nextcloud.common.NextcloudClient) GetUserInfoRemoteOperation(com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation) OperationCanceledException(android.accounts.OperationCanceledException) AuthenticatorException(android.accounts.AuthenticatorException) UserInfo(com.owncloud.android.lib.common.UserInfo) IOException(java.io.IOException) Quota(com.owncloud.android.lib.common.Quota)

Example 2 with GetUserInfoRemoteOperation

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;
}
Also used : Context(android.content.Context) RedirectionPath(com.owncloud.android.lib.common.network.RedirectionPath) NextcloudClient(com.nextcloud.common.NextcloudClient) GetUserInfoRemoteOperation(com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UserInfo(com.owncloud.android.lib.common.UserInfo) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Uri(android.net.Uri) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 3 with GetUserInfoRemoteOperation

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();
}
Also used : NextcloudClient(com.nextcloud.common.NextcloudClient) GetUserInfoRemoteOperation(com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation) AccountUtils(com.owncloud.android.lib.common.accounts.AccountUtils) UserInfo(com.owncloud.android.lib.common.UserInfo)

Example 4 with GetUserInfoRemoteOperation

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;
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) GetUserInfoRemoteOperation(com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation) NextcloudClient(com.nextcloud.common.NextcloudClient) UserInfo(com.owncloud.android.lib.common.UserInfo) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) AuthenticatorException(android.accounts.AuthenticatorException) OperationCanceledException(android.accounts.OperationCanceledException) IOException(java.io.IOException)

Example 5 with GetUserInfoRemoteOperation

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;
}
Also used : Account(android.accounts.Account) GetUserInfoRemoteOperation(com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation) UserInfo(com.owncloud.android.lib.common.UserInfo) AccountManager(android.accounts.AccountManager)

Aggregations

GetUserInfoRemoteOperation (com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation)6 UserInfo (com.owncloud.android.lib.common.UserInfo)5 NextcloudClient (com.nextcloud.common.NextcloudClient)4 Account (android.accounts.Account)3 AuthenticatorException (android.accounts.AuthenticatorException)2 OperationCanceledException (android.accounts.OperationCanceledException)2 Context (android.content.Context)2 User (com.nextcloud.client.account.User)2 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)2 IOException (java.io.IOException)2 AccountManager (android.accounts.AccountManager)1 Uri (android.net.Uri)1 Pair (android.util.Pair)1 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)1 OCFile (com.owncloud.android.datamodel.OCFile)1 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)1 OwnCloudCredentials (com.owncloud.android.lib.common.OwnCloudCredentials)1 Quota (com.owncloud.android.lib.common.Quota)1 AccountUtils (com.owncloud.android.lib.common.accounts.AccountUtils)1 RedirectionPath (com.owncloud.android.lib.common.network.RedirectionPath)1