From f0837fd4261a50c82197e3b08cb7e30399ec3395 Mon Sep 17 00:00:00 2001 From: tangrui <> Date: Mon, 5 Jun 2023 23:18:44 +0800 Subject: [PATCH] feat:Improve apk installation logic && fix some bugs. --- android/app/build.gradle | 5 + lib/l10n/intl_en_us.dart | 8 ++ lib/l10n/intl_keys.dart | 7 ++ lib/l10n/intl_zh_cn.dart | 7 ++ lib/screen/file_list/file_list_screen.dart | 26 +--- lib/screen/file_reader_screen.dart | 136 +++++++++++++++------ lib/screen/login_screen.dart | 6 +- lib/util/download_utils.dart | 1 + 8 files changed, 133 insertions(+), 63 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 22bf1de..3e57ccd 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -52,6 +52,11 @@ android { versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true + + ndk { + //选择要添加的对应cpu类型的.so库。还可以添加 'armeabi-v7a', 'x86_64', 'mips', 'mips64' ,'arm64-v8a' + abiFilters 'armeabi-v7a', 'arm64-v8a' + } } signingConfigs { diff --git a/lib/l10n/intl_en_us.dart b/lib/l10n/intl_en_us.dart index 2ae1f7c..542e6f1 100644 --- a/lib/l10n/intl_en_us.dart +++ b/lib/l10n/intl_en_us.dart @@ -62,4 +62,12 @@ const translationsEnUS = { "fileList_menu_fileName": "Name", "fileList_menu_fileType": "Type", "fileList_menu_modifyTime": "Time", + "installPermissionDialog_title": "Install permission", + "installPermissionDialog_content": + "Please grant the permission to install the apk file, otherwise the installation will fail", + "installPermissionDialog_btn_ok": "OK", + "installPermissionDialog_btn_cancel": "Cancel", + "installPermissionDialog_denied": "Permission denied", + "fileReaderScreen_openAgain": "Open again", + "fileReaderScreen_install": "Install", }; diff --git a/lib/l10n/intl_keys.dart b/lib/l10n/intl_keys.dart index 23da964..1bbf452 100644 --- a/lib/l10n/intl_keys.dart +++ b/lib/l10n/intl_keys.dart @@ -59,4 +59,11 @@ class Intl { static const String fileList_menu_fileName = "fileList_menu_fileName"; static const String fileList_menu_fileType = "fileList_menu_fileType"; static const String fileList_menu_modifyTime = "fileList_menu_modifyTime"; + static const String installPermissionDialog_title = "installPermissionDialog_title"; + static const String installPermissionDialog_content = "installPermissionDialog_content"; + static const String installPermissionDialog_btn_ok = "installPermissionDialog_btn_ok"; + static const String installPermissionDialog_btn_cancel = "installPermissionDialog_btn_cancel"; + static const String installPermissionDialog_denied = "installPermissionDialog_denied"; + static const String fileReaderScreen_openAgain = "fileReaderScreen_openAgain"; + static const String fileReaderScreen_install = "fileReaderScreen_install"; } \ No newline at end of file diff --git a/lib/l10n/intl_zh_cn.dart b/lib/l10n/intl_zh_cn.dart index 86b8dea..91a22c4 100644 --- a/lib/l10n/intl_zh_cn.dart +++ b/lib/l10n/intl_zh_cn.dart @@ -60,4 +60,11 @@ const translationsZhCN = { "fileList_menu_fileName": "文件名称", "fileList_menu_fileType": "文件类型", "fileList_menu_modifyTime": "修改时间", + "installPermissionDialog_title": "安装权限", + "installPermissionDialog_content": "请授予安装权限,否则无法安装应用", + "installPermissionDialog_btn_ok": "去授权", + "installPermissionDialog_btn_cancel": "取消", + "installPermissionDialog_denied": "权限被拒绝", + "fileReaderScreen_openAgain": "再次打开", + "fileReaderScreen_install": "安装", }; diff --git a/lib/screen/file_list/file_list_screen.dart b/lib/screen/file_list/file_list_screen.dart index 684c9da..bc2f241 100644 --- a/lib/screen/file_list/file_list_screen.dart +++ b/lib/screen/file_list/file_list_screen.dart @@ -246,26 +246,9 @@ class _FileListScreenState extends State void _onFileTap( BuildContext context, List files, - FileItemVO file, - ) async { + FileItemVO file, + ) { FileType fileType = file.type; - if (fileType == FileType.apk && - Platform.isAndroid && - !await Permission.requestInstallPackages.isGranted) { - var permissionResult = Permission.requestInstallPackages.request(); - if (await permissionResult.isGranted) { - if (mounted) { - _onFileTap(context, files, file); - } - } else { - SmartDialog.showToast("No permission to install apk"); - } - return; - } - - if (!mounted) { - return; - } switch (fileType) { case FileType.folder: @@ -314,10 +297,7 @@ class _FileListScreenState extends State case FileType.markdown: Get.toNamed( NamedRouter.markdownReader, - arguments: { - "markdownPath": file.path, - "title": file.name - }, + arguments: {"markdownPath": file.path, "title": file.name}, ); break; case FileType.txt: diff --git a/lib/screen/file_reader_screen.dart b/lib/screen/file_reader_screen.dart index 17da227..6a0b7b7 100644 --- a/lib/screen/file_reader_screen.dart +++ b/lib/screen/file_reader_screen.dart @@ -1,11 +1,16 @@ +import 'dart:io'; + +import 'package:alist/l10n/intl_keys.dart'; import 'package:alist/util/download_utils.dart'; import 'package:alist/util/file_type.dart'; import 'package:alist/widget/alist_scaffold.dart'; import 'package:dio/dio.dart'; +import 'package:flustars/flustars.dart'; import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:open_file/open_file.dart'; +import 'package:permission_handler/permission_handler.dart'; class FileReaderScreen extends StatelessWidget { FileReaderScreen({Key? key}) : super(key: key); @@ -81,16 +86,24 @@ class _FileReaderContainerState extends State<_FileReaderContainer> { return Text(failedMessage ?? ""); } else if (_downloadProgress < 100) { return Text("$_downloadProgress%"); - } else if (!_isOpenSuccessfully && failedMessage == null) { + } else if (!_isOpenSuccessfully && + failedMessage == null && + widget.fileType != FileType.apk) { return Text("$_downloadProgress%"); - } else if (_isOpenSuccessfully) { + } else if (_isOpenSuccessfully || widget.fileType == FileType.apk) { + String text; + if (widget.fileType == FileType.apk) { + text = Intl.fileReaderScreen_install.tr; + } else { + text = Intl.fileReaderScreen_openAgain.tr; + } return FilledButton( onPressed: () { if (null != _localPath) { _openFile(_localPath); } }, - child: const Text("Open again")); + child: Text(text)); } else { return Text(failedMessage ?? ""); } @@ -106,51 +119,96 @@ class _FileReaderContainerState extends State<_FileReaderContainer> { DownloadUtils.downloadByPath(remotePath, cancelToken: _cancelToken, onReceiveProgress: (count, total) { _downloadProgress = (count.toDouble() / total * 100).toInt(); + setState(() {}); }, onSuccess: (fileName, localPath) { - setState(() { - this.fileName = fileName; - }); - _openFile(localPath); - }, onFailed: (code, message) { - SmartDialog.showToast(message); - debugPrint("code:$code,message:$message"); - }); - } - - _openFile(String? filePath) { - String? openFileType; - switch (widget.fileType) { - case FileType.txt: - case FileType.code: - openFileType = "text/plain"; - break; - case FileType.pdf: - openFileType = "application/pdf"; - break; - case FileType.apk: - openFileType = "application/vnd.android.package-archive"; - break; - default: - openFileType = null; - break; - } - - OpenFile.open(filePath, type: openFileType).then((value) { - if (value.type == ResultType.done) { + LogUtil.d("onSuccess fileName=$fileName,localPath=$localPath"); + if (widget.fileType == FileType.apk && Platform.isAndroid) { setState(() { - _isOpenSuccessfully = true; + this.fileName = fileName; + _downloadProgress = 100; + _localPath = localPath; }); } else { setState(() { - _isOpenSuccessfully = false; - failedMessage = value.message; + this.fileName = fileName; }); + _openFile(localPath); } + }, onFailed: (code, message) { + SmartDialog.showToast(message); + debugPrint("code:$code,message:$message"); }); + } + + _openFile(String? filePath) async { + if (widget.fileType == FileType.apk && + Platform.isAndroid && + !await Permission.requestInstallPackages.isGranted) { + _showInstallPermissionDialog(); + } else { + String? openFileType; + switch (widget.fileType) { + case FileType.txt: + case FileType.code: + openFileType = "text/plain"; + break; + case FileType.pdf: + openFileType = "application/pdf"; + break; + case FileType.apk: + openFileType = "application/vnd.android.package-archive"; + break; + default: + openFileType = null; + break; + } + + OpenFile.open(filePath, type: openFileType).then((value) { + if (value.type == ResultType.done) { + setState(() { + _isOpenSuccessfully = true; + }); + } else { + setState(() { + _isOpenSuccessfully = false; + failedMessage = value.message; + }); + } + }); + setState(() { + _downloadProgress = 100; + _localPath = filePath; + }); + } + } - setState(() { - _downloadProgress = 100; - _localPath = filePath; + // just for android. + void _showInstallPermissionDialog() { + SmartDialog.show(builder: (context) { + return AlertDialog( + title: Text(Intl.installPermissionDialog_title.tr), + content: Text(Intl.installPermissionDialog_content.tr), + actions: [ + TextButton( + onPressed: () { + SmartDialog.dismiss(); + }, + child: Text(Intl.installPermissionDialog_btn_cancel.tr)), + TextButton( + onPressed: () { + SmartDialog.dismiss(); + Permission.requestInstallPackages.request().then((value) { + if (value.isGranted) { + _openFile(_localPath); + } else { + SmartDialog.showToast( + Intl.installPermissionDialog_denied.tr); + } + }); + }, + child: Text(Intl.installPermissionDialog_btn_ok.tr)), + ], + ); }); } } diff --git a/lib/screen/login_screen.dart b/lib/screen/login_screen.dart index f0a816f..019fc44 100644 --- a/lib/screen/login_screen.dart +++ b/lib/screen/login_screen.dart @@ -368,6 +368,7 @@ class LoginScreenController extends GetxController with WidgetsBindingObserver { _enterVisitorMode(baseUrl, useDemoServer: useDemoServer); return; } + SmartDialog.showToast(message); SmartDialog.dismiss(); }); } @@ -405,8 +406,11 @@ class LoginScreenController extends GetxController with WidgetsBindingObserver { ), TextButton( onPressed: () { - _enterVisitorMode(Global.demoServerBaseUrl, useDemoServer: true); SmartDialog.dismiss(); + Future.delayed(Duration.zero).then( + (value) => _enterVisitorMode(Global.demoServerBaseUrl, + useDemoServer: true), + ); }, child: Text(Intl.guestModeDialog_btn_ok.tr), ), diff --git a/lib/util/download_utils.dart b/lib/util/download_utils.dart index 12c6f38..dbd8819 100644 --- a/lib/util/download_utils.dart +++ b/lib/util/download_utils.dart @@ -157,6 +157,7 @@ class DownloadUtils { fileUrl, tmpFilePath, cancelToken: cancelToken, + onReceiveProgress: onReceiveProgress ) .then((value) async { if (await downloadFile.exists()) {