Skip to content

Commit eed8df7

Browse files
authored
[url_launcher] Android API 34 support (#4660)
fixed [#flutter/flutter/issues/126460 ](flutter/flutter#126460) Reland of #3973 which incorrectly used application context when registering and not when unregistering. This pr uses activity context for both which is aligned with what the behavior was before. - Register on the same context as unregister - Add integration test for android url launch and close Tested with manual test and new integration test.
1 parent 34683ac commit eed8df7

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

packages/url_launcher/url_launcher_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.0.38
2+
3+
* Updates android implementation to support api 34 broadcast receiver requirements.
4+
15
## 6.0.37
26

37
* Sets android.defaults.buildfeatures.buildconfig to true for compatibility with AGP 8.0+.

packages/url_launcher/url_launcher_android/android/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ android {
6262
}
6363

6464
dependencies {
65+
66+
// Java language implementation
67+
implementation "androidx.core:core:1.10.1"
6568
implementation 'androidx.annotation:annotation:1.6.0'
6669
testImplementation 'junit:junit:4.13.2'
6770
testImplementation 'org.mockito:mockito-core:5.1.1'

packages/url_launcher/url_launcher_android/android/src/main/java/io/flutter/plugins/urllauncher/WebViewActivity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import androidx.annotation.Nullable;
2424
import androidx.annotation.RequiresApi;
2525
import androidx.annotation.VisibleForTesting;
26+
import androidx.core.content.ContextCompat;
2627
import java.util.Collections;
2728
import java.util.HashMap;
2829
import java.util.Map;
@@ -143,7 +144,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
143144
webview.setWebChromeClient(new FlutterWebChromeClient());
144145

145146
// Register receiver that may finish this Activity.
146-
registerReceiver(broadcastReceiver, closeIntentFilter);
147+
ContextCompat.registerReceiver(
148+
this, broadcastReceiver, closeIntentFilter, ContextCompat.RECEIVER_EXPORTED);
147149
}
148150

149151
@VisibleForTesting

packages/url_launcher/url_launcher_android/example/integration_test/url_launcher_test.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:async';
6+
import 'dart:io';
7+
58
import 'package:flutter_test/flutter_test.dart';
69
import 'package:integration_test/integration_test.dart';
710
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
@@ -20,4 +23,41 @@ void main() {
2023
// sms:, tel:, and mailto: links may not be openable on every device, so
2124
// aren't tested here.
2225
});
26+
27+
testWidgets('launch and close', (WidgetTester _) async {
28+
final UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
29+
30+
// Setup fake http server.
31+
final HttpServer server = await HttpServer.bind(InternetAddress.anyIPv4, 0);
32+
unawaited(server.forEach((HttpRequest request) {
33+
if (request.uri.path == '/hello.txt') {
34+
request.response.writeln('Hello, world.');
35+
} else {
36+
fail('unexpected request: ${request.method} ${request.uri}');
37+
}
38+
request.response.close();
39+
}));
40+
// Https to avoid cleartext warning on android.
41+
final String prefixUrl = 'https://${server.address.address}:${server.port}';
42+
final String primaryUrl = '$prefixUrl/hello.txt';
43+
44+
// Launch a url then close.
45+
expect(
46+
await launcher.launch(primaryUrl,
47+
useSafariVC: true,
48+
useWebView: true,
49+
enableJavaScript: false,
50+
enableDomStorage: false,
51+
universalLinksOnly: false,
52+
headers: <String, String>{}),
53+
true);
54+
await launcher.closeWebView();
55+
// Delay required to catch android side crashes in onDestroy.
56+
//
57+
// If this test flakes with an android crash during this delay the test
58+
// should be considered failing because this integration test can have a
59+
// false positive pass if the test closes before an onDestroy crash.
60+
// See https://github.com/flutter/flutter/issues/126460 for more info.
61+
await Future<void>.delayed(const Duration(seconds: 5));
62+
});
2363
}

packages/url_launcher/url_launcher_android/pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ name: url_launcher_android
22
description: Android implementation of the url_launcher plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/url_launcher/url_launcher_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
5-
version: 6.0.37
6-
5+
version: 6.0.38
76
environment:
87
sdk: ">=2.18.0 <4.0.0"
98
flutter: ">=3.3.0"

0 commit comments

Comments
 (0)