197 lines
6.3 KiB
Dart
197 lines
6.3 KiB
Dart
import 'dart:io';
|
||
|
||
import 'package:flutter/widgets.dart';
|
||
|
||
import 'app_upgrade_plugin_platform_interface.dart';
|
||
import 'core/http_config.dart';
|
||
import 'models/install_strategy.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/app_upgrade_method.dart';
|
||
// 导出新定义的模型
|
||
export 'models/app_upgrade_version.dart';
|
||
export 'models/install_strategy.dart';
|
||
export 'models/upgrade_info.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<String?> getPlatformVersion() {
|
||
return AppUpgradePluginPlatform.instance.getPlatformVersion();
|
||
}
|
||
|
||
Future<int?> getAndroidSdkVersion() {
|
||
return AppUpgradePluginPlatform.instance.getAndroidSdkVersion();
|
||
}
|
||
|
||
/// 打开安装未知应用权限设置页面(仅Android)
|
||
///
|
||
/// 对于Android 8.0及以上版本,会直接跳转到该应用的"安装未知应用"权限页面
|
||
/// 对于Android 8.0以下版本,会跳转到应用详情页面
|
||
///
|
||
/// 返回true表示成功打开设置页面,false表示打开失败
|
||
Future<bool> openInstallPermissionSettings() {
|
||
if (!Platform.isAndroid) {
|
||
return Future.value(false);
|
||
}
|
||
return AppUpgradePluginPlatform.instance.openInstallPermissionSettings();
|
||
}
|
||
|
||
/// 获取设备信息(仅Android)
|
||
///
|
||
/// 返回包含设备制造商、型号、Android版本等信息的Map
|
||
/// 主要用于调试和了解设备特性
|
||
Future<Map<String, dynamic>?> getDeviceInfo() {
|
||
if (!Platform.isAndroid) {
|
||
return Future.value(null);
|
||
}
|
||
return AppUpgradePluginPlatform.instance.getDeviceInfo();
|
||
}
|
||
|
||
/// 获取设备上已安装的应用市场类型列表(仅Android)
|
||
///
|
||
/// 返回已安装的应用市场类型列表,如 ["huawei", "xiaomi"]
|
||
/// 用于检测当前设备支持哪些应用市场
|
||
Future<List<String>> getInstalledMarkets() {
|
||
if (!Platform.isAndroid) {
|
||
return Future.value([]);
|
||
}
|
||
return AppUpgradePluginPlatform.instance.getInstalledMarkets();
|
||
}
|
||
|
||
/// 获取当前App信息
|
||
Future<Map<String, String>> getAppInfo() {
|
||
return AppUpgradePluginPlatform.instance.getAppInfo();
|
||
}
|
||
|
||
/// 检查更新
|
||
/// [url] 检查更新的接口地址
|
||
/// [params] 额外的请求参数
|
||
Future<UpgradeInfo?> checkUpdate(String url, {Map<String, dynamic>? params}) {
|
||
return AppUpgradePluginPlatform.instance.checkUpdate(url, params: params);
|
||
}
|
||
|
||
/// 下载APK文件(仅Android)
|
||
/// [url] APK下载地址
|
||
/// [onProgress] 下载进度回调
|
||
/// [savePath] 保存路径(可选,默认使用系统下载目录)
|
||
Future<String?> 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<bool> installApk(String filePath) {
|
||
if (!Platform.isAndroid) {
|
||
return Future.value(false);
|
||
}
|
||
return AppUpgradePluginPlatform.instance.installApk(filePath);
|
||
}
|
||
|
||
/// 使用系统流程安装APK(仅Android)
|
||
///
|
||
/// 类似市面上主流应用的安装流程:
|
||
/// 1. 直接调用系统安装程序
|
||
/// 2. 系统自动处理权限检查
|
||
/// 3. 弹出"XX正尝试安装应用"对话框
|
||
/// 4. 用户点击"继续"即可安装
|
||
///
|
||
/// [filePath] APK文件路径
|
||
Future<bool> installApkWithSystemFlow(String filePath) {
|
||
if (!Platform.isAndroid) {
|
||
return Future.value(false);
|
||
}
|
||
return AppUpgradePluginPlatform.instance.installApkWithSystemFlow(filePath);
|
||
}
|
||
|
||
/// 使用配置策略安装APK(仅Android)
|
||
///
|
||
/// 根据[config]配置动态选择安装策略:
|
||
/// - [InstallStrategy.systemFlow]: 系统流程安装
|
||
/// - [InstallStrategy.preCheckPermission]: 预检查权限
|
||
/// - [InstallStrategy.smart]: 智能选择策略
|
||
///
|
||
/// [filePath] APK文件路径
|
||
/// [config] 安装配置,默认使用系统流程
|
||
Future<bool> installApkWithConfig(String filePath, {InstallConfig config = InstallConfig.systemFlow}) {
|
||
if (!Platform.isAndroid) {
|
||
return Future.value(false);
|
||
}
|
||
return AppUpgradePluginPlatform.instance.installApkWithConfig(filePath, config);
|
||
}
|
||
|
||
/// 跳转到应用商店
|
||
/// [url] 应用商店地址
|
||
Future<bool> goToAppStore(String url, {required BuildContext context}) {
|
||
return AppUpgradePluginPlatform.instance.goToAppStore(url, context: context);
|
||
}
|
||
|
||
/// 获取下载目录路径
|
||
Future<String?> getDownloadPath() {
|
||
return AppUpgradePluginPlatform.instance.getDownloadPath();
|
||
}
|
||
|
||
/// 检查是否已下载指定版本的APK
|
||
/// [version] 版本号
|
||
/// [md5] MD5值(可选)
|
||
Future<bool> checkApkExists(String version, String? md5) {
|
||
if (!Platform.isAndroid) {
|
||
return Future.value(false);
|
||
}
|
||
return AppUpgradePluginPlatform.instance.checkApkExists(version, md5);
|
||
}
|
||
}
|