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.Context;
20 import android.net.Uri;
21 import android.text.TextUtils;
22 
23 import com.android.messaging.Factory;
24 import com.android.messaging.util.LogUtil;
25 import com.google.common.annotations.VisibleForTesting;
26 
27 import java.io.File;
28 import java.io.IOException;
29 
30 /**
31  * A very simple content provider that can serve mms files from our cache directory.
32  */
33 public class MmsFileProvider extends FileProvider {
34     private static final String TAG = LogUtil.BUGLE_TAG;
35 
36     @VisibleForTesting
37     static final String AUTHORITY = "com.android.messaging.datamodel.MmsFileProvider";
38     private static final String RAW_MMS_DIR = "rawmms";
39 
40     /**
41      * Returns a uri that can be used to access a raw mms file.
42      *
43      * @return the URI for an raw mms file
44      */
buildRawMmsUri()45     public static Uri buildRawMmsUri() {
46         final Uri uri = FileProvider.buildFileUri(AUTHORITY, null);
47         final File file = getFile(uri.getPath());
48         if (!ensureFileExists(file)) {
49             LogUtil.e(TAG, "Failed to create temp file " + file.getAbsolutePath());
50         }
51         return uri;
52     }
53 
54     @Override
getFile(final String path, final String extension)55     File getFile(final String path, final String extension) {
56         return getFile(path);
57     }
58 
getFile(final Uri uri)59     public static File getFile(final Uri uri) {
60         return getFile(uri.getPath());
61     }
62 
getFile(final String path)63     private static File getFile(final String path) {
64         final Context context = Factory.get().getApplicationContext();
65         final File filePath = new File(getDirectory(context), path + ".dat");
66 
67         try {
68             if (!filePath.getCanonicalPath()
69                     .startsWith(getDirectory(context).getCanonicalPath())) {
70                 LogUtil.e(TAG, "getFile: path "
71                         + filePath.getCanonicalPath()
72                         + " does not start with "
73                         + getDirectory(context).getCanonicalPath());
74                 return null;
75             }
76         } catch (IOException e) {
77             LogUtil.e(TAG, "getFile: getCanonicalPath failed ", e);
78             return null;
79         }
80         return filePath;
81     }
82 
getDirectory(final Context context)83     private static File getDirectory(final Context context) {
84         return new File(context.getCacheDir(), RAW_MMS_DIR);
85     }
86 }
87