1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.androidx.webkit; 18 19 import android.app.Activity; 20 import android.net.Uri; 21 import android.os.Bundle; 22 import android.webkit.WebResourceRequest; 23 import android.webkit.WebResourceResponse; 24 import android.webkit.WebView; 25 import android.webkit.WebViewClient; 26 27 import androidx.appcompat.app.AppCompatActivity; 28 import androidx.webkit.WebViewAssetLoader; 29 30 import org.jspecify.annotations.NonNull; 31 import org.jspecify.annotations.Nullable; 32 33 /** 34 * An {@link Activity} to show case a very simple use case of using 35 * {@link androidx.webkit.WebViewAssetLoader}. 36 */ 37 public class AssetLoaderSimpleActivity extends AppCompatActivity { 38 39 private static class MyWebViewClient extends WebViewClient { 40 private final WebViewAssetLoader mAssetLoader; 41 MyWebViewClient(@onNull WebViewAssetLoader assetLoader)42 MyWebViewClient(@NonNull WebViewAssetLoader assetLoader) { 43 mAssetLoader = assetLoader; 44 } 45 46 @Override 47 @SuppressWarnings("deprecation") // use the old one for compatibility with all API levels. shouldOverrideUrlLoading(WebView view, String url)48 public boolean shouldOverrideUrlLoading(WebView view, String url) { 49 return false; 50 } 51 52 @Override shouldInterceptRequest(WebView view, WebResourceRequest request)53 public WebResourceResponse shouldInterceptRequest(WebView view, 54 WebResourceRequest request) { 55 return mAssetLoader.shouldInterceptRequest(request.getUrl()); 56 } 57 58 @Override 59 @SuppressWarnings("deprecation") // use the old one for compatibility with all API levels. shouldInterceptRequest(WebView view, String url)60 public WebResourceResponse shouldInterceptRequest(WebView view, String url) { 61 return mAssetLoader.shouldInterceptRequest(Uri.parse(url)); 62 } 63 } 64 65 @Override onCreate(@ullable Bundle savedInstanceState)66 protected void onCreate(@Nullable Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 69 setContentView(R.layout.activity_asset_loader); 70 setTitle(R.string.asset_loader_simple_activity_title); 71 WebkitHelpers.appendWebViewVersionToTitle(this); 72 73 WebView webView = findViewById(R.id.webview_asset_loader_webview); 74 75 // Host application assets under http://appassets.androidplatform.net/assets/... 76 WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder() 77 .addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(this)) 78 .build(); 79 80 webView.setWebViewClient(new MyWebViewClient(assetLoader)); 81 82 Uri path = new Uri.Builder() 83 .scheme("https") 84 .authority(WebViewAssetLoader.DEFAULT_DOMAIN) 85 .appendPath("assets") 86 .appendPath("www") 87 .appendPath("some_text.html") 88 .build(); 89 90 webView.loadUrl(path.toString()); 91 } 92 } 93