From 0a1a4287841fa10274c610de31e08765ffa0c269 Mon Sep 17 00:00:00 2001 From: Ben Lin Date: Wed, 8 Mar 2017 16:05:00 -0800 Subject: Add rawDocumentsUri handling to TrampolineActivity. Test: Build, compiles, does not crash. Bug: 36033829 Change-Id: I8756c6abd872e4b14cff3c604c37f82323b44c27 --- .../downloads/DownloadStorageProvider.java | 28 ++++----- .../android/providers/downloads/OpenHelper.java | 3 +- .../providers/downloads/RawDocumentsHelper.java | 68 ++++++++++++++++++++++ 3 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 src/com/android/providers/downloads/RawDocumentsHelper.java (limited to 'src/com/android/providers/downloads') diff --git a/src/com/android/providers/downloads/DownloadStorageProvider.java b/src/com/android/providers/downloads/DownloadStorageProvider.java index cbadeb49..8e4d7900 100644 --- a/src/com/android/providers/downloads/DownloadStorageProvider.java +++ b/src/com/android/providers/downloads/DownloadStorageProvider.java @@ -61,7 +61,6 @@ import javax.annotation.concurrent.GuardedBy; */ public class DownloadStorageProvider extends FileSystemProvider { private static final String TAG = "DownloadStorageProvider"; - private static final String RAW_PREFIX = "raw:"; private static final boolean DEBUG = false; private static final String AUTHORITY = Constants.STORAGE_AUTHORITY; @@ -135,7 +134,8 @@ public class DownloadStorageProvider extends FileSystemProvider { final long token = Binder.clearCallingIdentity(); try { String newDocumentId = super.createDocument(parentDocId, mimeType, displayName); - if (!Document.MIME_TYPE_DIR.equals(mimeType) && !isRawDocId(parentDocId)) { + if (!Document.MIME_TYPE_DIR.equals(mimeType) + && !RawDocumentsHelper.isRawDocId(parentDocId)) { File newFile = getFileForDocId(newDocumentId); newDocumentId = Long.toString(mDm.addCompletedDownload( newFile.getName(), newFile.getName(), true, mimeType, @@ -153,7 +153,7 @@ public class DownloadStorageProvider extends FileSystemProvider { // Delegate to real provider final long token = Binder.clearCallingIdentity(); try { - if (isRawDocId(docId)) { + if (RawDocumentsHelper.isRawDocId(docId)) { super.deleteDocument(docId); return; } @@ -171,7 +171,7 @@ public class DownloadStorageProvider extends FileSystemProvider { final long token = Binder.clearCallingIdentity(); try { - if (isRawDocId(docId)) { + if (RawDocumentsHelper.isRawDocId(docId)) { return super.renameDocument(docId, displayName); } @@ -193,7 +193,7 @@ public class DownloadStorageProvider extends FileSystemProvider { final long token = Binder.clearCallingIdentity(); Cursor cursor = null; try { - if (isRawDocId(docId)) { + if (RawDocumentsHelper.isRawDocId(docId)) { return super.queryDocument(docId, projection); } @@ -240,7 +240,7 @@ public class DownloadStorageProvider extends FileSystemProvider { final long token = Binder.clearCallingIdentity(); Cursor cursor = null; try { - if (isRawDocId(parentDocId)) { + if (RawDocumentsHelper.isRawDocId(parentDocId)) { return super.queryChildDocuments(parentDocId, projection, sortOrder); } @@ -346,7 +346,7 @@ public class DownloadStorageProvider extends FileSystemProvider { // Delegate to real provider final long token = Binder.clearCallingIdentity(); try { - if (isRawDocId(docId)) { + if (RawDocumentsHelper.isRawDocId(docId)) { return super.openDocument(docId, mode, signal); } @@ -368,8 +368,8 @@ public class DownloadStorageProvider extends FileSystemProvider { @Override protected File getFileForDocId(String docId, boolean visible) throws FileNotFoundException { - if (isRawDocId(docId)) { - return new File(getAbsoluteFilePath(docId)); + if (RawDocumentsHelper.isRawDocId(docId)) { + return new File(RawDocumentsHelper.getAbsoluteFilePath(docId)); } if (DOC_ID_ROOT.equals(docId)) { @@ -398,7 +398,7 @@ public class DownloadStorageProvider extends FileSystemProvider { @Override protected String getDocIdForFile(File file) throws FileNotFoundException { - return RAW_PREFIX + file.getAbsolutePath(); + return RawDocumentsHelper.getDocIdForFile(file); } @Override @@ -414,9 +414,7 @@ public class DownloadStorageProvider extends FileSystemProvider { Document.FLAG_DIR_PREFERS_LAST_MODIFIED | Document.FLAG_DIR_SUPPORTS_CREATE); } - private static String getAbsoluteFilePath(String rawDocumentId) { - return rawDocumentId.substring(RAW_PREFIX.length()); - } + /** * Adds the entry from the cursor to the result only if the entry is valid. That is, @@ -540,10 +538,6 @@ public class DownloadStorageProvider extends FileSystemProvider { includeFile(result, null, file); } - private boolean isRawDocId(String docId) { - return docId != null && docId.startsWith(RAW_PREFIX); - } - private static File getDownloadsDirectory() { return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); } diff --git a/src/com/android/providers/downloads/OpenHelper.java b/src/com/android/providers/downloads/OpenHelper.java index 69a44922..31561ff2 100644 --- a/src/com/android/providers/downloads/OpenHelper.java +++ b/src/com/android/providers/downloads/OpenHelper.java @@ -28,6 +28,7 @@ import android.content.ActivityNotFoundException; import android.content.ContentUris; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageInstaller; import android.database.Cursor; import android.net.Uri; import android.provider.DocumentsContract; @@ -132,7 +133,7 @@ public class OpenHelper { cursor.close(); } } - return -1; + return PackageInstaller.SessionParams.UID_UNKNOWN; } private static String getCursorString(Cursor cursor, String column) { diff --git a/src/com/android/providers/downloads/RawDocumentsHelper.java b/src/com/android/providers/downloads/RawDocumentsHelper.java new file mode 100644 index 00000000..9b14cdd2 --- /dev/null +++ b/src/com/android/providers/downloads/RawDocumentsHelper.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.providers.downloads; + +import static com.android.providers.downloads.Constants.TAG; + +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageInstaller; +import android.net.Uri; +import android.util.Log; + +import java.io.File; + +/** + * Contains helper methods to convert between raw document ids and their file-system file paths. + */ +public class RawDocumentsHelper { + /** The default prefix to raw file documentIds */ + public static final String RAW_PREFIX = "raw:"; + + public static boolean isRawDocId(String docId) { + return docId != null && docId.startsWith(RAW_PREFIX); + } + + public static String getDocIdForFile(File file) { + return RAW_PREFIX + file.getAbsolutePath(); + } + + public static String getAbsoluteFilePath(String rawDocumentId) { + return rawDocumentId.substring(RAW_PREFIX.length()); + } + + /** + * Build and start an {@link Intent} to view the download with given raw documentId. + */ + public static boolean startViewIntent(Context context, Uri documentUri) { + final Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(documentUri); + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION + | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + intent.putExtra(Intent.EXTRA_ORIGINATING_UID, PackageInstaller.SessionParams.UID_UNKNOWN); + + try { + context.startActivity(intent); + return true; + } catch (ActivityNotFoundException e) { + Log.w(TAG, "Failed to start " + intent + ": " + e); + return false; + } + } + +} -- cgit v1.2.3