|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_dynamic_weather/app/res/analytics_constant.dart'; |
| 5 | +import 'package:flutter_dynamic_weather/app/utils/toast.dart'; |
| 6 | +import 'package:flutter_dynamic_weather/net/weather_api.dart'; |
| 7 | +import 'package:flutter_dynamic_weather/views/app/flutter_app.dart'; |
| 8 | +import 'package:ota_update/ota_update.dart'; |
| 9 | +import 'package:package_info/package_info.dart'; |
| 10 | +import 'package:umeng_analytics_plugin/umeng_analytics_plugin.dart'; |
| 11 | + |
| 12 | +class OTAUtils { |
| 13 | + static startOTA(String url) async { |
| 14 | + try { |
| 15 | + OtaUpdate() |
| 16 | + .execute( |
| 17 | + url, |
| 18 | + destinationFilename: 'SimplicityWeather.apk', |
| 19 | + ) |
| 20 | + .listen( |
| 21 | + (OtaEvent event) { |
| 22 | + print('status: ${event.status}, value: ${event.value}'); |
| 23 | + if (event.status == OtaStatus.DOWNLOAD_ERROR) { |
| 24 | + ToastUtils.show("下载失败", globalKey.currentContext); |
| 25 | + UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "download_error"); |
| 26 | + } else if (event.status == OtaStatus.INTERNAL_ERROR) { |
| 27 | + ToastUtils.show("未知失败", globalKey.currentContext); |
| 28 | + UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "internal_error"); |
| 29 | + } else if (event.status == OtaStatus.PERMISSION_NOT_GRANTED_ERROR) { |
| 30 | + UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "permission_not_granted_error"); |
| 31 | + ToastUtils.show("请打开权限", globalKey.currentContext); |
| 32 | + } else if (event.status == OtaStatus.INSTALLING) { |
| 33 | + UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "installing"); |
| 34 | + ToastUtils.show("正在安装...", globalKey.currentContext); |
| 35 | + } |
| 36 | + }, |
| 37 | + ); |
| 38 | + } catch (e) { |
| 39 | + print('Failed to make OTA update. Details: $e'); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static initOTA() async { |
| 44 | + if (!Platform.isAndroid) { |
| 45 | + return; |
| 46 | + } |
| 47 | + var otaData = await WeatherApi().getOTA(); |
| 48 | + if (otaData != null && otaData["data"] != null) { |
| 49 | + String url = otaData["data"]["url"]; |
| 50 | + int appCode = int.parse(otaData["data"]["appCode"]); |
| 51 | + var packageInfo = await PackageInfo.fromPlatform(); |
| 52 | + var number = int.parse(packageInfo.buildNumber); |
| 53 | + if (appCode >= number) { |
| 54 | + UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "needOTA"); |
| 55 | + showDialog( |
| 56 | + context: globalKey.currentContext, |
| 57 | + barrierDismissible: false, |
| 58 | + builder: (BuildContext context) { |
| 59 | + return AlertDialog( |
| 60 | + title: Text("检测到新版本更新"), |
| 61 | + content: Text("是否更新"), |
| 62 | + actions: [ |
| 63 | + FlatButton( |
| 64 | + child: Text("更新"), |
| 65 | + onPressed: () { |
| 66 | + UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "start"); |
| 67 | + startOTA(url); |
| 68 | + Navigator.of(globalKey.currentContext).pop(); |
| 69 | + ToastUtils.show("正在后台下载中...", context); |
| 70 | + }, |
| 71 | + ), |
| 72 | + FlatButton( |
| 73 | + child: Text("取消"), |
| 74 | + onPressed: () { |
| 75 | + UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "cancel"); |
| 76 | + Navigator.of(globalKey.currentContext).pop(); |
| 77 | + }, |
| 78 | + ), |
| 79 | + ], |
| 80 | + ); |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments