Merge "Setting context in LauncherAppState before initialising it, as it can be called from a BroadcastReceiver" into ub-now-porkchop
diff --git a/src/com/android/launcher3/IconCache.java b/src/com/android/launcher3/IconCache.java
index ddc1bd9..8a3c319 100644
--- a/src/com/android/launcher3/IconCache.java
+++ b/src/com/android/launcher3/IconCache.java
@@ -229,7 +229,8 @@
             Iterator<Entry<CacheKey, CacheEntry>> it = mCache.entrySet().iterator();
             while (it.hasNext()) {
                 final CacheEntry e = it.next().getValue();
-                if (e.icon.getWidth() < grid.iconSizePx || e.icon.getHeight() < grid.iconSizePx) {
+                if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
+                        || e.icon.getHeight() < grid.iconSizePx)) {
                     it.remove();
                 }
             }
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index f7a0df6..909cf5f 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -4801,6 +4801,25 @@
     }
 
     void updateShortcutsAndWidgets(ArrayList<AppInfo> apps) {
+        // Break the appinfo list per user
+        final HashMap<UserHandleCompat, ArrayList<AppInfo>> appsPerUser =
+                new HashMap<UserHandleCompat, ArrayList<AppInfo>>();
+        for (AppInfo info : apps) {
+            ArrayList<AppInfo> filtered = appsPerUser.get(info.user);
+            if (filtered == null) {
+                filtered = new ArrayList<AppInfo>();
+                appsPerUser.put(info.user, filtered);
+            }
+            filtered.add(info);
+        }
+
+        for (Map.Entry<UserHandleCompat, ArrayList<AppInfo>> entry : appsPerUser.entrySet()) {
+            updateShortcutsAndWidgetsPerUser(entry.getValue(), entry.getKey());
+        }
+    }
+
+    private void updateShortcutsAndWidgetsPerUser(ArrayList<AppInfo> apps,
+            final UserHandleCompat user) {
         // Create a map of the apps to test against
         final HashMap<ComponentName, AppInfo> appsMap = new HashMap<ComponentName, AppInfo>();
         final HashSet<String> pkgNames = new HashSet<String>();
@@ -4808,9 +4827,8 @@
             appsMap.put(ai.componentName, ai);
             pkgNames.add(ai.componentName.getPackageName());
         }
+        final HashSet<ComponentName> iconsToRemove = new HashSet<ComponentName>();
 
