Updates OptionsPopupView to allow non-resource strings/icons.

This will be needed for Smartspace which gets its menu options
from AiAi.

Test: Hard-coded strings passed to Smartspace from the fake
data Launcher plugin. Also verified could access the menu
using TalkBack.
Bug: 184179894

Change-Id: I724c09db18085585c7a5224e32c0d8973acdab7c
diff --git a/src/com/android/launcher3/accessibility/LauncherAccessibilityDelegate.java b/src/com/android/launcher3/accessibility/LauncherAccessibilityDelegate.java
index db7fd3f..a5852ba 100644
--- a/src/com/android/launcher3/accessibility/LauncherAccessibilityDelegate.java
+++ b/src/com/android/launcher3/accessibility/LauncherAccessibilityDelegate.java
@@ -265,7 +265,7 @@
             List<OptionItem> actions = getSupportedResizeActions(host, info);
             Rect pos = new Rect();
             mLauncher.getDragLayer().getDescendantRectRelativeToSelf(host, pos);
-            ArrowPopup popup = OptionsPopupView.show(mLauncher, new RectF(pos), actions);
+            ArrowPopup popup = OptionsPopupView.show(mLauncher, new RectF(pos), actions, false);
             popup.requestFocus();
             popup.setOnCloseCallback(host::requestFocus);
             return true;
