DocumentsStorageProvider.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.Context;
  25. import android.content.Intent;
  26. import android.content.res.AssetFileDescriptor;
  27. import android.database.Cursor;
  28. import android.graphics.Point;
  29. import android.os.Build;
  30. import android.os.CancellationSignal;
  31. import android.os.ParcelFileDescriptor;
  32. import android.provider.DocumentsProvider;
  33. import com.owncloud.android.authentication.AccountUtils;
  34. import com.owncloud.android.datamodel.FileDataStorageManager;
  35. import com.owncloud.android.datamodel.OCFile;
  36. import com.owncloud.android.files.services.FileDownloader;
  37. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  38. import com.owncloud.android.operations.SynchronizeFileOperation;
  39. import com.owncloud.android.ui.activity.ConflictsResolveActivity;
  40. import com.owncloud.android.utils.FileStorageUtils;
  41. import org.nextcloud.providers.cursors.FileCursor;
  42. import org.nextcloud.providers.cursors.RootCursor;
  43. import java.io.File;
  44. import java.io.FileNotFoundException;
  45. import java.util.HashMap;
  46. import java.util.Map;
  47. import java.util.Vector;
  48. @TargetApi(Build.VERSION_CODES.KITKAT)
  49. public class DocumentsStorageProvider extends DocumentsProvider {
  50. private FileDataStorageManager mCurrentStorageManager = null;
  51. private static Map<Long, FileDataStorageManager> mRootIdToStorageManager;
  52. @Override
  53. public Cursor queryRoots(String[] projection) throws FileNotFoundException {
  54. initiateStorageMap();
  55. final RootCursor result = new RootCursor(projection);
  56. for (Account account : AccountUtils.getAccounts(getContext())) {
  57. result.addRoot(account, getContext());
  58. }
  59. return result;
  60. }
  61. @Override
  62. public Cursor queryDocument(String documentId, String[] projection) throws FileNotFoundException {
  63. final long docId = Long.parseLong(documentId);
  64. updateCurrentStorageManagerIfNeeded(docId);
  65. final FileCursor result = new FileCursor(projection);
  66. OCFile file = mCurrentStorageManager.getFileById(docId);
  67. if (file != null) {
  68. result.addFile(file);
  69. }
  70. return result;
  71. }
  72. @Override
  73. public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder)
  74. throws FileNotFoundException {
  75. final long folderId = Long.parseLong(parentDocumentId);
  76. updateCurrentStorageManagerIfNeeded(folderId);
  77. final FileCursor result = new FileCursor(projection);
  78. final OCFile browsedDir = mCurrentStorageManager.getFileById(folderId);
  79. for (OCFile file : mCurrentStorageManager.getFolderContent(browsedDir, false)) {
  80. result.addFile(file);
  81. }
  82. return result;
  83. }
  84. @Override
  85. public ParcelFileDescriptor openDocument(String documentId, String mode, CancellationSignal cancellationSignal)
  86. throws FileNotFoundException {
  87. final long docId = Long.parseLong(documentId);
  88. updateCurrentStorageManagerIfNeeded(docId);
  89. OCFile file = mCurrentStorageManager.getFileById(docId);
  90. Account account = mCurrentStorageManager.getAccount();
  91. Context context = getContext();
  92. if (!file.isDown()) {
  93. Intent i = new Intent(getContext(), FileDownloader.class);
  94. i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
  95. i.putExtra(FileDownloader.EXTRA_FILE, file);
  96. context.startService(i);
  97. do {
  98. if (!waitOrGetCancelled(cancellationSignal)) {
  99. return null;
  100. }
  101. file = mCurrentStorageManager.getFileById(docId);
  102. } while (!file.isDown());
  103. } else {
  104. FileDataStorageManager storageManager =
  105. new FileDataStorageManager(account, context.getContentResolver());
  106. SynchronizeFileOperation sfo =
  107. new SynchronizeFileOperation(file, null, account, true, context);
  108. RemoteOperationResult result = sfo.execute(storageManager, context);
  109. if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) {
  110. // ISSUE 5: if the user is not running the app (this is a service!),
  111. // this can be very intrusive; a notification should be preferred
  112. Intent i = new Intent(context, ConflictsResolveActivity.class);
  113. i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
  114. i.putExtra(ConflictsResolveActivity.EXTRA_FILE, file);
  115. i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, account);
  116. context.startActivity(i);
  117. } else {
  118. FileStorageUtils.checkIfFileFinishedSaving(file);
  119. }
  120. }
  121. return ParcelFileDescriptor.open(new File(file.getStoragePath()), ParcelFileDescriptor.parseMode(mode));
  122. }
  123. @Override
  124. public boolean onCreate() {
  125. return true;
  126. }
  127. @Override
  128. public AssetFileDescriptor openDocumentThumbnail(String documentId,
  129. Point sizeHint,
  130. CancellationSignal signal)
  131. throws FileNotFoundException {
  132. long docId = Long.parseLong(documentId);
  133. updateCurrentStorageManagerIfNeeded(docId);
  134. OCFile file = mCurrentStorageManager.getFileById(docId);
  135. File realFile = new File(file.getStoragePath());
  136. return new AssetFileDescriptor(
  137. ParcelFileDescriptor.open(realFile, ParcelFileDescriptor.MODE_READ_ONLY),
  138. 0,
  139. AssetFileDescriptor.UNKNOWN_LENGTH);
  140. }
  141. @Override
  142. public Cursor querySearchDocuments(String rootId, String query, String[] projection) throws FileNotFoundException {
  143. updateCurrentStorageManagerIfNeeded(rootId);
  144. OCFile root = mCurrentStorageManager.getFileByPath("/");
  145. FileCursor result = new FileCursor(projection);
  146. for (OCFile f : findFiles(root, query)) {
  147. result.addFile(f);
  148. }
  149. return result;
  150. }
  151. private void updateCurrentStorageManagerIfNeeded(long docId) {
  152. if (mCurrentStorageManager == null ||
  153. (mRootIdToStorageManager.containsKey(docId) &&
  154. mCurrentStorageManager != mRootIdToStorageManager.get(docId))) {
  155. mCurrentStorageManager = mRootIdToStorageManager.get(docId);
  156. }
  157. }
  158. private void updateCurrentStorageManagerIfNeeded(String rootId) {
  159. for (FileDataStorageManager data : mRootIdToStorageManager.values()) {
  160. if (data.getAccount().name.equals(rootId)) {
  161. mCurrentStorageManager = data;
  162. }
  163. }
  164. }
  165. private void initiateStorageMap() {
  166. mRootIdToStorageManager = new HashMap<>();
  167. ContentResolver contentResolver = getContext().getContentResolver();
  168. for (Account account : AccountUtils.getAccounts(getContext())) {
  169. final FileDataStorageManager storageManager =
  170. new FileDataStorageManager(account, contentResolver);
  171. final OCFile rootDir = storageManager.getFileByPath("/");
  172. mRootIdToStorageManager.put(rootDir.getFileId(), storageManager);
  173. }
  174. }
  175. private boolean waitOrGetCancelled(CancellationSignal cancellationSignal) {
  176. try {
  177. Thread.sleep(1000);
  178. } catch (InterruptedException e) {
  179. return false;
  180. }
  181. return !(cancellationSignal != null && cancellationSignal.isCanceled());
  182. }
  183. Vector<OCFile> findFiles(OCFile root, String query) {
  184. Vector<OCFile> result = new Vector<>();
  185. for (OCFile f : mCurrentStorageManager.getFolderContent(root, false)) {
  186. if (f.isFolder()) {
  187. result.addAll(findFiles(f, query));
  188. } else if (f.getFileName().contains(query)) {
  189. result.add(f);
  190. }
  191. }
  192. return result;
  193. }
  194. }