-
Notifications
You must be signed in to change notification settings - Fork 2
/
CustomWebViewManager.java
82 lines (66 loc) · 2.67 KB
/
CustomWebViewManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.overridenativeconfig;
import android.support.annotation.Nullable;
import android.webkit.WebView;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.views.webview.ReactWebViewManager;
import java.util.Map;
@ReactModule(name = CustomWebViewManager.REACT_CLASS)
public class CustomWebViewManager extends ReactWebViewManager {
/* This name must match what we're referring to in JS */
protected static final String REACT_CLASS = "RCTCustomWebView";
protected static class CustomWebViewClient extends ReactWebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean shouldOverride = super.shouldOverrideUrlLoading(view, url);
String finalUrl = ((CustomWebView) view).getFinalUrl();
if (!shouldOverride && url != null && finalUrl != null && new String(url).equals(finalUrl)) {
final WritableMap params = Arguments.createMap();
dispatchEvent(view, new NavigationCompletedEvent(view.getId(), params));
}
return shouldOverride;
}
}
protected static class CustomWebView extends ReactWebView {
public CustomWebView(ThemedReactContext reactContext) {
super(reactContext);
}
protected @Nullable String mFinalUrl;
public void setFinalUrl(String url) {
mFinalUrl = url;
}
public String getFinalUrl() {
return mFinalUrl;
}
}
@ReactProp(name = "finalUrl")
public void setFinalUrl(WebView view, String url) {
((CustomWebView) view).setFinalUrl(url);
}
@Override
protected ReactWebView createReactWebViewInstance(ThemedReactContext reactContext) {
return new CustomWebView(reactContext);
}
@Override
public String getName() {
return REACT_CLASS;
}
@Override
protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
view.setWebViewClient(new CustomWebViewClient());
}
@Override
public @Nullable
Map getExportedCustomDirectEventTypeConstants() {
Map<String, Object> export = super.getExportedCustomDirectEventTypeConstants();
if (export == null) {
export = MapBuilder.newHashMap();
}
export.put("navigationCompleted", MapBuilder.of("registrationName", "onNavigationCompleted"));
return export;
}
}