blob: ad387cb831040a34281d13f168e49d13aa3a42d4 [file]
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.binarytransparency
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.android.settings.R
import com.android.settings.binarytransparency.AttestationUtils.AttestedBuildInformation
import com.android.settings.binarytransparency.VerifyAnotherDeviceActivity.Step
import com.google.android.setupcompat.template.FooterBarMixin
import com.google.android.setupcompat.template.FooterButton
import com.google.android.setupdesign.GlifLayout
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
/** Fragment for step 4 of verifying the device with another device. */
class VerifyAnotherDeviceStep4Fragment : Fragment() {
private sealed interface AttestationResult {
data class Success(val buildInfo: Map<AttestedBuildInformation, String?>) :
AttestationResult
object DeviceError : AttestationResult
object NetworkError : AttestationResult
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View = inflater.inflate(R.layout.verify_another_device_step4, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val glifLayout = view.findViewById<GlifLayout>(R.id.setup_wizard_layout)
val mixin = glifLayout.getMixin(FooterBarMixin::class.java)
mixin.secondaryButton =
FooterButton.Builder(requireContext())
.setText(R.string.cancel)
.setListener { activity?.finish() }
.setButtonType(FooterButton.ButtonType.CANCEL)
.setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Secondary)
.build()
lifecycleScope.launch {
val activity = activity as? VerifyAnotherDeviceActivity ?: return@launch
val scannedInfo = activity.scannedInformation
if (scannedInfo.isEmpty() || !scannedInfo.startsWith("transparency://")) {
Log.e(TAG, "Invalid scanned information")
activity.showStep(Step.NETWORK_FAILURE)
return@launch
}
// 2. Split the information to get challenge and session id
// Assuming format is transparency://challenge/challenge_string/session_id
val uri = Uri.parse(scannedInfo)
val segments = uri.pathSegments
if (segments.size < 2) {
Log.e(TAG, "Invalid scanned information format: $scannedInfo")
activity.showStep(Step.NETWORK_FAILURE)
return@launch
}
val parts = segments.takeLast(2)
val challengeString = parts.first()
val sessionId = parts.last()
Log.i(TAG, "sessionId: $sessionId, challengeString: $challengeString")
val challengeBytes = challengeString.toByteArray(Charsets.UTF_8)
// 3. Perform attestation operations on Dispatchers.IO
val result =
withContext(Dispatchers.IO) {
val certs =
try {
AttestationUtils.generateAttestationCertificate(challengeBytes)
} catch (e: Exception) {
Log.e(TAG, "Failed to generate attestation certificate", e)
emptyList()
}
Log.i(TAG, "certs size: ${certs.size}")
if (certs.isEmpty()) {
Log.e(TAG, "Failed to generate attestation certificate")
return@withContext AttestationResult.DeviceError
}
// 4. Post the attestation certificate to the web app with session id
try {
NetworkUtils.postAttestationToWebApp(sessionId, certs)
} catch (e: Exception) {
Log.e(TAG, "Failed to post attestation to web app", e)
return@withContext AttestationResult.NetworkError
}
// 5. Extract build information from certificate
AttestationResult.Success(AttestationUtils.extractBuildInformation(certs))
}
// 6. Handle UI transition safely on Main Thread
if (!isAdded) return@launch
when (result) {
is AttestationResult.Success -> {
activity.buildInformation = result.buildInfo
activity.showStep(Step.RESULT)
}
AttestationResult.DeviceError -> {
activity.showStep(Step.DEVICE_FAILURE)
}
AttestationResult.NetworkError -> {
activity.showStep(Step.NETWORK_FAILURE)
}
}
}
}
companion object {
private const val TAG = "VerifyAnotherDeviceStep4Fragment"
}
}