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 | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 22 | import android.os.Bundle; |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 23 | import android.webkit.ValueCallback; |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 24 | |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 25 | import androidx.annotation.NonNull; |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 26 | import androidx.annotation.Nullable; |
| 27 | import androidx.appcompat.app.AppCompatActivity; |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 28 | import androidx.webkit.WebViewCompat; |
| 29 | import androidx.webkit.WebViewFeature; |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * An {@link Activity} to exercise Safe Browsing functionality. |
| 33 | */ |
| 34 | public class SafeBrowsingActivity extends AppCompatActivity { |
| 35 | |
| 36 | @Override |
| 37 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 38 | super.onCreate(savedInstanceState); |
| 39 | |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 40 | if (WebViewFeature.isFeatureSupported(WebViewFeature.START_SAFE_BROWSING)) { |
| 41 | WebViewCompat.startSafeBrowsing(this.getApplicationContext(), |
| 42 | new ValueCallback<Boolean>() { |
| 43 | @Override |
| 44 | public void onReceiveValue(@NonNull Boolean value) { |
| 45 | if (value) { |
| 46 | setupLayout(); |
| 47 | } else { |
| 48 | WebkitHelpers.showMessageInActivity(SafeBrowsingActivity.this, |
| 49 | R.string.cannot_start_safe_browsing); |
| 50 | } |
| 51 | } |
| 52 | }); |
| 53 | } else { |
| 54 | WebkitHelpers.showMessageInActivity(SafeBrowsingActivity.this, |
| 55 | R.string.webkit_api_not_available); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private void setupLayout() { |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 60 | setContentView(R.layout.activity_safe_browsing); |
| 61 | setTitle(R.string.safebrowsing_activity_title); |
| 62 | WebkitHelpers.appendWebViewVersionToTitle(this); |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 63 | |
| 64 | final Context activityContext = this; |
| 65 | MenuListView listView = findViewById(R.id.safe_browsing_list); |
| 66 | MenuListView.MenuItem[] menuItems = new MenuListView.MenuItem[] { |
| 67 | new MenuListView.MenuItem( |
| 68 | getResources().getString(R.string.small_interstitial_activity_title), |
| 69 | new Intent(activityContext, SmallInterstitialActivity.class)), |
Nate Fischer | 65108de | 2019-04-19 18:56:52 -0700 | [diff] [blame^] | 70 | new MenuListView.MenuItem( |
| 71 | getResources().getString(R.string.medium_wide_interstitial_activity_title), |
| 72 | new Intent(activityContext, MediumInterstitialActivity.class) |
| 73 | .putExtra(MediumInterstitialActivity.LAYOUT_HORIZONTAL, false)), |
| 74 | new MenuListView.MenuItem( |
| 75 | getResources().getString(R.string.medium_tall_interstitial_activity_title), |
| 76 | new Intent(activityContext, MediumInterstitialActivity.class) |
| 77 | .putExtra(MediumInterstitialActivity.LAYOUT_HORIZONTAL, true)), |
Nate Fischer | c4988b5 | 2019-04-15 22:52:47 -0700 | [diff] [blame] | 78 | }; |
| 79 | listView.setItems(menuItems); |
Nate Fischer | 29bbb1e | 2018-10-01 19:23:43 -0700 | [diff] [blame] | 80 | } |
| 81 | } |