Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7a33bd59 authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Fix cold startup of NetworkProviderCallsSmsController" into main

parents dd61b6bb fe2a106d
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
<!--
    Copyright (C) 2020 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:tint="?android:attr/colorControlNormal"
    >

    <path
        android:pathData="M 0 0 H 24 V 24 H 0 V 0 Z" />
    <path
        android:fillColor="#FF000000"
        android:pathData="M20.17,14.85l-3.26-0.65c-0.33-0.07-0.67,0.04-0.9,0.27l-2.62,2.62c-2.75-1.49-5.01-3.75-6.5-6.5l2.62-2.62 c0.24-0.24,0.34-0.58,0.27-0.9L9.13,3.82c-0.09-0.47-0.5-0.8-0.98-0.8H4c-0.56,0-1.03,0.47-1,1.03c0.17,2.91,1.04,5.63,2.43,8.01 c1.57,2.69,3.81,4.93,6.5,6.5c2.38,1.39,5.1,2.26,8.01,2.43c0.56,0.03,1.03-0.44,1.03-1v-4.15C20.97,15.36,20.64,14.95,20.17,14.85 L20.17,14.85z M12,3v10l3-3h6V3H12z M19,8h-5V5h5V8z" />
</vector>
+7 −1
Original line number Diff line number Diff line
@@ -31,10 +31,16 @@
        settings:keywords="@string/keywords_internet"
        settings:useAdminDisabledSummary="true" />

    <com.android.settings.spa.preference.ComposePreference
    <com.android.settingslib.RestrictedPreference
        android:key="calls_and_sms"
        android:title="@string/calls_and_sms"
        android:icon="@drawable/ic_calls_sms"
        android:order="-20"
        android:summary="@string/summary_placeholder"
        android:fragment="com.android.settings.network.NetworkProviderCallsSmsFragment"
        settings:userRestriction="no_config_mobile_networks"
        settings:allowDividerBelow="true"
        settings:useAdminDisabledSummary="true"
        settings:controller="com.android.settings.network.NetworkProviderCallsSmsController" />

    <com.android.settingslib.RestrictedPreference
+1 −0
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ public class NetworkDashboardFragment extends DashboardFragment implements
        super.onAttach(context);

        use(AirplaneModePreferenceController.class).setFragment(this);
        use(NetworkProviderCallsSmsController.class).init(this);
    }

    @Override
+29 −46
Original line number Diff line number Diff line
@@ -16,35 +16,23 @@

package com.android.settings.network

import android.app.settings.SettingsEnums
import android.content.Context
import android.content.IntentFilter
import android.os.UserManager
import android.telephony.SubscriptionInfo
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.layout.Column
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.PermPhoneMsg
import androidx.compose.material3.HorizontalDivider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.LifecycleOwner
import androidx.preference.PreferenceScreen
import com.android.settings.R
import com.android.settings.core.SubSettingLauncher
import com.android.settings.spa.preference.ComposePreferenceController
import com.android.settings.core.BasePreferenceController
import com.android.settingslib.RestrictedPreference
import com.android.settingslib.Utils
import com.android.settingslib.spa.widget.preference.PreferenceModel
import com.android.settingslib.spa.widget.ui.SettingsIcon
import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle
import com.android.settingslib.spaprivileged.framework.common.broadcastReceiverFlow
import com.android.settingslib.spaprivileged.framework.common.userManager
import com.android.settingslib.spaprivileged.framework.compose.placeholder
import com.android.settingslib.spaprivileged.model.enterprise.Restrictions
import com.android.settingslib.spaprivileged.template.preference.RestrictedPreference
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
@@ -64,7 +52,14 @@ open class NetworkProviderCallsSmsController @JvmOverloads constructor(
        SubscriptionUtil.getUniqueSubscriptionDisplayName(subInfo, context)
    },
    private val isInService: (Int) -> Boolean = IsInServiceImpl(context)::isInService,
) : ComposePreferenceController(context, preferenceKey) {
) : BasePreferenceController(context, preferenceKey) {

    private lateinit var lazyViewModel: Lazy<SubscriptionInfoListViewModel>
    private lateinit var preference: RestrictedPreference

    fun init(fragment: Fragment) {
        lazyViewModel = fragment.viewModels()
    }

    override fun getAvailabilityStatus() = when {
        !SubscriptionUtil.isSimHardwareVisible(mContext) -> UNSUPPORTED_ON_DEVICE
@@ -72,35 +67,23 @@ open class NetworkProviderCallsSmsController @JvmOverloads constructor(
        else -> AVAILABLE
    }

    @Composable
    override fun Content() {
        Column {
            CallsAndSms()
            HorizontalDivider()
        }
    }

    @Composable
    private fun CallsAndSms() {
        val viewModel: SubscriptionInfoListViewModel = viewModel()
        val subscriptionInfos by viewModel.subscriptionInfoListFlow.collectAsStateWithLifecycle()
        val summary by remember { summaryFlow(viewModel.subscriptionInfoListFlow) }
            .collectAsStateWithLifecycle(initialValue = placeholder())
        RestrictedPreference(
            model = object : PreferenceModel {
                override val title = stringResource(R.string.calls_and_sms)
                override val icon = @Composable { SettingsIcon(Icons.Outlined.PermPhoneMsg) }
                override val summary = { summary }
                override val enabled = { subscriptionInfos.isNotEmpty() }
                override val onClick = {
                    SubSettingLauncher(mContext).apply {
                        setDestination(NetworkProviderCallsSmsFragment::class.qualifiedName)
                        setSourceMetricsCategory(SettingsEnums.SETTINGS_NETWORK_CATEGORY)
                    }.launch()
    override fun displayPreference(screen: PreferenceScreen) {
        super.displayPreference(screen)
        preference = screen.findPreference(preferenceKey)!!
    }

    override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) {
        val viewModel by lazyViewModel

        summaryFlow(viewModel.subscriptionInfoListFlow)
            .collectLatestWithLifecycle(viewLifecycleOwner) { preference.summary = it }

        viewModel.subscriptionInfoListFlow
            .collectLatestWithLifecycle(viewLifecycleOwner) { subscriptionInfoList ->
                if (!preference.isDisabledByAdmin) {
                    preference.isEnabled = subscriptionInfoList.isNotEmpty()
                }
            }
            },
            restrictions = Restrictions(keys = listOf(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)),
        )
    }

    private fun summaryFlow(subscriptionInfoListFlow: Flow<List<SubscriptionInfo>>) = combine(