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-relelvant order.
22 */
23public class NotificationComparator
24        implements Comparator<NotificationRecord> {
25
26    @Override
27    public int compare(NotificationRecord left, NotificationRecord right) {
28        final int leftPackagePriority = left.getPackagePriority();
29        final int rightPackagePriority = right.getPackagePriority();
30        if (leftPackagePriority != rightPackagePriority) {
31            // by priority, high to low
32            return -1 * Integer.compare(leftPackagePriority, rightPackagePriority);
33        }
34
35        final int leftScore = left.sbn.getScore();
36        final int rightScore = right.sbn.getScore();
37        if (leftScore != rightScore) {
38            // by priority, high to low
39            return -1 * Integer.compare(leftScore, rightScore);
40        }
41
42        final float leftPeople = left.getContactAffinity();
43        final float rightPeople = right.getContactAffinity();
44        if (leftPeople != rightPeople) {
45            // by contact proximity, close to far
46            return -1 * Float.compare(leftPeople, rightPeople);
47        }
48
49        // then break ties by time, most recent first
50        return -1 * Long.compare(left.getRankingTimeMs(), right.getRankingTimeMs());
51    }
52}
53