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

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

Merge changes from topic "ApnTypeCheckBox" into main

* changes:
  Correct the APN type check box
  Fix cannot update APN settings in new edit page
parents 560f2499 93b0dfbf
Loading
Loading
Loading
Loading
+9 −47
Original line number Diff line number Diff line
@@ -38,17 +38,10 @@ import androidx.compose.ui.res.stringResource
import androidx.navigation.NavType
import androidx.navigation.navArgument
import com.android.settings.R
import com.android.settings.network.apn.ApnNetworkTypes.getNetworkTypeDisplayNames
import com.android.settings.network.apn.ApnNetworkTypes.getNetworkTypeSelectedOptionsState
import com.android.settings.network.apn.ApnTypes.APN_TYPES_OPTIONS
import com.android.settings.network.apn.ApnTypes.APN_TYPE_MMS
import com.android.settings.network.apn.ApnTypes.getApnTypeSelectedOptionsState
import com.android.settings.network.apn.ApnTypes.updateApnType
import com.android.settingslib.spa.framework.common.SettingsPageProvider
import com.android.settingslib.spa.framework.compose.LocalNavController
import com.android.settingslib.spa.framework.theme.SettingsDimension
import com.android.settingslib.spa.widget.editor.SettingsExposedDropdownMenuBox
import com.android.settingslib.spa.widget.editor.SettingsExposedDropdownMenuCheckBox
import com.android.settingslib.spa.widget.editor.SettingsOutlinedTextField
import com.android.settingslib.spa.widget.editor.SettingsTextFieldPassword
import com.android.settingslib.spa.widget.preference.SwitchPreference
@@ -79,7 +72,7 @@ object ApnEditPageProvider : SettingsPageProvider {
        val uriString = arguments!!.getString(URI)
        val uriInit = Uri.parse(String(Base64.getDecoder().decode(uriString)))
        val subId = arguments.getInt(SUB_ID)
        val apnDataInit = getApnDataInit(arguments, LocalContext.current, uriInit, subId)
        val apnDataInit = getApnDataInit(arguments, LocalContext.current, uriInit, subId) ?: return
        val apnDataCur = remember {
            mutableStateOf(apnDataInit)
        }
@@ -101,12 +94,7 @@ fun ApnPage(apnDataInit: ApnData, apnDataCur: MutableState<ApnData>, uriInit: Ur
    val context = LocalContext.current
    val authTypeOptions = stringArrayResource(R.array.apn_auth_entries).toList()
    val apnProtocolOptions = stringArrayResource(R.array.apn_protocol_entries).toList()
    val networkTypeSelectedOptionsState = remember {
        getNetworkTypeSelectedOptionsState(apnData.networkType)
    }
    var apnTypeSelectedOptionsState = remember {
        getApnTypeSelectedOptionsState(apnData.apnType)
    }
    var apnTypeMmsSelected by remember { mutableStateOf(false) }
    val navController = LocalNavController.current
    var valid: String?
    RegularScaffold(
@@ -114,11 +102,6 @@ fun ApnPage(apnDataInit: ApnData, apnDataCur: MutableState<ApnData>, uriInit: Ur
        actions = {
            if (!apnData.customizedConfig.readOnlyApn) {
                Button(onClick = {
                    apnData = apnData.copy(
                        networkType = ApnNetworkTypes.getNetworkType(
                            networkTypeSelectedOptionsState
                        )
                    )
                    valid = validateAndSaveApnData(
                        apnDataInit,
                        apnData,
@@ -193,27 +176,12 @@ fun ApnPage(apnDataInit: ApnData, apnDataCur: MutableState<ApnData>, uriInit: Ur
                label = stringResource(R.string.apn_server),
                enabled = apnData.serverEnabled
            ) { apnData = apnData.copy(server = it) }
            SettingsExposedDropdownMenuCheckBox(
                label = stringResource(R.string.apn_type),
                options = APN_TYPES_OPTIONS,
                selectedOptionsState = apnTypeSelectedOptionsState,
                enabled = apnData.apnTypeEnabled,
                errorMessage = validateAPNType(
                    apnData.validEnabled, apnData.apnType,
                    apnData.customizedConfig.readOnlyApnTypes, context
            ApnTypeCheckBox(
                apnData = apnData,
                onTypeChanged = { apnData = apnData.copy(apnType = it) },
                onMmsSelectedChanged = { apnTypeMmsSelected = it },
            )
            ) {
                val apnType = updateApnType(
                    apnTypeSelectedOptionsState,
                    apnData.customizedConfig.defaultApnTypes,
                    apnData.customizedConfig.readOnlyApnTypes
                )
                apnTypeSelectedOptionsState = getApnTypeSelectedOptionsState(apnType)
                apnData = apnData.copy(
                    apnType = apnType
                )
            }
            if (apnTypeSelectedOptionsState.contains(APN_TYPES_OPTIONS.indexOf(APN_TYPE_MMS))) {
            if (apnTypeMmsSelected) {
                SettingsOutlinedTextField(
                    value = apnData.mmsc,
                    label = stringResource(R.string.apn_mmsc),
@@ -249,13 +217,7 @@ fun ApnPage(apnDataInit: ApnData, apnDataCur: MutableState<ApnData>, uriInit: Ur
                selectedOptionIndex = apnData.apnRoaming,
                enabled = apnData.apnRoamingEnabled
            ) { apnData = apnData.copy(apnRoaming = it) }
            SettingsExposedDropdownMenuCheckBox(
                label = stringResource(R.string.network_type),
                options = getNetworkTypeDisplayNames(),
                selectedOptionsState = networkTypeSelectedOptionsState,
                emptyVal = stringResource(R.string.network_type_unspecified),
                enabled = apnData.networkTypeEnabled
            ) {}
            ApnNetworkTypeCheckBox(apnData) { apnData = apnData.copy(networkType = it) }
            SwitchPreference(
                object : SwitchPreferenceModel {
                    override val title = context.resources.getString(R.string.carrier_enabled)
+41 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.apn

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.res.stringResource
import com.android.settings.R
import com.android.settingslib.spa.widget.editor.SettingsDropdownCheckBox

@Composable
fun ApnNetworkTypeCheckBox(apnData: ApnData, onNetworkTypeChanged: (Long) -> Unit) {
    val options = remember { ApnNetworkTypes.getNetworkTypeOptions() }
    val selectedStateMap = remember {
        ApnNetworkTypes.networkTypeToSelectedStateMap(options, apnData.networkType)
    }
    SettingsDropdownCheckBox(
        label = stringResource(R.string.network_type),
        options = options,
        emptyText = stringResource(R.string.network_type_unspecified),
        enabled = apnData.networkTypeEnabled,
    ) {
        onNetworkTypeChanged(
            ApnNetworkTypes.selectedStateMapToNetworkType(options, selectedStateMap)
        )
    }
}
+21 −12
Original line number Diff line number Diff line
@@ -17,8 +17,9 @@
package com.android.settings.network.apn

import android.telephony.TelephonyManager
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.snapshots.SnapshotStateMap
import com.android.settingslib.spa.widget.editor.SettingsDropdownCheckOption

object ApnNetworkTypes {
    private val Types = listOf(
@@ -39,32 +40,40 @@ object ApnNetworkTypes {
        TelephonyManager.NETWORK_TYPE_NR,
    )

    fun getNetworkTypeDisplayNames(): List<String> =
        Types.map { TelephonyManager.getNetworkTypeName(it) }
    fun getNetworkTypeOptions(): List<SettingsDropdownCheckOption> =
        Types.map { SettingsDropdownCheckOption(TelephonyManager.getNetworkTypeName(it)) }

    /**
     * Gets the selected Network type Selected Options according to network type.
     * @param networkType Initialized network type bitmask, often multiple network type options may
     *                    be included.
     */
    fun getNetworkTypeSelectedOptionsState(networkType: Long): SnapshotStateList<Int> {
        val networkTypeSelectedOptionsState = mutableStateListOf<Int>()
    fun networkTypeToSelectedStateMap(
        options: List<SettingsDropdownCheckOption>,
        networkType: Long,
    ): SnapshotStateMap<SettingsDropdownCheckOption, Boolean> {
        val stateMap = mutableStateMapOf<SettingsDropdownCheckOption, Boolean>()
        Types.forEachIndexed { index, type ->
            if (networkType and TelephonyManager.getBitMaskForNetworkType(type) != 0L) {
                networkTypeSelectedOptionsState.add(index)
                stateMap[options[index]] = true
            }
        }
        return networkTypeSelectedOptionsState
        return stateMap
    }

    /**
     * Gets the network type according to the selected Network type Selected Options.
     * @param networkTypeSelectedOptionsState the selected Network type Selected Options.
     * @param stateMap the selected Network type Selected Options.
     */
    fun getNetworkType(networkTypeSelectedOptionsState: SnapshotStateList<Int>): Long {
    fun selectedStateMapToNetworkType(
        options: List<SettingsDropdownCheckOption>,
        stateMap: SnapshotStateMap<SettingsDropdownCheckOption, Boolean>,
    ): Long {
        var networkType = 0L
        networkTypeSelectedOptionsState.forEach { option ->
            networkType = networkType or TelephonyManager.getBitMaskForNetworkType(Types[option])
        options.forEachIndexed { index, option ->
            if (stateMap[option] == true) {
                networkType = networkType or TelephonyManager.getBitMaskForNetworkType(Types[index])
            }
        }
        return networkType
    }
+49 −100
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.network.apn

import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.Telephony
import android.telephony.SubscriptionManager
@@ -27,26 +28,7 @@ import com.android.settings.R
import com.android.settingslib.utils.ThreadUtils
import java.util.Locale

const val NAME_INDEX = 1
const val APN_INDEX = 2
const val PROXY_INDEX = 3
const val PORT_INDEX = 4
const val USER_INDEX = 5
const val SERVER_INDEX = 6
const val PASSWORD_INDEX = 7
const val MMSC_INDEX = 8
const val MMSPROXY_INDEX = 9
const val MMSPORT_INDEX = 10
const val AUTH_TYPE_INDEX = 11
const val TYPE_INDEX = 12
const val PROTOCOL_INDEX = 13
const val CARRIER_ENABLED_INDEX = 14
const val NETWORK_TYPE_INDEX = 15
const val ROAMING_PROTOCOL_INDEX = 16
const val EDITED_INDEX = 17
const val USER_EDITABLE_INDEX = 18

val sProjection = arrayOf(
val Projection = arrayOf(
    Telephony.Carriers._ID,  // 0
    Telephony.Carriers.NAME,  // 1
    Telephony.Carriers.APN,  // 2
@@ -68,7 +50,7 @@ val sProjection = arrayOf(
    Telephony.Carriers.USER_EDITABLE,  // 18
)

const val TAG = "ApnRepository"
private const val TAG = "ApnRepository"

/**
 * Query apn related information based on uri.
@@ -79,56 +61,39 @@ const val TAG = "ApnRepository"
fun getApnDataFromUri(uri: Uri, context: Context): ApnData {
    var apnData = ApnData()
    val contentResolver = context.contentResolver
    val apnProtocolOptions = context.resources.getStringArray(R.array.apn_protocol_entries).toList()

    contentResolver.query(
        uri,
        sProjection,
        Projection,
        null /* selection */,
        null /* selectionArgs */,
        null /* sortOrder */
    ).use { cursor ->
        if (cursor != null && cursor.moveToFirst()) {
            val name = cursor.getString(NAME_INDEX)
            val apn = cursor.getString(APN_INDEX)
            val proxy = cursor.getString(PROXY_INDEX)
            val port = cursor.getString(PORT_INDEX)
            val userName = cursor.getString(USER_INDEX)
            val server = cursor.getString(SERVER_INDEX)
            val passWord = cursor.getString(PASSWORD_INDEX)
            val mmsc = cursor.getString(MMSC_INDEX)
            val mmsProxy = cursor.getString(MMSPROXY_INDEX)
            val mmsPort = cursor.getString(MMSPORT_INDEX)
            val authType = cursor.getInt(AUTH_TYPE_INDEX)
            val apnType = cursor.getString(TYPE_INDEX)
            val apnProtocol = convertProtocol2Options(cursor.getString(PROTOCOL_INDEX), context)
            val apnRoaming =
                convertProtocol2Options(cursor.getString(ROAMING_PROTOCOL_INDEX), context)
            val apnEnable = cursor.getInt(CARRIER_ENABLED_INDEX) == 1
            val networkType = cursor.getLong(NETWORK_TYPE_INDEX)

            val edited = cursor.getInt(EDITED_INDEX)
            val userEditable = cursor.getInt(USER_EDITABLE_INDEX)

            apnData = apnData.copy(
                name = name,
                apn = apn,
                proxy = proxy,
                port = port,
                userName = userName,
                passWord = passWord,
                server = server,
                mmsc = mmsc,
                mmsProxy = mmsProxy,
                mmsPort = mmsPort,
                authType = authType,
                apnType = apnType,
                apnProtocol = apnProtocolOptions.indexOf(apnProtocol),
                apnRoaming = apnProtocolOptions.indexOf(apnRoaming),
                apnEnable = apnEnable,
                networkType = networkType,
                edited = edited,
                userEditable = userEditable,
            apnData = ApnData(
                id = cursor.getInt(Telephony.Carriers._ID),
                name = cursor.getString(Telephony.Carriers.NAME),
                apn = cursor.getString(Telephony.Carriers.APN),
                proxy = cursor.getString(Telephony.Carriers.PROXY),
                port = cursor.getString(Telephony.Carriers.PORT),
                userName = cursor.getString(Telephony.Carriers.USER),
                passWord = cursor.getString(Telephony.Carriers.PASSWORD),
                server = cursor.getString(Telephony.Carriers.SERVER),
                mmsc = cursor.getString(Telephony.Carriers.MMSC),
                mmsProxy = cursor.getString(Telephony.Carriers.MMSPROXY),
                mmsPort = cursor.getString(Telephony.Carriers.MMSPORT),
                authType = cursor.getInt(Telephony.Carriers.AUTH_TYPE),
                apnType = cursor.getString(Telephony.Carriers.TYPE),
                apnProtocol = context.convertProtocol2Options(
                    cursor.getString(Telephony.Carriers.PROTOCOL)
                ),
                apnRoaming = context.convertProtocol2Options(
                    cursor.getString(Telephony.Carriers.ROAMING_PROTOCOL)
                ),
                apnEnable = cursor.getInt(Telephony.Carriers.CARRIER_ENABLED) == 1,
                networkType = cursor.getLong(Telephony.Carriers.NETWORK_TYPE_BITMASK),
                edited = cursor.getInt(Telephony.Carriers.EDITED_STATUS),
                userEditable = cursor.getInt(Telephony.Carriers.USER_EDITABLE),
            )
        }
    }
@@ -138,42 +103,23 @@ fun getApnDataFromUri(uri: Uri, context: Context): ApnData {
    return apnData
}

private fun Cursor.getString(columnName: String) = getString(getColumnIndexOrThrow(columnName))
private fun Cursor.getInt(columnName: String) = getInt(getColumnIndexOrThrow(columnName))
private fun Cursor.getLong(columnName: String) = getLong(getColumnIndexOrThrow(columnName))

/**
 * Returns The UI choice (e.g., "IPv4/IPv6") corresponding to the given
 * raw value of the protocol preference (e.g., "IPV4V6"). If unknown,
 * return null.
 *
 * @return UI choice
 * Returns The UI choice index corresponding to the given raw value of the protocol preference
 * (e.g., "IPV4V6").
 * If unknown, return -1.
 */
private fun convertProtocol2Options(raw: String, context: Context): String {
    val apnProtocolOptions = context.resources.getStringArray(R.array.apn_protocol_entries).toList()
    val apnProtocolValues = context.resources.getStringArray(R.array.apn_protocol_values).toList()
    var uRaw = raw.uppercase(Locale.getDefault())
    uRaw = if (uRaw == "IPV4") "IP" else uRaw
    val protocolIndex = apnProtocolValues.indexOf(uRaw)
    return if (protocolIndex == -1) {
        ""
    } else {
        try {
            apnProtocolOptions[protocolIndex]
        } catch (e: ArrayIndexOutOfBoundsException) {
            ""
        }
    }
private fun Context.convertProtocol2Options(protocol: String): Int {
    var normalizedProtocol = protocol.uppercase(Locale.getDefault())
    if (normalizedProtocol == "IPV4") normalizedProtocol = "IP"
    return resources.getStringArray(R.array.apn_protocol_values).indexOf(normalizedProtocol)
}

fun convertOptions2Protocol(protocolIndex: Int, context: Context): String {
    val apnProtocolValues = context.resources.getStringArray(R.array.apn_protocol_values).toList()
    return if (protocolIndex == -1) {
        ""
    } else {
        try {
            apnProtocolValues[protocolIndex]
        } catch (e: ArrayIndexOutOfBoundsException) {
            ""
        }
    }
}
fun Context.convertOptions2Protocol(protocolIndex: Int): String =
    resources.getStringArray(R.array.apn_protocol_values).getOrElse(protocolIndex) { "" }

fun updateApnDataToDatabase(
    newApn: Boolean,
@@ -183,13 +129,13 @@ fun updateApnDataToDatabase(
) {
    ThreadUtils.postOnBackgroundThread {
        if (newApn) {
            // Add a new apn to the database
            Log.d(TAG, "Adding an new APN to the database $uriInit $values")
            val newUri = context.contentResolver.insert(uriInit, values)
            if (newUri == null) {
                Log.e(TAG, "Can't add a new apn to database $uriInit")
            }
        } else {
            // Update the existing apn
            Log.d(TAG, "Updating an existing APN to the database $uriInit $values")
            context.contentResolver.update(
                uriInit, values, null /* where */, null /* selection Args */
            )
@@ -210,9 +156,12 @@ private val NonDuplicatedKeys = setOf(
)

fun isItemExist(apnData: ApnData, context: Context): String? {
    val contentValueMap = apnData.getContentValueMap(context).filterKeys { it in NonDuplicatedKeys }
    val list = contentValueMap.entries.toList()
    val selection = list.joinToString(" AND ") { "${it.key} = ?" }
    val selectionMap = apnData.getContentValueMap(context).filterKeys { it in NonDuplicatedKeys }
        .mapKeys { "${it.key} = ?" }
        .toMutableMap()
    if (apnData.id != -1) selectionMap += "${Telephony.Carriers._ID} != ?" to apnData.id
    val list = selectionMap.entries.toList()
    val selection = list.joinToString(" AND ") { it.key }
    val selectionArgs: Array<String> = list.map { it.value.toString() }.toTypedArray()
    context.contentResolver.query(
        Uri.withAppendedPath(Telephony.Carriers.SIM_APN_URI, apnData.subId.toString()),
+60 −152

File changed.

Preview size limit exceeded, changes collapsed.

Loading