fix: request install-packages permission at runtime before APK install
Android 8+ requires canRequestPackageInstalls() to return true even when REQUEST_INSTALL_PACKAGES is declared in the manifest. Add permission_handler and check/request Permission.requestInstallPackages before downloading; redirects user to Settings if not granted and shows a clear error message. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:open_file/open_file.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
enum UpdateStatus { idle, checking, available, downloading, upToDate, error }
|
||||
|
||||
@@ -102,6 +103,22 @@ class UpdateNotifier extends Notifier<UpdateState> {
|
||||
|
||||
Future<void> downloadAndInstall() async {
|
||||
if (state.downloadUrl == null) return;
|
||||
|
||||
// Android 8+ requires explicit per-app "Install unknown apps" approval
|
||||
// beyond the manifest declaration. Check and redirect to Settings if needed.
|
||||
final installPermission = await Permission.requestInstallPackages.status;
|
||||
if (!installPermission.isGranted) {
|
||||
final result = await Permission.requestInstallPackages.request();
|
||||
if (!result.isGranted) {
|
||||
state = state.copyWith(
|
||||
status: UpdateStatus.error,
|
||||
errorMessage:
|
||||
'Grant "Install unknown apps" permission for Fabled in Settings, then try again.',
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
state = state.copyWith(status: UpdateStatus.downloading, downloadProgress: 0);
|
||||
|
||||
try {
|
||||
|
||||
@@ -22,6 +22,7 @@ dependencies:
|
||||
markdown: ^7.2.2
|
||||
package_info_plus: ^9.0.0
|
||||
open_file: ^3.3.2
|
||||
permission_handler: ^11.3.1
|
||||
flutter_inappwebview: ^6.1.5
|
||||
flutter_markdown_plus: ^1.0.7
|
||||
google_fonts: ^8.0.2
|
||||
|
||||
Reference in New Issue
Block a user