| /* | |
| * 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.widget.TextView | |
| 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.settingslib.widget.BannerMessagePreference | |
| import com.android.settingslib.widget.BannerMessagePreference.AttentionLevel | |
| import com.android.settingslib.widget.ExpandablePreference | |
| import com.android.settingslib.widget.IllustrationPreference | |
| import com.android.settingslib.widget.LayoutPreference | |
| import kotlinx.coroutines.launch | |
| class BannerPreferenceController(context: Context, key: String) : | |
| BasePreferenceController(context, key) { | |
| private val context: Context = context | |
| private val preferenceKey = key | |
| private var viewModel: BinaryTransparencyViewModel? = null | |
| private var expandableInformation: Map<String, OptionData> = emptyMap() | |
| private var preferenceCategory: PreferenceCategory? = null | |
| private var preferenceScreen: PreferenceScreen? = null | |
| private var fragment: BinaryTransparencyFragment? = null | |
| override fun getAvailabilityStatus(): Int = AVAILABLE | |
| fun init(viewModel: BinaryTransparencyViewModel) { | |
| this.viewModel = viewModel | |
| } | |
| fun setFragment(fragment: BinaryTransparencyFragment) { | |
| this.fragment = fragment | |
| } | |
| fun setExpandableInformation(expandableInformation: Map<String, OptionData>) { | |
| this.expandableInformation = expandableInformation | |
| } | |
| override fun displayPreference(preferenceScreen: PreferenceScreen) { | |
| super.displayPreference(preferenceScreen) | |
| this.preferenceScreen = preferenceScreen | |
| preferenceCategory = preferenceScreen.findPreference(preferenceKey) | |
| } | |
| 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 updateState(preference: Preference) { | |
| super.updateState(preference) | |
| val group = preference as? PreferenceCategory ?: return | |
| val isUiLoading = viewModel?.uiState?.value is BinaryTransparencyUiState.Loading | |
| val illustrationPreference = | |
| group?.findPreference<IllustrationPreference>(ILLUSTRATION_PREFERENCE_KEY) | |
| val riskBannerMessagePreference = | |
| group?.findPreference<BannerMessagePreference>(RISK_BANNER_MESSAGE_PREFERENCE_KEY) | |
| val statusPillPreference = | |
| group?.findPreference<LayoutPreference>(STATUS_PILL_PREFERENCE_KEY) | |
| val firstRiskKey = expandableInformation.entries.firstOrNull { it.value.isRisk }?.key | |
| if (isUiLoading) { | |
| // update the text in the pill to "loading" | |
| val textView = statusPillPreference?.findViewById<TextView>(R.id.status_pill_text) | |
| textView?.setText(R.string.settings_safetylegal_activity_loading) | |
| } else if (firstRiskKey == null) { | |
| val textView = statusPillPreference?.findViewById<TextView>(R.id.status_pill_text) | |
| textView?.setText(R.string.abt_banner_no_issues_detected) | |
| } | |
| if (firstRiskKey == null) { | |
| illustrationPreference?.isVisible = true | |
| statusPillPreference?.isVisible = true | |
| riskBannerMessagePreference?.isVisible = false | |
| } else { | |
| illustrationPreference?.isVisible = false | |
| statusPillPreference?.isVisible = false | |
| riskBannerMessagePreference?.setAttentionLevel(AttentionLevel.MEDIUM) | |
| riskBannerMessagePreference?.setPositiveButtonText( | |
| R.string.abt_banner_positive_button_text | |
| ) | |
| riskBannerMessagePreference?.setPositiveButtonVisible(true) | |
| riskBannerMessagePreference?.setPositiveButtonOnClickListener { | |
| val firstRiskPreference = | |
| preferenceScreen?.findPreference<ExpandablePreference>(firstRiskKey) | |
| if (firstRiskPreference != null && !firstRiskPreference.isExpanded()) { | |
| firstRiskPreference.setExpanded(true) | |
| } | |
| val explanationKey = "${firstRiskKey}_explanation" | |
| fragment?.listView?.post { fragment?.scrollToPreference(explanationKey) } | |
| } | |
| riskBannerMessagePreference?.isVisible = true | |
| } | |
| group?.isVisible = true | |
| } | |
| companion object { | |
| private const val TAG = "BannerPreferenceController" | |
| private const val ILLUSTRATION_PREFERENCE_KEY = "abt_illustration_preference_key" | |
| private const val RISK_BANNER_MESSAGE_PREFERENCE_KEY = "abt_risks_banner_preference" | |
| private const val STATUS_PILL_PREFERENCE_KEY = "abt_status_pill_preference" | |
| } | |
| } |