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*/
16
17package com.android.server.notification;
18
19import android.app.NotificationManager;
20import android.content.Context;
21import android.net.Uri;
22import android.util.Log;
23import android.util.Slog;
24
25import com.android.internal.annotations.VisibleForTesting;
26
27/**
28 * This {@link com.android.server.notification.NotificationSignalExtractor} notices noisy
29 * notifications and marks them to get a temporary ranking bump.
30 */
31public class NotificationIntrusivenessExtractor implements NotificationSignalExtractor {
32    private static final String TAG = "IntrusivenessExtractor";
33    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
34
35    /** Length of time (in milliseconds) that an intrusive or noisy notification will stay at
36    the top of the ranking order, before it falls back to its natural position. */
37    @VisibleForTesting
38    static final long HANG_TIME_MS = 10000;
39
40    public void initialize(Context ctx, NotificationUsageStats usageStats) {
41        if (DBG) Slog.d(TAG, "Initializing  " + getClass().getSimpleName() + ".");
42    }
43
44    public RankingReconsideration process(NotificationRecord record) {
45        if (record == null || record.getNotification() == null) {
46            if (DBG) Slog.d(TAG, "skipping empty notification");
47            return null;
48        }
49
50        if (record.getFreshnessMs(System.currentTimeMillis()) < HANG_TIME_MS
51                && record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT) {
52            if (record.getSound() != null && record.getSound() != Uri.EMPTY) {
53                record.setRecentlyIntrusive(true);
54            }
55            if (record.getVibration() != null) {
56                record.setRecentlyIntrusive(true);
57            }
58            if (record.getNotification().fullScreenIntent != null) {
59                record.setRecentlyIntrusive(true);
60            }
61        }
62
63        if (!record.isRecentlyIntrusive()) {
64            return null;
65        }
66
67        return new RankingReconsideration(record.getKey(), HANG_TIME_MS) {
68            @Override
69            public void work() {
70                // pass
71            }
72
73            @Override
74            public void applyChangesLocked(NotificationRecord record) {
75                // there will be another reconsideration in the message queue HANG_TIME_MS
76                // from each time this record alerts, which can finally clear this flag.
77                if ((System.currentTimeMillis() - record.getLastIntrusive()) >= HANG_TIME_MS) {
78                    record.setRecentlyIntrusive(false);
79                }
80            }
81        };
82    }
83
84    @Override
85    public void setConfig(RankingConfig config) {
86        // ignore: config has no relevant information yet.
87    }
88
89    @Override
90    public void setZenHelper(ZenModeHelper helper) {
91
92    }
93}
94