Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
Felipe Leme | 9de5807 | 2018-01-19 16:40:04 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.base.SharedMinimal.DEBUG; |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 20 | |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 21 | import android.content.ContentProviderClient; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.net.Uri; |
| 25 | import android.os.CancellationSignal; |
Jeff Sharkey | bb68a65 | 2019-02-19 11:17:30 -0700 | [diff] [blame] | 26 | import android.os.FileUtils; |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
Tony Huang | 26c3793 | 2019-05-10 11:59:33 +0800 | [diff] [blame] | 29 | import androidx.annotation.Nullable; |
| 30 | |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 31 | import com.android.documentsui.base.ApplicationScope; |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 32 | import com.android.documentsui.base.BooleanConsumer; |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 33 | import com.android.documentsui.base.CheckedTask; |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 34 | import com.android.documentsui.base.DocumentInfo; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 35 | import com.android.documentsui.base.Features; |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 36 | import com.android.documentsui.base.State; |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 37 | |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 38 | /** |
| 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 | */ |
| 43 | public class RefreshTask extends TimeoutTask<Void, Boolean> { |
| 44 | |
| 45 | private final static String TAG = "RefreshTask"; |
| 46 | |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 47 | private final @ApplicationScope Context mContext; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 48 | private final Features mFeatures; |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 49 | private final State mState; |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 50 | private final DocumentInfo mDoc; |
| 51 | private final BooleanConsumer mCallback; |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 52 | private final CancellationSignal mSignal; |
| 53 | |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 54 | |
| 55 | public RefreshTask(Features features, State state, DocumentInfo doc, long timeout, |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 56 | @ApplicationScope Context context, Check check, BooleanConsumer callback) { |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 57 | super(check, timeout); |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 58 | mFeatures = features; |
| 59 | mState = state; |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 60 | mDoc = doc; |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 61 | mContext = context; |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 62 | mCallback = callback; |
| 63 | mSignal = new CancellationSignal(); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public @Nullable Boolean run(Void... params) { |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 68 | if (mDoc == null) { |
| 69 | Log.w(TAG, "Ignoring attempt to refresh due to null DocumentInfo."); |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 73 | if (mState.stack.isEmpty()) { |
| 74 | Log.w(TAG, "Ignoring attempt to refresh due to empty stack."); |
| 75 | return false; |
| 76 | } |
| 77 | |
Tony Huang | 26c3793 | 2019-05-10 11:59:33 +0800 | [diff] [blame] | 78 | 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 Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 83 | if (!mDoc.derivedUri.equals(mState.stack.peek().derivedUri)) { |
| 84 | Log.w(TAG, "Ignoring attempt to refresh on a non-top-level uri."); |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | |
Kelvin Kwan | a649f54 | 2020-02-20 20:36:23 +0000 | [diff] [blame] | 88 | 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 Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 94 | // 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 McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 98 | if (!mFeatures.isContentRefreshEnabled()) { |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 99 | Log.w(TAG, "Ignoring attempt to call Refresh on an older Android platform."); |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
Kelvin Kwan | a649f54 | 2020-02-20 20:36:23 +0000 | [diff] [blame] | 103 | final ContentResolver resolver = mDoc.userId.getContentResolver(mContext); |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 104 | final String authority = mDoc.authority; |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 105 | boolean refreshSupported = false; |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 106 | ContentProviderClient client = null; |
| 107 | try { |
| 108 | client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority); |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 109 | refreshSupported = client.refresh(mDoc.derivedUri, null, mSignal); |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 110 | } catch (Exception e) { |
| 111 | Log.w(TAG, "Failed to refresh", e); |
| 112 | } finally { |
Jeff Sharkey | bb68a65 | 2019-02-19 11:17:30 -0700 | [diff] [blame] | 113 | FileUtils.closeQuietly(client); |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 114 | } |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 115 | return refreshSupported; |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | protected void onTimeout() { |
| 120 | mSignal.cancel(); |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 121 | Log.w(TAG, "Provider taking too long to respond. Cancelling."); |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 125 | public void finish(Boolean refreshSupported) { |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 126 | if (DEBUG) { |
Tomasz Mikolajewski | c9f23f0 | 2016-12-06 16:25:08 +0900 | [diff] [blame] | 127 | // In case of timeout, refreshSupported is null. |
| 128 | if (Boolean.TRUE.equals(refreshSupported)) { |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 129 | Log.v(TAG, "Provider supports refresh and has refreshed"); |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 130 | } else { |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 131 | Log.v(TAG, "Provider does not support refresh and did not refresh"); |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
Tomasz Mikolajewski | c9f23f0 | 2016-12-06 16:25:08 +0900 | [diff] [blame] | 134 | mCallback.accept(refreshSupported != null ? refreshSupported : Boolean.FALSE); |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 135 | } |
| 136 | } |