Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2ddc328e authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Merge branch '303-limite-quota-notification' into 'v1-oreo'

Limit notification to once a day

See merge request !134
parents 4e1bce22 5f5c98be
Loading
Loading
Loading
Loading
Loading
+29 −2
Original line number Diff line number Diff line
@@ -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: