blob: bf777e0dd43194b2ceffb9d86b898a51c7c6888a [file] [log] [blame]
Chris Wren5190c0f2014-05-08 10:42:09 -04001/*
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
Julia Reynolds85769912016-10-25 09:08:57 -040019import android.app.NotificationManager;
Chris Wren5190c0f2014-05-08 10:42:09 -040020import android.content.Context;
Julia Reynolds24450372017-05-02 15:04:34 -040021import android.net.Uri;
John Spurlock1d881a12015-03-18 19:21:54 -040022import android.util.Log;
Chris Wren5190c0f2014-05-08 10:42:09 -040023import android.util.Slog;
24
Julia Reynoldsbbc469c2017-07-19 13:48:07 -040025import com.android.internal.annotations.VisibleForTesting;
26
Chris Wren5190c0f2014-05-08 10:42:09 -040027/**
Julia Reynolds0dbb7312016-03-03 15:10:21 -050028 * This {@link com.android.server.notification.NotificationSignalExtractor} notices noisy
Chris Wren5190c0f2014-05-08 10:42:09 -040029 * notifications and marks them to get a temporary ranking bump.
30 */
31public class NotificationIntrusivenessExtractor implements NotificationSignalExtractor {
John Spurlock1d881a12015-03-18 19:21:54 -040032 private static final String TAG = "IntrusivenessExtractor";
33 private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
Chris Wren5190c0f2014-05-08 10:42:09 -040034
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. */
Julia Reynoldsbbc469c2017-07-19 13:48:07 -040037 @VisibleForTesting
38 static final long HANG_TIME_MS = 10000;
Chris Wren5190c0f2014-05-08 10:42:09 -040039
Chris Wren5eab2b72015-06-16 13:56:22 -040040 public void initialize(Context ctx, NotificationUsageStats usageStats) {
Chris Wren5190c0f2014-05-08 10:42:09 -040041 if (DBG) Slog.d(TAG, "Initializing " + getClass().getSimpleName() + ".");
42 }
43
Chris Wren470c1ac2014-05-21 15:28:10 -040044 public RankingReconsideration process(NotificationRecord record) {
Chris Wren5190c0f2014-05-08 10:42:09 -040045 if (record == null || record.getNotification() == null) {
46 if (DBG) Slog.d(TAG, "skipping empty notification");
47 return null;
48 }
49
Julia Reynoldsbbc469c2017-07-19 13:48:07 -040050 if (record.getFreshnessMs(System.currentTimeMillis()) < HANG_TIME_MS
51 && record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT) {
Julia Reynolds24450372017-05-02 15:04:34 -040052 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) {
Julia Reynolds0dbb7312016-03-03 15:10:21 -050059 record.setRecentlyIntrusive(true);
60 }
Chris Wren5190c0f2014-05-08 10:42:09 -040061 }
62
Julia Reynoldseb3dca72017-07-11 10:39:58 -040063 if (!record.isRecentlyIntrusive()) {
64 return null;
65 }
66
Chris Wren470c1ac2014-05-21 15:28:10 -040067 return new RankingReconsideration(record.getKey(), HANG_TIME_MS) {
Chris Wren5190c0f2014-05-08 10:42:09 -040068 @Override
69 public void work() {
Chris Wren470c1ac2014-05-21 15:28:10 -040070 // pass
71 }
72
73 @Override
74 public void applyChangesLocked(NotificationRecord record) {
Julia Reynolds309d1c82017-05-03 16:00:20 -040075 // 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 }
Chris Wren5190c0f2014-05-08 10:42:09 -040080 }
81 };
82 }
Chris Wren54bbef42014-07-09 18:37:56 -040083
84 @Override
85 public void setConfig(RankingConfig config) {
86 // ignore: config has no relevant information yet.
87 }
Julia Reynoldsc861a3d2018-02-15 10:34:49 -050088
89 @Override
90 public void setZenHelper(ZenModeHelper helper) {
91
92 }
Chris Wren54bbef42014-07-09 18:37:56 -040093}