Merge changes from topic 'ArcPagingBackport' into arc-apps

* changes:
  Make Sorting/paging honor ENABLE_OMC_FEATURES flag
  Test/Demo paging support in DocsUI.
  Don't sort pre-sorted results.
diff --git a/src/com/android/documentsui/DirectoryLoader.java b/src/com/android/documentsui/DirectoryLoader.java
index 8720701..5c93f76 100644
--- a/src/com/android/documentsui/DirectoryLoader.java
+++ b/src/com/android/documentsui/DirectoryLoader.java
@@ -23,6 +23,7 @@
 import android.database.ContentObserver;
 import android.database.Cursor;
 import android.net.Uri;
+import android.os.Bundle;
 import android.os.CancellationSignal;
 import android.os.Handler;
 import android.os.OperationCanceledException;
@@ -31,6 +32,7 @@
 import android.util.Log;
 
 import com.android.documentsui.archives.ArchivesProvider;
+import com.android.documentsui.base.DebugFlags;
 import com.android.documentsui.base.DocumentInfo;
 import com.android.documentsui.base.FilteringCursorWrapper;
 import com.android.documentsui.base.RootInfo;
@@ -97,8 +99,21 @@
                 ArchivesProvider.acquireArchive(client, mUri);
             }
             result.client = client;
-            cursor = client.query(
-                    mUri, null, null, null, mModel.getDocumentSortQuery(), mSignal);
+
+            if (Shared.ENABLE_OMC_API_FEATURES) {
+                Bundle queryArgs = new Bundle();
+                mModel.addQuerySortArgs(queryArgs);
+
+                // TODO: At some point we don't want forced flags to override real paging...
+                // and that point is when we have real paging.
+                DebugFlags.addForcedPagingArgs(queryArgs);
+
+                cursor = client.query(mUri, null, queryArgs, mSignal);
+            } else {
+                cursor = client.query(
+                        mUri, null, null, null, mModel.getDocumentSortQuery(), mSignal);
+            }
+
             if (cursor == null) {
                 throw new RemoteException("Provider returned null");
             }
diff --git a/src/com/android/documentsui/RecentsLoader.java b/src/com/android/documentsui/RecentsLoader.java
index c3ccbba..ead5e8f 100644
--- a/src/com/android/documentsui/RecentsLoader.java
+++ b/src/com/android/documentsui/RecentsLoader.java
@@ -35,15 +35,16 @@
 
 import com.android.documentsui.base.FilteringCursorWrapper;
 import com.android.documentsui.base.RootInfo;
+import com.android.documentsui.base.Shared;
 import com.android.documentsui.base.State;
 import com.android.documentsui.roots.RootCursorWrapper;
 import com.android.documentsui.roots.RootsAccess;
 import com.android.internal.annotations.GuardedBy;
 
-import libcore.io.IoUtils;
-
 import com.google.common.util.concurrent.AbstractFuture;
 
