Home | History | Annotate | Download | only in htmlviewer
      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 
     17 package com.android.htmlviewer;
     18 
     19 import java.io.FileNotFoundException;
     20 import java.io.File;
     21 import java.lang.UnsupportedOperationException;
     22 
     23 import android.content.ContentProvider;
     24 import android.content.ContentValues;
     25 import android.database.Cursor;
     26 import android.net.Uri;
     27 import android.os.Binder;
     28 import android.os.ParcelFileDescriptor;
     29 import 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 */
     37 public 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         File f = new File(filename);
     61         if (f.isDirectory()) {
     62             return null;
     63         }
     64         return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
     65     }
     66 
     67     @Override
     68     public int delete(Uri uri, String selection, String[] selectionArgs) {
     69         throw new UnsupportedOperationException();
     70     }
     71 
     72     @Override
     73     public Uri insert(Uri uri, ContentValues values) {
     74         throw new UnsupportedOperationException();
     75     }
     76 
     77     @Override
     78     public boolean onCreate() {
     79         return true;
     80     }
     81 
     82     @Override
     83     public Cursor query(Uri uri, String[] projection, String selection,
     84             String[] selectionArgs, String sortOrder) {
     85         throw new UnsupportedOperationException();
     86     }
     87 
     88     @Override
     89     public int update(Uri uri, ContentValues values, String selection,
     90             String[] selectionArgs) {
     91         throw new UnsupportedOperationException();
     92     }
     93 
     94 }
     95