refactor: update AndroidManifest and MainActivity for APK installation; remove flutter_app_installer dependency

This commit is contained in:
Moustapha Kodjo Amadou 2025-05-09 18:38:23 +01:00
parent c558c08376
commit d250c08424
7 changed files with 65 additions and 22 deletions

View file

@ -47,15 +47,17 @@
<data android:scheme="mangayomi" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:name="androidx.core.content.FileProvider"
android:authorities="com.kodjodevf.mangayomi.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data

View file

@ -6,6 +6,11 @@ import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.StandardMethodCodec
import io.flutter.embedding.android.FlutterActivity
import androidx.core.content.FileProvider
import android.content.Intent
import android.os.Build
import android.net.Uri
import java.io.File
class MainActivity: FlutterActivity() {
@ -32,5 +37,38 @@ class MainActivity: FlutterActivity() {
}
}
}
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
"com.kodjodevf.mangayomi.apk_install",
StandardMethodCodec.INSTANCE,
flutterEngine.dartExecutor.binaryMessenger.makeBackgroundTaskQueue()
).setMethodCallHandler { call, result ->
when (call.method) {
"installApk" -> {
val filePath = call.argument<String>("filePath")
installApk(filePath)
result.success(null)
}
else -> {
result.notImplemented()
}
}
}
}
private fun installApk(filePath: String?) {
if (filePath == null) return
val file = File(filePath)
val intent = Intent(Intent.ACTION_VIEW)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val apkUri: Uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
FileProvider.getUriForFile(this, "$packageName.fileprovider", file)
} else {
Uri.fromFile(file)
}
intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
startActivity(intent)
}
}

View file

@ -12,7 +12,7 @@ subprojects {
if (project.plugins.hasPlugin("com.android.application") ||
project.plugins.hasPlugin("com.android.library")) {
project.android {
compileSdkVersion 34
compileSdkVersion 35
buildToolsVersion "34.0.0"
}
}

View file

@ -218,9 +218,7 @@ async function parseEpubChapter(bookName, url, headers, chapterTitle) {
await File(p.join(tmpDirectory.path, ".nomedia")).create();
}
}
final file = File(
p.join(tmpDirectory.path, "$bookName.epub"),
);
final file = File(p.join(tmpDirectory.path, "$bookName.epub"));
if (await file.exists()) {
return await file.readAsBytes();
}

View file

@ -1,9 +1,11 @@
import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_installer/flutter_app_installer.dart';
// import 'package:flutter_app_installer/flutter_app_installer.dart';
import 'package:flutter_qjs/quickjs/ffi.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:http/http.dart' as http;
@ -143,8 +145,7 @@ class _DownloadFileScreenState extends ConsumerState<DownloadFileScreen> {
if (!status.isGranted) {
await Permission.requestInstallPackages.request();
}
final FlutterAppInstaller appInstaller = FlutterAppInstaller();
await appInstaller.installApk(filePath: file.path);
await ApkInstaller.installApk(file.path);
}
Future<void> _launchInBrowser(Uri url) async {
@ -153,3 +154,16 @@ class _DownloadFileScreenState extends ConsumerState<DownloadFileScreen> {
}
}
}
class ApkInstaller {
static const _platform = MethodChannel('com.kodjodevf.mangayomi.apk_install');
static Future<void> installApk(String filePath) async {
try {
await _platform.invokeMethod('installApk', {'filePath': filePath});
} catch (e) {
if (kDebugMode) {
log("Erreur d'installation : $e");
}
}
}
}

View file

@ -539,14 +539,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_app_installer:
dependency: "direct main"
description:
name: flutter_app_installer
sha256: "2243cf0e58d6f126420a800fcd06e7c5b3a048c5551914a006b29575125d8dc0"
url: "https://pub.dev"
source: hosted
version: "1.0.1"
flutter_cache_manager:
dependency: transitive
description:

View file

@ -83,7 +83,6 @@ dependencies:
win32: ^5.10.1
protobuf: ^4.0.0
device_info_plus: ^11.3.3
flutter_app_installer: ^1.0.0
marquee: ^2.2.3
epubx:
path: ./epubx