NotificationComparator.java revision 52b7a5a5973c05fe59b751b82ee357fdfc1c5ef7
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 leftScore = lhs.sbn.getScore();
32        final int rightScore = rhs.sbn.getScore();
33        if (leftScore != rightScore) {
34            // by priority, high to low
35            return -1 * Integer.compare(leftScore, rightScore);
36        }
37        final float leftPeple = lhs.getContactAffinity();
38        final float rightPeople = rhs.getContactAffinity();
39        if (leftPeple != rightPeople) {
40            // by contact proximity, close to far
41            return -1 * Float.compare(leftPeple, rightPeople);
42        }
43        // then break ties by time, most recent first
44        return -1 * Long.compare(lhs.getRankingTimeMs(), rhs.getRankingTimeMs());
45    }
46}
47