|
66 | 66 | import com.nextcloud.utils.extensions.BundleExtensionsKt; |
67 | 67 | import com.nextcloud.utils.extensions.FileExtensionsKt; |
68 | 68 | import com.nextcloud.utils.extensions.IntentExtensionsKt; |
69 | | -import com.nextcloud.utils.extensions.ViewExtensionsKt; |
70 | 69 | import com.nextcloud.utils.fileNameValidator.FileNameValidator; |
71 | 70 | import com.nextcloud.utils.view.FastScrollUtils; |
72 | 71 | import com.owncloud.android.MainApp; |
|
146 | 145 | import java.util.Collection; |
147 | 146 | import java.util.List; |
148 | 147 | import java.util.Optional; |
| 148 | +import java.util.concurrent.Executors; |
| 149 | +import java.util.concurrent.ScheduledExecutorService; |
| 150 | +import java.util.concurrent.TimeUnit; |
149 | 151 |
|
150 | 152 | import javax.inject.Inject; |
151 | 153 |
|
@@ -252,6 +254,8 @@ public class FileDisplayActivity extends FileActivity |
252 | 254 | */ |
253 | 255 | private long fileIDForImmediatePreview = -1; |
254 | 256 |
|
| 257 | + private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors()); |
| 258 | + |
255 | 259 | public void setFileIDForImmediatePreview(long fileIDForImmediatePreview) { |
256 | 260 | this.fileIDForImmediatePreview = fileIDForImmediatePreview; |
257 | 261 | } |
@@ -2180,35 +2184,36 @@ public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) { |
2180 | 2184 | * @param ignoreFocus reloads file list even without focus, e.g. on tablet mode, focus can still be in detail view |
2181 | 2185 | */ |
2182 | 2186 | public void startSyncFolderOperation(final OCFile folder, final boolean ignoreETag, boolean ignoreFocus) { |
2183 | | - |
2184 | 2187 | // the execution is slightly delayed to allow the activity get the window focus if it's being started |
2185 | 2188 | // or if the method is called from a dialog that is being dismissed |
2186 | | - if (TextUtils.isEmpty(searchQuery) && getUser().isPresent()) { |
2187 | | - getHandler().postDelayed(() -> { |
2188 | | - Optional<User> user = getUser(); |
2189 | | - |
2190 | | - if (!ignoreFocus && !hasWindowFocus() || !user.isPresent()) { |
2191 | | - // do not refresh if the user rotates the device while another window has focus |
2192 | | - // or if the current user is no longer valid |
2193 | | - return; |
2194 | | - } |
| 2189 | + if (!TextUtils.isEmpty(searchQuery) || getUser().isEmpty()) { |
| 2190 | + Log_OC.w(TAG,"Cannot startSyncFolderOperation, search query is empty or user not present"); |
| 2191 | + return; |
| 2192 | + } |
| 2193 | + |
| 2194 | + executor.schedule(() -> { |
| 2195 | + Optional<User> user = getUser(); |
| 2196 | + if (!ignoreFocus && !hasWindowFocus() || user.isEmpty()) { |
| 2197 | + Log_OC.w(TAG,"do not refresh if the user rotates the device while another window has focus or if the current user is no longer valid"); |
| 2198 | + return; |
| 2199 | + } |
2195 | 2200 |
|
2196 | | - long currentSyncTime = System.currentTimeMillis(); |
2197 | | - mSyncInProgress = true; |
| 2201 | + long currentSyncTime = System.currentTimeMillis(); |
| 2202 | + mSyncInProgress = true; |
2198 | 2203 |
|
2199 | | - // perform folder synchronization |
2200 | | - RemoteOperation refreshFolderOperation = new RefreshFolderOperation(folder, currentSyncTime, false, ignoreETag, getStorageManager(), user.get(), getApplicationContext()); |
2201 | | - refreshFolderOperation.execute(getAccount(), MainApp.getAppContext(), FileDisplayActivity.this, null, null); |
| 2204 | + // perform folder synchronization on background thread |
| 2205 | + final var refreshFolderOperation = new RefreshFolderOperation(folder, currentSyncTime, false, ignoreETag, getStorageManager(), user.get(), getApplicationContext()); |
| 2206 | + refreshFolderOperation.execute(getAccount(), MainApp.getAppContext(), FileDisplayActivity.this, null, null); |
2202 | 2207 |
|
| 2208 | + // switch back to main thread |
| 2209 | + getHandler().post(() -> { |
2203 | 2210 | OCFileListFragment fragment = getListOfFilesFragment(); |
2204 | | - |
2205 | 2211 | if (fragment != null && !(fragment instanceof GalleryFragment)) { |
2206 | 2212 | fragment.setLoading(true); |
2207 | 2213 | } |
2208 | | - |
2209 | 2214 | setBackgroundText(); |
2210 | | - }, DELAY_TO_REQUEST_REFRESH_OPERATION_LATER); |
2211 | | - } |
| 2215 | + }); |
| 2216 | + }, DELAY_TO_REQUEST_REFRESH_OPERATION_LATER, TimeUnit.MILLISECONDS); |
2212 | 2217 | } |
2213 | 2218 |
|
2214 | 2219 | private void requestForDownload(OCFile file, String downloadBehaviour, String packageName, String activityName) { |
@@ -2600,6 +2605,7 @@ public void onReceive(Context context, Intent intent) { |
2600 | 2605 |
|
2601 | 2606 | @Override |
2602 | 2607 | protected void onDestroy() { |
| 2608 | + executor.shutdown(); |
2603 | 2609 | LocalBroadcastManager.getInstance(this).unregisterReceiver(refreshFolderEventReceiver); |
2604 | 2610 | super.onDestroy(); |
2605 | 2611 | } |
|
0 commit comments