1 /*
2  * Copyright (C) 2014 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.example.android.apis.os;
18 
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 
25 import android.content.ContentProvider;
26 import android.content.ContentValues;
27 import android.database.Cursor;
28 import android.net.Uri;
29 import android.os.ParcelFileDescriptor;
30 import android.text.TextUtils;
31 
32 /**
33  * A very simple content provider that can serve mms files from our cache directory so that
34  * SmsManager#sendMultimdeiaMessage and SmsManager#downloadMultimediaMessage can read/write
35  * the content of the MMS messages to send/download.
36  */
37 public class MmsFileProvider extends ContentProvider {
38     @Override
onCreate()39     public boolean onCreate() {
40         return true;
41     }
42 
43     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)44     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
45             String sortOrder) {
46         // Don't support queries.
47         return null;
48     }
49 
50     @Override
insert(Uri uri, ContentValues values)51     public Uri insert(Uri uri, ContentValues values) {
52         // Don't support inserts.
53         return null;
54     }
55 
56     @Override
delete(Uri uri, String selection, String[] selectionArgs)57     public int delete(Uri uri, String selection, String[] selectionArgs) {
58         // Don't support deletes.
59         return 0;
60     }
61 
62     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)63     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
64         // Don't support updates.
65         return 0;
66     }
67 
68     @Override
getType(Uri uri)69     public String getType(Uri uri) {
70         // For this sample, assume all files have no type.
71         return null;
72     }
73 
74     @Override
openFile(Uri uri, String fileMode)75     public ParcelFileDescriptor openFile(Uri uri, String fileMode) throws FileNotFoundException {
76         File file = new File(getContext().getCacheDir(), uri.getPath());
77         int mode = (TextUtils.equals(fileMode, "r") ? ParcelFileDescriptor.MODE_READ_ONLY :
78             ParcelFileDescriptor.MODE_WRITE_ONLY
79                    |ParcelFileDescriptor.MODE_TRUNCATE
80                    |ParcelFileDescriptor.MODE_CREATE);
81         return ParcelFileDescriptor.open(file, mode);
82     }
83 }
84