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

Unverified Commit 85853878 authored by LuK1337's avatar LuK1337 Committed by Michael Bestas
Browse files

core: Move Lineage notification LED code to NotificationAttentionHelper

14 QRP3 enabled refactor_attention_helper flag.

Change-Id: I1fe99c53f7cfa2b10828f72d092bbcc46321ef78
parent 493b2c77
Loading
Loading
Loading
Loading
+70 −19
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 * Copyright (C) 2015 The CyanogenMod Project
 * Copyright (C) 2017 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -73,6 +75,9 @@ import com.android.server.EventLogTags;
import com.android.server.lights.LightsManager;
import com.android.server.lights.LogicalLight;

import org.lineageos.internal.notification.LedValues;
import org.lineageos.internal.notification.LineageNotificationLights;

import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -153,6 +158,7 @@ public final class NotificationAttentionHelper {
    private final AudioAttributes mInCallNotificationAudioAttributes;
    private final float mInCallNotificationVolume;
    private Binder mCallNotificationToken = null;
    private LineageNotificationLights mLineageNotificationLights;

    // Settings flags
    private boolean mNotificationCooldownEnabled;
@@ -205,6 +211,22 @@ public final class NotificationAttentionHelper {
                .build();
        mInCallNotificationVolume = resources.getFloat(R.dimen.config_inCallNotificationVolume);

        mLineageNotificationLights = new LineageNotificationLights(mContext,
                 new LineageNotificationLights.LedUpdater() {
            public void update() {
                updateLightsLocked();
            }
        });

        mZenModeHelper.addCallback(new ZenModeHelper.Callback() {
            @Override
            void onZenModeChanged() {
                Binder.withCleanCallingIdentity(() -> {
                    mLineageNotificationLights.setZenMode(mZenModeHelper.getZenMode());
                });
            }
        });

        if (Flags.politeNotifications()) {
            mStrategy = createPolitenessStrategy();
        } else {
@@ -809,10 +831,12 @@ public final class NotificationAttentionHelper {
    }

    private void clearLightsLocked() {
        // light
        // clear only if lockscreen is not active
        if (!mLineageNotificationLights.isKeyguardLocked()) {
            mLights.clear();
            updateLightsLocked();
        }
    }

    public void clearEffectsLocked(String key) {
        if (key.equals(mSoundNotificationKey)) {
@@ -849,25 +873,57 @@ public final class NotificationAttentionHelper {
            }
        }

        // Don't flash while we are in a call or screen is on
        if (ledNotification == null || isInCall() || mScreenOn) {
        NotificationRecord.Light light = ledNotification != null ?
                ledNotification.getLight() : null;
        if (ledNotification == null || mLineageNotificationLights == null || light == null) {
            mNotificationLight.turnOff();
            return;
        }

        int ledColor = light.color;
        if (isLedForcedOn(ledNotification) && ledColor == 0) {
            // User has requested color 0.  However, lineage-sdk interprets
            // color 0 as "supply a default" therefore adjust alpha to make
            // the color still black but non-zero.
            ledColor = 0x01000000;
        }

        LedValues ledValues = new LedValues(ledColor, light.onMs, light.offMs);
        mLineageNotificationLights.calcLights(ledValues, ledNotification.getSbn().getPackageName(),
                ledNotification.getSbn().getNotification(), mScreenOn || isInCall(),
                ledNotification.getSuppressedVisualEffects());

        if (!ledValues.isEnabled()) {
            mNotificationLight.turnOff();
        } else {
            mNotificationLight.setModes(ledValues.getBrightness());

            // we are using 1:0 to indicate LED should stay always on
            if (ledValues.getOnMs() == 1 && ledValues.getOffMs() == 0) {
                mNotificationLight.setColor(ledValues.getColor());
            } else {
            NotificationRecord.Light light = ledNotification.getLight();
            if (light != null && mNotificationPulseEnabled) {
                // pulse repeatedly
                mNotificationLight.setFlashing(light.color, LogicalLight.LIGHT_FLASH_TIMED,
                        light.onMs, light.offMs);
                mNotificationLight.setFlashing(ledValues.getColor(),
                        LogicalLight.LIGHT_FLASH_TIMED, ledValues.getOnMs(), ledValues.getOffMs());
            }
        }
    }

    private boolean isLedForcedOn(NotificationRecord nr) {
        return nr != null && mLineageNotificationLights.isForcedOn(nr.getSbn().getNotification());
    }

    boolean canShowLightsLocked(final NotificationRecord record, final Signals signals,
            boolean aboveThreshold) {
        // device lacks light
        if (!mHasLight) {
            return false;
        }
        // Forced on
        // Used by LineageParts light picker
        // eg to allow selecting battery light color when notification led is turned off.
        if (isLedForcedOn(record)) {
            return true;
        }
        // user turned lights off globally
        if (!mNotificationPulseEnabled) {
            return false;
@@ -880,10 +936,6 @@ public final class NotificationAttentionHelper {
        if (!aboveThreshold) {
            return false;
        }
        // suppressed due to DND
        if ((record.getSuppressedVisualEffects() & SUPPRESSED_EFFECT_LIGHTS) != 0) {
            return false;
        }
        // Suppressed because it's a silent update
        final Notification notification = record.getNotification();
        if (record.isUpdate && (notification.flags & FLAG_ONLY_ALERT_ONCE) != 0) {
@@ -893,10 +945,6 @@ public final class NotificationAttentionHelper {
        if (record.getSbn().isGroup() && record.getNotification().suppressAlertingDueToGrouping()) {
            return false;
        }
        // not if in call
        if (isInCall()) {
            return false;
        }
        // check current user
        if (!isNotificationForCurrentUser(record, signals)) {
            return false;
@@ -1464,8 +1512,11 @@ public final class NotificationAttentionHelper {
                mUserPresent = true;
                // turn off LED when user passes through lock screen
                if (mNotificationLight != null) {
                    // if lights with screen on is disabled.
                    if (!mLineageNotificationLights.showLightsScreenOn()) {
                        mNotificationLight.turnOff();
                    }
                }
            } else if (action.equals(Intent.ACTION_USER_ADDED)
                        || action.equals(Intent.ACTION_USER_REMOVED)
                        || action.equals(Intent.ACTION_USER_SWITCHED)
+18 −62
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 The Android Open Source Project
 * Copyright (C) 2015 The CyanogenMod Project
 * Copyright (C) 2017 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -372,9 +370,6 @@ import com.android.server.wm.ActivityTaskManagerInternal;
import com.android.server.wm.BackgroundActivityStartCallback;
import com.android.server.wm.WindowManagerInternal;
import org.lineageos.internal.notification.LedValues;
import org.lineageos.internal.notification.LineageNotificationLights;
import libcore.io.IoUtils;
import org.json.JSONException;
@@ -781,8 +776,6 @@ public class NotificationManagerService extends SystemService {
    // Broadcast intent receiver for notification permissions review-related intents
    private ReviewNotificationPermissionsReceiver mReviewNotificationPermissionsReceiver;
    private LineageNotificationLights mLineageNotificationLights;
    static class Archive {
        final SparseArray<Boolean> mEnabled;
        final int mBufferSize;
@@ -1859,12 +1852,9 @@ public class NotificationManagerService extends SystemService {
    @GuardedBy("mNotificationLock")
    private void clearLightsLocked() {
        // light
        // clear only if lockscreen is not active
        if (!mLineageNotificationLights.isKeyguardLocked()) {
        mLights.clear();
        updateLightsLocked();
    }
    }
    @GuardedBy("mNotificationLock")
    private void clearEffectsLocked(String key) {
@@ -2085,12 +2075,10 @@ public class NotificationManagerService extends SystemService {
                } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
                    // turn off LED when user passes through lock screen
                    if (mNotificationLight != null) {
                        if (!mLineageNotificationLights.showLightsScreenOn()) {
                        mNotificationLight.turnOff();
                    }
                }
            }
            }
            if (action.equals(Intent.ACTION_USER_STOPPED)) {
                int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
@@ -2585,7 +2573,6 @@ public class NotificationManagerService extends SystemService {
                            new Intent(ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL)
                                    .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT),
                            UserHandle.ALL, permission.MANAGE_NOTIFICATIONS);
                    mLineageNotificationLights.setZenMode(mZenModeHelper.getZenMode());
                    synchronized (mNotificationLock) {
                        updateInterruptionFilterLocked();
                    }
@@ -2890,13 +2877,6 @@ public class NotificationManagerService extends SystemService {
                getContext().getSystemService(PowerManager.class),
                new PostNotificationTrackerFactory() {});
        mLineageNotificationLights = new LineageNotificationLights(getContext(),
                 new LineageNotificationLights.LedUpdater() {
            public void update() {
                updateNotificationPulse();
            }
        });
        publishBinderService(Context.NOTIFICATION_SERVICE, mService, /* allowIsolated= */ false,
                DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL);
        publishBinderService(Context.NOTIFICATION_SERVICE, mService);
@@ -9365,12 +9345,6 @@ public class NotificationManagerService extends SystemService {
        if (!mHasLight) {
            return false;
        }
        // Forced on
        // Used by LineageParts light picker
        // eg to allow selecting battery light color when notification led is turned off.
        if (isLedForcedOn(record)) {
            return true;
        }
        // user turned lights off globally
        if (!mNotificationPulseEnabled) {
            return false;
@@ -9383,6 +9357,10 @@ public class NotificationManagerService extends SystemService {
        if (!aboveThreshold) {
            return false;
        }
        // suppressed due to DND
        if ((record.getSuppressedVisualEffects() & SUPPRESSED_EFFECT_LIGHTS) != 0) {
            return false;
        }
        // Suppressed because it's a silent update
        final Notification notification = record.getNotification();
        if (record.isUpdate && (notification.flags & FLAG_ONLY_ALERT_ONCE) != 0) {
@@ -9392,6 +9370,10 @@ public class NotificationManagerService extends SystemService {
        if (record.getSbn().isGroup() && record.getNotification().suppressAlertingDueToGrouping()) {
            return false;
        }
        // not if in call
        if (isInCall()) {
            return false;
        }
        // check current user
        if (!isNotificationForCurrentUser(record)) {
            return false;
@@ -10811,43 +10793,17 @@ public class NotificationManagerService extends SystemService {
            }
        }
        NotificationRecord.Light light = ledNotification != null ?
                ledNotification.getLight() : null;
        if (ledNotification == null || mLineageNotificationLights == null || light == null) {
        // Don't flash while we are in a call or screen is on
        if (ledNotification == null || isInCall() || mScreenOn) {
            mNotificationLight.turnOff();
            return;
        }
        int ledColor = light.color;
        if (isLedForcedOn(ledNotification) && ledColor == 0) {
            // User has requested color 0.  However, lineage-sdk interprets
            // color 0 as "supply a default" therefore adjust alpha to make
            // the color still black but non-zero.
            ledColor = 0x01000000;
        }
        LedValues ledValues = new LedValues(ledColor, light.onMs, light.offMs);
        mLineageNotificationLights.calcLights(ledValues, ledNotification.getSbn().getPackageName(),
                ledNotification.getSbn().getNotification(), mScreenOn || isInCall(),
                ledNotification.getSuppressedVisualEffects());
        if (!ledValues.isEnabled()) {
            mNotificationLight.turnOff();
        } else {
            mNotificationLight.setModes(ledValues.getBrightness());
            // we are using 1:0 to indicate LED should stay always on
            if (ledValues.getOnMs() == 1 && ledValues.getOffMs() == 0) {
                mNotificationLight.setColor(ledValues.getColor());
        } else {
                mNotificationLight.setFlashing(ledValues.getColor(),
                        LogicalLight.LIGHT_FLASH_TIMED, ledValues.getOnMs(), ledValues.getOffMs());
            }
            NotificationRecord.Light light = ledNotification.getLight();
            if (light != null && mNotificationPulseEnabled) {
                // pulse repeatedly
                mNotificationLight.setFlashing(light.color, LogicalLight.LIGHT_FLASH_TIMED,
                        light.onMs, light.offMs);
            }
        }
    private boolean isLedForcedOn(NotificationRecord nr) {
        return nr != null && mLineageNotificationLights.isForcedOn(nr.getSbn().getNotification());
    }
    @GuardedBy("mNotificationLock")