QuickViewIntentBuilder.java revision dcc68fdd0ca1f0d2d2dfb979dd837ac2dd2e16f3
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 17package com.android.documentsui; 18 19import static com.android.documentsui.Shared.DEBUG; 20import static com.android.documentsui.model.DocumentInfo.getCursorString; 21 22import android.content.ClipData; 23import android.content.ClipDescription; 24import android.content.ComponentName; 25import android.content.Intent; 26import android.content.pm.PackageManager; 27import android.content.res.Resources; 28import android.database.Cursor; 29import android.net.Uri; 30import android.provider.DocumentsContract; 31import android.provider.DocumentsContract.Document; 32import android.support.annotation.Nullable; 33import android.text.TextUtils; 34import android.util.Log; 35import android.util.Range; 36 37import com.android.documentsui.dirlist.Model; 38import com.android.documentsui.model.DocumentInfo; 39 40import java.util.ArrayList; 41import java.util.List; 42 43/** 44 * Provides support for gather a list of quick-viewable files into a quick view intent. 45 */ 46final class QuickViewIntentBuilder { 47 48 private static final String TAG = "QuickViewIntentBuilder"; 49 private static final int MAX_CLIP_ITEMS = 1000; 50 51 private final DocumentInfo mDocument; 52 private final Model mModel; 53 54 private final PackageManager mPkgManager; 55 private final Resources mResources; 56 57 public QuickViewIntentBuilder( 58 PackageManager pkgManager, 59 Resources resources, 60 DocumentInfo doc, 61 Model model) { 62 63 mPkgManager = pkgManager; 64 mResources = resources; 65 mDocument = doc; 66 mModel = model; 67 } 68 69 /** 70 * Builds the intent for quick viewing. Short circuits building if a handler cannot 71 * be resolved; in this case {@code null} is returned. 72 */ 73 @Nullable Intent build() { 74 if (DEBUG) Log.d(TAG, "Preparing intent for doc:" + mDocument.documentId); 75 76 String trustedPkg = mResources.getString(R.string.trusted_quick_viewer_package); 77 78 if (!TextUtils.isEmpty(trustedPkg)) { 79 Intent intent = new Intent(Intent.ACTION_QUICK_VIEW); 80 intent.setDataAndType(mDocument.derivedUri, mDocument.mimeType); 81 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 82 intent.setPackage(trustedPkg); 83 if (hasRegisteredHandler(intent)) { 84 final ArrayList<Uri> uris = new ArrayList<Uri>(); 85 final int documentLocation = collectViewableUris(uris); 86 final Range<Integer> range = computeSiblingsRange(uris, documentLocation); 87 88 ClipData clipData = null; 89 ClipData.Item item; 90 Uri uri; 91 for (int i = range.getLower(); i <= range.getUpper(); i++) { 92 uri = uris.get(i); 93 item = new ClipData.Item(uri); 94 if (DEBUG) Log.d(TAG, "Including file: " + uri); 95 if (clipData == null) { 96 clipData = new ClipData( 97 "URIs", new String[] { ClipDescription.MIMETYPE_TEXT_URILIST }, 98 item); 99 } else { 100 clipData.addItem(item); 101 } 102 } 103 104 intent.putExtra(Intent.EXTRA_INDEX, documentLocation); 105 intent.setClipData(clipData); 106 107 return intent; 108 } else { 109 Log.e(TAG, "Can't resolve trusted quick view package: " + trustedPkg); 110 } 111 } 112 113 return null; 114 } 115 116 private int collectViewableUris(ArrayList<Uri> uris) { 117 final String[] siblingIds = mModel.getModelIds(); 118 uris.ensureCapacity(siblingIds.length); 119 120 int documentLocation = 0; 121 Cursor cursor; 122 String mimeType; 123 String id; 124 String authority; 125 Uri uri; 126 127 // Cursor's are not guaranteed to be immutable. Hence, traverse it only once. 128 for (int i = 0; i < siblingIds.length; i++) { 129 cursor = mModel.getItem(siblingIds[i]); 130 131 if (cursor == null) { 132 if (DEBUG) Log.d(TAG, 133 "Unable to obtain cursor for sibling document, modelId: " 134 + siblingIds[i]); 135 continue; 136 } 137 138 mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); 139 if (Document.MIME_TYPE_DIR.equals(mimeType)) { 140 if (DEBUG) Log.d(TAG, 141 "Skipping directory, not supported by quick view. modelId: " 142 + siblingIds[i]); 143 continue; 144 } 145 146 id = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID); 147 authority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY); 148 uri = DocumentsContract.buildDocumentUri(authority, id); 149 150 if (id.equals(mDocument.documentId)) { 151 if (DEBUG) Log.d(TAG, "Found starting point for QV. " + i); 152 documentLocation = i; 153 } 154 155 uris.add(uri); 156 } 157 158 return documentLocation; 159 } 160 161 private static Range<Integer> computeSiblingsRange(List<Uri> uris, int documentLocation) { 162 // Restrict number of siblings to avoid hitting the IPC limit. 163 // TODO: Remove this restriction once ClipData can hold an arbitrary number of 164 // items. 165 int firstSibling; 166 int lastSibling; 167 if (documentLocation < uris.size() / 2) { 168 firstSibling = Math.max(0, documentLocation - MAX_CLIP_ITEMS / 2); 169 lastSibling = Math.min(uris.size() - 1, firstSibling + MAX_CLIP_ITEMS - 1); 170 } else { 171 lastSibling = Math.min(uris.size() - 1, documentLocation + MAX_CLIP_ITEMS / 2); 172 firstSibling = Math.max(0, lastSibling - MAX_CLIP_ITEMS + 1); 173 } 174 175 if (DEBUG) Log.d(TAG, "Copmuted siblings from index: " + firstSibling 176 + " to: " + lastSibling); 177 178 return new Range(firstSibling, lastSibling); 179 } 180 181 private boolean hasRegisteredHandler(Intent intent) { 182 // Try to resolve the intent. If a matching app isn't installed, it won't resolve. 183 return intent.resolveActivity(mPkgManager) != null; 184 } 185} 186