• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.annotation.MainThread;
20 import android.util.ArrayMap;
21 import android.util.Log;
22 
23 import androidx.annotation.NonNull;
24 
25 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
26 
27 import java.util.Map;
28 
29 /**
30  * A {@link BindStage} is an abstraction for a unit of work in inflating/binding/unbinding
31  * views to a notification. Used by {@link NotifBindPipeline}.
32  *
33  * Clients may also use {@link #getStageParams} to provide parameters for this stage for a given
34  * notification and request a rebind.
35  *
36  * @param <Params> params to do this stage
37  */
38 @MainThread
39 public abstract class BindStage<Params> extends BindRequester {
40 
41     private Map<NotificationEntry, Params> mContentParams = new ArrayMap<>();
42 
43     /**
44      * Execute the stage asynchronously.
45      *
46      * @param row notification top-level view to bind views to
47      * @param callback callback after stage finishes
48      */
executeStage( @onNull NotificationEntry entry, @NonNull ExpandableNotificationRow row, @NonNull StageCallback callback)49     protected abstract void executeStage(
50             @NonNull NotificationEntry entry,
51             @NonNull ExpandableNotificationRow row,
52             @NonNull StageCallback callback);
53 
54     /**
55      * Abort the stage if in progress.
56      *
57      * @param row notification top-level view to bind views to
58      */
abortStage( @onNull NotificationEntry entry, @NonNull ExpandableNotificationRow row)59     protected abstract void abortStage(
60             @NonNull NotificationEntry entry,
61             @NonNull ExpandableNotificationRow row);
62 
63     /**
64      * Get the stage parameters for the entry. Clients should use this to modify how the stage
65      * handles the notification content.
66      */
getStageParams(@onNull NotificationEntry entry)67     public final Params getStageParams(@NonNull NotificationEntry entry) {
68         Params params = mContentParams.get(entry);
69         if (params == null) {
70             // TODO: This should throw an exception but there are some cases of re-entrant calls
71             // in NotificationEntryManager (e.g. b/155324756) that cause removal in update that
72             // lead to inflation after the notification is "removed". We return an empty params
73             // to avoid any NPEs for now, but we should remove this when all re-entrant calls are
74             // fixed.
75             Log.wtf(TAG, String.format("Entry does not have any stage parameters. key: %s",
76                             entry.getKey()));
77             return newStageParams();
78         }
79         return params;
80     }
81 
82     /**
83      * Create a params entry for the notification for this stage.
84      */
createStageParams(@onNull NotificationEntry entry)85     final void createStageParams(@NonNull NotificationEntry entry) {
86         mContentParams.put(entry, newStageParams());
87     }
88 
89     /**
90      * Delete params entry for notification.
91      */
deleteStageParams(@onNull NotificationEntry entry)92     final void deleteStageParams(@NonNull NotificationEntry entry) {
93         mContentParams.remove(entry);
94     }
95 
96     /**
97      * Create a new, empty stage params object.
98      */
newStageParams()99     protected abstract Params newStageParams();
100 
101     private static final String TAG = "BindStage";
102 
103     /**
104      * Interface for callback.
105      */
106     interface StageCallback {
107         /**
108          * Callback for when the stage is complete.
109          */
onStageFinished(NotificationEntry entry)110         void onStageFinished(NotificationEntry entry);
111     }
112 }
113