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

Commit bea7701f authored by Steve Kondik's avatar Steve Kondik Committed by Steve Kondik
Browse files

lights: Automatically generate an LED color for notifications

 * When an app doesn't specify a color for the LED, as is the usual
   case, we currently just show the default color. This is boring.
   It's also a pain in the ass to manually map all your apps to
   colors.
 * Use our new helpers to generate this color based on the application's
   icon color. Keep a cache to avoid wasting cycles.

Change-Id: I7288e52499819a6a6c75ed9d9ba7cfa1b13c5669
parent e9227ac2
Loading
Loading
Loading
Loading
+45 −1
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ import android.content.pm.ParceledListSlice;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.graphics.drawable.Drawable;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.AudioManagerInternal;
@@ -124,6 +125,7 @@ import com.android.server.notification.ManagedServices.UserProfiles;
import com.android.server.statusbar.StatusBarManagerInternal;

import cyanogenmod.providers.CMSettings;
import cyanogenmod.util.ColorUtils;

import libcore.io.IoUtils;

@@ -248,6 +250,8 @@ public class NotificationManagerService extends SystemService {
    private boolean mMultipleNotificationLeds;
    private boolean mMultipleLedsEnabledSetting = false;

    private boolean mAutoGenerateNotificationColor = true;

    private boolean mScreenOnEnabled = false;
    private boolean mScreenOnDefault = false;

@@ -287,6 +291,8 @@ public class NotificationManagerService extends SystemService {
    private boolean mNotificationPulseEnabled;
    private HashMap<String, NotificationLedValues> mNotificationPulseCustomLedValues;
    private Map<String, String> mPackageNameMappings;
    private final Map<String, Integer> mGeneratedPackageLedColors =
        new HashMap<String, Integer>();

    // for checking lockscreen status
    private KeyguardManager mKeyguardManager;
@@ -1001,6 +1007,9 @@ public class NotificationManagerService extends SystemService {
            resolver.registerContentObserver(CMSettings.Global.getUriFor(
                    CMSettings.Global.ZEN_DISABLE_DUCKING_DURING_MEDIA_PLAYBACK), false,
                    this, UserHandle.USER_ALL);
            resolver.registerContentObserver(CMSettings.System.getUriFor(
                    CMSettings.System.NOTIFICATION_LIGHT_COLOR_AUTO), false,
                    this, UserHandle.USER_ALL);
            if (mAdjustableNotificationLedBrightness) {
                resolver.registerContentObserver(CMSettings.System.getUriFor(
                        CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL),
@@ -1025,6 +1034,11 @@ public class NotificationManagerService extends SystemService {
            mNotificationPulseEnabled = Settings.System.getIntForUser(resolver,
                    Settings.System.NOTIFICATION_LIGHT_PULSE, 0, UserHandle.USER_CURRENT) != 0;

            // Automatically pick a color for LED if not set
            mAutoGenerateNotificationColor = CMSettings.System.getIntForUser(resolver,
                    CMSettings.System.NOTIFICATION_LIGHT_COLOR_AUTO,
                    1, UserHandle.USER_CURRENT) != 0;

            // LED default color
            mDefaultNotificationColor = CMSettings.System.getIntForUser(resolver,
                    CMSettings.System.NOTIFICATION_LIGHT_PULSE_DEFAULT_COLOR,
@@ -1040,6 +1054,9 @@ public class NotificationManagerService extends SystemService {
                    CMSettings.System.NOTIFICATION_LIGHT_PULSE_DEFAULT_LED_OFF,
                    mDefaultNotificationLedOff, UserHandle.USER_CURRENT);

            // LED generated notification colors
            mGeneratedPackageLedColors.clear();

            // LED custom notification colors
            mNotificationPulseCustomLedValues.clear();
            if (CMSettings.System.getIntForUser(resolver,
@@ -3433,7 +3450,7 @@ public class NotificationManagerService extends SystemService {
                ledOnMS = ledValues.onMS >= 0 ? ledValues.onMS : mDefaultNotificationLedOn;
                ledOffMS = ledValues.offMS >= 0 ? ledValues.offMS : mDefaultNotificationLedOff;
            } else if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {
                ledARGB = mDefaultNotificationColor;
                ledARGB = generateLedColorForNotification(ledNotification);
                ledOnMS = mDefaultNotificationLedOn;
                ledOffMS = mDefaultNotificationLedOff;
            } else {
@@ -3494,6 +3511,33 @@ public class NotificationManagerService extends SystemService {
        return mNotificationPulseCustomLedValues.get(mapPackage(packageName));
    }

    private int generateLedColorForNotification(NotificationRecord ledNotification) {
        if (!mAutoGenerateNotificationColor) {
            return mDefaultNotificationColor;
        }
        final String packageName = ledNotification.sbn.getPackageName();
        final String mapping = mapPackage(packageName);
        int color = mDefaultNotificationColor;

        if (mGeneratedPackageLedColors.containsKey(mapping)) {
            return mGeneratedPackageLedColors.get(mapping);
        }

        PackageManager pm = getContext().getPackageManager();
        Drawable icon;
        try {
            icon = pm.getApplicationIcon(mapping);
        } catch (NameNotFoundException e) {
            Slog.e(TAG, e.getMessage(), e);
            return color;
        }

        color = ColorUtils.generateAlertColorFromDrawable(icon);
        mGeneratedPackageLedColors.put(mapping, color);

        return color;
    }

    private String mapPackage(String pkg) {
        if (!mPackageNameMappings.containsKey(pkg)) {
            return pkg;