Skip to content

Auto Flush Events on Network Restore#966

Open
deeksha-rgb wants to merge 58 commits intotask/release/8.1.0from
task/SDK-5605-auto-flush-on-network-restore
Open

Auto Flush Events on Network Restore#966
deeksha-rgb wants to merge 58 commits intotask/release/8.1.0from
task/SDK-5605-auto-flush-on-network-restore

Conversation

@deeksha-rgb
Copy link
Copy Markdown
Contributor

@deeksha-rgb deeksha-rgb commented Mar 17, 2026

Summary

  • Converted EventQueueManager.java to EventQueueManager.kt

Test plan

  • Build project and verify no compilation errors
  • Run existing unit tests for EventQueueManager

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved event handling when network connectivity is restored. The app now properly clears pending operations and retriggers event processing after reconnecting to the network, ensuring more reliable data synchronization in offline scenarios.

SuriyaKannimuthu and others added 30 commits February 27, 2026 13:57
…ork APIs

- Refine NetworkMonitor: use applicationContext, add @volatile on _currentState,
  add NET_CAPABILITY_VALIDATED for captive portal detection, add getNetworkTypeString()
- Update DeviceInfo to accept NetworkMonitor in constructor and delegate isWifiConnected()
- Update CleverTapFactory to create NetworkMonitor before DeviceInfo and pass to CoreState
- Update EventQueueManager to use networkMonitor instance instead of deprecated static calls
- Fix InAppController to use NetworkMonitor.isNetworkOnline() replacing removed static method
- Update MockDeviceInfo and CallbackManagerTest constructors for new DeviceInfo signature
- Add NetworkMonitorTest
 · Removed companion object (static isNetworkOnline) — memory leak risk,wasteful (new ConnectivityManager every call), ignores instance state
 · Replaced with private checkCurrentConnectivity() instance method
 · Removed _currentState, single source of truth: _stateFlow.value

- BitmapDownloadRequestHandler.kt:
  · Removed NetworkManager/NetworkMonitor static dependency
  · Direct ConnectivityManager check (open class cannot use internal NetworkMonitor instance)
- Replace synchronous system queries inside NetworkCallback with
  direct callback arguments to avoid race conditions
- onAvailable: mark isAvailable=true, wait for onCapabilitiesChanged
- onLost/onUnavailable: directly set NetworkState.DISCONNECTED
- onCapabilitiesChanged: calculate state from passed NetworkCapabilities
- Update getNetworkTypeFromCapabilities() to accept capabilities param
- Remove legacy activeNetworkInfo fallback (pre API 23)
- Remove Build.VERSION.SDK_INT checks
- Added ConnectivityManager?.isNetworkAvailable() extension function in
  NetworkExtensions.kt (network package) with NET_CAPABILITY_INTERNET
  and NET_CAPABILITY_VALIDATED checks
- Updated NetworkMonitor.checkCurrentConnectivity() to use the shared
  extension instead of inline duplication
- Updated BitmapDownloadRequestHandler to use the shared extension,
  removing redundant connectivity logic
…ght after config init so network monitoring starts earlier during SDK initialization
@deeksha-rgb
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 30, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkMonitor.kt`:
- Line 90: The network callback path (registerNetworkCallback) is a no-op on API
< 24 leaving _stateFlow static and observeNetworkRestore() never firing; add a
fallback for older APIs by detecting SDK < 24 inside the registerNetworkCallback
method and either (a) register a ConnectivityManager broadcast receiver for
CONNECTIVITY_ACTION or (b) start a small periodic poll using
ConnectivityManager.getActiveNetworkInfo()/getNetworkCapabilities() to detect
connectivity changes, update the same _stateFlow variable when connectivity
changes, and invoke the existing onNetworkRestored pathway so
observeNetworkRestore() works; ensure the fallback is cleaned up in the same
teardown/unregister path and reference the existing symbols
registerNetworkCallback, _stateFlow, observeNetworkRestore, and
onNetworkRestored when implementing the alternative.
- Around line 88-90: observeNetworkRestore is currently started after
registerNetworkCallback which can cause the collector's drop(1) to discard a
genuine restore event and the launched coroutine isn't tracked or canceled in
cleanup(), causing leaks; fix by initializing the observer before registering
the platform callback (call observeNetworkRestore() before
registerNetworkCallback()), change the collector launch inside
observeNetworkRestore to store its Job in a field (e.g., networkRestoreJob) and
cancel that Job in cleanup(), and ensure the collector logic on _stateFlow (and
any use of drop(1)) still correctly ignores only the initial emission and not a
real transition to CONNECTED by adjusting subscription order as above.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 2143f362-6d29-48cb-ba02-693b15361ba7

📥 Commits

Reviewing files that changed from the base of the PR and between bf7058c and 8ef4812.

📒 Files selected for processing (8)
  • clevertap-core/src/main/java/com/clevertap/android/sdk/CleverTapFactory.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/events/EventQueueManager.java
  • clevertap-core/src/main/java/com/clevertap/android/sdk/events/EventQueueManager.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkManager.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkMonitor.kt
  • clevertap-core/src/test/java/com/clevertap/android/sdk/EventQueueManagerTest.kt
  • clevertap-core/src/test/java/com/clevertap/android/sdk/network/NetworkManagerTest.kt
  • clevertap-core/src/test/java/com/clevertap/android/sdk/network/NetworkMonitorTest.kt
💤 Files with no reviewable changes (1)
  • clevertap-core/src/main/java/com/clevertap/android/sdk/events/EventQueueManager.java

Comment thread clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkMonitor.kt Outdated
Comment thread clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkMonitor.kt Outdated
@Anush-Shand Anush-Shand changed the title Converted EventQueueManager.java to EventQueueManager.kt Auto Flush Events on Network Restore Mar 30, 2026
@deeksha-rgb
Copy link
Copy Markdown
Contributor Author

@coderabbitai Addressed. Please check

@Anush-Shand Anush-Shand requested a review from CTLalit March 31, 2026 05:53
}


// only call async
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of comment, can use an annotation like @workerthread

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be a suspend func in that case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is already called inside postAsyncSafelyTask which runs it on a background thread

@CTLalit
Copy link
Copy Markdown
Collaborator

CTLalit commented Mar 31, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 31, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkManager.kt (1)

78-88: Note: callback executes on IO dispatcher; multiple calls register multiple observers.

The delegation and observation pattern look correct. Two observations:

  1. The onRestored callback executes on Dispatchers.IO context. Current callers (EventQueueManager.flushQueueAsync) handle this appropriately by posting to their own executor.

  2. Each call to observeNetworkRestore launches a new collector without a mechanism to cancel previous ones. If this method is called multiple times (e.g., due to SDK reinitialization edge cases), duplicate callbacks would fire. Consider documenting that this should only be called once per NetworkManager instance, or returning a Job for cancellation if needed in the future.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkManager.kt`
