Only fetching firstInstallTime for an activity when required

Change-Id: Ia16c05718938849f4250802d8770ddfe6dbef055
diff --git a/src/com/android/launcher3/AppInfo.java b/src/com/android/launcher3/AppInfo.java
index c95d558..ede6c71 100644
--- a/src/com/android/launcher3/AppInfo.java
+++ b/src/com/android/launcher3/AppInfo.java
@@ -51,11 +51,6 @@
      */
     boolean usingLowResIcon;
 
-    /**
-     * The time at which the app was first installed.
-     */
-    long firstInstallTime;
-
     public ComponentName componentName;
 
     static final int DOWNLOADED_FLAG = 1;
@@ -84,7 +79,6 @@
         this.container = ItemInfo.NO_ID;
 
         flags = initFlags(info);
-        firstInstallTime = info.getFirstInstallTime();
         iconCache.getTitleAndIcon(this, info, true /* useLowResIcon */);
         intent = makeLaunchIntent(context, info, user);
         this.user = user;
@@ -109,7 +103,6 @@
         title = Utilities.trim(info.title);
         intent = new Intent(info.intent);
         flags = info.flags;
-        firstInstallTime = info.firstInstallTime;
         iconBitmap = info.iconBitmap;
     }
 
@@ -129,7 +122,6 @@
         Log.d(tag, label + " size=" + list.size());
         for (AppInfo info: list) {
             Log.d(tag, "   title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap 
-                    + " firstInstallTime=" + info.firstInstallTime
                     + " componentName=" + info.componentName.getPackageName());
         }
     }
diff --git a/src/com/android/launcher3/ShortcutInfo.java b/src/com/android/launcher3/ShortcutInfo.java
index 5766cf2..6bdcb4b 100644
--- a/src/com/android/launcher3/ShortcutInfo.java
+++ b/src/com/android/launcher3/ShortcutInfo.java
@@ -52,7 +52,7 @@
     public static final int FLAG_AUTOINTALL_ICON = 2; //0B10;
 
     /**
-     * The icon is being installed. If {@link FLAG_RESTORED_ICON} or {@link FLAG_AUTOINTALL_ICON}
+     * The icon is being installed. If {@link #FLAG_RESTORED_ICON} or {@link #FLAG_AUTOINTALL_ICON}
      * is set, then the icon is either being installed or is in a broken state.
      */
     public static final int FLAG_INSTALL_SESSION_ACTIVE = 4; // 0B100;
@@ -126,19 +126,14 @@
     private int mInstallProgress;
 
     /**
-     * Refer {@link AppInfo#firstInstallTime}.
-     */
-    public long firstInstallTime;
-
-    /**
-     * TODO move this to {@link status}
+     * TODO move this to {@link #status}
      */
     int flags = 0;
 
     /**
      * If this shortcut is a placeholder, then intent will be a market intent for the package, and
      * this will hold the original intent from the database.  Otherwise, null.
-     * Refer {@link #FLAG_RESTORE_PENDING}, {@link #FLAG_INSTALL_PENDING}
+     * Refer {@link #FLAG_RESTORED_ICON}, {@link #FLAG_AUTOINTALL_ICON}
      */
     Intent promisedIntent;
 
@@ -172,7 +167,6 @@
         mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
         customIcon = info.customIcon;
         flags = info.flags;
-        firstInstallTime = info.firstInstallTime;
         user = info.user;
         status = info.status;
     }
@@ -184,7 +178,6 @@
         intent = new Intent(info.intent);
         customIcon = false;
         flags = info.flags;
-        firstInstallTime = info.firstInstallTime;
     }
 
     public void setIcon(Bitmap b) {
@@ -293,7 +286,6 @@
         shortcut.intent = AppInfo.makeLaunchIntent(context, info, info.getUser());
         shortcut.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
         shortcut.flags = AppInfo.initFlags(info);
-        shortcut.firstInstallTime = info.getFirstInstallTime();
         return shortcut;
     }
 }
