From a666ebf74db5b2591a8c6a0654ec55c8482a8fd1 Mon Sep 17 00:00:00 2001 From: "DESKTOP-I3JPKHK\\wy" <1111> Date: Tue, 16 Sep 2025 17:59:27 +0800 Subject: [PATCH] first commit --- .gitignore | 33 + .metadata | 33 + CHANGELOG.md | 65 + LICENSE | 1 + README.md | 343 +++++ analysis_options.yaml | 4 + android/.gitignore | 9 + android/build.gradle | 66 + android/settings.gradle | 1 + android/src/main/AndroidManifest.xml | 3 + .../app_upgrade_plugin/AppUpgradePlugin.kt | 230 ++++ .../AppUpgradePluginTest.kt | 27 + devtools_options.yaml | 3 + example/.gitignore | 45 + example/README.md | 16 + example/analysis_options.yaml | 28 + example/android/.gitignore | 14 + example/android/app/build.gradle.kts | 49 + .../android/app/src/debug/AndroidManifest.xml | 7 + .../android/app/src/main/AndroidManifest.xml | 61 + .../MainActivity.kt | 31 + .../res/drawable-v21/launch_background.xml | 12 + .../main/res/drawable/launch_background.xml | 12 + .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 544 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 442 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 721 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 1031 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 1443 bytes .../app/src/main/res/values-night/styles.xml | 18 + .../app/src/main/res/values/styles.xml | 18 + .../app/src/main/res/xml/file_paths.xml | 13 + .../app/src/profile/AndroidManifest.xml | 7 + example/android/build.gradle.kts | 21 + example/android/gradle.properties | 3 + .../gradle/wrapper/gradle-wrapper.properties | 5 + example/android/settings.gradle.kts | 25 + example/devtools_options.yaml | 3 + .../plugin_integration_test.dart | 25 + example/ios/.gitignore | 34 + example/ios/Flutter/AppFrameworkInfo.plist | 26 + example/ios/Flutter/Debug.xcconfig | 1 + example/ios/Flutter/Release.xcconfig | 1 + example/ios/Runner.xcodeproj/project.pbxproj | 616 +++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../xcshareddata/WorkspaceSettings.xcsettings | 8 + .../xcshareddata/xcschemes/Runner.xcscheme | 101 ++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../xcshareddata/WorkspaceSettings.xcsettings | 8 + example/ios/Runner/AppDelegate.swift | 13 + .../AppIcon.appiconset/Contents.json | 122 ++ .../Icon-App-1024x1024@1x.png | Bin 0 -> 10932 bytes .../AppIcon.appiconset/Icon-App-20x20@1x.png | Bin 0 -> 295 bytes .../AppIcon.appiconset/Icon-App-20x20@2x.png | Bin 0 -> 406 bytes .../AppIcon.appiconset/Icon-App-20x20@3x.png | Bin 0 -> 450 bytes .../AppIcon.appiconset/Icon-App-29x29@1x.png | Bin 0 -> 282 bytes .../AppIcon.appiconset/Icon-App-29x29@2x.png | Bin 0 -> 462 bytes .../AppIcon.appiconset/Icon-App-29x29@3x.png | Bin 0 -> 704 bytes .../AppIcon.appiconset/Icon-App-40x40@1x.png | Bin 0 -> 406 bytes .../AppIcon.appiconset/Icon-App-40x40@2x.png | Bin 0 -> 586 bytes .../AppIcon.appiconset/Icon-App-40x40@3x.png | Bin 0 -> 862 bytes .../AppIcon.appiconset/Icon-App-60x60@2x.png | Bin 0 -> 862 bytes .../AppIcon.appiconset/Icon-App-60x60@3x.png | Bin 0 -> 1674 bytes .../AppIcon.appiconset/Icon-App-76x76@1x.png | Bin 0 -> 762 bytes .../AppIcon.appiconset/Icon-App-76x76@2x.png | Bin 0 -> 1226 bytes .../Icon-App-83.5x83.5@2x.png | Bin 0 -> 1418 bytes .../LaunchImage.imageset/Contents.json | 23 + .../LaunchImage.imageset/LaunchImage.png | Bin 0 -> 68 bytes .../LaunchImage.imageset/LaunchImage@2x.png | Bin 0 -> 68 bytes .../LaunchImage.imageset/LaunchImage@3x.png | Bin 0 -> 68 bytes .../LaunchImage.imageset/README.md | 5 + .../Runner/Base.lproj/LaunchScreen.storyboard | 37 + example/ios/Runner/Base.lproj/Main.storyboard | 26 + example/ios/Runner/Info.plist | 49 + example/ios/Runner/Runner-Bridging-Header.h | 1 + example/ios/RunnerTests/RunnerTests.swift | 27 + example/lib/main.dart | 147 +++ example/lib/main_enhanced.dart | 738 +++++++++++ example/lib/main_simple.dart | 165 +++ example/pubspec.lock | 741 +++++++++++ example/pubspec.yaml | 91 ++ example/test/widget_test.dart | 160 +++ ios/.gitignore | 38 + ios/Assets/.gitkeep | 0 ios/Classes/AppUpgradePlugin.swift | 50 + ios/Resources/PrivacyInfo.xcprivacy | 14 + ios/app_upgrade_plugin.podspec | 29 + lib/app_upgrade_plugin.dart | 125 ++ lib/app_upgrade_plugin_enhanced.dart | 638 +++++++++ lib/app_upgrade_plugin_method_channel.dart | 561 ++++++++ ...app_upgrade_plugin_platform_interface.dart | 75 ++ lib/app_upgrade_simple.dart | 1152 +++++++++++++++++ lib/core/cache_manager.dart | 495 +++++++ lib/core/download_manager.dart | 474 +++++++ lib/core/http_config.dart | 50 + lib/core/network_monitor.dart | 516 ++++++++ lib/core/notification_helper.dart | 108 ++ lib/core/permission_helper.dart | 309 +++++ lib/core/upgrade_config.dart | 232 ++++ lib/core/upgrade_utils.dart | 8 + lib/core/version_comparator.dart | 350 +++++ lib/models/app_market.dart | 118 ++ lib/models/upgrade_info.dart | 96 ++ lib/widgets/download_progress_dialog.dart | 178 +++ lib/widgets/market_selection_dialog.dart | 77 ++ lib/widgets/upgrade_dialog.dart | 195 +++ lib/widgets/widgets.dart | 2 + pubspec.yaml | 86 ++ ...pp_upgrade_plugin_method_channel_test.dart | 27 + test/app_upgrade_plugin_test.dart | 220 ++++ 111 files changed, 10624 insertions(+) create mode 100644 .gitignore create mode 100644 .metadata create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 analysis_options.yaml create mode 100644 android/.gitignore create mode 100644 android/build.gradle create mode 100644 android/settings.gradle create mode 100644 android/src/main/AndroidManifest.xml create mode 100644 android/src/main/kotlin/com/example/app_upgrade_plugin/AppUpgradePlugin.kt create mode 100644 android/src/test/kotlin/com/example/app_upgrade_plugin/AppUpgradePluginTest.kt create mode 100644 devtools_options.yaml create mode 100644 example/.gitignore create mode 100644 example/README.md create mode 100644 example/analysis_options.yaml create mode 100644 example/android/.gitignore create mode 100644 example/android/app/build.gradle.kts create mode 100644 example/android/app/src/debug/AndroidManifest.xml create mode 100644 example/android/app/src/main/AndroidManifest.xml create mode 100644 example/android/app/src/main/kotlin/com/example/app_upgrade_plugin_example/MainActivity.kt create mode 100644 example/android/app/src/main/res/drawable-v21/launch_background.xml create mode 100644 example/android/app/src/main/res/drawable/launch_background.xml create mode 100644 example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 example/android/app/src/main/res/values-night/styles.xml create mode 100644 example/android/app/src/main/res/values/styles.xml create mode 100644 example/android/app/src/main/res/xml/file_paths.xml create mode 100644 example/android/app/src/profile/AndroidManifest.xml create mode 100644 example/android/build.gradle.kts create mode 100644 example/android/gradle.properties create mode 100644 example/android/gradle/wrapper/gradle-wrapper.properties create mode 100644 example/android/settings.gradle.kts create mode 100644 example/devtools_options.yaml create mode 100644 example/integration_test/plugin_integration_test.dart create mode 100644 example/ios/.gitignore create mode 100644 example/ios/Flutter/AppFrameworkInfo.plist create mode 100644 example/ios/Flutter/Debug.xcconfig create mode 100644 example/ios/Flutter/Release.xcconfig create mode 100644 example/ios/Runner.xcodeproj/project.pbxproj create mode 100644 example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings create mode 100644 example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme create mode 100644 example/ios/Runner.xcworkspace/contents.xcworkspacedata create mode 100644 example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings create mode 100644 example/ios/Runner/AppDelegate.swift create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png create mode 100644 example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png create mode 100644 example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json create mode 100644 example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png create mode 100644 example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png create mode 100644 example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png create mode 100644 example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md create mode 100644 example/ios/Runner/Base.lproj/LaunchScreen.storyboard create mode 100644 example/ios/Runner/Base.lproj/Main.storyboard create mode 100644 example/ios/Runner/Info.plist create mode 100644 example/ios/Runner/Runner-Bridging-Header.h create mode 100644 example/ios/RunnerTests/RunnerTests.swift create mode 100644 example/lib/main.dart create mode 100644 example/lib/main_enhanced.dart create mode 100644 example/lib/main_simple.dart create mode 100644 example/pubspec.lock create mode 100644 example/pubspec.yaml create mode 100644 example/test/widget_test.dart create mode 100644 ios/.gitignore create mode 100644 ios/Assets/.gitkeep create mode 100644 ios/Classes/AppUpgradePlugin.swift create mode 100644 ios/Resources/PrivacyInfo.xcprivacy create mode 100644 ios/app_upgrade_plugin.podspec create mode 100644 lib/app_upgrade_plugin.dart create mode 100644 lib/app_upgrade_plugin_enhanced.dart create mode 100644 lib/app_upgrade_plugin_method_channel.dart create mode 100644 lib/app_upgrade_plugin_platform_interface.dart create mode 100644 lib/app_upgrade_simple.dart create mode 100644 lib/core/cache_manager.dart create mode 100644 lib/core/download_manager.dart create mode 100644 lib/core/http_config.dart create mode 100644 lib/core/network_monitor.dart create mode 100644 lib/core/notification_helper.dart create mode 100644 lib/core/permission_helper.dart create mode 100644 lib/core/upgrade_config.dart create mode 100644 lib/core/upgrade_utils.dart create mode 100644 lib/core/version_comparator.dart create mode 100644 lib/models/app_market.dart create mode 100644 lib/models/upgrade_info.dart create mode 100644 lib/widgets/download_progress_dialog.dart create mode 100644 lib/widgets/market_selection_dialog.dart create mode 100644 lib/widgets/upgrade_dialog.dart create mode 100644 lib/widgets/widgets.dart create mode 100644 pubspec.yaml create mode 100644 test/app_upgrade_plugin_method_channel_test.dart create mode 100644 test/app_upgrade_plugin_test.dart diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e7d347d --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.build/ +.buildlog/ +.history +.svn/ +.swiftpm/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +.flutter-plugins +.flutter-plugins-dependencies +build/ diff --git a/.metadata b/.metadata new file mode 100644 index 0000000..f35eb89 --- /dev/null +++ b/.metadata @@ -0,0 +1,33 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "be698c48a6750c8cb8e61c740ca9991bb947aba2" + channel: "stable" + +project_type: plugin + +# Tracks metadata for the flutter migrate command +migration: + platforms: + - platform: root + create_revision: be698c48a6750c8cb8e61c740ca9991bb947aba2 + base_revision: be698c48a6750c8cb8e61c740ca9991bb947aba2 + - platform: android + create_revision: be698c48a6750c8cb8e61c740ca9991bb947aba2 + base_revision: be698c48a6750c8cb8e61c740ca9991bb947aba2 + - platform: ios + create_revision: be698c48a6750c8cb8e61c740ca9991bb947aba2 + base_revision: be698c48a6750c8cb8e61c740ca9991bb947aba2 + + # User provided section + + # List of Local paths (relative to this file) that should be + # ignored by the migrate tool. + # + # Files that are not part of the templates will be ignored by default. + unmanaged_files: + - 'lib/main.dart' + - 'ios/Runner.xcodeproj/project.pbxproj' diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..07b808c --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,65 @@ +# Changelog + +## 1.1.0 - 易用性大幅提升 + +### 新增功能 +* 🎯 **AppUpgradeSimple** - 极简API,一行代码实现升级功能 +* 🛡️ **PermissionHelper** - 智能权限管理,自动处理Android各版本权限差异 +* 📱 **Android兼容性优化** - 完美支持Android 6.0到Android 14的所有版本 +* 🚀 **自动权限请求** - 根据Android版本自动请求相应权限 +* 💡 **更友好的权限提示** - 提供权限说明对话框,引导用户授权 + +### 改进优化 +* ✨ 简化使用流程,新手也能快速上手 +* ✨ 自动处理Android权限,无需手动申请 +* ✨ 智能判断Android版本,自动适配不同权限策略 +* ✨ 提供完整的权限状态查询接口 +* ✨ 优化权限被拒绝时的处理流程 + +### 文档更新 +* 📝 添加极简示例代码 +* 📝 完善Android权限说明 +* 📝 添加快速集成指南 +* 📝 更新API文档 + +## 1.0.0 - 增强版发布 + +### 新增功能 +* 🚀 **智能版本管理** - 支持语义化版本、数字版本、时间戳、构建号等多种比较策略 +* 🚀 **高性能下载管理器** - 实现断点续传、多线程下载、自动重试机制 +* 🚀 **网络状态监测** - 实时监测网络类型、质量,提供智能下载策略 +* 🚀 **智能缓存系统** - 多级缓存、过期管理、自动清理、多种缓存策略 +* 🚀 **文件完整性校验** - 支持MD5/SHA256校验,在Isolate中执行避免阻塞 +* 🚀 **内存优化** - 使用弱引用、自动资源释放、防止内存泄漏 +* 🚀 **静默更新** - 支持后台下载、WiFi自动下载 +* 🚀 **灵活配置** - 丰富的配置选项,支持运行时动态调整 + +### 优化改进 +* ⚡ 优化下载性能,支持智能分块和并发控制 +* ⚡ 改进错误处理,添加自动重试机制 +* ⚡ 优化内存使用,防止大文件下载时OOM +* ⚡ 改进网络请求,添加超时控制和代理支持 +* ⚡ 优化UI组件,提供更好的用户体验 + +### 架构改进 +* 📦 模块化设计,核心功能独立封装 +* 📦 单例模式管理,避免重复实例化 +* 📦 观察者模式处理状态变化 +* 📦 策略模式处理不同平台差异 + +### 开发体验 +* 📝 完善的API文档和使用示例 +* 📝 详细的配置说明 +* 📝 丰富的回调接口 +* 📝 完整的错误信息 + +## 0.0.1 - 初始版本 + +* 初始版本发布 +* 支持版本检查功能 +* 支持Android APK下载和安装 +* 支持iOS跳转App Store +* 提供升级对话框UI组件 +* 提供下载进度显示组件 +* 支持强制和非强制更新 +* 完整的错误处理机制 \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ba75c69 --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +TODO: Add your license here. diff --git a/README.md b/README.md new file mode 100644 index 0000000..258a8ef --- /dev/null +++ b/README.md @@ -0,0 +1,343 @@ +# App Upgrade Plugin + +一款功能强大且灵活的 Flutter 应用内更新插件,专为提供符合主流平台用户习惯的无缝升级体验而设计。插件提供了简化版和增强版两套 API,满足不同复杂度的使用需求。 + +## ✨ 核心特性 + +- **🎯 智能平台适配**:Android 支持完整的下载-安装流程,iOS 自动跳转 App Store +- **🔄 双模式更新**:非强制更新(后台下载)+ 强制更新(阻塞式对话框) +- **🛡️ 全面权限管理**:自动适配不同 Android 版本的存储、安装、通知权限 +- **📱 现代化 UI**:Material Design 风格对话框,支持暗色主题和自定义样式 +- **🌐 健壮网络层**:基于 Dio,支持断点续传、重试机制、证书配置 +- **🔒 安全可靠**:MD5/SHA256 文件校验,防止下载文件损坏 +- **🎨 高度可定制**:丰富的配置选项和回调接口 + +## 📦 安装 + +在 `pubspec.yaml` 中添加依赖: + +```yaml +dependencies: + app_upgrade_plugin: ^1.0.0 +``` + +## 🚀 快速开始 + +### 方式一:简化版 API(推荐新手) + +```dart +import 'package:app_upgrade_plugin/app_upgrade_plugin.dart'; + +// 一行代码实现完整升级流程 +void checkUpdate(BuildContext context) { + AppUpgradeSimple.instance.checkUpdate( + context: context, + url: 'https://your-api.com/check-update', + params: {'channel': 'release'}, // 可选参数 + showNoUpdateToast: true, + autoDownload: true, + autoInstall: false, + ); +} +``` + +### 方式二:分离式 API(更灵活) + +```dart +// 1. 检查更新(纯逻辑,不涉及UI) +final upgradeInfo = await AppUpgradeSimple.instance.checkUpdateSilent( + url: 'https://your-api.com/check-update', + params: {'platform': 'android'}, +); + +// 2. 根据需要显示UI +if (upgradeInfo != null && context.mounted) { + AppUpgradeSimple.instance.showUpgradeDialog( + context: context, + info: upgradeInfo, + autoDownload: true, + autoInstall: false, + ); +} +``` + +### 方式三:增强版 API(高级用户) + +```dart +final plugin = AppUpgradePluginEnhanced.instance; + +// 配置插件 +plugin.configure( + debugMode: true, + wifiOnly: false, + autoCheck: true, +); + +// 添加下载监听 +plugin.addDownloadCallback((task) { + print('下载进度: ${task.progress}%'); +}); + +// 智能检查更新 +final info = await plugin.checkUpdateSmart( + 'https://your-api.com/check-update' +); +``` + +## 🎨 UI 展示 + +插件提供多种精美的 UI 组件: + +- **版本更新对话框**:显示版本信息、更新内容、文件大小 +- **下载进度对话框**:实时显示下载进度和状态 +- **应用市场选择**:支持多应用商店选择(华为、小米、OPPO等) +- **权限申请对话框**:友好的权限说明和引导 + +## ⚙️ Android 配置 + +### 1. 权限配置 + +在 `android/app/src/main/AndroidManifest.xml` 中添加: + +```xml + + + + + + + + + + + + + + + + + + + +``` + +### 2. FileProvider 路径配置 + +创建 `android/app/src/main/res/xml/file_paths.xml`: + +```xml + + + + + + + + + + + + + +``` + +### 3. Gradle 配置(可选) + +在 `android/app/build.gradle` 中: + +```kotlin +android { + compileSdk 34 + + compileOptions { + sourceCompatibility JavaVersion.VERSION_11 + targetCompatibility JavaVersion.VERSION_11 + isCoreLibraryDesugaringEnabled = true + } +} + +dependencies { + coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.4") +} +``` + +## 📡 服务端 API 协议 + +你的版本检查接口需要返回以下格式的 JSON: + +```json +{ + "hasUpdate": true, + "isForceUpdate": false, + "versionCode": "101", + "versionName": "1.0.1", + "updateContent": "1. 修复了登录问题\n2. 优化了界面显示\n3. 提升了性能", + "downloadUrl": "https://your-cdn.com/app-release.apk", + "appStoreUrl": "https://apps.apple.com/app/id123456789", + "apkSize": 25165824, + "apkMd5": "d41d8cd98f00b204e9800998ecf8427e", + "appMarkets": [ + { + "name": "华为应用市场", + "packageName": "com.huawei.appmarket", + "url": "appmarket://details?id=com.yourapp.package" + }, + { + "name": "小米应用商店", + "packageName": "com.xiaomi.market", + "url": "mimarket://details?id=com.yourapp.package" + } + ] +} +``` + +### 字段说明 + +| 字段 | 类型 | 必需 | 说明 | +|------|------|------|------| +| `hasUpdate` | boolean | ✅ | 是否有更新 | +| `isForceUpdate` | boolean | ✅ | 是否强制更新 | +| `versionCode` | string | ✅ | 版本号(数字字符串,用于版本比较) | +| `versionName` | string | ✅ | 版本名称(显示给用户) | +| `updateContent` | string | ✅ | 更新内容描述 | +| `downloadUrl` | string | Android必需 | APK下载地址 | +| `appStoreUrl` | string | iOS必需 | App Store链接 | +| `apkSize` | number | ❌ | APK文件大小(字节) | +| `apkMd5` | string | ❌ | APK文件MD5校验值 | +| `appMarkets` | array | ❌ | 应用市场列表 | + +## 🔧 高级配置 + +### 网络配置 + +```dart +// 自动模式(推荐) +// Debug: 自动绕过证书验证 +// Release: 严格证书验证 + +// 手动配置 +AppUpgradePlugin().configureHttp(HttpConfig( + ignoreCertificate: false, // 是否忽略证书 + enableLog: true, // 启用网络日志 + connectTimeout: 30, // 连接超时(秒) + receiveTimeout: 60, // 接收超时(秒) + defaultMethod: 'GET', // 默认请求方法 + headers: { // 自定义请求头 + 'User-Agent': 'MyApp/1.0', + }, +)); +``` + +### 权限管理 + +插件提供了独立的权限管理工具: + +```dart +import 'package:app_upgrade_plugin/core/permission_helper.dart'; + +// 检查并请求存储权限 +final hasStorage = await PermissionHelper.checkAndRequestStoragePermission( + context: context, +); + +// 检查并请求安装权限 +final hasInstall = await PermissionHelper.checkAndRequestInstallPermission( + context: context, +); + +// 检查并请求通知权限 +final hasNotification = await PermissionHelper.checkAndRequestNotificationPermission( + context: context, +); +``` + +## 🐛 故障排除 + +### 常见问题 + +**Q: 安装失败,提示 "解析包时出现问题"** + +A: 检查以下几点: +- APK 文件是否完整下载(检查文件大小) +- APK 签名是否正确 +- 设备架构是否匹配(armeabi-v7a, arm64-v8a) + +**Q: 权限申请失败** + +A: 确保: +- AndroidManifest.xml 中已声明相应权限 +- FileProvider 配置正确 +- 在 MaterialApp 环境中调用权限申请 + +**Q: 下载失败或进度不更新** + +A: 检查: +- 网络连接是否正常 +- 下载URL是否可访问 +- 服务器是否支持断点续传 + +**Q: iOS 不跳转 App Store** + +A: 确认: +- `appStoreUrl` 字段格式正确 +- URL 为有效的 App Store 链接 + +### 调试技巧 + +开启调试模式以获取详细日志: + +```dart +// 简化版 +AppUpgradeSimple.instance.init(debugMode: true); + +// 增强版 +AppUpgradePluginEnhanced.instance.configure(debugMode: true); +``` + +## 📚 API 文档 + +### AppUpgradeSimple + +| 方法 | 描述 | +|------|------| +| `checkUpdate()` | 检查更新并显示UI | +| `checkUpdateSilent()` | 静默检查更新 | +| `showUpgradeDialog()` | 显示升级对话框 | + +### AppUpgradePluginEnhanced + +| 方法 | 描述 | +|------|------| +| `configure()` | 配置插件 | +| `checkUpdateSmart()` | 智能检查更新 | +| `downloadApkSmart()` | 智能下载APK | +| `installApkSmart()` | 智能安装APK | +| `addDownloadCallback()` | 添加下载监听 | + +### PermissionHelper + +| 方法 | 描述 | +|------|------| +| `checkAndRequestStoragePermission()` | 存储权限 | +| `checkAndRequestInstallPermission()` | 安装权限 | +| `checkAndRequestNotificationPermission()` | 通知权限 | + +## 🤝 贡献 + +欢迎提交 Issue 和 Pull Request! + +## 📄 许可证 + +MIT License + +## 🔗 相关链接 + +- [Flutter 官网](https://flutter.dev) +- [Android 应用安装权限文档](https://developer.android.com/reference/android/Manifest.permission#REQUEST_INSTALL_PACKAGES) +- [FileProvider 使用指南](https://developer.android.com/reference/androidx/core/content/FileProvider) \ No newline at end of file diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..a5744c1 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,4 @@ +include: package:flutter_lints/flutter.yaml + +# Additional information about this file can be found at +# https://dart.dev/guides/language/analysis-options diff --git a/android/.gitignore b/android/.gitignore new file mode 100644 index 0000000..161bdcd --- /dev/null +++ b/android/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.cxx diff --git a/android/build.gradle b/android/build.gradle new file mode 100644 index 0000000..65322be --- /dev/null +++ b/android/build.gradle @@ -0,0 +1,66 @@ +group = "com.example.app_upgrade_plugin" +version = "1.0-SNAPSHOT" + +buildscript { + ext.kotlin_version = "2.1.0" + repositories { + google() + mavenCentral() + } + + dependencies { + classpath("com.android.tools.build:gradle:8.7.3") + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") + } +} + +allprojects { + repositories { + google() + mavenCentral() + } +} + +apply plugin: "com.android.library" +apply plugin: "kotlin-android" + +android { + namespace = "com.example.app_upgrade_plugin" + + compileSdk = 35 + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + + kotlinOptions { + jvmTarget = JavaVersion.VERSION_11 + } + + sourceSets { + main.java.srcDirs += "src/main/kotlin" + test.java.srcDirs += "src/test/kotlin" + } + + defaultConfig { + minSdk = 21 + } + + dependencies { + testImplementation("org.jetbrains.kotlin:kotlin-test") + testImplementation("org.mockito:mockito-core:5.0.0") + } + + testOptions { + unitTests.all { + useJUnitPlatform() + + testLogging { + events "passed", "skipped", "failed", "standardOut", "standardError" + outputs.upToDateWhen {false} + showStandardStreams = true + } + } + } +} diff --git a/android/settings.gradle b/android/settings.gradle new file mode 100644 index 0000000..eda28fe --- /dev/null +++ b/android/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'app_upgrade_plugin' diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..cdebb58 --- /dev/null +++ b/android/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + diff --git a/android/src/main/kotlin/com/example/app_upgrade_plugin/AppUpgradePlugin.kt b/android/src/main/kotlin/com/example/app_upgrade_plugin/AppUpgradePlugin.kt new file mode 100644 index 0000000..ea1f6c4 --- /dev/null +++ b/android/src/main/kotlin/com/example/app_upgrade_plugin/AppUpgradePlugin.kt @@ -0,0 +1,230 @@ +package com.example.app_upgrade_plugin + +import android.app.Activity +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.os.Build +import android.provider.Settings +import androidx.core.content.FileProvider +import io.flutter.embedding.engine.plugins.FlutterPlugin +import io.flutter.embedding.engine.plugins.activity.ActivityAware +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding +import io.flutter.plugin.common.MethodCall +import io.flutter.plugin.common.MethodChannel +import io.flutter.plugin.common.MethodChannel.MethodCallHandler +import io.flutter.plugin.common.MethodChannel.Result +import java.io.File +import java.io.FileInputStream +import java.math.BigInteger +import java.security.MessageDigest +import android.os.Environment + +/** AppUpgradePlugin */ +class AppUpgradePlugin: FlutterPlugin, MethodCallHandler, ActivityAware { + /// The MethodChannel that will the communication between Flutter and native Android + /// + /// This local reference serves to register the plugin with the Flutter Engine and unregister it + /// when the Flutter Engine is detached from the Activity + private lateinit var channel : MethodChannel + private lateinit var context: Context + private var activity: Activity? = null + private val REQUEST_INSTALL_PACKAGES = 1001 + + override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { + channel = MethodChannel(flutterPluginBinding.binaryMessenger, "app_upgrade_plugin") + channel.setMethodCallHandler(this) + context = flutterPluginBinding.applicationContext + } + + override fun onMethodCall(call: MethodCall, result: Result) { + when (call.method) { + "getPlatformVersion" -> { + result.success("Android ${android.os.Build.VERSION.RELEASE}") + } + "installApk" -> { + val filePath = call.argument("filePath") + if (filePath != null) { + installApk(filePath, result) + } else { + result.error("INVALID_ARGUMENT", "File path is required", null) + } + } + "getDownloadPath" -> { + val downloadDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + result.success(downloadDir.path) + } + "checkApkExists" -> { + val version = call.argument("version") + val md5 = call.argument("md5") + if (version == null) { + result.success(false) + return + } + + val downloadDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + val fileName = "app-upgrade-$version.apk" + val file = File(downloadDir, fileName) + + if (!file.exists()) { + result.success(false) + return + } + + if (md5 == null) { + // File exists and no MD5 check required + result.success(true) + return + } + + // MD5 check + try { + val calculatedMd5 = calculateMD5(file) + result.success(calculatedMd5.equals(md5, ignoreCase = true)) + } catch (e: Exception) { + result.error("MD5_ERROR", "Failed to calculate MD5", e.message) + } + } + "goToMarket" -> { + val packageName = call.argument("packageName") + val marketPackage = call.argument("marketPackage") + val url = call.argument("url") + goToMarket(packageName, marketPackage, url, result) + } + "goToAppStore" -> { + val url = call.argument("url") + if (url != null) { + goToAppStore(url) + result.success(true) + } else { + result.error("INVALID_ARGUMENT", "URL is null", null) + } + } + "getAndroidSdkVersion" -> { + result.success(Build.VERSION.SDK_INT) + } + else -> { + result.notImplemented() + } + } + } + + private fun installApk(filePath: String, result: Result) { + try { + val file = File(filePath) + if (!file.exists()) { + result.error("FILE_NOT_FOUND", "APK file not found at: $filePath", null) + return + } + + // Check if we have permission to install unknown apps (Android 8.0+) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (!context.packageManager.canRequestPackageInstalls()) { + result.error("PERMISSION_DENIED", "No permission to install unknown apps. Please grant permission in settings.", null) + return + } + } + + val intent = Intent(Intent.ACTION_VIEW) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + + val uri: Uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + // Android 7.0及以上使用FileProvider + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + try { + val authority = "${context.packageName}.fileprovider" + FileProvider.getUriForFile(context, authority, file) + } catch (e: IllegalArgumentException) { + // If FileProvider fails, try with a more generic authority + result.error("FILEPROVIDER_ERROR", "FileProvider configuration error. Please check AndroidManifest.xml", e.message) + return + } + } else { + // Android 6.0及以下直接使用file:// + Uri.fromFile(file) + } + + intent.setDataAndType(uri, "application/vnd.android.package-archive") + + // Try to start the install intent + try { + context.startActivity(intent) + result.success(true) + } catch (e: Exception) { + result.error("INTENT_ERROR", "Failed to start install intent", e.message) + } + + } catch (e: Exception) { + result.error("INSTALL_FAILED", "Install failed: ${e.message}", null) + } + } + + private fun goToMarket(packageName: String?, marketPackage: String?, url: String?, result: MethodChannel.Result) { + if (activity == null) { + result.success(false) + return + } + + try { + val finalPackageName = packageName ?: activity!!.packageName + val intent = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$finalPackageName")) + if (marketPackage != null) { + intent.setPackage(marketPackage) + } + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + activity!!.startActivity(intent) + result.success(true) + } catch (e: Exception) { + // 如果没有安装对应的应用市场,则通过URL跳转 + if (url != null) { + goToAppStore(url) + result.success(true) + } else { + result.error("MARKET_ERROR", "Failed to open app market", e.message) + } + } + } + + private fun goToAppStore(url: String) { + val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + activity?.startActivity(intent) + } + + private fun calculateMD5(file: File): String { + val digest = MessageDigest.getInstance("MD5") + val inputStream = FileInputStream(file) + val buffer = ByteArray(8192) + var read: Int + while (inputStream.read(buffer).also { read = it } > 0) { + digest.update(buffer, 0, read) + } + inputStream.close() + val md5sum: ByteArray = digest.digest() + val bigInt = BigInteger(1, md5sum) + var output = bigInt.toString(16) + // Fill to 32 chars + output = String.format("%32s", output).replace(' ', '0') + return output + } + + override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { + channel.setMethodCallHandler(null) + } + + override fun onAttachedToActivity(binding: ActivityPluginBinding) { + activity = binding.activity + } + + override fun onDetachedFromActivityForConfigChanges() { + activity = null + } + + override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { + activity = binding.activity + } + + override fun onDetachedFromActivity() { + activity = null + } +} \ No newline at end of file diff --git a/android/src/test/kotlin/com/example/app_upgrade_plugin/AppUpgradePluginTest.kt b/android/src/test/kotlin/com/example/app_upgrade_plugin/AppUpgradePluginTest.kt new file mode 100644 index 0000000..348ad3b --- /dev/null +++ b/android/src/test/kotlin/com/example/app_upgrade_plugin/AppUpgradePluginTest.kt @@ -0,0 +1,27 @@ +package com.example.app_upgrade_plugin + +import io.flutter.plugin.common.MethodCall +import io.flutter.plugin.common.MethodChannel +import kotlin.test.Test +import org.mockito.Mockito + +/* + * This demonstrates a simple unit test of the Kotlin portion of this plugin's implementation. + * + * Once you have built the plugin's example app, you can run these tests from the command + * line by running `./gradlew testDebugUnitTest` in the `example/android/` directory, or + * you can run them directly from IDEs that support JUnit such as Android Studio. + */ + +internal class AppUpgradePluginTest { + @Test + fun onMethodCall_getPlatformVersion_returnsExpectedValue() { + val plugin = AppUpgradePlugin() + + val call = MethodCall("getPlatformVersion", null) + val mockResult: MethodChannel.Result = Mockito.mock(MethodChannel.Result::class.java) + plugin.onMethodCall(call, mockResult) + + Mockito.verify(mockResult).success("Android " + android.os.Build.VERSION.RELEASE) + } +} diff --git a/devtools_options.yaml b/devtools_options.yaml new file mode 100644 index 0000000..fa0b357 --- /dev/null +++ b/devtools_options.yaml @@ -0,0 +1,3 @@ +description: This file stores settings for Dart & Flutter DevTools. +documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states +extensions: diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..79c113f --- /dev/null +++ b/example/.gitignore @@ -0,0 +1,45 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.build/ +.buildlog/ +.history +.svn/ +.swiftpm/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +**/ios/Flutter/.last_build_id +.dart_tool/ +.flutter-plugins +.flutter-plugins-dependencies +.pub-cache/ +.pub/ +/build/ + +# Symbolication related +app.*.symbols + +# Obfuscation related +app.*.map.json + +# Android Studio will place build artifacts here +/android/app/debug +/android/app/profile +/android/app/release diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..a88916e --- /dev/null +++ b/example/README.md @@ -0,0 +1,16 @@ +# app_upgrade_plugin_example + +Demonstrates how to use the app_upgrade_plugin plugin. + +## Getting Started + +This project is a starting point for a Flutter application. + +A few resources to get you started if this is your first Flutter project: + +- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab) +- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook) + +For help getting started with Flutter development, view the +[online documentation](https://docs.flutter.dev/), which offers tutorials, +samples, guidance on mobile development, and a full API reference. diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml new file mode 100644 index 0000000..0d29021 --- /dev/null +++ b/example/analysis_options.yaml @@ -0,0 +1,28 @@ +# This file configures the analyzer, which statically analyzes Dart code to +# check for errors, warnings, and lints. +# +# The issues identified by the analyzer are surfaced in the UI of Dart-enabled +# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be +# invoked from the command line by running `flutter analyze`. + +# The following line activates a set of recommended lints for Flutter apps, +# packages, and plugins designed to encourage good coding practices. +include: package:flutter_lints/flutter.yaml + +linter: + # The lint rules applied to this project can be customized in the + # section below to disable rules from the `package:flutter_lints/flutter.yaml` + # included above or to enable additional rules. A list of all available lints + # and their documentation is published at https://dart.dev/lints. + # + # Instead of disabling a lint rule for the entire project in the + # section below, it can also be suppressed for a single line of code + # or a specific dart file by using the `// ignore: name_of_lint` and + # `// ignore_for_file: name_of_lint` syntax on the line or in the file + # producing the lint. + rules: + # avoid_print: false # Uncomment to disable the `avoid_print` rule + # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule + +# Additional information about this file can be found at +# https://dart.dev/guides/language/analysis-options diff --git a/example/android/.gitignore b/example/android/.gitignore new file mode 100644 index 0000000..be3943c --- /dev/null +++ b/example/android/.gitignore @@ -0,0 +1,14 @@ +gradle-wrapper.jar +/.gradle +/captures/ +/gradlew +/gradlew.bat +/local.properties +GeneratedPluginRegistrant.java +.cxx/ + +# Remember to never publicly share your keystore. +# See https://flutter.dev/to/reference-keystore +key.properties +**/*.keystore +**/*.jks diff --git a/example/android/app/build.gradle.kts b/example/android/app/build.gradle.kts new file mode 100644 index 0000000..52ab8fb --- /dev/null +++ b/example/android/app/build.gradle.kts @@ -0,0 +1,49 @@ +plugins { + id("com.android.application") + id("kotlin-android") + // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins. + id("dev.flutter.flutter-gradle-plugin") +} + +android { + namespace = "com.example.app_upgrade_plugin_example" + compileSdk = flutter.compileSdkVersion + ndkVersion = "27.0.12077973" + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + isCoreLibraryDesugaringEnabled = true + } + + kotlinOptions { + jvmTarget = JavaVersion.VERSION_11.toString() + } + + defaultConfig { + // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). + applicationId = "com.example.app_upgrade_plugin_example" + // You can update the following values to match your application needs. + // For more information, see: https://flutter.dev/to/review-gradle-config. + minSdk = flutter.minSdkVersion + targetSdk = flutter.targetSdkVersion + versionCode = flutter.versionCode + versionName = flutter.versionName + } + + buildTypes { + release { + // TODO: Add your own signing config for the release build. + // Signing with the debug keys for now, so `flutter run --release` works. + signingConfig = signingConfigs.getByName("debug") + } + } +} + +flutter { + source = "../.." +} + +dependencies { + coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.4") +} diff --git a/example/android/app/src/debug/AndroidManifest.xml b/example/android/app/src/debug/AndroidManifest.xml new file mode 100644 index 0000000..399f698 --- /dev/null +++ b/example/android/app/src/debug/AndroidManifest.xml @@ -0,0 +1,7 @@ + + + + diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..f2d1d7d --- /dev/null +++ b/example/android/app/src/main/AndroidManifest.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/android/app/src/main/kotlin/com/example/app_upgrade_plugin_example/MainActivity.kt b/example/android/app/src/main/kotlin/com/example/app_upgrade_plugin_example/MainActivity.kt new file mode 100644 index 0000000..2515aef --- /dev/null +++ b/example/android/app/src/main/kotlin/com/example/app_upgrade_plugin_example/MainActivity.kt @@ -0,0 +1,31 @@ +package com.example.app_upgrade_plugin_example + +import io.flutter.embedding.android.FlutterActivity +import io.flutter.embedding.engine.FlutterEngine +import io.flutter.plugin.common.MethodChannel + +class MainActivity: FlutterActivity() { + private val CHANNEL = "app_upgrade_plugin_channel" // 与Dart端一致 + + override fun configureFlutterEngine(flutterEngine: FlutterEngine) { + super.configureFlutterEngine(flutterEngine) + MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> + // 可在此处理来自Dart的调用 + } + } + + override fun onResume() { + super.onResume() + // 处理通知点击启动 + intent?.getStringExtra("onNotificationClick")?.let { payload -> + if (payload.startsWith("download_complete:")) { + val filePath = payload.substringAfter("download_complete:") + // 通过channel通知Dart层执行安装 + MethodChannel(flutterEngine!!.dartExecutor.binaryMessenger, CHANNEL) + .invokeMethod("installApkFromNotification", filePath) + } + // 清除,防止重复触发 + intent.removeExtra("onNotificationClick") + } + } +} diff --git a/example/android/app/src/main/res/drawable-v21/launch_background.xml b/example/android/app/src/main/res/drawable-v21/launch_background.xml new file mode 100644 index 0000000..f74085f --- /dev/null +++ b/example/android/app/src/main/res/drawable-v21/launch_background.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/example/android/app/src/main/res/drawable/launch_background.xml b/example/android/app/src/main/res/drawable/launch_background.xml new file mode 100644 index 0000000..304732f --- /dev/null +++ b/example/android/app/src/main/res/drawable/launch_background.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png b/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000000000000000000000000000000000000..db77bb4b7b0906d62b1847e87f15cdcacf6a4f29 GIT binary patch literal 544 zcmeAS@N?(olHy`uVBq!ia0vp^9w5xY3?!3`olAj~WQl7;NpOBzNqJ&XDuZK6ep0G} zXKrG8YEWuoN@d~6R2!h8bpbvhu0Wd6uZuB!w&u2PAxD2eNXD>P5D~Wn-+_Wa#27Xc zC?Zj|6r#X(-D3u$NCt}(Ms06KgJ4FxJVv{GM)!I~&n8Bnc94O7-Hd)cjDZswgC;Qs zO=b+9!WcT8F?0rF7!Uys2bs@gozCP?z~o%U|N3vA*22NaGQG zlg@K`O_XuxvZ&Ks^m&R!`&1=spLvfx7oGDKDwpwW`#iqdw@AL`7MR}m`rwr|mZgU`8P7SBkL78fFf!WnuYWm$5Z0 zNXhDbCv&49sM544K|?c)WrFfiZvCi9h0O)B3Pgg&ebxsLQ05GG~ AQ2+n{ literal 0 HcmV?d00001 diff --git a/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png b/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000000000000000000000000000000000000..17987b79bb8a35cc66c3c1fd44f5a5526c1b78be GIT binary patch literal 442 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA3?vioaBc-sk|nMYCBgY=CFO}lsSJ)O`AMk? zp1FzXsX?iUDV2pMQ*D5Xx&nMcT!A!W`0S9QKQy;}1Cl^CgaH=;G9cpY;r$Q>i*pfB zP2drbID<_#qf;rPZx^FqH)F_D#*k@@q03KywUtLX8Ua?`H+NMzkczFPK3lFz@i_kW%1NOn0|D2I9n9wzH8m|-tHjsw|9>@K=iMBhxvkv6m8Y-l zytQ?X=U+MF$@3 zt`~i=@j|6y)RWMK--}M|=T`o&^Ni>IoWKHEbBXz7?A@mgWoL>!*SXo`SZH-*HSdS+ yn*9;$7;m`l>wYBC5bq;=U}IMqLzqbYCidGC!)_gkIk_C@Uy!y&wkt5C($~2D>~)O*cj@FGjOCM)M>_ixfudOh)?xMu#Fs z#}Y=@YDTwOM)x{K_j*Q;dPdJ?Mz0n|pLRx{4n|)f>SXlmV)XB04CrSJn#dS5nK2lM zrZ9#~WelCp7&e13Y$jvaEXHskn$2V!!DN-nWS__6T*l;H&Fopn?A6HZ-6WRLFP=R` zqG+CE#d4|IbyAI+rJJ`&x9*T`+a=p|0O(+s{UBcyZdkhj=yS1>AirP+0R;mf2uMgM zC}@~JfByORAh4SyRgi&!(cja>F(l*O+nd+@4m$|6K6KDn_&uvCpV23&>G9HJp{xgg zoq1^2_p9@|WEo z*X_Uko@K)qYYv~>43eQGMdbiGbo>E~Q& zrYBH{QP^@Sti!`2)uG{irBBq@y*$B zi#&(U-*=fp74j)RyIw49+0MRPMRU)+a2r*PJ$L5roHt2$UjExCTZSbq%V!HeS7J$N zdG@vOZB4v_lF7Plrx+hxo7(fCV&}fHq)$ literal 0 HcmV?d00001 diff --git a/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000000000000000000000000000000000000..d5f1c8d34e7a88e3f88bea192c3a370d44689c3c GIT binary patch literal 1031 zcmeAS@N?(olHy`uVBq!ia0vp^6F``Q8Ax83A=Cw=BuiW)N`mv#O3D+9QW+dm@{>{( zJaZG%Q-e|yQz{EjrrIztFa`(sgt!6~Yi|1%a`XoT0ojZ}lNrNjb9xjc(B0U1_% zz5^97Xt*%oq$rQy4?0GKNfJ44uvxI)gC`h-NZ|&0-7(qS@?b!5r36oQ}zyZrNO3 zMO=Or+<~>+A&uN&E!^Sl+>xE!QC-|oJv`ApDhqC^EWD|@=#J`=d#Xzxs4ah}w&Jnc z$|q_opQ^2TrnVZ0o~wh<3t%W&flvYGe#$xqda2bR_R zvPYgMcHgjZ5nSA^lJr%;<&0do;O^tDDh~=pIxA#coaCY>&N%M2^tq^U%3DB@ynvKo}b?yu-bFc-u0JHzced$sg7S3zqI(2 z#Km{dPr7I=pQ5>FuK#)QwK?Y`E`B?nP+}U)I#c1+FM*1kNvWG|a(TpksZQ3B@sD~b zpQ2)*V*TdwjFOtHvV|;OsiDqHi=6%)o4b!)x$)%9pGTsE z-JL={-Ffv+T87W(Xpooq<`r*VzWQcgBN$$`u}f>-ZQI1BB8ykN*=e4rIsJx9>z}*o zo~|9I;xof literal 0 HcmV?d00001 diff --git a/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000000000000000000000000000000000000..4d6372eebdb28e45604e46eeda8dd24651419bc0 GIT binary patch literal 1443 zcmb`G{WsKk6vsdJTdFg%tJav9_E4vzrOaqkWF|A724Nly!y+?N9`YV6wZ}5(X(D_N(?!*n3`|_r0Hc?=PQw&*vnU?QTFY zB_MsH|!j$PP;I}?dppoE_gA(4uc!jV&0!l7_;&p2^pxNo>PEcNJv za5_RT$o2Mf!<+r?&EbHH6nMoTsDOa;mN(wv8RNsHpG)`^ymG-S5By8=l9iVXzN_eG%Xg2@Xeq76tTZ*dGh~Lo9vl;Zfs+W#BydUw zCkZ$o1LqWQO$FC9aKlLl*7x9^0q%0}$OMlp@Kk_jHXOjofdePND+j!A{q!8~Jn+s3 z?~~w@4?egS02}8NuulUA=L~QQfm;MzCGd)XhiftT;+zFO&JVyp2mBww?;QByS_1w! zrQlx%{^cMj0|Bo1FjwY@Q8?Hx0cIPF*@-ZRFpPc#bBw{5@tD(5%sClzIfl8WU~V#u zm5Q;_F!wa$BSpqhN>W@2De?TKWR*!ujY;Yylk_X5#~V!L*Gw~;$%4Q8~Mad z@`-kG?yb$a9cHIApZDVZ^U6Xkp<*4rU82O7%}0jjHlK{id@?-wpN*fCHXyXh(bLt* zPc}H-x0e4E&nQ>y%B-(EL=9}RyC%MyX=upHuFhAk&MLbsF0LP-q`XnH78@fT+pKPW zu72MW`|?8ht^tz$iC}ZwLp4tB;Q49K!QCF3@!iB1qOI=?w z7In!}F~ij(18UYUjnbmC!qKhPo%24?8U1x{7o(+?^Zu0Hx81|FuS?bJ0jgBhEMzf< zCgUq7r2OCB(`XkKcN-TL>u5y#dD6D!)5W?`O5)V^>jb)P)GBdy%t$uUMpf$SNV31$ zb||OojAbvMP?T@$h_ZiFLFVHDmbyMhJF|-_)HX3%m=CDI+ID$0^C>kzxprBW)hw(v zr!Gmda);ICoQyhV_oP5+C%?jcG8v+D@9f?Dk*!BxY}dazmrT@64UrP3hlslANK)bq z$67n83eh}OeW&SV@HG95P|bjfqJ7gw$e+`Hxo!4cx`jdK1bJ>YDSpGKLPZ^1cv$ek zIB?0S<#tX?SJCLWdMd{-ME?$hc7A$zBOdIJ)4!KcAwb=VMov)nK;9z>x~rfT1>dS+ zZ6#`2v@`jgbqq)P22H)Tx2CpmM^o1$B+xT6`(v%5xJ(?j#>Q$+rx_R|7TzDZe{J6q zG1*EcU%tE?!kO%^M;3aM6JN*LAKUVb^xz8-Pxo#jR5(-KBeLJvA@-gxNHx0M-ZJLl z;#JwQoh~9V?`UVo#}{6ka@II>++D@%KqGpMdlQ}?9E*wFcf5(#XQnP$Dk5~%iX^>f z%$y;?M0BLp{O3a(-4A?ewryHrrD%cx#Q^%KY1H zNre$ve+vceSLZcNY4U(RBX&)oZn*Py()h)XkE?PL$!bNb{N5FVI2Y%LKEm%yvpyTP z(1P?z~7YxD~Rf<(a@_y` literal 0 HcmV?d00001 diff --git a/example/android/app/src/main/res/values-night/styles.xml b/example/android/app/src/main/res/values-night/styles.xml new file mode 100644 index 0000000..06952be --- /dev/null +++ b/example/android/app/src/main/res/values-night/styles.xml @@ -0,0 +1,18 @@ + + + + + + + diff --git a/example/android/app/src/main/res/values/styles.xml b/example/android/app/src/main/res/values/styles.xml new file mode 100644 index 0000000..cb1ef88 --- /dev/null +++ b/example/android/app/src/main/res/values/styles.xml @@ -0,0 +1,18 @@ + + + + + + + diff --git a/example/android/app/src/main/res/xml/file_paths.xml b/example/android/app/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000..34b8a79 --- /dev/null +++ b/example/android/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/android/app/src/profile/AndroidManifest.xml b/example/android/app/src/profile/AndroidManifest.xml new file mode 100644 index 0000000..399f698 --- /dev/null +++ b/example/android/app/src/profile/AndroidManifest.xml @@ -0,0 +1,7 @@ + + + + diff --git a/example/android/build.gradle.kts b/example/android/build.gradle.kts new file mode 100644 index 0000000..89176ef --- /dev/null +++ b/example/android/build.gradle.kts @@ -0,0 +1,21 @@ +allprojects { + repositories { + google() + mavenCentral() + } +} + +val newBuildDir: Directory = rootProject.layout.buildDirectory.dir("../../build").get() +rootProject.layout.buildDirectory.value(newBuildDir) + +subprojects { + val newSubprojectBuildDir: Directory = newBuildDir.dir(project.name) + project.layout.buildDirectory.value(newSubprojectBuildDir) +} +subprojects { + project.evaluationDependsOn(":app") +} + +tasks.register("clean") { + delete(rootProject.layout.buildDirectory) +} diff --git a/example/android/gradle.properties b/example/android/gradle.properties new file mode 100644 index 0000000..f018a61 --- /dev/null +++ b/example/android/gradle.properties @@ -0,0 +1,3 @@ +org.gradle.jvmargs=-Xmx8G -XX:MaxMetaspaceSize=4G -XX:ReservedCodeCacheSize=512m -XX:+HeapDumpOnOutOfMemoryError +android.useAndroidX=true +android.enableJetifier=true diff --git a/example/android/gradle/wrapper/gradle-wrapper.properties b/example/android/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..ac3b479 --- /dev/null +++ b/example/android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-all.zip diff --git a/example/android/settings.gradle.kts b/example/android/settings.gradle.kts new file mode 100644 index 0000000..ab39a10 --- /dev/null +++ b/example/android/settings.gradle.kts @@ -0,0 +1,25 @@ +pluginManagement { + val flutterSdkPath = run { + val properties = java.util.Properties() + file("local.properties").inputStream().use { properties.load(it) } + val flutterSdkPath = properties.getProperty("flutter.sdk") + require(flutterSdkPath != null) { "flutter.sdk not set in local.properties" } + flutterSdkPath + } + + includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") + + repositories { + google() + mavenCentral() + gradlePluginPortal() + } +} + +plugins { + id("dev.flutter.flutter-plugin-loader") version "1.0.0" + id("com.android.application") version "8.7.3" apply false + id("org.jetbrains.kotlin.android") version "2.1.0" apply false +} + +include(":app") diff --git a/example/devtools_options.yaml b/example/devtools_options.yaml new file mode 100644 index 0000000..fa0b357 --- /dev/null +++ b/example/devtools_options.yaml @@ -0,0 +1,3 @@ +description: This file stores settings for Dart & Flutter DevTools. +documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states +extensions: diff --git a/example/integration_test/plugin_integration_test.dart b/example/integration_test/plugin_integration_test.dart new file mode 100644 index 0000000..420d72b --- /dev/null +++ b/example/integration_test/plugin_integration_test.dart @@ -0,0 +1,25 @@ +// This is a basic Flutter integration test. +// +// Since integration tests run in a full Flutter application, they can interact +// with the host side of a plugin implementation, unlike Dart unit tests. +// +// For more information about Flutter integration tests, please see +// https://flutter.dev/to/integration-testing + + +import 'package:flutter_test/flutter_test.dart'; +import 'package:integration_test/integration_test.dart'; + +import 'package:app_upgrade_plugin/app_upgrade_plugin.dart'; + +void main() { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + testWidgets('getPlatformVersion test', (WidgetTester tester) async { + final AppUpgradePlugin plugin = AppUpgradePlugin(); + final String? version = await plugin.getPlatformVersion(); + // The version string depends on the host platform running the test, so + // just assert that some non-empty string is returned. + expect(version?.isNotEmpty, true); + }); +} diff --git a/example/ios/.gitignore b/example/ios/.gitignore new file mode 100644 index 0000000..7a7f987 --- /dev/null +++ b/example/ios/.gitignore @@ -0,0 +1,34 @@ +**/dgph +*.mode1v3 +*.mode2v3 +*.moved-aside +*.pbxuser +*.perspectivev3 +**/*sync/ +.sconsign.dblite +.tags* +**/.vagrant/ +**/DerivedData/ +Icon? +**/Pods/ +**/.symlinks/ +profile +xcuserdata +**/.generated/ +Flutter/App.framework +Flutter/Flutter.framework +Flutter/Flutter.podspec +Flutter/Generated.xcconfig +Flutter/ephemeral/ +Flutter/app.flx +Flutter/app.zip +Flutter/flutter_assets/ +Flutter/flutter_export_environment.sh +ServiceDefinitions.json +Runner/GeneratedPluginRegistrant.* + +# Exceptions to above rules. +!default.mode1v3 +!default.mode2v3 +!default.pbxuser +!default.perspectivev3 diff --git a/example/ios/Flutter/AppFrameworkInfo.plist b/example/ios/Flutter/AppFrameworkInfo.plist new file mode 100644 index 0000000..7c56964 --- /dev/null +++ b/example/ios/Flutter/AppFrameworkInfo.plist @@ -0,0 +1,26 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + App + CFBundleIdentifier + io.flutter.flutter.app + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + App + CFBundlePackageType + FMWK + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1.0 + MinimumOSVersion + 12.0 + + diff --git a/example/ios/Flutter/Debug.xcconfig b/example/ios/Flutter/Debug.xcconfig new file mode 100644 index 0000000..592ceee --- /dev/null +++ b/example/ios/Flutter/Debug.xcconfig @@ -0,0 +1 @@ +#include "Generated.xcconfig" diff --git a/example/ios/Flutter/Release.xcconfig b/example/ios/Flutter/Release.xcconfig new file mode 100644 index 0000000..592ceee --- /dev/null +++ b/example/ios/Flutter/Release.xcconfig @@ -0,0 +1 @@ +#include "Generated.xcconfig" diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj new file mode 100644 index 0000000..1ceb01c --- /dev/null +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -0,0 +1,616 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 54; + objects = { + +/* Begin PBXBuildFile section */ + 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; }; + 331C808B294A63AB00263BE5 /* RunnerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 331C807B294A618700263BE5 /* RunnerTests.swift */; }; + 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; }; + 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; }; + 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; + 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; }; + 97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; }; +/* End PBXBuildFile section */ + +/* Begin PBXContainerItemProxy section */ + 331C8085294A63A400263BE5 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 97C146E61CF9000F007C117D /* Project object */; + proxyType = 1; + remoteGlobalIDString = 97C146ED1CF9000F007C117D; + remoteInfo = Runner; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 9705A1C41CF9048500538489 /* Embed Frameworks */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + ); + name = "Embed Frameworks"; + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = ""; }; + 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = ""; }; + 331C807B294A618700263BE5 /* RunnerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RunnerTests.swift; sourceTree = ""; }; + 331C8081294A63A400263BE5 /* RunnerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; + 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = ""; }; + 74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + 7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = ""; }; + 9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = ""; }; + 9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = ""; }; + 97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 97C146FB1CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + 97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + 97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 97C146EB1CF9000F007C117D /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 331C8082294A63A400263BE5 /* RunnerTests */ = { + isa = PBXGroup; + children = ( + 331C807B294A618700263BE5 /* RunnerTests.swift */, + ); + path = RunnerTests; + sourceTree = ""; + }; + 9740EEB11CF90186004384FC /* Flutter */ = { + isa = PBXGroup; + children = ( + 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */, + 9740EEB21CF90195004384FC /* Debug.xcconfig */, + 7AFA3C8E1D35360C0083082E /* Release.xcconfig */, + 9740EEB31CF90195004384FC /* Generated.xcconfig */, + ); + name = Flutter; + sourceTree = ""; + }; + 97C146E51CF9000F007C117D = { + isa = PBXGroup; + children = ( + 9740EEB11CF90186004384FC /* Flutter */, + 97C146F01CF9000F007C117D /* Runner */, + 97C146EF1CF9000F007C117D /* Products */, + 331C8082294A63A400263BE5 /* RunnerTests */, + ); + sourceTree = ""; + }; + 97C146EF1CF9000F007C117D /* Products */ = { + isa = PBXGroup; + children = ( + 97C146EE1CF9000F007C117D /* Runner.app */, + 331C8081294A63A400263BE5 /* RunnerTests.xctest */, + ); + name = Products; + sourceTree = ""; + }; + 97C146F01CF9000F007C117D /* Runner */ = { + isa = PBXGroup; + children = ( + 97C146FA1CF9000F007C117D /* Main.storyboard */, + 97C146FD1CF9000F007C117D /* Assets.xcassets */, + 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */, + 97C147021CF9000F007C117D /* Info.plist */, + 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */, + 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */, + 74858FAE1ED2DC5600515810 /* AppDelegate.swift */, + 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */, + ); + path = Runner; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 331C8080294A63A400263BE5 /* RunnerTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = 331C8087294A63A400263BE5 /* Build configuration list for PBXNativeTarget "RunnerTests" */; + buildPhases = ( + 331C807D294A63A400263BE5 /* Sources */, + 331C807F294A63A400263BE5 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + 331C8086294A63A400263BE5 /* PBXTargetDependency */, + ); + name = RunnerTests; + productName = RunnerTests; + productReference = 331C8081294A63A400263BE5 /* RunnerTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; + 97C146ED1CF9000F007C117D /* Runner */ = { + isa = PBXNativeTarget; + buildConfigurationList = 97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */; + buildPhases = ( + 9740EEB61CF901F6004384FC /* Run Script */, + 97C146EA1CF9000F007C117D /* Sources */, + 97C146EB1CF9000F007C117D /* Frameworks */, + 97C146EC1CF9000F007C117D /* Resources */, + 9705A1C41CF9048500538489 /* Embed Frameworks */, + 3B06AD1E1E4923F5004D2608 /* Thin Binary */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Runner; + productName = Runner; + productReference = 97C146EE1CF9000F007C117D /* Runner.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 97C146E61CF9000F007C117D /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = YES; + LastUpgradeCheck = 1510; + ORGANIZATIONNAME = ""; + TargetAttributes = { + 331C8080294A63A400263BE5 = { + CreatedOnToolsVersion = 14.0; + TestTargetID = 97C146ED1CF9000F007C117D; + }; + 97C146ED1CF9000F007C117D = { + CreatedOnToolsVersion = 7.3.1; + LastSwiftMigration = 1100; + }; + }; + }; + buildConfigurationList = 97C146E91CF9000F007C117D /* Build configuration list for PBXProject "Runner" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 97C146E51CF9000F007C117D; + productRefGroup = 97C146EF1CF9000F007C117D /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 97C146ED1CF9000F007C117D /* Runner */, + 331C8080294A63A400263BE5 /* RunnerTests */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 331C807F294A63A400263BE5 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 97C146EC1CF9000F007C117D /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */, + 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */, + 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */, + 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXShellScriptBuildPhase section */ + 3B06AD1E1E4923F5004D2608 /* Thin Binary */ = { + isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}", + ); + name = "Thin Binary"; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" embed_and_thin"; + }; + 9740EEB61CF901F6004384FC /* Run Script */ = { + isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + name = "Run Script"; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build"; + }; +/* End PBXShellScriptBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 331C807D294A63A400263BE5 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 331C808B294A63AB00263BE5 /* RunnerTests.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 97C146EA1CF9000F007C117D /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */, + 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXTargetDependency section */ + 331C8086294A63A400263BE5 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 97C146ED1CF9000F007C117D /* Runner */; + targetProxy = 331C8085294A63A400263BE5 /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + +/* Begin PBXVariantGroup section */ + 97C146FA1CF9000F007C117D /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 97C146FB1CF9000F007C117D /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 97C147001CF9000F007C117D /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 249021D3217E4FDB00AE95B9 /* Profile */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = NO; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + SUPPORTED_PLATFORMS = iphoneos; + TARGETED_DEVICE_FAMILY = "1,2"; + VALIDATE_PRODUCT = YES; + }; + name = Profile; + }; + 249021D4217E4FDB00AE95B9 /* Profile */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; + CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; + ENABLE_BITCODE = NO; + INFOPLIST_FILE = Runner/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.example.appUpgradePluginExample; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; + SWIFT_VERSION = 5.0; + VERSIONING_SYSTEM = "apple-generic"; + }; + name = Profile; + }; + 331C8088294A63A400263BE5 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + GENERATE_INFOPLIST_FILE = YES; + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = com.example.appUpgradePluginExample.RunnerTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Runner"; + }; + name = Debug; + }; + 331C8089294A63A400263BE5 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + GENERATE_INFOPLIST_FILE = YES; + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = com.example.appUpgradePluginExample.RunnerTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Runner"; + }; + name = Release; + }; + 331C808A294A63A400263BE5 /* Profile */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + GENERATE_INFOPLIST_FILE = YES; + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = com.example.appUpgradePluginExample.RunnerTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Runner"; + }; + name = Profile; + }; + 97C147031CF9000F007C117D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = NO; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 97C147041CF9000F007C117D /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = NO; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + SUPPORTED_PLATFORMS = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + TARGETED_DEVICE_FAMILY = "1,2"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 97C147061CF9000F007C117D /* Debug */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; + CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; + ENABLE_BITCODE = NO; + INFOPLIST_FILE = Runner/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.example.appUpgradePluginExample; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + VERSIONING_SYSTEM = "apple-generic"; + }; + name = Debug; + }; + 97C147071CF9000F007C117D /* Release */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; + CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; + ENABLE_BITCODE = NO; + INFOPLIST_FILE = Runner/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.example.appUpgradePluginExample; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; + SWIFT_VERSION = 5.0; + VERSIONING_SYSTEM = "apple-generic"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 331C8087294A63A400263BE5 /* Build configuration list for PBXNativeTarget "RunnerTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 331C8088294A63A400263BE5 /* Debug */, + 331C8089294A63A400263BE5 /* Release */, + 331C808A294A63A400263BE5 /* Profile */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 97C146E91CF9000F007C117D /* Build configuration list for PBXProject "Runner" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 97C147031CF9000F007C117D /* Debug */, + 97C147041CF9000F007C117D /* Release */, + 249021D3217E4FDB00AE95B9 /* Profile */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 97C147061CF9000F007C117D /* Debug */, + 97C147071CF9000F007C117D /* Release */, + 249021D4217E4FDB00AE95B9 /* Profile */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 97C146E61CF9000F007C117D /* Project object */; +} diff --git a/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..f9b0d7c --- /dev/null +++ b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + PreviewsEnabled + + + diff --git a/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme new file mode 100644 index 0000000..e3773d4 --- /dev/null +++ b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/ios/Runner.xcworkspace/contents.xcworkspacedata b/example/ios/Runner.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..1d526a1 --- /dev/null +++ b/example/ios/Runner.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..f9b0d7c --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + PreviewsEnabled + + + diff --git a/example/ios/Runner/AppDelegate.swift b/example/ios/Runner/AppDelegate.swift new file mode 100644 index 0000000..6266644 --- /dev/null +++ b/example/ios/Runner/AppDelegate.swift @@ -0,0 +1,13 @@ +import Flutter +import UIKit + +@main +@objc class AppDelegate: FlutterAppDelegate { + override func application( + _ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? + ) -> Bool { + GeneratedPluginRegistrant.register(with: self) + return super.application(application, didFinishLaunchingWithOptions: launchOptions) + } +} diff --git a/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..d36b1fa --- /dev/null +++ b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,122 @@ +{ + "images" : [ + { + "size" : "20x20", + "idiom" : "iphone", + "filename" : "Icon-App-20x20@2x.png", + "scale" : "2x" + }, + { + "size" : "20x20", + "idiom" : "iphone", + "filename" : "Icon-App-20x20@3x.png", + "scale" : "3x" + }, + { + "size" : "29x29", + "idiom" : "iphone", + "filename" : "Icon-App-29x29@1x.png", + "scale" : "1x" + }, + { + "size" : "29x29", + "idiom" : "iphone", + "filename" : "Icon-App-29x29@2x.png", + "scale" : "2x" + }, + { + "size" : "29x29", + "idiom" : "iphone", + "filename" : "Icon-App-29x29@3x.png", + "scale" : "3x" + }, + { + "size" : "40x40", + "idiom" : "iphone", + "filename" : "Icon-App-40x40@2x.png", + "scale" : "2x" + }, + { + "size" : "40x40", + "idiom" : "iphone", + "filename" : "Icon-App-40x40@3x.png", + "scale" : "3x" + }, + { + "size" : "60x60", + "idiom" : "iphone", + "filename" : "Icon-App-60x60@2x.png", + "scale" : "2x" + }, + { + "size" : "60x60", + "idiom" : "iphone", + "filename" : "Icon-App-60x60@3x.png", + "scale" : "3x" + }, + { + "size" : "20x20", + "idiom" : "ipad", + "filename" : "Icon-App-20x20@1x.png", + "scale" : "1x" + }, + { + "size" : "20x20", + "idiom" : "ipad", + "filename" : "Icon-App-20x20@2x.png", + "scale" : "2x" + }, + { + "size" : "29x29", + "idiom" : "ipad", + "filename" : "Icon-App-29x29@1x.png", + "scale" : "1x" + }, + { + "size" : "29x29", + "idiom" : "ipad", + "filename" : "Icon-App-29x29@2x.png", + "scale" : "2x" + }, + { + "size" : "40x40", + "idiom" : "ipad", + "filename" : "Icon-App-40x40@1x.png", + "scale" : "1x" + }, + { + "size" : "40x40", + "idiom" : "ipad", + "filename" : "Icon-App-40x40@2x.png", + "scale" : "2x" + }, + { + "size" : "76x76", + "idiom" : "ipad", + "filename" : "Icon-App-76x76@1x.png", + "scale" : "1x" + }, + { + "size" : "76x76", + "idiom" : "ipad", + "filename" : "Icon-App-76x76@2x.png", + "scale" : "2x" + }, + { + "size" : "83.5x83.5", + "idiom" : "ipad", + "filename" : "Icon-App-83.5x83.5@2x.png", + "scale" : "2x" + }, + { + "size" : "1024x1024", + "idiom" : "ios-marketing", + "filename" : "Icon-App-1024x1024@1x.png", + "scale" : "1x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} diff --git a/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png new file mode 100644 index 0000000000000000000000000000000000000000..dc9ada4725e9b0ddb1deab583e5b5102493aa332 GIT binary patch literal 10932 zcmeHN2~<R zh`|8`A_PQ1nSu(UMFx?8j8PC!!VDphaL#`F42fd#7Vlc`zIE4n%Y~eiz4y1j|NDpi z?<@|pSJ-HM`qifhf@m%MamgwK83`XpBA<+azdF#2QsT{X@z0A9Bq>~TVErigKH1~P zRX-!h-f0NJ4Mh++{D}J+K>~~rq}d%o%+4dogzXp7RxX4C>Km5XEI|PAFDmo;DFm6G zzjVoB`@qW98Yl0Kvc-9w09^PrsobmG*Eju^=3f?0o-t$U)TL1B3;sZ^!++3&bGZ!o-*6w?;oOhf z=A+Qb$scV5!RbG+&2S}BQ6YH!FKb0``VVX~T$dzzeSZ$&9=X$3)_7Z{SspSYJ!lGE z7yig_41zpQ)%5dr4ff0rh$@ky3-JLRk&DK)NEIHecf9c*?Z1bUB4%pZjQ7hD!A0r-@NF(^WKdr(LXj|=UE7?gBYGgGQV zidf2`ZT@pzXf7}!NH4q(0IMcxsUGDih(0{kRSez&z?CFA0RVXsVFw3^u=^KMtt95q z43q$b*6#uQDLoiCAF_{RFc{!H^moH_cmll#Fc^KXi{9GDl{>%+3qyfOE5;Zq|6#Hb zp^#1G+z^AXfRKaa9HK;%b3Ux~U@q?xg<2DXP%6k!3E)PA<#4$ui8eDy5|9hA5&{?v z(-;*1%(1~-NTQ`Is1_MGdQ{+i*ccd96ab$R$T3=% zw_KuNF@vI!A>>Y_2pl9L{9h1-C6H8<)J4gKI6{WzGBi<@u3P6hNsXG=bRq5c+z;Gc3VUCe;LIIFDmQAGy+=mRyF++u=drBWV8-^>0yE9N&*05XHZpPlE zxu@?8(ZNy7rm?|<+UNe0Vs6&o?l`Pt>P&WaL~M&#Eh%`rg@Mbb)J&@DA-wheQ>hRV z<(XhigZAT z>=M;URcdCaiO3d^?H<^EiEMDV+7HsTiOhoaMX%P65E<(5xMPJKxf!0u>U~uVqnPN7T!X!o@_gs3Ct1 zlZ_$5QXP4{Aj645wG_SNT&6m|O6~Tsl$q?nK*)(`{J4b=(yb^nOATtF1_aS978$x3 zx>Q@s4i3~IT*+l{@dx~Hst21fR*+5}S1@cf>&8*uLw-0^zK(+OpW?cS-YG1QBZ5q! zgTAgivzoF#`cSz&HL>Ti!!v#?36I1*l^mkrx7Y|K6L#n!-~5=d3;K<;Zqi|gpNUn_ z_^GaQDEQ*jfzh;`j&KXb66fWEk1K7vxQIMQ_#Wu_%3 z4Oeb7FJ`8I>Px;^S?)}2+4D_83gHEq>8qSQY0PVP?o)zAv3K~;R$fnwTmI-=ZLK`= zTm+0h*e+Yfr(IlH3i7gUclNH^!MU>id$Jw>O?2i0Cila#v|twub21@e{S2v}8Z13( zNDrTXZVgris|qYm<0NU(tAPouG!QF4ZNpZPkX~{tVf8xY690JqY1NVdiTtW+NqyRP zZ&;T0ikb8V{wxmFhlLTQ&?OP7 z;(z*<+?J2~z*6asSe7h`$8~Se(@t(#%?BGLVs$p``;CyvcT?7Y!{tIPva$LxCQ&4W z6v#F*);|RXvI%qnoOY&i4S*EL&h%hP3O zLsrFZhv&Hu5tF$Lx!8(hs&?!Kx5&L(fdu}UI5d*wn~A`nPUhG&Rv z2#ixiJdhSF-K2tpVL=)5UkXRuPAFrEW}7mW=uAmtVQ&pGE-&az6@#-(Te^n*lrH^m@X-ftVcwO_#7{WI)5v(?>uC9GG{lcGXYJ~Q8q zbMFl7;t+kV;|;KkBW2!P_o%Czhw&Q(nXlxK9ak&6r5t_KH8#1Mr-*0}2h8R9XNkr zto5-b7P_auqTJb(TJlmJ9xreA=6d=d)CVbYP-r4$hDn5|TIhB>SReMfh&OVLkMk-T zYf%$taLF0OqYF?V{+6Xkn>iX@TuqQ?&cN6UjC9YF&%q{Ut3zv{U2)~$>-3;Dp)*(? zg*$mu8^i=-e#acaj*T$pNowo{xiGEk$%DusaQiS!KjJH96XZ-hXv+jk%ard#fu=@Q z$AM)YWvE^{%tDfK%nD49=PI|wYu}lYVbB#a7wtN^Nml@CE@{Gv7+jo{_V?I*jkdLD zJE|jfdrmVbkfS>rN*+`#l%ZUi5_bMS<>=MBDNlpiSb_tAF|Zy`K7kcp@|d?yaTmB^ zo?(vg;B$vxS|SszusORgDg-*Uitzdi{dUV+glA~R8V(?`3GZIl^egW{a919!j#>f` znL1o_^-b`}xnU0+~KIFLQ)$Q6#ym%)(GYC`^XM*{g zv3AM5$+TtDRs%`2TyR^$(hqE7Y1b&`Jd6dS6B#hDVbJlUXcG3y*439D8MrK!2D~6gn>UD4Imctb z+IvAt0iaW73Iq$K?4}H`7wq6YkTMm`tcktXgK0lKPmh=>h+l}Y+pDtvHnG>uqBA)l zAH6BV4F}v$(o$8Gfo*PB>IuaY1*^*`OTx4|hM8jZ?B6HY;F6p4{`OcZZ(us-RVwDx zUzJrCQlp@mz1ZFiSZ*$yX3c_#h9J;yBE$2g%xjmGF4ca z&yL`nGVs!Zxsh^j6i%$a*I3ZD2SoNT`{D%mU=LKaEwbN(_J5%i-6Va?@*>=3(dQy` zOv%$_9lcy9+(t>qohkuU4r_P=R^6ME+wFu&LA9tw9RA?azGhjrVJKy&8=*qZT5Dr8g--d+S8zAyJ$1HlW3Olryt`yE zFIph~Z6oF&o64rw{>lgZISC6p^CBer9C5G6yq%?8tC+)7*d+ib^?fU!JRFxynRLEZ zj;?PwtS}Ao#9whV@KEmwQgM0TVP{hs>dg(1*DiMUOKHdQGIqa0`yZnHk9mtbPfoLx zo;^V6pKUJ!5#n`w2D&381#5#_t}AlTGEgDz$^;u;-vxDN?^#5!zN9ngytY@oTv!nc zp1Xn8uR$1Z;7vY`-<*?DfPHB;x|GUi_fI9@I9SVRv1)qETbNU_8{5U|(>Du84qP#7 z*l9Y$SgA&wGbj>R1YeT9vYjZuC@|{rajTL0f%N@>3$DFU=`lSPl=Iv;EjuGjBa$Gw zHD-;%YOE@<-!7-Mn`0WuO3oWuL6tB2cpPw~Nvuj|KM@))ixuDK`9;jGMe2d)7gHin zS<>k@!x;!TJEc#HdL#RF(`|4W+H88d4V%zlh(7#{q2d0OQX9*FW^`^_<3r$kabWAB z$9BONo5}*(%kx zOXi-yM_cmB3>inPpI~)duvZykJ@^^aWzQ=eQ&STUa}2uT@lV&WoRzkUoE`rR0)`=l zFT%f|LA9fCw>`enm$p7W^E@U7RNBtsh{_-7vVz3DtB*y#*~(L9+x9*wn8VjWw|Q~q zKFsj1Yl>;}%MG3=PY`$g$_mnyhuV&~O~u~)968$0b2!Jkd;2MtAP#ZDYw9hmK_+M$ zb3pxyYC&|CuAbtiG8HZjj?MZJBFbt`ryf+c1dXFuC z0*ZQhBzNBd*}s6K_G}(|Z_9NDV162#y%WSNe|FTDDhx)K!c(mMJh@h87@8(^YdK$&d*^WQe8Z53 z(|@MRJ$Lk-&ii74MPIs80WsOFZ(NX23oR-?As+*aq6b?~62@fSVmM-_*cb1RzZ)`5$agEiL`-E9s7{GM2?(KNPgK1(+c*|-FKoy}X(D_b#etO|YR z(BGZ)0Ntfv-7R4GHoXp?l5g#*={S1{u-QzxCGng*oWr~@X-5f~RA14b8~B+pLKvr4 zfgL|7I>jlak9>D4=(i(cqYf7#318!OSR=^`xxvI!bBlS??`xxWeg?+|>MxaIdH1U~#1tHu zB{QMR?EGRmQ_l4p6YXJ{o(hh-7Tdm>TAX380TZZZyVkqHNzjUn*_|cb?T? zt;d2s-?B#Mc>T-gvBmQZx(y_cfkXZO~{N zT6rP7SD6g~n9QJ)8F*8uHxTLCAZ{l1Y&?6v)BOJZ)=R-pY=Y=&1}jE7fQ>USS}xP#exo57uND0i*rEk@$;nLvRB@u~s^dwRf?G?_enN@$t* zbL%JO=rV(3Ju8#GqUpeE3l_Wu1lN9Y{D4uaUe`g>zlj$1ER$6S6@{m1!~V|bYkhZA z%CvrDRTkHuajMU8;&RZ&itnC~iYLW4DVkP<$}>#&(`UO>!n)Po;Mt(SY8Yb`AS9lt znbX^i?Oe9r_o=?})IHKHoQGKXsps_SE{hwrg?6dMI|^+$CeC&z@*LuF+P`7LfZ*yr+KN8B4{Nzv<`A(wyR@!|gw{zB6Ha ziwPAYh)oJ(nlqSknu(8g9N&1hu0$vFK$W#mp%>X~AU1ay+EKWcFdif{% z#4!4aoVVJ;ULmkQf!ke2}3hqxLK>eq|-d7Ly7-J9zMpT`?dxo6HdfJA|t)?qPEVBDv z{y_b?4^|YA4%WW0VZd8C(ZgQzRI5(I^)=Ub`Y#MHc@nv0w-DaJAqsbEHDWG8Ia6ju zo-iyr*sq((gEwCC&^TYBWt4_@|81?=B-?#P6NMff(*^re zYqvDuO`K@`mjm_Jd;mW_tP`3$cS?R$jR1ZN09$YO%_iBqh5ftzSpMQQtxKFU=FYmP zeY^jph+g<4>YO;U^O>-NFLn~-RqlHvnZl2yd2A{Yc1G@Ga$d+Q&(f^tnPf+Z7serIU};17+2DU_f4Z z@GaPFut27d?!YiD+QP@)T=77cR9~MK@bd~pY%X(h%L={{OIb8IQmf-!xmZkm8A0Ga zQSWONI17_ru5wpHg3jI@i9D+_Y|pCqVuHJNdHUauTD=R$JcD2K_liQisqG$(sm=k9;L* z!L?*4B~ql7uioSX$zWJ?;q-SWXRFhz2Jt4%fOHA=Bwf|RzhwqdXGr78y$J)LR7&3T zE1WWz*>GPWKZ0%|@%6=fyx)5rzUpI;bCj>3RKzNG_1w$fIFCZ&UR0(7S?g}`&Pg$M zf`SLsz8wK82Vyj7;RyKmY{a8G{2BHG%w!^T|Njr!h9TO2LaP^_f22Q1=l$QiU84ao zHe_#{S6;qrC6w~7{y(hs-?-j?lbOfgH^E=XcSgnwW*eEz{_Z<_xN#0001NP)t-s|Ns9~ z#rXRE|M&d=0au&!`~QyF`q}dRnBDt}*!qXo`c{v z{Djr|@Adh0(D_%#_&mM$D6{kE_x{oE{l@J5@%H*?%=t~i_`ufYOPkAEn!pfkr2$fs z652Tz0001XNklqeeKN4RM4i{jKqmiC$?+xN>3Apn^ z0QfuZLym_5b<*QdmkHjHlj811{If)dl(Z2K0A+ekGtrFJb?g|wt#k#pV-#A~bK=OT ts8>{%cPtyC${m|1#B1A6#u!Q;umknL1chzTM$P~L002ovPDHLkV1lTfnu!1a literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..797d452e458972bab9d994556c8305db4c827017 GIT binary patch literal 406 zcmV;H0crk;P))>cdjpWt&rLJgVp-t?DREyuq1A%0Z4)6_WsQ7{nzjN zo!X zGXV)2i3kcZIL~_j>uIKPK_zib+3T+Nt3Mb&Br)s)UIaA}@p{wDda>7=Q|mGRp7pqY zkJ!7E{MNz$9nOwoVqpFb)}$IP24Wn2JJ=Cw(!`OXJBr45rP>>AQr$6c7slJWvbpNW z@KTwna6d?PP>hvXCcp=4F;=GR@R4E7{4VU^0p4F>v^#A|>07*qoM6N<$f*5nx ACIA2c literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png new file mode 100644 index 0000000000000000000000000000000000000000..6ed2d933e1120817fe9182483a228007b18ab6ae GIT binary patch literal 450 zcmV;z0X_bSP)iGWQ_5NJQ_~rNh*z)}eT%KUb z`7gNk0#AwF^#0T0?hIa^`~Ck;!}#m+_uT050aTR(J!bU#|IzRL%^UsMS#KsYnTF*!YeDOytlP4VhV?b} z%rz_<=#CPc)tU1MZTq~*2=8~iZ!lSa<{9b@2Jl;?IEV8)=fG217*|@)CCYgFze-x? zIFODUIA>nWKpE+bn~n7;-89sa>#DR>TSlqWk*!2hSN6D~Qb#VqbP~4Fk&m`@1$JGr zXPIdeRE&b2Thd#{MtDK$px*d3-Wx``>!oimf%|A-&-q*6KAH)e$3|6JV%HX{Hig)k suLT-RhftRq8b9;(V=235Wa|I=027H2wCDra;{X5v07*qoM6N<$f;9x^2LJ#7 literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png new file mode 100644 index 0000000000000000000000000000000000000000..4cd7b0099ca80c806f8fe495613e8d6c69460d76 GIT binary patch literal 282 zcmV+#0p(^bcu7P-R4C8Q z&e;xxFbF_Vrezo%_kH*OKhshZ6BFpG-Y1e10`QXJKbND7AMQ&cMj60B5TNObaZxYybcN07*qoM6N<$g3m;S%K!iX literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..fe730945a01f64a61e2235dbe3f45b08f7729182 GIT binary patch literal 462 zcmV;<0WtoGP)-}iV`2<;=$?g5M=KQbZ{F&YRNy7Nn@%_*5{gvDM0aKI4?ESmw z{NnZg)A0R`+4?NF_RZexyVB&^^ZvN!{I28tr{Vje;QNTz`dG&Jz0~Ek&f2;*Z7>B|cg}xYpxEFY+0YrKLF;^Q+-HreN0P{&i zK~zY`?b7ECf-n?@;d<&orQ*Q7KoR%4|C>{W^h6@&01>0SKS`dn{Q}GT%Qj_{PLZ_& zs`MFI#j-(>?bvdZ!8^xTwlY{qA)T4QLbY@j(!YJ7aXJervHy6HaG_2SB`6CC{He}f zHVw(fJWApwPq!6VY7r1w-Fs)@ox~N+q|w~e;JI~C4Vf^@d>Wvj=fl`^u9x9wd9 zR%3*Q+)t%S!MU_`id^@&Y{y7-r98lZX0?YrHlfmwb?#}^1b{8g&KzmkE(L>Z&)179 zp<)v6Y}pRl100G2FL_t(o!|l{-Q-VMg#&MKg7c{O0 z2wJImOS3Gy*Z2Qifdv~JYOp;v+U)a|nLoc7hNH;I$;lzDt$}rkaFw1mYK5_0Q(Sut zvbEloxON7$+HSOgC9Z8ltuC&0OSF!-mXv5caV>#bc3@hBPX@I$58-z}(ZZE!t-aOG zpjNkbau@>yEzH(5Yj4kZiMH32XI!4~gVXNnjAvRx;Sdg^`>2DpUEwoMhTs_st8pKG z(%SHyHdU&v%f36~uERh!bd`!T2dw;z6PrOTQ7Vt*#9F2uHlUVnb#ev_o^fh}Dzmq} zWtlk35}k=?xj28uO|5>>$yXadTUE@@IPpgH`gJ~Ro4>jd1IF|(+IX>8M4Ps{PNvmI zNj4D+XgN83gPt_Gm}`Ybv{;+&yu-C(Grdiahmo~BjG-l&mWM+{e5M1sm&=xduwgM9 z`8OEh`=F3r`^E{n_;%9weN{cf2%7=VzC@cYj+lg>+3|D|_1C@{hcU(DyQG_BvBWe? zvTv``=%b1zrol#=R`JB)>cdjpWt&rLJgVp-t?DREyuq1A%0Z4)6_WsQ7{nzjN zo!X zGXV)2i3kcZIL~_j>uIKPK_zib+3T+Nt3Mb&Br)s)UIaA}@p{wDda>7=Q|mGRp7pqY zkJ!7E{MNz$9nOwoVqpFb)}$IP24Wn2JJ=Cw(!`OXJBr45rP>>AQr$6c7slJWvbpNW z@KTwna6d?PP>hvXCcp=4F;=GR@R4E7{4VU^0p4F>v^#A|>07*qoM6N<$f*5nx ACIA2c literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..502f463a9bc882b461c96aadf492d1729e49e725 GIT binary patch literal 586 zcmV-Q0=4~#P)+}#`wDE{8-2Mebf5<{{PqV{TgVcv*r8?UZ3{-|G?_}T*&y;@cqf{ z{Q*~+qr%%p!1pS*_Uicl#q9lc(D`!D`LN62sNwq{oYw(Wmhk)k<@f$!$@ng~_5)Ru z0Z)trIA5^j{DIW^c+vT2%lW+2<(RtE2wR;4O@)Tm`Xr*?A(qYoM}7i5Yxw>D(&6ou zxz!_Xr~yNF+waPe00049Nkl*;a!v6h%{rlvIH#gW3s8p;bFr=l}mRqpW2h zw=OA%hdyL~z+UHOzl0eKhEr$YYOL-c-%Y<)=j?(bzDweB7{b+%_ypvm_cG{SvM=DK zhv{K@m>#Bw>2W$eUI#iU)Wdgs8Y3U+A$Gd&{+j)d)BmGKx+43U_!tik_YlN)>$7G! zhkE!s;%oku3;IwG3U^2kw?z+HM)jB{@zFhK8P#KMSytSthr+4!c(5c%+^UBn`0X*2 zy3(k600_CSZj?O$Qu%&$;|TGUJrptR(HzyIx>5E(2r{eA(<6t3e3I0B)7d6s7?Z5J zZ!rtKvA{MiEBm&KFtoifx>5P^Z=vl)95XJn()aS5%ad(s?4-=Tkis9IGu{`Fy8r+H07*qoM6N<$f20Z)wqMt%V?S?~D#06};F zA3KcL`Wb+>5ObvgQIG&ig8(;V04hz?@cqy3{mSh8o!|U|)cI!1_+!fWH@o*8vh^CU z^ws0;(c$gI+2~q^tO#GDHf@=;DncUw00J^eL_t(&-tE|HQ`%4vfZ;WsBqu-$0nu1R zq^Vj;p$clf^?twn|KHO+IGt^q#a3X?w9dXC@*yxhv&l}F322(8Y1&=P&I}~G@#h6; z1CV9ecD9ZEe87{{NtI*)_aJ<`kJa z?5=RBtFF50s;jQLFil-`)m2wrb=6h(&brpj%nG_U&ut~$?8Rokzxi8zJoWr#2dto5 zOX_URcc<1`Iky+jc;A%Vzx}1QU{2$|cKPom2Vf1{8m`vja4{F>HS?^Nc^rp}xo+Nh zxd}eOm`fm3@MQC1< zIk&aCjb~Yh%5+Yq0`)D;q{#-Uqlv*o+Oor zE!I71Z@ASH3grl8&P^L0WpavHoP|UX4e?!igT`4?AZk$hu*@%6WJ;zDOGlw7kj@ zY5!B-0ft0f?Lgb>C;$Ke07*qoM6N<$f~t1N9smFU literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..0ec303439225b78712f49115768196d8d76f6790 GIT binary patch literal 862 zcmV-k1EKthP)20Z)wqMt%V?S?~D#06};F zA3KcL`Wb+>5ObvgQIG&ig8(;V04hz?@cqy3{mSh8o!|U|)cI!1_+!fWH@o*8vh^CU z^ws0;(c$gI+2~q^tO#GDHf@=;DncUw00J^eL_t(&-tE|HQ`%4vfZ;WsBqu-$0nu1R zq^Vj;p$clf^?twn|KHO+IGt^q#a3X?w9dXC@*yxhv&l}F322(8Y1&=P&I}~G@#h6; z1CV9ecD9ZEe87{{NtI*)_aJ<`kJa z?5=RBtFF50s;jQLFil-`)m2wrb=6h(&brpj%nG_U&ut~$?8Rokzxi8zJoWr#2dto5 zOX_URcc<1`Iky+jc;A%Vzx}1QU{2$|cKPom2Vf1{8m`vja4{F>HS?^Nc^rp}xo+Nh zxd}eOm`fm3@MQC1< zIk&aCjb~Yh%5+Yq0`)D;q{#-Uqlv*o+Oor zE!I71Z@ASH3grl8&P^L0WpavHoP|UX4e?!igT`4?AZk$hu*@%6WJ;zDOGlw7kj@ zY5!B-0ft0f?Lgb>C;$Ke07*qoM6N<$f~t1N9smFU literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png new file mode 100644 index 0000000000000000000000000000000000000000..e9f5fea27c705180eb716271f41b582e76dcbd90 GIT binary patch literal 1674 zcmV;526g#~P){YQnis^a@{&-nmRmq)<&%Mztj67_#M}W?l>kYSliK<%xAp;0j{!}J0!o7b zE>q9${Lb$D&h7k=+4=!ek^n+`0zq>LL1O?lVyea53S5x`Nqqo2YyeuIrQrJj9XjOp z{;T5qbj3}&1vg1VK~#9!?b~^C5-}JC@Pyrv-6dSEqJqT}#j9#dJ@GzT@B8}x zU&J@bBI>f6w6en+CeI)3^kC*U?}X%OD8$Fd$H&LV$H&LV$H&LV#|K5~mLYf|VqzOc zkc7qL~0sOYuM{tG`rYEDV{DWY`Z8&)kW*hc2VkBuY+^Yx&92j&StN}Wp=LD zxoGxXw6f&8sB^u})h@b@z0RBeD`K7RMR9deyL(ZJu#39Z>rT)^>v}Khq8U-IbIvT> z?4pV9qGj=2)TNH3d)=De<+^w;>S7m_eFKTvzeaBeir45xY!^m!FmxnljbSS_3o=g( z->^wC9%qkR{kbGnW8MfFew_o9h3(r55Is`L$8KI@d+*%{=Nx+FXJ98L0PjFIu;rGnnfY zn1R5Qnp<{Jq0M1vX=X&F8gtLmcWv$1*M@4ZfF^9``()#hGTeKeP`1!iED ztNE(TN}M5}3Bbc*d=FIv`DNv&@|C6yYj{sSqUj5oo$#*0$7pu|Dd2TLI>t5%I zIa4Dvr(iayb+5x=j*Vum9&irk)xV1`t509lnPO0%skL8_1c#Xbamh(2@f?4yUI zhhuT5<#8RJhGz4%b$`PJwKPAudsm|at?u;*hGgnA zU1;9gnxVBC)wA(BsB`AW54N{|qmikJR*%x0c`{LGsSfa|NK61pYH(r-UQ4_JXd!Rsz)=k zL{GMc5{h138)fF5CzHEDM>+FqY)$pdN3}Ml+riTgJOLN0F*Vh?{9ESR{SVVg>*>=# zix;VJHPtvFFCRY$Ks*F;VX~%*r9F)W`PmPE9F!(&s#x07n2<}?S{(ygpXgX-&B&OM zONY&BRQ(#%0%jeQs?oJ4P!p*R98>qCy5p8w>_gpuh39NcOlp)(wOoz0sY-Qz55eB~ z7OC-fKBaD1sE3$l-6QgBJO!n?QOTza`!S_YK z_v-lm^7{VO^8Q@M_^8F)09Ki6%=s?2_5eupee(w1FB%aqSweusQ-T+CH0Xt{` zFjMvW{@C&TB)k25()nh~_yJ9coBRL(0oO@HK~z}7?bm5j;y@69;bvlHb2tf!$ReA~x{22wTq550 z?f?Hnw(;m3ip30;QzdV~7pi!wyMYhDtXW#cO7T>|f=bdFhu+F!zMZ2UFj;GUKX7tI z;hv3{q~!*pMj75WP_c}>6)IWvg5_yyg<9Op()eD1hWC19M@?_9_MHec{Z8n3FaF{8 z;u`Mw0ly(uE>*CgQYv{be6ab2LWhlaH1^iLIM{olnag$78^Fd}%dR7;JECQ+hmk|o z!u2&!3MqPfP5ChDSkFSH8F2WVOEf0(E_M(JL17G}Y+fg0_IuW%WQ zG(mG&u?|->YSdk0;8rc{yw2@2Z&GA}z{Wb91Ooz9VhA{b2DYE7RmG zjL}?eq#iX%3#k;JWMx_{^2nNax`xPhByFiDX+a7uTGU|otOvIAUy|dEKkXOm-`aWS z27pUzD{a)Ct<6p{{3)+lq@i`t@%>-wT4r?*S}k)58e09WZYP0{{R3FC5Sl00039P)t-s|Ns9~ z#rP?<_5oL$Q^olD{r_0T`27C={r>*`|Nj71npVa5OTzc(_WfbW_({R{p56NV{r*M2 z_xt?)2V0#0NsfV0u>{42ctGP(8vQj-Btk1n|O0ZD=YLwd&R{Ko41Gr9H= zY@z@@bOAMB5Ltl$E>bJJ{>JP30ZxkmI%?eW{k`b?Wy<&gOo;dS`~CR$Vwb@XWtR|N zi~t=w02?-0&j0TD{>bb6sNwsK*!p?V`RMQUl(*DVjk-9Cx+-z1KXab|Ka2oXhX5f% z`$|e!000AhNklrxs)5QTeTVRiEmz~MKK1WAjCw(c-JK6eox;2O)?`? zTG`AHia671e^vgmp!llKp|=5sVHk#C7=~epA~VAf-~%aPC=%Qw01h8mnSZ|p?hz91 z7p83F3%LVu9;S$tSI$C^%^yud1dfTM_6p2|+5Ejp$bd`GDvbR|xit>i!ZD&F>@CJrPmu*UjD&?DfZs=$@e3FQA(vNiU+$A*%a} z?`XcG2jDxJ_ZQ#Md`H{4Lpf6QBDp81_KWZ6Tk#yCy1)32zO#3<7>b`eT7UyYH1eGz z;O(rH$=QR*L%%ZcBpc=eGua?N55nD^K(8<#gl2+pN_j~b2MHs4#mcLmv%DkspS-3< zpI1F=^9siI0s-;IN_IrA;5xm~3?3!StX}pUv0vkxMaqm+zxrg7X7(I&*N~&dEd0kD z-FRV|g=|QuUsuh>-xCI}vD2imzYIOIdcCVV=$Bz@*u0+Bs<|L^)32nN*=wu3n%Ynw z@1|eLG>!8ruU1pFXUfb`j>(=Gy~?Rn4QJ-c3%3T|(Frd!bI`9u&zAnyFYTqlG#&J7 zAkD(jpw|oZLNiA>;>hgp1KX7-wxC~31II47gc zHcehD6Uxlf%+M^^uN5Wc*G%^;>D5qT{>=uxUhX%WJu^Z*(_Wq9y}npFO{Hhb>s6<9 zNi0pHXWFaVZnb)1+RS&F)xOv6&aeILcI)`k#0YE+?e)5&#r7J#c`3Z7x!LpTc01dx zrdC3{Z;joZ^KN&))zB_i)I9fWedoN>Zl-6_Iz+^G&*ak2jpF07*qoM6N<$f;w%0(f|Me literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..0467bf12aa4d28f374bb26596605a46dcbb3e7c8 GIT binary patch literal 1418 zcmV;51$Fv~P)q zKfU)WzW*n(@|xWGCA9ScMt*e9`2kdxPQ&&>|-UCa7_51w+ zLUsW@ZzZSW0y$)Hp~e9%PvP|a03ks1`~K?q{u;6NC8*{AOqIUq{CL&;p56Lf$oQGq z^={4hPQv)y=I|4n+?>7Fim=dxt1 z2H+Dm+1+fh+IF>G0SjJMkQQre1x4|G*Z==(Ot&kCnUrL4I(rf(ucITwmuHf^hXiJT zkdTm&kdTm&kdTm&kdP`esgWG0BcWCVkVZ&2dUwN`cgM8QJb`Z7Z~e<&Yj2(}>Tmf` zm1{eLgw!b{bXkjWbF%dTkTZEJWyWOb##Lfw4EK2}<0d6%>AGS{po>WCOy&f$Tay_> z?NBlkpo@s-O;0V%Y_Xa-G#_O08q5LR*~F%&)}{}r&L%Sbs8AS4t7Y0NEx*{soY=0MZExqA5XHQkqi#4gW3 zqODM^iyZl;dvf)-bOXtOru(s)Uc7~BFx{w-FK;2{`VA?(g&@3z&bfLFyctOH!cVsF z7IL=fo-qBndRUm;kAdXR4e6>k-z|21AaN%ubeVrHl*<|s&Ax@W-t?LR(P-24A5=>a z*R9#QvjzF8n%@1Nw@?CG@6(%>+-0ASK~jEmCV|&a*7-GKT72W<(TbSjf)&Eme6nGE z>Gkj4Sq&2e+-G%|+NM8OOm5zVl9{Z8Dd8A5z3y8mZ=4Bv4%>as_{9cN#bm~;h>62( zdqY93Zy}v&c4n($Vv!UybR8ocs7#zbfX1IY-*w~)p}XyZ-SFC~4w>BvMVr`dFbelV{lLL0bx7@*ZZdebr3`sP;? zVImji)kG)(6Juv0lz@q`F!k1FE;CQ(D0iG$wchPbKZQELlsZ#~rt8#90Y_Xh&3U-< z{s<&cCV_1`^TD^ia9!*mQDq& zn2{r`j};V|uV%_wsP!zB?m%;FeaRe+X47K0e+KE!8C{gAWF8)lCd1u1%~|M!XNRvw zvtqy3iz0WSpWdhn6$hP8PaRBmp)q`#PCA`Vd#Tc$@f1tAcM>f_I@bC)hkI9|o(Iqv zo}Piadq!j76}004RBio<`)70k^`K1NK)q>w?p^C6J2ZC!+UppiK6&y3Kmbv&O!oYF z34$0Z;QO!JOY#!`qyGH<3Pd}Pt@q*A0V=3SVtWKRR8d8Z&@)3qLPA19LPA19LPEUC YUoZo%k(ykuW&i*H07*qoM6N<$f+CH{y8r+H literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json new file mode 100644 index 0000000..0bedcf2 --- /dev/null +++ b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json @@ -0,0 +1,23 @@ +{ + "images" : [ + { + "idiom" : "universal", + "filename" : "LaunchImage.png", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "LaunchImage@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "filename" : "LaunchImage@3x.png", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} diff --git a/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png new file mode 100644 index 0000000000000000000000000000000000000000..9da19eacad3b03bb08bbddbbf4ac48dd78b3d838 GIT binary patch literal 68 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx0wlM}@Gt=>Zci7-kcv6Uzs@r-FtIZ-&5|)J Q1PU{Fy85}Sb4q9e0B4a5jsO4v literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..9da19eacad3b03bb08bbddbbf4ac48dd78b3d838 GIT binary patch literal 68 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx0wlM}@Gt=>Zci7-kcv6Uzs@r-FtIZ-&5|)J Q1PU{Fy85}Sb4q9e0B4a5jsO4v literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png new file mode 100644 index 0000000000000000000000000000000000000000..9da19eacad3b03bb08bbddbbf4ac48dd78b3d838 GIT binary patch literal 68 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx0wlM}@Gt=>Zci7-kcv6Uzs@r-FtIZ-&5|)J Q1PU{Fy85}Sb4q9e0B4a5jsO4v literal 0 HcmV?d00001 diff --git a/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md new file mode 100644 index 0000000..89c2725 --- /dev/null +++ b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md @@ -0,0 +1,5 @@ +# Launch Screen Assets + +You can customize the launch screen with your own desired assets by replacing the image files in this directory. + +You can also do it by opening your Flutter project's Xcode project with `open ios/Runner.xcworkspace`, selecting `Runner/Assets.xcassets` in the Project Navigator and dropping in the desired images. \ No newline at end of file diff --git a/example/ios/Runner/Base.lproj/LaunchScreen.storyboard b/example/ios/Runner/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..f2e259c --- /dev/null +++ b/example/ios/Runner/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/ios/Runner/Base.lproj/Main.storyboard b/example/ios/Runner/Base.lproj/Main.storyboard new file mode 100644 index 0000000..f3c2851 --- /dev/null +++ b/example/ios/Runner/Base.lproj/Main.storyboard @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/ios/Runner/Info.plist b/example/ios/Runner/Info.plist new file mode 100644 index 0000000..56edd98 --- /dev/null +++ b/example/ios/Runner/Info.plist @@ -0,0 +1,49 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleDisplayName + App Upgrade Plugin + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + app_upgrade_plugin_example + CFBundlePackageType + APPL + CFBundleShortVersionString + $(FLUTTER_BUILD_NAME) + CFBundleSignature + ???? + CFBundleVersion + $(FLUTTER_BUILD_NUMBER) + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + CADisableMinimumFrameDurationOnPhone + + UIApplicationSupportsIndirectInputEvents + + + diff --git a/example/ios/Runner/Runner-Bridging-Header.h b/example/ios/Runner/Runner-Bridging-Header.h new file mode 100644 index 0000000..308a2a5 --- /dev/null +++ b/example/ios/Runner/Runner-Bridging-Header.h @@ -0,0 +1 @@ +#import "GeneratedPluginRegistrant.h" diff --git a/example/ios/RunnerTests/RunnerTests.swift b/example/ios/RunnerTests/RunnerTests.swift new file mode 100644 index 0000000..bfacd0c --- /dev/null +++ b/example/ios/RunnerTests/RunnerTests.swift @@ -0,0 +1,27 @@ +import Flutter +import UIKit +import XCTest + + +@testable import app_upgrade_plugin + +// This demonstrates a simple unit test of the Swift portion of this plugin's implementation. +// +// See https://developer.apple.com/documentation/xctest for more information about using XCTest. + +class RunnerTests: XCTestCase { + + func testGetPlatformVersion() { + let plugin = AppUpgradePlugin() + + let call = FlutterMethodCall(methodName: "getPlatformVersion", arguments: []) + + let resultExpectation = expectation(description: "result block must be called.") + plugin.handle(call) { result in + XCTAssertEqual(result as! String, "iOS " + UIDevice.current.systemVersion) + resultExpectation.fulfill() + } + waitForExpectations(timeout: 1) + } + +} diff --git a/example/lib/main.dart b/example/lib/main.dart new file mode 100644 index 0000000..e364f8c --- /dev/null +++ b/example/lib/main.dart @@ -0,0 +1,147 @@ +import 'dart:async'; + +import 'package:app_upgrade_plugin/app_upgrade_plugin.dart'; +import 'package:app_upgrade_plugin/app_upgrade_plugin_enhanced.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; + +void main() { + // 确保Flutter绑定已初始化 + WidgetsFlutterBinding.ensureInitialized(); + + // 插件现在会自动配置证书验证: + // - Debug模式:自动绕过证书验证 + // - Release模式:严格证书验证 + + // 如需强制绕过证书(不推荐),可以使用: + // AppUpgradePlugin.ignoreCertificate = true; + + // 或手动配置HTTP设置: + // AppUpgradePlugin().configureHttp(HttpConfig.unsafe); + + // 延迟插件配置到Flutter完全初始化后 + Future.delayed(Duration.zero, () { + AppUpgradePluginEnhanced.instance.configure( + debugMode: true, + wifiOnly: false, + autoCheck: true, + ); + }); + + runApp(const MyApp()); +} + +class MyApp extends StatelessWidget { + const MyApp({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + // 这里是国际化支持,确保添加flutter_localizations依赖 + supportedLocales: const [Locale('zh', 'CN')], + localizationsDelegates: const [ + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + home: const HomePage(), + ); + } +} + +class HomePage extends StatefulWidget { + const HomePage({super.key}); + + @override + State createState() => _HomePageState(); +} + +class _HomePageState extends State { + String _platformVersion = 'Unknown'; + final _appUpgradePlugin = AppUpgradePlugin(); + + @override + void initState() { + super.initState(); + initPlatformState(); + // Use addPostFrameCallback to ensure the context is valid and mounted + // after the first frame has been rendered. + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) { + _testNetworkFunctionality(); + } + }); + } + + Future _testNetworkFunctionality() async { + await AppUpgradeSimple.instance.checkUpdate( + context: context, + url: 'https://dpc-teacher-api.23544.com/api/infra/AppVersion/Get', + params: { + 'appName': 'making_school_asignment_app', + 'ftuType': 1, + }, + showNoUpdateToast: true, + autoDownload: false, + autoInstall: true, + ); + debugPrint('=== 网络功能测试完成 ==='); + } + + // Platform messages are asynchronous, so we initialize in an async method. + Future initPlatformState() async { + String platformVersion; + // Platform messages may fail, so we use a try/catch PlatformException. + // We also handle the message potentially returning null. + try { + platformVersion = await _appUpgradePlugin.getPlatformVersion() ?? 'Unknown platform version'; + } on PlatformException { + platformVersion = 'Failed to get platform version.'; + } + + // If the widget was removed from the tree while the asynchronous platform + // message was in flight, we want to discard the reply rather than calling + // setState to update our non-existent appearance. + if (!mounted) return; + + setState(() { + _platformVersion = platformVersion; + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('App Upgrade Plugin 示例'), + ), + body: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text('Running on: $_platformVersion\n'), + const SizedBox(height: 16), + ElevatedButton( + onPressed: () { + AppUpgradeSimple.instance.checkUpdate( + context: context, + url: 'https://dpc-teacher-api.23544.com/api/infra/AppVersion/Get', + params: { + 'appName': 'making_school_asignment_app', + 'ftuType': 1, + }, + showNoUpdateToast: true, + autoDownload: false, + autoInstall: false, + ); + }, + child: const Text('检查更新'), + ), + ], + ), + ), + ); + } +} diff --git a/example/lib/main_enhanced.dart b/example/lib/main_enhanced.dart new file mode 100644 index 0000000..e1108cb --- /dev/null +++ b/example/lib/main_enhanced.dart @@ -0,0 +1,738 @@ +import 'dart:async'; + +import 'package:app_upgrade_plugin/app_upgrade_plugin_enhanced.dart'; +import 'package:flutter/material.dart'; +import 'package:permission_handler/permission_handler.dart'; + +void main() { + runApp(const MyEnhancedApp()); +} + +class MyEnhancedApp extends StatelessWidget { + const MyEnhancedApp({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + title: '增强版App升级插件示例', + theme: ThemeData( + colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), + useMaterial3: true, + ), + home: const EnhancedUpgradePage(), + ); + } +} + +class EnhancedUpgradePage extends StatefulWidget { + const EnhancedUpgradePage({super.key}); + + @override + State createState() => _EnhancedUpgradePageState(); +} + +class _EnhancedUpgradePageState extends State with SingleTickerProviderStateMixin { + final _plugin = AppUpgradePluginEnhanced.instance; + + late TabController _tabController; + + // 状态变量 + Map? _appInfo; + NetworkStatus? _networkStatus; + DownloadTask? _currentDownload; + UpgradeInfo? _upgradeInfo; + Map? _cacheStats; + + // 配置选项 + bool _wifiOnly = true; + bool _autoCheck = true; + bool _supportBreakpoint = true; + bool _verifyIntegrity = true; + VersionCompareStrategy _versionStrategy = VersionCompareStrategy.semantic; + + // 测试数据 + final _testVersions = ['1.0.0', '1.1.0', '1.2.0', '2.0.0-beta.1', '2.0.0']; + String _currentTestVersion = '1.0.0'; + String _remoteTestVersion = '2.0.0'; + + @override + void initState() { + super.initState(); + _tabController = TabController(length: 4, vsync: this); + _initPlugin(); + _loadData(); + } + + @override + void dispose() { + _tabController.dispose(); + _plugin.removeUpgradeCallback(_onUpgradeInfo); + _plugin.removeDownloadCallback(_onDownloadProgress); + _plugin.removeErrorCallback(_onError); + super.dispose(); + } + + void _initPlugin() { + // 配置插件 + _plugin.configure( + debugMode: true, + autoCheck: _autoCheck, + wifiOnly: _wifiOnly, + supportBreakpoint: _supportBreakpoint, + verifyIntegrity: _verifyIntegrity, + versionStrategy: _versionStrategy, + ); + + // 添加回调 + _plugin.addUpgradeCallback(_onUpgradeInfo); + _plugin.addDownloadCallback(_onDownloadProgress); + _plugin.addErrorCallback(_onError); + + // 监听网络状态 + NetworkMonitor.instance.statusStream.listen((status) { + setState(() { + _networkStatus = status; + }); + }); + } + + Future _loadData() async { + // 请求权限 + await _requestPermissions(); + + // 获取App信息 + _appInfo = await _plugin.getAppInfo(); + + // 获取网络状态 + _networkStatus = _plugin.networkStatus; + + // 获取缓存统计 + _cacheStats = await _plugin.getCacheStats(); + + setState(() {}); + } + + Future _requestPermissions() async { + if (Theme.of(context).platform == TargetPlatform.android) { + await [ + Permission.storage, + Permission.requestInstallPackages, + Permission.notification, + ].request(); + } + } + + void _onUpgradeInfo(UpgradeInfo info) { + setState(() { + _upgradeInfo = info; + }); + + // 显示升级对话框 + UpgradeDialog.show( + context, + upgradeInfo: info, + primaryColor: Theme.of(context).colorScheme.primary, + ); + } + + void _onDownloadProgress(DownloadTask task) { + setState(() { + _currentDownload = task; + }); + } + + void _onError(String error) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(error), + backgroundColor: Colors.red, + ), + ); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('增强版App升级插件'), + backgroundColor: Theme.of(context).colorScheme.inversePrimary, + bottom: TabBar( + controller: _tabController, + tabs: const [ + Tab(text: '基础功能'), + Tab(text: '高级配置'), + Tab(text: '网络监测'), + Tab(text: '版本管理'), + ], + ), + ), + body: TabBarView( + controller: _tabController, + children: [ + _buildBasicTab(), + _buildConfigTab(), + _buildNetworkTab(), + _buildVersionTab(), + ], + ), + ); + } + + // 基础功能标签页 + Widget _buildBasicTab() { + return SingleChildScrollView( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + // App信息卡片 + if (_appInfo != null) + Card( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + 'App信息', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 12), + Text('应用名称: ${_appInfo!['appName']}'), + Text('包名: ${_appInfo!['packageName']}'), + Text('版本: ${_appInfo!['version']}'), + Text('构建号: ${_appInfo!['buildNumber']}'), + ], + ), + ), + ), + + const SizedBox(height: 16), + + // 升级信息卡片 + if (_upgradeInfo != null) + Card( + color: Colors.amber.shade50, + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + const Icon(Icons.system_update, color: Colors.amber), + const SizedBox(width: 8), + const Text( + '发现新版本', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + ], + ), + const SizedBox(height: 12), + Text('版本: ${_upgradeInfo!.versionName}'), + Text('强制更新: ${_upgradeInfo!.isForceUpdate ? "是" : "否"}'), + if (_upgradeInfo!.apkSize != null) Text('大小: ${_formatBytes(_upgradeInfo!.apkSize!)}'), + ], + ), + ), + ), + + const SizedBox(height: 16), + + // 下载进度卡片 + if (_currentDownload != null) + Card( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + '下载进度', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 12), + LinearProgressIndicator(value: _currentDownload!.progress), + const SizedBox(height: 8), + Text('状态: ${_getDownloadStatusText(_currentDownload!.status)}'), + Text('进度: ${(_currentDownload!.progress * 100).toStringAsFixed(1)}%'), + if (_currentDownload!.totalSize != null) + Text( + '${_formatBytes(_currentDownload!.downloadedSize)} / ${_formatBytes(_currentDownload!.totalSize!)}'), + if (_currentDownload!.errorMessage != null) + Text('错误: ${_currentDownload!.errorMessage}', style: const TextStyle(color: Colors.red)), + ], + ), + ), + ), + + const SizedBox(height: 16), + + // 操作按钮 + Wrap( + spacing: 8, + runSpacing: 8, + children: [ + ElevatedButton.icon( + onPressed: () => _checkUpdate(), + icon: const Icon(Icons.refresh), + label: const Text('检查更新'), + ), + if (_currentDownload != null) ...[ + if (_currentDownload!.status == DownloadStatus.downloading) + ElevatedButton.icon( + onPressed: () => _plugin.pauseDownload(), + icon: const Icon(Icons.pause), + label: const Text('暂停'), + ), + if (_currentDownload!.status == DownloadStatus.paused) + ElevatedButton.icon( + onPressed: () => _plugin.resumeDownload(), + icon: const Icon(Icons.play_arrow), + label: const Text('继续'), + ), + if (_currentDownload!.status == DownloadStatus.failed) + ElevatedButton.icon( + onPressed: () => _plugin.retryDownload(), + icon: const Icon(Icons.replay), + label: const Text('重试'), + ), + OutlinedButton.icon( + onPressed: () => _plugin.cancelDownload(), + icon: const Icon(Icons.cancel), + label: const Text('取消'), + ), + ], + ], + ), + ], + ), + ); + } + + // 高级配置标签页 + Widget _buildConfigTab() { + return SingleChildScrollView( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + '升级配置', + style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 16), + + SwitchListTile( + title: const Text('仅WiFi下载'), + subtitle: const Text('只在WiFi环境下自动下载更新'), + value: _wifiOnly, + onChanged: (value) { + setState(() { + _wifiOnly = value; + }); + _plugin.configure(wifiOnly: value); + }, + ), + + SwitchListTile( + title: const Text('自动检查更新'), + subtitle: const Text('定期自动检查是否有新版本'), + value: _autoCheck, + onChanged: (value) { + setState(() { + _autoCheck = value; + }); + _plugin.configure(autoCheck: value); + }, + ), + + SwitchListTile( + title: const Text('断点续传'), + subtitle: const Text('支持暂停后继续下载'), + value: _supportBreakpoint, + onChanged: (value) { + setState(() { + _supportBreakpoint = value; + }); + _plugin.configure(supportBreakpoint: value); + }, + ), + + SwitchListTile( + title: const Text('文件校验'), + subtitle: const Text('下载完成后校验文件完整性'), + value: _verifyIntegrity, + onChanged: (value) { + setState(() { + _verifyIntegrity = value; + }); + _plugin.configure(verifyIntegrity: value); + }, + ), + + const Divider(), + + ListTile( + title: const Text('版本比较策略'), + subtitle: Text(_getVersionStrategyText(_versionStrategy)), + trailing: DropdownButton( + value: _versionStrategy, + onChanged: (value) { + if (value != null) { + setState(() { + _versionStrategy = value; + }); + _plugin.configure(versionStrategy: value); + } + }, + items: VersionCompareStrategy.values.map((strategy) { + return DropdownMenuItem( + value: strategy, + child: Text(_getVersionStrategyText(strategy)), + ); + }).toList(), + ), + ), + + const Divider(), + + // 缓存管理 + Card( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + '缓存管理', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 12), + if (_cacheStats != null) ...[ + Text( + '内存缓存: ${_cacheStats!['memoryCache']['sizeFormatted']} (${_cacheStats!['memoryCache']['count']}项)'), + Text( + '磁盘缓存: ${_cacheStats!['diskCache']['sizeFormatted']} (${_cacheStats!['diskCache']['count']}项)'), + Text('总计: ${_cacheStats!['total']['sizeFormatted']} (${_cacheStats!['total']['count']}项)'), + ], + const SizedBox(height: 12), + Row( + children: [ + ElevatedButton( + onPressed: () async { + _cacheStats = await _plugin.getCacheStats(); + setState(() {}); + }, + child: const Text('刷新'), + ), + const SizedBox(width: 8), + OutlinedButton( + onPressed: () async { + await _plugin.clearCache(); + _cacheStats = await _plugin.getCacheStats(); + if (mounted) { + setState(() {}); + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('缓存已清空')), + ); + } + }, + child: const Text('清空缓存'), + ), + ], + ), + ], + ), + ), + ), + ], + ), + ); + } + + // 网络监测标签页 + Widget _buildNetworkTab() { + return SingleChildScrollView( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + '网络状态', + style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 16), + if (_networkStatus != null) ...[ + Card( + color: _networkStatus!.isConnected ? Colors.green.shade50 : Colors.red.shade50, + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + Icon( + _networkStatus!.isConnected ? Icons.wifi : Icons.wifi_off, + color: _networkStatus!.isConnected ? Colors.green : Colors.red, + ), + const SizedBox(width: 8), + Text( + _networkStatus!.isConnected ? '已连接' : '未连接', + style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + ], + ), + const SizedBox(height: 12), + Text('网络类型: ${_getNetworkTypeText(_networkStatus!.type)}'), + Text('网络质量: ${_getNetworkQualityText(_networkStatus!.quality)}'), + Text('计费网络: ${_networkStatus!.isMetered ? "是" : "否"}'), + if (_networkStatus!.downloadSpeed != null) + Text('下载速度: ${_formatBytes(_networkStatus!.downloadSpeed!.toInt())}/s'), + if (_networkStatus!.ping != null) Text('延迟: ${_networkStatus!.ping} ms'), + const SizedBox(height: 12), + Text( + '适合大文件下载: ${_networkStatus!.isSuitableForLargeDownload ? "是" : "否"}', + style: TextStyle( + color: _networkStatus!.isSuitableForLargeDownload ? Colors.green : Colors.orange, + fontWeight: FontWeight.bold, + ), + ), + ], + ), + ), + ), + + const SizedBox(height: 16), + + // 下载策略建议 + Card( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + '下载策略建议', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 12), + ...NetworkMonitor.instance.getSuggestedDownloadStrategy().entries.map((e) { + return Text('${e.key}: ${e.value}'); + }), + ], + ), + ), + ), + ] else + const Center(child: CircularProgressIndicator()), + ], + ), + ); + } + + // 版本管理标签页 + Widget _buildVersionTab() { + final comparator = _plugin.versionComparator; + + return SingleChildScrollView( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + '版本比较测试', + style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 16), + + // 版本选择 + Row( + children: [ + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text('当前版本:'), + DropdownButton( + value: _currentTestVersion, + isExpanded: true, + onChanged: (value) { + setState(() { + _currentTestVersion = value!; + }); + }, + items: _testVersions.map((v) { + return DropdownMenuItem(value: v, child: Text(v)); + }).toList(), + ), + ], + ), + ), + const SizedBox(width: 16), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text('远程版本:'), + DropdownButton( + value: _remoteTestVersion, + isExpanded: true, + onChanged: (value) { + setState(() { + _remoteTestVersion = value!; + }); + }, + items: _testVersions.map((v) { + return DropdownMenuItem(value: v, child: Text(v)); + }).toList(), + ), + ], + ), + ), + ], + ), + + const SizedBox(height: 16), + + // 比较结果 + Card( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + '比较结果', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 12), + Text('需要更新: ${comparator.isUpdateAvailable(_currentTestVersion, _remoteTestVersion) ? "是" : "否"}'), + Text('更新类型: ${comparator.getVersionDifference(_currentTestVersion, _remoteTestVersion)}'), + Text('主要版本更新: ${comparator.isMajorUpdate(_currentTestVersion, _remoteTestVersion) ? "是" : "否"}'), + Text('次要版本更新: ${comparator.isMinorUpdate(_currentTestVersion, _remoteTestVersion) ? "是" : "否"}'), + Text('修订版本更新: ${comparator.isPatchUpdate(_currentTestVersion, _remoteTestVersion) ? "是" : "否"}'), + ], + ), + ), + ), + + const SizedBox(height: 16), + + // 版本列表排序 + Card( + child: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + '版本排序', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 12), + Text('升序: ${comparator.sortVersions(_testVersions).join(', ')}'), + Text('降序: ${comparator.sortVersions(_testVersions, descending: true).join(', ')}'), + Text('最新版本: ${comparator.getLatestVersion(_testVersions)}'), + ], + ), + ), + ), + ], + ), + ); + } + + // 检查更新 + Future _checkUpdate() async { + // 模拟服务器地址 + const url = 'https://api.example.com/check-update'; + + final info = await _plugin.checkUpdateSmart( + url, + forceRefresh: true, + cacheDuration: const Duration(hours: 1), + ); + + if (info == null) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('已是最新版本')), + ); + } + } + + // 工具方法 + String _formatBytes(int bytes) { + if (bytes < 1024) return '$bytes B'; + if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(2)} KB'; + if (bytes < 1024 * 1024 * 1024) return '${(bytes / (1024 * 1024)).toStringAsFixed(2)} MB'; + return '${(bytes / (1024 * 1024 * 1024)).toStringAsFixed(2)} GB'; + } + + String _getDownloadStatusText(DownloadStatus status) { + switch (status) { + case DownloadStatus.pending: + return '等待中'; + case DownloadStatus.downloading: + return '下载中'; + case DownloadStatus.paused: + return '已暂停'; + case DownloadStatus.completed: + return '已完成'; + case DownloadStatus.failed: + return '失败'; + case DownloadStatus.cancelled: + return '已取消'; + } + } + + String _getNetworkTypeText(NetworkType type) { + switch (type) { + case NetworkType.none: + return '无网络'; + case NetworkType.mobile: + return '移动网络'; + case NetworkType.wifi: + return 'WiFi'; + case NetworkType.ethernet: + return '以太网'; + case NetworkType.bluetooth: + return '蓝牙'; + case NetworkType.vpn: + return 'VPN'; + case NetworkType.other: + return '其他'; + } + } + + String _getNetworkQualityText(NetworkQuality quality) { + switch (quality) { + case NetworkQuality.unknown: + return '未知'; + case NetworkQuality.poor: + return '差'; + case NetworkQuality.moderate: + return '中等'; + case NetworkQuality.good: + return '良好'; + case NetworkQuality.excellent: + return '优秀'; + } + } + + String _getVersionStrategyText(VersionCompareStrategy strategy) { + switch (strategy) { + case VersionCompareStrategy.numeric: + return '数字比较'; + case VersionCompareStrategy.semantic: + return '语义化版本'; + case VersionCompareStrategy.timestamp: + return '时间戳'; + case VersionCompareStrategy.buildNumber: + return '构建号'; + case VersionCompareStrategy.custom: + return '自定义'; + } + } +} diff --git a/example/lib/main_simple.dart b/example/lib/main_simple.dart new file mode 100644 index 0000000..c14639f --- /dev/null +++ b/example/lib/main_simple.dart @@ -0,0 +1,165 @@ +import 'package:app_upgrade_plugin/app_upgrade_simple.dart'; +import 'package:flutter/material.dart'; + +/// 最简单的使用示例 +/// 展示如何用最少的代码实现App升级功能 +void main() { + runApp(const MyApp()); +} + +class MyApp extends StatelessWidget { + const MyApp({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'App Upgrade Simple Example', + theme: ThemeData( + primarySwatch: Colors.blue, + ), + home: const MyHomePage(), + ); + } +} + +class MyHomePage extends StatefulWidget { + const MyHomePage({super.key}); + + @override + State createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + // 模拟的更新检查URL(实际使用时替换为真实的API地址) + static const String checkUpdateUrl = 'https://api.example.com/check-update'; + + @override + void initState() { + super.initState(); + + // 可选:启动时自动检查更新(静默检查) + _checkUpdateOnStart(); + } + + /// 启动时静默检查更新 + void _checkUpdateOnStart() async { + // 延迟2秒,避免启动时界面还未完全加载 + await Future.delayed(const Duration(seconds: 2)); + + if (mounted) { + // 静默检查,有更新才显示对话框 + final info = await AppUpgradeSimple.instance.checkUpdateSilent( + url: checkUpdateUrl, + ); + + if (info != null && mounted) { + // 有更新时显示对话框 + AppUpgradeSimple.instance.checkUpdate( + context: context, + url: checkUpdateUrl, + showNoUpdateToast: false, // 不显示"已是最新版本"提示 + autoDownload: false, // 不自动下载,让用户选择 + ); + } + } + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('App升级 - 极简示例'), + backgroundColor: Theme.of(context).colorScheme.inversePrimary, + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Icon( + Icons.rocket_launch, + size: 80, + color: Colors.blue, + ), + const SizedBox(height: 24), + const Text( + '最简单的App升级实现', + style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 8), + const Text( + '一行代码搞定升级功能', + style: TextStyle(fontSize: 16, color: Colors.grey), + ), + const SizedBox(height: 48), + + // 示例1:最简单的使用方式(一行代码) + ElevatedButton.icon( + onPressed: () { + // 🚀 一行代码检查更新! + AppUpgradeSimple.instance.checkUpdate( + context: context, + url: checkUpdateUrl, + ); + }, + icon: const Icon(Icons.flash_on), + label: const Text('一键检查更新(最简单)'), + style: ElevatedButton.styleFrom( + padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12), + ), + ), + + const SizedBox(height: 16), + + const SizedBox(height: 16), + + const SizedBox(height: 48), + + // 使用说明 + Container( + margin: const EdgeInsets.symmetric(horizontal: 32), + padding: const EdgeInsets.all(16), + decoration: BoxDecoration( + color: Colors.blue.shade50, + borderRadius: BorderRadius.circular(12), + border: Border.all(color: Colors.blue.shade200), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: const [ + Text( + '💡 使用提示', + style: TextStyle( + fontSize: 16, + fontWeight: FontWeight.bold, + color: Colors.blue, + ), + ), + SizedBox(height: 8), + Text('1. 替换 checkUpdateUrl 为您的API地址'), + Text('2. API返回格式请参考文档'), + Text('3. Android需要配置权限和FileProvider'), + Text('4. iOS需要配置App Store地址'), + ], + ), + ), + ], + ), + ), + ); + } +} + +/// API返回格式示例: +/// ```json +/// { +/// "hasUpdate": true, +/// "isForceUpdate": false, +/// "versionCode": "2", +/// "versionName": "1.1.0", +/// "updateContent": "1. 修复已知问题\n2. 优化用户体验", +/// "downloadUrl": "https://example.com/app-v1.1.0.apk", // Android +/// "appStoreUrl": "https://apps.apple.com/app/id123456", // iOS +/// "apkSize": 26214400, +/// "apkMd5": "abc123def456" +/// } +/// ``` diff --git a/example/pubspec.lock b/example/pubspec.lock new file mode 100644 index 0000000..fabc538 --- /dev/null +++ b/example/pubspec.lock @@ -0,0 +1,741 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + app_upgrade_plugin: + dependency: "direct main" + description: + path: ".." + relative: true + source: path + version: "1.0.0" + args: + dependency: transitive + description: + name: args + sha256: d0481093c50b1da8910eb0bb301626d4d8eb7284aa739614d2b394ee09e3ea04 + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.7.0" + async: + dependency: transitive + description: + name: async + sha256: d2872f9c19731c2e5f10444b14686eb7cc85c76274bd6c16e1816bff9a3bab63 + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.12.0" + boolean_selector: + dependency: transitive + description: + name: boolean_selector + sha256: "8aab1771e1243a5063b8b0ff68042d67334e3feab9e95b9490f9a6ebf73b42ea" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.2" + characters: + dependency: transitive + description: + name: characters + sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803 + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.4.0" + clock: + dependency: transitive + description: + name: clock + sha256: fddb70d9b5277016c77a80201021d40a2247104d9f4aa7bab7157b7e3f05b84b + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.1.2" + collection: + dependency: transitive + description: + name: collection + sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76" + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.19.1" + connectivity_plus: + dependency: transitive + description: + name: connectivity_plus + sha256: b5e72753cf63becce2c61fd04dfe0f1c430cc5278b53a1342dc5ad839eab29ec + url: "https://pub.flutter-io.cn" + source: hosted + version: "6.1.5" + connectivity_plus_platform_interface: + dependency: transitive + description: + name: connectivity_plus_platform_interface + sha256: "42657c1715d48b167930d5f34d00222ac100475f73d10162ddf43e714932f204" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.0.1" + crypto: + dependency: transitive + description: + name: crypto + sha256: "1e445881f28f22d6140f181e07737b22f1e099a5e1ff94b0af2f9e4a463f4855" + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.0.6" + cupertino_icons: + dependency: "direct main" + description: + name: cupertino_icons + sha256: ba631d1c7f7bef6b729a622b7b752645a2d076dba9976925b8f25725a30e1ee6 + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.0.8" + dbus: + dependency: transitive + description: + name: dbus + sha256: "79e0c23480ff85dc68de79e2cd6334add97e48f7f4865d17686dd6ea81a47e8c" + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.7.11" + device_info_plus: + dependency: transitive + description: + name: device_info_plus + sha256: "98f28b42168cc509abc92f88518882fd58061ea372d7999aecc424345c7bff6a" + url: "https://pub.flutter-io.cn" + source: hosted + version: "11.5.0" + device_info_plus_platform_interface: + dependency: transitive + description: + name: device_info_plus_platform_interface + sha256: e1ea89119e34903dca74b883d0dd78eb762814f97fb6c76f35e9ff74d261a18f + url: "https://pub.flutter-io.cn" + source: hosted + version: "7.0.3" + dio: + dependency: transitive + description: + name: dio + sha256: d90ee57923d1828ac14e492ca49440f65477f4bb1263575900be731a3dac66a9 + url: "https://pub.flutter-io.cn" + source: hosted + version: "5.9.0" + dio_web_adapter: + dependency: transitive + description: + name: dio_web_adapter + sha256: "7586e476d70caecaf1686d21eee7247ea43ef5c345eab9e0cc3583ff13378d78" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.1" + fake_async: + dependency: transitive + description: + name: fake_async + sha256: "6a95e56b2449df2273fd8c45a662d6947ce1ebb7aafe80e550a3f68297f3cacc" + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.3.2" + ffi: + dependency: transitive + description: + name: ffi + sha256: "289279317b4b16eb2bb7e271abccd4bf84ec9bdcbe999e278a94b804f5630418" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.4" + file: + dependency: transitive + description: + name: file + sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4 + url: "https://pub.flutter-io.cn" + source: hosted + version: "7.0.1" + flutter: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" + flutter_driver: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + flutter_lints: + dependency: "direct dev" + description: + name: flutter_lints + sha256: "5398f14efa795ffb7a33e9b6a08798b26a180edac4ad7db3f231e40f82ce11e1" + url: "https://pub.flutter-io.cn" + source: hosted + version: "5.0.0" + flutter_local_notifications: + dependency: transitive + description: + name: flutter_local_notifications + sha256: ef41ae901e7529e52934feba19ed82827b11baa67336829564aeab3129460610 + url: "https://pub.flutter-io.cn" + source: hosted + version: "18.0.1" + flutter_local_notifications_linux: + dependency: transitive + description: + name: flutter_local_notifications_linux + sha256: "8f685642876742c941b29c32030f6f4f6dacd0e4eaecb3efbb187d6a3812ca01" + url: "https://pub.flutter-io.cn" + source: hosted + version: "5.0.0" + flutter_local_notifications_platform_interface: + dependency: transitive + description: + name: flutter_local_notifications_platform_interface + sha256: "6c5b83c86bf819cdb177a9247a3722067dd8cc6313827ce7c77a4b238a26fd52" + url: "https://pub.flutter-io.cn" + source: hosted + version: "8.0.0" + flutter_localizations: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" + flutter_test: + dependency: "direct dev" + description: flutter + source: sdk + version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + fluttertoast: + dependency: transitive + description: + name: fluttertoast + sha256: "25e51620424d92d3db3832464774a6143b5053f15e382d8ffbfd40b6e795dcf1" + url: "https://pub.flutter-io.cn" + source: hosted + version: "8.2.12" + fuchsia_remote_debug_protocol: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + http: + dependency: transitive + description: + name: http + sha256: bb2ce4590bc2667c96f318d68cac1b5a7987ec819351d32b1c987239a815e007 + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.5.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571" + url: "https://pub.flutter-io.cn" + source: hosted + version: "4.1.2" + integration_test: + dependency: "direct dev" + description: flutter + source: sdk + version: "0.0.0" + intl: + dependency: "direct main" + description: + name: intl + sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.19.0" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: c35baad643ba394b40aac41080300150a4f08fd0fd6a10378f8f7c6bc161acec + url: "https://pub.flutter-io.cn" + source: hosted + version: "10.0.8" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: f8b613e7e6a13ec79cfdc0e97638fddb3ab848452eff057653abd3edba760573 + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.0.9" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.0.1" + lints: + dependency: transitive + description: + name: lints + sha256: c35bb79562d980e9a453fc715854e1ed39e24e7d0297a880ef54e17f9874a9d7 + url: "https://pub.flutter-io.cn" + source: hosted + version: "5.1.1" + matcher: + dependency: transitive + description: + name: matcher + sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2 + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.12.17" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.11.1" + meta: + dependency: transitive + description: + name: meta + sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.16.0" + mime: + dependency: transitive + description: + name: mime + sha256: "41a20518f0cb1256669420fdba0cd90d21561e560ac240f26ef8322e45bb7ed6" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.0.0" + nm: + dependency: transitive + description: + name: nm + sha256: "2c9aae4127bdc8993206464fcc063611e0e36e72018696cd9631023a31b24254" + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.5.0" + package_info_plus: + dependency: transitive + description: + name: package_info_plus + sha256: "16eee997588c60225bda0488b6dcfac69280a6b7a3cf02c741895dd370a02968" + url: "https://pub.flutter-io.cn" + source: hosted + version: "8.3.1" + package_info_plus_platform_interface: + dependency: transitive + description: + name: package_info_plus_platform_interface + sha256: "202a487f08836a592a6bd4f901ac69b3a8f146af552bbd14407b6b41e1c3f086" + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.2.1" + path: + dependency: transitive + description: + name: path + sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5" + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.9.1" + path_provider: + dependency: transitive + description: + name: path_provider + sha256: "50c5dd5b6e1aaf6fb3a78b33f6aa3afca52bf903a8a5298f53101fdaee55bbcd" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.5" + path_provider_android: + dependency: transitive + description: + name: path_provider_android + sha256: "993381400e94d18469750e5b9dcb8206f15bc09f9da86b9e44a9b0092a0066db" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.2.18" + path_provider_foundation: + dependency: transitive + description: + name: path_provider_foundation + sha256: "16eef174aacb07e09c351502740fa6254c165757638eba1e9116b0a781201bbd" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.4.2" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279 + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.2.1" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.2" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + sha256: bd6f00dbd873bfb70d0761682da2b3a2c2fccc2b9e84c495821639601d81afe7 + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.3.0" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + sha256: "59adad729136f01ea9e35a48f5d1395e25cba6cea552249ddbe9cf950f5d7849" + url: "https://pub.flutter-io.cn" + source: hosted + version: "11.4.0" + permission_handler_android: + dependency: transitive + description: + name: permission_handler_android + sha256: d3971dcdd76182a0c198c096b5db2f0884b0d4196723d21a866fc4cdea057ebc + url: "https://pub.flutter-io.cn" + source: hosted + version: "12.1.0" + permission_handler_apple: + dependency: transitive + description: + name: permission_handler_apple + sha256: f000131e755c54cf4d84a5d8bd6e4149e262cc31c5a8b1d698de1ac85fa41023 + url: "https://pub.flutter-io.cn" + source: hosted + version: "9.4.7" + permission_handler_html: + dependency: transitive + description: + name: permission_handler_html + sha256: "38f000e83355abb3392140f6bc3030660cfaef189e1f87824facb76300b4ff24" + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.1.3+5" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + sha256: eb99b295153abce5d683cac8c02e22faab63e50679b937fa1bf67d58bb282878 + url: "https://pub.flutter-io.cn" + source: hosted + version: "4.3.0" + permission_handler_windows: + dependency: transitive + description: + name: permission_handler_windows + sha256: "1a790728016f79a41216d88672dbc5df30e686e811ad4e698bfc51f76ad91f1e" + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.2.1" + petitparser: + dependency: transitive + description: + name: petitparser + sha256: "07c8f0b1913bcde1ff0d26e57ace2f3012ccbf2b204e070290dad3bb22797646" + url: "https://pub.flutter-io.cn" + source: hosted + version: "6.1.0" + platform: + dependency: transitive + description: + name: platform + sha256: "5d6b1b0036a5f331ebc77c850ebc8506cbc1e9416c27e59b439f917a902a4984" + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.1.6" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.8" + process: + dependency: transitive + description: + name: process + sha256: "107d8be718f120bbba9dcd1e95e3bd325b1b4a4f07db64154635ba03f2567a0d" + url: "https://pub.flutter-io.cn" + source: hosted + version: "5.0.3" + shared_preferences: + dependency: transitive + description: + name: shared_preferences + sha256: "6e8bf70b7fef813df4e9a36f658ac46d107db4b4cfe1048b477d4e453a8159f5" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.5.3" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: a2608114b1ffdcbc9c120eb71a0e207c71da56202852d4aab8a5e30a82269e74 + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.4.12" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: "6a52cfcdaeac77cad8c97b539ff688ccfc458c007b4db12be584fbe5c0e49e03" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.5.4" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: "580abfd40f415611503cae30adf626e6656dfb2f0cee8f465ece7b6defb40f2f" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.4.1" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: "57cbf196c486bc2cf1f02b85784932c6094376284b3ad5779d1b1c6c6a816b80" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.4.1" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: c49bd060261c9a3f0ff445892695d6212ff603ef3115edbb448509d407600019 + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.4.3" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: "94ef0f72b2d71bc3e700e025db3710911bd51a71cefb65cc609dd0d9a982e3c1" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.4.1" + sky_engine: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + source_span: + dependency: transitive + description: + name: source_span + sha256: "254ee5351d6cb365c859e20ee823c3bb479bf4a293c22d17a9f1bf144ce86f7c" + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.10.1" + stack_trace: + dependency: transitive + description: + name: stack_trace + sha256: "8b27215b45d22309b5cddda1aa2b19bdfec9df0e765f2de506401c071d38d1b1" + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.12.1" + stream_channel: + dependency: transitive + description: + name: stream_channel + sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.4" + string_scanner: + dependency: transitive + description: + name: string_scanner + sha256: "921cd31725b72fe181906c6a94d987c78e3b98c2e205b397ea399d4054872b43" + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.4.1" + sync_http: + dependency: transitive + description: + name: sync_http + sha256: "7f0cd72eca000d2e026bcd6f990b81d0ca06022ef4e32fb257b30d3d1014a961" + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.3.1" + term_glyph: + dependency: transitive + description: + name: term_glyph + sha256: "7f554798625ea768a7518313e58f83891c7f5024f88e46e7182a4558850a4b8e" + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.2.2" + test_api: + dependency: transitive + description: + name: test_api + sha256: fb31f383e2ee25fbbfe06b40fe21e1e458d14080e3c67e7ba0acfde4df4e0bbd + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.7.4" + timezone: + dependency: transitive + description: + name: timezone + sha256: dd14a3b83cfd7cb19e7888f1cbc20f258b8d71b54c06f79ac585f14093a287d1 + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.10.1" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006 + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.4.0" + url_launcher: + dependency: transitive + description: + name: url_launcher + sha256: f6a7e5c4835bb4e3026a04793a4199ca2d14c739ec378fdfe23fc8075d0439f8 + url: "https://pub.flutter-io.cn" + source: hosted + version: "6.3.2" + url_launcher_android: + dependency: transitive + description: + name: url_launcher_android + sha256: "81777b08c498a292d93ff2feead633174c386291e35612f8da438d6e92c4447e" + url: "https://pub.flutter-io.cn" + source: hosted + version: "6.3.20" + url_launcher_ios: + dependency: transitive + description: + name: url_launcher_ios + sha256: d80b3f567a617cb923546034cc94bfe44eb15f989fe670b37f26abdb9d939cb7 + url: "https://pub.flutter-io.cn" + source: hosted + version: "6.3.4" + url_launcher_linux: + dependency: transitive + description: + name: url_launcher_linux + sha256: "4e9ba368772369e3e08f231d2301b4ef72b9ff87c31192ef471b380ef29a4935" + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.2.1" + url_launcher_macos: + dependency: transitive + description: + name: url_launcher_macos + sha256: c043a77d6600ac9c38300567f33ef12b0ef4f4783a2c1f00231d2b1941fea13f + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.2.3" + url_launcher_platform_interface: + dependency: transitive + description: + name: url_launcher_platform_interface + sha256: "552f8a1e663569be95a8190206a38187b531910283c3e982193e4f2733f01029" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.3.2" + url_launcher_web: + dependency: transitive + description: + name: url_launcher_web + sha256: "4bd2b7b4dc4d4d0b94e5babfffbca8eac1a126c7f3d6ecbc1a11013faa3abba2" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.4.1" + url_launcher_windows: + dependency: transitive + description: + name: url_launcher_windows + sha256: "3284b6d2ac454cf34f114e1d3319866fdd1e19cdc329999057e44ffe936cfa77" + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.1.4" + vector_math: + dependency: transitive + description: + name: vector_math + sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.4" + vm_service: + dependency: transitive + description: + name: vm_service + sha256: "0968250880a6c5fe7edc067ed0a13d4bae1577fe2771dcf3010d52c4a9d3ca14" + url: "https://pub.flutter-io.cn" + source: hosted + version: "14.3.1" + web: + dependency: transitive + description: + name: web + sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a" + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.1.1" + webdriver: + dependency: transitive + description: + name: webdriver + sha256: "3d773670966f02a646319410766d3b5e1037efb7f07cc68f844d5e06cd4d61c8" + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.0.4" + win32: + dependency: transitive + description: + name: win32 + sha256: "329edf97fdd893e0f1e3b9e88d6a0e627128cc17cc316a8d67fda8f1451178ba" + url: "https://pub.flutter-io.cn" + source: hosted + version: "5.13.0" + win32_registry: + dependency: transitive + description: + name: win32_registry + sha256: "6f1b564492d0147b330dd794fee8f512cec4977957f310f9951b5f9d83618dae" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.0" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + sha256: "7a3f37b05d989967cdddcbb571f1ea834867ae2faa29725fd085180e0883aa15" + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.1.0" + xml: + dependency: transitive + description: + name: xml + sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226 + url: "https://pub.flutter-io.cn" + source: hosted + version: "6.5.0" +sdks: + dart: ">=3.7.0 <4.0.0" + flutter: ">=3.29.0" diff --git a/example/pubspec.yaml b/example/pubspec.yaml new file mode 100644 index 0000000..69ac2e0 --- /dev/null +++ b/example/pubspec.yaml @@ -0,0 +1,91 @@ +name: app_upgrade_plugin_example +description: "Demonstrates how to use the app_upgrade_plugin plugin." +# The following line prevents the package from being accidentally published to +# pub.dev using `flutter pub publish`. This is preferred for private packages. +publish_to: 'none' # Remove this line if you wish to publish to pub.dev + +environment: + sdk: '>=3.0.0 <4.0.0' + +# Dependencies specify other packages that your package needs in order to work. +# To automatically upgrade your package dependencies to the latest versions +# consider running `flutter pub upgrade --major-versions`. Alternatively, +# dependencies can be manually updated by changing the version numbers below to +# the latest version available on pub.dev. To see which dependencies have newer +# versions available, run `flutter pub outdated`. +dependencies: + flutter: + sdk: flutter + flutter_localizations: + sdk: flutter + + intl: ^0.19.0 + + + app_upgrade_plugin: + # When depending on this package from a real application you should use: + # app_upgrade_plugin: ^x.y.z + # See https://dart.dev/tools/pub/dependencies#version-constraints + # The example app is bundled with the plugin so we use a path dependency on + # the parent directory to use the current plugin's version. + path: ../ + + # The following adds the Cupertino Icons font to your application. + # Use with the CupertinoIcons class for iOS style icons. + cupertino_icons: ^1.0.8 + permission_handler: ^11.3.1 + +dev_dependencies: + integration_test: + sdk: flutter + flutter_test: + sdk: flutter + + # The "flutter_lints" package below contains a set of recommended lints to + # encourage good coding practices. The lint set provided by the package is + # activated in the `analysis_options.yaml` file located at the root of your + # package. See that file for information about deactivating specific lint + # rules and activating additional ones. + flutter_lints: ^5.0.0 + +# For information on the generic Dart part of this file, see the +# following page: https://dart.dev/tools/pub/pubspec + +# The following section is specific to Flutter packages. +flutter: + + # The following line ensures that the Material Icons font is + # included with your application, so that you can use the icons in + # the material Icons class. + uses-material-design: true + + # To add assets to your application, add an assets section, like this: + # assets: + # - images/a_dot_burr.jpeg + # - images/a_dot_ham.jpeg + + # An image asset can refer to one or more resolution-specific "variants", see + # https://flutter.dev/to/resolution-aware-images + + # For details regarding adding assets from package dependencies, see + # https://flutter.dev/to/asset-from-package + + # To add custom fonts to your application, add a fonts section here, + # in this "flutter" section. Each entry in this list should have a + # "family" key with the font family name, and a "fonts" key with a + # list giving the asset and other descriptors for the font. For + # example: + # fonts: + # - family: Schyler + # fonts: + # - asset: fonts/Schyler-Regular.ttf + # - asset: fonts/Schyler-Italic.ttf + # style: italic + # - family: Trajan Pro + # fonts: + # - asset: fonts/TrajanPro.ttf + # - asset: fonts/TrajanPro_Bold.ttf + # weight: 700 + # + # For details regarding fonts from package dependencies, + # see https://flutter.dev/to/font-from-package diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart new file mode 100644 index 0000000..0fcabc0 --- /dev/null +++ b/example/test/widget_test.dart @@ -0,0 +1,160 @@ +// This is a basic Flutter widget test. +// +// To perform an interaction with a widget in your test, use the WidgetTester +// utility in the flutter_test package. For example, you can send tap and scroll +// gestures. You can also use WidgetTester to find child widgets in the widget +// tree, read text, and verify that the values of widget properties are correct. + +import 'package:app_upgrade_plugin/app_upgrade_plugin.dart'; +import 'package:app_upgrade_plugin/app_upgrade_plugin_enhanced.dart'; +import 'package:app_upgrade_plugin_example/main.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; + +class MockAppUpgradePluginEnhanced implements AppUpgradePluginEnhanced { + // 在这里 mock AppUpgradePluginEnhanced 的所有方法和属性 + // ... + // 你可以根据测试需要返回特定的值 + + @override + void addDownloadCallback(DownloadCallback callback) {} + @override + void addErrorCallback(ErrorCallback callback) {} + @override + void addUpgradeCallback(UpgradeCallback callback) {} + @override + Future checkApkExists(String version, String? md5) async => false; + @override + Future checkUpdateSmart(String url, + {Map? params, bool forceRefresh = false, Duration? cacheDuration}) async => + null; + @override + Future clearCache() async {} + @override + UpgradeConfig get config => UpgradeConfig.instance; + @override + void configure( + {bool? debugMode, + int? checkIntervalHours, + bool? autoCheck, + bool? wifiOnly, + int? downloadTimeout, + int? connectTimeout, + int? maxRetryCount, + bool? supportBreakpoint, + bool? verifyIntegrity, + VersionCompareStrategy? versionStrategy, + Map? customHeaders}) {} + @override + void dispose() {} + @override + Future downloadApkSmart(String url, + {String? versionName, + Function(DownloadProgress p1)? onProgress, + String? savePath, + String? md5, + String? sha256, + bool resumeIfExists = true}) async => + null; + @override + Future> getAppInfo() async => {}; + @override + Future> getCacheStats() async => {}; + @override + DownloadTask? getCurrentDownloadTask() => null; + @override + Future getPlatformVersion() async => 'mock'; + @override + Future goToAppStore(String url) async => false; + @override + Future installApkSmart(String filePath) async => false; + @override + NetworkStatus? get networkStatus => NetworkStatus( + type: NetworkType.wifi, + quality: NetworkQuality.good, + isConnected: true, + isMetered: false, + ); + @override + void pauseDownload() {} + @override + void removeDownloadCallback(DownloadCallback callback) {} + @override + void removeErrorCallback(ErrorCallback callback) {} + @override + void removeUpgradeCallback(UpgradeCallback callback) {} + @override + Future resumeDownload() async => false; + @override + Future retryDownload() async => false; + @override + Future refreshNetworkStatus() async {} + @override + VersionComparator get versionComparator => VersionComparator(); + @override + void cancelDownload() {} +} + +void main() { + // 关键修复:确保测试绑定已初始化 + TestWidgetsFlutterBinding.ensureInitialized(); + + setUp(() { + // 为所有方法通道设置一个默认的 mock 处理器,防止未 mock 的调用抛出异常 + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('plugins.flutter.io/package_info'), + (MethodCall methodCall) async { + if (methodCall.method == 'getAll') { + return { + 'appName': 'app_upgrade_plugin_example', + 'packageName': 'com.example.app_upgrade_plugin_example', + 'version': '1.0.0', + 'buildNumber': '1', + }; + } + return null; + }, + ); + + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('dexterous.com/flutter/local_notifications'), + (MethodCall methodCall) async => null, // 对通知插件的所有调用都返回 null + ); + + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('dev.flutter.plugins/connectivity'), + (MethodCall methodCall) async { + if (methodCall.method == 'check') { + return 'wifi'; // 模拟网络状态为 WiFi + } + return null; + }, + ); + }); + + testWidgets('Verify Platform version', (WidgetTester tester) async { + // 使用 Mock enhanced plugin + AppUpgradeSimple.instance = AppUpgradeSimple.private(plugin: MockAppUpgradePluginEnhanced()); + + await tester.pumpWidget(const MyApp()); + + // 只验证应用能正常启动,不验证具体的UI内容 + expect(find.byType(MyApp), findsOneWidget); + }); + + testWidgets('Verify Network Status Detection', (WidgetTester tester) async { + // 使用 Mock enhanced plugin + final mockPlugin = MockAppUpgradePluginEnhanced(); + AppUpgradeSimple.instance = AppUpgradeSimple.private(plugin: mockPlugin); + + await tester.pumpWidget(const MyApp()); + + // 验证网络状态检测功能 + final networkStatus = mockPlugin.networkStatus; + + expect(networkStatus, isNotNull); + expect(networkStatus!.isConnected, isTrue); + expect(networkStatus.type, equals(NetworkType.wifi)); + expect(networkStatus.isMetered, isFalse); + }); +} diff --git a/ios/.gitignore b/ios/.gitignore new file mode 100644 index 0000000..034771f --- /dev/null +++ b/ios/.gitignore @@ -0,0 +1,38 @@ +.idea/ +.vagrant/ +.sconsign.dblite +.svn/ + +.DS_Store +*.swp +profile + +DerivedData/ +build/ +GeneratedPluginRegistrant.h +GeneratedPluginRegistrant.m + +.generated/ + +*.pbxuser +*.mode1v3 +*.mode2v3 +*.perspectivev3 + +!default.pbxuser +!default.mode1v3 +!default.mode2v3 +!default.perspectivev3 + +xcuserdata + +*.moved-aside + +*.pyc +*sync/ +Icon? +.tags* + +/Flutter/Generated.xcconfig +/Flutter/ephemeral/ +/Flutter/flutter_export_environment.sh diff --git a/ios/Assets/.gitkeep b/ios/Assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/ios/Classes/AppUpgradePlugin.swift b/ios/Classes/AppUpgradePlugin.swift new file mode 100644 index 0000000..167334a --- /dev/null +++ b/ios/Classes/AppUpgradePlugin.swift @@ -0,0 +1,50 @@ +import Flutter +import UIKit + +public class AppUpgradePlugin: NSObject, FlutterPlugin { + public static func register(with registrar: FlutterPluginRegistrar) { + let channel = FlutterMethodChannel(name: "app_upgrade_plugin", binaryMessenger: registrar.messenger()) + let instance = AppUpgradePlugin() + registrar.addMethodCallDelegate(instance, channel: channel) + } + + public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + switch call.method { + case "getPlatformVersion": + result( + "iOS " + UIDevice.current.systemVersion + ) + case "goToAppStore": + let args = call.arguments as? [String: Any] + let url = args?["url"] as? String + if let url = url { + if let appUrl = URL(string: url) { + UIApplication.shared.open(appUrl, options: [:], completionHandler: nil) + result(true) + } else { + result(false) + } + } else { + result(false) + } + case "getDownloadPath": + let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) + result(paths.first?.path) + case "checkApkExists": + // Not applicable for iOS + result(false) + case "goToMarket": + // For iOS, goToMarket is the same as goToAppStore + let args = call.arguments as? [String: Any] + let url = args?["url"] as? String + if let url = url, let appUrl = URL(string: url) { + UIApplication.shared.open(appUrl, options: [:], completionHandler: nil) + result(true) + } else { + result(false) + } + default: + result(FlutterMethodNotImplemented) + } + } +} \ No newline at end of file diff --git a/ios/Resources/PrivacyInfo.xcprivacy b/ios/Resources/PrivacyInfo.xcprivacy new file mode 100644 index 0000000..a34b7e2 --- /dev/null +++ b/ios/Resources/PrivacyInfo.xcprivacy @@ -0,0 +1,14 @@ + + + + + NSPrivacyTrackingDomains + + NSPrivacyAccessedAPITypes + + NSPrivacyCollectedDataTypes + + NSPrivacyTracking + + + diff --git a/ios/app_upgrade_plugin.podspec b/ios/app_upgrade_plugin.podspec new file mode 100644 index 0000000..d5d1a83 --- /dev/null +++ b/ios/app_upgrade_plugin.podspec @@ -0,0 +1,29 @@ +# +# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html. +# Run `pod lib lint app_upgrade_plugin.podspec` to validate before publishing. +# +Pod::Spec.new do |s| + s.name = 'app_upgrade_plugin' + s.version = '0.0.1' + s.summary = 'A new Flutter plugin project.' + s.description = <<-DESC +A new Flutter plugin project. + DESC + s.homepage = 'http://example.com' + s.license = { :file => '../LICENSE' } + s.author = { 'Your Company' => 'email@example.com' } + s.source = { :path => '.' } + s.source_files = 'Classes/**/*' + s.dependency 'Flutter' + s.platform = :ios, '12.0' + + # Flutter.framework does not contain a i386 slice. + s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' } + s.swift_version = '5.0' + + # If your plugin requires a privacy manifest, for example if it uses any + # required reason APIs, update the PrivacyInfo.xcprivacy file to describe your + # plugin's privacy impact, and then uncomment this line. For more information, + # see https://developer.apple.com/documentation/bundleresources/privacy_manifest_files + # s.resource_bundles = {'app_upgrade_plugin_privacy' => ['Resources/PrivacyInfo.xcprivacy']} +end diff --git a/lib/app_upgrade_plugin.dart b/lib/app_upgrade_plugin.dart new file mode 100644 index 0000000..baabb4f --- /dev/null +++ b/lib/app_upgrade_plugin.dart @@ -0,0 +1,125 @@ +import 'dart:io'; + +// 确保方法通道实现被导入,以便正确初始化 +// ignore: unused_import +import 'app_upgrade_plugin_method_channel.dart'; +import 'app_upgrade_plugin_platform_interface.dart'; +import 'core/http_config.dart'; +import 'models/upgrade_info.dart'; + +// 简化版API(推荐使用) +export 'app_upgrade_simple.dart'; +// HTTP配置 +export 'core/http_config.dart'; +// 权限帮助类 +export 'core/permission_helper.dart'; +export 'models/upgrade_info.dart'; +export 'widgets/widgets.dart'; + +class AppUpgradePlugin { + // 单例模式,确保全局只有一个实例 + static final AppUpgradePlugin _instance = AppUpgradePlugin._internal(); + + factory AppUpgradePlugin() => _instance; + + /// 全局设置:是否忽略证书验证 + /// 默认情况下,插件会根据编译模式自动决定(Debug模式忽略,Release模式验证) + /// 设置为true将强制在所有环境下忽略证书验证 + static bool ignoreCertificate = true; + + AppUpgradePlugin._internal() { + // 确保平台接口已正确初始化 + _ensurePlatformInitialized(); + + // 根据全局设置配置HTTP + if (ignoreCertificate) { + configureHttp(HttpConfig.unsafe); + } + } + + // 确保平台接口初始化 + void _ensurePlatformInitialized() { + // 触发平台接口的懒加载初始化 + final _ = AppUpgradePluginPlatform.instance; + } + + /// 配置HTTP设置 + /// + /// 示例: + /// ```dart + /// // 开发环境配置 + /// AppUpgradePlugin().configureHttp(HttpConfig.development); + /// + /// // 自定义配置 + /// AppUpgradePlugin().configureHttp(HttpConfig( + /// ignoreCertificate: true, + /// connectTimeout: 60, + /// defaultMethod: 'POST', + /// )); + /// ``` + void configureHttp(HttpConfig config) { + AppUpgradePluginPlatform.instance.configureHttp(config); + } + + /// 获取平台版本 + Future getPlatformVersion() { + return AppUpgradePluginPlatform.instance.getPlatformVersion(); + } + + Future getAndroidSdkVersion() { + return AppUpgradePluginPlatform.instance.getAndroidSdkVersion(); + } + + /// 获取当前App信息 + Future> getAppInfo() { + return AppUpgradePluginPlatform.instance.getAppInfo(); + } + + /// 检查更新 + /// [url] 检查更新的接口地址 + /// [params] 额外的请求参数 + Future checkUpdate(String url, {Map? params}) { + return AppUpgradePluginPlatform.instance.checkUpdate(url, params: params); + } + + /// 下载APK文件(仅Android) + /// [url] APK下载地址 + /// [onProgress] 下载进度回调 + /// [savePath] 保存路径(可选,默认使用系统下载目录) + Future downloadApk(String url, {Function(DownloadProgress)? onProgress, String? savePath}) { + if (!Platform.isAndroid) { + throw UnsupportedError('downloadApk only supports Android platform'); + } + return AppUpgradePluginPlatform.instance.downloadApk(url, onProgress: onProgress, savePath: savePath); + } + + /// 安装APK(仅Android) + /// [filePath] APK文件路径 + Future installApk(String filePath) { + if (!Platform.isAndroid) { + return Future.value(false); + } + return AppUpgradePluginPlatform.instance.installApk(filePath); + } + + /// 跳转到应用商店 + /// [url] 应用商店地址 + Future goToAppStore(String url) { + return AppUpgradePluginPlatform.instance.goToAppStore(url); + } + + /// 获取下载目录路径 + Future getDownloadPath() { + return AppUpgradePluginPlatform.instance.getDownloadPath(); + } + + /// 检查是否已下载指定版本的APK + /// [version] 版本号 + /// [md5] MD5值(可选) + Future checkApkExists(String version, String? md5) { + if (!Platform.isAndroid) { + return Future.value(false); + } + return AppUpgradePluginPlatform.instance.checkApkExists(version, md5); + } +} diff --git a/lib/app_upgrade_plugin_enhanced.dart b/lib/app_upgrade_plugin_enhanced.dart new file mode 100644 index 0000000..dd18772 --- /dev/null +++ b/lib/app_upgrade_plugin_enhanced.dart @@ -0,0 +1,638 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; // Added for MethodChannel +import 'package:flutter/widgets.dart'; // Added for WidgetsBinding +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:package_info_plus/package_info_plus.dart'; +import 'package:url_launcher/url_launcher.dart'; + +import 'app_upgrade_plugin_platform_interface.dart'; +import 'core/cache_manager.dart'; +import 'core/download_manager.dart'; +import 'core/network_monitor.dart'; +import 'core/notification_helper.dart'; +import 'core/upgrade_config.dart'; +import 'core/version_comparator.dart'; +import 'models/upgrade_info.dart'; + +export 'core/cache_manager.dart'; +export 'core/download_manager.dart'; +export 'core/network_monitor.dart'; +export 'core/upgrade_config.dart'; +export 'core/version_comparator.dart'; +export 'models/upgrade_info.dart'; +export 'widgets/widgets.dart'; + +/// 升级回调 +typedef UpgradeCallback = void Function(UpgradeInfo info); +typedef DownloadCallback = void Function(DownloadTask task); +typedef ErrorCallback = void Function(String error); + +/// 增强版App升级插件 +class AppUpgradePluginEnhanced { + static AppUpgradePluginEnhanced? _instance; + + /// 获取单例实例 + static AppUpgradePluginEnhanced get instance { + _instance ??= AppUpgradePluginEnhanced._(); + return _instance!; + } + + AppUpgradePluginEnhanced._() { + _init(); + } + // 核心组件 + + final UpgradeConfig _config = UpgradeConfig.instance; + final DownloadManager _downloadManager = DownloadManager.instance; + final NetworkMonitor _networkMonitor = NetworkMonitor.instance; + final CacheManager _cacheManager = CacheManager.instance; + final NotificationHelper _notificationHelper = NotificationHelper.instance; + late VersionComparator _versionComparator; + final Dio _dio = Dio(); + + // 回调 + final List _upgradeCallbacks = []; + final List _downloadCallbacks = []; + final List _errorCallbacks = []; + + // 状态 + UpgradeInfo? _currentUpgradeInfo; + String? _currentDownloadTaskId; + Timer? _autoCheckTimer; + StreamSubscription? _networkSubscription; + + // 内存管理 + final List _subscriptions = []; + final Map> _weakRefs = {}; + + /// 初始化 + void _init() { + _versionComparator = VersionComparator( + strategy: _config.debugMode ? VersionCompareStrategy.buildNumber : VersionCompareStrategy.semantic, + ); + + // 延迟初始化Flutter相关服务,直到绑定完全初始化 + _scheduleFlutterServicesInit(); + + // 监听网络状态 - 这个可以立即初始化,因为不依赖Flutter绑定 + _networkSubscription = _networkMonitor.statusStream.listen(_onNetworkStatusChanged); + _subscriptions.add(_networkSubscription!); + + // 启动自动检查 + if (_config.autoCheck) { + _startAutoCheck(); + } + } + + /// 延迟初始化Flutter相关服务 + void _scheduleFlutterServicesInit() { + // 如果绑定已经初始化,直接执行 + _initFlutterServices(); + } + + /// 初始化Flutter相关服务 + void _initFlutterServices() { + try { + // 初始化通知,并设置点击回调 + _notificationHelper.initialize(_onNotificationClick); + + // 监听原生渠道的安装请求 + const MethodChannel('app_upgrade_plugin_channel').setMethodCallHandler((call) async { + if (call.method == 'installApkFromNotification') { + final filePath = call.arguments as String?; + if (filePath != null) { + await installApkSmart(filePath); + } + } + }); + } catch (e) { + debugPrint('Failed to initialize Flutter services: $e'); + } + } + + /// 配置插件 + void configure({ + bool? debugMode, + int? checkIntervalHours, + bool? autoCheck, + bool? wifiOnly, + int? downloadTimeout, + int? connectTimeout, + int? maxRetryCount, + bool? supportBreakpoint, + bool? verifyIntegrity, + VersionCompareStrategy? versionStrategy, + Map? customHeaders, + }) { + _config.updateConfig( + debugMode: debugMode, + checkIntervalHours: checkIntervalHours, + autoCheck: autoCheck, + wifiOnly: wifiOnly, + downloadTimeout: downloadTimeout, + connectTimeout: connectTimeout, + maxRetryCount: maxRetryCount, + supportBreakpoint: supportBreakpoint, + verifyIntegrity: verifyIntegrity, + customHeaders: customHeaders, + ); + + if (versionStrategy != null) { + _versionComparator = VersionComparator(strategy: versionStrategy); + } + + // 重启自动检查 + if (autoCheck != null) { + if (autoCheck) { + _startAutoCheck(); + } else { + _stopAutoCheck(); + } + } + } + + /// 获取平台版本(无需平台接口) + Future getPlatformVersion() async { + try { + final info = await PackageInfo.fromPlatform(); + return info.version; + } catch (e) { + debugPrint('getPlatformVersion failed: $e'); + return null; + } + } + + /// 获取当前App信息 + Future> getAppInfo() async { + return await _cacheManager.get>( + 'app_info', + strategy: CacheStrategy.cacheFirst, + networkFetcher: _getPackageInfoMap, + ) ?? + {}; + } + + /// 检查更新(智能版) + Future checkUpdateSmart( + String url, { + Map? params, + bool forceRefresh = false, + Duration? cacheDuration, + }) async { + try { + // 检查网络状态 + if (!_networkMonitor.isConnected) { + _notifyError('No network connection'); + return null; + } + + // 检查是否可以下载 + if (_config.wifiOnly && !_networkMonitor.isWifi) { + _notifyError('WiFi required for update check'); + return null; + } + + // 使用缓存策略 + final strategy = forceRefresh ? CacheStrategy.networkFirst : CacheStrategy.cacheFirst; + + final upgradeInfo = await _cacheManager.get( + 'upgrade_info_$url', + strategy: strategy, + networkFetcher: () async { + final info = await _fetchUpgradeInfo(url, params: params); + if (info != null) { + // 智能版本比较 + final appInfo = await getAppInfo(); + final currentVersion = appInfo['version'] ?? '0.0.0'; + + if (_versionComparator.isUpdateAvailable(currentVersion, info.versionName)) { + // 判断更新类型 + final isMajor = _versionComparator.isMajorUpdate(currentVersion, info.versionName); + + // 根据更新类型调整策略 + if (isMajor && !info.isForceUpdate) { + // 主要版本更新建议强制 + debugPrint('Major update detected, suggesting force update'); + } + + return info; + } + } + return null; + }, + ); + + if (upgradeInfo != null) { + _currentUpgradeInfo = upgradeInfo; + _notifyUpgrade(upgradeInfo); + + // 预下载(如果配置了静默下载) + if (_config.silentDownload && upgradeInfo.downloadUrl != null && _networkMonitor.isWifi) { + _startSilentDownload(upgradeInfo.downloadUrl!); + } + } + + return upgradeInfo; + } catch (e) { + _notifyError('Check update failed: $e'); + return null; + } + } + + Future> _getPackageInfoMap() async { + final packageInfo = await PackageInfo.fromPlatform(); + return { + 'appName': packageInfo.appName, + 'packageName': packageInfo.packageName, + 'version': packageInfo.version, + 'buildNumber': packageInfo.buildNumber, + }; + } + + /// 直接通过网络请求拉取升级信息,避免依赖平台接口实现 + Future _fetchUpgradeInfo( + String url, { + Map? params, + }) async { + try { + final appInfo = await getAppInfo(); + final requestParams = { + 'version': appInfo['version'], + 'buildNumber': appInfo['buildNumber'], + 'platform': Platform.isAndroid ? 'android' : 'ios', + ...?params, + }; + + final response = await _dio.get(url, queryParameters: requestParams); + if (response.statusCode == 200 && response.data != null) { + return UpgradeInfo.fromJson(response.data); + } + return null; + } catch (e) { + debugPrint('fetch upgrade info failed: $e'); + return null; + } + } + + /// 智能下载APK + Future downloadApkSmart( + String url, { + String? versionName, // 新增版本名,用于标准化文件名 + Function(DownloadProgress)? onProgress, + String? savePath, + String? md5, + String? sha256, + bool resumeIfExists = true, + }) async { + if (!Platform.isAndroid) { + throw UnsupportedError('downloadApk only supports Android platform'); + } + + try { + // 检查网络状态 + final networkStatus = _networkMonitor.currentStatus; + if (networkStatus == null || !networkStatus.isConnected) { + _notifyError('No network connection for download'); + return null; + } + + // 创建下载任务 + _currentDownloadTaskId = await _downloadManager.createTask( + url: url, + savePath: savePath, + md5: md5, + sha256: sha256, + versionName: versionName ?? _currentUpgradeInfo?.versionName, + ); + + // 监听下载进度 + final progressSubscription = _downloadManager.getProgressStream(_currentDownloadTaskId!)?.listen((task) { + _notifyDownload(task); + + // 更新通知栏进度 + _updateNotificationForTask(task); + + if (onProgress != null && task.totalSize != null) { + onProgress(DownloadProgress(received: task.downloadedSize, total: task.totalSize!)); + } + }); + + if (progressSubscription != null) { + _subscriptions.add(progressSubscription); + } + + // 开始下载 + final success = await _downloadManager.startDownload(_currentDownloadTaskId!); + + if (success) { + final task = _downloadManager.getTask(_currentDownloadTaskId!); + return task?.savePath; + } + + return null; + } catch (e) { + _notifyError('Download failed: $e'); + return null; + } + } + + /// 安装APK(带权限检查) + Future installApkSmart(String filePath) async { + if (!Platform.isAndroid) { + return false; + } + + try { + // 检查文件是否存在 + final file = File(filePath); + if (!await file.exists()) { + _notifyError('APK file not found'); + return false; + } + + // 检查文件完整性(如果有MD5) + if (_currentUpgradeInfo?.apkMd5 != null) { + final isValid = await DownloadManager.verifyFileInIsolate({ + 'filePath': filePath, + 'md5': _currentUpgradeInfo!.apkMd5, + }); + + if (!isValid) { + _notifyError('APK file integrity check failed'); + return false; + } + } + + return await AppUpgradePluginPlatform.instance.installApk(filePath); + } catch (e) { + _notifyError('Install failed: $e'); + return false; + } + } + + /// 跳转到应用商店(直接使用 url_launcher,避免依赖平台通道) + Future goToAppStore(String url) async { + try { + final uri = Uri.parse(url); + if (await canLaunchUrl(uri)) { + return await launchUrl(uri, mode: LaunchMode.externalApplication); + } + return false; + } catch (e) { + _notifyError('Failed to open app store: $e'); + return false; + } + } + + /// 暂停下载 + void pauseDownload() { + if (_currentDownloadTaskId != null) { + _downloadManager.pauseDownload(_currentDownloadTaskId!); + } + } + + /// 恢复下载 + Future resumeDownload() async { + if (_currentDownloadTaskId != null) { + return await _downloadManager.resumeDownload(_currentDownloadTaskId!); + } + return false; + } + + /// 取消下载 + void cancelDownload() { + if (_currentDownloadTaskId != null) { + _downloadManager.cancelDownload(_currentDownloadTaskId!); + _notificationHelper.cancelNotification(); // 取消时移除通知 + _currentDownloadTaskId = null; + } + } + + /// 重试下载 + Future retryDownload() async { + if (_currentDownloadTaskId != null) { + return await _downloadManager.retryDownload(_currentDownloadTaskId!); + } + return false; + } + + /// 获取下载任务 + DownloadTask? getCurrentDownloadTask() { + if (_currentDownloadTaskId != null) { + return _downloadManager.getTask(_currentDownloadTaskId!); + } + return null; + } + + /// 添加升级回调 + void addUpgradeCallback(UpgradeCallback callback) { + _upgradeCallbacks.add(callback); + } + + /// 移除升级回调 + void removeUpgradeCallback(UpgradeCallback callback) { + _upgradeCallbacks.remove(callback); + } + + /// 添加下载回调 + void addDownloadCallback(DownloadCallback callback) { + _downloadCallbacks.add(callback); + } + + /// 移除下载回调 + void removeDownloadCallback(DownloadCallback callback) { + _downloadCallbacks.remove(callback); + } + + /// 添加错误回调 + void addErrorCallback(ErrorCallback callback) { + _errorCallbacks.add(callback); + } + + /// 移除错误回调 + void removeErrorCallback(ErrorCallback callback) { + _errorCallbacks.remove(callback); + } + + /// 开始自动检查 + void _startAutoCheck() { + _stopAutoCheck(); + + _autoCheckTimer = Timer.periodic(Duration(hours: _config.checkIntervalHours), (_) async { + // 自动检查需要在WiFi环境下 + if (_config.wifiOnly && !_networkMonitor.isWifi) { + return; + } + + // 执行检查(使用缓存的URL) + final lastCheckUrl = await _cacheManager.get('last_check_url'); + if (lastCheckUrl != null) { + await checkUpdateSmart(lastCheckUrl); + } + }); + } + + /// 停止自动检查 + void _stopAutoCheck() { + _autoCheckTimer?.cancel(); + _autoCheckTimer = null; + } + + /// 静默下载 + void _startSilentDownload(String url) async { + if (!_config.silentDownload) return; + if (!_networkMonitor.isWifi) return; + + try { + final taskId = await _downloadManager.createTask(url: url); + await _downloadManager.startDownload(taskId); + } catch (e) { + debugPrint('Silent download failed: $e'); + } + } + + /// 网络状态变化处理 + void _onNetworkStatusChanged(NetworkStatus status) { + if (_config.debugMode) { + debugPrint('Network status changed: ${status.toJson()}'); + } + + // 网络恢复时恢复下载 + if (status.isConnected && _currentDownloadTaskId != null) { + final task = _downloadManager.getTask(_currentDownloadTaskId!); + if (task != null && task.status == DownloadStatus.paused) { + resumeDownload(); + } + } + + // WiFi环境下启动静默下载 + if (status.type == NetworkType.wifi && _config.silentDownload && _currentUpgradeInfo?.downloadUrl != null) { + _startSilentDownload(_currentUpgradeInfo!.downloadUrl!); + } + } + + /// 通知升级信息 + void _notifyUpgrade(UpgradeInfo info) { + for (final callback in _upgradeCallbacks) { + callback(info); + } + } + + /// 通知下载进度 + void _notifyDownload(DownloadTask task) { + for (final callback in _downloadCallbacks) { + callback(task); + } + } + + /// 通知错误 + void _notifyError(String error) { + if (_config.debugMode) { + debugPrint('AppUpgrade Error: $error'); + } + + for (final callback in _errorCallbacks) { + callback(error); + } + } + + /// 通知点击回调 + Future _onNotificationClick(NotificationResponse response) async { + if (response.payload != null && response.payload!.startsWith('download_complete:')) { + final filePath = response.payload!.split(':').last; + await installApkSmart(filePath); + } + } + + /// 根据下载任务状态更新通知 + void _updateNotificationForTask(DownloadTask task) async { + final progress = (task.progress * 100).toInt(); + final appInfo = await _cacheManager.get>('app_info', strategy: CacheStrategy.cacheFirst); + final appName = appInfo?['appName'] ?? '应用'; + + switch (task.status) { + case DownloadStatus.downloading: + _notificationHelper.showDownloadProgressNotification( + progress: progress, + title: '$appName 下载中...', + body: '${(task.progress * 100).toStringAsFixed(1)}%', + ); + break; + case DownloadStatus.completed: + _notificationHelper.showDownloadCompleteNotification( + title: '$appName 下载完成', + body: '点击安装新版本', + filePath: task.savePath, + ); + break; + case DownloadStatus.failed: + _notificationHelper.showDownloadFailedNotification( + title: '$appName 下载失败', + body: task.errorMessage ?? '请检查网络后重试', + ); + break; + case DownloadStatus.cancelled: + _notificationHelper.cancelNotification(); + break; + default: + break; + } + } + + /// 获取缓存统计 + Future> getCacheStats() { + return _cacheManager.getCacheStats(); + } + + /// 清空缓存 + Future clearCache() { + return _cacheManager.clear(); + } + + /// 获取网络状态 + NetworkStatus? get networkStatus => _networkMonitor.currentStatus; + + /// 刷新网络状态 + Future refreshNetworkStatus() async { + await _networkMonitor.refreshNetworkStatus(); + } + + /// 获取当前配置 + UpgradeConfig get config => _config; + + /// 获取版本比较器 + VersionComparator get versionComparator => _versionComparator; + + /// 释放资源(优化内存) + void dispose() { + // 停止自动检查 + _stopAutoCheck(); + + // 取消所有订阅 + for (final subscription in _subscriptions) { + subscription.cancel(); + } + _subscriptions.clear(); + + // 清理回调 + _upgradeCallbacks.clear(); + _downloadCallbacks.clear(); + _errorCallbacks.clear(); + + // 释放组件资源 + _downloadManager.dispose(); + _networkMonitor.dispose(); + _cacheManager.dispose(); + + // 清理弱引用 + _weakRefs.clear(); + + // 清空当前状态 + _currentUpgradeInfo = null; + _currentDownloadTaskId = null; + } +} diff --git a/lib/app_upgrade_plugin_method_channel.dart b/lib/app_upgrade_plugin_method_channel.dart new file mode 100644 index 0000000..1668e25 --- /dev/null +++ b/lib/app_upgrade_plugin_method_channel.dart @@ -0,0 +1,561 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:dio/io.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; +import 'package:package_info_plus/package_info_plus.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:url_launcher/url_launcher.dart'; + +import 'app_upgrade_plugin_platform_interface.dart'; +import 'core/http_config.dart'; +import 'models/upgrade_info.dart'; + +/// An implementation of [AppUpgradePluginPlatform] that uses method channels. +class MethodChannelAppUpgradePlugin extends AppUpgradePluginPlatform { + /// The method channel used to interact with the native platform. + @visibleForTesting + final methodChannel = const MethodChannel('app_upgrade_plugin'); + + late Dio _dio; + HttpConfig _httpConfig = const HttpConfig(); + + MethodChannelAppUpgradePlugin() { + // 默认配置:根据环境自动决定 + if (!const bool.fromEnvironment('dart.vm.product')) { + // 开发环境:自动使用开发配置(绕过证书) + _httpConfig = HttpConfig.development; + } else { + // 生产环境:使用生产配置(严格验证) + _httpConfig = HttpConfig.production; + } + + // 初始化Dio + _initializeDio(_httpConfig); + } + + /// 初始化Dio实例 + void _initializeDio(HttpConfig config) { + _httpConfig = config; + + // 配置 Dio 实例 + _dio = Dio(BaseOptions( + connectTimeout: Duration(seconds: config.connectTimeout), + receiveTimeout: Duration(seconds: config.receiveTimeout), + sendTimeout: Duration(seconds: config.connectTimeout), + headers: { + 'User-Agent': 'AppUpgradePlugin/1.0', + 'Accept': 'application/json', + 'Content-Type': 'application/json', + ...?config.headers, + }, + // 允许所有状态码,我们自己处理 + validateStatus: (status) => true, + )); + + // 配置HTTPS支持 + (_dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () { + final client = HttpClient(); + if (config.ignoreCertificate) { + client.badCertificateCallback = (cert, host, port) => true; + } + return client; + }; + + // 清除旧的拦截器 + _dio.interceptors.clear(); + + // 添加拦截器 + if (config.enableLog) { + _dio.interceptors.add(InterceptorsWrapper( + onRequest: (options, handler) { + debugPrint('==== HTTP请求 ===='); + debugPrint('${options.method} ${options.uri}'); + debugPrint('请求头: ${options.headers}'); + if (options.data != null) { + debugPrint('请求体: ${options.data}'); + } + handler.next(options); + }, + onResponse: (response, handler) { + debugPrint('==== HTTP响应 ===='); + debugPrint('状态码: ${response.statusCode}'); + debugPrint('URL: ${response.requestOptions.uri}'); + handler.next(response); + }, + onError: (DioException e, handler) { + _logDioError(e); + handler.next(e); + }, + )); + } + } + + @override + void configureHttp(HttpConfig config) { + _initializeDio(config); + } + + @override + Future getPlatformVersion() async { + final version = await methodChannel.invokeMethod('getPlatformVersion'); + return version; + } + + @override + Future checkUpdate(String url, {Map? params}) async { + try { + final packageInfo = await PackageInfo.fromPlatform(); + final currentVersion = packageInfo.version; + final currentBuildNumber = packageInfo.buildNumber; + + // 准备请求参数 + final requestParams = { + 'version': currentVersion, + 'buildNumber': currentBuildNumber, + 'platform': Platform.isAndroid ? 'android' : 'ios', + ...?params, + }; + + debugPrint('==== 检查更新 ===='); + debugPrint('当前版本: $currentVersion (Build: $currentBuildNumber)'); + debugPrint('请求URL: $url'); + + Response? response; + + // 根据配置或参数决定请求方式 + final usePost = _httpConfig.defaultMethod.toUpperCase() == 'POST' || + (params != null && params.containsKey('_method') && params['_method'] == 'POST'); + + if (usePost) { + // POST请求 + debugPrint('使用POST请求'); + response = await _dio.post( + url, + data: requestParams, + ); + } else { + // GET请求 + debugPrint('使用GET请求'); + response = await _dio.get( + url, + queryParameters: requestParams, + ); + } + + debugPrint('响应状态: ${response.statusCode}'); + + if (response.statusCode == 200) { + // 处理响应数据 + dynamic responseData = response.data; + + // 如果响应是字符串,尝试解析为JSON + if (responseData is String) { + if (responseData.isEmpty) { + throw Exception('服务器返回空内容'); + } + try { + responseData = json.decode(responseData); + } catch (_) { + throw Exception('响应数据格式错误'); + } + } + + debugPrint('响应数据: $responseData'); + + // TODO 解析更新信息 + // final upgradeInfo = UpgradeInfo.fromJson(responseData); + final upgradeInfo = UpgradeInfo.fromJson({ + "hasUpdate": true, + "isForceUpdate": true, + "versionCode": "101", // Android buildNumber, 必须是数字字符串 + "versionName": "1.0.1", // 显示的版本名 + "updateContent": "1. 修复了xxx Bug。\n2. 优化了用户体验。", + "downloadUrl": + "https://dpc-job-oss.23544.com/infra-app/making_school_asignment_app/1.0.5/1/app-release.apk", // APK 下载地址 + "appStoreUrl": "https://itunes.apple.com/app/id123456", // iOS App Store 地址 + "apkSize": 20971520, // APK 文件大小 (单位: byte) + "apkMd5": "b10a8db164e0754105b7a99be72e3fe5" // APK 的 MD5 (可选,用于校验) + }); + + // 比较版本 + if (_compareVersion(upgradeInfo.versionCode, currentBuildNumber) > 0) { + debugPrint('✅ 发现新版本: ${upgradeInfo.versionName}'); + return upgradeInfo; + } else { + debugPrint('✅ 当前已是最新版本'); + return null; + } + } else { + throw Exception('服务器响应错误: ${response.statusCode}'); + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError && e.error is SocketException) { + final socketError = e.error as SocketException; + if (socketError.message.contains('Failed host lookup')) { + debugPrint('❌ DNS解析失败,请检查网络连接或域名配置'); + } + } + _handleNetworkError(e); + + return null; + } catch (e) { + debugPrint('❌ 检查更新失败: $e'); + return null; + } + } + + @override + Future getAndroidSdkVersion() async { + if (!Platform.isAndroid) return null; + return await methodChannel.invokeMethod('getAndroidSdkVersion'); + } + + @override + Future> getAppInfo() async { + final packageInfo = await PackageInfo.fromPlatform(); + return { + 'appName': packageInfo.appName, + 'packageName': packageInfo.packageName, + 'version': packageInfo.version, + 'buildNumber': packageInfo.buildNumber, + }; + } + + @override + Future downloadApk(String url, {Function(DownloadProgress)? onProgress, String? savePath}) async { + if (!Platform.isAndroid) { + throw PlatformException(code: 'PLATFORM_NOT_SUPPORTED', message: 'downloadApk only supports Android platform'); + } + + debugPrint('开始下载APK: $url'); + + // 先测试URL连接性 + final canConnect = await testDownloadUrl(url); + if (!canConnect) { + debugPrint('错误: 无法连接到下载URL'); + return null; + } + + try { + // 获取保存路径 + savePath ??= await getDownloadPath(); + if (savePath == null) { + debugPrint('错误: 无法获取下载路径'); + throw Exception('无法获取下载路径'); + } + debugPrint('下载路径: $savePath'); + + // 创建文件名 + final fileName = 'app_${DateTime.now().millisecondsSinceEpoch}.apk'; + final filePath = '$savePath/$fileName'; + debugPrint('文件将保存到: $filePath'); + + // 配置下载选项 + final options = Options( + responseType: ResponseType.bytes, + followRedirects: true, + validateStatus: (status) { + return status != null && status < 500; + }, + headers: { + 'Accept': '*/*', + 'Accept-Encoding': 'gzip, deflate, br', + 'Connection': 'keep-alive', + }, + ); + + // 下载文件 + final response = await _dio.download( + url, + filePath, + onReceiveProgress: (received, total) { + if (total != -1) { + final progress = (received / total * 100).toStringAsFixed(0); + debugPrint('下载进度: $progress% ($received/$total)'); + } + if (onProgress != null) { + onProgress(DownloadProgress(received: received, total: total)); + } + }, + options: options, + deleteOnError: true, + ); + + debugPrint('下载完成,响应状态码: ${response.statusCode}'); + + // 检查文件是否存在 + final file = File(filePath); + if (await file.exists()) { + final fileSize = await file.length(); + debugPrint('文件保存成功,大小: $fileSize 字节'); + return filePath; + } else { + debugPrint('错误: 文件保存失败'); + return null; + } + } on DioException catch (e) { + debugPrint('Dio下载异常:'); + debugPrint(' 类型: ${e.type}'); + debugPrint(' 消息: ${e.message}'); + debugPrint(' 错误: ${e.error}'); + if (e.response != null) { + debugPrint(' 响应状态码: ${e.response?.statusCode}'); + debugPrint(' 响应数据: ${e.response?.data}'); + } + return null; + } catch (e, stackTrace) { + debugPrint('下载APK失败: $e'); + debugPrint('堆栈跟踪: $stackTrace'); + return null; + } + } + + @override + Future installApk(String filePath) async { + if (!Platform.isAndroid) { + debugPrint('installApk: 非Android平台'); + return false; + } + + try { + debugPrint('开始安装APK: $filePath'); + + // Check if file exists + final file = File(filePath); + if (!await file.exists()) { + debugPrint('安装失败: APK文件不存在 - $filePath'); + return false; + } + + final result = await methodChannel.invokeMethod('installApk', {'filePath': filePath}); + debugPrint('安装APK结果: $result'); + return result ?? false; + } catch (e) { + debugPrint('安装APK异常: $e'); + + // Provide more specific error messages + if (e.toString().contains('PERMISSION_DENIED')) { + debugPrint('安装失败: 缺少安装未知应用权限'); + } else if (e.toString().contains('FILE_NOT_FOUND')) { + debugPrint('安装失败: APK文件未找到'); + } else if (e.toString().contains('FILEPROVIDER_ERROR')) { + debugPrint('安装失败: FileProvider配置错误'); + } else if (e.toString().contains('INTENT_ERROR')) { + debugPrint('安装失败: 无法启动安装程序'); + } + + return false; + } + } + + @override + Future goToAppStore(String url) async { + try { + final uri = Uri.parse(url); + if (await canLaunchUrl(uri)) { + await launchUrl(uri, mode: LaunchMode.externalApplication); + return true; + } + return false; + } catch (e) { + debugPrint('跳转应用商店失败: $e'); + return false; + } + } + + @override + Future getDownloadPath() async { + try { + // 首先尝试使用原生方法获取下载路径 + final nativePath = await methodChannel.invokeMethod('getDownloadPath'); + if (nativePath != null && nativePath.isNotEmpty) { + debugPrint('使用原生下载路径: $nativePath'); + return nativePath; + } + } catch (e) { + debugPrint('获取原生下载路径失败: $e'); + } + + // 备用方案:使用 path_provider + try { + Directory? directory; + + if (Platform.isAndroid) { + // Android: 优先使用外部存储下载目录 + directory = await getExternalStorageDirectory(); + if (directory != null) { + // 创建 Download 子目录 + final downloadDir = Directory('${directory.path}/Download'); + if (!await downloadDir.exists()) { + await downloadDir.create(recursive: true); + } + debugPrint('使用外部存储下载路径: ${downloadDir.path}'); + return downloadDir.path; + } + } + + // 如果外部存储不可用,使用应用文档目录 + directory = await getApplicationDocumentsDirectory(); + final downloadDir = Directory('${directory.path}/downloads'); + if (!await downloadDir.exists()) { + await downloadDir.create(recursive: true); + } + debugPrint('使用应用文档下载路径: ${downloadDir.path}'); + return downloadDir.path; + } catch (e) { + debugPrint('获取备用下载路径失败: $e'); + return null; + } + } + + @override + Future checkApkExists(String version, String? md5) async { + if (!Platform.isAndroid) return false; + final result = await methodChannel.invokeMethod('checkApkExists', { + 'version': version, + 'md5': md5, + }); + return result ?? false; + } + + /// 比较版本号 + /// 返回值:1表示v1大于v2,0表示相等,-1表示v1小于v2 + int _compareVersion(String v1, String v2) { + try { + final version1 = int.tryParse(v1) ?? 0; + final version2 = int.tryParse(v2) ?? 0; + + if (version1 > version2) { + return 1; + } else if (version1 < version2) { + return -1; + } else { + return 0; + } + } catch (e) { + return 0; + } + } + + /// 测试下载URL的连接性 + Future testDownloadUrl(String url) async { + try { + debugPrint('测试下载URL连接: $url'); + + final response = await _dio.head( + url, + options: Options( + receiveTimeout: const Duration(seconds: 10), + sendTimeout: const Duration(seconds: 10), + validateStatus: (status) => true, + headers: { + 'User-Agent': 'AppUpgradePlugin/1.0', + }, + ), + ); + + debugPrint('URL测试响应状态码: ${response.statusCode}'); + debugPrint('响应头: ${response.headers.map}'); + + if (response.statusCode == 200 || response.statusCode == 206) { + final contentLength = response.headers.value('content-length'); + if (contentLength != null) { + final size = int.tryParse(contentLength) ?? 0; + debugPrint('文件大小: ${(size / 1024 / 1024).toStringAsFixed(2)} MB'); + } + return true; + } else if (response.statusCode == 404) { + debugPrint('错误: 文件不存在 (404)'); + } else if (response.statusCode == 403) { + debugPrint('错误: 访问被拒绝 (403)'); + } else if (response.statusCode == 401) { + debugPrint('错误: 需要认证 (401)'); + } else { + debugPrint('错误: 未知状态 (${response.statusCode})'); + } + + return false; + } catch (e) { + debugPrint('测试URL连接失败: $e'); + return false; + } + } + + /// 记录Dio错误详情 + void _logDioError(DioException e) { + debugPrint('========== Dio错误详情 =========='); + debugPrint('错误类型: ${e.type}'); + debugPrint('错误消息: ${e.message}'); + + if (e.error != null) { + debugPrint('原始错误: ${e.error}'); + if (e.error is SocketException) { + final socketError = e.error as SocketException; + debugPrint('Socket错误代码: ${socketError.osError?.errorCode}'); + debugPrint('Socket错误消息: ${socketError.osError?.message}'); + } + } + + if (e.response != null) { + debugPrint('响应状态码: ${e.response?.statusCode}'); + debugPrint('响应消息: ${e.response?.statusMessage}'); + } + + debugPrint('请求URL: ${e.requestOptions.uri}'); + debugPrint('请求方法: ${e.requestOptions.method}'); + debugPrint('================================'); + } + + /// 处理网络错误,提供友好的错误消息 + void _handleNetworkError(DioException e) { + String errorMessage = '网络请求失败'; + + switch (e.type) { + case DioExceptionType.connectionTimeout: + errorMessage = '连接超时,请检查网络'; + break; + case DioExceptionType.sendTimeout: + errorMessage = '发送超时,请检查网络'; + break; + case DioExceptionType.receiveTimeout: + errorMessage = '接收超时,请检查网络'; + break; + case DioExceptionType.badResponse: + errorMessage = '服务器响应错误 (${e.response?.statusCode})'; + break; + case DioExceptionType.cancel: + errorMessage = '请求已取消'; + break; + case DioExceptionType.connectionError: + if (e.error is SocketException) { + final socketError = e.error as SocketException; + if (socketError.message.contains('Failed host lookup')) { + errorMessage = 'DNS解析失败,请检查域名或网络设置'; + } else if (socketError.message.contains('Connection refused')) { + errorMessage = '连接被拒绝,请检查服务器状态'; + } else if (socketError.message.contains('Network is unreachable')) { + errorMessage = '网络不可达,请检查网络连接'; + } else { + errorMessage = '网络连接错误: ${socketError.message}'; + } + } else { + errorMessage = '连接错误: ${e.message}'; + } + break; + case DioExceptionType.badCertificate: + errorMessage = '证书验证失败'; + break; + case DioExceptionType.unknown: + errorMessage = '未知错误: ${e.message}'; + break; + } + + debugPrint('网络错误: $errorMessage'); + + // 如果需要,可以在这里抛出自定义异常或显示Toast + // throw NetworkException(errorMessage); + } +} diff --git a/lib/app_upgrade_plugin_platform_interface.dart b/lib/app_upgrade_plugin_platform_interface.dart new file mode 100644 index 0000000..5441abd --- /dev/null +++ b/lib/app_upgrade_plugin_platform_interface.dart @@ -0,0 +1,75 @@ +import 'package:plugin_platform_interface/plugin_platform_interface.dart'; + +import 'app_upgrade_plugin_method_channel.dart'; +import 'core/http_config.dart'; +import 'models/upgrade_info.dart'; + +abstract class AppUpgradePluginPlatform extends PlatformInterface { + /// Constructs a AppUpgradePluginPlatform. + AppUpgradePluginPlatform() : super(token: _token); + + static final Object _token = Object(); + + static AppUpgradePluginPlatform _instance = MethodChannelAppUpgradePlugin(); + + /// The default instance of [AppUpgradePluginPlatform] to use. + /// + /// Defaults to [MethodChannelAppUpgradePlugin]. + static AppUpgradePluginPlatform get instance => _instance; + + /// Platform-specific implementations should set this with their own + /// platform-specific class that extends [AppUpgradePluginPlatform] when + /// they register themselves. + static set instance(AppUpgradePluginPlatform instance) { + PlatformInterface.verifyToken(instance, _token); + _instance = instance; + } + + Future getPlatformVersion() { + throw UnimplementedError('platformVersion() has not been implemented.'); + } + + Future getAndroidSdkVersion() { + throw UnimplementedError('getAndroidSdkVersion() has not been implemented.'); + } + + /// 配置HTTP设置 + void configureHttp(HttpConfig config) { + throw UnimplementedError('configureHttp() has not been implemented.'); + } + + /// 获取当前App版本信息 + Future> getAppInfo() { + throw UnimplementedError('getAppInfo() has not been implemented.'); + } + + /// 检查更新 + Future checkUpdate(String url, {Map? params}) { + throw UnimplementedError('checkUpdate() has not been implemented.'); + } + + /// 下载APK文件(仅Android) + Future downloadApk(String url, {Function(DownloadProgress)? onProgress, String? savePath}) { + throw UnimplementedError('downloadApk() has not been implemented.'); + } + + /// 安装APK(仅Android) + Future installApk(String filePath) { + throw UnimplementedError('installApk() has not been implemented.'); + } + + /// 跳转到应用商店 + Future goToAppStore(String url) { + throw UnimplementedError('goToAppStore() has not been implemented.'); + } + + /// 获取下载目录路径 + Future getDownloadPath() { + throw UnimplementedError('getDownloadPath() has not been implemented.'); + } + + /// 检查是否已下载指定版本的APK + Future checkApkExists(String version, String? md5) { + throw UnimplementedError('checkApkExists() has not been implemented.'); + } +} diff --git a/lib/app_upgrade_simple.dart b/lib/app_upgrade_simple.dart new file mode 100644 index 0000000..49a65d6 --- /dev/null +++ b/lib/app_upgrade_simple.dart @@ -0,0 +1,1152 @@ +import 'dart:io'; + +import 'package:app_upgrade_plugin/widgets/market_selection_dialog.dart'; +import 'package:flutter/material.dart'; +import 'package:fluttertoast/fluttertoast.dart'; + +import 'app_upgrade_plugin.dart'; +import 'core/upgrade_utils.dart'; + +/// 简化版App升级管理器 +/// 提供最简单的API,一行代码即可实现App升级功能 +class AppUpgradeSimple { + static AppUpgradeSimple? _instance; + + /// 获取单例实例 + static AppUpgradeSimple get instance { + _instance ??= AppUpgradeSimple._(); + return _instance!; + } + + @visibleForTesting + static set instance(AppUpgradeSimple value) { + _instance = value; + } + + @visibleForTesting + AppUpgradeSimple.private({AppUpgradePlugin? plugin}) : _plugin = plugin ?? AppUpgradePlugin(); + + AppUpgradeSimple._() : _plugin = AppUpgradePlugin(); + + final AppUpgradePlugin _plugin; + + /// 一键检查更新(最简单的使用方式) + /// + /// 示例: + /// ```dart + /// AppUpgradeSimple.instance.checkUpdate( + /// context: context, + /// url: 'https://api.example.com/check-update', + /// ); + /// ``` + /// + /// [context] 需要是一个能够访问到 `Navigator` 和 `MaterialLocalizations` 的有效上下文。 + /// 如果您不确定,建议在 `init` 方法中提供 `navigatorKey`,这样插件可以获取一个可靠的上下文。 + Future checkUpdate({ + required BuildContext context, + required String url, + Map? params, + bool showNoUpdateToast = true, + bool autoDownload = false, + bool autoInstall = false, + VoidCallback? onComplete, + }) async { + try { + assert(_canShowMaterialDialog(context), '请在 MaterialApp 环境内调用'); + + // 检查更新 + final info = await _plugin.checkUpdate(url, params: params); + + if (info == null || !info.hasUpdate) { + if (showNoUpdateToast && context.mounted) _showToast('已是最新版本'); + onComplete?.call(); + return; + } + + await _showUpgradeDialog( + context: context, + info: info, + autoDownload: autoDownload, + autoInstall: autoInstall, + onComplete: onComplete, + ); + } catch (e) { + debugPrint('检查更新失败: $e'); + + String errorMessage = '检查更新失败'; + + if (e.toString().contains('无网络连接')) { + errorMessage = '无网络连接,请检查网络设置'; + } else if (e.toString().contains('Failed host lookup')) { + errorMessage = '无法连接到服务器,请检查网络或稍后重试'; + } else if (e.toString().contains('Connection refused')) { + errorMessage = '服务器拒绝连接,请稍后重试'; + } else if (e.toString().contains('timeout')) { + errorMessage = '连接超时,请检查网络'; + } else { + errorMessage = '检查更新失败: ${e.toString().split(':').first}'; + } + + if (context.mounted) { + _showToast(errorMessage); + + // 如果是网络问题,显示网络诊断建议 + if (e.toString().contains('Failed host lookup') || e.toString().contains('无网络连接')) { + debugPrint('建议: 请检查网络连接或尝试使用网络诊断功能'); + } + } + onComplete?.call(); + } + } + + /// 在无法显示对话框时的后备逻辑:显示Toast并尝试后台下载/安装 + Future _showToastAndDownloadInBackground({ + required BuildContext context, + required UpgradeInfo info, + required bool autoDownload, + required bool autoInstall, + }) async { + if (info.isForceUpdate) { + _showToast('有重要更新,但无法显示对话框。请在 MaterialApp 环境内重试。'); + } else { + _showToast('发现新版本: ${info.versionName}'); + if (autoDownload && Platform.isAndroid && info.downloadUrl != null) { + final filePath = await _plugin.downloadApk(info.downloadUrl!, onProgress: (_) {}); + if (filePath != null && autoInstall) { + await _installApkHeadless(context, filePath); + } + } + } + } + + /// 静默检查更新(不显示无更新提示) + Future checkUpdateSilent({ + required String url, + Map? params, + }) async { + try { + return await _plugin.checkUpdate(url, params: params); + } catch (e) { + debugPrint('静默检查更新失败: $e'); + return null; + } + } + + /// 显示升级对话框 + Future _showUpgradeDialog({ + required BuildContext context, + required UpgradeInfo info, + required bool autoDownload, + required bool autoInstall, + VoidCallback? onComplete, + }) { + return showDialog( + context: context, + barrierDismissible: !info.isForceUpdate, + builder: (context) { + if (info.isForceUpdate) { + return _ForceUpgradeDialog(info: info); + } else { + return _SimpleUpgradeDialog( + info: info, + autoDownload: autoDownload, + autoInstall: autoInstall, + onComplete: onComplete, + showToast: (message) => _showToast(message), + ); + } + }, + ); + } + + /// 显示备用升级对话框(当MaterialApp环境不可用时) + void _showFallbackUpgradeDialog({ + required BuildContext context, + required UpgradeInfo info, + required bool autoDownload, + required bool autoInstall, + VoidCallback? onComplete, + }) { + showGeneralDialog( + context: context, + barrierDismissible: !info.isForceUpdate, + barrierLabel: 'Dismiss', // Provide a non-localized label + pageBuilder: (buildContext, animation, secondaryAnimation) { + // 使用一个包装器来提供基本的文本样式和方向,这对于独立于Material的对话框是必需的 + return Directionality( + textDirection: TextDirection.ltr, + child: _FallbackUpgradeDialog( + info: info, + autoDownload: autoDownload, + autoInstall: autoInstall, + onComplete: onComplete, + showToast: (message) => _showToast(message), + ), + ); + }, + ).then((_) { + // The dialog has been dismissed. + onComplete?.call(); + }); + } + + /// 显示Toast提示 + void _showToast(String message) { + Fluttertoast.showToast(msg: message); + } + + /// 检查是否存在可用的 Material 环境(仅用于对话框) + bool _canShowMaterialDialog(BuildContext context) { + if (!context.mounted) return false; + try { + // Localizations.of will throw if not found. + return Localizations.of(context, MaterialLocalizations) != null; + } catch (_) { + // If it throws, it means we're not in a Material scope. + return false; + } + } + + /// 共享的安装逻辑 (无UI) + Future _installApkHeadless(BuildContext context, String filePath) async { + if (!Platform.isAndroid) return; + final hasPermission = await PermissionHelper.checkAndRequestInstallPermission(context: context); + if (!hasPermission) { + _showToast('未授予安装权限,无法完成更新'); + return; + } + final success = await _plugin.installApk(filePath); + if (!success) { + _showToast('安装失败,请手动安装'); + } + } +} + +/// 共享的升级对话框内容构建器 +class _UpgradeDialogContent extends StatelessWidget { + final UpgradeInfo info; + final bool isDownloading; + final double downloadProgress; + final String statusText; + + const _UpgradeDialogContent({ + required this.info, + required this.isDownloading, + required this.downloadProgress, + required this.statusText, + }); + + @override + Widget build(BuildContext context) { + final List changeItems = + info.updateContent.split(RegExp(r'\r?\n')).map((e) => e.trim()).where((e) => e.isNotEmpty).toList(); + + return Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '版本:${info.versionName}', + style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold), + ), + if (info.apkSize != null) ...[ + const SizedBox(height: 4), + Text('大小:${formatBytes(info.apkSize!)}'), + ], + const SizedBox(height: 12), + const Text('更新内容:', style: TextStyle(fontWeight: FontWeight.bold)), + const SizedBox(height: 8), + Container( + constraints: const BoxConstraints(maxHeight: 220), + child: SingleChildScrollView( + child: changeItems.isEmpty + ? Text(info.updateContent) + : Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: changeItems + .map( + (line) => Padding( + padding: const EdgeInsets.only(bottom: 6), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text('• '), + Expanded(child: Text(line)), + ], + ), + ), + ) + .toList(), + ), + ), + ), + if (isDownloading) ...[ + const SizedBox(height: 16), + ClipRRect( + borderRadius: const BorderRadius.all(Radius.circular(4)), + child: LinearProgressIndicator( + value: downloadProgress.clamp(0.0, 1.0), + minHeight: 6, + ), + ), + const SizedBox(height: 8), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Expanded(child: Text(statusText, overflow: TextOverflow.ellipsis)), + Text('${(downloadProgress * 100).toStringAsFixed(1)}%'), + ], + ), + ], + ], + ); + } +} + +/// 共享的升级操作逻辑 +mixin _UpgradeDialogLogic on State { + final _plugin = AppUpgradePlugin(); + bool _isDownloading = false; + double _downloadProgress = 0; + String _statusText = ''; + + UpgradeInfo get info; + void Function(String) get showToast; + VoidCallback? get onComplete; + bool get autoDownload; + bool get autoInstall; + + @override + void initState() { + super.initState(); + if (autoDownload && Platform.isAndroid) { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) { + _startDownloadAndInstall(); + } + }); + } + } + + Future _startDownloadAndInstall() async { + if (!Platform.isAndroid || info.downloadUrl == null) return; + if (!mounted) return; + + final hasStorage = await PermissionHelper.checkAndRequestStoragePermission(context: context); + if (!hasStorage) { + showToast('缺少存储权限,无法下载'); + return; + } + await PermissionHelper.checkAndRequestNotificationPermission(context: context); + + setState(() { + _isDownloading = true; + _statusText = '下载中...'; + _downloadProgress = 0; + }); + + final filePath = await _plugin.downloadApk( + info.downloadUrl!, + onProgress: (p) { + if (!mounted) return; + setState(() { + _downloadProgress = p.progress; + _statusText = '下载中 ${p.percentage}%'; + }); + }, + ); + + if (!mounted) return; + + if (filePath == null) { + setState(() { + _isDownloading = false; + _statusText = '下载失败'; + }); + return; + } + + setState(() { + _statusText = '下载完成'; + _downloadProgress = 1.0; + }); + + if (autoInstall) { + await _installApk(filePath); + } + } + + Future _installApk(String filePath) async { + if (!mounted) return; + final hasPermission = await PermissionHelper.checkAndRequestInstallPermission(context: context); + if (!hasPermission) { + if (mounted) { + showToast('未授予安装权限,无法完成更新'); + } + return; + } + + final success = await _plugin.installApk(filePath); + if (!success && mounted) { + showToast('安装失败,请手动安装'); + } + } + + Future _showInstallMethodChooser() async { + final bool hasMarkets = info.appMarkets != null && info.appMarkets!.isNotEmpty; + final bool hasDownload = info.downloadUrl != null; + + if (!mounted) return; + + // 始终提供“应用市场”入口;无具体列表则用通用 market:// 链接 + final choice = await showModalBottomSheet( + context: context, + isScrollControlled: false, + useRootNavigator: true, + // UI Beautification: Rounded corners + shape: const RoundedRectangleBorder( + borderRadius: BorderRadius.vertical(top: Radius.circular(20)), + ), + builder: (ctx) { + return SafeArea( + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 8, 16, 16), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + // Draggable handle + Container( + width: 40, + height: 4, + margin: const EdgeInsets.symmetric(vertical: 8), + decoration: BoxDecoration( + color: Colors.grey.shade300, + borderRadius: BorderRadius.circular(2), + ), + ), + // Title and Close Button + Padding( + padding: const EdgeInsets.only(bottom: 8.0), + child: Stack( + alignment: Alignment.center, + children: [ + const Text('选择更新方式', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), + Positioned( + right: -12, + child: IconButton( + icon: const Icon(Icons.close), + onPressed: () => Navigator.of(ctx).pop(), + ), + ), + ], + ), + ), + + // Option 1: App Market + ListTile( + leading: const Icon(Icons.storefront_outlined), + title: const Text('前往应用市场更新'), + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + onTap: () => Navigator.of(ctx).pop('market'), + ), + + // Option 2: Direct Download + if (hasDownload) + ListTile( + leading: const Icon(Icons.download_for_offline_outlined), + title: const Text('直接下载安装包'), + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + onTap: () => Navigator.of(ctx).pop('download'), + ), + + const Divider(height: 24), + + // Cancel Button + SizedBox( + width: double.infinity, + child: ElevatedButton( + style: ElevatedButton.styleFrom( + padding: const EdgeInsets.symmetric(vertical: 12), + backgroundColor: Colors.white, + foregroundColor: Theme.of(context).textTheme.bodyLarge?.color, + elevation: 2, + shadowColor: Colors.grey.withOpacity(0.5), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(12), + side: BorderSide(color: Colors.grey.shade300), + ), + ), + onPressed: () => Navigator.of(ctx).pop(), + child: const Text('取消', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500)), + ), + ), + ], + ), + ), + ); + }, + ); + + if (choice == 'market') { + if (hasMarkets) { + await MarketSelectionDialog.show( + context, + markets: info.appMarkets!, + onSelected: (market) async { + if (market.url != null && market.url!.isNotEmpty) { + _plugin.goToAppStore(market.url!); + } else { + final appInfo = await _plugin.getAppInfo(); + final pkg = appInfo['packageName'] ?? ''; + if (pkg.isNotEmpty) { + _plugin.goToAppStore('market://details?id=$pkg'); + } + } + }, + ); + } else { + final appInfo = await _plugin.getAppInfo(); + final pkg = appInfo['packageName'] ?? ''; + if (pkg.isNotEmpty) { + _plugin.goToAppStore('market://details?id=$pkg'); + } + } + if (!mounted) return; + Navigator.of(context).pop(); + onComplete?.call(); + return; + } + + if (choice == 'download' && hasDownload && !_isDownloading) { + await _startDownloadAndInstall(); + } + } + + void _handleAction() { + if (Platform.isAndroid) { + _handleAndroidAction(); + } else if (Platform.isIOS) { + _handleIosAction(); + } else { + showToast('Unsupported platform'); + } + } + + /// Handles the upgrade action for iOS. + void _handleIosAction() { + if (info.appStoreUrl != null) { + _plugin.goToAppStore(info.appStoreUrl!); + // Pop the upgrade dialog. + if (Navigator.canPop(context)) { + Navigator.of(context).pop(); + } + onComplete?.call(); + } else { + showToast('App Store URL is not available.'); + } + } + + /// Handles the upgrade action for Android. + Future _handleAndroidAction() async { + // On Android, we always assume a market option is available, + // because we can fall back to a generic market:// intent. + const bool hasMarketOption = true; + final bool hasDownloadOption = info.downloadUrl != null; + + // This case is unlikely on Android, but kept for robustness. + if (!hasMarketOption && !hasDownloadOption) { + showToast('No update method available.'); + return; + } + + // If a download option is not available, going to the market is the only choice. + if (!hasDownloadOption) { + _handleMarketAction(); + return; + } + + // If a download option exists, always give the user a choice, + // as the market option is also implicitly available. + await _showDownloadChoiceSheet(); + } + + /// Opens the app store or shows a market selection dialog. + Future _performMarketAction() async { + final hasMarkets = info.appMarkets?.isNotEmpty ?? false; + if (hasMarkets) { + await MarketSelectionDialog.show( + context, + markets: info.appMarkets!, + onSelected: (market) { + _plugin.goToAppStore(market.url ?? market.packageName ?? ''); + }, + ); + } else { + // No specific markets, try a generic market link. + final appInfo = await _plugin.getAppInfo(); + final pkg = appInfo['packageName'] ?? ''; + if (pkg.isNotEmpty) { + _plugin.goToAppStore('market://details?id=$pkg'); + // _plugin.goToAppStore('market://details?id=$pkg'); + } else { + showToast('Could not determine app package name.'); + } + } + } + + /// Pops the current dialog and then performs the market action. + Future _handleMarketAction() async { + // Pop the upgrade dialog before proceeding. + if (Navigator.canPop(context)) { + Navigator.of(context).pop(); + } + if (!mounted) return; + await _performMarketAction(); + onComplete?.call(); + } + + Future _showDownloadChoiceSheet() async { + final bool hasDownload = info.downloadUrl != null; + + if (!mounted) return; + + final choice = await showModalBottomSheet( + context: context, + isScrollControlled: false, + useRootNavigator: true, + // UI Beautification: Rounded corners + shape: const RoundedRectangleBorder( + borderRadius: BorderRadius.vertical(top: Radius.circular(20)), + ), + builder: (ctx) { + return SafeArea( + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 8, 16, 16), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + // Draggable handle + Container( + width: 40, + height: 4, + margin: const EdgeInsets.symmetric(vertical: 8), + decoration: BoxDecoration( + color: Colors.grey.shade300, + borderRadius: BorderRadius.circular(2), + ), + ), + // Title and Close Button + Padding( + padding: const EdgeInsets.only(bottom: 8.0), + child: Stack( + alignment: Alignment.center, + children: [ + const Text('选择更新方式', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), + Positioned( + right: -12, + child: IconButton( + icon: const Icon(Icons.close), + onPressed: () => Navigator.of(ctx).pop(), + ), + ), + ], + ), + ), + + // Option 1: App Market + ListTile( + leading: const Icon(Icons.storefront_outlined), + title: const Text('前往应用市场更新'), + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + onTap: () => Navigator.of(ctx).pop('market'), + ), + + // Option 2: Direct Download + if (hasDownload) + ListTile( + leading: const Icon(Icons.download_for_offline_outlined), + title: const Text('直接下载安装包'), + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + onTap: () => Navigator.of(ctx).pop('download'), + ), + + const Divider(height: 24), + + // Cancel Button + SizedBox( + width: double.infinity, + child: ElevatedButton( + style: ElevatedButton.styleFrom( + padding: const EdgeInsets.symmetric(vertical: 12), + backgroundColor: Colors.white, + foregroundColor: Theme.of(context).textTheme.bodyLarge?.color, + elevation: 2, + shadowColor: Colors.grey.withOpacity(0.5), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(12), + side: BorderSide(color: Colors.grey.shade300), + ), + ), + onPressed: () => Navigator.of(ctx).pop(), + child: const Text('取消', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500)), + ), + ), + ], + ), + ), + ); + }, + ); + + if (choice == 'market') { + await _performMarketAction(); + onComplete?.call(); + return; + } + + if (choice == 'download' && hasDownload && !_isDownloading) { + await _startDownloadAndInstall(); + } + } +} + +/// 简化版升级对话框(非强制更新) +class _SimpleUpgradeDialog extends StatefulWidget { + final UpgradeInfo info; + final bool autoDownload; + final bool autoInstall; + final VoidCallback? onComplete; + final void Function(String) showToast; + + const _SimpleUpgradeDialog({ + required this.info, + required this.autoDownload, + required this.autoInstall, + this.onComplete, + required this.showToast, + }); + + @override + State<_SimpleUpgradeDialog> createState() => _SimpleUpgradeDialogState(); +} + +class _SimpleUpgradeDialogState extends State<_SimpleUpgradeDialog> with _UpgradeDialogLogic { + @override + UpgradeInfo get info => widget.info; + @override + void Function(String) get showToast => widget.showToast; + @override + VoidCallback? get onComplete => widget.onComplete; + @override + bool get autoDownload => widget.autoDownload; + @override + bool get autoInstall => widget.autoInstall; + + @override + Widget build(BuildContext context) { + final theme = Theme.of(context); + + return AlertDialog( + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(16), + ), + contentPadding: const EdgeInsets.fromLTRB(20, 8, 20, 16), + actionsPadding: const EdgeInsets.fromLTRB(16, 0, 16, 12), + title: Row( + children: [ + Icon( + Icons.system_update, + color: theme.colorScheme.primary, + ), + const SizedBox(width: 8), + const Text('发现新版本'), + ], + ), + content: _UpgradeDialogContent( + info: widget.info, + isDownloading: _isDownloading, + downloadProgress: _downloadProgress, + statusText: _statusText, + ), + actionsAlignment: MainAxisAlignment.end, + actions: _isDownloading + ? [] + : [ + if (!widget.info.isForceUpdate) + TextButton( + onPressed: () { + Navigator.of(context).pop(); + widget.onComplete?.call(); + }, + child: const Text('稍后更新'), + ), + ElevatedButton( + onPressed: _handleAction, + child: Text(Platform.isAndroid ? '立即更新' : '前往更新'), + ), + ], + ); + } +} + +/// 强制更新对话框 +class _ForceUpgradeDialog extends StatefulWidget { + final UpgradeInfo info; + + const _ForceUpgradeDialog({required this.info}); + + @override + State<_ForceUpgradeDialog> createState() => _ForceUpgradeDialogState(); +} + +class _ForceUpgradeDialogState extends State<_ForceUpgradeDialog> { + final _plugin = AppUpgradePlugin(); + bool _isDownloading = false; + double _downloadProgress = 0; + String _statusText = ''; + + @override + void initState() { + super.initState(); + // 强制更新也需用户交互后再开始下载 + } + + Future _startDownload() async { + // 下载前申请权限 (Just-in-Time) + if (Platform.isAndroid) { + final hasStorage = await PermissionHelper.checkAndRequestStoragePermission(context: context); + if (!hasStorage) { + setState(() { + _statusText = '缺少存储权限,无法下载'; + }); + return; + } + // 通知权限仅为辅助 + await PermissionHelper.checkAndRequestNotificationPermission(context: context); + } + + if (widget.info.downloadUrl == null) { + setState(() { + _statusText = '下载地址无效'; + }); + return; + } + + setState(() { + _isDownloading = true; + _statusText = '准备下载...'; + }); + + final filePath = await _plugin.downloadApk( + widget.info.downloadUrl!, + onProgress: (p) { + if (!mounted) return; + setState(() { + _downloadProgress = p.progress; + _statusText = '下载中 ${p.percentage}%'; + }); + }, + ); + + if (filePath != null) { + await _installApk(context, filePath); + } + } + + // 进度已在 downloadApk 的回调中更新 + + Future _installApk(BuildContext context, String filePath) async { + final hasPermission = await PermissionHelper.checkAndRequestInstallPermission(context: context); + if (!hasPermission) { + if (context.mounted) { + setState(() { + _statusText = '未授予安装权限,请手动授权后重试'; + }); + } + return; + } + await _plugin.installApk(filePath); + } + + @override + Widget build(BuildContext context) { + final theme = Theme.of(context); + return WillPopScope( + onWillPop: () async => false, // 强制更新,不允许返回 + child: AlertDialog( + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(16), + ), + contentPadding: const EdgeInsets.fromLTRB(20, 8, 20, 16), + actionsPadding: const EdgeInsets.fromLTRB(16, 0, 16, 12), + title: Row( + children: [ + Icon( + Icons.system_update, + color: theme.colorScheme.primary, + ), + const SizedBox(width: 8), + const Text('发现新版本 (强制)'), + ], + ), + content: _UpgradeDialogContent( + info: widget.info, + isDownloading: _isDownloading, + downloadProgress: _downloadProgress, + statusText: _statusText, + ), + actionsAlignment: MainAxisAlignment.end, + actions: _isDownloading + ? [] + : [ + ElevatedButton( + onPressed: () async { + // 强制更新下也先让用户选择安装方式 + final hasMarkets = widget.info.appMarkets != null && widget.info.appMarkets!.isNotEmpty; + final hasDownload = widget.info.downloadUrl != null; + + if (hasMarkets && hasDownload) { + final choice = await showModalBottomSheet( + context: context, + isScrollControlled: false, + useRootNavigator: true, + // UI Beautification: Rounded corners + shape: const RoundedRectangleBorder( + borderRadius: BorderRadius.vertical(top: Radius.circular(20)), + ), + builder: (ctx) { + return SafeArea( + child: Padding( + padding: const EdgeInsets.fromLTRB(16, 8, 16, 16), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + // Draggable handle + Container( + width: 40, + height: 4, + margin: const EdgeInsets.symmetric(vertical: 8), + decoration: BoxDecoration( + color: Colors.grey.shade300, + borderRadius: BorderRadius.circular(2), + ), + ), + // Title and Close Button + Padding( + padding: const EdgeInsets.only(bottom: 8.0), + child: Stack( + alignment: Alignment.center, + children: [ + const Text('选择更新方式', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), + Positioned( + right: -12, + child: IconButton( + icon: const Icon(Icons.close), + onPressed: () => Navigator.of(ctx).pop(), + ), + ), + ], + ), + ), + + // Option 1: App Market + ListTile( + leading: const Icon(Icons.storefront_outlined), + title: const Text('前往应用市场更新'), + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + onTap: () => Navigator.of(ctx).pop('market'), + ), + + // Option 2: Direct Download + if (hasDownload) + ListTile( + leading: const Icon(Icons.download_for_offline_outlined), + title: const Text('直接下载安装包'), + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + onTap: () => Navigator.of(ctx).pop('download'), + ), + + const Divider(height: 24), + + // Cancel Button + SizedBox( + width: double.infinity, + child: ElevatedButton( + style: ElevatedButton.styleFrom( + padding: const EdgeInsets.symmetric(vertical: 12), + backgroundColor: Colors.white, + foregroundColor: Theme.of(context).textTheme.bodyLarge?.color, + elevation: 2, + shadowColor: Colors.grey.withOpacity(0.5), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(12), + side: BorderSide(color: Colors.grey.shade300), + ), + ), + onPressed: () => Navigator.of(ctx).pop(), + child: + const Text('取消', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500)), + ), + ), + ], + ), + ), + ); + }, + ); + + if (choice == 'market') { + Navigator.of(context).pop(); + MarketSelectionDialog.show( + context, + markets: widget.info.appMarkets!, + onSelected: (market) { + _plugin.goToAppStore(market.url ?? market.packageName ?? ''); + }, + ); + return; + } + if (choice == 'download') { + await _startDownload(); + return; + } + return; + } + + if (hasMarkets) { + Navigator.of(context).pop(); + MarketSelectionDialog.show( + context, + markets: widget.info.appMarkets!, + onSelected: (market) { + _plugin.goToAppStore(market.url ?? market.packageName ?? ''); + }, + ); + return; + } + + if (hasDownload) { + await _startDownload(); + return; + } + }, + child: const Text('立即更新'), + ), + ], + ), + ); + } +} + +/// 当 [MaterialApp] 不可用时,提供一个基础的回退升级对话框 +class _FallbackUpgradeDialog extends StatefulWidget { + final UpgradeInfo info; + final bool autoDownload; + final bool autoInstall; + final VoidCallback? onComplete; + final void Function(String) showToast; + + const _FallbackUpgradeDialog({ + required this.info, + required this.autoDownload, + required this.autoInstall, + this.onComplete, + required this.showToast, + }); + + @override + State<_FallbackUpgradeDialog> createState() => _FallbackUpgradeDialogState(); +} + +class _FallbackUpgradeDialogState extends State<_FallbackUpgradeDialog> with _UpgradeDialogLogic { + @override + UpgradeInfo get info => widget.info; + @override + void Function(String) get showToast => widget.showToast; + @override + VoidCallback? get onComplete => widget.onComplete; + @override + bool get autoDownload => widget.autoDownload; + @override + bool get autoInstall => widget.autoInstall; + + @override + Widget build(BuildContext context) { + final title = '发现新版本${widget.info.isForceUpdate ? " (强制)" : ""}'; + + final body = Center( + child: Material( + type: MaterialType.transparency, + child: Container( + margin: const EdgeInsets.all(24.0), + padding: const EdgeInsets.all(16.0), + decoration: BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.circular(16.0), + ), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text( + title, + style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black), + textAlign: TextAlign.center, + ), + const SizedBox(height: 16), + DefaultTextStyle( + style: const TextStyle(color: Colors.black87, fontSize: 14), + child: _UpgradeDialogContent( + info: widget.info, + isDownloading: _isDownloading, + downloadProgress: _downloadProgress, + statusText: _statusText, + ), + ), + const SizedBox(height: 24), + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + if (!widget.info.isForceUpdate && !_isDownloading) ...[ + GestureDetector( + onTap: () { + Navigator.of(context).pop(); + widget.onComplete?.call(); + }, + child: const Padding( + padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8), + child: Text('稍后更新', style: TextStyle(color: Colors.blue)), + ), + ), + const SizedBox(width: 8), + ], + GestureDetector( + onTap: _isDownloading ? null : _handleAction, + child: Container( + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), + decoration: BoxDecoration( + color: _isDownloading ? Colors.grey : Colors.blue, + borderRadius: BorderRadius.circular(8), + ), + child: Text( + _isDownloading + ? '下载中...' + : Platform.isAndroid + ? '立即更新' + : '前往更新', + style: const TextStyle(color: Colors.white), + ), + ), + ), + ], + ), + ], + ), + ), + ), + ); + + return widget.info.isForceUpdate ? WillPopScope(onWillPop: () async => false, child: body) : body; + } +} diff --git a/lib/core/cache_manager.dart b/lib/core/cache_manager.dart new file mode 100644 index 0000000..2a5925b --- /dev/null +++ b/lib/core/cache_manager.dart @@ -0,0 +1,495 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; // Added for WidgetsBinding +import 'package:path_provider/path_provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +/// 缓存条目 +class CacheEntry { + final String key; + final T data; + final DateTime createdAt; + final DateTime? expiresAt; + final Map? metadata; + + CacheEntry({required this.key, required this.data, required this.createdAt, this.expiresAt, this.metadata}); + + bool get isExpired { + if (expiresAt == null) return false; + return DateTime.now().isAfter(expiresAt!); + } + + Map toJson() => { + 'key': key, + 'data': data is Map || data is List ? data : data.toString(), + 'createdAt': createdAt.toIso8601String(), + 'expiresAt': expiresAt?.toIso8601String(), + 'metadata': metadata, + }; + + factory CacheEntry.fromJson(Map json) { + return CacheEntry( + key: json['key'], + data: json['data'] as T, + createdAt: DateTime.parse(json['createdAt']), + expiresAt: json['expiresAt'] != null ? DateTime.parse(json['expiresAt']) : null, + metadata: json['metadata'], + ); + } +} + +/// 缓存策略 +enum CacheStrategy { + /// 优先使用缓存,缓存过期后才请求网络 + cacheFirst, + + /// 优先使用网络,网络失败时使用缓存 + networkFirst, + + /// 只使用缓存 + cacheOnly, + + /// 只使用网络 + networkOnly, + + /// 同时请求网络和缓存,返回最快的结果 + fastest, +} + +/// 智能缓存管理器 +class CacheManager { + static CacheManager? _instance; + static CacheManager get instance { + _instance ??= CacheManager._(); + return _instance!; + } + + CacheManager._() { + _scheduleInit(); + } + + late SharedPreferences _prefs; + late Directory _cacheDir; + final Map _memoryCache = {}; + final Map _autoCleanTimers = {}; + + static const String _cachePrefix = 'app_upgrade_cache_'; + static const String _cacheMetaKey = 'cache_metadata'; + static const int _maxMemoryCacheSize = 50; // 最大内存缓存条目数 + static const int _maxDiskCacheSize = 100 * 1024 * 1024; // 100MB + + /// 延迟初始化 + void _scheduleInit() { + // 如果绑定已经初始化,直接执行 + _init().catchError((e) { + debugPrint('Failed to initialize CacheManager: $e'); + }); + } + + /// 初始化 + Future _init() async { + _prefs = await SharedPreferences.getInstance(); + _cacheDir = await _getCacheDirectory(); + _startAutoClean(); + await _cleanExpiredCache(); + } + + /// 获取缓存目录 + Future _getCacheDirectory() async { + final tempDir = await getTemporaryDirectory(); + final cacheDir = Directory('${tempDir.path}/app_upgrade_cache'); + if (!await cacheDir.exists()) { + await cacheDir.create(recursive: true); + } + return cacheDir; + } + + /// 保存到缓存 + Future save({ + required String key, + required T data, + Duration? expiration, + CacheStrategy strategy = CacheStrategy.cacheFirst, + Map? metadata, + }) async { + final fullKey = '$_cachePrefix$key'; + final expiresAt = expiration != null ? DateTime.now().add(expiration) : null; + + final entry = CacheEntry( + key: fullKey, + data: data, + createdAt: DateTime.now(), + expiresAt: expiresAt, + metadata: metadata, + ); + + // 保存到内存缓存 + _saveToMemory(fullKey, entry); + + // 保存到磁盘缓存 + await _saveToDisk(fullKey, entry); + + // 更新缓存元数据 + await _updateCacheMetadata(fullKey, entry); + } + + /// 从缓存获取 + Future get( + String key, { + CacheStrategy strategy = CacheStrategy.cacheFirst, + Future Function()? networkFetcher, + }) async { + final fullKey = '$_cachePrefix$key'; + + switch (strategy) { + case CacheStrategy.cacheFirst: + return await _getCacheFirst(fullKey, networkFetcher); + case CacheStrategy.networkFirst: + return await _getNetworkFirst(fullKey, networkFetcher); + case CacheStrategy.cacheOnly: + return await _getCacheOnly(fullKey); + case CacheStrategy.networkOnly: + return await _getNetworkOnly(networkFetcher); + case CacheStrategy.fastest: + return await _getFastest(fullKey, networkFetcher); + } + } + + /// 缓存优先策略 + Future _getCacheFirst(String key, Future Function()? fetcher) async { + // 先从内存缓存获取 + final memoryEntry = _memoryCache[key]; + if (memoryEntry != null && !memoryEntry.isExpired) { + return memoryEntry.data as T?; + } + + // 从磁盘缓存获取 + final diskEntry = await _getFromDisk(key); + if (diskEntry != null && !diskEntry.isExpired) { + _saveToMemory(key, diskEntry); + return diskEntry.data; + } + + // 缓存无效,从网络获取 + if (fetcher != null) { + try { + final data = await fetcher(); + if (data != null) { + await save(key: key.replaceFirst(_cachePrefix, ''), data: data); + } + return data; + } catch (e) { + // 网络失败,返回过期的缓存 + if (diskEntry != null) { + return diskEntry.data; + } + rethrow; + } + } + + return null; + } + + /// 网络优先策略 + Future _getNetworkFirst(String key, Future Function()? fetcher) async { + if (fetcher != null) { + try { + final data = await fetcher(); + if (data != null) { + await save(key: key.replaceFirst(_cachePrefix, ''), data: data); + } + return data; + } catch (e) { + // 网络失败,使用缓存 + final entry = await _getFromDisk(key) ?? _memoryCache[key] as CacheEntry?; + if (entry != null) { + return entry.data; + } + rethrow; + } + } + + return null; + } + + /// 仅缓存策略 + Future _getCacheOnly(String key) async { + final memoryEntry = _memoryCache[key]; + if (memoryEntry != null) { + return memoryEntry.data as T?; + } + + final diskEntry = await _getFromDisk(key); + if (diskEntry != null) { + _saveToMemory(key, diskEntry); + return diskEntry.data; + } + + return null; + } + + /// 仅网络策略 + Future _getNetworkOnly(Future Function()? fetcher) async { + if (fetcher != null) { + return await fetcher(); + } + return null; + } + + /// 最快响应策略 + Future _getFastest(String key, Future Function()? fetcher) async { + final futures = >[]; + + // 添加缓存获取 + futures.add(_getCacheOnly(key)); + + // 添加网络获取 + if (fetcher != null) { + futures.add(fetcher()); + } + + // 返回最快的结果 + try { + final result = await Future.any(futures); + + // 如果是网络结果,保存到缓存 + if (result != null && fetcher != null) { + final cacheResult = await _getCacheOnly(key); + if (cacheResult == null || cacheResult != result) { + await save(key: key.replaceFirst(_cachePrefix, ''), data: result); + } + } + + return result; + } catch (e) { + return null; + } + } + + /// 保存到内存缓存 + void _saveToMemory(String key, CacheEntry entry) { + _memoryCache[key] = entry; + + // 限制内存缓存大小 + if (_memoryCache.length > _maxMemoryCacheSize) { + // 移除最旧的条目 + final oldestKey = + _memoryCache.entries.reduce((a, b) => a.value.createdAt.isBefore(b.value.createdAt) ? a : b).key; + _memoryCache.remove(oldestKey); + } + } + + /// 保存到磁盘缓存 + Future _saveToDisk(String key, CacheEntry entry) async { + try { + final file = File('${_cacheDir.path}/${_encodeKey(key)}.json'); + final json = jsonEncode(entry.toJson()); + await file.writeAsString(json); + + // 检查磁盘缓存大小 + await _checkDiskCacheSize(); + } catch (e) { + debugPrint('Failed to save to disk cache: $e'); + } + } + + /// 从磁盘缓存获取 + Future?> _getFromDisk(String key) async { + try { + final file = File('${_cacheDir.path}/${_encodeKey(key)}.json'); + if (!await file.exists()) return null; + + final json = await file.readAsString(); + final data = jsonDecode(json); + return CacheEntry.fromJson(data); + } catch (e) { + debugPrint('Failed to get from disk cache: $e'); + return null; + } + } + + /// 更新缓存元数据 + Future _updateCacheMetadata(String key, CacheEntry entry) async { + final metadata = _prefs.getString(_cacheMetaKey); + final metaMap = metadata != null ? jsonDecode(metadata) : {}; + + metaMap[key] = { + 'createdAt': entry.createdAt.toIso8601String(), + 'expiresAt': entry.expiresAt?.toIso8601String(), + 'size': entry.toJson().toString().length, + }; + + await _prefs.setString(_cacheMetaKey, jsonEncode(metaMap)); + } + + /// 删除缓存 + Future delete(String key) async { + final fullKey = '$_cachePrefix$key'; + + // 从内存缓存删除 + _memoryCache.remove(fullKey); + + // 从磁盘缓存删除 + final file = File('${_cacheDir.path}/${_encodeKey(fullKey)}.json'); + if (await file.exists()) { + await file.delete(); + } + + // 更新元数据 + final metadata = _prefs.getString(_cacheMetaKey); + if (metadata != null) { + final metaMap = jsonDecode(metadata); + metaMap.remove(fullKey); + await _prefs.setString(_cacheMetaKey, jsonEncode(metaMap)); + } + } + + /// 清空所有缓存 + Future clear() async { + // 清空内存缓存 + _memoryCache.clear(); + + // 清空磁盘缓存 + if (await _cacheDir.exists()) { + await _cacheDir.delete(recursive: true); + await _cacheDir.create(); + } + + // 清空元数据 + await _prefs.remove(_cacheMetaKey); + } + + /// 清理过期缓存 + Future _cleanExpiredCache() async { + // 清理内存缓存 + _memoryCache.removeWhere((key, entry) => entry.isExpired); + + // 清理磁盘缓存 + final metadata = _prefs.getString(_cacheMetaKey); + if (metadata != null) { + final metaMap = jsonDecode(metadata) as Map; + final keysToRemove = []; + + for (final entry in metaMap.entries) { + final expiresAt = entry.value['expiresAt']; + if (expiresAt != null) { + final expireTime = DateTime.parse(expiresAt); + if (DateTime.now().isAfter(expireTime)) { + keysToRemove.add(entry.key); + } + } + } + + for (final key in keysToRemove) { + await delete(key.replaceFirst(_cachePrefix, '')); + } + } + } + + /// 检查磁盘缓存大小 + Future _checkDiskCacheSize() async { + int totalSize = 0; + final files = await _cacheDir.list().toList(); + + for (final file in files) { + if (file is File) { + totalSize += await file.length(); + } + } + + if (totalSize > _maxDiskCacheSize) { + // 删除最旧的文件 + final fileStats = >[]; + for (final file in files) { + if (file is File) { + fileStats.add(MapEntry(file, await file.stat())); + } + } + + fileStats.sort((a, b) => a.value.accessed.compareTo(b.value.accessed)); + + // 删除文件直到大小合适 + for (final entry in fileStats) { + final fileSize = await entry.key.length(); + await entry.key.delete(); + totalSize -= fileSize; + if (totalSize <= (_maxDiskCacheSize * 0.8).toInt()) break; + } + } + } + + /// 编码缓存键 + String _encodeKey(String key) { + return base64Encode(utf8.encode(key)).replaceAll('/', '_'); + } + + /// 启动自动清理 + void _startAutoClean() { + // 每小时清理一次过期缓存 + _autoCleanTimers['expired'] = Timer.periodic(const Duration(hours: 1), (_) => _cleanExpiredCache()); + } + + /// 获取缓存统计信息 + Future> getCacheStats() async { + int memoryCount = _memoryCache.length; + int memorySize = 0; + + for (final entry in _memoryCache.values) { + memorySize += entry.toJson().toString().length; + } + + int diskCount = 0; + int diskSize = 0; + + final files = await _cacheDir.list().toList(); + for (final file in files) { + if (file is File) { + diskCount++; + diskSize += await file.length(); + } + } + + return { + 'memoryCache': {'count': memoryCount, 'size': memorySize, 'sizeFormatted': _formatBytes(memorySize)}, + 'diskCache': {'count': diskCount, 'size': diskSize, 'sizeFormatted': _formatBytes(diskSize)}, + 'total': { + 'count': memoryCount + diskCount, + 'size': memorySize + diskSize, + 'sizeFormatted': _formatBytes(memorySize + diskSize), + }, + }; + } + + /// 格式化字节数 + String _formatBytes(int bytes) { + if (bytes < 1024) return '$bytes B'; + if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(2)} KB'; + if (bytes < 1024 * 1024 * 1024) return '${(bytes / (1024 * 1024)).toStringAsFixed(2)} MB'; + return '${(bytes / (1024 * 1024 * 1024)).toStringAsFixed(2)} GB'; + } + + /// 预加载缓存 + Future preload(List keys) async { + for (final key in keys) { + final fullKey = '$_cachePrefix$key'; + if (!_memoryCache.containsKey(fullKey)) { + final entry = await _getFromDisk(fullKey); + if (entry != null && !entry.isExpired) { + _saveToMemory(fullKey, entry); + } + } + } + } + + /// 释放资源 + void dispose() { + for (final timer in _autoCleanTimers.values) { + timer.cancel(); + } + _autoCleanTimers.clear(); + _memoryCache.clear(); + } +} diff --git a/lib/core/download_manager.dart b/lib/core/download_manager.dart new file mode 100644 index 0000000..37f5781 --- /dev/null +++ b/lib/core/download_manager.dart @@ -0,0 +1,474 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:dio/dio.dart'; +import 'package:flutter/foundation.dart'; +import 'package:path_provider/path_provider.dart'; + +import 'upgrade_config.dart'; + +/// 下载任务状态 +enum DownloadStatus { pending, downloading, paused, completed, failed, cancelled } + +/// 下载任务信息 +class DownloadTask { + final String id; + final String url; + final String savePath; + final Map? headers; + final String? md5; + final String? sha256; + int? totalSize; + + DownloadStatus status; + int downloadedSize; + double progress; + String? errorMessage; + int retryCount; + DateTime? startTime; + DateTime? endTime; + + DownloadTask({ + required this.id, + required this.url, + required this.savePath, + this.headers, + this.md5, + this.sha256, + this.totalSize, + this.status = DownloadStatus.pending, + this.downloadedSize = 0, + this.progress = 0.0, + this.errorMessage, + this.retryCount = 0, + this.startTime, + this.endTime, + }); + + Map toJson() => { + 'id': id, + 'url': url, + 'savePath': savePath, + 'headers': headers, + 'md5': md5, + 'sha256': sha256, + 'totalSize': totalSize, + 'status': status.toString(), + 'downloadedSize': downloadedSize, + 'progress': progress, + 'errorMessage': errorMessage, + 'retryCount': retryCount, + 'startTime': startTime?.toIso8601String(), + 'endTime': endTime?.toIso8601String(), + }; +} + +/// 高性能下载管理器 +class DownloadManager { + static DownloadManager? _instance; + static DownloadManager get instance { + _instance ??= DownloadManager._(); + return _instance!; + } + + DownloadManager._() { + _initializeDio(); + } + + final Dio _dio = Dio(); + final Map _tasks = {}; + final Map _cancelTokens = {}; + final Map> _progressControllers = {}; + final UpgradeConfig _config = UpgradeConfig.instance; + + Timer? _speedCalculatorTimer; + final Map> _speedSamples = {}; + + /// 初始化Dio配置 + void _initializeDio() { + _dio.options.connectTimeout = Duration(seconds: _config.connectTimeout); + _dio.options.receiveTimeout = Duration(seconds: _config.downloadTimeout); + + if (_config.proxyUrl != null) { + (_dio.httpClientAdapter as dynamic).onHttpClientCreate = (client) { + client.findProxy = (uri) { + return 'PROXY ${_config.proxyUrl}'; + }; + return client; + }; + } + + // 添加拦截器 + _dio.interceptors.add(LogInterceptor(request: _config.debugMode, responseBody: false, error: _config.debugMode)); + + _dio.interceptors.add( + InterceptorsWrapper( + onRequest: (options, handler) { + // 添加自定义请求头 + options.headers.addAll(_config.customHeaders); + handler.next(options); + }, + onError: (error, handler) { + if (_config.debugMode) { + debugPrint('Download error: ${error.message}'); + } + handler.next(error); + }, + ), + ); + } + + /// 创建下载任务 + Future createTask({ + required String url, + String? savePath, + Map? headers, + String? md5, + String? sha256, + int? expectedSize, + String? versionName, // 新增版本名参数 + }) async { + final taskId = DateTime.now().millisecondsSinceEpoch.toString(); + + if (savePath == null) { + final dir = await _getDownloadDirectory(); + // 如果提供了版本名,则使用标准化的文件名 + final fileName = versionName != null ? 'app-upgrade-$versionName.apk' : url.split('/').last.split('?').first; + savePath = '${dir.path}/$fileName'; + } + + final task = DownloadTask( + id: taskId, + url: url, + savePath: savePath, + headers: headers, + md5: md5, + sha256: sha256, + totalSize: expectedSize, + ); + + _tasks[taskId] = task; + _progressControllers[taskId] = StreamController.broadcast(); + + return taskId; + } + + /// 开始下载 + Future startDownload(String taskId) async { + final task = _tasks[taskId]; + if (task == null) return false; + + task.status = DownloadStatus.downloading; + task.startTime = DateTime.now(); + + final cancelToken = CancelToken(); + _cancelTokens[taskId] = cancelToken; + + // 启动速度计算定时器 + _startSpeedCalculator(taskId); + + try { + // 检查是否支持断点续传 + if (_config.supportBreakpoint && await _checkBreakpointSupport(task.url)) { + await _downloadWithBreakpoint(task, cancelToken); + } else { + await _downloadNormal(task, cancelToken); + } + + // 校验文件完整性 + if (_config.verifyIntegrity && (task.md5 != null || task.sha256 != null)) { + final isValid = await _verifyFileIntegrity(task); + if (!isValid) { + throw Exception('File integrity check failed'); + } + } + + task.status = DownloadStatus.completed; + task.endTime = DateTime.now(); + task.progress = 1.0; + _notifyProgress(task); + + return true; + } catch (e) { + if (e is DioException && e.type == DioExceptionType.cancel) { + task.status = DownloadStatus.cancelled; + } else { + task.status = DownloadStatus.failed; + task.errorMessage = e.toString(); + + // 自动重试 + if (task.retryCount < _config.maxRetryCount) { + task.retryCount++; + await Future.delayed(Duration(seconds: _config.retryDelay)); + return startDownload(taskId); + } + } + + _notifyProgress(task); + return false; + } finally { + _stopSpeedCalculator(taskId); + _cancelTokens.remove(taskId); + } + } + + /// 普通下载 + Future _downloadNormal(DownloadTask task, CancelToken cancelToken) async { + final response = await _dio.download( + task.url, + task.savePath, + cancelToken: cancelToken, + options: Options(headers: task.headers), + onReceiveProgress: (received, total) { + task.downloadedSize = received; + task.totalSize ??= total; + task.progress = total > 0 ? received / total : 0.0; + _notifyProgress(task); + }, + ); + + if (response.statusCode != 200 && response.statusCode != 206) { + throw Exception('Download failed with status: ${response.statusCode}'); + } + } + + /// 断点续传下载 + Future _downloadWithBreakpoint(DownloadTask task, CancelToken cancelToken) async { + final file = File(task.savePath); + int downloadedBytes = 0; + + // 检查文件是否已存在 + if (await file.exists()) { + downloadedBytes = await file.length(); + task.downloadedSize = downloadedBytes; + } + + // 如果文件已下载完成 + if (task.totalSize != null && downloadedBytes >= task.totalSize!) { + task.progress = 1.0; + _notifyProgress(task); + return; + } + + // 设置Range头 + final headers = Map.from(task.headers ?? {}); + headers['Range'] = 'bytes=$downloadedBytes-'; + + // 创建文件流 + final raf = await file.open(mode: FileMode.append); + + try { + final response = await _dio.get( + task.url, + cancelToken: cancelToken, + options: Options(headers: headers, responseType: ResponseType.stream), + ); + + final total = int.tryParse(response.headers.value('content-length') ?? '0') ?? 0; + + task.totalSize ??= total + downloadedBytes; + + // 写入文件 + await for (final chunk in response.data!.stream) { + await raf.writeFrom(chunk); + task.downloadedSize += chunk.length; + task.progress = task.totalSize! > 0 ? task.downloadedSize / task.totalSize! : 0.0; + _notifyProgress(task); + } + } finally { + await raf.close(); + } + } + + /// 检查是否支持断点续传 + Future _checkBreakpointSupport(String url) async { + try { + final response = await _dio.head(url); + final acceptRanges = response.headers.value('accept-ranges'); + return acceptRanges == 'bytes'; + } catch (e) { + return false; + } + } + + /// 暂停下载 + void pauseDownload(String taskId) { + final cancelToken = _cancelTokens[taskId]; + if (cancelToken != null && !cancelToken.isCancelled) { + cancelToken.cancel('User paused'); + final task = _tasks[taskId]; + if (task != null) { + task.status = DownloadStatus.paused; + _notifyProgress(task); + } + } + } + + /// 恢复下载 + Future resumeDownload(String taskId) async { + final task = _tasks[taskId]; + if (task != null && task.status == DownloadStatus.paused) { + return startDownload(taskId); + } + return false; + } + + /// 取消下载 + void cancelDownload(String taskId) { + final cancelToken = _cancelTokens[taskId]; + if (cancelToken != null && !cancelToken.isCancelled) { + cancelToken.cancel('User cancelled'); + } + + final task = _tasks[taskId]; + if (task != null) { + task.status = DownloadStatus.cancelled; + _notifyProgress(task); + + // 删除未完成的文件 + final file = File(task.savePath); + if (file.existsSync()) { + file.deleteSync(); + } + } + + _cleanupTask(taskId); + } + + /// 重试下载 + Future retryDownload(String taskId) async { + final task = _tasks[taskId]; + if (task != null && task.status == DownloadStatus.failed) { + task.retryCount = 0; + task.errorMessage = null; + return startDownload(taskId); + } + return false; + } + + /// 获取任务信息 + DownloadTask? getTask(String taskId) => _tasks[taskId]; + + /// 获取所有任务 + List getAllTasks() => _tasks.values.toList(); + + /// 获取任务进度流 + Stream? getProgressStream(String taskId) { + return _progressControllers[taskId]?.stream; + } + + /// 清理任务 + void _cleanupTask(String taskId) { + _tasks.remove(taskId); + _cancelTokens.remove(taskId); + _progressControllers[taskId]?.close(); + _progressControllers.remove(taskId); + _speedSamples.remove(taskId); + } + + /// 通知进度更新 + void _notifyProgress(DownloadTask task) { + _progressControllers[task.id]?.add(task); + } + + /// 启动速度计算器 + void _startSpeedCalculator(String taskId) { + _speedSamples[taskId] = []; + _speedCalculatorTimer?.cancel(); + _speedCalculatorTimer = Timer.periodic(const Duration(seconds: 1), (timer) { + final task = _tasks[taskId]; + if (task != null && task.status == DownloadStatus.downloading) { + final samples = _speedSamples[taskId]!; + samples.add(task.downloadedSize); + + // 保留最近10个采样点 + if (samples.length > 10) { + samples.removeAt(0); + } + + // 计算平均速度 + if (samples.length >= 2) { + final speed = samples.last - samples.first; + final timeSpan = samples.length - 1; + final avgSpeed = speed / timeSpan; + + // 可以在这里发送速度信息 + if (_config.debugMode) { + debugPrint('Download speed: ${_formatBytes(avgSpeed.toInt())}/s'); + } + } + } + }); + } + + /// 停止速度计算器 + void _stopSpeedCalculator(String taskId) { + _speedSamples.remove(taskId); + if (_speedSamples.isEmpty) { + _speedCalculatorTimer?.cancel(); + _speedCalculatorTimer = null; + } + } + + /// 验证文件完整性 + Future _verifyFileIntegrity(DownloadTask task) async { + return compute(verifyFileInIsolate, {'filePath': task.savePath, 'md5': task.md5, 'sha256': task.sha256}); + } + + /// 在Isolate中验证文件 + static Future verifyFileInIsolate(Map params) async { + final filePath = params['filePath'] as String; + final expectedMd5 = params['md5'] as String?; + final expectedSha256 = params['sha256'] as String?; + + final file = File(filePath); + if (!await file.exists()) return false; + + final bytes = await file.readAsBytes(); + + if (expectedMd5 != null) { + final md5Hash = md5.convert(bytes).toString(); + if (md5Hash != expectedMd5) return false; + } + + if (expectedSha256 != null) { + final sha256Hash = sha256.convert(bytes).toString(); + if (sha256Hash != expectedSha256) return false; + } + + return true; + } + + /// 获取下载目录 + Future _getDownloadDirectory() async { + if (Platform.isAndroid) { + return (await getExternalStorageDirectory())!; + } else { + return getApplicationDocumentsDirectory(); + } + } + + /// 格式化字节数 + String _formatBytes(int bytes) { + if (bytes < 1024) return '$bytes B'; + if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(2)} KB'; + if (bytes < 1024 * 1024 * 1024) return '${(bytes / (1024 * 1024)).toStringAsFixed(2)} MB'; + return '${(bytes / (1024 * 1024 * 1024)).toStringAsFixed(2)} GB'; + } + + /// 清理所有任务 + void clearAllTasks() { + for (final taskId in _tasks.keys.toList()) { + cancelDownload(taskId); + } + _tasks.clear(); + } + + /// 释放资源 + void dispose() { + clearAllTasks(); + _speedCalculatorTimer?.cancel(); + _dio.close(); + } +} diff --git a/lib/core/http_config.dart b/lib/core/http_config.dart new file mode 100644 index 0000000..2a2fa43 --- /dev/null +++ b/lib/core/http_config.dart @@ -0,0 +1,50 @@ +/// HTTP配置类 +class HttpConfig { + /// 是否忽略HTTPS证书验证(仅在开发环境建议使用) + final bool ignoreCertificate; + + /// 连接超时时间(秒) + final int connectTimeout; + + /// 接收超时时间(秒) + final int receiveTimeout; + + /// 是否启用请求日志 + final bool enableLog; + + /// 自定义请求头 + final Map? headers; + + /// 默认请求方法(GET或POST) + final String defaultMethod; + + const HttpConfig({ + this.ignoreCertificate = false, + this.connectTimeout = 30, + this.receiveTimeout = 60, + this.enableLog = true, + this.headers, + this.defaultMethod = 'GET', + }); + + /// 开发环境配置(自动绕过证书) + static const HttpConfig development = HttpConfig( + ignoreCertificate: true, + enableLog: true, + ); + + /// 生产环境配置(严格证书验证) + static const HttpConfig production = HttpConfig( + ignoreCertificate: false, + enableLog: false, + ); + + /// 自动配置(根据编译模式自动选择) + static HttpConfig get auto => const bool.fromEnvironment('dart.vm.product') ? production : development; + + /// 不安全配置(总是绕过证书,谨慎使用) + static const HttpConfig unsafe = HttpConfig( + ignoreCertificate: true, + enableLog: true, + ); +} diff --git a/lib/core/network_monitor.dart b/lib/core/network_monitor.dart new file mode 100644 index 0000000..6c01857 --- /dev/null +++ b/lib/core/network_monitor.dart @@ -0,0 +1,516 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:connectivity_plus/connectivity_plus.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; // Added for WidgetsBinding + +/// 网络类型 +enum NetworkType { none, mobile, wifi, ethernet, bluetooth, vpn, other } + +/// 网络质量 +enum NetworkQuality { + unknown, + poor, // 差 (<100KB/s) + moderate, // 中等 (100KB/s - 500KB/s) + good, // 良好 (500KB/s - 2MB/s) + excellent, // 优秀 (>2MB/s) +} + +/// 网络状态信息 +class NetworkStatus { + final NetworkType type; + final NetworkQuality quality; + final bool isConnected; + final bool isMetered; + final double? downloadSpeed; + final double? uploadSpeed; + final int? ping; + final DateTime timestamp; + + NetworkStatus({ + required this.type, + required this.quality, + required this.isConnected, + required this.isMetered, + this.downloadSpeed, + this.uploadSpeed, + this.ping, + DateTime? timestamp, + }) : timestamp = timestamp ?? DateTime.now(); + + /// 是否适合下载大文件 + bool get isSuitableForLargeDownload { + return isConnected && (type == NetworkType.wifi || type == NetworkType.ethernet) && quality != NetworkQuality.poor; + } + + /// 是否应该限速 + bool get shouldLimitSpeed { + return type == NetworkType.mobile || quality == NetworkQuality.poor; + } + + Map toJson() => { + 'type': type.toString(), + 'quality': quality.toString(), + 'isConnected': isConnected, + 'isMetered': isMetered, + 'downloadSpeed': downloadSpeed, + 'uploadSpeed': uploadSpeed, + 'ping': ping, + 'timestamp': timestamp.toIso8601String(), + }; +} + +/// 网络监测器 +class NetworkMonitor { + static NetworkMonitor? _instance; + static NetworkMonitor get instance { + _instance ??= NetworkMonitor._(); + return _instance!; + } + + NetworkMonitor._() { + _init().catchError((e) { + debugPrint('NetworkMonitor initialization failed: $e'); + }); + } + + final Connectivity _connectivity = Connectivity(); + NetworkStatus? _currentStatus; + + final _statusController = StreamController.broadcast(); + Stream get statusStream => _statusController.stream; + + StreamSubscription>? _connectivitySubscription; + Timer? _qualityCheckTimer; + + final List _speedSamples = []; + static const int _maxSamples = 10; + + /// 初始化 + Future _init() async { + debugPrint('NetworkMonitor: Starting initialization'); + + // 立即开始网络监听,不等待Flutter绑定 + _startMonitoring(); + + // 检查初始网络状态 + await _checkInitialStatus(); + + // 启动质量检查 + _startQualityCheck(); + + debugPrint('NetworkMonitor: Initialization completed'); + } + + /// 检查初始状态 + Future _checkInitialStatus() async { + try { + debugPrint('NetworkMonitor: Checking initial connectivity...'); + final result = await _connectivity.checkConnectivity(); + debugPrint('NetworkMonitor: Initial connectivity result: $result'); + + // 强制触发网络状态更新 + await _updateStatus(result); + } catch (e) { + debugPrint('NetworkMonitor: Failed to check initial network status: $e'); + // 如果检查失败,设置默认状态 + await _updateStatus(ConnectivityResult.none); + } + } + + /// 开始监听网络变化 + void _startMonitoring() { + debugPrint('NetworkMonitor: Starting connectivity monitoring...'); + _connectivitySubscription?.cancel(); // 确保之前的监听器被取消 + + _connectivitySubscription = _connectivity.onConnectivityChanged.listen( + (List results) async { + debugPrint('NetworkMonitor: Connectivity changed: $results'); + await _updateStatus(results); + }, + onError: (error) { + debugPrint('NetworkMonitor: Connectivity monitoring error: $error'); + }, + cancelOnError: false, + ); + } + + /// 更新网络状态 + Future _updateStatus(dynamic results) async { + List connectivityResults; + + // 处理不同类型的输入 + if (results is ConnectivityResult) { + connectivityResults = [results]; + } else if (results is List) { + connectivityResults = results; + } else { + debugPrint('NetworkMonitor: Invalid results type: ${results.runtimeType}'); + return; + } + debugPrint('NetworkMonitor: Updating status with results: $results'); + + NetworkType type = NetworkType.none; + bool isConnected = false; + bool isMetered = false; + + // 检查是否有有效的网络连接 + if (results.isNotEmpty && !results.contains(ConnectivityResult.none)) { + isConnected = true; + + // 确定网络类型(优先级:WiFi > Ethernet > Mobile > VPN > Bluetooth > Other) + if (results.contains(ConnectivityResult.wifi)) { + type = NetworkType.wifi; + isMetered = false; + debugPrint('NetworkMonitor: Detected WiFi connection'); + } else if (results.contains(ConnectivityResult.ethernet)) { + type = NetworkType.ethernet; + isMetered = false; + debugPrint('NetworkMonitor: Detected Ethernet connection'); + } else if (results.contains(ConnectivityResult.mobile)) { + type = NetworkType.mobile; + isMetered = true; + debugPrint('NetworkMonitor: Detected Mobile connection'); + } else if (results.contains(ConnectivityResult.vpn)) { + type = NetworkType.vpn; + isMetered = false; + debugPrint('NetworkMonitor: Detected VPN connection'); + } else if (results.contains(ConnectivityResult.bluetooth)) { + type = NetworkType.bluetooth; + isMetered = true; + debugPrint('NetworkMonitor: Detected Bluetooth connection'); + } else { + type = NetworkType.other; + isMetered = true; + debugPrint('NetworkMonitor: Detected Other connection type'); + } + } else { + debugPrint('NetworkMonitor: No network connection detected'); + type = NetworkType.none; + isConnected = false; + } + + // 更新当前状态 + final previousStatus = _currentStatus; + _currentStatus = NetworkStatus( + type: type, + quality: _currentStatus?.quality ?? NetworkQuality.unknown, + isConnected: isConnected, + isMetered: isMetered, + downloadSpeed: _currentStatus?.downloadSpeed, + uploadSpeed: _currentStatus?.uploadSpeed, + ping: _currentStatus?.ping, + ); + + debugPrint('NetworkMonitor: Status updated - Type: $type, Connected: $isConnected, Metered: $isMetered'); + + // 只有在状态真正改变时才发送通知 + if (previousStatus == null || + previousStatus.type != type || + previousStatus.isConnected != isConnected || + previousStatus.isMetered != isMetered) { + _statusController.add(_currentStatus!); + debugPrint('NetworkMonitor: Status change detected, notifying listeners'); + } + + // 如果已连接,开始网络质量测试 + if (isConnected && (type == NetworkType.wifi || type == NetworkType.ethernet)) { + _testNetworkQuality(); + } + } + + /// 开始网络质量检查 + void _startQualityCheck() { + _qualityCheckTimer?.cancel(); + _qualityCheckTimer = Timer.periodic(const Duration(seconds: 30), (_) { + if (_currentStatus?.isConnected == true) { + _testNetworkQuality(); + } + }); + } + + /// 测试网络质量 + Future _testNetworkQuality() async { + try { + // 测试延迟 + final ping = await _testPing(); + + // 测试下载速度 + final downloadSpeed = await _testDownloadSpeed(); + + // 计算网络质量 + final quality = _calculateQuality(downloadSpeed, ping); + + _currentStatus = NetworkStatus( + type: _currentStatus?.type ?? NetworkType.other, + quality: quality, + isConnected: _currentStatus?.isConnected ?? true, + isMetered: _currentStatus?.isMetered ?? false, + downloadSpeed: downloadSpeed, + uploadSpeed: _currentStatus?.uploadSpeed, + ping: ping, + ); + + _statusController.add(_currentStatus!); + } catch (e) { + debugPrint('Failed to test network quality: $e'); + } + } + + /// 测试延迟 + Future _testPing() async { + try { + final stopwatch = Stopwatch()..start(); + final socket = await Socket.connect('8.8.8.8', 53, timeout: const Duration(seconds: 5)); + socket.destroy(); + stopwatch.stop(); + return stopwatch.elapsedMilliseconds; + } catch (e) { + return 9999; // 超时或失败 + } + } + + /// 测试下载速度 + Future _testDownloadSpeed() async { + try { + // 使用小文件测试速度 + const testUrl = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'; + final stopwatch = Stopwatch()..start(); + + final client = HttpClient(); + client.connectionTimeout = const Duration(seconds: 5); + + final request = await client.getUrl(Uri.parse(testUrl)); + final response = await request.close(); + + double bytes = 0; + await for (final chunk in response) { + bytes += chunk.length; + } + + stopwatch.stop(); + final seconds = stopwatch.elapsedMilliseconds / 1000; + final speed = bytes / seconds; // bytes per second + + // 添加到采样列表 + _speedSamples.add(speed); + if (_speedSamples.length > _maxSamples) { + _speedSamples.removeAt(0); + } + + // 返回平均速度 + final avgSpeed = _speedSamples.reduce((a, b) => a + b) / _speedSamples.length; + return avgSpeed; + } catch (e) { + return 0; + } + } + + /// 计算网络质量 + NetworkQuality _calculateQuality(double downloadSpeed, int ping) { + // 基于下载速度和延迟综合评估 + if (downloadSpeed == 0 || ping > 1000) { + return NetworkQuality.poor; + } + + // 速度权重70%,延迟权重30% + double score = 0; + + // 速度评分 (0-70) + if (downloadSpeed > 2 * 1024 * 1024) { + // > 2MB/s + score += 70; + } else if (downloadSpeed > 500 * 1024) { + // > 500KB/s + score += 50; + } else if (downloadSpeed > 100 * 1024) { + // > 100KB/s + score += 30; + } else { + score += 10; + } + + // 延迟评分 (0-30) + if (ping < 50) { + score += 30; + } else if (ping < 100) { + score += 20; + } else if (ping < 200) { + score += 10; + } else { + score += 5; + } + + // 根据总分判断质量 + if (score >= 80) { + return NetworkQuality.excellent; + } else if (score >= 60) { + return NetworkQuality.good; + } else if (score >= 40) { + return NetworkQuality.moderate; + } else { + return NetworkQuality.poor; + } + } + + /// 获取当前网络状态 + NetworkStatus? get currentStatus => _currentStatus; + + /// 手动刷新网络状态 + Future refreshNetworkStatus() async { + debugPrint('NetworkMonitor: Manual refresh requested'); + try { + final result = await _connectivity.checkConnectivity(); + debugPrint('NetworkMonitor: Manual refresh result: $result'); + await _updateStatus(result); + } catch (e) { + debugPrint('NetworkMonitor: Manual refresh failed: $e'); + await _updateStatus(ConnectivityResult.none); + } + } + + /// 是否已连接 + bool get isConnected => _currentStatus?.isConnected ?? false; + + /// 是否WiFi连接 + bool get isWifi => _currentStatus?.type == NetworkType.wifi; + + /// 是否移动网络 + bool get isMobile => _currentStatus?.type == NetworkType.mobile; + + /// 是否计费网络 + bool get isMetered => _currentStatus?.isMetered ?? false; + + /// 等待网络连接 + Future waitForConnection({Duration? timeout}) async { + if (isConnected) return true; + + final completer = Completer(); + StreamSubscription? subscription; + Timer? timer; + + subscription = statusStream.listen((status) { + if (status.isConnected) { + completer.complete(true); + subscription?.cancel(); + timer?.cancel(); + } + }); + + if (timeout != null) { + timer = Timer(timeout, () { + if (!completer.isCompleted) { + completer.complete(false); + subscription?.cancel(); + } + }); + } + + return completer.future; + } + + /// 等待WiFi连接 + Future waitForWifi({Duration? timeout}) async { + if (isWifi) return true; + + final completer = Completer(); + StreamSubscription? subscription; + Timer? timer; + + subscription = statusStream.listen((status) { + if (status.type == NetworkType.wifi) { + completer.complete(true); + subscription?.cancel(); + timer?.cancel(); + } + }); + + if (timeout != null) { + timer = Timer(timeout, () { + if (!completer.isCompleted) { + completer.complete(false); + subscription?.cancel(); + } + }); + } + + return completer.future; + } + + /// 检查是否可以下载 + bool canDownload({required bool wifiOnly, required bool allowCellular}) { + if (!isConnected) return false; + + if (wifiOnly && !isWifi) return false; + + if (!allowCellular && isMobile) return false; + + return true; + } + + /// 获取建议的下载策略 + Map getSuggestedDownloadStrategy() { + if (!isConnected) { + return {'canDownload': false, 'reason': 'No network connection'}; + } + + final quality = _currentStatus?.quality ?? NetworkQuality.unknown; + final type = _currentStatus?.type ?? NetworkType.other; + + return { + 'canDownload': true, + 'useParallelDownload': quality == NetworkQuality.excellent, + 'maxConcurrentChunks': _getSuggestedChunks(quality), + 'chunkSize': _getSuggestedChunkSize(quality), + 'shouldCompress': type == NetworkType.mobile, + 'recommendedTimeout': _getSuggestedTimeout(quality), + }; + } + + int _getSuggestedChunks(NetworkQuality quality) { + switch (quality) { + case NetworkQuality.excellent: + return 5; + case NetworkQuality.good: + return 3; + case NetworkQuality.moderate: + return 2; + default: + return 1; + } + } + + int _getSuggestedChunkSize(NetworkQuality quality) { + switch (quality) { + case NetworkQuality.excellent: + return 2 * 1024 * 1024; // 2MB + case NetworkQuality.good: + return 1024 * 1024; // 1MB + case NetworkQuality.moderate: + return 512 * 1024; // 512KB + default: + return 256 * 1024; // 256KB + } + } + + int _getSuggestedTimeout(NetworkQuality quality) { + switch (quality) { + case NetworkQuality.excellent: + return 30; + case NetworkQuality.good: + return 60; + case NetworkQuality.moderate: + return 120; + default: + return 300; + } + } + + /// 释放资源 + void dispose() { + _connectivitySubscription?.cancel(); + _qualityCheckTimer?.cancel(); + _statusController.close(); + } +} diff --git a/lib/core/notification_helper.dart b/lib/core/notification_helper.dart new file mode 100644 index 0000000..06a10bf --- /dev/null +++ b/lib/core/notification_helper.dart @@ -0,0 +1,108 @@ +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; + +/// 通知助手类,用于显示下载进度 +class NotificationHelper { + static final NotificationHelper _instance = NotificationHelper._(); + static NotificationHelper get instance => _instance; + NotificationHelper._(); + + final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); + + /// 初始化 + Future initialize(Future Function(NotificationResponse)? onDidReceiveNotificationResponse) async { + const AndroidInitializationSettings initializationSettingsAndroid = + AndroidInitializationSettings('@mipmap/ic_launcher'); // 使用App图标 + + const InitializationSettings initializationSettings = InitializationSettings( + android: initializationSettingsAndroid, + ); + + await _flutterLocalNotificationsPlugin.initialize( + initializationSettings, + onDidReceiveNotificationResponse: onDidReceiveNotificationResponse, + ); + } + + /// 显示下载进度通知 + Future showDownloadProgressNotification({ + required int progress, + required String title, + required String body, + }) async { + final AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails( + 'app_upgrade_download_channel', + '下载通知', + channelDescription: '显示应用更新的下载进度', + importance: Importance.low, // 设置为 low,避免下载过程中声音干扰 + priority: Priority.low, + showProgress: true, + maxProgress: 100, + progress: progress, + onlyAlertOnce: true, // 只在首次显示时响铃 + ); + final NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics); + + await _flutterLocalNotificationsPlugin.show( + 0, // 使用固定的 ID,以便更新通知 + title, + body, + platformChannelSpecifics, + payload: 'download_progress', + ); + } + + /// 显示下载完成通知 + Future showDownloadCompleteNotification({ + required String title, + required String body, + required String filePath, + }) async { + final AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails( + 'app_upgrade_download_channel', // 与进度通知使用相同渠道 + '下载通知', + channelDescription: '显示应用更新的下载进度', + importance: Importance.high, // 完成时使用高优先级,以便用户看到 + priority: Priority.high, + autoCancel: true, + ); + final NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics); + + await _flutterLocalNotificationsPlugin.show( + 0, // ID 仍然为 0,覆盖之前的进度通知 + title, + body, + platformChannelSpecifics, + payload: 'download_complete:$filePath', // payload 中带上文件路径 + ); + } + + /// 显示下载失败通知 + Future showDownloadFailedNotification({ + required String title, + required String body, + }) async { + // 与完成通知类似,但内容不同 + final AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails( + 'app_upgrade_download_channel', + '下载通知', + channelDescription: '显示应用更新的下载进度', + importance: Importance.high, + priority: Priority.high, + autoCancel: true, + ); + final NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics); + + await _flutterLocalNotificationsPlugin.show( + 0, + title, + body, + platformChannelSpecifics, + payload: 'download_failed', + ); + } + + /// 取消通知 + Future cancelNotification() async { + await _flutterLocalNotificationsPlugin.cancel(0); + } +} diff --git a/lib/core/permission_helper.dart b/lib/core/permission_helper.dart new file mode 100644 index 0000000..727f8ef --- /dev/null +++ b/lib/core/permission_helper.dart @@ -0,0 +1,309 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:permission_handler/permission_handler.dart'; + +import '../app_upgrade_plugin.dart'; + +/// Enum representing the status of the install permission. +enum InstallPermissionStatus { + /// The user has granted the permission. + granted, + + /// The user has denied the permission. + denied, + + /// The user has denied the permission and selected "Don't ask again". + /// On Android, this means the user must go to settings to enable it. + permanentlyDenied, + + /// The permission is restricted by the system (e.g., parental controls). + restricted, +} + +/// A helper class for handling permissions required by the app upgrade process. +class PermissionHelper { + static final _plugin = AppUpgradePlugin(); + + /// Checks and requests storage permission (required for downloading files). + /// + /// This method handles different Android versions appropriately: + /// - Android 13+ (API 33+): No storage permission needed for app-specific directories + /// - Android 11-12 (API 30-32): Uses MANAGE_EXTERNAL_STORAGE when needed + /// - Android 10 (API 29): Uses legacy storage permission with scoped storage fallback + /// - Android 9 and below: Uses legacy WRITE_EXTERNAL_STORAGE permission + static Future checkAndRequestStoragePermission({required BuildContext context}) async { + if (!Platform.isAndroid) return true; + + final sdkVersion = await _getAndroidSdkVersion(); + + // Android 13+ (API 33+): No storage permission needed for app-specific directories + if (sdkVersion >= 33) { + debugPrint('Android 13+: Using scoped storage, no permission required'); + return true; + } + + // Android 11-12 (API 30-32): Try MANAGE_EXTERNAL_STORAGE first, fallback to scoped storage + if (sdkVersion >= 30) { + var status = await Permission.manageExternalStorage.status; + if (status.isGranted) return true; + + if (context.mounted) { + final proceed = await _showRationaleDialog( + context, + title: '需要文件管理权限', + content: '为了下载更新文件到您选择的位置,我们需要访问设备存储。您也可以选择"拒绝",应用将使用私有存储空间。', + ); + if (proceed) { + status = await Permission.manageExternalStorage.request(); + if (status.isGranted) return true; + } + } + + // If MANAGE_EXTERNAL_STORAGE is denied, we can still use app-specific directories + debugPrint('MANAGE_EXTERNAL_STORAGE denied, using app-specific storage'); + return true; + } + + // Android 10 (API 29): Use legacy storage permission, but it's limited due to scoped storage + if (sdkVersion >= 29) { + var status = await Permission.storage.status; + if (status.isGranted) return true; + + if (context.mounted) { + final proceed = await _showRationaleDialog( + context, + title: '需要存储权限', + content: '为了下载更新文件,我们需要访问设备存储空间。', + ); + if (!proceed) return false; + } + + status = await Permission.storage.request(); + // Even if denied, we can still use scoped storage + return true; + } + + // Android 9 and below (API 28-): Use traditional WRITE_EXTERNAL_STORAGE + var status = await Permission.storage.status; + if (status.isGranted) return true; + + if (context.mounted) { + final proceed = await _showRationaleDialog( + context, + title: '需要存储权限', + content: '为了下载更新文件,我们需要访问设备存储空间。', + ); + if (!proceed) return false; + } + + status = await Permission.storage.request(); + return status.isGranted; + } + + /// Checks and requests notification permission. + /// + /// This is useful for showing download progress notifications. + static Future checkAndRequestNotificationPermission({required BuildContext context}) async { + if (!Platform.isAndroid) return true; + + var status = await Permission.notification.status; + if (status.isGranted) return true; + + // From Android 13, notification permission must be requested. + // On older versions, it's granted by default. + if (await _getAndroidSdkVersion() >= 33) { + if (context.mounted) { + final proceed = await _showRationaleDialog( + context, + title: '需要通知权限', + content: '为了在后台下载时向您显示更新进度,我们需要发送通知的权限。', + ); + if (!proceed) return false; + } + status = await Permission.notification.request(); + } + return status.isGranted; + } + + /// Checks the current status of the "Install Unknown Apps" permission. + /// + /// Returns [InstallPermissionStatus]. + static Future checkInstallPermission() async { + if (!Platform.isAndroid) return InstallPermissionStatus.granted; + + // On Android versions below 8.0, this permission doesn't exist. + if (await _getAndroidSdkVersion() < 26) { + return InstallPermissionStatus.granted; + } + + final status = await Permission.requestInstallPackages.status; + if (status.isGranted) return InstallPermissionStatus.granted; + if (status.isPermanentlyDenied) return InstallPermissionStatus.permanentlyDenied; + if (status.isRestricted) return InstallPermissionStatus.restricted; + return InstallPermissionStatus.denied; + } + + /// Requests the "Install Unknown Apps" permission. + /// + /// On Android 8.0 (API 26) and above, this will navigate the user to the + /// system settings page for the app. + /// + /// Shows a rationale dialog to the user before navigating to settings. + /// + /// Returns the [InstallPermissionStatus] after the user returns from settings. + static Future requestInstallPermission({ + required BuildContext context, + }) async { + if (!Platform.isAndroid || !context.mounted) { + return InstallPermissionStatus.granted; + } + if (await _getAndroidSdkVersion() < 26) { + return InstallPermissionStatus.granted; + } + + // Show a rationale dialog explaining why we need the permission and that + // we are about to navigate to the system settings. + final proceed = await _showRationaleDialog( + context, + title: '需要安装权限', + content: '为了完成应用更新,您需要允许我们安装应用。接下来将跳转到系统设置页面,请在那里开启“允许来自此来源的应用”选项。', + ); + + if (!proceed) return InstallPermissionStatus.denied; + + // For Android 8.0+, we need to open the system settings page manually + // because REQUEST_INSTALL_PACKAGES cannot be requested via normal dialog + try { + // First try the permission request (this will open settings on most devices) + await Permission.requestInstallPackages.request(); + + // Wait a moment for the user to potentially grant the permission + await Future.delayed(const Duration(milliseconds: 500)); + + // Check the permission status after the settings interaction + final finalStatus = await checkInstallPermission(); + + // If still not granted, try to open settings manually as fallback + if (finalStatus != InstallPermissionStatus.granted) { + if (context.mounted) { + final openSettings = await _showRationaleDialog( + context, + title: '需要手动开启权限', + content: '请在设置中找到"安装未知应用"选项并开启。是否现在前往设置?', + ); + + if (openSettings) { + await openAppSettings(); + // Give user time to change settings + await Future.delayed(const Duration(seconds: 1)); + } + } + } + + // Final check after all attempts + return await checkInstallPermission(); + } catch (e) { + debugPrint('Error requesting install permission: $e'); + // If there's an error, try opening app settings as fallback + if (context.mounted) { + final openSettings = await _showRationaleDialog( + context, + title: '权限设置异常', + content: '无法自动跳转到权限设置。请手动前往应用设置开启"安装未知应用"权限。', + ); + + if (openSettings) { + await openAppSettings(); + } + } + + return InstallPermissionStatus.denied; + } + } + + /// A comprehensive method to check and request the "Install Unknown Apps" + /// permission, handling all statuses. + /// + /// 1. Checks permission status. + /// 2. If denied, it requests the permission (with a rationale dialog). + /// 3. Handles permanently denied and restricted states with user feedback. + /// + /// Returns `true` if the permission is granted, `false` otherwise. + static Future checkAndRequestInstallPermission({ + required BuildContext context, + }) async { + var status = await checkInstallPermission(); + if (status == InstallPermissionStatus.granted) { + return true; + } + + // If permission is denied, request it. + if (status == InstallPermissionStatus.denied) { + status = await requestInstallPermission(context: context); + } + + // Handle final status after request attempt. + if (status != InstallPermissionStatus.granted && context.mounted) { + String message; + switch (status) { + case InstallPermissionStatus.permanentlyDenied: + message = '安装权限已被永久拒绝。请前往系统设置手动开启。'; + break; + case InstallPermissionStatus.restricted: + message = '安装应用的功能受限(例如,被家长控制),无法完成操作。'; + break; + case InstallPermissionStatus.denied: + default: + message = '未授予安装权限,无法完成更新。'; + break; + } + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(message)), + ); + } + + return status == InstallPermissionStatus.granted; + } + + /// Shows a dialog explaining why a permission is needed. + /// + /// Returns `true` if the user taps "Continue", `false` if they tap "Cancel". + static Future _showRationaleDialog( + BuildContext context, { + required String title, + required String content, + }) async { + final result = await showDialog( + context: context, + builder: (context) => AlertDialog( + title: Text(title), + content: Text(content), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(false), + child: const Text('取消'), + ), + TextButton( + onPressed: () => Navigator.of(context).pop(true), + child: const Text('继续'), + ), + ], + ), + ); + return result ?? false; + } + + /// A cached value for the Android SDK version. + static int? _sdkVersion; + + /// Gets the Android SDK version. + /// + /// Caches the result to avoid repeated platform channel calls. + static Future _getAndroidSdkVersion() async { + if (!Platform.isAndroid) return 0; + _sdkVersion ??= await _plugin.getAndroidSdkVersion() ?? 0; + return _sdkVersion!; + } +} diff --git a/lib/core/upgrade_config.dart b/lib/core/upgrade_config.dart new file mode 100644 index 0000000..95f56b5 --- /dev/null +++ b/lib/core/upgrade_config.dart @@ -0,0 +1,232 @@ +import 'package:flutter/foundation.dart'; + +/// 升级配置类 +class UpgradeConfig { + /// 单例实例 + static UpgradeConfig? _instance; + + /// 获取单例实例 + static UpgradeConfig get instance { + _instance ??= UpgradeConfig._(); + return _instance!; + } + + UpgradeConfig._(); + + /// 是否启用调试模式 + bool debugMode = kDebugMode; + + /// 检查更新的间隔时间(小时) + int checkIntervalHours = 24; + + /// 是否自动检查更新 + bool autoCheck = true; + + /// 是否只在WiFi下自动下载 + bool wifiOnly = true; + + /// 下载超时时间(秒) + int downloadTimeout = 300; + + /// 连接超时时间(秒) + int connectTimeout = 30; + + /// 最大重试次数 + int maxRetryCount = 3; + + /// 重试延迟时间(秒) + int retryDelay = 5; + + /// 是否使用缓存 + bool useCache = true; + + /// 缓存有效期(小时) + int cacheValidHours = 1; + + /// 是否显示下载通知(Android) + bool showNotification = true; + + /// 是否支持断点续传 + bool supportBreakpoint = true; + + /// 下载块大小(字节) + int chunkSize = 1024 * 1024; // 1MB + + /// 最大并发下载数 + int maxConcurrentDownloads = 3; + + /// 是否校验文件完整性 + bool verifyIntegrity = true; + + /// 是否允许移动网络下载 + bool allowCellular = false; + + /// 是否静默下载(后台下载) + bool silentDownload = false; + + /// 是否保存下载历史 + bool saveDownloadHistory = true; + + /// 自定义请求头 + Map customHeaders = {}; + + /// 代理设置 + String? proxyUrl; + + /// 更新配置 + void updateConfig({ + bool? debugMode, + int? checkIntervalHours, + bool? autoCheck, + bool? wifiOnly, + int? downloadTimeout, + int? connectTimeout, + int? maxRetryCount, + int? retryDelay, + bool? useCache, + int? cacheValidHours, + bool? showNotification, + bool? supportBreakpoint, + int? chunkSize, + int? maxConcurrentDownloads, + bool? verifyIntegrity, + bool? allowCellular, + bool? silentDownload, + bool? saveDownloadHistory, + Map? customHeaders, + String? proxyUrl, + }) { + if (debugMode != null) this.debugMode = debugMode; + if (checkIntervalHours != null) this.checkIntervalHours = checkIntervalHours; + if (autoCheck != null) this.autoCheck = autoCheck; + if (wifiOnly != null) this.wifiOnly = wifiOnly; + if (downloadTimeout != null) this.downloadTimeout = downloadTimeout; + if (connectTimeout != null) this.connectTimeout = connectTimeout; + if (maxRetryCount != null) this.maxRetryCount = maxRetryCount; + if (retryDelay != null) this.retryDelay = retryDelay; + if (useCache != null) this.useCache = useCache; + if (cacheValidHours != null) this.cacheValidHours = cacheValidHours; + if (showNotification != null) this.showNotification = showNotification; + if (supportBreakpoint != null) this.supportBreakpoint = supportBreakpoint; + if (chunkSize != null) this.chunkSize = chunkSize; + if (maxConcurrentDownloads != null) this.maxConcurrentDownloads = maxConcurrentDownloads; + if (verifyIntegrity != null) this.verifyIntegrity = verifyIntegrity; + if (allowCellular != null) this.allowCellular = allowCellular; + if (silentDownload != null) this.silentDownload = silentDownload; + if (saveDownloadHistory != null) this.saveDownloadHistory = saveDownloadHistory; + if (customHeaders != null) this.customHeaders = customHeaders; + if (proxyUrl != null) this.proxyUrl = proxyUrl; + } + + /// 重置为默认配置 + void reset() { + debugMode = kDebugMode; + checkIntervalHours = 24; + autoCheck = true; + wifiOnly = true; + downloadTimeout = 300; + connectTimeout = 30; + maxRetryCount = 3; + retryDelay = 5; + useCache = true; + cacheValidHours = 1; + showNotification = true; + supportBreakpoint = true; + chunkSize = 1024 * 1024; + maxConcurrentDownloads = 3; + verifyIntegrity = true; + allowCellular = false; + silentDownload = false; + saveDownloadHistory = true; + customHeaders = {}; + proxyUrl = null; + } + + /// 导出配置为Map + Map toMap() { + return { + 'debugMode': debugMode, + 'checkIntervalHours': checkIntervalHours, + 'autoCheck': autoCheck, + 'wifiOnly': wifiOnly, + 'downloadTimeout': downloadTimeout, + 'connectTimeout': connectTimeout, + 'maxRetryCount': maxRetryCount, + 'retryDelay': retryDelay, + 'useCache': useCache, + 'cacheValidHours': cacheValidHours, + 'showNotification': showNotification, + 'supportBreakpoint': supportBreakpoint, + 'chunkSize': chunkSize, + 'maxConcurrentDownloads': maxConcurrentDownloads, + 'verifyIntegrity': verifyIntegrity, + 'allowCellular': allowCellular, + 'silentDownload': silentDownload, + 'saveDownloadHistory': saveDownloadHistory, + 'customHeaders': customHeaders, + 'proxyUrl': proxyUrl, + }; + } + + /// 从Map导入配置 + void fromMap(Map map) { + updateConfig( + debugMode: map['debugMode'], + checkIntervalHours: map['checkIntervalHours'], + autoCheck: map['autoCheck'], + wifiOnly: map['wifiOnly'], + downloadTimeout: map['downloadTimeout'], + connectTimeout: map['connectTimeout'], + maxRetryCount: map['maxRetryCount'], + retryDelay: map['retryDelay'], + useCache: map['useCache'], + cacheValidHours: map['cacheValidHours'], + showNotification: map['showNotification'], + supportBreakpoint: map['supportBreakpoint'], + chunkSize: map['chunkSize'], + maxConcurrentDownloads: map['maxConcurrentDownloads'], + verifyIntegrity: map['verifyIntegrity'], + allowCellular: map['allowCellular'], + silentDownload: map['silentDownload'], + saveDownloadHistory: map['saveDownloadHistory'], + customHeaders: map['customHeaders'] != null ? Map.from(map['customHeaders']) : null, + proxyUrl: map['proxyUrl'], + ); + } +} + +/// 升级策略 +enum UpgradeStrategy { + /// 立即升级 + immediate, + + /// 空闲时升级 + idle, + + /// 定时升级 + scheduled, + + /// 静默升级 + silent, + + /// 灰度升级 + grayScale, +} + +/// 版本比较策略 +enum VersionCompareStrategy { + /// 数字比较(如:1.2.3) + numeric, + + /// 语义化版本比较(如:1.2.3-beta.1) + semantic, + + /// 时间戳比较 + timestamp, + + /// 构建号比较 + buildNumber, + + /// 自定义比较 + custom, +} diff --git a/lib/core/upgrade_utils.dart b/lib/core/upgrade_utils.dart new file mode 100644 index 0000000..e760888 --- /dev/null +++ b/lib/core/upgrade_utils.dart @@ -0,0 +1,8 @@ +String formatBytes(int bytes) { + if (bytes < 1024) return '$bytes B'; + if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(2)} KB'; + if (bytes < 1024 * 1024 * 1024) { + return '${(bytes / (1024 * 1024)).toStringAsFixed(2)} MB'; + } + return '${(bytes / (1024 * 1024 * 1024)).toStringAsFixed(2)} GB'; +} diff --git a/lib/core/version_comparator.dart b/lib/core/version_comparator.dart new file mode 100644 index 0000000..16559fd --- /dev/null +++ b/lib/core/version_comparator.dart @@ -0,0 +1,350 @@ +import 'upgrade_config.dart'; + +/// 版本信息 +class Version { + final String raw; + final int? major; + final int? minor; + final int? patch; + final String? preRelease; + final String? buildMetadata; + final int? buildNumber; + final DateTime? timestamp; + + Version({ + required this.raw, + this.major, + this.minor, + this.patch, + this.preRelease, + this.buildMetadata, + this.buildNumber, + this.timestamp, + }); + + /// 从字符串解析版本 + factory Version.parse(String version) { + final raw = version.trim(); + + // 尝试解析语义化版本 (1.2.3-beta.1+build.123) + final semanticRegex = RegExp(r'^(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z0-9\.\-]+))?(?:\+([a-zA-Z0-9\.\-]+))?$'); + + final match = semanticRegex.firstMatch(raw); + if (match != null) { + return Version( + raw: raw, + major: int.tryParse(match.group(1)!), + minor: int.tryParse(match.group(2)!), + patch: int.tryParse(match.group(3)!), + preRelease: match.group(4), + buildMetadata: match.group(5), + ); + } + + // 先尝试解析为纯数字构建号 + final buildNumber = int.tryParse(raw); + if (buildNumber != null && !raw.contains('.')) { + return Version(raw: raw, buildNumber: buildNumber); + } + + // 尝试解析简单版本 (1.2.3) + final simpleRegex = RegExp(r'^(\d+)(?:\.(\d+))?(?:\.(\d+))?$'); + final simpleMatch = simpleRegex.firstMatch(raw); + if (simpleMatch != null) { + return Version( + raw: raw, + major: int.tryParse(simpleMatch.group(1)!), + minor: simpleMatch.group(2) != null ? int.tryParse(simpleMatch.group(2)!) : null, + patch: simpleMatch.group(3) != null ? int.tryParse(simpleMatch.group(3)!) : null, + ); + } + + // 尝试解析为时间戳 + final timestamp = DateTime.tryParse(raw); + if (timestamp != null) { + return Version(raw: raw, timestamp: timestamp); + } + + // 无法解析,返回原始字符串 + return Version(raw: raw); + } + + /// 转换为字符串 + @override + String toString() => raw; + + /// 是否是预发布版本 + bool get isPreRelease => preRelease != null && preRelease!.isNotEmpty; + + /// 获取版本数组 + List get versionArray { + final arr = []; + if (major != null) arr.add(major!); + if (minor != null) arr.add(minor!); + if (patch != null) arr.add(patch!); + return arr; + } +} + +/// 版本比较器 +class VersionComparator { + final VersionCompareStrategy strategy; + final Function(Version, Version)? customComparator; + + VersionComparator({this.strategy = VersionCompareStrategy.semantic, this.customComparator}); + + /// 比较两个版本 + /// 返回: 1 表示 v1 > v2, 0 表示相等, -1 表示 v1 < v2 + int compare(String version1, String version2) { + if (version1 == version2) return 0; + + final v1 = Version.parse(version1); + final v2 = Version.parse(version2); + + switch (strategy) { + case VersionCompareStrategy.numeric: + return _compareNumeric(v1, v2); + case VersionCompareStrategy.semantic: + return _compareSemantic(v1, v2); + case VersionCompareStrategy.timestamp: + return _compareTimestamp(v1, v2); + case VersionCompareStrategy.buildNumber: + return _compareBuildNumber(v1, v2); + case VersionCompareStrategy.custom: + if (customComparator != null) { + return customComparator!(v1, v2); + } + return _compareSemantic(v1, v2); + } + } + + /// 数字比较 + int _compareNumeric(Version v1, Version v2) { + final arr1 = v1.versionArray; + final arr2 = v2.versionArray; + + if (arr1.isEmpty && arr2.isEmpty) { + return v1.raw.compareTo(v2.raw); + } + + if (arr1.isEmpty) return -1; + if (arr2.isEmpty) return 1; + + final maxLength = arr1.length > arr2.length ? arr1.length : arr2.length; + + for (int i = 0; i < maxLength; i++) { + final num1 = i < arr1.length ? arr1[i] : 0; + final num2 = i < arr2.length ? arr2[i] : 0; + + if (num1 > num2) return 1; + if (num1 < num2) return -1; + } + + return 0; + } + + /// 语义化版本比较 + int _compareSemantic(Version v1, Version v2) { + // 先比较主版本号 + final majorCompare = _compareInt(v1.major, v2.major); + if (majorCompare != 0) return majorCompare; + + // 比较次版本号 + final minorCompare = _compareInt(v1.minor, v2.minor); + if (minorCompare != 0) return minorCompare; + + // 比较修订号 + final patchCompare = _compareInt(v1.patch, v2.patch); + if (patchCompare != 0) return patchCompare; + + // 比较预发布版本 + if (v1.preRelease != null && v2.preRelease == null) return -1; + if (v1.preRelease == null && v2.preRelease != null) return 1; + if (v1.preRelease != null && v2.preRelease != null) { + return _comparePreRelease(v1.preRelease!, v2.preRelease!); + } + + return 0; + } + + /// 比较预发布版本 + int _comparePreRelease(String pre1, String pre2) { + final parts1 = pre1.split('.'); + final parts2 = pre2.split('.'); + + final maxLength = parts1.length > parts2.length ? parts1.length : parts2.length; + + for (int i = 0; i < maxLength; i++) { + if (i >= parts1.length) return -1; + if (i >= parts2.length) return 1; + + final part1 = parts1[i]; + final part2 = parts2[i]; + + // 尝试数字比较 + final num1 = int.tryParse(part1); + final num2 = int.tryParse(part2); + + if (num1 != null && num2 != null) { + if (num1 > num2) return 1; + if (num1 < num2) return -1; + } else { + // 字符串比较 + final compare = part1.compareTo(part2); + if (compare != 0) return compare; + } + } + + return 0; + } + + /// 时间戳比较 + int _compareTimestamp(Version v1, Version v2) { + if (v1.timestamp == null && v2.timestamp == null) { + return _compareSemantic(v1, v2); + } + + if (v1.timestamp == null) return -1; + if (v2.timestamp == null) return 1; + + return v1.timestamp!.compareTo(v2.timestamp!); + } + + /// 构建号比较 + int _compareBuildNumber(Version v1, Version v2) { + if (v1.buildNumber == null && v2.buildNumber == null) { + return _compareSemantic(v1, v2); + } + + if (v1.buildNumber == null) return -1; + if (v2.buildNumber == null) return 1; + + if (v1.buildNumber! > v2.buildNumber!) return 1; + if (v1.buildNumber! < v2.buildNumber!) return -1; + + return 0; + } + + /// 比较两个可空整数 + int _compareInt(int? a, int? b) { + if (a == null && b == null) return 0; + if (a == null) return -1; + if (b == null) return 1; + if (a > b) return 1; + if (a < b) return -1; + return 0; + } + + /// 检查是否需要更新 + bool isUpdateAvailable(String currentVersion, String remoteVersion) { + return compare(remoteVersion, currentVersion) > 0; + } + + /// 检查是否是主要版本更新 + bool isMajorUpdate(String currentVersion, String remoteVersion) { + final v1 = Version.parse(currentVersion); + final v2 = Version.parse(remoteVersion); + + if (v1.major != null && v2.major != null) { + return v2.major! > v1.major!; + } + + return false; + } + + /// 检查是否是次要版本更新 + bool isMinorUpdate(String currentVersion, String remoteVersion) { + final v1 = Version.parse(currentVersion); + final v2 = Version.parse(remoteVersion); + + if (v1.major != null && v2.major != null && v1.minor != null && v2.minor != null) { + return v2.major! == v1.major! && v2.minor! > v1.minor!; + } + + return false; + } + + /// 检查是否是修订版本更新 + bool isPatchUpdate(String currentVersion, String remoteVersion) { + final v1 = Version.parse(currentVersion); + final v2 = Version.parse(remoteVersion); + + if (v1.major != null && + v2.major != null && + v1.minor != null && + v2.minor != null && + v1.patch != null && + v2.patch != null) { + return v2.major! == v1.major! && v2.minor! == v1.minor! && v2.patch! > v1.patch!; + } + + return false; + } + + /// 获取版本差异描述 + String getVersionDifference(String currentVersion, String remoteVersion) { + if (isMajorUpdate(currentVersion, remoteVersion)) { + return '主要版本更新'; + } else if (isMinorUpdate(currentVersion, remoteVersion)) { + return '功能更新'; + } else if (isPatchUpdate(currentVersion, remoteVersion)) { + return 'Bug修复'; + } else if (isUpdateAvailable(currentVersion, remoteVersion)) { + return '新版本'; + } else { + return '已是最新版本'; + } + } + + /// 批量比较版本,返回最新版本 + String? getLatestVersion(List versions) { + if (versions.isEmpty) return null; + if (versions.length == 1) return versions.first; + + String latest = versions.first; + for (final version in versions.skip(1)) { + if (compare(version, latest) > 0) { + latest = version; + } + } + + return latest; + } + + /// 按版本排序 + List sortVersions(List versions, {bool descending = false}) { + final sorted = List.from(versions); + sorted.sort((a, b) => descending ? compare(b, a) : compare(a, b)); + return sorted; + } + + /// 过滤出符合条件的版本 + List filterVersions( + List versions, { + String? minVersion, + String? maxVersion, + bool includePreRelease = false, + }) { + return versions.where((version) { + // 检查最小版本 + if (minVersion != null && compare(version, minVersion) < 0) { + return false; + } + + // 检查最大版本 + if (maxVersion != null && compare(version, maxVersion) > 0) { + return false; + } + + // 检查预发布版本 + if (!includePreRelease) { + final v = Version.parse(version); + if (v.isPreRelease) { + return false; + } + } + + return true; + }).toList(); + } +} diff --git a/lib/models/app_market.dart b/lib/models/app_market.dart new file mode 100644 index 0000000..c896eb5 --- /dev/null +++ b/lib/models/app_market.dart @@ -0,0 +1,118 @@ +/// 应用商店信息 +class AppMarketInfo { + /// 应用商店 + final AppMarket market; + + /// 跳转链接(可选,用于网页跳转) + final String? url; + + /// 应用包名(可选,用于原生跳转) + final String? packageName; + + /// 自定义名称(当 market 为 custom 时使用) + final String? customName; + + AppMarketInfo({ + required this.market, + this.url, + this.packageName, + this.customName, + }); + + /// 从JSON创建 + factory AppMarketInfo.fromJson(Map json) { + return AppMarketInfo( + market: AppMarket.fromString(json['market']), + url: json['url'], + packageName: json['packageName'], + customName: json['customName'], + ); + } + + /// 转换为JSON + Map toJson() => { + 'market': market.name, + 'url': url, + 'packageName': packageName, + 'customName': customName, + }; + + /// 获取商店名称 + String get marketName { + if (market == AppMarket.custom && customName != null) { + return customName!; + } + return market.displayName; + } + + @override + String toString() { + return 'AppMarketInfo{market: $market, url: $url, packageName: $packageName, customName: $customName}'; + } +} + +/// 应用商店枚举 +enum AppMarket { + googlePlay, + appStore, + huawei, + oppo, + vivo, + xiaomi, + tencent, + coolapk, + custom, + unknown; + + /// 从字符串创建 + static AppMarket fromString(String? market) { + switch (market?.toLowerCase()) { + case 'googleplay': + return AppMarket.googlePlay; + case 'appstore': + return AppMarket.appStore; + case 'huawei': + return AppMarket.huawei; + case 'oppo': + return AppMarket.oppo; + case 'vivo': + return AppMarket.vivo; + case 'xiaomi': + return AppMarket.xiaomi; + case 'tencent': + return AppMarket.tencent; + case 'coolapk': + return AppMarket.coolapk; + case 'custom': + return AppMarket.custom; + default: + return AppMarket.unknown; + } + } + + /// 获取显示名称 + String get displayName { + switch (this) { + case AppMarket.googlePlay: + return 'Google Play'; + case AppMarket.appStore: + return 'App Store'; + case AppMarket.huawei: + return '华为应用市场'; + case AppMarket.oppo: + return 'OPPO软件商店'; + case AppMarket.vivo: + return 'vivo应用商店'; + case AppMarket.xiaomi: + return '小米应用商店'; + case AppMarket.tencent: + return '腾讯应用宝'; + case AppMarket.coolapk: + return '酷安'; + case AppMarket.custom: + return '自定义'; + default: + return '未知'; + } + } +} diff --git a/lib/models/upgrade_info.dart b/lib/models/upgrade_info.dart new file mode 100644 index 0000000..4e8db20 --- /dev/null +++ b/lib/models/upgrade_info.dart @@ -0,0 +1,96 @@ +import 'package:app_upgrade_plugin/models/app_market.dart'; + +/// App升级信息模型 +class UpgradeInfo { + /// 是否有新版本 + final bool hasUpdate; + + /// 是否强制更新 + final bool isForceUpdate; + + /// 版本号 + final String versionCode; + + /// 版本名称 + final String versionName; + + /// 更新说明 + final String updateContent; + + /// 下载地址(Android APK地址) + final String? downloadUrl; + + /// App Store地址(iOS) + final String? appStoreUrl; + + /// APK文件大小(字节) + final int? apkSize; + + /// APK MD5值(用于校验) + final String? apkMd5; + + /// 应用商店列表(用于Android多渠道更新) + final List? appMarkets; + + UpgradeInfo({ + this.hasUpdate = false, + required this.isForceUpdate, + required this.versionCode, + required this.versionName, + required this.updateContent, + this.downloadUrl, + this.appStoreUrl, + this.apkSize, + this.apkMd5, + this.appMarkets, + }); + + factory UpgradeInfo.fromJson(Map json) { + return UpgradeInfo( + hasUpdate: json['hasUpdate'] as bool? ?? false, + isForceUpdate: json['isForceUpdate'] ?? false, + versionCode: json['versionCode'] ?? '', + versionName: json['versionName'] ?? '', + updateContent: json['updateContent'] ?? '', + downloadUrl: json['downloadUrl'] as String?, + appStoreUrl: json['appStoreUrl'] as String?, + apkSize: json['apkSize'] as int?, + apkMd5: json['apkMd5'] as String?, + appMarkets: (json['appMarkets'] as List?) + ?.map((e) => AppMarketInfo.fromJson(e as Map)) + .toList(), + ); + } + + Map toJson() { + return { + 'hasUpdate': hasUpdate, + 'isForceUpdate': isForceUpdate, + 'versionCode': versionCode, + 'versionName': versionName, + 'updateContent': updateContent, + 'downloadUrl': downloadUrl, + 'appStoreUrl': appStoreUrl, + 'apkSize': apkSize, + 'apkMd5': apkMd5, + 'appMarkets': appMarkets?.map((e) => e.toJson()).toList(), + }; + } +} + +/// 下载进度信息 +class DownloadProgress { + /// 已下载字节数 + final int received; + + /// 总字节数 + final int total; + + /// 下载进度(0.0 - 1.0) + double get progress => total > 0 ? received / total : 0.0; + + /// 进度百分比(0 - 100) + int get percentage => (progress * 100).toInt(); + + DownloadProgress({required this.received, required this.total}); +} diff --git a/lib/widgets/download_progress_dialog.dart b/lib/widgets/download_progress_dialog.dart new file mode 100644 index 0000000..60cdee0 --- /dev/null +++ b/lib/widgets/download_progress_dialog.dart @@ -0,0 +1,178 @@ +import 'package:flutter/material.dart'; + +import '../app_upgrade_plugin.dart'; +import '../core/upgrade_utils.dart'; + +/// 下载进度对话框 +class DownloadProgressDialog extends StatefulWidget { + final String downloadUrl; + final Color? primaryColor; + + const DownloadProgressDialog({super.key, required this.downloadUrl, this.primaryColor}); + + /// 显示下载进度对话框 + static Future show(BuildContext context, {required String downloadUrl, Color? primaryColor}) { + return showDialog( + context: context, + barrierDismissible: false, + builder: (context) => DownloadProgressDialog(downloadUrl: downloadUrl, primaryColor: primaryColor), + ); + } + + @override + State createState() => _DownloadProgressDialogState(); +} + +class _DownloadProgressDialogState extends State { + final AppUpgradePlugin _plugin = AppUpgradePlugin(); + + double _progress = 0.0; + String _progressText = '准备下载...'; + int _received = 0; + int _total = 0; + bool _isDownloading = false; + String? _errorMessage; + + @override + void initState() { + super.initState(); + _startDownload(); + } + + @override + Widget build(BuildContext context) { + final primaryColor = widget.primaryColor ?? Theme.of(context).primaryColor; + + return WillPopScope( + onWillPop: () async => false, + child: Dialog( + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + child: Padding( + padding: const EdgeInsets.all(24), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + // 图标 + Icon( + _errorMessage != null ? Icons.error_outline : Icons.download_rounded, + size: 48, + color: _errorMessage != null ? Colors.red : primaryColor, + ), + const SizedBox(height: 16), + + // 标题 + Text( + _errorMessage != null ? '下载失败' : '正在下载更新', + style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 16), + + if (_errorMessage != null) ...[ + // 错误信息 + Text( + _errorMessage!, + style: TextStyle(fontSize: 14, color: Colors.grey[600]), + textAlign: TextAlign.center, + ), + const SizedBox(height: 16), + + // 按钮 + Row( + children: [ + Expanded( + child: TextButton( + onPressed: () { + Navigator.of(context).pop(null); + }, + child: const Text('取消'), + ), + ), + const SizedBox(width: 16), + Expanded( + child: ElevatedButton( + onPressed: () { + setState(() { + _errorMessage = null; + _progress = 0.0; + _progressText = '准备下载...'; + }); + _startDownload(); + }, + style: ElevatedButton.styleFrom(backgroundColor: primaryColor), + child: const Text('重试'), + ), + ), + ], + ), + ] else ...[ + // 进度条 + LinearProgressIndicator( + value: _isDownloading ? _progress : null, + backgroundColor: Colors.grey[300], + valueColor: AlwaysStoppedAnimation(primaryColor), + ), + const SizedBox(height: 12), + + // 进度文本 + Text(_progressText, style: TextStyle(fontSize: 14, color: Colors.grey[600])), + const SizedBox(height: 8), + + // 文件大小信息 + if (_total > 0) + Text( + '${formatBytes(_received)} / ${formatBytes(_total)}', + style: TextStyle(fontSize: 12, color: Colors.grey[500]), + ), + ], + ], + ), + ), + ), + ); + } + + Future _startDownload() async { + setState(() { + _isDownloading = true; + _errorMessage = null; + }); + + try { + final filePath = await _plugin.downloadApk( + widget.downloadUrl, + onProgress: (progress) { + setState(() { + _progress = progress.progress; + _received = progress.received; + _total = progress.total; + _progressText = '下载中 ${progress.percentage}%'; + }); + }, + ); + + if (filePath != null) { + setState(() { + _progressText = '下载完成'; + }); + + // 延迟一下让用户看到完成状态 + await Future.delayed(const Duration(milliseconds: 500)); + + // 返回文件路径 + if (mounted) { + Navigator.of(context).pop(filePath); + } + } else { + setState(() { + _errorMessage = '下载失败,请检查网络连接'; + _isDownloading = false; + }); + } + } catch (e) { + setState(() { + _errorMessage = '下载出错:${e.toString()}'; + _isDownloading = false; + }); + } + } +} diff --git a/lib/widgets/market_selection_dialog.dart b/lib/widgets/market_selection_dialog.dart new file mode 100644 index 0000000..2c802c2 --- /dev/null +++ b/lib/widgets/market_selection_dialog.dart @@ -0,0 +1,77 @@ +import 'package:flutter/material.dart'; + +import '../models/app_market.dart'; + +/// 应用商店选择对话框 +class MarketSelectionDialog extends StatelessWidget { + final List markets; + final ValueChanged onSelected; + + const MarketSelectionDialog({ + super.key, + required this.markets, + required this.onSelected, + }); + + static Future show( + BuildContext context, { + required List markets, + required ValueChanged onSelected, + }) async { + await showDialog( + context: context, + useRootNavigator: true, + builder: (context) => MarketSelectionDialog( + markets: markets, + onSelected: onSelected, + ), + ); + } + + @override + Widget build(BuildContext context) { + return AlertDialog( + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), + title: const Text('选择应用商店'), + content: ConstrainedBox( + constraints: const BoxConstraints(maxHeight: 360), + child: SingleChildScrollView( + child: Column( + mainAxisSize: MainAxisSize.min, + children: markets.map((market) { + return ListTile( + leading: _getMarketIcon(market.market), + title: Text(market.marketName), + onTap: () { + Navigator.of(context).pop(); + onSelected(market); + }, + ); + }).toList(), + ), + ), + ), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(), + child: const Text('取消'), + ), + ], + ); + } + + Widget _getMarketIcon(AppMarket market) { + // 这里可以根据不同的商店返回不同的图标 + // 为了简化,这里统一使用一个图标 + switch (market) { + case AppMarket.googlePlay: + return const Icon(Icons.shop, color: Colors.green); + case AppMarket.huawei: + return const Icon(Icons.shop, color: Colors.red); + case AppMarket.tencent: + return const Icon(Icons.shop, color: Colors.blue); + default: + return const Icon(Icons.store); + } + } +} diff --git a/lib/widgets/upgrade_dialog.dart b/lib/widgets/upgrade_dialog.dart new file mode 100644 index 0000000..a8c2401 --- /dev/null +++ b/lib/widgets/upgrade_dialog.dart @@ -0,0 +1,195 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; + +import '../app_upgrade_plugin.dart'; +import '../core/upgrade_utils.dart'; + +/// App升级对话框 +class UpgradeDialog extends StatefulWidget { + final UpgradeInfo upgradeInfo; + final VoidCallback? onCancel; + final VoidCallback? onConfirm; + final Color? primaryColor; + + const UpgradeDialog({super.key, required this.upgradeInfo, this.onCancel, this.onConfirm, this.primaryColor}); + + /// 显示升级对话框 + static Future show(BuildContext context, {required UpgradeInfo upgradeInfo, Color? primaryColor}) { + return showDialog( + context: context, + barrierDismissible: !upgradeInfo.isForceUpdate, + builder: (BuildContext context) { + return UpgradeDialog( + upgradeInfo: upgradeInfo, + primaryColor: primaryColor, + ); + }, + ); + } + + @override + State createState() => _UpgradeDialogState(); +} + +class _UpgradeDialogState extends State { + final AppUpgradePlugin _plugin = AppUpgradePlugin(); + bool _isDownloading = false; + + @override + Widget build(BuildContext context) { + final primaryColor = widget.primaryColor ?? Theme.of(context).primaryColor; + + return WillPopScope( + onWillPop: () async => !widget.upgradeInfo.isForceUpdate && !_isDownloading, + child: Dialog( + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + // 顶部图片或图标 + Container( + height: 120, + decoration: BoxDecoration( + color: primaryColor, + borderRadius: const BorderRadius.vertical(top: Radius.circular(16)), + ), + child: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Icon(Icons.system_update, size: 48, color: Colors.white), + const SizedBox(height: 8), + Text( + '发现新版本 ${widget.upgradeInfo.versionName}', + style: const TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold), + ), + ], + ), + ), + ), + + // 更新内容 + Container( + padding: const EdgeInsets.all(16), + constraints: const BoxConstraints(maxHeight: 200), + child: SingleChildScrollView( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text('更新内容:', style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold)), + const SizedBox(height: 8), + Text(widget.upgradeInfo.updateContent, style: const TextStyle(fontSize: 14, height: 1.5)), + ], + ), + ), + ), + + // 文件大小提示(如果有) + if (widget.upgradeInfo.apkSize != null && Platform.isAndroid) + Padding( + padding: const EdgeInsets.symmetric(horizontal: 16), + child: Text( + '新版本大小:${formatBytes(widget.upgradeInfo.apkSize!)}', + style: TextStyle(fontSize: 12, color: Colors.grey[600]), + ), + ), + + const SizedBox(height: 16), + + // 按钮 + Container( + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), + child: Row( + children: [ + if (!widget.upgradeInfo.isForceUpdate) ...[ + Expanded( + child: TextButton( + onPressed: _isDownloading + ? null + : () { + widget.onCancel?.call(); + Navigator.of(context).pop(false); + }, + child: Text('稍后更新', style: TextStyle(color: Colors.grey[600])), + ), + ), + const SizedBox(width: 16), + ], + Expanded( + child: ElevatedButton( + onPressed: _isDownloading ? null : () => _handleConfirm(), + style: ElevatedButton.styleFrom( + backgroundColor: primaryColor, + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24)), + ), + child: const Text('立即更新'), + ), + ), + ], + ), + ), + const SizedBox(height: 8), + ], + ), + ), + ); + } + + void _handleConfirm() async { + widget.onConfirm?.call(); + + if (Platform.isAndroid) { + // Android平台:下载并安装APK + if (widget.upgradeInfo.downloadUrl != null) { + setState(() { + _isDownloading = true; + }); + + // 显示下载进度对话框 + final filePath = await DownloadProgressDialog.show( + context, + downloadUrl: widget.upgradeInfo.downloadUrl!, + primaryColor: widget.primaryColor, + ); + + setState(() { + _isDownloading = false; + }); + + if (filePath != null) { + // 安装前再次检查并请求权限 + final hasInstallPermission = await PermissionHelper.checkAndRequestInstallPermission(context: context); + if (!hasInstallPermission) { + _showError('未授予安装权限,无法完成更新'); + return; + } + + // 安装APK + final success = await _plugin.installApk(filePath); + if (success) { + if (!widget.upgradeInfo.isForceUpdate) { + Navigator.of(context).pop(true); + } + } else { + _showError('安装失败,请检查权限设置'); + } + } + } + } else if (Platform.isIOS) { + // iOS平台:跳转到App Store + if (widget.upgradeInfo.appStoreUrl != null) { + final success = await _plugin.goToAppStore(widget.upgradeInfo.appStoreUrl!); + if (success) { + Navigator.of(context).pop(true); + } else { + _showError('跳转App Store失败'); + } + } + } + } + + void _showError(String message) { + ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message), backgroundColor: Colors.red)); + } +} diff --git a/lib/widgets/widgets.dart b/lib/widgets/widgets.dart new file mode 100644 index 0000000..3b0cb1a --- /dev/null +++ b/lib/widgets/widgets.dart @@ -0,0 +1,2 @@ +export 'download_progress_dialog.dart'; +export 'upgrade_dialog.dart'; diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..67be71f --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,86 @@ +name: app_upgrade_plugin +description: "A universal, robust, and high-performance Flutter app upgrade plugin with smart update features." +version: 1.0.0 +homepage: https://github.com/yourusername/app_upgrade_plugin + +environment: + sdk: '>=3.0.0 <4.0.0' + flutter: '>=3.3.0' + +dependencies: + flutter: + sdk: flutter + flutter_localizations: + sdk: flutter + + plugin_platform_interface: ^2.0.2 + dio: ^5.7.0 + path_provider: ^2.1.5 + package_info_plus: ^8.1.0 + url_launcher: ^6.3.1 + permission_handler: ^11.3.1 + connectivity_plus: ^6.1.0 + crypto: ^3.0.6 + shared_preferences: ^2.3.3 + flutter_local_notifications: ^18.0.1 + device_info_plus: ^11.2.0 + fluttertoast: ^8.2.11 + +dev_dependencies: + flutter_test: + sdk: flutter + flutter_lints: ^5.0.0 + +# For information on the generic Dart part of this file, see the +# following page: https://dart.dev/tools/pub/pubspec + +# The following section is specific to Flutter packages. +flutter: + # This section identifies this Flutter project as a plugin project. + # The 'pluginClass' specifies the class (in Java, Kotlin, Swift, Objective-C, etc.) + # which should be registered in the plugin registry. This is required for + # using method channels. + # The Android 'package' specifies package in which the registered class is. + # This is required for using method channels on Android. + # The 'ffiPlugin' specifies that native code should be built and bundled. + # This is required for using `dart:ffi`. + # All these are used by the tooling to maintain consistency when + # adding or updating assets for this project. + plugin: + platforms: + android: + package: com.example.app_upgrade_plugin + pluginClass: AppUpgradePlugin + ios: + pluginClass: AppUpgradePlugin + + # To add assets to your plugin package, add an assets section, like this: + # assets: + # - images/a_dot_burr.jpeg + # - images/a_dot_ham.jpeg + # + # For details regarding assets in packages, see + # https://flutter.dev/to/asset-from-package + # + # An image asset can refer to one or more resolution-specific "variants", see + # https://flutter.dev/to/resolution-aware-images + + # To add custom fonts to your plugin package, add a fonts section here, + # in this "flutter" section. Each entry in this list should have a + # "family" key with the font family name, and a "fonts" key with a + # list giving the asset and other descriptors for the font. For + # example: + # fonts: + # - family: Schyler + # fonts: + # - asset: fonts/Schyler-Regular.ttf + # - asset: fonts/Schyler-Italic.ttf + # style: italic + # - family: Trajan Pro + # fonts: + # - asset: fonts/TrajanPro.ttf + # - asset: fonts/TrajanPro_Bold.ttf + # weight: 700 + # + # For details regarding fonts in packages, see + # https://flutter.dev/to/font-from-package diff --git a/test/app_upgrade_plugin_method_channel_test.dart b/test/app_upgrade_plugin_method_channel_test.dart new file mode 100644 index 0000000..93c8674 --- /dev/null +++ b/test/app_upgrade_plugin_method_channel_test.dart @@ -0,0 +1,27 @@ +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:app_upgrade_plugin/app_upgrade_plugin_method_channel.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + MethodChannelAppUpgradePlugin platform = MethodChannelAppUpgradePlugin(); + const MethodChannel channel = MethodChannel('app_upgrade_plugin'); + + setUp(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + channel, + (MethodCall methodCall) async { + return '42'; + }, + ); + }); + + tearDown(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(channel, null); + }); + + test('getPlatformVersion', () async { + expect(await platform.getPlatformVersion(), '42'); + }); +} diff --git a/test/app_upgrade_plugin_test.dart b/test/app_upgrade_plugin_test.dart new file mode 100644 index 0000000..47cb006 --- /dev/null +++ b/test/app_upgrade_plugin_test.dart @@ -0,0 +1,220 @@ +import 'package:app_upgrade_plugin/app_upgrade_plugin_enhanced.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + group('版本比较测试', () { + late VersionComparator comparator; + + setUp(() { + comparator = VersionComparator(strategy: VersionCompareStrategy.semantic); + }); + + test('语义化版本比较', () { + expect(comparator.compare('1.0.0', '2.0.0'), equals(-1)); + expect(comparator.compare('2.0.0', '1.0.0'), equals(1)); + expect(comparator.compare('1.0.0', '1.0.0'), equals(0)); + expect(comparator.compare('1.2.3', '1.2.4'), equals(-1)); + expect(comparator.compare('1.2.3', '1.3.0'), equals(-1)); + expect(comparator.compare('2.0.0', '1.9.9'), equals(1)); + }); + + test('预发布版本比较', () { + expect(comparator.compare('1.0.0-alpha', '1.0.0-beta'), equals(-1)); + expect(comparator.compare('1.0.0-beta', '1.0.0'), equals(-1)); + expect(comparator.compare('1.0.0', '1.0.0-beta'), equals(1)); + }); + + test('版本更新检测', () { + expect(comparator.isUpdateAvailable('1.0.0', '2.0.0'), isTrue); + expect(comparator.isUpdateAvailable('2.0.0', '1.0.0'), isFalse); + expect(comparator.isMajorUpdate('1.0.0', '2.0.0'), isTrue); + expect(comparator.isMinorUpdate('1.0.0', '1.1.0'), isTrue); + expect(comparator.isPatchUpdate('1.0.0', '1.0.1'), isTrue); + }); + + test('批量版本操作', () { + final versions = ['1.0.0', '2.0.0', '1.5.0', '1.2.0']; + expect(comparator.getLatestVersion(versions), equals('2.0.0')); + + final sorted = comparator.sortVersions(versions); + expect(sorted, equals(['1.0.0', '1.2.0', '1.5.0', '2.0.0'])); + + final sortedDesc = comparator.sortVersions(versions, descending: true); + expect(sortedDesc, equals(['2.0.0', '1.5.0', '1.2.0', '1.0.0'])); + }); + }); + + group('升级信息模型测试', () { + test('从JSON创建', () { + final json = { + 'hasUpdate': true, + 'isForceUpdate': false, + 'versionCode': '2', + 'versionName': '1.2.0', + 'updateContent': 'Bug fixes', + 'downloadUrl': 'https://example.com/app.apk', + 'appStoreUrl': 'https://apps.apple.com/app/id123', + 'apkSize': 1024 * 1024 * 10, + 'apkMd5': 'abc123', + }; + + final info = UpgradeInfo.fromJson(json); + expect(info.hasUpdate, isTrue); + expect(info.isForceUpdate, isFalse); + expect(info.versionCode, equals('2')); + expect(info.versionName, equals('1.2.0')); + expect(info.updateContent, equals('Bug fixes')); + expect(info.downloadUrl, equals('https://example.com/app.apk')); + expect(info.appStoreUrl, equals('https://apps.apple.com/app/id123')); + expect(info.apkSize, equals(1024 * 1024 * 10)); + expect(info.apkMd5, equals('abc123')); + }); + + test('转换为JSON', () { + final info = UpgradeInfo( + hasUpdate: true, + isForceUpdate: true, + versionCode: '3', + versionName: '2.0.0', + updateContent: 'Major update', + ); + + final json = info.toJson(); + expect(json['hasUpdate'], isTrue); + expect(json['isForceUpdate'], isTrue); + expect(json['versionCode'], equals('3')); + expect(json['versionName'], equals('2.0.0')); + expect(json['updateContent'], equals('Major update')); + }); + }); + + group('下载进度测试', () { + test('进度计算', () { + final progress = DownloadProgress(received: 500, total: 1000); + expect(progress.progress, equals(0.5)); + expect(progress.percentage, equals(50)); + }); + + test('处理总大小为0', () { + final progress = DownloadProgress(received: 100, total: 0); + expect(progress.progress, equals(0.0)); + expect(progress.percentage, equals(0)); + }); + }); + + group('配置管理测试', () { + test('配置更新', () { + final config = UpgradeConfig.instance; + + config.updateConfig(debugMode: false, wifiOnly: false, maxRetryCount: 5); + + expect(config.debugMode, isFalse); + expect(config.wifiOnly, isFalse); + expect(config.maxRetryCount, equals(5)); + }); + + test('配置重置', () { + final config = UpgradeConfig.instance; + + config.updateConfig(maxRetryCount: 10); + config.reset(); + + expect(config.maxRetryCount, equals(3)); // 默认值 + }); + + test('配置导出导入', () { + final config = UpgradeConfig.instance; + + config.updateConfig(debugMode: false, wifiOnly: false, maxRetryCount: 5); + + final exportedMap = config.toMap(); + expect(exportedMap['debugMode'], isFalse); + expect(exportedMap['wifiOnly'], isFalse); + expect(exportedMap['maxRetryCount'], equals(5)); + + config.reset(); + config.fromMap(exportedMap); + + expect(config.debugMode, isFalse); + expect(config.wifiOnly, isFalse); + expect(config.maxRetryCount, equals(5)); + }); + }); + + // 插件基础功能测试需要mock平台通道,暂时跳过 + // group('插件基础功能测试', () { + // test('插件单例', () { + // final plugin1 = AppUpgradePluginEnhanced.instance; + // final plugin2 = AppUpgradePluginEnhanced.instance; + + // expect(identical(plugin1, plugin2), isTrue); + // }); + + // test('回调管理', () { + // final plugin = AppUpgradePluginEnhanced.instance; + + // int upgradeCallCount = 0; + // int downloadCallCount = 0; + // int errorCallCount = 0; + + // void upgradeCallback(UpgradeInfo info) { + // upgradeCallCount++; + // } + + // void downloadCallback(DownloadTask task) { + // downloadCallCount++; + // } + + // void errorCallback(String error) { + // errorCallCount++; + // } + + // plugin.addUpgradeCallback(upgradeCallback); + // plugin.addDownloadCallback(downloadCallback); + // plugin.addErrorCallback(errorCallback); + + // // 移除回调 + // plugin.removeUpgradeCallback(upgradeCallback); + // plugin.removeDownloadCallback(downloadCallback); + // plugin.removeErrorCallback(errorCallback); + + // // 验证回调已移除 + // expect(upgradeCallCount, equals(0)); + // expect(downloadCallCount, equals(0)); + // expect(errorCallCount, equals(0)); + // }); + // }); + + group('版本解析测试', () { + test('解析语义化版本', () { + final version = Version.parse('1.2.3-beta.1+build.123'); + expect(version.major, equals(1)); + expect(version.minor, equals(2)); + expect(version.patch, equals(3)); + expect(version.preRelease, equals('beta.1')); + expect(version.buildMetadata, equals('build.123')); + expect(version.isPreRelease, isTrue); + }); + + test('解析简单版本', () { + final version = Version.parse('1.2.3'); + expect(version.major, equals(1)); + expect(version.minor, equals(2)); + expect(version.patch, equals(3)); + expect(version.preRelease, isNull); + expect(version.isPreRelease, isFalse); + }); + + test('解析构建号', () { + final version = Version.parse('123'); + expect(version.buildNumber, equals(123)); + expect(version.major, isNull); + }); + + test('版本数组', () { + final version = Version.parse('1.2.3'); + expect(version.versionArray, equals([1, 2, 3])); + }); + }); +}