Nessuna descrizione

ContentFilesystem.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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;
  18. import android.content.ContentResolver;
  19. import android.content.Context;
  20. import android.database.Cursor;
  21. import android.net.Uri;
  22. import android.provider.DocumentsContract;
  23. import android.provider.MediaStore;
  24. import android.provider.OpenableColumns;
  25. import java.io.File;
  26. import java.io.FileNotFoundException;
  27. import java.io.IOException;
  28. import org.apache.cordova.CordovaResourceApi;
  29. import org.json.JSONException;
  30. import org.json.JSONObject;
  31. public class ContentFilesystem extends Filesystem {
  32. private final Context context;
  33. public ContentFilesystem(Context context, CordovaResourceApi resourceApi) {
  34. super(Uri.parse("content://"), "content", resourceApi);
  35. this.context = context;
  36. }
  37. @Override
  38. public Uri toNativeUri(LocalFilesystemURL inputURL) {
  39. String authorityAndPath = inputURL.uri.getEncodedPath().substring(this.name.length() + 2);
  40. if (authorityAndPath.length() < 2) {
  41. return null;
  42. }
  43. String ret = "content://" + authorityAndPath;
  44. String query = inputURL.uri.getEncodedQuery();
  45. if (query != null) {
  46. ret += '?' + query;
  47. }
  48. String frag = inputURL.uri.getEncodedFragment();
  49. if (frag != null) {
  50. ret += '#' + frag;
  51. }
  52. return Uri.parse(ret);
  53. }
  54. @Override
  55. public LocalFilesystemURL toLocalUri(Uri inputURL) {
  56. if (!"content".equals(inputURL.getScheme())) {
  57. return null;
  58. }
  59. String subPath = inputURL.getEncodedPath();
  60. if (subPath.length() > 0) {
  61. subPath = subPath.substring(1);
  62. }
  63. Uri.Builder b = new Uri.Builder()
  64. .scheme(LocalFilesystemURL.FILESYSTEM_PROTOCOL)
  65. .authority("localhost")
  66. .path(name)
  67. .appendPath(inputURL.getAuthority());
  68. if (subPath.length() > 0) {
  69. b.appendEncodedPath(subPath);
  70. }
  71. Uri localUri = b.encodedQuery(inputURL.getEncodedQuery())
  72. .encodedFragment(inputURL.getEncodedFragment())
  73. .build();
  74. return LocalFilesystemURL.parse(localUri);
  75. }
  76. @Override
  77. public JSONObject getFileForLocalURL(LocalFilesystemURL inputURL,
  78. String fileName, JSONObject options, boolean directory) throws IOException, TypeMismatchException, JSONException {
  79. throw new UnsupportedOperationException("getFile() not supported for content:. Use resolveLocalFileSystemURL instead.");
  80. }
  81. @Override
  82. public boolean removeFileAtLocalURL(LocalFilesystemURL inputURL)
  83. throws NoModificationAllowedException {
  84. Uri contentUri = toNativeUri(inputURL);
  85. try {
  86. context.getContentResolver().delete(contentUri, null, null);
  87. } catch (UnsupportedOperationException t) {
  88. // Was seeing this on the File mobile-spec tests on 4.0.3 x86 emulator.
  89. // The ContentResolver applies only when the file was registered in the
  90. // first case, which is generally only the case with images.
  91. NoModificationAllowedException nmae = new NoModificationAllowedException("Deleting not supported for content uri: " + contentUri);
  92. nmae.initCause(t);
  93. throw nmae;
  94. }
  95. return true;
  96. }
  97. @Override
  98. public boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL)
  99. throws NoModificationAllowedException {
  100. throw new NoModificationAllowedException("Cannot remove content url");
  101. }
  102. @Override
  103. public LocalFilesystemURL[] listChildren(LocalFilesystemURL inputURL) throws FileNotFoundException {
  104. throw new UnsupportedOperationException("readEntriesAtLocalURL() not supported for content:. Use resolveLocalFileSystemURL instead.");
  105. }
  106. @Override
  107. public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
  108. long size = -1;
  109. long lastModified = 0;
  110. Uri nativeUri = toNativeUri(inputURL);
  111. String mimeType = resourceApi.getMimeType(nativeUri);
  112. Cursor cursor = openCursorForURL(nativeUri);
  113. try {
  114. if (cursor != null && cursor.moveToFirst()) {
  115. Long sizeForCursor = resourceSizeForCursor(cursor);
  116. if (sizeForCursor != null) {
  117. size = sizeForCursor.longValue();
  118. }
  119. Long modified = lastModifiedDateForCursor(cursor);
  120. if (modified != null)
  121. lastModified = modified.longValue();
  122. } else {
  123. // Some content providers don't support cursors at all!
  124. CordovaResourceApi.OpenForReadResult offr = resourceApi.openForRead(nativeUri);
  125. size = offr.length;
  126. }
  127. } catch (IOException e) {
  128. FileNotFoundException fnfe = new FileNotFoundException();
  129. fnfe.initCause(e);
  130. throw fnfe;
  131. } finally {
  132. if (cursor != null)
  133. cursor.close();
  134. }
  135. JSONObject metadata = new JSONObject();
  136. try {
  137. metadata.put("size", size);
  138. metadata.put("type", mimeType);
  139. metadata.put("name", name);
  140. metadata.put("fullPath", inputURL.path);
  141. metadata.put("lastModifiedDate", lastModified);
  142. } catch (JSONException e) {
  143. return null;
  144. }
  145. return metadata;
  146. }
  147. @Override
  148. public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,
  149. int offset, boolean isBinary) throws NoModificationAllowedException {
  150. throw new NoModificationAllowedException("Couldn't write to file given its content URI");
  151. }
  152. @Override
  153. public long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
  154. throws NoModificationAllowedException {
  155. throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
  156. }
  157. protected Cursor openCursorForURL(Uri nativeUri) {
  158. ContentResolver contentResolver = context.getContentResolver();
  159. try {
  160. return contentResolver.query(nativeUri, null, null, null, null);
  161. } catch (UnsupportedOperationException e) {
  162. return null;
  163. }
  164. }
  165. private Long resourceSizeForCursor(Cursor cursor) {
  166. int columnIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
  167. if (columnIndex != -1) {
  168. String sizeStr = cursor.getString(columnIndex);
  169. if (sizeStr != null) {
  170. return Long.parseLong(sizeStr);
  171. }
  172. }
  173. return null;
  174. }
  175. protected Long lastModifiedDateForCursor(Cursor cursor) {
  176. int columnIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATE_MODIFIED);
  177. if (columnIndex == -1) {
  178. columnIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED);
  179. }
  180. if (columnIndex != -1) {
  181. String dateStr = cursor.getString(columnIndex);
  182. if (dateStr != null) {
  183. return Long.parseLong(dateStr);
  184. }
  185. }
  186. return null;
  187. }
  188. @Override
  189. public String filesystemPathForURL(LocalFilesystemURL url) {
  190. File f = resourceApi.mapUriToFile(toNativeUri(url));
  191. return f == null ? null : f.getAbsolutePath();
  192. }
  193. @Override
  194. public LocalFilesystemURL URLforFilesystemPath(String path) {
  195. // Returns null as we don't support reverse mapping back to content:// URLs
  196. return null;
  197. }
  198. @Override
  199. public boolean canRemoveFileAtLocalURL(LocalFilesystemURL inputURL) {
  200. return true;
  201. }
  202. }