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 feebfa19d14b05207645ec908beaca3305173c91..47c45896751b3940663df41ac37daccf609f5ae6 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 0000000000000000000000000000000000000000..b121c4f78b361c4fc60bec3e756e6634902645c5 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/utils/AccountUtils.java @@ -0,0 +1,50 @@ +/* + * 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 { + + + 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 c57b8c3b8d192c0cd44665176297d88d1b87aa0c..948978010067b78d6a70d3dfeab570137caf1105 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));