Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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; |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Nate Fischer | c3d5cbe | 2020-02-26 18:39:38 -0800 | [diff] [blame] | 22 | import android.net.Uri; |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 23 | import android.os.Bundle; |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 24 | import android.webkit.ValueCallback; |
Nate Fischer | c3d5cbe | 2020-02-26 18:39:38 -0800 | [diff] [blame] | 25 | import android.widget.TextView; |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 26 | |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 27 | import androidx.annotation.NonNull; |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 28 | import androidx.annotation.Nullable; |
| 29 | import androidx.appcompat.app.AppCompatActivity; |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 30 | import androidx.webkit.WebViewCompat; |
| 31 | import androidx.webkit.WebViewFeature; |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * An {@link Activity} to exercise Safe Browsing functionality. |
| 35 | */ |
| 36 | public class SafeBrowsingActivity extends AppCompatActivity { |
| 37 | |
| 38 | @Override |
| 39 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 40 | super.onCreate(savedInstanceState); |
| 41 | |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 42 | if (WebViewFeature.isFeatureSupported(WebViewFeature.START_SAFE_BROWSING)) { |
| 43 | WebViewCompat.startSafeBrowsing(this.getApplicationContext(), |
| 44 | new ValueCallback<Boolean>() { |
| 45 | @Override |
| 46 | public void onReceiveValue(@NonNull Boolean value) { |
| 47 | if (value) { |
| 48 | setupLayout(); |
| 49 | } else { |
Nate Fischer | c3d5cbe | 2020-02-26 18:39:38 -0800 | [diff] [blame] | 50 | TextView t = WebkitHelpers.showMessageInActivity( |
| 51 | SafeBrowsingActivity.this, |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 52 | R.string.cannot_start_safe_browsing); |
Nate Fischer | c3d5cbe | 2020-02-26 18:39:38 -0800 | [diff] [blame] | 53 | t.setOnClickListener(v -> { |
| 54 | showSafeBrowsingRequirementsInBrowser(); |
| 55 | }); |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | }); |
| 59 | } else { |
| 60 | WebkitHelpers.showMessageInActivity(SafeBrowsingActivity.this, |
| 61 | R.string.webkit_api_not_available); |
| 62 | } |
| 63 | } |
| 64 | |
Nate Fischer | c3d5cbe | 2020-02-26 18:39:38 -0800 | [diff] [blame] | 65 | private void showSafeBrowsingRequirementsInBrowser() { |
| 66 | // Open documentation for WebView Safe Browsing to help the user |
| 67 | // debug what's wrong. |
| 68 | Uri safeBrowsingRequirementsUri = new Uri.Builder() |
| 69 | .scheme("https") |
| 70 | .authority("chromium.googlesource.com") |
| 71 | .path("/chromium/src/+/master/android_webview/browser/safe_browsing/README.md") |
| 72 | .encodedFragment("opt_in_consent_requirements") |
| 73 | .build(); |
| 74 | |
| 75 | Intent i = new Intent(Intent.ACTION_VIEW); |
| 76 | i.setData(safeBrowsingRequirementsUri); |
| 77 | startActivity(i); |
| 78 | } |
| 79 | |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 80 | private void setupLayout() { |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 81 | setContentView(R.layout.activity_safe_browsing); |
| 82 | setTitle(R.string.safebrowsing_activity_title); |
| 83 | WebkitHelpers.appendWebViewVersionToTitle(this); |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 84 | |
| 85 | final Context activityContext = this; |
| 86 | MenuListView listView = findViewById(R.id.safe_browsing_list); |
| 87 | MenuListView.MenuItem[] menuItems = new MenuListView.MenuItem[] { |
| 88 | new MenuListView.MenuItem( |
| 89 | getResources().getString(R.string.small_interstitial_activity_title), |
| 90 | new Intent(activityContext, SmallInterstitialActivity.class)), |
Nate Fischer | 65108de | 2019-04-19 18:56:52 -0700 | [diff] [blame] | 91 | new MenuListView.MenuItem( |
| 92 | getResources().getString(R.string.medium_wide_interstitial_activity_title), |
| 93 | new Intent(activityContext, MediumInterstitialActivity.class) |
| 94 | .putExtra(MediumInterstitialActivity.LAYOUT_HORIZONTAL, false)), |
| 95 | new MenuListView.MenuItem( |
| 96 | getResources().getString(R.string.medium_tall_interstitial_activity_title), |
| 97 | new Intent(activityContext, MediumInterstitialActivity.class) |
| 98 | .putExtra(MediumInterstitialActivity.LAYOUT_HORIZONTAL, true)), |
Nate Fischer | d58611ec | 2019-04-25 17:45:11 -0700 | [diff] [blame] | 99 | new MenuListView.MenuItem( |
| 100 | getResources().getString(R.string.loud_interstitial_activity_title), |
| 101 | new Intent(activityContext, LoudInterstitialActivity.class)), |
Nate Fischer | e752fc2 | 2019-05-01 21:56:17 -0700 | [diff] [blame] | 102 | new MenuListView.MenuItem( |
| 103 | getResources().getString(R.string.giant_interstitial_activity_title), |
| 104 | new Intent(activityContext, GiantInterstitialActivity.class)), |
Nate Fischer | d3c3eaa | 2019-05-06 14:40:26 -0700 | [diff] [blame] | 105 | new MenuListView.MenuItem( |
| 106 | getResources().getString(R.string.per_web_view_enable_activity_title), |
| 107 | new Intent(activityContext, PerWebViewEnableActivity.class)), |
Nate Fischer | bb24028 | 2019-05-13 21:02:39 -0700 | [diff] [blame] | 108 | new MenuListView.MenuItem( |
| 109 | getResources().getString(R.string.invisible_activity_title), |
| 110 | new Intent(activityContext, InvisibleActivity.class)), |
| 111 | new MenuListView.MenuItem( |
| 112 | getResources().getString(R.string.unattached_activity_title), |
| 113 | new Intent(activityContext, UnattachedActivity.class)), |
Nate Fischer | 2d596f5 | 2019-05-14 21:50:53 -0700 | [diff] [blame] | 114 | new MenuListView.MenuItem( |
| 115 | getResources().getString(R.string.custom_interstitial_activity_title), |
| 116 | new Intent(activityContext, CustomInterstitialActivity.class)), |
Nate Fischer | dc7f862 | 2019-05-15 22:14:40 -0700 | [diff] [blame] | 117 | new MenuListView.MenuItem( |
Nate Fischer | d626ac5 | 2020-07-16 14:20:33 -0700 | [diff] [blame^] | 118 | getResources().getString(R.string.allowlist_activity_title), |
| 119 | new Intent(activityContext, AllowlistActivity.class)), |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 120 | }; |
| 121 | listView.setItems(menuItems); |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 122 | } |
| 123 | } |