upgrade.dart 3.5 KB

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