Skip to content

Commit 099b67c

Browse files
committed
[function] 自动更新
Signed-off-by: xiaweizi <1012126908@qq.com>
1 parent 38e8d59 commit 099b67c

File tree

11 files changed

+126
-4
lines changed

11 files changed

+126
-4
lines changed

android/app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,15 @@
5959
android:name="com.amap.api.v2.apikey"
6060
android:value="9c8852cc13bc895241c4d3f55d111401" />
6161

62+
<provider
63+
android:name="sk.fourq.otaupdate.OtaUpdateFileProvider"
64+
android:authorities="${applicationId}.ota_update_provider"
65+
android:exported="false"
66+
android:grantUriPermissions="true">
67+
<meta-data
68+
android:name="android.support.FILE_PROVIDER_PATHS"
69+
android:resource="@xml/filepaths" />
70+
</provider>
71+
6272
</application>
6373
</manifest>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<paths xmlns:android="http://schemas.android.com/apk/res/android">
3+
<external-path name="external_download" path="Download"/>
4+
</paths>

lib/app/res/analytics_constant.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ class AnalyticsConstant {
77
static const String aboutClick = "about_click";
88
static const String pageShow = "page_show";
99
static const String weatherType = "weather_type";
10+
static const String ota = "ota";
1011

1112
}

lib/app/utils/ota_utils.dart

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

lib/net/net_manager.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter_dynamic_weather/net/response_interceptor.dart';
55
const String weatherBaseUrl = "https://api.caiyunapp.com/v2.5/sas9gfwyRX2NVehl/";
66
const String cityBaseUrl = "https://restapi.amap.com/v3/config/district?subdistrict=1&key=ac9de7774d497989173a1b16dc0290ae&keywords=";
77
const String geoBaseUrl = "https://restapi.amap.com/v3/geocode/regeo?key=ac9de7774d497989173a1b16dc0290ae&location=";
8+
const String otaBaseUrl = "http://xiaweizi.online/config/ota/";
89
const int _kReceiveTimeout = 15000;
910
const int _kSendTimeout = 15000;
1011
const int _kConnectTimeout = 15000;

lib/net/weather_api.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/services.dart';
12
import 'package:flutter_dynamic_weather/model/district_model_entity.dart';
23
import 'package:flutter_dynamic_weather/net/net_manager.dart';
34

@@ -39,4 +40,12 @@ class WeatherApi {
3940
}
4041
return null;
4142
}
43+
44+
Future<dynamic> getOTA() async {
45+
var res = await NetManager.getInstance().baseUrl(otaBaseUrl).get("");
46+
if (res != null && res.status) {
47+
return res.data;
48+
}
49+
return null;
50+
}
4251
}

lib/views/bg/weather_rain_snow_bg.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ class _WeatherRainSnowBgState extends State<WeatherRainSnowBg>
104104
);
105105
}
106106
}
107-
double thunderAlpha = 0.0;
107+
double thunderAlpha = 0.2;
108108
bool isShow = false;
109-
bool needShow = false;
109+
bool needShow = true;
110110
int timeCount = 0;
111+
int timeMacCount = Random().nextInt(200);
111112

112113
class RainSnowPainter extends CustomPainter {
113114
var _paint = Paint();
@@ -172,6 +173,7 @@ class RainSnowPainter extends CustomPainter {
172173
isShow = false;
173174
needShow = false;
174175
timeCount = 0;
176+
timeMacCount = 100 + Random().nextInt(200);
175177
}
176178
if (isShow == false) {
177179
thunderAlpha += 0.06;
@@ -180,7 +182,7 @@ class RainSnowPainter extends CustomPainter {
180182
}
181183
}
182184
timeCount += 1;
183-
if (timeCount >= (250 + Random().nextInt(200))) {
185+
if (timeCount >= timeMacCount) {
184186
needShow = true;
185187
}
186188
}

lib/views/pages/about/about_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class _AboutPageState extends State<AboutPage> {
200200
child: Row(
201201
crossAxisAlignment: CrossAxisAlignment.end,
202202
children: [
203-
Text(_appName == null ? "" : _appName,
203+
Text(_appName == null ? "简悦天气" : _appName,
204204
style: TextStyle(
205205
fontWeight: FontWeight.bold,
206206
color: Colors.white,

lib/views/pages/home/home_page.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
55
import 'package:flutter_bloc/flutter_bloc.dart';
66
import 'package:flutter_dynamic_weather/app/res/analytics_constant.dart';
77
import 'package:flutter_dynamic_weather/app/router.dart';
8+
import 'package:flutter_dynamic_weather/app/utils/ota_utils.dart';
89
import 'package:flutter_dynamic_weather/app/utils/print_utils.dart';
910
import 'package:flutter_dynamic_weather/app/utils/shared_preference_util.dart';
1011
import 'package:flutter_dynamic_weather/app/utils/toast.dart';
@@ -42,6 +43,7 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin{
4243
void initState() {
4344
weatherPrint("umeng init event ${Platform.operatingSystem}");
4445
init();
46+
OTAUtils.initOTA();
4547
super.initState();
4648
}
4749

pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ packages:
212212
url: "https://pub.flutter-io.cn"
213213
source: hosted
214214
version: "0.0.4"
215+
ota_update:
216+
dependency: "direct main"
217+
description:
218+
name: ota_update
219+
url: "https://pub.flutter-io.cn"
220+
source: hosted
221+
version: "2.4.1"
215222
package_info:
216223
dependency: "direct main"
217224
description:

0 commit comments

Comments
 (0)