From 5f5c98be2894bd521ce8168e01273c5edeb5d556 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Tue, 17 May 2022 08:55:15 +0200 Subject: [PATCH] Limit notification to once a day We don't want the user to be flooded by quota notifications. Therefore, we decided to notify the user once a day max. --- .../e/drive/work/AccountUserInfoWorker.java | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) 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 b2532894..700338d0 100644 --- a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java +++ b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java @@ -21,6 +21,7 @@ import android.app.NotificationManager; import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; @@ -120,18 +121,44 @@ public class AccountUserInfoWorker extends Worker { private void addNotifAboutQuota(final double relativeQuota) { final Context context = getApplicationContext(); + boolean needToNotify = needToSendNotification(context); + final NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); if (relativeQuota >= 99.0) { addNotification(manager, context.getString(R.string.notif_quota_99Plus_title), context.getString(R.string.notif_quota_99Plus_text), true); - } else if (relativeQuota >= 90.0) { + } else if (relativeQuota >= 90.0 && needToNotify) { addNotification(manager, context.getString(R.string.notif_quota_80plus_title), context.getString(R.string.notif_quota_90Plus_text), false); - } else if (relativeQuota >= 80.0) { + } else if (relativeQuota >= 80.0 && needToNotify) { addNotification(manager, context.getString(R.string.notif_quota_80plus_title), context.getString(R.string.notif_quota_80Plus_text), false); } else { manager.cancelAll(); } } + private boolean needToSendNotification(Context context) { + final String LAST_NOTIFICATION_TIMESTAMP_KEY = "last_notification_timestamp"; + final long MILLI_SECONDS_IN_A_DAY = 24 * 60 * 60 * 1000; + + SharedPreferences sharedPreferences = context.getSharedPreferences( + AppConstants.SHARED_PREFERENCE_NAME, + Context.MODE_PRIVATE + ); + + long previousTimestamp = sharedPreferences.getLong(LAST_NOTIFICATION_TIMESTAMP_KEY, 0); + long currentTimeStamp = System.currentTimeMillis(); + + if (previousTimestamp == 0 || + currentTimeStamp - previousTimestamp > MILLI_SECONDS_IN_A_DAY) { + sharedPreferences + .edit() + .putLong(LAST_NOTIFICATION_TIMESTAMP_KEY, currentTimeStamp) + .apply(); + return true; + } + + return false; + } + /** * send notification to inform user that he lacks space on ecloud * Improvement idea: -- GitLab