Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.systemui.statusbar.notification.collection.coordinator; |
| 18 | |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 19 | import com.android.systemui.statusbar.notification.collection.NotifInflaterImpl; |
Ned Burns | 8172d3a | 2020-01-10 00:24:17 -0500 | [diff] [blame] | 20 | import com.android.systemui.statusbar.notification.collection.NotifPipeline; |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 21 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Ned Burns | 012048d | 2020-01-08 19:57:30 -0500 | [diff] [blame] | 22 | import com.android.systemui.statusbar.notification.collection.inflation.NotifInflater; |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 23 | import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.NotifFilter; |
Ned Burns | 012048d | 2020-01-08 19:57:30 -0500 | [diff] [blame] | 24 | import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener; |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.List; |
| 28 | |
| 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | /** |
| 33 | * Kicks off notification inflation and view rebinding when a notification is added or updated. |
| 34 | * Aborts inflation when a notification is removed. |
| 35 | * |
| 36 | * If a notification is not done inflating, this coordinator will filter the notification out |
| 37 | * from the NotifListBuilder. |
| 38 | */ |
| 39 | @Singleton |
| 40 | public class PreparationCoordinator implements Coordinator { |
| 41 | private static final String TAG = "PreparationCoordinator"; |
| 42 | |
Ned Burns | afe77bc | 2020-01-30 20:45:07 -0500 | [diff] [blame] | 43 | private final PreparationCoordinatorLogger mLogger; |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 44 | private final NotifInflater mNotifInflater; |
| 45 | private final List<NotificationEntry> mPendingNotifications = new ArrayList<>(); |
| 46 | |
| 47 | @Inject |
Ned Burns | afe77bc | 2020-01-30 20:45:07 -0500 | [diff] [blame] | 48 | public PreparationCoordinator( |
| 49 | PreparationCoordinatorLogger logger, |
| 50 | NotifInflaterImpl notifInflater) { |
| 51 | mLogger = logger; |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 52 | mNotifInflater = notifInflater; |
| 53 | mNotifInflater.setInflationCallback(mInflationCallback); |
| 54 | } |
| 55 | |
| 56 | @Override |
Ned Burns | 8172d3a | 2020-01-10 00:24:17 -0500 | [diff] [blame] | 57 | public void attach(NotifPipeline pipeline) { |
| 58 | pipeline.addCollectionListener(mNotifCollectionListener); |
| 59 | pipeline.addPreRenderFilter(mNotifInflationErrorFilter); |
| 60 | pipeline.addPreRenderFilter(mNotifInflatingFilter); |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private final NotifCollectionListener mNotifCollectionListener = new NotifCollectionListener() { |
| 64 | @Override |
| 65 | public void onEntryAdded(NotificationEntry entry) { |
| 66 | inflateEntry(entry, "entryAdded"); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onEntryUpdated(NotificationEntry entry) { |
| 71 | rebind(entry, "entryUpdated"); |
| 72 | } |
| 73 | |
| 74 | @Override |
Ned Burns | 4258d70 | 2020-01-08 19:58:57 -0500 | [diff] [blame] | 75 | public void onEntryRemoved(NotificationEntry entry, int reason) { |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 76 | abortInflation(entry, "entryRemoved reason=" + reason); |
| 77 | } |
| 78 | }; |
| 79 | |
Beverly | 6e6ce1b | 2020-01-07 17:04:16 -0500 | [diff] [blame] | 80 | private final NotifFilter mNotifInflationErrorFilter = new NotifFilter( |
| 81 | TAG + "InflationError") { |
| 82 | /** |
| 83 | * Filters out notifications that threw an error when attempting to inflate. |
| 84 | */ |
| 85 | @Override |
| 86 | public boolean shouldFilterOut(NotificationEntry entry, long now) { |
| 87 | if (entry.hasInflationError()) { |
| 88 | mPendingNotifications.remove(entry); |
| 89 | return true; |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | private final NotifFilter mNotifInflatingFilter = new NotifFilter(TAG + "Inflating") { |
| 96 | /** |
| 97 | * Filters out notifications that haven't been inflated yet |
| 98 | */ |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 99 | @Override |
| 100 | public boolean shouldFilterOut(NotificationEntry entry, long now) { |
| 101 | return mPendingNotifications.contains(entry); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | private final NotifInflater.InflationCallback mInflationCallback = |
| 106 | new NotifInflater.InflationCallback() { |
| 107 | @Override |
| 108 | public void onInflationFinished(NotificationEntry entry) { |
Ned Burns | afe77bc | 2020-01-30 20:45:07 -0500 | [diff] [blame] | 109 | mLogger.logNotifInflated(entry.getKey()); |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 110 | mPendingNotifications.remove(entry); |
Beverly | 6e6ce1b | 2020-01-07 17:04:16 -0500 | [diff] [blame] | 111 | mNotifInflatingFilter.invalidateList(); |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 112 | } |
| 113 | }; |
| 114 | |
| 115 | private void inflateEntry(NotificationEntry entry, String reason) { |
| 116 | abortInflation(entry, reason); |
| 117 | mPendingNotifications.add(entry); |
| 118 | mNotifInflater.inflateViews(entry); |
| 119 | } |
| 120 | |
| 121 | private void rebind(NotificationEntry entry, String reason) { |
| 122 | mNotifInflater.rebindViews(entry); |
| 123 | } |
| 124 | |
| 125 | private void abortInflation(NotificationEntry entry, String reason) { |
Ned Burns | afe77bc | 2020-01-30 20:45:07 -0500 | [diff] [blame] | 126 | mLogger.logInflationAborted(entry.getKey(), reason); |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 127 | entry.abortTask(); |
| 128 | mPendingNotifications.remove(entry); |
| 129 | } |
| 130 | } |