to top
ActionBarCompat-ShareActionProvider / src / com.example.android.actionbarcompat.shareactionprovider / content /

AssetProvider.java

1
/*
2
 * Copyright (C) 2013 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.actionbarcompat.shareactionprovider.content;
18
 
19
import android.content.ContentProvider;
20
import android.content.ContentValues;
21
import android.content.res.AssetFileDescriptor;
22
import android.content.res.AssetManager;
23
import android.database.Cursor;
24
import android.net.Uri;
25
import android.text.TextUtils;
26
 
27
import java.io.FileNotFoundException;
28
import java.io.IOException;
29
 
30
/**
31
 * A simple ContentProvider which can serve files from this application's assets. The majority of
32
 * functionality is in {@link #openAssetFile(android.net.Uri, String)}.
33
 */
34
public class AssetProvider extends ContentProvider {
35
 
36
    public static String CONTENT_URI = "com.example.android.actionbarcompat.shareactionprovider";
37
 
38
    @Override
39
    public boolean onCreate() {
40
        return true;
41
    }
42
 
43
    @Override
44
    public int delete(Uri uri, String selection, String[] selectionArgs) {
45
        // Do not support delete requests.
46
        return 0;
47
    }
48
 
49
    @Override
50
    public String getType(Uri uri) {
51
        // Do not support returning the data type
52
        return null;
53
    }
54
 
55
    @Override
56
    public Uri insert(Uri uri, ContentValues values) {
57
        // Do not support insert requests.
58
        return null;
59
    }
60
 
61
    @Override
62
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
63
            String sortOrder) {
64
        // Do not support query requests.
65
        return null;
66
    }
67
 
68
    @Override
69
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
70
        // Do not support update requests.
71
        return 0;
72
    }
73
 
74
    @Override
75
    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
76
        // The asset file name should be the last path segment
77
        final String assetName = uri.getLastPathSegment();
78
 
79
        // If the given asset name is empty, throw an exception
80
        if (TextUtils.isEmpty(assetName)) {
81
            throw new FileNotFoundException();
82
        }
83
 
84
        try {
85
            // Try and return a file descriptor for the given asset name
86
            AssetManager am = getContext().getAssets();
87
            return am.openFd(assetName);
88
        } catch (IOException e) {
89
            e.printStackTrace();
90
            return super.openAssetFile(uri, mode);
91
        }
92
    }
93
}