Skip to content

An open-source cross-platform alternative to AirDrop

feat: support clipboard for android (#1754)

Seidko2227716c9c2f
diff --git a/app/lib/util/native/file_picker.dart b/app/lib/util/native/file_picker.dart
index a22f4503..6057ee0a 100644
--- a/app/lib/util/native/file_picker.dart
+++ b/app/lib/util/native/file_picker.dart
@@ -2,17 +2,18 @@ import 'dart:async';
import 'package:common/model/file_type.dart';
import 'package:common/util/sleep.dart';
-import 'package:file_selector/file_selector.dart' as file_selector;
import 'package:file_selector/file_selector.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:localsend_app/config/theme.dart';
import 'package:localsend_app/gen/strings.g.dart';
+import 'package:localsend_app/model/cross_file.dart';
import 'package:localsend_app/pages/apk_picker_page.dart';
import 'package:localsend_app/provider/device_info_provider.dart';
import 'package:localsend_app/provider/selection/selected_sending_files_provider.dart';
import 'package:localsend_app/util/determine_image_type.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/cross_file_converters.dart';
import 'package:localsend_app/util/native/pick_directory_path.dart';
@@ -26,9 +27,11 @@ import 'package:pasteboard/pasteboard.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:refena_flutter/refena_flutter.dart';
import 'package:routerino/routerino.dart';
+import 'package:uri_content/uri_content.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
final _logger = Logger('FilePickerHelper');
+final _uriContent = UriContent();
enum FilePickerOption {
file(Icons.description),
@@ -76,6 +79,7 @@ enum FilePickerOption {
return [
FilePickerOption.file,
FilePickerOption.media,
+ FilePickerOption.clipboard,
FilePickerOption.text,
FilePickerOption.folder,
FilePickerOption.app,
@@ -156,7 +160,7 @@ Future<void> _pickFiles(BuildContext context, Ref ref) async {
));
}
} else {
- final result = await file_selector.openFiles();
+ final result = await openFiles();
await ref.redux(selectedSendingFilesProvider).dispatchAsync(AddFilesAction(
files: result,
converter: CrossFileConverters.convertXFile,
@@ -259,18 +263,6 @@ Future<void> _pickText(BuildContext context, Ref ref) async {
}
Future<void> _pickClipboard(BuildContext context, Ref ref) async {
- late List<String> files = [];
- for (final file in await Pasteboard.files()) {
- files.add(file);
- }
- if (files.isNotEmpty) {
- await ref.redux(selectedSendingFilesProvider).dispatchAsync(AddFilesAction(
- files: files.map((e) => XFile(e)).toList(),
- converter: CrossFileConverters.convertXFile,
- ));
- return;
- }
-
final data = await Clipboard.getData(Clipboard.kTextPlain);
if (data?.text != null) {
ref.redux(selectedSendingFilesProvider).dispatch(AddMessageAction(message: data!.text!));
@@ -290,6 +282,31 @@ Future<void> _pickClipboard(BuildContext context, Ref ref) async {
return;
}
+ final List<String> files = await Pasteboard.files();
+ if (files.isNotEmpty) {
+ await ref.redux(selectedSendingFilesProvider).dispatchAsync(AddFilesAction(
+ files: files.map((e) => XFile(e)).toList(),
+ converter: (file) async {
+ if (!file.path.startsWith('content://')) {
+ return CrossFileConverters.convertXFile(file);
+ }
+ // handle content uri
+ return CrossFile(
+ name: file.name,
+ fileType: file.name.guessFileType(),
+ size: await _uriContent.getContentLength(Uri.parse(file.path)) ?? -1,
+ path: file.path,
+ thumbnail: null,
+ asset: null,
+ bytes: null,
+ lastModified: null,
+ lastAccessed: null,
+ );
+ },
+ ));
+ return;
+ }
+
if (!context.mounted) {
return;
}
diff --git a/app/pubspec.lock b/app/pubspec.lock
index 470311d1..f7abd115 100644
--- a/app/pubspec.lock
+++ b/app/pubspec.lock
@@ -1004,10 +1004,11 @@ packages:
pasteboard:
dependency: "direct main"
description:
- name: pasteboard
- sha256: "7bf733f3a00c7188ec1f2c6f0612854248b302cf91ef3611a2b7bb141c0f9d55"
- url: "https://pub.dev"
- source: hosted
+ path: "packages/pasteboard"
+ ref: "58748dae405df5e68a131e4905d48e75d0624be2"
+ resolved-ref: "58748dae405df5e68a131e4905d48e75d0624be2"
+ url: "https://github.com/Seidko/flutter-plugins.git"
+ source: git
version: "0.3.0"
path:
dependency: "direct main"
@@ -1134,10 +1135,10 @@ packages:
dependency: transitive
description:
name: photo_manager
- sha256: "2b5c0d02014b64b971e57aa840ae7d7ffd81b0482a082579eb17ff83e5dc408e"
+ sha256: "32a1ce1095aeaaa792a29f28c1f74613aa75109f21c2d4ab85be3ad9964230a4"
url: "https://pub.dev"
source: hosted
- version: "3.4.0"
+ version: "3.5.0"
photo_manager_image_provider:
dependency: transitive
description:
diff --git a/app/pubspec.yaml b/app/pubspec.yaml
index 7b8093fd..83ce4563 100644
--- a/app/pubspec.yaml
+++ b/app/pubspec.yaml
@@ -84,6 +84,12 @@ dev_dependencies:
test: ^1.24.7
dependency_overrides:
+ pasteboard:
+ # This is temporary workaround, wait upstream for update
+ git:
+ url: https://github.com/Seidko/flutter-plugins.git
+ ref: 58748dae405df5e68a131e4905d48e75d0624be2
+ path: packages/pasteboard
permission_handler_windows:
# Remove windows support as it causes trouble with Windows 7
# https://github.com/Baseflow/flutter-permission-handler/issues/1034