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 | |
| 18 | import java.util.Comparator; |
| 19 | |
| 20 | /** |
Julia Reynolds | 0421e6d | 2016-01-08 09:51:24 -0500 | [diff] [blame] | 21 | * Sorts notifications individually into attention-relevant order. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 22 | */ |
| 23 | public class NotificationComparator |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 24 | implements Comparator<NotificationRecord> { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 25 | |
| 26 | @Override |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 27 | public int compare(NotificationRecord left, NotificationRecord right) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 28 | final int leftImportance = left.getImportance(); |
| 29 | final int rightImportance = right.getImportance(); |
| 30 | if (leftImportance != rightImportance) { |
Julia Reynolds | dbc114f | 2016-03-03 11:22:12 -0500 | [diff] [blame] | 31 | // by importance, high to low |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 32 | return -1 * Integer.compare(leftImportance, rightImportance); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 33 | } |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 34 | |
Julia Reynolds | dbc114f | 2016-03-03 11:22:12 -0500 | [diff] [blame] | 35 | // Whether or not the notification can bypass DND. |
Julia Reynolds | 0421e6d | 2016-01-08 09:51:24 -0500 | [diff] [blame] | 36 | final int leftPackagePriority = left.getPackagePriority(); |
| 37 | final int rightPackagePriority = right.getPackagePriority(); |
| 38 | if (leftPackagePriority != rightPackagePriority) { |
| 39 | // by priority, high to low |
| 40 | return -1 * Integer.compare(leftPackagePriority, rightPackagePriority); |
| 41 | } |
| 42 | |
Julia Reynolds | dbc114f | 2016-03-03 11:22:12 -0500 | [diff] [blame] | 43 | final int leftPriority = left.sbn.getNotification().priority; |
| 44 | final int rightPriority = right.sbn.getNotification().priority; |
| 45 | if (leftPriority != rightPriority) { |
| 46 | // by priority, high to low |
| 47 | return -1 * Integer.compare(leftPriority, rightPriority); |
| 48 | } |
| 49 | |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 50 | final float leftPeople = left.getContactAffinity(); |
| 51 | final float rightPeople = right.getContactAffinity(); |
| 52 | if (leftPeople != rightPeople) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 53 | // by contact proximity, close to far |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 54 | return -1 * Float.compare(leftPeople, rightPeople); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 55 | } |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 56 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 57 | // then break ties by time, most recent first |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 58 | return -1 * Long.compare(left.getRankingTimeMs(), right.getRankingTimeMs()); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 59 | } |
| 60 | } |