blob: 09fadc98a7e0d58758fe12f9f62934150c55a751 [file] [log] [blame]
Jeff Sharkey66516692013-08-06 11:26:10 -07001/*
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
17package com.android.documentsui;
18
Steve McKay83df8c02015-09-16 15:07:31 -070019import static com.android.documentsui.Shared.DEBUG;
Jeff Sharkey66516692013-08-06 11:26:10 -070020
Jeff Sharkey87314082016-03-11 17:25:11 -070021import android.content.BroadcastReceiver.PendingResult;
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -070022import android.content.ContentProviderClient;
23import android.content.ContentResolver;
Jeff Sharkey66516692013-08-06 11:26:10 -070024import android.content.Context;
Jeff Sharkey85f5f812013-10-07 10:16:12 -070025import android.content.Intent;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070026import android.content.pm.ApplicationInfo;
Jeff Sharkey66516692013-08-06 11:26:10 -070027import android.content.pm.PackageManager;
28import android.content.pm.ProviderInfo;
Jeff Sharkey85f5f812013-10-07 10:16:12 -070029import android.content.pm.ResolveInfo;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070030import android.database.ContentObserver;
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070031import android.database.Cursor;
Jeff Sharkey66516692013-08-06 11:26:10 -070032import android.net.Uri;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070033import android.os.AsyncTask;
Jeff Sharkey87314082016-03-11 17:25:11 -070034import android.os.Bundle;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070035import android.os.Handler;
36import android.os.SystemClock;
Jeff Sharkey66516692013-08-06 11:26:10 -070037import android.provider.DocumentsContract;
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070038import android.provider.DocumentsContract.Root;
Steve McKay7a3b88c2015-09-23 17:21:40 -070039import android.support.annotation.VisibleForTesting;
Jeff Sharkey66516692013-08-06 11:26:10 -070040import android.util.Log;
Jeff Sharkey66516692013-08-06 11:26:10 -070041
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070042import com.android.documentsui.model.RootInfo;
Jeff Sharkey66516692013-08-06 11:26:10 -070043import com.android.internal.annotations.GuardedBy;
Steve McKay58efce32015-08-20 16:19:38 +000044
Jeff Sharkey87314082016-03-11 17:25:11 -070045import libcore.io.IoUtils;
46
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070047import com.google.common.collect.ArrayListMultimap;
48import com.google.common.collect.Multimap;
Jeff Sharkey66516692013-08-06 11:26:10 -070049
Steve McKay58efce32015-08-20 16:19:38 +000050import java.util.ArrayList;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070051import java.util.Collection;
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +090052import java.util.Collections;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070053import java.util.HashSet;
Jeff Sharkey66516692013-08-06 11:26:10 -070054import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080055import java.util.Objects;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070056import java.util.concurrent.CountDownLatch;
57import java.util.concurrent.TimeUnit;
Jeff Sharkey66516692013-08-06 11:26:10 -070058
59/**
60 * Cache of known storage backends and their roots.
61 */
62public class RootsCache {
Jeff Sharkey46de7b52013-10-23 09:59:06 -070063 public static final Uri sNotificationUri = Uri.parse(
64 "content://com.android.documentsui.roots/");
Jeff Sharkey66516692013-08-06 11:26:10 -070065
Steve McKayea9ec292016-03-01 15:11:56 -080066 private static final String TAG = "RootsCache";
67
Jeff Sharkey4eb407a2013-08-18 17:38:20 -070068 private final Context mContext;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070069 private final ContentObserver mObserver;
Daichi Hirono60e9a072015-12-25 11:08:42 +090070 private OnCacheUpdateListener mCacheUpdateListener;
Jeff Sharkey66516692013-08-06 11:26:10 -070071
Steve McKay4a1ca862016-02-17 18:25:47 -080072 private final RootInfo mRecentsRoot;
Jeff Sharkey66516692013-08-06 11:26:10 -070073
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070074 private final Object mLock = new Object();
75 private final CountDownLatch mFirstLoad = new CountDownLatch(1);
76
77 @GuardedBy("mLock")
Jeff Sharkey87314082016-03-11 17:25:11 -070078 private boolean mFirstLoadDone;
79 @GuardedBy("mLock")
80 private PendingResult mBootCompletedResult;
81
82 @GuardedBy("mLock")
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070083 private Multimap<String, RootInfo> mRoots = ArrayListMultimap.create();
84 @GuardedBy("mLock")
Steve McKay58efce32015-08-20 16:19:38 +000085 private HashSet<String> mStoppedAuthorities = new HashSet<>();
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070086
87 @GuardedBy("mObservedAuthorities")
Steve McKay58efce32015-08-20 16:19:38 +000088 private final HashSet<String> mObservedAuthorities = new HashSet<>();
Jeff Sharkey4eb407a2013-08-18 17:38:20 -070089
90 public RootsCache(Context context) {
91 mContext = context;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -070092 mObserver = new RootsChangedObserver();
Steve McKay4a1ca862016-02-17 18:25:47 -080093
94 // Create a new anonymous "Recents" RootInfo. It's a faker.
95 mRecentsRoot = new RootInfo() {{
Steve McKay008e9482016-02-18 15:32:16 -080096 // Special root for recents
97 derivedIcon = R.drawable.ic_root_recent;
98 derivedType = RootInfo.TYPE_RECENTS;
Steve McKay327c3132016-02-26 15:30:19 -080099 flags = Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_IS_CHILD
100 | Root.FLAG_SUPPORTS_CREATE;
Steve McKay008e9482016-02-18 15:32:16 -0800101 title = mContext.getString(R.string.root_recent);
102 availableBytes = -1;
103 }};
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700104 }
105
106 private class RootsChangedObserver extends ContentObserver {
107 public RootsChangedObserver() {
108 super(new Handler());
109 }
110
111 @Override
112 public void onChange(boolean selfChange, Uri uri) {
Steve McKay83df8c02015-09-16 15:07:31 -0700113 if (DEBUG) Log.d(TAG, "Updating roots due to change at " + uri);
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700114 updateAuthorityAsync(uri.getAuthority());
115 }
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700116 }
Jeff Sharkey66516692013-08-06 11:26:10 -0700117
Daichi Hirono60e9a072015-12-25 11:08:42 +0900118 static interface OnCacheUpdateListener {
119 void onCacheUpdate();
120 }
121
Jeff Sharkey66516692013-08-06 11:26:10 -0700122 /**
123 * Gather roots from all known storage providers.
124 */
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700125 public void updateAsync() {
Steve McKay355d82b2016-03-01 12:57:44 -0800126
127 // NOTE: This method is called when the UI language changes.
Jeff Sharkey87314082016-03-11 17:25:11 -0700128 // For that reason we update our RecentsRoot to reflect
Steve McKay355d82b2016-03-01 12:57:44 -0800129 // the current language.
130 mRecentsRoot.title = mContext.getString(R.string.root_recent);
131
132 // Nothing else about the root should ever change.
Steve McKaya1f76802016-02-25 13:34:03 -0800133 assert(mRecentsRoot.authority == null);
134 assert(mRecentsRoot.rootId == null);
135 assert(mRecentsRoot.derivedIcon == R.drawable.ic_root_recent);
136 assert(mRecentsRoot.derivedType == RootInfo.TYPE_RECENTS);
137 assert(mRecentsRoot.flags == (Root.FLAG_LOCAL_ONLY
138 | Root.FLAG_SUPPORTS_IS_CHILD
139 | Root.FLAG_SUPPORTS_CREATE));
Steve McKaya1f76802016-02-25 13:34:03 -0800140 assert(mRecentsRoot.availableBytes == -1);
141
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700142 new UpdateTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
143 }
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700144
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700145 /**
146 * Gather roots from storage providers belonging to given package name.
147 */
148 public void updatePackageAsync(String packageName) {
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700149 new UpdateTask(packageName).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
150 }
151
152 /**
153 * Gather roots from storage providers belonging to given authority.
154 */
155 public void updateAuthorityAsync(String authority) {
156 final ProviderInfo info = mContext.getPackageManager().resolveContentProvider(authority, 0);
157 if (info != null) {
158 updatePackageAsync(info.packageName);
159 }
160 }
161
Jeff Sharkey87314082016-03-11 17:25:11 -0700162 public void setBootCompletedResult(PendingResult result) {
163 synchronized (mLock) {
164 // Quickly check if we've already finished loading, otherwise hang
165 // out until first pass is finished.
166 if (mFirstLoadDone) {
167 result.finish();
168 } else {
169 mBootCompletedResult = result;
170 }
171 }
172 }
173
174 /**
175 * Block until the first {@link UpdateTask} pass has finished.
176 *
177 * @return {@code true} if cached roots is ready to roll, otherwise
178 * {@code false} if we timed out while waiting.
179 */
180 private boolean waitForFirstLoad() {
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700181 boolean success = false;
182 try {
183 success = mFirstLoad.await(15, TimeUnit.SECONDS);
184 } catch (InterruptedException e) {
185 }
186 if (!success) {
187 Log.w(TAG, "Timeout waiting for first update");
188 }
Jeff Sharkey87314082016-03-11 17:25:11 -0700189 return success;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700190 }
191
192 /**
193 * Load roots from authorities that are in stopped state. Normal
194 * {@link UpdateTask} passes ignore stopped applications.
195 */
196 private void loadStoppedAuthorities() {
197 final ContentResolver resolver = mContext.getContentResolver();
198 synchronized (mLock) {
199 for (String authority : mStoppedAuthorities) {
Steve McKay83df8c02015-09-16 15:07:31 -0700200 if (DEBUG) Log.d(TAG, "Loading stopped authority " + authority);
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600201 mRoots.putAll(authority, loadRootsForAuthority(resolver, authority, true));
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700202 }
203 mStoppedAuthorities.clear();
204 }
205 }
206
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900207 /**
208 * Load roots from a stopped authority. Normal {@link UpdateTask} passes
209 * ignore stopped applications.
210 */
211 private void loadStoppedAuthority(String authority) {
212 final ContentResolver resolver = mContext.getContentResolver();
213 synchronized (mLock) {
Tomasz Mikolajewskidf676dc2016-02-03 15:18:22 +0900214 if (!mStoppedAuthorities.contains(authority)) {
215 return;
216 }
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900217 if (DEBUG) {
218 Log.d(TAG, "Loading stopped authority " + authority);
219 }
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600220 mRoots.putAll(authority, loadRootsForAuthority(resolver, authority, true));
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900221 mStoppedAuthorities.remove(authority);
222 }
223 }
224
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700225 private class UpdateTask extends AsyncTask<Void, Void, Void> {
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600226 private final String mForceRefreshPackage;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700227
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700228 private final Multimap<String, RootInfo> mTaskRoots = ArrayListMultimap.create();
Steve McKay58efce32015-08-20 16:19:38 +0000229 private final HashSet<String> mTaskStoppedAuthorities = new HashSet<>();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700230
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700231 /**
232 * Update all roots.
233 */
234 public UpdateTask() {
235 this(null);
Jeff Sharkey66516692013-08-06 11:26:10 -0700236 }
237
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700238 /**
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600239 * Force update roots belonging to given package name. Other roots will
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700240 * be copied from cached {@link #mRoots} values.
241 */
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600242 public UpdateTask(String forceRefreshPackage) {
243 mForceRefreshPackage = forceRefreshPackage;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700244 }
Jeff Sharkey66516692013-08-06 11:26:10 -0700245
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700246 @Override
247 protected Void doInBackground(Void... params) {
248 final long start = SystemClock.elapsedRealtime();
249
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600250 if (mForceRefreshPackage != null) {
Jeff Sharkey87314082016-03-11 17:25:11 -0700251 // We must have previously cached values to fill in non-matching
252 // packages, so wait around for successful first load.
253 if (!waitForFirstLoad()) {
254 return null;
255 }
Jeff Sharkeyc9d71502014-09-10 11:23:15 -0700256 }
257
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700258 mTaskRoots.put(mRecentsRoot.authority, mRecentsRoot);
Jeff Sharkey5545f562013-09-21 13:57:33 -0700259
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700260 final ContentResolver resolver = mContext.getContentResolver();
261 final PackageManager pm = mContext.getPackageManager();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700262
263 // Pick up provider with action string
264 final Intent intent = new Intent(DocumentsContract.PROVIDER_INTERFACE);
265 final List<ResolveInfo> providers = pm.queryIntentContentProviders(intent, 0);
266 for (ResolveInfo info : providers) {
267 handleDocumentsProvider(info.providerInfo);
268 }
269
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700270 final long delta = SystemClock.elapsedRealtime() - start;
Steve McKay83df8c02015-09-16 15:07:31 -0700271 if (DEBUG)
272 Log.d(TAG, "Update found " + mTaskRoots.size() + " roots in " + delta + "ms");
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700273 synchronized (mLock) {
Jeff Sharkey87314082016-03-11 17:25:11 -0700274 mFirstLoadDone = true;
275 if (mBootCompletedResult != null) {
276 mBootCompletedResult.finish();
277 mBootCompletedResult = null;
278 }
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700279 mRoots = mTaskRoots;
280 mStoppedAuthorities = mTaskStoppedAuthorities;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700281 }
282 mFirstLoad.countDown();
Jeff Sharkey46de7b52013-10-23 09:59:06 -0700283 resolver.notifyChange(sNotificationUri, null, false);
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700284 return null;
285 }
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700286
Daichi Hirono60e9a072015-12-25 11:08:42 +0900287 @Override
288 protected void onPostExecute(Void result) {
289 if (mCacheUpdateListener != null) {
290 mCacheUpdateListener.onCacheUpdate();
291 }
292 }
293
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700294 private void handleDocumentsProvider(ProviderInfo info) {
295 // Ignore stopped packages for now; we might query them
296 // later during UI interaction.
297 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_STOPPED) != 0) {
Steve McKay83df8c02015-09-16 15:07:31 -0700298 if (DEBUG) Log.d(TAG, "Ignoring stopped authority " + info.authority);
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700299 mTaskStoppedAuthorities.add(info.authority);
300 return;
301 }
302
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600303 final boolean forceRefresh = Objects.equals(mForceRefreshPackage, info.packageName);
304 mTaskRoots.putAll(info.authority, loadRootsForAuthority(mContext.getContentResolver(),
305 info.authority, forceRefresh));
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700306 }
Jeff Sharkey66516692013-08-06 11:26:10 -0700307 }
308
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700309 /**
310 * Bring up requested provider and query for all active roots.
311 */
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600312 private Collection<RootInfo> loadRootsForAuthority(ContentResolver resolver, String authority,
313 boolean forceRefresh) {
Steve McKay83df8c02015-09-16 15:07:31 -0700314 if (DEBUG) Log.d(TAG, "Loading roots for " + authority);
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700315
316 synchronized (mObservedAuthorities) {
317 if (mObservedAuthorities.add(authority)) {
318 // Watch for any future updates
319 final Uri rootsUri = DocumentsContract.buildRootsUri(authority);
320 mContext.getContentResolver().registerContentObserver(rootsUri, true, mObserver);
321 }
322 }
323
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700324 final Uri rootsUri = DocumentsContract.buildRootsUri(authority);
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600325 if (!forceRefresh) {
Jeff Sharkey87314082016-03-11 17:25:11 -0700326 // Look for roots data that we might have cached for ourselves in the
327 // long-lived system process.
328 final Bundle systemCache = resolver.getCache(rootsUri);
329 if (systemCache != null) {
330 if (DEBUG) Log.d(TAG, "System cache hit for " + authority);
331 return systemCache.getParcelableArrayList(TAG);
332 }
333 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700334
Jeff Sharkey87314082016-03-11 17:25:11 -0700335 final ArrayList<RootInfo> roots = new ArrayList<>();
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700336 ContentProviderClient client = null;
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700337 Cursor cursor = null;
338 try {
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700339 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700340 cursor = client.query(rootsUri, null, null, null, null);
341 while (cursor.moveToNext()) {
342 final RootInfo root = RootInfo.fromRootsCursor(authority, cursor);
343 roots.add(root);
344 }
345 } catch (Exception e) {
346 Log.w(TAG, "Failed to load some roots from " + authority + ": " + e);
347 } finally {
348 IoUtils.closeQuietly(cursor);
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700349 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700350 }
Jeff Sharkey87314082016-03-11 17:25:11 -0700351
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600352 // Cache these freshly parsed roots over in the long-lived system
353 // process, in case our process goes away. The system takes care of
354 // invalidating the cache if the package or Uri changes.
355 final Bundle systemCache = new Bundle();
356 systemCache.putParcelableArrayList(TAG, roots);
357 resolver.putCache(rootsUri, systemCache);
Jeff Sharkey87314082016-03-11 17:25:11 -0700358
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700359 return roots;
360 }
361
362 /**
363 * Return the requested {@link RootInfo}, but only loading the roots for the
364 * requested authority. This is useful when we want to load fast without
365 * waiting for all the other roots to come back.
366 */
367 public RootInfo getRootOneshot(String authority, String rootId) {
368 synchronized (mLock) {
369 RootInfo root = getRootLocked(authority, rootId);
370 if (root == null) {
Jeff Sharkey7732e1e2016-03-30 17:14:23 -0600371 mRoots.putAll(authority,
372 loadRootsForAuthority(mContext.getContentResolver(), authority, false));
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700373 root = getRootLocked(authority, rootId);
374 }
375 return root;
376 }
377 }
378
379 public RootInfo getRootBlocking(String authority, String rootId) {
380 waitForFirstLoad();
381 loadStoppedAuthorities();
382 synchronized (mLock) {
383 return getRootLocked(authority, rootId);
384 }
385 }
386
387 private RootInfo getRootLocked(String authority, String rootId) {
388 for (RootInfo root : mRoots.get(authority)) {
Kenny Roote6585b32013-12-13 12:00:26 -0800389 if (Objects.equals(root.rootId, rootId)) {
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700390 return root;
391 }
392 }
393 return null;
Jeff Sharkey66516692013-08-06 11:26:10 -0700394 }
395
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700396 public boolean isIconUniqueBlocking(RootInfo root) {
397 waitForFirstLoad();
398 loadStoppedAuthorities();
399 synchronized (mLock) {
400 final int rootIcon = root.derivedIcon != 0 ? root.derivedIcon : root.icon;
401 for (RootInfo test : mRoots.get(root.authority)) {
Kenny Roote6585b32013-12-13 12:00:26 -0800402 if (Objects.equals(test.rootId, root.rootId)) {
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700403 continue;
404 }
Jeff Sharkeyf6db1542013-09-13 13:42:19 -0700405 final int testIcon = test.derivedIcon != 0 ? test.derivedIcon : test.icon;
406 if (testIcon == rootIcon) {
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700407 return false;
408 }
409 }
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700410 return true;
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700411 }
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700412 }
413
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700414 public RootInfo getRecentsRoot() {
Jeff Sharkey4eb407a2013-08-18 17:38:20 -0700415 return mRecentsRoot;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700416 }
417
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700418 public boolean isRecentsRoot(RootInfo root) {
Steve McKay4a1ca862016-02-17 18:25:47 -0800419 return mRecentsRoot.equals(root);
Jeff Sharkey66516692013-08-06 11:26:10 -0700420 }
421
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700422 public Collection<RootInfo> getRootsBlocking() {
423 waitForFirstLoad();
424 loadStoppedAuthorities();
425 synchronized (mLock) {
426 return mRoots.values();
427 }
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -0700428 }
Jeff Sharkey66516692013-08-06 11:26:10 -0700429
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700430 public Collection<RootInfo> getMatchingRootsBlocking(State state) {
431 waitForFirstLoad();
432 loadStoppedAuthorities();
433 synchronized (mLock) {
434 return getMatchingRoots(mRoots.values(), state);
435 }
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700436 }
437
Tomasz Mikolajewskia6120da2016-01-27 17:36:51 +0900438 /**
439 * Returns a list of roots for the specified authority. If not found, then
440 * an empty list is returned.
441 */
442 public Collection<RootInfo> getRootsForAuthorityBlocking(String authority) {
443 waitForFirstLoad();
444 loadStoppedAuthority(authority);
445 synchronized (mLock) {
446 final Collection<RootInfo> roots = mRoots.get(authority);
447 return roots != null ? roots : Collections.<RootInfo>emptyList();
448 }
449 }
450
Daichi Hirono60e9a072015-12-25 11:08:42 +0900451 public void setOnCacheUpdateListener(OnCacheUpdateListener cacheUpdateListener) {
452 mCacheUpdateListener = cacheUpdateListener;
453 }
454
Jeff Sharkeya9ce0492013-09-19 15:25:56 -0700455 @VisibleForTesting
456 static List<RootInfo> getMatchingRoots(Collection<RootInfo> roots, State state) {
Steve McKay58efce32015-08-20 16:19:38 +0000457 final List<RootInfo> matching = new ArrayList<>();
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700458 for (RootInfo root : roots) {
Steve McKayea9ec292016-03-01 15:11:56 -0800459
460 if (DEBUG) Log.d(TAG, "Evaluating " + root);
461
462 if (state.action == State.ACTION_CREATE && !root.supportsCreate()) {
463 if (DEBUG) Log.d(TAG, "Excluding read-only root because: ACTION_CREATE.");
464 continue;
465 }
466
Steve McKay4a1ca862016-02-17 18:25:47 -0800467 if (state.action == State.ACTION_PICK_COPY_DESTINATION
Steve McKayea9ec292016-03-01 15:11:56 -0800468 && !root.supportsCreate()) {
469 if (DEBUG) Log.d(
470 TAG, "Excluding read-only root because: ACTION_PICK_COPY_DESTINATION.");
471 continue;
472 }
473
474 if (state.action == State.ACTION_OPEN_TREE && !root.supportsChildren()) {
475 if (DEBUG) Log.d(
476 TAG, "Excluding root !supportsChildren because: ACTION_OPEN_TREE.");
477 continue;
478 }
479
Aga Wronska774cc932016-03-30 18:07:59 -0700480 if (!state.showAdvanced && root.isAdvanced()) {
481 if (DEBUG) Log.d(TAG, "Excluding root because: unwanted advanced device.");
482 continue;
483 }
484
Steve McKayea9ec292016-03-01 15:11:56 -0800485 if (state.localOnly && !root.isLocalOnly()) {
486 if (DEBUG) Log.d(TAG, "Excluding root because: unwanted non-local device.");
487 continue;
488 }
489
Steve McKayea9ec292016-03-01 15:11:56 -0800490 if (state.directoryCopy && root.isDownloads()) {
491 if (DEBUG) Log.d(
492 TAG, "Excluding downloads root because: unsupported directory copy.");
493 continue;
494 }
Steve McKay83df8c02015-09-16 15:07:31 -0700495
Steve McKayea9ec292016-03-01 15:11:56 -0800496 if (state.action == State.ACTION_OPEN && root.isEmpty()) {
497 if (DEBUG) Log.d(TAG, "Excluding empty root because: ACTION_OPEN.");
498 continue;
499 }
500
Steve McKayea9ec292016-03-01 15:11:56 -0800501 if (state.action == State.ACTION_GET_CONTENT && root.isEmpty()) {
502 if (DEBUG) Log.d(TAG, "Excluding empty root because: ACTION_GET_CONTENT.");
Steve McKay83df8c02015-09-16 15:07:31 -0700503 continue;
504 }
Jeff Sharkey348ad682013-09-02 17:19:40 -0700505
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700506 final boolean overlap =
507 MimePredicate.mimeMatches(root.derivedMimeTypes, state.acceptMimes) ||
508 MimePredicate.mimeMatches(state.acceptMimes, root.derivedMimeTypes);
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700509 if (!overlap) {
Steve McKayea9ec292016-03-01 15:11:56 -0800510 if (DEBUG) Log.d(
511 TAG, "Excluding root because: unsupported content types > "
512 + state.acceptMimes);
Jeff Sharkey923396b2013-09-05 13:55:35 -0700513 continue;
Jeff Sharkey348ad682013-09-02 17:19:40 -0700514 }
515
Ben Kwa77797402015-05-29 15:40:31 -0700516 if (state.excludedAuthorities.contains(root.authority)) {
Steve McKay7c439582016-03-01 15:41:47 -0800517 if (DEBUG) Log.d(TAG, "Excluding root because: owned by calling package.");
Ben Kwa77797402015-05-29 15:40:31 -0700518 continue;
519 }
520
Steve McKayea9ec292016-03-01 15:11:56 -0800521 if (DEBUG) Log.d(TAG, "Including " + root);
Jeff Sharkey348ad682013-09-02 17:19:40 -0700522 matching.add(root);
523 }
524 return matching;
525 }
Jeff Sharkey66516692013-08-06 11:26:10 -0700526}