DocumentsStorageProvider.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * nextCloud Android client application
  3. *
  4. * @author Bartosz Przybylski
  5. * Copyright (C) 2016 Bartosz Przybylski <bart.p.pl@gmail.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.providers;
  21. import android.accounts.Account;
  22. import android.annotation.TargetApi;
  23. import android.content.ContentResolver;
  24. import android.content.Intent;
  25. import android.content.res.AssetFileDescriptor;
  26. import android.database.Cursor;
  27. import android.graphics.Point;
  28. import android.os.Build;
  29. import android.os.CancellationSignal;
  30. import android.os.ParcelFileDescriptor;
  31. import android.provider.DocumentsProvider;
  32. import com.owncloud.android.authentication.AccountUtils;
  33. import com.owncloud.android.datamodel.FileDataStorageManager;
  34. import com.owncloud.android.datamodel.OCFile;
  35. import com.owncloud.android.files.services.FileDownloader;
  36. import org.nextcloud.providers.cursors.FileCursor;
  37. import org.nextcloud.providers.cursors.RootCursor;
  38. import java.io.File;
  39. import java.io.FileNotFoundException;
  40. import java.util.HashMap;
  41. import java.util.Map;
  42. import java.util.Vector;
  43. @TargetApi(Build.VERSION_CODES.KITKAT)
  44. public class DocumentsStorageProvider extends DocumentsProvider {
  45. private FileDataStorageManager mCurrentStorageManager = null;
  46. private static Map<Long, FileDataStorageManager> mRootIdToStorageManager;
  47. @Override
  48. public Cursor queryRoots(String[] projection) throws FileNotFoundException {
  49. initiateStorageMap();
  50. final RootCursor result = new RootCursor(projection);
  51. for (Account account : AccountUtils.getAccounts(getContext())) {
  52. result.addRoot(account, getContext());
  53. }
  54. return result;
  55. }
  56. @Override
  57. public Cursor queryDocument(String documentId, String[] projection) throws FileNotFoundException {
  58. final long docId = Long.parseLong(documentId);
  59. updateCurrentStorageManagerIfNeeded(docId);
  60. final FileCursor result = new FileCursor(projection);
  61. OCFile file = mCurrentStorageManager.getFileById(docId);
  62. if (file != null) {
  63. result.addFile(file);
  64. }
  65. return result;
  66. }
  67. @Override
  68. public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder)
  69. throws FileNotFoundException {
  70. final long folderId = Long.parseLong(parentDocumentId);
  71. updateCurrentStorageManagerIfNeeded(folderId);
  72. final FileCursor result = new FileCursor(projection);
  73. final OCFile browsedDir = mCurrentStorageManager.getFileById(folderId);
  74. for (OCFile file : mCurrentStorageManager.getFolderContent(browsedDir, false)) {
  75. result.addFile(file);
  76. }
  77. return result;
  78. }
  79. @Override
  80. public ParcelFileDescriptor openDocument(String documentId, String mode, CancellationSignal cancellationSignal)
  81. throws FileNotFoundException {
  82. final long docId = Long.parseLong(documentId);
  83. updateCurrentStorageManagerIfNeeded(docId);
  84. OCFile file = mCurrentStorageManager.getFileById(docId);
  85. if (!file.isDown()) {
  86. Intent i = new Intent(getContext(), FileDownloader.class);
  87. i.putExtra(FileDownloader.EXTRA_ACCOUNT, mCurrentStorageManager.getAccount());
  88. i.putExtra(FileDownloader.EXTRA_FILE, file);
  89. getContext().startService(i);
  90. do {
  91. if (!waitOrGetCancelled(cancellationSignal)) {
  92. return null;
  93. }
  94. file = mCurrentStorageManager.getFileById(docId);
  95. } while (!file.isDown());
  96. }
  97. return ParcelFileDescriptor.open(new File(file.getStoragePath()), ParcelFileDescriptor.parseMode(mode));
  98. }
  99. @Override
  100. public boolean onCreate() {
  101. return true;
  102. }
  103. @Override
  104. public AssetFileDescriptor openDocumentThumbnail(String documentId,
  105. Point sizeHint,
  106. CancellationSignal signal)
  107. throws FileNotFoundException {
  108. long docId = Long.parseLong(documentId);
  109. updateCurrentStorageManagerIfNeeded(docId);
  110. OCFile file = mCurrentStorageManager.getFileById(docId);
  111. File realFile = new File(file.getStoragePath());
  112. return new AssetFileDescriptor(
  113. ParcelFileDescriptor.open(realFile, ParcelFileDescriptor.MODE_READ_ONLY),
  114. 0,
  115. AssetFileDescriptor.UNKNOWN_LENGTH);
  116. }
  117. @Override
  118. public Cursor querySearchDocuments(String rootId, String query, String[] projection) throws FileNotFoundException {
  119. updateCurrentStorageManagerIfNeeded(rootId);
  120. OCFile root = mCurrentStorageManager.getFileByPath("/");
  121. FileCursor result = new FileCursor(projection);
  122. for (OCFile f : findFiles(root, query)) {
  123. result.addFile(f);
  124. }
  125. return result;
  126. }
  127. private void updateCurrentStorageManagerIfNeeded(long docId) {
  128. if (mCurrentStorageManager == null ||
  129. (mRootIdToStorageManager.containsKey(docId) &&
  130. mCurrentStorageManager != mRootIdToStorageManager.get(docId))) {
  131. mCurrentStorageManager = mRootIdToStorageManager.get(docId);
  132. }
  133. }
  134. private void updateCurrentStorageManagerIfNeeded(String rootId) {
  135. for (FileDataStorageManager data : mRootIdToStorageManager.values()) {
  136. if (data.getAccount().name.equals(rootId)) {
  137. mCurrentStorageManager = data;
  138. }
  139. }
  140. }
  141. private void initiateStorageMap() {
  142. mRootIdToStorageManager = new HashMap<>();
  143. ContentResolver contentResolver = getContext().getContentResolver();
  144. for (Account account : AccountUtils.getAccounts(getContext())) {
  145. final FileDataStorageManager storageManager =
  146. new FileDataStorageManager(account, contentResolver);
  147. final OCFile rootDir = storageManager.getFileByPath("/");
  148. mRootIdToStorageManager.put(rootDir.getFileId(), storageManager);
  149. }
  150. }
  151. private boolean waitOrGetCancelled(CancellationSignal cancellationSignal) {
  152. try {
  153. Thread.sleep(1000);
  154. } catch (InterruptedException e) {
  155. return false;
  156. }
  157. return !(cancellationSignal != null && cancellationSignal.isCanceled());
  158. }
  159. Vector<OCFile> findFiles(OCFile root, String query) {
  160. Vector<OCFile> result = new Vector<>();
  161. for (OCFile f : mCurrentStorageManager.getFolderContent(root, false)) {
  162. if (f.isFolder()) {
  163. result.addAll(findFiles(f, query));
  164. } else if (f.getFileName().contains(query)) {
  165. result.add(f);
  166. }
  167. }
  168. return result;
  169. }
  170. }