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