PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/cordova-plugin-file/src/android/ContentFilesystem.java

https://gitlab.com/rollbrettler/gallery
Java | 216 lines | 174 code | 19 blank | 23 comment | 27 complexity | abfc571c363e5f8fdebe65d6f5e6e9bf MD5 | raw file
  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. throw new NoModificationAllowedException("Deleting not supported for content uri: " + contentUri);
  92. }
  93. return true;
  94. }
  95. @Override
  96. public boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL)
  97. throws NoModificationAllowedException {
  98. throw new NoModificationAllowedException("Cannot remove content url");
  99. }
  100. @Override
  101. public LocalFilesystemURL[] listChildren(LocalFilesystemURL inputURL) throws FileNotFoundException {
  102. throw new UnsupportedOperationException("readEntriesAtLocalURL() not supported for content:. Use resolveLocalFileSystemURL instead.");
  103. }
  104. @Override
  105. public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
  106. long size = -1;
  107. long lastModified = 0;
  108. Uri nativeUri = toNativeUri(inputURL);
  109. String mimeType = resourceApi.getMimeType(nativeUri);
  110. Cursor cursor = openCursorForURL(nativeUri);
  111. try {
  112. if (cursor != null && cursor.moveToFirst()) {
  113. size = resourceSizeForCursor(cursor);
  114. Long modified = lastModifiedDateForCursor(cursor);
  115. if (modified != null)
  116. lastModified = modified.longValue();
  117. } else {
  118. // Some content providers don't support cursors at all!
  119. CordovaResourceApi.OpenForReadResult offr = resourceApi.openForRead(nativeUri);
  120. size = offr.length;
  121. }
  122. } catch (IOException e) {
  123. throw new FileNotFoundException();
  124. } finally {
  125. if (cursor != null)
  126. cursor.close();
  127. }
  128. JSONObject metadata = new JSONObject();
  129. try {
  130. metadata.put("size", size);
  131. metadata.put("type", mimeType);
  132. metadata.put("name", name);
  133. metadata.put("fullPath", inputURL.path);
  134. metadata.put("lastModifiedDate", lastModified);
  135. } catch (JSONException e) {
  136. return null;
  137. }
  138. return metadata;
  139. }
  140. @Override
  141. public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,
  142. int offset, boolean isBinary) throws NoModificationAllowedException {
  143. throw new NoModificationAllowedException("Couldn't write to file given its content URI");
  144. }
  145. @Override
  146. public long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
  147. throws NoModificationAllowedException {
  148. throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
  149. }
  150. protected Cursor openCursorForURL(Uri nativeUri) {
  151. ContentResolver contentResolver = context.getContentResolver();
  152. try {
  153. return contentResolver.query(nativeUri, null, null, null, null);
  154. } catch (UnsupportedOperationException e) {
  155. return null;
  156. }
  157. }
  158. private Long resourceSizeForCursor(Cursor cursor) {
  159. int columnIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
  160. if (columnIndex != -1) {
  161. String sizeStr = cursor.getString(columnIndex);
  162. if (sizeStr != null) {
  163. return Long.parseLong(sizeStr);
  164. }
  165. }
  166. return null;
  167. }
  168. protected Long lastModifiedDateForCursor(Cursor cursor) {
  169. int columnIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATE_MODIFIED);
  170. if (columnIndex == -1) {
  171. columnIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED);
  172. }
  173. if (columnIndex != -1) {
  174. String dateStr = cursor.getString(columnIndex);
  175. if (dateStr != null) {
  176. return Long.parseLong(dateStr);
  177. }
  178. }
  179. return null;
  180. }
  181. @Override
  182. public String filesystemPathForURL(LocalFilesystemURL url) {
  183. File f = resourceApi.mapUriToFile(toNativeUri(url));
  184. return f == null ? null : f.getAbsolutePath();
  185. }
  186. @Override
  187. public LocalFilesystemURL URLforFilesystemPath(String path) {
  188. // Returns null as we don't support reverse mapping back to content:// URLs
  189. return null;
  190. }
  191. @Override
  192. public boolean canRemoveFileAtLocalURL(LocalFilesystemURL inputURL) {
  193. return true;
  194. }
  195. }