# azhon/appupdate architecture

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

- Interactive diagram: https://gitdiagram.com/azhon/appupdate
- Repository: https://github.com/azhon/appupdate

## Overview

##### Project type and purpose
- This is an Android client library (appupdate) plus a no-op variant (appupdate-no-op) and a demo application (app).
- Purpose: provide an in-app APK update mechanism (dialog, background download, notification, install) that any Android app can integrate via Gradle.

##### High-level module structure
- app/                → Demo Android application
- appupdate/          → Core AAR library module with full implementation
- appupdate-no-op/    → A no-operation stub library (for Google Play flavor)
- build scripts & configs at root (settings.gradle, maven_publish.sh, Gradle wrappers)

a. Demo App (MainActivity)
- Invokes manager.download()
b. Library Core
- DownloadManager
– Builder pattern to collect parameters and kick off process
– Version check logic (apkVersionCode)
– Decides whether to show dialog or just download
- UI Layer
– UpdateDialogActivity (customizable dialog with progress bar for forced update)
- Service
– DownloadService handles background download on Android 10+ via startForegroundService
- Download Manager Implementation
– BaseHttpDownloadManager (abstract class)
– HttpDownloadManager (uses HttpURLConnection to fetch APK)
- Notification Manager
– NotificationUtil posts progress notifications, requests notification permission on Android 13
- File & APK Utilities
– ApkUtil (delete old APKs, trigger install via FileProvider)
– FileUtil, DensityUtil, LogUtil
c. Android OS
- Notification subsystem (permission, channel, progress bar)
- Background service lifecycle (Service / foreground requirement)
- FileProvider (install intent)
- Activity stack (dialog vs. background restrictions)

##### Interactions and data flows
1. Demo App → DownloadManager.Builder: set parameters
2. DownloadManager.build() → internal Controller
3. Controller checks remote versionCode vs. current
– If update needed: show UpdateDialogActivity
- User taps “Update” → triggers download
– Else if no version check: proceed straight to download
4. DownloadManager hands off to DownloadService (foreground)
5. DownloadService instantiates HttpDownloadManager
6. HttpDownloadManager streams .apk bytes → writes to externalCacheDir
- Emits progress → NotificationUtil updates notification progress bar
7. On complete → NotificationUtil posts “install” notification / DownloadService calls ApkUtil.install()
8. ApkUtil invokes FileProvider intent → OS package installer UI

##### Architectural patterns & principles
- Modularization: demo app separated from core update logic
- Builder pattern: DownloadManager.Builder for fluent configuration
- Template method / inheritance: BaseHttpDownloadManager → concrete HttpDownloadManager or custom subclass
- Service for resilience: background download survivable across Activity lifecycle
- Separation of concerns: UI (dialog), network (HttpDownloadManager), file I/O (FileUtil), notifications (NotificationUtil)

## Diagram (Mermaid)

```mermaid
flowchart TD
    title["AppUpdate System Architecture v4.3.6"]

    subgraph "Demo App"
        MA["MainActivity"]:::ui
        MD["MyDownload"]:::green
    end

    subgraph "Library Core"
        DM["DownloadManager.Builder"]:::green
        BS["BaseHttpDownloadManager"]:::green
        HD["HttpDownloadManager"]:::green
        DS["DownloadService (foreground)"]:::green
        UD["UpdateDialogActivity"]:::ui
        NB["NumberProgressBar"]:::ui
        NU["NotificationUtil"]:::orange
        AU["ApkUtil"]:::orange
        FU["FileUtil"]:::orange
        DU["DensityUtil"]:::orange
        LU["LogUtil"]:::orange
        FP["AppUpdateFileProvider"]:::gray
        CO["Constant"]:::gray
        LI1["OnDownloadListener"]:::gray
        LI2["OnDownloadListenerAdapter"]:::gray
        LI3["OnButtonClickListener"]:::gray
        LI4["LifecycleCallbacksAdapter"]:::gray
    end

    subgraph "No-Op Library"
        NODM["DownloadManager (No-Op)"]:::green
        NOBD["BaseHttpDownloadManager (No-Op)"]:::green
    end

    subgraph "Android OS"
        OSN["Notification Manager"]:::gray
        OSF["FileProvider"]:::gray
        OSS["Service Lifecycle"]:::gray
        OSI["Package Installer UI"]:::gray
    end

    ExternalCache["External Storage"]:::storage

    %% Flavour selection
    MA -->|"gradle flavor"| DM
    MA -->|"gradle flavor"| NODM

    %% Update flow
    MA -->|".download() call"| DM
    DM -->|"build() → Controller"| DS
    DM -->|"check version"| UD
    UD -->|"on update tap"| DS

    DS -->|"startForegroundService()"| OSS
    DS -->|"instantiates"| HD
    HD -->|"streams .apk bytes"| ExternalCache
    ExternalCache -->|"writes APK"| HD
    HD -->|"emits progress"| NU
    NU -->|"posts progress"| OSN
    HD -->|"onComplete"| AU
    AU -->|"install intent"| FP
    FP -->|"FileProvider intent"| OSF
    OSF -->|"launch installer"| OSI

    %% Utility references
    DS --> BS
    BS --> HD
    BS --> FU
    BS --> DU
    BS --> LU
    NU --> CO
    DS --> LI1
    DS --> LI2
    UD --> LI3
    OSS --> LI4
    UD --> NB

    %% Click Events
    click MA "https://github.com/azhon/appupdate/blob/main/app/src/main/java/com/azhon/app/MainActivity.kt"
    click MD "https://github.com/azhon/appupdate/blob/main/app/src/main/java/com/azhon/app/MyDownload.kt"
    click DM "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/manager/DownloadManager.kt"
    click NODM "https://github.com/azhon/appupdate/blob/main/appupdate-no-op/src/main/java/com/azhon/appupdate/manager/DownloadManager.kt"
    click BS "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/base/BaseHttpDownloadManager.kt"
    click NOBD "https://github.com/azhon/appupdate/blob/main/appupdate-no-op/src/main/java/com/azhon/appupdate/base/BaseHttpDownloadManager.kt"
    click HD "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/manager/HttpDownloadManager.kt"
    click DS "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/service/DownloadService.kt"
    click UD "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/view/UpdateDialogActivity.kt"
    click NB "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/view/NumberProgressBar.java"
    click NU "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/util/NotificationUtil.kt"
    click AU "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/util/ApkUtil.kt"
    click FU "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/util/FileUtil.kt"
    click DU "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/util/DensityUtil.kt"
    click LU "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/util/LogUtil.kt"
    click FP "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/config/AppUpdateFileProvider.kt"
    click CO "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/config/Constant.kt"
    click LI1 "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/listener/OnDownloadListener.kt"
    click LI2 "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/listener/OnDownloadListenerAdapter.kt"
    click LI3 "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/listener/OnButtonClickListener.kt"
    click LI4 "https://github.com/azhon/appupdate/blob/main/appupdate/src/main/java/com/azhon/appupdate/listener/LifecycleCallbacksAdapter.kt"

    %% Styles
    classDef ui fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef green fill:#90EE90,stroke:#333,stroke-width:1px
    classDef orange fill:#FFD580,stroke:#333,stroke-width:1px
    classDef gray fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef storage fill:#E0FFFF,stroke:#333,stroke-width:1px
    class title fill:#fff,stroke:none,font-weight:bold,font-size:16px
```
