From e5b15a3fa71bd6ff67b8adc7a38c4c059b84f225 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 22 Apr 2022 10:44:59 +0200 Subject: [PATCH 01/10] add base for workRequest to check quota regularly --- .../e/drive/work/QuotaManagerWorker.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java diff --git a/app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java b/app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java new file mode 100644 index 00000000..335898e7 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java @@ -0,0 +1,76 @@ +package foundation.e.drive.work; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.content.Context; + +import androidx.annotation.NonNull; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.UserInfo; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation; + +import foundation.e.drive.R; +import foundation.e.drive.operations.GetAliasOperation; +import foundation.e.drive.utils.CommonUtils; + +public class QuotaManagerWorker extends Worker { + + final Account account; + final AccountManager accountManager; + + public QuotaManagerWorker(@NonNull Context context, @NonNull WorkerParameters workerParams){ + super( context, workerParams); + accountManager = AccountManager.get(context); + account = CommonUtils.getAccount(context.getString(R.string.eelo_account_type), + accountManager); + } + + @NonNull + @Override + public Result doWork() { + final Context context = getApplicationContext(); + + if (account == null || !CommonUtils.haveNetworkConnection(context, true)) { + return Result.failure(); + } + + final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, context); + + if (client == null) { return Result.failure(); } + + if (checkQuota(client) && checkAliases(client)) { + return Result.success(); + } else { + return Result.failure(); + } + } + + private boolean checkQuota(OwnCloudClient client) { + final GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation(); + final RemoteOperationResult ocsResult = getRemoteUserInfoOperation.execute(client); + + if (ocsResult.isSuccess() && ocsResult.getData() != null && !ocsResult.getData().isEmpty()) { + final UserInfo userInfo = (UserInfo) ocsResult.getData().get(0); + accountManager.setUserData(account, "currentQuota", userInfo.getQuota().relative+""); + return true; + } + return false; + } + + + private boolean checkAliases(OwnCloudClient client ) { + final GetAliasOperation getAliasOperation = new GetAliasOperation(); + final RemoteOperationResult ocsResult = getAliasOperation.execute(client); + + if (ocsResult.isSuccess() && ocsResult.getData() != null && !ocsResult.getData().isEmpty()) { + final String alias = (String) ocsResult.getData().get(0); + accountManager.setUserData(account, "aliases", alias); + return true; + } + return false; + } +} -- GitLab From f05ade73c43452171bbeb9e732a71a3d3c679e55 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 22 Apr 2022 11:17:08 +0200 Subject: [PATCH 02/10] Update QuotaManagerWorker.java to send notification based on quota Also store total quota & usedQuota in Account. Remove method about notification from SynchronizationService --- .../services/SynchronizationService.java | 29 +---------- .../e/drive/work/QuotaManagerWorker.java | 49 +++++++++++++++++-- 2 files changed, 47 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java index b05476ac..7d8a34dd 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -158,13 +158,6 @@ public class SynchronizationService extends Service implements OnRemoteOperation if (callerOperation instanceof UploadFileOperation && callerOperationResultData != null && callerOperationResultData.size() > 1) { final Float relativeQuota = (Float) result.getData().get(1); - if (relativeQuota >= 99.0) { - addNotification(getString(R.string.notif_quota_99Plus_title), getString(R.string.notif_quota_99Plus_text)); - } else if (relativeQuota >= 90.0) { - addNotification(getString(R.string.notif_quota_80plus_title), getString(R.string.notif_quota_90Plus_text)); - } else if (relativeQuota >= 80.0) { - addNotification(getString(R.string.notif_quota_80plus_title), getString(R.string.notif_quota_80Plus_text)); - } } switch (result.getCode()) { case OK: @@ -204,7 +197,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation case QUOTA_EXCEEDED: //Case specific to UploadFileOperation Log.w(TAG, "Quota_EXCEEDED"); - addNotification(getString(R.string.notif_quota_99Plus_title), getString(R.string.notif_quota_execeeded_text)); + //addNotification(getString(R.string.notif_quota_99Plus_title), getString(R.string.notif_quota_execeeded_text)); break; case FILE_NOT_FOUND: //Case specific to DownloadFileOperation @@ -237,26 +230,6 @@ public class SynchronizationService extends Service implements OnRemoteOperation return operation; } - /** - * send notification to inform user that he lacks space on ecloud - * Improvement idea: - * - add translatable message & title - * - make message & title to be a parameter of the method, so we could reuse the function somewhere - * - else with different notification. File conflict for example. - **/ - private void addNotification(String title, String text){ - final NotificationCompat.Builder builder = - new NotificationCompat.Builder(this, AppConstants.notificationChannelID) - .setSmallIcon(android.R.drawable.stat_sys_warning) - .setContentTitle(title) - .setContentText(text) - .setStyle(new NotificationCompat.BigTextStyle().bigText(text)); - // Add as notification - final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - manager.notify(0, builder.build()); - } - - /** * Handler for the class diff --git a/app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java b/app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java index 335898e7..8a4684a6 100644 --- a/app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java +++ b/app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java @@ -2,9 +2,11 @@ package foundation.e.drive.work; import android.accounts.Account; import android.accounts.AccountManager; +import android.app.NotificationManager; import android.content.Context; import androidx.annotation.NonNull; +import androidx.core.app.NotificationCompat; import androidx.work.Worker; import androidx.work.WorkerParameters; @@ -15,6 +17,7 @@ import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation; import foundation.e.drive.R; import foundation.e.drive.operations.GetAliasOperation; +import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; public class QuotaManagerWorker extends Worker { @@ -43,26 +46,66 @@ public class QuotaManagerWorker extends Worker { if (client == null) { return Result.failure(); } if (checkQuota(client) && checkAliases(client)) { + // @todo Call wiget update if widget exist + return Result.success(); } else { return Result.failure(); } } - private boolean checkQuota(OwnCloudClient client) { + private boolean checkQuota(final OwnCloudClient client) { final GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation(); final RemoteOperationResult ocsResult = getRemoteUserInfoOperation.execute(client); if (ocsResult.isSuccess() && ocsResult.getData() != null && !ocsResult.getData().isEmpty()) { final UserInfo userInfo = (UserInfo) ocsResult.getData().get(0); - accountManager.setUserData(account, "currentQuota", userInfo.getQuota().relative+""); + final double relativeQuota = userInfo.getQuota().relative; + final long totalQuota = userInfo.getQuota().total; + final long usedQuota = userInfo.getQuota().used; + accountManager.setUserData(account, "totalQuota", ""+totalQuota); + accountManager.setUserData(account, "relativeQuota", ""+relativeQuota); + accountManager.setUserData(account, "usedQuota", ""+usedQuota); + + if (relativeQuota >= 80.0) { + addNotifAboutQuota(relativeQuota); + } return true; } return false; } + private void addNotifAboutQuota(final double relativeQuota) { + final Context context = getApplicationContext(); + if (relativeQuota >= 99.0) { + addNotification(context.getString(R.string.notif_quota_99Plus_title), context.getString(R.string.notif_quota_99Plus_text)); + } else if (relativeQuota >= 90.0) { + addNotification(context.getString(R.string.notif_quota_80plus_title), context.getString(R.string.notif_quota_90Plus_text)); + } else { + addNotification(context.getString(R.string.notif_quota_80plus_title), context.getString(R.string.notif_quota_80Plus_text)); + } + } + + /** + * send notification to inform user that he lacks space on ecloud + * Improvement idea: + * - add translatable message & title + * - make message & title to be a parameter of the method, so we could reuse the function somewhere + * - else with different notification. File conflict for example. + **/ + private void addNotification(String title, String text){ + final NotificationCompat.Builder builder = + new NotificationCompat.Builder(getApplicationContext(), AppConstants.notificationChannelID) + .setSmallIcon(android.R.drawable.stat_sys_warning) + .setContentTitle(title) + .setContentText(text) + .setStyle(new NotificationCompat.BigTextStyle().bigText(text)); + // Add as notification + final NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); + manager.notify(0, builder.build()); + } - private boolean checkAliases(OwnCloudClient client ) { + private boolean checkAliases(final OwnCloudClient client ) { final GetAliasOperation getAliasOperation = new GetAliasOperation(); final RemoteOperationResult ocsResult = getAliasOperation.execute(client); -- GitLab From 29dbe5af6b18d8a4e1ab17a1de7e01a7fed70f86 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 22 Apr 2022 11:48:35 +0200 Subject: [PATCH 03/10] Rename QuotaManagerWorker into AccountUserInfoWorker - Defined account's data key in AppConstants - Added license & author header - Added method to send intent for widget update --- .../e/drive/utils/AppConstants.java | 5 +++ ...Worker.java => AccountUserInfoWorker.java} | 43 +++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) rename app/src/main/java/foundation/e/drive/work/{QuotaManagerWorker.java => AccountUserInfoWorker.java} (72%) diff --git a/app/src/main/java/foundation/e/drive/utils/AppConstants.java b/app/src/main/java/foundation/e/drive/utils/AppConstants.java index d6642947..03b44720 100644 --- a/app/src/main/java/foundation/e/drive/utils/AppConstants.java +++ b/app/src/main/java/foundation/e/drive/utils/AppConstants.java @@ -31,6 +31,11 @@ public abstract class AppConstants { public static final String SHARED_PREFERENCE_NAME ="preferences"; public static final String KEY_LAST_SYNC_TIME = "lastSyncTimestamp"; + public static final String ACCOUNT_DATA_USED_QUOTA_KEY = "used_quota"; + public static final String ACCOUNT_DATA_TOTAL_QUOTA_KEY = "total_quota"; + public static final String ACCOUNT_DATA_RELATIVE_QUOTA_KEY = "relative_quota"; + public static final String ACCOUNT_DATA_ALIAS_KEY = "alias"; + public static final String[] MEDIA_SYNCABLE_CATEGORIES = new String[]{"Images", "Movies", "Music", "Ringtones", "Documents", "Podcasts"}; public static final String[] SETTINGS_SYNCABLE_CATEGORIES = new String[] {"Rom settings"}; diff --git a/app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java similarity index 72% rename from app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java rename to app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java index 8a4684a6..3956fc57 100644 --- a/app/src/main/java/foundation/e/drive/work/QuotaManagerWorker.java +++ b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java @@ -1,9 +1,23 @@ +/* + * Copyright © ECORP SAS 2022. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ package foundation.e.drive.work; +import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_ALIAS_KEY; +import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_RELATIVE_QUOTA_KEY; +import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_TOTAL_QUOTA_KEY; +import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_USED_QUOTA_KEY; + import android.accounts.Account; import android.accounts.AccountManager; import android.app.NotificationManager; +import android.appwidget.AppWidgetManager; import android.content.Context; +import android.content.Intent; import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; @@ -19,13 +33,18 @@ import foundation.e.drive.R; import foundation.e.drive.operations.GetAliasOperation; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; +import foundation.e.drive.widgets.EDriveWidget; -public class QuotaManagerWorker extends Worker { +/** + * @author vincent Bourgmayer + * @author TheScarastic + */ +public class AccountUserInfoWorker extends Worker { final Account account; final AccountManager accountManager; - public QuotaManagerWorker(@NonNull Context context, @NonNull WorkerParameters workerParams){ + public AccountUserInfoWorker(@NonNull Context context, @NonNull WorkerParameters workerParams){ super( context, workerParams); accountManager = AccountManager.get(context); account = CommonUtils.getAccount(context.getString(R.string.eelo_account_type), @@ -43,11 +62,10 @@ public class QuotaManagerWorker extends Worker { final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, context); - if (client == null) { return Result.failure(); } + if (client == null) return Result.failure(); if (checkQuota(client) && checkAliases(client)) { - // @todo Call wiget update if widget exist - + updateWidget(context); return Result.success(); } else { return Result.failure(); @@ -63,9 +81,9 @@ public class QuotaManagerWorker extends Worker { final double relativeQuota = userInfo.getQuota().relative; final long totalQuota = userInfo.getQuota().total; final long usedQuota = userInfo.getQuota().used; - accountManager.setUserData(account, "totalQuota", ""+totalQuota); - accountManager.setUserData(account, "relativeQuota", ""+relativeQuota); - accountManager.setUserData(account, "usedQuota", ""+usedQuota); + 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); if (relativeQuota >= 80.0) { addNotifAboutQuota(relativeQuota); @@ -111,9 +129,16 @@ public class QuotaManagerWorker extends Worker { if (ocsResult.isSuccess() && ocsResult.getData() != null && !ocsResult.getData().isEmpty()) { final String alias = (String) ocsResult.getData().get(0); - accountManager.setUserData(account, "aliases", alias); + accountManager.setUserData(account, ACCOUNT_DATA_ALIAS_KEY, alias); return true; } return false; } + + private void updateWidget(final Context context){ + final Intent updateIntent=new Intent(context, EDriveWidget.class); + updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); + updateIntent.putExtra("account", account); + context.sendBroadcast(updateIntent); + } } -- GitLab From 46f9513ebc64de97e8ea4d1d04096d5485f2703a Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 22 Apr 2022 12:04:02 +0200 Subject: [PATCH 04/10] try to update widget from AccountUserInfoWorker --- .../java/foundation/e/drive/widgets/EDriveWidget.java | 9 +++++++++ .../foundation/e/drive/work/AccountUserInfoWorker.java | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) 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 9c0480f4..4e0cc251 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -22,6 +22,7 @@ import android.net.Uri; import android.os.Handler; import android.os.HandlerThread; import android.provider.Settings; +import android.util.Log; import android.view.View; import android.widget.RemoteViews; @@ -44,6 +45,7 @@ import foundation.e.drive.utils.CommonUtils; * Implementation of App Widget functionality. */ public class EDriveWidget extends AppWidgetProvider { + public static final String INTENT_EXTRA_ACCOUNT_KEY = "account"; public static final String WEBPAGE = "https://esolutions.shop/ecloud-subscriptions/?username=%s&token=placeholder¤t-quota=%s&from=wp"; public static final String ACCOUNT_MANAGER_PACKAGE_NAME = "foundation.e.accountmanager"; @@ -52,6 +54,7 @@ public class EDriveWidget extends AppWidgetProvider { ".ui.setup.LoginActivity"; private static final String SETUP_ACCOUNT_PROVIDER_TYPE = "setup_account_provider_type"; private static final String ACCOUNT_PROVIDER_EELO = "eelo"; + private static final String TAG = "EDriveWidget"; private static final String SHOW_ALIAS = "show_alias"; private static final String HIDE_ALIAS = "hide_alias"; @@ -170,6 +173,12 @@ public class EDriveWidget extends AppWidgetProvider { final String action = intent.getAction(); if (action == null) return; switch (action) { + case AppWidgetManager.ACTION_APPWIDGET_UPDATE: + Log.d(TAG, "Intent received for Update widget"); + if (intent.getExtras() != null) { + final Account account = (Account) intent.getExtras().get(INTENT_EXTRA_ACCOUNT_KEY); + } + break; case Intent.ACTION_BOOT_COMPLETED: registerConnectivityCallback(context); case AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION: diff --git a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java index 3956fc57..2797a0b5 100644 --- a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java +++ b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java @@ -11,6 +11,7 @@ import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_ALIAS_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_RELATIVE_QUOTA_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_TOTAL_QUOTA_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_USED_QUOTA_KEY; +import static foundation.e.drive.widgets.EDriveWidget.INTENT_EXTRA_ACCOUNT_KEY; import android.accounts.Account; import android.accounts.AccountManager; @@ -138,7 +139,7 @@ public class AccountUserInfoWorker extends Worker { private void updateWidget(final Context context){ final Intent updateIntent=new Intent(context, EDriveWidget.class); updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); - updateIntent.putExtra("account", account); + updateIntent.putExtra(INTENT_EXTRA_ACCOUNT_KEY, account); context.sendBroadcast(updateIntent); } } -- GitLab From 93997c208990ca7a8496b7ef6fee56c22b6f69d2 Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Fri, 22 Apr 2022 18:01:39 +0530 Subject: [PATCH 05/10] eDrive: Adapt widget for workmanager --- app/build.gradle | 4 + .../e/drive/activity/AccountsActivity.java | 4 +- .../e/drive/utils/AppConstants.java | 21 +-- .../e/drive/widgets/EDriveWidget.java | 134 ++++++------------ .../e/drive/work/AccountUserInfoWorker.java | 95 +++++++------ 5 files changed, 115 insertions(+), 143 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7144efd7..6f4aaa57 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -56,6 +56,10 @@ android { buildFeatures { viewBinding true } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } } 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 90f47e07..ff01990d 100644 --- a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java +++ b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java @@ -134,8 +134,8 @@ public class AccountsActivity extends AppCompatActivity { } binding.email.setText(userInfo.id); - final int usedMB = convertIntoMB(userInfo.quota.used); - final int totalMB = convertIntoMB(userInfo.quota.total); + final int usedMB = convertIntoMB(String.valueOf(userInfo.quota.used)); + final int totalMB = convertIntoMB(String.valueOf(userInfo.quota.total)); binding.progress.setMax(totalMB); binding.progress.setProgress(usedMB); diff --git a/app/src/main/java/foundation/e/drive/utils/AppConstants.java b/app/src/main/java/foundation/e/drive/utils/AppConstants.java index 03b44720..8f036bab 100644 --- a/app/src/main/java/foundation/e/drive/utils/AppConstants.java +++ b/app/src/main/java/foundation/e/drive/utils/AppConstants.java @@ -9,7 +9,6 @@ package foundation.e.drive.utils; import android.os.Build; -import foundation.e.drive.BuildConfig; import java.text.SimpleDateFormat; import java.util.Locale; @@ -24,31 +23,35 @@ public abstract class AppConstants { public static final String MEDIASYNC_PROVIDER_AUTHORITY = "foundation.e.drive.providers.MediasSyncProvider"; public static final String SETTINGSYNC_PROVIDER_AUTHORITY = "foundation.e.drive.providers.SettingsSyncProvider"; public static final String METERED_NETWORK_ALLOWED_AUTHORITY = "foundation.e.drive.providers.MeteredConnectionAllowedProvider"; - public static final String INITIALIZATION_HAS_BEEN_DONE ="initService_has_run"; + public static final String INITIALIZATION_HAS_BEEN_DONE = "initService_has_run"; public static final String INITIALFOLDERS_NUMBER = "initial_folder_number"; public static final String APPLICATIONS_LIST_FILE_NAME = "packages_list.csv"; public static final String APPLICATIONS_LIST_FILE_NAME_TMP = "tmp_packages_list.csv"; - public static final String SHARED_PREFERENCE_NAME ="preferences"; + public static final String SHARED_PREFERENCE_NAME = "preferences"; public static final String KEY_LAST_SYNC_TIME = "lastSyncTimestamp"; + public static final String ACCOUNT_DATA_NAME = "display_name"; public static final String ACCOUNT_DATA_USED_QUOTA_KEY = "used_quota"; public static final String ACCOUNT_DATA_TOTAL_QUOTA_KEY = "total_quota"; public static final String ACCOUNT_DATA_RELATIVE_QUOTA_KEY = "relative_quota"; + public static final String ACCOUNT_DATA_GROUPS = "group"; public static final String ACCOUNT_DATA_ALIAS_KEY = "alias"; + public static final String ACCOUNT_DATA_EMAIL = "email"; public static final String[] MEDIA_SYNCABLE_CATEGORIES = new String[]{"Images", "Movies", "Music", "Ringtones", "Documents", "Podcasts"}; - public static final String[] SETTINGS_SYNCABLE_CATEGORIES = new String[] {"Rom settings"}; + public static final String[] SETTINGS_SYNCABLE_CATEGORIES = new String[]{"Rom settings"}; - public static final String notificationChannelID ="foundation.e.drive"; - public static final String WORK_GENERIC_TAG="eDrive"; - public static final String WORK_INITIALIZATION_TAG="eDrive-init"; - public static final String USER_AGENT = "eos("+getBuildTime()+")-eDrive("+ BuildConfig.VERSION_NAME +")"; + public static final String notificationChannelID = "foundation.e.drive"; + public static final String WORK_GENERIC_TAG = "eDrive"; + public static final String WORK_INITIALIZATION_TAG = "eDrive-init"; + public static final String USER_AGENT = "eos(" + getBuildTime() + ")-eDrive(" + BuildConfig.VERSION_NAME + ")"; /** * Get a readable OS's build date String + * * @return Os'build date */ - private static String getBuildTime(){ + private static String getBuildTime() { long ts = Build.TIME; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.getDefault()); return sdf.format(ts); 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 4e0cc251..03219129 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -8,6 +8,13 @@ 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; + import android.accounts.Account; import android.accounts.AccountManager; import android.app.PendingIntent; @@ -19,25 +26,15 @@ import android.content.Intent; import android.net.ConnectivityManager; import android.net.Network; import android.net.Uri; -import android.os.Handler; -import android.os.HandlerThread; import android.provider.Settings; -import android.util.Log; import android.view.View; import android.widget.RemoteViews; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.UserInfo; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation; - import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Calendar; import java.util.Locale; import foundation.e.drive.R; -import foundation.e.drive.operations.GetAliasOperation; import foundation.e.drive.utils.CommonUtils; /** @@ -49,30 +46,23 @@ public class EDriveWidget extends AppWidgetProvider { public static final String WEBPAGE = "https://esolutions.shop/ecloud-subscriptions/?username=%s&token=placeholder¤t-quota=%s&from=wp"; public static final String ACCOUNT_MANAGER_PACKAGE_NAME = "foundation.e.accountmanager"; + private static final String TAG = EDriveWidget.class.getSimpleName(); private static final String ADD_ACCOUNT_WEBPAGE = "https://e.foundation/e-email-invite/"; private static final String GET_ACCOUNT_MANAGER_COMPONENT_NAME = ACCOUNT_MANAGER_PACKAGE_NAME + ".ui.setup.LoginActivity"; private static final String SETUP_ACCOUNT_PROVIDER_TYPE = "setup_account_provider_type"; private static final String ACCOUNT_PROVIDER_EELO = "eelo"; - private static final String TAG = "EDriveWidget"; private static final String SHOW_ALIAS = "show_alias"; private static final String HIDE_ALIAS = "hide_alias"; private static final String COPY_ALIAS = "copy_alias"; - private static boolean showAlias = false; private static boolean isNetworkAvailable = false; - - private static UserInfo userInfo = null; - private static ArrayList aliases = new ArrayList<>(); - private final Calendar calender = Calendar.getInstance(); private final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault()); - private final GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation(); - private final GetAliasOperation getAliasOperation = new GetAliasOperation(); - private OwnCloudClient client = null; private RemoteViews views; + private Account account = null; public static String dataForWeb(Long bytes) { @@ -91,8 +81,8 @@ public class EDriveWidget extends AppWidgetProvider { return intent; } - public static int convertIntoMB(Long quota) { - return (int) (quota / 1048576); // 1024.0 * 1024.0 = 1048576.0 + public static int convertIntoMB(String quota) { + return (int) (Long.parseLong(quota) / 1048576); // 1024.0 * 1024.0 = 1048576.0 } public void updateAppWidget(final Context context) { @@ -108,64 +98,25 @@ public class EDriveWidget extends AppWidgetProvider { public void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) { final AccountManager accountManager = AccountManager.get(context); - final Account account = CommonUtils.getAccount(context.getString(R.string.eelo_account_type), - accountManager); - if (account == null || isNetworkAvailable) { - noAccountView(context); - appWidgetManager.updateAppWidget(appWidgetId, views); - return; + // Make Last check to see if account is available + if (account == null) { + account = CommonUtils.getAccount(context.getString(R.string.eelo_account_type), + accountManager); } - client = CommonUtils.getOwnCloudClient(account, context); - final HandlerThread handlerThread = new HandlerThread("Network Request"); - handlerThread.start(); - final Handler mHandler = new Handler(handlerThread.getLooper()); - - final EDriveNetworkCallback callback = new EDriveNetworkCallback() { - @Override - public void onComplete() { - onNetworkRequestCompleted(context); - appWidgetManager.updateAppWidget(appWidgetId, views); - } - }; - - mHandler.post(new Runnable() { - @Override - public void run() { - if (client != null) { - RemoteOperationResult ocsResult = getRemoteUserInfoOperation.execute(client); - if (ocsResult.isSuccess() && ocsResult.getData() != null) { - userInfo = (UserInfo) ocsResult.getData().get(0); - } - RemoteOperationResult aliasResult = getAliasOperation.execute(client); - if (aliasResult.isSuccess() && aliasResult.getData() != null) { - aliases = aliasResult.getData(); - } - } - callback.onComplete(); - } - }); - } - - @Override - public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { - // There may be multiple widgets active, so update all of them - for (int appWidgetId : appWidgetIds) { - updateAppWidget(context, appWidgetManager, appWidgetId); + if (account == null && isNetworkAvailable) { + noAccountView(context); + appWidgetManager.updateAppWidget(appWidgetId, views); + } else { + onAccountAvailable(context, accountManager); } - super.onUpdate(context, appWidgetManager, appWidgetIds); } @Override public void onEnabled(Context context) { - updateAppWidget(context); super.onEnabled(context); - } - - @Override - public void onDisabled(Context context) { - // Enter relevant functionality for when the last widget is disabled + updateAppWidget(context); } @Override @@ -174,9 +125,9 @@ public class EDriveWidget extends AppWidgetProvider { if (action == null) return; switch (action) { case AppWidgetManager.ACTION_APPWIDGET_UPDATE: - Log.d(TAG, "Intent received for Update widget"); if (intent.getExtras() != null) { - final Account account = (Account) intent.getExtras().get(INTENT_EXTRA_ACCOUNT_KEY); + account = (Account) intent.getExtras().get(INTENT_EXTRA_ACCOUNT_KEY); + updateAppWidget(context); } break; case Intent.ACTION_BOOT_COMPLETED: @@ -216,8 +167,8 @@ public class EDriveWidget extends AppWidgetProvider { views.setTextViewText(R.id.summary, context.getString(R.string.login_summary)); } - private void onNetworkRequestCompleted(Context context) { - if (!isNetworkAvailable && userInfo == null) { + private void onAccountAvailable(Context context, AccountManager accountManager) { + if (!isNetworkAvailable) { views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget_login); views.setViewVisibility(R.id.button_container, View.GONE); views.setTextViewText(R.id.summary, context.getString(R.string.no_internet_widget)); @@ -226,47 +177,46 @@ public class EDriveWidget extends AppWidgetProvider { views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget); - // Construct the RemoteViews object - final int usedMB = convertIntoMB(userInfo.quota.used); - final int totalMB = convertIntoMB(userInfo.quota.total); + final String usedMB = accountManager.getUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY); + final String totalMB = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); + final String email = accountManager.getUserData(account, ACCOUNT_DATA_EMAIL); - views.setTextViewText(R.id.email, userInfo.id); - if (!userInfo.alternateDisplayName.isEmpty()) { - views.setTextViewText(R.id.name, userInfo.alternateDisplayName); - } else { - views.setTextViewText(R.id.name, userInfo.displayName); - } + views.setTextViewText(R.id.email, email); + views.setTextViewText(R.id.name, accountManager.getUserData(account, ACCOUNT_DATA_NAME)); - views.setProgressBar(R.id.progress, totalMB, usedMB, false); + views.setProgressBar(R.id.progress, convertIntoMB(totalMB), convertIntoMB(usedMB), false); views.setTextViewText(R.id.planName, context.getString(R.string.free_plan, - CommonUtils.humanReadableByteCountBin(userInfo.quota.total))); + CommonUtils.humanReadableByteCountBin(Long.parseLong(totalMB)))); - for (String group : userInfo.groups) { + 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; } } + views.setTextViewText(R.id.status, context.getString(R.string.progress_status, - CommonUtils.humanReadableByteCountBin(userInfo.quota.used), - CommonUtils.humanReadableByteCountBin(userInfo.quota.total))); + CommonUtils.humanReadableByteCountBin(Long.parseLong(usedMB)), + CommonUtils.humanReadableByteCountBin(Long.parseLong(totalMB)))); views.setTextViewText(R.id.sync, context.getString(R.string.last_synced, sdf.format(calender.getTime()))); + String aliases = accountManager.getUserData(account, ACCOUNT_DATA_ALIAS_KEY); if (aliases.isEmpty()) { views.setViewVisibility(R.id.show_alias, View.GONE); views.setViewVisibility(R.id.alias1_container, View.GONE); views.setViewVisibility(R.id.hide_alias, View.GONE); } else { - views.setTextViewText(R.id.alias1, context.getString(R.string.alias_dot) + aliases.get(0)); + views.setTextViewText(R.id.alias1, context.getString(R.string.alias_dot) + aliases.split(",")[0]); views.setOnClickPendingIntent(R.id.show_alias, getPendingSelfIntent(context, SHOW_ALIAS, null)); views.setOnClickPendingIntent(R.id.hide_alias, getPendingSelfIntent(context, HIDE_ALIAS, null)); views.setOnClickPendingIntent(R.id.alias1_clipboard, getPendingSelfIntent(context, COPY_ALIAS, - String.valueOf(aliases.get(0)))); + String.valueOf(aliases.split(",")[0]))); if (showAlias) { views.setViewVisibility(R.id.show_alias, View.GONE); @@ -284,8 +234,8 @@ public class EDriveWidget extends AppWidgetProvider { views.setOnClickPendingIntent(R.id.settings, pendingIntentSettings); final PendingIntent pendingIntentUpgrade = PendingIntent.getActivity(context, 0, - buildIntent(Intent.ACTION_VIEW, String.format(WEBPAGE, userInfo.id, - dataForWeb(userInfo.quota.total))), + buildIntent(Intent.ACTION_VIEW, String.format(WEBPAGE, email, + dataForWeb(Long.parseLong(totalMB)))), PendingIntent.FLAG_IMMUTABLE); views.setOnClickPendingIntent(R.id.upgrade, pendingIntentUpgrade); } diff --git a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java index 2797a0b5..1bf131d5 100644 --- a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java +++ b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java @@ -8,6 +8,9 @@ package foundation.e.drive.work; 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_RELATIVE_QUOTA_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_TOTAL_QUOTA_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_USED_QUOTA_KEY; @@ -30,6 +33,8 @@ import com.owncloud.android.lib.common.UserInfo; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation; +import java.util.ArrayList; + import foundation.e.drive.R; import foundation.e.drive.operations.GetAliasOperation; import foundation.e.drive.utils.AppConstants; @@ -42,11 +47,16 @@ import foundation.e.drive.widgets.EDriveWidget; */ public class AccountUserInfoWorker extends Worker { - final Account account; final AccountManager accountManager; + final GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation(); + final GetAliasOperation getAliasOperation = new GetAliasOperation(); + private final Account account; + private final Context mContext; + - public AccountUserInfoWorker(@NonNull Context context, @NonNull WorkerParameters workerParams){ - super( context, workerParams); + public AccountUserInfoWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + mContext = context; accountManager = AccountManager.get(context); account = CommonUtils.getAccount(context.getString(R.string.eelo_account_type), accountManager); @@ -55,43 +65,47 @@ public class AccountUserInfoWorker extends Worker { @NonNull @Override public Result doWork() { - final Context context = getApplicationContext(); - - if (account == null || !CommonUtils.haveNetworkConnection(context, true)) { - return Result.failure(); - } - - final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, context); - - if (client == null) return Result.failure(); - - if (checkQuota(client) && checkAliases(client)) { - updateWidget(context); - return Result.success(); - } else { - return Result.failure(); + if (account != null && CommonUtils.haveNetworkConnection(mContext, true)) { + final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, mContext); + + if (client != null) { + fetchUserInfo(client); + fetchAliases(client); + updateWidget(mContext); + return Result.success(); + } } + updateWidget(mContext); + return Result.failure(); } - private boolean checkQuota(final OwnCloudClient client) { - final GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation(); + private void fetchUserInfo(final OwnCloudClient client) { final RemoteOperationResult ocsResult = getRemoteUserInfoOperation.execute(client); if (ocsResult.isSuccess() && ocsResult.getData() != null && !ocsResult.getData().isEmpty()) { final UserInfo userInfo = (UserInfo) ocsResult.getData().get(0); + + final String name; + if (userInfo.displayName.isEmpty()) { + name = userInfo.alternateDisplayName; + } else { + name = userInfo.displayName; + } final double relativeQuota = userInfo.getQuota().relative; final long totalQuota = userInfo.getQuota().total; final long usedQuota = userInfo.getQuota().used; - 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); + final String groups = String.join(",", userInfo.groups); + final String email = userInfo.id; - if (relativeQuota >= 80.0) { - addNotifAboutQuota(relativeQuota); - } - return true; + accountManager.setUserData(account, ACCOUNT_DATA_NAME, name); + accountManager.setUserData(account, ACCOUNT_DATA_EMAIL, email); + 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); } - return false; } private void addNotifAboutQuota(final double relativeQuota) { @@ -100,19 +114,19 @@ public class AccountUserInfoWorker extends Worker { addNotification(context.getString(R.string.notif_quota_99Plus_title), context.getString(R.string.notif_quota_99Plus_text)); } else if (relativeQuota >= 90.0) { addNotification(context.getString(R.string.notif_quota_80plus_title), context.getString(R.string.notif_quota_90Plus_text)); - } else { + } else if (relativeQuota >= 80.0) { addNotification(context.getString(R.string.notif_quota_80plus_title), context.getString(R.string.notif_quota_80Plus_text)); } } /** - * send notification to inform user that he lacks space on ecloud + * send notification to inform user that he lacks space on ecloud * Improvement idea: * - add translatable message & title * - make message & title to be a parameter of the method, so we could reuse the function somewhere * - else with different notification. File conflict for example. **/ - private void addNotification(String title, String text){ + private void addNotification(String title, String text) { final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), AppConstants.notificationChannelID) .setSmallIcon(android.R.drawable.stat_sys_warning) @@ -124,20 +138,21 @@ public class AccountUserInfoWorker extends Worker { manager.notify(0, builder.build()); } - private boolean checkAliases(final OwnCloudClient client ) { - final GetAliasOperation getAliasOperation = new GetAliasOperation(); + private void fetchAliases(final OwnCloudClient client) { final RemoteOperationResult ocsResult = getAliasOperation.execute(client); - + String aliases = ""; if (ocsResult.isSuccess() && ocsResult.getData() != null && !ocsResult.getData().isEmpty()) { - final String alias = (String) ocsResult.getData().get(0); - accountManager.setUserData(account, ACCOUNT_DATA_ALIAS_KEY, alias); - return true; + ArrayList alias = new ArrayList<>(ocsResult.getData().size()); + for (Object object : ocsResult.getData()) { + alias.add(object.toString()); + } + aliases = String.join(",", alias); } - return false; + accountManager.setUserData(account, ACCOUNT_DATA_ALIAS_KEY, aliases); } - private void updateWidget(final Context context){ - final Intent updateIntent=new Intent(context, EDriveWidget.class); + private void updateWidget(final Context context) { + final Intent updateIntent = new Intent(context, EDriveWidget.class); updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); updateIntent.putExtra(INTENT_EXTRA_ACCOUNT_KEY, account); context.sendBroadcast(updateIntent); -- GitLab From a224739366eca0ad598f6f5dd5bdb7718c24f3bc Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 22 Apr 2022 15:09:01 +0200 Subject: [PATCH 06/10] add method in CommonUtils to start AccountUserInfoWorker --- .../foundation/e/drive/utils/CommonUtils.java | 17 +++++++++++++++++ .../e/drive/work/AccountUserInfoWorker.java | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java index b93257c4..8ffa8374 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -46,6 +46,7 @@ import java.util.concurrent.TimeUnit; import foundation.e.drive.R; import foundation.e.drive.models.SyncedFolder; +import foundation.e.drive.work.AccountUserInfoWorker; import foundation.e.drive.work.CreateRemoteFolderWorker; import foundation.e.drive.work.FirstStartWorker; import foundation.e.drive.work.FullScanWorker; @@ -456,4 +457,20 @@ public abstract class CommonUtils { Toast.LENGTH_SHORT).show(); } + + /** + * Job for Widget & notification about quota + * @param workManager + */ + public static void registerPeriodicUserInfoChecking(WorkManager workManager){ + final PeriodicWorkRequest periodicUserInfoScanRequest = + new PeriodicWorkRequest.Builder(AccountUserInfoWorker.class, + 31, TimeUnit.MINUTES, + 5, TimeUnit.MINUTES) + .addTag(AppConstants.WORK_GENERIC_TAG) + .build(); + + workManager.enqueueUniquePeriodicWork(AccountUserInfoWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.REPLACE, periodicUserInfoScanRequest); + } + } diff --git a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java index 1bf131d5..21ec3ca6 100644 --- a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java +++ b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java @@ -46,7 +46,7 @@ import foundation.e.drive.widgets.EDriveWidget; * @author TheScarastic */ public class AccountUserInfoWorker extends Worker { - + public static final String UNIQUE_WORK_NAME = "AccountUserInfoWorker"; final AccountManager accountManager; final GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation(); final GetAliasOperation getAliasOperation = new GetAliasOperation(); -- GitLab From 0bf56578a6b9d67431f72ae631918c07748cb3b9 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 22 Apr 2022 15:13:28 +0200 Subject: [PATCH 07/10] Trigger periodic worker for AccountUserInfoWorker --- .../java/foundation/e/drive/services/InitializerService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/foundation/e/drive/services/InitializerService.java b/app/src/main/java/foundation/e/drive/services/InitializerService.java index d493028d..fdb73abc 100644 --- a/app/src/main/java/foundation/e/drive/services/InitializerService.java +++ b/app/src/main/java/foundation/e/drive/services/InitializerService.java @@ -100,6 +100,8 @@ public class InitializerService extends Service { return; } + CommonUtils.registerPeriodicUserInfoChecking(WorkManager.getInstance(this)); + final List syncCategories = new ArrayList<>(); syncCategories.addAll(Arrays.asList(MEDIA_SYNCABLE_CATEGORIES)); -- GitLab From 261ea349b669dc8064fb59e8205590f82b3fbd67 Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Tue, 26 Apr 2022 14:53:58 +0530 Subject: [PATCH 08/10] eDrive: Adapt activity for workmanager * also fix widget and workmanager --- .../e/drive/activity/AccountsActivity.java | 177 ++++++------------ .../foundation/e/drive/utils/CommonUtils.java | 53 +++--- .../drive/widgets/EDriveNetworkCallback.java | 16 -- .../e/drive/widgets/EDriveWidget.java | 55 ++---- .../e/drive/work/AccountUserInfoWorker.java | 39 ++-- 5 files changed, 119 insertions(+), 221 deletions(-) delete mode 100644 app/src/main/java/foundation/e/drive/widgets/EDriveNetworkCallback.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 ff01990d..85fad65c 100644 --- a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java +++ b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java @@ -8,6 +8,12 @@ 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; import static foundation.e.drive.widgets.EDriveWidget.buildIntent; import static foundation.e.drive.widgets.EDriveWidget.convertIntoMB; import static foundation.e.drive.widgets.EDriveWidget.dataForWeb; @@ -18,41 +24,26 @@ import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.os.Handler; -import android.os.HandlerThread; import android.view.View; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; import com.bumptech.glide.Glide; +import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.UserInfo; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation; - -import java.util.ArrayList; import foundation.e.drive.R; import foundation.e.drive.databinding.ActivityAccountsBinding; -import foundation.e.drive.operations.GetAliasOperation; import foundation.e.drive.utils.CommonUtils; -import foundation.e.drive.widgets.EDriveNetworkCallback; import foundation.e.drive.widgets.EDriveWidget; public class AccountsActivity extends AppCompatActivity { - private static final String NON_OFFICIAL_AVATAR_PATH = "/index.php/avatar/"; + public static final String NON_OFFICIAL_AVATAR_PATH = "/index.php/avatar/"; private static final String ACCOUNT_SETTINGS = EDriveWidget.ACCOUNT_MANAGER_PACKAGE_NAME + ".ui.AccountsActivity"; - private final GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation(); - private final GetAliasOperation getAliasOperation = new GetAliasOperation(); - private UserInfo userInfo = null; - private ArrayList aliases; - private OwnCloudClient client; - private Account account; - private ActivityAccountsBinding binding; @Override @@ -63,139 +54,85 @@ public class AccountsActivity extends AppCompatActivity { setContentView(binding.getRoot()); setSupportActionBar(binding.toolbar); - binding.toolbar.setNavigationOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - onBackPressed(); - } - }); + binding.toolbar.setNavigationOnClickListener(v -> onBackPressed()); - binding.settings.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - final Intent settingsIntent = buildIntent(Intent.ACTION_VIEW, "") - .setComponent(new ComponentName( - EDriveWidget.ACCOUNT_MANAGER_PACKAGE_NAME, ACCOUNT_SETTINGS)); - startActivity(settingsIntent); - } + binding.settings.setOnClickListener(v -> { + final Intent settingsIntent = buildIntent(Intent.ACTION_VIEW, "") + .setComponent(new ComponentName( + EDriveWidget.ACCOUNT_MANAGER_PACKAGE_NAME, ACCOUNT_SETTINGS)); + startActivity(settingsIntent); }); + showDetails(); + } + private void showDetails() { final AccountManager accountManager = AccountManager.get(this); - account = CommonUtils.getAccount(getString(R.string.eelo_account_type), + final Account account = CommonUtils.getAccount(getString(R.string.eelo_account_type), accountManager); - client = CommonUtils.getOwnCloudClient(account, this); - - final HandlerThread handlerThread = new HandlerThread("Network Request"); - handlerThread.start(); - final Handler mHandler = new Handler(handlerThread.getLooper()); - - final EDriveNetworkCallback callback = new EDriveNetworkCallback() { - @Override - public void onComplete() { - runOnUiThread(new Runnable() { - @Override - public void run() { - onNetworkRequestCompleted(); - } - }); - } - }; - - if (!CommonUtils.haveNetworkConnection(this, true)) { - handleNoInternetConnection(); - return; - } - - binding.loading.setVisibility(View.VISIBLE); - mHandler.post(new Runnable() { - @Override - public void run() { - if (client != null) { - RemoteOperationResult ocsResult = getRemoteUserInfoOperation.execute(client); - if (ocsResult.isSuccess() && ocsResult.getData() != null) { - userInfo = (UserInfo) ocsResult.getData().get(0); - } - RemoteOperationResult aliasResult = getAliasOperation.execute(client); - if (aliasResult.isSuccess() && aliasResult.getData() != null) { - aliases = aliasResult.getData(); - } - } - callback.onComplete(); - } - }); - } + final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, this); - private void onNetworkRequestCompleted() { - binding.loading.setVisibility(View.GONE); - if (userInfo.displayName == null) { - binding.name.setText(userInfo.alternateDisplayName); - } else { - binding.name.setText(userInfo.displayName); - } - binding.email.setText(userInfo.id); + final String usedMB = accountManager.getUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY); + final String totalMB = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); + final String email = accountManager.getUserData(account, ACCOUNT_DATA_EMAIL); - final int usedMB = convertIntoMB(String.valueOf(userInfo.quota.used)); - final int totalMB = convertIntoMB(String.valueOf(userInfo.quota.total)); + binding.name.setText(accountManager.getUserData(account, ACCOUNT_DATA_NAME)); + binding.email.setText(email); - binding.progress.setMax(totalMB); - binding.progress.setProgress(usedMB); + binding.progress.setMax(convertIntoMB(totalMB)); + binding.progress.setProgress(convertIntoMB(usedMB)); binding.progress.setVisibility(View.VISIBLE); binding.plan.setText(getString(R.string.free_plan, - CommonUtils.humanReadableByteCountBin(userInfo.quota.total))); - for (String group : userInfo.groups) { + CommonUtils.humanReadableByteCountBin(Long.parseLong(totalMB)))); + + 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])); + binding.plan.setText(getString(R.string.premium_plan, + group.split("-")[1])); + break; } } + binding.myPlan.setVisibility(View.VISIBLE); binding.plan.setVisibility(View.VISIBLE); binding.status.setText(getString(R.string.progress_status, - CommonUtils.humanReadableByteCountBin(userInfo.quota.used), - CommonUtils.humanReadableByteCountBin(userInfo.quota.total))); + CommonUtils.humanReadableByteCountBin(Long.parseLong(usedMB)), + CommonUtils.humanReadableByteCountBin(Long.parseLong(totalMB)))); + String aliases = accountManager.getUserData(account, ACCOUNT_DATA_ALIAS_KEY); if (aliases != null && !aliases.isEmpty()) { binding.alias.setVisibility(View.VISIBLE); - binding.alias1.setText(aliases.get(0).toString()); + binding.alias1.setText(aliases.split(",")[0]); binding.aliasDivider.setVisibility(View.VISIBLE); } else { binding.alias.setVisibility(View.GONE); binding.alias1Container.setVisibility(View.GONE); } - binding.alias1Clipboard.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { + binding.alias1Clipboard.setOnClickListener(v -> CommonUtils.copyToClipboard(Uri.parse(binding.alias1.getText().toString()), - v.getContext(), v.getContext().getString(R.string.alias)); - } - }); + v.getContext(), v.getContext().getString(R.string.alias))); binding.upgrade.setVisibility(View.VISIBLE); - binding.upgrade.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - final Intent upgradeIntent = buildIntent(Intent.ACTION_VIEW, - String.format(EDriveWidget.WEBPAGE, userInfo.id, - dataForWeb(userInfo.quota.total))); - startActivity(upgradeIntent); - } + binding.upgrade.setOnClickListener(v -> { + final Intent upgradeIntent = buildIntent(Intent.ACTION_VIEW, + String.format(EDriveWidget.WEBPAGE, email, + dataForWeb(Long.parseLong(totalMB)))); + startActivity(upgradeIntent); }); - binding.alias.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (binding.alias1Container.getVisibility() == View.VISIBLE) { - binding.alias1Container.setVisibility(View.GONE); - binding.alias.setCompoundDrawablesWithIntrinsicBounds(null, null, - ContextCompat.getDrawable(v.getContext(), R.drawable.ic_expand_more), null); - } else { - binding.alias1Container.setVisibility(View.VISIBLE); - binding.alias.setCompoundDrawablesWithIntrinsicBounds(null, null, - ContextCompat.getDrawable(v.getContext(), R.drawable.ic_expand_less), null); - } + binding.alias.setOnClickListener(v -> { + if (binding.alias1Container.getVisibility() == View.VISIBLE) { + binding.alias1Container.setVisibility(View.GONE); + binding.alias.setCompoundDrawablesWithIntrinsicBounds(null, null, + ContextCompat.getDrawable(v.getContext(), R.drawable.ic_expand_more), null); + } else { + binding.alias1Container.setVisibility(View.VISIBLE); + binding.alias.setCompoundDrawablesWithIntrinsicBounds(null, null, + ContextCompat.getDrawable(v.getContext(), R.drawable.ic_expand_less), null); } }); @@ -206,10 +143,4 @@ public class AccountsActivity extends AppCompatActivity { .into(binding.avatar); binding.avatar.setVisibility(View.VISIBLE); } - - private void handleNoInternetConnection() { - binding.name.setText(account.name); - binding.email.setText(account.type); - binding.avatar.setVisibility(View.VISIBLE); - } } diff --git a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java index 8ffa8374..e3461d2d 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -70,6 +70,7 @@ import androidx.work.BackoffPolicy; import androidx.work.Constraints; import androidx.work.Data; import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.ExistingWorkPolicy; import androidx.work.NetworkType; import androidx.work.OneTimeWorkRequest; import androidx.work.PeriodicWorkRequest; @@ -170,10 +171,11 @@ public abstract class CommonUtils { /** * Read accountManager settings + * * @param account * @return true if usage of metered connection is allowed */ - public static boolean isMeteredNetworkAllowed(Account account){ + public static boolean isMeteredNetworkAllowed(Account account) { return ContentResolver.getSyncAutomatically(account, METERED_NETWORK_ALLOWED_AUTHORITY); } @@ -190,8 +192,8 @@ public abstract class CommonUtils { oc = OwnCloudClientFactory.createOwnCloudClient(serverUri, context, true); oc.setCredentials(new OwnCloudBasicCredentials(account.name, AccountManager.get(context).getPassword(account))); - Log.d(TAG, "user agent: "+AppConstants.USER_AGENT); - if (!AppConstants.USER_AGENT.equals(OwnCloudClientManagerFactory.getUserAgent()) ){ + Log.d(TAG, "user agent: " + AppConstants.USER_AGENT); + if (!AppConstants.USER_AGENT.equals(OwnCloudClientManagerFactory.getUserAgent())) { OwnCloudClientManagerFactory.setUserAgent(AppConstants.USER_AGENT); } @@ -225,7 +227,7 @@ public abstract class CommonUtils { /** * Tell if there is internet connection * - * @param context Activity or service which are calling this method + * @param context Activity or service which are calling this method * @param meteredNetworkAllowed true if service can use metered network / false either * @return True if there is connection, false either */ @@ -239,14 +241,13 @@ public abstract class CommonUtils { && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) && (capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) - || meteredNetworkAllowed)) { + || meteredNetworkAllowed)) { return true; } return false; } - /** * Get mimetype of file from the file itself * @@ -344,11 +345,13 @@ public abstract class CommonUtils { value *= Long.signum(bytes); return String.format(Locale.ENGLISH, "%.1f %cB", value / 1024.0, ci.current()); } + /** * Enqueue a unique periodic worker to look for file to be synchronized (remote files + local files + * * @param workManager the instance of workManager */ - public static void registerPeriodicFullScanWorker(WorkManager workManager){ + public static void registerPeriodicFullScanWorker(WorkManager workManager) { final Constraints constraints = new Constraints.Builder() .setRequiredNetworkType(NetworkType.UNMETERED) .setRequiresBatteryNotLow(true) @@ -368,20 +371,20 @@ public abstract class CommonUtils { /** * This method create a chain of WorkRequests to perform Initialization tasks: - * Firstly, it creates WorkRequest to create remote root folders on ecloud - * Then, once all folders are present on cloud, run the FirstStartWorker. - * - * in details: - * - Create 9 remote folders on ecloud - * - Run a first fullscan (ObserverService) - * - start SynchronizationService - * - Active FileObserver - * - Schedule periodic fullscan. + * Firstly, it creates WorkRequest to create remote root folders on ecloud + * Then, once all folders are present on cloud, run the FirstStartWorker. + *

+ * in details: + * - Create 9 remote folders on ecloud + * - Run a first fullscan (ObserverService) + * - start SynchronizationService + * - Active FileObserver + * - Schedule periodic fullscan. * * @param syncedFolders List of SyncedFolder for which we want to create a remote folder on ecloud - * @param workManager WorkManager instance to register WorkRequest + * @param workManager WorkManager instance to register WorkRequest */ - public static void registerInitializationWorkers(List syncedFolders, WorkManager workManager){ + public static void registerInitializationWorkers(List syncedFolders, WorkManager workManager) { if (syncedFolders == null || syncedFolders.isEmpty()) { Log.e(TAG, "Can't create remote folders. List is empty"); return; @@ -434,7 +437,7 @@ public abstract class CommonUtils { .build(); } - public static void createNotificationChannel(Context context){ + public static void createNotificationChannel(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { final CharSequence name = context.getString(R.string.notif_channel_name); final String description = context.getString(R.string.notif_channel_description); @@ -460,17 +463,21 @@ public abstract class CommonUtils { /** * Job for Widget & notification about quota + * * @param workManager */ - public static void registerPeriodicUserInfoChecking(WorkManager workManager){ + public static void registerPeriodicUserInfoChecking(WorkManager workManager) { + final Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(); + final PeriodicWorkRequest periodicUserInfoScanRequest = new PeriodicWorkRequest.Builder(AccountUserInfoWorker.class, - 31, TimeUnit.MINUTES, - 5, TimeUnit.MINUTES) + 30, TimeUnit.MINUTES) .addTag(AppConstants.WORK_GENERIC_TAG) + .setConstraints(constraints) .build(); workManager.enqueueUniquePeriodicWork(AccountUserInfoWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.REPLACE, periodicUserInfoScanRequest); } - } diff --git a/app/src/main/java/foundation/e/drive/widgets/EDriveNetworkCallback.java b/app/src/main/java/foundation/e/drive/widgets/EDriveNetworkCallback.java deleted file mode 100644 index 3e0706e5..00000000 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveNetworkCallback.java +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright © ECORP SAS 2022. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the GNU Public License v3.0 - * which accompanies this distribution, and is available at - * http://www.gnu.org/licenses/gpl.html - */ - -package foundation.e.drive.widgets; - -/** - * @author TheScarastic - */ -public interface EDriveNetworkCallback { - void onComplete(); -} 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 03219129..159539ad 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -23,10 +23,9 @@ import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.net.ConnectivityManager; -import android.net.Network; import android.net.Uri; import android.provider.Settings; +import android.util.Log; import android.view.View; import android.widget.RemoteViews; @@ -42,14 +41,14 @@ import foundation.e.drive.utils.CommonUtils; * Implementation of App Widget functionality. */ public class EDriveWidget extends AppWidgetProvider { - public static final String INTENT_EXTRA_ACCOUNT_KEY = "account"; public static final String WEBPAGE = "https://esolutions.shop/ecloud-subscriptions/?username=%s&token=placeholder¤t-quota=%s&from=wp"; - public static final String ACCOUNT_MANAGER_PACKAGE_NAME = "foundation.e.accountmanager"; - private static final String TAG = EDriveWidget.class.getSimpleName(); private static final String ADD_ACCOUNT_WEBPAGE = "https://e.foundation/e-email-invite/"; + + public static final String ACCOUNT_MANAGER_PACKAGE_NAME = "foundation.e.accountmanager"; private static final String GET_ACCOUNT_MANAGER_COMPONENT_NAME = ACCOUNT_MANAGER_PACKAGE_NAME + ".ui.setup.LoginActivity"; + private static final String SETUP_ACCOUNT_PROVIDER_TYPE = "setup_account_provider_type"; private static final String ACCOUNT_PROVIDER_EELO = "eelo"; @@ -57,14 +56,13 @@ public class EDriveWidget extends AppWidgetProvider { private static final String HIDE_ALIAS = "hide_alias"; private static final String COPY_ALIAS = "copy_alias"; private static boolean showAlias = false; - private static boolean isNetworkAvailable = false; + private final Calendar calender = Calendar.getInstance(); private final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault()); private RemoteViews views; private Account account = null; - public static String dataForWeb(Long bytes) { final String space = CommonUtils.humanReadableByteCountBin(bytes); final String[] split = space.split(" "); @@ -105,12 +103,12 @@ public class EDriveWidget extends AppWidgetProvider { accountManager); } - if (account == null && isNetworkAvailable) { + if (account == null && CommonUtils.haveNetworkConnection(context, true)) { noAccountView(context); - appWidgetManager.updateAppWidget(appWidgetId, views); } else { onAccountAvailable(context, accountManager); } + appWidgetManager.updateAppWidget(appWidgetId, views); } @Override @@ -125,13 +123,6 @@ public class EDriveWidget extends AppWidgetProvider { if (action == null) return; switch (action) { case AppWidgetManager.ACTION_APPWIDGET_UPDATE: - if (intent.getExtras() != null) { - account = (Account) intent.getExtras().get(INTENT_EXTRA_ACCOUNT_KEY); - updateAppWidget(context); - } - break; - case Intent.ACTION_BOOT_COMPLETED: - registerConnectivityCallback(context); case AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION: updateAppWidget(context); break; @@ -168,7 +159,11 @@ public class EDriveWidget extends AppWidgetProvider { } private void onAccountAvailable(Context context, AccountManager accountManager) { - if (!isNetworkAvailable) { + final String usedMB = accountManager.getUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY); + final String totalMB = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); + final String email = accountManager.getUserData(account, ACCOUNT_DATA_EMAIL); + + if (email == null || email.isEmpty()) { views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget_login); views.setViewVisibility(R.id.button_container, View.GONE); views.setTextViewText(R.id.summary, context.getString(R.string.no_internet_widget)); @@ -177,10 +172,6 @@ public class EDriveWidget extends AppWidgetProvider { views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget); - final String usedMB = accountManager.getUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY); - final String totalMB = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); - final String email = accountManager.getUserData(account, ACCOUNT_DATA_EMAIL); - views.setTextViewText(R.id.email, email); views.setTextViewText(R.id.name, accountManager.getUserData(account, ACCOUNT_DATA_NAME)); @@ -248,26 +239,4 @@ public class EDriveWidget extends AppWidgetProvider { } return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE); } - - private void registerConnectivityCallback(final Context context) { - ConnectivityManager cm = (ConnectivityManager) context.getSystemService( - Context.CONNECTIVITY_SERVICE); - cm.registerDefaultNetworkCallback(new ConnectivityManager.NetworkCallback() { - @Override - public void onAvailable(Network network) { - if (!isNetworkAvailable) { - isNetworkAvailable = true; - updateAppWidget(context); - } - } - - @Override - public void onLost(Network network) { - if (isNetworkAvailable) { - isNetworkAvailable = false; - updateAppWidget(context); - } - } - }); - } } \ No newline at end of file diff --git a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java index 21ec3ca6..26be1483 100644 --- a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java +++ b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java @@ -14,7 +14,6 @@ import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_NAME; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_RELATIVE_QUOTA_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_TOTAL_QUOTA_KEY; import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_USED_QUOTA_KEY; -import static foundation.e.drive.widgets.EDriveWidget.INTENT_EXTRA_ACCOUNT_KEY; import android.accounts.Account; import android.accounts.AccountManager; @@ -28,6 +27,8 @@ import androidx.core.app.NotificationCompat; import androidx.work.Worker; import androidx.work.WorkerParameters; +import com.bumptech.glide.Glide; +import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.UserInfo; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -36,6 +37,7 @@ import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation; import java.util.ArrayList; import foundation.e.drive.R; +import foundation.e.drive.activity.AccountsActivity; import foundation.e.drive.operations.GetAliasOperation; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; @@ -50,43 +52,46 @@ public class AccountUserInfoWorker extends Worker { final AccountManager accountManager; final GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation(); final GetAliasOperation getAliasOperation = new GetAliasOperation(); - private final Account account; private final Context mContext; - + private Account account; public AccountUserInfoWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); mContext = context; accountManager = AccountManager.get(context); - account = CommonUtils.getAccount(context.getString(R.string.eelo_account_type), - accountManager); } @NonNull @Override public Result doWork() { - if (account != null && CommonUtils.haveNetworkConnection(mContext, true)) { - final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, mContext); - - if (client != null) { - fetchUserInfo(client); - fetchAliases(client); + account = CommonUtils.getAccount(mContext.getString(R.string.eelo_account_type), + accountManager); + OwnCloudClient client = CommonUtils.getOwnCloudClient(account, mContext); + + if (account != null && client != null) { + if (fetchUserInfo(client) && fetchAliases(client)) { + Glide.with(mContext) + .load(client.getBaseUri() + AccountsActivity.NON_OFFICIAL_AVATAR_PATH + + client.getCredentials().getUsername() + "/" + 300) + .diskCacheStrategy(DiskCacheStrategy.ALL) + .preload(); updateWidget(mContext); return Result.success(); + } else { + return Result.retry(); } } - updateWidget(mContext); return Result.failure(); } - private void fetchUserInfo(final OwnCloudClient client) { + private boolean fetchUserInfo(final OwnCloudClient client) { final RemoteOperationResult ocsResult = getRemoteUserInfoOperation.execute(client); if (ocsResult.isSuccess() && ocsResult.getData() != null && !ocsResult.getData().isEmpty()) { final UserInfo userInfo = (UserInfo) ocsResult.getData().get(0); final String name; - if (userInfo.displayName.isEmpty()) { + if (userInfo.displayName == null || userInfo.displayName.isEmpty()) { name = userInfo.alternateDisplayName; } else { name = userInfo.displayName; @@ -105,7 +110,9 @@ public class AccountUserInfoWorker extends Worker { accountManager.setUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY, "" + usedQuota); addNotifAboutQuota(relativeQuota); + return true; } + return false; } private void addNotifAboutQuota(final double relativeQuota) { @@ -138,7 +145,7 @@ public class AccountUserInfoWorker extends Worker { manager.notify(0, builder.build()); } - private void fetchAliases(final OwnCloudClient client) { + private boolean fetchAliases(final OwnCloudClient client) { final RemoteOperationResult ocsResult = getAliasOperation.execute(client); String aliases = ""; if (ocsResult.isSuccess() && ocsResult.getData() != null && !ocsResult.getData().isEmpty()) { @@ -149,12 +156,12 @@ public class AccountUserInfoWorker extends Worker { aliases = String.join(",", alias); } accountManager.setUserData(account, ACCOUNT_DATA_ALIAS_KEY, aliases); + return true; } private void updateWidget(final Context context) { final Intent updateIntent = new Intent(context, EDriveWidget.class); updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); - updateIntent.putExtra(INTENT_EXTRA_ACCOUNT_KEY, account); context.sendBroadcast(updateIntent); } } -- GitLab From 4fb909782778e590330d24ee7ee81f987c593369 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Tue, 26 Apr 2022 12:25:05 +0000 Subject: [PATCH 09/10] Apply 1 suggestion(s) to 1 file(s) --- app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 159539ad..afff7745 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -163,7 +163,7 @@ public class EDriveWidget extends AppWidgetProvider { final String totalMB = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); final String email = accountManager.getUserData(account, ACCOUNT_DATA_EMAIL); - if (email == null || email.isEmpty()) { + if (email == null || email.trim().isEmpty()) { views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget_login); views.setViewVisibility(R.id.button_container, View.GONE); views.setTextViewText(R.id.summary, context.getString(R.string.no_internet_widget)); -- GitLab From 044f4d8539b03dc72d088f86daeab80d086932f8 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 26 Apr 2022 14:28:12 +0200 Subject: [PATCH 10/10] remove commented line and rename variables in Widget --- .../e/drive/activity/AccountsActivity.java | 18 ++++++++---------- .../drive/services/SynchronizationService.java | 1 - .../e/drive/widgets/EDriveWidget.java | 18 ++++++++---------- 3 files changed, 16 insertions(+), 21 deletions(-) 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 85fad65c..d0c2fdbf 100644 --- a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java +++ b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java @@ -30,7 +30,6 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; import com.bumptech.glide.Glide; -import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.owncloud.android.lib.common.OwnCloudClient; import foundation.e.drive.R; @@ -71,26 +70,25 @@ public class AccountsActivity extends AppCompatActivity { accountManager); final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, this); - final String usedMB = accountManager.getUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY); - final String totalMB = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); + final String usedQuota = accountManager.getUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY); + final String totalQuota = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); final String email = accountManager.getUserData(account, ACCOUNT_DATA_EMAIL); binding.name.setText(accountManager.getUserData(account, ACCOUNT_DATA_NAME)); binding.email.setText(email); - binding.progress.setMax(convertIntoMB(totalMB)); - binding.progress.setProgress(convertIntoMB(usedMB)); + binding.progress.setMax(convertIntoMB(totalQuota)); + binding.progress.setProgress(convertIntoMB(usedQuota)); binding.progress.setVisibility(View.VISIBLE); binding.plan.setText(getString(R.string.free_plan, - CommonUtils.humanReadableByteCountBin(Long.parseLong(totalMB)))); + CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)))); 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; } } @@ -99,8 +97,8 @@ public class AccountsActivity extends AppCompatActivity { binding.plan.setVisibility(View.VISIBLE); binding.status.setText(getString(R.string.progress_status, - CommonUtils.humanReadableByteCountBin(Long.parseLong(usedMB)), - CommonUtils.humanReadableByteCountBin(Long.parseLong(totalMB)))); + CommonUtils.humanReadableByteCountBin(Long.parseLong(usedQuota)), + CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)))); String aliases = accountManager.getUserData(account, ACCOUNT_DATA_ALIAS_KEY); if (aliases != null && !aliases.isEmpty()) { @@ -120,7 +118,7 @@ public class AccountsActivity extends AppCompatActivity { binding.upgrade.setOnClickListener(v -> { final Intent upgradeIntent = buildIntent(Intent.ACTION_VIEW, String.format(EDriveWidget.WEBPAGE, email, - dataForWeb(Long.parseLong(totalMB)))); + dataForWeb(Long.parseLong(totalQuota)))); startActivity(upgradeIntent); }); diff --git a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java index 7d8a34dd..72cee50c 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -197,7 +197,6 @@ public class SynchronizationService extends Service implements OnRemoteOperation case QUOTA_EXCEEDED: //Case specific to UploadFileOperation Log.w(TAG, "Quota_EXCEEDED"); - //addNotification(getString(R.string.notif_quota_99Plus_title), getString(R.string.notif_quota_execeeded_text)); break; case FILE_NOT_FOUND: //Case specific to DownloadFileOperation 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 afff7745..f2476841 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -25,7 +25,6 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; import android.provider.Settings; -import android.util.Log; import android.view.View; import android.widget.RemoteViews; @@ -43,9 +42,8 @@ import foundation.e.drive.utils.CommonUtils; public class EDriveWidget extends AppWidgetProvider { public static final String WEBPAGE = "https://esolutions.shop/ecloud-subscriptions/?username=%s&token=placeholder¤t-quota=%s&from=wp"; - private static final String ADD_ACCOUNT_WEBPAGE = "https://e.foundation/e-email-invite/"; - public static final String ACCOUNT_MANAGER_PACKAGE_NAME = "foundation.e.accountmanager"; + private static final String ADD_ACCOUNT_WEBPAGE = "https://e.foundation/e-email-invite/"; private static final String GET_ACCOUNT_MANAGER_COMPONENT_NAME = ACCOUNT_MANAGER_PACKAGE_NAME + ".ui.setup.LoginActivity"; @@ -159,8 +157,8 @@ public class EDriveWidget extends AppWidgetProvider { } private void onAccountAvailable(Context context, AccountManager accountManager) { - final String usedMB = accountManager.getUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY); - final String totalMB = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); + final String usedQuota = accountManager.getUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY); + final String totalQuota = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); final String email = accountManager.getUserData(account, ACCOUNT_DATA_EMAIL); if (email == null || email.trim().isEmpty()) { @@ -175,10 +173,10 @@ public class EDriveWidget extends AppWidgetProvider { views.setTextViewText(R.id.email, email); views.setTextViewText(R.id.name, accountManager.getUserData(account, ACCOUNT_DATA_NAME)); - views.setProgressBar(R.id.progress, convertIntoMB(totalMB), convertIntoMB(usedMB), false); + views.setProgressBar(R.id.progress, convertIntoMB(totalQuota), convertIntoMB(usedQuota), false); views.setTextViewText(R.id.planName, context.getString(R.string.free_plan, - CommonUtils.humanReadableByteCountBin(Long.parseLong(totalMB)))); + CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)))); String[] groups = accountManager.getUserData(account, ACCOUNT_DATA_GROUPS).split(","); for (String group : groups) { @@ -190,8 +188,8 @@ public class EDriveWidget extends AppWidgetProvider { } views.setTextViewText(R.id.status, context.getString(R.string.progress_status, - CommonUtils.humanReadableByteCountBin(Long.parseLong(usedMB)), - CommonUtils.humanReadableByteCountBin(Long.parseLong(totalMB)))); + CommonUtils.humanReadableByteCountBin(Long.parseLong(usedQuota)), + CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)))); views.setTextViewText(R.id.sync, context.getString(R.string.last_synced, sdf.format(calender.getTime()))); @@ -226,7 +224,7 @@ public class EDriveWidget extends AppWidgetProvider { final PendingIntent pendingIntentUpgrade = PendingIntent.getActivity(context, 0, buildIntent(Intent.ACTION_VIEW, String.format(WEBPAGE, email, - dataForWeb(Long.parseLong(totalMB)))), + dataForWeb(Long.parseLong(totalQuota)))), PendingIntent.FLAG_IMMUTABLE); views.setOnClickPendingIntent(R.id.upgrade, pendingIntentUpgrade); } -- GitLab