Skip to content

Commit

Permalink
[url_launcher] Add InAppBrowserConfiguration parameter in implement…
Browse files Browse the repository at this point in the history
…ations (#5759)

Platform implementation portion of #5166. Implements `InAppBrowserConfiguration` support on Android, as well as support for `InAppBrowserConfiguration.showTitle` parameter for hiding/showing webpage title in Android Custom Tabs.
  • Loading branch information
Alex-Usmanov authored Feb 13, 2024
1 parent 49e3f1c commit 1bc9fee
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 54 deletions.
4 changes: 3 additions & 1 deletion packages/url_launcher/url_launcher_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT
## 6.3.0

* Adds support for `BrowserConfiguration`.
* Implements `showTitle` functionality for Android Custom Tabs.
* Updates compileSdk version to 34.

## 6.2.3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Autogenerated from Pigeon (v10.1.4), do not edit directly.
// Autogenerated from Pigeon (v10.1.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package io.flutter.plugins.urllauncher;
Expand Down Expand Up @@ -156,6 +156,55 @@ ArrayList<Object> toList() {
}
}

/** Generated class from Pigeon that represents data sent in messages. */
public static final class BrowserOptions {
private @NonNull Boolean showTitle;

public @NonNull Boolean getShowTitle() {
return showTitle;
}

public void setShowTitle(@NonNull Boolean setterArg) {
if (setterArg == null) {
throw new IllegalStateException("Nonnull field \"showTitle\" is null.");
}
this.showTitle = setterArg;
}

/** Constructor is non-public to enforce null safety; use Builder. */
BrowserOptions() {}

public static final class Builder {

private @Nullable Boolean showTitle;

public @NonNull Builder setShowTitle(@NonNull Boolean setterArg) {
this.showTitle = setterArg;
return this;
}

public @NonNull BrowserOptions build() {
BrowserOptions pigeonReturn = new BrowserOptions();
pigeonReturn.setShowTitle(showTitle);
return pigeonReturn;
}
}

@NonNull
ArrayList<Object> toList() {
ArrayList<Object> toListResult = new ArrayList<Object>(1);
toListResult.add(showTitle);
return toListResult;
}

static @NonNull BrowserOptions fromList(@NonNull ArrayList<Object> list) {
BrowserOptions pigeonResult = new BrowserOptions();
Object showTitle = list.get(0);
pigeonResult.setShowTitle((Boolean) showTitle);
return pigeonResult;
}
}

private static class UrlLauncherApiCodec extends StandardMessageCodec {
public static final UrlLauncherApiCodec INSTANCE = new UrlLauncherApiCodec();

Expand All @@ -165,6 +214,8 @@ private UrlLauncherApiCodec() {}
protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {
switch (type) {
case (byte) 128:
return BrowserOptions.fromList((ArrayList<Object>) readValue(buffer));
case (byte) 129:
return WebViewOptions.fromList((ArrayList<Object>) readValue(buffer));
default:
return super.readValueOfType(type, buffer);
Expand All @@ -173,8 +224,11 @@ protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {

@Override
protected void writeValue(@NonNull ByteArrayOutputStream stream, Object value) {
if (value instanceof WebViewOptions) {
if (value instanceof BrowserOptions) {
stream.write(128);
writeValue(stream, ((BrowserOptions) value).toList());
} else if (value instanceof WebViewOptions) {
stream.write(129);
writeValue(stream, ((WebViewOptions) value).toList());
} else {
super.writeValue(stream, value);
Expand All @@ -190,12 +244,13 @@ public interface UrlLauncherApi {
/** Opens the URL externally, returning true if successful. */
@NonNull
Boolean launchUrl(@NonNull String url, @NonNull Map<String, String> headers);
/**
* Opens the URL in an in-app Custom Tab or WebView, returning true if it opens successfully.
*/
/** Opens the URL in an in-app WebView, returning true if it opens successfully. */
@NonNull
Boolean openUrlInApp(
@NonNull String url, @NonNull Boolean allowCustomTab, @NonNull WebViewOptions options);
@NonNull String url,
@NonNull Boolean allowCustomTab,
@NonNull WebViewOptions webViewOptions,
@NonNull BrowserOptions browserOptions);

@NonNull
Boolean supportsCustomTabs();
Expand Down Expand Up @@ -272,9 +327,12 @@ static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable UrlLaunche
ArrayList<Object> args = (ArrayList<Object>) message;
String urlArg = (String) args.get(0);
Boolean allowCustomTabArg = (Boolean) args.get(1);
WebViewOptions optionsArg = (WebViewOptions) args.get(2);
WebViewOptions webViewOptionsArg = (WebViewOptions) args.get(2);
BrowserOptions browserOptionsArg = (BrowserOptions) args.get(3);
try {
Boolean output = api.openUrlInApp(urlArg, allowCustomTabArg, optionsArg);
Boolean output =
api.openUrlInApp(
urlArg, allowCustomTabArg, webViewOptionsArg, browserOptionsArg);
wrapped.add(0, output);
} catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import androidx.annotation.VisibleForTesting;
import androidx.browser.customtabs.CustomTabsClient;
import androidx.browser.customtabs.CustomTabsIntent;
import io.flutter.plugins.urllauncher.Messages.BrowserOptions;
import io.flutter.plugins.urllauncher.Messages.UrlLauncherApi;
import io.flutter.plugins.urllauncher.Messages.WebViewOptions;
import java.util.Collections;
Expand Down Expand Up @@ -98,17 +99,20 @@ void setActivity(@Nullable Activity activity) {

@Override
public @NonNull Boolean openUrlInApp(
@NonNull String url, @NonNull Boolean allowCustomTab, @NonNull WebViewOptions options) {
@NonNull String url,
@NonNull Boolean allowCustomTab,
@NonNull WebViewOptions webViewOptions,
@NonNull BrowserOptions browserOptions) {
ensureActivity();
assert activity != null;

Bundle headersBundle = extractBundle(options.getHeaders());
Bundle headersBundle = extractBundle(webViewOptions.getHeaders());

// Try to launch using Custom Tabs if they have the necessary functionality, unless the caller
// specifically requested a web view.
if (allowCustomTab && !containsRestrictedHeader(options.getHeaders())) {
if (allowCustomTab && !containsRestrictedHeader(webViewOptions.getHeaders())) {
Uri uri = Uri.parse(url);
if (openCustomTab(activity, uri, headersBundle)) {
if (openCustomTab(activity, uri, headersBundle, browserOptions)) {
return true;
}
}
Expand All @@ -118,8 +122,8 @@ void setActivity(@Nullable Activity activity) {
WebViewActivity.createIntent(
activity,
url,
options.getEnableJavaScript(),
options.getEnableDomStorage(),
webViewOptions.getEnableJavaScript(),
webViewOptions.getEnableDomStorage(),
headersBundle);
try {
activity.startActivity(launchIntent);
Expand All @@ -141,9 +145,14 @@ public void closeWebView() {
}

private static boolean openCustomTab(
@NonNull Context context, @NonNull Uri uri, @NonNull Bundle headersBundle) {
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
@NonNull Context context,
@NonNull Uri uri,
@NonNull Bundle headersBundle,
@NonNull BrowserOptions options) {
CustomTabsIntent customTabsIntent =
new CustomTabsIntent.Builder().setShowTitle(options.getShowTitle()).build();
customTabsIntent.intent.putExtra(Browser.EXTRA_HEADERS, headersBundle);

try {
customTabsIntent.launchUrl(context, uri);
} catch (ActivityNotFoundException ex) {
Expand Down
Loading

0 comments on commit 1bc9fee

Please sign in to comment.