From be7f84d5a1e255aec159a79a5e4623888062f254 Mon Sep 17 00:00:00 2001 From: Sebastian Franco Date: Tue, 26 Apr 2022 12:40:42 -0500 Subject: [PATCH] Adding a case when there is only 1 system shortcut on the Popup Container When there is only 1 system shortcut the Popup Container looks weird because it shows the icon without text and it has a lot of space. We already have a view to show the sorcuts with text and icons so I reused that view and show it only if there is 1 system shortcut. I also did a small refactor separating the sortcuts into widgetShortcuts and nonWidgetShortcuts. The way I did it is not the most efficient because it uses two passes, but I think it is easier to read this way and we'll never have too many shortcuts. Fix: 229356716 Test: Long press on the app icons in the workspace and in the app drawer. Try different app some with one shortcut like the phone app and some with multiple like. Change-Id: Ie054af546758d0686914fc934772a76a3c690fe5 --- .../popup/PopupContainerWithArrow.java | 75 ++++++++++++------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/src/com/android/launcher3/popup/PopupContainerWithArrow.java b/src/com/android/launcher3/popup/PopupContainerWithArrow.java index 49d97d21c1..8e7a10ca24 100644 --- a/src/com/android/launcher3/popup/PopupContainerWithArrow.java +++ b/src/com/android/launcher3/popup/PopupContainerWithArrow.java @@ -72,6 +72,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.stream.Collectors; /** @@ -244,23 +245,32 @@ public class PopupContainerWithArrow mNotificationContainer); } + private void initializeSystemShortcuts(List shortcuts) { + if (shortcuts.isEmpty()) { + return; + } + // If there is only 1 shortcut, add it to its own container so it can show text and icon + if (shortcuts.size() == 1) { + initializeSystemShortcut(R.layout.system_shortcut, this, shortcuts.get(0)); + return; + } + mSystemShortcutContainer = inflateAndAdd(R.layout.system_shortcut_icons, this); + for (SystemShortcut shortcut : shortcuts) { + initializeSystemShortcut( + R.layout.system_shortcut_icon_only, mSystemShortcutContainer, + shortcut); + } + } + @TargetApi(Build.VERSION_CODES.P) public void populateAndShow(final BubbleTextView originalIcon, int shortcutCount, - final List notificationKeys, List systemShortcuts) { + final List notificationKeys, List shortcuts) { mNumNotifications = notificationKeys.size(); mOriginalIcon = originalIcon; boolean hasDeepShortcuts = shortcutCount > 0; mContainerWidth = getResources().getDimensionPixelSize(R.dimen.bg_popup_item_width); - // if there are deep shortcuts, we might want to increase the width of shortcuts to fit - // horizontally laid out system shortcuts. - if (hasDeepShortcuts) { - mContainerWidth = Math.max(mContainerWidth, - systemShortcuts.size() * getResources() - .getDimensionPixelSize(R.dimen.system_shortcut_header_icon_touch_size) - ); - } // Add views if (mNumNotifications > 0) { // Add notification entries @@ -279,6 +289,24 @@ public class PopupContainerWithArrow mDeepShortcutContainer = findViewById(R.id.deep_shortcuts_container); } if (hasDeepShortcuts) { + // Remove the widget shortcut fom the list + List systemShortcuts = shortcuts + .stream() + .filter(shortcut -> !(shortcut instanceof SystemShortcut.Widgets)) + .collect(Collectors.toList()); + Optional widgetShortcutOpt = shortcuts + .stream() + .filter(shortcut -> shortcut instanceof SystemShortcut.Widgets) + .map(SystemShortcut.Widgets.class::cast) + .findFirst(); + + // if there are deep shortcuts, we might want to increase the width of shortcuts to fit + // horizontally laid out system shortcuts. + mContainerWidth = Math.max(mContainerWidth, + systemShortcuts.size() * getResources() + .getDimensionPixelSize(R.dimen.system_shortcut_header_icon_touch_size) + ); + mDeepShortcutContainer.setVisibility(View.VISIBLE); for (int i = shortcutCount; i > 0; i--) { @@ -288,30 +316,19 @@ public class PopupContainerWithArrow } updateHiddenShortcuts(); - if (!systemShortcuts.isEmpty()) { - for (SystemShortcut shortcut : systemShortcuts) { - if (shortcut instanceof SystemShortcut.Widgets) { - if (mWidgetContainer == null) { - mWidgetContainer = inflateAndAdd(R.layout.widget_shortcut_container, - this); - } - initializeWidgetShortcut(mWidgetContainer, shortcut); - } - } - mSystemShortcutContainer = inflateAndAdd(R.layout.system_shortcut_icons, this); - - for (SystemShortcut shortcut : systemShortcuts) { - if (!(shortcut instanceof SystemShortcut.Widgets)) { - initializeSystemShortcut( - R.layout.system_shortcut_icon_only, mSystemShortcutContainer, - shortcut); - } + if (widgetShortcutOpt.isPresent()) { + if (mWidgetContainer == null) { + mWidgetContainer = inflateAndAdd(R.layout.widget_shortcut_container, + this); } + initializeWidgetShortcut(mWidgetContainer, widgetShortcutOpt.get()); } + + initializeSystemShortcuts(systemShortcuts); } else { mDeepShortcutContainer.setVisibility(View.GONE); - if (!systemShortcuts.isEmpty()) { - for (SystemShortcut shortcut : systemShortcuts) { + if (!shortcuts.isEmpty()) { + for (SystemShortcut shortcut : shortcuts) { initializeSystemShortcut(R.layout.system_shortcut, this, shortcut); } }