blob: 1e5946a85cfaef8a04508c60af4a320aa8f6fe0a [file] [log] [blame]
Beverly8b493df2019-12-18 14:16:50 -05001/*
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
17package com.android.systemui.statusbar.notification.collection.coordinator;
18
Beverly8b493df2019-12-18 14:16:50 -050019import com.android.systemui.statusbar.notification.collection.NotifInflaterImpl;
Ned Burns8172d3a2020-01-10 00:24:17 -050020import com.android.systemui.statusbar.notification.collection.NotifPipeline;
Beverly8b493df2019-12-18 14:16:50 -050021import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns012048d2020-01-08 19:57:30 -050022import com.android.systemui.statusbar.notification.collection.inflation.NotifInflater;
Beverly8b493df2019-12-18 14:16:50 -050023import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.NotifFilter;
Ned Burns012048d2020-01-08 19:57:30 -050024import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
Beverly8b493df2019-12-18 14:16:50 -050025
26import java.util.ArrayList;
27import java.util.List;
28
29import javax.inject.Inject;
30import 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
40public class PreparationCoordinator implements Coordinator {
41 private static final String TAG = "PreparationCoordinator";
42
Ned Burnsafe77bc2020-01-30 20:45:07 -050043 private final PreparationCoordinatorLogger mLogger;
Beverly8b493df2019-12-18 14:16:50 -050044 private final NotifInflater mNotifInflater;
45 private final List<NotificationEntry> mPendingNotifications = new ArrayList<>();
46
47 @Inject
Ned Burnsafe77bc2020-01-30 20:45:07 -050048 public PreparationCoordinator(
49 PreparationCoordinatorLogger logger,
50 NotifInflaterImpl notifInflater) {
51 mLogger = logger;
Beverly8b493df2019-12-18 14:16:50 -050052 mNotifInflater = notifInflater;
53 mNotifInflater.setInflationCallback(mInflationCallback);
54 }
55
56 @Override
Ned Burns8172d3a2020-01-10 00:24:17 -050057 public void attach(NotifPipeline pipeline) {
58 pipeline.addCollectionListener(mNotifCollectionListener);
59 pipeline.addPreRenderFilter(mNotifInflationErrorFilter);
60 pipeline.addPreRenderFilter(mNotifInflatingFilter);
Beverly8b493df2019-12-18 14:16:50 -050061 }
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 Burns4258d702020-01-08 19:58:57 -050075 public void onEntryRemoved(NotificationEntry entry, int reason) {
Beverly8b493df2019-12-18 14:16:50 -050076 abortInflation(entry, "entryRemoved reason=" + reason);
77 }
78 };
79
Beverly6e6ce1b2020-01-07 17:04:16 -050080 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 */
Beverly8b493df2019-12-18 14:16:50 -050099 @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 Burnsafe77bc2020-01-30 20:45:07 -0500109 mLogger.logNotifInflated(entry.getKey());
Beverly8b493df2019-12-18 14:16:50 -0500110 mPendingNotifications.remove(entry);
Beverly6e6ce1b2020-01-07 17:04:16 -0500111 mNotifInflatingFilter.invalidateList();
Beverly8b493df2019-12-18 14:16:50 -0500112 }
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 Burnsafe77bc2020-01-30 20:45:07 -0500126 mLogger.logInflationAborted(entry.getKey(), reason);
Beverly8b493df2019-12-18 14:16:50 -0500127 entry.abortTask();
128 mPendingNotifications.remove(entry);
129 }
130}