diff options
author | Marcus Hagerott <mhagerott@google.com> | 2021-01-28 13:38:00 -0800 |
---|---|---|
committer | Marcus Hagerott <mhagerott@google.com> | 2021-02-16 06:47:23 -0800 |
commit | 407164df118553406c5ac9d04db1ef91cdd8fb23 (patch) | |
tree | 08e0ea10fc7c23f06ea6ef4abf1061389a5e97b9 /telephony/java | |
parent | 6916f91f55ff44530635e7298a37f3422ed4ee12 (diff) |
Add UCS-2 support for SimPhonebookContract
This simplifies the API by eliminating the need to attempt to detect
and report unsupported characters.
Test: atest FrameworksTelephonyTests:com.android.internal.telephony.uicc.IccUtilsTest TeleServiceTests:SimPhonebookProviderTest
Bug: 154363919
Change-Id: Ica8f3a642ee50ca81c43822ba39c0a071c279705
Diffstat (limited to 'telephony/java')
-rw-r--r-- | telephony/java/com/android/internal/telephony/uicc/IccUtils.java | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/telephony/java/com/android/internal/telephony/uicc/IccUtils.java b/telephony/java/com/android/internal/telephony/uicc/IccUtils.java index d79225fe5369..ec1204042260 100644 --- a/telephony/java/com/android/internal/telephony/uicc/IccUtils.java +++ b/telephony/java/com/android/internal/telephony/uicc/IccUtils.java @@ -16,6 +16,7 @@ package com.android.internal.telephony.uicc; +import android.annotation.NonNull; import android.compat.annotation.UnsupportedAppUsage; import android.content.res.Resources; import android.content.res.Resources.NotFoundException; @@ -28,6 +29,7 @@ import com.android.internal.telephony.GsmAlphabet; import com.android.telephony.Rlog; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.List; /** @@ -253,13 +255,48 @@ public class IccUtils { } if ((b & 0x0f) <= 0x09) { - ret += (b & 0xf); + ret += (b & 0xf); } return ret; } /** + * Encodes a string to be formatted like the EF[ADN] alpha identifier. + * + * <p>See javadoc for {@link #adnStringFieldToString(byte[], int, int)} for more details on + * the relevant specs. + * + * <p>This will attempt to encode using the GSM 7-bit alphabet but will fallback to UCS-2 if + * there are characters that are not supported by it. + * + * @return the encoded string including the prefix byte necessary to identify the encoding. + * @see #adnStringFieldToString(byte[], int, int) + */ + @NonNull + public static byte[] stringToAdnStringField(@NonNull String alphaTag) { + int septets = GsmAlphabet.countGsmSeptetsUsingTables(alphaTag, false, 0, 0); + if (septets != -1) { + byte[] ret = new byte[septets]; + GsmAlphabet.stringToGsm8BitUnpackedField(alphaTag, ret, 0, ret.length); + return ret; + } + + // Strictly speaking UCS-2 disallows surrogate characters but it's much more complicated to + // validate that the string contains only valid UCS-2 characters. Since the read path + // in most modern software will decode "UCS-2" by treating it as UTF-16 this should be fine + // (e.g. the adnStringFieldToString has done this for a long time on Android). Also there's + // already a precedent in SMS applications to ignore the UCS-2/UTF-16 distinction. + byte[] alphaTagBytes = alphaTag.getBytes(StandardCharsets.UTF_16BE); + byte[] ret = new byte[alphaTagBytes.length + 1]; + // 0x80 tags the remaining bytes as UCS-2 + ret[0] = (byte) 0x80; + System.arraycopy(alphaTagBytes, 0, ret, 1, alphaTagBytes.length); + + return ret; + } + + /** * Decodes a string field that's formatted like the EF[ADN] alpha * identifier * @@ -309,7 +346,7 @@ public class IccUtils { ret = new String(data, offset + 1, ucslen * 2, "utf-16be"); } catch (UnsupportedEncodingException ex) { Rlog.e(LOG_TAG, "implausible UnsupportedEncodingException", - ex); + ex); } if (ret != null) { @@ -342,7 +379,7 @@ public class IccUtils { len = length - 4; base = (char) (((data[offset + 2] & 0xFF) << 8) | - (data[offset + 3] & 0xFF)); + (data[offset + 3] & 0xFF)); offset += 4; isucs2 = true; } @@ -366,7 +403,7 @@ public class IccUtils { count++; ret.append(GsmAlphabet.gsm8BitUnpackedToString(data, - offset, count)); + offset, count)); offset += count; len -= count; |