Home | History | Annotate | Download | only in content
      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.database.MatrixCursor;
     30 import android.net.Uri;
     31 import android.os.Bundle;
     32 import android.os.ParcelFileDescriptor;
     33 import android.provider.OpenableColumns;
     34 import android.util.Log;
     35 
     36 /**
     37  * A very simple content provider that can serve arbitrary asset files from
     38  * our .apk.
     39  */
     40 public class FileProvider extends ContentProvider
     41         implements PipeDataWriter<InputStream> {
     42     @Override
     43     public boolean onCreate() {
     44         return true;
     45     }
     46 
     47     @Override
     48     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
     49             String sortOrder) {
     50 
     51         // content providers that support open and openAssetFile should support queries for all
     52         // android.provider.OpenableColumns.
     53 
     54         int displayNameIndex = -1;
     55         int sizeIndex = -1;
     56 
     57         // If projection is null, return all columns.
     58         if (projection == null) {
     59             projection = new String[] {
     60                     OpenableColumns.DISPLAY_NAME,
     61                     OpenableColumns.SIZE};
     62         }
     63 
     64         for (int i = 0; i < projection.length; i++) {
     65             if (OpenableColumns.DISPLAY_NAME.equals(projection[i])) {
     66                 displayNameIndex = i;
     67             }
     68             if (OpenableColumns.SIZE.equals(projection[i])) {
     69                 sizeIndex = i;
     70             }
     71         }
     72 
     73         MatrixCursor cursor = new MatrixCursor(projection);
     74         Object[] result = new Object[projection.length];
     75 
     76         for (int i = 0; i < result.length; i++) {
     77             if (i == displayNameIndex) {
     78                 result[i] = uri.getPath();
     79             }
     80             if (i == sizeIndex) {
     81                 result[i] = null; // Size is unknown, so null, if it was known, it would go here.
     82             }
     83         }
     84 
     85         cursor.addRow(result);
     86         return cursor;
     87     }
     88 
     89     @Override
     90     public Uri insert(Uri uri, ContentValues values) {
     91         // Don't support inserts.
     92         return null;
     93     }
     94 
     95     @Override
     96     public int delete(Uri uri, String selection, String[] selectionArgs) {
     97         // Don't support deletes.
     98         return 0;
     99     }
    100 
    101     @Override
    102     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    103         // Don't support updates.
    104         return 0;
    105     }
    106 
    107     @Override
    108     public String getType(Uri uri) {
    109         // For this sample, assume all files are .apks.
    110         return "application/vnd.android.package-archive";
    111     }
    112 
    113     @Override
    114     public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
    115         // Try to open an asset with the given name.
    116         try {
    117             InputStream is = getContext().getAssets().open(uri.getPath());
    118             // Start a new thread that pipes the stream data back to the caller.
    119             return new AssetFileDescriptor(
    120                     openPipeHelper(uri, null, null, is, this), 0,
    121                     AssetFileDescriptor.UNKNOWN_LENGTH);
    122         } catch (IOException e) {
    123             FileNotFoundException fnf = new FileNotFoundException("Unable to open " + uri);
    124             throw fnf;
    125         }
    126     }
    127 
    128     @Override
    129     public void writeDataToPipe(ParcelFileDescriptor output, Uri uri, String mimeType,
    130             Bundle opts, InputStream args) {
    131         // Transfer data from the asset to the pipe the client is reading.
    132         byte[] buffer = new byte[8192];
    133         int n;
    134         FileOutputStream fout = new FileOutputStream(output.getFileDescriptor());
    135         try {
    136             while ((n=args.read(buffer)) >= 0) {
    137                 fout.write(buffer, 0, n);
    138             }
    139         } catch (IOException e) {
    140             Log.i("InstallApk", "Failed transferring", e);
    141         } finally {
    142             try {
    143                 args.close();
    144             } catch (IOException e) {
    145             }
    146             try {
    147                 fout.close();
    148             } catch (IOException e) {
    149             }
    150         }
    151     }
    152 }
    153