Fix cannot update APN settings in new edit page

Fix: 326164397
Test: manual - edit existing APN
Test: unit test - existing tests still pass
Change-Id: I92a2e375b990f0e478dbb30c389371a8e0596608
This commit is contained in:
Chaohui Wang
2024-02-21 13:42:52 +08:00
parent bfaa3398bb
commit 6664870eb8
3 changed files with 57 additions and 105 deletions

View File

@@ -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()),

View File

@@ -34,7 +34,10 @@ import com.android.settings.network.apn.ApnTypes.APN_TYPE_IMS
import com.android.settings.network.apn.ApnTypes.APN_TYPE_MCX import com.android.settings.network.apn.ApnTypes.APN_TYPE_MCX
import java.util.Locale import java.util.Locale
private const val TAG = "ApnStatus"
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 +89,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,
@@ -207,7 +210,7 @@ fun validateApnData(apnData: ApnData, context: Context): String? {
context context
) )
} }
return errorMsg return errorMsg?.apply { Log.d(TAG, "APN data not valid, reason: $this") }
} }
private fun getUserEnteredApnType(apnType: String, readOnlyApnTypes: List<String>): String { private fun getUserEnteredApnType(apnType: String, readOnlyApnTypes: List<String>): String {

View File

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