blob: d323d80955251df881211698c71cb47f63557bf1 [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
Julia Reynolds924eed12017-01-19 09:52:07 -050020import android.content.Context;
21import android.util.Slog;
Lyn Han43fbcbd2020-06-03 17:43:30 -070022import android.app.Notification;
Julia Reynolds924eed12017-01-19 09:52:07 -050023
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 }
Julia Reynolds924eed12017-01-19 09:52:07 -050069 return null;
70 }
71
72 @Override
73 public void setConfig(RankingConfig config) {
74 mConfig = config;
75 }
Julia Reynoldsc861a3d2018-02-15 10:34:49 -050076
77 @Override
78 public void setZenHelper(ZenModeHelper helper) {
Julia Reynoldsc861a3d2018-02-15 10:34:49 -050079 }
Julia Reynolds924eed12017-01-19 09:52:07 -050080}