1/**
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16
17package com.android.htmlviewer;
18
19import java.io.FileNotFoundException;
20import java.io.File;
21import java.lang.UnsupportedOperationException;
22
23import android.content.ContentProvider;
24import android.content.ContentValues;
25import android.database.Cursor;
26import android.net.Uri;
27import android.os.Binder;
28import android.os.ParcelFileDescriptor;
29import android.os.Process;
30
31/**
32 * WebView does not support file: loading. This class wraps a file load
33 * with a content provider.
34 * As HTMLViewer does not have internet access nor does it allow
35 * Javascript to be run, it is safe to load file based HTML content.
36*/
37public class FileContentProvider extends ContentProvider {
38
39    public static final String BASE_URI =
40            "content://com.android.htmlfileprovider";
41
42    @Override
43    public String getType(Uri uri) {
44        // If the mimetype is not appended to the uri, then return an empty string
45        String mimetype = uri.getQuery();
46        return mimetype == null ? "" : mimetype;
47    }
48
49    @Override
50    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
51        // android:exported="false" is broken in older releases so we have to
52        // manually enforce the calling identity.
53        if (Process.myUid() != Binder.getCallingUid()) {
54            throw new SecurityException("Permission denied");
55        }
56        if (!"r".equals(mode)) {
57            throw new FileNotFoundException("Bad mode for " + uri + ": " + mode);
58        }
59        String filename = uri.getPath();
60        return ParcelFileDescriptor.open(new File(filename),
61            ParcelFileDescriptor.MODE_READ_ONLY);
62    }
63
64    @Override
65    public int delete(Uri uri, String selection, String[] selectionArgs) {
66        throw new UnsupportedOperationException();
67    }
68
69    @Override
70    public Uri insert(Uri uri, ContentValues values) {
71        throw new UnsupportedOperationException();
72    }
73
74    @Override
75    public boolean onCreate() {
76        return true;
77    }
78
79    @Override
80    public Cursor query(Uri uri, String[] projection, String selection,
81            String[] selectionArgs, String sortOrder) {
82        throw new UnsupportedOperationException();
83    }
84
85    @Override
86    public int update(Uri uri, ContentValues values, String selection,
87            String[] selectionArgs) {
88        throw new UnsupportedOperationException();
89    }
90
91}
92