+import libcore.io.IoUtils;
+
 import java.io.Closeable;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -326,11 +327,17 @@
                 final Cursor[] res = new Cursor[rootIds.size()];
                 mCursors = new Cursor[rootIds.size()];
                 for (int i = 0; i < rootIds.size(); i++) {
-                    final Uri uri = DocumentsContract.buildRecentDocumentsUri(authority,
-                            rootIds.get(i));
+                    final Uri uri =
+                            DocumentsContract.buildRecentDocumentsUri(authority, rootIds.get(i));
                     try {
-                        res[i] = client.query(
-                                uri, null, null, null, mState.sortModel.getDocumentSortQuery());
+                        if (Shared.ENABLE_OMC_API_FEATURES) {
+                            final Bundle queryArgs = new Bundle();
+                            mState.sortModel.addQuerySortArgs(queryArgs);
+                            res[i] = client.query(uri, null, queryArgs, null);
+                        } else {
+                            res[i] = client.query(
+                                    uri, null, null, null, mState.sortModel.getDocumentSortQuery());
+                        }
                         mCursors[i] = new RootCursorWrapper(authority, rootIds.get(i), res[i],
                                 MAX_DOCS_FROM_ROOT);
                     } catch (Exception e) {
diff --git a/src/com/android/documentsui/base/DebugFlags.java b/src/com/android/documentsui/base/DebugFlags.java
index 09638bb..e131d8e 100644
--- a/src/com/android/documentsui/base/DebugFlags.java
+++ b/src/com/android/documentsui/base/DebugFlags.java
@@ -15,6 +15,11 @@
  */
 package com.android.documentsui.base;
 
+import android.content.ContentResolver;
+import android.os.Bundle;
+
+import com.android.documentsui.queries.DebugCommandProcessor;
+
 import javax.annotation.Nullable;
 
 /**
@@ -27,6 +32,8 @@
     private static String mQvPackage;
     private static boolean sGestureScaleEnabled;
     private static boolean sDocumentDetailsEnabled;
+    private static int sForcedPageOffset = -1;
+    private static int sForcedPageLimit = -1;
 
     public static void setQuickViewer(@Nullable String qvPackage) {
         mQvPackage = qvPackage;
@@ -51,4 +58,23 @@
     public static boolean getGestureScaleEnabled() {
         return sGestureScaleEnabled;
     }
+
+    public static void setForcedPaging(int offset, int limit) {
+        sForcedPageOffset = offset;
+        sForcedPageLimit = limit;
+    }
+
+    public static boolean addForcedPagingArgs(Bundle queryArgs) {
+        assert(Shared.ENABLE_OMC_API_FEATURES);
+        boolean flagsAdded = false;
+        if (sForcedPageOffset >= 0) {
+            queryArgs.putInt(ContentResolver.QUERY_ARG_OFFSET, sForcedPageOffset);
+            flagsAdded |= true;
+        }
+        if (sForcedPageLimit >= 0) {
+            queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, sForcedPageLimit);
+            flagsAdded |= true;
+        }
+        return flagsAdded;
+    }
 }
diff --git a/src/com/android/documentsui/queries/DebugCommandProcessor.java b/src/com/android/documentsui/queries/DebugCommandProcessor.java
index 727d72e..ff62526 100644
--- a/src/com/android/documentsui/queries/DebugCommandProcessor.java
+++ b/src/com/android/documentsui/queries/DebugCommandProcessor.java
@@ -24,6 +24,7 @@
 import com.android.documentsui.DocumentsApplication;
 import com.android.documentsui.base.DebugFlags;
 import com.android.documentsui.base.EventHandler;
+import com.android.documentsui.base.Shared;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -42,6 +43,7 @@
             mCommands.add(DebugCommandProcessor::quickViewer);
             mCommands.add(DebugCommandProcessor::gestureScale);
             mCommands.add(DebugCommandProcessor::docDetails);
+            mCommands.add(DebugCommandProcessor::forcePaging);
         }
     }
 
@@ -108,6 +110,36 @@
         return false;
     }
 
+    private static boolean forcePaging(String[] tokens) {
+        if (!Shared.ENABLE_OMC_API_FEATURES) {
+            Log.i(TAG, "Paging is disabled.");
+            return false;
+        }
+
+        if ("page".equals(tokens[0])) {
+            if (tokens.length >= 2) {
+                try {
+                    int offset = Integer.parseInt(tokens[1]);
+                    int limit = (tokens.length == 3) ? Integer.parseInt(tokens[2]) : -1;
+                    DebugFlags.setForcedPaging(offset, limit);
+                    Log.i(TAG, "Set forced paging to offset: " + offset + ", limit: " + limit);
+                    return true;
+                } catch (NumberFormatException e) {
+                    Log.w(TAG, "Command input does not contain valid numbers: "
+                            + TextUtils.join(" ", tokens));
+                    return false;
+                }
+            } else {
+                Log.w(TAG, "Invalid command structure: " + TextUtils.join(" ", tokens));
+            }
+        } else if ("deqv".equals(tokens[0])) {
+            Log.i(TAG, "Unset quick viewer");
+            DebugFlags.setQuickViewer(null);
+            return true;
+        }
+        return false;
+    }
+
     private static final boolean asBool(String val) {
         if (val == null || val.equals("0")) {
             return false;
diff --git a/src/com/android/documentsui/sorting/SortModel.java b/src/com/android/documentsui/sorting/SortModel.java
index a5c8459..e3eb1e9 100644
--- a/src/com/android/documentsui/sorting/SortModel.java
+++ b/src/com/android/documentsui/sorting/SortModel.java
@@ -17,10 +17,14 @@
 package com.android.documentsui.sorting;
 
 import static com.android.documentsui.base.Shared.DEBUG;
+import static com.android.documentsui.base.Shared.ENABLE_OMC_API_FEATURES;
+import static com.android.documentsui.base.Shared.VERBOSE;
 
 import android.annotation.IntDef;
 import android.annotation.Nullable;
+import android.content.ContentResolver;
 import android.database.Cursor;
+import android.os.Bundle;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.provider.DocumentsContract.Document;
@@ -29,6 +33,7 @@
 import android.view.View;
 
 import com.android.documentsui.R;
+import com.android.documentsui.base.Shared;
 import com.android.documentsui.sorting.SortDimension.SortDirection;
 
 import java.lang.annotation.Retention;
@@ -220,6 +225,15 @@
     }
 
     public Cursor sortCursor(Cursor cursor) {
+        if (ENABLE_OMC_API_FEATURES
+                && cursor.getExtras().containsKey(ContentResolver.QUERY_ARG_SORT_COLUMNS)) {
+            if (VERBOSE) Log.i(TAG, "Cursor is pre-sorted by provider. Skipping sort. Booya!");
+
+            // TODO: assert that the contents of QUERY_ARG_SORT_COLUMNS
+            // matches the sort dimension...once we're returning any pre-sorted results.
+            return cursor;
+        }
+
         if (mSortedDimension != null) {
             return new SortingCursorWrapper(cursor, mSortedDimension);
         } else {
@@ -227,7 +241,54 @@
         }
     }
 
+    public void addQuerySortArgs(Bundle queryArgs) {
+        assert(Shared.ENABLE_OMC_API_FEATURES);
+
+        final int id = getSortedDimensionId();
+        switch (id) {
+            case SORT_DIMENSION_ID_UNKNOWN:
+                return;
+            case SortModel.SORT_DIMENSION_ID_TITLE:
+                queryArgs.putStringArray(
+                        ContentResolver.QUERY_ARG_SORT_COLUMNS,
+                        new String[]{ Document.COLUMN_DISPLAY_NAME });
+                break;
+            case SortModel.SORT_DIMENSION_ID_DATE:
+                queryArgs.putStringArray(
+                        ContentResolver.QUERY_ARG_SORT_COLUMNS,
+                        new String[]{ Document.COLUMN_LAST_MODIFIED });
+                break;
+            case SortModel.SORT_DIMENSION_ID_SIZE:
+                queryArgs.putStringArray(
+                        ContentResolver.QUERY_ARG_SORT_COLUMNS,
+                        new String[]{ Document.COLUMN_SIZE });
+                break;
+            default:
+                throw new IllegalStateException(
+                        "Unexpected sort dimension id: " + id);
+        }
+
+        final SortDimension dimension = getDimensionById(id);
+        switch (dimension.getSortDirection()) {
+            case SortDimension.SORT_DIRECTION_ASCENDING:
+                queryArgs.putInt(
+                        ContentResolver.QUERY_ARG_SORT_DIRECTION,
+                        ContentResolver.QUERY_SORT_DIRECTION_ASCENDING);
+                break;
+            case SortDimension.SORT_DIRECTION_DESCENDING:
+                queryArgs.putInt(
+                        ContentResolver.QUERY_ARG_SORT_DIRECTION,
+                        ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);
+                break;
+            default:
+                throw new IllegalStateException(
+                        "Unexpected sort direction: " + dimension.getSortDirection());
+        }
+    }
+
     public @Nullable String getDocumentSortQuery() {
+        assert(!Shared.ENABLE_OMC_API_FEATURES);
+
         final int id = getSortedDimensionId();
         final String columnName;
         switch (id) {
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 6f4e78b..bdc0125 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -17,6 +17,7 @@
                 <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
             </intent-filter>
        </provider>
+
        <!-- Provider that demostrates some features, like display of INFO and ERROR messages. -->
        <provider
             android:name="com.android.documentsui.DemoProvider"
@@ -29,6 +30,7 @@
                 <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
             </intent-filter>
        </provider>
+
        <!-- Provider for testing archives. -->
        <provider
             android:name="com.android.documentsui.archives.ResourcesProvider"
@@ -41,6 +43,19 @@
                 <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
             </intent-filter>
       </provider>
+
+       <!-- Provider with support for paging. -->
+       <provider
+            android:name="com.android.documentsui.PagingProvider"
+            android:authorities="com.android.documentsui.pagingprovider"
+            android:exported="true"
+            android:grantUriPermissions="true"
+            android:permission="android.permission.MANAGE_DOCUMENTS"
+            android:enabled="true">
+            <intent-filter>
+                <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
+            </intent-filter>
+       </provider>
     </application>
 
     <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
diff --git a/tests/common/com/android/documentsui/PagingProvider.java b/tests/common/com/android/documentsui/PagingProvider.java
new file mode 100644
index 0000000..e05cec5
--- /dev/null
+++ b/tests/common/com/android/documentsui/PagingProvider.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.documentsui;
+
+import android.content.ContentResolver;
+import android.database.Cursor;
+import android.database.MatrixCursor;
+import android.os.Bundle;
+import android.provider.DocumentsContract.Root;
+
+import java.io.FileNotFoundException;
+
+/**
+ * Test provider w/ support for paging, and various sub-scenarios of paging.
+ */
+public class PagingProvider extends TestRootProvider {
+
+    /**
+     * Pass test result size to inform the provider of the result size. Defaults to 1k.
+     */
+    private static final String TEST_RECORDSET_SIZE = "test-recordset-size";
+    private static final int DEFAULT_RECORDSET_SIZE = 100;
+    private static final int UNDETERMINED_RECORDSET_SIZE = -1;
+
+    private static final String ROOT_ID = "paging-root";
+    private static final String ROOT_DOC_ID = "root0";
+    private static final int ROOT_FLAGS = Root.FLAG_SUPPORTS_SEARCH | Root.FLAG_SUPPORTS_IS_CHILD;
+
+    public PagingProvider() {
+        super("Paging Root", ROOT_ID, ROOT_FLAGS, ROOT_DOC_ID);
+    }
+
+    @Override
+    public Cursor queryDocument(String documentId, String[] projection)
+            throws FileNotFoundException {
+        MatrixCursor c = createDocCursor(projection);
+        addFolder(c, documentId);
+        return c;
+    }
+
+    @Override
+    public Cursor queryChildDocuments(
+            String parentDocumentId, String[] projection, String sortOrder)
+            throws FileNotFoundException {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Cursor queryChildDocuments(
+            String parentDocumentId, String[] projection, Bundle queryArgs)
+            throws FileNotFoundException {
+
+        // TODO: Content notification.
+
+        MatrixCursor c = createDocCursor(projection);
+        Bundle extras = c.getExtras();
+
+        int offset = queryArgs.getInt(ContentResolver.QUERY_ARG_OFFSET, 0);
+        int limit = queryArgs.getInt(ContentResolver.QUERY_ARG_LIMIT, Integer.MIN_VALUE);
+        int recordsetSize = queryArgs.getInt(TEST_RECORDSET_SIZE, DEFAULT_RECORDSET_SIZE);
+
+        // Can be -1 (magic unknown), or 0 or more, but not less than -1.
+        assert(recordsetSize > -2);
+
+        // Client may force override the recordset size to -1 which is MAGIC unknown value.
+        // Even if, we still need some finite number against to work.
+        int size = (recordsetSize == UNDETERMINED_RECORDSET_SIZE)
+                ? DEFAULT_RECORDSET_SIZE
+                : recordsetSize;
+
+        // Calculate the number of items to include in the cursor.
+        int numItems = (limit >= 0)
+                ? Math.min(limit, size - offset)
+                : size - offset;
+
+        assert(offset >= 0);
+        assert(numItems >= 0);
+        for (int i = 0; i < numItems; i++) {
+            addFile(c, String.format("%05d", offset + i));
+        }
+        extras.putInt(ContentResolver.QUERY_RESULT_SIZE, recordsetSize);
+        return c;
+    }
+}