Без опису

TestContentProvider.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova.file.test;
  18. import android.content.ContentProvider;
  19. import android.net.Uri;
  20. import android.content.res.AssetFileDescriptor;
  21. import android.content.res.AssetManager;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileNotFoundException;
  25. import android.content.ContentValues;
  26. import android.database.Cursor;
  27. import android.os.ParcelFileDescriptor;
  28. import org.apache.cordova.CordovaResourceApi;
  29. import java.io.IOException;
  30. import java.util.HashMap;
  31. public class TestContentProvider extends ContentProvider {
  32. @Override
  33. public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
  34. String fileName = uri.getQueryParameter("realPath");
  35. if (fileName == null) {
  36. fileName = uri.getPath();
  37. }
  38. if (fileName == null || fileName.length() < 1) {
  39. throw new FileNotFoundException();
  40. }
  41. CordovaResourceApi resourceApi = new CordovaResourceApi(getContext(), null);
  42. try {
  43. File f = File.createTempFile("test-content-provider", ".tmp");
  44. resourceApi.copyResource(Uri.parse("file:///android_asset" + fileName), Uri.fromFile(f));
  45. return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
  46. } catch (FileNotFoundException e) {
  47. throw e;
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. throw new FileNotFoundException("IO error: " + e.toString());
  51. }
  52. }
  53. @Override
  54. public boolean onCreate() {
  55. return false;
  56. }
  57. @Override
  58. public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  59. throw new UnsupportedOperationException();
  60. }
  61. @Override
  62. public String getType(Uri uri) {
  63. return "text/html";
  64. }
  65. @Override
  66. public Uri insert(Uri uri, ContentValues values) {
  67. throw new UnsupportedOperationException();
  68. }
  69. @Override
  70. public int delete(Uri uri, String selection, String[] selectionArgs) {
  71. throw new UnsupportedOperationException();
  72. }
  73. @Override
  74. public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  75. throw new UnsupportedOperationException();
  76. }
  77. }