1 /*
2  * Copyright (C) 2022 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.android.sdksandboxcode_webview;
18 
19 import android.app.sdksandbox.SandboxedSdk;
20 import android.app.sdksandbox.SandboxedSdkProvider;
21 import android.app.sdksandbox.interfaces.IWebViewSdkApi;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.view.View;
27 import android.webkit.WebSettings;
28 import android.webkit.WebView;
29 
30 
31 public class SandboxedSdkWebViewProvider extends SandboxedSdkProvider {
32 
33     private WebView mWebView = null;
34     private static final Handler sHandler = new Handler(Looper.getMainLooper());
35 
36     @Override
onLoadSdk(Bundle params)37     public SandboxedSdk onLoadSdk(Bundle params) {
38         IWebViewSdkApi.Stub webviewProxy =
39                 new IWebViewSdkApi.Stub() {
40                     public void loadUrl(String url) {
41                         sHandler.post(() -> mWebView.loadUrl(url));
42                     }
43 
44                     public void destroy() {
45                         sHandler.post(() -> mWebView.destroy());
46                     }
47                 };
48         return new SandboxedSdk(webviewProxy);
49     }
50 
51     @Override
getView(Context windowContext, Bundle params, int width, int height)52     public View getView(Context windowContext, Bundle params, int width, int height) {
53         mWebView = new WebView(windowContext);
54         initializeSettings(mWebView.getSettings());
55         mWebView.loadUrl("https://www.google.com/");
56         return mWebView;
57     }
58 
initializeSettings(WebSettings settings)59     private void initializeSettings(WebSettings settings) {
60         settings.setJavaScriptEnabled(true);
61 
62         settings.setGeolocationEnabled(true);
63         settings.setSupportZoom(true);
64         settings.setDatabaseEnabled(true);
65         settings.setDomStorageEnabled(true);
66         settings.setAllowFileAccess(true);
67         settings.setAllowContentAccess(true);
68 
69         // Default layout behavior for chrome on android.
70         settings.setUseWideViewPort(true);
71         settings.setLoadWithOverviewMode(true);
72         settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
73     }
74 
75 }
76