From c52c254b916d863332020ec64d1b877ae10a4ee0 Mon Sep 17 00:00:00 2001 From: LaksCastro Date: Sat, 20 Nov 2021 23:54:18 -0300 Subject: [PATCH 01/11] (#3) Add `Environment.getExternalStorageDirectory` support --- .gitignore | 2 ++ android/build.gradle | 2 +- android/settings.gradle | 2 +- android/src/main/AndroidManifest.xml | 2 +- .../sharedstorage/EnvironmentDirectoryOf.kt | 5 ++-- .../sharedstorage/SharedStoragePlugin.kt | 7 ++++- example/README.md | 4 +-- example/android/app/build.gradle | 4 +-- .../android/app/src/main/AndroidManifest.xml | 26 ++++++++-------- example/lib/main.dart | 18 +++++++++-- example/pubspec.lock | 30 ++++++++++++++----- example/pubspec.yaml | 8 ++--- lib/shared_storage.dart | 19 ++++++++++++ 13 files changed, 89 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index fe5a19e..d754b83 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,8 @@ .history .svn/ +*/**/pubspec.lock + # IntelliJ related *.iml *.ipr diff --git a/android/build.gradle b/android/build.gradle index 76c12f3..b104d07 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,4 +1,4 @@ -group 'com.pinciat.external_path' +group 'io.lakscastro.sharedstorage' version '1.0-SNAPSHOT' buildscript { diff --git a/android/settings.gradle b/android/settings.gradle index e688d1d..0f44934 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -1 +1 @@ -rootProject.name = 'external_path' +rootProject.name = 'sharedstorage' diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 4f813cd..d8a8d77 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,3 +1,3 @@ + package="io.lakscastro.sharedstorage"> diff --git a/android/src/main/kotlin/io/lakscastro/sharedstorage/EnvironmentDirectoryOf.kt b/android/src/main/kotlin/io/lakscastro/sharedstorage/EnvironmentDirectoryOf.kt index f65e94c..344e9b7 100644 --- a/android/src/main/kotlin/io/lakscastro/sharedstorage/EnvironmentDirectoryOf.kt +++ b/android/src/main/kotlin/io/lakscastro/sharedstorage/EnvironmentDirectoryOf.kt @@ -1,8 +1,9 @@ package io.lakscastro.sharedstorage import android.os.Environment +import java.io.File -fun environmentDirectoryOf(directory: String): String { +fun environmentDirectoryOf(directory: String): File { val mapper = mapOf( "EnvironmentDirectory.Alarms" to Environment.DIRECTORY_ALARMS, "EnvironmentDirectory.DCIM" to Environment.DIRECTORY_DCIM, @@ -15,5 +16,5 @@ fun environmentDirectoryOf(directory: String): String { "EnvironmentDirectory.Ringtones" to Environment.DIRECTORY_RINGTONES ) - return mapper[directory] ?: directory + return Environment.getExternalStoragePublicDirectory(mapper[directory] ?: directory) } \ No newline at end of file diff --git a/android/src/main/kotlin/io/lakscastro/sharedstorage/SharedStoragePlugin.kt b/android/src/main/kotlin/io/lakscastro/sharedstorage/SharedStoragePlugin.kt index 1ad70a2..30cbd36 100644 --- a/android/src/main/kotlin/io/lakscastro/sharedstorage/SharedStoragePlugin.kt +++ b/android/src/main/kotlin/io/lakscastro/sharedstorage/SharedStoragePlugin.kt @@ -25,6 +25,8 @@ class SharedStoragePlugin : FlutterPlugin, MethodCallHandler { const val METHOD_CHANNEL_NAME = "io.lakscastro.plugins/sharedstorage" const val GET_EXTERNAL_STORAGE_PUBLIC_DIRECTORY = "getExternalStoragePublicDirectory" + const val GET_EXTERNAL_STORAGE_DIRECTORY = "getExternalStorageDirectory" + const val GET_MEDIA_STORE_CONTENT_DIRECTORY = "getMediaStoreContentDirectory" const val GET_ROOT_DIRECTORY = "getRootDirectory" } @@ -42,13 +44,16 @@ class SharedStoragePlugin : FlutterPlugin, MethodCallHandler { override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { when (call.method) { GET_EXTERNAL_STORAGE_PUBLIC_DIRECTORY -> getExternalStoragePublicDirectory(result, call.argument("directory") as String) + GET_EXTERNAL_STORAGE_DIRECTORY -> getExternalStorageDirectory(result) GET_MEDIA_STORE_CONTENT_DIRECTORY -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) getMediaStoreContentDirectory(result, call.argument("collection") as String) else result.notImplemented() GET_ROOT_DIRECTORY -> getRootDirectory(result) else -> result.notImplemented() } } - private fun getExternalStoragePublicDirectory(result: Result, directory: String) = result.success(Environment.getExternalStoragePublicDirectory(environmentDirectoryOf(directory)).absolutePath) + private fun getExternalStoragePublicDirectory(result: Result, directory: String) = result.success(environmentDirectoryOf(directory).absolutePath) + + private fun getExternalStorageDirectory(result: Result) = result.success(Environment.getExternalStorageDirectory().absolutePath) @RequiresApi(Build.VERSION_CODES.Q) private fun getMediaStoreContentDirectory(result: Result, collection: String) = result.success(mediaStoreOf(collection)) diff --git a/example/README.md b/example/README.md index 9649278..8bdf14f 100644 --- a/example/README.md +++ b/example/README.md @@ -1,6 +1,6 @@ -# external_path_example +# sharedstorage_example -Demonstrates how to use the external_path plugin. +Demonstrates how to use the shared_storage plugin. ## Getting Started diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 51b8236..5f20f05 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 30 + compileSdkVersion 29 sourceSets { main.java.srcDirs += 'src/main/kotlin' @@ -35,7 +35,7 @@ android { defaultConfig { applicationId "io.lakscastro.sharedstorage.example" minSdkVersion 16 - targetSdkVersion 30 + targetSdkVersion 29 versionCode flutterVersionCode.toInteger() versionName flutterVersionName } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 537c76b..c3cb6c3 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,36 +1,36 @@ - + + + android:name="io.flutter.embedding.android.NormalTheme" + android:resource="@style/NormalTheme" /> + android:name="io.flutter.embedding.android.SplashScreenDrawable" + android:resource="@drawable/launch_background" /> - - + +