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

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

Merge changes from topic "PreferredNetworkModeSearchItem" into main

* changes:
  Dual SIM search for Preferred network type
  Refactor MobileNetworkSettingsSearchIndex
parents 0526b754 25d5816f
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -152,6 +152,7 @@
            android:summary="@string/contact_discovery_opt_in_summary"
            settings:controller="com.android.settings.network.telephony.ContactDiscoveryPreferenceController"/>

        <!-- Settings search is handled by PreferredNetworkModeSearchItem. -->
        <ListPreference
            android:key="preferred_network_mode_key"
            android:title="@string/preferred_network_mode_title"
@@ -159,8 +160,10 @@
            android:entries="@array/preferred_network_mode_choices"
            android:entryValues="@array/preferred_network_mode_values"
            android:dialogTitle="@string/preferred_network_mode_dialogtitle"
            settings:searchable="false"
            settings:controller="com.android.settings.network.telephony.PreferredNetworkModePreferenceController"/>

        <!-- Settings search is handled by EnabledNetworkModeSearchItem. -->
        <ListPreference
            android:key="enabled_networks_key"
            android:title="@string/preferred_network_mode_title"
@@ -168,6 +171,7 @@
            android:entries="@array/enabled_networks_choices"
            android:entryValues="@array/enabled_networks_values"
            android:dialogTitle="@string/preferred_network_mode_dialogtitle"
            settings:searchable="false"
            settings:controller="com.android.settings.network.telephony.EnabledNetworkModePreferenceController"/>

        <Preference
+17 −28
Original line number Diff line number Diff line
@@ -19,9 +19,11 @@ package com.android.settings.network.telephony;
import static androidx.lifecycle.Lifecycle.Event.ON_START;
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;

import static com.android.settings.network.telephony.EnabledNetworkModePreferenceControllerHelperKt.getNetworkModePreferenceType;
import static com.android.settings.network.telephony.EnabledNetworkModePreferenceControllerHelperKt.setAllowedNetworkTypes;

import android.content.Context;
import android.content.res.Resources;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionInfo;
@@ -44,6 +46,7 @@ import androidx.preference.PreferenceScreen;

import com.android.internal.telephony.flags.Flags;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.network.AllowedNetworkTypesListener;
import com.android.settings.network.CarrierConfigCache;
import com.android.settings.network.SubscriptionsChangeListener;
@@ -61,16 +64,17 @@ import java.util.stream.Stream;
 * Preference controller for "Enabled network mode"
 */
public class EnabledNetworkModePreferenceController extends
        TelephonyBasePreferenceController implements
        BasePreferenceController implements
        ListPreference.OnPreferenceChangeListener, LifecycleObserver,
        SubscriptionsChangeListener.SubscriptionsChangeListenerClient {

    private static final String LOG_TAG = "EnabledNetworkMode";

    private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
    private AllowedNetworkTypesListener mAllowedNetworkTypesListener;
    private Preference mPreference;
    private PreferenceScreen mPreferenceScreen;
    private TelephonyManager mTelephonyManager;
    private CarrierConfigCache mCarrierConfigCache;
    private PreferenceEntriesBuilder mBuilder;
    private SubscriptionsChangeListener mSubscriptionsListener;
    private int mCallState = TelephonyManager.CALL_STATE_IDLE;
@@ -81,36 +85,16 @@ public class EnabledNetworkModePreferenceController extends
    public EnabledNetworkModePreferenceController(Context context, String key) {
        super(context, key);
        mSubscriptionsListener = new SubscriptionsChangeListener(context, this);
        mCarrierConfigCache = CarrierConfigCache.getInstance(context);
        if (mTelephonyCallback == null) {
            mTelephonyCallback = new PhoneCallStateTelephonyCallback();
        }
    }

    @Override
    public int getAvailabilityStatus(int subId) {
        boolean visible;

        final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId);
        if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
            visible = false;
        } else if (carrierConfig == null
                || !CarrierConfigManager.isConfigForIdentifiedCarrier(carrierConfig)) {
            visible = false;
        } else if (carrierConfig.getBoolean(
                CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL)
                || carrierConfig.getBoolean(
                CarrierConfigManager.KEY_HIDE_PREFERRED_NETWORK_TYPE_BOOL)) {
            visible = false;
        } else if (carrierConfig.getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL)) {
            visible = false;
        } else if (!isCallStateIdle()) {
            return AVAILABLE_UNSEARCHABLE;
        } else {
            visible = true;
        }

        return visible ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    public int getAvailabilityStatus() {
        return getNetworkModePreferenceType(mContext, mSubId)
                == NetworkModePreferenceType.EnabledNetworkMode
                ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    }

    protected boolean isCallStateIdle() {
@@ -953,9 +937,14 @@ public class EnabledNetworkModePreferenceController extends
        }
    }

    /**
     * Returns the resources associated with Subscription.
     *
     * @return Resources associated with Subscription.
     */
    @VisibleForTesting
    PhoneCallStateTelephonyCallback getTelephonyCallback() {
        return mTelephonyCallback;
    Resources getResourcesForSubId() {
        return SubscriptionManager.getResourcesForSubId(mContext, mSubId);
    }

    @Override
+65 −0
Original line number Diff line number Diff line
@@ -16,9 +16,15 @@

package com.android.settings.network.telephony

import android.content.Context
import android.telephony.CarrierConfigManager
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.android.settings.R
import com.android.settings.network.telephony.MobileNetworkSettingsSearchIndex.MobileNetworkSettingsSearchItem
import com.android.settings.network.telephony.MobileNetworkSettingsSearchIndex.MobileNetworkSettingsSearchResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@@ -33,3 +39,62 @@ fun TelephonyManager.setAllowedNetworkTypes(
        )
    }
}