diff --git a/src/com/android/launcher3/util/ManagedProfileHeuristic.java b/src/com/android/launcher3/util/ManagedProfileHeuristic.java
index 849b05c..fb9bbb2 100644
--- a/src/com/android/launcher3/util/ManagedProfileHeuristic.java
+++ b/src/com/android/launcher3/util/ManagedProfileHeuristic.java
@@ -42,6 +42,7 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -85,6 +86,7 @@
 
     private ArrayList<ShortcutInfo> mHomescreenApps;
     private ArrayList<ShortcutInfo> mWorkFolderApps;
+    private HashMap<ShortcutInfo, Long> mShortcutToInstallTimeMap;
 
     private ManagedProfileHeuristic(Context context, UserHandleCompat user) {
         mContext = context;
@@ -100,32 +102,29 @@
                 Context.MODE_PRIVATE);
     }
 
+    private void initVars() {
+        mHomescreenApps = new ArrayList<>();
+        mWorkFolderApps = new ArrayList<>();
+        mShortcutToInstallTimeMap = new HashMap<>();
+    }
+
     /**
      * Checks the list of user apps and adds icons for newly installed apps on the homescreen or
      * workfolder.
      */
     public void processUserApps(List<LauncherActivityInfoCompat> apps) {
-        mHomescreenApps = new ArrayList<>();
-        mWorkFolderApps = new ArrayList<>();
+        initVars();
 
         HashSet<String> packageSet = new HashSet<>();
         final boolean userAppsExisted = getUserApps(packageSet);
 
         boolean newPackageAdded = false;
-
         for (LauncherActivityInfoCompat info : apps) {
             String packageName = info.getComponentName().getPackageName();
             if (!packageSet.contains(packageName)) {
                 packageSet.add(packageName);
                 newPackageAdded = true;
-
-                try {
-                    PackageInfo pkgInfo = mContext.getPackageManager()
-                            .getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
-                    markForAddition(info, pkgInfo.firstInstallTime);
-                } catch (NameNotFoundException e) {
-                    Log.e(TAG, "Unknown package " + packageName, e);
-                }
+                markForAddition(info, info.getFirstInstallTime());
             }
         }
 
@@ -142,7 +141,22 @@
         ArrayList<ShortcutInfo> targetList =
                 (installTime <= mUserCreationTime + AUTO_ADD_TO_FOLDER_DURATION) ?
                         mWorkFolderApps : mHomescreenApps;
-        targetList.add(ShortcutInfo.fromActivityInfo(info, mContext));
+        ShortcutInfo si = ShortcutInfo.fromActivityInfo(info, mContext);
+        mShortcutToInstallTimeMap.put(si, installTime);
+        targetList.add(si);
+    }
+
+    private void sortList(ArrayList<ShortcutInfo> infos) {
+        Collections.sort(infos, new Comparator<ShortcutInfo>() {
+
+            @Override
+            public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
+                Long lhsTime = mShortcutToInstallTimeMap.get(lhs);
+                Long rhsTime = mShortcutToInstallTimeMap.get(rhs);
+                return Utilities.longCompare(lhsTime == null ? 0 : lhsTime,
+                        rhsTime == null ? 0 : rhsTime);
+            }
+        });
     }
 
     /**
@@ -152,13 +166,7 @@
         if (mWorkFolderApps.isEmpty()) {
             return;
         }
-        Collections.sort(mWorkFolderApps, new Comparator<ShortcutInfo>() {
-
-            @Override
-            public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
-                return Utilities.longCompare(lhs.firstInstallTime, rhs.firstInstallTime);
-            }
-        });
+        sortList(mWorkFolderApps);
 
         // Try to get a work folder.
         String folderIdKey = USER_FOLDER_ID_PREFIX + mUserSerial;
@@ -222,6 +230,7 @@
         finalizeWorkFolder();
 
         if (addHomeScreenShortcuts && !mHomescreenApps.isEmpty()) {
+            sortList(mHomescreenApps);
             mModel.addAndBindAddedWorkspaceItems(mContext, mHomescreenApps);
         }
     }
@@ -230,9 +239,7 @@
      * Updates the list of installed apps and adds any new icons on homescreen or work folder.
      */
     public void processPackageAdd(String[] packages) {
-        mHomescreenApps = new ArrayList<>();
-        mWorkFolderApps = new ArrayList<>();
-
+        initVars();
         HashSet<String> packageSet = new HashSet<>();
         final boolean userAppsExisted = getUserApps(packageSet);