Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.base.Shared.DEBUG; |
| 20 | import static com.android.documentsui.base.Shared.TAG; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 21 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 23 | import android.content.AsyncTaskLoader; |
| 24 | import android.content.ContentProviderClient; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.database.Cursor; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 27 | import android.database.MatrixCursor; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 28 | import android.database.MergeCursor; |
| 29 | import android.net.Uri; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 30 | import android.os.Bundle; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 31 | import android.provider.DocumentsContract; |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 32 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 33 | import android.text.format.DateUtils; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 34 | import android.util.Log; |
| 35 | |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 36 | import com.android.documentsui.base.Features; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 37 | import com.android.documentsui.base.FilteringCursorWrapper; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 38 | import com.android.documentsui.base.RootInfo; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 39 | import com.android.documentsui.base.State; |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 40 | import com.android.documentsui.roots.ProvidersAccess; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 41 | import com.android.documentsui.roots.RootCursorWrapper; |
Jeff Sharkey | 6ee1dfe | 2015-10-19 17:46:04 -0700 | [diff] [blame] | 42 | import com.android.internal.annotations.GuardedBy; |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 43 | |
Garfield Tan | 2010ff7 | 2016-09-30 14:55:32 -0700 | [diff] [blame] | 44 | import com.google.common.util.concurrent.AbstractFuture; |
| 45 | |
Steve McKay | 50b9bae | 2017-01-17 11:12:08 -0800 | [diff] [blame] | 46 | import libcore.io.IoUtils; |
| 47 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 48 | import java.io.Closeable; |
| 49 | import java.io.IOException; |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 50 | import java.util.ArrayList; |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 51 | import java.util.Collection; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 52 | import java.util.HashMap; |
| 53 | import java.util.List; |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 54 | import java.util.Map; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 55 | import java.util.concurrent.CountDownLatch; |
| 56 | import java.util.concurrent.ExecutionException; |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 57 | import java.util.concurrent.Semaphore; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 58 | import java.util.concurrent.TimeUnit; |
| 59 | |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 60 | public class RecentsLoader extends AsyncTaskLoader<DirectoryResult> { |
Jeff Sharkey | f73d81a | 2013-11-18 17:41:33 -0800 | [diff] [blame] | 61 | // TODO: clean up cursor ownership so background thread doesn't traverse |
| 62 | // previously returned cursors for filtering/sorting; this currently races |
| 63 | // with the UI thread. |
| 64 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 65 | private static final int MAX_OUTSTANDING_RECENTS = 4; |
| 66 | private static final int MAX_OUTSTANDING_RECENTS_SVELTE = 2; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Time to wait for first pass to complete before returning partial results. |
| 70 | */ |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 71 | private static final int MAX_FIRST_PASS_WAIT_MILLIS = 500; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 72 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 73 | /** Maximum documents from a single root. */ |
| 74 | private static final int MAX_DOCS_FROM_ROOT = 64; |
| 75 | |
| 76 | /** Ignore documents older than this age. */ |
| 77 | private static final long REJECT_OLDER_THAN = 45 * DateUtils.DAY_IN_MILLIS; |
| 78 | |
| 79 | /** MIME types that should always be excluded from recents. */ |
| 80 | private static final String[] RECENT_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR }; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 81 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 82 | private final Semaphore mQueryPermits; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 83 | |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 84 | private final ProvidersAccess mProviders; |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 85 | private final State mState; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 86 | private final Features mFeatures; |
Jeff Sharkey | 1c903cc | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 87 | |
Jeff Sharkey | 6ee1dfe | 2015-10-19 17:46:04 -0700 | [diff] [blame] | 88 | @GuardedBy("mTasks") |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 89 | /** A authority -> RecentsTask map */ |
| 90 | private final Map<String, RecentsTask> mTasks = new HashMap<>(); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 91 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 92 | private CountDownLatch mFirstPassLatch; |
| 93 | private volatile boolean mFirstPassDone; |
| 94 | |
| 95 | private DirectoryResult mResult; |
| 96 | |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 97 | public RecentsLoader(Context context, ProvidersAccess providers, State state, Features features) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 98 | super(context); |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 99 | mProviders = providers; |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 100 | mState = state; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 101 | mFeatures = features; |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 102 | |
| 103 | // Keep clients around on high-RAM devices, since we'd be spinning them |
| 104 | // up moments later to fetch thumbnails anyway. |
| 105 | final ActivityManager am = (ActivityManager) getContext().getSystemService( |
| 106 | Context.ACTIVITY_SERVICE); |
| 107 | mQueryPermits = new Semaphore( |
| 108 | am.isLowRamDevice() ? MAX_OUTSTANDING_RECENTS_SVELTE : MAX_OUTSTANDING_RECENTS); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public DirectoryResult loadInBackground() { |
Jeff Sharkey | 6ee1dfe | 2015-10-19 17:46:04 -0700 | [diff] [blame] | 113 | synchronized (mTasks) { |
| 114 | return loadInBackgroundLocked(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private DirectoryResult loadInBackgroundLocked() { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 119 | if (mFirstPassLatch == null) { |
| 120 | // First time through we kick off all the recent tasks, and wait |
| 121 | // around to see if everyone finishes quickly. |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 122 | Map<String, List<String>> rootsIndex = indexRecentsRoots(); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 123 | |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 124 | for (String authority : rootsIndex.keySet()) { |
| 125 | mTasks.put(authority, new RecentsTask(authority, rootsIndex.get(authority))); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | mFirstPassLatch = new CountDownLatch(mTasks.size()); |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 129 | for (RecentsTask task : mTasks.values()) { |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 130 | ProviderExecutor.forAuthority(task.authority).execute(task); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | try { |
| 134 | mFirstPassLatch.await(MAX_FIRST_PASS_WAIT_MILLIS, TimeUnit.MILLISECONDS); |
| 135 | mFirstPassDone = true; |
| 136 | } catch (InterruptedException e) { |
| 137 | throw new RuntimeException(e); |
| 138 | } |
| 139 | } |
| 140 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 141 | final long rejectBefore = System.currentTimeMillis() - REJECT_OLDER_THAN; |
| 142 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 143 | // Collect all finished tasks |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 144 | boolean allDone = true; |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 145 | int totalQuerySize = 0; |
Garfield Tan | 2b9578f | 2016-10-10 15:47:24 -0700 | [diff] [blame] | 146 | List<Cursor> cursors = new ArrayList<>(mTasks.size()); |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 147 | for (RecentsTask task : mTasks.values()) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 148 | if (task.isDone()) { |
| 149 | try { |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 150 | final Cursor[] taskCursors = task.get(); |
| 151 | if (taskCursors == null || taskCursors.length == 0) continue; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 152 | |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 153 | totalQuerySize += taskCursors.length; |
| 154 | for (Cursor cursor : taskCursors) { |
| 155 | if (cursor == null) { |
| 156 | // It's possible given an authority, some roots fail to return a cursor |
| 157 | // after a query. |
| 158 | continue; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 159 | } |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 160 | final FilteringCursorWrapper filtered = new FilteringCursorWrapper( |
| 161 | cursor, mState.acceptMimes, RECENT_REJECT_MIMES, rejectBefore) { |
| 162 | @Override |
| 163 | public void close() { |
| 164 | // Ignored, since we manage cursor lifecycle internally |
| 165 | } |
| 166 | }; |
| 167 | cursors.add(filtered); |
| 168 | } |
| 169 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 170 | } catch (InterruptedException e) { |
| 171 | throw new RuntimeException(e); |
| 172 | } catch (ExecutionException e) { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 173 | // We already logged on other side |
Garfield Tan | 2b9578f | 2016-10-10 15:47:24 -0700 | [diff] [blame] | 174 | } catch (Exception e) { |
| 175 | // Catch exceptions thrown when we read the cursor. |
| 176 | Log.e(TAG, "Failed to query Recents for authority: " + task.authority |
| 177 | + ". Skip this authority in Recents.", e); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 178 | } |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 179 | } else { |
| 180 | allDone = false; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 184 | if (DEBUG) { |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 185 | Log.d(TAG, |
| 186 | "Found " + cursors.size() + " of " + totalQuerySize + " recent queries done"); |
Jeff Sharkey | 4045780 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 189 | final DirectoryResult result = new DirectoryResult(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 190 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 191 | final Cursor merged; |
| 192 | if (cursors.size() > 0) { |
| 193 | merged = new MergeCursor(cursors.toArray(new Cursor[cursors.size()])); |
| 194 | } else { |
| 195 | // Return something when nobody is ready |
| 196 | merged = new MatrixCursor(new String[0]); |
| 197 | } |
| 198 | |
Garfield Tan | 2010ff7 | 2016-09-30 14:55:32 -0700 | [diff] [blame] | 199 | final Cursor sorted = mState.sortModel.sortCursor(merged); |
| 200 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 201 | // Tell the UI if this is an in-progress result. When loading is complete, another update is |
| 202 | // sent with EXTRA_LOADING set to false. |
| 203 | Bundle extras = new Bundle(); |
| 204 | extras.putBoolean(DocumentsContract.EXTRA_LOADING, !allDone); |
Garfield Tan | 2010ff7 | 2016-09-30 14:55:32 -0700 | [diff] [blame] | 205 | sorted.setExtras(extras); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 206 | |
Garfield Tan | 2010ff7 | 2016-09-30 14:55:32 -0700 | [diff] [blame] | 207 | result.cursor = sorted; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 208 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 209 | return result; |
| 210 | } |
| 211 | |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Returns a map of Authority -> rootIds |
| 214 | */ |
| 215 | private Map<String, List<String>> indexRecentsRoots() { |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 216 | final Collection<RootInfo> roots = mProviders.getMatchingRootsBlocking(mState); |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 217 | HashMap<String, List<String>> rootsIndex = new HashMap<>(); |
| 218 | for (RootInfo root : roots) { |
| 219 | if (!root.supportsRecents()) { |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | if (!rootsIndex.containsKey(root.authority)) { |
| 224 | rootsIndex.put(root.authority, new ArrayList<>()); |
| 225 | } |
| 226 | rootsIndex.get(root.authority).add(root.rootId); |
| 227 | } |
| 228 | |
| 229 | return rootsIndex; |
| 230 | } |
| 231 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 232 | @Override |
| 233 | public void cancelLoadInBackground() { |
| 234 | super.cancelLoadInBackground(); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void deliverResult(DirectoryResult result) { |
| 239 | if (isReset()) { |
| 240 | IoUtils.closeQuietly(result); |
| 241 | return; |
| 242 | } |
| 243 | DirectoryResult oldResult = mResult; |
| 244 | mResult = result; |
| 245 | |
| 246 | if (isStarted()) { |
| 247 | super.deliverResult(result); |
| 248 | } |
| 249 | |
| 250 | if (oldResult != null && oldResult != result) { |
| 251 | IoUtils.closeQuietly(oldResult); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | protected void onStartLoading() { |
| 257 | if (mResult != null) { |
| 258 | deliverResult(mResult); |
| 259 | } |
| 260 | if (takeContentChanged() || mResult == null) { |
| 261 | forceLoad(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | protected void onStopLoading() { |
| 267 | cancelLoad(); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void onCanceled(DirectoryResult result) { |
| 272 | IoUtils.closeQuietly(result); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | protected void onReset() { |
| 277 | super.onReset(); |
| 278 | |
| 279 | // Ensure the loader is stopped |
| 280 | onStopLoading(); |
| 281 | |
Jeff Sharkey | 6ee1dfe | 2015-10-19 17:46:04 -0700 | [diff] [blame] | 282 | synchronized (mTasks) { |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 283 | for (RecentsTask task : mTasks.values()) { |
Jeff Sharkey | 6ee1dfe | 2015-10-19 17:46:04 -0700 | [diff] [blame] | 284 | IoUtils.closeQuietly(task); |
| 285 | } |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | IoUtils.closeQuietly(mResult); |
| 289 | mResult = null; |
| 290 | } |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 291 | |
| 292 | // TODO: create better transfer of ownership around cursor to ensure its |
| 293 | // closed in all edge cases. |
| 294 | |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 295 | public class RecentsTask extends AbstractFuture<Cursor[]> implements Runnable, Closeable { |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 296 | public final String authority; |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 297 | public final List<String> rootIds; |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 298 | |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 299 | private Cursor[] mCursors; |
Jon Mann | 9f404c2 | 2017-03-22 18:49:56 -0700 | [diff] [blame] | 300 | private boolean mIsClosed = false; |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 301 | |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 302 | public RecentsTask(String authority, List<String> rootIds) { |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 303 | this.authority = authority; |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 304 | this.rootIds = rootIds; |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public void run() { |
| 309 | if (isCancelled()) return; |
| 310 | |
| 311 | try { |
| 312 | mQueryPermits.acquire(); |
| 313 | } catch (InterruptedException e) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | try { |
| 318 | runInternal(); |
| 319 | } finally { |
| 320 | mQueryPermits.release(); |
| 321 | } |
| 322 | } |
| 323 | |
Jon Mann | 9f404c2 | 2017-03-22 18:49:56 -0700 | [diff] [blame] | 324 | public synchronized void runInternal() { |
| 325 | if (mIsClosed) { |
| 326 | return; |
| 327 | } |
| 328 | |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 329 | ContentProviderClient client = null; |
| 330 | try { |
| 331 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 332 | getContext().getContentResolver(), authority); |
| 333 | |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 334 | final Cursor[] res = new Cursor[rootIds.size()]; |
| 335 | mCursors = new Cursor[rootIds.size()]; |
| 336 | for (int i = 0; i < rootIds.size(); i++) { |
Steve McKay | 50b9bae | 2017-01-17 11:12:08 -0800 | [diff] [blame] | 337 | final Uri uri = |
| 338 | DocumentsContract.buildRecentDocumentsUri(authority, rootIds.get(i)); |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 339 | try { |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 340 | if (mFeatures.isContentPagingEnabled()) { |
Steve McKay | f208d84 | 2017-02-27 12:57:58 -0800 | [diff] [blame] | 341 | final Bundle queryArgs = new Bundle(); |
| 342 | mState.sortModel.addQuerySortArgs(queryArgs); |
| 343 | res[i] = client.query(uri, null, queryArgs, null); |
| 344 | } else { |
| 345 | res[i] = client.query( |
| 346 | uri, null, null, null, mState.sortModel.getDocumentSortQuery()); |
| 347 | } |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 348 | mCursors[i] = new RootCursorWrapper(authority, rootIds.get(i), res[i], |
| 349 | MAX_DOCS_FROM_ROOT); |
| 350 | } catch (Exception e) { |
| 351 | Log.w(TAG, "Failed to load " + authority + ", " + rootIds.get(i), e); |
| 352 | } |
| 353 | } |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 354 | |
| 355 | } catch (Exception e) { |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 356 | Log.w(TAG, "Failed to acquire content resolver for authority: " + authority); |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 357 | } finally { |
| 358 | ContentProviderClient.releaseQuietly(client); |
| 359 | } |
| 360 | |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 361 | set(mCursors); |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 362 | |
| 363 | mFirstPassLatch.countDown(); |
| 364 | if (mFirstPassDone) { |
| 365 | onContentChanged(); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | @Override |
Jon Mann | 9f404c2 | 2017-03-22 18:49:56 -0700 | [diff] [blame] | 370 | public synchronized void close() throws IOException { |
| 371 | if (mCursors == null) { |
| 372 | return; |
| 373 | } |
| 374 | |
Ben Lin | 1427c60 | 2016-09-22 14:12:19 -0700 | [diff] [blame] | 375 | for (Cursor cursor : mCursors) { |
| 376 | IoUtils.closeQuietly(cursor); |
| 377 | } |
Jon Mann | 9f404c2 | 2017-03-22 18:49:56 -0700 | [diff] [blame] | 378 | |
| 379 | mIsClosed = true; |
Steve McKay | 1aeb395 | 2016-02-18 09:48:39 -0800 | [diff] [blame] | 380 | } |
| 381 | } |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 382 | } |