| /* | |
| * 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.content.Intent | |
| import android.net.Uri | |
| import android.os.Bundle | |
| import android.view.View | |
| import androidx.fragment.app.Fragment | |
| import androidx.fragment.app.FragmentActivity | |
| import androidx.fragment.app.FragmentManager | |
| import com.android.settings.R | |
| import com.android.settings.SetupWizardUtils | |
| import com.google.android.setupdesign.GlifLayout | |
| import com.google.android.setupdesign.util.ThemeHelper | |
| import java.util.HashMap | |
| /** Activity for verifying the device with another device. */ | |
| class VerifyAnotherDeviceActivity : FragmentActivity() { | |
| enum class FailureReason(val title: Int, val description: Int) { | |
| NETWORK_ERROR( | |
| R.string.abt_error_dialog_network_error_title, | |
| R.string.abt_error_dialog_network_error, | |
| ), | |
| DEVICE_ERROR( | |
| R.string.abt_error_dialog_device_failure_title, | |
| R.string.abt_error_dialog_device_failure, | |
| ), | |
| } | |
| enum class Step { | |
| PRE_REQS, | |
| SHOW_WEB_ADDRESS, | |
| SCAN_QR_CODE, | |
| SCANNED_QR_CODE, // When launched from Camera. | |
| PROCESSING, | |
| RESULT, | |
| NETWORK_FAILURE, | |
| DEVICE_FAILURE, | |
| } | |
| var scannedInformation: String = "" | |
| var buildInformation: Map<AttestationUtils.AttestedBuildInformation, String?> = emptyMap() | |
| private val fragmentLifecycleCallbacks = | |
| object : FragmentManager.FragmentLifecycleCallbacks() { | |
| override fun onFragmentViewCreated( | |
| fm: FragmentManager, | |
| f: Fragment, | |
| v: View, | |
| savedInstanceState: Bundle?, | |
| ) { | |
| super.onFragmentViewCreated(fm, f, v, savedInstanceState) | |
| val glifLayout = v.findViewById<GlifLayout>(R.id.setup_wizard_layout) | |
| if (glifLayout != null) { | |
| val headerText = glifLayout.headerText | |
| if (!headerText.isNullOrEmpty()) { | |
| setTitle(headerText) | |
| v.accessibilityPaneTitle = headerText | |
| } | |
| } | |
| } | |
| } | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| if (ThemeHelper.shouldApplyGlifExpressiveStyle(applicationContext)) { | |
| ThemeHelper.trySetSuwTheme(this) | |
| } else { | |
| setTheme(SetupWizardUtils.getTheme(this, getIntent())) | |
| } | |
| ThemeHelper.trySetDynamicColor(this) | |
| super.onCreate(savedInstanceState) | |
| val action: String? = intent?.action | |
| val uri: Uri? = intent?.data | |
| if (action == Intent.ACTION_VIEW && uri != null) { | |
| scannedInformation = uri.toString() | |
| } | |
| supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, true) | |
| if (savedInstanceState == null) { | |
| val fragment = | |
| if (scannedInformation.isNotEmpty()) { | |
| // Go to processing step if the activity was started with a QR code scan. | |
| createFragment(Step.SCANNED_QR_CODE) | |
| } else { | |
| // Otherwise, go to the first step. | |
| createFragment(Step.PRE_REQS) | |
| } | |
| supportFragmentManager | |
| .beginTransaction() | |
| .replace(android.R.id.content, fragment) | |
| .commit() | |
| } else { | |
| scannedInformation = savedInstanceState.getString(SCANNED_INFORMATION_KEY, "") | |
| val hashMap = | |
| savedInstanceState.getSerializable(BUILD_INFORMATION_KEY, HashMap::class.java) | |
| ?: hashMapOf<AttestationUtils.AttestedBuildInformation, String>() | |
| buildInformation = | |
| hashMap.entries.associate { | |
| it.key as AttestationUtils.AttestedBuildInformation to it.value as String? | |
| } | |
| } | |
| } | |
| override fun onSaveInstanceState(outState: Bundle) { | |
| super.onSaveInstanceState(outState) | |
| outState.putString(SCANNED_INFORMATION_KEY, scannedInformation) | |
| outState.putSerializable( | |
| BUILD_INFORMATION_KEY, | |
| HashMap<AttestationUtils.AttestedBuildInformation, String>(buildInformation), | |
| ) | |
| } | |
| override fun onDestroy() { | |
| super.onDestroy() | |
| supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks) | |
| } | |
| fun showStep(step: Step) { | |
| supportFragmentManager | |
| .beginTransaction() | |
| .replace(android.R.id.content, createFragment(step)) | |
| .addToBackStack(null) | |
| .commit() | |
| } | |
| fun createFragment(step: Step): Fragment { | |
| return when (step) { | |
| Step.PRE_REQS -> VerifyAnotherDeviceStep1Fragment() | |
| Step.SHOW_WEB_ADDRESS -> VerifyAnotherDeviceStep2Fragment() | |
| Step.SCAN_QR_CODE -> VerifyAnotherDeviceStep3Fragment() | |
| Step.SCANNED_QR_CODE -> VerifyAnotherDeviceScannedFragment() | |
| Step.PROCESSING -> VerifyAnotherDeviceStep4Fragment() | |
| Step.RESULT -> VerifyAnotherDeviceStep5Fragment() | |
| Step.NETWORK_FAILURE -> | |
| VerifyAnotherDeviceFailureFragment.newInstance(FailureReason.NETWORK_ERROR) | |
| Step.DEVICE_FAILURE -> | |
| VerifyAnotherDeviceFailureFragment.newInstance(FailureReason.DEVICE_ERROR) | |
| } | |
| } | |
| companion object { | |
| private const val TAG = "VerifyAnotherDeviceActivity" | |
| private const val SCANNED_INFORMATION_KEY = "scanned_information" | |
| private const val BUILD_INFORMATION_KEY = "build_information" | |
| const val WEB_APP_URL = "https://verify.android" | |
| } | |
| } |