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.files;
18 
19 import static com.android.documentsui.base.DocumentInfo.getCursorString;
20 import static com.android.documentsui.base.SharedMinimal.DEBUG;
21 import static com.android.documentsui.base.Shared.MAX_DOCS_IN_INTENT;
22 
23 import android.content.ClipData;
24 import android.content.ClipDescription;
25 import android.content.Intent;
26 import android.content.QuickViewConstants;
27 import android.content.pm.PackageManager;
28 import android.content.res.Resources;
29 import android.database.Cursor;
30 import android.net.Uri;
31 import android.os.Build;
32 import android.provider.DocumentsContract;
33 import android.provider.DocumentsContract.Document;
34 import androidx.annotation.Nullable;
35 import android.text.TextUtils;
36 import android.util.Log;
37 import android.util.Range;
38 
39 import com.android.documentsui.R;
40 import com.android.documentsui.base.DebugFlags;
41 import com.android.documentsui.base.DocumentInfo;
42 import com.android.documentsui.Model;
43 import com.android.documentsui.roots.RootCursorWrapper;
44 
45 import java.util.ArrayList;
46 import java.util.List;
47 
48 /**
49  * Provides support for gather a list of quick-viewable files into a quick view intent.
50  */
51 public final class QuickViewIntentBuilder {
52 
53     // trusted quick view package can be set via system property on debug builds.
54     // Unfortunately when the value is set, it interferes with testing (supercedes
55     // any value set in the resource system).
56     // For that reason when trusted quick view package is set to this magic value
57     // we won't honor the system property.
58     public static final String IGNORE_DEBUG_PROP = "*disabled*";
59     private static final String TAG = "QuickViewIntentBuilder";
60 
61     private static final String[] IN_ARCHIVE_FEATURES = {};
62     private static final String[] FULL_FEATURES = {
63             QuickViewConstants.FEATURE_VIEW,
64             QuickViewConstants.FEATURE_EDIT,
65             QuickViewConstants.FEATURE_DELETE,
66             QuickViewConstants.FEATURE_SEND,
67             QuickViewConstants.FEATURE_DOWNLOAD,
68             QuickViewConstants.FEATURE_PRINT
69     };
70     private static final String[] PICKER_FEATURES = {
71             QuickViewConstants.FEATURE_VIEW
72     };
73 
74     private final DocumentInfo mDocument;
75     private final Model mModel;
76 
77     private final PackageManager mPackageMgr;
78     private final Resources mResources;
79 
80     private final boolean mFromPicker;
81 
QuickViewIntentBuilder( PackageManager packageMgr, Resources resources, DocumentInfo doc, Model model, boolean fromPicker)82     public QuickViewIntentBuilder(
83             PackageManager packageMgr,
84             Resources resources,
85             DocumentInfo doc,
86             Model model,
87             boolean fromPicker) {
88 
89         assert(packageMgr != null);
90         assert(resources != null);
91         assert(doc != null);
92         assert(model != null);
93 
94         mPackageMgr = packageMgr;
95         mResources = resources;
96         mDocument = doc;
97         mModel = model;
98         mFromPicker = fromPicker;
99     }
100 
101     /**
102      * Builds the intent for quick viewing. Short circuits building if a handler cannot
103      * be resolved; in this case {@code null} is returned.
104      */
build()105     @Nullable public Intent build() {
106         if (DEBUG) {
107             Log.d(TAG, "Preparing intent for doc:" + mDocument.documentId);
108         }
109 
110         String trustedPkg = getQuickViewPackage();
111 
112         if (!TextUtils.isEmpty(trustedPkg)) {
113             Intent intent = new Intent(Intent.ACTION_QUICK_VIEW);
114             intent.setDataAndType(mDocument.derivedUri, mDocument.mimeType);
115             intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
116                     | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
117             intent.setPackage(trustedPkg);
118             if (hasRegisteredHandler(intent)) {
119                 includeQuickViewFeaturesFlag(intent, mDocument, mFromPicker);
120 
121                 final ArrayList<Uri> uris = new ArrayList<>();
122                 final int documentLocation = collectViewableUris(uris);
123                 final Range<Integer> range = computeSiblingsRange(uris, documentLocation);
124 
125                 ClipData clipData = null;
126                 ClipData.Item item;
127                 Uri uri;
128                 for (int i = range.getLower(); i <= range.getUpper(); i++) {
129                     uri = uris.get(i);
130                     item = new ClipData.Item(uri);
131                     if (DEBUG) {
132                         Log.d(TAG, "Including file: " + uri);
133                     }
134                     if (clipData == null) {
135                         clipData = new ClipData(
136                                 "URIs", new String[] { ClipDescription.MIMETYPE_TEXT_URILIST },
137                                 item);
138                     } else {
139                         clipData.addItem(item);
140                     }
141                 }
142 
143                 // The documentLocation variable contains an index in "uris". However,
144                 // ClipData contains a slice of "uris", so we need to shift the location
145                 // so it points to the same Uri.
146                 intent.putExtra(Intent.EXTRA_INDEX, documentLocation - range.getLower());
147                 intent.setClipData(clipData);
148 
149                 return intent;
150             } else {
151                 Log.e(TAG, "Can't resolve trusted quick view package: " + trustedPkg);
152             }
153         }
154 
155         return null;
156     }
157 
getQuickViewPackage()158     private String getQuickViewPackage() {
159         String resValue = mResources.getString(R.string.trusted_quick_viewer_package);
160 
161         // Allow automated tests to hard-disable quick viewing.
162         if (IGNORE_DEBUG_PROP.equals(resValue)) {
163             return "";
164         }
165 
166         // Allow users of debug devices to override default quick viewer
167         // for the purposes of testing.
168         if (DEBUG) {
169             String quickViewer = DebugFlags.getQuickViewer();
170             if (quickViewer != null) {
171                 return quickViewer;
172             }
173         }
174         return resValue;
175     }
176 
collectViewableUris(ArrayList<Uri> uris)177     private int collectViewableUris(ArrayList<Uri> uris) {
178         final String[] siblingIds = mModel.getModelIds();
179         uris.ensureCapacity(siblingIds.length);
180 
181         int documentLocation = 0;
182         Cursor cursor;
183         String mimeType;
184         String id;
185         String authority;
186         Uri uri;
187 
188         // Cursor's are not guaranteed to be immutable. Hence, traverse it only once.
189         for (int i = 0; i < siblingIds.length; i++) {
190             cursor = mModel.getItem(siblingIds[i]);
191 
192             if (cursor == null) {
193                 if (DEBUG) {
194                     Log.d(TAG,
195                         "Unable to obtain cursor for sibling document, modelId: "
196                             + siblingIds[i]);
197                 }
198                 continue;
199             }
200 
201             mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
202             if (Document.MIME_TYPE_DIR.equals(mimeType)) {
203                 if (DEBUG) {
204                     Log.d(TAG,
205                         "Skipping directory, not supported by quick view. modelId: "
206                             + siblingIds[i]);
207                 }
208                 continue;
209             }
210 
211             id = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
212             authority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
213             uri = DocumentsContract.buildDocumentUri(authority, id);
214 
215             uris.add(uri);
216 
217             if (id.equals(mDocument.documentId)) {
218                 documentLocation = uris.size() - 1;  // Position in "uris", not in the model.
219                 if (DEBUG) {
220                     Log.d(TAG, "Found starting point for QV. " + documentLocation);
221                 }
222             }
223         }
224 
225         return documentLocation;
226     }
227 
hasRegisteredHandler(Intent intent)228     private boolean hasRegisteredHandler(Intent intent) {
229         // Try to resolve the intent. If a matching app isn't installed, it won't resolve.
230         return intent.resolveActivity(mPackageMgr) != null;
231     }
232 
includeQuickViewFeaturesFlag(Intent intent, DocumentInfo doc, boolean fromPicker)233     private static void includeQuickViewFeaturesFlag(Intent intent, DocumentInfo doc,
234             boolean fromPicker) {
235         intent.putExtra(
236                 Intent.EXTRA_QUICK_VIEW_FEATURES,
237                 doc.isInArchive() ? IN_ARCHIVE_FEATURES
238                         : fromPicker ? PICKER_FEATURES : FULL_FEATURES);
239     }
240 
computeSiblingsRange(List<Uri> uris, int documentLocation)241     private static Range<Integer> computeSiblingsRange(List<Uri> uris, int documentLocation) {
242         // Restrict number of siblings to avoid hitting the IPC limit.
243         // TODO: Remove this restriction once ClipData can hold an arbitrary number of
244         // items.
245         int firstSibling;
246         int lastSibling;
247         if (documentLocation < uris.size() / 2) {
248             firstSibling = Math.max(0, documentLocation - MAX_DOCS_IN_INTENT / 2);
249             lastSibling = Math.min(uris.size() - 1, firstSibling + MAX_DOCS_IN_INTENT - 1);
250         } else {
251             lastSibling = Math.min(uris.size() - 1, documentLocation + MAX_DOCS_IN_INTENT / 2);
252             firstSibling = Math.max(0, lastSibling - MAX_DOCS_IN_INTENT + 1);
253         }
254 
255         if (DEBUG) {
256             Log.d(TAG, "Copmuted siblings from index: " + firstSibling
257                 + " to: " + lastSibling);
258         }
259 
260         return new Range(firstSibling, lastSibling);
261     }
262 }
263