From 2e5da77c6071d071a94265a89bcb73852cd74e2f Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 5 Apr 2022 11:04:12 +0200 Subject: [PATCH 1/9] Create NotificationChannel when SDK is >= Oreo Add method to create notificationChannel in CommonUtils and add few strings (in res/values folder) for that purpose --- .../foundation/e/drive/EdriveApplication.java | 5 +++++ .../foundation/e/drive/utils/AppConstants.java | 3 +-- .../foundation/e/drive/utils/CommonUtils.java | 18 ++++++++++++++++++ app/src/main/res/values/strings.xml | 4 ++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/EdriveApplication.java b/app/src/main/java/foundation/e/drive/EdriveApplication.java index fcb1a0f8..4551cbe2 100644 --- a/app/src/main/java/foundation/e/drive/EdriveApplication.java +++ b/app/src/main/java/foundation/e/drive/EdriveApplication.java @@ -11,9 +11,12 @@ package foundation.e.drive; import android.accounts.Account; import android.accounts.AccountManager; import android.app.Application; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.os.Build; import android.os.Environment; import android.util.Log; @@ -43,11 +46,13 @@ public class EdriveApplication extends Application { mFileObserver = new RecursiveFileObserver(getApplicationContext(), pathForObserver, fileEventListener); SharedPreferences prefs = getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); + CommonUtils.createNotificationChannel(getApplicationContext()); if (prefs.getString(AccountManager.KEY_ACCOUNT_NAME, null) != null) { Log.d(TAG, "Account already registered"); startRecursiveFileObserver(); + Intent SynchronizationServiceIntent = new Intent(getApplicationContext(), SynchronizationService.class); startService(SynchronizationServiceIntent); 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 fec0762d..63f00e48 100644 --- a/app/src/main/java/foundation/e/drive/utils/AppConstants.java +++ b/app/src/main/java/foundation/e/drive/utils/AppConstants.java @@ -34,8 +34,7 @@ public abstract class AppConstants { 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 notificationChannelID ="3310"; - public static final String notificationChannelName="eDrive channel"; + 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 +")"; 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 2cab9888..563841fd 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -12,6 +12,8 @@ package foundation.e.drive.utils; import android.accounts.Account; import android.accounts.AccountManager; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.Service; import android.content.ContentResolver; import android.content.Context; @@ -19,6 +21,7 @@ import android.media.MediaScannerConnection; import android.net.ConnectivityManager; import android.net.NetworkCapabilities; import android.net.Uri; +import android.os.Build; import android.util.Log; import android.webkit.MimeTypeMap; @@ -35,6 +38,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; +import foundation.e.drive.R; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.work.CreateRemoteFolderWorker; import foundation.e.drive.work.FirstStartWorker; @@ -389,4 +393,18 @@ public abstract class CommonUtils { .putBoolean(DATA_KEY_MEDIATYPE, folder.isMediaType()) .build(); } + + 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); + int importance = NotificationManager.IMPORTANCE_DEFAULT; + final NotificationChannel channel = new NotificationChannel(AppConstants.notificationChannelID, name, importance); + channel.setDescription(description); + // Register the channel with the system; you can't change the importance + // or other notification behaviors after this + final NotificationManager notificationManager = context.getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(channel); + } + } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8f9814a8..54ee1424 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,4 +1,8 @@ /e/ Drive e.foundation.webdav.eelo + eDrive\'s notification + eDrive\'s notification channel + Drive Quota Exceeded + eDrive\'s notification channel -- GitLab From d99ca938b7aef52c6f01ff987826319cfa72337c Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 5 Apr 2022 11:41:49 +0200 Subject: [PATCH 2/9] read user's relative quota before to upload a file --- .../foundation/e/drive/operations/UploadFileOperation.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java index ff3f00c8..248c6dda 100644 --- a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java @@ -85,10 +85,13 @@ public class UploadFileOperation extends RemoteOperation { return new RemoteOperationResult(ResultCode.SYNC_CONFLICT); } + Double relativeQuotaBeforeFileUpload = 0.0; if (this.availableQuota == -1) { RemoteOperationResult checkQuotaResult = checkAvailableSpace(client, file.length()); if (checkQuotaResult.getCode() != ResultCode.OK) { return new RemoteOperationResult(checkQuotaResult.getCode()); + }else{ + relativeQuotaBeforeFileUpload = (Double) checkQuotaResult.getSingleData(); } } @@ -155,6 +158,7 @@ public class UploadFileOperation extends RemoteOperation { ArrayList datas = new ArrayList<>(); datas.add(syncedState.getSyncedFolderId()); + datas.add(relativeQuotaBeforeFileUpload); final RemoteOperationResult finalResult = new RemoteOperationResult(resultCode); finalResult.setData(datas); return finalResult; @@ -194,6 +198,8 @@ public class UploadFileOperation extends RemoteOperation { return new RemoteOperationResult(ResultCode.QUOTA_EXCEEDED); } else { Log.d(TAG, "Quota Okay"); + RemoteOperationResult result = new RemoteOperationResult(ResultCode.OK); + result.setSingleData((Double) userInfo.getQuota().getRelative()); return new RemoteOperationResult(ResultCode.OK); } } else { -- GitLab From e3c27fff185329a2fa647a7558e4263c714c148e Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 5 Apr 2022 12:11:30 +0200 Subject: [PATCH 3/9] Synchronization service create notification for quota usage --- app/src/main/AndroidManifest.xml | 2 + .../services/SynchronizationService.java | 46 +++++++++++++------ app/src/main/res/values/strings.xml | 8 +++- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 29b9ba12..d2d8002a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,6 +17,8 @@ http://www.gnu.org/licenses/gpl.html + + = 99.0) { + addNotification(getString(R.string.notif_quota_nearlyReached_title), getString(R.string.notif_quota_99Plus_text)); + } else if (relativeQuota >= 90.0) { + addNotification(getString(R.string.notif_quota_nearlyReached_title), getString(R.string.notif_quota_90Plus_text)); + } else if (relativeQuota >= 80.0) { + addNotification(getString(R.string.notif_quota_nearlyReached_title), getString(R.string.notif_quota_80Plus_text)); + } + } switch (result.getCode()) { case OK: Log.d(TAG, operationClassName + " Succeed"); @@ -187,19 +200,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation case QUOTA_EXCEEDED: //Case specific to UploadFileOperation Log.w(TAG, "Quota_EXCEEDED"); - - NotificationManager nM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); - - Notification notif = new Notification.Builder(this, AppConstants.notificationChannelID) - .setContentIntent(PendingIntent.getActivity(getApplicationContext(), - 0, - new Intent(Intent.ACTION_VIEW, client.getBaseUri()), - 0)) - .setContentText("Your drive lacks of space. Tap to check " + client.getBaseUri()) - .setSmallIcon(android.R.drawable.stat_sys_warning) - .build(); - - nM.notify(1,notif ); + addNotification(getString(R.string.notif_quota_execeeded_title), getString(R.string.notif_quota_execeeded_text)); break; case FILE_NOT_FOUND: //Case specific to DownloadFileOperation @@ -233,6 +234,25 @@ 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+ client.getBaseUri()); + // 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/res/values/strings.xml b/app/src/main/res/values/strings.xml index 54ee1424..4ba1b935 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,6 +3,10 @@ e.foundation.webdav.eelo eDrive\'s notification eDrive\'s notification channel - Drive Quota Exceeded - eDrive\'s notification channel + /e/ account\'s quota reached + /e/ account\'s quota nearly reached + eDrive can\'t upload a file that is bigger than remaining quota at: + You filled 99% of your /e/\'s account quota. check: + You filled 90% of your /e/\'s account quota. check: + You filled 80% of your /e/\'s account quota. check: -- GitLab From 47d0d57a9bd2883dde76d1591552ba33dc0f2da5 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 5 Apr 2022 12:21:34 +0200 Subject: [PATCH 4/9] add missing dependency for notification --- app/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/app/build.gradle b/app/build.gradle index 018d91fd..e1ec7cf1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -60,6 +60,7 @@ dependencies { api project(':NextcloudLib') implementation fileTree(include: ['*.jar'], dir: 'libs') api 'androidx.annotation:annotation:1.3.0' + implementation 'androidx.core:core:1.6.0' def work_version = "2.7.1" // (Java only) -- GitLab From 56f1075759f2b5c0f188347080d744641a766daf Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 5 Apr 2022 15:23:55 +0200 Subject: [PATCH 5/9] UploadFileOperation: fix quota checking --- .../java/foundation/e/drive/operations/UploadFileOperation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java index 248c6dda..48826b7c 100644 --- a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java @@ -200,7 +200,7 @@ public class UploadFileOperation extends RemoteOperation { Log.d(TAG, "Quota Okay"); RemoteOperationResult result = new RemoteOperationResult(ResultCode.OK); result.setSingleData((Double) userInfo.getQuota().getRelative()); - return new RemoteOperationResult(ResultCode.OK); + return result; } } else { Log.w(TAG, "getRemoteUserInfoOperation failed: "+ocsResult.getHttpCode() ); -- GitLab From 65c114d7377ab3bf4a62dc237fce460ab270089b Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Tue, 5 Apr 2022 13:25:32 +0000 Subject: [PATCH 6/9] Apply 1 suggestion(s) to 1 file(s) --- .../java/foundation/e/drive/operations/UploadFileOperation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java index 48826b7c..e0171369 100644 --- a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java @@ -90,7 +90,7 @@ public class UploadFileOperation extends RemoteOperation { RemoteOperationResult checkQuotaResult = checkAvailableSpace(client, file.length()); if (checkQuotaResult.getCode() != ResultCode.OK) { return new RemoteOperationResult(checkQuotaResult.getCode()); - }else{ + } else { relativeQuotaBeforeFileUpload = (Double) checkQuotaResult.getSingleData(); } } -- GitLab From 7de4135428109edb7b871970e2d97bba2ec32a85 Mon Sep 17 00:00:00 2001 From: Vincent Bourgmayer Date: Tue, 5 Apr 2022 13:34:03 +0000 Subject: [PATCH 7/9] Apply 1 suggestion(s) to 1 file(s) --- app/src/main/java/foundation/e/drive/EdriveApplication.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/EdriveApplication.java b/app/src/main/java/foundation/e/drive/EdriveApplication.java index 4551cbe2..a8f39f23 100644 --- a/app/src/main/java/foundation/e/drive/EdriveApplication.java +++ b/app/src/main/java/foundation/e/drive/EdriveApplication.java @@ -52,7 +52,6 @@ public class EdriveApplication extends Application { Log.d(TAG, "Account already registered"); startRecursiveFileObserver(); - Intent SynchronizationServiceIntent = new Intent(getApplicationContext(), SynchronizationService.class); startService(SynchronizationServiceIntent); -- GitLab From c5f2c70a91759fa957fe418292833403cc6b6f59 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 5 Apr 2022 15:59:16 +0200 Subject: [PATCH 8/9] fix parsing for user quota --- .../foundation/e/drive/operations/UploadFileOperation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java index 48826b7c..aa4acf55 100644 --- a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java @@ -85,13 +85,13 @@ public class UploadFileOperation extends RemoteOperation { return new RemoteOperationResult(ResultCode.SYNC_CONFLICT); } - Double relativeQuotaBeforeFileUpload = 0.0; + Float relativeQuotaBeforeFileUpload = 0.0f; if (this.availableQuota == -1) { RemoteOperationResult checkQuotaResult = checkAvailableSpace(client, file.length()); if (checkQuotaResult.getCode() != ResultCode.OK) { return new RemoteOperationResult(checkQuotaResult.getCode()); }else{ - relativeQuotaBeforeFileUpload = (Double) checkQuotaResult.getSingleData(); + relativeQuotaBeforeFileUpload = ((Double) checkQuotaResult.getSingleData()).floatValue(); } } -- GitLab From a18048cfd2a16118865437635be2bc0fe1d87d66 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 5 Apr 2022 16:04:50 +0200 Subject: [PATCH 9/9] refix space around else statement --- .../java/foundation/e/drive/operations/UploadFileOperation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java index aa4acf55..5d972a0b 100644 --- a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java @@ -90,7 +90,7 @@ public class UploadFileOperation extends RemoteOperation { RemoteOperationResult checkQuotaResult = checkAvailableSpace(client, file.length()); if (checkQuotaResult.getCode() != ResultCode.OK) { return new RemoteOperationResult(checkQuotaResult.getCode()); - }else{ + } else { relativeQuotaBeforeFileUpload = ((Double) checkQuotaResult.getSingleData()).floatValue(); } } -- GitLab