• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.messaging.datamodel;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.database.Cursor;
22 import android.database.MatrixCursor;
23 import android.database.MatrixCursor.RowBuilder;
24 import android.net.Uri;
25 import android.provider.OpenableColumns;
26 import android.support.v4.util.SimpleArrayMap;
27 import android.text.TextUtils;
28 
29 import com.android.messaging.Factory;
30 import com.android.messaging.util.Assert;
31 import com.android.messaging.util.LogUtil;
32 import com.google.common.annotations.VisibleForTesting;
33 
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.List;
37 
38 /**
39  * A very simple content provider that can serve media files from our cache directory.
40  */
41 public class MediaScratchFileProvider extends FileProvider {
42     private static final String TAG = LogUtil.BUGLE_TAG;
43 
44     private static final SimpleArrayMap<Uri, String> sUriToDisplayNameMap =
45             new SimpleArrayMap<Uri, String>();
46 
47     @VisibleForTesting
48     public static final String AUTHORITY =
49             "com.android.messaging.datamodel.MediaScratchFileProvider";
50     private static final String MEDIA_SCRATCH_SPACE_DIR = "mediascratchspace";
51 
isMediaScratchSpaceUri(final Uri uri)52     public static boolean isMediaScratchSpaceUri(final Uri uri) {
53         if (uri == null) {
54             return false;
55         }
56 
57         final List<String> segments = uri.getPathSegments();
58         return (TextUtils.equals(uri.getScheme(), ContentResolver.SCHEME_CONTENT) &&
59                 TextUtils.equals(uri.getAuthority(), AUTHORITY) &&
60                 segments.size() == 1 && FileProvider.isValidFileId(segments.get(0)));
61     }
62 
63     /**
64      * Returns a uri that can be used to access a raw mms file.
65      *
66      * @return the URI for an raw mms file
67      */
buildMediaScratchSpaceUri(final String extension)68     public static Uri buildMediaScratchSpaceUri(final String extension) {
69         final Uri uri = FileProvider.buildFileUri(AUTHORITY, extension);
70         final File file = getFileWithExtension(uri.getPath(), extension);
71         if (!ensureFileExists(file)) {
72             LogUtil.e(TAG, "Failed to create temp file " + file.getAbsolutePath());
73         }
74         return uri;
75     }
76 
getFileFromUri(final Uri uri)77     public static File getFileFromUri(final Uri uri) {
78         Assert.equals(AUTHORITY, uri.getAuthority());
79         return getFileWithExtension(uri.getPath(), getExtensionFromUri(uri));
80     }
81 
getUriBuilder()82     public static Uri.Builder getUriBuilder() {
83         return (new Uri.Builder()).authority(AUTHORITY).scheme(ContentResolver.SCHEME_CONTENT);
84     }
85 
86     @Override
getFile(final String path, final String extension)87     File getFile(final String path, final String extension) {
88         return getFileWithExtension(path, extension);
89     }
90 
getFileWithExtension(final String path, final String extension)91     private static File getFileWithExtension(final String path, final String extension) {
92         final Context context = Factory.get().getApplicationContext();
93         final File filePath = new File(getDirectory(context),
94                 TextUtils.isEmpty(extension) ? path : path + "." + extension);
95 
96         try {
97             if (!filePath.getCanonicalPath()
98                     .startsWith(getDirectory(context).getCanonicalPath())) {
99                 LogUtil.e(TAG, "getFileWithExtension: path "
100                         + filePath.getCanonicalPath()
101                         + " does not start with "
102                         + getDirectory(context).getCanonicalPath());
103                 return null;
104             }
105         } catch (IOException e) {
106             LogUtil.e(TAG, "getFileWithExtension: getCanonicalPath failed ", e);
107             return null;
108         }
109         return filePath;
110     }
111 
getDirectory(final Context context)112     private static File getDirectory(final Context context) {
113         return new File(context.getCacheDir(), MEDIA_SCRATCH_SPACE_DIR);
114     }
115 
116     @Override
query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder)117     public Cursor query(final Uri uri, final String[] projection, final String selection,
118             final String[] selectionArgs, final String sortOrder) {
119         if (projection != null && projection.length > 0 &&
120                 TextUtils.equals(projection[0], OpenableColumns.DISPLAY_NAME) &&
121                 isMediaScratchSpaceUri(uri)) {
122             // Retrieve the display name associated with a temp file. This is used by the Contacts
123             // ImportVCardActivity to retrieve the name of the contact(s) being imported.
124             String displayName;
125             synchronized (sUriToDisplayNameMap) {
126                 displayName = sUriToDisplayNameMap.get(uri);
127             }
128             if (!TextUtils.isEmpty(displayName)) {
129                 MatrixCursor cursor =
130                         new MatrixCursor(new String[] { OpenableColumns.DISPLAY_NAME });
131                 RowBuilder row = cursor.newRow();
132                 row.add(displayName);
133                 return cursor;
134             }
135         }
136         return null;
137     }
138 
addUriToDisplayNameEntry(final Uri scratchFileUri, final String displayName)139     public static void addUriToDisplayNameEntry(final Uri scratchFileUri,
140             final String displayName) {
141         if (TextUtils.isEmpty(displayName)) {
142             return;
143         }
144         synchronized (sUriToDisplayNameMap) {
145             sUriToDisplayNameMap.put(scratchFileUri, displayName);
146         }
147     }
148 }
149