implement open in app feature
This commit is contained in:
@@ -8,6 +8,7 @@ export 'src/data/models/load_url_flags.dart';
|
||||
export 'src/data/models/source.dart';
|
||||
export 'src/domain/entities/default_selection_actions.dart';
|
||||
export 'src/domain/services/gecko_addon.dart';
|
||||
export 'src/domain/services/gecko_app_links.dart';
|
||||
export 'src/domain/services/gecko_bookmarks.dart';
|
||||
export 'src/domain/services/gecko_browser.dart';
|
||||
export 'src/domain/services/gecko_browser_extension.dart';
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import 'package:flutter_mozilla_components/src/pigeons/gecko.g.dart';
|
||||
|
||||
final _api = GeckoAppLinksApi();
|
||||
|
||||
/// Service for detecting and launching external applications that can handle URLs.
|
||||
///
|
||||
/// This service wraps Mozilla Android Components' AppLinksUseCases to allow
|
||||
/// checking if native apps can handle URLs and launching them directly.
|
||||
/// This matches the behavior in Firefox/Fenix for "Open in App" functionality.
|
||||
class GeckoAppLinksService {
|
||||
/// Checks if an external application is available to handle the given URL.
|
||||
///
|
||||
/// This method uses mozilla-components AppLinksUseCases to determine if
|
||||
/// a native app can handle the URL (e.g., YouTube app for youtube.com links).
|
||||
///
|
||||
/// @param url The URL to check.
|
||||
/// @return true if an external app is available, false otherwise.
|
||||
Future<bool> hasExternalApp(Uri url) {
|
||||
return _api.hasExternalApp(url.toString());
|
||||
}
|
||||
|
||||
/// Opens the URL in an external application if available.
|
||||
///
|
||||
/// This method will:
|
||||
/// 1. Check if an external app can handle the URL
|
||||
/// 2. If available, launch the app directly with Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
/// 3. Return true if successfully launched, false otherwise
|
||||
///
|
||||
/// @param url The URL to open in external app.
|
||||
/// @return true if URL was opened in external app, false if no app available.
|
||||
Future<bool> openAppLink(Uri url) {
|
||||
return _api.openAppLink(url.toString());
|
||||
}
|
||||
}
|
||||
@@ -8272,3 +8272,89 @@ class GeckoTrackingProtectionApi {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// API for detecting and launching external applications that can handle URLs.
|
||||
///
|
||||
/// This API wraps Mozilla Android Components' AppLinksUseCases to allow Flutter
|
||||
/// code to check if native apps can handle URLs and launch them directly.
|
||||
class GeckoAppLinksApi {
|
||||
/// Constructor for [GeckoAppLinksApi]. The [binaryMessenger] named argument is
|
||||
/// available for dependency injection. If it is left null, the default
|
||||
/// BinaryMessenger will be used which routes to the host platform.
|
||||
GeckoAppLinksApi({BinaryMessenger? binaryMessenger, String messageChannelSuffix = ''})
|
||||
: pigeonVar_binaryMessenger = binaryMessenger,
|
||||
pigeonVar_messageChannelSuffix = messageChannelSuffix.isNotEmpty ? '.$messageChannelSuffix' : '';
|
||||
final BinaryMessenger? pigeonVar_binaryMessenger;
|
||||
|
||||
static const MessageCodec<Object?> pigeonChannelCodec = _PigeonCodec();
|
||||
|
||||
final String pigeonVar_messageChannelSuffix;
|
||||
|
||||
/// Checks if an external application is available to handle the given URL.
|
||||
///
|
||||
/// This method uses mozilla-components AppLinksUseCases to determine if
|
||||
/// a native app can handle the URL (e.g., YouTube app for youtube.com links).
|
||||
///
|
||||
/// Returns true if an external app is available, false otherwise.
|
||||
Future<bool> hasExternalApp(String url) async {
|
||||
final pigeonVar_channelName = 'dev.flutter.pigeon.flutter_mozilla_components.GeckoAppLinksApi.hasExternalApp$pigeonVar_messageChannelSuffix';
|
||||
final pigeonVar_channel = BasicMessageChannel<Object?>(
|
||||
pigeonVar_channelName,
|
||||
pigeonChannelCodec,
|
||||
binaryMessenger: pigeonVar_binaryMessenger,
|
||||
);
|
||||
final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(<Object?>[url]);
|
||||
final pigeonVar_replyList = await pigeonVar_sendFuture as List<Object?>?;
|
||||
if (pigeonVar_replyList == null) {
|
||||
throw _createConnectionError(pigeonVar_channelName);
|
||||
} else if (pigeonVar_replyList.length > 1) {
|
||||
throw PlatformException(
|
||||
code: pigeonVar_replyList[0]! as String,
|
||||
message: pigeonVar_replyList[1] as String?,
|
||||
details: pigeonVar_replyList[2],
|
||||
);
|
||||
} else if (pigeonVar_replyList[0] == null) {
|
||||
throw PlatformException(
|
||||
code: 'null-error',
|
||||
message: 'Host platform returned null value for non-null return value.',
|
||||
);
|
||||
} else {
|
||||
return (pigeonVar_replyList[0] as bool?)!;
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the URL in an external application if available.
|
||||
///
|
||||
/// This method will:
|
||||
/// 1. Check if an external app can handle the URL
|
||||
/// 2. If available, launch the app directly with Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
/// 3. Return true if successfully launched, false otherwise
|
||||
///
|
||||
/// Returns true if URL was opened in external app, false if no app available.
|
||||
Future<bool> openAppLink(String url) async {
|
||||
final pigeonVar_channelName = 'dev.flutter.pigeon.flutter_mozilla_components.GeckoAppLinksApi.openAppLink$pigeonVar_messageChannelSuffix';
|
||||
final pigeonVar_channel = BasicMessageChannel<Object?>(
|
||||
pigeonVar_channelName,
|
||||
pigeonChannelCodec,
|
||||
binaryMessenger: pigeonVar_binaryMessenger,
|
||||
);
|
||||
final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(<Object?>[url]);
|
||||
final pigeonVar_replyList = await pigeonVar_sendFuture as List<Object?>?;
|
||||
if (pigeonVar_replyList == null) {
|
||||
throw _createConnectionError(pigeonVar_channelName);
|
||||
} else if (pigeonVar_replyList.length > 1) {
|
||||
throw PlatformException(
|
||||
code: pigeonVar_replyList[0]! as String,
|
||||
message: pigeonVar_replyList[1] as String?,
|
||||
details: pigeonVar_replyList[2],
|
||||
);
|
||||
} else if (pigeonVar_replyList[0] == null) {
|
||||
throw PlatformException(
|
||||
code: 'null-error',
|
||||
message: 'Host platform returned null value for non-null return value.',
|
||||
);
|
||||
} else {
|
||||
return (pigeonVar_replyList[0] as bool?)!;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user