1 /*
2  * Copyright (C) 2019 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.listbuilder.pluggable;
18 
19 import android.annotation.Nullable;
20 import android.os.Trace;
21 
22 import com.android.systemui.statusbar.notification.collection.NotifPipeline;
23 
24 /**
25  * Generic superclass for chunks of code that can plug into the {@link NotifPipeline}.
26  *
27  * A pluggable is fundamentally three things:
28  * 1. A name (for debugging purposes)
29  * 2. The functionality that the pluggable provides to the pipeline (this is determined by the
30  *    subclass).
31  * 3. A way for the pluggable to inform the pipeline that its state has changed and the pipeline
32  *    should be rerun (in this case, the invalidate() method).
33  *
34  * @param <This> The type of the subclass. Subclasses should bind their own type here.
35  */
36 public abstract class Pluggable<This> {
37     private final String mName;
38     @Nullable private PluggableListener<This> mListener;
39 
Pluggable(String name)40     Pluggable(String name) {
41         mName = name;
42     }
43 
getName()44     public final String getName() {
45         return mName;
46     }
47 
48     /**
49      * Call this method when something has caused this pluggable's behavior to change. The pipeline
50      * will be re-run.
51      */
invalidateList(@ullable String reason)52     public final void invalidateList(@Nullable String reason) {
53         if (mListener != null) {
54             if (Trace.isEnabled()) {
55                 Trace.traceBegin(Trace.TRACE_TAG_APP, "Pluggable<" + mName + ">.invalidateList");
56             }
57             mListener.onPluggableInvalidated((This) this, reason);
58             Trace.endSection();
59         }
60     }
61 
62     /** Set a listener to be notified when a pluggable is invalidated. */
setInvalidationListener(PluggableListener<This> listener)63     public final void setInvalidationListener(PluggableListener<This> listener) {
64         mListener = listener;
65     }
66 
67     /**
68      * Called on the pluggable once at the end of every pipeline run. Override this method to
69      * perform any necessary cleanup.
70      */
onCleanup()71     public void onCleanup() { }
72 
73     /**
74      * Listener interface for when pluggables are invalidated.
75      *
76      * @param <T> The type of pluggable that is being listened to.
77      */
78     public interface PluggableListener<T> {
79         /** Called whenever {@link #invalidateList(String)} is called on this pluggable. */
onPluggableInvalidated(T pluggable, @Nullable String reason)80         void onPluggableInvalidated(T pluggable, @Nullable String reason);
81     }
82 }
83