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