openEventsPlatformApp/plugins/cordova-plugin-file/tests/src/android/TestContentProvider.java
plugins/cordova-plugin-file/tests/src/android/TestContentProvider.javaRaw
1/*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19package org.apache.cordova.file.test;
20
21import android.content.ContentProvider;
22import android.net.Uri;
23import android.content.res.AssetFileDescriptor;
24import android.content.res.AssetManager;
25
26import java.io.File;
27import java.io.FileInputStream;
28import java.io.FileNotFoundException;
29import android.content.ContentValues;
30import android.database.Cursor;
31import android.os.ParcelFileDescriptor;
32
33import org.apache.cordova.CordovaResourceApi;
34
35import java.io.IOException;
36import java.util.HashMap;
37
38public class TestContentProvider extends ContentProvider {
39
40 @Override
41 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
42 String fileName = uri.getQueryParameter("realPath");
43 if (fileName == null) {
44 fileName = uri.getPath();
45 }
46 if (fileName == null || fileName.length() < 1) {
47 throw new FileNotFoundException();
48 }
49 CordovaResourceApi resourceApi = new CordovaResourceApi(getContext(), null);
50 try {
51 File f = File.createTempFile("test-content-provider", ".tmp");
52 resourceApi.copyResource(Uri.parse("file:///android_asset" + fileName), Uri.fromFile(f));
53 return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
54 } catch (FileNotFoundException e) {
55 throw e;
56 } catch (IOException e) {
57 e.printStackTrace();
58 throw new FileNotFoundException("IO error: " + e.toString());
59 }
60 }
61
62 @Override
63 public boolean onCreate() {
64 return false;
65 }
66
67 @Override
68 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
69 throw new UnsupportedOperationException();
70 }
71
72 @Override
73 public String getType(Uri uri) {
74 return "text/html";
75 }
76
77 @Override
78 public Uri insert(Uri uri, ContentValues values) {
79 throw new UnsupportedOperationException();
80 }
81
82 @Override
83 public int delete(Uri uri, String selection, String[] selectionArgs) {
84 throw new UnsupportedOperationException();
85 }
86
87 @Override
88 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
89 throw new UnsupportedOperationException();
90 }
91
92
93}