blob: 5a80c393646eac8d658f78687aacc0133c9cd63f [file] [log] [blame]
Steve McKay351a7492015-08-04 10:11:01 -07001/*
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
17package com.android.documentsui;
18
Steve McKayf8a5e082015-09-23 17:21:40 -070019import static com.android.documentsui.Shared.DEBUG;
20import static com.android.documentsui.Shared.TAG;
Steve McKay351a7492015-08-04 10:11:01 -070021import static com.android.documentsui.model.DocumentInfo.getCursorString;
22
Steve McKay351a7492015-08-04 10:11:01 -070023import android.content.ClipData;
24import android.content.ClipDescription;
25import android.content.ComponentName;
26import android.content.Intent;
27import android.content.pm.PackageManager;
Steve McKay3bd316c2015-10-15 15:27:30 -070028import android.content.res.Resources;
Steve McKay351a7492015-08-04 10:11:01 -070029import android.database.Cursor;
30import android.net.Uri;
31import android.provider.DocumentsContract;
32import android.provider.DocumentsContract.Document;
Steve McKayfefcd702015-08-20 16:19:38 +000033import android.support.annotation.Nullable;
Steve McKay3bd316c2015-10-15 15:27:30 -070034import android.text.TextUtils;
Steve McKay351a7492015-08-04 10:11:01 -070035import android.util.Log;
36
Ben Kwab8a5e082015-12-07 13:25:27 -080037import com.android.documentsui.BaseActivity.SiblingProvider;
Steve McKay351a7492015-08-04 10:11:01 -070038import com.android.documentsui.model.DocumentInfo;
39
40/**
41 * Provides support for gather a list of quick-viewable files into a quick view intent.
42 */
43final class QuickViewIntentBuilder {
44
Steve McKay351a7492015-08-04 10:11:01 -070045 private final DocumentInfo mDocument;
Ben Kwab8a5e082015-12-07 13:25:27 -080046 private final SiblingProvider mSiblings;
Steve McKay351a7492015-08-04 10:11:01 -070047
Steve McKay3bd316c2015-10-15 15:27:30 -070048 private final PackageManager mPkgManager;
49 private final Resources mResources;
50
51 private ClipData mClipData;
52 private int mDocumentLocation;
Steve McKay351a7492015-08-04 10:11:01 -070053
54 public QuickViewIntentBuilder(
Steve McKay3bd316c2015-10-15 15:27:30 -070055 PackageManager pkgManager,
56 Resources resources,
57 DocumentInfo doc,
Ben Kwab8a5e082015-12-07 13:25:27 -080058 SiblingProvider siblings) {
Steve McKay3bd316c2015-10-15 15:27:30 -070059
Steve McKay351a7492015-08-04 10:11:01 -070060 mPkgManager = pkgManager;
Steve McKay3bd316c2015-10-15 15:27:30 -070061 mResources = resources;
Steve McKay351a7492015-08-04 10:11:01 -070062 mDocument = doc;
Ben Kwab8a5e082015-12-07 13:25:27 -080063 mSiblings = siblings;
Steve McKay351a7492015-08-04 10:11:01 -070064 }
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 McKay3bd316c2015-10-15 15:27:30 -070073 String trustedPkg = mResources.getString(R.string.trusted_quick_viewer_package);
74
Steve McKay351a7492015-08-04 10:11:01 -070075 Intent intent = new Intent(Intent.ACTION_QUICK_VIEW);
76 intent.setDataAndType(mDocument.derivedUri, mDocument.mimeType);
Steve McKay351a7492015-08-04 10:11:01 -070077 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Steve McKay351a7492015-08-04 10:11:01 -070078
Steve McKay3bd316c2015-10-15 15:27:30 -070079 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 Kwab8a5e082015-12-07 13:25:27 -080087 Cursor cursor = mSiblings.getCursor();
Steve McKay3bd316c2015-10-15 15:27:30 -070088 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 McKayb4a6b362015-10-23 09:04:09 -070095 } else {
96 Log.e(TAG, "Can't resolve trusted quick view package: " + trustedPkg);
Steve McKay3bd316c2015-10-15 15:27:30 -070097 }
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 McKay351a7492015-08-04 10:11:01 -0700106 }
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}