OSDN Git Service

Add _COUNT support for PhotoProvider to help performance of All Photos.
authorGeorge Mount <mount@google.com>
Tue, 9 Apr 2013 20:45:09 +0000 (13:45 -0700)
committerGeorge Mount <mount@google.com>
Thu, 11 Apr 2013 17:16:06 +0000 (10:16 -0700)
 Bug 8594818

Change-Id: Iab14a945e33ffcbf539b1d7acc704538e4bf48b6

src/com/android/photos/data/PhotoProvider.java

index cffd123..d4310ca 100644 (file)
@@ -226,6 +226,9 @@ public class PhotoProvider extends SQLiteContentProvider {
     protected static final String IN = " IN ";
     protected static final String NESTED_SELECT_START = "(";
     protected static final String NESTED_SELECT_END = ")";
+    protected static final String[] PROJECTION_COUNT = {
+        "COUNT(*)"
+    };
 
     /**
      * For selecting the mime-type for an image.
@@ -310,6 +313,7 @@ public class PhotoProvider extends SQLiteContentProvider {
     @Override
     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
             String sortOrder, CancellationSignal cancellationSignal) {
+        projection = replaceCount(projection);
         int match = matchUri(uri);
         selection = addIdToSelection(match, selection);
         selectionArgs = addIdToSelectionArgs(match, uri, selectionArgs);
@@ -521,4 +525,12 @@ public class PhotoProvider extends SQLiteContentProvider {
             return db.query(table, columns, selection, selectionArgs, null, null, orderBy);
         }
     }
+
+    protected static String[] replaceCount(String[] projection) {
+        if (projection != null && projection.length == 1
+                && BaseColumns._COUNT.equals(projection[0])) {
+            return PROJECTION_COUNT;
+        }
+        return projection;
+    }
 }