Integration ContentResolver#refresh into RefreshLayout.

Bug: 32873048
Change-Id: Ifbe114f3c91c6b48cde482ebc3874e8a7f3fb2f5
diff --git a/src/com/android/documentsui/RefreshTask.java b/src/com/android/documentsui/RefreshTask.java
new file mode 100644
index 0000000..2521976
--- /dev/null
+++ b/src/com/android/documentsui/RefreshTask.java
@@ -0,0 +1,103 @@
+/*
+ * 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 static com.android.documentsui.base.Shared.DEBUG;
+
+import android.annotation.Nullable;
+import android.app.Activity;
+import android.app.Fragment;
+import android.content.ContentProviderClient;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.net.Uri;
+import android.os.CancellationSignal;
+import android.util.Log;
+
+import com.android.documentsui.TimeoutTask;
+import com.android.documentsui.base.CheckedTask;
+import com.android.documentsui.base.State;
+import com.android.documentsui.dirlist.Model;
+
+import java.util.function.Consumer;
+
+/**
+ * A {@link CheckedTask} that calls
+ * {@link ContentResolver#refresh(Uri, android.os.Bundle, android.os.CancellationSignal)} on the
+ * current directory, and then calls the supplied callback with the refresh return value.
+ */
+public class RefreshTask extends TimeoutTask<Void, Boolean> {
+
+    private final static String TAG = "RefreshTask";
+
+    private final Context mContext;
+    private final State mState;
+    private final Consumer<Boolean> mCallback;
+    private final CancellationSignal mSignal;
+
+
+    public RefreshTask(State state, Activity activity, Consumer<Boolean> callback) {
+        this(state, activity, activity::isDestroyed, callback);
+    }
+
+    public RefreshTask(State state, Fragment fragment, Consumer<Boolean> callback) {
+        this(state, fragment.getContext(), fragment::isDetached, callback);
+    }
+
+    public RefreshTask(State state, Context context, Check check, Consumer<Boolean> callback) {
+        super(check);
+        mContext = context.getApplicationContext();
+        mState = state;
+        mCallback = callback;
+        mSignal = new CancellationSignal();
+    }
+
+    @Override
+    public @Nullable Boolean run(Void... params) {
+        final Uri uri = mState.stack.peek().derivedUri;
+        final ContentResolver resolver = mContext.getContentResolver();
+        final String authority = uri.getAuthority();
+        boolean refreshed = false;
+        ContentProviderClient client = null;
+        try {
+            client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
+            refreshed = client.refresh(uri, null, mSignal);
+        } catch (Exception e) {
+            Log.w(TAG, "Failed to refresh", e);
+        } finally {
+            ContentProviderClient.releaseQuietly(client);
+        }
+        return refreshed;
+    }
+
+    @Override
+    protected void onTimeout() {
+        mSignal.cancel();
+    }
+
+    @Override
+    public void finish(Boolean refreshed) {
+        if (DEBUG) {
+            if (refreshed) {
+                Log.v(TAG, "Provider has new content and has refreshed");
+            } else {
+                Log.v(TAG, "Provider has no new content and did not refresh");
+            }
+        }
+        mCallback.accept(refreshed);
+    }
+}
diff --git a/src/com/android/documentsui/TimeoutTask.java b/src/com/android/documentsui/TimeoutTask.java
index 1d36b48..ffa4da6 100644
--- a/src/com/android/documentsui/TimeoutTask.java
+++ b/src/com/android/documentsui/TimeoutTask.java
@@ -51,10 +51,14 @@
         Handler handler = new Handler();
         handler.postDelayed(() -> {
             if (getStatus() == AsyncTask.Status.RUNNING) {
+                onTimeout();
                 cancel(true);
                 this.finish(null);
             }
         }, mTimeout);
     }
 
+    // Override this do more proper clean up in case of timeout, such as using
+    // CancellationSignal#cancel.
+    protected void onTimeout() { }
 }
