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