Browse Source

add WebViewAssetLoader

duxiaofeng-github 4 years ago
parent
commit
fc9868accc

+ 1
- 0
android/build.gradle View File

134
   //noinspection GradleDynamicVersion
134
   //noinspection GradleDynamicVersion
135
   implementation 'com.facebook.react:react-native:+'
135
   implementation 'com.facebook.react:react-native:+'
136
   implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
136
   implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
137
+  implementation 'androidx.webkit:webkit:1.2.0'
137
 }
138
 }

+ 18
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

43
 import androidx.annotation.RequiresApi;
43
 import androidx.annotation.RequiresApi;
44
 import androidx.core.content.ContextCompat;
44
 import androidx.core.content.ContextCompat;
45
 import androidx.core.util.Pair;
45
 import androidx.core.util.Pair;
46
+import androidx.webkit.WebViewAssetLoader;
47
+import androidx.webkit.WebViewAssetLoader.ResourcesPathHandler;
48
+import androidx.webkit.WebViewAssetLoader.AssetsPathHandler;
46
 
49
 
47
 import com.facebook.common.logging.FLog;
50
 import com.facebook.common.logging.FLog;
48
 import com.facebook.react.views.scroll.ScrollEvent;
51
 import com.facebook.react.views.scroll.ScrollEvent;
149
   protected @Nullable String mUserAgent = null;
152
   protected @Nullable String mUserAgent = null;
150
   protected @Nullable String mUserAgentWithApplicationName = null;
153
   protected @Nullable String mUserAgentWithApplicationName = null;
151
 
154
 
155
+  private static WebViewAssetLoader assetLoader;
156
+
152
   public RNCWebViewManager() {
157
   public RNCWebViewManager() {
153
     mWebViewConfig = new WebViewConfig() {
158
     mWebViewConfig = new WebViewConfig() {
154
       public void configWebView(WebView webView) {
159
       public void configWebView(WebView webView) {
196
     }
201
     }
197
     setMixedContentMode(webView, "never");
202
     setMixedContentMode(webView, "never");
198
 
203
 
204
+    if (assetLoader == null) {
205
+      assetLoader = new WebViewAssetLoader.Builder()
206
+        .addPathHandler("/assets/", new AssetsPathHandler(reactContext))
207
+        .addPathHandler("/res/", new ResourcesPathHandler(reactContext))
208
+        .build();
209
+    }
210
+
199
     // Fixes broken full-screen modals/galleries due to body height being 0.
211
     // Fixes broken full-screen modals/galleries due to body height being 0.
200
     webView.setLayoutParams(
212
     webView.setLayoutParams(
201
       new LayoutParams(LayoutParams.MATCH_PARENT,
213
       new LayoutParams(LayoutParams.MATCH_PARENT,
778
     protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
790
     protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
779
     protected @Nullable String ignoreErrFailedForThisURL = null;
791
     protected @Nullable String ignoreErrFailedForThisURL = null;
780
 
792
 
793
+    @Override
794
+    public WebResourceResponse shouldInterceptRequest(WebView view,
795
+                                                      WebResourceRequest request) {
796
+      return assetLoader.shouldInterceptRequest(request.getUrl());
797
+    }
798
+
781
     public void setIgnoreErrFailedForThisURL(@Nullable String url) {
799
     public void setIgnoreErrFailedForThisURL(@Nullable String url) {
782
       ignoreErrFailedForThisURL = url;
800
       ignoreErrFailedForThisURL = url;
783
     }
801
     }