Simplyfing SystemShortcut rendering

Instead of opening the popup again when the status of AppSharing
changes, updating the UI in-place.

Bug: 321179015
Test: Presubmit
Flag: N/A
Change-Id: I6d7bd8cdf2014fd881324347e3e01b8e7e84ef1e
diff --git a/go/quickstep/src/com/android/launcher3/AppSharing.java b/go/quickstep/src/com/android/launcher3/AppSharing.java
index 78524d1..e15b132 100644
--- a/go/quickstep/src/com/android/launcher3/AppSharing.java
+++ b/go/quickstep/src/com/android/launcher3/AppSharing.java
@@ -31,6 +31,8 @@
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.View;
+import android.widget.ImageView;
+import android.widget.TextView;
 import android.widget.Toast;
 
 import androidx.core.content.FileProvider;
@@ -45,6 +47,9 @@
 import com.android.launcher3.views.ActivityContext;
 
 import java.io.File;
+import java.util.Collections;
+import java.util.Set;
+import java.util.WeakHashMap;
 
 /**
  * Defines the Share system shortcut and its factory.
@@ -112,6 +117,9 @@
         private final PopupDataProvider mPopupDataProvider;
         private final boolean mSharingEnabledForUser;
 
+        private final Set<View> mBoundViews = Collections.newSetFromMap(new WeakHashMap<>());
+        private boolean mIsEnabled = true;
+
         public Share(Launcher target, ItemInfo itemInfo, View originalView) {
             super(R.drawable.ic_share, R.string.app_share_drop_target_label, target, itemInfo,
                     originalView);
@@ -128,10 +136,23 @@
         }
 
         @Override
+        public void setIconAndLabelFor(View iconView, TextView labelView) {
+            super.setIconAndLabelFor(iconView, labelView);
+            mBoundViews.add(iconView);
+            mBoundViews.add(labelView);
+        }
+
+        @Override
+        public void setIconAndContentDescriptionFor(ImageView view) {
+            super.setIconAndContentDescriptionFor(view);
+            mBoundViews.add(view);
+        }
+
+        @Override
         public void onClick(View view) {
             ActivityContext.lookupContext(view.getContext())
                     .getStatsLogManager().logger().log(LAUNCHER_SYSTEM_SHORTCUT_APP_SHARE_TAP);
-            if (!isEnabled()) {
+            if (!mIsEnabled) {
                 showCannotShareToast(view.getContext());
                 return;
             }
@@ -179,7 +200,6 @@
                 return;
             }
             checkShareability(/* requestUpdateIfUnknown */ false);
-            mTarget.runOnUiThread(mPopupDataProvider::redrawSystemShortcuts);
         }
 
         private void checkShareability(boolean requestUpdateIfUnknown) {
@@ -209,6 +229,17 @@
             int duration = Toast.LENGTH_SHORT;
             Toast.makeText(context, text, duration).show();
         }
+
+        public void setEnabled(boolean isEnabled) {
+            if (mIsEnabled != isEnabled) {
+                mIsEnabled = isEnabled;
+                mBoundViews.forEach(v -> v.setEnabled(isEnabled));
+            }
+        }
+
+        public boolean isEnabled() {
+            return mIsEnabled;
+        }
     }
 
     /**
diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarPopupController.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarPopupController.java
index a667dca..ca192c8 100644
--- a/quickstep/src/com/android/launcher3/taskbar/TaskbarPopupController.java
+++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarPopupController.java
@@ -40,7 +40,6 @@
 import com.android.launcher3.notification.NotificationListener;
 import com.android.launcher3.popup.PopupContainerWithArrow;
 import com.android.launcher3.popup.PopupDataProvider;
-import com.android.launcher3.popup.PopupLiveUpdateHandler;
 import com.android.launcher3.popup.SystemShortcut;
 import com.android.launcher3.shortcuts.DeepShortcutView;
 import com.android.launcher3.splitscreen.SplitShortcut;
@@ -166,13 +165,6 @@
                     R.layout.popup_container, context.getDragLayer(), false);
         container.populateAndShowRows(icon, deepShortcutCount, systemShortcuts);
 
-        container.addOnAttachStateChangeListener(
-                new PopupLiveUpdateHandler<BaseTaskbarContext>(context, container) {
-                    @Override
-                    protected void showPopupContainerForIcon(BubbleTextView originalIcon) {
-                        showForIcon(originalIcon);
-                    }
-                });
         // TODO (b/198438631): configure for taskbar/context
         container.setPopupItemDragHandler(new TaskbarPopupItemDragHandler());
         mControllers.taskbarDragController.addDragListener(container);
diff --git a/src/com/android/launcher3/popup/LauncherPopupLiveUpdateHandler.java b/src/com/android/launcher3/popup/LauncherPopupLiveUpdateHandler.java
index c0a04b1..89b5ba1 100644
--- a/src/com/android/launcher3/popup/LauncherPopupLiveUpdateHandler.java
+++ b/src/com/android/launcher3/popup/LauncherPopupLiveUpdateHandler.java
@@ -87,9 +87,4 @@
             }
         }
     }
-
-    @Override
-    protected void showPopupContainerForIcon(BubbleTextView originalIcon) {
-        PopupContainerWithArrow.showForIcon(originalIcon);
-    }
 }
diff --git a/src/com/android/launcher3/popup/PopupDataProvider.java b/src/com/android/launcher3/popup/PopupDataProvider.java
index 962dffd..5f17959 100644
--- a/src/com/android/launcher3/popup/PopupDataProvider.java
+++ b/src/com/android/launcher3/popup/PopupDataProvider.java
@@ -241,13 +241,6 @@
         writer.println(prefix + "\tmPackageUserToDotInfos:" + mPackageUserToDotInfos);
     }
 
-    /**
-     * Tells the listener that the system shortcuts have been updated, causing them to be redrawn.
-     */
-    public void redrawSystemShortcuts() {
-        mChangeListener.onSystemShortcutsUpdated();
-    }
-
     public interface PopupDataChangeListener {
 
         PopupDataChangeListener INSTANCE = new PopupDataChangeListener() { };
@@ -256,8 +249,5 @@
 
         /** A callback to get notified when recommended widgets are bound. */
         default void onRecommendedWidgetsBound() { }
-
-        /** A callback to get notified when system shortcuts have been updated. */
-        default void onSystemShortcutsUpdated() { }
     }
 }
