From aba11694641a3d15c06ac698752b885ea5352da3 Mon Sep 17 00:00:00 2001 From: netharu methmitha Date: Tue, 19 Apr 2022 10:10:59 +0530 Subject: [PATCH] fixed the issue with android 10 permission --- android/app/src/main/AndroidManifest.xml | 3 +- lib/pages/statuses_page.dart | 52 ++++++++++++++++++++---- pubspec.lock | 42 +++++++++++++++++++ pubspec.yaml | 3 +- 4 files changed, 91 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 8bf2eac..8324834 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -13,7 +13,8 @@ + android:icon="@mipmap/ic_launcher" + android:requestLegacyExternalStorage="true"> get isGranted async { + if (androidVersion == null) { + _androidDeviceInfo = await DeviceInfoPlugin().androidInfo; + androidVersion = int.parse(_androidDeviceInfo.version.release ?? '0'); + } + if (androidVersion! < 11) { + return true; + } else { + return await Permission.manageExternalStorage.isGranted; + } + } + + Future requestIfNotFound() async { + if (await isGranted == false) { + return await requestPermission(); + } else { + return PermissionStatus.granted; + } + } + + Future requestPermission() async { + return await Permission.manageExternalStorage.request(); + } + + Future _init() async { + _androidDeviceInfo = await DeviceInfoPlugin().androidInfo; + androidVersion = int.parse(_androidDeviceInfo.version.release ?? '0'); + requestIfNotFound(); + } +} class StatusesPage extends StatefulWidget { const StatusesPage({Key? key}) : super(key: key); @@ -14,6 +54,7 @@ class _StatusesPageState extends State { bool? isWhatsappInstalled; bool? isPermissionGranted; List _statusFiles = []; + final Permissions _permissions = Permissions(); Future> getFileList() async { Directory dir = Directory( @@ -31,10 +72,7 @@ class _StatusesPageState extends State { } Future _init() async { - isPermissionGranted = await Permission.manageExternalStorage.isGranted; - if (!isPermissionGranted!) { - await Permission.manageExternalStorage.request(); - } + isPermissionGranted = await _permissions.isGranted; if (!await Directory( '/storage/emulated/0/Android/media/com.whatsapp/Whatsapp/Media/.Statuses/') @@ -63,9 +101,9 @@ class _StatusesPageState extends State { } else if (!isPermissionGranted!) { return GestureDetector( onTap: () async { - PermissionStatus permissionStatus = - await Permission.manageExternalStorage.request(); - if (permissionStatus == PermissionStatus.granted) { + PermissionStatus permissionGranted = + await _permissions.requestPermission(); + if (permissionGranted == PermissionStatus.granted) { await _init(); } }, diff --git a/pubspec.lock b/pubspec.lock index ca0fa2e..2753495 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -85,6 +85,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.4" + device_info_plus: + dependency: "direct main" + description: + name: device_info_plus + url: "https://pub.dartlang.org" + source: hosted + version: "3.2.3" + device_info_plus_linux: + dependency: transitive + description: + name: device_info_plus_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + device_info_plus_macos: + dependency: transitive + description: + name: device_info_plus_macos + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.3" + device_info_plus_platform_interface: + dependency: transitive + description: + name: device_info_plus_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.0+1" + device_info_plus_web: + dependency: transitive + description: + name: device_info_plus_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + device_info_plus_windows: + dependency: transitive + description: + name: device_info_plus_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" fake_async: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 05b1e57..abe28c1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 1.0.3+3 +version: 1.0.3+4 environment: sdk: ">=2.16.2 <3.0.0" @@ -46,6 +46,7 @@ dependencies: url_launcher: ^6.0.20 provider: ^6.0.2 shared_preferences: ^2.0.13 + device_info_plus: ^3.2.3 dev_dependencies: flutter_test: