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.view.View; 19 import android.view.ViewGroup; 20 21 import com.android.launcher3.BubbleTextView; 22 import com.android.launcher3.Launcher; 23 import com.android.launcher3.R; 24 import com.android.launcher3.model.data.ItemInfo; 25 26 /** 27 * Utility class to handle updates while the popup is visible on the Launcher 28 */ 29 public class LauncherPopupLiveUpdateHandler extends PopupLiveUpdateHandler<Launcher> { 30 LauncherPopupLiveUpdateHandler( Launcher launcher, PopupContainerWithArrow<Launcher> popupContainerWithArrow)31 public LauncherPopupLiveUpdateHandler( 32 Launcher launcher, PopupContainerWithArrow<Launcher> popupContainerWithArrow) { 33 super(launcher, popupContainerWithArrow); 34 } 35 getWidgetsView(ViewGroup container)36 private View getWidgetsView(ViewGroup container) { 37 for (int i = container.getChildCount() - 1; i >= 0; --i) { 38 View systemShortcutView = container.getChildAt(i); 39 if (systemShortcutView.getTag() instanceof SystemShortcut.Widgets) { 40 return systemShortcutView; 41 } 42 } 43 return null; 44 } 45 46 @Override onWidgetsBound()47 public void onWidgetsBound() { 48 BubbleTextView originalIcon = mPopupContainerWithArrow.getOriginalIcon(); 49 SystemShortcut widgetInfo = SystemShortcut.WIDGETS.getShortcut(mContext, 50 (ItemInfo) originalIcon.getTag(), originalIcon); 51 View widgetsView = getWidgetsView(mPopupContainerWithArrow); 52 if (widgetsView == null && mPopupContainerWithArrow.getWidgetContainer() != null) { 53 widgetsView = getWidgetsView(mPopupContainerWithArrow.getWidgetContainer()); 54 } 55 56 if (widgetInfo != null && widgetsView == null) { 57 // We didn't have any widgets cached but now there are some, so enable the shortcut. 58 if (mPopupContainerWithArrow.getSystemShortcutContainer() 59 != mPopupContainerWithArrow) { 60 if (mPopupContainerWithArrow.getWidgetContainer() == null) { 61 mPopupContainerWithArrow.setWidgetContainer( 62 mPopupContainerWithArrow.inflateAndAdd( 63 R.layout.widget_shortcut_container, 64 mPopupContainerWithArrow)); 65 } 66 mPopupContainerWithArrow.initializeWidgetShortcut( 67 mPopupContainerWithArrow.getWidgetContainer(), 68 widgetInfo); 69 } else { 70 // If using the expanded system shortcut (as opposed to just the icon), we need 71 // to reopen the container to ensure measurements etc. all work out. While this 72 // could be quite janky, in practice the user would typically see a small 73 // flicker as the animation restarts partway through, and this is a very rare 74 // edge case anyway. 75 mPopupContainerWithArrow.close(false); 76 PopupContainerWithArrow.showForIcon(mPopupContainerWithArrow.getOriginalIcon()); 77 } 78 } else if (widgetInfo == null && widgetsView != null) { 79 // No widgets exist, but we previously added the shortcut so remove it. 80 if (mPopupContainerWithArrow.getSystemShortcutContainer() 81 != mPopupContainerWithArrow 82 && mPopupContainerWithArrow.getWidgetContainer() != null) { 83 mPopupContainerWithArrow.getWidgetContainer().removeView(widgetsView); 84 } else { 85 mPopupContainerWithArrow.close(false); 86 PopupContainerWithArrow.showForIcon(mPopupContainerWithArrow.getOriginalIcon()); 87 } 88 } 89 } 90 91 @Override showPopupContainerForIcon(BubbleTextView originalIcon)92 protected void showPopupContainerForIcon(BubbleTextView originalIcon) { 93 PopupContainerWithArrow.showForIcon(originalIcon); 94 } 95 } 96