Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update Dart
  • Loading branch information
stuartmorgan-g committed May 19, 2023
commit 67b825fe5a46f8d5473281a5434ea1dcfdcae410
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/foundation.dart' show visibleForTesting;
import 'package:flutter/services.dart';
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

const MethodChannel _channel =
MethodChannel('plugins.flutter.io/url_launcher_android');
import 'src/messages.g.dart';

/// An implementation of [UrlLauncherPlatform] for Android.
class UrlLauncherAndroid extends UrlLauncherPlatform {
/// Creates a new plugin implementation instance.
UrlLauncherAndroid({
@visibleForTesting UrlLauncherApi? api,
}) : _hostApi = api ?? UrlLauncherApi();

final UrlLauncherApi _hostApi;

/// Registers this class as the default instance of [UrlLauncherPlatform].
static void registerWith() {
UrlLauncherPlatform.instance = UrlLauncherAndroid();
Expand All @@ -23,7 +28,7 @@ class UrlLauncherAndroid extends UrlLauncherPlatform {

@override
Future<bool> canLaunch(String url) async {
final bool canLaunchSpecificUrl = await _canLaunchUrl(url);
final bool canLaunchSpecificUrl = await _hostApi.canLaunchUrl(url);
if (!canLaunchSpecificUrl) {
final String scheme = _getUrlScheme(url);
// canLaunch can return false when a custom application is registered to
Expand All @@ -33,24 +38,19 @@ class UrlLauncherAndroid extends UrlLauncherPlatform {
// returns true, then there is a browser, which means that there is
// at least one handler for the original URL.
if (scheme == 'http' || scheme == 'https') {
return _canLaunchUrl('$scheme://flutter.dev');
return _hostApi.canLaunchUrl('$scheme://flutter.dev');
}
}
return canLaunchSpecificUrl;
}

Future<bool> _canLaunchUrl(String url) {
return _channel.invokeMethod<bool>(
'canLaunch',
<String, Object>{'url': url},
).then((bool? value) => value ?? false);
}

@override
Future<void> closeWebView() {
return _channel.invokeMethod<void>('closeWebView');
return _hostApi.closeWebView();
}

// TODO(stuartmorgan): Implement launchUrl, and make this a passthrough
// to launchUrl. See also https://github.com/flutter/flutter/issues/66721
@override
Future<bool> launch(
String url, {
Expand All @@ -61,18 +61,35 @@ class UrlLauncherAndroid extends UrlLauncherPlatform {
required bool universalLinksOnly,
required Map<String, String> headers,
String? webOnlyWindowName,
}) {
return _channel.invokeMethod<bool>(
'launch',
<String, Object>{
'url': url,
'useWebView': useWebView,
'enableJavaScript': enableJavaScript,
'enableDomStorage': enableDomStorage,
'universalLinksOnly': universalLinksOnly,
'headers': headers,
},
).then((bool? value) => value ?? false);
}) async {
final LaunchStatus status;
if (useWebView) {
status = (await _hostApi.openUrlInWebView(
url,
WebViewOptions(
enableJavaScript: enableJavaScript,
enableDomStorage: enableDomStorage,
headers: headers)))
.value;
} else {
status = (await _hostApi.launchUrl(url, headers)).value;
}
// TODO(stuartmorgan): Convert noHandlingActivity to returning false, and
// noCurrentActivity to a more generic structured error, as part of a
// breaking change to rework failure handling across all platform. The
// current behavior is backwards compatible with previous Java errors.
switch (status) {
case LaunchStatus.success:
return true;
case LaunchStatus.noCurrentActivity:
throw PlatformException(
code: 'NO_ACTIVITY',
message: 'Launching a URL requires a foreground activity.');
case LaunchStatus.noHandlingActivity:
throw PlatformException(
code: 'ACTIVITY_NOT_FOUND',
message: 'No Activity found to handle intent { $url }');
}
}

// Returns the part of [url] up to the first ':', or an empty string if there
Expand Down
Loading