Adding a provider class for various popup actions
so that it can be customized easily.

Change-Id: I633846631ac3d4c70ebb3ea35a3edc2feee8339e
diff --git a/res/values/config.xml b/res/values/config.xml
index 2a6b25c..9f97478 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -79,27 +79,15 @@
 <!-- Hotseat -->
     <bool name="hotseat_transpose_layout_with_orientation">true</bool>
 
-    <!-- Name of a subclass of com.android.launcher3.AppFilter used to
-         filter the activities shown in the launcher. Can be empty. -->
+    <!-- Various classes overriden by projects/build flavors. -->
     <string name="app_filter_class" translatable="false"></string>
-
-    <!-- Name of an icon provider class. -->
     <string name="icon_provider_class" translatable="false"></string>
-
-    <!-- Name of a drawable factory class. -->
     <string name="drawable_factory_class" translatable="false"></string>
-
-    <!-- Name of a user event dispatcher class. -->
     <string name="user_event_dispatcher_class" translatable="false"></string>
-
-    <!-- Name of an app transition manager class. -->
     <string name="app_transition_manager_class" translatable="false"></string>
-
-    <!-- Name of a subclass of com.android.launcher3.util.InstantAppResolver. Can be empty. -->
     <string name="instant_app_resolver_class" translatable="false"></string>
-
-    <!-- Name of a main process initializer class. -->
     <string name="main_process_initializer_class" translatable="false"></string>
+    <string name="system_shortcut_factory_class" translatable="false"></string>
 
     <!-- Package name of the default wallpaper picker. -->
     <string name="wallpaper_picker_package" translatable="false"></string>
diff --git a/src/com/android/launcher3/popup/PopupContainerWithArrow.java b/src/com/android/launcher3/popup/PopupContainerWithArrow.java
index 10be925..6877cc4 100644
--- a/src/com/android/launcher3/popup/PopupContainerWithArrow.java
+++ b/src/com/android/launcher3/popup/PopupContainerWithArrow.java
@@ -184,17 +184,10 @@
             return null;
         }
 
-        PopupDataProvider popupDataProvider = launcher.getPopupDataProvider();
-        List<String> shortcutIds = popupDataProvider.getShortcutIdsForItem(itemInfo);
-        List<NotificationKeyData> notificationKeys = popupDataProvider
-                .getNotificationKeysForItem(itemInfo);
-        List<SystemShortcut> systemShortcuts = popupDataProvider
-                .getEnabledSystemShortcutsForItem(itemInfo);
-
         final PopupContainerWithArrow container =
                 (PopupContainerWithArrow) launcher.getLayoutInflater().inflate(
                         R.layout.popup_container, launcher.getDragLayer(), false);
-        container.populateAndShow(icon, shortcutIds, notificationKeys, systemShortcuts);
+        container.populateAndShow(icon, itemInfo, SystemShortcutFactory.INSTANCE.get(launcher));
         return container;
     }
 
@@ -219,6 +212,15 @@
         }
     }
 
+    protected void populateAndShow(
+            BubbleTextView icon, ItemInfo item, SystemShortcutFactory factory) {
+        PopupDataProvider popupDataProvider = mLauncher.getPopupDataProvider();
+        populateAndShow(icon,
+                popupDataProvider.getShortcutIdsForItem(item),
+                popupDataProvider.getNotificationKeysForItem(item),
+                factory.getEnabledShortcuts(mLauncher, item));
+    }
+
     @TargetApi(Build.VERSION_CODES.P)
     protected void populateAndShow(final BubbleTextView originalIcon, final List<String> shortcutIds,
             final List<NotificationKeyData> notificationKeys, List<SystemShortcut> systemShortcuts) {
diff --git a/src/com/android/launcher3/popup/PopupDataProvider.java b/src/com/android/launcher3/popup/PopupDataProvider.java
index 3faec44..4d5a9c6 100644
--- a/src/com/android/launcher3/popup/PopupDataProvider.java
+++ b/src/com/android/launcher3/popup/PopupDataProvider.java
@@ -50,13 +50,6 @@
     private static final boolean LOGD = false;
     private static final String TAG = "PopupDataProvider";
 
-    /** Note that these are in order of priority. */
-    private static final SystemShortcut[] SYSTEM_SHORTCUTS = new SystemShortcut[] {
-            new SystemShortcut.AppInfo(),
-            new SystemShortcut.Widgets(),
-            new SystemShortcut.Install()
-    };
-
     private final Launcher mLauncher;
 
     /** Maps launcher activity components to their list of shortcut ids. */
@@ -192,16 +185,6 @@
                 : notificationListener.getNotificationsForKeys(notificationKeys);
     }
 
