1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.telephony.uicc; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.telephony.PhoneNumberUtils; 23 import android.text.TextUtils; 24 25 import com.android.internal.telephony.GsmAlphabet; 26 import com.android.telephony.Rlog; 27 28 import java.util.Arrays; 29 30 /** 31 * 32 * Used to load or store ADNs (Abbreviated Dialing Numbers). 33 * 34 * {@hide} 35 * 36 */ 37 public class AdnRecord implements Parcelable { 38 static final String LOG_TAG = "AdnRecord"; 39 40 //***** Instance Variables 41 42 @UnsupportedAppUsage 43 String mAlphaTag = null; 44 @UnsupportedAppUsage 45 String mNumber = null; 46 @UnsupportedAppUsage 47 String[] mEmails; 48 @UnsupportedAppUsage 49 int mExtRecord = 0xff; 50 @UnsupportedAppUsage 51 int mEfid; // or 0 if none 52 @UnsupportedAppUsage 53 int mRecordNumber; // or 0 if none 54 55 56 //***** Constants 57 58 // In an ADN record, everything but the alpha identifier 59 // is in a footer that's 14 bytes 60 static final int FOOTER_SIZE_BYTES = 14; 61 62 // Maximum size of the un-extended number field 63 static final int MAX_NUMBER_SIZE_BYTES = 11; 64 65 static final int EXT_RECORD_LENGTH_BYTES = 13; 66 static final int EXT_RECORD_TYPE_ADDITIONAL_DATA = 2; 67 static final int EXT_RECORD_TYPE_MASK = 3; 68 static final int MAX_EXT_CALLED_PARTY_LENGTH = 0xa; 69 70 // ADN offset 71 static final int ADN_BCD_NUMBER_LENGTH = 0; 72 static final int ADN_TON_AND_NPI = 1; 73 static final int ADN_DIALING_NUMBER_START = 2; 74 static final int ADN_DIALING_NUMBER_END = 11; 75 static final int ADN_CAPABILITY_ID = 12; 76 static final int ADN_EXTENSION_ID = 13; 77 78 //***** Static Methods 79 80 @UnsupportedAppUsage 81 public static final Parcelable.Creator<AdnRecord> CREATOR 82 = new Parcelable.Creator<AdnRecord>() { 83 @Override 84 public AdnRecord createFromParcel(Parcel source) { 85 int efid; 86 int recordNumber; 87 String alphaTag; 88 String number; 89 String[] emails; 90 91 efid = source.readInt(); 92 recordNumber = source.readInt(); 93 alphaTag = source.readString(); 94 number = source.readString(); 95 emails = source.createStringArray(); 96 97 return new AdnRecord(efid, recordNumber, alphaTag, number, emails); 98 } 99 100 @Override 101 public AdnRecord[] newArray(int size) { 102 return new AdnRecord[size]; 103 } 104 }; 105 106 107 //***** Constructor 108 @UnsupportedAppUsage AdnRecord(byte[] record)109 public AdnRecord (byte[] record) { 110 this(0, 0, record); 111 } 112 113 @UnsupportedAppUsage AdnRecord(int efid, int recordNumber, byte[] record)114 public AdnRecord (int efid, int recordNumber, byte[] record) { 115 this.mEfid = efid; 116 this.mRecordNumber = recordNumber; 117 parseRecord(record); 118 } 119 120 @UnsupportedAppUsage AdnRecord(String alphaTag, String number)121 public AdnRecord (String alphaTag, String number) { 122 this(0, 0, alphaTag, number); 123 } 124 125 @UnsupportedAppUsage AdnRecord(String alphaTag, String number, String[] emails)126 public AdnRecord (String alphaTag, String number, String[] emails) { 127 this(0, 0, alphaTag, number, emails); 128 } 129 130 @UnsupportedAppUsage AdnRecord(int efid, int recordNumber, String alphaTag, String number, String[] emails)131 public AdnRecord (int efid, int recordNumber, String alphaTag, String number, String[] emails) { 132 this.mEfid = efid; 133 this.mRecordNumber = recordNumber; 134 this.mAlphaTag = alphaTag; 135 this.mNumber = number; 136 this.mEmails = emails; 137 } 138 139 @UnsupportedAppUsage AdnRecord(int efid, int recordNumber, String alphaTag, String number)140 public AdnRecord(int efid, int recordNumber, String alphaTag, String number) { 141 this.mEfid = efid; 142 this.mRecordNumber = recordNumber; 143 this.mAlphaTag = alphaTag; 144 this.mNumber = number; 145 this.mEmails = null; 146 } 147 148 //***** Instance Methods 149 getAlphaTag()150 public String getAlphaTag() { 151 return mAlphaTag; 152 } 153 getEfid()154 public int getEfid() { 155 return mEfid; 156 } 157 getRecId()158 public int getRecId() { 159 return mRecordNumber; 160 } 161 162 @UnsupportedAppUsage getNumber()163 public String getNumber() { 164 return mNumber; 165 } 166 setNumber(String number)167 public void setNumber(String number) { 168 mNumber = number; 169 } 170 171 @UnsupportedAppUsage getEmails()172 public String[] getEmails() { 173 return mEmails; 174 } 175 176 @UnsupportedAppUsage setEmails(String[] emails)177 public void setEmails(String[] emails) { 178 this.mEmails = emails; 179 } 180 181 @Override toString()182 public String toString() { 183 return "ADN Record '" + mAlphaTag + "' '" + Rlog.pii(LOG_TAG, mNumber) + " " 184 + Rlog.pii(LOG_TAG, mEmails) + "'"; 185 } 186 187 @UnsupportedAppUsage isEmpty()188 public boolean isEmpty() { 189 return TextUtils.isEmpty(mAlphaTag) && TextUtils.isEmpty(mNumber) && mEmails == null; 190 } 191 hasExtendedRecord()192 public boolean hasExtendedRecord() { 193 return mExtRecord != 0 && mExtRecord != 0xff; 194 } 195 196 /** Helper function for {@link #isEqual}. */ stringCompareNullEqualsEmpty(String s1, String s2)197 private static boolean stringCompareNullEqualsEmpty(String s1, String s2) { 198 if (s1 == s2) { 199 return true; 200 } 201 if (s1 == null) { 202 s1 = ""; 203 } 204 if (s2 == null) { 205 s2 = ""; 206 } 207 return (s1.equals(s2)); 208 } 209 isEqual(AdnRecord adn)210 public boolean isEqual(AdnRecord adn) { 211 return ( stringCompareNullEqualsEmpty(mAlphaTag, adn.mAlphaTag) && 212 stringCompareNullEqualsEmpty(mNumber, adn.mNumber) && 213 Arrays.equals(mEmails, adn.mEmails)); 214 } 215 //***** Parcelable Implementation 216 217 @Override describeContents()218 public int describeContents() { 219 return 0; 220 } 221 222 @Override writeToParcel(Parcel dest, int flags)223 public void writeToParcel(Parcel dest, int flags) { 224 dest.writeInt(mEfid); 225 dest.writeInt(mRecordNumber); 226 dest.writeString(mAlphaTag); 227 dest.writeString(mNumber); 228 dest.writeStringArray(mEmails); 229 } 230 231 /** 232 * Build adn hex byte array based on record size 233 * The format of byte array is defined in 51.011 10.5.1 234 * 235 * @param recordSize is the size X of EF record 236 * @return hex byte[recordSize] to be written to EF record 237 * return null for wrong format of dialing number or tag 238 */ 239 @UnsupportedAppUsage buildAdnString(int recordSize)240 public byte[] buildAdnString(int recordSize) { 241 byte[] bcdNumber; 242 byte[] byteTag; 243 byte[] adnString; 244 int footerOffset = recordSize - FOOTER_SIZE_BYTES; 245 246 // create an empty record 247 adnString = new byte[recordSize]; 248 for (int i = 0; i < recordSize; i++) { 249 adnString[i] = (byte) 0xFF; 250 } 251 252 if (TextUtils.isEmpty(mNumber)) { 253 Rlog.w(LOG_TAG, "[buildAdnString] Empty dialing number"); 254 return adnString; // return the empty record (for delete) 255 } else if (mNumber.length() 256 > (ADN_DIALING_NUMBER_END - ADN_DIALING_NUMBER_START + 1) * 2) { 257 Rlog.w(LOG_TAG, 258 "[buildAdnString] Max length of dialing number is 20"); 259 return null; 260 } 261 262 byteTag = !TextUtils.isEmpty(mAlphaTag) ? GsmAlphabet.stringToGsm8BitPacked(mAlphaTag) 263 : new byte[0]; 264 265 if (byteTag.length > footerOffset) { 266 Rlog.w(LOG_TAG, "[buildAdnString] Max length of tag is " + footerOffset); 267 return null; 268 } else { 269 bcdNumber = PhoneNumberUtils.numberToCalledPartyBCD( 270 mNumber, PhoneNumberUtils.BCD_EXTENDED_TYPE_EF_ADN); 271 272 System.arraycopy(bcdNumber, 0, adnString, 273 footerOffset + ADN_TON_AND_NPI, bcdNumber.length); 274 275 adnString[footerOffset + ADN_BCD_NUMBER_LENGTH] 276 = (byte) (bcdNumber.length); 277 adnString[footerOffset + ADN_CAPABILITY_ID] 278 = (byte) 0xFF; // Capability Id 279 adnString[footerOffset + ADN_EXTENSION_ID] 280 = (byte) 0xFF; // Extension Record Id 281 282 if (byteTag.length > 0) { 283 System.arraycopy(byteTag, 0, adnString, 0, byteTag.length); 284 } 285 286 return adnString; 287 } 288 } 289 290 /** 291 * See TS 51.011 10.5.10 292 */ 293 public void appendExtRecord(byte[] extRecord)294 appendExtRecord (byte[] extRecord) { 295 try { 296 if (extRecord.length != EXT_RECORD_LENGTH_BYTES) { 297 return; 298 } 299 300 if ((extRecord[0] & EXT_RECORD_TYPE_MASK) 301 != EXT_RECORD_TYPE_ADDITIONAL_DATA) { 302 return; 303 } 304 305 if ((0xff & extRecord[1]) > MAX_EXT_CALLED_PARTY_LENGTH) { 306 // invalid or empty record 307 return; 308 } 309 310 mNumber += PhoneNumberUtils.calledPartyBCDFragmentToString( 311 extRecord, 312 2, 313 0xff & extRecord[1], 314 PhoneNumberUtils.BCD_EXTENDED_TYPE_EF_ADN); 315 316 // We don't support ext record chaining. 317 318 } catch (RuntimeException ex) { 319 Rlog.w(LOG_TAG, "Error parsing AdnRecord ext record", ex); 320 } 321 } 322 323 //***** Private Methods 324 325 /** 326 * alphaTag and number are set to null on invalid format 327 */ 328 private void parseRecord(byte[] record)329 parseRecord(byte[] record) { 330 try { 331 mAlphaTag = IccUtils.adnStringFieldToString( 332 record, 0, record.length - FOOTER_SIZE_BYTES); 333 334 int footerOffset = record.length - FOOTER_SIZE_BYTES; 335 336 int numberLength = 0xff & record[footerOffset]; 337 338 if (numberLength > MAX_NUMBER_SIZE_BYTES) { 339 // Invalid number length 340 mNumber = ""; 341 return; 342 } 343 344 // Please note 51.011 10.5.1: 345 // 346 // "If the Dialling Number/SSC String does not contain 347 // a dialling number, e.g. a control string deactivating 348 // a service, the TON/NPI byte shall be set to 'FF' by 349 // the ME (see note 2)." 350 351 mNumber = PhoneNumberUtils.calledPartyBCDToString( 352 record, 353 footerOffset + 1, 354 numberLength, 355 PhoneNumberUtils.BCD_EXTENDED_TYPE_EF_ADN); 356 357 358 mExtRecord = 0xff & record[record.length - 1]; 359 360 mEmails = null; 361 362 } catch (RuntimeException ex) { 363 Rlog.w(LOG_TAG, "Error parsing AdnRecord", ex); 364 mNumber = ""; 365 mAlphaTag = ""; 366 mEmails = null; 367 } 368 } 369 } 370