Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -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 | |
| 19 | import static com.android.documentsui.DocumentsActivity.TAG; |
| 20 | |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 21 | import android.content.ContentProviderClient; |
| 22 | import android.content.ContentResolver; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.ProviderInfo; |
| 27 | import android.content.pm.ResolveInfo; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.net.Uri; |
| 30 | import android.provider.DocumentsContract; |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 31 | import android.provider.DocumentsContract.DocumentRoot; |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 32 | import android.provider.DocumentsContract.Documents; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 34 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 35 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 36 | import com.android.internal.util.Objects; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 37 | import com.google.android.collect.Lists; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 38 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 39 | import java.util.List; |
| 40 | |
| 41 | /** |
| 42 | * Cache of known storage backends and their roots. |
| 43 | */ |
| 44 | public class RootsCache { |
| 45 | |
| 46 | // TODO: cache roots in local provider to avoid spinning up backends |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 47 | // TODO: root updates should trigger UI refresh |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 48 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 49 | private final Context mContext; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 50 | |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 51 | public List<DocumentRoot> mRoots = Lists.newArrayList(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 52 | |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 53 | private DocumentRoot mRecentsRoot; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 54 | |
| 55 | public RootsCache(Context context) { |
| 56 | mContext = context; |
| 57 | update(); |
| 58 | } |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Gather roots from all known storage providers. |
| 62 | */ |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 63 | @GuardedBy("ActivityThread") |
| 64 | public void update() { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 65 | mRoots.clear(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 66 | |
| 67 | { |
| 68 | // Create special root for recents |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 69 | final DocumentRoot root = new DocumentRoot(); |
| 70 | root.rootType = DocumentRoot.ROOT_TYPE_SHORTCUT; |
| 71 | root.docId = null; |
| 72 | root.icon = R.drawable.ic_dir; |
| 73 | root.title = mContext.getString(R.string.root_recent); |
| 74 | root.summary = null; |
| 75 | root.availableBytes = -1; |
| 76 | |
| 77 | mRoots.add(root); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 78 | mRecentsRoot = root; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // Query for other storage backends |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 82 | final ContentResolver resolver = mContext.getContentResolver(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 83 | final PackageManager pm = mContext.getPackageManager(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 84 | final List<ProviderInfo> providers = pm.queryContentProviders( |
| 85 | null, -1, PackageManager.GET_META_DATA); |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 86 | for (ProviderInfo info : providers) { |
| 87 | if (info.metaData != null && info.metaData.containsKey( |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 88 | DocumentsContract.META_DATA_DOCUMENT_PROVIDER)) { |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 89 | |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 90 | // TODO: remove deprecated customRoots flag |
| 91 | // TODO: populate roots on background thread, and cache results |
| 92 | final ContentProviderClient client = resolver |
| 93 | .acquireUnstableContentProviderClient(info.authority); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 94 | try { |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 95 | final List<DocumentRoot> roots = DocumentsContract.getDocumentRoots(client); |
| 96 | for (DocumentRoot root : roots) { |
| 97 | root.authority = info.authority; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 98 | } |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 99 | mRoots.addAll(roots); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 100 | } catch (Exception e) { |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 101 | Log.w(TAG, "Failed to load some roots from " + info.authority + ": " + e); |
| 102 | } finally { |
| 103 | ContentProviderClient.closeQuietly(client); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 109 | public DocumentRoot findRoot(Uri uri) { |
| 110 | final String authority = uri.getAuthority(); |
| 111 | final String docId = DocumentsContract.getDocId(uri); |
| 112 | for (DocumentRoot root : mRoots) { |
| 113 | if (Objects.equal(root.authority, authority) && Objects.equal(root.docId, docId)) { |
| 114 | return root; |
| 115 | } |
| 116 | } |
| 117 | return null; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @GuardedBy("ActivityThread") |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 121 | public DocumentRoot getRecentsRoot() { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 122 | return mRecentsRoot; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @GuardedBy("ActivityThread") |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 126 | public boolean isRecentsRoot(DocumentRoot root) { |
| 127 | return mRecentsRoot == root; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @GuardedBy("ActivityThread") |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 131 | public List<DocumentRoot> getRoots() { |
| 132 | return mRoots; |
| 133 | } |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 134 | |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 135 | @GuardedBy("ActivityThread") |
| 136 | public static Drawable resolveDocumentIcon(Context context, String mimeType) { |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 137 | if (Documents.MIME_TYPE_DIR.equals(mimeType)) { |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 138 | return context.getResources().getDrawable(R.drawable.ic_dir); |
| 139 | } else { |
| 140 | final PackageManager pm = context.getPackageManager(); |
| 141 | final Intent intent = new Intent(Intent.ACTION_VIEW); |
| 142 | intent.setType(mimeType); |
| 143 | |
| 144 | final ResolveInfo activityInfo = pm.resolveActivity( |
| 145 | intent, PackageManager.MATCH_DEFAULT_ONLY); |
| 146 | if (activityInfo != null) { |
| 147 | return activityInfo.loadIcon(pm); |
| 148 | } else { |
| 149 | return null; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |