|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | + * https://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.identity.credentialmanager |
| 18 | + |
| 19 | +import android.os.Bundle |
| 20 | +import android.util.Log |
| 21 | +import android.webkit.WebView |
| 22 | +import androidx.activity.ComponentActivity |
| 23 | +import androidx.activity.compose.setContent |
| 24 | +import androidx.compose.ui.viewinterop.AndroidView |
| 25 | +import androidx.webkit.WebSettingsCompat |
| 26 | +import androidx.webkit.WebViewFeature |
| 27 | + |
| 28 | +// [START android_identity_webview_main_activity] |
| 29 | +class WebViewActivity : ComponentActivity() { |
| 30 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 31 | + super.onCreate(savedInstanceState) |
| 32 | + setContent { |
| 33 | + // Put your webview link here. |
| 34 | + val url = "https://project-sesame-426206.appspot.com/passkey-signup" |
| 35 | + AndroidView( |
| 36 | + factory = { context -> |
| 37 | + WebView(context).apply { |
| 38 | + settings.javaScriptEnabled = true |
| 39 | + |
| 40 | + webViewClient = WebViewClientImpl() |
| 41 | + } |
| 42 | + }, |
| 43 | + update = { webView -> |
| 44 | + run { |
| 45 | + webView.loadUrl(url) |
| 46 | + if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_AUTHENTICATION)) { |
| 47 | + WebSettingsCompat.setWebAuthenticationSupport( |
| 48 | + webView.settings, |
| 49 | + WebSettingsCompat.WEB_AUTHENTICATION_SUPPORT_FOR_APP, |
| 50 | + ) |
| 51 | + // Check if getWebauthenticationSupport may have been disabled by the WebView. |
| 52 | + Log.e( |
| 53 | + "WebViewPasskeyDemo", |
| 54 | + "getWebAuthenticationSupport result: " + WebSettingsCompat.getWebAuthenticationSupport( |
| 55 | + webView.settings |
| 56 | + ), |
| 57 | + ) |
| 58 | + } else { |
| 59 | + Log.e("WebViewPasskeyDemo", "WebView does not support passkeys.") |
| 60 | + } |
| 61 | + } |
| 62 | + }, |
| 63 | + ) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +// [END android_identity_webview_main_activity] |
0 commit comments