1 /* 2 * Copyright 2023 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.util.Log; 21 import android.webkit.WebView; 22 import android.webkit.WebViewClient; 23 import android.widget.Toast; 24 25 import androidx.appcompat.app.AppCompatActivity; 26 import androidx.webkit.Profile; 27 import androidx.webkit.ProfileStore; 28 import androidx.webkit.WebViewCompat; 29 import androidx.webkit.WebViewFeature; 30 31 import org.jspecify.annotations.Nullable; 32 33 /** 34 * An {@link android.app.Activity} to demonstrate using Multi-Profile feature. 35 * 36 * <p> 37 * 38 * It creates two WebViews and assigns the default profile to one and a newly created profile to 39 * the other one. There's a button above each WebView to print the cookie value as a confirmation 40 * that each WebView get different cookie value. 41 * 42 */ 43 public class MultiProfileTestActivity extends AppCompatActivity { 44 45 private static final String INITIAL_URL = "https://www.google.com"; 46 47 @Override onCreate(@ullable Bundle savedInstanceState)48 protected void onCreate(@Nullable Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 setContentView(R.layout.activity_multi_profile); 51 52 if (!WebViewFeature.isFeatureSupported(WebViewFeature.MULTI_PROFILE)) { 53 WebkitHelpers.showMessageInActivity(MultiProfileTestActivity.this, 54 R.string.multi_profile_not_supported); 55 return; 56 } 57 58 initializeDefault(); 59 initializeCreatedProfile(); 60 } 61 initializeDefault()62 private void initializeDefault() { 63 WebView defaultWebView = findViewById(R.id.default_webview); 64 defaultWebView.setWebViewClient(new WebViewClient()); 65 defaultWebView.getSettings().setJavaScriptEnabled(true); 66 defaultWebView.loadUrl(INITIAL_URL); 67 findViewById(R.id.default_profile_cookie_text).setOnClickListener( 68 v -> logCookieForProfile(defaultWebView)); 69 } 70 initializeCreatedProfile()71 private void initializeCreatedProfile() { 72 WebView createdProfileWebView = findViewById(R.id.first_profile); 73 ProfileStore profileStore = ProfileStore.getInstance(); 74 Profile createdProfile = profileStore.getOrCreateProfile("First"); 75 WebViewCompat.setProfile(createdProfileWebView, createdProfile.getName()); 76 createdProfileWebView.setWebViewClient(new WebViewClient()); 77 createdProfileWebView.getSettings().setJavaScriptEnabled(true); 78 createdProfileWebView.loadUrl(INITIAL_URL); 79 findViewById(R.id.created_profile_cookie_text).setOnClickListener( 80 v -> logCookieForProfile(createdProfileWebView)); 81 } 82 83 /** 84 * Show the cookies of the loaded page to the user to let them confirm that the two WebViews 85 * get different cookie values. 86 */ logCookieForProfile(WebView requestedWebView)87 private void logCookieForProfile(WebView requestedWebView) { 88 String cookieInfo = WebViewCompat.getProfile(requestedWebView).getCookieManager().getCookie( 89 requestedWebView.getUrl()); 90 Toast.makeText(MultiProfileTestActivity.this, cookieInfo, 91 Toast.LENGTH_SHORT).show(); 92 Log.i(MultiProfileTestActivity.this.getLocalClassName(), cookieInfo); 93 } 94 } 95