From 8a542bf5c2f5dd2cd275111074f29b71b2b11dd7 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 28 Oct 2020 08:05:10 +0100 Subject: [PATCH] fix: Android Download --- android/app/build.gradle | 6 ++---- lib/utils/matrix_file_extension.dart | 12 +++++++++--- lib/utils/platform_infos.dart | 3 +++ pubspec.lock | 21 +++++++++++++++++++++ pubspec.yaml | 2 ++ 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 0ff405a..8ff431e 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -44,7 +44,7 @@ android { defaultConfig { applicationId "chat.fluffy.fluffychat" - minSdkVersion 18 + minSdkVersion 21 targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName @@ -87,6 +87,4 @@ dependencies { implementation "net.zetetic:android-database-sqlcipher:4.4.0" // needed for moor_ffi w/ sqlcipher } -if(file("google-services.json").exists() || System.getenv('CI')){ - apply plugin: 'com.google.gms.google-services' -} \ No newline at end of file +apply plugin: 'com.google.gms.google-services' \ No newline at end of file diff --git a/lib/utils/matrix_file_extension.dart b/lib/utils/matrix_file_extension.dart index 2f1dc44..d7daef3 100644 --- a/lib/utils/matrix_file_extension.dart +++ b/lib/utils/matrix_file_extension.dart @@ -1,11 +1,14 @@ import 'dart:io'; import 'package:famedlysdk/famedlysdk.dart'; +import 'package:fluffychat/utils/platform_infos.dart'; import 'package:flutter/foundation.dart'; import 'package:open_file/open_file.dart'; import 'package:path_provider/path_provider.dart'; import 'package:universal_html/prefer_universal/html.dart' as html; import 'package:mime_type/mime_type.dart'; +import 'package:downloads_path_provider_28/downloads_path_provider_28.dart'; +import 'package:permission_handler/permission_handler.dart'; extension MatrixFileExtension on MatrixFile { void open() async { @@ -24,9 +27,12 @@ extension MatrixFileExtension on MatrixFile { element.click(); element.remove(); } else { - final downloadsDir = Platform.isAndroid - ? (await getExternalStorageDirectory()) - : (await getApplicationDocumentsDirectory()); + if (!(await Permission.storage.request()).isGranted) return; + final downloadsDir = PlatformInfos.isDesktop + ? (await getDownloadsDirectory()) + : Platform.isAndroid + ? (await DownloadsPathProvider.downloadsDirectory) + : (await getApplicationDocumentsDirectory()); final file = File(downloadsDir.path + '/' + name.split('/').last); file.writeAsBytesSync(bytes); diff --git a/lib/utils/platform_infos.dart b/lib/utils/platform_infos.dart index 5473a8c..3bc4898 100644 --- a/lib/utils/platform_infos.dart +++ b/lib/utils/platform_infos.dart @@ -11,5 +11,8 @@ abstract class PlatformInfos { static bool get isBetaDesktop => !kIsWeb && (Platform.isWindows || Platform.isLinux); + static bool get isDesktop => + !kIsWeb && (Platform.isLinux || Platform.isWindows || Platform.isMacOS); + static bool get usesTouchscreen => !isMobile; } diff --git a/pubspec.lock b/pubspec.lock index 17ea035..351bde9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -183,6 +183,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.0.3" + downloads_path_provider_28: + dependency: "direct main" + description: + name: downloads_path_provider_28 + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.0" encrypt: dependency: transitive description: @@ -683,6 +690,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.10.0-nullsafety.1" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + url: "https://pub.dartlang.org" + source: hosted + version: "5.0.1+1" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" petitparser: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index b3fb5ca..3c2919b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,6 +39,8 @@ dependencies: # desktop_notifications: ^0.0.0-dev.4 // Currently blocked by: https://github.com/canonical/desktop_notifications.dart/issues/5 matrix_link_text: ^0.3.1 path_provider: ^1.5.1 + downloads_path_provider_28: ^0.1.0 + permission_handler: ^5.0.1+1 webview_flutter: ^0.3.19+9 share: ^0.6.3+5 flutter_secure_storage: ^3.3.5