Skip to content

Commit

Permalink
feat(android): ability to add listeners to the Capacitor WebView (#4405)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlpoole authored Apr 8, 2021
1 parent 9ec444f commit 7bdcc15
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
42 changes: 42 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public class Bridge {
// Any URI that was passed to the app on start
private Uri intentUri;

// A list of listeners that trigger when webView events occur
private List<WebViewListener> webViewListeners = new ArrayList<>();

/**
* Create the Bridge with a reference to the main {@link Activity} for the
* app, and a reference to the {@link WebView} our app will use.
Expand Down Expand Up @@ -1107,12 +1110,37 @@ public void setWebViewClient(BridgeWebViewClient client) {
this.webViewClient = client;
}

List<WebViewListener> getWebViewListeners() {
return webViewListeners;
}

void setWebViewListeners(List<WebViewListener> webViewListeners) {
this.webViewListeners = webViewListeners;
}

/**
* Add a listener that the WebViewClient can trigger on certain events.
* @param webViewListener A {@link WebViewListener} to add.
*/
public void addWebViewListener(WebViewListener webViewListener) {
webViewListeners.add(webViewListener);
}

/**
* Remove a listener that the WebViewClient triggers on certain events.
* @param webViewListener A {@link WebViewListener} to remove.
*/
public void removeWebViewListener(WebViewListener webViewListener) {
webViewListeners.remove(webViewListener);
}

static class Builder {

private Bundle instanceState = null;
private CapConfig config = null;
private List<Class<? extends Plugin>> plugins = new ArrayList<>();
private AppCompatActivity activity;
private final List<WebViewListener> webViewListeners = new ArrayList<>();

Builder(AppCompatActivity activity) {
this.activity = activity;
Expand Down Expand Up @@ -1146,6 +1174,19 @@ public Builder addPlugins(List<Class<? extends Plugin>> plugins) {
return this;
}

public Builder addWebViewListener(WebViewListener webViewListener) {
webViewListeners.add(webViewListener);
return this;
}

public Builder addWebViewListeners(List<WebViewListener> webViewListeners) {
for (WebViewListener listener : webViewListeners) {
this.addWebViewListener(listener);
}

return this;
}

public Bridge create() {
// Cordova initialization
ConfigXmlParser parser = new ConfigXmlParser();
Expand All @@ -1168,6 +1209,7 @@ public Bridge create() {
// Bridge initialization
Bridge bridge = new Bridge(activity, webView, plugins, cordovaInterface, pluginManager, preferences, config);
bridge.setCordovaWebView(mockWebView);
bridge.setWebViewListeners(webViewListeners);

if (instanceState != null) {
bridge.restoreInstanceState(instanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class BridgeFragment extends Fragment {
private final List<Class<? extends Plugin>> initialPlugins = new ArrayList<>();
private CapConfig config = null;

private final List<WebViewListener> webViewListeners = new ArrayList<>();

public BridgeFragment() {
// Required empty public constructor
}
Expand Down Expand Up @@ -55,6 +57,10 @@ public void setConfig(CapConfig config) {
this.config = config;
}

public void addWebViewListener(WebViewListener webViewListener) {
webViewListeners.add(webViewListener);
}

/**
* Load the WebView and create the Bridge
*/
Expand All @@ -73,6 +79,7 @@ protected void load(Bundle savedInstanceState) {
.setInstanceState(savedInstanceState)
.setPlugins(initialPlugins)
.setConfig(config)
.addWebViewListeners(webViewListeners)
.create();

if (startDir != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.List;

public class BridgeWebViewClient extends WebViewClient {

Expand All @@ -29,4 +30,17 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return bridge.launchIntent(Uri.parse(url));
}

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);

List<WebViewListener> webViewListeners = bridge.getWebViewListeners();

if (webViewListeners != null && view.getProgress() == 100) {
for (WebViewListener listener : bridge.getWebViewListeners()) {
listener.onPageLoaded(view);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.getcapacitor;

import android.webkit.WebView;

/**
* Provides callbacks associated with the {@link BridgeWebViewClient}
*/
public abstract class WebViewListener {

/**
* Callback for page load event.
*
* @param webView The WebView that loaded
*/
public void onPageLoaded(WebView webView) {
// Override me to add behavior to the page loaded event
}
}

0 comments on commit 7bdcc15

Please sign in to comment.