OSDN Git Service

Align id selection from QueryTask to sort order from Media query
authorAlan Newberger <alann@google.com>
Mon, 27 Oct 2014 18:29:34 +0000 (11:29 -0700)
committerAlan Newberger <alann@google.com>
Mon, 27 Oct 2014 18:59:00 +0000 (11:59 -0700)
In QueryTask we pick the id from the first result and consider it
the max id, yet we query ordered by date taken. On most phones
they're the same but by correcting the query to satisfy the
assumption in QueryTask, we remove costly scenarios where most
photos are reprocessed in LoadNewPhotosTask. One line fix in
LocalMediaData with more verbose logging added in CameraDataAdapter.

Bug: 17437217
Change-Id: Ia643f38d1bfb77ff0beaba989f0effe9a4dec31c

src/com/android/camera/data/CameraDataAdapter.java
src/com/android/camera/data/LocalMediaData.java

index 07100ef..7fd2d09 100644 (file)
@@ -180,7 +180,7 @@ public class CameraDataAdapter implements LocalDataAdapter {
         int pos = findDataByContentUri(uri);
         if (pos != -1) {
             // a duplicate one, just do a substitute.
-            Log.v(TAG, "found duplicate data");
+            Log.v(TAG, "found duplicate data: " + uri);
             updateData(pos, newData);
             return false;
         } else {
@@ -339,6 +339,7 @@ public class CameraDataAdapter implements LocalDataAdapter {
         @Override
         protected List<LocalData> doInBackground(ContentResolver... contentResolvers) {
             if (mMinPhotoId != LocalMediaData.QUERY_ALL_MEDIA_ID) {
+                Log.v(TAG, "updating media metadata with photos newer than id: " + mMinPhotoId);
                 final ContentResolver cr = contentResolvers[0];
                 return LocalMediaData.PhotoData.query(cr, LocalMediaData.PhotoData.CONTENT_URI,
                         mMinPhotoId);
@@ -348,11 +349,19 @@ public class CameraDataAdapter implements LocalDataAdapter {
 
         @Override
         protected void onPostExecute(List<LocalData> newPhotoData) {
+            if (newPhotoData == null) {
+                Log.w(TAG, "null data returned from new photos query");
+                return;
+            }
+            Log.v(TAG, "new photos query return num items: " + newPhotoData.size());
             if (!newPhotoData.isEmpty()) {
                 LocalData newestPhoto = newPhotoData.get(0);
                 // We may overlap with another load task or a query task, in which case we want
                 // to be sure we never decrement the oldest seen id.
-                mLastPhotoId = Math.max(mLastPhotoId, newestPhoto.getContentId());
+                long newLastPhotoId = newestPhoto.getContentId();
+                Log.v(TAG, "updating last photo id (old:new) " +
+                        mLastPhotoId + ":" + newLastPhotoId);
+                mLastPhotoId = Math.max(mLastPhotoId, newLastPhotoId);
             }
             // We may add data that is already present, but if we do, it will be deduped in addData.
             // addData does not dedupe session items, so we ignore them here
@@ -406,12 +415,27 @@ public class CameraDataAdapter implements LocalDataAdapter {
 
             long lastPhotoId = LocalMediaData.QUERY_ALL_MEDIA_ID;
             if (!photoData.isEmpty()) {
+                // This relies on {@link LocalMediaData.QUERY_ORDER} returning
+                // items sorted descending by ID, as such we can just pull the
+                // ID from the first item in the result to establish the last
+                // (max) photo ID.
                 lastPhotoId = photoData.get(0).getContentId();
             }
 
-            l.addAll(photoData);
-            l.addAll(videoData);
+            if (photoData != null) {
+                Log.v(TAG, "retrieved photo metadata, number of items: " + photoData.size());
+                l.addAll(photoData);
+            }
+            if (videoData != null) {
+                Log.v(TAG, "retrieved video metadata, number of items: " + videoData.size());
+                l.addAll(videoData);
+            }
+            Log.v(TAG, "sorting video/photo metadata");
+            // Photos should be sorted within photo/video by ID, which in most
+            // cases should correlate well to the date taken/modified. This sort
+            // operation makes all photos/videos sorted by date in one list.
             l.sort(new LocalData.NewestFirstComparator());
+            Log.v(TAG, "sorted video/photo metadata");
 
             // Load enough metadata so it's already loaded when we open the filmstrip.
             for (int i = 0; i < MAX_METADATA && i < l.size(); i++) {
index ab57058..83399e6 100644 (file)
@@ -333,8 +333,10 @@ public abstract class LocalMediaData implements LocalData {
 
         static final Uri CONTENT_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
 
-        private static final String QUERY_ORDER = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC, "
-                + MediaStore.Images.ImageColumns._ID + " DESC";
+        // Sort all data by ID. This must be aligned with
+        // {@link CameraDataAdapter.QueryTask} which relies on the highest ID
+        // being first in any data returned.
+        private static final String QUERY_ORDER = MediaStore.Images.ImageColumns._ID + " DESC";
         /**
          * These values should be kept in sync with column IDs (COL_*) above.
          */