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