1f953664dc17dca23bd724bd64f89189c16c83263Chris Wren/*
2f953664dc17dca23bd724bd64f89189c16c83263Chris Wren * Copyright (C) 2014 The Android Open Source Project
3f953664dc17dca23bd724bd64f89189c16c83263Chris Wren *
4f953664dc17dca23bd724bd64f89189c16c83263Chris Wren * Licensed under the Apache License, Version 2.0 (the "License");
5f953664dc17dca23bd724bd64f89189c16c83263Chris Wren * you may not use this file except in compliance with the License.
6f953664dc17dca23bd724bd64f89189c16c83263Chris Wren * You may obtain a copy of the License at
7f953664dc17dca23bd724bd64f89189c16c83263Chris Wren *
8f953664dc17dca23bd724bd64f89189c16c83263Chris Wren *      http://www.apache.org/licenses/LICENSE-2.0
9f953664dc17dca23bd724bd64f89189c16c83263Chris Wren *
10f953664dc17dca23bd724bd64f89189c16c83263Chris Wren * Unless required by applicable law or agreed to in writing, software
11f953664dc17dca23bd724bd64f89189c16c83263Chris Wren * distributed under the License is distributed on an "AS IS" BASIS,
12f953664dc17dca23bd724bd64f89189c16c83263Chris Wren * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f953664dc17dca23bd724bd64f89189c16c83263Chris Wren * See the License for the specific language governing permissions and
14f953664dc17dca23bd724bd64f89189c16c83263Chris Wren * limitations under the License.
15f953664dc17dca23bd724bd64f89189c16c83263Chris Wren */
16f953664dc17dca23bd724bd64f89189c16c83263Chris Wrenpackage com.android.server.notification;
17f953664dc17dca23bd724bd64f89189c16c83263Chris Wren
18f953664dc17dca23bd724bd64f89189c16c83263Chris Wrenimport java.util.Comparator;
19f953664dc17dca23bd724bd64f89189c16c83263Chris Wren
20f953664dc17dca23bd724bd64f89189c16c83263Chris Wren/**
210421e6d58635f347f785ae4c78e8b2a5da327138Julia Reynolds * Sorts notifications individually into attention-relevant order.
22f953664dc17dca23bd724bd64f89189c16c83263Chris Wren */
23f953664dc17dca23bd724bd64f89189c16c83263Chris Wrenpublic class NotificationComparator
24333a61c3a5a83fe9c50ebeb5c947317f61385b7bChris Wren        implements Comparator<NotificationRecord> {
25f953664dc17dca23bd724bd64f89189c16c83263Chris Wren
26f953664dc17dca23bd724bd64f89189c16c83263Chris Wren    @Override
271031c974855ff4117a6d7866e664295786840319Chris Wren    public int compare(NotificationRecord left, NotificationRecord right) {
28bdf3376616c276ed18a51185351b44fd16eeae29Chris Wren        final int leftImportance = left.getImportance();
29bdf3376616c276ed18a51185351b44fd16eeae29Chris Wren        final int rightImportance = right.getImportance();
30bdf3376616c276ed18a51185351b44fd16eeae29Chris Wren        if (leftImportance != rightImportance) {
31dbc114f4bb762150df161e556b8c3577567f2630Julia Reynolds            // by importance, high to low
32bdf3376616c276ed18a51185351b44fd16eeae29Chris Wren            return -1 * Integer.compare(leftImportance, rightImportance);
33f953664dc17dca23bd724bd64f89189c16c83263Chris Wren        }
341031c974855ff4117a6d7866e664295786840319Chris Wren
35dbc114f4bb762150df161e556b8c3577567f2630Julia Reynolds        // Whether or not the notification can bypass DND.
360421e6d58635f347f785ae4c78e8b2a5da327138Julia Reynolds        final int leftPackagePriority = left.getPackagePriority();
370421e6d58635f347f785ae4c78e8b2a5da327138Julia Reynolds        final int rightPackagePriority = right.getPackagePriority();
380421e6d58635f347f785ae4c78e8b2a5da327138Julia Reynolds        if (leftPackagePriority != rightPackagePriority) {
390421e6d58635f347f785ae4c78e8b2a5da327138Julia Reynolds            // by priority, high to low
400421e6d58635f347f785ae4c78e8b2a5da327138Julia Reynolds            return -1 * Integer.compare(leftPackagePriority, rightPackagePriority);
410421e6d58635f347f785ae4c78e8b2a5da327138Julia Reynolds        }
420421e6d58635f347f785ae4c78e8b2a5da327138Julia Reynolds
43dbc114f4bb762150df161e556b8c3577567f2630Julia Reynolds        final int leftPriority = left.sbn.getNotification().priority;
44dbc114f4bb762150df161e556b8c3577567f2630Julia Reynolds        final int rightPriority = right.sbn.getNotification().priority;
45dbc114f4bb762150df161e556b8c3577567f2630Julia Reynolds        if (leftPriority != rightPriority) {
46dbc114f4bb762150df161e556b8c3577567f2630Julia Reynolds            // by priority, high to low
47dbc114f4bb762150df161e556b8c3577567f2630Julia Reynolds            return -1 * Integer.compare(leftPriority, rightPriority);
48dbc114f4bb762150df161e556b8c3577567f2630Julia Reynolds        }
49dbc114f4bb762150df161e556b8c3577567f2630Julia Reynolds
501031c974855ff4117a6d7866e664295786840319Chris Wren        final float leftPeople = left.getContactAffinity();
511031c974855ff4117a6d7866e664295786840319Chris Wren        final float rightPeople = right.getContactAffinity();
521031c974855ff4117a6d7866e664295786840319Chris Wren        if (leftPeople != rightPeople) {
53f953664dc17dca23bd724bd64f89189c16c83263Chris Wren            // by contact proximity, close to far
541031c974855ff4117a6d7866e664295786840319Chris Wren            return -1 * Float.compare(leftPeople, rightPeople);
55f953664dc17dca23bd724bd64f89189c16c83263Chris Wren        }
561031c974855ff4117a6d7866e664295786840319Chris Wren
57f953664dc17dca23bd724bd64f89189c16c83263Chris Wren        // then break ties by time, most recent first
581031c974855ff4117a6d7866e664295786840319Chris Wren        return -1 * Long.compare(left.getRankingTimeMs(), right.getRankingTimeMs());
59f953664dc17dca23bd724bd64f89189c16c83263Chris Wren    }
60f953664dc17dca23bd724bd64f89189c16c83263Chris Wren}
61