6.6 KiB
Network State Manager Improvement Design
Overview
The current NetworkStateManager implementation does not support interrupting notifications. When a notification popup is still active and a new notification with higher priority arrives, the system cannot interrupt the previous notification. The requirement is to implement a notification interruption mechanism where higher priority notifications can preempt lower priority ones.
Current Architecture Analysis
NetworkStateManager Components
The current implementation consists of several key components:
- NetworkStateManager: Main manager class that handles network state changes and notification display
- NetworkStateMonitor/SimpleNetworkStateManager: Monitors network state changes
- NotificationManager: Manages notification display and hiding
- CommonNotificationActivity: The actual notification UI activity
Current Notification Flow
sequenceDiagram
participant NSM as NetworkStateManager
participant NM as NotificationManager
participant CNA as CommonNotificationActivity
NSM->>NM: showNotification()
NM->>NM: Check if activity showing
NM->>CNA: Start notification activity
CNA->>CNA: Display notification for duration
CNA->>NM: Broadcast on close
Limitation Analysis
The current implementation has the following limitations:
- Notifications cannot be interrupted - if one is showing, new notifications are ignored
- No priority mechanism for different notification types
- No queue management for pending notifications
Improved Architecture Design
Design Goals
- Support notification interruption based on priority
- Maintain backward compatibility
- Ensure proper resource cleanup
- Handle notification queue management
Enhanced Notification Flow
sequenceDiagram
participant NSM as NetworkStateManager
participant ENM as EnhancedNotificationManager
participant CNA as CommonNotificationActivity
NSM->>ENM: showNotification(priority, ...)
ENM->>ENM: Check current notification
alt Higher priority
ENM->>CNA: Interrupt current notification
ENM->>CNA: Show new notification
else Same or lower priority
ENM->>ENM: Queue notification
end
Component Modifications
1. Notification Priority Enum
A new priority enumeration will be introduced:
Priority | Value | Description |
---|---|---|
LOW | 0 | Low priority notifications |
NORMAL | 1 | Normal priority notifications |
HIGH | 2 | High priority notifications |
URGENT | 3 | Urgent notifications that can interrupt any other |
2. Enhanced Notification Manager
The NotificationManager will be enhanced with the following capabilities:
- Priority-based interruption: Higher priority notifications can interrupt lower priority ones
- Notification queue: Pending notifications will be queued based on priority
- Current notification tracking: Track the currently displayed notification's priority
3. Notification Data Structure
A new data structure will be created to hold notification information:
class NotificationRequest {
NotificationType type;
String message;
int iconResId;
int durationSeconds;
String soundPath;
NotificationPriority priority;
}
Implementation Plan
Phase 1: Core Enhancement
-
Add NotificationPriority enum:
- Define priority levels (LOW, NORMAL, HIGH, URGENT)
- Associate with NotificationType for default priorities
-
Enhance NotificationManager:
- Add priority parameter to showNotification methods
- Implement interruption logic
- Add notification queue management
-
Modify NetworkStateManager:
- Update notification calls to include priority
- Set appropriate priorities for different network events
Phase 2: Queue Management
-
Implement notification queue:
- FIFO for same priority notifications
- Priority-based for different priority notifications
-
Add queue processing logic:
- Process next notification when current one closes
- Handle queue cleanup
Phase 3: Testing and Validation
-
Unit tests:
- Test interruption scenarios
- Test queue management
- Test backward compatibility
-
Integration tests:
- Test with actual network state changes
- Validate priority-based behavior
API Changes
New Enum: NotificationPriority
public enum NotificationPriority {
LOW(0),
NORMAL(1),
HIGH(2),
URGENT(3);
private final int value;
NotificationPriority(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Enhanced NotificationManager Methods
// New methods with priority support
public void showNotification(NotificationType type, String message, int iconResId,
int durationSeconds, NotificationPriority priority);
public void showNotification(NotificationType type, String message, int iconResId,
int durationSeconds, String soundPath, NotificationPriority priority);
Updated NetworkStateManager
The NetworkStateManager will use appropriate priorities for different network events:
- NETWORK_LOST: HIGH priority
- NETWORK_CONNECTED: NORMAL priority
- Other events: As appropriate
Backward Compatibility
To maintain backward compatibility:
- Existing method signatures will be preserved
- Default priority (NORMAL) will be used for existing calls
- All existing functionality will continue to work as before
Testing Strategy
Unit Tests
-
Interruption Tests:
- Test that high priority notifications interrupt lower priority ones
- Test that same priority notifications do not interrupt
- Test urgent notifications interrupt any other
-
Queue Management Tests:
- Test notification queuing with different priorities
- Test FIFO behavior for same priority notifications
- Test queue processing when notifications close
Integration Tests
- Network State Change Tests:
- Simulate network loss and connection with different timing
- Validate that urgent network notifications interrupt others
- Verify proper notification display and cleanup
Conclusion
This design addresses the requirement for notification interruption in the NetworkStateManager by introducing a priority-based notification system. The solution maintains backward compatibility while adding the necessary functionality to handle urgent notifications that can preempt existing ones. The implementation is modular and can be extended for future requirements.