diff --git a/src/com/android/launcher3/popup/PopupLiveUpdateHandler.java b/src/com/android/launcher3/popup/PopupLiveUpdateHandler.java
index 9d6f2a5..4c94f94 100644
--- a/src/com/android/launcher3/popup/PopupLiveUpdateHandler.java
+++ b/src/com/android/launcher3/popup/PopupLiveUpdateHandler.java
@@ -18,7 +18,6 @@
 import android.content.Context;
 import android.view.View;
 
-import com.android.launcher3.BubbleTextView;
 import com.android.launcher3.views.ActivityContext;
 
 /**
@@ -56,12 +55,4 @@
             popupDataProvider.setChangeListener(null);
         }
     }
-
-    @Override
-    public void onSystemShortcutsUpdated() {
-        mPopupContainerWithArrow.close(true);
-        showPopupContainerForIcon(mPopupContainerWithArrow.getOriginalIcon());
-    }
-
-    protected abstract void showPopupContainerForIcon(BubbleTextView originalIcon);
 }
diff --git a/src/com/android/launcher3/popup/RemoteActionShortcut.java b/src/com/android/launcher3/popup/RemoteActionShortcut.java
index eab0969..8df58d2 100644
--- a/src/com/android/launcher3/popup/RemoteActionShortcut.java
+++ b/src/com/android/launcher3/popup/RemoteActionShortcut.java
@@ -119,9 +119,4 @@
                     .show();
         }
     }
-
-    @Override
-    public boolean isLeftGroup() {
-        return true;
-    }
 }
diff --git a/src/com/android/launcher3/popup/SystemShortcut.java b/src/com/android/launcher3/popup/SystemShortcut.java
index fa7700b..3030ed4 100644
--- a/src/com/android/launcher3/popup/SystemShortcut.java
+++ b/src/com/android/launcher3/popup/SystemShortcut.java
@@ -49,7 +49,6 @@
 public abstract class SystemShortcut<T extends Context & ActivityContext> extends ItemInfo
         implements View.OnClickListener {
 
-    private static final String TAG = SystemShortcut.class.getSimpleName();
     private final int mIconResId;
     protected final int mLabelResId;
     protected int mAccessibilityActionId;
@@ -58,11 +57,6 @@
     protected final ItemInfo mItemInfo;
     protected final View mOriginalView;
 
-    /**
-     * Indicates if it's invokable or not through some disabled UI
-     */
-    private boolean isEnabled = true;
-
     public SystemShortcut(int iconResId, int labelResId, T target, ItemInfo itemInfo,
             View originalView) {
         mIconResId = iconResId;
@@ -82,24 +76,14 @@
         mOriginalView = other.mOriginalView;
     }
 
-    /**
-     * Should be in the left group of icons in app's context menu header.
-     */
-    public boolean isLeftGroup() {
-        return false;
-    }
-
     public void setIconAndLabelFor(View iconView, TextView labelView) {
         iconView.setBackgroundResource(mIconResId);
-        iconView.setEnabled(isEnabled);
         labelView.setText(mLabelResId);
-        labelView.setEnabled(isEnabled);
     }
 
     public void setIconAndContentDescriptionFor(ImageView view) {
         view.setImageResource(mIconResId);
         view.setContentDescription(view.getContext().getText(mLabelResId));
-        view.setEnabled(isEnabled);
     }
 
     public AccessibilityNodeInfo.AccessibilityAction createAccessibilityAction(Context context) {
@@ -107,14 +91,6 @@
                 mAccessibilityActionId, context.getText(mLabelResId));
     }
 
-    public void setEnabled(boolean enabled) {
-        isEnabled = enabled;
-    }
-
-    public boolean isEnabled() {
-        return isEnabled;
-    }
-
     public boolean hasHandlerForAction(int action) {
         return mAccessibilityActionId == action;
     }