blob: bcdfac83c7b0470924436ade909532b6243c4770 [file] [log] [blame]
Ben Linbbb7d032016-11-15 13:35:41 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Felipe Leme9de58072018-01-19 16:40:04 -080019import static com.android.documentsui.base.SharedMinimal.DEBUG;
Ben Linbbb7d032016-11-15 13:35:41 -080020
Ben Linbbb7d032016-11-15 13:35:41 -080021import android.content.ContentProviderClient;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.net.Uri;
25import android.os.CancellationSignal;
Jeff Sharkeybb68a652019-02-19 11:17:30 -070026import android.os.FileUtils;
Ben Linbbb7d032016-11-15 13:35:41 -080027import android.util.Log;
28
Tony Huang26c37932019-05-10 11:59:33 +080029import androidx.annotation.Nullable;
30
Ben Lin6537acd2016-11-16 12:06:21 -080031import com.android.documentsui.base.ApplicationScope;
Ben Line9abd2d2016-12-06 11:39:52 -080032import com.android.documentsui.base.BooleanConsumer;
Ben Linbbb7d032016-11-15 13:35:41 -080033import com.android.documentsui.base.CheckedTask;
Ben Line9abd2d2016-12-06 11:39:52 -080034import com.android.documentsui.base.DocumentInfo;
Steve McKay98f8c5f2017-03-03 13:52:14 -080035import com.android.documentsui.base.Features;
Ben Linbbb7d032016-11-15 13:35:41 -080036import com.android.documentsui.base.State;
Ben Linbbb7d032016-11-15 13:35:41 -080037
Ben Linbbb7d032016-11-15 13:35:41 -080038/**
39 * A {@link CheckedTask} that calls
40 * {@link ContentResolver#refresh(Uri, android.os.Bundle, android.os.CancellationSignal)} on the
41 * current directory, and then calls the supplied callback with the refresh return value.
42 */
43public class RefreshTask extends TimeoutTask<Void, Boolean> {
44
45 private final static String TAG = "RefreshTask";
46
Ben Lin6537acd2016-11-16 12:06:21 -080047 private final @ApplicationScope Context mContext;
Steve McKay98f8c5f2017-03-03 13:52:14 -080048 private final Features mFeatures;
Ben Linbbb7d032016-11-15 13:35:41 -080049 private final State mState;
Ben Line9abd2d2016-12-06 11:39:52 -080050 private final DocumentInfo mDoc;
51 private final BooleanConsumer mCallback;
Ben Linbbb7d032016-11-15 13:35:41 -080052 private final CancellationSignal mSignal;
53
Steve McKay98f8c5f2017-03-03 13:52:14 -080054
55 public RefreshTask(Features features, State state, DocumentInfo doc, long timeout,
Ben Line9abd2d2016-12-06 11:39:52 -080056 @ApplicationScope Context context, Check check, BooleanConsumer callback) {
Ben Lin30b0dc12017-03-07 15:37:16 -080057 super(check, timeout);
Steve McKay98f8c5f2017-03-03 13:52:14 -080058 mFeatures = features;
59 mState = state;
Ben Line9abd2d2016-12-06 11:39:52 -080060 mDoc = doc;
Ben Lin6537acd2016-11-16 12:06:21 -080061 mContext = context;
Ben Linbbb7d032016-11-15 13:35:41 -080062 mCallback = callback;
63 mSignal = new CancellationSignal();
64 }
65
66 @Override
67 public @Nullable Boolean run(Void... params) {
Ben Line9abd2d2016-12-06 11:39:52 -080068 if (mDoc == null) {
69 Log.w(TAG, "Ignoring attempt to refresh due to null DocumentInfo.");
Ben Lin6537acd2016-11-16 12:06:21 -080070 return false;
71 }
72
Ben Line9abd2d2016-12-06 11:39:52 -080073 if (mState.stack.isEmpty()) {
74 Log.w(TAG, "Ignoring attempt to refresh due to empty stack.");
75 return false;
76 }
77
Tony Huang26c37932019-05-10 11:59:33 +080078 if (mDoc.derivedUri == null) {
79 Log.w(TAG, "Ignoring attempt to refresh due to null derived uri in DocumentInfo.");
80 return false;
81 }
82
Ben Line9abd2d2016-12-06 11:39:52 -080083 if (!mDoc.derivedUri.equals(mState.stack.peek().derivedUri)) {
84 Log.w(TAG, "Ignoring attempt to refresh on a non-top-level uri.");
Ben Lin6537acd2016-11-16 12:06:21 -080085 return false;
86 }
87
88 // API O introduces ContentResolver#refresh, and if available and the ContentProvider
89 // supports it, the ContentProvider will automatically send a content updated notification
90 // and we will update accordingly. Else, we just tell the callback that Refresh is not
91 // supported.
Steve McKay98f8c5f2017-03-03 13:52:14 -080092 if (!mFeatures.isContentRefreshEnabled()) {
Ben Line9abd2d2016-12-06 11:39:52 -080093 Log.w(TAG, "Ignoring attempt to call Refresh on an older Android platform.");
Ben Lin6537acd2016-11-16 12:06:21 -080094 return false;
95 }
96
Ben Linbbb7d032016-11-15 13:35:41 -080097 final ContentResolver resolver = mContext.getContentResolver();
Ben Line9abd2d2016-12-06 11:39:52 -080098 final String authority = mDoc.authority;
Ben Lin6537acd2016-11-16 12:06:21 -080099 boolean refreshSupported = false;
Ben Linbbb7d032016-11-15 13:35:41 -0800100 ContentProviderClient client = null;
101 try {
102 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Ben Line9abd2d2016-12-06 11:39:52 -0800103 refreshSupported = client.refresh(mDoc.derivedUri, null, mSignal);
Ben Linbbb7d032016-11-15 13:35:41 -0800104 } catch (Exception e) {
105 Log.w(TAG, "Failed to refresh", e);
106 } finally {
Jeff Sharkeybb68a652019-02-19 11:17:30 -0700107 FileUtils.closeQuietly(client);
Ben Linbbb7d032016-11-15 13:35:41 -0800108 }
Ben Lin6537acd2016-11-16 12:06:21 -0800109 return refreshSupported;
Ben Linbbb7d032016-11-15 13:35:41 -0800110 }
111
112 @Override
113 protected void onTimeout() {
114 mSignal.cancel();
Ben Lin6537acd2016-11-16 12:06:21 -0800115 Log.w(TAG, "Provider taking too long to respond. Cancelling.");
Ben Linbbb7d032016-11-15 13:35:41 -0800116 }
117
118 @Override
Ben Lin6537acd2016-11-16 12:06:21 -0800119 public void finish(Boolean refreshSupported) {
Ben Linbbb7d032016-11-15 13:35:41 -0800120 if (DEBUG) {
Tomasz Mikolajewskic9f23f02016-12-06 16:25:08 +0900121 // In case of timeout, refreshSupported is null.
122 if (Boolean.TRUE.equals(refreshSupported)) {
Ben Lin6537acd2016-11-16 12:06:21 -0800123 Log.v(TAG, "Provider supports refresh and has refreshed");
Ben Linbbb7d032016-11-15 13:35:41 -0800124 } else {
Ben Lin6537acd2016-11-16 12:06:21 -0800125 Log.v(TAG, "Provider does not support refresh and did not refresh");
Ben Linbbb7d032016-11-15 13:35:41 -0800126 }
127 }
Tomasz Mikolajewskic9f23f02016-12-06 16:25:08 +0900128 mCallback.accept(refreshSupported != null ? refreshSupported : Boolean.FALSE);
Ben Linbbb7d032016-11-15 13:35:41 -0800129 }
130}