Fix cold startup of NetworkProviderCallsSmsController

Network & internet is shown at the very beginning when activity
embedding is enabled.

Fix: 311604902
Test: manual - with TestDPC
Change-Id: Ic27455e34a9ba4b48b8e72fd30d5b8d9e4be7194
This commit is contained in:
Chaohui Wang
2023-12-11 16:25:14 +08:00
parent dd2cc366a6
commit fe2a106d5c
4 changed files with 66 additions and 47 deletions

View File

@@ -0,0 +1,29 @@
<!--
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>

View File

@@ -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

View File

@@ -59,6 +59,7 @@ public class NetworkDashboardFragment extends DashboardFragment implements
super.onAttach(context);
use(AirplaneModePreferenceController.class).setFragment(this);
use(NetworkProviderCallsSmsController.class).init(this);
}
@Override

View File

@@ -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()
}
override fun displayPreference(screen: PreferenceScreen) {
super.displayPreference(screen)
preference = screen.findPreference(preferenceKey)!!
}
@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 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(