Merge changes from topic "ApnTypeCheckBox" into main
* changes: Correct the APN type check box Fix cannot update APN settings in new edit page
This commit is contained in:
@@ -38,17 +38,10 @@ import androidx.compose.ui.res.stringResource
|
|||||||
import androidx.navigation.NavType
|
import androidx.navigation.NavType
|
||||||
import androidx.navigation.navArgument
|
import androidx.navigation.navArgument
|
||||||
import com.android.settings.R
|
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.common.SettingsPageProvider
|
||||||
import com.android.settingslib.spa.framework.compose.LocalNavController
|
import com.android.settingslib.spa.framework.compose.LocalNavController
|
||||||
import com.android.settingslib.spa.framework.theme.SettingsDimension
|
import com.android.settingslib.spa.framework.theme.SettingsDimension
|
||||||
import com.android.settingslib.spa.widget.editor.SettingsExposedDropdownMenuBox
|
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.SettingsOutlinedTextField
|
||||||
import com.android.settingslib.spa.widget.editor.SettingsTextFieldPassword
|
import com.android.settingslib.spa.widget.editor.SettingsTextFieldPassword
|
||||||
import com.android.settingslib.spa.widget.preference.SwitchPreference
|
import com.android.settingslib.spa.widget.preference.SwitchPreference
|
||||||
@@ -79,7 +72,7 @@ object ApnEditPageProvider : SettingsPageProvider {
|
|||||||
val uriString = arguments!!.getString(URI)
|
val uriString = arguments!!.getString(URI)
|
||||||
val uriInit = Uri.parse(String(Base64.getDecoder().decode(uriString)))
|
val uriInit = Uri.parse(String(Base64.getDecoder().decode(uriString)))
|
||||||
val subId = arguments.getInt(SUB_ID)
|
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 {
|
val apnDataCur = remember {
|
||||||
mutableStateOf(apnDataInit)
|
mutableStateOf(apnDataInit)
|
||||||
}
|
}
|
||||||
@@ -101,12 +94,7 @@ fun ApnPage(apnDataInit: ApnData, apnDataCur: MutableState<ApnData>, uriInit: Ur
|
|||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val authTypeOptions = stringArrayResource(R.array.apn_auth_entries).toList()
|
val authTypeOptions = stringArrayResource(R.array.apn_auth_entries).toList()
|
||||||
val apnProtocolOptions = stringArrayResource(R.array.apn_protocol_entries).toList()
|
val apnProtocolOptions = stringArrayResource(R.array.apn_protocol_entries).toList()
|
||||||
val networkTypeSelectedOptionsState = remember {
|
var apnTypeMmsSelected by remember { mutableStateOf(false) }
|
||||||
getNetworkTypeSelectedOptionsState(apnData.networkType)
|
|
||||||
}
|
|
||||||
var apnTypeSelectedOptionsState = remember {
|
|
||||||
getApnTypeSelectedOptionsState(apnData.apnType)
|
|
||||||
}
|
|
||||||
val navController = LocalNavController.current
|
val navController = LocalNavController.current
|
||||||
var valid: String?
|
var valid: String?
|
||||||
RegularScaffold(
|
RegularScaffold(
|
||||||
@@ -114,11 +102,6 @@ fun ApnPage(apnDataInit: ApnData, apnDataCur: MutableState<ApnData>, uriInit: Ur
|
|||||||
actions = {
|
actions = {
|
||||||
if (!apnData.customizedConfig.readOnlyApn) {
|
if (!apnData.customizedConfig.readOnlyApn) {
|
||||||
Button(onClick = {
|
Button(onClick = {
|
||||||
apnData = apnData.copy(
|
|
||||||
networkType = ApnNetworkTypes.getNetworkType(
|
|
||||||
networkTypeSelectedOptionsState
|
|
||||||
)
|
|
||||||
)
|
|
||||||
valid = validateAndSaveApnData(
|
valid = validateAndSaveApnData(
|
||||||
apnDataInit,
|
apnDataInit,
|
||||||
apnData,
|
apnData,
|
||||||
@@ -193,27 +176,12 @@ fun ApnPage(apnDataInit: ApnData, apnDataCur: MutableState<ApnData>, uriInit: Ur
|
|||||||
label = stringResource(R.string.apn_server),
|
label = stringResource(R.string.apn_server),
|
||||||
enabled = apnData.serverEnabled
|
enabled = apnData.serverEnabled
|
||||||
) { apnData = apnData.copy(server = it) }
|
) { apnData = apnData.copy(server = it) }
|
||||||
SettingsExposedDropdownMenuCheckBox(
|
ApnTypeCheckBox(
|
||||||
label = stringResource(R.string.apn_type),
|
apnData = apnData,
|
||||||
options = APN_TYPES_OPTIONS,
|
onTypeChanged = { apnData = apnData.copy(apnType = it) },
|
||||||
selectedOptionsState = apnTypeSelectedOptionsState,
|
onMmsSelectedChanged = { apnTypeMmsSelected = it },
|
||||||
enabled = apnData.apnTypeEnabled,
|
|
||||||
errorMessage = validateAPNType(
|
|
||||||
apnData.validEnabled, apnData.apnType,
|
|
||||||
apnData.customizedConfig.readOnlyApnTypes, context
|
|
||||||
)
|
)
|
||||||
) {
|
if (apnTypeMmsSelected) {
|
||||||
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))) {
|
|
||||||
SettingsOutlinedTextField(
|
SettingsOutlinedTextField(
|
||||||
value = apnData.mmsc,
|
value = apnData.mmsc,
|
||||||
label = stringResource(R.string.apn_mmsc),
|
label = stringResource(R.string.apn_mmsc),
|
||||||
@@ -249,13 +217,7 @@ fun ApnPage(apnDataInit: ApnData, apnDataCur: MutableState<ApnData>, uriInit: Ur
|
|||||||
selectedOptionIndex = apnData.apnRoaming,
|
selectedOptionIndex = apnData.apnRoaming,
|
||||||
enabled = apnData.apnRoamingEnabled
|
enabled = apnData.apnRoamingEnabled
|
||||||
) { apnData = apnData.copy(apnRoaming = it) }
|
) { apnData = apnData.copy(apnRoaming = it) }
|
||||||
SettingsExposedDropdownMenuCheckBox(
|
ApnNetworkTypeCheckBox(apnData) { apnData = apnData.copy(networkType = it) }
|
||||||
label = stringResource(R.string.network_type),
|
|
||||||
options = getNetworkTypeDisplayNames(),
|
|
||||||
selectedOptionsState = networkTypeSelectedOptionsState,
|
|
||||||
emptyVal = stringResource(R.string.network_type_unspecified),
|
|
||||||
enabled = apnData.networkTypeEnabled
|
|
||||||
) {}
|
|
||||||
SwitchPreference(
|
SwitchPreference(
|
||||||
object : SwitchPreferenceModel {
|
object : SwitchPreferenceModel {
|
||||||
override val title = context.resources.getString(R.string.carrier_enabled)
|
override val title = context.resources.getString(R.string.carrier_enabled)
|
||||||
|
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* 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)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@@ -17,8 +17,9 @@
|
|||||||
package com.android.settings.network.apn
|
package com.android.settings.network.apn
|
||||||
|
|
||||||
import android.telephony.TelephonyManager
|
import android.telephony.TelephonyManager
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
import androidx.compose.runtime.mutableStateMapOf
|
||||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
import androidx.compose.runtime.snapshots.SnapshotStateMap
|
||||||
|
import com.android.settingslib.spa.widget.editor.SettingsDropdownCheckOption
|
||||||
|
|
||||||
object ApnNetworkTypes {
|
object ApnNetworkTypes {
|
||||||
private val Types = listOf(
|
private val Types = listOf(
|
||||||
@@ -39,32 +40,40 @@ object ApnNetworkTypes {
|
|||||||
TelephonyManager.NETWORK_TYPE_NR,
|
TelephonyManager.NETWORK_TYPE_NR,
|
||||||
)
|
)
|
||||||
|
|
||||||
fun getNetworkTypeDisplayNames(): List<String> =
|
fun getNetworkTypeOptions(): List<SettingsDropdownCheckOption> =
|
||||||
Types.map { TelephonyManager.getNetworkTypeName(it) }
|
Types.map { SettingsDropdownCheckOption(TelephonyManager.getNetworkTypeName(it)) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the selected Network type Selected Options according to network type.
|
* Gets the selected Network type Selected Options according to network type.
|
||||||
* @param networkType Initialized network type bitmask, often multiple network type options may
|
* @param networkType Initialized network type bitmask, often multiple network type options may
|
||||||
* be included.
|
* be included.
|
||||||
*/
|
*/
|
||||||
fun getNetworkTypeSelectedOptionsState(networkType: Long): SnapshotStateList<Int> {
|
fun networkTypeToSelectedStateMap(
|
||||||
val networkTypeSelectedOptionsState = mutableStateListOf<Int>()
|
options: List<SettingsDropdownCheckOption>,
|
||||||
|
networkType: Long,
|
||||||
|
): SnapshotStateMap<SettingsDropdownCheckOption, Boolean> {
|
||||||
|
val stateMap = mutableStateMapOf<SettingsDropdownCheckOption, Boolean>()
|
||||||
Types.forEachIndexed { index, type ->
|
Types.forEachIndexed { index, type ->
|
||||||
if (networkType and TelephonyManager.getBitMaskForNetworkType(type) != 0L) {
|
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.
|
* 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
|
var networkType = 0L
|
||||||
networkTypeSelectedOptionsState.forEach { option ->
|
options.forEachIndexed { index, option ->
|
||||||
networkType = networkType or TelephonyManager.getBitMaskForNetworkType(Types[option])
|
if (stateMap[option] == true) {
|
||||||
|
networkType = networkType or TelephonyManager.getBitMaskForNetworkType(Types[index])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return networkType
|
return networkType
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,7 @@ package com.android.settings.network.apn
|
|||||||
|
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.database.Cursor
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import android.telephony.SubscriptionManager
|
import android.telephony.SubscriptionManager
|
||||||
@@ -27,26 +28,7 @@ import com.android.settings.R
|
|||||||
import com.android.settingslib.utils.ThreadUtils
|
import com.android.settingslib.utils.ThreadUtils
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
const val NAME_INDEX = 1
|
val Projection = arrayOf(
|
||||||
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(
|
|
||||||
Telephony.Carriers._ID, // 0
|
Telephony.Carriers._ID, // 0
|
||||||
Telephony.Carriers.NAME, // 1
|
Telephony.Carriers.NAME, // 1
|
||||||
Telephony.Carriers.APN, // 2
|
Telephony.Carriers.APN, // 2
|
||||||
@@ -68,7 +50,7 @@ val sProjection = arrayOf(
|
|||||||
Telephony.Carriers.USER_EDITABLE, // 18
|
Telephony.Carriers.USER_EDITABLE, // 18
|
||||||
)
|
)
|
||||||
|
|
||||||
const val TAG = "ApnRepository"
|
private const val TAG = "ApnRepository"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query apn related information based on uri.
|
* Query apn related information based on uri.
|
||||||
@@ -79,56 +61,39 @@ const val TAG = "ApnRepository"
|
|||||||
fun getApnDataFromUri(uri: Uri, context: Context): ApnData {
|
fun getApnDataFromUri(uri: Uri, context: Context): ApnData {
|
||||||
var apnData = ApnData()
|
var apnData = ApnData()
|
||||||
val contentResolver = context.contentResolver
|
val contentResolver = context.contentResolver
|
||||||
val apnProtocolOptions = context.resources.getStringArray(R.array.apn_protocol_entries).toList()
|
|
||||||
|
|
||||||
contentResolver.query(
|
contentResolver.query(
|
||||||
uri,
|
uri,
|
||||||
sProjection,
|
Projection,
|
||||||
null /* selection */,
|
null /* selection */,
|
||||||
null /* selectionArgs */,
|
null /* selectionArgs */,
|
||||||
null /* sortOrder */
|
null /* sortOrder */
|
||||||
).use { cursor ->
|
).use { cursor ->
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
val name = cursor.getString(NAME_INDEX)
|
apnData = ApnData(
|
||||||
val apn = cursor.getString(APN_INDEX)
|
id = cursor.getInt(Telephony.Carriers._ID),
|
||||||
val proxy = cursor.getString(PROXY_INDEX)
|
name = cursor.getString(Telephony.Carriers.NAME),
|
||||||
val port = cursor.getString(PORT_INDEX)
|
apn = cursor.getString(Telephony.Carriers.APN),
|
||||||
val userName = cursor.getString(USER_INDEX)
|
proxy = cursor.getString(Telephony.Carriers.PROXY),
|
||||||
val server = cursor.getString(SERVER_INDEX)
|
port = cursor.getString(Telephony.Carriers.PORT),
|
||||||
val passWord = cursor.getString(PASSWORD_INDEX)
|
userName = cursor.getString(Telephony.Carriers.USER),
|
||||||
val mmsc = cursor.getString(MMSC_INDEX)
|
passWord = cursor.getString(Telephony.Carriers.PASSWORD),
|
||||||
val mmsProxy = cursor.getString(MMSPROXY_INDEX)
|
server = cursor.getString(Telephony.Carriers.SERVER),
|
||||||
val mmsPort = cursor.getString(MMSPORT_INDEX)
|
mmsc = cursor.getString(Telephony.Carriers.MMSC),
|
||||||
val authType = cursor.getInt(AUTH_TYPE_INDEX)
|
mmsProxy = cursor.getString(Telephony.Carriers.MMSPROXY),
|
||||||
val apnType = cursor.getString(TYPE_INDEX)
|
mmsPort = cursor.getString(Telephony.Carriers.MMSPORT),
|
||||||
val apnProtocol = convertProtocol2Options(cursor.getString(PROTOCOL_INDEX), context)
|
authType = cursor.getInt(Telephony.Carriers.AUTH_TYPE),
|
||||||
val apnRoaming =
|
apnType = cursor.getString(Telephony.Carriers.TYPE),
|
||||||
convertProtocol2Options(cursor.getString(ROAMING_PROTOCOL_INDEX), context)
|
apnProtocol = context.convertProtocol2Options(
|
||||||
val apnEnable = cursor.getInt(CARRIER_ENABLED_INDEX) == 1
|
cursor.getString(Telephony.Carriers.PROTOCOL)
|
||||||
val networkType = cursor.getLong(NETWORK_TYPE_INDEX)
|
),
|
||||||
|
apnRoaming = context.convertProtocol2Options(
|
||||||
val edited = cursor.getInt(EDITED_INDEX)
|
cursor.getString(Telephony.Carriers.ROAMING_PROTOCOL)
|
||||||
val userEditable = cursor.getInt(USER_EDITABLE_INDEX)
|
),
|
||||||
|
apnEnable = cursor.getInt(Telephony.Carriers.CARRIER_ENABLED) == 1,
|
||||||
apnData = apnData.copy(
|
networkType = cursor.getLong(Telephony.Carriers.NETWORK_TYPE_BITMASK),
|
||||||
name = name,
|
edited = cursor.getInt(Telephony.Carriers.EDITED_STATUS),
|
||||||
apn = apn,
|
userEditable = cursor.getInt(Telephony.Carriers.USER_EDITABLE),
|
||||||
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,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -138,42 +103,23 @@ fun getApnDataFromUri(uri: Uri, context: Context): ApnData {
|
|||||||
return 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
|
* Returns The UI choice index corresponding to the given raw value of the protocol preference
|
||||||
* raw value of the protocol preference (e.g., "IPV4V6"). If unknown,
|
* (e.g., "IPV4V6").
|
||||||
* return null.
|
* If unknown, return -1.
|
||||||
*
|
|
||||||
* @return UI choice
|
|
||||||
*/
|
*/
|
||||||
private fun convertProtocol2Options(raw: String, context: Context): String {
|
private fun Context.convertProtocol2Options(protocol: String): Int {
|
||||||
val apnProtocolOptions = context.resources.getStringArray(R.array.apn_protocol_entries).toList()
|
var normalizedProtocol = protocol.uppercase(Locale.getDefault())
|
||||||
val apnProtocolValues = context.resources.getStringArray(R.array.apn_protocol_values).toList()
|
if (normalizedProtocol == "IPV4") normalizedProtocol = "IP"
|
||||||
var uRaw = raw.uppercase(Locale.getDefault())
|
return resources.getStringArray(R.array.apn_protocol_values).indexOf(normalizedProtocol)
|
||||||
uRaw = if (uRaw == "IPV4") "IP" else uRaw
|
|
||||||
val protocolIndex = apnProtocolValues.indexOf(uRaw)
|
|
||||||
return if (protocolIndex == -1) {
|
|
||||||
""
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
apnProtocolOptions[protocolIndex]
|
|
||||||
} catch (e: ArrayIndexOutOfBoundsException) {
|
|
||||||
""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun convertOptions2Protocol(protocolIndex: Int, context: Context): String {
|
fun Context.convertOptions2Protocol(protocolIndex: Int): String =
|
||||||
val apnProtocolValues = context.resources.getStringArray(R.array.apn_protocol_values).toList()
|
resources.getStringArray(R.array.apn_protocol_values).getOrElse(protocolIndex) { "" }
|
||||||
return if (protocolIndex == -1) {
|
|
||||||
""
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
apnProtocolValues[protocolIndex]
|
|
||||||
} catch (e: ArrayIndexOutOfBoundsException) {
|
|
||||||
""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun updateApnDataToDatabase(
|
fun updateApnDataToDatabase(
|
||||||
newApn: Boolean,
|
newApn: Boolean,
|
||||||
@@ -183,13 +129,13 @@ fun updateApnDataToDatabase(
|
|||||||
) {
|
) {
|
||||||
ThreadUtils.postOnBackgroundThread {
|
ThreadUtils.postOnBackgroundThread {
|
||||||
if (newApn) {
|
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)
|
val newUri = context.contentResolver.insert(uriInit, values)
|
||||||
if (newUri == null) {
|
if (newUri == null) {
|
||||||
Log.e(TAG, "Can't add a new apn to database $uriInit")
|
Log.e(TAG, "Can't add a new apn to database $uriInit")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Update the existing apn
|
Log.d(TAG, "Updating an existing APN to the database $uriInit $values")
|
||||||
context.contentResolver.update(
|
context.contentResolver.update(
|
||||||
uriInit, values, null /* where */, null /* selection Args */
|
uriInit, values, null /* where */, null /* selection Args */
|
||||||
)
|
)
|
||||||
@@ -210,9 +156,12 @@ private val NonDuplicatedKeys = setOf(
|
|||||||
)
|
)
|
||||||
|
|
||||||
fun isItemExist(apnData: ApnData, context: Context): String? {
|
fun isItemExist(apnData: ApnData, context: Context): String? {
|
||||||
val contentValueMap = apnData.getContentValueMap(context).filterKeys { it in NonDuplicatedKeys }
|
val selectionMap = apnData.getContentValueMap(context).filterKeys { it in NonDuplicatedKeys }
|
||||||
val list = contentValueMap.entries.toList()
|
.mapKeys { "${it.key} = ?" }
|
||||||
val selection = list.joinToString(" AND ") { "${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()
|
val selectionArgs: Array<String> = list.map { it.value.toString() }.toTypedArray()
|
||||||
context.contentResolver.query(
|
context.contentResolver.query(
|
||||||
Uri.withAppendedPath(Telephony.Carriers.SIM_APN_URI, apnData.subId.toString()),
|
Uri.withAppendedPath(Telephony.Carriers.SIM_APN_URI, apnData.subId.toString()),
|
||||||
|
@@ -22,19 +22,14 @@ import android.net.Uri
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import android.telephony.CarrierConfigManager
|
import android.telephony.CarrierConfigManager
|
||||||
import android.text.TextUtils
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.android.internal.util.ArrayUtils
|
|
||||||
import com.android.settings.R
|
import com.android.settings.R
|
||||||
import com.android.settings.network.apn.ApnTypes.APN_TYPES
|
import com.android.settings.network.apn.ApnTypes.getPreSelectedApnType
|
||||||
import com.android.settings.network.apn.ApnTypes.APN_TYPE_ALL
|
|
||||||
import com.android.settings.network.apn.ApnTypes.APN_TYPE_EMERGENCY
|
private const val TAG = "ApnStatus"
|
||||||
import com.android.settings.network.apn.ApnTypes.APN_TYPE_IA
|
|
||||||
import com.android.settings.network.apn.ApnTypes.APN_TYPE_IMS
|
|
||||||
import com.android.settings.network.apn.ApnTypes.APN_TYPE_MCX
|
|
||||||
import java.util.Locale
|
|
||||||
|
|
||||||
data class ApnData(
|
data class ApnData(
|
||||||
|
val id: Int = -1,
|
||||||
val name: String = "",
|
val name: String = "",
|
||||||
val apn: String = "",
|
val apn: String = "",
|
||||||
val proxy: String = "",
|
val proxy: String = "",
|
||||||
@@ -86,8 +81,8 @@ data class ApnData(
|
|||||||
Telephony.Carriers.MMSPROXY to mmsProxy,
|
Telephony.Carriers.MMSPROXY to mmsProxy,
|
||||||
Telephony.Carriers.MMSPORT to mmsPort,
|
Telephony.Carriers.MMSPORT to mmsPort,
|
||||||
Telephony.Carriers.AUTH_TYPE to authType,
|
Telephony.Carriers.AUTH_TYPE to authType,
|
||||||
Telephony.Carriers.PROTOCOL to convertOptions2Protocol(apnProtocol, context),
|
Telephony.Carriers.PROTOCOL to context.convertOptions2Protocol(apnProtocol),
|
||||||
Telephony.Carriers.ROAMING_PROTOCOL to convertOptions2Protocol(apnRoaming, context),
|
Telephony.Carriers.ROAMING_PROTOCOL to context.convertOptions2Protocol(apnRoaming),
|
||||||
Telephony.Carriers.TYPE to apnType,
|
Telephony.Carriers.TYPE to apnType,
|
||||||
Telephony.Carriers.NETWORK_TYPE_BITMASK to networkType,
|
Telephony.Carriers.NETWORK_TYPE_BITMASK to networkType,
|
||||||
Telephony.Carriers.CARRIER_ENABLED to apnEnable,
|
Telephony.Carriers.CARRIER_ENABLED to apnEnable,
|
||||||
@@ -105,7 +100,7 @@ data class CustomizedConfig(
|
|||||||
val isAddApnAllowed: Boolean = true,
|
val isAddApnAllowed: Boolean = true,
|
||||||
val readOnlyApnTypes: List<String> = emptyList(),
|
val readOnlyApnTypes: List<String> = emptyList(),
|
||||||
val readOnlyApnFields: List<String> = emptyList(),
|
val readOnlyApnFields: List<String> = emptyList(),
|
||||||
val defaultApnTypes: List<String> = emptyList(),
|
val defaultApnTypes: List<String>? = null,
|
||||||
val defaultApnProtocol: String = "",
|
val defaultApnProtocol: String = "",
|
||||||
val defaultApnRoamingProtocol: String = "",
|
val defaultApnRoamingProtocol: String = "",
|
||||||
)
|
)
|
||||||
@@ -118,19 +113,18 @@ data class CustomizedConfig(
|
|||||||
*
|
*
|
||||||
* @return Initialized CustomizedConfig information.
|
* @return Initialized CustomizedConfig information.
|
||||||
*/
|
*/
|
||||||
fun getApnDataInit(arguments: Bundle, context: Context, uriInit: Uri, subId: Int): ApnData {
|
fun getApnDataInit(arguments: Bundle, context: Context, uriInit: Uri, subId: Int): ApnData? {
|
||||||
|
val uriType = arguments.getString(URI_TYPE) ?: return null
|
||||||
val uriType = arguments.getString(URI_TYPE)!!
|
|
||||||
|
|
||||||
if (!uriInit.isPathPrefixMatch(Telephony.Carriers.CONTENT_URI)) {
|
if (!uriInit.isPathPrefixMatch(Telephony.Carriers.CONTENT_URI)) {
|
||||||
Log.e(TAG, "Insert request not for carrier table. Uri: $uriInit")
|
Log.e(TAG, "Insert request not for carrier table. Uri: $uriInit")
|
||||||
return ApnData() //TODO: finish
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
var apnDataInit = when (uriType) {
|
var apnDataInit = when (uriType) {
|
||||||
EDIT_URL -> getApnDataFromUri(uriInit, context)
|
EDIT_URL -> getApnDataFromUri(uriInit, context)
|
||||||
INSERT_URL -> ApnData()
|
INSERT_URL -> ApnData()
|
||||||
else -> ApnData() //TODO: finish
|
else -> return null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uriType == INSERT_URL) {
|
if (uriType == INSERT_URL) {
|
||||||
@@ -143,13 +137,18 @@ fun getApnDataInit(arguments: Bundle, context: Context, uriInit: Uri, subId: Int
|
|||||||
apnDataInit =
|
apnDataInit =
|
||||||
apnDataInit.copy(customizedConfig = getCarrierCustomizedConfig(apnDataInit, configManager))
|
apnDataInit.copy(customizedConfig = getCarrierCustomizedConfig(apnDataInit, configManager))
|
||||||
|
|
||||||
|
if (apnDataInit.newApn) {
|
||||||
|
apnDataInit = apnDataInit.copy(
|
||||||
|
apnType = getPreSelectedApnType(apnDataInit.customizedConfig)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
apnDataInit = apnDataInit.copy(
|
apnDataInit = apnDataInit.copy(
|
||||||
apnEnableEnabled =
|
apnEnableEnabled =
|
||||||
context.resources.getBoolean(R.bool.config_allow_edit_carrier_enabled)
|
context.resources.getBoolean(R.bool.config_allow_edit_carrier_enabled)
|
||||||
)
|
)
|
||||||
// TODO: mIsCarrierIdApn
|
// TODO: mIsCarrierIdApn
|
||||||
disableInit(apnDataInit)
|
return disableInit(apnDataInit)
|
||||||
return apnDataInit
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -199,53 +198,7 @@ fun validateApnData(apnData: ApnData, context: Context): String? {
|
|||||||
if (errorMsg == null) {
|
if (errorMsg == null) {
|
||||||
errorMsg = isItemExist(apnData, context)
|
errorMsg = isItemExist(apnData, context)
|
||||||
}
|
}
|
||||||
if (errorMsg == null) {
|
return errorMsg?.apply { Log.d(TAG, "APN data not valid, reason: $this") }
|
||||||
errorMsg = validateAPNType(
|
|
||||||
true,
|
|
||||||
apnData.apnType,
|
|
||||||
apnData.customizedConfig.readOnlyApnTypes,
|
|
||||||
context
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return errorMsg
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getUserEnteredApnType(apnType: String, readOnlyApnTypes: List<String>): String {
|
|
||||||
// if user has not specified a type, map it to "ALL APN TYPES THAT ARE NOT READ-ONLY"
|
|
||||||
// but if user enter empty type, map it just for default
|
|
||||||
var userEnteredApnType = apnType
|
|
||||||
if (userEnteredApnType != "") userEnteredApnType =
|
|
||||||
userEnteredApnType.trim { it <= ' ' }
|
|
||||||
if (TextUtils.isEmpty(userEnteredApnType) || APN_TYPE_ALL == userEnteredApnType) {
|
|
||||||
userEnteredApnType = getEditableApnType(readOnlyApnTypes)
|
|
||||||
}
|
|
||||||
Log.d(
|
|
||||||
TAG, "getUserEnteredApnType: changed apn type to editable apn types: "
|
|
||||||
+ userEnteredApnType
|
|
||||||
)
|
|
||||||
return userEnteredApnType
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getEditableApnType(readOnlyApnTypes: List<String>): String {
|
|
||||||
val editableApnTypes = StringBuilder()
|
|
||||||
var first = true
|
|
||||||
for (apnType in APN_TYPES) {
|
|
||||||
// add APN type if it is not read-only and is not wild-cardable
|
|
||||||
if (!readOnlyApnTypes.contains(apnType)
|
|
||||||
&& apnType != APN_TYPE_IA
|
|
||||||
&& apnType != APN_TYPE_EMERGENCY
|
|
||||||
&& apnType != APN_TYPE_MCX
|
|
||||||
&& apnType != APN_TYPE_IMS
|
|
||||||
) {
|
|
||||||
if (first) {
|
|
||||||
first = false
|
|
||||||
} else {
|
|
||||||
editableApnTypes.append(",")
|
|
||||||
}
|
|
||||||
editableApnTypes.append(apnType)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return editableApnTypes.toString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -258,6 +211,10 @@ fun getCarrierCustomizedConfig(
|
|||||||
apnInit: ApnData,
|
apnInit: ApnData,
|
||||||
configManager: CarrierConfigManager
|
configManager: CarrierConfigManager
|
||||||
): CustomizedConfig {
|
): CustomizedConfig {
|
||||||
|
fun log(message: String) {
|
||||||
|
Log.d(TAG, "getCarrierCustomizedConfig: $message")
|
||||||
|
}
|
||||||
|
|
||||||
val b = configManager.getConfigForSubId(
|
val b = configManager.getConfigForSubId(
|
||||||
apnInit.subId,
|
apnInit.subId,
|
||||||
CarrierConfigManager.KEY_READ_ONLY_APN_TYPES_STRING_ARRAY,
|
CarrierConfigManager.KEY_READ_ONLY_APN_TYPES_STRING_ARRAY,
|
||||||
@@ -270,72 +227,61 @@ fun getCarrierCustomizedConfig(
|
|||||||
val customizedConfig = CustomizedConfig(
|
val customizedConfig = CustomizedConfig(
|
||||||
readOnlyApnTypes = b.getStringArray(
|
readOnlyApnTypes = b.getStringArray(
|
||||||
CarrierConfigManager.KEY_READ_ONLY_APN_TYPES_STRING_ARRAY
|
CarrierConfigManager.KEY_READ_ONLY_APN_TYPES_STRING_ARRAY
|
||||||
)?.toList() ?: emptyList(), readOnlyApnFields = b.getStringArray(
|
)?.toList() ?: emptyList(),
|
||||||
|
readOnlyApnFields = b.getStringArray(
|
||||||
CarrierConfigManager.KEY_READ_ONLY_APN_FIELDS_STRING_ARRAY
|
CarrierConfigManager.KEY_READ_ONLY_APN_FIELDS_STRING_ARRAY
|
||||||
)?.toList() ?: emptyList(), defaultApnTypes = b.getStringArray(
|
)?.toList() ?: emptyList(),
|
||||||
|
defaultApnTypes = b.getStringArray(
|
||||||
CarrierConfigManager.KEY_APN_SETTINGS_DEFAULT_APN_TYPES_STRING_ARRAY
|
CarrierConfigManager.KEY_APN_SETTINGS_DEFAULT_APN_TYPES_STRING_ARRAY
|
||||||
)?.toList() ?: emptyList(), defaultApnProtocol = b.getString(
|
)?.toList(),
|
||||||
|
defaultApnProtocol = b.getString(
|
||||||
CarrierConfigManager.Apn.KEY_SETTINGS_DEFAULT_PROTOCOL_STRING
|
CarrierConfigManager.Apn.KEY_SETTINGS_DEFAULT_PROTOCOL_STRING
|
||||||
) ?: "", defaultApnRoamingProtocol = b.getString(
|
) ?: "",
|
||||||
|
defaultApnRoamingProtocol = b.getString(
|
||||||
CarrierConfigManager.Apn.KEY_SETTINGS_DEFAULT_ROAMING_PROTOCOL_STRING
|
CarrierConfigManager.Apn.KEY_SETTINGS_DEFAULT_ROAMING_PROTOCOL_STRING
|
||||||
) ?: "", isAddApnAllowed = b.getBoolean(CarrierConfigManager.KEY_ALLOW_ADDING_APNS_BOOL)
|
) ?: "",
|
||||||
)
|
isAddApnAllowed = b.getBoolean(CarrierConfigManager.KEY_ALLOW_ADDING_APNS_BOOL),
|
||||||
if (!ArrayUtils.isEmpty(customizedConfig.readOnlyApnTypes)) {
|
|
||||||
Log.d(
|
|
||||||
TAG,
|
|
||||||
"getCarrierCustomizedConfig: read only APN type: " + customizedConfig.readOnlyApnTypes.joinToString(
|
|
||||||
", "
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
if (customizedConfig.readOnlyApnTypes.isNotEmpty()) {
|
||||||
|
log("read only APN type: " + customizedConfig.readOnlyApnTypes)
|
||||||
}
|
}
|
||||||
if (!ArrayUtils.isEmpty(customizedConfig.defaultApnTypes)) {
|
customizedConfig.defaultApnTypes?.takeIf { it.isNotEmpty() }?.let {
|
||||||
Log.d(
|
log("default apn types: $it")
|
||||||
TAG,
|
|
||||||
"getCarrierCustomizedConfig: default apn types: " + customizedConfig.defaultApnTypes.joinToString(
|
|
||||||
", "
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
if (!TextUtils.isEmpty(customizedConfig.defaultApnProtocol)) {
|
if (customizedConfig.defaultApnProtocol.isNotEmpty()) {
|
||||||
Log.d(
|
log("default apn protocol: ${customizedConfig.defaultApnProtocol}")
|
||||||
TAG,
|
|
||||||
"getCarrierCustomizedConfig: default apn protocol: ${customizedConfig.defaultApnProtocol}"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
if (!TextUtils.isEmpty(customizedConfig.defaultApnRoamingProtocol)) {
|
if (customizedConfig.defaultApnRoamingProtocol.isNotEmpty()) {
|
||||||
Log.d(
|
log("default apn roaming protocol: ${customizedConfig.defaultApnRoamingProtocol}")
|
||||||
TAG,
|
|
||||||
"getCarrierCustomizedConfig: default apn roaming protocol: ${customizedConfig.defaultApnRoamingProtocol}"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
if (!customizedConfig.isAddApnAllowed) {
|
if (!customizedConfig.isAddApnAllowed) {
|
||||||
Log.d(TAG, "getCarrierCustomizedConfig: not allow to add new APN")
|
log("not allow to add new APN")
|
||||||
}
|
}
|
||||||
return customizedConfig
|
return customizedConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
fun disableInit(apnDataInit: ApnData): ApnData {
|
private fun ApnData.isReadOnly(): Boolean {
|
||||||
var apnData = apnDataInit
|
Log.d(TAG, "isReadOnly: edited $edited")
|
||||||
val isUserEdited = apnDataInit.edited == Telephony.Carriers.USER_EDITED
|
if (edited == Telephony.Carriers.USER_EDITED) return false
|
||||||
Log.d(TAG, "disableInit: EDITED $isUserEdited")
|
|
||||||
// if it's not a USER_EDITED apn, check if it's read-only
|
// if it's not a USER_EDITED apn, check if it's read-only
|
||||||
if (!isUserEdited && (apnDataInit.userEditable == 0
|
return userEditable == 0 ||
|
||||||
|| apnTypesMatch(apnDataInit.customizedConfig.readOnlyApnTypes, apnDataInit.apnType))
|
ApnTypes.isApnTypeReadOnly(apnType, customizedConfig.readOnlyApnTypes)
|
||||||
) {
|
}
|
||||||
|
|
||||||
|
fun disableInit(apnDataInit: ApnData): ApnData {
|
||||||
|
if (apnDataInit.isReadOnly()) {
|
||||||
Log.d(TAG, "disableInit: read-only APN")
|
Log.d(TAG, "disableInit: read-only APN")
|
||||||
apnData =
|
val apnData = apnDataInit.copy(
|
||||||
apnDataInit.copy(customizedConfig = apnDataInit.customizedConfig.copy(readOnlyApn = true))
|
customizedConfig = apnDataInit.customizedConfig.copy(readOnlyApn = true)
|
||||||
apnData = disableAllFields(apnData)
|
|
||||||
} else if (!ArrayUtils.isEmpty(apnData.customizedConfig.readOnlyApnFields)) {
|
|
||||||
Log.d(
|
|
||||||
TAG,
|
|
||||||
"disableInit: mReadOnlyApnFields ${
|
|
||||||
apnData.customizedConfig.readOnlyApnFields.joinToString(", ")
|
|
||||||
})"
|
|
||||||
)
|
)
|
||||||
apnData = disableFields(apnData.customizedConfig.readOnlyApnFields, apnData)
|
return disableAllFields(apnData)
|
||||||
}
|
}
|
||||||
return apnData
|
val readOnlyApnFields = apnDataInit.customizedConfig.readOnlyApnFields
|
||||||
|
if (readOnlyApnFields.isNotEmpty()) {
|
||||||
|
Log.d(TAG, "disableInit: readOnlyApnFields $readOnlyApnFields)")
|
||||||
|
return disableFields(readOnlyApnFields, apnDataInit)
|
||||||
|
}
|
||||||
|
return apnDataInit
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -402,23 +348,6 @@ private fun disableByFieldName(apnField: String, apnDataInit: ApnData): ApnData
|
|||||||
return apnData
|
return apnData
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun apnTypesMatch(apnTypeList: List<String>, apnType: String): Boolean {
|
|
||||||
val normalizeApnTypeList = apnTypeList.map(::normalizeApnType)
|
|
||||||
return hasAllApns(normalizeApnTypeList) ||
|
|
||||||
apnType.split(",").map(::normalizeApnType).all { it in normalizeApnTypeList }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun hasAllApns(apnTypes: List<String>): Boolean {
|
|
||||||
if (APN_TYPE_ALL in apnTypes) {
|
|
||||||
Log.d(TAG, "hasAllApns: true because apnTypes.contains(APN_TYPE_ALL)")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return APN_TYPES.all { it in apnTypes }
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun normalizeApnType(apnType: String): String =
|
|
||||||
apnType.trim().lowercase(Locale.getDefault())
|
|
||||||
|
|
||||||
fun deleteApn(uri: Uri, context: Context) {
|
fun deleteApn(uri: Uri, context: Context) {
|
||||||
val contentResolver = context.contentResolver
|
val contentResolver = context.contentResolver
|
||||||
contentResolver.delete(uri, null, null)
|
contentResolver.delete(uri, null, null)
|
||||||
@@ -439,24 +368,3 @@ fun validateAPN(validEnabled: Boolean, apn: String, context: Context): String? {
|
|||||||
return if (validEnabled && (apn == "")) context.resources.getString(R.string.error_apn_empty)
|
return if (validEnabled && (apn == "")) context.resources.getString(R.string.error_apn_empty)
|
||||||
else null
|
else null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun validateAPNType(
|
|
||||||
validEnabled: Boolean,
|
|
||||||
apnType: String,
|
|
||||||
readOnlyApnTypes: List<String>,
|
|
||||||
context: Context
|
|
||||||
): String? {
|
|
||||||
// if carrier does not allow editing certain apn types, make sure type does not include those
|
|
||||||
if (validEnabled && !ArrayUtils.isEmpty(readOnlyApnTypes)
|
|
||||||
&& apnTypesMatch(
|
|
||||||
readOnlyApnTypes,
|
|
||||||
getUserEnteredApnType(apnType, readOnlyApnTypes)
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
return String.format(
|
|
||||||
context.resources.getString(R.string.error_adding_apn_type),
|
|
||||||
readOnlyApnTypes.joinToString(", ")
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
53
src/com/android/settings/network/apn/ApnTypeCheckBox.kt
Normal file
53
src/com/android/settings/network/apn/ApnTypeCheckBox.kt
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* 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 android.telephony.data.ApnSetting
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import com.android.settings.R
|
||||||
|
import com.android.settings.network.apn.ApnTypes.toApnType
|
||||||
|
import com.android.settingslib.spa.widget.editor.SettingsDropdownCheckBox
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ApnTypeCheckBox(
|
||||||
|
apnData: ApnData,
|
||||||
|
onTypeChanged: (String) -> Unit,
|
||||||
|
onMmsSelectedChanged: (Boolean) -> Unit,
|
||||||
|
) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
val apnTypeOptions = remember {
|
||||||
|
ApnTypes.getOptions(context, apnData.apnType, apnData.customizedConfig.readOnlyApnTypes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateMmsSelected() {
|
||||||
|
val apnTypeOptionMms = apnTypeOptions.single { it.text == ApnSetting.TYPE_MMS_STRING }
|
||||||
|
onMmsSelectedChanged(apnTypeOptionMms.selected.value)
|
||||||
|
}
|
||||||
|
LaunchedEffect(Unit) { updateMmsSelected() }
|
||||||
|
SettingsDropdownCheckBox(
|
||||||
|
label = stringResource(R.string.apn_type),
|
||||||
|
options = apnTypeOptions,
|
||||||
|
enabled = apnData.apnTypeEnabled,
|
||||||
|
) {
|
||||||
|
onTypeChanged(apnTypeOptions.toApnType())
|
||||||
|
updateMmsSelected()
|
||||||
|
}
|
||||||
|
}
|
@@ -16,128 +16,112 @@
|
|||||||
|
|
||||||
package com.android.settings.network.apn
|
package com.android.settings.network.apn
|
||||||
|
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
import android.content.Context
|
||||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
import android.telephony.data.ApnSetting
|
||||||
|
import android.util.Log
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.ui.text.intl.Locale
|
||||||
|
import androidx.compose.ui.text.toLowerCase
|
||||||
|
import com.android.settings.R
|
||||||
|
import com.android.settingslib.spa.widget.editor.SettingsDropdownCheckOption
|
||||||
|
|
||||||
object ApnTypes {
|
object ApnTypes {
|
||||||
/**
|
private const val TAG = "ApnTypes"
|
||||||
* APN types for data connections. These are usage categories for an APN
|
|
||||||
* entry. One APN entry may support multiple APN types, eg, a single APN
|
|
||||||
* may service regular internet traffic ("default") as well as MMS-specific
|
|
||||||
* connections.<br></br>
|
|
||||||
* APN_TYPE_ALL is a special type to indicate that this APN entry can
|
|
||||||
* service all data connections.
|
|
||||||
*/
|
|
||||||
const val APN_TYPE_ALL = "*"
|
|
||||||
|
|
||||||
/** APN type for default data traffic */
|
private val APN_TYPES = arrayOf(
|
||||||
const val APN_TYPE_DEFAULT = "default"
|
ApnSetting.TYPE_DEFAULT_STRING,
|
||||||
|
ApnSetting.TYPE_MMS_STRING,
|
||||||
/** APN type for MMS traffic */
|
ApnSetting.TYPE_SUPL_STRING,
|
||||||
const val APN_TYPE_MMS = "mms"
|
ApnSetting.TYPE_DUN_STRING,
|
||||||
|
ApnSetting.TYPE_HIPRI_STRING,
|
||||||
/** APN type for SUPL assisted GPS */
|
ApnSetting.TYPE_FOTA_STRING,
|
||||||
const val APN_TYPE_SUPL = "supl"
|
ApnSetting.TYPE_IMS_STRING,
|
||||||
|
ApnSetting.TYPE_CBS_STRING,
|
||||||
/** APN type for DUN traffic */
|
ApnSetting.TYPE_IA_STRING,
|
||||||
const val APN_TYPE_DUN = "dun"
|
ApnSetting.TYPE_EMERGENCY_STRING,
|
||||||
|
ApnSetting.TYPE_MCX_STRING,
|
||||||
/** APN type for HiPri traffic */
|
ApnSetting.TYPE_XCAP_STRING,
|
||||||
const val APN_TYPE_HIPRI = "hipri"
|
ApnSetting.TYPE_VSIM_STRING,
|
||||||
|
ApnSetting.TYPE_BIP_STRING,
|
||||||
/** APN type for FOTA */
|
ApnSetting.TYPE_ENTERPRISE_STRING,
|
||||||
const val APN_TYPE_FOTA = "fota"
|
|
||||||
|
|
||||||
/** APN type for IMS */
|
|
||||||
const val APN_TYPE_IMS = "ims"
|
|
||||||
|
|
||||||
/** APN type for CBS */
|
|
||||||
const val APN_TYPE_CBS = "cbs"
|
|
||||||
|
|
||||||
/** APN type for IA Initial Attach APN */
|
|
||||||
const val APN_TYPE_IA = "ia"
|
|
||||||
|
|
||||||
/** APN type for Emergency PDN. This is not an IA apn, but is used
|
|
||||||
* for access to carrier services in an emergency call situation. */
|
|
||||||
const val APN_TYPE_EMERGENCY = "emergency"
|
|
||||||
|
|
||||||
/** APN type for Mission Critical Services */
|
|
||||||
const val APN_TYPE_MCX = "mcx"
|
|
||||||
|
|
||||||
/** APN type for XCAP */
|
|
||||||
const val APN_TYPE_XCAP = "xcap"
|
|
||||||
|
|
||||||
/** APN type for VSIM */
|
|
||||||
const val APN_TYPE_VSIM = "vsim"
|
|
||||||
|
|
||||||
/** APN type for BIP */
|
|
||||||
const val APN_TYPE_BIP = "bip"
|
|
||||||
|
|
||||||
/** APN type for ENTERPRISE */
|
|
||||||
const val APN_TYPE_ENTERPRISE = "enterprise"
|
|
||||||
|
|
||||||
val APN_TYPES = arrayOf(
|
|
||||||
APN_TYPE_DEFAULT,
|
|
||||||
APN_TYPE_MMS,
|
|
||||||
APN_TYPE_SUPL,
|
|
||||||
APN_TYPE_DUN,
|
|
||||||
APN_TYPE_HIPRI,
|
|
||||||
APN_TYPE_FOTA,
|
|
||||||
APN_TYPE_IMS,
|
|
||||||
APN_TYPE_CBS,
|
|
||||||
APN_TYPE_IA,
|
|
||||||
APN_TYPE_EMERGENCY,
|
|
||||||
APN_TYPE_MCX,
|
|
||||||
APN_TYPE_XCAP,
|
|
||||||
APN_TYPE_VSIM,
|
|
||||||
APN_TYPE_BIP,
|
|
||||||
APN_TYPE_ENTERPRISE
|
|
||||||
)
|
)
|
||||||
|
|
||||||
val APN_TYPES_OPTIONS = listOf(APN_TYPE_ALL) + APN_TYPES
|
private fun splitToList(apnType: String): List<String> {
|
||||||
|
val types = apnType.split(',').map { it.trim().toLowerCase(Locale.current) }
|
||||||
fun getApnTypeSelectedOptionsState(apnType: String): SnapshotStateList<Int> {
|
if (ApnSetting.TYPE_ALL_STRING in types || APN_TYPES.all { it in types }) {
|
||||||
val apnTypeSelectedOptionsState = mutableStateListOf<Int>()
|
return listOf(ApnSetting.TYPE_ALL_STRING)
|
||||||
if (apnType.contains(APN_TYPE_ALL))
|
|
||||||
APN_TYPES_OPTIONS.forEachIndexed { index, _ ->
|
|
||||||
apnTypeSelectedOptionsState.add(index)
|
|
||||||
}
|
}
|
||||||
else {
|
return APN_TYPES.filter { it in types }
|
||||||
APN_TYPES_OPTIONS.forEachIndexed { index, type ->
|
|
||||||
if (apnType.contains(type)) {
|
|
||||||
apnTypeSelectedOptionsState.add(index)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (apnTypeSelectedOptionsState.size == APN_TYPES.size)
|
|
||||||
apnTypeSelectedOptionsState.add(APN_TYPES_OPTIONS.indexOf(APN_TYPE_ALL))
|
|
||||||
}
|
|
||||||
return apnTypeSelectedOptionsState
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateApnType(
|
fun isApnTypeReadOnly(apnType: String, readOnlyTypes: List<String>): Boolean {
|
||||||
apnTypeSelectedOptionsState: SnapshotStateList<Int>,
|
val apnTypes = splitToList(apnType)
|
||||||
defaultApnTypes: List<String>,
|
return ApnSetting.TYPE_ALL_STRING in readOnlyTypes ||
|
||||||
readOnlyApnTypes: List<String>
|
ApnSetting.TYPE_ALL_STRING in apnTypes && readOnlyTypes.isNotEmpty() ||
|
||||||
): String {
|
apnTypes.any { it in readOnlyTypes }
|
||||||
val apnType = apnTypeSelectedOptionsState.joinToString { APN_TYPES_OPTIONS[it] }
|
|
||||||
if (apnType.contains(APN_TYPE_ALL)) return APN_TYPE_ALL
|
|
||||||
return if (apnType == "" && defaultApnTypes.isNotEmpty())
|
|
||||||
getEditableApnType(defaultApnTypes, readOnlyApnTypes)
|
|
||||||
else
|
|
||||||
apnType
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getEditableApnType(
|
fun getOptions(context: Context, apnType: String, readOnlyTypes: List<String>) = buildList {
|
||||||
defaultApnTypes: List<String>,
|
val apnTypes = splitToList(apnType)
|
||||||
readOnlyApnTypes: List<String>
|
add(
|
||||||
): String {
|
context.createSettingsDropdownCheckOption(
|
||||||
return defaultApnTypes.filterNot { apnType ->
|
text = ApnSetting.TYPE_ALL_STRING,
|
||||||
readOnlyApnTypes.contains(apnType) || apnType in listOf(
|
isSelectAll = true,
|
||||||
APN_TYPE_IA,
|
changeable = readOnlyTypes.isEmpty(),
|
||||||
APN_TYPE_EMERGENCY,
|
selected = ApnSetting.TYPE_ALL_STRING in apnTypes,
|
||||||
APN_TYPE_MCX,
|
)
|
||||||
APN_TYPE_IMS,
|
)
|
||||||
|
for (type in APN_TYPES) {
|
||||||
|
add(
|
||||||
|
context.createSettingsDropdownCheckOption(
|
||||||
|
text = type,
|
||||||
|
changeable = ApnSetting.TYPE_ALL_STRING !in readOnlyTypes &&
|
||||||
|
type !in readOnlyTypes,
|
||||||
|
selected = ApnSetting.TYPE_ALL_STRING in apnTypes || type in apnTypes,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}.joinToString()
|
|
||||||
}
|
}
|
||||||
|
}.also { Log.d(TAG, "APN Type options: $it") }
|
||||||
|
|
||||||
|
private fun Context.createSettingsDropdownCheckOption(
|
||||||
|
text: String,
|
||||||
|
isSelectAll: Boolean = false,
|
||||||
|
changeable: Boolean,
|
||||||
|
selected: Boolean,
|
||||||
|
) = SettingsDropdownCheckOption(
|
||||||
|
text = text,
|
||||||
|
isSelectAll = isSelectAll,
|
||||||
|
changeable = changeable,
|
||||||
|
selected = mutableStateOf(selected),
|
||||||
|
) {
|
||||||
|
if (!changeable) {
|
||||||
|
val message = resources.getString(R.string.error_adding_apn_type, text)
|
||||||
|
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun List<SettingsDropdownCheckOption>.toApnType(): String {
|
||||||
|
val (selectAllOptions, regularOptions) = partition { it.isSelectAll }
|
||||||
|
for (selectAllOption in selectAllOptions) {
|
||||||
|
if (selectAllOption.selected.value) return ApnSetting.TYPE_ALL_STRING
|
||||||
|
}
|
||||||
|
return regularOptions.filter { it.selected.value }.joinToString(",") { it.text }
|
||||||
|
}
|
||||||
|
|
||||||
|
private val NotPreSelectedTypes = setOf(
|
||||||
|
ApnSetting.TYPE_IMS_STRING,
|
||||||
|
ApnSetting.TYPE_IA_STRING,
|
||||||
|
ApnSetting.TYPE_EMERGENCY_STRING,
|
||||||
|
ApnSetting.TYPE_MCX_STRING,
|
||||||
|
)
|
||||||
|
|
||||||
|
fun getPreSelectedApnType(customizedConfig: CustomizedConfig): String =
|
||||||
|
(customizedConfig.defaultApnTypes
|
||||||
|
?: defaultPreSelectedApnTypes(customizedConfig.readOnlyApnTypes))
|
||||||
|
.joinToString(",")
|
||||||
|
|
||||||
|
private fun defaultPreSelectedApnTypes(readOnlyApnTypes: List<String>) =
|
||||||
|
if (ApnSetting.TYPE_ALL_STRING in readOnlyApnTypes) emptyList()
|
||||||
|
else APN_TYPES.filter { it !in readOnlyApnTypes + NotPreSelectedTypes }
|
||||||
}
|
}
|
@@ -58,7 +58,7 @@ class ApnRepositoryTest {
|
|||||||
@Test
|
@Test
|
||||||
fun getApnDataFromUri() {
|
fun getApnDataFromUri() {
|
||||||
// mock out resources and the feature provider
|
// mock out resources and the feature provider
|
||||||
val cursor = MatrixCursor(sProjection)
|
val cursor = MatrixCursor(Projection)
|
||||||
cursor.addRow(
|
cursor.addRow(
|
||||||
arrayOf<Any>(
|
arrayOf<Any>(
|
||||||
0,
|
0,
|
||||||
@@ -82,7 +82,7 @@ class ApnRepositoryTest {
|
|||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
whenever(contentResolver.query(uri, sProjection, null, null, null)).thenReturn(cursor)
|
whenever(contentResolver.query(uri, Projection, null, null, null)).thenReturn(cursor)
|
||||||
|
|
||||||
val apnData = getApnDataFromUri(uri, context)
|
val apnData = getApnDataFromUri(uri, context)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user