blob: 091c64b255957d5979223691c577429c47bb961e [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
Kelvin Kwana649f542020-02-20 20:36:23 +000088 if (!mState.canInteractWith(mDoc.userId) || mDoc.userId.isQuietModeEnabled(mContext)) {
89 // No result was returned by these errors so it does not support refresh.
90 Log.w(TAG, "Cannot refresh due to cross profile error.");
91 return false;
92 }
93
Ben Lin6537acd2016-11-16 12:06:21 -080094 // API O introduces ContentResolver#refresh, and if available and the ContentProvider
95 // supports it, the ContentProvider will automatically send a content updated notification
96 // and we will update accordingly. Else, we just tell the callback that Refresh is not
97 // supported.
Steve McKay98f8c5f2017-03-03 13:52:14 -080098 if (!mFeatures.isContentRefreshEnabled()) {
Ben Line9abd2d2016-12-06 11:39:52 -080099 Log.w(TAG, "Ignoring attempt to call Refresh on an older Android platform.");
Ben Lin6537acd2016-11-16 12:06:21 -0800100 return false;
101 }
102
Kelvin Kwana649f542020-02-20 20:36:23 +0000103 final ContentResolver resolver = mDoc.userId.getContentResolver(mContext);
Ben Line9abd2d2016-12-06 11:39:52 -0800104 final String authority = mDoc.authority;
Ben Lin6537acd2016-11-16 12:06:21 -0800105 boolean refreshSupported = false;
Ben Linbbb7d032016-11-15 13:35:41 -0800106 ContentProviderClient client = null;
107 try {
108 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Ben Line9abd2d2016-12-06 11:39:52 -0800109 refreshSupported = client.refresh(mDoc.derivedUri, null, mSignal);
Ben Linbbb7d032016-11-15 13:35:41 -0800110 } catch (Exception e) {
111 Log.w(TAG, "Failed to refresh", e);
112 } finally {
Jeff Sharkeybb68a652019-02-19 11:17:30 -0700113 FileUtils.closeQuietly(client);
Ben Linbbb7d032016-11-15 13:35:41 -0800114 }
Ben Lin6537acd2016-11-16 12:06:21 -0800115 return refreshSupported;
Ben Linbbb7d032016-11-15 13:35:41 -0800116 }
117
118 @Override
119 protected void onTimeout() {
120 mSignal.cancel();
Ben Lin6537acd2016-11-16 12:06:21 -0800121 Log.w(TAG, "Provider taking too long to respond. Cancelling.");
Ben Linbbb7d032016-11-15 13:35:41 -0800122 }
123
124 @Override
Ben Lin6537acd2016-11-16 12:06:21 -0800125 public void finish(Boolean refreshSupported) {
Ben Linbbb7d032016-11-15 13:35:41 -0800126 if (DEBUG) {
Tomasz Mikolajewskic9f23f02016-12-06 16:25:08 +0900127 // In case of timeout, refreshSupported is null.
128 if (Boolean.TRUE.equals(refreshSupported)) {
Ben Lin6537acd2016-11-16 12:06:21 -0800129 Log.v(TAG, "Provider supports refresh and has refreshed");
Ben Linbbb7d032016-11-15 13:35:41 -0800130 } else {
Ben Lin6537acd2016-11-16 12:06:21 -0800131 Log.v(TAG, "Provider does not support refresh and did not refresh");
Ben Linbbb7d032016-11-15 13:35:41 -0800132 }
133 }
Tomasz Mikolajewskic9f23f02016-12-06 16:25:08 +0900134 mCallback.accept(refreshSupported != null ? refreshSupported : Boolean.FALSE);
Ben Linbbb7d032016-11-15 13:35:41 -0800135 }
136}