# etcparking/screencapture architecture

> Architecture diagram of the GitHub repository etcparking/screencapture, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/etcparking/screencapture
- Repository: https://github.com/etcparking/screencapture

## Overview

1. The “app” module, which is a sample Android application demonstrating how to integrate and use the ScreenCapture library.
2. The “screencapture” module, which contains the core library code (including classes like ScreenCapture, VideoComposer, and associated Android utility classes).

##### Main Components
- Sample Application (app module): Represents the consumer or demonstration of the library. It includes the Android UI and activities (e.g., MainActivity) where the ScreenCapture API is instantiated and used.
- ScreenCapture Library Module (screencapture): Houses the core classes that actualize functionalities like screen capture, screen recording, and event handling.
- VideoComposer Component: A specialized part of the library for joining video files, which depends on specific video/audio encoding parameters.
- Android Framework & External Services: Components such as MediaProjection, permission and activity lifecycle callbacks (onActivityResult, onRequestPermissionsResult), and file system storage for saving screens captures and recordings.

##### Relationships and Interactions
- The Sample Application interacts with the ScreenCapture library API by:
- Creating an instance (via newInstance(this)).
- Setting up event listeners to receive callbacks on successful or failed operation (for both capture and recording).
- Handling Android permissions and lifecycle events to forward them to the library (e.g., onActivityResult for MediaProjection permissions).
- The ScreenCapture library internally uses Android’s MediaProjection API to trigger the actual screen capture/recording.
- The library manages file I/O to save images and videos and interacts with external storage paths as configured by the client.
- The VideoComposer gets fed a list of video files (ensuring matching encoding conditions) and outputs a composed (merged) video.

##### Important Architectural Patterns and Design Principles
- Modularity: The clear separation between the sample application (client side) and the library itself clarifies responsibilities – the app only handles UI and permission logic, whereas the library encapsulates the actual media handling.
- Callback/Listener Pattern: The library uses listener interfaces (e.g., OnCaptureListener) to notify the application about events such as success or failure, ensuring loose coupling.
- Lifecycle Integration: Hooks into Android’s activity lifecycle (e.g., onDestroy, onActivityResult) are managed to ensure the library cleans up resources properly.
- Abstraction and Reusability: Exposing a simple API (like the newInstance() method) and relying on configuration via setter methods (e.g., to set image/video save paths) encourages reusability in varied Android contexts.

##### Relevant Technologies, Frameworks, or Libraries
- Android Framework (Activities, Permissions, MediaProjection).
- Gradle Build System (as seen in the build.gradle files at the root and module levels).
- Java for Android (as indicated by the source files and package structure, e.g., com.shark.screencapture).
- Standard Android UI elements (such as the layout defined in activity_main.xml) for the sample app.
- Media codecs and file I/O for processing video and images.

##### Elements
- How the app initiates a screen capture or recording.
- Event flow from MediaProjection API (invoked by the library) leading to image/video file storage.
- Use of callbacks in notifying the app about results (success or failure).
- The composition process where VideoComposer compiles multiple videos into one output file.

- Left: “Sample Application” block (showing UI layer and MainActivity).
- Right: “ScreenCapture Library” block (containing subcomponents for capture/record, video composition, and utility classes).
- Below or alongside these, include a block for “Android Framework & External Services” (showing MediaProjection API, Storage APIs, and Permission Management).
- Instantiation (newInstance)
- Setting listener callbacks (e.g., setCaptureListener)
- Event forwarding (onActivityResult, onRequestPermissionsResult)
- Invocation of functions (screenCapture() and record())
- Within the “ScreenCapture Library” block, visually separate:
- The screen capture/recording functionality that interacts with MediaProjection.
- The VideoComposer portion for merging videos.
- Use annotations to highlight critical points such as cleanup operations and configuration of file storage paths.
- Every component is clearly labeled and that data flow directions (requests, responses, event notifications) are explicitly indicated.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Sample Application Block
    subgraph "Sample Application"
        A["MainActivity"] 
    end

    %% ScreenCapture Library Block
    subgraph "ScreenCapture Library"
        B["ScreenCapture"]
        C["VideoComposer"]
        D["AndroidUtil"]
    end

    %% Android Framework & External Services Block
    subgraph "Android Framework & External Services"
        E["MediaProjection API"]
        F["PermissionManagement"]
        G["FileStorage"]
    end

    %% Interactions from Sample Application to Library
    A -->|"newInstance"| B
    A -->|"setCaptureListener"| B
    A -->|"onActivityResult"| B

    %% Library internal interactions and external calls
    B -->|"uses"| D
    B -->|"invokes"| E
    B -->|"checks"| F
    B -->|"savesCapture"| G
    B -->|"callsComposition"| C

    %% VideoComposer interaction with external storage
    C -->|"mergesVideos"| G

    %% Callback from Library to Sample Application
    B -->|"callbackNotification"| A

    %% Click Events
    click A "https://github.com/etcparking/screencapture/blob/master/app/src/main/java/com/shark/sample/MainActivity.java"
    click B "https://github.com/etcparking/screencapture/blob/master/screencapture/src/main/java/com/shark/screencapture/ScreenCapture.java"
    click C "https://github.com/etcparking/screencapture/blob/master/screencapture/src/main/java/com/shark/screencapture/VideoComposer.java"
    click D "https://github.com/etcparking/screencapture/blob/master/screencapture/src/main/java/com/shark/screencapture/AndroidUtil.java"

    %% Styles
    classDef sampleApp fill:#fdd,stroke:#f66,stroke-width:2px;
    classDef library fill:#dfd,stroke:#6f6,stroke-width:2px;
    classDef android fill:#ddf,stroke:#66f,stroke-width:2px;

    class A sampleApp;
    class B,C,D library;
    class E,F,G android;
```
