Gallery: improve timeline loading performance

MediaSet's getTotalMediaItemCount() method involves database queries
which is slow. The call is not needed in time clustering as the items
will be enumerated right after that.

Change-Id: I8eaa68237c923486109935180247188da94f397b
CRs-Fixed: 984190
diff --git a/src/com/android/gallery3d/data/TimeClustering.java b/src/com/android/gallery3d/data/TimeClustering.java
index 87111f2..db47c40 100644
--- a/src/com/android/gallery3d/data/TimeClustering.java
+++ b/src/com/android/gallery3d/data/TimeClustering.java
@@ -98,14 +98,12 @@
 
     @Override
     public void run(MediaSet baseSet) {
-        final int total = baseSet.getTotalMediaItemCount();
-        final SmallItem[] buf = new SmallItem[total];
         final double[] latLng = new double[2];
 
+        final ArrayList<SmallItem> items = new ArrayList<>();
         baseSet.enumerateTotalMediaItems(new MediaSet.ItemConsumer() {
             @Override
             public void consume(int index, MediaItem item) {
-                if (index < 0 || index >= total) return;
                 SmallItem s = new SmallItem();
                 s.path = item.getPath();
                 s.mediaType = item.getMediaType();
@@ -113,17 +111,10 @@
                 item.getLatLong(latLng);
                 s.lat = latLng[0];
                 s.lng = latLng[1];
-                buf[index] = s;
+                items.add(s);
             }
         });
 
-        ArrayList<SmallItem> items = new ArrayList<SmallItem>(total);
-        for (int i = 0; i < total; i++) {
-            if (buf[i] != null) {
-                items.add(buf[i]);
-            }
-        }
-
         Collections.sort(items, sDateComparator);
 
         int n = items.size();