use of com.owncloud.android.lib.resources.files.SearchRemoteOperation in project android by nextcloud.
the class FileDataStorageManagerIT method testPhotoSearch.
/**
* This test creates an image, does a photo search (now returned image is not yet in file hierarchy), then root
* folder is refreshed and it is verified that the same image file is used in database
*/
@Test
public void testPhotoSearch() throws IOException {
String remotePath = "/imageFile.png";
VirtualFolderType virtualType = VirtualFolderType.GALLERY;
assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
assertEquals(1, sut.getAllFiles().size());
File imageFile = getFile("imageFile.png");
assertTrue(new UploadFileRemoteOperation(imageFile.getAbsolutePath(), remotePath, "image/png", String.valueOf(System.currentTimeMillis() / 1000)).execute(client).isSuccess());
assertNull(sut.getFileByDecryptedRemotePath(remotePath));
// search
SearchRemoteOperation searchRemoteOperation = new SearchRemoteOperation("image/%", PHOTO_SEARCH, false, capability);
RemoteOperationResult<List<RemoteFile>> searchResult = searchRemoteOperation.execute(client);
TestCase.assertTrue(searchResult.isSuccess());
TestCase.assertEquals(1, searchResult.getResultData().size());
OCFile ocFile = FileStorageUtils.fillOCFile(searchResult.getResultData().get(0));
sut.saveFile(ocFile);
List<ContentValues> contentValues = new ArrayList<>();
ContentValues cv = new ContentValues();
cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, virtualType.toString());
cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());
contentValues.add(cv);
sut.saveVirtuals(contentValues);
assertEquals(remotePath, ocFile.getRemotePath());
assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
assertEquals(1, sut.getVirtualFolderContent(virtualType, false).size());
assertEquals(2, sut.getAllFiles().size());
// update root
assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/"), System.currentTimeMillis() / 1000, false, false, sut, user, targetContext).execute(client).isSuccess());
assertEquals(1, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
assertEquals(1, sut.getVirtualFolderContent(virtualType, false).size());
assertEquals(2, sut.getAllFiles().size());
assertEquals(sut.getVirtualFolderContent(virtualType, false).get(0), sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).get(0));
}
use of com.owncloud.android.lib.resources.files.SearchRemoteOperation in project android by nextcloud.
the class FileDataStorageManagerIT method testGallerySearch.
/**
* This test creates an image and a video, does a gallery search (now returned image and video is not yet in file
* hierarchy), then root folder is refreshed and it is verified that the same image file is used in database
*/
@Test
public void testGallerySearch() throws IOException {
sut = new FileDataStorageManager(user, targetContext.getContentResolver().acquireContentProviderClient(ProviderMeta.ProviderTableMeta.CONTENT_URI));
String imagePath = "/imageFile.png";
VirtualFolderType virtualType = VirtualFolderType.GALLERY;
assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
assertEquals(1, sut.getAllFiles().size());
File imageFile = getFile("imageFile.png");
assertTrue(new UploadFileRemoteOperation(imageFile.getAbsolutePath(), imagePath, "image/png", String.valueOf((System.currentTimeMillis() - 10000) / 1000)).execute(client).isSuccess());
// Check that file does not yet exist in local database
assertNull(sut.getFileByDecryptedRemotePath(imagePath));
String videoPath = "/videoFile.mp4";
File videoFile = getFile("videoFile.mp4");
assertTrue(new UploadFileRemoteOperation(videoFile.getAbsolutePath(), videoPath, "video/mpeg", String.valueOf((System.currentTimeMillis() + 10000) / 1000)).execute(client).isSuccess());
// Check that file does not yet exist in local database
assertNull(sut.getFileByDecryptedRemotePath(videoPath));
// search
SearchRemoteOperation searchRemoteOperation = new SearchRemoteOperation("", GALLERY_SEARCH, false, capability);
RemoteOperationResult<List<RemoteFile>> searchResult = searchRemoteOperation.execute(client);
TestCase.assertTrue(searchResult.isSuccess());
TestCase.assertEquals(2, searchResult.getResultData().size());
// newest file must be video path (as sorted by recently modified)
OCFile ocFile = FileStorageUtils.fillOCFile(searchResult.getResultData().get(0));
sut.saveFile(ocFile);
assertEquals(videoPath, ocFile.getRemotePath());
List<ContentValues> contentValues = new ArrayList<>();
ContentValues cv = new ContentValues();
cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, virtualType.toString());
cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());
contentValues.add(cv);
// second is image file, as older
OCFile ocFile2 = FileStorageUtils.fillOCFile(searchResult.getResultData().get(1));
sut.saveFile(ocFile2);
assertEquals(imagePath, ocFile2.getRemotePath());
ContentValues cv2 = new ContentValues();
cv2.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, virtualType.toString());
cv2.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile2.getFileId());
contentValues.add(cv2);
sut.saveVirtuals(contentValues);
assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
assertEquals(2, sut.getVirtualFolderContent(virtualType, false).size());
assertEquals(3, sut.getAllFiles().size());
// update root
assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/"), System.currentTimeMillis() / 1000, false, false, sut, user, targetContext).execute(client).isSuccess());
assertEquals(2, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
assertEquals(2, sut.getVirtualFolderContent(virtualType, false).size());
assertEquals(3, sut.getAllFiles().size());
assertEquals(sut.getVirtualFolderContent(virtualType, false).get(0), sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).get(0));
}
use of com.owncloud.android.lib.resources.files.SearchRemoteOperation in project android by nextcloud.
the class GallerySearchTask method doInBackground.
@Override
protected GallerySearchTask.Result doInBackground(Void... voids) {
if (photoFragmentWeakReference.get() == null) {
return new Result(false, false, -1);
}
GalleryFragment photoFragment = photoFragmentWeakReference.get();
if (isCancelled()) {
return new Result(false, false, -1);
} else {
OCCapability ocCapability = storageManager.getCapability(user.getAccountName());
SearchRemoteOperation searchRemoteOperation = new SearchRemoteOperation("", SearchRemoteOperation.SearchType.GALLERY_SEARCH, false, ocCapability);
searchRemoteOperation.setLimit(limit);
searchRemoteOperation.setStartDate(startDate);
searchRemoteOperation.setEndDate(endDate);
if (photoFragment.getContext() != null) {
Log_OC.d(this, "Start gallery search with " + new Date(startDate * 1000L) + " - " + new Date(endDate * 1000L) + " with limit: " + limit);
RemoteOperationResult result = searchRemoteOperation.execute(user.toPlatformAccount(), photoFragment.getContext());
boolean emptySearch = parseMedia(startDate, endDate, result.getData());
long lastTimeStamp = findLastTimestamp(result.getData());
photoFragment.getAdapter().showAllGalleryItems(storageManager);
return new Result(result.isSuccess(), emptySearch, lastTimeStamp);
} else {
return new Result(false, false, -1);
}
}
}
use of com.owncloud.android.lib.resources.files.SearchRemoteOperation in project android by nextcloud.
the class FetchRemoteFileTask method doInBackground.
@Override
protected String doInBackground(Void... voids) {
SearchRemoteOperation searchRemoteOperation = new SearchRemoteOperation(fileId, FILE_ID_SEARCH, false, fileDisplayActivity.getCapabilities());
RemoteOperationResult remoteOperationResult = searchRemoteOperation.execute(user.toPlatformAccount(), fileDisplayActivity);
if (remoteOperationResult.isSuccess() && remoteOperationResult.getData() != null) {
if (remoteOperationResult.getData().isEmpty()) {
return fileDisplayActivity.getString(R.string.remote_file_fetch_failed);
}
String remotePath = ((RemoteFile) remoteOperationResult.getData().get(0)).getRemotePath();
ReadFileRemoteOperation operation = new ReadFileRemoteOperation(remotePath);
RemoteOperationResult result = operation.execute(user.toPlatformAccount(), fileDisplayActivity);
if (!result.isSuccess()) {
Exception exception = result.getException();
String message = "Fetching file " + remotePath + " fails with: " + result.getLogMessage();
if (exception != null) {
return exception.getMessage();
} else {
return message;
}
}
RemoteFile remoteFile = (RemoteFile) result.getData().get(0);
OCFile ocFile = FileStorageUtils.fillOCFile(remoteFile);
FileStorageUtils.searchForLocalFileInDefaultPath(ocFile, user.getAccountName());
ocFile = storageManager.saveFileWithParent(ocFile, fileDisplayActivity);
// also sync folder content
OCFile toSync;
if (ocFile.isFolder()) {
toSync = ocFile;
} else {
toSync = storageManager.getFileById(ocFile.getParentId());
}
long currentSyncTime = System.currentTimeMillis();
RemoteOperation refreshFolderOperation = new RefreshFolderOperation(toSync, currentSyncTime, true, true, storageManager, user, fileDisplayActivity);
refreshFolderOperation.execute(user.toPlatformAccount(), fileDisplayActivity);
fileDisplayActivity.setFile(ocFile);
} else {
return remoteOperationResult.getLogMessage();
}
return "";
}
use of com.owncloud.android.lib.resources.files.SearchRemoteOperation in project android by nextcloud.
the class OCFileListFragment method handleSearchEvent.
private void handleSearchEvent(SearchEvent event) {
if (SearchRemoteOperation.SearchType.PHOTO_SEARCH == event.getSearchType()) {
return;
}
prepareCurrentSearch(event);
searchFragment = true;
setEmptyListLoadingMessage();
mAdapter.setData(new ArrayList<>(), SearchType.NO_SEARCH, mContainerActivity.getStorageManager(), mFile, true);
setFabVisible(false);
Runnable switchViewsRunnable = () -> {
if (isGridViewPreferred(mFile) && !isGridEnabled()) {
switchToGridView();
} else if (!isGridViewPreferred(mFile) && isGridEnabled()) {
switchToListView();
}
};
new Handler(Looper.getMainLooper()).post(switchViewsRunnable);
final User currentUser = accountManager.getUser();
final RemoteOperation remoteOperation;
if (currentSearchType != SearchType.SHARED_FILTER) {
boolean searchOnlyFolders = false;
if (getArguments() != null && getArguments().getBoolean(ARG_SEARCH_ONLY_FOLDER, false)) {
searchOnlyFolders = true;
}
OCCapability ocCapability = mContainerActivity.getStorageManager().getCapability(currentUser.getAccountName());
remoteOperation = new SearchRemoteOperation(event.getSearchQuery(), event.getSearchType(), searchOnlyFolders, ocCapability);
} else {
remoteOperation = new GetSharesRemoteOperation();
}
remoteOperationAsyncTask = new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... voids) {
setTitle();
if (getContext() != null && !isCancelled()) {
RemoteOperationResult remoteOperationResult = remoteOperation.execute(currentUser.toPlatformAccount(), getContext());
FileDataStorageManager storageManager = null;
if (mContainerActivity != null && mContainerActivity.getStorageManager() != null) {
storageManager = mContainerActivity.getStorageManager();
}
if (remoteOperationResult.isSuccess() && remoteOperationResult.getResultData() != null && !isCancelled() && searchFragment) {
searchEvent = event;
if (remoteOperationResult.getResultData() == null || ((List) remoteOperationResult.getResultData()).isEmpty()) {
setEmptyView(event);
} else {
mAdapter.setData(((RemoteOperationResult<List>) remoteOperationResult).getResultData(), currentSearchType, storageManager, mFile, true);
}
final ToolbarActivity fileDisplayActivity = (ToolbarActivity) getActivity();
if (fileDisplayActivity != null) {
fileDisplayActivity.runOnUiThread(() -> {
if (fileDisplayActivity != null) {
setLoading(false);
}
});
}
}
return remoteOperationResult.isSuccess();
} else {
return Boolean.FALSE;
}
}
@Override
protected void onPostExecute(Boolean bool) {
if (!isCancelled()) {
mAdapter.notifyDataSetChanged();
}
}
};
remoteOperationAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Aggregations