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