blob: 7dff2c1bdd23e184e1583d1cb289f002fe8d94ac [file] [log] [blame]
Chris Wrenf9536642014-04-17 10:01:54 -04001/*
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 */
16package com.android.server.notification;
17
18import java.util.Comparator;
19
20/**
Julia Reynolds0421e6d2016-01-08 09:51:24 -050021 * Sorts notifications individually into attention-relevant order.
Chris Wrenf9536642014-04-17 10:01:54 -040022 */
23public class NotificationComparator
Chris Wren333a61c2014-05-28 16:40:57 -040024 implements Comparator<NotificationRecord> {
Chris Wrenf9536642014-04-17 10:01:54 -040025
26 @Override
Chris Wren1031c972014-07-23 13:11:45 +000027 public int compare(NotificationRecord left, NotificationRecord right) {
Chris Wrenbdf33762015-12-04 15:50:51 -050028 final int leftImportance = left.getImportance();
29 final int rightImportance = right.getImportance();
30 if (leftImportance != rightImportance) {
Julia Reynoldsdbc114f2016-03-03 11:22:12 -050031 // by importance, high to low
Chris Wrenbdf33762015-12-04 15:50:51 -050032 return -1 * Integer.compare(leftImportance, rightImportance);
Chris Wrenf9536642014-04-17 10:01:54 -040033 }
Chris Wren1031c972014-07-23 13:11:45 +000034
Julia Reynoldsdbc114f2016-03-03 11:22:12 -050035 // Whether or not the notification can bypass DND.
Julia Reynolds0421e6d2016-01-08 09:51:24 -050036 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 Reynoldsdbc114f2016-03-03 11:22:12 -050043 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 Wren1031c972014-07-23 13:11:45 +000050 final float leftPeople = left.getContactAffinity();
51 final float rightPeople = right.getContactAffinity();
52 if (leftPeople != rightPeople) {
Chris Wrenf9536642014-04-17 10:01:54 -040053 // by contact proximity, close to far
Chris Wren1031c972014-07-23 13:11:45 +000054 return -1 * Float.compare(leftPeople, rightPeople);
Chris Wrenf9536642014-04-17 10:01:54 -040055 }
Chris Wren1031c972014-07-23 13:11:45 +000056
Chris Wrenf9536642014-04-17 10:01:54 -040057 // then break ties by time, most recent first
Chris Wren1031c972014-07-23 13:11:45 +000058 return -1 * Long.compare(left.getRankingTimeMs(), right.getRankingTimeMs());
Chris Wrenf9536642014-04-17 10:01:54 -040059 }
60}