around lines 78 - 88, The observeNetworkRestore currently launches a collector
on networkManagerScope each call and runs on Dispatchers.IO, so repeated calls
create duplicate observers and callbacks. Modify observeNetworkRestore (and its
callers) to return the launched Job (i.e., return the result of
networkManagerScope.launch) so callers can cancel the observer when
reinitializing; additionally document in the KDoc for observeNetworkRestore and
isNetworkOnline that the onRestored callback is invoked on the IO dispatcher and
callers should marshal to their own executors if needed. Ensure you reference
observeNetworkRestore, networkManagerScope, networkMonitor.networkRestoreEvents,
and onRestored when making the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In
`@clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkManager.kt`:
- Around line 78-88: The observeNetworkRestore currently launches a collector on
networkManagerScope each call and runs on Dispatchers.IO, so repeated calls
create duplicate observers and callbacks. Modify observeNetworkRestore (and its
callers) to return the launched Job (i.e., return the result of
networkManagerScope.launch) so callers can cancel the observer when
reinitializing; additionally document in the KDoc for observeNetworkRestore and
isNetworkOnline that the onRestored callback is invoked on the IO dispatcher and
callers should marshal to their own executors if needed. Ensure you reference
observeNetworkRestore, networkManagerScope, networkMonitor.networkRestoreEvents,
and onRestored when making the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 92a97806-071e-43c1-893b-8ff98310b026

📥 Commits

Reviewing files that changed from the base of the PR and between 8ef4812 and 5568368.

📒 Files selected for processing (6)
  • clevertap-core/src/main/java/com/clevertap/android/sdk/CleverTapFactory.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/events/EventQueueManager.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkManager.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkMonitor.kt
  • clevertap-core/src/test/java/com/clevertap/android/sdk/EventQueueManagerTest.kt
  • clevertap-core/src/test/java/com/clevertap/android/sdk/network/NetworkMonitorTest.kt
🚧 Files skipped from review as they are similar to previous changes (4)
  • clevertap-core/src/test/java/com/clevertap/android/sdk/network/NetworkMonitorTest.kt
  • clevertap-core/src/test/java/com/clevertap/android/sdk/EventQueueManagerTest.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/CleverTapFactory.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkMonitor.kt

Base automatically changed from task/SDK-4890-network-info-fetch to task/release/8.1.0 April 1, 2026 06:02
- When the device transitions from offline → online, the auto-flush drains all queued events from the DB. However, any previously scheduled timer-based flush (commsRunnable / pushNotificationViewedRunnable) would still fire and perform a redundant empty DB read.
- Cancel those pending timers before auto-flushing since the network-restore flush already sends everything in the DB.
@deeksha-rgb
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 9, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkManager.kt`:
- Around line 85-94: The observeNetworkRestore registration is racy and won’t
allow re-registration after completion; wrap the networkRestoreJob check and
assignment in a short synchronized/lock block (use a private lock object or
synchronized(this)) and change the condition to create a new job when
networkRestoreJob is null or not active (e.g., networkRestoreJob == null ||
networkRestoreJob?.isActive == false); inside the launched job
(networkManagerScope.launch { networkMonitor.networkRestoreEvents.collect { ...
} }) attach invokeOnCompletion to set networkRestoreJob = null so the observer
can be restarted, and keep using config.accountId, logger.debug and onRestored
as before.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: e5000ec5-aad6-4782-8caf-59848e7479af

📥 Commits

Reviewing files that changed from the base of the PR and between 5568368 and 905e816.

📒 Files selected for processing (5)
  • clevertap-core/src/main/java/com/clevertap/android/sdk/CleverTapFactory.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/events/EventQueueManager.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkManager.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkMonitor.kt
  • clevertap-core/src/test/java/com/clevertap/android/sdk/EventQueueManagerTest.kt
🚧 Files skipped from review as they are similar to previous changes (3)
  • clevertap-core/src/main/java/com/clevertap/android/sdk/network/NetworkMonitor.kt
  • clevertap-core/src/main/java/com/clevertap/android/sdk/CleverTapFactory.kt
  • clevertap-core/src/test/java/com/clevertap/android/sdk/EventQueueManagerTest.kt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants