• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.car.qc.provider;
18 
19 import android.content.Context;
20 
21 import com.android.car.qc.QCItem;
22 
23 /**
24  * Base class for local Quick Control providers.
25  */
26 public abstract class BaseLocalQCProvider {
27 
28     /**
29      * Callback to be executed when the QCItem updates.
30      */
31     public interface Notifier {
32         /**
33          * Called when the QCItem has been updated.
34          */
notifyUpdate()35         default void notifyUpdate() {
36         }
37     }
38 
39     private Notifier mNotifier;
40     private boolean mIsListening;
41     protected final Context mContext;
42 
BaseLocalQCProvider(Context context)43     public BaseLocalQCProvider(Context context) {
44         mContext = context;
45     }
46 
47     /**
48      * Set the notifier that should be called when the QCItem updates.
49      */
setNotifier(Notifier notifier)50     public void setNotifier(Notifier notifier) {
51         mNotifier = notifier;
52     }
53 
54     /**
55      * Update whether or not the provider should be listening for live updates.
56      */
shouldListen(boolean listen)57     public void shouldListen(boolean listen) {
58         if (mIsListening == listen) {
59             return;
60         }
61         mIsListening = listen;
62         if (listen) {
63             onSubscribed();
64         } else {
65             onUnsubscribed();
66         }
67     }
68 
69     /**
70      * Method to create and return a {@link QCItem}.
71      */
getQCItem()72     public abstract QCItem getQCItem();
73 
74     /**
75      * Called to inform the provider that it has been subscribed to.
76      */
onSubscribed()77     protected void onSubscribed() {
78     }
79 
80     /**
81      * Called to inform the provider that it has been unsubscribed from.
82      */
onUnsubscribed()83     protected void onUnsubscribed() {
84     }
85 
86     /**
87      * Called to inform the provider that it is being destroyed.
88      */
onDestroy()89     public void onDestroy() {
90     }
91 
notifyChange()92     protected void notifyChange() {
93         if (mNotifier != null) {
94             mNotifier.notifyUpdate();
95         }
96     }
97 }
98