Hazem Ashmawy | f9db5851 | 2019-01-10 21:46:17 +0000 | [diff] [blame] | 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.annotation.Nullable; |
| 28 | import androidx.annotation.RequiresApi; |
| 29 | import androidx.appcompat.app.AppCompatActivity; |
| 30 | import androidx.webkit.WebViewAssetLoader; |
| 31 | |
| 32 | /** |
| 33 | * An {@link Activity} to show case a very simple use case of using |
| 34 | * {@link androidx.webkit.WebViewAssetLoader}. |
| 35 | */ |
| 36 | public class AssetLoaderSimpleActivity extends AppCompatActivity { |
| 37 | |
| 38 | private class MyWebViewClient extends WebViewClient { |
| 39 | @Override |
| 40 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | @RequiresApi(21) |
| 46 | public WebResourceResponse shouldInterceptRequest(WebView view, |
| 47 | WebResourceRequest request) { |
Hazem Ashmawy | ab20c7e | 2019-06-14 16:25:32 +0100 | [diff] [blame] | 48 | return mAssetLoader.shouldInterceptRequest(request.getUrl()); |
Hazem Ashmawy | f9db5851 | 2019-01-10 21:46:17 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public WebResourceResponse shouldInterceptRequest(WebView view, String request) { |
Hazem Ashmawy | ab20c7e | 2019-06-14 16:25:32 +0100 | [diff] [blame] | 53 | return mAssetLoader.shouldInterceptRequest(Uri.parse(request)); |
Hazem Ashmawy | f9db5851 | 2019-01-10 21:46:17 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | private WebViewAssetLoader mAssetLoader; |
| 58 | private WebView mWebView; |
| 59 | |
| 60 | @Override |
| 61 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 62 | super.onCreate(savedInstanceState); |
| 63 | |
| 64 | setContentView(R.layout.activity_asset_loader); |
| 65 | setTitle(R.string.asset_loader_simple_activity_title); |
| 66 | WebkitHelpers.appendWebViewVersionToTitle(this); |
| 67 | |
Hazem Ashmawy | f9db5851 | 2019-01-10 21:46:17 +0000 | [diff] [blame] | 68 | mWebView = findViewById(R.id.webview_asset_loader_webview); |
| 69 | mWebView.setWebViewClient(new MyWebViewClient()); |
| 70 | |
| 71 | // Host application assets under http://appassets.androidplatform.net/assets/... |
Hazem Ashmawy | ab20c7e | 2019-06-14 16:25:32 +0100 | [diff] [blame] | 72 | mAssetLoader = new WebViewAssetLoader.Builder() |
| 73 | .register("/assets/", new WebViewAssetLoader.AssetsPathHandler(this)) |
| 74 | .build(); |
| 75 | Uri path = new Uri.Builder() |
| 76 | .scheme("https") |
| 77 | .authority(WebViewAssetLoader.DEFAULT_DOMAIN) |
| 78 | .appendPath("assets") |
| 79 | .appendPath("www") |
| 80 | .appendPath("some_text.html") |
| 81 | .build(); |
Hazem Ashmawy | f9db5851 | 2019-01-10 21:46:17 +0000 | [diff] [blame] | 82 | |
| 83 | mWebView.loadUrl(path.toString()); |
| 84 | } |
| 85 | } |