Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.server.notification; |
| 18 | |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 19 | import android.app.NotificationManager; |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 20 | import android.content.Context; |
Julia Reynolds | 2445037 | 2017-05-02 15:04:34 -0400 | [diff] [blame] | 21 | import android.net.Uri; |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 22 | import android.util.Log; |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 23 | import android.util.Slog; |
| 24 | |
Julia Reynolds | bbc469c | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 25 | import com.android.internal.annotations.VisibleForTesting; |
| 26 | |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 27 | /** |
Julia Reynolds | 0dbb731 | 2016-03-03 15:10:21 -0500 | [diff] [blame] | 28 | * This {@link com.android.server.notification.NotificationSignalExtractor} notices noisy |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 29 | * notifications and marks them to get a temporary ranking bump. |
| 30 | */ |
| 31 | public class NotificationIntrusivenessExtractor implements NotificationSignalExtractor { |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 32 | private static final String TAG = "IntrusivenessExtractor"; |
| 33 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 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. */ |
Julia Reynolds | bbc469c | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 37 | @VisibleForTesting |
| 38 | static final long HANG_TIME_MS = 10000; |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 39 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 40 | public void initialize(Context ctx, NotificationUsageStats usageStats) { |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 41 | if (DBG) Slog.d(TAG, "Initializing " + getClass().getSimpleName() + "."); |
| 42 | } |
| 43 | |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 44 | public RankingReconsideration process(NotificationRecord record) { |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 45 | if (record == null || record.getNotification() == null) { |
| 46 | if (DBG) Slog.d(TAG, "skipping empty notification"); |
| 47 | return null; |
| 48 | } |
| 49 | |
Julia Reynolds | bbc469c | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 50 | if (record.getFreshnessMs(System.currentTimeMillis()) < HANG_TIME_MS |
| 51 | && record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT) { |
Julia Reynolds | 2445037 | 2017-05-02 15:04:34 -0400 | [diff] [blame] | 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) { |
Julia Reynolds | 0dbb731 | 2016-03-03 15:10:21 -0500 | [diff] [blame] | 59 | record.setRecentlyIntrusive(true); |
| 60 | } |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 63 | if (!record.isRecentlyIntrusive()) { |
| 64 | return null; |
| 65 | } |
| 66 | |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 67 | return new RankingReconsideration(record.getKey(), HANG_TIME_MS) { |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 68 | @Override |
| 69 | public void work() { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 70 | // pass |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void applyChangesLocked(NotificationRecord record) { |
Julia Reynolds | 309d1c8 | 2017-05-03 16:00:20 -0400 | [diff] [blame] | 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 | } |
Chris Wren | 5190c0f | 2014-05-08 10:42:09 -0400 | [diff] [blame] | 80 | } |
| 81 | }; |
| 82 | } |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 83 | |
| 84 | @Override |
| 85 | public void setConfig(RankingConfig config) { |
| 86 | // ignore: config has no relevant information yet. |
| 87 | } |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 88 | |
| 89 | @Override |
| 90 | public void setZenHelper(ZenModeHelper helper) { |
| 91 | |
| 92 | } |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 93 | } |