blob: 0c7b1c6e06f63528a4c8ce274be4e4598a2d1bb1 [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.app.AlertDialog
import android.app.settings.SettingsEnums
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.widget.TextView
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.withStarted
import androidx.preference.Preference
import com.android.settings.R
import com.android.settings.binarytransparency.AttestationUtils.AttestedBuildInformation
import com.android.settings.dashboard.DashboardFragment
import com.android.settingslib.widget.FooterPreference
import com.google.android.material.button.MaterialButton
import kotlinx.coroutines.launch
/** Fragment for displaying binary transparency information. */
class BinaryTransparencyFragment() : DashboardFragment() {
private val viewModel: BinaryTransparencyViewModel by viewModels {
BinaryTransparencyViewModelFactory()
}
override fun getPreferenceScreenResId() = R.xml.binary_transparency_screen
override fun getLogTag() = TAG
override fun getMetricsCategory() = SettingsEnums.ANDROID_OS_VERIFICATION
override fun onAttach(context: Context) {
super.onAttach(context)
use(ExpandableOptionsPreferenceController::class.java).init(viewModel)
use(BannerPreferenceController::class.java).init(viewModel)
use(BannerPreferenceController::class.java).setFragment(this)
lifecycleScope.launch { setFooterPreference() }
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect { state -> handleUiState(state) }
}
}
viewModel.generateAndValidateKeyAttestationCertificate()
}
private suspend fun setFooterPreference() {
withStarted {
val learnMoreIntent = Intent(Intent.ACTION_VIEW)
learnMoreIntent.data = Uri.parse("https://verify.android/about")
val footerPreference =
preferenceScreen.findPreference(FOOTER_PREFERENCE_KEY) as FooterPreference?
if (footerPreference != null) {
footerPreference.setLearnMoreText(
requireActivity().getString(R.string.abt_learn_more)
)
footerPreference.setLearnMoreAction { startActivity(learnMoreIntent) }
}
}
}
private fun handleUiState(state: BinaryTransparencyUiState) {
when (state) {
is BinaryTransparencyUiState.Loading -> {
// No-op or placeholder logic if desired
}
is BinaryTransparencyUiState.Success -> {
setDeviceInformationPreference(state.buildInformation)
}
is BinaryTransparencyUiState.Error -> {
when (state.errorType) {
ErrorType.DEVICE_FAILURE -> {
showAlertDialogAndTerminateActivity(
R.string.abt_error_dialog_device_failure_title,
R.string.abt_error_dialog_device_failure,
)
}
ErrorType.NETWORK_ERROR -> {
showAlertDialogAndTerminateActivity(
R.string.abt_error_dialog_network_error_title,
R.string.abt_error_dialog_network_error,
)
}
}
}
}
}
private fun showAlertDialogAndTerminateActivity(titleResId: Int, messageResId: Int) {
Log.i(TAG, "showAlertDialogAndTerminateActivity")
requireActivity().runOnUiThread {
val inflater = requireActivity().layoutInflater
val view = inflater.inflate(R.layout.abt_error_dialog, null)
view.findViewById<TextView>(R.id.dialog_title).setText(titleResId)
view.findViewById<TextView>(R.id.dialog_message).setText(messageResId)
val dialog =
AlertDialog.Builder(requireActivity()).setView(view).setCancelable(false).create()
view.findViewById<MaterialButton>(R.id.dialog_button).setOnClickListener {
dialog.dismiss()
requireActivity().finish()
}
dialog.show()
}
}
private fun setDeviceInformationPreference(buildInfo: Map<AttestedBuildInformation, String?>) {
val preference = preferenceScreen.findPreference(DEVICE_INFO_PREFERENCE_KEY) as Preference?
val extra = preference?.extras ?: return
for (deviceIdentifier in DeviceIdentifierFragment.DeviceIdentifier.values()) {
deviceIdentifier
.getValue(buildInfo)
?.takeIf { it.isNotBlank() }
?.let { value -> extra.putString(deviceIdentifier.representation, value) }
}
}
companion object {
private const val TAG = "BinaryTransparencyFragment"
private const val DEVICE_INFO_PREFERENCE_KEY = "abt_device_information_preference"
private const val FOOTER_PREFERENCE_KEY = "abt_footer_preference_key"
const val CERTIFIED_BUILD_KEY = "abt_official_certified_build_preference_group"
const val BOOTLOADER_LOCKED_KEY = "abt_bootloader_locked_preference_group"
const val BOOT_STATE_KEY = "abt_boot_state_preference_group"
}
}