• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
onCreate()43     public boolean onCreate() {
44         return true;
45     }
46 
47     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)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
insert(Uri uri, ContentValues values)90     public Uri insert(Uri uri, ContentValues values) {
91         // Don't support inserts.
92         return null;
93     }
94 
95     @Override
delete(Uri uri, String selection, String[] selectionArgs)96     public int delete(Uri uri, String selection, String[] selectionArgs) {
97         // Don't support deletes.
98         return 0;
99     }
100 
101     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)102     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
103         // Don't support updates.
104         return 0;
105     }
106 
107     @Override
getType(Uri uri)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
openFile(Uri uri, String mode)114     public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
115         // Try to open an asset with the given name.
116         try {
117             String path = uri.getPath();
118             int off = path.indexOf('/', 1);
119             if (off < 0 || off >= (path.length()-1)) {
120                 throw new FileNotFoundException("Unable to open " + uri);
121             }
122             int cookie = Integer.parseInt(path.substring(1, off));
123             String assetPath = path.substring(off+1);
124             AssetFileDescriptor asset = getContext().getAssets().openNonAssetFd(cookie, assetPath);
125             return new ParcelFileDescriptor(openPipeHelper(uri, null, null,
126                     asset.createInputStream(), this));
127         } catch (IOException e) {
128             FileNotFoundException fnf = new FileNotFoundException("Unable to open " + uri);
129             throw fnf;
130         }
131     }
132 
133     @Override
writeDataToPipe(ParcelFileDescriptor output, Uri uri, String mimeType, Bundle opts, InputStream args)134     public void writeDataToPipe(ParcelFileDescriptor output, Uri uri, String mimeType,
135             Bundle opts, InputStream args) {
136         // Transfer data from the asset to the pipe the client is reading.
137         byte[] buffer = new byte[8192];
138         int n;
139         FileOutputStream fout = new FileOutputStream(output.getFileDescriptor());
140         try {
141             while ((n=args.read(buffer)) >= 0) {
142                 fout.write(buffer, 0, n);
143             }
144         } catch (IOException e) {
145             Log.i("InstallApk", "Failed transferring", e);
146         } finally {
147             try {
148                 args.close();
149             } catch (IOException e) {
150             }
151             try {
152                 fout.close();
153             } catch (IOException e) {
154             }
155         }
156     }
157 }
158