enum class NetworkModePreferenceType {
    EnabledNetworkMode,
    PreferredNetworkMode,
    None,
}

fun getNetworkModePreferenceType(context: Context, subId: Int): NetworkModePreferenceType {
    if (!SubscriptionManager.isValidSubscriptionId(subId)) return NetworkModePreferenceType.None
    data class Config(
        val carrierConfigApplied: Boolean,
        val hideCarrierNetworkSettings: Boolean,
        val hidePreferredNetworkType: Boolean,
        val worldPhone: Boolean,
    )

    val config =
        CarrierConfigRepository(context).transformConfig(subId) {
            Config(
                carrierConfigApplied =
                    getBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL),
                hideCarrierNetworkSettings =
                    getBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL),
                hidePreferredNetworkType =
                    getBoolean(CarrierConfigManager.KEY_HIDE_PREFERRED_NETWORK_TYPE_BOOL),
                worldPhone = getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL),
            )
        }

    return when {
        !config.carrierConfigApplied ||
            config.hideCarrierNetworkSettings ||
            config.hidePreferredNetworkType -> NetworkModePreferenceType.None
        config.worldPhone -> NetworkModePreferenceType.PreferredNetworkMode
        else -> NetworkModePreferenceType.EnabledNetworkMode
    }
}

class PreferredNetworkModeSearchItem(private val context: Context) :
    MobileNetworkSettingsSearchItem {
    private val title: String = context.getString(R.string.preferred_network_mode_title)

    override fun getSearchResult(subId: Int): MobileNetworkSettingsSearchResult? =
        when (getNetworkModePreferenceType(context, subId)) {
            NetworkModePreferenceType.PreferredNetworkMode ->
                MobileNetworkSettingsSearchResult(
                    key = "preferred_network_mode_key",
                    title = title,
                )

            NetworkModePreferenceType.EnabledNetworkMode ->
                MobileNetworkSettingsSearchResult(
                    key = "enabled_networks_key",
                    title = title,
                )

            else -> null
        }
}
+13 −5
Original line number Diff line number Diff line
@@ -20,11 +20,13 @@ import android.content.Context
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import android.telephony.data.ApnSetting
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.LifecycleOwner
import androidx.preference.PreferenceScreen
import com.android.settings.R
import com.android.settings.Settings.MobileNetworkActivity.EXTRA_MMS_MESSAGE
import com.android.settings.core.TogglePreferenceController
import com.android.settings.network.telephony.MobileNetworkSettingsSearchIndex.MobileNetworkSettingsSearchResult
import com.android.settings.network.telephony.MobileNetworkSettingsSearchIndex.MobileNetworkSettingsSearchItem
import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle
import kotlinx.coroutines.flow.combine
@@ -109,7 +111,7 @@ constructor(
        }

        class MmsMessageSearchItem(
            context: Context,
            private val context: Context,
            private val getDefaultDataSubId: () -> Int = {
                SubscriptionManager.getDefaultDataSubscriptionId()
            },
@@ -117,12 +119,18 @@ constructor(
            private var telephonyManager: TelephonyManager =
                context.getSystemService(TelephonyManager::class.java)!!

            override val key: String = EXTRA_MMS_MESSAGE
            override val title: String = context.getString(R.string.mms_message_title)

            override fun isAvailable(subId: Int): Boolean =
            @VisibleForTesting
            fun isAvailable(subId: Int): Boolean =
                getAvailabilityStatus(
                    telephonyManager.createForSubscriptionId(subId), subId, getDefaultDataSubId)

            override fun getSearchResult(subId: Int): MobileNetworkSettingsSearchResult? {
                if (!isAvailable(subId)) return null
                return MobileNetworkSettingsSearchResult(
                    key = EXTRA_MMS_MESSAGE,
                    title = context.getString(R.string.mms_message_title),
                )
            }
        }
    }
}
+17 −15
Original line number Diff line number Diff line
@@ -39,15 +39,14 @@ class MobileNetworkSettingsSearchIndex(
    private val searchItemsFactory: (context: Context) -> List<MobileNetworkSettingsSearchItem> =
        ::createSearchItems,
) {
    interface MobileNetworkSettingsSearchItem {
        val key: String

        val title: String

        val keywords: String?
            get() = null
    data class MobileNetworkSettingsSearchResult(
        val key: String,
        val title: String,
        val keywords: String? = null,
    )

        fun isAvailable(subId: Int): Boolean
    interface MobileNetworkSettingsSearchItem {
        fun getSearchResult(subId: Int): MobileNetworkSettingsSearchResult?
    }

    fun createSearchIndexableData(): SearchIndexableData {
@@ -71,13 +70,15 @@ class MobileNetworkSettingsSearchIndex(
        searchItem: MobileNetworkSettingsSearchItem,
        subInfos: List<SubscriptionInfo>
    ): List<SearchIndexableRaw> =
        subInfos
            .filter { searchItem.isAvailable(it.subscriptionId) }
            .map { subInfo -> searchIndexableRaw(context, searchItem, subInfo) }
        subInfos.mapNotNull { subInfo ->
            searchItem.getSearchResult(subInfo.subscriptionId)?.let { searchResult ->
                searchIndexableRaw(context, searchResult, subInfo)
            }
        }

    private fun searchIndexableRaw(
        context: Context,
        searchItem: MobileNetworkSettingsSearchItem,
        searchResult: MobileNetworkSettingsSearchResult,
        subInfo: SubscriptionInfo,
    ): SearchIndexableRaw {
        val key =
@@ -85,7 +86,7 @@ class MobileNetworkSettingsSearchIndex(
                .setFragment(
                    SpaSearchLandingFragment.newBuilder()
                        .setFragmentName(MobileNetworkSettings::class.java.name)
                        .setPreferenceKey(searchItem.key)
                        .setPreferenceKey(searchResult.key)
                        .putArguments(
                            Settings.EXTRA_SUB_ID,
                            BundleValue.newBuilder().setIntValue(subInfo.subscriptionId).build()))
@@ -94,8 +95,8 @@ class MobileNetworkSettingsSearchIndex(
        return createSearchIndexableRaw(
            context = context,
            spaSearchLandingKey = key,
            itemTitle = searchItem.title,
            keywords = searchItem.keywords,
            itemTitle = searchResult.title,
            keywords = searchResult.keywords,
            indexableClass = MobileNetworkSettings::class.java,
            pageTitle = "$simsTitle > ${subInfo.displayName}",
        )
@@ -115,6 +116,7 @@ class MobileNetworkSettingsSearchIndex(
            listOf(
                MmsMessageSearchItem(context),
                NrAdvancedCallingSearchItem(context),
                PreferredNetworkModeSearchItem(context),
                RoamingSearchItem(context),
                WifiCallingSearchItem(context),
            )
Loading