-        final HashMap<UserHandleCompat, HashSet<ComponentName>> iconsToRemove =
-                new HashMap<UserHandleCompat, HashSet<ComponentName>>();
         mapOverItems(MAP_RECURSE, new ItemOperator() {
             @Override
             public boolean evaluate(ItemInfo info, View v, View parent) {
@@ -4818,7 +4836,8 @@
                     ShortcutInfo shortcutInfo = (ShortcutInfo) info;
                     ComponentName cn = shortcutInfo.getTargetComponent();
                     AppInfo appInfo = appsMap.get(cn);
-                    if (cn != null && LauncherModel.isShortcutInfoUpdateable(info)
+                    if (user.equals(shortcutInfo.user) && cn != null
+                            && LauncherModel.isShortcutInfoUpdateable(info)
                             && pkgNames.contains(cn.getPackageName())) {
                         boolean promiseStateChanged = false;
                         boolean infoUpdated = false;
@@ -4841,13 +4860,7 @@
 
                                     if ((intent == null) || (appsMap == null)) {
                                         // Could not find a default activity. Remove this item.
-                                        HashSet<ComponentName> cnSet = iconsToRemove
-                                                .get(shortcutInfo.user);
-                                        if (cnSet == null) {
-                                            cnSet = new HashSet<>();
-                                            iconsToRemove.put(shortcutInfo.user, cnSet);
-                                        }
-                                        cnSet.add(shortcutInfo.getTargetComponent());
+                                        iconsToRemove.add(shortcutInfo.getTargetComponent());
 
                                         // process next shortcut.
                                         return false;
@@ -4894,12 +4907,11 @@
         });
 
         if (!iconsToRemove.isEmpty()) {
-            for (Map.Entry<UserHandleCompat, HashSet<ComponentName>> entry :
-                iconsToRemove.entrySet()) {
-                removeItemsByComponentName(entry.getValue(), entry.getKey());
-            }
+            removeItemsByComponentName(iconsToRemove, user);
         }
-        restorePendingWidgets(pkgNames);
+        if (user.equals(UserHandleCompat.myUserHandle())) {
+            restorePendingWidgets(pkgNames);
+        }
     }
 
     public void removeAbandonedPromise(String packageName, UserHandleCompat user) {
@@ -4946,6 +4958,7 @@
             }
         }
 
+        // Note that package states are sent only for myUser
         if (!completedPackages.isEmpty()) {
             restorePendingWidgets(completedPackages);
         }
diff --git a/src/com/android/launcher3/compat/AppWidgetManagerCompat.java b/src/com/android/launcher3/compat/AppWidgetManagerCompat.java
index 57fac7f..5997e7b 100644
--- a/src/com/android/launcher3/compat/AppWidgetManagerCompat.java
+++ b/src/com/android/launcher3/compat/AppWidgetManagerCompat.java
@@ -41,9 +41,9 @@
             // TODO change this to use api version once L gets an API number.
             if (sInstance == null) {
                 if (Utilities.isLmp()) {
-                    sInstance = new AppWidgetManagerCompatVL(context);
+                    sInstance = new AppWidgetManagerCompatVL(context.getApplicationContext());
                 } else {
-                    sInstance = new AppWidgetManagerCompatV16(context);
+                    sInstance = new AppWidgetManagerCompatV16(context.getApplicationContext());
                 }
             }
             return sInstance;
diff --git a/src/com/android/launcher3/compat/AppWidgetManagerCompatVL.java b/src/com/android/launcher3/compat/AppWidgetManagerCompatVL.java
index 7ca35b7..c3853ab 100644
--- a/src/com/android/launcher3/compat/AppWidgetManagerCompatVL.java
+++ b/src/com/android/launcher3/compat/AppWidgetManagerCompatVL.java
@@ -121,15 +121,9 @@
         } else {
             badgeLocation.offset(bitmap.getWidth() - badgeSize - badgeMargin, top);
         }
-        Drawable drawable = null;
-        // STOPSHIP(mokani): Remove catch block once dogfood build is bigger than LRW70.
-        // This hack is just to prevent crash in older builds.
-        try {
-            drawable = mPm.getUserBadgedDrawableForDensity(new BitmapDrawable(res, bitmap),
-                    info.getProfile(), badgeLocation, 0);
-        } catch (Throwable e) {
-            return bitmap;
-        }
+
+        Drawable drawable = mPm.getUserBadgedDrawableForDensity(
+                new BitmapDrawable(res, bitmap), info.getProfile(), badgeLocation, 0);
 
         if (drawable instanceof BitmapDrawable) {
             return ((BitmapDrawable) drawable).getBitmap();
diff --git a/src/com/android/launcher3/compat/LauncherAppsCompat.java b/src/com/android/launcher3/compat/LauncherAppsCompat.java
index de8c669..e3879ee 100644
--- a/src/com/android/launcher3/compat/LauncherAppsCompat.java
+++ b/src/com/android/launcher3/compat/LauncherAppsCompat.java
@@ -51,9 +51,9 @@
             // STOPSHIP(kennyguy) change this to use api version once L gets an API number.
             if (sInstance == null) {
                 if ("L".equals(Build.VERSION.CODENAME)) {
-                    sInstance = new LauncherAppsCompatVL(context);
+                    sInstance = new LauncherAppsCompatVL(context.getApplicationContext());
                 } else {
-                    sInstance = new LauncherAppsCompatV16(context);
+                    sInstance = new LauncherAppsCompatV16(context.getApplicationContext());
                 }
             }
             return sInstance;
diff --git a/src/com/android/launcher3/compat/LauncherAppsCompatV16.java b/src/com/android/launcher3/compat/LauncherAppsCompatV16.java
index c47f223..7e5e6bf 100644
--- a/src/com/android/launcher3/compat/LauncherAppsCompatV16.java
+++ b/src/com/android/launcher3/compat/LauncherAppsCompatV16.java
@@ -22,19 +22,17 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.ActivityInfo;
+import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
-import android.content.pm.PackageInfo;
 import android.content.pm.ResolveInfo;
 import android.graphics.Rect;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
-import android.os.UserHandle;
 import android.provider.Settings;
 
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 
 /**
diff --git a/src/com/android/launcher3/compat/PackageInstallerCompatVL.java b/src/com/android/launcher3/compat/PackageInstallerCompatVL.java
index eba91cb..2d56adf 100644
--- a/src/com/android/launcher3/compat/PackageInstallerCompatVL.java
+++ b/src/com/android/launcher3/compat/PackageInstallerCompatVL.java
@@ -48,11 +48,7 @@
         mResumed = false;
         mBound = false;
 
-        // STOPSHIP(mokani): Remove catch block once dogfood build is bigger than LRW70.
-        // This hack is just to prevent crash in older builds.
-        try {
-            mInstaller.registerSessionCallback(mCallback);
-        } catch (Throwable e) { }
+        mInstaller.registerSessionCallback(mCallback);
 
         // On start, send updates for all active sessions
         for (SessionInfo info : mInstaller.getAllSessions()) {
@@ -78,11 +74,7 @@
 
     @Override
     public void onStop() {
-        // STOPSHIP(mokani): Remove catch block once dogfood build is bigger than LRW70.
-        // This hack is just to prevent crash in older builds.
-        try {
-            mInstaller.unregisterSessionCallback(mCallback);
-        } catch (Throwable e) { }
+        mInstaller.unregisterSessionCallback(mCallback);
     }
 
     @Override
diff --git a/src/com/android/launcher3/compat/UserManagerCompatVL.java b/src/com/android/launcher3/compat/UserManagerCompatVL.java
index ddef431..19eeabd 100644
--- a/src/com/android/launcher3/compat/UserManagerCompatVL.java
+++ b/src/com/android/launcher3/compat/UserManagerCompatVL.java
@@ -51,13 +51,7 @@
 
     @Override
     public Drawable getBadgedDrawableForUser(Drawable unbadged, UserHandleCompat user) {
-        // STOPSHIP(mokani): Remove catch block once dogfood build is bigger than LRW70.
-        // This hack is just to prevent crash in older builds.
-        try {
-            return mPm.getUserBadgedIcon(unbadged, user.getUser());
-        } catch (Throwable e) {
-            return unbadged;
-        }
+        return mPm.getUserBadgedIcon(unbadged, user.getUser());
     }
 
     @Override
@@ -65,13 +59,7 @@
         if (user == null) {
             return label;
         }
-        // STOPSHIP(mokani): Remove catch block once dogfood build is bigger than LRW70.
-        // This hack is just to prevent crash in older builds.
-        try {
-            return mPm.getUserBadgedLabel(label, user.getUser());
-        } catch (Throwable e) {
-            return label;
-        }
+        return mPm.getUserBadgedLabel(label, user.getUser());
     }
 }