• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.providers.downloads;
18 
19 import static com.android.providers.downloads.Constants.TAG;
20 
21 import android.content.ActivityNotFoundException;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.PackageInstaller;
25 import android.net.Uri;
26 import android.util.Log;
27 
28 import java.io.File;
29 
30 /**
31  * Contains helper methods to convert between raw document ids and their file-system file paths.
32  */
33 public class RawDocumentsHelper {
34     /** The default prefix to raw file documentIds */
35     public static final String RAW_PREFIX = "raw:";
36 
isRawDocId(String docId)37     public static boolean isRawDocId(String docId) {
38         return docId != null && docId.startsWith(RAW_PREFIX);
39     }
40 
getDocIdForFile(File file)41     public static String getDocIdForFile(File file) {
42         return RAW_PREFIX + file.getAbsolutePath();
43     }
44 
getAbsoluteFilePath(String rawDocumentId)45     public static String getAbsoluteFilePath(String rawDocumentId) {
46         return rawDocumentId.substring(RAW_PREFIX.length());
47     }
48 
49     /**
50      * Build and start an {@link Intent} to view the download with given raw documentId.
51      */
startViewIntent(Context context, Uri documentUri)52     public static boolean startViewIntent(Context context, Uri documentUri) {
53         final Intent intent = new Intent(Intent.ACTION_VIEW);
54         intent.setData(documentUri);
55         intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
56                 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
57         intent.putExtra(Intent.EXTRA_ORIGINATING_UID, PackageInstaller.SessionParams.UID_UNKNOWN);
58 
59         try {
60             context.startActivity(intent);
61             return true;
62         } catch (ActivityNotFoundException e) {
63             Log.w(TAG, "Failed to start " + intent + ": " + e);
64             return false;
65         }
66     }
67 
68 }
69