blob: fc9bc4d544d7838e8b86bd43fd762ea181e78a1b [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
19import static com.android.documentsui.base.Shared.DEBUG;
20
21import android.annotation.Nullable;
Ben Linbbb7d032016-11-15 13:35:41 -080022import android.content.ContentProviderClient;
23import android.content.ContentResolver;
24import android.content.Context;
25import android.net.Uri;
26import android.os.CancellationSignal;
27import android.util.Log;
28
Ben Lin6537acd2016-11-16 12:06:21 -080029import com.android.documentsui.base.ApplicationScope;
Ben Line9abd2d2016-12-06 11:39:52 -080030import com.android.documentsui.base.BooleanConsumer;
Ben Linbbb7d032016-11-15 13:35:41 -080031import com.android.documentsui.base.CheckedTask;
Ben Line9abd2d2016-12-06 11:39:52 -080032import com.android.documentsui.base.DocumentInfo;
Steve McKay98f8c5f2017-03-03 13:52:14 -080033import com.android.documentsui.base.Features;
Ben Linbbb7d032016-11-15 13:35:41 -080034import com.android.documentsui.base.State;
Ben Linbbb7d032016-11-15 13:35:41 -080035
Ben Linbbb7d032016-11-15 13:35:41 -080036/**
37 * A {@link CheckedTask} that calls
38 * {@link ContentResolver#refresh(Uri, android.os.Bundle, android.os.CancellationSignal)} on the
39 * current directory, and then calls the supplied callback with the refresh return value.
40 */
41public class RefreshTask extends TimeoutTask<Void, Boolean> {
42
43 private final static String TAG = "RefreshTask";
44
Ben Lin6537acd2016-11-16 12:06:21 -080045 private final @ApplicationScope Context mContext;
Steve McKay98f8c5f2017-03-03 13:52:14 -080046 private final Features mFeatures;
Ben Linbbb7d032016-11-15 13:35:41 -080047 private final State mState;
Ben Line9abd2d2016-12-06 11:39:52 -080048 private final DocumentInfo mDoc;
49 private final BooleanConsumer mCallback;
Ben Linbbb7d032016-11-15 13:35:41 -080050 private final CancellationSignal mSignal;
51
Steve McKay98f8c5f2017-03-03 13:52:14 -080052
53 public RefreshTask(Features features, State state, DocumentInfo doc, long timeout,
Ben Line9abd2d2016-12-06 11:39:52 -080054 @ApplicationScope Context context, Check check, BooleanConsumer callback) {
Ben Lin30b0dc12017-03-07 15:37:16 -080055 super(check, timeout);
Steve McKay98f8c5f2017-03-03 13:52:14 -080056 mFeatures = features;
57 mState = state;
Ben Line9abd2d2016-12-06 11:39:52 -080058 mDoc = doc;
Ben Lin6537acd2016-11-16 12:06:21 -080059 mContext = context;
Ben Linbbb7d032016-11-15 13:35:41 -080060 mCallback = callback;
61 mSignal = new CancellationSignal();
62 }
63
64 @Override
65 public @Nullable Boolean run(Void... params) {
Ben Line9abd2d2016-12-06 11:39:52 -080066 if (mDoc == null) {
67 Log.w(TAG, "Ignoring attempt to refresh due to null DocumentInfo.");
Ben Lin6537acd2016-11-16 12:06:21 -080068 return false;
69 }
70
Ben Line9abd2d2016-12-06 11:39:52 -080071 if (mState.stack.isEmpty()) {
72 Log.w(TAG, "Ignoring attempt to refresh due to empty stack.");
73 return false;
74 }
75
76 if (!mDoc.derivedUri.equals(mState.stack.peek().derivedUri)) {
77 Log.w(TAG, "Ignoring attempt to refresh on a non-top-level uri.");
Ben Lin6537acd2016-11-16 12:06:21 -080078 return false;
79 }
80
81 // API O introduces ContentResolver#refresh, and if available and the ContentProvider
82 // supports it, the ContentProvider will automatically send a content updated notification
83 // and we will update accordingly. Else, we just tell the callback that Refresh is not
84 // supported.
Steve McKay98f8c5f2017-03-03 13:52:14 -080085 if (!mFeatures.isContentRefreshEnabled()) {
Ben Line9abd2d2016-12-06 11:39:52 -080086 Log.w(TAG, "Ignoring attempt to call Refresh on an older Android platform.");
Ben Lin6537acd2016-11-16 12:06:21 -080087 return false;
88 }
89
Ben Linbbb7d032016-11-15 13:35:41 -080090 final ContentResolver resolver = mContext.getContentResolver();
Ben Line9abd2d2016-12-06 11:39:52 -080091 final String authority = mDoc.authority;
Ben Lin6537acd2016-11-16 12:06:21 -080092 boolean refreshSupported = false;
Ben Linbbb7d032016-11-15 13:35:41 -080093 ContentProviderClient client = null;
94 try {
95 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Ben Line9abd2d2016-12-06 11:39:52 -080096 refreshSupported = client.refresh(mDoc.derivedUri, null, mSignal);
Ben Linbbb7d032016-11-15 13:35:41 -080097 } catch (Exception e) {
98 Log.w(TAG, "Failed to refresh", e);
99 } finally {
100 ContentProviderClient.releaseQuietly(client);
101 }
Ben Lin6537acd2016-11-16 12:06:21 -0800102 return refreshSupported;
Ben Linbbb7d032016-11-15 13:35:41 -0800103 }
104
105 @Override
106 protected void onTimeout() {
107 mSignal.cancel();
Ben Lin6537acd2016-11-16 12:06:21 -0800108 Log.w(TAG, "Provider taking too long to respond. Cancelling.");
Ben Linbbb7d032016-11-15 13:35:41 -0800109 }
110
111 @Override
Ben Lin6537acd2016-11-16 12:06:21 -0800112 public void finish(Boolean refreshSupported) {
Ben Linbbb7d032016-11-15 13:35:41 -0800113 if (DEBUG) {
Tomasz Mikolajewskic9f23f02016-12-06 16:25:08 +0900114 // In case of timeout, refreshSupported is null.
115 if (Boolean.TRUE.equals(refreshSupported)) {
Ben Lin6537acd2016-11-16 12:06:21 -0800116 Log.v(TAG, "Provider supports refresh and has refreshed");
Ben Linbbb7d032016-11-15 13:35:41 -0800117 } else {
Ben Lin6537acd2016-11-16 12:06:21 -0800118 Log.v(TAG, "Provider does not support refresh and did not refresh");
Ben Linbbb7d032016-11-15 13:35:41 -0800119 }
120 }
Tomasz Mikolajewskic9f23f02016-12-06 16:25:08 +0900121 mCallback.accept(refreshSupported != null ? refreshSupported : Boolean.FALSE);
Ben Linbbb7d032016-11-15 13:35:41 -0800122 }
123}