1 /* 2 * Copyright (C) 2011 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.content; 18 19 import java.io.FileNotFoundException; 20 import java.io.FileOutputStream; 21 import java.io.IOException; 22 import java.io.InputStream; 23 24 import android.content.ContentProvider; 25 import android.content.ContentValues; 26 import android.content.ContentProvider.PipeDataWriter; 27 import android.content.res.AssetFileDescriptor; 28 import android.database.Cursor; 29 import android.net.Uri; 30 import android.os.Bundle; 31 import android.os.ParcelFileDescriptor; 32 import android.util.Log; 33 34 /** 35 * A very simple content provider that can serve arbitrary asset files from 36 * our .apk. 37 */ 38 public class FileProvider extends ContentProvider 39 implements PipeDataWriter<InputStream> { 40 @Override 41 public boolean onCreate() { 42 return true; 43 } 44 45 @Override 46 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 47 String sortOrder) { 48 // Don't support queries. 49 return null; 50 } 51 52 @Override 53 public Uri insert(Uri uri, ContentValues values) { 54 // Don't support inserts. 55 return null; 56 } 57 58 @Override 59 public int delete(Uri uri, String selection, String[] selectionArgs) { 60 // Don't support deletes. 61 return 0; 62 } 63 64 @Override 65 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 66 // Don't support updates. 67 return 0; 68 } 69 70 @Override 71 public String getType(Uri uri) { 72 // For this sample, assume all files are .apks. 73 return "application/vnd.android.package-archive"; 74 } 75 76 @Override 77 public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException { 78 // Try to open an asset with the given name. 79 try { 80 InputStream is = getContext().getAssets().open(uri.getPath()); 81 // Start a new thread that pipes the stream data back to the caller. 82 return new AssetFileDescriptor( 83 openPipeHelper(uri, null, null, is, this), 0, 84 AssetFileDescriptor.UNKNOWN_LENGTH); 85 } catch (IOException e) { 86 FileNotFoundException fnf = new FileNotFoundException("Unable to open " + uri); 87 throw fnf; 88 } 89 } 90 91 @Override 92 public void writeDataToPipe(ParcelFileDescriptor output, Uri uri, String mimeType, 93 Bundle opts, InputStream args) { 94 // Transfer data from the asset to the pipe the client is reading. 95 byte[] buffer = new byte[8192]; 96 int n; 97 FileOutputStream fout = new FileOutputStream(output.getFileDescriptor()); 98 try { 99 while ((n=args.read(buffer)) >= 0) { 100 fout.write(buffer, 0, n); 101 } 102 } catch (IOException e) { 103 Log.i("InstallApk", "Failed transferring", e); 104 } finally { 105 try { 106 args.close(); 107 } catch (IOException e) { 108 } 109 try { 110 fout.close(); 111 } catch (IOException e) { 112 } 113 } 114 } 115 } 116