feat: android deeplink and file selector (#118)

This commit is contained in:
Li Wei authored and GitHub committed 2023-06-06 20:39:07 +08:00
1 parent e4c3b449a4
commit 0100f0131d
15 files changed
+202 -66

No files matched your search

@@ -12,9 +12,54 @@
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:exported="true" android:exported="true"
android:hardwareAccelerated="true" android:hardwareAccelerated="true"
android:launchMode="singleTop" android:launchMode="singleInstance"
android:theme="@style/LaunchTheme" android:theme="@style/LaunchTheme"
android:windowSoftInputMode="adjustResize"> android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="file"
android:host="*"
android:pathPattern=".*torrent" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="file"
android:host="*"
android:mimeType="application/x-bittorrent" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="content"
android:host="*"
android:pathPattern=".*torrent" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="content"
android:host="*"
android:mimeType="application/x-bittorrent" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="magnet" />
</intent-filter>
<!-- Specifies an Android theme to apply to this Activity as soon as <!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues while the Flutter UI initializes. After that, this theme continues
@@ -1,10 +1,13 @@
import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:ui' as ui; import 'dart:ui' as ui;
import 'package:app_links/app_links.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:uri_to_file/uri_to_file.dart';
import '../../../../api/api.dart'; import '../../../../api/api.dart';
import '../../../../api/model/downloader_config.dart'; import '../../../../api/model/downloader_config.dart';
@@ -13,6 +16,7 @@ import '../../../../generated/locales.g.dart';
import '../../../../util/localeManager.dart'; import '../../../../util/localeManager.dart';
import '../../../../util/log_util.dart'; import '../../../../util/log_util.dart';
import '../../../../util/util.dart'; import '../../../../util/util.dart';
import '../../../routes/app_pages.dart';
const _startConfigNetwork = "start.network"; const _startConfigNetwork = "start.network";
const _startConfigAddress = "start.address"; const _startConfigAddress = "start.address";
@@ -62,6 +66,42 @@ class AppController extends GetxController {
final runningPort = 0.obs; final runningPort = 0.obs;
final downloaderConfig = DownloaderConfig().obs; final downloaderConfig = DownloaderConfig().obs;
late AppLinks _appLinks;
StreamSubscription<Uri>? _linkSubscription;
@override
void onReady() {
super.onReady();
_initDeepLinks();
}
@override
void onClose() {
_linkSubscription?.cancel();
}
Future<void> _initDeepLinks() async {
_appLinks = AppLinks();
// Handle link when app is in warm state (front or background)
_linkSubscription = _appLinks.uriLinkStream.listen((uri) async {
await _toCreate(uri);
});
// Check initial link if app was in cold state (terminated)
final uri = await _appLinks.getInitialAppLink();
if (uri != null) {
await _toCreate(uri);
}
}
Future<void> _toCreate(Uri uri) async {
final path = uri.scheme == "magnet"
? uri.toString()
: (await toFile(uri.toString())).path;
await Get.rootDelegate.offAndToNamed(Routes.CREATE, arguments: path);
}
String runningAddress() { String runningAddress() {
if (startConfig.value.network == 'unix') { if (startConfig.value.network == 'unix') {
return startConfig.value.address; return startConfig.value.address;
@@ -1,4 +1,5 @@
import 'package:desktop_drop/desktop_drop.dart'; import 'package:desktop_drop/desktop_drop.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../../../../api/model/resolve_result.dart'; import '../../../../api/model/resolve_result.dart';
@@ -11,8 +12,8 @@ import '../../../../api/model/request.dart';
import '../../../../util/message.dart'; import '../../../../util/message.dart';
import '../../../../util/util.dart'; import '../../../../util/util.dart';
import '../../../routes/app_pages.dart'; import '../../../routes/app_pages.dart';
import '../../../views/views/directory_selector.dart'; import '../../../views/directory_selector.dart';
import '../../../views/views/file_list_view.dart'; import '../../../views/file_list_view.dart';
import '../../app/controllers/app_controller.dart'; import '../../app/controllers/app_controller.dart';
import '../controllers/create_controller.dart'; import '../controllers/create_controller.dart';
@@ -26,6 +27,14 @@ class CreateView extends GetView<CreateController> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final String? filePath = Get.rootDelegate.arguments();
if (_urlController.text.isEmpty && filePath != null) {
_urlController.text = filePath;
WidgetsBinding.instance.addPostFrameCallback((_) async {
await _doResolve();
});
}
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
leading: IconButton( leading: IconButton(
@@ -45,21 +54,42 @@ class CreateView extends GetView<CreateController> {
autovalidateMode: AutovalidateMode.always, autovalidateMode: AutovalidateMode.always,
child: Column( child: Column(
children: [ children: [
TextFormField( Row(
autofocus: true, children: [
controller: _urlController, Expanded(
minLines: 1, child: TextFormField(
maxLines: 30, autofocus: true,
decoration: InputDecoration( controller: _urlController,
hintText: _hitText(), minLines: 1,
hintStyle: const TextStyle(fontSize: 12), maxLines: 30,
labelText: 'downloadLink'.tr, decoration: InputDecoration(
icon: const Icon(Icons.link)), hintText: _hitText(),
validator: (v) { hintStyle: const TextStyle(fontSize: 12),
return v!.trim().isNotEmpty labelText: 'downloadLink'.tr,
? null icon: const Icon(Icons.link),
: 'downloadLinkValid'.tr; suffixIcon: IconButton(
}), onPressed: _urlController.clear,
icon: const Icon(Icons.clear),
),
),
validator: (v) {
return v!.trim().isNotEmpty
? null
: 'downloadLinkValid'.tr;
}),
),
IconButton(
icon: const Icon(Icons.folder_open),
onPressed: () async {
var pr = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ["torrent"]);
if (pr != null) {
_urlController.text = pr.files[0].path ?? "";
}
}),
],
),
Center( Center(
child: Padding( child: Padding(
padding: const EdgeInsets.all(15.0), padding: const EdgeInsets.all(15.0),
@@ -70,21 +100,7 @@ class CreateView extends GetView<CreateController> {
), ),
child: RoundedLoadingButton( child: RoundedLoadingButton(
color: Get.theme.colorScheme.secondary, color: Get.theme.colorScheme.secondary,
onPressed: () async { onPressed: _doResolve,
try {
_confirmController.start();
if (_resolveFormKey.currentState!.validate()) {
final rr = await resolve(Request(
url: _urlController.text,
));
await _showResolveDialog(rr);
}
} catch (e) {
showErrorMessage(e);
} finally {
_confirmController.reset();
}
},
controller: _confirmController, controller: _confirmController,
child: Text('confirm'.tr), child: Text('confirm'.tr),
), ),
@@ -98,6 +114,25 @@ class CreateView extends GetView<CreateController> {
); );
} }
Future<void> _doResolve() async {
if (_confirmController.currentState == ButtonState.loading) {
return;
}
try {
_confirmController.start();
if (_resolveFormKey.currentState!.validate()) {
final rr = await resolve(Request(
url: _urlController.text,
));
await _showResolveDialog(rr);
}
} catch (e) {
showErrorMessage(e);
} finally {
_confirmController.reset();
}
}
String _hitText() { String _hitText() {
return 'downloadLinkHit'.trParams({ return 'downloadLinkHit'.trParams({
'append': Util.isDesktop() ? 'downloadLinkHitDesktop'.tr : '', 'append': Util.isDesktop() ? 'downloadLinkHitDesktop'.tr : '',
@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../../../routes/app_pages.dart'; import '../../../routes/app_pages.dart';
import '../../../views/views/responsive_builder.dart'; import '../../../views/responsive_builder.dart';
import '../controllers/home_controller.dart'; import '../controllers/home_controller.dart';
class HomeView extends GetView<HomeController> { class HomeView extends GetView<HomeController> {
@@ -1,21 +1,21 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:badges/badges.dart' as badges;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../../../../util/message.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
import 'package:badges/badges.dart' as badges;
import '../../../../generated/locales.g.dart'; import '../../../../generated/locales.g.dart';
import '../../../../util/localeManager.dart'; import '../../../../util/localeManager.dart';
import '../../../../util/message.dart';
import '../../../../util/package_info.dart'; import '../../../../util/package_info.dart';
import '../../../../util/util.dart'; import '../../../../util/util.dart';
import '../../../views/views/check_list_view.dart'; import '../../../views/check_list_view.dart';
import '../../../views/views/directory_selector.dart'; import '../../../views/directory_selector.dart';
import '../../../views/views/outlined_button_loading.dart'; import '../../../views/outlined_button_loading.dart';
import '../../app/controllers/app_controller.dart'; import '../../app/controllers/app_controller.dart';
import '../controllers/setting_controller.dart'; import '../controllers/setting_controller.dart';
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../../../views/views/buid_task_list_view.dart'; import '../../../views/buid_task_list_view.dart';
import '../controllers/task_downloaded_controller.dart'; import '../controllers/task_downloaded_controller.dart';
class TaskDownloadedView extends GetView<TaskDownloadedController> { class TaskDownloadedView extends GetView<TaskDownloadedController> {
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../../../views/views/buid_task_list_view.dart'; import '../../../views/buid_task_list_view.dart';
import '../controllers/task_downloading_controller.dart'; import '../controllers/task_downloading_controller.dart';
class TaskDownloadingView extends GetView<TaskDownloadingController> { class TaskDownloadingView extends GetView<TaskDownloadingController> {
@@ -3,14 +3,14 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
import '../../../api/api.dart'; import '../../api/api.dart';
import '../../../api/model/task.dart'; import '../../api/model/task.dart';
import '../../../util/file_explorer.dart'; import '../../util/file_explorer.dart';
import '../../../util/file_icon.dart'; import '../../util/file_icon.dart';
import '../../../util/icons.dart'; import '../../util/icons.dart';
import '../../../util/message.dart'; import '../../util/message.dart';
import '../../../util/util.dart'; import '../../util/util.dart';
import '../../routes/app_pages.dart'; import '../routes/app_pages.dart';
class BuildTaskListView extends GetView { class BuildTaskListView extends GetView {
final List<Task> tasks; final List<Task> tasks;
@@ -2,8 +2,8 @@ import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../../../util/mac_secure_util.dart'; import '../../util/mac_secure_util.dart';
import '../../../util/util.dart'; import '../../util/util.dart';
class DirectorySelector extends StatefulWidget { class DirectorySelector extends StatefulWidget {
final TextEditingController controller; final TextEditingController controller;
@@ -35,19 +35,17 @@ class _DirectorySelectorState extends State<DirectorySelector> {
return v!.trim().isNotEmpty ? null : 'downloadDirValid'.tr; return v!.trim().isNotEmpty ? null : 'downloadDirValid'.tr;
}, },
)), )),
!Util.isDesktop() Util.isDesktop() || Util.isAndroid()
? null ? IconButton(
: IconButton(
icon: const Icon(Icons.folder_open), icon: const Icon(Icons.folder_open),
onPressed: () async { onPressed: () async {
if (GetPlatform.isDesktop) { var dir = await FilePicker.platform.getDirectoryPath();
var dir = await FilePicker.platform.getDirectoryPath(); if (dir != null) {
if (dir != null) { widget.controller.text = dir;
widget.controller.text = dir; MacSecureUtil.saveBookmark(dir);
MacSecureUtil.saveBookmark(dir);
}
} }
}) })
: null
].where((e) => e != null).map((e) => e!).toList(), ].where((e) => e != null).map((e) => e!).toList(),
); );
} }
@@ -3,14 +3,14 @@ import 'dart:ui' as ui;
import 'package:fluent_ui/fluent_ui.dart' as fluent; import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../../../api/model/resource.dart'; import '../../api/model/resource.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
import '../../../util/file_icon.dart'; import '../../util/file_icon.dart';
import '../../../util/icons.dart'; import '../../util/icons.dart';
import '../../../util/util.dart'; import '../../util/util.dart';
import '../../modules/app/controllers/app_controller.dart'; import '../modules/app/controllers/app_controller.dart';
import '../../modules/create/controllers/create_controller.dart'; import '../modules/create/controllers/create_controller.dart';
class FileListView extends GetView { class FileListView extends GetView {
final List<FileInfo> files; final List<FileInfo> files;
+16
View File
@@ -17,6 +17,14 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "5.0.0" version: "5.0.0"
app_links:
dependency: "direct main"
description:
name: app_links
sha256: d572dcdff49c4cfcfa6f315e2683e518ec6eb54e084d01e51d9631a4dcc1b5e8
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.4.2"
archive: archive:
dependency: transitive dependency: transitive
description: description:
@@ -899,6 +907,14 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "1.3.1" version: "1.3.1"
uri_to_file:
dependency: "direct main"
description:
name: uri_to_file
sha256: "84afd633b1492fc465c768141e1a29edd519061bf99935b6b4d0d5de8ec7c108"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.2.0"
url_launcher: url_launcher:
dependency: "direct main" dependency: "direct main"
description: description:
+2
View File
@@ -53,6 +53,8 @@ dependencies:
fluent_ui: ^4.4.0 fluent_ui: ^4.4.0
path: any path: any
badges: ^3.0.3 badges: ^3.0.3
app_links: ^3.4.2
uri_to_file: ^0.2.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter