How xuningjack/anrmanager works
1. Repository shape
- This is a small Android multi-module project with two main modules:
- library/: the reusable ANR / UI-freeze monitoring library.
- app/: a sample Android application that demonstrates integration and manual freeze testing.
- Root Gradle files wire the modules together; settings.gradle includes both.
2. Primary purpose
- The library detects when the UI thread is blocked long enough to be considered a freeze/ANR precursor.
- When a threshold is crossed, it collects thread stack traces and reports them through a callback for logging, analysis, or upload.
3. Core runtime boundary
- The main architectural boundary is between:
- App code: initializes the monitor and receives callbacks.
- Library internals: instruments the main thread, observes timing, and gathers diagnostics.
- The app does not directly perform monitoring logic; it only configures and consumes events.
4. Library entry point
- com.jack.anr.AnrManager is the central facade.
- It likely exposes:
- a default initialize(this) bootstrap path,
- a Builder for configuration,
- a callback interface for block events,
- and a monitor-mode switch.
5. Configuration model
- Config and AnrConstant define monitoring parameters and defaults such as:
- threshold time for declaring a block,
- stack collection interval,
- logging enablement,
- monitoring mode.
- The README indicates valid ranges and a constraint that collection interval must not exceed the block threshold.
6. Two monitoring strategies
- MonitorMode.FRAME:
- Uses Android’s frame scheduling path via Choreographer / FrameCallback.
- Measures frame gaps to detect rendering stalls.
- README notes this is API 16+ and otherwise falls back to looper-based monitoring.
- MonitorMode.LOOPER:
- Hooks the main thread Looper through a Printer.
- Measures how long main-thread message dispatch takes.
- These are the main detection branches in the architecture.
7. Observation pipeline
- observer/ contains the monitoring hooks that watch the UI thread:
- UILooperObserver for looper dispatch timing,
- FPSFrameCallBack for frame-based timing,
- BlockHandler likely coordinates block detection and response.
- These components are the “sensor layer” that converts UI-thread latency into block events.
8. Stack collection subsystem
- collector/ contains the diagnostic capture path:
- Collector as the abstraction,
- StackTraceCollector for actually gathering thread stack traces.
- This is the “evidence collection” stage that runs when a block is detected.
9. Event reporting flow
- On block detection, the library emits:
- collected thread stack traces,
- optional ANR-related info,
- and mode-specific timing arguments.
- The app supplies an AnrCallback to receive this data and decide whether to log it, persist it, or send it to a server.
10. Sample app role
- app/src/main/java/com/jack/test/MainActivity.java is a demo screen used to simulate blocking behavior.
- CustomApp.java likely initializes AnrManager from the application class, which matches the README’s recommended integration point.
11. UI and packaging
- The sample app has a single layout activity_main.xml and standard Android resources, indicating this is a demo/verification harness rather than a feature-rich app.
- The library module has its own AndroidManifest.xml and ProGuard rules, making it publishable as a standalone Android artifact.
12. Performance and operational characteristics
- The README explicitly calls out overhead from continuous monitoring and stack collection, roughly 2–3% depending on collection frequency.
- That makes the system a background diagnostic service with a tradeoff between sensitivity and runtime cost.
13. External/platform dependencies
- The architecture depends on Android framework APIs for:
- main-thread Looper,
- Choreographer frame callbacks,
- thread stack inspection.
- No backend service is built into the repo; external upload/storage is left to the app’s callback implementation.
14. What to draw in an architecture diagram
- Show the app calling AnrManager.initialize(...) during startup.
- From AnrManager, branch to either frame-based or looper-based observation.
- Feed observed timing into block detection logic.
- On threshold breach, invoke stack collection.
- Emit results through the callback back into app code for logging or reporting.
Main entry points: AnrManager.java AnrManager · BlockHandler.java BlockHandler · library library · UILooperObserver.java UILooperObserver
Components 16
App module
Sample Android app that boots the monitor and simulates UI freezes.
- app · Sample application wiring the library and demoing block scenarios.
app - CustomApp · Application entry point that initializes monitoring.
app/src/main/java/com/jack/test/CustomApp.java - MainActivity · Screen used to trigger or observe freeze behavior.
app/src/main/java/com/jack/test/MainActivity.java - Main layout · Single demo UI layout for the sample app.
app/src/main/res/layout/activity_main.xml
Library module
Reusable ANR / UI-freeze monitoring library.
- library · Standalone monitoring library published as an Android artifact.
library - AnrManager · Central entry point for initializing and configuring monitoring.
library/src/main/java/com/jack/anr/AnrManager.java - Config · Monitoring parameters such as thresholds, intervals, and mode.
library/src/main/java/com/jack/anr/Config.java - AnrConstant · Default values and constraints used by the monitor.
library/src/main/java/com/jack/anr/AnrConstant.java - BlockHandler · Coordinates block detection and response when timing exceeds thresholds.
library/src/main/java/com/jack/anr/observer/BlockHandler.java - UILooperObserver · Observes main-thread message dispatch latency via Looper printing.
library/src/main/java/com/jack/anr/observer/UILooperObserver.java - FPSFrameCallBack · Observes frame timing through Choreographer callbacks.
library/src/main/java/com/jack/anr/observer/FPSFrameCallBack.java - Collector · Abstract collection stage for gathering evidence during a block.
library/src/main/java/com/jack/anr/collector/Collector.java - StackTraceCollector · Collects thread stack traces for reporting and analysis.
library/src/main/java/com/jack/anr/collector/StackTraceCollector.java - AnrCallback · App-supplied sink for block events, stacks, and timing details.
Other components
- Main Looper · Platform message loop used by the looper-based monitor.
- Choreographer · Platform frame scheduler used by the frame-based monitor.
Connections 22
- CustomApp → AnrManager: initialize · Application bootstraps the monitor during startup.
- MainActivity → Main layout: uses · Demo activity renders the sample UI.
- app → CustomApp: contains · Sample app module packages the application class.
- app → MainActivity: contains · Sample app module packages the demo activity.
- AnrManager → Config: reads · Facade consumes runtime configuration.
- AnrManager → AnrConstant: defaults · Facade relies on predefined thresholds and constraints.
- AnrManager → UILooperObserver: selects · Configurable mode can route detection through looper observation.
- AnrManager → FPSFrameCallBack: selects · Configurable mode can route detection through frame observation.
- AnrManager → BlockHandler: starts · Facade activates the core detection pipeline.
- AnrManager → AnrCallback: registers · App callback receives detected block events.
- UILooperObserver → Main Looper: hooks · Looper-based detection instruments the main thread message loop.
- FPSFrameCallBack → Choreographer: hooks · Frame-based detection listens to frame scheduling callbacks.
- UILooperObserver → BlockHandler: reports timing · Dispatch duration is forwarded for block evaluation.
- FPSFrameCallBack → BlockHandler: reports timing · Frame gap timing is forwarded for block evaluation.
- BlockHandler → Collector: requests evidence · Threshold breach triggers diagnostic collection.
- Collector → StackTraceCollector: implements · Concrete collector gathers thread stack traces.
- StackTraceCollector → AnrCallback: emits · Collected stacks and block details are delivered back to the app.
- BlockHandler → AnrCallback: notifies · Detector forwards block events to the app sink.
- library → AnrManager: exposes · Library module packages the facade and internals.
- library → Config: contains · Library module includes configuration model.
- library → Collector: contains · Library module includes diagnostic collection code.
- library → BlockHandler: contains · Library module includes detection coordination code.