upgrade.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'dart:io';
  2. import 'package:dio/dio.dart';
  3. import 'package:fis_common/logger/logger.dart';
  4. import 'package:fis_jsonrpc/rpc.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:install_plugin_custom/install_plugin_custom.dart';
  7. import 'package:package_info_plus/package_info_plus.dart';
  8. import 'package:path_provider/path_provider.dart';
  9. import 'package:platform_device_id/platform_device_id.dart';
  10. import 'package:vitalapp/managers/interfaces/models/upgrade.dart';
  11. import 'package:vitalapp/rpc.dart';
  12. import 'interfaces/upgrade.dart';
  13. class UpgradeManager implements IUpgradeManager {
  14. static const _apkCacheRoot = "cache/install/";
  15. @override
  16. Future<String> getCurrentVersion() async {
  17. PackageInfo packageInfo = await PackageInfo.fromPlatform();
  18. final version = packageInfo.version;
  19. return version;
  20. }
  21. @override
  22. Future<UpgradeCheckResultModel?> checkNewVersionArrival() async {
  23. try {
  24. final currVersion = await getCurrentVersion();
  25. logger.i("current version $currVersion.");
  26. final deviceId = await PlatformDeviceId.getDeviceId;
  27. final request = GetLastUpgradeRequest(
  28. clientVersion: currVersion,
  29. deviceSerialNumber: deviceId,
  30. );
  31. final result = await rpc.vitalUpgrade.getLastUpgradeAsync(request);
  32. final model = UpgradeCheckResultModel(
  33. version: result.version ?? '',
  34. isCoerce: result.isCoerce,
  35. notes: result.upgradeNotes ?? '',
  36. downloadUrl: result.upgradeFileUrl ?? '',
  37. isNeedUpdate: result.isNeedUpdate,
  38. );
  39. return model;
  40. } catch (e) {
  41. logger.e("UpgradeManager check new version arrival error.", e);
  42. }
  43. return null;
  44. }
  45. @override
  46. Future<bool> downloadPackage(
  47. String version,
  48. String url, {
  49. ValueChanged<double>? onProgress,
  50. CancelToken? cancelToken,
  51. }) async {
  52. try {
  53. final savePath = await _getApkFilePath(version);
  54. final dio = Dio();
  55. await dio.download(
  56. url,
  57. savePath,
  58. cancelToken: cancelToken,
  59. onReceiveProgress: (received, total) {
  60. if (total != -1) {
  61. // 计算进度
  62. final double progress = received / total;
  63. onProgress?.call(progress);
  64. logger.i(
  65. "UpgradeManager downloading install-apk: V$version, progress: ${(progress * 100).toStringAsFixed(0)}%.");
  66. }
  67. },
  68. );
  69. return true;
  70. } catch (e) {
  71. logger.e(
  72. "UpgradeManager download package error."
  73. "Version: $version , FileUrl: $url.",
  74. e,
  75. );
  76. return false;
  77. }
  78. }
  79. @override
  80. Future<void> install(String version) async {
  81. try {
  82. final packageInfo = await PackageInfo.fromPlatform();
  83. final appid = packageInfo.packageName;
  84. final apkPath = await _getApkFilePath(version);
  85. final result = await InstallPluginCustom.installApk(apkPath, appid);
  86. logger.i("Upgrade install apk v$version, result: $result.");
  87. } catch (e) {
  88. logger.e("Upgrade install apk v$version error.", e);
  89. }
  90. }
  91. static Future<String> _getApkFilePath(String version) async {
  92. final root = await getExternalStorageDirectory();
  93. return "${root!.path}/$_apkCacheRoot$version.apk";
  94. }
  95. @override
  96. Future<void> deleteFile(String version) async {
  97. try {
  98. final apkPath = await _getApkFilePath(version);
  99. final file = File(apkPath);
  100. file.deleteSync(); // 同步删除文件
  101. } catch (e) {
  102. logger.e("Upgrade deleteFile apk v$version error.", e);
  103. }
  104. }
  105. }