Fixing up handling of shortcuts

Change-Id: I70dd044b608f4ef18a9c436964a11122168305d0
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 46943b5..db7a2ba 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -44,6 +44,7 @@
 import android.content.SharedPreferences;
 import android.content.pm.ActivityInfo;
 import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.res.Configuration;
 import android.content.res.Resources;
@@ -2042,7 +2043,8 @@
             final Intent intent = ((ShortcutInfo) tag).intent;
 
             ComponentName widgetComp = new ComponentName(this, WidgetAdder.class);
-            if (intent.getComponent().getClassName().equals(widgetComp.getClassName())) {
+            if (intent.getComponent() != null &&
+                    intent.getComponent().getClassName().equals(widgetComp.getClassName())) {
                 showAllApps(true);
                 return;
             }
@@ -3641,11 +3643,11 @@
         mWorkspaceLoading = false;
         if (upgradePath) {
             mWorkspace.saveWorkspaceToDb();
-
-            // Run through this twice... a little hackleberry, but the right solution is complex.
-            mWorkspace.stripDuplicateApps();
-            mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
+            ArrayList<ComponentName> allApps = constructAllAppsComponents();
+            mWorkspace.stripDuplicateApps(allApps);
+            mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps(allApps);
         }
+
         mWorkspace.post(new Runnable() {
             @Override
             public void run() {
@@ -3654,6 +3656,25 @@
         });
     }
 
+    private ArrayList<ComponentName> constructAllAppsComponents() {
+        // Run through this twice... a little hackleberry, but the right solution is complex.
+        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
+        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
+
+        List<ResolveInfo> apps = getPackageManager().queryIntentActivities(mainIntent, 0);
+        ArrayList<ComponentName> allApps = new ArrayList<ComponentName>();
+
+        int count = apps.size();
+        for (int i = 0; i < count; i++) {
+            ActivityInfo ai = apps.get(i).activityInfo;
+
+            ComponentName cn =
+                    new ComponentName(ai.applicationInfo.packageName, ai.name);
+            allApps.add(cn);
+        }
+        return allApps;
+    }
+
     private boolean canRunNewAppsAnimation() {
         long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
         return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index abf3b98..dc784ee 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -29,6 +29,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.content.pm.ResolveInfo;
 import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
@@ -39,6 +40,7 @@
 import android.graphics.Rect;
 import android.graphics.Region.Op;
 import android.graphics.drawable.Drawable;
+import android.net.Uri;
 import android.os.IBinder;
 import android.os.Parcelable;
 import android.util.AttributeSet;
@@ -60,6 +62,7 @@
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
 /**
@@ -3427,18 +3430,19 @@
         }
     }
 
-    ArrayList<ComponentName> stripDuplicateApps() {
+    ArrayList<ComponentName> stripDuplicateApps(ArrayList<ComponentName> allApps) {
         ArrayList<ComponentName> uniqueIntents = new ArrayList<ComponentName>();
-        stripDuplicateApps((CellLayout) mLauncher.getHotseat().getLayout(), uniqueIntents);
+        stripDuplicateApps((CellLayout) mLauncher.getHotseat().getLayout(), uniqueIntents, allApps);
         int count = getChildCount();
         for (int i = 0; i < count; i++) {
             CellLayout cl = (CellLayout) getChildAt(i);
-            stripDuplicateApps(cl, uniqueIntents);
+            stripDuplicateApps(cl, uniqueIntents, allApps);
         }
         return uniqueIntents;
     }
 
-    void stripDuplicateApps(CellLayout cl,  ArrayList<ComponentName> uniqueIntents) {
+    void stripDuplicateApps(CellLayout cl, ArrayList<ComponentName> uniqueIntents,
+            ArrayList<ComponentName> allApps) {
         int count = cl.getShortcutsAndWidgets().getChildCount();
 
         ArrayList<View> children = new ArrayList<View>();
@@ -3454,7 +3458,15 @@
             if (info instanceof ShortcutInfo) {
                 ShortcutInfo si = (ShortcutInfo) info;
                 ComponentName cn = si.intent.getComponent();
+                Uri dataUri = si.intent.getData();
 
+                // If dataUri is not null / empty or if this component isn't one that would
+                // have previously showed up in the AllApps list, then this is a widget-type
+                // shortcut, so ignore it.
+                if ((dataUri != null && !dataUri.equals(Uri.EMPTY))
+                        || !allApps.contains(cn)) {
+                    continue;
+                }
                 if (!uniqueIntents.contains(cn)) {
                     uniqueIntents.add(cn);
                 } else {
@@ -3469,7 +3481,15 @@
                     if (items.get(j).getTag() instanceof ShortcutInfo) {
                         ShortcutInfo si = (ShortcutInfo) items.get(j).getTag();
                         ComponentName cn = si.intent.getComponent();
+                        Uri dataUri = si.intent.getData();
 
+                        // If dataUri is not null / empty or if this component isn't one that would
+                        // have previously showed up in the AllApps list, then this is a widget-type
+                        // shortcut, so ignore it.
+                        if (dataUri != null && !dataUri.equals(Uri.EMPTY)
+                                || !allApps.contains(cn)) {
+                            continue;
+                        }
                         if (!uniqueIntents.contains(cn)) {
                             uniqueIntents.add(cn);
                         } else {