Commit fc1130fa by Nayan

Added support of opening `Recent` folder in `Android` and any `sub-folder`…

Added support of opening `Recent` folder in `Android` and any `sub-folder` inside app's document in `iOS`
parent f0b696db
......@@ -15,3 +15,7 @@
## 0.0.4
- Upload preview
## 1.0.0
- Added support of opening `Recent` folder in `Android` and any `sub-folder` inside app's document in `iOS`
\ No newline at end of file
......@@ -8,12 +8,14 @@ A flutter plugin to open the default file manager app.
## How it works?
### Android
The `Android` app can open either `Recent` folder or `Download` folder.
The plugin will show the available file manager apps in the bottom popup and you can select one app to open.
That app will open with the `Download` folder which is a public folder.
That app will open with the given folder in selected app.
### iOS
Plugin will open the `Files` app in iOS. You need to add the following code snippet in `Info.plist` to view your app folder inside `On My iPhone`.
Also, you need to save at least one file to view your app's folder
The `iOS` app can open app's document folder and it's sub directory if provided.
Plugin will open the `Files` app in iOS. You need to add the following code snippet in `Info.plist` to view your app's document folder inside `On My iPhone`.
Also, you need to save at least one file to view your app's folder.
```xml
<key>UISupportsDocumentBrowser</key>
......@@ -22,14 +24,25 @@ Also, you need to save at least one file to view your app's folder
## Usage
It's a very simple to use. There is only one line of code!!!
It's a very simple to use. Just call the below method and add `config` if required.
```dart
import 'package:open_file_manager/open_file_manager.dart'
openFileManager();
openFileManager(
androidConfig: AndroidConfig(
folderType: FolderType.recent,
),
iosConfig: IosConfig(
// Path is case-sensitive here.
subFolderPath: 'Pictures/Screenshots',
),
);
```
- If `androidConfig` doesn't provided, Android app will open `Download` folder by default.
- If `iosConfig` doesn't provided, iOS app will open app's document folder by default.
## Preview
......
......@@ -3,6 +3,8 @@ package com.aubergine.open_file_manager
import android.app.DownloadManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Environment
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
......@@ -16,32 +18,46 @@ class OpenFileManagerPlugin : FlutterPlugin, MethodCallHandler {
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var channel: MethodChannel
private lateinit var context: Context;
private lateinit var context: Context
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "open_file_manager")
channel.setMethodCallHandler(this)
context = flutterPluginBinding.applicationContext;
context = flutterPluginBinding.applicationContext
}
override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "openFileManager") {
openFileManager(result)
} else {
when (call.method) {
"openFileManager" -> {
val args = call.arguments as HashMap<*, *>?
openFileManager(result, args?.get("folderType") as String?)
}
else -> {
result.notImplemented()
}
}
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
private fun openFileManager(result: Result) {
private fun openFileManager(result: Result, folderType: String?) {
try {
if (folderType == null || folderType == "download") {
val downloadIntent = Intent(DownloadManager.ACTION_VIEW_DOWNLOADS)
downloadIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(downloadIntent)
result.success(true)
} else if (folderType == "recent") {
val uri = Environment.getExternalStorageDirectory().absolutePath
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.setDataAndType(Uri.parse(uri), "*/*")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
result.success(true)
}
} catch (e: Exception) {
result.error("$e", "Unable to open the file manager", "")
}
......
......@@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
# Uncomment this line to define a global platform for your project
# platform :ios, '11.0'
# platform :ios, '12.0'
# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
......
......@@ -14,9 +14,9 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/open_file_manager/ios"
SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
open_file_manager: 42cc4527e1ddfea91cd0cc9ed4553200596a5827
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
open_file_manager: cdc5849f062aa70cad3da24a0dde74955d78c1ec
PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3
PODFILE CHECKSUM: c4c93c5f6502fe2754f48404d3594bf779584011
COCOAPODS: 1.12.1
COCOAPODS: 1.15.2
......@@ -155,7 +155,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
......
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
......
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:open_file_manager/open_file_manager.dart';
......@@ -13,6 +15,10 @@ class MyApp extends StatefulWidget {
}
class _MyAppState extends State<MyApp> {
final _androidFolderTypes = [FolderType.recent, FolderType.download];
var _selectedFolderType = FolderType.download;
final _subFolderPathCtrl = TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
......@@ -21,13 +27,61 @@ class _MyAppState extends State<MyApp> {
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: ElevatedButton(
body: Column(
children: [
const SizedBox(height: 32),
if (Platform.isAndroid) ...[
const Text(
'Select Android Folder type',
style: TextStyle(fontSize: 20),
),
ListView.builder(
itemCount: _androidFolderTypes.length,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, i) => RadioListTile<FolderType>(
value: _androidFolderTypes[i],
groupValue: _selectedFolderType,
title: Text(_androidFolderTypes[i].name),
onChanged: (folderType) {
if (folderType != null &&
folderType != _selectedFolderType) {
setState(() => _selectedFolderType = folderType);
}
},
),
),
],
if (Platform.isIOS) ...[
const Text(
'Write iOS sub-folder path',
style: TextStyle(fontSize: 20),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: TextFormField(
controller: _subFolderPathCtrl,
decoration: const InputDecoration(
hintText: 'Sub folder path (Optional)',
),
),
),
],
const SizedBox(height: 32),
ElevatedButton(
onPressed: () {
openFileManager();
openFileManager(
androidConfig: AndroidConfig(
folderType: _selectedFolderType,
),
iosConfig: IosConfig(
subFolderPath: _subFolderPathCtrl.text.trim(),
),
);
},
child: const Text('Open File Manager'),
),
],
),
),
);
......
......@@ -137,7 +137,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.4"
version: "1.0.0"
path:
dependency: transitive
description:
......@@ -224,4 +224,5 @@ packages:
source: hosted
version: "13.0.0"
sdks:
dart: ">=3.2.0-0 <4.0.0"
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.0.0"
......@@ -6,7 +6,7 @@ description: Demonstrates how to use the open_file_manager plugin.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
environment:
sdk: '>=2.18.2 <3.0.0'
sdk: '>=3.1.0 <4.0.0'
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
......
......@@ -13,14 +13,19 @@ public class SwiftOpenFileManagerPlugin: NSObject, FlutterPlugin {
result(FlutterMethodNotImplemented)
return
}
let path = getDocumentsDirectory().absoluteString.replacingOccurrences(of: "file://", with: "shareddocuments://")
let url = URL(string: path)!
UIApplication.shared.open(url)
}
private func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
guard let arguments = call.arguments as? [String: Any] else {
result(FlutterError(code: "INVALID_ARGUMENTS", message: "Arguments must be a map", details: nil))
return
}
let subFolderPath = arguments["subFolderPath"] as? String
var path = documentsDirectory.absoluteString.replacingOccurrences(of: "file://", with: "shareddocuments://")
path.append(subFolderPath ?? "")
UIApplication.shared.open(URL(string: path)!)
}
}
part of 'open_file_manager.dart';
final class AndroidConfig {
final FolderType folderType;
AndroidConfig({required this.folderType});
}
final class IosConfig {
final String subFolderPath;
IosConfig({required this.subFolderPath});
}
enum FolderType { recent, download }
library open_file_manager;
import 'open_file_manager_platform_interface.dart';
Future<bool> openFileManager() {
return OpenFileManagerPlatform.instance.openFileManager();
part 'config.dart';
Future<bool> openFileManager({
AndroidConfig? androidConfig,
IosConfig? iosConfig,
}) {
return OpenFileManagerPlatform.instance.openFileManager(
androidConfig: androidConfig,
iosConfig: iosConfig,
);
}
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'open_file_manager.dart';
import 'open_file_manager_platform_interface.dart';
/// An implementation of [OpenFileManagerPlatform] that uses method channels.
......@@ -10,8 +13,18 @@ class MethodChannelOpenFileManager extends OpenFileManagerPlatform {
final methodChannel = const MethodChannel('open_file_manager');
@override
Future<bool> openFileManager() async {
final version = await methodChannel.invokeMethod<bool?>('openFileManager');
Future<bool> openFileManager({
AndroidConfig? androidConfig,
IosConfig? iosConfig,
}) async {
final data = <String, dynamic>{};
if (Platform.isAndroid && androidConfig != null) {
data['folderType'] = androidConfig.folderType.name;
} else if (Platform.isIOS && iosConfig != null) {
data['subFolderPath'] = iosConfig.subFolderPath;
}
final version =
await methodChannel.invokeMethod<bool?>('openFileManager', data);
return version ?? false;
}
}
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'open_file_manager.dart';
import 'open_file_manager_method_channel.dart';
abstract class OpenFileManagerPlatform extends PlatformInterface {
......@@ -23,7 +24,10 @@ abstract class OpenFileManagerPlatform extends PlatformInterface {
_instance = instance;
}
Future<bool> openFileManager() {
throw UnimplementedError('platformVersion() has not been implemented.');
Future<bool> openFileManager({
AndroidConfig? androidConfig,
IosConfig? iosConfig,
}) {
throw UnimplementedError('openFileManager() has not been implemented.');
}
}
name: open_file_manager
description: A flutter plugin to open default file manager app. on an Android, it opens Download folder on selected file manager app. on iOS, it opens app folder in default Files app.
version: 0.0.4
description: A flutter plugin to open default file manager app. on an Android, it opens Download/Recent folder on file manager app. on iOS, it opens app's document folder in Files app.
version: 1.0.0
homepage: https://auberginesolutions.com
repository: https://github.com/nayanAubie/open_file_manager
environment:
sdk: ">=3.1.0 <4.0.0"
sdk: '>=3.3.0 <4.0.0'
flutter: ">=3.0.0"
dependencies:
flutter:
......
......@@ -8,7 +8,11 @@ class MockOpenFileManagerPlatform
with MockPlatformInterfaceMixin
implements OpenFileManagerPlatform {
@override
Future<bool> openFileManager() => Future.value(true);
Future<bool> openFileManager({
AndroidConfig? androidConfig,
IosConfig? iosConfig,
}) =>
Future.value(true);
}
void main() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment