PhotoSearchTask.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2019 Tobias Kaminsky
  6. * Copyright (C) 2019 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.asynctasks;
  22. import android.os.AsyncTask;
  23. import com.nextcloud.client.account.User;
  24. import com.owncloud.android.datamodel.FileDataStorageManager;
  25. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  26. import com.owncloud.android.lib.common.utils.Log_OC;
  27. import com.owncloud.android.lib.resources.files.SearchRemoteOperation;
  28. import com.owncloud.android.ui.activity.ToolbarActivity;
  29. import com.owncloud.android.ui.adapter.OCFileListAdapter;
  30. import com.owncloud.android.ui.fragment.ExtendedListFragment;
  31. import com.owncloud.android.ui.fragment.PhotoFragment;
  32. import java.lang.ref.WeakReference;
  33. public class PhotoSearchTask extends AsyncTask<Void, Void, RemoteOperationResult> {
  34. private int columnCount;
  35. private User user;
  36. private WeakReference<PhotoFragment> photoFragmentWeakReference;
  37. private SearchRemoteOperation searchRemoteOperation;
  38. private FileDataStorageManager storageManager;
  39. public PhotoSearchTask(int columnsCount,
  40. PhotoFragment photoFragment,
  41. User user,
  42. SearchRemoteOperation searchRemoteOperation,
  43. FileDataStorageManager storageManager) {
  44. this.columnCount = columnsCount;
  45. this.user = user;
  46. this.photoFragmentWeakReference = new WeakReference<>(photoFragment);
  47. this.searchRemoteOperation = searchRemoteOperation;
  48. this.storageManager = storageManager;
  49. }
  50. @Override
  51. protected void onPreExecute() {
  52. super.onPreExecute();
  53. if (photoFragmentWeakReference.get() == null) {
  54. return;
  55. }
  56. PhotoFragment photoFragment = photoFragmentWeakReference.get();
  57. photoFragment.setPhotoSearchQueryRunning(true);
  58. }
  59. @Override
  60. protected RemoteOperationResult doInBackground(Void... voids) {
  61. if (photoFragmentWeakReference.get() == null) {
  62. return new RemoteOperationResult(new Exception("Photo fragment is null"));
  63. }
  64. PhotoFragment photoFragment = photoFragmentWeakReference.get();
  65. OCFileListAdapter adapter = photoFragment.getAdapter();
  66. if (isCancelled()) {
  67. return new RemoteOperationResult(new Exception("Cancelled"));
  68. } else {
  69. int limit = 15 * columnCount;
  70. long timestamp = -1;
  71. if (adapter.getLastTimestamp() > 0) {
  72. timestamp = adapter.getLastTimestamp();
  73. }
  74. searchRemoteOperation.setLimit(limit);
  75. searchRemoteOperation.setTimestamp(timestamp);
  76. if (photoFragment.getContext() != null) {
  77. return searchRemoteOperation.execute(user.toPlatformAccount(), photoFragment.getContext());
  78. } else {
  79. return new RemoteOperationResult(new IllegalStateException("No context available"));
  80. }
  81. }
  82. }
  83. @Override
  84. protected void onPostExecute(RemoteOperationResult result) {
  85. if (photoFragmentWeakReference.get() != null) {
  86. PhotoFragment photoFragment = photoFragmentWeakReference.get();
  87. if (result.isSuccess() && result.getData() != null && !isCancelled()) {
  88. if (result.getData() == null || result.getData().size() == 0) {
  89. photoFragment.setSearchDidNotFindNewPhotos(true);
  90. } else {
  91. OCFileListAdapter adapter = photoFragment.getAdapter();
  92. adapter.setData(result.getData(),
  93. ExtendedListFragment.SearchType.PHOTO_SEARCH,
  94. storageManager,
  95. null,
  96. false);
  97. adapter.notifyDataSetChanged();
  98. Log_OC.d(this, "Search: count: " + result.getData().size() + " total: " + adapter.getFiles().size());
  99. }
  100. }
  101. final ToolbarActivity fileDisplayActivity = (ToolbarActivity) photoFragment.getActivity();
  102. if (fileDisplayActivity != null) {
  103. fileDisplayActivity.setIndeterminate(false);
  104. }
  105. if (!result.isSuccess() && !isCancelled()) {
  106. photoFragment.setEmptyListMessage(ExtendedListFragment.SearchType.PHOTO_SEARCH);
  107. }
  108. photoFragment.setPhotoSearchQueryRunning(false);
  109. }
  110. }
  111. }