upgrade_service.dart 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import 'dart:io';
  2. import 'package:dio/dio.dart';
  3. import 'package:install_plugin/install_plugin.dart';
  4. import 'package:package_info_plus/package_info_plus.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:permission_handler/permission_handler.dart';
  7. import '../main.dart';
  8. import '../network/my_api.dart';
  9. import '../router/my_navigator.dart';
  10. import '../widget/upgrade_dialog.dart';
  11. class UpgradeService {
  12. UpgradeService._();
  13. static Future<Response> download(String apkUrl, String apkName, ProgressCallback progress) async {
  14. String savePath = '${await getApkDirectoryPath()}/$apkName';
  15. var rsp = await Dio().download(
  16. apkUrl,
  17. savePath,
  18. onReceiveProgress: (count, total) {
  19. if (total < 0) return;
  20. progress.call(count, total);
  21. },
  22. );
  23. return rsp;
  24. }
  25. /// 获取apk文件夹路径
  26. static Future<String> getApkDirectoryPath() async {
  27. return '${(await getApplicationSupportDirectory()).path}/apk';
  28. }
  29. /// 获取apk文件
  30. static Future<File> getApkFile(String apkName) async {
  31. return File('${await getApkDirectoryPath()}/$apkName');
  32. }
  33. /// 安装APK
  34. static Future<bool> installApk(String path) async {
  35. var status = await Permission.requestInstallPackages.status;
  36. if (!status.isGranted) {
  37. Permission.requestInstallPackages.request();
  38. return false;
  39. }
  40. await InstallPlugin.install(path);
  41. return true;
  42. }
  43. /// 从路径中获取文件名
  44. static String getName(String path) {
  45. var pathSplit = path.split('/');
  46. return pathSplit[pathSplit.length - 1];
  47. }
  48. static bool checking = false;
  49. /// 检查版本更新
  50. static Future<void> checkUpgrade(bool showLoading) async {
  51. if (!Platform.isAndroid) return;
  52. if (checking) return;
  53. checking = true;
  54. if (showLoading) MyNavigator.showLoading(msg: '正在获取版本更新');
  55. try {
  56. // 获取apk下载地址
  57. var path = (await MyApi.get().getAppDownloadUrl()).data;
  58. if (showLoading) MyNavigator.dismissLoading();
  59. if (path != null) {
  60. // 获取线上版本 lszlgl_release_0.0.2_2.apk
  61. List<String> nameSplit = UpgradeService.getName(path).split('_');
  62. int onlineCode = int.parse(nameSplit[nameSplit.length - 1].split('.').first);
  63. int localCode = int.parse((await PackageInfo.fromPlatform()).buildNumber);
  64. logger.d('versionUpgrade:localCode:$localCode onlineCode:$onlineCode');
  65. if (localCode >= onlineCode) {
  66. if (showLoading) MyNavigator.showToast('已是最新版本');
  67. } else {
  68. // 版本更新
  69. UpgradeDialog.showDialog(path);
  70. }
  71. }
  72. } catch (e) {
  73. logger.e(e);
  74. if (showLoading) MyNavigator.dismissLoading();
  75. }
  76. checking = false;
  77. }
  78. }