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;
18 
19 import androidx.annotation.NonNull;
20 
21 import com.android.systemui.dagger.SysUISingleton;
22 import com.android.systemui.statusbar.notification.InflationException;
23 import com.android.systemui.statusbar.notification.collection.inflation.NotifInflater;
24 import com.android.systemui.statusbar.notification.collection.inflation.NotificationRowBinderImpl;
25 import com.android.systemui.statusbar.notification.row.NotifInflationErrorManager;
26 import com.android.systemui.statusbar.notification.row.NotificationContentInflater;
27 
28 import javax.inject.Inject;
29 
30 /**
31  * Handles notification inflating, rebinding, and inflation aborting.
32  *
33  * Currently a wrapper for NotificationRowBinderImpl.
34  */
35 @SysUISingleton
36 public class NotifInflaterImpl implements NotifInflater {
37 
38     private final NotifInflationErrorManager mNotifErrorManager;
39     private final NotifInflaterLogger mLogger;
40 
41     private NotificationRowBinderImpl mNotificationRowBinder;
42 
43     @Inject
NotifInflaterImpl(NotifInflationErrorManager errorManager, NotifInflaterLogger logger)44     public NotifInflaterImpl(NotifInflationErrorManager errorManager, NotifInflaterLogger logger) {
45         mNotifErrorManager = errorManager;
46         mLogger = logger;
47     }
48 
49     /**
50      * Attaches the row binder for inflation.
51      */
setRowBinder(NotificationRowBinderImpl rowBinder)52     public void setRowBinder(NotificationRowBinderImpl rowBinder) {
53         mNotificationRowBinder = rowBinder;
54     }
55 
56     /**
57      * Called to inflate the views of an entry.  Views are not considered inflated until all of its
58      * views are bound.
59      */
60     @Override
inflateViews(@onNull NotificationEntry entry, @NonNull Params params, @NonNull InflationCallback callback)61     public void inflateViews(@NonNull NotificationEntry entry, @NonNull Params params,
62             @NonNull InflationCallback callback) {
63         mLogger.logInflatingViews(entry, params);
64         inflateViewsImpl(entry, params, callback);
65         mLogger.logInflatedViews(entry);
66     }
67     @Override
rebindViews(@onNull NotificationEntry entry, @NonNull Params params, @NonNull InflationCallback callback)68     public void rebindViews(@NonNull NotificationEntry entry, @NonNull Params params,
69             @NonNull InflationCallback callback) {
70         mLogger.logRebindingViews(entry, params);
71         inflateViewsImpl(entry, params, callback);
72         mLogger.logReboundViews(entry);
73     }
74 
inflateViewsImpl(@onNull NotificationEntry entry, @NonNull Params params, @NonNull InflationCallback callback)75     private void inflateViewsImpl(@NonNull NotificationEntry entry, @NonNull Params params,
76             @NonNull InflationCallback callback) {
77         try {
78             requireBinder().inflateViews(
79                     entry,
80                     params,
81                     wrapInflationCallback(callback));
82         } catch (InflationException e) {
83             mLogger.logInflationException(entry, e);
84             mNotifErrorManager.setInflationError(entry, e);
85         }
86     }
87 
88     @Override
abortInflation(NotificationEntry entry)89     public boolean abortInflation(NotificationEntry entry) {
90         final boolean abortedTask = entry.abortTask();
91         if (abortedTask) {
92             mLogger.logAbortInflationAbortedTask(entry);
93         }
94         return abortedTask;
95     }
96 
97     @Override
releaseViews(@onNull NotificationEntry entry)98     public void releaseViews(@NonNull NotificationEntry entry) {
99         mLogger.logReleasingViews(entry);
100         requireBinder().releaseViews(entry);
101     }
102 
wrapInflationCallback( InflationCallback callback)103     private NotificationContentInflater.InflationCallback wrapInflationCallback(
104             InflationCallback callback) {
105         return new NotificationContentInflater.InflationCallback() {
106             @Override
107             public void handleInflationException(
108                     NotificationEntry entry,
109                     Exception e) {
110                 mNotifErrorManager.setInflationError(entry, e);
111             }
112 
113             @Override
114             public void onAsyncInflationFinished(NotificationEntry entry) {
115                 mNotifErrorManager.clearInflationError(entry);
116                 if (callback != null) {
117                     callback.onInflationFinished(entry, entry.getRowController());
118                 }
119             }
120         };
121     }
122 
123     private NotificationRowBinderImpl requireBinder() {
124         if (mNotificationRowBinder == null) {
125             throw new RuntimeException("NotificationRowBinder must be attached before using "
126                     + "NotifInflaterImpl.");
127         }
128         return mNotificationRowBinder;
129     }
130 }
131