android icon indicating copy to clipboard operation
android copied to clipboard

SyncBroadcastReceiver in FileDisplayActivity Triggers Multiple Times Causing Repeated Directory Listing

Open alperozturk96 opened this issue 8 months ago • 3 comments

Steps to reproduce

  1. Upload files in a directory.
  2. Observe FileDisplayActivity.SyncBroadcastReceiver being triggered multiple times during the process.
  3. As a result, listDirectory() is called repeatedly.
  4. These repeated calls lead to multiple invocations of OCFileListAdapter.swapDirectory().

Suggestion

The broadcast receiver should be triggered only when necessary, and directory listing should occur a single time per meaningful change.

alperozturk96 avatar May 02 '25 08:05 alperozturk96

Currently, the following code retrieves the path of the remote folder that needs to be synchronized:

String synchFolderRemotePath = intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);

Subsequently, we fetch the list of remote files inside that folder with:

RemoteOperationResult synchResult = (RemoteOperationResult) DataHolderUtil.getInstance().retrieve(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));

Given that we already have access to the current list of files in the local directory, we can calculate and compare the hash codes of both local and remote file lists. If the hash codes match, we can safely skip the synchronization process. If they differ, we proceed with the update.

This approach would result in a significant performance improvement. Even a single unnecessary folder update currently triggers a full directory listing, including database queries, thumbnail cache checks, and redundant rendering of thumbnails for each file.

Implementation Steps

Convert FileDisplayActivity.SyncBroadcastReceiver to Kotlin (First PR)

Refactor the logic for better readability, maintainability and performance.

~~**Introduce Hash Code Comparison (Second PR)**Implement the hash code comparison logic within FileDisplayActivity.SyncBroadcastReceiver.~~

Analyze SyncBroadcastReceiver Usage (Third PR)

Review the usage of FileDisplayActivity.SyncBroadcastReceiver throughout the codebase. Identify and remove any redundant or unnecessary triggers if it exists.

Better OCFileListAdapter implementation - this is big one and should be discussed in different issue


These steps will bring the Android implementation more in line with iOS in terms of performance and state management, and it's a noticeable user experience improvement.

For each change, we must validate the following functional areas:

  • Folder and file listing behavior
  • "Favorites", "Shared with you", "Personal", and "On-device files" filters.
  • General sync behavior such as folder sync, file download.

I believe we should prioritize this task.

@tobiasKaminsky @ZetaTom Please let me know if this approach is acceptable. I can start working on the first pull request.

alperozturk96 avatar Jun 06 '25 05:06 alperozturk96

To prevent unnecessary calls, the following broadcast events should be reviewed.

FileSyncAdapter.EVENT_FULL_SYNC_START, 
FileSyncAdapter.EVENT_FULL_SYNC_END, 
FileSyncAdapter.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED,
RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED,
RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED

After eliminating any redundant event triggers, we can explore alternative solutions if the issue persists.

alperozturk96 avatar Jun 12 '25 11:06 alperozturk96

In the FileSyncAdapter.onPerformSync method, we currently send two broadcast events:

sendLocalBroadcast(EVENT_FULL_SYNC_START, null, null);
// ...
// ...
// ...
sendLocalBroadcast(EVENT_FULL_SYNC_END, null, mLastFailedResult);

EVENT_FULL_SYNC_START is used to set mSyncInProgress boolean variables.

alperozturk96 avatar Jun 13 '25 14:06 alperozturk96

As mentioned in #15081, these broadcast events may also cause other side effects. In the FileDisplayActivity, currentDir is sometimes null when rapidly switching between tabs (Files, Favourites and Media). This causes can cause issues, for example #15103.

Investigating this issue and minimising broadcast events should increase overall reliability and decrease and the number of UI glitches and edge cases we have to handle.

ZetaTom avatar Jul 09 '25 09:07 ZetaTom

Steps to reproduce the null currentDir scenario:

  1. Open the Shared section from the navigation drawer.
  2. Then, navigate to All Files using the bottom navigation bar.

alperozturk96 avatar Jul 10 '25 10:07 alperozturk96