|
@@ -25,110 +25,177 @@ import android.os.AsyncTask;
|
|
|
|
|
|
import com.nextcloud.client.account.User;
|
|
|
import com.owncloud.android.datamodel.FileDataStorageManager;
|
|
|
+import com.owncloud.android.datamodel.OCFile;
|
|
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
|
|
import com.owncloud.android.lib.common.utils.Log_OC;
|
|
|
import com.owncloud.android.lib.resources.files.SearchRemoteOperation;
|
|
|
-import com.owncloud.android.ui.adapter.OCFileListAdapter;
|
|
|
+import com.owncloud.android.lib.resources.files.model.RemoteFile;
|
|
|
+import com.owncloud.android.lib.resources.status.OCCapability;
|
|
|
+import com.owncloud.android.operations.RefreshFolderOperation;
|
|
|
import com.owncloud.android.ui.fragment.ExtendedListFragment;
|
|
|
import com.owncloud.android.ui.fragment.GalleryFragment;
|
|
|
+import com.owncloud.android.utils.FileStorageUtils;
|
|
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
-
|
|
|
-public class GallerySearchTask extends AsyncTask<Void, Void, RemoteOperationResult> {
|
|
|
-
|
|
|
- private int columnCount;
|
|
|
- private User user;
|
|
|
- private WeakReference<GalleryFragment> photoFragmentWeakReference;
|
|
|
- private SearchRemoteOperation searchRemoteOperation;
|
|
|
- private FileDataStorageManager storageManager;
|
|
|
- private int limit;
|
|
|
-
|
|
|
- public GallerySearchTask(int columnsCount,
|
|
|
- GalleryFragment photoFragment,
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class GallerySearchTask extends AsyncTask<Void, Void, GallerySearchTask.Result> {
|
|
|
+
|
|
|
+ private final User user;
|
|
|
+ private final WeakReference<GalleryFragment> photoFragmentWeakReference;
|
|
|
+ private final FileDataStorageManager storageManager;
|
|
|
+ private final int limit;
|
|
|
+ private final long startDate;
|
|
|
+ private final long endDate;
|
|
|
+
|
|
|
+ public GallerySearchTask(GalleryFragment photoFragment,
|
|
|
User user,
|
|
|
- SearchRemoteOperation searchRemoteOperation,
|
|
|
- FileDataStorageManager storageManager) {
|
|
|
- this.columnCount = columnsCount;
|
|
|
+ FileDataStorageManager storageManager,
|
|
|
+ long startDate,
|
|
|
+ long endDate,
|
|
|
+ int limit) {
|
|
|
this.user = user;
|
|
|
this.photoFragmentWeakReference = new WeakReference<>(photoFragment);
|
|
|
- this.searchRemoteOperation = searchRemoteOperation;
|
|
|
this.storageManager = storageManager;
|
|
|
+ this.startDate = startDate;
|
|
|
+ this.endDate = endDate;
|
|
|
+ this.limit = limit;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected void onPreExecute() {
|
|
|
- super.onPreExecute();
|
|
|
-
|
|
|
- if (photoFragmentWeakReference.get() == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- GalleryFragment photoFragment = photoFragmentWeakReference.get();
|
|
|
- photoFragment.setPhotoSearchQueryRunning(true);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected RemoteOperationResult doInBackground(Void... voids) {
|
|
|
+ protected GallerySearchTask.Result doInBackground(Void... voids) {
|
|
|
if (photoFragmentWeakReference.get() == null) {
|
|
|
- return new RemoteOperationResult(new Exception("Photo fragment is null"));
|
|
|
+ return new Result(false, false, -1);
|
|
|
}
|
|
|
GalleryFragment photoFragment = photoFragmentWeakReference.get();
|
|
|
- OCFileListAdapter adapter = photoFragment.getAdapter();
|
|
|
|
|
|
if (isCancelled()) {
|
|
|
- return new RemoteOperationResult(new Exception("Cancelled"));
|
|
|
+ return new Result(false, false, -1);
|
|
|
} else {
|
|
|
- limit = 15 * columnCount;
|
|
|
+ OCCapability ocCapability = storageManager.getCapability(user.getAccountName());
|
|
|
+
|
|
|
+ SearchRemoteOperation searchRemoteOperation = new SearchRemoteOperation("",
|
|
|
+ SearchRemoteOperation.SearchType.GALLERY_SEARCH,
|
|
|
+ false,
|
|
|
+ ocCapability);
|
|
|
|
|
|
- long timestamp = -1;
|
|
|
- if (adapter.getLastTimestamp() > 0) {
|
|
|
- timestamp = adapter.getLastTimestamp();
|
|
|
- }
|
|
|
|
|
|
searchRemoteOperation.setLimit(limit);
|
|
|
- searchRemoteOperation.setTimestamp(timestamp);
|
|
|
+ searchRemoteOperation.setStartDate(startDate);
|
|
|
+ searchRemoteOperation.setEndDate(endDate);
|
|
|
|
|
|
if (photoFragment.getContext() != null) {
|
|
|
- return searchRemoteOperation.execute(user.toPlatformAccount(), photoFragment.getContext());
|
|
|
+ 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 RemoteOperationResult(new IllegalStateException("No context available"));
|
|
|
+ return new Result(false, false, -1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected void onPostExecute(RemoteOperationResult result) {
|
|
|
+ protected void onPostExecute(GallerySearchTask.Result result) {
|
|
|
if (photoFragmentWeakReference.get() != null) {
|
|
|
GalleryFragment photoFragment = photoFragmentWeakReference.get();
|
|
|
|
|
|
- if (result.isSuccess() && result.getData() != null && !isCancelled()) {
|
|
|
- if (result.getData() == null || result.getData().size() == 0) {
|
|
|
- photoFragment.setSearchDidNotFindNewPhotos(true);
|
|
|
- photoFragment.setEmptyListMessage(ExtendedListFragment.SearchType.GALLERY_SEARCH);
|
|
|
- } else {
|
|
|
- OCFileListAdapter adapter = photoFragment.getAdapter();
|
|
|
-
|
|
|
- if (result.getData().size() < limit) {
|
|
|
- // stop loading spinner
|
|
|
- photoFragment.setSearchDidNotFindNewPhotos(true);
|
|
|
- }
|
|
|
-
|
|
|
- adapter.setData(result.getData(),
|
|
|
- ExtendedListFragment.SearchType.GALLERY_SEARCH,
|
|
|
- storageManager,
|
|
|
- null,
|
|
|
- false);
|
|
|
- adapter.notifyDataSetChanged();
|
|
|
- Log_OC.d(this, "Search: count: " + result.getData().size() + " total: " + adapter.getFiles().size());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
photoFragment.setLoading(false);
|
|
|
+ photoFragment.searchCompleted(result.emptySearch, result.lastTimestamp);
|
|
|
|
|
|
- if (!result.isSuccess() && !isCancelled()) {
|
|
|
+ if (!result.success) {
|
|
|
photoFragment.setEmptyListMessage(ExtendedListFragment.SearchType.GALLERY_SEARCH);
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private long findLastTimestamp(ArrayList<RemoteFile> remoteFiles) {
|
|
|
+ int lastPosition = remoteFiles.size() - 1;
|
|
|
+
|
|
|
+ if (lastPosition < 0) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ RemoteFile lastFile = remoteFiles.get(lastPosition);
|
|
|
+ return lastFile.getModifiedTimestamp() / 1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean parseMedia(long startDate, long endDate, List<Object> remoteFiles) {
|
|
|
+ // retrieve all between startDate and endDate
|
|
|
+ Map<String, OCFile> localFilesMap = RefreshFolderOperation.prefillLocalFilesMap(null,
|
|
|
+ storageManager.getGalleryItems(startDate * 1000L,
|
|
|
+ endDate * 1000L));
|
|
|
+ List<OCFile> filesToAdd = new ArrayList<>();
|
|
|
+ List<OCFile> filesToUpdate = new ArrayList<>();
|
|
|
+
|
|
|
+ OCFile localFile;
|
|
|
+ for (Object file : remoteFiles) {
|
|
|
+ OCFile ocFile = FileStorageUtils.fillOCFile((RemoteFile) file);
|
|
|
+
|
|
|
+ localFile = localFilesMap.remove(ocFile.getRemotePath());
|
|
|
+
|
|
|
+ if (localFile == null) {
|
|
|
+ // add new file
|
|
|
+ filesToAdd.add(ocFile);
|
|
|
+ } else if (!localFile.getEtag().equals(ocFile.getEtag())) {
|
|
|
+ // update file
|
|
|
+ ocFile.setLastSyncDateForData(System.currentTimeMillis());
|
|
|
+ filesToUpdate.add(ocFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // add new files
|
|
|
+ for (OCFile file : filesToAdd) {
|
|
|
+ storageManager.saveFile(file);
|
|
|
+ }
|
|
|
+
|
|
|
+ // update existing files
|
|
|
+ for (OCFile file : filesToUpdate) {
|
|
|
+ storageManager.saveFile(file);
|
|
|
+ }
|
|
|
+
|
|
|
+ // existing files to remove
|
|
|
+ for (OCFile file : localFilesMap.values()) {
|
|
|
+ storageManager.removeFile(file, true, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ Log_OC.d(this, "Gallery search result:" +
|
|
|
+ " new: " + filesToAdd.size() +
|
|
|
+ " updated: " + filesToUpdate.size() +
|
|
|
+ " deleted: " + localFilesMap.size());
|
|
|
+
|
|
|
+ return didNotFindNewResults(filesToAdd, filesToUpdate, localFilesMap.values());
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean didNotFindNewResults(List<OCFile> filesToAdd,
|
|
|
+ List<OCFile> filesToUpdate,
|
|
|
+ Collection<OCFile> filesToRemove) {
|
|
|
+ return filesToAdd.isEmpty() && filesToUpdate.isEmpty() && filesToRemove.isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class Result {
|
|
|
+ public boolean success;
|
|
|
+ public boolean emptySearch;
|
|
|
+ public long lastTimestamp;
|
|
|
|
|
|
- photoFragment.setPhotoSearchQueryRunning(false);
|
|
|
+ public Result(boolean success, boolean emptySearch, long lastTimestamp) {
|
|
|
+ this.success = success;
|
|
|
+ this.emptySearch = emptySearch;
|
|
|
+ this.lastTimestamp = lastTimestamp;
|
|
|
}
|
|
|
}
|
|
|
}
|