1 /* 2 * Copyright 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.example.androidx.webkit; 18 19 import android.os.Bundle; 20 import android.webkit.WebView; 21 import android.webkit.WebViewClient; 22 import android.widget.TextView; 23 24 import androidx.appcompat.app.AppCompatActivity; 25 import androidx.webkit.WebViewCompat; 26 import androidx.webkit.WebViewFeature; 27 28 import com.google.common.collect.ImmutableMap; 29 30 import org.jspecify.annotations.Nullable; 31 32 /** An {@link android.app.Activity} to demonstrate the getVariationsHeader() API. */ 33 public class GetVariationsHeaderActivity extends AppCompatActivity { 34 @Override onCreate(@ullable Bundle savedInstanceState)35 protected void onCreate(@Nullable Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.activity_get_variations_header); 38 setTitle(R.string.variations_header_activity_title); 39 WebkitHelpers.appendWebViewVersionToTitle(this); 40 41 if (WebViewFeature.isFeatureSupported(WebViewFeature.GET_VARIATIONS_HEADER)) { 42 TextView textView = findViewById(R.id.textview); 43 WebView webView = findViewById(R.id.webview); 44 String headerValue = WebViewCompat.getVariationsHeader(); 45 textView.setText(getString(R.string.variations_header_message, headerValue)); 46 webView.setWebViewClient(new WebViewClient()); 47 webView.loadUrl( 48 "https://www.google.com", ImmutableMap.of("X-Client-Data", headerValue)); 49 } else { 50 WebkitHelpers.showMessageInActivity( 51 GetVariationsHeaderActivity.this, R.string.variations_header_unavailable); 52 } 53 } 54 } 55