• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package com.android.providers.downloads;
17 
18 import android.content.ContentUris;
19 import android.net.Uri;
20 import android.provider.MediaStore;
21 
22 public class MediaStoreDownloadsHelper {
23 
24     private static final String MEDIASTORE_DOWNLOAD_FILE_PREFIX = "msf:";
25     private static final String MEDIASTORE_DOWNLOAD_DIR_PREFIX = "msd:";
26 
getDocIdForMediaStoreDownload(long id, boolean isDir)27     public static String getDocIdForMediaStoreDownload(long id, boolean isDir) {
28         return (isDir ? MEDIASTORE_DOWNLOAD_DIR_PREFIX : MEDIASTORE_DOWNLOAD_FILE_PREFIX) + id;
29     }
30 
isMediaStoreDownload(String docId)31     public static boolean isMediaStoreDownload(String docId) {
32         return docId != null && (docId.startsWith(MEDIASTORE_DOWNLOAD_FILE_PREFIX)
33                 || docId.startsWith(MEDIASTORE_DOWNLOAD_DIR_PREFIX));
34     }
35 
getMediaStoreId(String docId)36     public static long getMediaStoreId(String docId) {
37         return Long.parseLong(getMediaStoreIdString(docId));
38     }
39 
40 
getMediaStoreIdString(String docId)41     public static String getMediaStoreIdString(String docId) {
42         final int index = docId.indexOf(":");
43         return docId.substring(index + 1);
44     }
45 
isMediaStoreDownloadDir(String docId)46     public static boolean isMediaStoreDownloadDir(String docId) {
47         return docId != null && docId.startsWith(MEDIASTORE_DOWNLOAD_DIR_PREFIX);
48     }
49 
50     /**
51      * The returned uri always appends external volume {@link MediaStore#VOLUME_EXTERNAL}.
52      * It doesn't consider the item is located on second volume. It can't be used to update
53      * or insert.
54      * @param docId the doc id
55      * @return external uri for query
56      */
getMediaStoreUriForQuery(String docId)57     public static Uri getMediaStoreUriForQuery(String docId) {
58         return getMediaStoreUri(MediaStore.VOLUME_EXTERNAL, docId);
59     }
60 
getMediaStoreUri(String volume, String docId)61     public static Uri getMediaStoreUri(String volume, String docId) {
62         return MediaStore.Downloads.getContentUri(volume, getMediaStoreId(docId));
63     }
64 }
65