Query voice radio tech when the SIMRecords finish loading.

This is required since the overlays may change once the SIMRecords have
finished loading and the overlays help determine the volte radio tech
replacement.

Bug: 18108765
Change-Id: Iebc17fda48cae28abb2e9658c4bb3b7d2ed18e60
diff --git a/src/java/com/android/internal/telephony/PhoneProxy.java b/src/java/com/android/internal/telephony/PhoneProxy.java
index c308123..bdbb646 100644
--- a/src/java/com/android/internal/telephony/PhoneProxy.java
+++ b/src/java/com/android/internal/telephony/PhoneProxy.java
@@ -72,6 +72,7 @@
     private static final int EVENT_REQUEST_VOICE_RADIO_TECH_DONE = 3;
     private static final int EVENT_RIL_CONNECTED = 4;
     private static final int EVENT_UPDATE_PHONE_OBJECT = 5;
+    private static final int EVENT_SIM_RECORDS_LOADED = 6;
 
     private int mPhoneId = 0;
 
@@ -144,6 +145,16 @@
             phoneObjectUpdater(msg.arg1);
             break;
 
+        case EVENT_SIM_RECORDS_LOADED:
+            // Only check for the voice radio tech if it not going to be updated by the voice
+            // registration changes.
+            if (!mActivePhone.getContext().getResources().getBoolean(
+                    com.android.internal.R.bool.config_switch_phone_on_voice_reg_state_change)) {
+                mCommandsInterface.getVoiceRadioTechnology(obtainMessage(
+                        EVENT_REQUEST_VOICE_RADIO_TECH_DONE));
+            }
+            break;
+
         default:
             loge("Error! This handler was not registered for this message type. Message: "
                     + msg.what);
@@ -264,12 +275,12 @@
 
         if (oldPhone != null) {
             outgoingPhoneName = ((PhoneBase) oldPhone).getPhoneName();
+            oldPhone.unregisterForSimRecordsLoaded(this);
         }
 
         logd("Switching Voice Phone : " + outgoingPhoneName + " >>> "
                 + (ServiceState.isGsm(newVoiceRadioTech) ? "GSM" : "CDMA"));
 
-
         if (ServiceState.isCdma(newVoiceRadioTech)) {
             mActivePhone = PhoneFactory.getCdmaPhone(mPhoneId);
         } else if (ServiceState.isGsm(newVoiceRadioTech)) {
@@ -289,6 +300,7 @@
             if (imsPhone != null) {
                 mActivePhone.acquireOwnershipOfImsPhone(imsPhone);
             }
+            mActivePhone.registerForSimRecordsLoaded(this, EVENT_SIM_RECORDS_LOADED, null);
         }
 
         if (oldPhone != null) {
@@ -1291,6 +1303,9 @@
 
     @Override
     public void dispose() {
+        if (mActivePhone != null) {
+            mActivePhone.unregisterForSimRecordsLoaded(this);
+        }
         mCommandsInterface.unregisterForOn(this);
         mCommandsInterface.unregisterForVoiceRadioTechChanged(this);
         mCommandsInterface.unregisterForRilConnected(this);
diff --git a/src/java/com/android/internal/telephony/cdma/CDMALTEPhone.java b/src/java/com/android/internal/telephony/cdma/CDMALTEPhone.java
index a3438b9..6836727 100644
--- a/src/java/com/android/internal/telephony/cdma/CDMALTEPhone.java
+++ b/src/java/com/android/internal/telephony/cdma/CDMALTEPhone.java
@@ -104,6 +104,9 @@
     @Override
     public void dispose() {
         synchronized(PhoneProxy.lockForRadioTechnologyChange) {
+            if (mSimRecords != null) {
+                mSimRecords.unregisterForRecordsLoaded(this);
+            }
             super.dispose();
         }
     }
@@ -114,6 +117,25 @@
     }
 
     @Override
+    public void handleMessage(Message msg) {
+        AsyncResult ar;
+        Message onComplete;
+
+        if (!mIsTheCurrentActivePhone) {
+            Rlog.e(LOG_TAG, "Received message " + msg +
+                    "[" + msg.what + "] while being destroyed. Ignoring.");
+            return;
+        }
+        switch(msg.what) {
+            case EVENT_SIM_RECORDS_LOADED:
+                mSimRecordsLoadedRegistrants.notifyRegistrants();
+                break;
+
+            default:
+                super.handleMessage(msg);
+        }
+    }
+    @Override
     public PhoneConstants.DataState getDataConnectionState(String apnType) {
         PhoneConstants.DataState ret = PhoneConstants.DataState.DISCONNECTED;
 
@@ -234,6 +256,10 @@
 
     @Override
     protected void onUpdateIccAvailability() {
+        if (mSimRecords != null) {
+            mSimRecords.unregisterForRecordsLoaded(this);
+        }
+
         if (mUiccController == null ) {
             return;
         }
@@ -256,6 +282,9 @@
             newSimRecords = (SIMRecords) newUiccApplication.getIccRecords();
         }
         mSimRecords = newSimRecords;
+        if (mSimRecords != null) {
+            mSimRecords.registerForRecordsLoaded(this, EVENT_SIM_RECORDS_LOADED, null);
+        }
 
         super.onUpdateIccAvailability();
     }
@@ -397,6 +426,17 @@
     }
 
     @Override
+    public void registerForSimRecordsLoaded(Handler h, int what, Object obj) {
+        mSimRecordsLoadedRegistrants.addUnique(h, what, obj);
+    }
+
+    @Override
+    public void unregisterForSimRecordsLoaded(Handler h) {
+        mSimRecordsLoadedRegistrants.remove(h);
+    }
+
+
+    @Override
     protected void log(String s) {
             Rlog.d(LOG_LTE_TAG, s);
     }
diff --git a/src/java/com/android/internal/telephony/cdma/CdmaLteServiceStateTracker.java b/src/java/com/android/internal/telephony/cdma/CdmaLteServiceStateTracker.java
index 483a9ea..1170037 100644
--- a/src/java/com/android/internal/telephony/cdma/CdmaLteServiceStateTracker.java
+++ b/src/java/com/android/internal/telephony/cdma/CdmaLteServiceStateTracker.java
@@ -66,6 +66,7 @@
     public CdmaLteServiceStateTracker(CDMALTEPhone phone) {
         super(phone, new CellInfoLte());
         mCdmaLtePhone = phone;
+        mCdmaLtePhone.registerForSimRecordsLoaded(this, EVENT_SIM_RECORDS_LOADED, null);
         mCellInfoLte = (CellInfoLte) mCellInfo;
 
         ((CellInfoLte)mCellInfo).setCellSignalStrength(new CellSignalStrengthLte());
@@ -75,6 +76,12 @@
     }
 
     @Override
+    public void dispose() {
+        mPhone.unregisterForSimRecordsLoaded(this);
+        super.dispose();
+    }
+
+    @Override
     public void handleMessage(Message msg) {
         AsyncResult ar;
         int[] ints;
@@ -114,6 +121,9 @@
             // the latest variables.
             pollState();
             break;
+        case EVENT_SIM_RECORDS_LOADED:
+            updatePhoneObject();
+            break;
         case EVENT_ALL_DATA_DISCONNECTED:
             int dds = SubscriptionManager.getDefaultDataSubId();
             ProxyController.getInstance().unregisterForAllDataDisconnected(dds, this);