Search in sources :

Example 1 with SearchRemoteOperation

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));
}
Also used : ContentValues(android.content.ContentValues) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) File(java.io.File) Test(org.junit.Test)

Example 2 with SearchRemoteOperation

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));
}
Also used : ContentValues(android.content.ContentValues) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) ArrayList(java.util.ArrayList) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) ArrayList(java.util.ArrayList) List(java.util.List) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) File(java.io.File) Test(org.junit.Test)

Example 3 with SearchRemoteOperation

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);
        }
    }
}
Also used : GalleryFragment(com.owncloud.android.ui.fragment.GalleryFragment) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) OCCapability(com.owncloud.android.lib.resources.status.OCCapability) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) Date(java.util.Date) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult)

Example 4 with SearchRemoteOperation

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 "";
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) ReadFileRemoteOperation(com.owncloud.android.lib.resources.files.ReadFileRemoteOperation) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile)

Example 5 with SearchRemoteOperation

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);
}
Also used : ToolbarActivity(com.owncloud.android.ui.activity.ToolbarActivity) ToggleEncryptionRemoteOperation(com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation) RichWorkspaceDirectEditingRemoteOperation(com.nextcloud.android.lib.richWorkspace.RichWorkspaceDirectEditingRemoteOperation) GetSharesRemoteOperation(com.owncloud.android.lib.resources.shares.GetSharesRemoteOperation) ToggleFavoriteRemoteOperation(com.owncloud.android.lib.resources.files.ToggleFavoriteRemoteOperation) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) User(com.nextcloud.client.account.User) OCCapability(com.owncloud.android.lib.resources.status.OCCapability) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) Handler(android.os.Handler) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) GetSharesRemoteOperation(com.owncloud.android.lib.resources.shares.GetSharesRemoteOperation) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

SearchRemoteOperation (com.owncloud.android.lib.resources.files.SearchRemoteOperation)5 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)3 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)3 RefreshFolderOperation (com.owncloud.android.operations.RefreshFolderOperation)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ContentValues (android.content.ContentValues)2 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)2 UploadFileRemoteOperation (com.owncloud.android.lib.resources.files.UploadFileRemoteOperation)2 OCCapability (com.owncloud.android.lib.resources.status.OCCapability)2 File (java.io.File)2 Test (org.junit.Test)2 Handler (android.os.Handler)1 RichWorkspaceDirectEditingRemoteOperation (com.nextcloud.android.lib.richWorkspace.RichWorkspaceDirectEditingRemoteOperation)1 User (com.nextcloud.client.account.User)1 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)1 OCFile (com.owncloud.android.datamodel.OCFile)1 ToggleEncryptionRemoteOperation (com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation)1 ReadFileRemoteOperation (com.owncloud.android.lib.resources.files.ReadFileRemoteOperation)1 ToggleFavoriteRemoteOperation (com.owncloud.android.lib.resources.files.ToggleFavoriteRemoteOperation)1