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 static android.view.View.GONE; 19 20 import android.content.Context; 21 import android.view.View; 22 23 import com.android.launcher3.BubbleTextView; 24 import com.android.launcher3.dot.DotInfo; 25 import com.android.launcher3.model.data.ItemInfo; 26 import com.android.launcher3.notification.NotificationContainer; 27 import com.android.launcher3.notification.NotificationKeyData; 28 import com.android.launcher3.util.PackageUserKey; 29 import com.android.launcher3.views.ActivityContext; 30 31 import java.util.Map; 32 import java.util.function.Predicate; 33 34 /** 35 * Utility class to handle updates while the popup is visible (like widgets and 36 * notification changes) 37 * 38 * @param <T> The activity on which the popup shows 39 */ 40 public abstract class PopupLiveUpdateHandler<T extends Context & ActivityContext> implements 41 PopupDataProvider.PopupDataChangeListener, View.OnAttachStateChangeListener { 42 43 protected final T mContext; 44 protected final PopupContainerWithArrow<T> mPopupContainerWithArrow; 45 PopupLiveUpdateHandler( T context, PopupContainerWithArrow<T> popupContainerWithArrow)46 public PopupLiveUpdateHandler( 47 T context, PopupContainerWithArrow<T> popupContainerWithArrow) { 48 mContext = context; 49 mPopupContainerWithArrow = popupContainerWithArrow; 50 } 51 52 @Override onViewAttachedToWindow(View view)53 public void onViewAttachedToWindow(View view) { 54 PopupDataProvider popupDataProvider = mContext.getPopupDataProvider(); 55 56 if (popupDataProvider != null) { 57 popupDataProvider.setChangeListener(this); 58 } 59 } 60 61 @Override onViewDetachedFromWindow(View view)62 public void onViewDetachedFromWindow(View view) { 63 PopupDataProvider popupDataProvider = mContext.getPopupDataProvider(); 64 65 if (popupDataProvider != null) { 66 popupDataProvider.setChangeListener(null); 67 } 68 } 69 70 /** 71 * Updates the notification header if the original icon's dot updated. 72 */ 73 @Override onNotificationDotsUpdated(Predicate<PackageUserKey> updatedDots)74 public void onNotificationDotsUpdated(Predicate<PackageUserKey> updatedDots) { 75 ItemInfo itemInfo = (ItemInfo) mPopupContainerWithArrow.getOriginalIcon().getTag(); 76 PackageUserKey packageUser = PackageUserKey.fromItemInfo(itemInfo); 77 if (updatedDots.test(packageUser)) { 78 mPopupContainerWithArrow.updateNotificationHeader(); 79 } 80 } 81 82 83 @Override trimNotifications(Map<PackageUserKey, DotInfo> updatedDots)84 public void trimNotifications(Map<PackageUserKey, DotInfo> updatedDots) { 85 NotificationContainer notificationContainer = 86 mPopupContainerWithArrow.getNotificationContainer(); 87 if (notificationContainer == null) { 88 return; 89 } 90 ItemInfo originalInfo = (ItemInfo) mPopupContainerWithArrow.getOriginalIcon().getTag(); 91 DotInfo dotInfo = updatedDots.get(PackageUserKey.fromItemInfo(originalInfo)); 92 if (dotInfo == null || dotInfo.getNotificationKeys().size() == 0) { 93 // No more notifications, remove the notification views and expand all shortcuts. 94 notificationContainer.setVisibility(GONE); 95 mPopupContainerWithArrow.updateHiddenShortcuts(); 96 mPopupContainerWithArrow.assignMarginsAndBackgrounds(mPopupContainerWithArrow); 97 mPopupContainerWithArrow.updateArrowColor(); 98 } else { 99 notificationContainer.trimNotifications( 100 NotificationKeyData.extractKeysOnly(dotInfo.getNotificationKeys())); 101 } 102 } 103 104 @Override onSystemShortcutsUpdated()105 public void onSystemShortcutsUpdated() { 106 mPopupContainerWithArrow.close(true); 107 showPopupContainerForIcon(mPopupContainerWithArrow.getOriginalIcon()); 108 } 109 showPopupContainerForIcon(BubbleTextView originalIcon)110 protected abstract void showPopupContainerForIcon(BubbleTextView originalIcon); 111 } 112