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 static com.android.systemui.statusbar.notification.row.NotificationRowContentBinder.FLAG_CONTENT_VIEW_ALL;
20 
21 import androidx.annotation.NonNull;
22 
23 import com.android.systemui.dagger.SysUISingleton;
24 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
25 import com.android.systemui.statusbar.notification.row.NotificationRowContentBinder.BindParams;
26 import com.android.systemui.statusbar.notification.row.NotificationRowContentBinder.InflationCallback;
27 import com.android.systemui.statusbar.notification.row.NotificationRowContentBinder.InflationFlag;
28 
29 import javax.inject.Inject;
30 
31 /**
32  * A stage that binds all content views for an already inflated {@link ExpandableNotificationRow}.
33  *
34  * In the farther future, the binder logic and consequently this stage should be broken into
35  * smaller stages.
36  */
37 @SysUISingleton
38 public class RowContentBindStage extends BindStage<RowContentBindParams> {
39     private final NotificationRowContentBinder mBinder;
40     private final NotifInflationErrorManager mNotifInflationErrorManager;
41     private final RowContentBindStageLogger mLogger;
42 
43     @Inject
RowContentBindStage( NotificationRowContentBinder binder, NotifInflationErrorManager errorManager, RowContentBindStageLogger logger)44     RowContentBindStage(
45             NotificationRowContentBinder binder,
46             NotifInflationErrorManager errorManager,
47             RowContentBindStageLogger logger) {
48         mBinder = binder;
49         mNotifInflationErrorManager = errorManager;
50         mLogger = logger;
51     }
52 
53     @Override
executeStage( @onNull NotificationEntry entry, @NonNull ExpandableNotificationRow row, @NonNull StageCallback callback)54     protected void executeStage(
55             @NonNull NotificationEntry entry,
56             @NonNull ExpandableNotificationRow row,
57             @NonNull StageCallback callback) {
58         RowContentBindParams params = getStageParams(entry);
59 
60         mLogger.logExecutingStage(entry, params);
61 
62         // Resolve content to bind/unbind.
63         @InflationFlag int inflationFlags = params.getContentViews();
64         @InflationFlag int invalidatedFlags = params.getDirtyContentViews();
65 
66         // Rebind the content views which are needed now, and the corresponding old views are
67         // invalidated
68         @InflationFlag int contentToBind = invalidatedFlags & inflationFlags;
69         // Unbind the content views that are not needed
70         @InflationFlag int contentToUnbind = inflationFlags ^ FLAG_CONTENT_VIEW_ALL;
71 
72         // Bind/unbind with parameters
73         mBinder.unbindContent(entry, row, contentToUnbind);
74 
75         BindParams bindParams = new BindParams();
76         bindParams.isMinimized = params.useMinimized();
77         bindParams.usesIncreasedHeight = params.useIncreasedHeight();
78         bindParams.usesIncreasedHeadsUpHeight = params.useIncreasedHeadsUpHeight();
79         boolean forceInflate = params.needsReinflation();
80 
81         InflationCallback inflationCallback = new InflationCallback() {
82             @Override
83             public void handleInflationException(NotificationEntry entry, Exception e) {
84                 mNotifInflationErrorManager.setInflationError(entry, e);
85             }
86 
87             @Override
88             public void onAsyncInflationFinished(NotificationEntry entry) {
89                 mNotifInflationErrorManager.clearInflationError(entry);
90                 getStageParams(entry).clearDirtyContentViews();
91                 callback.onStageFinished(entry);
92             }
93         };
94         mBinder.cancelBind(entry, row);
95         mBinder.bindContent(entry, row, contentToBind, bindParams, forceInflate, inflationCallback);
96     }
97 
98     @Override
abortStage( @onNull NotificationEntry entry, @NonNull ExpandableNotificationRow row)99     protected void abortStage(
100             @NonNull NotificationEntry entry,
101             @NonNull ExpandableNotificationRow row) {
102         final boolean cancelledBind = mBinder.cancelBind(entry, row);
103         if (cancelledBind) {
104             mLogger.logAbortStageCancelledBind(entry);
105         }
106     }
107 
108     @Override
newStageParams()109     protected RowContentBindParams newStageParams() {
110         return new RowContentBindParams();
111     }
112 }
113