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 */
16package com.android.server.notification;
17
18import java.util.Comparator;
19
20/**
21 * Sorts notifications individually into attention-relevant order.
22 */
23public class NotificationComparator
24        implements Comparator<NotificationRecord> {
25
26    @Override
27    public int compare(NotificationRecord left, NotificationRecord right) {
28        final int leftImportance = left.getImportance();
29        final int rightImportance = right.getImportance();
30        if (leftImportance != rightImportance) {
31            // by importance, high to low
32            return -1 * Integer.compare(leftImportance, rightImportance);
33        }
34
35        // Whether or not the notification can bypass DND.
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
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
50        final float leftPeople = left.getContactAffinity();
51        final float rightPeople = right.getContactAffinity();
52        if (leftPeople != rightPeople) {
53            // by contact proximity, close to far
54            return -1 * Float.compare(leftPeople, rightPeople);
55        }
56
57        // then break ties by time, most recent first
58        return -1 * Long.compare(left.getRankingTimeMs(), right.getRankingTimeMs());
59    }
60}
61