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

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

Merge "New MobileDataEnabledFlow" into main

parents fead9257 9e72afdc
Loading
Loading
Loading
Loading
+6 −24
Original line number Diff line number Diff line
@@ -31,9 +31,10 @@ import androidx.preference.Preference
import com.android.settings.R
import com.android.settings.datausage.lib.BillingCycleRepository
import com.android.settings.datausage.lib.NetworkUsageData
import com.android.settings.network.MobileDataEnabledListener
import com.android.settings.network.MobileNetworkRepository
import com.android.settings.network.mobileDataEnabledFlow
import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity
import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle
import com.android.settingslib.spaprivileged.framework.common.userManager
import com.android.settingslib.utils.ThreadUtils
import kotlin.jvm.optionals.getOrNull
@@ -43,10 +44,7 @@ import kotlin.jvm.optionals.getOrNull
 * to inspect based on usage cycle and control through [NetworkPolicy].
 */
@OpenForTesting
open class DataUsageList : DataUsageBaseFragment(), MobileDataEnabledListener.Client {
    @VisibleForTesting
    lateinit var dataStateListener: MobileDataEnabledListener

open class DataUsageList : DataUsageBaseFragment() {
    @JvmField
    @VisibleForTesting
    var template: NetworkTemplate? = null
@@ -89,7 +87,6 @@ open class DataUsageList : DataUsageBaseFragment(), MobileDataEnabledListener.Cl
            return
        }
        updateSubscriptionInfoEntity()
        dataStateListener = MobileDataEnabledListener(activity, this)
        dataUsageListAppsController = use(DataUsageListAppsController::class.java).apply {
            init(template)
        }
@@ -103,6 +100,9 @@ open class DataUsageList : DataUsageBaseFragment(), MobileDataEnabledListener.Cl
    override fun onViewCreated(v: View, savedInstanceState: Bundle?) {
        super.onViewCreated(v, savedInstanceState)

        requireContext().mobileDataEnabledFlow(subId)
            .collectLatestWithLifecycle(viewLifecycleOwner) { updatePolicy() }

        val template = template ?: return
        dataUsageListHeaderController = DataUsageListHeaderController(
            setPinnedHeaderView(R.layout.apps_filter_spinner),
@@ -114,17 +114,6 @@ open class DataUsageList : DataUsageBaseFragment(), MobileDataEnabledListener.Cl
        )
    }

    override fun onResume() {
        super.onResume()
        dataStateListener.start(subId)
        updatePolicy()
    }

    override fun onPause() {
        super.onPause()
        dataStateListener.stop()
    }

    override fun getPreferenceScreenResId() = R.xml.data_usage_list

    override fun getLogTag() = TAG
@@ -154,13 +143,6 @@ open class DataUsageList : DataUsageBaseFragment(), MobileDataEnabledListener.Cl
        }
    }

    /**
     * Implementation of `MobileDataEnabledListener.Client`
     */
    override fun onMobileDataEnabledChange() {
        updatePolicy()
    }

    /** Update chart sweeps and cycle list to reflect [NetworkPolicy] for current [template]. */
    @VisibleForTesting
    fun updatePolicy() {
+43 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.network

import android.content.Context
import android.provider.Settings
import android.telephony.SubscriptionManager
import com.android.settingslib.spaprivileged.settingsprovider.settingsGlobalChangeFlow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.merge

/**
 * Flow for mobile data enabled changed event.
 *
 * Note: This flow can only notify enabled status changes, cannot provide the latest status.
 */
fun Context.mobileDataEnabledFlow(subId: Int): Flow<Unit> {
    val flow = settingsGlobalChangeFlow(Settings.Global.MOBILE_DATA)
    return when (subId) {
        SubscriptionManager.INVALID_SUBSCRIPTION_ID -> flow
        else -> {
            val subIdFlow = settingsGlobalChangeFlow(
                name = Settings.Global.MOBILE_DATA + subId,
                sendInitialValue = false,
            )
            merge(flow, subIdFlow)
        }
    }
}
+6 −1
Original line number Diff line number Diff line
@@ -20,7 +20,12 @@ import android.content.Context;
import android.provider.Settings;
import android.telephony.SubscriptionManager;

/** Helper class to listen for changes in the enabled state of mobile data. */
/**
 *  Helper class to listen for changes in the enabled state of mobile data.
 *
 * @deprecated use {@link MobileDataEnabledFlowKt} instead
 */
@Deprecated
public class MobileDataEnabledListener {
    private Context mContext;
    private Client mClient;
+9 −10
Original line number Diff line number Diff line
@@ -28,15 +28,16 @@ import com.android.settings.SettingsPreferenceFragment
import com.android.settings.dashboard.DashboardFragment
import com.android.settings.network.telephony.MobileNetworkUtils
import com.android.settings.search.BaseSearchIndexProvider
import com.android.settings.utils.observeSettingsGlobalBoolean
import com.android.settingslib.search.SearchIndexable
import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle
import com.android.settingslib.spaprivileged.framework.common.userManager
import com.android.settingslib.spaprivileged.settingsprovider.settingsGlobalBooleanFlow

@SearchIndexable(forTarget = SearchIndexable.ALL and SearchIndexable.ARC.inv())
class MobileNetworkListFragment : DashboardFragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        observeAirplaneModeAndFinishIfOn()
        collectAirplaneModeAndFinishIfOn()
    }

    override fun onResume() {
@@ -59,11 +60,9 @@ class MobileNetworkListFragment : DashboardFragment() {
        private const val KEY_ADD_SIM = "add_sim"

        @JvmStatic
        fun SettingsPreferenceFragment.observeAirplaneModeAndFinishIfOn() {
            requireContext().observeSettingsGlobalBoolean(
                name = Settings.Global.AIRPLANE_MODE_ON,
                lifecycle = viewLifecycleOwner.lifecycle,
            ) { isAirplaneModeOn: Boolean ->
        fun SettingsPreferenceFragment.collectAirplaneModeAndFinishIfOn() {
            requireContext().settingsGlobalBooleanFlow(Settings.Global.AIRPLANE_MODE_ON)
                .collectLatestWithLifecycle(viewLifecycleOwner) { isAirplaneModeOn ->
                    if (isAirplaneModeOn) {
                        finish()
                    }
+2 −2
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

package com.android.settings.network.telephony;

import static com.android.settings.network.MobileNetworkListFragment.observeAirplaneModeAndFinishIfOn;
import static com.android.settings.network.MobileNetworkListFragment.collectAirplaneModeAndFinishIfOn;

import android.app.Activity;
import android.app.settings.SettingsEnums;
@@ -334,7 +334,7 @@ public class MobileNetworkSettings extends AbstractMobileNetworkSettings impleme
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        observeAirplaneModeAndFinishIfOn(this);
        collectAirplaneModeAndFinishIfOn(this);
    }

    @Override
Loading