1 /* 2 * Copyright (C) 2021 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.hardware.radio.V1_6.PhonebookRecordInfo; 20 import android.telephony.PhoneNumberUtils; 21 import android.text.TextUtils; 22 23 import com.android.internal.telephony.util.ArrayUtils; 24 25 import java.util.ArrayList; 26 import java.util.Arrays; 27 28 /** 29 * Represents a Phonebook entry from the SIM. 30 * 31 * {@hide} 32 */ 33 public class SimPhonebookRecord { 34 // Instance variables 35 private int mRecordId = 0; 36 private String mAlphaTag; 37 private String mNumber; 38 private String[] mEmails; 39 private String[] mAdditionalNumbers; 40 41 // Instance methods SimPhonebookRecord(int recordId, String alphaTag, String number, String[] emails, String[] adNumbers)42 public SimPhonebookRecord (int recordId, String alphaTag, String number, 43 String[] emails, String[] adNumbers) { 44 mRecordId = recordId; 45 mAlphaTag = alphaTag; 46 mNumber = convertRecordFormatToNumber(number); 47 mEmails = emails; 48 if (adNumbers != null) { 49 mAdditionalNumbers = new String[adNumbers.length]; 50 for (int i = 0 ; i < adNumbers.length; i++) { 51 mAdditionalNumbers[i] = convertRecordFormatToNumber(adNumbers[i]); 52 } 53 } 54 } 55 SimPhonebookRecord(PhonebookRecordInfo recInfo)56 public SimPhonebookRecord(PhonebookRecordInfo recInfo) { 57 if (recInfo != null) { 58 mRecordId = recInfo.recordId; 59 mAlphaTag = recInfo.name; 60 mNumber = recInfo.number; 61 mEmails = recInfo.emails == null ? null 62 : recInfo.emails.toArray(new String[recInfo.emails.size()]); 63 mAdditionalNumbers = recInfo.additionalNumbers == null ? null 64 : recInfo.additionalNumbers.toArray( 65 new String[recInfo.additionalNumbers.size()]); 66 } 67 } 68 SimPhonebookRecord()69 public SimPhonebookRecord() {} 70 toPhonebookRecordInfo()71 public PhonebookRecordInfo toPhonebookRecordInfo() { 72 PhonebookRecordInfo pbRecordInfo = new PhonebookRecordInfo(); 73 pbRecordInfo.recordId = mRecordId; 74 pbRecordInfo.name = convertNullToEmptyString(mAlphaTag); 75 pbRecordInfo.number = convertNullToEmptyString(convertNumberToRecordFormat(mNumber)); 76 if (mEmails != null) { 77 pbRecordInfo.emails = new ArrayList<>(Arrays.asList(mEmails)); 78 } 79 if (mAdditionalNumbers != null) { 80 for (String addNum : mAdditionalNumbers) { 81 pbRecordInfo.additionalNumbers.add(convertNumberToRecordFormat(addNum)); 82 } 83 } 84 return pbRecordInfo; 85 } 86 toPhonebookRecordInfoAidl()87 public android.hardware.radio.sim.PhonebookRecordInfo toPhonebookRecordInfoAidl() { 88 android.hardware.radio.sim.PhonebookRecordInfo pbRecordInfo = 89 new android.hardware.radio.sim.PhonebookRecordInfo(); 90 pbRecordInfo.recordId = mRecordId; 91 pbRecordInfo.name = convertNullToEmptyString(mAlphaTag); 92 pbRecordInfo.number = convertNullToEmptyString(convertNumberToRecordFormat(mNumber)); 93 if (mEmails != null) { 94 pbRecordInfo.emails = mEmails; 95 } else { 96 pbRecordInfo.emails = new String[0]; 97 } 98 if (mAdditionalNumbers != null) { 99 String[] additionalNumbers = new String[mAdditionalNumbers.length]; 100 for (int i = 0; i < additionalNumbers.length; i++) { 101 additionalNumbers[i] = convertNumberToRecordFormat(mAdditionalNumbers[i]); 102 } 103 pbRecordInfo.additionalNumbers = additionalNumbers; 104 } else { 105 pbRecordInfo.additionalNumbers = new String[0]; 106 } 107 return pbRecordInfo; 108 } 109 getRecordId()110 public int getRecordId() { 111 return mRecordId; 112 } 113 getAlphaTag()114 public String getAlphaTag() { 115 return mAlphaTag; 116 } 117 getNumber()118 public String getNumber() { 119 return mNumber; 120 } 121 getEmails()122 public String[] getEmails() { 123 return mEmails; 124 } 125 getAdditionalNumbers()126 public String[] getAdditionalNumbers() { 127 return mAdditionalNumbers; 128 } 129 130 /** Convert null to an empty String */ convertNullToEmptyString(String str)131 private String convertNullToEmptyString(String str) { 132 return str != null ? str : ""; 133 } 134 135 /** 136 * Convert the SIM PhonebookRecordInfo number to the GSM pause/wild/wait number 137 * @param input the SIM PhonebookRecordInfo number 138 * @return The converted GSM pause/wild/wait number 139 */ convertRecordFormatToNumber(String input)140 private String convertRecordFormatToNumber(String input) { 141 return input == null ? null : input.replace('e', PhoneNumberUtils.WAIT) 142 .replace('T', PhoneNumberUtils.PAUSE) 143 .replace('?', PhoneNumberUtils.WILD); 144 } 145 146 /** 147 * Convert the GSM pause/wild/wait characters to the phone number in the SIM PhonebookRecordInfo 148 * number format 149 * @param input GSM pause/wild/wait character 150 * @return The converted PhonebookRecordInfo number 151 */ convertNumberToRecordFormat(String input)152 private String convertNumberToRecordFormat(String input) { 153 return input == null ? null : input.replace(PhoneNumberUtils.WAIT, 'e') 154 .replace(PhoneNumberUtils.PAUSE, 'T') 155 .replace(PhoneNumberUtils.WILD, '?'); 156 } 157 158 isEmpty()159 public boolean isEmpty() { 160 return TextUtils.isEmpty(mAlphaTag) 161 && TextUtils.isEmpty(mNumber) 162 && ArrayUtils.isEmpty(mEmails) 163 && ArrayUtils.isEmpty(mAdditionalNumbers); 164 } 165 166 @Override toString()167 public String toString() { 168 StringBuilder sb = new StringBuilder(); 169 sb.append("SimPhoneBookRecord{").append("ID =") 170 .append(mRecordId).append(", name = ") 171 .append(mAlphaTag == null ? "null" : mAlphaTag) 172 .append(", number = ").append(mNumber == null ? "null" : mNumber) 173 .append(", email count = ").append(mEmails == null ? 0 : mEmails.length) 174 .append(", email = ").append(Arrays.toString(mEmails)) 175 .append(", ad number count = ") 176 .append(mAdditionalNumbers == null ? 0 : mAdditionalNumbers.length) 177 .append(", ad number = ").append(Arrays.toString(mAdditionalNumbers)) 178 .append("}"); 179 return sb.toString(); 180 } 181 182 public final static class Builder { 183 private int mRecordId = 0; 184 private String mAlphaTag = null; 185 private String mNumber = null; 186 private String[] mEmails; 187 private String[] mAdditionalNumbers; 188 build()189 public SimPhonebookRecord build() { 190 SimPhonebookRecord record = new SimPhonebookRecord(); 191 record.mAlphaTag = mAlphaTag; 192 record.mRecordId = mRecordId; 193 record.mNumber = mNumber; 194 if (mEmails != null) { 195 record.mEmails = mEmails; 196 } 197 if (mAdditionalNumbers != null) { 198 record.mAdditionalNumbers = mAdditionalNumbers; 199 } 200 return record; 201 } 202 setRecordId(int recordId)203 public Builder setRecordId(int recordId) { 204 mRecordId = recordId; 205 return this; 206 } 207 setAlphaTag(String tag)208 public Builder setAlphaTag(String tag) { 209 mAlphaTag = tag; 210 return this; 211 } 212 setNumber(String number)213 public Builder setNumber(String number) { 214 mNumber = number; 215 return this; 216 } 217 setEmails(String[] emails)218 public Builder setEmails(String[] emails) { 219 mEmails = emails; 220 return this; 221 } 222 setAdditionalNumbers(String[] anrs)223 public Builder setAdditionalNumbers(String[] anrs) { 224 mAdditionalNumbers = anrs; 225 return this; 226 } 227 } 228 } 229