diff --git a/src/com/android/documentsui/dirlist/DirectoryFragment.java b/src/com/android/documentsui/dirlist/DirectoryFragment.java
index 4e04ef8..c1bd936 100644
--- a/src/com/android/documentsui/dirlist/DirectoryFragment.java
+++ b/src/com/android/documentsui/dirlist/DirectoryFragment.java
@@ -16,6 +16,7 @@
 
 package com.android.documentsui.dirlist;
 
+import static android.content.ContentResolver.EXTRA_REFRESH_SUPPORTED;
 import static com.android.documentsui.base.DocumentInfo.getCursorInt;
 import static com.android.documentsui.base.DocumentInfo.getCursorString;
 import static com.android.documentsui.base.Shared.DEBUG;
@@ -42,6 +43,7 @@
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.Looper;
 import android.os.Parcelable;
 import android.provider.DocumentsContract;
 import android.provider.DocumentsContract.Document;
@@ -82,6 +84,7 @@
 import com.android.documentsui.Metrics;
 import com.android.documentsui.R;
 import com.android.documentsui.RecentsLoader;
+import com.android.documentsui.RefreshTask;
 import com.android.documentsui.ThumbnailCache;
 import com.android.documentsui.base.DocumentInfo;
 import com.android.documentsui.base.EventHandler;
@@ -145,7 +148,7 @@
     private static final int LOADER_ID = 42;
 
     private static final int CACHE_EVICT_LIMIT = 100;
-    private static final int REFRESH_SPINNER_DISMISS_DELAY = 500;
+    private static final int REFRESH_SPINNER_TIMEOUT = 500;
 
     private BaseActivity mActivity;
 
@@ -1142,8 +1145,18 @@
             cache.removeUri(mModel.getItemUri(ids[i]));
         }
 
-        // Trigger loading
-        getLoaderManager().restartLoader(LOADER_ID, null, mLoaderCallbacks);
+        if (Shared.ENABLE_OMC_API_FEATURES) {
+            RefreshTask refreshTask = new RefreshTask(mState, this,
+                    (Boolean refreshed) -> {
+                        new Handler(Looper.getMainLooper())
+                                .post(() -> mRefreshLayout.setRefreshing(false));
+                    });
+            refreshTask.setTimeout(REFRESH_SPINNER_TIMEOUT);
+            refreshTask.executeOnExecutor(mActivity.getExecutorForCurrentDirectory());
+        } else {
+            // If Refresh API isn't available, we will explicitly reload the loader
+            getLoaderManager().restartLoader(LOADER_ID, null, mLoaderCallbacks);
+        }
     }
 
     private final class ModelUpdateListener implements EventListener<Model.Update> {
@@ -1286,6 +1299,8 @@
 
             mAdapter.notifyDataSetChanged();
             mModel.update(result);
+            mRefreshLayout.setEnabled(
+                    result.cursor.getExtras().getBoolean(EXTRA_REFRESH_SUPPORTED, false));
 
             updateLayout(mState.derivedMode);
 
@@ -1316,10 +1331,10 @@
             mLocalState.mLastSortDimensionId = curSortedDimension.getId();
             mLocalState.mLastSortDirection = curSortedDimension.getSortDirection();
 
-            if (mRefreshLayout.isRefreshing()) {
+            if (!Shared.ENABLE_OMC_API_FEATURES && mRefreshLayout.isRefreshing()) {
                 new Handler().postDelayed(
                         () -> mRefreshLayout.setRefreshing(false),
-                        REFRESH_SPINNER_DISMISS_DELAY);
+                        REFRESH_SPINNER_TIMEOUT);
             }
         }
 
@@ -1329,7 +1344,9 @@
                     + DocumentInfo.debugString(mLocalState.mDocument));
             mModel.onLoaderReset();
 
-            mRefreshLayout.setRefreshing(false);
+            if (!Shared.ENABLE_OMC_API_FEATURES) {
+                mRefreshLayout.setRefreshing(false);
+            }
         }
     }
 }