Skip to content

An open-source cross-platform alternative to AirDrop

feat: open received file in gallery

Tien Do Namcf67a773566e
diff --git a/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt b/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
index 345651f2..9ac6ccd1 100644
--- a/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
+++ b/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
@@ -49,6 +49,11 @@ class MainActivity : FlutterActivity() {
result.success(null)
}
+ "openGallery" -> {
+ openGallery()
+ result.success(null)
+ }
+
else -> result.notImplemented()
}
}
@@ -236,6 +241,13 @@ class MainActivity : FlutterActivity() {
}
return false
}
+
+ private fun openGallery() {
+ val intent = Intent()
+ intent.action = Intent.ACTION_VIEW
+ intent.type = "image/*"
+ startActivity(intent)
+ }
}
data class PickDirectoryResult(
diff --git a/app/lib/provider/network/server/controller/receive_controller.dart b/app/lib/provider/network/server/controller/receive_controller.dart
index f8512aca..1d549ea1 100644
--- a/app/lib/provider/network/server/controller/receive_controller.dart
+++ b/app/lib/provider/network/server/controller/receive_controller.dart
@@ -559,14 +559,14 @@ class ReceiveController {
// ignore: use_build_context_synchronously
Routerino.context.pushRootImmediately(() => const HomePage(initialTab: HomeTab.receive, appStart: false));
- /* open the dialog to open file instantly*/
+ // open the dialog to open file instantly
if (destinationPath.isNotEmpty) {
OpenFileDialog.open(
- // ignore: use_build_context_synchronously
- Routerino.context,
- filePath: destinationPath,
- fileName: receivingFile.desiredName!,
- fileType: fileType);
+ Routerino.context, // ignore: use_build_context_synchronously
+ filePath: destinationPath,
+ fileType: fileType,
+ openGallery: saveToGallery,
+ );
}
});
}
diff --git a/app/lib/provider/selection/selected_sending_files_provider.dart b/app/lib/provider/selection/selected_sending_files_provider.dart
index e678d4aa..fc551035 100644
--- a/app/lib/provider/selection/selected_sending_files_provider.dart
+++ b/app/lib/provider/selection/selected_sending_files_provider.dart
@@ -5,8 +5,8 @@ import 'dart:typed_data';
import 'package:common/model/file_type.dart';
import 'package:localsend_app/model/cross_file.dart';
import 'package:localsend_app/util/file_path_helper.dart';
-import 'package:localsend_app/util/native/android_saf.dart';
import 'package:localsend_app/util/native/cache_helper.dart';
+import 'package:localsend_app/util/native/channel/android_channel.dart' as android_channel;
import 'package:localsend_app/util/native/content_uri_helper.dart';
import 'package:localsend_app/util/native/cross_file_converters.dart';
import 'package:logging/logging.dart';
@@ -184,7 +184,7 @@ class AddDirectoryAction extends AsyncReduxAction<SelectedSendingFilesNotifier,
/// A special [AddDirectoryAction] specifically for Android.
class AddAndroidDirectoryAction extends AsyncReduxAction<SelectedSendingFilesNotifier, List<CrossFile>> {
- final PickDirectoryResult result;
+ final android_channel.PickDirectoryResult result;
AddAndroidDirectoryAction(this.result);
diff --git a/app/lib/util/image_converter.dart b/app/lib/util/image_converter.dart
index 9b4714e8..6f73945a 100644
--- a/app/lib/util/image_converter.dart
+++ b/app/lib/util/image_converter.dart
@@ -4,14 +4,14 @@ import 'package:image/image.dart';
/// Converts BMP image data to PNG image data.
Future<Uint8List> convertBmpToPng(Uint8List bmp) async {
- final pngImage = await (Command()
- ..decodeBmp(bmp)
- ..encodePng())
+ final pngImage = await (Command()
+ ..decodeBmp(bmp)
+ ..encodePng())
.getBytesThread();
- if (pngImage == null) {
- throw Exception('Failed to convert BMP to PNG');
- }
+ if (pngImage == null) {
+ throw Exception('Failed to convert BMP to PNG');
+ }
- return pngImage;
+ return pngImage;
}
diff --git a/app/lib/util/native/android_saf.dart b/app/lib/util/native/channel/android_channel.dart
similarity index 95%
rename from app/lib/util/native/android_saf.dart
rename to app/lib/util/native/channel/android_channel.dart
index 2ba31aff..6ec860ff 100644
--- a/app/lib/util/native/android_saf.dart
+++ b/app/lib/util/native/channel/android_channel.dart
@@ -3,7 +3,7 @@ import 'package:flutter/services.dart';
import 'package:localsend_app/util/native/content_uri_helper.dart';
import 'package:logging/logging.dart';
-part 'android_saf.mapper.dart';
+part 'android_channel.mapper.dart';
const _methodChannel = MethodChannel('org.localsend.localsend_app/localsend');
final _logger = Logger('AndroidSaf');
@@ -83,6 +83,11 @@ Future<void> openContentUri({
});
}
+Future<void> openGallery() async {
+ _logger.info('Opening gallery');
+ await _methodChannel.invokeMethod('openGallery');
+}
+
@MappableClass()
class PickDirectoryResult with PickDirectoryResultMappable {
final String directoryUri;
diff --git a/app/lib/util/native/android_saf.mapper.dart b/app/lib/util/native/channel/android_channel.mapper.dart
similarity index 99%
rename from app/lib/util/native/android_saf.mapper.dart
rename to app/lib/util/native/channel/android_channel.mapper.dart
index b3490540..1d57a34f 100644
--- a/app/lib/util/native/android_saf.mapper.dart
+++ b/app/lib/util/native/channel/android_channel.mapper.dart
@@ -4,7 +4,7 @@
// ignore_for_file: unused_element, unnecessary_cast, override_on_non_overriding_member
// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter
-part of 'android_saf.dart';
+part of 'android_channel.dart';
class PickDirectoryResultMapper extends ClassMapperBase<PickDirectoryResult> {
PickDirectoryResultMapper._();
diff --git a/app/lib/util/native/cross_file_converters.dart b/app/lib/util/native/cross_file_converters.dart
index 4807132c..aa43c4c9 100644
--- a/app/lib/util/native/cross_file_converters.dart
+++ b/app/lib/util/native/cross_file_converters.dart
@@ -6,7 +6,7 @@ import 'package:flutter/foundation.dart';
import 'package:image_picker/image_picker.dart';
import 'package:localsend_app/model/cross_file.dart';
import 'package:localsend_app/util/file_path_helper.dart';
-import 'package:localsend_app/util/native/android_saf.dart';
+import 'package:localsend_app/util/native/channel/android_channel.dart' as android_channel;
import 'package:share_handler/share_handler.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
@@ -55,7 +55,7 @@ class CrossFileConverters {
);
}
- static Future<CrossFile> convertFileInfo(FileInfo file) async {
+ static Future<CrossFile> convertFileInfo(android_channel.FileInfo file) async {
return CrossFile(
name: file.name,
fileType: file.name.guessFileType(),
diff --git a/app/lib/util/native/file_picker.dart b/app/lib/util/native/file_picker.dart
index c373ed2c..ff1e5a9c 100644
--- a/app/lib/util/native/file_picker.dart
+++ b/app/lib/util/native/file_picker.dart
@@ -15,7 +15,7 @@ import 'package:localsend_app/provider/selection/selected_sending_files_provider
import 'package:localsend_app/util/determine_image_type.dart';
import 'package:localsend_app/util/file_path_helper.dart';
import 'package:localsend_app/util/image_converter.dart';
-import 'package:localsend_app/util/native/android_saf.dart';
+import 'package:localsend_app/util/native/channel/android_channel.dart' as android_channel;
import 'package:localsend_app/util/native/cross_file_converters.dart';
import 'package:localsend_app/util/native/pick_directory_path.dart';
import 'package:localsend_app/util/native/platform_check.dart';
@@ -154,7 +154,7 @@ Future<void> _pickFiles(BuildContext context, Ref ref) async {
}
try {
if (defaultTargetPlatform == TargetPlatform.android) {
- final result = await pickFilesAndroid();
+ final result = await android_channel.pickFilesAndroid();
if (result != null) {
await ref.redux(selectedSendingFilesProvider).dispatchAsync(AddFilesAction(
files: result,
@@ -205,9 +205,9 @@ Future<void> _pickFolder(BuildContext context, Ref ref) async {
);
await sleepAsync(200); // Wait for the dialog to be shown
try {
- if (defaultTargetPlatform == TargetPlatform.android && (ref.read(deviceInfoProvider).androidSdkInt ?? 0) >= contentUriMinSdk) {
+ if (defaultTargetPlatform == TargetPlatform.android && (ref.read(deviceInfoProvider).androidSdkInt ?? 0) >= android_channel.contentUriMinSdk) {
// Android 8 and above have more predictable content URIs that we can parse.
- final result = await pickDirectoryAndroid();
+ final result = await android_channel.pickDirectoryAndroid();
if (result != null) {
await ref.redux(selectedSendingFilesProvider).dispatchAsync(AddAndroidDirectoryAction(result));
}
diff --git a/app/lib/util/native/file_saver.dart b/app/lib/util/native/file_saver.dart
index 7b091cbb..e76d0b3b 100644
--- a/app/lib/util/native/file_saver.dart
+++ b/app/lib/util/native/file_saver.dart
@@ -4,7 +4,7 @@ import 'dart:typed_data';
import 'package:gal/gal.dart';
import 'package:legalize/legalize.dart';
import 'package:localsend_app/util/file_path_helper.dart';
-import 'package:localsend_app/util/native/android_saf.dart';
+import 'package:localsend_app/util/native/channel/android_channel.dart' as android_channel;
import 'package:localsend_app/util/native/content_uri_helper.dart';
import 'package:logging/logging.dart';
import 'package:mime/mime.dart';
@@ -169,7 +169,7 @@ Future<(String, String?, String)> digestFilePathAndPrepareDirectory({
final String documentUri;
if (fileName.contains('/')) {
try {
- await createMissingDirectoriesAndroid(parentUri: parentDirectory, fileName: fileName, createdDirectories: createdDirectories);
+ await android_channel.createMissingDirectoriesAndroid(parentUri: parentDirectory, fileName: fileName, createdDirectories: createdDirectories);
} catch (e) {
_logger.warning('Could not create missing directories', e);
}
diff --git a/app/lib/util/native/open_file.dart b/app/lib/util/native/open_file.dart
index e5dd18bf..0edd9e5a 100644
--- a/app/lib/util/native/open_file.dart
+++ b/app/lib/util/native/open_file.dart
@@ -1,6 +1,6 @@
import 'package:common/model/file_type.dart';
import 'package:flutter/material.dart';
-import 'package:localsend_app/util/native/android_saf.dart';
+import 'package:localsend_app/util/native/channel/android_channel.dart' as android_channel;
import 'package:localsend_app/util/native/platform_check.dart';
import 'package:localsend_app/widget/dialogs/cannot_open_file_dialog.dart';
import 'package:open_filex/open_filex.dart';
@@ -18,7 +18,7 @@ Future<void> openFile(
}
if (filePath.startsWith('content://')) {
- await openContentUri(uri: filePath);
+ await android_channel.openContentUri(uri: filePath);
return;
}
diff --git a/app/lib/util/native/open_folder.dart b/app/lib/util/native/open_folder.dart
index 1ecff588..1f05a6af 100644
--- a/app/lib/util/native/open_folder.dart
+++ b/app/lib/util/native/open_folder.dart
@@ -1,5 +1,5 @@
import 'package:flutter/foundation.dart';
-import 'package:localsend_app/util/native/android_saf.dart';
+import 'package:localsend_app/util/native/channel/android_channel.dart' as android_channel;
import 'package:localsend_app/util/native/platform_check.dart';
import 'package:logging/logging.dart';
import 'package:open_dir/open_dir.dart';
@@ -13,7 +13,7 @@ Future<void> openFolder({
String? fileName,
}) async {
if (folderPath.startsWith('content://')) {
- await openContentUri(uri: folderPath);
+ await android_channel.openContentUri(uri: folderPath);
return;
}
diff --git a/app/lib/util/native/pick_directory_path.dart b/app/lib/util/native/pick_directory_path.dart
index 2805fcb8..a15e1aaa 100644
--- a/app/lib/util/native/pick_directory_path.dart
+++ b/app/lib/util/native/pick_directory_path.dart
@@ -2,7 +2,7 @@ import 'package:file_picker/file_picker.dart';
import 'package:file_selector/file_selector.dart' as file_selector;
import 'package:flutter/foundation.dart';
import 'package:localsend_app/provider/device_info_provider.dart';
-import 'package:localsend_app/util/native/android_saf.dart';
+import 'package:localsend_app/util/native/channel/android_channel.dart' as android_channel;
import 'package:localsend_app/util/native/platform_check.dart';
import 'package:refena_flutter/refena_flutter.dart';
@@ -10,8 +10,8 @@ import 'package:refena_flutter/refena_flutter.dart';
/// Returns null if the user cancels the selection.
Future<String?> pickDirectoryPath() async {
if (defaultTargetPlatform == TargetPlatform.android &&
- (RefenaScope.defaultRef.read(deviceRawInfoProvider).androidSdkInt ?? 0) >= contentUriMinSdk) {
- return await pickDirectoryPathAndroid();
+ (RefenaScope.defaultRef.read(deviceRawInfoProvider).androidSdkInt ?? 0) >= android_channel.contentUriMinSdk) {
+ return await android_channel.pickDirectoryPathAndroid();
}
if (checkPlatform([TargetPlatform.android, TargetPlatform.iOS])) {
diff --git a/app/lib/widget/dialogs/open_file_dialog.dart b/app/lib/widget/dialogs/open_file_dialog.dart
index 7210b9e2..07768d72 100644
--- a/app/lib/widget/dialogs/open_file_dialog.dart
+++ b/app/lib/widget/dialogs/open_file_dialog.dart
@@ -2,32 +2,39 @@ import 'dart:async';
import 'dart:io';
import 'package:common/model/file_type.dart';
+import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:localsend_app/gen/strings.g.dart';
+import 'package:localsend_app/util/native/channel/android_channel.dart' as android_channel;
import 'package:localsend_app/util/native/open_file.dart';
import 'package:localsend_app/util/native/open_folder.dart';
import 'package:path/path.dart' as path;
import 'package:routerino/routerino.dart';
class OpenFileDialog extends StatefulWidget {
- final String fileName;
final String filePath;
final FileType fileType;
+ final bool openGallery;
- const OpenFileDialog({super.key, required this.filePath, required this.fileName, required this.fileType});
+ const OpenFileDialog({
+ super.key,
+ required this.filePath,
+ required this.fileType,
+ required this.openGallery,
+ });
static Future<void> open(
BuildContext context, {
required String filePath,
- required String fileName,
required FileType fileType,
+ required bool openGallery,
}) async {
await showDialog(
context: context,
builder: (context) => OpenFileDialog(
filePath: filePath,
- fileName: fileName,
fileType: fileType,
+ openGallery: openGallery,
),
);
}
@@ -71,16 +78,23 @@ class _OpenFileDialogState extends State<OpenFileDialog> {
content: Text(t.dialogs.openFile.content),
actions: [
TextButton(
- onPressed: () async => openFile(context, widget.fileType, widget.filePath),
+ onPressed: () async {
+ if (widget.openGallery && defaultTargetPlatform == TargetPlatform.android) {
+ await android_channel.openGallery();
+ } else {
+ await openFile(context, widget.fileType, widget.filePath);
+ }
+ },
child: Text(t.general.open),
),
- TextButton(
- onPressed: () async => await openFolder(
- folderPath: File(widget.filePath).parent.path,
- fileName: path.basename(widget.filePath),
+ if (!widget.openGallery)
+ TextButton(
+ onPressed: () async => await openFolder(
+ folderPath: File(widget.filePath).parent.path,
+ fileName: path.basename(widget.filePath),
+ ),
+ child: Text(t.receiveHistoryPage.entryActions.showInFolder),
),
- child: Text(t.receiveHistoryPage.entryActions.showInFolder),
- ),
TextButton(
onPressed: () {
_timer.cancel();