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.os.Bundle; 20 import android.webkit.WebView; 21 22 import androidx.appcompat.app.AppCompatActivity; 23 import androidx.webkit.WebSettingsCompat; 24 import androidx.webkit.WebViewFeature; 25 26 import org.jspecify.annotations.Nullable; 27 28 /** 29 * An {@link android.app.Activity} to demonstrate functionality to selectively enable/disable Safe 30 * Browsing for a subset of the application's {@link WebView}s. This shows three WebViews, to show 31 * Safe Browsing enabled, disabled, and inheriting the default setting (see inline comment for how 32 * this works). 33 */ 34 public class PerWebViewEnableActivity extends AppCompatActivity { 35 36 @Override onCreate(@ullable Bundle savedInstanceState)37 protected void onCreate(@Nullable Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.activity_per_web_view_enable); 40 setTitle(R.string.per_web_view_enable_activity_title); 41 WebkitHelpers.appendWebViewVersionToTitle(this); 42 43 WebView enabledWebView = findViewById(R.id.enabled_webview); 44 WebView disabledWebView = findViewById(R.id.disabled_webview); 45 WebView defaultWebView = findViewById(R.id.default_webview); 46 47 if (WebViewFeature.isFeatureSupported(WebViewFeature.SAFE_BROWSING_ENABLE)) { 48 WebSettingsCompat.setSafeBrowsingEnabled(enabledWebView.getSettings(), true); 49 WebSettingsCompat.setSafeBrowsingEnabled(disabledWebView.getSettings(), false); 50 // defaultWebView will defer to the EnableSafeBrowsing metadata manifest tag. If that's 51 // not set, then Safe Browsing will be enabled by default for WebViews >= M66, per 52 // https://blog.chromium.org/2018/04/protecting-webview-with-safe-browsing.html. 53 } else { 54 WebkitHelpers.showMessageInActivity(this, R.string.webkit_api_not_available); 55 } 56 57 enabledWebView.loadUrl(SafeBrowsingHelpers.MALWARE_URL); 58 disabledWebView.loadUrl(SafeBrowsingHelpers.MALWARE_URL); 59 defaultWebView.loadUrl(SafeBrowsingHelpers.MALWARE_URL); 60 } 61 } 62