Search in sources :

Example 1 with NextcloudClient

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();
}
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 NextcloudClient

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

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();
}
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 NextcloudClient

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);
    }
}
Also used : NextcloudClient(com.nextcloud.common.NextcloudClient) GetPredefinedStatusesRemoteOperation(com.owncloud.android.lib.resources.users.GetPredefinedStatusesRemoteOperation) PredefinedStatus(com.owncloud.android.lib.resources.users.PredefinedStatus) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson)

Example 5 with NextcloudClient

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

Aggregations

NextcloudClient (com.nextcloud.common.NextcloudClient)8 UserInfo (com.owncloud.android.lib.common.UserInfo)5 GetUserInfoRemoteOperation (com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation)4 AuthenticatorException (android.accounts.AuthenticatorException)2 OperationCanceledException (android.accounts.OperationCanceledException)2 Context (android.content.Context)2 IOException (java.io.IOException)2 Account (android.accounts.Account)1 Uri (android.net.Uri)1 Gson (com.google.gson.Gson)1 GetHoverCardRemoteOperation (com.nextcloud.android.lib.resources.profile.GetHoverCardRemoteOperation)1 HoverCard (com.nextcloud.android.lib.resources.profile.HoverCard)1 User (com.nextcloud.client.account.User)1 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)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 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)1