Skip to content

Commit

Permalink
feat:Improve apk installation logic && fix some bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tangrui committed Jun 5, 2023
1 parent 3a09df3 commit f0837fd
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 63 deletions.
5 changes: 5 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions lib/l10n/intl_en_us.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
7 changes: 7 additions & 0 deletions lib/l10n/intl_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
7 changes: 7 additions & 0 deletions lib/l10n/intl_zh_cn.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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": "安装",
};
26 changes: 3 additions & 23 deletions lib/screen/file_list/file_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,26 +246,9 @@ class _FileListScreenState extends State<FileListScreen>
void _onFileTap(
BuildContext context,
List<FileItemVO> 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:
Expand Down Expand Up @@ -314,10 +297,7 @@ class _FileListScreenState extends State<FileListScreen>
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:
Expand Down
136 changes: 97 additions & 39 deletions lib/screen/file_reader_screen.dart
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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 ?? "");
}
Expand All @@ -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)),
],
);
});
}
}
6 changes: 5 additions & 1 deletion lib/screen/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ class LoginScreenController extends GetxController with WidgetsBindingObserver {
_enterVisitorMode(baseUrl, useDemoServer: useDemoServer);
return;
}
SmartDialog.showToast(message);
SmartDialog.dismiss();
});
}
Expand Down Expand Up @@ -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),
),
Expand Down
1 change: 1 addition & 0 deletions lib/util/download_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class DownloadUtils {
fileUrl,
tmpFilePath,
cancelToken: cancelToken,
onReceiveProgress: onReceiveProgress
)
.then((value) async {
if (await downloadFile.exists()) {
Expand Down

0 comments on commit f0837fd

Please sign in to comment.