Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
| 20 | import static com.android.documentsui.Shared.TAG; |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 21 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
| 22 | |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 23 | import android.content.ClipData; |
| 24 | import android.content.ClipDescription; |
| 25 | import android.content.ComponentName; |
| 26 | import android.content.Intent; |
| 27 | import android.content.pm.PackageManager; |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 28 | import android.content.res.Resources; |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 29 | import android.database.Cursor; |
| 30 | import android.net.Uri; |
| 31 | import android.provider.DocumentsContract; |
| 32 | import android.provider.DocumentsContract.Document; |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 33 | import android.support.annotation.Nullable; |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 34 | import android.text.TextUtils; |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 37 | import com.android.documentsui.BaseActivity.SiblingProvider; |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 38 | import com.android.documentsui.model.DocumentInfo; |
| 39 | |
| 40 | /** |
| 41 | * Provides support for gather a list of quick-viewable files into a quick view intent. |
| 42 | */ |
| 43 | final class QuickViewIntentBuilder { |
| 44 | |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 45 | private final DocumentInfo mDocument; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 46 | private final SiblingProvider mSiblings; |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 47 | |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 48 | private final PackageManager mPkgManager; |
| 49 | private final Resources mResources; |
| 50 | |
| 51 | private ClipData mClipData; |
| 52 | private int mDocumentLocation; |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 53 | |
| 54 | public QuickViewIntentBuilder( |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 55 | PackageManager pkgManager, |
| 56 | Resources resources, |
| 57 | DocumentInfo doc, |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 58 | SiblingProvider siblings) { |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 59 | |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 60 | mPkgManager = pkgManager; |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 61 | mResources = resources; |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 62 | mDocument = doc; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 63 | mSiblings = siblings; |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Builds the intent for quick viewing. Short circuits building if a handler cannot |
| 68 | * be resolved; in this case {@code null} is returned. |
| 69 | */ |
| 70 | @Nullable Intent build() { |
| 71 | if (DEBUG) Log.d(TAG, "Preparing intent for doc:" + mDocument.documentId); |
| 72 | |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 73 | String trustedPkg = mResources.getString(R.string.trusted_quick_viewer_package); |
| 74 | |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 75 | Intent intent = new Intent(Intent.ACTION_QUICK_VIEW); |
| 76 | intent.setDataAndType(mDocument.derivedUri, mDocument.mimeType); |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 77 | intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 78 | |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 79 | if (TextUtils.isEmpty(trustedPkg)) { |
| 80 | if (hasRegisteredHandler(intent)) { |
| 81 | return intent; |
| 82 | } |
| 83 | } else { |
| 84 | intent.setPackage(trustedPkg); |
| 85 | if (hasRegisteredHandler(intent)) { |
| 86 | // We have a trusted handler. Load all of the docs into the intent. |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 87 | Cursor cursor = mSiblings.getCursor(); |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 88 | for (int i = 0; i < cursor.getCount(); i++) { |
| 89 | onNextItem(i, cursor); |
| 90 | } |
| 91 | intent.putExtra(Intent.EXTRA_INDEX, mDocumentLocation); |
| 92 | intent.setClipData(mClipData); |
| 93 | |
| 94 | return intent; |
Steve McKay | 1eafb66 | 2015-10-23 09:04:09 -0700 | [diff] [blame] | 95 | } else { |
| 96 | Log.e(TAG, "Can't resolve trusted quick view package: " + trustedPkg); |
Steve McKay | 3b2ad11 | 2015-10-15 15:27:30 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | private boolean hasRegisteredHandler(Intent intent) { |
| 104 | // Try to resolve the intent. If a matching app isn't installed, it won't resolve. |
| 105 | return intent.resolveActivity(mPkgManager) != null; |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | private void onNextItem(int index, Cursor cursor) { |
| 109 | cursor.moveToPosition(index); |
| 110 | |
| 111 | String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 112 | if (Document.MIME_TYPE_DIR.equals(mimeType)) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | String id = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID); |
| 117 | String authority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY); |
| 118 | Uri uri = DocumentsContract.buildDocumentUri(authority, id); |
| 119 | if (DEBUG) Log.d(TAG, "Including file[" + id + "] @ " + uri); |
| 120 | |
| 121 | if (id.equals(mDocument.documentId)) { |
| 122 | if (DEBUG) Log.d(TAG, "Found starting point for QV. " + index); |
| 123 | mDocumentLocation = index; |
| 124 | } |
| 125 | |
| 126 | ClipData.Item item = new ClipData.Item(uri); |
| 127 | if (mClipData == null) { |
| 128 | mClipData = new ClipData( |
| 129 | "URIs", new String[]{ClipDescription.MIMETYPE_TEXT_URILIST}, item); |
| 130 | } else { |
| 131 | mClipData.addItem(item); |
| 132 | } |
| 133 | } |
| 134 | } |