PageRenderTime 18ms CodeModel.GetById 9ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/WebAccess/src/com/ideal/webaccess/LocalJsProvider.java

http://eyes-free.googlecode.com/
Java | 92 lines | 57 code | 14 blank | 21 comment | 5 complexity | 2e2216aba6ea9c9d613335f9c0ccf8f5 MD5 | raw file
 1/*
 2 * Copyright (C) 2010 The IDEAL Group
 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
17package com.ideal.webaccess;
18
19import java.io.File;
20import java.io.FileNotFoundException;
21
22import android.content.ContentProvider;
23import android.content.ContentValues;
24import android.database.Cursor;
25import android.net.Uri;
26import android.os.Environment;
27import android.os.ParcelFileDescriptor;
28
29/**
30 * Content Provider for serving JavaScript. JavaScript files must be stored
31 * under: /sdcard/ideal-webaccess/js/ for example:
32 * /sdcard/ideal-webaccess/js/MY_JS_FILE.js JavaScript files can be accessed by
33 * in the WebView by using: content://com.ideal.webaccess.localjs/MY_JS_FILE.js
34 */
35public class LocalJsProvider extends ContentProvider {
36    private static final String URI_PREFIX = "content://com.ideal.webaccess.localjs";
37
38    public static String constructUri(String url) {
39        Uri uri = Uri.parse(url);
40        return uri.isAbsolute() ? url : URI_PREFIX + url;
41    }
42
43    @Override
44    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
45        String filename = uri.toString();
46        if (filename.length() > URI_PREFIX.length()) {
47            filename = filename.substring(URI_PREFIX.length() + 1);
48            if ((filename.indexOf("//") != -1) || (filename.indexOf("..") != -1)) {
49                return null;
50            }
51            filename = Environment.getExternalStorageDirectory() + "/ideal-webaccess/js/"
52                    + filename;
53
54            File file = new File(filename);
55            ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file,
56                    ParcelFileDescriptor.MODE_READ_ONLY);
57            return parcel;
58        }
59        return null;
60    }
61
62    @Override
63    public boolean onCreate() {
64        return true;
65    }
66
67    @Override
68    public int delete(Uri uri, String s, String[] as) {
69        throw new UnsupportedOperationException("Not supported by this provider");
70    }
71
72    @Override
73    public String getType(Uri uri) {
74        throw new UnsupportedOperationException("Not supported by this provider");
75    }
76
77    @Override
78    public Uri insert(Uri uri, ContentValues contentvalues) {
79        throw new UnsupportedOperationException("Not supported by this provider");
80    }
81
82    @Override
83    public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
84        throw new UnsupportedOperationException("Not supported by this provider");
85    }
86
87    @Override
88    public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
89        throw new UnsupportedOperationException("Not supported by this provider");
90    }
91
92}