1 /** 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.server.notification; 17 18 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; 19 20 import android.app.Notification; 21 import android.content.Context; 22 import android.util.Slog; 23 24 /** 25 * Determines whether a badge should be shown for this notification 26 */ 27 public class BadgeExtractor implements NotificationSignalExtractor { 28 private static final String TAG = "BadgeExtractor"; 29 private static final boolean DBG = false; 30 31 private RankingConfig mConfig; 32 initialize(Context ctx, NotificationUsageStats usageStats)33 public void initialize(Context ctx, NotificationUsageStats usageStats) { 34 if (DBG) Slog.d(TAG, "Initializing " + getClass().getSimpleName() + "."); 35 } 36 process(NotificationRecord record)37 public RankingReconsideration process(NotificationRecord record) { 38 if (record == null || record.getNotification() == null) { 39 if (DBG) Slog.d(TAG, "skipping empty notification"); 40 return null; 41 } 42 43 if (mConfig == null) { 44 if (DBG) Slog.d(TAG, "missing config"); 45 return null; 46 } 47 boolean userWantsBadges = mConfig.badgingEnabled(record.getSbn().getUser()); 48 boolean appCanShowBadge = 49 mConfig.canShowBadge(record.getSbn().getPackageName(), record.getSbn().getUid()); 50 if (!userWantsBadges || !appCanShowBadge) { 51 record.setShowBadge(false); 52 } else { 53 if (record.getChannel() != null) { 54 record.setShowBadge(record.getChannel().canShowBadge() && appCanShowBadge); 55 } else { 56 record.setShowBadge(appCanShowBadge); 57 } 58 } 59 60 if (record.isIntercepted() 61 && (record.getSuppressedVisualEffects() & SUPPRESSED_EFFECT_BADGE) != 0) { 62 record.setShowBadge(false); 63 } 64 65 Notification.BubbleMetadata metadata = record.getNotification().getBubbleMetadata(); 66 if (metadata != null && metadata.isNotificationSuppressed()) { 67 record.setShowBadge(false); 68 } 69 70 if (mConfig.isMediaNotificationFilteringEnabled()) { 71 final Notification notif = record.getNotification(); 72 if (notif.hasMediaSession()) { 73 if (notif.isStyle(Notification.DecoratedMediaCustomViewStyle.class) 74 || notif.isStyle(Notification.MediaStyle.class)) { 75 record.setShowBadge(false); 76 } 77 } 78 } 79 return null; 80 } 81 82 @Override setConfig(RankingConfig config)83 public void setConfig(RankingConfig config) { 84 mConfig = config; 85 } 86 87 @Override setZenHelper(ZenModeHelper helper)88 public void setZenHelper(ZenModeHelper helper) { 89 } 90 } 91