Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 18 | import android.app.Notification; |
| 19 | import android.app.NotificationManager; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 24 | import android.telecom.TelecomManager; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 25 | |
Selim Cinek | 7d1009b | 2017-01-25 15:28:28 -0800 | [diff] [blame] | 26 | import com.android.internal.util.NotificationMessagingUtil; |
| 27 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 28 | import java.util.Comparator; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 29 | import java.util.Objects; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 30 | |
| 31 | /** |
Julia Reynolds | 0421e6d | 2016-01-08 09:51:24 -0500 | [diff] [blame] | 32 | * Sorts notifications individually into attention-relevant order. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 33 | */ |
| 34 | public class NotificationComparator |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 35 | implements Comparator<NotificationRecord> { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 36 | |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 37 | private final Context mContext; |
Selim Cinek | 7d1009b | 2017-01-25 15:28:28 -0800 | [diff] [blame] | 38 | private final NotificationMessagingUtil mMessagingUtil; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 39 | private String mDefaultPhoneApp; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 40 | |
| 41 | public NotificationComparator(Context context) { |
| 42 | mContext = context; |
| 43 | mContext.registerReceiver(mPhoneAppBroadcastReceiver, |
| 44 | new IntentFilter(TelecomManager.ACTION_DEFAULT_DIALER_CHANGED)); |
Selim Cinek | 7d1009b | 2017-01-25 15:28:28 -0800 | [diff] [blame] | 45 | mMessagingUtil = new NotificationMessagingUtil(mContext); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 48 | @Override |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 49 | public int compare(NotificationRecord left, NotificationRecord right) { |
Selim Cinek | 7b9605b | 2017-01-19 17:36:00 -0800 | [diff] [blame] | 50 | // first all colorized notifications |
Selim Cinek | c6c639cb | 2017-05-05 19:49:22 -0700 | [diff] [blame] | 51 | boolean leftImportantColorized = isImportantColorized(left); |
| 52 | boolean rightImportantColorized = isImportantColorized(right); |
Selim Cinek | 7b9605b | 2017-01-19 17:36:00 -0800 | [diff] [blame] | 53 | |
| 54 | if (leftImportantColorized != rightImportantColorized) { |
| 55 | return -1 * Boolean.compare(leftImportantColorized, rightImportantColorized); |
| 56 | } |
| 57 | |
| 58 | // sufficiently important ongoing notifications of certain categories |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 59 | boolean leftImportantOngoing = isImportantOngoing(left); |
| 60 | boolean rightImportantOngoing = isImportantOngoing(right); |
| 61 | |
| 62 | if (leftImportantOngoing != rightImportantOngoing) { |
| 63 | // by ongoing, ongoing higher than non-ongoing |
| 64 | return -1 * Boolean.compare(leftImportantOngoing, rightImportantOngoing); |
| 65 | } |
| 66 | |
Selim Cinek | 7d1009b | 2017-01-25 15:28:28 -0800 | [diff] [blame] | 67 | boolean leftMessaging = isImportantMessaging(left); |
| 68 | boolean rightMessaging = isImportantMessaging(right); |
| 69 | if (leftMessaging != rightMessaging) { |
| 70 | return -1 * Boolean.compare(leftMessaging, rightMessaging); |
| 71 | } |
| 72 | |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 73 | // Next: sufficiently import person to person communication |
Selim Cinek | 7d1009b | 2017-01-25 15:28:28 -0800 | [diff] [blame] | 74 | boolean leftPeople = isImportantPeople(left); |
| 75 | boolean rightPeople = isImportantPeople(right); |
Julia Reynolds | 2445037 | 2017-05-02 15:04:34 -0400 | [diff] [blame] | 76 | final int contactAffinityComparison = |
| 77 | Float.compare(left.getContactAffinity(), right.getContactAffinity()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 78 | |
| 79 | if (leftPeople && rightPeople){ |
| 80 | // by contact proximity, close to far. if same proximity, check further fields. |
Julia Reynolds | 2445037 | 2017-05-02 15:04:34 -0400 | [diff] [blame] | 81 | if (contactAffinityComparison != 0) { |
| 82 | return -1 * contactAffinityComparison; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 83 | } |
| 84 | } else if (leftPeople != rightPeople) { |
| 85 | // People, messaging higher than non-messaging |
| 86 | return -1 * Boolean.compare(leftPeople, rightPeople); |
| 87 | } |
| 88 | |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 89 | final int leftImportance = left.getImportance(); |
| 90 | final int rightImportance = right.getImportance(); |
| 91 | if (leftImportance != rightImportance) { |
Julia Reynolds | dbc114f | 2016-03-03 11:22:12 -0500 | [diff] [blame] | 92 | // by importance, high to low |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 93 | return -1 * Integer.compare(leftImportance, rightImportance); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 94 | } |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 95 | |
Julia Reynolds | 2445037 | 2017-05-02 15:04:34 -0400 | [diff] [blame] | 96 | // by contact proximity, close to far. if same proximity, check further fields. |
| 97 | if (contactAffinityComparison != 0) { |
| 98 | return -1 * contactAffinityComparison; |
| 99 | } |
| 100 | |
Julia Reynolds | dbc114f | 2016-03-03 11:22:12 -0500 | [diff] [blame] | 101 | // Whether or not the notification can bypass DND. |
Julia Reynolds | 0421e6d | 2016-01-08 09:51:24 -0500 | [diff] [blame] | 102 | final int leftPackagePriority = left.getPackagePriority(); |
| 103 | final int rightPackagePriority = right.getPackagePriority(); |
| 104 | if (leftPackagePriority != rightPackagePriority) { |
| 105 | // by priority, high to low |
| 106 | return -1 * Integer.compare(leftPackagePriority, rightPackagePriority); |
| 107 | } |
| 108 | |
Julia Reynolds | dbc114f | 2016-03-03 11:22:12 -0500 | [diff] [blame] | 109 | final int leftPriority = left.sbn.getNotification().priority; |
| 110 | final int rightPriority = right.sbn.getNotification().priority; |
| 111 | if (leftPriority != rightPriority) { |
| 112 | // by priority, high to low |
| 113 | return -1 * Integer.compare(leftPriority, rightPriority); |
| 114 | } |
| 115 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 116 | // then break ties by time, most recent first |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 117 | return -1 * Long.compare(left.getRankingTimeMs(), right.getRankingTimeMs()); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 118 | } |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 119 | |
Selim Cinek | c6c639cb | 2017-05-05 19:49:22 -0700 | [diff] [blame] | 120 | private boolean isImportantColorized(NotificationRecord record) { |
Selim Cinek | 7b9605b | 2017-01-19 17:36:00 -0800 | [diff] [blame] | 121 | if (record.getImportance() < NotificationManager.IMPORTANCE_LOW) { |
| 122 | return false; |
| 123 | } |
| 124 | return record.getNotification().isColorized(); |
| 125 | } |
| 126 | |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 127 | private boolean isImportantOngoing(NotificationRecord record) { |
| 128 | if (!isOngoing(record)) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | if (record.getImportance() < NotificationManager.IMPORTANCE_LOW) { |
| 133 | return false; |
| 134 | } |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 135 | |
| 136 | return isCall(record) || isMediaNotification(record); |
| 137 | } |
| 138 | |
Selim Cinek | 7d1009b | 2017-01-25 15:28:28 -0800 | [diff] [blame] | 139 | protected boolean isImportantPeople(NotificationRecord record) { |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 140 | if (record.getImportance() < NotificationManager.IMPORTANCE_LOW) { |
| 141 | return false; |
| 142 | } |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 143 | if (record.getContactAffinity() > ValidateNotificationPeople.NONE) { |
| 144 | return true; |
| 145 | } |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | |
Selim Cinek | 7d1009b | 2017-01-25 15:28:28 -0800 | [diff] [blame] | 149 | protected boolean isImportantMessaging(NotificationRecord record) { |
| 150 | return mMessagingUtil.isImportantMessaging(record.sbn, record.getImportance()); |
| 151 | } |
| 152 | |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 153 | private boolean isOngoing(NotificationRecord record) { |
Julia Reynolds | 889280c | 2017-04-12 12:27:00 -0400 | [diff] [blame] | 154 | final int ongoingFlags = Notification.FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 155 | return (record.getNotification().flags & ongoingFlags) != 0; |
| 156 | } |
| 157 | |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 158 | private boolean isMediaNotification(NotificationRecord record) { |
Selim Cinek | 9910483 | 2017-01-25 14:47:33 -0800 | [diff] [blame] | 159 | return record.getNotification().hasMediaSession(); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private boolean isCall(NotificationRecord record) { |
Julia Reynolds | 472f01c | 2018-01-18 09:25:44 -0500 | [diff] [blame] | 163 | return record.isCategory(Notification.CATEGORY_CALL) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 164 | && isDefaultPhoneApp(record.sbn.getPackageName()); |
| 165 | } |
| 166 | |
| 167 | private boolean isDefaultPhoneApp(String pkg) { |
| 168 | if (mDefaultPhoneApp == null) { |
| 169 | final TelecomManager telecomm = |
| 170 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
| 171 | mDefaultPhoneApp = telecomm != null ? telecomm.getDefaultDialerPackage() : null; |
| 172 | } |
| 173 | return Objects.equals(pkg, mDefaultPhoneApp); |
| 174 | } |
| 175 | |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 176 | private final BroadcastReceiver mPhoneAppBroadcastReceiver = new BroadcastReceiver() { |
| 177 | @Override |
| 178 | public void onReceive(Context context, Intent intent) { |
| 179 | mDefaultPhoneApp = |
| 180 | intent.getStringExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME); |
| 181 | } |
| 182 | }; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 183 | } |