Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions flutter_app_upgrade/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,35 @@ rootProject.allprojects {
}
}

def dartEnvironmentVariables = [
// 是否是Google Play版, Google Play禁止App从第三方升级, 此时需要禁用掉插件中相关功能
// 可用通过gradle.properties配置该属性的默认值, 若不配置, 默认为false
flutter_app_upgrade_googlePlay: project.findProperty('flutter_app_upgrade_googlePlayDefaultValue') ?: 'false',
]
if (project.hasProperty('dart-defines')) {
dartEnvironmentVariables += project.property('dart-defines').split(',').collectEntries {
// def pair = new String(Base64.decoder.decode(it)).split('=')
def pair = URLDecoder.decode(it).split('=')
[(pair.first()): pair.last()]
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28
compileSdkVersion 30

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
main {
java.srcDirs += 'src/main/kotlin'
manifest.srcFile Boolean.parseBoolean(dartEnvironmentVariables.flutter_app_upgrade_googlePlay) ? 'src/google/AndroidManifest.xml' : 'src/main/AndroidManifest.xml'
}
}
defaultConfig {
minSdkVersion 16
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField 'boolean', 'googlePlay', dartEnvironmentVariables.flutter_app_upgrade_googlePlay
}
lintOptions {
disable 'InvalidPackage'
Expand Down
4 changes: 4 additions & 0 deletions flutter_app_upgrade/android/src/google/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flutter.flutter_app_upgrade">
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ public class FlutterAppUpgradePlugin : FlutterPlugin, MethodCallHandler, Activit
if (call.method == "getAppInfo") {
getAppInfo(mContext, result)
} else if (call.method == "getApkDownloadPath") {
result.success(mContext.getExternalFilesDir("").absolutePath)
result.success(mContext.getExternalFilesDir("")?.absolutePath)
} else if (call.method == "install") {
//安装app
val path = call.argument<String>("path")
path?.also {
startInstall(mContext, it)
if (BuildConfig.googlePlay) {
result.error("error", "googlePlay=true, The app installation function is disabled.", null)
} else {
// 安装app
val path = call.argument<String>("path")
path?.also {
startInstall(mContext, it)
}
}
} else if (call.method == "getInstallMarket") {
var packageList = getInstallMarket(call.argument<List<String>>("packages"))
Expand Down Expand Up @@ -124,7 +128,7 @@ public class FlutterAppUpgradePlugin : FlutterPlugin, MethodCallHandler, Activit
if (nameEmpty || classEmpty) {
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
} else {
goToMarket.setClassName(marketPackageName, marketClassName)
goToMarket.setClassName(marketPackageName!!, marketClassName!!)
}
context.startActivity(goToMarket)
} catch (e: ActivityNotFoundException) {
Expand Down Expand Up @@ -157,11 +161,7 @@ public class FlutterAppUpgradePlugin : FlutterPlugin, MethodCallHandler, Activit
val intent = Intent().setPackage(packageName)
val infos = manager.queryIntentActivities(intent,
PackageManager.GET_INTENT_FILTERS)
return if (infos == null || infos.size < 1) {
false
} else {
true
}
return infos.size >= 1
}

/**
Expand Down
1 change: 1 addition & 0 deletions flutter_app_upgrade/example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
flutter_app_upgrade_googlePlayDefaultValue=false
3 changes: 0 additions & 3 deletions flutter_app_upgrade/example/android/local.properties

This file was deleted.

28 changes: 25 additions & 3 deletions flutter_app_upgrade/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_app_upgrade/flutter_app_upgrade.dart';

void main() => runApp(MyApp());
Expand Down Expand Up @@ -44,7 +45,7 @@ class Home extends StatefulWidget {
}

class _HomeState extends State<Home> {
AppInfo _appInfo;
AppInfo? _appInfo;
List<AppMarketInfo> _appMarketList = [];
String _installMarkets = '';

Expand Down Expand Up @@ -79,6 +80,27 @@ class _HomeState extends State<Home> {
);
}

///自定义界面
_checkAppUpgrade2() {
AppUpgrade.appUpgrade(context, _checkAppInfo(),
iosAppId: 'id88888888',
downloadProgress: (count, total) {
print('count:$count,total:$total');
},
downloadStatusChange: (DownloadStatus status, {dynamic error}) {
print('status:$status,error:$error');
},
dialogBuilder: (onOk) => MaterialButton(
onPressed: onOk,
padding: EdgeInsets.symmetric(horizontal: 40, vertical: 2),
color: Colors.blueAccent,
child: Text("升级",
style: const TextStyle(color: Colors.white, fontSize: 16)),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
));
}

Future<AppUpgradeInfo> _checkAppInfo() async {
//这里一般访问网络接口,将返回的数据解析成如下格式
return Future.delayed(Duration(seconds: 1), () {
Expand Down Expand Up @@ -121,4 +143,4 @@ class _HomeState extends State<Home> {
],
);
}
}
}
2 changes: 1 addition & 1 deletion flutter_app_upgrade/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Demonstrates how to use the flutter_app_upgrade plugin.
publish_to: 'none'

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
Expand Down
4 changes: 2 additions & 2 deletions flutter_app_upgrade/example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ void main() {
// Verify that platform version is retrieved.
expect(
find.byWidgetPredicate(
(Widget widget) => widget is Text &&
widget.data.startsWith('Running on:'),
(Widget widget) =>
widget is Text && widget.data!.startsWith('Running on:'),
),
findsOneWidget,
);
Expand Down
4 changes: 2 additions & 2 deletions flutter_app_upgrade/lib/src/app_market.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class AppMarket {
return marketList;
}

static AppMarketInfo getBuildInMarket(String packageName) {
AppMarketInfo _info;
static AppMarketInfo? getBuildInMarket(String packageName) {
AppMarketInfo? _info;
buildInMarketList.forEach((f) {
if (f.packageName == packageName) {
_info = f;
Expand Down
151 changes: 83 additions & 68 deletions flutter_app_upgrade/lib/src/app_upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_app_upgrade/flutter_app_upgrade.dart';

import 'custom_app_upgrade.dart';
import 'download_status.dart';
import 'simple_app_upgrade.dart';

Expand Down Expand Up @@ -47,27 +48,28 @@ class AppUpgrade {
///
/// `downloadStatusChange`:下载状态变化回调
///
static appUpgrade(
static void appUpgrade(
BuildContext context,
Future<AppUpgradeInfo> future, {
TextStyle titleStyle,
TextStyle contentStyle,
String cancelText,
TextStyle cancelTextStyle,
String okText,
TextStyle okTextStyle,
List<Color> okBackgroundColors,
Color progressBarColor,
Future<AppUpgradeInfo?> future, {
TextStyle? titleStyle,
TextStyle? contentStyle,
String? cancelText,
TextStyle? cancelTextStyle,
String? okText,
TextStyle? okTextStyle,
List<Color>? okBackgroundColors,
Color? progressBarColor,
double borderRadius = 20.0,
String iosAppId,
AppMarketInfo appMarketInfo,
VoidCallback onCancel,
VoidCallback onOk,
DownloadProgressCallback downloadProgress,
DownloadStatusChangeCallback downloadStatusChange,
required String iosAppId,
AppMarketInfo? appMarketInfo,
VoidCallback? onCancel,
VoidCallback? onOk,
DownloadProgressCallback? downloadProgress,
DownloadStatusChangeCallback? downloadStatusChange,
Widget Function(VoidCallback onOk)? dialogBuilder,
}) {
future.then((AppUpgradeInfo appUpgradeInfo) {
if (appUpgradeInfo != null && appUpgradeInfo.title != null) {
future.then((AppUpgradeInfo? appUpgradeInfo) {
if (appUpgradeInfo != null) {
_showUpgradeDialog(
context, appUpgradeInfo.title, appUpgradeInfo.contents,
apkDownloadUrl: appUpgradeInfo.apkDownloadUrl,
Expand All @@ -86,7 +88,9 @@ class AppUpgrade {
onCancel: onCancel,
onOk: onOk,
downloadProgress: downloadProgress,
downloadStatusChange: downloadStatusChange);
downloadStatusChange: downloadStatusChange,
//自定义升级界面
dialogBuilder: dialogBuilder);
}
}).catchError((onError) {
print('$onError');
Expand All @@ -96,27 +100,28 @@ class AppUpgrade {
///
/// 展示app升级提示框
///
static _showUpgradeDialog(
static void _showUpgradeDialog(
BuildContext context,
String title,
List<String> contents, {
String apkDownloadUrl,
String? apkDownloadUrl,
bool force = false,
TextStyle titleStyle,
TextStyle contentStyle,
String cancelText,
TextStyle cancelTextStyle,
String okText,
TextStyle okTextStyle,
List<Color> okBackgroundColors,
Color progressBarColor,
TextStyle? titleStyle,
TextStyle? contentStyle,
String? cancelText,
TextStyle? cancelTextStyle,
String? okText,
TextStyle? okTextStyle,
List<Color>? okBackgroundColors,
Color? progressBarColor,
double borderRadius = 20.0,
String iosAppId,
AppMarketInfo appMarketInfo,
VoidCallback onCancel,
VoidCallback onOk,
DownloadProgressCallback downloadProgress,
DownloadStatusChangeCallback downloadStatusChange,
required String iosAppId,
AppMarketInfo? appMarketInfo,
VoidCallback? onCancel,
VoidCallback? onOk,
DownloadProgressCallback? downloadProgress,
DownloadStatusChangeCallback? downloadStatusChange,
Widget Function(VoidCallback onOk)? dialogBuilder,
}) {
showDialog(
context: context,
Expand All @@ -126,42 +131,52 @@ class AppUpgrade {
onWillPop: () async {
return false;
},
child: Dialog(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(borderRadius))),
child: SimpleAppUpgradeWidget(
title: title,
titleStyle: titleStyle,
contents: contents,
contentStyle: contentStyle,
cancelText: cancelText,
cancelTextStyle: cancelTextStyle,
okText: okText,
okTextStyle: okTextStyle,
okBackgroundColors: okBackgroundColors ??
[
Theme.of(context).primaryColor,
Theme.of(context).primaryColor
],
progressBarColor: progressBarColor,
borderRadius: borderRadius,
downloadUrl: apkDownloadUrl,
force: force,
iosAppId: iosAppId,
appMarketInfo: appMarketInfo,
onCancel: onCancel,
onOk: onOk,
downloadProgress: downloadProgress,
downloadStatusChange: downloadStatusChange
)),
child: dialogBuilder != null
? Dialog(
insetPadding:
EdgeInsets.symmetric(horizontal: 0.0, vertical: 0.0),
child: CustomAppUpgradeWidget(
iosAppId: iosAppId, dialogBuilder: dialogBuilder),
backgroundColor: Colors.transparent,
)
: Dialog(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(borderRadius))),
child: SimpleAppUpgradeWidget(
title: title,
titleStyle: titleStyle,
contents: contents,
contentStyle: contentStyle,
cancelText: cancelText,
cancelTextStyle: cancelTextStyle,
okText: okText,
okTextStyle: okTextStyle,
okBackgroundColors: okBackgroundColors ??
[
Theme.of(context).primaryColor,
Theme.of(context).primaryColor
],
progressBarColor: progressBarColor,
borderRadius: borderRadius,
downloadUrl: apkDownloadUrl,
force: force,
iosAppId: iosAppId,
appMarketInfo: appMarketInfo,
onCancel: onCancel,
onOk: onOk,
downloadProgress: downloadProgress,
downloadStatusChange: downloadStatusChange)),
);
});
}
}

class AppInfo {
AppInfo({this.versionName, this.versionCode, this.packageName});
AppInfo(
{required this.versionName,
required this.versionCode,
required this.packageName});

String versionName;
String versionCode;
Expand All @@ -170,8 +185,8 @@ class AppInfo {

class AppUpgradeInfo {
AppUpgradeInfo(
{@required this.title,
@required this.contents,
{required this.title,
required this.contents,
this.apkDownloadUrl,
this.force = false});

Expand All @@ -188,7 +203,7 @@ class AppUpgradeInfo {
///
/// apk下载url
///
final String apkDownloadUrl;
final String? apkDownloadUrl;

///
/// 是否强制升级
Expand Down
Loading