Skip to content

Commit 0212ba8

Browse files
committed
remove hardcoded delay use onWindowFocusChanged instead
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent a8797f7 commit 0212ba8

2 files changed

Lines changed: 51 additions & 35 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ public abstract class FileActivity extends DrawerActivity
144144
public static final int REQUEST_CODE__UPDATE_CREDENTIALS = 0;
145145
public static final int REQUEST_CODE__LAST_SHARED = REQUEST_CODE__UPDATE_CREDENTIALS;
146146

147-
protected static final long DELAY_TO_REQUEST_OPERATIONS_LATER = 200;
148-
149147
/* Dialog tags */
150148
private static final String DIALOG_UNTRUSTED_CERT = "DIALOG_UNTRUSTED_CERT";
151149
private static final String DIALOG_CERT_NOT_SAVED = "DIALOG_CERT_NOT_SAVED";

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

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ class FileDisplayActivity :
216216
setEmptyListState()
217217
}
218218

219+
private var pendingSyncFolderOperation: Runnable? = null
220+
219221
private var mWaitingToSend: OCFile? = null
220222

221223
private var mDrawerMenuItemstoShowHideList: MutableCollection<MenuItem>? = null
@@ -2528,45 +2530,63 @@ class FileDisplayActivity :
25282530
fun startSyncFolderOperation(folder: OCFile?, ignoreETag: Boolean, ignoreFocus: Boolean = false) {
25292531
Log_OC.d(TAG, "startSyncFolderOperation called, ignoreEtag: $ignoreETag, ignoreFocus: $ignoreFocus")
25302532

2531-
// the execution is slightly delayed to allow the activity get the window focus if it's being started
2532-
// or if the method is called from a dialog that is being dismissed
2533+
if (!TextUtils.isEmpty(searchQuery) || !user.isPresent) {
2534+
return
2535+
}
25332536

2534-
if (TextUtils.isEmpty(searchQuery) && user.isPresent) {
2535-
mSyncInProgress = true
2537+
val syncFolder = Runnable { executeSyncFolderOperation(folder, ignoreETag) }
25362538

2537-
handler.postDelayed({
2538-
val user = getUser()
2539-
if ((!ignoreFocus && !hasWindowFocus()) || !user.isPresent) {
2540-
// do not refresh if the user rotates the device while another window has focus
2541-
// or if the current user is no longer valid
2542-
mSyncInProgress = false
2543-
return@postDelayed
2544-
}
2539+
// The refresh must not run while another window floats over the activity, e.g. a dialog that is being
2540+
// dismissed or a rotation. Rather than waiting a fixed delay run right away when it already has focus
2541+
// and replay the request on the next focus gain.
2542+
if (ignoreFocus || hasWindowFocus()) {
2543+
pendingSyncFolderOperation = null
2544+
syncFolder.run()
2545+
} else {
2546+
pendingSyncFolderOperation = syncFolder
2547+
}
2548+
}
25452549

2546-
val currentSyncTime = System.currentTimeMillis()
2550+
override fun onWindowFocusChanged(hasFocus: Boolean) {
2551+
super.onWindowFocusChanged(hasFocus)
25472552

2548-
val operation = RefreshFolderOperation(
2549-
folder,
2550-
currentSyncTime,
2551-
false,
2552-
ignoreETag,
2553-
storageManager,
2554-
user.get(),
2555-
applicationContext
2556-
)
2557-
operation.execute(
2558-
account,
2559-
MainApp.getAppContext(),
2560-
this@FileDisplayActivity,
2561-
null,
2562-
null
2563-
)
2553+
if (!hasFocus) {
2554+
return
2555+
}
25642556

2565-
fetchRecommendedFilesIfNeeded(ignoreETag, folder)
2566-
}, DELAY_TO_REQUEST_REFRESH_OPERATION_LATER)
2557+
pendingSyncFolderOperation?.let {
2558+
pendingSyncFolderOperation = null
2559+
it.run()
25672560
}
25682561
}
25692562

2563+
private fun executeSyncFolderOperation(folder: OCFile?, ignoreETag: Boolean) {
2564+
val user = getUser()
2565+
if (!user.isPresent) {
2566+
return
2567+
}
2568+
2569+
mSyncInProgress = true
2570+
2571+
RefreshFolderOperation(
2572+
folder,
2573+
System.currentTimeMillis(),
2574+
false,
2575+
ignoreETag,
2576+
storageManager,
2577+
user.get(),
2578+
applicationContext
2579+
).execute(
2580+
account,
2581+
MainApp.getAppContext(),
2582+
this@FileDisplayActivity,
2583+
null,
2584+
null
2585+
)
2586+
2587+
fetchRecommendedFilesIfNeeded(ignoreETag, folder)
2588+
}
2589+
25702590
private fun fetchRecommendedFilesIfNeeded(ignoreETag: Boolean, folder: OCFile?) {
25712591
val optionalCapabilities = capabilities
25722592
if (optionalCapabilities.isEmpty) {
@@ -3294,8 +3314,6 @@ class FileDisplayActivity :
32943314
@JvmField
32953315
val REQUEST_CODE__SELECT_CONTENT_FROM_APPS_AUTO_RENAME: Int = REQUEST_CODE__LAST_SHARED + 7
32963316

3297-
protected val DELAY_TO_REQUEST_REFRESH_OPERATION_LATER: Long = DELAY_TO_REQUEST_OPERATIONS_LATER + 350
3298-
32993317
private val TAG: String = FileDisplayActivity::class.java.getSimpleName()
33003318

33013319
const val TAG_LIST_OF_FILES: String = "LIST_OF_FILES"

0 commit comments

Comments
 (0)