1/* 2 * Copyright (C) 2011 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 17package com.android.internal.telephony.ims; 18 19import android.content.Context; 20import android.os.AsyncResult; 21import android.os.Handler; 22import android.os.Message; 23import android.util.Log; 24 25import com.android.internal.telephony.AdnRecord; 26import com.android.internal.telephony.AdnRecordCache; 27import com.android.internal.telephony.CommandsInterface; 28import com.android.internal.telephony.IccFileHandler; 29import com.android.internal.telephony.IccRecords; 30import com.android.internal.telephony.UiccCardApplication; 31import com.android.internal.telephony.gsm.SimTlv; 32import com.android.internal.telephony.gsm.SpnOverride; 33//import com.android.internal.telephony.gsm.VoiceMailConstants; 34 35import java.nio.charset.Charset; 36import java.util.ArrayList; 37 38import static com.android.internal.telephony.IccConstants.EF_DOMAIN; 39import static com.android.internal.telephony.IccConstants.EF_IMPI; 40import static com.android.internal.telephony.IccConstants.EF_IMPU; 41 42/** 43 * {@hide} 44 */ 45public final class IsimUiccRecords extends IccRecords implements IsimRecords { 46 protected static final String LOG_TAG = "GSM"; 47 48 private static final boolean DBG = true; 49 private static final boolean DUMP_RECORDS = false; // Note: PII is logged when this is true 50 51 private static final int EVENT_APP_READY = 1; 52 53 // ISIM EF records (see 3GPP TS 31.103) 54 private String mIsimImpi; // IMS private user identity 55 private String mIsimDomain; // IMS home network domain name 56 private String[] mIsimImpu; // IMS public user identity(s) 57 58 private static final int TAG_ISIM_VALUE = 0x80; // From 3GPP TS 31.103 59 60 public IsimUiccRecords(UiccCardApplication app, Context c, CommandsInterface ci) { 61 super(app, c, ci); 62 63 recordsRequested = false; // No load request is made till SIM ready 64 65 // recordsToLoad is set to 0 because no requests are made yet 66 recordsToLoad = 0; 67 68 mParentApp.registerForReady(this, EVENT_APP_READY, null); 69 } 70 71 @Override 72 public void dispose() { 73 log("Disposing " + this); 74 //Unregister for all events 75 mParentApp.unregisterForReady(this); 76 resetRecords(); 77 super.dispose(); 78 } 79 80 // ***** Overridden from Handler 81 public void handleMessage(Message msg) { 82 if (mDestroyed.get()) { 83 Log.e(LOG_TAG, "Received message " + msg + 84 "[" + msg.what + "] while being destroyed. Ignoring."); 85 return; 86 } 87 88 try { 89 switch (msg.what) { 90 case EVENT_APP_READY: 91 onReady(); 92 break; 93 94 default: 95 super.handleMessage(msg); // IccRecords handles generic record load responses 96 97 } 98 } catch (RuntimeException exc) { 99 // I don't want these exceptions to be fatal 100 Log.w(LOG_TAG, "Exception parsing SIM record", exc); 101 } 102 } 103 104 protected void fetchIsimRecords() { 105 recordsRequested = true; 106 107 mFh.loadEFTransparent(EF_IMPI, obtainMessage( 108 IccRecords.EVENT_GET_ICC_RECORD_DONE, new EfIsimImpiLoaded())); 109 recordsToLoad++; 110 111 mFh.loadEFLinearFixedAll(EF_IMPU, obtainMessage( 112 IccRecords.EVENT_GET_ICC_RECORD_DONE, new EfIsimImpuLoaded())); 113 recordsToLoad++; 114 115 mFh.loadEFTransparent(EF_DOMAIN, obtainMessage( 116 IccRecords.EVENT_GET_ICC_RECORD_DONE, new EfIsimDomainLoaded())); 117 recordsToLoad++; 118 119 log("fetchIsimRecords " + recordsToLoad); 120 } 121 122 protected void resetRecords() { 123 // recordsRequested is set to false indicating that the SIM 124 // read requests made so far are not valid. This is set to 125 // true only when fresh set of read requests are made. 126 recordsRequested = false; 127 } 128 129 private class EfIsimImpiLoaded implements IccRecords.IccRecordLoaded { 130 public String getEfName() { 131 return "EF_ISIM_IMPI"; 132 } 133 public void onRecordLoaded(AsyncResult ar) { 134 byte[] data = (byte[]) ar.result; 135 mIsimImpi = isimTlvToString(data); 136 if (DUMP_RECORDS) log("EF_IMPI=" + mIsimImpi); 137 } 138 } 139 140 private class EfIsimImpuLoaded implements IccRecords.IccRecordLoaded { 141 public String getEfName() { 142 return "EF_ISIM_IMPU"; 143 } 144 public void onRecordLoaded(AsyncResult ar) { 145 ArrayList<byte[]> impuList = (ArrayList<byte[]>) ar.result; 146 if (DBG) log("EF_IMPU record count: " + impuList.size()); 147 mIsimImpu = new String[impuList.size()]; 148 int i = 0; 149 for (byte[] identity : impuList) { 150 String impu = isimTlvToString(identity); 151 if (DUMP_RECORDS) log("EF_IMPU[" + i + "]=" + impu); 152 mIsimImpu[i++] = impu; 153 } 154 } 155 } 156 157 private class EfIsimDomainLoaded implements IccRecords.IccRecordLoaded { 158 public String getEfName() { 159 return "EF_ISIM_DOMAIN"; 160 } 161 public void onRecordLoaded(AsyncResult ar) { 162 byte[] data = (byte[]) ar.result; 163 mIsimDomain = isimTlvToString(data); 164 if (DUMP_RECORDS) log("EF_DOMAIN=" + mIsimDomain); 165 } 166 } 167 168 /** 169 * ISIM records for IMS are stored inside a Tag-Length-Value record as a UTF-8 string 170 * with tag value 0x80. 171 * @param record the byte array containing the IMS data string 172 * @return the decoded String value, or null if the record can't be decoded 173 */ 174 private static String isimTlvToString(byte[] record) { 175 SimTlv tlv = new SimTlv(record, 0, record.length); 176 do { 177 if (tlv.getTag() == TAG_ISIM_VALUE) { 178 return new String(tlv.getData(), Charset.forName("UTF-8")); 179 } 180 } while (tlv.nextObject()); 181 182 Log.e(LOG_TAG, "[ISIM] can't find TLV tag in ISIM record, returning null"); 183 return null; 184 } 185 186 @Override 187 protected void onRecordLoaded() { 188 // One record loaded successfully or failed, In either case 189 // we need to update the recordsToLoad count 190 recordsToLoad -= 1; 191 192 if (recordsToLoad == 0 && recordsRequested == true) { 193 onAllRecordsLoaded(); 194 } else if (recordsToLoad < 0) { 195 loge("recordsToLoad <0, programmer error suspected"); 196 recordsToLoad = 0; 197 } 198 } 199 200 @Override 201 protected void onAllRecordsLoaded() { 202 recordsLoadedRegistrants.notifyRegistrants( 203 new AsyncResult(null, null, null)); 204 } 205 206 /** 207 * Return the IMS private user identity (IMPI). 208 * Returns null if the IMPI hasn't been loaded or isn't present on the ISIM. 209 * @return the IMS private user identity string, or null if not available 210 */ 211 @Override 212 public String getIsimImpi() { 213 return mIsimImpi; 214 } 215 216 /** 217 * Return the IMS home network domain name. 218 * Returns null if the IMS domain hasn't been loaded or isn't present on the ISIM. 219 * @return the IMS home network domain name, or null if not available 220 */ 221 @Override 222 public String getIsimDomain() { 223 return mIsimDomain; 224 } 225 226 /** 227 * Return an array of IMS public user identities (IMPU). 228 * Returns null if the IMPU hasn't been loaded or isn't present on the ISIM. 229 * @return an array of IMS public user identity strings, or null if not available 230 */ 231 @Override 232 public String[] getIsimImpu() { 233 return (mIsimImpu != null) ? mIsimImpu.clone() : null; 234 } 235 236 @Override 237 public int getDisplayRule(String plmn) { 238 // Not applicable to Isim 239 return 0; 240 } 241 242 @Override 243 public void onReady() { 244 fetchIsimRecords(); 245 } 246 247 @Override 248 public void onRefresh(boolean fileChanged, int[] fileList) { 249 // We do not handle it in Isim 250 } 251 252 @Override 253 public void setVoiceMailNumber(String alphaTag, String voiceNumber, 254 Message onComplete) { 255 // Not applicable to Isim 256 } 257 258 @Override 259 public void setVoiceMessageWaiting(int line, int countWaiting) { 260 // Not applicable to Isim 261 } 262 263 @Override 264 protected void log(String s) { 265 if (DBG) Log.d(LOG_TAG, "[ISIM] " + s); 266 } 267 268 @Override 269 protected void loge(String s) { 270 if (DBG) Log.e(LOG_TAG, "[ISIM] " + s); 271 } 272} 273