blob: 0681d95ad199c8bf97bccd105881a67c0dcce57d [file] [log] [blame]
Julia Reynolds924eed12017-01-19 09:52:07 -05001/**
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*/
16package com.android.server.notification;
17
Julia Reynoldsccc6ae62018-03-01 16:24:49 -050018import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
19
Robert Snoebergerdaf50b52020-06-18 21:40:30 -040020import android.app.Notification;
Julia Reynolds924eed12017-01-19 09:52:07 -050021import android.content.Context;
22import android.util.Slog;
23
24/**
25 * Determines whether a badge should be shown for this notification
26 */
27public class BadgeExtractor implements NotificationSignalExtractor {
28 private static final String TAG = "BadgeExtractor";
29 private static final boolean DBG = false;
30
31 private RankingConfig mConfig;
32
33 public void initialize(Context ctx, NotificationUsageStats usageStats) {
34 if (DBG) Slog.d(TAG, "Initializing " + getClass().getSimpleName() + ".");
35 }
36
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 }
Julia Reynolds24edc002020-01-29 16:35:32 -050047 boolean userWantsBadges = mConfig.badgingEnabled(record.getSbn().getUser());
Julia Reynolds924eed12017-01-19 09:52:07 -050048 boolean appCanShowBadge =
Julia Reynolds24edc002020-01-29 16:35:32 -050049 mConfig.canShowBadge(record.getSbn().getPackageName(), record.getSbn().getUid());
Chris Wren89aa2262017-05-05 18:05:56 -040050 if (!userWantsBadges || !appCanShowBadge) {
Julia Reynolds924eed12017-01-19 09:52:07 -050051 record.setShowBadge(false);
52 } else {
Julia Reynoldseb3dca72017-07-11 10:39:58 -040053 if (record.getChannel() != null) {
54 record.setShowBadge(record.getChannel().canShowBadge() && appCanShowBadge);
55 } else {
56 record.setShowBadge(appCanShowBadge);
57 }
Julia Reynolds924eed12017-01-19 09:52:07 -050058 }
59
Julia Reynoldsccc6ae62018-03-01 16:24:49 -050060 if (record.isIntercepted()
61 && (record.getSuppressedVisualEffects() & SUPPRESSED_EFFECT_BADGE) != 0) {
62 record.setShowBadge(false);
63 }
64
Lyn Han43fbcbd2020-06-03 17:43:30 -070065 Notification.BubbleMetadata metadata = record.getNotification().getBubbleMetadata();
66 if (metadata != null && metadata.isNotificationSuppressed()) {
67 record.setShowBadge(false);
68 }
Robert Snoebergerdaf50b52020-06-18 21:40:30 -040069
70 if (mConfig.isMediaNotificationFilteringEnabled()) {
71 final Notification notif = record.getNotification();
72 if (notif.hasMediaSession()) {
73 Class<? extends Notification.Style> notifStyle = notif.getNotificationStyle();
74 if (Notification.DecoratedMediaCustomViewStyle.class.equals(notifStyle)
75 || Notification.MediaStyle.class.equals(notifStyle)) {
76 record.setShowBadge(false);
77 }
78 }
79 }
Julia Reynolds924eed12017-01-19 09:52:07 -050080 return null;
81 }
82
83 @Override
84 public void setConfig(RankingConfig config) {
85 mConfig = config;
86 }
Julia Reynoldsc861a3d2018-02-15 10:34:49 -050087
88 @Override
89 public void setZenHelper(ZenModeHelper helper) {
Julia Reynoldsc861a3d2018-02-15 10:34:49 -050090 }
Julia Reynolds924eed12017-01-19 09:52:07 -050091}