prepare for multiple apps
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'package:fast_equatable/fast_equatable.dart';
|
||||
import 'package:weblibre/utils/input_classification.dart';
|
||||
|
||||
sealed class SharedContent with FastEquatable {
|
||||
final String? contextId;
|
||||
|
||||
SharedContent({this.contextId});
|
||||
|
||||
factory SharedContent.parse(String content, {String? contextId}) {
|
||||
if (parseSharedIntentUrl(content) case final Uri uri) {
|
||||
return SharedUrl(uri, contextId: contextId);
|
||||
} else {
|
||||
return SharedText(content, contextId: contextId);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get hashParameters => [contextId];
|
||||
}
|
||||
|
||||
final class SharedUrl extends SharedContent {
|
||||
final Uri url;
|
||||
|
||||
SharedUrl(this.url, {super.contextId});
|
||||
|
||||
@override
|
||||
String toString() => url.toString();
|
||||
|
||||
@override
|
||||
List<Object?> get hashParameters => [url, ...super.hashParameters];
|
||||
}
|
||||
|
||||
final class SharedText extends SharedContent {
|
||||
final String text;
|
||||
|
||||
SharedText(this.text, {super.contextId});
|
||||
|
||||
@override
|
||||
String toString() => text;
|
||||
|
||||
@override
|
||||
List<Object?> get hashParameters => [text, ...super.hashParameters];
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:mime/mime.dart' as mime;
|
||||
import 'package:nullability/nullability.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:simple_intent_receiver/simple_intent_receiver.dart';
|
||||
import 'package:uri_to_file/uri_to_file.dart' as uri_to_file;
|
||||
import 'package:weblibre/core/logger.dart';
|
||||
import 'package:weblibre/data/models/received_intent_parameter.dart';
|
||||
|
||||
part 'sharing_intent.g.dart';
|
||||
|
||||
final _sharingIntentTransformer =
|
||||
StreamTransformer<Intent, ReceivedIntentParameter>.fromHandlers(
|
||||
handleData: (intent, sink) async {
|
||||
final data = switch (intent.action) {
|
||||
'android.intent.action.PROCESS_TEXT' =>
|
||||
intent.extra['android.intent.extra.PROCESS_TEXT'] as String?,
|
||||
'android.intent.action.WEB_SEARCH' =>
|
||||
intent.extra['query'] as String?,
|
||||
'android.intent.action.VIEW' => intent.data,
|
||||
'android.intent.action.SEND' =>
|
||||
intent.extra['android.intent.extra.STREAM'] as String? ??
|
||||
intent.extra['android.intent.extra.TEXT'] as String?,
|
||||
_ => null,
|
||||
};
|
||||
|
||||
// Extract container context from shortcut intents
|
||||
final contextId =
|
||||
intent.action == 'android.intent.action.VIEW'
|
||||
? intent.extra['pwa_context_id'] as String?
|
||||
: null;
|
||||
|
||||
if (data != null) {
|
||||
if (uri_to_file.isUriSupported(data)) {
|
||||
var path = data;
|
||||
if (p.extension(data).whenNotEmpty == null) {
|
||||
if (intent.mimeType.whenNotEmpty != null) {
|
||||
final ext = mime.extensionFromMime(intent.mimeType!);
|
||||
if (ext != null) {
|
||||
path = p.setExtension(path, '.$ext');
|
||||
} else {
|
||||
logger.w(
|
||||
'Could not determine file extension for: ${intent.mimeType}',
|
||||
);
|
||||
}
|
||||
} else {
|
||||
logger.w(
|
||||
'Received intent without extension and mime type $path',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
final file = await uri_to_file.toFile(path);
|
||||
final mimeType = mime.lookupMimeType(file.path);
|
||||
switch (mimeType) {
|
||||
case 'application/pdf':
|
||||
sink.add(
|
||||
ReceivedIntentParameter(path, null, contextId: contextId),
|
||||
);
|
||||
default:
|
||||
logger.w('Unhandled mime type: $mimeType');
|
||||
}
|
||||
} catch (e) {
|
||||
logger.e('Failed to convert URI to file: $e');
|
||||
// Fallback: pass the original URI
|
||||
sink.add(
|
||||
ReceivedIntentParameter(data, null, contextId: contextId),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
sink.add(
|
||||
ReceivedIntentParameter(data, null, contextId: contextId),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
Raw<Stream<ReceivedIntentParameter>> sharingIntentStream(Ref ref) {
|
||||
final receiver = IntentReceiver.setUp();
|
||||
|
||||
return receiver.events.transform(_sharingIntentTransformer);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'sharing_intent.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(sharingIntentStream)
|
||||
final sharingIntentStreamProvider = SharingIntentStreamProvider._();
|
||||
|
||||
final class SharingIntentStreamProvider
|
||||
extends
|
||||
$FunctionalProvider<
|
||||
Raw<Stream<ReceivedIntentParameter>>,
|
||||
Raw<Stream<ReceivedIntentParameter>>,
|
||||
Raw<Stream<ReceivedIntentParameter>>
|
||||
>
|
||||
with $Provider<Raw<Stream<ReceivedIntentParameter>>> {
|
||||
SharingIntentStreamProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'sharingIntentStreamProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$sharingIntentStreamHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<Raw<Stream<ReceivedIntentParameter>>> $createElement(
|
||||
$ProviderPointer pointer,
|
||||
) => $ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
Raw<Stream<ReceivedIntentParameter>> create(Ref ref) {
|
||||
return sharingIntentStream(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(Raw<Stream<ReceivedIntentParameter>> value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride:
|
||||
$SyncValueProvider<Raw<Stream<ReceivedIntentParameter>>>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$sharingIntentStreamHash() =>
|
||||
r'486f994fc0e01a2cffdb19d93f0a332f96a9850c';
|
||||
Reference in New Issue
Block a user