Fix bug with user of shortcut not being remembered over restart.

Intent extra was being set as int rather than long so being
missed.
Also use LauncherApps if neccessary in case where launching
without launch animation.
Unregister for package notifications when terminating.

Bug: 14636181
Change-Id: I1575f6eed806446782092dffe8c01afe7b6507fe
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index bf2a02d..01b1cde 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -2638,23 +2638,26 @@
                     !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
             LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
             UserManagerCompat userManager = UserManagerCompat.getInstance(this);
-            long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, 0);
-            UserHandleCompat user = serialNumber == 0 ? null :
-                    userManager.getUserForSerialNumber(serialNumber);
 
+            UserHandleCompat user = null;
+            if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
+                long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
+                user = userManager.getUserForSerialNumber(serialNumber);
+            }
+
+            Bundle optsBundle = null;
             if (useLaunchAnimation) {
                 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
                         v.getMeasuredWidth(), v.getMeasuredHeight());
-                if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
-                    // Could be launching some bookkeeping activity
-                    startActivity(intent, opts.toBundle());
-                } else {
-                    launcherApps.startActivityForProfile(intent.getComponent(),
-                            intent.getSourceBounds(),
-                            opts.toBundle(), user);
-                }
+                optsBundle = opts.toBundle();
+            }
+
+            if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
+                // Could be launching some bookkeeping activity
+                startActivity(intent, optsBundle);
             } else {
-                startActivity(intent);
+                launcherApps.startActivityForProfile(intent.getComponent(),
+                        intent.getSourceBounds(), optsBundle, user);
             }
             return true;
         } catch (SecurityException e) {
diff --git a/src/com/android/launcher3/LauncherAppState.java b/src/com/android/launcher3/LauncherAppState.java
index 0725a65..79bc084 100644
--- a/src/com/android/launcher3/LauncherAppState.java
+++ b/src/com/android/launcher3/LauncherAppState.java
@@ -114,7 +114,7 @@
         resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
                 mFavoritesObserver);
     }
-    
+
     public void recreateWidgetPreviewDb() {
         if (mWidgetPreviewCacheDb != null) {
             mWidgetPreviewCacheDb.close();
@@ -127,6 +127,8 @@
      */
     public void onTerminate() {
         sContext.unregisterReceiver(mModel);
+        final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
+        launcherApps.removeOnAppsChangedListener(mModel);
 
         ContentResolver resolver = sContext.getContentResolver();
         resolver.unregisterContentObserver(mFavoritesObserver);
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index f0ffe1d..8af0970 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -902,7 +902,7 @@
                 item.container = c.getInt(containerIndex);
                 item.itemType = c.getInt(itemTypeIndex);
                 item.screenId = c.getInt(screenIndex);
-                int serialNumber = c.getInt(profileIdIndex);
+                long serialNumber = c.getInt(profileIdIndex);
                 item.user = userManager.getUserForSerialNumber(serialNumber);
                 // Skip if user has been deleted.
                 if (item.user != null) {
@@ -1894,7 +1894,7 @@
                             case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
                                 id = c.getLong(idIndex);
                                 intentDescription = c.getString(intentIndex);
-                                int serialNumber = c.getInt(profileIdIndex);
+                                long serialNumber = c.getInt(profileIdIndex);
                                 user = mUserManager.getUserForSerialNumber(serialNumber);
                                 if (user == null) {
                                     // User has been deleted remove the item.