QuickViewIntentBuilder.java revision 58efce36cd1b26006e08ddd696292bf06fc34c81
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.model.DocumentInfo.getCursorString;
20
21import android.content.ClipData;
22import android.content.ClipDescription;
23import android.content.ComponentName;
24import android.content.Intent;
25import android.content.pm.PackageManager;
26import android.database.Cursor;
27import android.net.Uri;
28import android.provider.DocumentsContract;
29import android.provider.DocumentsContract.Document;
30import android.support.annotation.Nullable;
31import android.util.Log;
32
33import com.android.documentsui.BaseActivity.DocumentContext;
34import com.android.documentsui.model.DocumentInfo;
35
36/**
37 * Provides support for gather a list of quick-viewable files into a quick view intent.
38 */
39final class QuickViewIntentBuilder {
40
41    private static final String TAG = "QvIntentBuilder";
42    private static final boolean DEBUG = false;
43
44    private final DocumentInfo mDocument;
45    private final DocumentContext mContext;
46
47    public ClipData mClipData;
48    public int mDocumentLocation;
49    private PackageManager mPkgManager;
50
51    public QuickViewIntentBuilder(
52            PackageManager pkgManager, DocumentInfo doc, DocumentContext context) {
53        mPkgManager = pkgManager;
54        mDocument = doc;
55        mContext = context;
56    }
57
58    /**
59     * Builds the intent for quick viewing. Short circuits building if a handler cannot
60     * be resolved; in this case {@code null} is returned.
61     */
62    @Nullable Intent build() {
63        if (DEBUG) Log.d(TAG, "Preparing intent for doc:" + mDocument.documentId);
64
65        Intent intent = new Intent(Intent.ACTION_QUICK_VIEW);
66        intent.setDataAndType(mDocument.derivedUri, mDocument.mimeType);
67
68        // Try to resolve the intent. If a matching app isn't installed, it won't resolve.
69        ComponentName handler = intent.resolveActivity(mPkgManager);
70        if (handler == null) {
71            return null;
72        }
73
74        Cursor cursor = mContext.getCursor();
75        for (int i = 0; i < cursor.getCount(); i++) {
76            onNextItem(i, cursor);
77        }
78
79        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
80        intent.putExtra(Intent.EXTRA_INDEX, mDocumentLocation);
81        intent.setClipData(mClipData);
82
83        return intent;
84    }
85
86    private void onNextItem(int index, Cursor cursor) {
87        cursor.moveToPosition(index);
88
89        String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
90        if (Document.MIME_TYPE_DIR.equals(mimeType)) {
91            return;
92        }
93
94        String id = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
95        String authority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
96        Uri uri = DocumentsContract.buildDocumentUri(authority, id);
97        if (DEBUG) Log.d(TAG, "Including file[" + id + "] @ " + uri);
98
99        if (id.equals(mDocument.documentId)) {
100            if (DEBUG) Log.d(TAG, "Found starting point for QV. " + index);
101            mDocumentLocation = index;
102        }
103
104        ClipData.Item item = new ClipData.Item(uri);
105        if (mClipData == null) {
106            mClipData = new ClipData(
107                    "URIs", new String[]{ClipDescription.MIMETYPE_TEXT_URILIST}, item);
108        } else {
109            mClipData.addItem(item);
110        }
111    }
112}
113