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

Commit d9d218cb authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge "Notification: Display on a regular basis" into main

parents f74910d4 cb6f6832
Loading
Loading
Loading
Loading
+44 −17
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ import android.util.Pair;
import com.android.bluetooth.R;
import com.android.internal.messages.SystemMessageProto.SystemMessage;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Map;

public class NotificationHelperService extends Service {
@@ -61,8 +63,6 @@ public class NotificationHelperService extends Service {

    private static final String AUTO_ON_USER_ACTION =
            "android.bluetooth.notification.action.AUTO_ON_USER_ACTION";
    private static final String AUTO_ON_USER_EXTRA =
            "android.bluetooth.notification.extra.AUTO_ON_DISABLE";

    private static final Map<String, Pair<Integer /* titleId */, Integer /* messageId */>>
            NOTIFICATION_MAP =
@@ -96,7 +96,7 @@ public class NotificationHelperService extends Service {
                sendToggleNotification(intent.getStringExtra(NOTIFICATION_EXTRA));
            }
            case AUTO_ON_USER_ACTION -> {
                autoOnUserAction(intent.getBooleanExtra(AUTO_ON_USER_EXTRA, false));
                autoOnUserAction();
            }
        }
        return Service.START_NOT_STICKY;
@@ -110,13 +110,9 @@ public class NotificationHelperService extends Service {
            return;
        }

        if (!isFirstTimeNotification(notificationReason)) {
            Log.d(TAG, logHeader + "already displayed");
        if (!shouldDisplayNotification(notificationReason)) {
            return;
        }
        Settings.Secure.putInt(getContentResolver(), notificationReason, 1);

        Log.d(TAG, logHeader + "sending");

        NotificationManager notificationManager =
                requireNonNull(getSystemService(NotificationManager.class));
@@ -131,7 +127,7 @@ public class NotificationHelperService extends Service {
                new NotificationChannel(
                        NOTIFICATION_CHANNEL,
                        getString(NOTIFICATION_GROUP),
                        NotificationManager.IMPORTANCE_HIGH));
                        NotificationManager.IMPORTANCE_LOW));

        String title = getString(notificationContent.first);
        String message = getString(notificationContent.second);
@@ -166,7 +162,7 @@ public class NotificationHelperService extends Service {
                    PendingIntent.getService(
                            this,
                            0,
                            new Intent(baseIntent).putExtra(AUTO_ON_USER_EXTRA, true),
                            new Intent(baseIntent),
                            PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);
            builder.addAction(
                    new Notification.Action.Builder(
@@ -180,14 +176,45 @@ public class NotificationHelperService extends Service {
                tag, SystemMessage.ID.NOTE_BT_APM_NOTIFICATION_VALUE, builder.build());
    }

    /** Return whether the notification has been shown */
    private boolean isFirstTimeNotification(String name) {
        return Settings.Secure.getInt(getContentResolver(), name, 0) == 0;
    private boolean shouldDisplayNotification(String countKey) {
        final LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
        final String dateKey = countKey + "_date";
        final String date = Settings.Secure.getString(getContentResolver(), dateKey);
        final int countShown = Settings.Secure.getInt(getContentResolver(), countKey, 0);

        // The notification is always displayed the first time and if it has been at least…:
        //  * … 1 week since the first display (aka recurring only once)
        //  * … 6 months since the last display (aka recurring forever)

        LocalDateTime savedDate = null;
        if (date != null) {
            savedDate = LocalDateTime.parse(date);
            if ((countShown == 1 && now.isBefore(savedDate.plusWeeks(1)))
                    || now.isBefore(savedDate.plusMonths(6))) {
                Log.i(
                        TAG,
                        ("shouldDisplayNotification(" + countKey + "): Notification discarded.")
                                + (" countShown=" + countShown)
                                + (" savedDate=" + savedDate));
                return false;
            }
        }

    private void autoOnUserAction(boolean disableAutoOn) {
        if (disableAutoOn) {
            getSystemService(BluetoothManager.class).getAdapter().setAutoOnEnabled(false);
        Settings.Secure.putInt(getContentResolver(), countKey, Math.min(3, countShown + 1));
        Settings.Secure.putString(getContentResolver(), dateKey, now.toString());
        Log.i(
                TAG,
                ("shouldDisplayNotification(" + countKey + "): Notification is being shown.")
                        + (" countShown=" + countShown)
                        + (" savedDate=" + savedDate));
        return true;
    }

    /** Process the tap on the Auto On popup action, that will disable the feature */
    private void autoOnUserAction() {
        getSystemService(BluetoothManager.class).getAdapter().setAutoOnEnabled(false);
        String tag = NOTIFICATION_TAG + "/" + AUTO_ON_BT_ENABLED_NOTIFICATION;
        getSystemService(NotificationManager.class)
                .cancel(tag, SystemMessage.ID.NOTE_BT_APM_NOTIFICATION_VALUE);
    }
}