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 package com.android.launcher3.popup;
17 
18 import android.content.Context;
19 import android.view.View;
20 
21 import com.android.launcher3.views.ActivityContext;
22 
23 /**
24  * Utility class to handle updates while the popup is visible (like widgets and
25  * notification changes)
26  *
27  * @param <T> The activity on which the popup shows
28  */
29 public abstract class PopupLiveUpdateHandler<T extends Context & ActivityContext> implements
30         PopupDataProvider.PopupDataChangeListener, View.OnAttachStateChangeListener {
31 
32     protected final T mContext;
33     protected final PopupContainerWithArrow<T> mPopupContainerWithArrow;
34 
PopupLiveUpdateHandler( T context, PopupContainerWithArrow<T> popupContainerWithArrow)35     public PopupLiveUpdateHandler(
36             T context, PopupContainerWithArrow<T> popupContainerWithArrow) {
37         mContext = context;
38         mPopupContainerWithArrow = popupContainerWithArrow;
39     }
40 
41     @Override
onViewAttachedToWindow(View view)42     public void onViewAttachedToWindow(View view) {
43         PopupDataProvider popupDataProvider = mContext.getPopupDataProvider();
44 
45         if (popupDataProvider != null) {
46             popupDataProvider.setChangeListener(this);
47         }
48     }
49 
50     @Override
onViewDetachedFromWindow(View view)51     public void onViewDetachedFromWindow(View view) {
52         PopupDataProvider popupDataProvider = mContext.getPopupDataProvider();
53 
54         if (popupDataProvider != null) {
55             popupDataProvider.setChangeListener(null);
56         }
57     }
58 }
59