aj_flutter_appsp.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:flutter/services.dart';
  4. import 'aj_flutter_appsp_lib.dart';
  5. /// Copyright © 2018 anji-plus
  6. /// 安吉加加信息技术有限公司
  7. /// http:///www.anji-plus.com
  8. /// All rights reserved.
  9. /// AppSp功能
  10. ///
  11. typedef OnApkDownloadProgressCallback = void Function(dynamic result);
  12. class AjFlutterAppSp {
  13. OnApkDownloadProgressCallback _apkDownloadProgressCallback;
  14. static const MethodChannel _channel = const MethodChannel('aj_flutter_appsp');
  15. AjFlutterAppSp() {
  16. print("AjFlutterAppspPush constructor");
  17. _channel.setMethodCallHandler(_handlerMethodCallback);
  18. }
  19. ///下载进度回调
  20. void getApkDownloadProgressCallback(OnApkDownloadProgressCallback callback) {
  21. if (callback == null) {
  22. return;
  23. }
  24. this._apkDownloadProgressCallback = callback;
  25. }
  26. /// native - flutter
  27. Future<dynamic> _handlerMethodCallback(MethodCall call) async {
  28. switch (call.method) {
  29. case "updateProgress":
  30. dynamic result = call.arguments;
  31. if (_apkDownloadProgressCallback != null) {
  32. _apkDownloadProgressCallback(result);
  33. }
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. ///设置基础地址,如果在开发测试场景会用到,生产时候记得改成生产地址,或者最好不要对暴露set方法
  40. ///[appKey] 应用唯一标识
  41. ///[host] 设置请求基础地址
  42. ///[debug] 是否打开日志开关,true为打开
  43. static Future<String> init(
  44. {String appKey = '', String host = '', bool debug = true}) async {
  45. final String result = await _channel
  46. .invokeMethod('init', {"appKey": appKey, "host": host, "debug": debug});
  47. return result;
  48. }
  49. ///获取版本信息
  50. static Future<SpRespUpdateModel> getUpdateModel() async {
  51. final String jsonStr = await _channel.invokeMethod('getUpdateModel');
  52. SpRespUpdateModel updateModel =
  53. SpRespUpdateModel.fromJson(json.decode(jsonStr));
  54. return updateModel;
  55. }
  56. ///获取公告信息
  57. static Future<SpRespNoticeModel> getNoticeModel() async {
  58. final String jsonStr = await _channel.invokeMethod('getNoticeModel');
  59. SpRespNoticeModel noticeModel =
  60. SpRespNoticeModel.fromJson(json.decode(jsonStr));
  61. return noticeModel;
  62. }
  63. ///下载apk并且安装
  64. ///返回 ”UrlFormatError“ =》url格式错误
  65. ///返回 ”notSamePackage“ =》不是统一包名
  66. ///返回 ”downloadApkFailed“ =》apk下载失败
  67. Future<String> downloadApkAndInstall(String url, String path) async {
  68. final String result = await _channel
  69. .invokeMethod('downloadApkAndInstall', {"url": url, "path": path});
  70. return result;
  71. }
  72. ///apk安装
  73. ///返回 ”FilePathFormatError“ =》文件格式错误
  74. Future<String> installApk(String path) async {
  75. final String result =
  76. await _channel.invokeMethod('installApk', {"path": path});
  77. return result;
  78. }
  79. ///apk下载取消
  80. Future<String> cancelDownload() async {
  81. final String result = await _channel.invokeMethod('cancelDownload');
  82. return result;
  83. }
  84. ///是否同一包名
  85. Future<String> checkSamePackage(String path) async {
  86. final String result =
  87. await _channel.invokeMethod('checkSamePackage', {"path": path});
  88. return result;
  89. }
  90. }