Skip to content

Commit

Permalink
content: Attempt to open external app for links on Android
Browse files Browse the repository at this point in the history
This is a lot better than the default, though still not ideal.
Comments describe a couple of upstream bugs that we should file
or comment on.
  • Loading branch information
gnprice committed Jul 6, 2023
1 parent 5a6d8df commit 622c7f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
21 changes: 20 additions & 1 deletion lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,32 @@ void _launchUrl(BuildContext context, String urlString) async {
url = store.account.realmUrl.resolve(urlString);
} on FormatException { // TODO(log)
await showError(context, null);
if (!context.mounted) return; // TODO(dart): redundant for sake of lint
return;
}

bool launched = false;
String? errorMessage;
try {
launched = await ZulipBinding.instance.launchUrl(url);
launched = await ZulipBinding.instance.launchUrl(url,
mode: switch (Theme.of(context).platform) {
// TODO(upstream) The url_launcher default on Android is a weird UX:
// opens a webview in-app, but on a blank black background.
// The status bar is hidden:
// https://github.com/flutter/flutter/issues/120883
// but also there's no app bar, no location bar, no share button;
// no browser chrome at all.
// Probably what we really want is a "Chrome custom tab":
// https://github.com/flutter/flutter/issues/18589
// TODO(upstream) With url_launcher's LaunchMode.externalApplication
// on Android, we still don't get the normal Android UX for
// opening a URL in a browser, where the system gives the user
// a bit of UI to choose which browser to use:
// https://github.com/zulip/zulip-flutter/issues/74#issuecomment-1514040730
TargetPlatform.android => LaunchMode.externalApplication,
_ => LaunchMode.platformDefault,
},
);
} on PlatformException catch (e) {
errorMessage = e.message;
}
Expand Down
5 changes: 3 additions & 2 deletions test/widgets/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {
TestZulipBinding.ensureInitialized();

group('LinkNode interactions', () {
const expectedModeAndroid = LaunchMode.platformDefault;
const expectedModeAndroid = LaunchMode.externalApplication;

// The Flutter test font uses square glyphs, so width equals height:
// https://github.com/flutter/flutter/wiki/Flutter-Test-Fonts
Expand All @@ -43,7 +43,8 @@ void main() {
'<p><a href="https://example/">hello</a></p>');

await tester.tap(find.text('hello'));
const expectedMode = LaunchMode.platformDefault;
final expectedMode = defaultTargetPlatform == TargetPlatform.android ?
LaunchMode.externalApplication : LaunchMode.platformDefault;
check(TestZulipBinding.instance.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://example/'), mode: expectedMode));
}, variant: const TargetPlatformVariant({TargetPlatform.android, TargetPlatform.iOS}));
Expand Down

0 comments on commit 622c7f9

Please sign in to comment.