Skip to content

Commit

Permalink
fixed the issue with android 10 permission
Browse files Browse the repository at this point in the history
  • Loading branch information
netharuM committed Apr 19, 2022
1 parent b75de0e commit aba1169
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
3 changes: 2 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<application
android:label="whatskit"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
android:icon="@mipmap/ic_launcher"
android:requestLegacyExternalStorage="true">
<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
52 changes: 45 additions & 7 deletions lib/pages/statuses_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,46 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:whatskit/widgets/status_card.dart';
import 'package:device_info_plus/device_info_plus.dart';

class Permissions {
late AndroidDeviceInfo _androidDeviceInfo;
int? androidVersion;

Permissions() {
_init();
}

Future<bool> 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<PermissionStatus> requestIfNotFound() async {
if (await isGranted == false) {
return await requestPermission();
} else {
return PermissionStatus.granted;
}
}

Future<PermissionStatus> requestPermission() async {
return await Permission.manageExternalStorage.request();
}

Future<void> _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);
Expand All @@ -14,6 +54,7 @@ class _StatusesPageState extends State<StatusesPage> {
bool? isWhatsappInstalled;
bool? isPermissionGranted;
List<FileSystemEntity> _statusFiles = [];
final Permissions _permissions = Permissions();

Future<List<FileSystemEntity>> getFileList() async {
Directory dir = Directory(
Expand All @@ -31,10 +72,7 @@ class _StatusesPageState extends State<StatusesPage> {
}

Future<void> _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/')
Expand Down Expand Up @@ -63,9 +101,9 @@ class _StatusesPageState extends State<StatusesPage> {
} 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();
}
},
Expand Down
42 changes: 42 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit aba1169

Please sign in to comment.