Skip to content

Commit c7dded7

Browse files
committed
since startSyncFolderOperation called onResume and startSyncFolderOperation runs on main thread it must be run on background thread thus executor is used.
Signed-off-by: alperozturk <alper_ozturk@proton.me>
1 parent 30b45ed commit c7dded7

1 file changed

Lines changed: 26 additions & 20 deletions

File tree

app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
import com.nextcloud.utils.extensions.BundleExtensionsKt;
6767
import com.nextcloud.utils.extensions.FileExtensionsKt;
6868
import com.nextcloud.utils.extensions.IntentExtensionsKt;
69-
import com.nextcloud.utils.extensions.ViewExtensionsKt;
7069
import com.nextcloud.utils.fileNameValidator.FileNameValidator;
7170
import com.nextcloud.utils.view.FastScrollUtils;
7271
import com.owncloud.android.MainApp;
@@ -146,6 +145,9 @@
146145
import java.util.Collection;
147146
import java.util.List;
148147
import java.util.Optional;
148+
import java.util.concurrent.Executors;
149+
import java.util.concurrent.ScheduledExecutorService;
150+
import java.util.concurrent.TimeUnit;
149151

150152
import javax.inject.Inject;
151153

@@ -252,6 +254,8 @@ public class FileDisplayActivity extends FileActivity
252254
*/
253255
private long fileIDForImmediatePreview = -1;
254256

257+
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
258+
255259
public void setFileIDForImmediatePreview(long fileIDForImmediatePreview) {
256260
this.fileIDForImmediatePreview = fileIDForImmediatePreview;
257261
}
@@ -2180,35 +2184,36 @@ public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) {
21802184
* @param ignoreFocus reloads file list even without focus, e.g. on tablet mode, focus can still be in detail view
21812185
*/
21822186
public void startSyncFolderOperation(final OCFile folder, final boolean ignoreETag, boolean ignoreFocus) {
2183-
21842187
// the execution is slightly delayed to allow the activity get the window focus if it's being started
21852188
// 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+
}
21952200

2196-
long currentSyncTime = System.currentTimeMillis();
2197-
mSyncInProgress = true;
2201+
long currentSyncTime = System.currentTimeMillis();
2202+
mSyncInProgress = true;
21982203

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);
22022207

2208+
// switch back to main thread
2209+
getHandler().post(() -> {
22032210
OCFileListFragment fragment = getListOfFilesFragment();
2204-
22052211
if (fragment != null && !(fragment instanceof GalleryFragment)) {
22062212
fragment.setLoading(true);
22072213
}
2208-
22092214
setBackgroundText();
2210-
}, DELAY_TO_REQUEST_REFRESH_OPERATION_LATER);
2211-
}
2215+
});
2216+
}, DELAY_TO_REQUEST_REFRESH_OPERATION_LATER, TimeUnit.MILLISECONDS);
22122217
}
22132218

22142219
private void requestForDownload(OCFile file, String downloadBehaviour, String packageName, String activityName) {
@@ -2600,6 +2605,7 @@ public void onReceive(Context context, Intent intent) {
26002605

26012606
@Override
26022607
protected void onDestroy() {
2608+
executor.shutdown();
26032609
LocalBroadcastManager.getInstance(this).unregisterReceiver(refreshFolderEventReceiver);
26042610
super.onDestroy();
26052611
}

0 commit comments

Comments
 (0)