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.row;
18 
19 import androidx.annotation.NonNull;
20 import androidx.collection.ArraySet;
21 
22 import com.android.systemui.dagger.SysUISingleton;
23 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Set;
28 
29 import javax.inject.Inject;
30 
31 /**
32  * A manager handling the error state of a notification when it encounters an exception while
33  * inflating. We don't want to show these notifications to the user but may want to keep them
34  * around for logging purposes.
35  */
36 @SysUISingleton
37 public class NotifInflationErrorManager {
38 
39     Set<String> mErroredNotifs = new ArraySet<>();
40     List<NotifInflationErrorListener> mListeners = new ArrayList<>();
41 
42     @Inject
NotifInflationErrorManager()43     public NotifInflationErrorManager() { }
44 
45     /**
46      * Mark the notification as errored out due to encountering an exception while inflating.
47      *
48      * @param e the exception encountered while inflating
49      */
setInflationError(NotificationEntry entry, Exception e)50     public void setInflationError(NotificationEntry entry, Exception e) {
51         mErroredNotifs.add(entry.getKey());
52         for (int i = 0; i < mListeners.size(); i++) {
53             mListeners.get(i).onNotifInflationError(entry, e);
54         }
55     }
56 
57     /**
58      * Notification inflated successfully and is no longer errored out.
59      */
clearInflationError(NotificationEntry entry)60     public void clearInflationError(NotificationEntry entry) {
61         if (mErroredNotifs.contains(entry.getKey())) {
62             mErroredNotifs.remove(entry.getKey());
63             for (int i = 0; i < mListeners.size(); i++) {
64                 mListeners.get(i).onNotifInflationErrorCleared(entry);
65             }
66         }
67     }
68 
69     /**
70      * Whether or not the notification encountered an exception while inflating.
71      */
hasInflationError(@onNull NotificationEntry entry)72     public boolean hasInflationError(@NonNull NotificationEntry entry) {
73         return mErroredNotifs.contains(entry.getKey());
74     }
75 
76     /**
77      * Add listener for changes in inflation error state.
78      */
addInflationErrorListener(NotifInflationErrorListener listener)79     public void addInflationErrorListener(NotifInflationErrorListener listener) {
80         mListeners.add(listener);
81     }
82 
83     /**
84      * Listener for changes in notification inflation error state.
85      */
86     public interface NotifInflationErrorListener {
87 
88         /**
89          * Called when notification encounters an inflation exception.
90          *
91          * @param e the exception encountered while inflating
92          */
onNotifInflationError(NotificationEntry entry, Exception e)93         void onNotifInflationError(NotificationEntry entry, Exception e);
94 
95         /**
96          * Called when notification inflation error is cleared.
97          */
onNotifInflationErrorCleared(NotificationEntry entry)98         default void onNotifInflationErrorCleared(NotificationEntry entry) {}
99     }
100 }
101