no message
This commit is contained in:
parent
e727cb3d20
commit
309c708847
|
|
@ -11,13 +11,18 @@ import 'dart:io';
|
||||||
|
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
import 'package:install_plugin/install_plugin.dart';
|
import 'package:install_plugin/install_plugin.dart';
|
||||||
import 'package:marking_app/provider/upgrade_provider.dart';
|
import 'package:marking_app/provider/upgrade_provider.dart';
|
||||||
import 'package:marking_app/utils/app_upgrade/model/UpdateAppEvent.dart';
|
import 'package:marking_app/utils/app_upgrade/model/UpdateAppEvent.dart';
|
||||||
|
import 'package:marking_app/utils/index.dart';
|
||||||
import 'package:permission_handler/permission_handler.dart';
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:app_installer/app_installer.dart';
|
import 'package:app_installer/app_installer.dart';
|
||||||
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
|
import 'UpgradePermission.dart';
|
||||||
|
|
||||||
class DownloadApk {
|
class DownloadApk {
|
||||||
/// 下载安卓更新包
|
/// 下载安卓更新包
|
||||||
|
|
@ -58,26 +63,43 @@ class DownloadApk {
|
||||||
debugPrint('make sure the apk file is set');
|
debugPrint('make sure the apk file is set');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Map<Permission, PermissionStatus> statuses = await [Permission.storage].request();
|
|
||||||
if (statuses[Permission.storage]!.isGranted) {
|
|
||||||
// String? result = await InstallPlugin.installApk(_apkFilePath, appId: event.packageName).catchError((error) {
|
|
||||||
// debugPrint('install apk error: $error');
|
|
||||||
// });
|
|
||||||
// debugPrint('install apk $result');
|
|
||||||
// ToastUtils.getFluttertoast(context: context, msg: 'install apk $result');
|
|
||||||
try {
|
try {
|
||||||
// InstallPlugin.install(filePathOrUrlString)com.example.marking_app
|
await showDialog<bool>(
|
||||||
await AppInstaller.installApk(_apkFilePath);
|
context: context,
|
||||||
// var result = await InstallPlugin.installApk(_apkFilePath, appId: event.packageName);
|
builder: (BuildContext context) {
|
||||||
// print('这是是执行安装的程序:' + jsonEncode(result));
|
return AlertDialog(
|
||||||
// if (!(result?['isSuccess'] ?? false)) {}
|
title: const Text("未知应用安装权限提示"),
|
||||||
// Restadget.restartApp(context); // 安装成功 重启APP
|
content: const Text("更新时若出现需要同意安装未知应用权限时,请同意"),
|
||||||
|
actions: [
|
||||||
|
MaterialButton(
|
||||||
|
color: Theme.of(context).primaryColor,
|
||||||
|
child: const Text("我已知晓", style: TextStyle(color: Colors.white)),
|
||||||
|
onPressed: () => Navigator.of(context).pop(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
await AppInstaller.installApk(_apkFilePath); // 安装APK
|
||||||
|
// 如果2秒还没有进入安装引导页面 一律视为更新失败,弹出其他方式更新
|
||||||
|
await setTimeOut(5000, () async {
|
||||||
|
try {
|
||||||
|
// 其他方式下载
|
||||||
|
// await SystemNavigator.pop(); // 退出APP
|
||||||
|
var options = ['应用市场更新APP', '浏览器下载并安装APP'];
|
||||||
|
var uri = Uri.parse('market://details?id=com.example.marking_app'); // 应用市场URI
|
||||||
|
if (!await canLaunchUrl(uri)) options.removeAt(0); // 如果不能打开应用市场 就屏蔽掉 这个安装方式
|
||||||
|
String? option = await UpgradePermission.showCustomModalBottomSheet(context, options);
|
||||||
|
if (option == '应用市场更新APP') await launchUrl(uri);
|
||||||
|
if (option == '浏览器下载并安装APP') await launchUrl(Uri.parse(event.link));
|
||||||
|
} catch (e) {}
|
||||||
|
});
|
||||||
|
|
||||||
print('安装执行完成了..............0.0');
|
print('安装执行完成了..............0.0');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('安装进入报错....');
|
print('安装进入报错....');
|
||||||
print(e);
|
print(e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -177,10 +177,9 @@ class DownloadButton extends ConsumerWidget {
|
||||||
onPressed: () => easyThrottle('DownloadButton_App_Upgrade', () async {
|
onPressed: () => easyThrottle('DownloadButton_App_Upgrade', () async {
|
||||||
if (deviceInfo == "android" && updateAppEvent.equipment == Equipment.android) {
|
if (deviceInfo == "android" && updateAppEvent.equipment == Equipment.android) {
|
||||||
// 权限检查 判断是否有读写内存的权限
|
// 权限检查 判断是否有读写内存的权限
|
||||||
bool flag = await UpgradePermission(updateAppEvent.deviceInfo).checkPermission(context);
|
bool flag = await UpgradePermission(updateAppEvent.deviceInfo).checkPermission(context, updateAppEvent);
|
||||||
if (flag) {
|
if (flag) {
|
||||||
flag = await DownloadApk.installApk(context, updateAppEvent, ref);
|
flag = await DownloadApk.installApk(context, updateAppEvent, ref);
|
||||||
|
|
||||||
if (!flag) {
|
if (!flag) {
|
||||||
print('执行到了重置更新按钮的地方....');
|
print('执行到了重置更新按钮的地方....');
|
||||||
ref.read(upgradeProvider.notifier).clean(); // 更新失败重置 更新按钮
|
ref.read(upgradeProvider.notifier).clean(); // 更新失败重置 更新按钮
|
||||||
|
|
@ -190,8 +189,6 @@ class DownloadButton extends ConsumerWidget {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 进入应用市场进行下载
|
|
||||||
await toLaunch(updateAppEvent);
|
|
||||||
// await FlutterClipboard.copy(updateAppEvent.link);
|
// await FlutterClipboard.copy(updateAppEvent.link);
|
||||||
// setTimeOut(1000, () => ToastUtils.showInfo('下载链接已经复制到设备,可前往浏览器下载安装'));
|
// setTimeOut(1000, () => ToastUtils.showInfo('下载链接已经复制到设备,可前往浏览器下载安装'));
|
||||||
} else if (deviceInfo == "ios" && updateAppEvent.equipment == Equipment.ios) {
|
} else if (deviceInfo == "ios" && updateAppEvent.equipment == Equipment.ios) {
|
||||||
|
|
|
||||||
|
|
@ -6,23 +6,50 @@
|
||||||
* @LastEditors: wangyang 1147192855@qq.com
|
* @LastEditors: wangyang 1147192855@qq.com
|
||||||
* @LastEditTime: 2022-08-01 14:08:57
|
* @LastEditTime: 2022-08-01 14:08:57
|
||||||
*/
|
*/
|
||||||
|
import 'package:app_installer/app_installer.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
||||||
import 'package:marking_app/utils/index.dart';
|
import 'package:marking_app/utils/index.dart';
|
||||||
import 'package:permission_handler/permission_handler.dart';
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
|
import 'model/UpdateAppEvent.dart';
|
||||||
|
|
||||||
class UpgradePermission {
|
class UpgradePermission {
|
||||||
final String _flatform;
|
final String _flatform;
|
||||||
const UpgradePermission(this._flatform);
|
const UpgradePermission(this._flatform);
|
||||||
|
|
||||||
/// 检查是否有权限,用于安卓
|
/// 检查是否有权限,用于安卓
|
||||||
Future<bool> checkPermission(BuildContext context) async {
|
/// noExecutions 执行次数
|
||||||
|
Future<bool> checkPermission(BuildContext context, UpdateAppEvent updateAppEvent, [int? noExecutions]) async {
|
||||||
|
noExecutions ??= 1;
|
||||||
if (_flatform != 'android') return true; // 非安卓
|
if (_flatform != 'android') return true; // 非安卓
|
||||||
var status = await Permission.storage.request();
|
var status = await Permission.storage.request();
|
||||||
if (status.isGranted) return true;
|
if (status.isGranted) return true;
|
||||||
|
|
||||||
if (status.isDenied) {
|
if (status.isDenied) {
|
||||||
// 普通拒绝 可以再进行提示
|
// 普通拒绝 可以再进行提示
|
||||||
|
|
||||||
|
if (noExecutions == 3) {
|
||||||
|
// 拒绝并不再提示 或者某些机型不会提示
|
||||||
|
await showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
barrierDismissible: false,
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: const Text("提示"),
|
||||||
|
content: const Text("若“拒绝并不再提示”或“没有弹出权限询问对话框”,再次点击同意按钮出现其他更新方式。"),
|
||||||
|
actions: [
|
||||||
|
MaterialButton(
|
||||||
|
color: Theme.of(context).primaryColor,
|
||||||
|
child: const Text("确定", style: TextStyle(color: Colors.white)),
|
||||||
|
onPressed: () => Navigator.of(context).pop(),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
await showDialog<bool>(
|
await showDialog<bool>(
|
||||||
context: context,
|
context: context,
|
||||||
barrierDismissible: false,
|
barrierDismissible: false,
|
||||||
|
|
@ -41,17 +68,18 @@ class UpgradePermission {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isPad()) {
|
// if (isPad()) {
|
||||||
// 平板无法发起权限询问 直接进入APP权限设置页面
|
// // 平板无法发起权限询问 直接进入APP权限设置页面
|
||||||
await openAppSettings();
|
// await openAppSettings();
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
return checkPermission(context);
|
if (noExecutions <= 2) return checkPermission(context, updateAppEvent, ++noExecutions);
|
||||||
|
// 执行次数大于3次,就不再询问直接打开设置权限页面(防止某些机型不会弹起权限询问交互弹框)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 拒绝并不再提示
|
// 拒绝并不再提示
|
||||||
await showDialog(
|
bool? res = await showDialog<bool>(
|
||||||
context: context,
|
context: context,
|
||||||
barrierDismissible: false,
|
barrierDismissible: false,
|
||||||
builder: (BuildContext context) {
|
builder: (BuildContext context) {
|
||||||
|
|
@ -61,20 +89,82 @@ class UpgradePermission {
|
||||||
actions: [
|
actions: [
|
||||||
MaterialButton(
|
MaterialButton(
|
||||||
color: Colors.green.shade900,
|
color: Colors.green.shade900,
|
||||||
child: const Text("退出APP", style: TextStyle(color: Colors.white)),
|
child: const Text("其它方式更新", style: TextStyle(color: Colors.white)),
|
||||||
onPressed: () => Navigator.of(context).pop(false),
|
onPressed: () => Navigator.of(context).pop(false),
|
||||||
),
|
),
|
||||||
MaterialButton(
|
MaterialButton(
|
||||||
color: Colors.green.shade900,
|
color: Theme.of(context).primaryColor,
|
||||||
child: const Text("确定", style: TextStyle(color: Colors.white)),
|
child: const Text("前往设置", style: TextStyle(color: Colors.white)),
|
||||||
onPressed: () => Navigator.of(context).pop(true),
|
onPressed: () => Navigator.of(context).pop(true),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
if (res == null || !res) {
|
||||||
|
// 其他方式下载
|
||||||
|
// await SystemNavigator.pop(); // 退出APP
|
||||||
|
var options = ['应用市场更新APP', '浏览器下载并安装APP'];
|
||||||
|
var uri = Uri.parse('market://details?id=com.example.marking_app'); // 应用市场URI
|
||||||
|
// if (!await canLaunchUrl(uri)) options.removeAt(0); // 如果不能打开应用市场 就屏蔽掉 这个安装方式
|
||||||
|
String? option = await showCustomModalBottomSheet(context, options);
|
||||||
|
if (option == '应用市场更新APP') {
|
||||||
|
if (await canLaunchUrl(uri))
|
||||||
|
await launchUrl(uri);
|
||||||
|
else
|
||||||
|
await AppInstaller.goStore('com.example.marking_app', 'iOSAppId');
|
||||||
|
}
|
||||||
|
if (option == '浏览器下载并安装APP') await launchUrl(Uri.parse(updateAppEvent.link));
|
||||||
|
} else
|
||||||
await openAppSettings();
|
await openAppSettings();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 其他方式下载选择
|
||||||
|
static Future<String?> showCustomModalBottomSheet(context, List<String> options) async {
|
||||||
|
return showModalBottomSheet<String>(
|
||||||
|
backgroundColor: Colors.transparent,
|
||||||
|
isScrollControlled: true,
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
return Container(
|
||||||
|
clipBehavior: Clip.antiAlias,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.white,
|
||||||
|
borderRadius: BorderRadius.only(
|
||||||
|
topLeft: const Radius.circular(20.0),
|
||||||
|
topRight: const Radius.circular(20.0),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
height: MediaQuery.of(context).size.height / 4.0,
|
||||||
|
child: Column(children: [
|
||||||
|
SizedBox(
|
||||||
|
height: 50,
|
||||||
|
child: Stack(
|
||||||
|
textDirection: TextDirection.rtl,
|
||||||
|
children: [
|
||||||
|
Center(child: Text('选择其它方式更新APP', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0))),
|
||||||
|
IconButton(icon: Icon(Icons.close), onPressed: () => Navigator.of(context).pop()),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Divider(height: 1.0),
|
||||||
|
Expanded(
|
||||||
|
child: ListView.builder(
|
||||||
|
itemCount: options.length,
|
||||||
|
itemBuilder: (BuildContext context, int index) {
|
||||||
|
var name = options[index];
|
||||||
|
return ListTile(
|
||||||
|
title: Text(name),
|
||||||
|
trailing: Icon(name.contains('浏览器') ? Icons.browser_updated_outlined : Icons.local_grocery_store_outlined),
|
||||||
|
onTap: () => Navigator.of(context).pop(name),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue