diff --git a/app/src/main/java/foundation/e/drive/account/AccountUserInfoWorker.java b/app/src/main/java/foundation/e/drive/account/AccountUserInfoWorker.java index a295ee45ece27d593d00f30f46c901dbba9ab34a..3c09be867a5ff06db0685702beca44b3802eb63a 100644 --- a/app/src/main/java/foundation/e/drive/account/AccountUserInfoWorker.java +++ b/app/src/main/java/foundation/e/drive/account/AccountUserInfoWorker.java @@ -24,6 +24,7 @@ import android.content.Context; import android.content.SharedPreferences; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; import androidx.work.Worker; import androidx.work.WorkerParameters; @@ -31,7 +32,6 @@ import androidx.work.WorkerParameters; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.nextcloud.common.NextcloudClient; -import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.Quota; import com.owncloud.android.lib.common.UserInfo; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -52,57 +52,63 @@ import timber.log.Timber; * @author TheScarastic */ public class AccountUserInfoWorker extends Worker { - public static final String UNIQUE_WORK_NAME = "AccountUserInfoWorker"; - private final AccountManager accountManager; - private final GetUserInfoRemoteOperation GetUserInfoRemoteOperation = new GetUserInfoRemoteOperation(); - private static final int QUOTA_NOTIFICATION_ID = 11; + public static final String UNIQUE_WORK_NAME = "AccountUserInfoWorker"; - private final Context mContext; + private final AccountManager accountManager; + private final Context context; private Account account; public AccountUserInfoWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); - Timber.tag(AccountUserInfoWorker.class.getSimpleName()); - mContext = context; + this.context = context; accountManager = AccountManager.get(context); + account = CommonUtils.getAccount(context.getString(R.string.eelo_account_type), accountManager); } @NonNull @Override public Result doWork() { + final NextcloudClient client = getNextcloudClient(); + if (client == null) { + Timber.d("Cannot fetch user info & aliases becauses NC client is null"); + return Result.failure(); + } - try { - account = CommonUtils.getAccount(mContext.getString(R.string.eelo_account_type), accountManager); + Result result = Result.failure(); - if (account == null) { - return Result.failure(); + try { + if (fetchUserInfo(client) && fetchAliases(client)) { + Glide.with(context) + .load(client.getBaseUri() + AccountsActivity.NON_OFFICIAL_AVATAR_PATH + + client.getUserId() + "/" + 300) + .diskCacheStrategy(DiskCacheStrategy.ALL) + .preload(); + ViewUtils.updateWidgetView(context); + DavClientProvider.getInstance().saveAccounts(context); + result = Result.success(); + } else { + result = Result.retry(); } + } catch (Throwable exception) { + Timber.e(exception, "Exception on retrieving accountUserInfo."); + } - final NextcloudClient client = DavClientProvider.getInstance().getNcClientInstance(account, mContext); - if (client != null) { - if (fetchUserInfo(client) && fetchAliases()) { - Glide.with(mContext) - .load(client.getBaseUri() + AccountsActivity.NON_OFFICIAL_AVATAR_PATH - + client.getUserId() + "/" + 300) - .diskCacheStrategy(DiskCacheStrategy.ALL) - .preload(); - ViewUtils.updateWidgetView(mContext); - DavClientProvider.getInstance().saveAccounts(mContext); - return Result.success(); - } else { - return Result.retry(); - } - } - } catch (Throwable e) { - Timber.e(e, "Exception on retrieving accountUserInfo."); + return result; + } + + @Nullable + private NextcloudClient getNextcloudClient() { + if (account == null) { + return null; } - return Result.failure(); + return DavClientProvider.getInstance().getNcClientInstance(account, context); } private boolean fetchUserInfo(final NextcloudClient client) { - final RemoteOperationResult ocsResult = GetUserInfoRemoteOperation.execute(client); + final GetUserInfoRemoteOperation getUserInfoRemoteOperation = new GetUserInfoRemoteOperation(); + final RemoteOperationResult ocsResult = getUserInfoRemoteOperation.execute(client); if (ocsResult.isSuccess()) { final UserInfo userInfo = ocsResult.getResultData(); @@ -112,36 +118,19 @@ public class AccountUserInfoWorker extends Worker { if (userId != null) { client.setUserId(userId); - AccountManager.get(mContext).setUserData(account, ACCOUNT_USER_ID_KEY, userId); + AccountManager.get(context).setUserData(account, ACCOUNT_USER_ID_KEY, userId); Timber.v("UserId %s saved for account", userId); } } - final Quota userQuota = userInfo.getQuota(); - long totalQuota = 0L; - long usedQuota = 0L; - double relativeQuota = 0.0D; - - if (userQuota != null) { - totalQuota = userQuota.getTotal(); - if (totalQuota <= 0) { - totalQuota = 0; - } - - usedQuota = userQuota.getUsed(); - relativeQuota = userQuota.getRelative(); - } + updateQuotaInUserData(userInfo); final String groups = (userInfo.getGroups() != null) ? String.join(",", userInfo.getGroups()) : ""; accountManager.setUserData(account, ACCOUNT_DATA_NAME, userInfo.getDisplayName()); accountManager.setUserData(account, ACCOUNT_DATA_EMAIL, userInfo.getEmail()); accountManager.setUserData(account, ACCOUNT_DATA_GROUPS, groups); - accountManager.setUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY, "" + totalQuota); - accountManager.setUserData(account, ACCOUNT_DATA_RELATIVE_QUOTA_KEY, "" + relativeQuota); - accountManager.setUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY, "" + usedQuota); - addNotifAboutQuota(relativeQuota); Timber.d("fetchUserInfo(): success"); return true; } @@ -149,6 +138,29 @@ public class AccountUserInfoWorker extends Worker { return false; } + private void updateQuotaInUserData(UserInfo userInfo) { + final Quota userQuota = userInfo.getQuota(); + long totalQuota = 0L; + long usedQuota = 0L; + double relativeQuota = 0.0D; + + if (userQuota != null) { + totalQuota = userQuota.getTotal(); + if (totalQuota <= 0) { + totalQuota = 0; + } + + usedQuota = userQuota.getUsed(); + relativeQuota = userQuota.getRelative(); + } + + accountManager.setUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY, "" + totalQuota); + accountManager.setUserData(account, ACCOUNT_DATA_RELATIVE_QUOTA_KEY, "" + relativeQuota); + accountManager.setUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY, "" + usedQuota); + + addNotifAboutQuota(relativeQuota); + } + private void addNotifAboutQuota(final double relativeQuota) { final Context context = getApplicationContext(); boolean needToNotify = needToSendNotification(context); @@ -216,8 +228,7 @@ public class AccountUserInfoWorker extends Worker { manager.notify(QUOTA_NOTIFICATION_ID, builder.build()); } - private boolean fetchAliases() { - final OwnCloudClient ocClient = DavClientProvider.getInstance().getClientInstance(account, mContext); + private boolean fetchAliases(NextcloudClient client) { final String userId = accountManager.getUserData(account, ACCOUNT_USER_ID_KEY); if (userId == null || userId.isEmpty()) { @@ -225,7 +236,7 @@ public class AccountUserInfoWorker extends Worker { } final GetAliasOperation getAliasOperation = new GetAliasOperation(userId); - final RemoteOperationResult> ocsResult = getAliasOperation.execute(ocClient); + final RemoteOperationResult> ocsResult = getAliasOperation.execute(client); String aliases = ""; if (ocsResult.isSuccess()) { @@ -237,7 +248,7 @@ public class AccountUserInfoWorker extends Worker { accountManager.setUserData(account, ACCOUNT_DATA_ALIAS_KEY, aliases); Timber.d("fetchAliases(): success: %s", ocsResult.isSuccess()); - DavClientProvider.getInstance().saveAccounts(mContext); + DavClientProvider.getInstance().saveAccounts(context); return true; } diff --git a/app/src/main/java/foundation/e/drive/account/GetAliasOperation.java b/app/src/main/java/foundation/e/drive/account/GetAliasOperation.java index 5896f3edc3d73dbd33e2b9b9e30006952e134863..637763869fd44c74d3e4a95d0c99cd84ac46085a 100644 --- a/app/src/main/java/foundation/e/drive/account/GetAliasOperation.java +++ b/app/src/main/java/foundation/e/drive/account/GetAliasOperation.java @@ -11,26 +11,27 @@ package foundation.e.drive.account; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.GetMethod; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.NameValuePair; -import org.apache.commons.httpclient.methods.GetMethod; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import timber.log.Timber; /** * @author TheScarastic + * @author vincent Bourgmayer * /!\ Doesn't require NextcloudClient yet */ - public class GetAliasOperation extends RemoteOperation> { private static final String ALIAS_PATH = "/ocs/v1.php/cloud/hide-my-email/"; @@ -51,24 +52,28 @@ public class GetAliasOperation extends RemoteOperation> { @Override @NonNull - protected RemoteOperationResult> run(@NonNull OwnCloudClient client) { + public RemoteOperationResult> run(@NonNull NextcloudClient client) { + GetMethod getMethod = null; RemoteOperationResult> result; - GetMethod get = null; + final String uri = client.getBaseUri() + ALIAS_PATH + mID; try { - get = new GetMethod(uri); - get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - get.setQueryString(new NameValuePair[]{new NameValuePair("format", "json")}); + getMethod = new GetMethod(uri, true); + final Map queryString = new HashMap<>(); + queryString.put("format", "json"); - if (isSuccess(client.executeMethod(get))) { - final String response = get.getResponseBodyAsString(); - // parse + getMethod.setQueryString(queryString); + final int status = getMethod.execute(client); + if (isSuccess(status)) { + final String response = getMethod.getResponseBodyAsString(); final JSONArray aliases = parseResponse(response); final ArrayList resultAliases = new ArrayList<>(); - result = new RemoteOperationResult<>(true, get); + + result = new RemoteOperationResult<>(true, getMethod); + if (aliases != null) { for (int i = 0; i < aliases.length(); i++) { resultAliases.add(aliases.get(i).toString()); @@ -76,17 +81,17 @@ public class GetAliasOperation extends RemoteOperation> { } result.setResultData(resultAliases); } else { - result = new RemoteOperationResult<>(false, get); + result = new RemoteOperationResult<>(false, getMethod); } - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - Timber.d(e, "Fetching aliases failed"); + } catch (Exception exception){ + result = new RemoteOperationResult<>(exception); + Timber.d(exception, "Fetching aliases failed"); } finally { - if (get != null) - get.releaseConnection(); + if (getMethod != null) { + getMethod.releaseConnection(); + } } - return result; }