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