pwa improvements: custom name and context selection

This commit is contained in:
Fabian Freund
2026-04-20 12:20:55 +02:00
parent 7af0f1d4e3
commit b699ef5fba
23 changed files with 1220 additions and 439 deletions
@@ -0,0 +1,49 @@
/*
* 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/>.
*/
enum IntentContainerMode {
useSelected('use-selected'),
unassigned('unassigned'),
specific('specific');
const IntentContainerMode(this.wireValue);
final String wireValue;
String? get queryValueOrNull =>
this == IntentContainerMode.useSelected ? null : wireValue;
static IntentContainerMode fromWireValue(
String? wireValue, {
String? contextId,
}) {
for (final mode in values) {
if (mode.wireValue == wireValue) {
return mode;
}
}
if (contextId != null) {
return IntentContainerMode.specific;
}
return IntentContainerMode.useSelected;
}
}
@@ -18,29 +18,42 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:fast_equatable/fast_equatable.dart';
import 'package:weblibre/features/share_intent/domain/entities/intent_container_mode.dart';
import 'package:weblibre/utils/input_classification.dart';
sealed class SharedContent with FastEquatable {
final String? contextId;
final IntentContainerMode containerMode;
SharedContent({this.contextId});
SharedContent({
this.contextId,
this.containerMode = IntentContainerMode.useSelected,
});
factory SharedContent.parse(String content, {String? contextId}) {
factory SharedContent.parse(
String content, {
String? contextId,
IntentContainerMode containerMode = IntentContainerMode.useSelected,
}) {
if (parseSharedIntentUrl(content) case final Uri uri) {
return SharedUrl(uri, contextId: contextId);
return SharedUrl(uri, contextId: contextId, containerMode: containerMode);
} else {
return SharedText(content, contextId: contextId);
return SharedText(
content,
contextId: contextId,
containerMode: containerMode,
);
}
}
@override
List<Object?> get hashParameters => [contextId];
List<Object?> get hashParameters => [contextId, containerMode];
}
final class SharedUrl extends SharedContent {
final Uri url;
SharedUrl(this.url, {super.contextId});
SharedUrl(this.url, {super.contextId, super.containerMode});
@override
String toString() => url.toString();
@@ -52,7 +65,7 @@ final class SharedUrl extends SharedContent {
final class SharedText extends SharedContent {
final String text;
SharedText(this.text, {super.contextId});
SharedText(this.text, {super.contextId, super.containerMode});
@override
String toString() => text;
@@ -28,93 +28,109 @@ 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';
import 'package:weblibre/features/intent_gatekeeper/domain/services/intent_gatekeeper.dart';
import 'package:weblibre/features/share_intent/domain/entities/intent_container_mode.dart';
part 'sharing_intent.g.dart';
StreamTransformer<Intent, ReceivedIntentParameter>
_buildSharingIntentTransformer(IntentGatekeeper gatekeeper) =>
StreamTransformer<Intent, ReceivedIntentParameter>.fromHandlers(
handleData: (intent, sink) async {
// PWA shortcut intents carry our own signed context id — always allow.
final pwaContextId =
intent.action == 'android.intent.action.VIEW'
? intent.extra['pwa_context_id'] as String?
: null;
_buildSharingIntentTransformer(
IntentGatekeeper gatekeeper,
) => StreamTransformer<Intent, ReceivedIntentParameter>.fromHandlers(
handleData: (intent, sink) async {
final shortcutContextId = intent.action == 'android.intent.action.VIEW'
? intent.extra['pwa_context_id'] as String?
: null;
final containerMode = intent.action == 'android.intent.action.VIEW'
? IntentContainerMode.fromWireValue(
intent.extra['shortcut_container_mode'] as String?,
contextId: shortcutContextId,
)
: IntentContainerMode.useSelected;
if (pwaContextId == null) {
final allowed = await gatekeeper.shouldAllow(
fromPackageName: intent.fromPackageName,
url: intent.data,
);
if (!allowed) {
logger.i(
'Blocked intent from ${intent.fromPackageName ?? 'unknown app'}',
);
return;
}
}
final allowed = await gatekeeper.shouldAllow(
fromPackageName: intent.fromPackageName,
url: intent.data,
);
if (!allowed) {
logger.i(
'Blocked intent from ${intent.fromPackageName ?? 'unknown app'}',
);
return;
}
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,
};
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 = pwaContextId;
// Extract container context from shortcut intents.
final contextId = shortcutContextId;
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),
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 {
sink.add(
ReceivedIntentParameter(data, null, contextId: contextId),
);
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,
containerMode: containerMode,
),
);
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,
containerMode: containerMode,
),
);
}
} else {
sink.add(
ReceivedIntentParameter(
data,
null,
contextId: contextId,
containerMode: containerMode,
),
);
}
}
},
);
@Riverpod(keepAlive: true)
Raw<Stream<ReceivedIntentParameter>> sharingIntentStream(Ref ref) {