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     public static final int BASE_URI_LEN = BASE_URI.length();
     42 
     43     @Override
     44     public String getType(Uri uri) {
     45         // If the mimetype is not appended to the uri, then return an empty string
     46         String mimetype = uri.getQuery();
     47         return mimetype == null ? "" : mimetype;
     48     }
     49 
     50     @Override
     51     public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
     52         // android:exported="false" is broken in older releases so we have to
     53         // manually enforce the calling identity.
     54         if (Process.myUid() != Binder.getCallingUid()) {
     55             throw new SecurityException("Permission denied");
     56         }
     57         if (!"r".equals(mode)) {
     58             throw new FileNotFoundException("Bad mode for " + uri + ": " + mode);
     59         }
     60         String filename = uri.toString().substring(BASE_URI_LEN);
     61         return ParcelFileDescriptor.open(new File(filename),
     62             ParcelFileDescriptor.MODE_READ_ONLY);
     63     }
     64 
     65     @Override
     66     public int delete(Uri uri, String selection, String[] selectionArgs) {
     67         throw new UnsupportedOperationException();
     68     }
     69 
     70     @Override
     71     public Uri insert(Uri uri, ContentValues values) {
     72         throw new UnsupportedOperationException();
     73     }
     74 
     75     @Override
     76     public boolean onCreate() {
     77         return true;
     78     }
     79 
     80     @Override
     81     public Cursor query(Uri uri, String[] projection, String selection,
     82             String[] selectionArgs, String sortOrder) {
     83         throw new UnsupportedOperationException();
     84     }
     85 
     86     @Override
     87     public int update(Uri uri, ContentValues values, String selection,
     88             String[] selectionArgs) {
     89         throw new UnsupportedOperationException();
     90     }
     91 
     92 }
     93