NotificationComparator.java revision 5190c0fefac9923931b5c1c02cab2d00c2d6b82b
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<NotificationManagerService.NotificationRecord> {
25
26    @Override
27    public int compare(NotificationManagerService.NotificationRecord lhs,
28            NotificationManagerService.NotificationRecord rhs) {
29        if (lhs.isRecentlyIntrusive() != rhs.isRecentlyIntrusive()) {
30            return lhs.isRecentlyIntrusive() ? -1 : 1;
31        }
32        final int leftScore = lhs.sbn.getScore();
33        final int rightScore = rhs.sbn.getScore();
34        if (leftScore != rightScore) {
35            // by priority, high to low
36            return -1 * Integer.compare(leftScore, rightScore);
37        }
38        final float leftPeple = lhs.getContactAffinity();
39        final float rightPeople = rhs.getContactAffinity();
40        if (leftPeple != rightPeople) {
41            // by contact proximity, close to far
42            return -1 * Float.compare(leftPeple, rightPeople);
43        }
44        // then break ties by time, most recent first
45        return -1 * Long.compare(lhs.sbn.getPostTime(), rhs.sbn.getPostTime());
46    }
47}
48