Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 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 | |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 18 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; |
| 19 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.util.Slog; |
Lyn Han | 43fbcbd | 2020-06-03 17:43:30 -0700 | [diff] [blame] | 22 | import android.app.Notification; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 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 | |
| 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 Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 47 | boolean userWantsBadges = mConfig.badgingEnabled(record.getSbn().getUser()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 48 | boolean appCanShowBadge = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 49 | mConfig.canShowBadge(record.getSbn().getPackageName(), record.getSbn().getUid()); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 50 | if (!userWantsBadges || !appCanShowBadge) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 51 | record.setShowBadge(false); |
| 52 | } else { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 53 | if (record.getChannel() != null) { |
| 54 | record.setShowBadge(record.getChannel().canShowBadge() && appCanShowBadge); |
| 55 | } else { |
| 56 | record.setShowBadge(appCanShowBadge); |
| 57 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 60 | if (record.isIntercepted() |
| 61 | && (record.getSuppressedVisualEffects() & SUPPRESSED_EFFECT_BADGE) != 0) { |
| 62 | record.setShowBadge(false); |
| 63 | } |
| 64 | |
Lyn Han | 43fbcbd | 2020-06-03 17:43:30 -0700 | [diff] [blame] | 65 | Notification.BubbleMetadata metadata = record.getNotification().getBubbleMetadata(); |
| 66 | if (metadata != null && metadata.isNotificationSuppressed()) { |
| 67 | record.setShowBadge(false); |
| 68 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 69 | return null; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void setConfig(RankingConfig config) { |
| 74 | mConfig = config; |
| 75 | } |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public void setZenHelper(ZenModeHelper helper) { |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 79 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 80 | } |