@@ -294,15 +294,17 @@
         if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0) {
             if (layout.isRegionVacant(info.cellX + info.spanX, info.cellY, 1, info.spanY) ||
                     layout.isRegionVacant(info.cellX - 1, info.cellY, 1, info.spanY)) {
-                actions.add(new OptionItem(
-                        R.string.action_increase_width, R.drawable.ic_widget_width_increase,
+                actions.add(new OptionItem(mLauncher,
+                        R.string.action_increase_width,
+                        R.drawable.ic_widget_width_increase,
                         IGNORE,
                         v -> performResizeAction(R.string.action_increase_width, host, info)));
             }
 
             if (info.spanX > info.minSpanX && info.spanX > 1) {
-                actions.add(new OptionItem(
-                        R.string.action_decrease_width, R.drawable.ic_widget_width_decrease,
+                actions.add(new OptionItem(mLauncher,
+                        R.string.action_decrease_width,
+                        R.drawable.ic_widget_width_decrease,
                         IGNORE,
                         v -> performResizeAction(R.string.action_decrease_width, host, info)));
             }
@@ -311,15 +313,17 @@
         if ((providerInfo.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
             if (layout.isRegionVacant(info.cellX, info.cellY + info.spanY, info.spanX, 1) ||
                     layout.isRegionVacant(info.cellX, info.cellY - 1, info.spanX, 1)) {
-                actions.add(new OptionItem(
-                        R.string.action_increase_height, R.drawable.ic_widget_height_increase,
+                actions.add(new OptionItem(mLauncher,
+                        R.string.action_increase_height,
+                        R.drawable.ic_widget_height_increase,
                         IGNORE,
                         v -> performResizeAction(R.string.action_increase_height, host, info)));
             }
 
             if (info.spanY > info.minSpanY && info.spanY > 1) {
-                actions.add(new OptionItem(
-                        R.string.action_decrease_height, R.drawable.ic_widget_height_decrease,
+                actions.add(new OptionItem(mLauncher,
+                        R.string.action_decrease_height,
+                        R.drawable.ic_widget_height_decrease,
                         IGNORE,
                         v -> performResizeAction(R.string.action_decrease_height, host, info)));
             }
diff --git a/src/com/android/launcher3/views/AccessibilityActionsView.java b/src/com/android/launcher3/views/AccessibilityActionsView.java
index 0eacaa3..1d136c3 100644
--- a/src/com/android/launcher3/views/AccessibilityActionsView.java
+++ b/src/com/android/launcher3/views/AccessibilityActionsView.java
@@ -67,7 +67,7 @@
         info.addAction(new AccessibilityAction(
                 R.string.all_apps_button_label, l.getText(R.string.all_apps_button_label)));
         for (OptionItem item : OptionsPopupView.getOptions(l)) {
-            info.addAction(new AccessibilityAction(item.labelRes, l.getText(item.labelRes)));
+            info.addAction(new AccessibilityAction(item.labelRes, item.label));
         }
         return info;
     }
diff --git a/src/com/android/launcher3/views/OptionsPopupView.java b/src/com/android/launcher3/views/OptionsPopupView.java
index 9a2db10..98cc876 100644
--- a/src/com/android/launcher3/views/OptionsPopupView.java
+++ b/src/com/android/launcher3/views/OptionsPopupView.java
@@ -26,6 +26,7 @@
 import android.content.Intent;
 import android.graphics.Rect;
 import android.graphics.RectF;
+import android.graphics.drawable.Drawable;
 import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.AttributeSet;
@@ -37,6 +38,7 @@
 
 import androidx.annotation.Nullable;
 import androidx.annotation.VisibleForTesting;
+import androidx.core.content.ContextCompat;
 
 import com.android.launcher3.Launcher;
 import com.android.launcher3.LauncherSettings;
@@ -62,6 +64,7 @@
 
     private final ArrayMap<View, OptionItem> mItemMap = new ArrayMap<>();
     private RectF mTargetRect;
+    private boolean mShouldAddArrow;
 
     public OptionsPopupView(Context context, AttributeSet attrs) {
         this(context, attrs, 0);
@@ -113,9 +116,13 @@
         return (type & TYPE_OPTIONS_POPUP) != 0;
     }
 
+    public void setShouldAddArrow(boolean shouldAddArrow) {
+        mShouldAddArrow = shouldAddArrow;
+    }
+
     @Override
     protected boolean shouldAddArrow() {
-        return false;
+        return mShouldAddArrow;
     }
 
     @Override
@@ -124,16 +131,17 @@
     }
 
     public static OptionsPopupView show(
-            Launcher launcher, RectF targetRect, List<OptionItem> items) {
+            Launcher launcher, RectF targetRect, List<OptionItem> items, boolean shouldAddArrow) {
         OptionsPopupView popup = (OptionsPopupView) launcher.getLayoutInflater()
                 .inflate(R.layout.longpress_options_menu, launcher.getDragLayer(), false);
         popup.mTargetRect = targetRect;
+        popup.setShouldAddArrow(shouldAddArrow);
 
         for (OptionItem item : items) {
             DeepShortcutView view =
                     (DeepShortcutView) popup.inflateAndAdd(R.layout.system_shortcut, popup);
-            view.getIconView().setBackgroundResource(item.iconRes);
-            view.getBubbleText().setText(item.labelRes);
+            view.getIconView().setBackgroundDrawable(item.icon);
+            view.getBubbleText().setText(item.label);
             view.setOnClickListener(popup);
             view.setOnLongClickListener(popup);
             popup.mItemMap.put(view, item);
@@ -154,7 +162,7 @@
             y = launcher.getDragLayer().getHeight() / 2;
         }
         RectF target = new RectF(x - halfSize, y - halfSize, x + halfSize, y + halfSize);
-        show(launcher, target, getOptions(launcher));
+        show(launcher, target, getOptions(launcher), false);
     }
 
     /**
@@ -162,11 +170,15 @@
      */
     public static ArrayList<OptionItem> getOptions(Launcher launcher) {
         ArrayList<OptionItem> options = new ArrayList<>();
-        options.add(new OptionItem(R.string.settings_button_text, R.drawable.ic_setting,
+        options.add(new OptionItem(launcher,
+                R.string.settings_button_text,
+                R.drawable.ic_setting,
                 LAUNCHER_SETTINGS_BUTTON_TAP_OR_LONGPRESS,
                 OptionsPopupView::startSettings));
         if (!WidgetsModel.GO_DISABLE_WIDGETS) {
-            options.add(new OptionItem(R.string.widget_button_text, R.drawable.ic_widget,
+            options.add(new OptionItem(launcher,
+                    R.string.widget_button_text,
+                    R.drawable.ic_widget,
                     LAUNCHER_WIDGETSTRAY_BUTTON_TAP_OR_LONGPRESS,
                     OptionsPopupView::onWidgetsClicked));
         }
@@ -174,7 +186,9 @@
                 R.string.styles_wallpaper_button_text : R.string.wallpaper_button_text;
         int resDrawable = Utilities.existsStyleWallpapers(launcher) ?
                 R.drawable.ic_palette : R.drawable.ic_wallpaper;
-        options.add(new OptionItem(resString, resDrawable,
+        options.add(new OptionItem(launcher,
+                resString,
+                resDrawable,
                 IGNORE,
                 OptionsPopupView::startWallpaperPicker));
         return options;
@@ -241,15 +255,28 @@
 
     public static class OptionItem {
 
+        // Used to create AccessibilityNodeInfo in AccessibilityActionsView.java.
         public final int labelRes;
-        public final int iconRes;
+
+        public final CharSequence label;
+        public final Drawable icon;
         public final EventEnum eventId;
         public final OnLongClickListener clickListener;
 
-        public OptionItem(int labelRes, int iconRes, EventEnum eventId,
-                OnLongClickListener clickListener) {
+        public OptionItem(Context context, int labelRes, int iconRes, EventEnum eventId,
+                          OnLongClickListener clickListener) {
             this.labelRes = labelRes;
-            this.iconRes = iconRes;
+            this.label = context.getText(labelRes);
+            this.icon = ContextCompat.getDrawable(context, iconRes);
+            this.eventId = eventId;
+            this.clickListener = clickListener;
+        }
+
+        public OptionItem(CharSequence label, Drawable icon, EventEnum eventId,
+                          OnLongClickListener clickListener) {
+            this.labelRes = 0;
+            this.label = label;
+            this.icon = icon;
             this.eventId = eventId;
             this.clickListener = clickListener;
         }