| /* | |
| * 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.Context | |
| import android.text.SpannableString | |
| import android.text.Spanned | |
| import android.text.style.URLSpan | |
| import androidx.lifecycle.Lifecycle | |
| import androidx.lifecycle.LifecycleOwner | |
| import androidx.lifecycle.lifecycleScope | |
| import androidx.lifecycle.repeatOnLifecycle | |
| import androidx.preference.Preference | |
| import androidx.preference.PreferenceCategory | |
| import androidx.preference.PreferenceScreen | |
| import com.android.settings.R | |
| import com.android.settings.core.BasePreferenceController | |
| import com.android.settings.widget.LinkifySummaryPreference | |
| import com.android.settingslib.widget.ExpandablePreference | |
| import kotlinx.coroutines.launch | |
| sealed class OptionData { | |
| abstract val summaryResId: Int | |
| abstract val iconResId: Int | |
| abstract val explanationResId: Int | |
| abstract val learnMoreResId: Int | |
| abstract val learnMoreUrl: String | |
| abstract val isRisk: Boolean | |
| data class PlayProtectData(val isApproved: Boolean) : OptionData() { | |
| override val summaryResId = | |
| if (isApproved) R.string.abt_official_certified_android_build_summary_positive | |
| else R.string.abt_official_certified_android_build_summary_negative | |
| override val iconResId = | |
| if (isApproved) R.drawable.ic_check_circle_green else R.drawable.ic_abt_recommendation | |
| override val explanationResId = | |
| if (isApproved) | |
| R.string.abt_official_certified_android_build_summary_explanation_positive | |
| else R.string.abt_official_certified_android_build_summary_explanation_negative | |
| override val learnMoreResId = R.string.abt_official_certified_android_build_learn_more | |
| override val learnMoreUrl = "https://verify.android/about#topic-playprotect" | |
| override val isRisk = !isApproved | |
| } | |
| data class BootloaderData(val isLocked: Boolean) : OptionData() { | |
| override val summaryResId = | |
| if (isLocked) R.string.abt_bootloader_locked_summary_positive | |
| else R.string.abt_bootloader_locked_summary_negative | |
| override val iconResId = | |
| if (isLocked) R.drawable.ic_check_circle_green else R.drawable.ic_abt_recommendation | |
| override val explanationResId = | |
| if (isLocked) R.string.abt_bootloader_locked_summary_explanation_positive | |
| else R.string.abt_bootloader_locked_summary_explanation_negative | |
| override val learnMoreResId = R.string.abt_bootloader_locked_learn_more | |
| override val learnMoreUrl = "https://verify.android/about#topic-bootloader" | |
| override val isRisk = !isLocked | |
| } | |
| data class BootData(val status: BootStatus) : OptionData() { | |
| override val summaryResId = | |
| when (status) { | |
| BootStatus.VERIFIED -> R.string.abt_boot_state_verified_summary | |
| BootStatus.SELF_SIGNED -> R.string.abt_boot_state_self_signed_summary | |
| BootStatus.UNVERIFIED -> R.string.abt_boot_state_unverified_summary | |
| BootStatus.FAILED -> R.string.abt_boot_state_failed_summary | |
| } | |
| override val iconResId = | |
| when (status) { | |
| BootStatus.VERIFIED -> R.drawable.ic_check_circle_green | |
| else -> R.drawable.ic_abt_recommendation | |
| } | |
| override val explanationResId = | |
| when (status) { | |
| BootStatus.VERIFIED -> R.string.abt_boot_state_verified_explanation | |
| BootStatus.SELF_SIGNED -> R.string.abt_boot_state_self_signed_explanation | |
| BootStatus.UNVERIFIED -> R.string.abt_boot_state_unverified_explanation | |
| BootStatus.FAILED -> R.string.abt_boot_state_failed_explanation | |
| } | |
| override val learnMoreResId = R.string.abt_boot_state_learn_more | |
| override val learnMoreUrl = "https://verify.android/about#topic-bootstate" | |
| override val isRisk = status != BootStatus.VERIFIED | |
| } | |
| } | |
| enum class BootStatus { | |
| VERIFIED, | |
| SELF_SIGNED, | |
| UNVERIFIED, | |
| FAILED, | |
| } | |
| class ExpandableOptionsPreferenceController(context: Context, key: String) : | |
| BasePreferenceController(context, key) { | |
| private val preferenceKey = key | |
| private val context = context | |
| private var viewModel: BinaryTransparencyViewModel? = null | |
| private var expandableInformation: Map<String, OptionData> = emptyMap() | |
| private var preferenceCategory: PreferenceCategory? = null | |
| // Enum class representing the expandable options in the preference screen. | |
| enum class ExpandableOptions(val preferenceKey: String, val titleResId: Int) { | |
| CERTIFIED_ANDROID_BUILD( | |
| BinaryTransparencyFragment.CERTIFIED_BUILD_KEY, | |
| R.string.abt_official_certified_android_build_title, | |
| ), | |
| BOOTLOADER_LOCKED( | |
| BinaryTransparencyFragment.BOOTLOADER_LOCKED_KEY, | |
| R.string.abt_bootloader_locked_title, | |
| ), | |
| BOOT_STATE(BinaryTransparencyFragment.BOOT_STATE_KEY, R.string.abt_boot_state_title), | |
| } | |
| fun init(viewModel: BinaryTransparencyViewModel) { | |
| this.viewModel = viewModel | |
| } | |
| fun setExpandableInformation(expandableInformation: Map<String, OptionData>) { | |
| this.expandableInformation = expandableInformation | |
| } | |
| override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) { | |
| val vm = viewModel ?: return | |
| viewLifecycleOwner.lifecycleScope.launch { | |
| viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { | |
| vm.uiState.collect { state -> | |
| if (state is BinaryTransparencyUiState.Success) { | |
| expandableInformation = state.expandableInformation | |
| } | |
| preferenceCategory?.let { updateState(it) } | |
| } | |
| } | |
| } | |
| } | |
| override fun displayPreference(screen: PreferenceScreen) { | |
| super.displayPreference(screen) | |
| preferenceCategory = screen.findPreference(preferenceKey) | |
| } | |
| override fun updateState(preference: Preference) { | |
| super.updateState(preference) | |
| val group = preference as? PreferenceCategory ?: return | |
| val isUiLoading = viewModel?.uiState?.value is BinaryTransparencyUiState.Loading | |
| for (expandableOption in ExpandableOptions.values()) { | |
| val data = expandableInformation[expandableOption.preferenceKey] | |
| val existingGroup = | |
| group.findPreference<ExpandablePreference>(expandableOption.preferenceKey) | |
| ?: ExpandablePreference(context).apply { | |
| key = expandableOption.preferenceKey | |
| title = context.getString(expandableOption.titleResId) | |
| summary = context.getString(R.string.abt_device_no_information_available) | |
| group.addPreference(this) | |
| } | |
| val explanationKey = "${expandableOption.preferenceKey}_explanation" | |
| val existingExpl = existingGroup.findPreference<Preference>(explanationKey) | |
| if (isUiLoading) { | |
| existingGroup.summary = | |
| context.getString(R.string.settings_safetylegal_activity_loading) | |
| existingGroup.icon = context.getDrawable(R.drawable.ic_abt_null_state) | |
| if (existingExpl != null) { | |
| existingGroup.removePreference(existingExpl) | |
| } | |
| } else if (data != null) { | |
| existingGroup.summary = context.getString(data.summaryResId) | |
| existingGroup.icon = context.getDrawable(data.iconResId) | |
| if (existingExpl != null) { | |
| existingExpl.summary = getExplanationSummary(data) | |
| existingExpl.isSelectable = false | |
| } else { | |
| existingGroup.addPreference( | |
| LinkifySummaryPreference(context).apply { | |
| key = explanationKey | |
| summary = getExplanationSummary(data) | |
| isSelectable = false | |
| } | |
| ) | |
| } | |
| } else { | |
| existingGroup.summary = | |
| context.getString(R.string.abt_device_no_information_available) | |
| existingGroup.icon = context.getDrawable(R.drawable.ic_abt_null_state) | |
| if (existingExpl != null) { | |
| existingGroup.removePreference(existingExpl) | |
| } | |
| } | |
| } | |
| } | |
| override fun getAvailabilityStatus(): Int { | |
| return AVAILABLE | |
| } | |
| fun getExplanationSummary(data: OptionData): CharSequence { | |
| val learnMore = context.getString(data.learnMoreResId) | |
| val message = context.getString(data.explanationResId, learnMore) | |
| val learnMoreUrl = data.learnMoreUrl | |
| return buildMessageWithLink(message, learnMore, learnMoreUrl) | |
| } | |
| fun buildMessageWithLink( | |
| message: String, | |
| learnMore: String, | |
| learnMoreUrl: String, | |
| ): CharSequence { | |
| val startIndex = message.indexOf(learnMore) | |
| if (startIndex == -1) { | |
| return message | |
| } | |
| val endIndex = startIndex + learnMore.length | |
| val spannable: SpannableString = SpannableString(message) | |
| spannable.setSpan( | |
| URLSpan(learnMoreUrl), | |
| startIndex, | |
| endIndex, | |
| Spanned.SPAN_INCLUSIVE_EXCLUSIVE, | |
| ) | |
| return spannable | |
| } | |
| companion object { | |
| private const val TAG = "ExpandableOptionsPreferenceController" | |
| } | |
| } |