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