-
Notifications
You must be signed in to change notification settings - Fork 3.1k
#296: Add documents provider #1368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- | ||
| ownCloud Android client application | ||
|
|
||
| Copyright (C) 2015 ownCloud Inc. | ||
| Copyright (C) 2015 Bartosz Przybylski | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License version 2, | ||
| as published by the Free Software Foundation. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| --> | ||
| <resources> | ||
| <bool name="atLeastKitKat">true</bool> | ||
| </resources> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| /** | ||
| * ownCloud Android client application | ||
| * | ||
| * @author Bartosz Przybylski | ||
| * Copyright (C) 2015 Bartosz Przybylski | ||
| * Copyright (C) 2015 ownCloud Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
|
|
||
| package com.owncloud.android.providers; | ||
|
|
||
| import android.accounts.Account; | ||
| import android.annotation.TargetApi; | ||
| import android.content.ContentResolver; | ||
| import android.content.Intent; | ||
| import android.content.res.AssetFileDescriptor; | ||
| import android.database.Cursor; | ||
| import android.graphics.Point; | ||
| import android.os.Build; | ||
| import android.os.CancellationSignal; | ||
| import android.os.ParcelFileDescriptor; | ||
| import android.provider.DocumentsProvider; | ||
|
|
||
| import com.owncloud.android.authentication.AccountUtils; | ||
| import com.owncloud.android.datamodel.FileDataStorageManager; | ||
| import com.owncloud.android.datamodel.OCFile; | ||
| import com.owncloud.android.files.services.FileDownloader; | ||
| import com.owncloud.android.providers.cursors.FileCursor; | ||
| import com.owncloud.android.providers.cursors.RootCursor; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Vector; | ||
|
|
||
| @TargetApi(Build.VERSION_CODES.KITKAT) | ||
| public class DocumentsStorageProvider extends DocumentsProvider { | ||
|
|
||
| private FileDataStorageManager mCurrentStorageManager = null; | ||
| private static Map<Long, FileDataStorageManager> mRootIdToStorageManager; | ||
|
|
||
| @Override | ||
| public Cursor queryRoots(String[] projection) throws FileNotFoundException { | ||
| initiateStorageMap(); | ||
|
|
||
| final RootCursor result = new RootCursor(projection); | ||
|
|
||
| for (Account account : AccountUtils.getAccounts(getContext())) | ||
| result.addRoot(account, getContext()); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public Cursor queryDocument(String documentId, String[] projection) throws FileNotFoundException { | ||
| final long docId = Long.parseLong(documentId); | ||
| updateCurrentStorageManagerIfNeeded(docId); | ||
|
|
||
| final FileCursor result = new FileCursor(projection); | ||
| if (result != null) | ||
| result.addFile(mCurrentStorageManager.getFileById(docId)); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) | ||
| throws FileNotFoundException { | ||
|
|
||
| final long folderId = Long.parseLong(parentDocumentId); | ||
| updateCurrentStorageManagerIfNeeded(folderId); | ||
|
|
||
| final FileCursor result = new FileCursor(projection); | ||
|
|
||
| final OCFile browsedDir = mCurrentStorageManager.getFileById(folderId); | ||
| for (OCFile file : mCurrentStorageManager.getFolderContent(browsedDir)) | ||
| result.addFile(file); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public ParcelFileDescriptor openDocument(String documentId, String mode, CancellationSignal cancellationSignal) | ||
| throws FileNotFoundException { | ||
| final long docId = Long.parseLong(documentId); | ||
| updateCurrentStorageManagerIfNeeded(docId); | ||
|
|
||
| OCFile file = mCurrentStorageManager.getFileById(docId); | ||
|
|
||
| if (!file.isDown()) { | ||
|
|
||
| Intent i = new Intent(getContext(), FileDownloader.class); | ||
| i.putExtra(FileDownloader.EXTRA_ACCOUNT, mCurrentStorageManager.getAccount()); | ||
| i.putExtra(FileDownloader.EXTRA_FILE, file); | ||
| getContext().startService(i); | ||
|
|
||
| do { | ||
| if (!waitOrGetCancelled(cancellationSignal)) | ||
| return null; | ||
| file = mCurrentStorageManager.getFileById(docId); | ||
|
|
||
| } while (!file.isDown()); | ||
| } | ||
|
|
||
| return ParcelFileDescriptor.open( | ||
| new File(file.getStoragePath()), ParcelFileDescriptor.MODE_READ_ONLY); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onCreate() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException { | ||
| long docId = Long.parseLong(documentId); | ||
| updateCurrentStorageManagerIfNeeded(docId); | ||
|
|
||
| OCFile file = mCurrentStorageManager.getFileById(docId); | ||
|
|
||
| File realFile = new File(file.getStoragePath()); | ||
|
|
||
| return new AssetFileDescriptor( | ||
| ParcelFileDescriptor.open(realFile, ParcelFileDescriptor.MODE_READ_ONLY), | ||
| 0, | ||
| AssetFileDescriptor.UNKNOWN_LENGTH); | ||
| } | ||
|
|
||
| @Override | ||
| public Cursor querySearchDocuments(String rootId, String query, String[] projection) throws FileNotFoundException { | ||
| updateCurrentStorageManagerIfNeeded(rootId); | ||
|
|
||
| OCFile root = mCurrentStorageManager.getFileByPath("/"); | ||
| FileCursor result = new FileCursor(projection); | ||
|
|
||
| for (OCFile f : findFiles(root, query)) | ||
| result.addFile(f); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private void updateCurrentStorageManagerIfNeeded(long docId) { | ||
| if (mCurrentStorageManager == null || | ||
| (mRootIdToStorageManager.containsKey(docId) && | ||
| mCurrentStorageManager != mRootIdToStorageManager.get(docId))) { | ||
| mCurrentStorageManager = mRootIdToStorageManager.get(docId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It tries to get a map element in an unsafe mode, because the map could be not instantiate and results in a NullPointerException.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The map is always instantiated because of how DocumentsProvider is calling methods in this class. It is guaranteed that |
||
| } | ||
| } | ||
|
|
||
| private void updateCurrentStorageManagerIfNeeded(String rootId) { | ||
| for (FileDataStorageManager data : mRootIdToStorageManager.values()) | ||
| if (data.getAccount().name.equals(rootId)) | ||
| mCurrentStorageManager = data; | ||
| } | ||
|
|
||
| private void initiateStorageMap() { | ||
|
|
||
| mRootIdToStorageManager = new HashMap<Long, FileDataStorageManager>(); | ||
|
|
||
| ContentResolver contentResolver = getContext().getContentResolver(); | ||
|
|
||
| for (Account account : AccountUtils.getAccounts(getContext())) { | ||
| final FileDataStorageManager storageManager = | ||
| new FileDataStorageManager(account, contentResolver); | ||
| final OCFile rootDir = storageManager.getFileByPath("/"); | ||
| mRootIdToStorageManager.put(rootDir.getFileId(), storageManager); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private boolean waitOrGetCancelled(CancellationSignal cancellationSignal) { | ||
| try { | ||
| Thread.sleep(1000); | ||
| } catch (InterruptedException e) { | ||
| return false; | ||
| } | ||
|
|
||
| if (cancellationSignal != null && cancellationSignal.isCanceled()) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| Vector<OCFile> findFiles(OCFile root, String query) { | ||
| Vector<OCFile> result = new Vector<OCFile>(); | ||
| for (OCFile f : mCurrentStorageManager.getFolderContent(root)) { | ||
| if (f.isFolder()) { | ||
| result.addAll(findFiles(f, query)); | ||
| } else { | ||
| if (f.getFileName().contains(query)) | ||
| result.add(f); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /** | ||
| * ownCloud Android client application | ||
| * | ||
| * @author Bartosz Przybylski | ||
| * Copyright (C) 2015 Bartosz Przybylski | ||
| * Copyright (C) 2015 ownCloud Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
|
|
||
| package com.owncloud.android.providers.cursors; | ||
|
|
||
| import android.annotation.TargetApi; | ||
| import android.database.MatrixCursor; | ||
| import android.os.Build; | ||
| import android.provider.DocumentsContract.Document; | ||
|
|
||
| import com.owncloud.android.datamodel.OCFile; | ||
| import com.owncloud.android.utils.MimetypeIconUtil; | ||
|
|
||
| @TargetApi(Build.VERSION_CODES.KITKAT) | ||
| public class FileCursor extends MatrixCursor { | ||
|
|
||
| private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] { | ||
| Document.COLUMN_DOCUMENT_ID, Document.COLUMN_DISPLAY_NAME, | ||
| Document.COLUMN_MIME_TYPE, Document.COLUMN_SIZE, | ||
| Document.COLUMN_FLAGS, Document.COLUMN_LAST_MODIFIED | ||
| }; | ||
|
|
||
| public FileCursor(String[] projection) { | ||
| super(projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION); | ||
| } | ||
|
|
||
| public void addFile(OCFile file) { | ||
| if (file == null) return; | ||
|
|
||
| final int iconRes = MimetypeIconUtil.getFileTypeIconId(file.getMimetype(), file.getFileName()); | ||
| final String mimeType = file.isFolder() ? Document.MIME_TYPE_DIR : file.getMimetype(); | ||
| final String imagePath = file.isImage() && file.isDown() ? file.getStoragePath() : null; | ||
| int flags = imagePath != null ? Document.FLAG_SUPPORTS_THUMBNAIL : 0; | ||
|
|
||
| newRow().add(Document.COLUMN_DOCUMENT_ID, Long.toString(file.getFileId())) | ||
| .add(Document.COLUMN_DISPLAY_NAME, file.getFileName()) | ||
| .add(Document.COLUMN_LAST_MODIFIED, file.getModificationTimestamp()) | ||
| .add(Document.COLUMN_SIZE, file.getFileLength()) | ||
| .add(Document.COLUMN_FLAGS, flags) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting the "FLAG_SUPPORTS_THUMBNAIL" flag does not imply implement both "buildSearchDocumentsUri" and I'm not sure about it but maybe both are expected (not only one) when that flag is set. Please correct me if I'm wrong ;)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, you need to tell the caller that it is safe to call those methods. |
||
| .add(Document.COLUMN_ICON, iconRes) | ||
| .add(Document.COLUMN_MIME_TYPE, mimeType); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to make this existing Provider public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, documents provider is queried by external application, and at that point its outside owncloud permission so exported content provider is required.