import 'dart:io';
import 'dart:isolate';
import 'dart:ui';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:path_provider/path_provider.dart';
import 'package:install_plugin/install_plugin.dart';
import 'package:android_path_provider/android_path_provider.dart';
class DownloadProvide with ChangeNotifier{
ReceivePort _port = ReceivePort();
bool _permissisonReady;
String _localPath;
String apkDownLoadUrl;
String appName;
initDownload(){
IsolateNameServer.removePortNameMapping("downloader_send_port");
IsolateNameServer.registerPortWithName(_port.sendPort, "downloader_send_port");
_port.listen((message) {
String id = message[0];
DownloadTaskStatus status = message[1];
int progress = message[2];
debugPrint("id====="+id+"=======progress$progress");
if(status == DownloadTaskStatus.complete){
debugPrint("下载完成,自动安装");
clickInstallApk();
}
});
FlutterDownloader.registerCallback(downloadCallback);
}
static void downloadCallback(String id, DownloadTaskStatus status, int progress){
final SendPort send = IsolateNameServer.lookupPortByName("downloader_send_port");
send.send([id,status,progress]);
}
void clickInstallApk() async {
var _apkFilePath = _localPath+"/$appName";
if (_apkFilePath.isEmpty) {
print('make sure the apk file is set');
return;
}
InstallPlugin.installApk(_apkFilePath, '包名').then((result) {
print('install apk $result');
}).catchError((error) {
print('install apk error: $error');
});
}
downLoadApk() async{
_permissisonReady = false;
await _prepare();
if (_permissisonReady) {
await _prepareDeleteFile();
_requestDownload();
} else {
_checkPermission().then((hasGranted) {
_permissisonReady = hasGranted;
});
}
}
Future<String> get _apkLocalPath async {
var externalStorageDirPath;
if (Platform.isAndroid) {
try {
externalStorageDirPath = await AndroidPathProvider.downloadsPath;
} catch (e) {
final directory = await getExternalStorageDirectory();
externalStorageDirPath = directory?.path;
}
} else if (Platform.isIOS) {
externalStorageDirPath =
(await getApplicationDocumentsDirectory()).absolute.path;
}
return externalStorageDirPath;
}
void _requestDownload() async {
await FlutterDownloader.enqueue(
url: apkDownLoadUrl,
fileName: "$appName",
savedDir: _localPath,
showNotification: true,
openFileFromNotification: false,
saveInPublicStorage: true,
);
}
Future<Null> _prepare() async {
_permissisonReady = await _checkPermission();
}
Future<bool> _checkPermission() async {
if (Platform.isAndroid) {
final status = await Permission.storage.status;
if (status != PermissionStatus.granted) {
final result = await Permission.storage.request();
if (result == PermissionStatus.granted) {
return true;
}
} else {
return true;
}
} else {
return true;
}
return false;
}
Future<void> _prepareDeleteFile() async {
_localPath = await _apkLocalPath;
File file = File(_localPath+"/"+appName);
bool exist = file.existsSync();
if(exist){
file.deleteSync(recursive: true);
}
}
}
|