upgrade.dart 4.0 KB

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