From c97d8d44d8257b3553ed59f88b53f5b02e5d82e6 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 16 Nov 2022 17:17:34 +0600 Subject: [PATCH 1/2] Refactor retrieve premiumGroup data from account code issue: https://gitlab.e.foundation/e/backlog/-/issues/5793 - refactor logic & move to separate utility class to reduce copy-paste same logic - add null check on groupData string before String::split call to avoid NPE. --- .../e/drive/activity/AccountsActivity.java | 13 ++--- .../e/drive/utils/AccountUtils.java | 52 +++++++++++++++++++ .../e/drive/widgets/EDriveWidget.java | 14 ++--- 3 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/foundation/e/drive/utils/AccountUtils.java diff --git a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java index feebfa19..47c45896 100644 --- a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java +++ b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java @@ -10,7 +10,6 @@ package foundation.e.drive.activity; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_ALIAS_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_EMAIL; -import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_GROUPS; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_NAME; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_TOTAL_QUOTA_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_USED_QUOTA_KEY; @@ -34,6 +33,7 @@ import com.owncloud.android.lib.common.OwnCloudClient; import foundation.e.drive.R; import foundation.e.drive.databinding.ActivityAccountsBinding; +import foundation.e.drive.utils.AccountUtils; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.DavClientProvider; import foundation.e.drive.widgets.EDriveWidget; @@ -112,14 +112,9 @@ public class AccountsActivity extends AppCompatActivity { binding.plan.setText(getString(R.string.free_plan, totalShownQuota)); - String[] groups = accountManager.getUserData(account, ACCOUNT_DATA_GROUPS).split(","); - for (String group : groups) { - if (group.contains("premium-")) { - binding.plan.setText(getString(R.string.premium_plan, - group.split("-")[1])); - break; - } - } + AccountUtils.getPremiumGroup(accountManager, account) + .ifPresent(group -> binding.plan.setText(getString(R.string.premium_plan, + group.split("-")[1]))); binding.myPlan.setVisibility(View.VISIBLE); binding.plan.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/foundation/e/drive/utils/AccountUtils.java b/app/src/main/java/foundation/e/drive/utils/AccountUtils.java new file mode 100644 index 00000000..1e86ed41 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/utils/AccountUtils.java @@ -0,0 +1,52 @@ +/* + * Copyright MURENA SAS 2022 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package foundation.e.drive.utils; + +import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_GROUPS; + +import android.accounts.Account; +import android.accounts.AccountManager; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.util.Arrays; +import java.util.Optional; + +public class AccountUtils { + + private AccountUtils() { + } + + public static Optional getPremiumGroup(@NonNull AccountManager accountManager, @Nullable Account account) { + if (account == null) { + return Optional.empty(); + } + + final String groupData = accountManager.getUserData(account, ACCOUNT_DATA_GROUPS); + + if (groupData == null || groupData.isEmpty()) { + return Optional.empty(); + } + + final String[] groups = groupData.split(","); + + return Arrays.stream(groups) + .filter(group -> group.contains("premium-")) + .findFirst(); + } +} diff --git a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java index c57b8c3b..94897801 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -10,7 +10,6 @@ package foundation.e.drive.widgets; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_ALIAS_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_EMAIL; -import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_GROUPS; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_NAME; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_TOTAL_QUOTA_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_USED_QUOTA_KEY; @@ -36,6 +35,7 @@ import java.util.Calendar; import java.util.Locale; import foundation.e.drive.R; +import foundation.e.drive.utils.AccountUtils; import foundation.e.drive.utils.CommonUtils; import timber.log.Timber; @@ -240,14 +240,10 @@ public class EDriveWidget extends AppWidgetProvider { views.setTextViewText(R.id.planName, context.getString(R.string.free_plan, totalShownQuota)); - final String[] groups = accountManager.getUserData(account, ACCOUNT_DATA_GROUPS).split(","); - for (String group : groups) { - if (group.contains("premium-")) { - views.setTextViewText(R.id.planName, context.getString(R.string.premium_plan, - group.split("-")[1])); - break; - } - } + AccountUtils.getPremiumGroup(accountManager, account) + .ifPresent(group -> views.setTextViewText(R.id.planName, + context.getString(R.string.premium_plan, + group.split("-")[1]))); views.setTextViewText(R.id.status, context.getString(R.string.progress_status, usedShownQuota, totalShownQuota)); -- GitLab From d60d5df6e9ea6d4024178362e7b7f1f99a1709a9 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Thu, 17 Nov 2022 04:08:24 +0000 Subject: [PATCH 2/2] Apply 1 suggestion(s) to 1 file(s) --- app/src/main/java/foundation/e/drive/utils/AccountUtils.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/utils/AccountUtils.java b/app/src/main/java/foundation/e/drive/utils/AccountUtils.java index 1e86ed41..b121c4f7 100644 --- a/app/src/main/java/foundation/e/drive/utils/AccountUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/AccountUtils.java @@ -29,8 +29,6 @@ import java.util.Optional; public class AccountUtils { - private AccountUtils() { - } public static Optional getPremiumGroup(@NonNull AccountManager accountManager, @Nullable Account account) { if (account == null) { -- GitLab