-    public @NonNull List<SystemShortcut> getEnabledSystemShortcutsForItem(ItemInfo info) {
-        List<SystemShortcut> systemShortcuts = new ArrayList<>();
-        for (SystemShortcut systemShortcut : SYSTEM_SHORTCUTS) {
-            if (systemShortcut.getOnClickListener(mLauncher, info) != null) {
-                systemShortcuts.add(systemShortcut);
-            }
-        }
-        return systemShortcuts;
-    }
-
     public void cancelNotification(String notificationKey) {
         NotificationListener notificationListener = NotificationListener.getInstanceIfConnected();
         if (notificationListener == null) {
diff --git a/src/com/android/launcher3/popup/SystemShortcutFactory.java b/src/com/android/launcher3/popup/SystemShortcutFactory.java
new file mode 100644
index 0000000..516fafa
--- /dev/null
+++ b/src/com/android/launcher3/popup/SystemShortcutFactory.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.launcher3.popup;
+
+import com.android.launcher3.ItemInfo;
+import com.android.launcher3.Launcher;
+import com.android.launcher3.R;
+import com.android.launcher3.util.MainThreadInitializedObject;
+import com.android.launcher3.util.ResourceBasedOverride;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import androidx.annotation.NonNull;
+
+public class SystemShortcutFactory implements ResourceBasedOverride {
+
+    public static final MainThreadInitializedObject<SystemShortcutFactory> INSTANCE =
+            new MainThreadInitializedObject<>(c -> Overrides.getObject(
+                    SystemShortcutFactory.class, c, R.string.system_shortcut_factory_class));
+
+    /** Note that these are in order of priority. */
+    private final SystemShortcut[] mAllShortcuts;
+
+    @SuppressWarnings("unused")
+    public SystemShortcutFactory() {
+        this(new SystemShortcut.AppInfo(),
+                new SystemShortcut.Widgets(), new SystemShortcut.Install());
+    }
+
+    protected SystemShortcutFactory(SystemShortcut... shortcuts) {
+        mAllShortcuts = shortcuts;
+    }
+
+    public @NonNull List<SystemShortcut> getEnabledShortcuts(Launcher launcher, ItemInfo info) {
+        List<SystemShortcut> systemShortcuts = new ArrayList<>();
+        for (SystemShortcut systemShortcut : mAllShortcuts) {
+            if (systemShortcut.getOnClickListener(launcher, info) != null) {
+                systemShortcuts.add(systemShortcut);
+            }
+        }
+        return systemShortcuts;
+    }
+}
diff --git a/src/com/android/launcher3/util/FlagOp.java b/src/com/android/launcher3/util/FlagOp.java
index 5e26ed1..a012c86 100644
--- a/src/com/android/launcher3/util/FlagOp.java
+++ b/src/com/android/launcher3/util/FlagOp.java
@@ -1,30 +1,16 @@
 package com.android.launcher3.util;
 
-public abstract class FlagOp {
+public interface FlagOp {
 
-    public static FlagOp NO_OP = new FlagOp() {};
+    FlagOp NO_OP = i -> i;
 
-    private FlagOp() {}
+    int apply(int flags);
 
-    public int apply(int flags) {
-        return flags;
+    static FlagOp addFlag(int flag) {
+        return i -> i + flag;
     }
 
-    public static FlagOp addFlag(final int flag) {
-        return new FlagOp() {
-            @Override
-            public int apply(int flags) {
-                return flags | flag;
-            }
-        };
-    }
-
-    public static FlagOp removeFlag(final int flag) {
-        return new FlagOp() {
-            @Override
-            public int apply(int flags) {
-                return flags & ~flag;
-            }
-        };
+    static FlagOp removeFlag(int flag) {
+        return i -> i & ~flag;
     }
 }