OSDN Git Service

lights: Automatically generate an LED color for notifications
[android-x86/frameworks-base.git] / services / core / java / com / android / server / notification / NotificationManagerService.java
index 2f1ffa1..a15d7b1 100644 (file)
@@ -80,6 +80,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;
@@ -147,6 +148,7 @@ import com.android.server.vr.VrManagerInternal;
 import com.android.server.notification.ManagedServices.UserProfiles;
 
 import cyanogenmod.providers.CMSettings;
+import cyanogenmod.util.ColorUtils;
 
 import cyanogenmod.providers.CMSettings;
 import libcore.io.IoUtils;
@@ -262,6 +264,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;
 
@@ -286,6 +290,8 @@ public class NotificationManagerService extends SystemService {
     private boolean mNotificationPulseEnabled;
     private ArrayMap<String, NotificationLedValues> mNotificationPulseCustomLedValues;
     private Map<String, String> mPackageNameMappings;
+    private final ArrayMap<String, Integer> mGeneratedPackageLedColors =
+        new ArrayMap<String, Integer>();
 
     // for checking lockscreen status
     private KeyguardManager mKeyguardManager;
@@ -324,6 +330,8 @@ public class NotificationManagerService extends SystemService {
     private ConditionProviders mConditionProviders;
     private NotificationUsageStats mUsageStats;
 
+    private boolean mMultiColorNotificationLed;
+
     private static final int MY_UID = Process.myUid();
     private static final int MY_PID = Process.myPid();
     private RankingHandler mRankingHandler;
@@ -892,6 +900,9 @@ public class NotificationManagerService extends SystemService {
             resolver.registerContentObserver(CMSettings.System.getUriFor(
                     CMSettings.System.NOTIFICATION_LIGHT_SCREEN_ON),
                     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),
@@ -916,6 +927,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,
@@ -931,6 +947,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,
@@ -1104,6 +1123,9 @@ public class NotificationManagerService extends SystemService {
         mDefaultNotificationLedOff = resources.getInteger(
                 R.integer.config_defaultNotificationLedOff);
 
+        mMultiColorNotificationLed = resources.getBoolean(
+                R.bool.config_multiColorNotificationLed);
+
         mNotificationPulseCustomLedValues = new ArrayMap<String, NotificationLedValues>();
 
         mPackageNameMappings = new ArrayMap<String, String>();
@@ -3747,7 +3769,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 {
@@ -3809,6 +3831,36 @@ public class NotificationManagerService extends SystemService {
         return mNotificationPulseCustomLedValues.get(mapPackage(packageName));
     }
 
+    private int generateLedColorForNotification(NotificationRecord ledNotification) {
+        if (!mAutoGenerateNotificationColor) {
+            return mDefaultNotificationColor;
+        }
+        if (!mMultiColorNotificationLed) {
+            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;