blob: 9c0d16b35d7815e0a2a6d707101ef6c335cde677 [file] [log] [blame]
Hazem Ashmawyf9db58512019-01-10 21:46:17 +00001/*
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
17package com.example.androidx.webkit;
18
19import android.app.Activity;
20import android.net.Uri;
21import android.os.Bundle;
22import android.webkit.WebResourceRequest;
23import android.webkit.WebResourceResponse;
24import android.webkit.WebView;
25import android.webkit.WebViewClient;
26
27import androidx.annotation.Nullable;
28import androidx.annotation.RequiresApi;
29import androidx.appcompat.app.AppCompatActivity;
30import 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 */
36public 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 Ashmawyab20c7e2019-06-14 16:25:32 +010048 return mAssetLoader.shouldInterceptRequest(request.getUrl());
Hazem Ashmawyf9db58512019-01-10 21:46:17 +000049 }
50
51 @Override
52 public WebResourceResponse shouldInterceptRequest(WebView view, String request) {
Hazem Ashmawyab20c7e2019-06-14 16:25:32 +010053 return mAssetLoader.shouldInterceptRequest(Uri.parse(request));
Hazem Ashmawyf9db58512019-01-10 21:46:17 +000054 }
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 Ashmawyf9db58512019-01-10 21:46:17 +000068 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 Ashmawyab20c7e2019-06-14 16:25:32 +010072 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 Ashmawyf9db58512019-01-10 21:46:17 +000082
83 mWebView.loadUrl(path.toString());
84 }
85}