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