[automerger skipped] Merge "Fixed the mismatched protocol anomaly detector" into tm-qpr-dev am: 087c47037c am: bff9235022 -s ours

am skip reason: Merged-In I13bb670117a51c4778a06e8125db6693bbde2cd8 with SHA-1 0d0a315b9b is already in history

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/opt/telephony/+/20956705

Change-Id: I7e6a58c48125013ba768b02acb6f085b3c7583a4
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/Android.bp b/Android.bp
index fab53d1..4097571 100644
--- a/Android.bp
+++ b/Android.bp
@@ -83,13 +83,14 @@
         "android.hardware.radio-V1.4-java",
         "android.hardware.radio-V1.5-java",
         "android.hardware.radio-V1.6-java",
-        "android.hardware.radio.config-V1-java",
-        "android.hardware.radio.data-V1-java",
-        "android.hardware.radio.messaging-V1-java",
-        "android.hardware.radio.modem-V1-java",
-        "android.hardware.radio.network-V1-java",
-        "android.hardware.radio.sim-V1-java",
-        "android.hardware.radio.voice-V1-java",
+        "android.hardware.radio.config-V2-java",
+        "android.hardware.radio.data-V2-java",
+        "android.hardware.radio.ims-V1-java",
+        "android.hardware.radio.messaging-V2-java",
+        "android.hardware.radio.modem-V2-java",
+        "android.hardware.radio.network-V2-java",
+        "android.hardware.radio.sim-V2-java",
+        "android.hardware.radio.voice-V2-java",
         "voip-common",
         "ims-common",
         "unsupportedappusage",
diff --git a/proto/src/persist_atoms.proto b/proto/src/persist_atoms.proto
index 3a79cdc..30ad097 100644
--- a/proto/src/persist_atoms.proto
+++ b/proto/src/persist_atoms.proto
@@ -180,6 +180,15 @@
 
     /* Unmetered networks information. */
     repeated UnmeteredNetworks unmetered_networks = 52;
+
+    /* Outgoing Short Code SMS statistics and information. */
+    repeated OutgoingShortCodeSms outgoing_short_code_sms = 53;
+
+    /* Timestamp of last outgoing_short_code_sms pull. */
+    optional int64 outgoing_short_code_sms_pull_timestamp_millis = 54;
+
+    /* Number of time the user toggled the data switch feature since the last collection. */
+    optional int32 auto_data_switch_toggle_count = 55;
 }
 
 // The canonical versions of the following enums live in:
@@ -250,6 +259,7 @@
     optional int32 carrier_id = 13;
     optional int64 message_id = 14;
     optional int32 count = 15;
+    optional bool is_managed_profile = 16;
 
     // Internal use only
     optional int32 hashCode = 10001;
@@ -271,6 +281,9 @@
     optional int32 retry_id = 13;
     optional int64 interval_millis = 14;
     optional int32 count = 15;
+    optional int32 send_error_code = 16;
+    optional int32 network_error_code = 17;
+    optional bool is_managed_profile = 18;
 
     // Internal use only
     optional int32 hashCode = 10001;
@@ -305,6 +318,7 @@
     optional int32 band_at_end = 19;
     repeated int32 handover_failure_causes = 20;
     repeated int32 handover_failure_rat = 21;
+    optional bool is_non_dds = 22;
 }
 
 message CellularServiceState {
@@ -318,6 +332,7 @@
     optional int32 carrier_id = 8;
     optional int64 total_time_millis = 9; // Duration needs to be rounded when pulled
     optional bool is_emergency_only = 10;
+    optional bool is_internet_pdn_up = 11;
 
     // Internal use only
     optional int64 last_used_millis = 10001;
@@ -517,3 +532,9 @@
     optional int32 carrier_id = 2;
     optional int64 unmetered_networks_bitmask = 3;
 }
+
+message OutgoingShortCodeSms {
+    optional int32 category = 1;
+    optional int32 xml_version = 2;
+    optional int32 short_code_sms_count = 3;
+}
diff --git a/proto/src/telephony.proto b/proto/src/telephony.proto
index 6d3d711..b87728b 100644
--- a/proto/src/telephony.proto
+++ b/proto/src/telephony.proto
@@ -931,6 +931,9 @@
 
       /** Data switch caused by CBRS switch. */
       DATA_SWITCH_REASON_CBRS = 3;
+
+      /** Data switch caused by non-default SIM having better availability(e.g.signal). */
+      DATA_SWITCH_REASON_AUTO = 4;
     }
 
     /** The reason for data switch. */
diff --git a/src/java/com/android/internal/telephony/BaseCommands.java b/src/java/com/android/internal/telephony/BaseCommands.java
index 972884a..f36161a 100644
--- a/src/java/com/android/internal/telephony/BaseCommands.java
+++ b/src/java/com/android/internal/telephony/BaseCommands.java
@@ -114,6 +114,10 @@
     protected RegistrantList mBarringInfoChangedRegistrants = new RegistrantList();
     protected RegistrantList mSimPhonebookChangedRegistrants = new RegistrantList();
     protected RegistrantList mSimPhonebookRecordsReceivedRegistrants = new RegistrantList();
+    protected RegistrantList mEmergencyNetworkScanRegistrants = new RegistrantList();
+    protected RegistrantList mConnectionSetupFailureRegistrants = new RegistrantList();
+    protected RegistrantList mNotifyAnbrRegistrants = new RegistrantList();
+    protected RegistrantList mTriggerImsDeregistrationRegistrants = new RegistrantList();
 
     @UnsupportedAppUsage
     protected Registrant mGsmSmsRegistrant;
@@ -1132,4 +1136,56 @@
     @Override
     public void updateSimPhonebookRecord(SimPhonebookRecord phonebookRecord, Message result) {
     }
+
+    /**
+     * Register for Emergency network scan result.
+     *
+     * @param h Handler for notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+    @Override
+    public void registerForEmergencyNetworkScan(Handler h, int what, Object obj) {
+        mEmergencyNetworkScanRegistrants.add(h, what, obj);
+    }
+
+    /**
+     * Unregister for Emergency network scan result.
+     *
+     * @param h Handler to be removed from the registrant list.
+     */
+    @Override
+    public void unregisterForEmergencyNetworkScan(Handler h) {
+        mEmergencyNetworkScanRegistrants.remove(h);
+    }
+
+    @Override
+    public void registerForConnectionSetupFailure(Handler h, int what, Object obj) {
+        mConnectionSetupFailureRegistrants.addUnique(h, what, obj);
+    }
+
+    @Override
+    public void unregisterForConnectionSetupFailure(Handler h) {
+        mConnectionSetupFailureRegistrants.remove(h);
+    }
+
+    @Override
+    public void registerForNotifyAnbr(Handler h, int what, Object obj) {
+        mNotifyAnbrRegistrants.addUnique(h, what, obj);
+    }
+
+    @Override
+    public void unregisterForNotifyAnbr(Handler h) {
+        mNotifyAnbrRegistrants.remove(h);
+    }
+
+    @Override
+    public void registerForTriggerImsDeregistration(Handler h, int what, Object obj) {
+        mTriggerImsDeregistrationRegistrants.add(h, what, obj);
+    }
+
+    @Override
+    public void unregisterForTriggerImsDeregistration(Handler h) {
+        mTriggerImsDeregistrationRegistrants.remove(h);
+    }
 }
diff --git a/src/java/com/android/internal/telephony/CallStateException.java b/src/java/com/android/internal/telephony/CallStateException.java
index 3fdc444..4de16cb 100644
--- a/src/java/com/android/internal/telephony/CallStateException.java
+++ b/src/java/com/android/internal/telephony/CallStateException.java
@@ -35,6 +35,7 @@
     public static final int ERROR_CALLING_DISABLED = 5;
     public static final int ERROR_TOO_MANY_CALLS = 6;
     public static final int ERROR_OTASP_PROVISIONING_IN_PROCESS = 7;
+    public static final int ERROR_FDN_BLOCKED = 8;
 
     public
     CallStateException()
diff --git a/src/java/com/android/internal/telephony/CallWaitingController.java b/src/java/com/android/internal/telephony/CallWaitingController.java
new file mode 100644
index 0000000..4940eb6
--- /dev/null
+++ b/src/java/com/android/internal/telephony/CallWaitingController.java
@@ -0,0 +1,697 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import static android.telephony.CarrierConfigManager.ImsSs.CALL_WAITING_SYNC_FIRST_CHANGE;
+import static android.telephony.CarrierConfigManager.ImsSs.CALL_WAITING_SYNC_FIRST_POWER_UP;
+import static android.telephony.CarrierConfigManager.ImsSs.CALL_WAITING_SYNC_IMS_ONLY;
+import static android.telephony.CarrierConfigManager.ImsSs.CALL_WAITING_SYNC_NONE;
+import static android.telephony.CarrierConfigManager.ImsSs.CALL_WAITING_SYNC_USER_CHANGE;
+import static android.telephony.CarrierConfigManager.ImsSs.KEY_TERMINAL_BASED_CALL_WAITING_DEFAULT_ENABLED_BOOL;
+import static android.telephony.CarrierConfigManager.ImsSs.KEY_TERMINAL_BASED_CALL_WAITING_SYNC_TYPE_INT;
+import static android.telephony.CarrierConfigManager.ImsSs.KEY_UT_TERMINAL_BASED_SERVICES_INT_ARRAY;
+import static android.telephony.CarrierConfigManager.ImsSs.SUPPLEMENTARY_SERVICE_CW;
+
+import static com.android.internal.telephony.CommandsInterface.SERVICE_CLASS_NONE;
+import static com.android.internal.telephony.CommandsInterface.SERVICE_CLASS_VOICE;
+
+import android.annotation.Nullable;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.SharedPreferences;
+import android.os.AsyncResult;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
+import android.telephony.ServiceState;
+import android.telephony.SubscriptionManager;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.IndentingPrintWriter;
+import com.android.telephony.Rlog;
+
+import java.io.PrintWriter;
+
+/**
+ * Controls the change of the user setting of the call waiting service
+ */
+public class CallWaitingController extends Handler {
+
+    public static final String LOG_TAG = "CallWaitingCtrl";
+    private static final boolean DBG = false; /* STOPSHIP if true */
+
+    // Terminal-based call waiting is not supported. */
+    public static final int TERMINAL_BASED_NOT_SUPPORTED = -1;
+    // Terminal-based call waiting is supported but not activated. */
+    public static final int TERMINAL_BASED_NOT_ACTIVATED = 0;
+    // Terminal-based call waiting is supported and activated. */
+    public static final int TERMINAL_BASED_ACTIVATED = 1;
+
+    private static final int EVENT_SET_CALL_WAITING_DONE = 1;
+    private static final int EVENT_GET_CALL_WAITING_DONE = 2;
+    private static final int EVENT_REGISTERED_TO_NETWORK = 3;
+    private static final int EVENT_CARRIER_CONFIG_CHANGED = 4;
+
+    // Class to pack mOnComplete object passed by the caller
+    private static class Cw {
+        final boolean mEnable;
+        final Message mOnComplete;
+        final boolean mImsRegistered;
+
+        Cw(boolean enable, boolean imsRegistered, Message onComplete) {
+            mEnable = enable;
+            mOnComplete = onComplete;
+            mImsRegistered = imsRegistered;
+        }
+    }
+
+    @VisibleForTesting
+    public static final String PREFERENCE_TBCW = "terminal_based_call_waiting";
+    @VisibleForTesting
+    public static final String KEY_SUB_ID = "subId";
+    @VisibleForTesting
+    public static final String KEY_STATE = "state";
+    @VisibleForTesting
+    public static final String KEY_CS_SYNC = "cs_sync";
+
+    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (intent == null) {
+                return;
+            }
+            if (CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) {
+                Bundle bundle = intent.getExtras();
+                if (bundle == null) {
+                    return;
+                }
+                int slotId = bundle.getInt(CarrierConfigManager.EXTRA_SLOT_INDEX,
+                        SubscriptionManager.INVALID_PHONE_INDEX);
+
+                if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
+                    loge("onReceive ACTION_CARRIER_CONFIG_CHANGED invalid slotId "
+                            + slotId);
+                    return;
+                }
+
+                if (slotId == mPhone.getPhoneId()) {
+                    sendEmptyMessage(EVENT_CARRIER_CONFIG_CHANGED);
+                }
+            }
+        }
+    };
+
+    private boolean mSupportedByImsService = false;
+    private boolean mValidSubscription = false;
+
+    // The user's last setting of terminal-based call waiting
+    private int mCallWaitingState = TERMINAL_BASED_NOT_SUPPORTED;
+
+    private int mSyncPreference = CALL_WAITING_SYNC_NONE;
+    private int mLastSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+
+    private boolean mCsEnabled = false;
+    private boolean mRegisteredForNetworkAttach = false;
+    private boolean mImsRegistered = false;
+
+    private final GsmCdmaPhone mPhone;
+    private final ServiceStateTracker mSST;
+    private final Context mContext;
+
+    // Constructors
+    public CallWaitingController(GsmCdmaPhone phone) {
+        mPhone = phone;
+        mSST = phone.getServiceStateTracker();
+        mContext = phone.getContext();
+    }
+
+    private void initialize() {
+        mContext.registerReceiver(mReceiver, new IntentFilter(
+                CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));
+
+        int phoneId = mPhone.getPhoneId();
+        int subId = mPhone.getSubId();
+        SharedPreferences sp =
+                mContext.getSharedPreferences(PREFERENCE_TBCW, Context.MODE_PRIVATE);
+        mLastSubId = sp.getInt(KEY_SUB_ID + phoneId, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        mCallWaitingState = sp.getInt(KEY_STATE + subId, TERMINAL_BASED_NOT_SUPPORTED);
+        mSyncPreference = sp.getInt(KEY_CS_SYNC + phoneId, CALL_WAITING_SYNC_NONE);
+
+        logi("initialize phoneId=" + phoneId
+                + ", lastSubId=" + mLastSubId + ", subId=" + subId
+                + ", state=" + mCallWaitingState + ", sync=" + mSyncPreference
+                + ", csEnabled=" + mCsEnabled);
+    }
+
+    /**
+     * Returns the cached user setting.
+     *
+     * Possible values are
+     * {@link #TERMINAL_BASED_NOT_SUPPORTED},
+     * {@link #TERMINAL_BASED_NOT_ACTIVATED}, and
+     * {@link #TERMINAL_BASED_ACTIVATED}.
+     *
+     * @param forCsOnly indicates the caller expects the result for CS calls only
+     */
+    @VisibleForTesting
+    public synchronized int getTerminalBasedCallWaitingState(boolean forCsOnly) {
+        if (forCsOnly && (!mImsRegistered) && mSyncPreference == CALL_WAITING_SYNC_IMS_ONLY) {
+            return TERMINAL_BASED_NOT_SUPPORTED;
+        }
+        if (!mValidSubscription) return TERMINAL_BASED_NOT_SUPPORTED;
+        return mCallWaitingState;
+    }
+
+    /**
+     * Serves the user's requests to interrogate the call waiting service
+     *
+     * @return true when terminal-based call waiting is supported, otherwise false
+     */
+    @VisibleForTesting
+    public synchronized boolean getCallWaiting(@Nullable Message onComplete) {
+        if (mCallWaitingState == TERMINAL_BASED_NOT_SUPPORTED) return false;
+
+        logi("getCallWaiting " + mCallWaitingState);
+
+        if (mSyncPreference == CALL_WAITING_SYNC_FIRST_CHANGE) {
+            // Interrogate CW in CS network
+            if (!mCsEnabled) {
+                // skip interrogation if CS is not available and IMS is registered
+                if (isCircuitSwitchedNetworkAvailable() || !isImsRegistered()) {
+                    Cw cw = new Cw(false, isImsRegistered(), onComplete);
+                    Message resp = obtainMessage(EVENT_GET_CALL_WAITING_DONE, 0, 0, cw);
+                    mPhone.mCi.queryCallWaiting(SERVICE_CLASS_NONE, resp);
+                    return true;
+                }
+            }
+        }
+
+        if (mSyncPreference == CALL_WAITING_SYNC_NONE
+                || mSyncPreference == CALL_WAITING_SYNC_FIRST_CHANGE
+                || mSyncPreference == CALL_WAITING_SYNC_FIRST_POWER_UP
+                || isSyncImsOnly()) {
+            sendGetCallWaitingResponse(onComplete);
+            return true;
+        } else if (mSyncPreference == CALL_WAITING_SYNC_USER_CHANGE
+                || mSyncPreference == CALL_WAITING_SYNC_IMS_ONLY) {
+            Cw cw = new Cw(false, isImsRegistered(), onComplete);
+            Message resp = obtainMessage(EVENT_GET_CALL_WAITING_DONE, 0, 0, cw);
+            mPhone.mCi.queryCallWaiting(SERVICE_CLASS_NONE, resp);
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Serves the user's requests to set the call waiting service
+     *
+     * @param serviceClass the target service class. Values are CommandsInterface.SERVICE_CLASS_*.
+     * @return true when terminal-based call waiting is supported, otherwise false
+     */
+    @VisibleForTesting
+    public synchronized boolean setCallWaiting(boolean enable,
+            int serviceClass, @Nullable Message onComplete) {
+        if (mCallWaitingState == TERMINAL_BASED_NOT_SUPPORTED) return false;
+
+        if ((serviceClass & SERVICE_CLASS_VOICE) != SERVICE_CLASS_VOICE) return false;
+
+        logi("setCallWaiting enable=" + enable + ", service=" + serviceClass);
+
+        if (mSyncPreference == CALL_WAITING_SYNC_FIRST_CHANGE) {
+            // Enable CW in the CS network
+            if (!mCsEnabled && enable) {
+                if (isCircuitSwitchedNetworkAvailable() || !isImsRegistered()) {
+                    Cw cw = new Cw(true, isImsRegistered(), onComplete);
+                    Message resp = obtainMessage(EVENT_SET_CALL_WAITING_DONE, 0, 0, cw);
+                    mPhone.mCi.setCallWaiting(true, serviceClass, resp);
+                    return true;
+                } else {
+                    // CS network is not available, however, IMS is registered.
+                    // Enabling the service in the CS network will be delayed.
+                    registerForNetworkAttached();
+                }
+            }
+        }
+
+        if (mSyncPreference == CALL_WAITING_SYNC_NONE
+                || mSyncPreference == CALL_WAITING_SYNC_FIRST_CHANGE
+                || mSyncPreference == CALL_WAITING_SYNC_FIRST_POWER_UP
+                || isSyncImsOnly()) {
+            updateState(
+                    enable ? TERMINAL_BASED_ACTIVATED : TERMINAL_BASED_NOT_ACTIVATED);
+
+            sendToTarget(onComplete, null, null);
+            return true;
+        } else if (mSyncPreference == CALL_WAITING_SYNC_USER_CHANGE
+                || mSyncPreference == CALL_WAITING_SYNC_IMS_ONLY) {
+            Cw cw = new Cw(enable, isImsRegistered(), onComplete);
+            Message resp = obtainMessage(EVENT_SET_CALL_WAITING_DONE, 0, 0, cw);
+            mPhone.mCi.setCallWaiting(enable, serviceClass, resp);
+            return true;
+        }
+
+        return false;
+    }
+
+    @Override
+    public void handleMessage(Message msg) {
+        switch (msg.what) {
+            case EVENT_SET_CALL_WAITING_DONE:
+                onSetCallWaitingDone((AsyncResult) msg.obj);
+                break;
+            case EVENT_GET_CALL_WAITING_DONE:
+                onGetCallWaitingDone((AsyncResult) msg.obj);
+                break;
+            case EVENT_REGISTERED_TO_NETWORK:
+                onRegisteredToNetwork();
+                break;
+            case EVENT_CARRIER_CONFIG_CHANGED:
+                onCarrierConfigChanged();
+                break;
+            default:
+                break;
+        }
+    }
+
+    private synchronized void onSetCallWaitingDone(AsyncResult ar) {
+        if (ar.userObj == null) {
+            // For the case, CALL_WAITING_SYNC_FIRST_POWER_UP
+            if (DBG) logd("onSetCallWaitingDone to sync on network attached");
+            if (ar.exception == null) {
+                updateSyncState(true);
+            } else {
+                loge("onSetCallWaitingDone e=" + ar.exception);
+            }
+            return;
+        }
+
+        if (!(ar.userObj instanceof Cw)) {
+            // Unexpected state
+            if (DBG) logd("onSetCallWaitingDone unexpected result");
+            return;
+        }
+
+        if (DBG) logd("onSetCallWaitingDone");
+        Cw cw = (Cw) ar.userObj;
+
+        if (mSyncPreference == CALL_WAITING_SYNC_IMS_ONLY) {
+            // do not synchronize service state between CS and IMS
+            sendToTarget(cw.mOnComplete, ar.result, ar.exception);
+            return;
+        }
+
+        if (ar.exception == null) {
+            if (mSyncPreference == CALL_WAITING_SYNC_FIRST_CHANGE) {
+                // SYNC_FIRST_CHANGE implies cw.mEnable is true.
+                updateSyncState(true);
+            }
+            updateState(
+                    cw.mEnable ? TERMINAL_BASED_ACTIVATED : TERMINAL_BASED_NOT_ACTIVATED);
+        } else if (mSyncPreference == CALL_WAITING_SYNC_FIRST_CHANGE) {
+            if (cw.mImsRegistered) {
+                // IMS is registered. Do not notify error.
+                // SYNC_FIRST_CHANGE implies cw.mEnable is true.
+                updateState(TERMINAL_BASED_ACTIVATED);
+                sendToTarget(cw.mOnComplete, null, null);
+                return;
+            }
+        }
+        sendToTarget(cw.mOnComplete, ar.result, ar.exception);
+    }
+
+    private synchronized void onGetCallWaitingDone(AsyncResult ar) {
+        if (ar.userObj == null) {
+            // For the case, CALL_WAITING_SYNC_FIRST_POWER_UP
+            if (DBG) logd("onGetCallWaitingDone to sync on network attached");
+            boolean enabled = false;
+            if (ar.exception == null) {
+                //resp[0]: 1 if enabled, 0 otherwise
+                //resp[1]: bitwise ORs of SERVICE_CLASS_* constants
+                int[] resp = (int[]) ar.result;
+                if (resp != null && resp.length > 1) {
+                    enabled = (resp[0] == 1)
+                            && (resp[1] & SERVICE_CLASS_VOICE) == SERVICE_CLASS_VOICE;
+                } else {
+                    loge("onGetCallWaitingDone unexpected response");
+                }
+            } else {
+                loge("onGetCallWaitingDone e=" + ar.exception);
+            }
+            if (enabled) {
+                updateSyncState(true);
+            } else {
+                logi("onGetCallWaitingDone enabling CW service in CS network");
+                mPhone.mCi.setCallWaiting(true, SERVICE_CLASS_VOICE,
+                        obtainMessage(EVENT_SET_CALL_WAITING_DONE));
+            }
+            unregisterForNetworkAttached();
+            return;
+        }
+
+        if (!(ar.userObj instanceof Cw)) {
+            // Unexpected state
+            if (DBG) logd("onGetCallWaitingDone unexpected result");
+            return;
+        }
+
+        if (DBG) logd("onGetCallWaitingDone");
+        Cw cw = (Cw) ar.userObj;
+
+        if (mSyncPreference == CALL_WAITING_SYNC_IMS_ONLY) {
+            // do not synchronize service state between CS and IMS
+            sendToTarget(cw.mOnComplete, ar.result, ar.exception);
+            return;
+        }
+
+        if (ar.exception == null) {
+            int[] resp = (int[]) ar.result;
+            //resp[0]: 1 if enabled, 0 otherwise
+            //resp[1]: bitwise ORs of SERVICE_CLASS_
+            if (resp == null || resp.length < 2) {
+                logi("onGetCallWaitingDone unexpected response");
+                if (mSyncPreference == CALL_WAITING_SYNC_FIRST_CHANGE) {
+                    // no exception but unexpected response, local setting is preferred.
+                    sendGetCallWaitingResponse(cw.mOnComplete);
+                } else {
+                    sendToTarget(cw.mOnComplete, ar.result, ar.exception);
+                }
+                return;
+            }
+
+            boolean enabled = resp[0] == 1
+                    && (resp[1] & SERVICE_CLASS_VOICE) == SERVICE_CLASS_VOICE;
+
+            if (mSyncPreference == CALL_WAITING_SYNC_FIRST_CHANGE) {
+                updateSyncState(enabled);
+
+                if (!enabled && !cw.mImsRegistered) {
+                    // IMS is not registered, change the local setting
+                    logi("onGetCallWaitingDone CW in CS network is disabled.");
+                    updateState(TERMINAL_BASED_NOT_ACTIVATED);
+                }
+
+                // return the user setting saved
+                sendGetCallWaitingResponse(cw.mOnComplete);
+                return;
+            }
+            updateState(enabled ? TERMINAL_BASED_ACTIVATED : TERMINAL_BASED_NOT_ACTIVATED);
+        } else if (mSyncPreference == CALL_WAITING_SYNC_FIRST_CHANGE) {
+            // Got an exception
+            if (cw.mImsRegistered) {
+                // queryCallWaiting failed. However, IMS is registered. Do not notify error.
+                // return the user setting saved
+                logi("onGetCallWaitingDone get an exception, but IMS is registered");
+                sendGetCallWaitingResponse(cw.mOnComplete);
+                return;
+            }
+        }
+        sendToTarget(cw.mOnComplete, ar.result, ar.exception);
+    }
+
+    private void sendToTarget(Message onComplete, Object result, Throwable exception) {
+        if (onComplete != null) {
+            AsyncResult.forMessage(onComplete, result, exception);
+            onComplete.sendToTarget();
+        }
+    }
+
+    private void sendGetCallWaitingResponse(Message onComplete) {
+        if (onComplete != null) {
+            int serviceClass = SERVICE_CLASS_NONE;
+            if (mCallWaitingState == TERMINAL_BASED_ACTIVATED) {
+                serviceClass = SERVICE_CLASS_VOICE;
+            }
+            sendToTarget(onComplete, new int[] { mCallWaitingState, serviceClass }, null);
+        }
+    }
+
+    private synchronized void onRegisteredToNetwork() {
+        if (mCsEnabled) return;
+
+        if (DBG) logd("onRegisteredToNetwork");
+
+        mPhone.mCi.queryCallWaiting(SERVICE_CLASS_NONE,
+                obtainMessage(EVENT_GET_CALL_WAITING_DONE));
+    }
+
+    private synchronized void onCarrierConfigChanged() {
+        int subId = mPhone.getSubId();
+        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
+            logi("onCarrierConfigChanged invalid subId=" + subId);
+
+            mValidSubscription = false;
+            unregisterForNetworkAttached();
+            return;
+        }
+
+        CarrierConfigManager configManager = mContext.getSystemService(CarrierConfigManager.class);
+        PersistableBundle b = configManager.getConfigForSubId(subId);
+
+        updateCarrierConfig(subId, b, false);
+
+        logi("onCarrierConfigChanged cs_enabled=" + mCsEnabled);
+
+        if (mSyncPreference == CALL_WAITING_SYNC_FIRST_POWER_UP) {
+            if (!mCsEnabled) {
+                registerForNetworkAttached();
+            }
+        }
+    }
+
+    /**
+     * @param ignoreSavedState only used for test
+     */
+    @VisibleForTesting
+    public void updateCarrierConfig(int subId, PersistableBundle b, boolean ignoreSavedState) {
+        mValidSubscription = true;
+
+        if (b == null) return;
+
+        boolean supportsTerminalBased = false;
+        int[] services = b.getIntArray(KEY_UT_TERMINAL_BASED_SERVICES_INT_ARRAY);
+        if (services != null) {
+            for (int service : services) {
+                if (service == SUPPLEMENTARY_SERVICE_CW) {
+                    supportsTerminalBased = true;
+                }
+            }
+        }
+        int syncPreference = b.getInt(KEY_TERMINAL_BASED_CALL_WAITING_SYNC_TYPE_INT,
+                CALL_WAITING_SYNC_FIRST_CHANGE);
+        boolean activated = b.getBoolean(KEY_TERMINAL_BASED_CALL_WAITING_DEFAULT_ENABLED_BOOL);
+        int defaultState = supportsTerminalBased
+                ? (activated ? TERMINAL_BASED_ACTIVATED : TERMINAL_BASED_NOT_ACTIVATED)
+                : TERMINAL_BASED_NOT_SUPPORTED;
+        int savedState = getSavedState(subId);
+
+        if (DBG) {
+            logd("updateCarrierConfig phoneId=" + mPhone.getPhoneId()
+                    + ", subId=" + subId + ", support=" + supportsTerminalBased
+                    + ", sync=" + syncPreference + ", default=" + defaultState
+                    + ", savedState=" + savedState);
+        }
+
+        int desiredState = savedState;
+
+        if (ignoreSavedState) {
+            desiredState = defaultState;
+        } else if ((mLastSubId != subId)
+                && (syncPreference == CALL_WAITING_SYNC_FIRST_POWER_UP
+                        || syncPreference == CALL_WAITING_SYNC_FIRST_CHANGE)) {
+            desiredState = defaultState;
+        } else {
+            if (defaultState == TERMINAL_BASED_NOT_SUPPORTED) {
+                desiredState = TERMINAL_BASED_NOT_SUPPORTED;
+            } else if (savedState == TERMINAL_BASED_NOT_SUPPORTED) {
+                desiredState = defaultState;
+            }
+        }
+
+        updateState(desiredState, syncPreference, ignoreSavedState);
+    }
+
+    private void updateState(int state) {
+        updateState(state, mSyncPreference, false);
+    }
+
+    private void updateState(int state, int syncPreference, boolean ignoreSavedState) {
+        int subId = mPhone.getSubId();
+
+        if (mLastSubId == subId
+                && mCallWaitingState == state
+                && mSyncPreference == syncPreference
+                && (!ignoreSavedState)) {
+            return;
+        }
+
+        int phoneId = mPhone.getPhoneId();
+
+        logi("updateState phoneId=" + phoneId
+                + ", subId=" + subId + ", state=" + state
+                + ", sync=" + syncPreference + ", ignoreSavedState=" + ignoreSavedState);
+
+        SharedPreferences sp =
+                mContext.getSharedPreferences(PREFERENCE_TBCW, Context.MODE_PRIVATE);
+
+        SharedPreferences.Editor editor = sp.edit();
+        editor.putInt(KEY_SUB_ID + phoneId, subId);
+        editor.putInt(KEY_STATE + subId, state);
+        editor.putInt(KEY_CS_SYNC + phoneId, syncPreference);
+        editor.apply();
+
+        mCallWaitingState = state;
+        mLastSubId = subId;
+        mSyncPreference = syncPreference;
+        if (mLastSubId != subId) {
+            mCsEnabled = false;
+        }
+
+        mPhone.setTerminalBasedCallWaitingStatus(mCallWaitingState);
+    }
+
+    private int getSavedState(int subId) {
+        SharedPreferences sp =
+                mContext.getSharedPreferences(PREFERENCE_TBCW, Context.MODE_PRIVATE);
+        int state = sp.getInt(KEY_STATE + subId, TERMINAL_BASED_NOT_SUPPORTED);
+
+        logi("getSavedState subId=" + subId + ", state=" + state);
+
+        return state;
+    }
+
+    private void updateSyncState(boolean enabled) {
+        int phoneId = mPhone.getPhoneId();
+
+        logi("updateSyncState phoneId=" + phoneId + ", enabled=" + enabled);
+
+        mCsEnabled = enabled;
+    }
+
+    /**
+     * @return whether the service is enabled in the CS network
+     */
+    @VisibleForTesting
+    public boolean getSyncState() {
+        return mCsEnabled;
+    }
+
+    private boolean isCircuitSwitchedNetworkAvailable() {
+        logi("isCircuitSwitchedNetworkAvailable="
+                + (mSST.getServiceState().getState() == ServiceState.STATE_IN_SERVICE));
+        return mSST.getServiceState().getState() == ServiceState.STATE_IN_SERVICE;
+    }
+
+    private boolean isImsRegistered() {
+        logi("isImsRegistered " + mImsRegistered);
+        return mImsRegistered;
+    }
+
+    /**
+     * Sets the registration state of IMS service.
+     */
+    public synchronized void setImsRegistrationState(boolean registered) {
+        logi("setImsRegistrationState prev=" + mImsRegistered
+                + ", new=" + registered);
+        mImsRegistered = registered;
+    }
+
+    private void registerForNetworkAttached() {
+        logi("registerForNetworkAttached");
+        if (mRegisteredForNetworkAttach) return;
+
+        mSST.registerForNetworkAttached(this, EVENT_REGISTERED_TO_NETWORK, null);
+        mRegisteredForNetworkAttach = true;
+    }
+
+    private void unregisterForNetworkAttached() {
+        logi("unregisterForNetworkAttached");
+        if (!mRegisteredForNetworkAttach) return;
+
+        mSST.unregisterForNetworkAttached(this);
+        removeMessages(EVENT_REGISTERED_TO_NETWORK);
+        mRegisteredForNetworkAttach = false;
+    }
+
+    /**
+     * Sets whether the device supports the terminal-based call waiting.
+     * Only for test
+     */
+    @VisibleForTesting
+    public synchronized void setTerminalBasedCallWaitingSupported(boolean supported) {
+        if (mSupportedByImsService == supported) return;
+
+        logi("setTerminalBasedCallWaitingSupported " + supported);
+
+        mSupportedByImsService = supported;
+
+        if (supported) {
+            initialize();
+            onCarrierConfigChanged();
+        } else {
+            mContext.unregisterReceiver(mReceiver);
+            updateState(TERMINAL_BASED_NOT_SUPPORTED);
+        }
+    }
+
+    /**
+     * Notifies that the UE has attached to the network
+     * Only for test
+     */
+    @VisibleForTesting
+    public void notifyRegisteredToNetwork() {
+        sendEmptyMessage(EVENT_REGISTERED_TO_NETWORK);
+    }
+
+    private boolean isSyncImsOnly() {
+        return (mSyncPreference == CALL_WAITING_SYNC_IMS_ONLY && mImsRegistered);
+    }
+
+    /**
+     * Dump this instance into a readable format for dumpsys usage.
+     */
+    public void dump(PrintWriter printWriter) {
+        IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, "  ");
+        pw.increaseIndent();
+        pw.println("CallWaitingController:");
+        pw.println(" mSupportedByImsService=" + mSupportedByImsService);
+        pw.println(" mValidSubscription=" + mValidSubscription);
+        pw.println(" mCallWaitingState=" + mCallWaitingState);
+        pw.println(" mSyncPreference=" + mSyncPreference);
+        pw.println(" mLastSubId=" + mLastSubId);
+        pw.println(" mCsEnabled=" + mCsEnabled);
+        pw.println(" mRegisteredForNetworkAttach=" + mRegisteredForNetworkAttach);
+        pw.println(" mImsRegistered=" + mImsRegistered);
+        pw.decreaseIndent();
+    }
+
+    private void loge(String msg) {
+        Rlog.e(LOG_TAG, "[" + mPhone.getPhoneId() + "] " + msg);
+    }
+
+    private void logi(String msg) {
+        Rlog.i(LOG_TAG, "[" + mPhone.getPhoneId() + "] " + msg);
+    }
+
+    private void logd(String msg) {
+        Rlog.d(LOG_TAG, "[" + mPhone.getPhoneId() + "] " + msg);
+    }
+}
diff --git a/src/java/com/android/internal/telephony/CarrierPrivilegesTracker.java b/src/java/com/android/internal/telephony/CarrierPrivilegesTracker.java
index 3144229..b9c45c4 100644
--- a/src/java/com/android/internal/telephony/CarrierPrivilegesTracker.java
+++ b/src/java/com/android/internal/telephony/CarrierPrivilegesTracker.java
@@ -16,11 +16,8 @@
 
 package com.android.internal.telephony;
 
-import static android.telephony.CarrierConfigManager.EXTRA_SLOT_INDEX;
-import static android.telephony.CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX;
 import static android.telephony.CarrierConfigManager.KEY_CARRIER_CERTIFICATE_STRING_ARRAY;
 import static android.telephony.SubscriptionManager.INVALID_SIM_SLOT_INDEX;
-import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
 import static android.telephony.TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
 import static android.telephony.TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
 import static android.telephony.TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
@@ -31,8 +28,6 @@
 import static android.telephony.TelephonyManager.SIM_STATE_READY;
 import static android.telephony.TelephonyManager.SIM_STATE_UNKNOWN;
 
-import static com.android.internal.telephony.SubscriptionInfoUpdater.simStateString;
-
 import android.annotation.ElapsedRealtimeLong;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -86,6 +81,7 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
@@ -129,6 +125,13 @@
                     | PackageManager.MATCH_HIDDEN_UNTIL_INSTALLED_COMPONENTS;
 
     /**
+     * All carrier config keys used in this class should list here in alphabetical order.
+     */
+    private static final String[] CARRIER_CONFIG_KEYS = {
+            KEY_CARRIER_CERTIFICATE_STRING_ARRAY,
+    };
+
+    /**
      * Action to register a Registrant with this Tracker.
      * obj: Registrant that will be notified of Carrier Privileged UID changes.
      */
@@ -141,13 +144,6 @@
     private static final int ACTION_UNREGISTER_LISTENER = 2;
 
     /**
-     * Action for tracking when Carrier Configs are updated.
-     * arg1: Subscription Id for the Carrier Configs update being broadcast
-     * arg2: Slot Index for the Carrier Configs update being broadcast
-     */
-    private static final int ACTION_CARRIER_CONFIG_CERTS_UPDATED = 3;
-
-    /**
      * Action for tracking when the Phone's SIM state changes.
      * arg1: slotId that this Action applies to
      * arg2: simState reported by this Broadcast
@@ -296,19 +292,6 @@
                     if (action == null) return;
 
                     switch (action) {
-                        case CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED: {
-                            Bundle extras = intent.getExtras();
-                            int slotIndex = extras.getInt(EXTRA_SLOT_INDEX);
-                            int subId =
-                                    extras.getInt(
-                                            EXTRA_SUBSCRIPTION_INDEX, INVALID_SUBSCRIPTION_ID);
-                            sendMessage(
-                                    obtainMessage(
-                                            ACTION_CARRIER_CONFIG_CERTS_UPDATED,
-                                            subId,
-                                            slotIndex));
-                            break;
-                        }
                         case TelephonyManager.ACTION_SIM_CARD_STATE_CHANGED: // fall through
                         case TelephonyManager.ACTION_SIM_APPLICATION_STATE_CHANGED: {
                             Bundle extras = intent.getExtras();
@@ -371,13 +354,16 @@
         mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
         mCarrierConfigManager =
                 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        // Callback is executed in handler thread and directly handles carrier config update
+        mCarrierConfigManager.registerCarrierConfigChangeListener(this::post,
+                (slotIndex, subId, carrierId, specificCarrierId) -> handleCarrierConfigUpdated(
+                        subId, slotIndex));
         mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
         mTelephonyRegistryManager =
                 (TelephonyRegistryManager)
                         mContext.getSystemService(Context.TELEPHONY_REGISTRY_SERVICE);
 
         IntentFilter certFilter = new IntentFilter();
-        certFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
         certFilter.addAction(TelephonyManager.ACTION_SIM_CARD_STATE_CHANGED);
         certFilter.addAction(TelephonyManager.ACTION_SIM_APPLICATION_STATE_CHANGED);
         mContext.registerReceiver(mIntentReceiver, certFilter);
@@ -407,12 +393,6 @@
                 handleUnregisterListener((Handler) msg.obj);
                 break;
             }
-            case ACTION_CARRIER_CONFIG_CERTS_UPDATED: {
-                int subId = msg.arg1;
-                int slotIndex = msg.arg2;
-                handleCarrierConfigUpdated(subId, slotIndex);
-                break;
-            }
             case ACTION_SIM_STATE_UPDATED: {
                 handleSimStateChanged(msg.arg1, msg.arg2);
                 break;
@@ -489,7 +469,14 @@
 
     @NonNull
     private List<UiccAccessRule> getCarrierConfigRules(int subId) {
-        PersistableBundle carrierConfigs = mCarrierConfigManager.getConfigForSubId(subId);
+        PersistableBundle carrierConfigs = null;
+        try {
+            carrierConfigs = mCarrierConfigManager.getConfigForSubId(subId, CARRIER_CONFIG_KEYS);
+        } catch (RuntimeException e) {
+            mLocalLog.log("CarrierConfigLoader is not available, try it later.");
+        }
+
+        // CarrierConfigManager#isConfigForIdentifiedCarrier can handle null or empty bundle
         if (!mCarrierConfigManager.isConfigForIdentifiedCarrier(carrierConfigs)) {
             return Collections.EMPTY_LIST;
         }
@@ -525,7 +512,7 @@
                         SystemClock.uptimeMillis() + CLEAR_UICC_RULES_DELAY_MILLIS;
                 sendMessageAtTime(obtainMessage(ACTION_CLEAR_UICC_RULES),
                         mClearUiccRulesUptimeMillis);
-                mLocalLog.log("SIM is gone, simState=" + simStateString(simState)
+                mLocalLog.log("SIM is gone, simState=" + TelephonyManager.simStateToString(simState)
                         + ". Delay " + TimeUnit.MILLISECONDS.toSeconds(
                         CLEAR_UICC_RULES_DELAY_MILLIS) + " seconds to clear UICC rules.");
             } else {
@@ -610,10 +597,10 @@
         List<Signature> signatures = UiccAccessRule.getSignatures(pkg);
         for (Signature signature : signatures) {
             byte[] sha1 = UiccAccessRule.getCertHash(signature, SHA_1);
-            certs.add(IccUtils.bytesToHexString(sha1).toUpperCase());
+            certs.add(IccUtils.bytesToHexString(sha1).toUpperCase(Locale.ROOT));
 
             byte[] sha256 = UiccAccessRule.getCertHash(signature, SHA_256);
-            certs.add(IccUtils.bytesToHexString(sha256).toUpperCase());
+            certs.add(IccUtils.bytesToHexString(sha256).toUpperCase(Locale.ROOT));
         }
 
         mInstalledPackageCerts.put(pkg.packageName, certs);
diff --git a/src/java/com/android/internal/telephony/CarrierResolver.java b/src/java/com/android/internal/telephony/CarrierResolver.java
index ec6a5a0..4036cd9 100644
--- a/src/java/com/android/internal/telephony/CarrierResolver.java
+++ b/src/java/com/android/internal/telephony/CarrierResolver.java
@@ -43,6 +43,7 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.metrics.CarrierIdMatchStats;
 import com.android.internal.telephony.metrics.TelephonyMetrics;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.IccRecords;
 import com.android.internal.telephony.uicc.UiccController;
 import com.android.internal.telephony.util.TelephonyUtils;
@@ -54,6 +55,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Locale;
 
 /**
  * CarrierResolver identifies the subscription carrier and returns a canonical carrier Id
@@ -193,7 +195,7 @@
     /**
      * This is triggered from SubscriptionInfoUpdater after sim state change.
      * The sequence of sim loading would be
-     *  1. ACTION_SUBINFO_CONTENT_CHANGE
+     *  1. OnSubscriptionsChangedListener
      *  2. ACTION_SIM_STATE_CHANGED/ACTION_SIM_CARD_STATE_CHANGED
      *  /ACTION_SIM_APPLICATION_STATE_CHANGED
      *  3. ACTION_SUBSCRIPTION_CARRIER_IDENTITY_CHANGED
@@ -547,7 +549,12 @@
         // subscriptioninfo db to make sure we have correct carrier id set.
         if (SubscriptionManager.isValidSubscriptionId(mPhone.getSubId()) && !isSimOverride) {
             // only persist carrier id to simInfo db when subId is valid.
-            SubscriptionController.getInstance().setCarrierId(mCarrierId, mPhone.getSubId());
+            if (mPhone.isSubscriptionManagerServiceEnabled()) {
+                SubscriptionManagerService.getInstance().setCarrierId(mPhone.getSubId(),
+                        mCarrierId);
+            } else {
+                SubscriptionController.getInstance().setCarrierId(mCarrierId, mPhone.getSubId());
+            }
         }
     }
 
@@ -751,7 +758,8 @@
         // Ideally we should do full string match. However due to SIM manufacture issues
         // gid from some SIM might has garbage tail.
         private boolean gidMatch(String gidFromSim, String gid) {
-            return (gidFromSim != null) && gidFromSim.toLowerCase().startsWith(gid.toLowerCase());
+            return (gidFromSim != null) && gidFromSim.toLowerCase(Locale.ROOT)
+                    .startsWith(gid.toLowerCase(Locale.ROOT));
         }
 
         private boolean carrierPrivilegeRulesMatch(List<String> certsFromSubscription,
diff --git a/src/java/com/android/internal/telephony/CarrierServiceBindHelper.java b/src/java/com/android/internal/telephony/CarrierServiceBindHelper.java
index dfa53b3..960d794 100644
--- a/src/java/com/android/internal/telephony/CarrierServiceBindHelper.java
+++ b/src/java/com/android/internal/telephony/CarrierServiceBindHelper.java
@@ -16,6 +16,7 @@
 
 package com.android.internal.telephony;
 
+import android.annotation.NonNull;
 import android.compat.annotation.UnsupportedAppUsage;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
@@ -200,7 +201,13 @@
         }
     }
 
-    void updateForPhoneId(int phoneId, String simState) {
+    /**
+     * Update SIM state.
+     *
+     * @param phoneId The phone id.
+     * @param simState The legacy SIM state.
+     */
+    public void updateForPhoneId(int phoneId, @NonNull String simState) {
         logdWithLocalLog("update binding for phoneId: " + phoneId + " simState: " + simState);
         if (!SubscriptionManager.isValidPhoneId(phoneId)) {
             return;
diff --git a/src/java/com/android/internal/telephony/CarrierServiceStateTracker.java b/src/java/com/android/internal/telephony/CarrierServiceStateTracker.java
index 64dc7ec..9fa26cc 100644
--- a/src/java/com/android/internal/telephony/CarrierServiceStateTracker.java
+++ b/src/java/com/android/internal/telephony/CarrierServiceStateTracker.java
@@ -376,6 +376,7 @@
         Notification.Builder builder = getNotificationBuilder(notificationType);
         // set some common attributes
         builder.setWhen(System.currentTimeMillis())
+                .setShowWhen(true)
                 .setAutoCancel(true)
                 .setSmallIcon(com.android.internal.R.drawable.stat_sys_warning)
                 .setColor(context.getResources().getColor(
diff --git a/src/java/com/android/internal/telephony/CellBroadcastConfigTracker.java b/src/java/com/android/internal/telephony/CellBroadcastConfigTracker.java
new file mode 100644
index 0000000..32cfdfe
--- /dev/null
+++ b/src/java/com/android/internal/telephony/CellBroadcastConfigTracker.java
@@ -0,0 +1,462 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import android.annotation.NonNull;
+import android.os.AsyncResult;
+import android.os.Build;
+import android.os.Handler;
+import android.os.Message;
+import android.telephony.CellBroadcastIdRange;
+import android.telephony.SmsCbMessage;
+import android.telephony.TelephonyManager;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.cdma.CdmaSmsBroadcastConfigInfo;
+import com.android.internal.telephony.gsm.SmsBroadcastConfigInfo;
+import com.android.internal.util.State;
+import com.android.internal.util.StateMachine;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.function.Consumer;
+
+/**
+ * This class is to track the state to set cell broadcast config
+ */
+
+public final class CellBroadcastConfigTracker extends StateMachine {
+    private static final boolean DBG = Build.IS_DEBUGGABLE;
+
+    private static final int EVENT_REQUEST = 1;
+    private static final int EVENT_CONFIGURATION_DONE = 2;
+    private static final int EVENT_ACTIVATION_DONE = 3;
+
+    private static final int SMS_CB_CODE_SCHEME_MIN = 0;
+    private static final int SMS_CB_CODE_SCHEME_MAX = 255;
+
+    // Cache of current cell broadcast id ranges of 3gpp
+    private List<CellBroadcastIdRange> mCbRanges3gpp = new CopyOnWriteArrayList<>();
+    // Cache of current cell broadcast id ranges of 3gpp2
+    private List<CellBroadcastIdRange> mCbRanges3gpp2 = new CopyOnWriteArrayList<>();
+    private Phone mPhone;
+
+
+    /**
+     * The class is to present the request to set cell broadcast id ranges
+     */
+    private static class Request {
+        private final List<CellBroadcastIdRange> mCbRangesRequest3gpp =
+                new CopyOnWriteArrayList<>();
+        private final List<CellBroadcastIdRange> mCbRangesRequest3gpp2 =
+                new CopyOnWriteArrayList<>();
+        Consumer<Integer> mCallback;
+
+        Request(@NonNull List<CellBroadcastIdRange> ranges, @NonNull Consumer<Integer> callback) {
+            ranges.forEach(r -> {
+                if (r.getType() == SmsCbMessage.MESSAGE_FORMAT_3GPP) {
+                    mCbRangesRequest3gpp.add(r);
+                } else {
+                    mCbRangesRequest3gpp2.add(r);
+                }
+            });
+            mCallback = callback;
+        }
+
+        List<CellBroadcastIdRange> get3gppRanges() {
+            return mCbRangesRequest3gpp;
+        }
+
+        List<CellBroadcastIdRange> get3gpp2Ranges() {
+            return mCbRangesRequest3gpp2;
+        }
+
+        Consumer<Integer> getCallback() {
+            return mCallback;
+        }
+
+        @Override
+        public String toString() {
+            return "Request[mCbRangesRequest3gpp = " + mCbRangesRequest3gpp + ", "
+                    + "mCbRangesRequest3gpp2 = " + mCbRangesRequest3gpp2 + ", "
+                    + "mCallback = " + mCallback + "]";
+        }
+    }
+
+    /*
+     * The idle state which does not have ongoing radio request.
+     */
+    private class IdleState extends State {
+        @Override
+        public boolean processMessage(Message msg) {
+            boolean retVal = NOT_HANDLED;
+            if (DBG) {
+                logd("IdleState message:" + msg.what);
+            }
+            switch (msg.what) {
+                case EVENT_REQUEST:
+                    Request request = (Request) msg.obj;
+                    if (DBG) {
+                        logd("IdleState handle EVENT_REQUEST with request:" + request);
+                    }
+                    if (!mCbRanges3gpp.equals(request.get3gppRanges())) {
+                        // set gsm config if the config is changed
+                        setGsmConfig(request.get3gppRanges(), request);
+                        transitionTo(mGsmConfiguringState);
+                    } else if (!mCbRanges3gpp2.equals(request.get3gpp2Ranges())) {
+                        // set cdma config directly if no gsm config change but cdma config is
+                        // changed
+                        setCdmaConfig(request.get3gpp2Ranges(), request);
+                        transitionTo(mCdmaConfiguringState);
+                    } else {
+                        logd("Do nothing as the requested ranges are same as now");
+                        request.getCallback().accept(
+                                TelephonyManager.CELL_BROADCAST_RESULT_SUCCESS);
+                    }
+                    retVal = HANDLED;
+                    break;
+                default:
+                    break;
+            }
+            return retVal;
+        }
+    }
+    private IdleState mIdleState = new IdleState();
+
+    /*
+     * The state waiting for the result to set gsm config.
+     */
+    private class GsmConfiguringState extends State {
+        @Override
+        public boolean processMessage(Message msg) {
+            boolean retVal = NOT_HANDLED;
+            if (DBG) {
+                logd("GsmConfiguringState message:" + msg.what);
+            }
+            switch (msg.what) {
+                case EVENT_REQUEST:
+                    deferMessage(msg);
+                    retVal = HANDLED;
+                    break;
+                case EVENT_CONFIGURATION_DONE:
+                    AsyncResult ar = (AsyncResult) msg.obj;
+                    Request request = (Request) ar.userObj;
+                    if (DBG) {
+                        logd("GsmConfiguringState handle EVENT_CONFIGURATION_DONE with request:"
+                                + request);
+                    }
+                    if (ar.exception == null) {
+                        // set gsm activation and transit to gsm activating state
+                        setActivation(SmsCbMessage.MESSAGE_FORMAT_3GPP,
+                                !request.get3gppRanges().isEmpty(), request);
+                        transitionTo(mGsmActivatingState);
+                    } else {
+                        logd("Failed to set gsm config");
+                        request.getCallback().accept(
+                                TelephonyManager.CELL_BROADCAST_RESULT_FAIL_CONFIG);
+                        // transit to idle state on the failure case
+                        transitionTo(mIdleState);
+                    }
+                    retVal = HANDLED;
+                    break;
+                default:
+                    break;
+            }
+            return retVal;
+        }
+    }
+    private GsmConfiguringState mGsmConfiguringState = new GsmConfiguringState();
+
+    /*
+     * The state waiting for the result to set gsm activation.
+     */
+    private class GsmActivatingState extends State {
+        @Override
+        public boolean processMessage(Message msg) {
+            boolean retVal = NOT_HANDLED;
+            if (DBG) {
+                logd("GsmActivatingState message:" + msg.what);
+            }
+            switch (msg.what) {
+                case EVENT_REQUEST:
+                    deferMessage(msg);
+                    retVal = HANDLED;
+                    break;
+                case EVENT_ACTIVATION_DONE:
+                    AsyncResult ar = (AsyncResult) msg.obj;
+                    Request request = (Request) ar.userObj;
+                    if (DBG) {
+                        logd("GsmActivatingState handle EVENT_ACTIVATION_DONE with request:"
+                                + request);
+                    }
+                    if (ar.exception == null) {
+                        mCbRanges3gpp = request.get3gppRanges();
+                        if (!mCbRanges3gpp2.equals(request.get3gpp2Ranges())) {
+                            // set cdma config and transit to cdma configuring state if the config
+                            // is changed.
+                            setCdmaConfig(request.get3gpp2Ranges(), request);
+                            transitionTo(mCdmaConfiguringState);
+                        } else {
+                            logd("Done as no need to update ranges for 3gpp2");
+                            request.getCallback().accept(
+                                    TelephonyManager.CELL_BROADCAST_RESULT_SUCCESS);
+                            // transit to idle state if there is no cdma config change
+                            transitionTo(mIdleState);
+                        }
+                    } else {
+                        logd("Failed to set gsm activation");
+                        request.getCallback().accept(
+                                TelephonyManager.CELL_BROADCAST_RESULT_FAIL_ACTIVATION);
+                        // transit to idle state on the failure case
+                        transitionTo(mIdleState);
+                    }
+                    retVal = HANDLED;
+                    break;
+                default:
+                    break;
+            }
+            return retVal;
+        }
+    }
+    private GsmActivatingState mGsmActivatingState = new GsmActivatingState();
+
+    /*
+     * The state waiting for the result to set cdma config.
+     */
+    private class CdmaConfiguringState extends State {
+        @Override
+        public boolean processMessage(Message msg) {
+            boolean retVal = NOT_HANDLED;
+            if (DBG) {
+                logd("CdmaConfiguringState message:" + msg.what);
+            }
+            switch (msg.what) {
+                case EVENT_REQUEST:
+                    deferMessage(msg);
+                    retVal = HANDLED;
+                    break;
+                case EVENT_CONFIGURATION_DONE:
+                    AsyncResult ar = (AsyncResult) msg.obj;
+                    Request request = (Request) ar.userObj;
+                    if (DBG) {
+                        logd("CdmaConfiguringState handle EVENT_ACTIVATION_DONE with request:"
+                                + request);
+                    }
+                    if (ar.exception == null) {
+                        // set cdma activation and transit to cdma activating state
+                        setActivation(SmsCbMessage.MESSAGE_FORMAT_3GPP2,
+                                !request.get3gpp2Ranges().isEmpty(), request);
+                        transitionTo(mCdmaActivatingState);
+                    } else {
+                        logd("Failed to set cdma config");
+                        request.getCallback().accept(
+                                TelephonyManager.CELL_BROADCAST_RESULT_FAIL_CONFIG);
+                        // transit to idle state on the failure case
+                        transitionTo(mIdleState);
+                    }
+                    retVal = HANDLED;
+                    break;
+                default:
+                    break;
+            }
+            return retVal;
+        }
+    }
+    private CdmaConfiguringState mCdmaConfiguringState = new CdmaConfiguringState();
+
+    /*
+     * The state waiting for the result to set cdma activation.
+     */
+    private class CdmaActivatingState extends State {
+        @Override
+        public boolean processMessage(Message msg) {
+            boolean retVal = NOT_HANDLED;
+            if (DBG) {
+                logd("CdmaActivatingState message:" + msg.what);
+            }
+            switch (msg.what) {
+                case EVENT_REQUEST:
+                    deferMessage(msg);
+                    retVal = HANDLED;
+                    break;
+                case EVENT_ACTIVATION_DONE:
+                    AsyncResult ar = (AsyncResult) msg.obj;
+                    Request request = (Request) ar.userObj;
+                    if (DBG) {
+                        logd("CdmaActivatingState handle EVENT_ACTIVATION_DONE with request:"
+                                + request);
+                    }
+                    if (ar.exception == null) {
+                        mCbRanges3gpp2 = request.get3gpp2Ranges();
+                        request.getCallback().accept(
+                                    TelephonyManager.CELL_BROADCAST_RESULT_SUCCESS);
+                    } else {
+                        logd("Failed to set cdma activation");
+                        request.getCallback().accept(
+                                TelephonyManager.CELL_BROADCAST_RESULT_FAIL_ACTIVATION);
+                    }
+                    // transit to idle state anyway
+                    transitionTo(mIdleState);
+                    retVal = HANDLED;
+                    break;
+                default:
+                    break;
+            }
+            return retVal;
+        }
+    }
+    private CdmaActivatingState mCdmaActivatingState = new CdmaActivatingState();
+
+    private CellBroadcastConfigTracker(Phone phone) {
+        super("CellBroadcastConfigTracker-" + phone.getPhoneId());
+        init(phone);
+    }
+
+    private CellBroadcastConfigTracker(Phone phone, Handler handler) {
+        super("CellBroadcastConfigTracker-" + phone.getPhoneId(), handler);
+        init(phone);
+    }
+
+    private void init(Phone phone) {
+        logd("init");
+        mPhone = phone;
+
+        addState(mIdleState);
+        addState(mGsmConfiguringState);
+        addState(mGsmActivatingState);
+        addState(mCdmaConfiguringState);
+        addState(mCdmaActivatingState);
+        setInitialState(mIdleState);
+    }
+
+    /**
+     * create a CellBroadcastConfigTracker instance for the phone
+     */
+    public static CellBroadcastConfigTracker make(Phone phone, Handler handler) {
+        CellBroadcastConfigTracker tracker = handler == null
+                ? new CellBroadcastConfigTracker(phone)
+                : new CellBroadcastConfigTracker(phone, handler);
+        tracker.start();
+        return tracker;
+    }
+
+    /**
+     * Return current cell broadcast ranges.
+     */
+    @NonNull public List<CellBroadcastIdRange> getCellBroadcastIdRanges() {
+        List<CellBroadcastIdRange> ranges = new ArrayList<>();
+        ranges.addAll(mCbRanges3gpp);
+        ranges.addAll(mCbRanges3gpp2);
+        return ranges;
+    }
+
+    /**
+     * Set reception of cell broadcast messages with the list of the given ranges.
+     */
+    public void setCellBroadcastIdRanges(
+            @NonNull List<CellBroadcastIdRange> ranges, @NonNull Consumer<Integer> callback) {
+        if (DBG) {
+            logd("setCellBroadcastIdRanges with ranges:" + ranges);
+        }
+        ranges = mergeRangesAsNeeded(ranges);
+        sendMessage(EVENT_REQUEST, new Request(ranges, callback));
+    }
+
+    /**
+     * Merge the overlapped CellBroadcastIdRanges in the list as needed
+     * @param ranges the list of CellBroadcastIdRanges
+     * @return the list of CellBroadcastIdRanges without overlapping
+     *
+     * @throws IllegalArgumentException if there is conflict of the ranges. For instance,
+     * the channel is enabled in some range, but disable in others.
+     */
+    @VisibleForTesting
+    public static @NonNull List<CellBroadcastIdRange> mergeRangesAsNeeded(
+            @NonNull List<CellBroadcastIdRange> ranges) throws IllegalArgumentException {
+        ranges.sort((r1, r2) -> r1.getType() != r2.getType() ? r1.getType() - r2.getType()
+                : (r1.getStartId() != r2.getStartId() ? r1.getStartId() - r2.getStartId()
+                : r2.getEndId() - r1.getEndId()));
+        final List<CellBroadcastIdRange> newRanges = new ArrayList<>();
+        ranges.forEach(r -> {
+            if (newRanges.isEmpty() || newRanges.get(newRanges.size() - 1).getType() != r.getType()
+                    || newRanges.get(newRanges.size() - 1).getEndId() + 1 < r.getStartId()
+                    || (newRanges.get(newRanges.size() - 1).getEndId() + 1 == r.getStartId()
+                    && newRanges.get(newRanges.size() - 1).isEnabled() != r.isEnabled())) {
+                newRanges.add(new CellBroadcastIdRange(r.getStartId(), r.getEndId(),
+                        r.getType(), r.isEnabled()));
+            } else {
+                if (newRanges.get(newRanges.size() - 1).isEnabled() != r.isEnabled()) {
+                    throw new IllegalArgumentException("range conflict " + r);
+                }
+                if (r.getEndId() > newRanges.get(newRanges.size() - 1).getEndId()) {
+                    CellBroadcastIdRange range = newRanges.get(newRanges.size() - 1);
+                    newRanges.set(newRanges.size() - 1, new CellBroadcastIdRange(
+                            range.getStartId(), r.getEndId(), range.getType(), range.isEnabled()));
+                }
+            }
+        });
+        return newRanges;
+    }
+
+    private void setGsmConfig(List<CellBroadcastIdRange> ranges, Request request) {
+        if (DBG) {
+            logd("setGsmConfig with " + ranges);
+        }
+
+        SmsBroadcastConfigInfo[] configs = new SmsBroadcastConfigInfo[ranges.size()];
+        for (int i = 0; i < configs.length; i++) {
+            CellBroadcastIdRange r = ranges.get(i);
+            configs[i] = new SmsBroadcastConfigInfo(r.getStartId(), r.getEndId(),
+                    SMS_CB_CODE_SCHEME_MIN, SMS_CB_CODE_SCHEME_MAX, r.isEnabled());
+        }
+
+        Message response = obtainMessage(EVENT_CONFIGURATION_DONE, request);
+        mPhone.mCi.setGsmBroadcastConfig(configs, response);
+    }
+
+    private void setCdmaConfig(List<CellBroadcastIdRange> ranges, Request request) {
+        if (DBG) {
+            logd("setCdmaConfig with " + ranges);
+        }
+
+        CdmaSmsBroadcastConfigInfo[] configs =
+                new CdmaSmsBroadcastConfigInfo[ranges.size()];
+        for (int i = 0; i < configs.length; i++) {
+            CellBroadcastIdRange r = ranges.get(i);
+            configs[i] = new CdmaSmsBroadcastConfigInfo(
+                    r.getStartId(), r.getEndId(), 1, r.isEnabled());
+        }
+
+        Message response = obtainMessage(EVENT_CONFIGURATION_DONE, request);
+        mPhone.mCi.setCdmaBroadcastConfig(configs, response);
+    }
+
+    private void setActivation(int type, boolean activate, Request request) {
+        if (DBG) {
+            logd("setActivation(" + type + "." + activate + ')');
+        }
+
+        Message response = obtainMessage(EVENT_ACTIVATION_DONE, request);
+
+        if (type == SmsCbMessage.MESSAGE_FORMAT_3GPP) {
+            mPhone.mCi.setGsmBroadcastActivation(activate, response);
+        } else if (type == SmsCbMessage.MESSAGE_FORMAT_3GPP2) {
+            mPhone.mCi.setCdmaBroadcastActivation(activate, response);
+        }
+    }
+}
diff --git a/src/java/com/android/internal/telephony/CellularNetworkService.java b/src/java/com/android/internal/telephony/CellularNetworkService.java
index 4253905..bd47d3d 100644
--- a/src/java/com/android/internal/telephony/CellularNetworkService.java
+++ b/src/java/com/android/internal/telephony/CellularNetworkService.java
@@ -18,9 +18,9 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.hardware.radio.V1_0.RegState;
 import android.hardware.radio.V1_4.DataRegStateResult.VopsInfo.hidl_discriminator;
 import android.hardware.radio.V1_6.RegStateResult.AccessTechnologySpecificInfo;
+import android.hardware.radio.network.RegState;
 import android.os.AsyncResult;
 import android.os.Handler;
 import android.os.Looper;
@@ -35,6 +35,7 @@
 import android.telephony.CellIdentityNr;
 import android.telephony.CellIdentityTdscdma;
 import android.telephony.CellIdentityWcdma;
+import android.telephony.DataSpecificRegistrationInfo;
 import android.telephony.LteVopsSupportInfo;
 import android.telephony.NetworkRegistrationInfo;
 import android.telephony.NetworkService;
@@ -190,6 +191,8 @@
                     return NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN;
                 case RegState.REG_ROAMING:
                     return NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING;
+                case RegState.REG_EM:
+                    return NetworkRegistrationInfo.REGISTRATION_STATE_EMERGENCY;
                 default:
                     return NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING;
             }
@@ -201,6 +204,7 @@
                 case RegState.NOT_REG_MT_SEARCHING_OP_EM:
                 case RegState.REG_DENIED_EM:
                 case RegState.UNKNOWN_EM:
+                case RegState.REG_EM:
                     return true;
                 case RegState.NOT_REG_MT_NOT_SEARCHING_OP:
                 case RegState.REG_HOME:
@@ -500,14 +504,6 @@
             final String rplmn = regResult.registeredPlmn;
             final int reasonForDenial = regResult.reasonForDenial;
 
-            if (regState == NetworkRegistrationInfo.REGISTRATION_STATE_DENIED
-                    && reasonForDenial
-                    == android.hardware.radio.network.RegistrationFailCause.NONE) {
-                AnomalyReporter.reportAnomaly(
-                        UUID.fromString("62ed270f-e139-418a-a427-8bcc1bca8f20"),
-                            "RIL Missing Reg Fail Reason", mPhone.getCarrierId());
-            }
-
             int networkType = ServiceState.rilRadioTechnologyToNetworkType(regResult.rat);
             if (networkType == TelephonyManager.NETWORK_TYPE_LTE_CA) {
                 networkType = TelephonyManager.NETWORK_TYPE_LTE;
@@ -522,6 +518,8 @@
             boolean isNrAvailable = false;
             boolean isDcNrRestricted = false;
             VopsSupportInfo vopsInfo = null;
+            int lteAttachResultType = 0;
+            int lteAttachExtraInfo = 0;
 
             android.hardware.radio.network.AccessTechnologySpecificInfo info =
                     regResult.accessTechnologySpecificInfo;
@@ -540,6 +538,8 @@
                     vopsInfo = convertHalLteVopsSupportInfo(
                             info.getEutranInfo().lteVopsInfo.isVopsSupported,
                             info.getEutranInfo().lteVopsInfo.isEmcBearerSupported);
+                    lteAttachResultType = info.getEutranInfo().lteAttachResultType;
+                    lteAttachExtraInfo = info.getEutranInfo().extraInfo;
                     break;
                 case android.hardware.radio.network.AccessTechnologySpecificInfo.ngranNrVopsInfo:
                     vopsInfo = new NrVopsSupportInfo(info.getNgranNrVopsInfo().vopsSupported,
@@ -565,10 +565,26 @@
                     loge("Unknown domain passed to CellularNetworkService= " + domain);
                     // fall through
                 case NetworkRegistrationInfo.DOMAIN_PS:
-                    return new NetworkRegistrationInfo(domain, transportType, regState, networkType,
-                            reasonForDenial, isEmergencyOnly, availableServices, cellIdentity,
-                            rplmn, MAX_DATA_CALLS, isDcNrRestricted, isNrAvailable, isEndcAvailable,
-                            vopsInfo);
+                    return new NetworkRegistrationInfo.Builder()
+                        .setDomain(domain)
+                        .setTransportType(transportType)
+                        .setRegistrationState(regState)
+                        .setAccessNetworkTechnology(networkType)
+                        .setRejectCause(reasonForDenial)
+                        .setEmergencyOnly(isEmergencyOnly)
+                        .setAvailableServices(availableServices)
+                        .setCellIdentity(cellIdentity)
+                        .setRegisteredPlmn(rplmn)
+                        .setDataSpecificInfo(
+                                new DataSpecificRegistrationInfo.Builder(MAX_DATA_CALLS)
+                                     .setDcNrRestricted(isDcNrRestricted)
+                                     .setNrAvailable(isNrAvailable)
+                                     .setEnDcAvailable(isEndcAvailable)
+                                     .setVopsSupportInfo(vopsInfo)
+                                     .setLteAttachResultType(lteAttachResultType)
+                                     .setLteAttachExtraInfo(lteAttachExtraInfo)
+                                     .build())
+                        .build();
             }
         }
 
@@ -590,14 +606,6 @@
             networkType =
                     getNetworkTypeForCellIdentity(networkType, cellIdentity, mPhone.getCarrierId());
 
-            if (regState == NetworkRegistrationInfo.REGISTRATION_STATE_DENIED
-                    && reasonForDenial
-                    == android.hardware.radio.network.RegistrationFailCause.NONE) {
-                AnomalyReporter.reportAnomaly(
-                        UUID.fromString("62ed270f-e139-418a-a427-8bcc1bca8f20"),
-                            "RIL Missing Reg Fail Reason", mPhone.getCarrierId());
-            }
-
             // Conditional parameters for specific RANs
             boolean cssSupported = false;
             int roamingIndicator = 0;
diff --git a/src/java/com/android/internal/telephony/CommandsInterface.java b/src/java/com/android/internal/telephony/CommandsInterface.java
index 27cedfe..92f1696 100644
--- a/src/java/com/android/internal/telephony/CommandsInterface.java
+++ b/src/java/com/android/internal/telephony/CommandsInterface.java
@@ -25,22 +25,31 @@
 import android.os.Handler;
 import android.os.Message;
 import android.os.WorkSource;
+import android.telephony.AccessNetworkConstants;
 import android.telephony.AccessNetworkConstants.AccessNetworkType;
+import android.telephony.BarringInfo;
 import android.telephony.CarrierRestrictionRules;
 import android.telephony.ClientRequestStats;
+import android.telephony.DomainSelectionService;
 import android.telephony.ImsiEncryptionInfo;
 import android.telephony.NetworkScanRequest;
 import android.telephony.RadioAccessSpecifier;
 import android.telephony.SignalThresholdInfo;
 import android.telephony.TelephonyManager;
+import android.telephony.TelephonyManager.HalService;
 import android.telephony.data.DataCallResponse;
 import android.telephony.data.DataProfile;
 import android.telephony.data.NetworkSliceInfo;
 import android.telephony.data.TrafficDescriptor;
 import android.telephony.emergency.EmergencyNumber;
+import android.telephony.ims.RegistrationManager;
+import android.telephony.ims.feature.MmTelFeature;
+import android.telephony.ims.stub.ImsRegistrationImplBase;
 
 import com.android.internal.telephony.cdma.CdmaSmsBroadcastConfigInfo;
+import com.android.internal.telephony.emergency.EmergencyConstants;
 import com.android.internal.telephony.gsm.SmsBroadcastConfigInfo;
+import com.android.internal.telephony.imsphone.ImsCallInfo;
 import com.android.internal.telephony.uicc.IccCardApplicationStatus.PersoSubState;
 import com.android.internal.telephony.uicc.IccCardStatus;
 import com.android.internal.telephony.uicc.SimPhonebookRecord;
@@ -124,6 +133,15 @@
     static final int CDMA_SMS_FAIL_CAUSE_OTHER_TERMINAL_PROBLEM     = 39;
     static final int CDMA_SMS_FAIL_CAUSE_ENCODING_PROBLEM           = 96;
 
+    /** IMS voice capability */
+    int IMS_MMTEL_CAPABILITY_VOICE = 1 << 0;
+    /** IMS video capability */
+    int IMS_MMTEL_CAPABILITY_VIDEO = 1 << 1;
+    /** IMS SMS capability */
+    int IMS_MMTEL_CAPABILITY_SMS = 1 << 2;
+    /** IMS RCS capabilities */
+    int IMS_RCS_CAPABILITIES = 1 << 3;
+
     //***** Methods
 
     /**
@@ -1773,6 +1791,17 @@
     public void getDeviceIdentity(Message response);
 
     /**
+     * Request the device IMEI / IMEI type / IMEISV
+     * "response" is ImeiInfo object that contains
+     *  [0] ImeiType Indicates whether IMEI is of primary or secondary type
+     *  [1] IMEI if GSM subscription is available
+     *  [2] IMEISV if GSM subscription is available
+     *
+     * @param response Message
+     */
+    public void getImei(Message response);
+
+    /**
      * Request the device MDN / H_SID / H_NID / MIN.
      * "response" is const char **
      *   [0] is MDN if CDMA subscription is available
@@ -2112,6 +2141,27 @@
             int p1, int p2, int p3, String data, Message response);
 
     /**
+     * Exchange APDUs with the SIM on a logical channel.
+     *
+     * Input parameters equivalent to TS 27.007 AT+CGLA command.
+     *
+     * @param channel Channel id of the channel to use for communication. Has to
+     *            be greater than zero.
+     * @param cla Class of the APDU command.
+     * @param instruction Instruction of the APDU command.
+     * @param p1 P1 value of the APDU command.
+     * @param p2 P2 value of the APDU command.
+     * @param p3 P3 value of the APDU command. If p3 is negative a 4 byte APDU
+     *            is sent to the SIM.
+     * @param data Data to be sent with the APDU.
+     * @param isEs10Command whether APDU command is an ES10 command or a regular APDU
+     * @param response Callback message. response.obj.userObj will be
+     *            an IccIoResult on success.
+     */
+    void iccTransmitApduLogicalChannel(int channel, int cla, int instruction,
+            int p1, int p2, int p3, String data, boolean isEs10Command, Message response);
+
+    /**
      * Exchange APDUs with the SIM on a basic channel.
      *
      * Input parameters equivalent to TS 27.007 AT+CSIM command.
@@ -2186,11 +2236,21 @@
 
     /**
      * @return the radio hal version
+     * @deprecated use {@link #getHalVersion(int)}
      */
+    @Deprecated
     default HalVersion getHalVersion() {
         return HalVersion.UNKNOWN;
     }
 
+    /**
+     * @param service indicate the service id to query.
+     * @return the hal version of a specific service
+     */
+    default HalVersion getHalVersion(@HalService int service) {
+        return HalVersion.UNKNOWN;
+    }
+
    /**
      * Sets user selected subscription at Modem.
      *
@@ -2619,6 +2679,15 @@
     default void getBarringInfo(Message result) {};
 
     /**
+     * Returns the last barring information received.
+     *
+     * @return the last barring information.
+     */
+    default @Nullable BarringInfo getLastBarringInfo() {
+        return null;
+    };
+
+    /**
      * Allocates a pdu session id
      *
      * AsyncResult.result is the allocated pdu session id
@@ -2747,6 +2816,54 @@
      public void unregisterForSimPhonebookRecordsReceived(Handler h);
 
     /**
+     * Registers for notifications of connection setup failure.
+     *
+     * @param h Handler for notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+    default void registerForConnectionSetupFailure(Handler h, int what, Object obj) {}
+
+    /**
+     * Unregisters for notifications of connection setup failure.
+     *
+     * @param h Handler to be removed from the registrant list.
+     */
+    default void unregisterForConnectionSetupFailure(Handler h) {}
+
+    /**
+     * Registers for notifications when ANBR is received form the network.
+     *
+     * @param h Handler for notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+    default void registerForNotifyAnbr(Handler h, int what, Object obj) {}
+
+    /**
+     * Unregisters for notifications when ANBR is received form the network.
+     *
+     * @param h Handler to be removed from the registrant list.
+     */
+    default void unregisterForNotifyAnbr(Handler h) {}
+
+    /**
+     * Registers for IMS deregistration trigger from modem.
+     *
+     * @param h Handler for notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+    default void registerForTriggerImsDeregistration(Handler h, int what, Object obj) {}
+
+    /**
+     * Unregisters for IMS deregistration trigger from modem.
+     *
+     * @param h Handler to be removed from the registrant list.
+     */
+    default void unregisterForTriggerImsDeregistration(Handler h) {}
+
+    /**
      * Set the UE's usage setting.
      *
      * @param result Callback message containing the success or failure status.
@@ -2761,4 +2878,151 @@
      * @param result Callback message containing the usage setting (or a failure status).
      */
     default void getUsageSetting(Message result) {}
+
+    /**
+     * Sets the emergency mode.
+     *
+     * @param emcMode Defines the radio emergency mode type.
+     * @param result Callback message containing the success or failure status.
+     */
+    default void setEmergencyMode(@EmergencyConstants.EmergencyMode int emcMode,
+            @Nullable Message result) {}
+
+    /**
+     * Triggers an emergency network scan.
+     *
+     * @param accessNetwork Contains the list of access network types to be prioritized
+     *        during emergency scan. The 1st entry has the highest priority.
+     * @param scanType Indicates the type of scans to be performed i.e. limited scan,
+     *        full service scan or both.
+     * @param result Callback message containing the success or failure status.
+     */
+    default void triggerEmergencyNetworkScan(
+            @NonNull @AccessNetworkConstants.RadioAccessNetworkType int[] accessNetwork,
+            @DomainSelectionService.EmergencyScanType int scanType, @Nullable Message result) {}
+
+    /**
+     * Cancels ongoing emergency network scan.
+     *
+     * @param resetScan Indicates how the next {@link #triggerEmergencyNetworkScan} should work.
+     *        If {@code true}, then the modem shall start the new scan from the beginning,
+     *        otherwise the modem shall resume from the last search.
+     * @param result Callback message containing the success or failure status.
+     */
+    default void cancelEmergencyNetworkScan(boolean resetScan, @Nullable Message result) {}
+
+    /**
+     * Exits ongoing emergency mode.
+     *
+     * @param result Callback message containing the success or failure status.
+     */
+    default void exitEmergencyMode(@Nullable Message result) {}
+
+    /**
+     * Registers for emergency network scan result.
+     *
+     * @param h Handler for notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+    default void registerForEmergencyNetworkScan(@NonNull Handler h,
+            int what, @Nullable Object obj) {}
+
+    /**
+     * Unregisters for emergency network scan result.
+     *
+     * @param h Handler to be removed from the registrant list.
+     */
+    default void unregisterForEmergencyNetworkScan(@NonNull Handler h) {}
+
+    /**
+     * Provides a list of SRVCC call information to radio
+     *
+     * @param srvccConnections the list of connections.
+     */
+    default void setSrvccCallInfo(SrvccConnection[] srvccConnections, Message result) {}
+
+    /**
+     * Updates the IMS registration information to the radio.
+     *
+     * @param state The current IMS registration state.
+     * @param imsRadioTech The type of underlying radio access network used.
+     * @param suggestedAction The suggested action for the radio to perform.
+     * @param capabilities IMS capabilities such as VOICE, VIDEO and SMS.
+     */
+    default void updateImsRegistrationInfo(int state,
+            @ImsRegistrationImplBase.ImsRegistrationTech int imsRadioTech,
+            @RegistrationManager.SuggestedAction int suggestedAction,
+            int capabilities, Message result) {}
+
+    /**
+     * Notifies the NAS and RRC layers of the radio the type of upcoming IMS traffic.
+     *
+     * @param token A nonce to identify the request.
+     * @param trafficType IMS traffic type like registration, voice, video, SMS, emergency, and etc.
+     * @param accessNetworkType The type of underlying radio access network used.
+     * @param trafficDirection Indicates whether traffic is originated by mobile originated or
+     *        mobile terminated use case eg. MO/MT call/SMS etc.
+     */
+    default void startImsTraffic(int token,
+            @MmTelFeature.ImsTrafficType int trafficType,
+            @AccessNetworkConstants.RadioAccessNetworkType int accessNetworkType,
+            @MmTelFeature.ImsTrafficDirection int trafficDirection,
+            Message result) {}
+
+    /**
+     * Notifies IMS traffic has been stopped.
+     *
+     * @param token The token assigned by startImsTraffic.
+     */
+    default void stopImsTraffic(int token, Message result) {}
+
+    /**
+     * Triggers the UE initiated EPS fallback procedure.
+     *
+     * @param reason Specifies the reason for EPS fallback.
+     */
+    default void triggerEpsFallback(int reason, Message result) {}
+
+    /**
+     * Triggers radio to send ANBRQ message to the network.
+     *
+     * @param mediaType Media type is used to identify media stream such as audio or video.
+     * @param direction Direction of this packet stream (e.g. uplink or downlink).
+     * @param bitsPerSecond The bit rate requested by the opponent UE.
+     * @param result Callback message to receive the result.
+     */
+    default void sendAnbrQuery(int mediaType, int direction, int bitsPerSecond, Message result) {}
+
+    /**
+     * Set the UE's ability to accept/reject null ciphered and/or null integrity-protected
+     * connections.
+     *
+     * @param enabled true to allow null ciphered and/or null integrity-protected connections,
+     * false to disallow.
+     * @param result Callback message containing the success or failure status.
+     */
+    default void setNullCipherAndIntegrityEnabled(boolean enabled, Message result) {}
+
+    /**
+     * Notifies the IMS call status to the modem.
+     *
+     * @param imsCallInfo The list of {@link ImsCallInfo}.
+     * @param result A callback to receive the response.
+     */
+    default void updateImsCallStatus(@NonNull List<ImsCallInfo> imsCallInfo, Message result) {}
+
+    /**
+     * Enables or disables N1 mode (access to 5G core network) in accordance with
+     * 3GPP TS 24.501 4.9.
+     * @param enable {@code true} to enable N1 mode, {@code false} to disable N1 mode.
+     * @param result Callback message to receive the result.
+     */
+    default void setN1ModeEnabled(boolean enable, Message result) {}
+
+    /**
+     * Check whether N1 mode (access to 5G core network) is enabled or not.
+     * @param result Callback message to receive the result.
+     */
+    default void isN1ModeEnabled(Message result) {}
 }
diff --git a/src/java/com/android/internal/telephony/Connection.java b/src/java/com/android/internal/telephony/Connection.java
index c60e5df..68fd6ab 100644
--- a/src/java/com/android/internal/telephony/Connection.java
+++ b/src/java/com/android/internal/telephony/Connection.java
@@ -27,6 +27,8 @@
 import android.telephony.ServiceState.RilRadioTechnology;
 import android.telephony.emergency.EmergencyNumber;
 import android.telephony.ims.RtpHeaderExtension;
+import android.telephony.ims.feature.MmTelFeature;
+import android.telephony.ims.feature.MmTelFeature.ImsAudioHandler;
 import android.util.Log;
 
 import com.android.ims.internal.ConferenceParticipant;
@@ -139,6 +141,13 @@
          * @param extensionData The extension data.
          */
         public void onReceivedRtpHeaderExtensions(@NonNull Set<RtpHeaderExtension> extensionData);
+
+        /**
+         * Indicates that the audio handler for this connection is changed.
+         *
+         * @param imsAudioHandler {@link MmTelFeature#ImsAudioHandler}.
+         */
+        void onAudioModeIsVoipChanged(@ImsAudioHandler int imsAudioHandler);
     }
 
     /**
@@ -194,6 +203,8 @@
         public void onReceivedDtmfDigit(char digit) {}
         @Override
         public void onReceivedRtpHeaderExtensions(@NonNull Set<RtpHeaderExtension> extensionData) {}
+        @Override
+        public void onAudioModeIsVoipChanged(@ImsAudioHandler int imsAudioHandler) {}
     }
 
     public static final int AUDIO_QUALITY_STANDARD = 1;
@@ -328,6 +339,41 @@
     /* Instance Methods */
 
     /**
+     * PhoneFactory Dependencies for testing.
+     */
+    @VisibleForTesting
+    public interface PhoneFactoryProxy {
+        Phone getPhone(int index);
+        Phone getDefaultPhone();
+        Phone[] getPhones();
+    }
+
+    private PhoneFactoryProxy mPhoneFactoryProxy = new PhoneFactoryProxy() {
+        @Override
+        public Phone getPhone(int index) {
+            return PhoneFactory.getPhone(index);
+        }
+
+        @Override
+        public Phone getDefaultPhone() {
+            return PhoneFactory.getDefaultPhone();
+        }
+
+        @Override
+        public Phone[] getPhones() {
+            return PhoneFactory.getPhones();
+        }
+    };
+
+    /**
+     * Overrides PhoneFactory dependencies for testing.
+     */
+    @VisibleForTesting
+    public void setPhoneFactoryProxy(PhoneFactoryProxy proxy) {
+        mPhoneFactoryProxy = proxy;
+    }
+
+    /**
      * @return The telecom internal call ID associated with this connection.  Only to be used for
      * debugging purposes.
      */
@@ -590,14 +636,35 @@
      */
     public void setEmergencyCallInfo(CallTracker ct) {
         if (ct != null) {
-            Phone phone = ct.getPhone();
-            if (phone != null) {
-                EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
+            Phone currentPhone = ct.getPhone();
+            if (currentPhone != null) {
+                EmergencyNumberTracker tracker = currentPhone.getEmergencyNumberTracker();
                 if (tracker != null) {
                     EmergencyNumber num = tracker.getEmergencyNumber(mAddress);
+                    Phone[] allPhones = mPhoneFactoryProxy.getPhones();
                     if (num != null) {
                         mIsEmergencyCall = true;
                         mEmergencyNumberInfo = num;
+                    } else if (allPhones.length > 1) {
+                        // If there are multiple active SIMs, check all instances:
+                        boolean found = false;
+                        for (Phone phone : allPhones) {
+                            // If the current iteration was already checked, skip:
+                            if (phone.getPhoneId() == currentPhone.getPhoneId()){
+                                continue;
+                            }
+                            num = phone.getEmergencyNumberTracker()
+                                    .getEmergencyNumber(mAddress);
+                            if (num != null){
+                                found = true;
+                                mIsEmergencyCall = true;
+                                mEmergencyNumberInfo = num;
+                                break;
+                            }
+                        }
+                        if (!found){
+                            Rlog.e(TAG, "setEmergencyCallInfo: emergency number is null");
+                        }
                     } else {
                         Rlog.e(TAG, "setEmergencyCallInfo: emergency number is null");
                     }
@@ -1519,6 +1586,25 @@
     }
 
     /**
+     * Called to report audio mode changed for Voip.
+     * @param imsAudioHandler the received value to handle the audio for this IMS call.
+     */
+    public void onAudioModeIsVoipChanged(@ImsAudioHandler int imsAudioHandler) {
+        Rlog.i(TAG, "onAudioModeIsVoipChanged: conn imsAudioHandler " + imsAudioHandler);
+
+        boolean isVoip = imsAudioHandler == MmTelFeature.AUDIO_HANDLER_ANDROID;
+        if (isVoip == mAudioModeIsVoip) return;
+        mAudioModeIsVoip = isVoip;
+
+        Rlog.i(TAG, "onAudioModeIsVoipChanged: isVoip: " + isVoip
+                + "mAudioModeIsVoip:" + mAudioModeIsVoip);
+
+        for (Listener l : mListeners) {
+            l.onAudioModeIsVoipChanged(imsAudioHandler);
+        }
+    }
+
+    /**
      * Called to report RTP header extensions received from the network.
      * @param extensionData the received extension data.
      */
diff --git a/src/java/com/android/internal/telephony/DataIndication.java b/src/java/com/android/internal/telephony/DataIndication.java
index 3467955..205c4d8 100644
--- a/src/java/com/android/internal/telephony/DataIndication.java
+++ b/src/java/com/android/internal/telephony/DataIndication.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_DATA;
+
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_DATA_CALL_LIST_CHANGED;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_KEEPALIVE_STATUS;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_PCO_DATA;
@@ -53,7 +55,7 @@
      */
     public void dataCallListChanged(int indicationType,
             android.hardware.radio.data.SetupDataCallResult[] dcList) {
-        mRil.processIndication(RIL.DATA_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_DATA, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_DATA_CALL_LIST_CHANGED, dcList);
         ArrayList<DataCallResponse> response = RILUtils.convertHalDataCallResultList(dcList);
@@ -68,7 +70,7 @@
      */
     public void keepaliveStatus(int indicationType,
             android.hardware.radio.data.KeepaliveStatus halStatus) {
-        mRil.processIndication(RIL.DATA_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_DATA, indicationType);
 
         if (mRil.isLogOrTrace()) {
             mRil.unsljLogRet(
@@ -87,7 +89,7 @@
      * @param pco New PcoData
      */
     public void pcoData(int indicationType, android.hardware.radio.data.PcoDataInfo pco) {
-        mRil.processIndication(RIL.DATA_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_DATA, indicationType);
 
         PcoData response = new PcoData(pco.cid, pco.bearerProto, pco.pcoId, pco.contents);
 
@@ -104,7 +106,7 @@
      */
     public void unthrottleApn(int indicationType, android.hardware.radio.data.DataProfileInfo dpi)
             throws RemoteException {
-        mRil.processIndication(RIL.DATA_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_DATA, indicationType);
         DataProfile response = RILUtils.convertToDataProfile(dpi);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_UNTHROTTLE_APN, response);
@@ -120,7 +122,7 @@
      */
     public void slicingConfigChanged(int indicationType,
             android.hardware.radio.data.SlicingConfig slicingConfig) throws RemoteException {
-        mRil.processIndication(RIL.DATA_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_DATA, indicationType);
         if (mRil.isLogOrTrace()) {
             mRil.unsljLogRet(RIL_UNSOL_SLICING_CONFIG_CHANGED, slicingConfig);
         }
diff --git a/src/java/com/android/internal/telephony/DataResponse.java b/src/java/com/android/internal/telephony/DataResponse.java
index 7cfe13b..bef1da7 100644
--- a/src/java/com/android/internal/telephony/DataResponse.java
+++ b/src/java/com/android/internal/telephony/DataResponse.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_DATA;
+
 import android.hardware.radio.RadioError;
 import android.hardware.radio.RadioResponseInfo;
 import android.hardware.radio.data.IRadioDataResponse;
@@ -51,7 +53,7 @@
      * @param id The pdu session id allocated
      */
     public void allocatePduSessionIdResponse(RadioResponseInfo responseInfo, int id) {
-        RILRequest rr = mRil.processResponse(RIL.DATA_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_DATA, responseInfo);
         if (rr != null) {
             if (responseInfo.error == RadioError.NONE) {
                 RadioResponse.sendMessageResponse(rr.mResult, id);
@@ -64,14 +66,14 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void cancelHandoverResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.DATA_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_DATA, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void deactivateDataCallResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.DATA_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_DATA, mRil, responseInfo);
     }
 
     /**
@@ -80,7 +82,7 @@
      */
     public void getDataCallListResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.data.SetupDataCallResult[] dataCallResultList) {
-        RILRequest rr = mRil.processResponse(RIL.DATA_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_DATA, responseInfo);
 
         if (rr != null) {
             ArrayList<DataCallResponse> response =
@@ -98,7 +100,7 @@
      */
     public void getSlicingConfigResponse(RadioResponseInfo responseInfo,
                 android.hardware.radio.data.SlicingConfig slicingConfig) {
-        RILRequest rr = mRil.processResponse(RIL.DATA_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_DATA, responseInfo);
 
         if (rr != null) {
             NetworkSlicingConfig ret = RILUtils.convertHalSlicingConfig(slicingConfig);
@@ -113,35 +115,35 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void releasePduSessionIdResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.DATA_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_DATA, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setDataAllowedResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.DATA_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_DATA, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setDataProfileResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.DATA_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_DATA, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setDataThrottlingResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.DATA_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_DATA, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setInitialAttachApnResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.DATA_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_DATA, mRil, responseInfo);
     }
 
     /**
@@ -150,7 +152,7 @@
      */
     public void setupDataCallResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.data.SetupDataCallResult setupDataCallResult) {
-        RILRequest rr = mRil.processResponse(RIL.DATA_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_DATA, responseInfo);
 
         if (rr != null) {
             DataCallResponse response = RILUtils.convertHalDataCallResult(setupDataCallResult);
@@ -165,7 +167,7 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void startHandoverResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.DATA_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_DATA, mRil, responseInfo);
     }
 
     /**
@@ -175,7 +177,7 @@
     public void startKeepaliveResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.data.KeepaliveStatus keepaliveStatus) {
 
-        RILRequest rr = mRil.processResponse(RIL.DATA_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_DATA, responseInfo);
         if (rr == null) return;
 
         KeepaliveStatus ret = null;
@@ -214,7 +216,7 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void stopKeepaliveResponse(RadioResponseInfo responseInfo) {
-        RILRequest rr = mRil.processResponse(RIL.DATA_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_DATA, responseInfo);
         if (rr == null) return;
 
         try {
diff --git a/src/java/com/android/internal/telephony/DefaultPhoneNotifier.java b/src/java/com/android/internal/telephony/DefaultPhoneNotifier.java
index e4aff4c..8eafeba 100644
--- a/src/java/com/android/internal/telephony/DefaultPhoneNotifier.java
+++ b/src/java/com/android/internal/telephony/DefaultPhoneNotifier.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.content.Context;
+import android.telephony.Annotation;
 import android.telephony.Annotation.RadioPowerState;
 import android.telephony.Annotation.SrvccState;
 import android.telephony.BarringInfo;
@@ -34,7 +35,9 @@
 import android.telephony.TelephonyManager.DataEnabledReason;
 import android.telephony.TelephonyRegistryManager;
 import android.telephony.emergency.EmergencyNumber;
+import android.telephony.ims.ImsCallSession;
 import android.telephony.ims.ImsReasonInfo;
+import android.telephony.ims.MediaQualityStatus;
 
 import com.android.telephony.Rlog;
 
@@ -142,15 +145,28 @@
         mTelephonyRegistryMgr.notifyCellInfoChanged(subId, cellInfo);
     }
 
-    public void notifyPreciseCallState(Phone sender) {
+    /**
+     * Notify precise call state of foreground, background and ringing call states.
+     *
+     * @param imsCallIds Array of IMS call session ID{@link ImsCallSession#getCallId} for
+     *                   ringing, foreground & background calls.
+     * @param imsCallServiceTypes Array of IMS call service type for ringing, foreground &
+     *                        background calls.
+     * @param imsCallTypes Array of IMS call type for ringing, foreground & background calls.
+     */
+    public void notifyPreciseCallState(Phone sender, String[] imsCallIds,
+            @Annotation.ImsCallServiceType int[] imsCallServiceTypes,
+            @Annotation.ImsCallType int[] imsCallTypes) {
         Call ringingCall = sender.getRingingCall();
         Call foregroundCall = sender.getForegroundCall();
         Call backgroundCall = sender.getBackgroundCall();
+
         if (ringingCall != null && foregroundCall != null && backgroundCall != null) {
-            mTelephonyRegistryMgr.notifyPreciseCallState(sender.getPhoneId(), sender.getSubId(),
-                    convertPreciseCallState(ringingCall.getState()),
+            int[] callStates = {convertPreciseCallState(ringingCall.getState()),
                     convertPreciseCallState(foregroundCall.getState()),
-                    convertPreciseCallState(backgroundCall.getState()));
+                    convertPreciseCallState(backgroundCall.getState())};
+            mTelephonyRegistryMgr.notifyPreciseCallState(sender.getPhoneId(), sender.getSubId(),
+                    callStates, imsCallIds, imsCallServiceTypes, imsCallTypes);
         }
     }
 
@@ -223,6 +239,12 @@
     }
 
     @Override
+    public void notifyMediaQualityStatusChanged(Phone sender, MediaQualityStatus status) {
+        mTelephonyRegistryMgr.notifyMediaQualityStatusChanged(
+                sender.getPhoneId(), sender.getSubId(), status);
+    }
+
+    @Override
     public void notifyRegistrationFailed(Phone sender, @NonNull CellIdentity cellIdentity,
             @NonNull String chosenPlmn, int domain, int causeCode, int additionalCauseCode) {
         mTelephonyRegistryMgr.notifyRegistrationFailed(sender.getPhoneId(), sender.getSubId(),
diff --git a/src/java/com/android/internal/telephony/DeviceStateMonitor.java b/src/java/com/android/internal/telephony/DeviceStateMonitor.java
index 3d63a29..ecc6208 100644
--- a/src/java/com/android/internal/telephony/DeviceStateMonitor.java
+++ b/src/java/com/android/internal/telephony/DeviceStateMonitor.java
@@ -20,6 +20,7 @@
 import static android.hardware.radio.V1_0.DeviceStateType.CHARGING_STATE;
 import static android.hardware.radio.V1_0.DeviceStateType.LOW_DATA_EXPECTED;
 import static android.hardware.radio.V1_0.DeviceStateType.POWER_SAVE_MODE;
+import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
 
 import android.app.UiModeManager;
 import android.content.BroadcastReceiver;
@@ -669,7 +670,7 @@
                 LINK_CAPACITY_UPLINK_THRESHOLDS, AccessNetworkType.EUTRAN);
         mPhone.setLinkCapacityReportingCriteria(LINK_CAPACITY_DOWNLINK_THRESHOLDS,
                 LINK_CAPACITY_UPLINK_THRESHOLDS, AccessNetworkType.CDMA2000);
-        if (mPhone.getHalVersion().greaterOrEqual(RIL.RADIO_HAL_VERSION_1_5)) {
+        if (mPhone.getHalVersion(HAL_SERVICE_NETWORK).greaterOrEqual(RIL.RADIO_HAL_VERSION_1_5)) {
             mPhone.setLinkCapacityReportingCriteria(LINK_CAPACITY_DOWNLINK_THRESHOLDS,
                     LINK_CAPACITY_UPLINK_THRESHOLDS, AccessNetworkType.NGRAN);
         }
diff --git a/src/java/com/android/internal/telephony/DisplayInfoController.java b/src/java/com/android/internal/telephony/DisplayInfoController.java
index 886a899..597ae90 100644
--- a/src/java/com/android/internal/telephony/DisplayInfoController.java
+++ b/src/java/com/android/internal/telephony/DisplayInfoController.java
@@ -18,11 +18,11 @@
 
 import android.annotation.NonNull;
 import android.os.Handler;
+import android.os.Message;
 import android.os.Registrant;
 import android.os.RegistrantList;
-import android.telephony.AccessNetworkConstants;
 import android.telephony.AnomalyReporter;
-import android.telephony.NetworkRegistrationInfo;
+import android.telephony.ServiceState;
 import android.telephony.TelephonyDisplayInfo;
 import android.telephony.TelephonyManager;
 import android.util.IndentingPrintWriter;
@@ -45,8 +45,6 @@
  * TelephonyDisplayInfo via {@link #getTelephonyDisplayInfo}.
  */
 public class DisplayInfoController extends Handler {
-    private static final String TAG = "DisplayInfoController";
-
     private final String mLogTag;
     private final LocalLog mLocalLog = new LocalLog(128);
 
@@ -66,22 +64,36 @@
                     TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED)
             );
 
+    /** Event for service state changed (roaming). */
+    private static final int EVENT_SERVICE_STATE_CHANGED = 1;
+
     private final Phone mPhone;
     private final NetworkTypeController mNetworkTypeController;
     private final RegistrantList mTelephonyDisplayInfoChangedRegistrants = new RegistrantList();
-    private TelephonyDisplayInfo mTelephonyDisplayInfo;
+    private @NonNull TelephonyDisplayInfo mTelephonyDisplayInfo;
+    private @NonNull ServiceState mServiceState;
 
     public DisplayInfoController(Phone phone) {
         mPhone = phone;
         mLogTag = "DIC-" + mPhone.getPhoneId();
+        mTelephonyDisplayInfo = new TelephonyDisplayInfo(
+                TelephonyManager.NETWORK_TYPE_UNKNOWN,
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE);
         mNetworkTypeController = new NetworkTypeController(phone, this);
         mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+
+        mServiceState = mPhone.getServiceStateTracker().getServiceState();
+        post(() -> {
+            mPhone.getServiceStateTracker()
+                    .registerForServiceStateChanged(this, EVENT_SERVICE_STATE_CHANGED, null);
+            updateTelephonyDisplayInfo();
+        });
     }
 
     /**
      * @return the current TelephonyDisplayInfo
      */
-    public TelephonyDisplayInfo getTelephonyDisplayInfo() {
+    public @NonNull TelephonyDisplayInfo getTelephonyDisplayInfo() {
         return mTelephonyDisplayInfo;
     }
 
@@ -90,12 +102,10 @@
      * NetworkTypeController.
      */
     public void updateTelephonyDisplayInfo() {
-        NetworkRegistrationInfo nri =  mPhone.getServiceState().getNetworkRegistrationInfo(
-                NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
-        int dataNetworkType = nri == null ? TelephonyManager.NETWORK_TYPE_UNKNOWN
-                : nri.getAccessNetworkTechnology();
-        TelephonyDisplayInfo newDisplayInfo = new TelephonyDisplayInfo(dataNetworkType,
-                mNetworkTypeController.getOverrideNetworkType());
+        TelephonyDisplayInfo newDisplayInfo = new TelephonyDisplayInfo(
+                mNetworkTypeController.getDataNetworkType(),
+                mNetworkTypeController.getOverrideNetworkType(),
+                mServiceState.getRoaming());
         if (!newDisplayInfo.equals(mTelephonyDisplayInfo)) {
             logl("TelephonyDisplayInfo changed from " + mTelephonyDisplayInfo + " to "
                     + newDisplayInfo);
@@ -157,6 +167,17 @@
         mTelephonyDisplayInfoChangedRegistrants.remove(h);
     }
 
+    @Override
+    public void handleMessage(@NonNull Message msg) {
+        switch (msg.what) {
+            case EVENT_SERVICE_STATE_CHANGED:
+                mServiceState = mPhone.getServiceStateTracker().getServiceState();
+                log("ServiceState updated, isRoaming=" + mServiceState.getRoaming());
+                updateTelephonyDisplayInfo();
+                break;
+        }
+    }
+
     /**
      * Log debug messages.
      * @param s debug messages
diff --git a/src/java/com/android/internal/telephony/FdnUtils.java b/src/java/com/android/internal/telephony/FdnUtils.java
new file mode 100644
index 0000000..aa2bcfd
--- /dev/null
+++ b/src/java/com/android/internal/telephony/FdnUtils.java
@@ -0,0 +1,190 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import android.text.TextUtils;
+
+import com.android.i18n.phonenumbers.NumberParseException;
+import com.android.i18n.phonenumbers.PhoneNumberUtil;
+import com.android.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
+import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.uicc.AdnRecord;
+import com.android.internal.telephony.uicc.AdnRecordCache;
+import com.android.internal.telephony.uicc.IccConstants;
+import com.android.internal.telephony.uicc.IccRecords;
+import com.android.internal.telephony.uicc.UiccCardApplication;
+import com.android.internal.telephony.uicc.UiccController;
+import com.android.internal.telephony.uicc.UiccProfile;
+import com.android.telephony.Rlog;
+
+import java.util.ArrayList;
+
+/**
+ * This is a basic utility class for common functions related to Fixed Dialing Numbers
+ * designed as per 3GPP 22.101.
+ */
+public class FdnUtils {
+    private static final boolean VDBG = false;
+    private static final String LOG_TAG = FdnUtils.class.getSimpleName();
+
+    /**
+     * The following function checks if dialed number is blocked due to FDN.
+     *
+     * @param phoneId The phone object id for which the FDN check is performed
+     * @param dialStr dialed phone number
+     * @param defaultCountryIso country ISO for the subscription associated with this phone
+     * @return {@code true} if dialStr is blocked due to FDN check.
+     */
+    public static boolean isNumberBlockedByFDN(int phoneId, String dialStr,
+            String defaultCountryIso) {
+        if (!isFdnEnabled(phoneId)) {
+            return false;
+        }
+
+        ArrayList<AdnRecord> fdnList = getFdnList(phoneId);
+        return !isFDN(dialStr, defaultCountryIso, fdnList);
+    }
+
+    /**
+     * Checks if FDN is enabled
+     * @param phoneId The phone object id for which the FDN check is performed
+     * @return {@code true} if FDN is enabled
+     */
+    public static boolean isFdnEnabled(int phoneId) {
+        UiccCardApplication app = getUiccCardApplication(phoneId);
+        if (app == null || (!app.getIccFdnAvailable())) {
+            return false;
+        }
+
+        return app.getIccFdnEnabled();
+    }
+
+    /**
+     * If FDN is enabled, check to see if the given supplementary service control strings are
+     * blocked due to FDN.
+     * @param phoneId The phone object id for which the FDN check is performed
+     * @param controlStrings control strings associated with the supplementary service request
+     * @param defaultCountryIso country ISO for the subscription associated with this phone
+     * @return {@code true} if the FDN list does not contain any of the control strings.
+     */
+    public static boolean isSuppServiceRequestBlockedByFdn(int phoneId,
+            ArrayList<String> controlStrings, String defaultCountryIso) {
+        if (!isFdnEnabled(phoneId)) {
+            return false;
+        }
+
+        ArrayList<AdnRecord> fdnList = getFdnList(phoneId);
+        for(String controlString : controlStrings) {
+            if(isFDN(controlString, defaultCountryIso, fdnList)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Checks if dialStr is part of FDN list.
+     *
+     * @param fdnList List of all FDN records associated with a sim card
+     * @param dialStr dialed phone number
+     * @param defaultCountryIso country ISO for the subscription associated with this phone
+     * @return {@code true} if dialStr is present in the fdnList.
+     */
+    @VisibleForTesting
+    public static boolean isFDN(String dialStr, String defaultCountryIso,
+            ArrayList<AdnRecord> fdnList) {
+        if (fdnList == null || fdnList.isEmpty() || TextUtils.isEmpty(dialStr)) {
+            Rlog.w(LOG_TAG, "isFDN: unexpected null value");
+            return false;
+        }
+
+        // Parse the dialStr and convert it to E164 format
+        String dialStrE164 = null;
+        String dialStrNational = null;
+        final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
+        try {
+            PhoneNumber phoneNumber = phoneNumberUtil.parse(dialStr, defaultCountryIso);
+            dialStrE164 = phoneNumberUtil.format(phoneNumber, PhoneNumberFormat.E164);
+            dialStrNational = String.valueOf(phoneNumber.getNationalNumber());
+        } catch (NumberParseException ignored) {
+            Rlog.w(LOG_TAG, "isFDN: could not parse dialStr");
+        }
+
+        /**
+         * Returns true if dialStrE164 or dialStrNational or dialStr starts with fdnNumber
+         * E.g.1: returns true if fdnNumber="123" and dialStr="12345"
+         * E.g.2: does not return true if fdnNumber="1123" and dialStr="12345"
+         */
+        for (AdnRecord fdn: fdnList) {
+            String fdnNumber = fdn.getNumber();
+            if (TextUtils.isEmpty(fdnNumber)) {
+                continue;
+            }
+
+            if(!TextUtils.isEmpty(dialStrE164)) {
+                if(dialStrE164.startsWith(fdnNumber)) {
+                    return true;
+                }
+            }
+
+            if(!TextUtils.isEmpty(dialStrNational)) {
+                if (dialStrNational.startsWith(fdnNumber)) {
+                    return true;
+                }
+            }
+
+            if (dialStr.startsWith(fdnNumber)) {
+                return true;
+            }
+        }
+
+        if (VDBG) {
+            Rlog.i(LOG_TAG, "isFDN: dialed number not present in FDN list");
+        }
+        return false;
+    }
+
+    private static ArrayList<AdnRecord> getFdnList(int phoneId) {
+        UiccCardApplication app = getUiccCardApplication(phoneId);
+        if (app == null) {
+            return null;
+        }
+
+        IccRecords iccRecords = app.getIccRecords();
+        if (iccRecords == null) {
+            return null;
+        }
+
+        AdnRecordCache adnRecordCache = iccRecords.getAdnCache();
+        if(adnRecordCache == null) {
+            return null;
+        }
+
+        return adnRecordCache.getRecordsIfLoaded(IccConstants.EF_FDN);
+    }
+
+    private static UiccCardApplication getUiccCardApplication(int phoneId) {
+        UiccProfile uiccProfile = UiccController.getInstance()
+                .getUiccProfileForPhone(phoneId);
+        if (uiccProfile == null) {
+            return null;
+        }
+
+        return uiccProfile.getApplication(UiccController.APP_FAM_3GPP);
+    }
+}
\ No newline at end of file
diff --git a/src/java/com/android/internal/telephony/GsmCdmaCallTracker.java b/src/java/com/android/internal/telephony/GsmCdmaCallTracker.java
old mode 100755
new mode 100644
index 7738b44..263bfeb
--- a/src/java/com/android/internal/telephony/GsmCdmaCallTracker.java
+++ b/src/java/com/android/internal/telephony/GsmCdmaCallTracker.java
@@ -959,6 +959,8 @@
                             } else {
                                 newUnknownConnectionCdma = mConnections[i];
                             }
+                        } else if (hangupWaitingCallSilently(i)) {
+                            return;
                         }
                     }
                 }
@@ -1010,6 +1012,9 @@
 
                 if (mConnections[i].getCall() == mRingingCall) {
                     newRinging = mConnections[i];
+                    if (hangupWaitingCallSilently(i)) {
+                        return;
+                    }
                 } // else something strange happened
                 hasNonHangupStateChanged = true;
             } else if (conn != null && dc != null) { /* implicit conn.compareTo(dc) */
@@ -1898,4 +1903,22 @@
     public void cleanupCalls() {
         pollCallsWhenSafe();
     }
+
+    private boolean hangupWaitingCallSilently(int index) {
+        if (index < 0 || index >= mConnections.length) return false;
+
+        GsmCdmaConnection newRinging = mConnections[index];
+        if (newRinging == null) return false;
+
+        if ((mPhone.getTerminalBasedCallWaitingState(true)
+                        == CallWaitingController.TERMINAL_BASED_NOT_ACTIVATED)
+                && (newRinging.getState() == Call.State.WAITING)) {
+            Rlog.d(LOG_TAG, "hangupWaitingCallSilently");
+            newRinging.dispose();
+            mConnections[index] = null;
+            mCi.hangupWaitingOrBackground(obtainCompleteMessage());
+            return true;
+        }
+        return false;
+    }
 }
diff --git a/src/java/com/android/internal/telephony/GsmCdmaPhone.java b/src/java/com/android/internal/telephony/GsmCdmaPhone.java
index 932461c..b22176b 100644
--- a/src/java/com/android/internal/telephony/GsmCdmaPhone.java
+++ b/src/java/com/android/internal/telephony/GsmCdmaPhone.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.NetworkRegistrationInfo.DOMAIN_PS;
+
 import static com.android.internal.telephony.CommandException.Error.GENERIC_FAILURE;
 import static com.android.internal.telephony.CommandException.Error.SIM_BUSY;
 import static com.android.internal.telephony.CommandsInterface.CF_ACTION_DISABLE;
@@ -40,6 +42,7 @@
 import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.database.SQLException;
+import android.hardware.radio.modem.ImeiInfo;
 import android.net.Uri;
 import android.os.AsyncResult;
 import android.os.Build;
@@ -57,6 +60,7 @@
 import android.os.UserHandle;
 import android.os.WorkSource;
 import android.preference.PreferenceManager;
+import android.provider.DeviceConfig;
 import android.provider.Settings;
 import android.provider.Telephony;
 import android.sysprop.TelephonyProperties;
@@ -64,10 +68,12 @@
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
 import android.telecom.VideoProfile;
+import android.telephony.AccessNetworkConstants.TransportType;
 import android.telephony.Annotation.DataActivityType;
 import android.telephony.Annotation.RadioPowerState;
 import android.telephony.BarringInfo;
 import android.telephony.CarrierConfigManager;
+import android.telephony.CellBroadcastIdRange;
 import android.telephony.CellIdentity;
 import android.telephony.ImsiEncryptionInfo;
 import android.telephony.LinkCapacityEstimate;
@@ -94,12 +100,15 @@
 import com.android.internal.telephony.data.LinkBandwidthEstimator;
 import com.android.internal.telephony.emergency.EmergencyNumberTracker;
 import com.android.internal.telephony.gsm.GsmMmiCode;
+import com.android.internal.telephony.gsm.SsData;
 import com.android.internal.telephony.gsm.SuppServiceNotification;
 import com.android.internal.telephony.imsphone.ImsPhone;
 import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
 import com.android.internal.telephony.imsphone.ImsPhoneMmiCode;
 import com.android.internal.telephony.metrics.TelephonyMetrics;
 import com.android.internal.telephony.metrics.VoiceCallSessionStats;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService.SubscriptionManagerServiceCallback;
 import com.android.internal.telephony.test.SimulatedRadioControl;
 import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
 import com.android.internal.telephony.uicc.IccCardStatus;
@@ -126,6 +135,8 @@
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
+import java.util.Set;
 import java.util.function.Consumer;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -222,10 +233,16 @@
 
     private final RegistrantList mVolteSilentRedialRegistrants = new RegistrantList();
     private DialArgs mDialArgs = null;
-
+    private final RegistrantList mEmergencyDomainSelectedRegistrants = new RegistrantList();
     private String mImei;
     private String mImeiSv;
     private String mVmNumber;
+    private int mImeiType = IMEI_TYPE_UNKNOWN;
+
+    CellBroadcastConfigTracker mCellBroadcastConfigTracker =
+            CellBroadcastConfigTracker.make(this, null);
+
+    private boolean mIsNullCipherAndIntegritySupported = false;
 
     // Create Cfu (Call forward unconditional) so that dialing number &
     // mOnComplete (Message object passed by client) can be packed &
@@ -270,6 +287,7 @@
     private final CarrierPrivilegesTracker mCarrierPrivilegesTracker;
 
     private final SubscriptionManager.OnSubscriptionsChangedListener mSubscriptionsChangedListener;
+    private final CallWaitingController mCallWaitingController;
 
     // Constructors
 
@@ -339,14 +357,27 @@
 
         mSST.registerForNetworkAttached(this, EVENT_REGISTERED_TO_NETWORK, null);
         mSST.registerForVoiceRegStateOrRatChanged(this, EVENT_VRS_OR_RAT_CHANGED, null);
+        mSST.getServiceStateStats().registerDataNetworkControllerCallback();
 
-        SubscriptionController.getInstance().registerForUiccAppsEnabled(this,
-                EVENT_UICC_APPS_ENABLEMENT_SETTING_CHANGED, null, false);
+        if (isSubscriptionManagerServiceEnabled()) {
+            mSubscriptionManagerService.registerCallback(new SubscriptionManagerServiceCallback(
+                    this::post) {
+                @Override
+                public void onUiccApplicationsEnabled(int subId) {
+                    reapplyUiccAppsEnablementIfNeeded(ENABLE_UICC_APPS_MAX_RETRIES);
+                }
+            });
+        } else {
+            SubscriptionController.getInstance().registerForUiccAppsEnabled(this,
+                    EVENT_UICC_APPS_ENABLEMENT_SETTING_CHANGED, null, false);
+        }
 
         mLinkBandwidthEstimator = mTelephonyComponentFactory
                 .inject(LinkBandwidthEstimator.class.getName())
                 .makeLinkBandwidthEstimator(this);
 
+        mCallWaitingController = new CallWaitingController(this);
+
         loadTtyMode();
 
         CallManager.getInstance().registerPhone(this);
@@ -440,6 +471,8 @@
         mCi.registerForLceInfo(this, EVENT_LINK_CAPACITY_CHANGED, null);
         mCi.registerForCarrierInfoForImsiEncryption(this,
                 EVENT_RESET_CARRIER_KEY_IMSI_ENCRYPTION, null);
+        mCi.registerForTriggerImsDeregistration(this, EVENT_IMS_DEREGISTRATION_TRIGGERED, null);
+        mCi.registerForNotifyAnbr(this, EVENT_TRIGGER_NOTIFY_ANBR, null);
         IntentFilter filter = new IntentFilter(
                 CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
         filter.addAction(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED);
@@ -495,7 +528,11 @@
                 logd("update icc_operator_numeric=" + operatorNumeric);
                 tm.setSimOperatorNumericForPhone(mPhoneId, operatorNumeric);
 
-                SubscriptionController.getInstance().setMccMnc(operatorNumeric, getSubId());
+                if (isSubscriptionManagerServiceEnabled()) {
+                    mSubscriptionManagerService.setMccMnc(getSubId(), operatorNumeric);
+                } else {
+                    SubscriptionController.getInstance().setMccMnc(operatorNumeric, getSubId());
+                }
 
                 // Sets iso country property by retrieving from build-time system property
                 String iso = "";
@@ -507,7 +544,11 @@
 
                 logd("init: set 'gsm.sim.operator.iso-country' to iso=" + iso);
                 tm.setSimCountryIsoForPhone(mPhoneId, iso);
-                SubscriptionController.getInstance().setCountryIso(iso, getSubId());
+                if (isSubscriptionManagerServiceEnabled()) {
+                    mSubscriptionManagerService.setCountryIso(getSubId(), iso);
+                } else {
+                    SubscriptionController.getInstance().setCountryIso(iso, getSubId());
+                }
 
                 // Updates MCC MNC device configuration information
                 logd("update mccmnc=" + operatorNumeric);
@@ -716,7 +757,10 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     public void notifyPreciseCallStateChanged() {
         /* we'd love it if this was package-scoped*/
-        super.notifyPreciseCallStateChangedP();
+        AsyncResult ar = new AsyncResult(null, this, null);
+        mPreciseCallStateRegistrants.notifyRegistrants(ar);
+
+        mNotifier.notifyPreciseCallState(this, null, null, null);
     }
 
     public void notifyNewRingingConnection(Connection c) {
@@ -908,14 +952,6 @@
     }
 
     @Override
-    public void dispose() {
-        // Note: this API is currently never called. We are defining actions here in case
-        // we need to dispose GsmCdmaPhone/Phone object.
-        super.dispose();
-        SubscriptionController.getInstance().unregisterForUiccAppsEnabled(this);
-    }
-
-    @Override
     public void enableEnhancedVoicePrivacy(boolean enable, Message onComplete) {
         if (isPhoneTypeGsm()) {
             loge("enableEnhancedVoicePrivacy: not expected on GSM");
@@ -1319,8 +1355,7 @@
         // emergency number list on another SIM, but is not on theirs.  In this case we will use the
         // emergency number list for this carrier's SIM only.
         if (useOnlyDialedSimEccList) {
-            isEmergency = getEmergencyNumberTracker().isEmergencyNumber(dialString,
-                    true /* exactMatch */);
+            isEmergency = getEmergencyNumberTracker().isEmergencyNumber(dialString);
             logi("dial; isEmergency=" + isEmergency
                     + " (based on this phone only); globalIsEmergency="
                     + tm.isEmergencyNumber(dialString));
@@ -1329,6 +1364,12 @@
             logi("dial; isEmergency=" + isEmergency + " (based on all phones)");
         }
 
+        // Undetectable emergeny number indicated by new domain selection service
+        if (dialArgs.isEmergency) {
+            logi("dial; isEmergency=" + isEmergency + " (domain selection module)");
+            isEmergency = true;
+        }
+
         /** Check if the call is Wireless Priority Service call */
         boolean isWpsCall = dialString != null ? (dialString.startsWith(PREFIX_WPS)
                 || dialString.startsWith(PREFIX_WPS_CLIR_ACTIVATE)
@@ -1354,6 +1395,27 @@
         boolean useImsForCall = useImsForCall(dialArgs)
                 && (isWpsCall ? allowWpsOverIms : true);
 
+        Bundle extras = dialArgs.intentExtras;
+        // Only when the domain selection service is supported, EXTRA_DIAL_DOMAIN extra shall exist.
+        if (extras != null && extras.containsKey(PhoneConstants.EXTRA_DIAL_DOMAIN)) {
+            int domain = extras.getInt(PhoneConstants.EXTRA_DIAL_DOMAIN);
+            logi("dial domain=" + domain);
+            useImsForCall = false;
+            useImsForUt = false;
+            useImsForEmergency = false;
+            if (domain == DOMAIN_PS) {
+                if (isEmergency) {
+                    useImsForEmergency = true;
+                } else if (!isMmiCode || isPotentialUssdCode) {
+                    useImsForCall = true;
+                } else {
+                    // should not reach here
+                    loge("dial unexpected Ut domain selection, ignored");
+                }
+            }
+            extras.remove(PhoneConstants.EXTRA_DIAL_DOMAIN);
+        }
+
         if (DBG) {
             logi("useImsForCall=" + useImsForCall
                     + ", useOnlyDialedSimEccList=" + useOnlyDialedSimEccList
@@ -1376,6 +1438,12 @@
                     + ((imsPhone != null) ? imsPhone.getServiceState().getState() : "N/A"));
         }
 
+        // Perform FDN check for non-emergency calls - shouldn't dial if number is blocked by FDN
+        if(!isEmergency && FdnUtils.isNumberBlockedByFDN(mPhoneId, dialString, getCountryIso())) {
+            throw new CallStateException(CallStateException.ERROR_FDN_BLOCKED,
+                    "cannot dial number blocked by FDN");
+        }
+
         // Bypass WiFi Only WFC check if this is an emergency call - we should still try to
         // place over cellular if possible.
         if (!isEmergency) {
@@ -1573,6 +1641,13 @@
             return true;
         }
 
+        // Perform FDN check
+        if(FdnUtils.isNumberBlockedByFDN(mPhoneId, ussdRequest, getCountryIso())) {
+            sendUssdResponse(ussdRequest, null, TelephonyManager.USSD_RETURN_FAILURE,
+                    wrappedCallback );
+            return true;
+        }
+
         // Try over IMS if possible.
         Phone imsPhone = mImsPhone;
         if ((imsPhone != null)
@@ -1672,7 +1747,7 @@
     public void setRadioPower(boolean power, boolean forEmergencyCall,
             boolean isSelectedPhoneForEmergencyCall, boolean forceApply) {
         setRadioPowerForReason(power, forEmergencyCall, isSelectedPhoneForEmergencyCall, forceApply,
-                Phone.RADIO_POWER_REASON_USER);
+                TelephonyManager.RADIO_POWER_REASON_USER);
     }
 
     @Override
@@ -1682,6 +1757,11 @@
                 forceApply, reason);
     }
 
+    @Override
+    public Set<Integer> getRadioPowerOffReasons() {
+        return mSST.getRadioPowerOffReasons();
+    }
+
     private void storeVoiceMailNumber(String number) {
         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
         SharedPreferences.Editor editor = sp.edit();
@@ -1831,6 +1911,11 @@
         return mImei;
     }
 
+    @Override
+    public int getImeiType() {
+        return mImeiType;
+    }
+
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     @Override
     public String getEsn() {
@@ -2216,6 +2301,15 @@
     @Override
     public void getCallForwardingOption(int commandInterfaceCFReason, int serviceClass,
             Message onComplete) {
+        // Perform FDN check
+        SsData.ServiceType serviceType = GsmMmiCode.cfReasonToServiceType(commandInterfaceCFReason);
+        if(isRequestBlockedByFDN(SsData.RequestType.SS_INTERROGATION, serviceType)) {
+            AsyncResult.forMessage(onComplete, null,
+                    new CommandException(CommandException.Error.FDN_CHECK_FAILURE));
+            onComplete.sendToTarget();
+            return;
+        }
+
         Phone imsPhone = mImsPhone;
         if (useSsOverIms(onComplete)) {
             imsPhone.getCallForwardingOption(commandInterfaceCFReason, serviceClass, onComplete);
@@ -2265,6 +2359,16 @@
             int serviceClass,
             int timerSeconds,
             Message onComplete) {
+        // Perform FDN check
+        SsData.RequestType requestType = GsmMmiCode.cfActionToRequestType(commandInterfaceCFAction);
+        SsData.ServiceType serviceType = GsmMmiCode.cfReasonToServiceType(commandInterfaceCFReason);
+        if(isRequestBlockedByFDN(requestType, serviceType)) {
+            AsyncResult.forMessage(onComplete, null,
+                    new CommandException(CommandException.Error.FDN_CHECK_FAILURE));
+            onComplete.sendToTarget();
+            return;
+        }
+
         Phone imsPhone = mImsPhone;
         if (useSsOverIms(onComplete)) {
             imsPhone.setCallForwardingOption(commandInterfaceCFAction, commandInterfaceCFReason,
@@ -2318,6 +2422,15 @@
     @Override
     public void getCallBarring(String facility, String password, Message onComplete,
             int serviceClass) {
+        // Perform FDN check
+        SsData.ServiceType serviceType = GsmMmiCode.cbFacilityToServiceType(facility);
+        if (isRequestBlockedByFDN(SsData.RequestType.SS_INTERROGATION, serviceType)) {
+            AsyncResult.forMessage(onComplete, null,
+                    new CommandException(CommandException.Error.FDN_CHECK_FAILURE));
+            onComplete.sendToTarget();
+            return;
+        }
+
         Phone imsPhone = mImsPhone;
         if (useSsOverIms(onComplete)) {
             imsPhone.getCallBarring(facility, password, onComplete, serviceClass);
@@ -2334,6 +2447,17 @@
     @Override
     public void setCallBarring(String facility, boolean lockState, String password,
             Message onComplete, int serviceClass) {
+        // Perform FDN check
+        SsData.RequestType requestType = lockState ? SsData.RequestType.SS_ACTIVATION :
+                SsData.RequestType.SS_DEACTIVATION;
+        SsData.ServiceType serviceType = GsmMmiCode.cbFacilityToServiceType(facility);
+        if (isRequestBlockedByFDN(requestType, serviceType)) {
+            AsyncResult.forMessage(onComplete, null,
+                    new CommandException(CommandException.Error.FDN_CHECK_FAILURE));
+            onComplete.sendToTarget();
+            return;
+        }
+
         Phone imsPhone = mImsPhone;
         if (useSsOverIms(onComplete)) {
             imsPhone.setCallBarring(facility, lockState, password, onComplete, serviceClass);
@@ -2357,6 +2481,18 @@
      */
     public void changeCallBarringPassword(String facility, String oldPwd, String newPwd,
             Message onComplete) {
+        // Perform FDN check
+        SsData.ServiceType serviceType = GsmMmiCode.cbFacilityToServiceType(facility);
+        ArrayList<String> controlStrings = GsmMmiCode.getControlStringsForPwd(
+                SsData.RequestType.SS_REGISTRATION,
+                serviceType);
+        if(FdnUtils.isSuppServiceRequestBlockedByFdn(mPhoneId, controlStrings, getCountryIso())) {
+            AsyncResult.forMessage(onComplete, null,
+                    new CommandException(CommandException.Error.FDN_CHECK_FAILURE));
+            onComplete.sendToTarget();
+            return;
+        }
+
         if (isPhoneTypeGsm()) {
             mCi.changeBarringPassword(facility, oldPwd, newPwd, onComplete);
         } else {
@@ -2366,7 +2502,16 @@
 
     @Override
     public void getOutgoingCallerIdDisplay(Message onComplete) {
+        // Perform FDN check
+        if(isRequestBlockedByFDN(SsData.RequestType.SS_INTERROGATION, SsData.ServiceType.SS_CLIR)){
+            AsyncResult.forMessage(onComplete, null,
+                    new CommandException(CommandException.Error.FDN_CHECK_FAILURE));
+            onComplete.sendToTarget();
+            return;
+        }
+
         Phone imsPhone = mImsPhone;
+
         if (useSsOverIms(onComplete)) {
             imsPhone.getOutgoingCallerIdDisplay(onComplete);
             return;
@@ -2384,6 +2529,15 @@
 
     @Override
     public void setOutgoingCallerIdDisplay(int commandInterfaceCLIRMode, Message onComplete) {
+        // Perform FDN check
+        SsData.RequestType requestType = GsmMmiCode.clirModeToRequestType(commandInterfaceCLIRMode);
+        if (isRequestBlockedByFDN(requestType, SsData.ServiceType.SS_CLIR)) {
+            AsyncResult.forMessage(onComplete, null,
+                    new CommandException(CommandException.Error.FDN_CHECK_FAILURE));
+            onComplete.sendToTarget();
+            return;
+        }
+
         Phone imsPhone = mImsPhone;
         if (useSsOverIms(onComplete)) {
             imsPhone.setOutgoingCallerIdDisplay(commandInterfaceCLIRMode, onComplete);
@@ -2406,6 +2560,14 @@
 
     @Override
     public void queryCLIP(Message onComplete) {
+        // Perform FDN check
+        if(isRequestBlockedByFDN(SsData.RequestType.SS_INTERROGATION, SsData.ServiceType.SS_CLIP)){
+            AsyncResult.forMessage(onComplete, null,
+                    new CommandException(CommandException.Error.FDN_CHECK_FAILURE));
+            onComplete.sendToTarget();
+            return;
+        }
+
         Phone imsPhone = mImsPhone;
         if (useSsOverIms(onComplete)) {
             imsPhone.queryCLIP(onComplete);
@@ -2424,6 +2586,16 @@
 
     @Override
     public void getCallWaiting(Message onComplete) {
+        // Perform FDN check
+        if(isRequestBlockedByFDN(SsData.RequestType.SS_INTERROGATION, SsData.ServiceType.SS_WAIT)){
+            AsyncResult.forMessage(onComplete, null,
+                    new CommandException(CommandException.Error.FDN_CHECK_FAILURE));
+            onComplete.sendToTarget();
+            return;
+        }
+
+        if (mCallWaitingController.getCallWaiting(onComplete)) return;
+
         Phone imsPhone = mImsPhone;
         if (useSsOverIms(onComplete)) {
             imsPhone.getCallWaiting(onComplete);
@@ -2465,6 +2637,18 @@
 
     @Override
     public void setCallWaiting(boolean enable, int serviceClass, Message onComplete) {
+        // Perform FDN check
+        SsData.RequestType requestType = enable ? SsData.RequestType.SS_ACTIVATION :
+                SsData.RequestType.SS_DEACTIVATION;
+        if (isRequestBlockedByFDN(requestType, SsData.ServiceType.SS_WAIT)) {
+            AsyncResult.forMessage(onComplete, null,
+                    new CommandException(CommandException.Error.FDN_CHECK_FAILURE));
+            onComplete.sendToTarget();
+            return;
+        }
+
+        if (mCallWaitingController.setCallWaiting(enable, serviceClass, onComplete)) return;
+
         Phone imsPhone = mImsPhone;
         if (useSsOverIms(onComplete)) {
             imsPhone.setCallWaiting(enable, onComplete);
@@ -2495,6 +2679,23 @@
     }
 
     @Override
+    public int getTerminalBasedCallWaitingState(boolean forCsOnly) {
+        return mCallWaitingController.getTerminalBasedCallWaitingState(forCsOnly);
+    }
+
+    @Override
+    public void setTerminalBasedCallWaitingStatus(int state) {
+        if (mImsPhone != null) {
+            mImsPhone.setTerminalBasedCallWaitingStatus(state);
+        }
+    }
+
+    @Override
+    public void setTerminalBasedCallWaitingSupported(boolean supported) {
+        mCallWaitingController.setTerminalBasedCallWaitingSupported(supported);
+    }
+
+    @Override
     public void getAvailableNetworks(Message response) {
         if (isPhoneTypeGsm() || isPhoneTypeCdmaLte()) {
             Message msg = obtainMessage(EVENT_GET_AVAILABLE_NETWORKS_DONE, response);
@@ -2764,11 +2965,12 @@
 
     private void handleRadioAvailable() {
         mCi.getBasebandVersion(obtainMessage(EVENT_GET_BASEBAND_VERSION_DONE));
-
+        mCi.getImei(obtainMessage(EVENT_GET_DEVICE_IMEI_DONE));
         mCi.getDeviceIdentity(obtainMessage(EVENT_GET_DEVICE_IDENTITY_DONE));
         mCi.getRadioCapability(obtainMessage(EVENT_GET_RADIO_CAPABILITY));
         mCi.areUiccApplicationsEnabled(obtainMessage(EVENT_GET_UICC_APPS_ENABLEMENT_DONE));
 
+        handleNullCipherEnabledChange();
         startLceAfterRadioIsAvailable();
     }
 
@@ -2814,7 +3016,21 @@
                 handleRadioAvailable();
             }
             break;
-
+            case EVENT_GET_DEVICE_IMEI_DONE :
+                ar = (AsyncResult)msg.obj;
+                if (ar.exception != null || ar.result == null) {
+                    loge("Exception received : " + ar.exception);
+                    break;
+                }
+                ImeiInfo imeiInfo = (ImeiInfo) ar.result;
+                if (!TextUtils.isEmpty(imeiInfo.imei)) {
+                    mImeiType = imeiInfo.type;
+                    mImei = imeiInfo.imei;
+                    mImeiSv = imeiInfo.svn;
+                } else {
+                    // TODO Report telephony anomaly
+                }
+                break;
             case EVENT_GET_DEVICE_IDENTITY_DONE:{
                 ar = (AsyncResult)msg.obj;
 
@@ -2822,8 +3038,10 @@
                     break;
                 }
                 String[] respId = (String[])ar.result;
-                mImei = respId[0];
-                mImeiSv = respId[1];
+                if (TextUtils.isEmpty(mImei)) {
+                    mImei = respId[0];
+                    mImeiSv = respId[1];
+                }
                 mEsn  =  respId[2];
                 mMeid =  respId[3];
                 // some modems return all 0's instead of null/empty string when MEID is unavailable
@@ -3223,7 +3441,38 @@
                 logd("EVENT_SUBSCRIPTIONS_CHANGED");
                 updateUsageSetting();
                 break;
+            case EVENT_SET_NULL_CIPHER_AND_INTEGRITY_DONE:
+                logd("EVENT_SET_NULL_CIPHER_AND_INTEGRITY_DONE");
+                ar = (AsyncResult) msg.obj;
+                if (ar == null || ar.exception == null) {
+                    mIsNullCipherAndIntegritySupported = true;
+                    return;
+                }
+                CommandException.Error error = ((CommandException) ar.exception).getCommandError();
+                mIsNullCipherAndIntegritySupported = !error.equals(
+                        CommandException.Error.REQUEST_NOT_SUPPORTED);
+                break;
 
+            case EVENT_IMS_DEREGISTRATION_TRIGGERED:
+                logd("EVENT_IMS_DEREGISTRATION_TRIGGERED");
+                ar = (AsyncResult) msg.obj;
+                if (ar.exception == null) {
+                    mImsPhone.triggerImsDeregistration(((int[]) ar.result)[0]);
+                } else {
+                    Rlog.e(LOG_TAG, "Unexpected unsol with exception", ar.exception);
+                }
+                break;
+
+            case EVENT_TRIGGER_NOTIFY_ANBR:
+                logd("EVENT_TRIGGER_NOTIFY_ANBR");
+                ar = (AsyncResult) msg.obj;
+                if (ar.exception == null) {
+                    if (mImsPhone != null) {
+                        mImsPhone.triggerNotifyAnbr(((int[]) ar.result)[0], ((int[]) ar.result)[1],
+                                ((int[]) ar.result)[2]);
+                    }
+                }
+                break;
             default:
                 super.handleMessage(msg);
         }
@@ -4073,6 +4322,7 @@
     @Override
     public void setImsRegistrationState(boolean registered) {
         mSST.setImsRegistrationState(registered);
+        mCallWaitingController.setImsRegistrationState(registered);
     }
 
     @Override
@@ -4135,6 +4385,12 @@
                         + ServiceState.rilServiceStateToString(mTelecomVoiceServiceStateOverride)
                         + ")");
         pw.flush();
+        try {
+            mCallWaitingController.dump(pw);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        pw.flush();
     }
 
     @Override
@@ -4253,7 +4509,7 @@
         if (subInfo == null || TextUtils.isEmpty(subInfo.getCountryIso())) {
             return null;
         }
-        return subInfo.getCountryIso().toUpperCase();
+        return subInfo.getCountryIso().toUpperCase(Locale.ROOT);
     }
 
     public void notifyEcbmTimerReset(Boolean flag) {
@@ -4359,6 +4615,27 @@
         mVolteSilentRedialRegistrants.notifyRegistrants(ar);
     }
 
+    /** {@inheritDoc} */
+    @Override
+    public void registerForEmergencyDomainSelected(
+            @NonNull Handler h, int what, @Nullable Object obj) {
+        mEmergencyDomainSelectedRegistrants.addUnique(h, what, obj);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void unregisterForEmergencyDomainSelected(@NonNull Handler h) {
+        mEmergencyDomainSelectedRegistrants.remove(h);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void notifyEmergencyDomainSelected(@TransportType int transportType) {
+        logd("notifyEmergencyDomainSelected transportType=" + transportType);
+        mEmergencyDomainSelectedRegistrants.notifyRegistrants(
+                new AsyncResult(null, transportType, null));
+    }
+
     /**
      * Sets the SIM voice message waiting indicator records.
      * @param line GSM Subscriber Profile Number, one-based. Only '1' is supported
@@ -4531,18 +4808,28 @@
             return;
         }
 
-        SubscriptionInfo info = SubscriptionController.getInstance().getSubInfoForIccId(
-                IccUtils.stripTrailingFs(iccId));
+        SubscriptionInfo info;
+        if (isSubscriptionManagerServiceEnabled()) {
+            info = mSubscriptionManagerService
+                    .getAllSubInfoList(mContext.getOpPackageName(), mContext.getAttributionTag())
+                    .stream()
+                    .filter(subInfo -> subInfo.getIccId().equals(IccUtils.stripTrailingFs(iccId)))
+                    .findFirst()
+                    .orElse(null);
+        } else {
+            info = SubscriptionController.getInstance().getSubInfoForIccId(
+                    IccUtils.stripTrailingFs(iccId));
+        }
 
         // If info is null, it could be a new subscription. By default we enable it.
-        boolean expectedValue = info == null ? true : info.areUiccApplicationsEnabled();
+        boolean expectedValue = info == null || info.areUiccApplicationsEnabled();
 
         // If for any reason current state is different from configured state, re-apply the
         // configured state.
         if (expectedValue != mUiccApplicationsEnabled) {
             mCi.enableUiccApplications(expectedValue, Message.obtain(
                     this, EVENT_REAPPLY_UICC_APPS_ENABLEMENT_DONE,
-                    new Pair<Boolean, Integer>(expectedValue, retries)));
+                    new Pair<>(expectedValue, retries)));
         }
     }
 
@@ -4634,13 +4921,19 @@
         boolean mDefaultVonr =
                 config.getBoolean(CarrierConfigManager.KEY_VONR_ON_BY_DEFAULT_BOOL);
 
-        String result = SubscriptionController.getInstance().getSubscriptionProperty(
-                getSubId(),
-                SubscriptionManager.NR_ADVANCED_CALLING_ENABLED);
-
         int setting = -1;
-        if (result != null) {
-            setting = Integer.parseInt(result);
+        if (isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = mSubscriptionManagerService
+                    .getSubscriptionInfoInternal(getSubId());
+            if (subInfo != null) {
+                setting = subInfo.getNrAdvancedCallingEnabled();
+            }
+        } else {
+            String result = SubscriptionController.getInstance().getSubscriptionProperty(
+                    getSubId(), SubscriptionManager.NR_ADVANCED_CALLING_ENABLED);
+            if (result != null) {
+                setting = Integer.parseInt(result);
+            }
         }
 
         logd("VoNR setting from telephony.db:"
@@ -4650,15 +4943,9 @@
                 + " ,vonr_on_by_default_bool:"
                 + mDefaultVonr);
 
-        if (!mIsVonrEnabledByCarrier) {
-            mCi.setVoNrEnabled(false, obtainMessage(EVENT_SET_VONR_ENABLED_DONE), null);
-        } else if (setting == -1) {
-            mCi.setVoNrEnabled(mDefaultVonr, obtainMessage(EVENT_SET_VONR_ENABLED_DONE), null);
-        } else if (setting == 1) {
-            mCi.setVoNrEnabled(true, obtainMessage(EVENT_SET_VONR_ENABLED_DONE), null);
-        } else if (setting == 0) {
-            mCi.setVoNrEnabled(false, obtainMessage(EVENT_SET_VONR_ENABLED_DONE), null);
-        }
+        boolean enbleVonr = mIsVonrEnabledByCarrier
+                && (setting == 1 || (setting == -1 && mDefaultVonr));
+        mCi.setVoNrEnabled(enbleVonr, obtainMessage(EVENT_SET_VONR_ENABLED_DONE), null);
     }
 
     private void updateCdmaRoamingSettingsAfterCarrierConfigChanged(PersistableBundle config) {
@@ -4720,4 +5007,49 @@
     public InboundSmsHandler getInboundSmsHandler(boolean is3gpp2) {
         return mIccSmsInterfaceManager.getInboundSmsHandler(is3gpp2);
     }
+
+    /**
+     * Return current cell broadcast ranges.
+     */
+    public List<CellBroadcastIdRange> getCellBroadcastIdRanges() {
+        return mCellBroadcastConfigTracker.getCellBroadcastIdRanges();
+    }
+
+    /**
+     * Set reception of cell broadcast messages with the list of the given ranges.
+     */
+    @Override
+    public void setCellBroadcastIdRanges(
+            @NonNull List<CellBroadcastIdRange> ranges, Consumer<Integer> callback) {
+        mCellBroadcastConfigTracker.setCellBroadcastIdRanges(ranges, callback);
+    }
+
+    /**
+     * The following function checks if supplementary service request is blocked due to FDN.
+     * @param requestType request type associated with the supplementary service
+     * @param serviceType supplementary service type
+     * @return {@code true} if request is blocked due to FDN.
+     */
+    private boolean isRequestBlockedByFDN(SsData.RequestType requestType,
+            SsData.ServiceType serviceType) {
+        ArrayList<String> controlStrings = GsmMmiCode.getControlStrings(requestType, serviceType);
+        return FdnUtils.isSuppServiceRequestBlockedByFdn(mPhoneId, controlStrings, getCountryIso());
+    }
+
+    @Override
+    public void handleNullCipherEnabledChange() {
+        if (!DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_CELLULAR_SECURITY,
+                TelephonyManager.PROPERTY_ENABLE_NULL_CIPHER_TOGGLE, false)) {
+            logi("Not handling null cipher update. Feature disabled by DeviceConfig.");
+            return;
+        }
+        mCi.setNullCipherAndIntegrityEnabled(
+                getNullCipherAndIntegrityEnabledPreference(),
+                obtainMessage(EVENT_SET_NULL_CIPHER_AND_INTEGRITY_DONE));
+    }
+
+    @Override
+    public boolean isNullCipherAndIntegritySupported() {
+        return mIsNullCipherAndIntegritySupported;
+    }
 }
diff --git a/src/java/com/android/internal/telephony/HalVersion.java b/src/java/com/android/internal/telephony/HalVersion.java
index f83d790..c05111b 100644
--- a/src/java/com/android/internal/telephony/HalVersion.java
+++ b/src/java/com/android/internal/telephony/HalVersion.java
@@ -25,6 +25,9 @@
  */
 public class HalVersion implements Comparable<HalVersion> {
 
+    /** The HAL Version indicating that the version is unsupported */
+    public static final HalVersion UNSUPPORTED = new HalVersion(-2, -2);
+
     /** The HAL Version indicating that the version is unknown or invalid */
     public static final HalVersion UNKNOWN = new HalVersion(-1, -1);
 
diff --git a/src/java/com/android/internal/telephony/IccPhoneBookInterfaceManager.java b/src/java/com/android/internal/telephony/IccPhoneBookInterfaceManager.java
index 742cc90..ab62aa4 100644
--- a/src/java/com/android/internal/telephony/IccPhoneBookInterfaceManager.java
+++ b/src/java/com/android/internal/telephony/IccPhoneBookInterfaceManager.java
@@ -39,6 +39,7 @@
 import com.android.telephony.Rlog;
 
 import java.util.List;
+import java.util.Locale;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
@@ -337,7 +338,10 @@
         }
 
         efid = updateEfForIccType(efid);
-        if (DBG) logd("getAdnRecordsInEF: efid=0x" + Integer.toHexString(efid).toUpperCase());
+        if (DBG) {
+            logd("getAdnRecordsInEF: efid=0x" + Integer.toHexString(efid)
+                    .toUpperCase(Locale.ROOT));
+        }
 
         checkThread();
         Request loadRequest = new Request();
diff --git a/src/java/com/android/internal/telephony/IccProvider.java b/src/java/com/android/internal/telephony/IccProvider.java
index 7a128c0..b7c7e7b 100644
--- a/src/java/com/android/internal/telephony/IccProvider.java
+++ b/src/java/com/android/internal/telephony/IccProvider.java
@@ -37,6 +37,7 @@
 import com.android.telephony.Rlog;
 
 import java.util.List;
+import java.util.Locale;
 
 /**
  * {@hide}
@@ -424,7 +425,7 @@
 
     private MatrixCursor loadFromEf(int efType, int subId) {
         if (DBG) log("loadFromEf: efType=0x" +
-                Integer.toHexString(efType).toUpperCase() + ", subscription=" + subId);
+                Integer.toHexString(efType).toUpperCase(Locale.ROOT) + ", subscription=" + subId);
 
         List<AdnRecord> adnRecords = null;
         try {
diff --git a/src/java/com/android/internal/telephony/IccSmsInterfaceManager.java b/src/java/com/android/internal/telephony/IccSmsInterfaceManager.java
index d3e6a0d..cdcf344 100644
--- a/src/java/com/android/internal/telephony/IccSmsInterfaceManager.java
+++ b/src/java/com/android/internal/telephony/IccSmsInterfaceManager.java
@@ -176,6 +176,41 @@
         mSmsPermissions = smsPermissions;
     }
 
+    /**
+     * PhoneFactory Dependencies for testing.
+     */
+    @VisibleForTesting
+    public interface PhoneFactoryProxy {
+        Phone getPhone(int index);
+        Phone getDefaultPhone();
+        Phone[] getPhones();
+    }
+
+    private PhoneFactoryProxy mPhoneFactoryProxy = new PhoneFactoryProxy() {
+        @Override
+        public Phone getPhone(int index) {
+            return PhoneFactory.getPhone(index);
+        }
+
+        @Override
+        public Phone getDefaultPhone() {
+            return PhoneFactory.getDefaultPhone();
+        }
+
+        @Override
+        public Phone[] getPhones() {
+            return PhoneFactory.getPhones();
+        }
+    };
+
+    /**
+     * Overrides PhoneFactory dependencies for testing.
+     */
+    @VisibleForTesting
+    public void setPhoneFactoryProxy(PhoneFactoryProxy proxy) {
+        mPhoneFactoryProxy = proxy;
+    }
+
     private void enforceNotOnHandlerThread(String methodName) {
         if (Looper.myLooper() == mHandler.getLooper()) {
             throw new RuntimeException("This method " + methodName + " will deadlock if called from"
@@ -457,11 +492,11 @@
      */
     public void sendText(String callingPackage, String destAddr, String scAddr,
             String text, PendingIntent sentIntent, PendingIntent deliveryIntent,
-            boolean persistMessageForNonDefaultSmsApp, long messageId) {
+            boolean persistMessageForNonDefaultSmsApp, long messageId, boolean skipShortCodeCheck) {
         sendTextInternal(callingPackage, destAddr, scAddr, text, sentIntent, deliveryIntent,
                 persistMessageForNonDefaultSmsApp, SMS_MESSAGE_PRIORITY_NOT_SPECIFIED,
                 false /* expectMore */, SMS_MESSAGE_PERIOD_NOT_SPECIFIED, false /* isForVvm */,
-                messageId);
+                messageId, skipShortCodeCheck);
     }
 
     /**
@@ -481,6 +516,16 @@
                 SMS_MESSAGE_PERIOD_NOT_SPECIFIED, isForVvm, 0L /* messageId */);
     }
 
+
+    private void sendTextInternal(String callingPackage, String destAddr, String scAddr,
+            String text, PendingIntent sentIntent, PendingIntent deliveryIntent,
+            boolean persistMessageForNonDefaultSmsApp, int priority, boolean expectMore,
+            int validityPeriod, boolean isForVvm, long messageId) {
+        sendTextInternal(callingPackage, destAddr, scAddr, text, sentIntent, deliveryIntent,
+                persistMessageForNonDefaultSmsApp, priority, expectMore, validityPeriod, isForVvm,
+                messageId, false);
+    }
+
     /**
      * Send a text based SMS.
      *
@@ -527,12 +572,13 @@
      *  Any Other values including negative considered as Invalid Validity Period of the message.
      * @param messageId An id that uniquely identifies the message requested to be sent.
      *                 Used for logging and diagnostics purposes. The id may be 0.
+     * @param skipShortCodeCheck Skip check for short code type destination address.
      */
 
     private void sendTextInternal(String callingPackage, String destAddr, String scAddr,
             String text, PendingIntent sentIntent, PendingIntent deliveryIntent,
             boolean persistMessageForNonDefaultSmsApp, int priority, boolean expectMore,
-            int validityPeriod, boolean isForVvm, long messageId) {
+            int validityPeriod, boolean isForVvm, long messageId, boolean skipShortCodeCheck) {
         if (Rlog.isLoggable("SMS", Log.VERBOSE)) {
             log("sendText: destAddr=" + destAddr + " scAddr=" + scAddr
                     + " text='" + text + "' sentIntent=" + sentIntent + " deliveryIntent="
@@ -544,7 +590,7 @@
         destAddr = filterDestAddress(destAddr);
         mDispatchersController.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent,
                 null/*messageUri*/, callingPackage, persistMessageForNonDefaultSmsApp,
-                priority, expectMore, validityPeriod, isForVvm, messageId);
+                priority, expectMore, validityPeriod, isForVvm, messageId, skipShortCodeCheck);
     }
 
     /**
@@ -1452,11 +1498,27 @@
         return null;
     }
 
-    private void notifyIfOutgoingEmergencySms(String destAddr) {
+    @VisibleForTesting
+    public void notifyIfOutgoingEmergencySms(String destAddr) {
+        Phone[] allPhones = mPhoneFactoryProxy.getPhones();
         EmergencyNumber emergencyNumber = mPhone.getEmergencyNumberTracker().getEmergencyNumber(
                 destAddr);
         if (emergencyNumber != null) {
             mPhone.notifyOutgoingEmergencySms(emergencyNumber);
+        } else if (allPhones.length > 1) {
+            // If there are multiple active SIMs, check all instances:
+            for (Phone phone : allPhones) {
+                // If the current iteration was already checked, skip:
+                if (phone.getPhoneId() == mPhone.getPhoneId()) {
+                    continue;
+                }
+                emergencyNumber = phone.getEmergencyNumberTracker()
+                        .getEmergencyNumber(destAddr);
+                if (emergencyNumber != null) {
+                    mPhone.notifyOutgoingEmergencySms(emergencyNumber);
+                    break;
+                }
+            }
         }
     }
 
diff --git a/src/java/com/android/internal/telephony/ImsIndication.java b/src/java/com/android/internal/telephony/ImsIndication.java
new file mode 100644
index 0000000..00652f8
--- /dev/null
+++ b/src/java/com/android/internal/telephony/ImsIndication.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import static android.telephony.TelephonyManager.HAL_SERVICE_IMS;
+
+import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CONNECTION_SETUP_FAILURE;
+import static com.android.internal.telephony.RILConstants.RIL_UNSOL_NOTIFY_ANBR;
+import static com.android.internal.telephony.RILConstants.RIL_UNSOL_TRIGGER_IMS_DEREGISTRATION;
+
+import android.hardware.radio.ims.IRadioImsIndication;
+import android.os.AsyncResult;
+import android.telephony.ims.feature.ConnectionFailureInfo;
+
+/**
+ * Interface declaring unsolicited radio indications for IMS APIs.
+ */
+public class ImsIndication extends IRadioImsIndication.Stub {
+    private final RIL mRil;
+
+    public ImsIndication(RIL ril) {
+        mRil = ril;
+    }
+
+    @Override
+    public String getInterfaceHash() {
+        return IRadioImsIndication.HASH;
+    }
+
+    @Override
+    public int getInterfaceVersion() {
+        return IRadioImsIndication.VERSION;
+    }
+
+    /**
+     * Fired by radio when any IMS traffic is not sent to network due to any failure
+     * on cellular networks.
+     *
+     * @param indicationType Type of radio indication.
+     * @param token The token provided by {@link #startImsTraffic}.
+     * @param failureInfo Connection failure information.
+     */
+    public void onConnectionSetupFailure(int indicationType, int token,
+            android.hardware.radio.ims.ConnectionFailureInfo failureInfo) {
+        mRil.processIndication(HAL_SERVICE_IMS, indicationType);
+
+        Object[] response = new Object[2];
+        response[0] = token;
+        response[1] = new ConnectionFailureInfo(
+                RILUtils.convertHalConnectionFailureReason(failureInfo.failureReason),
+                failureInfo.causeCode, failureInfo.waitTimeMillis);
+
+        if (RIL.RILJ_LOGD) mRil.unsljLogRet(RIL_UNSOL_CONNECTION_SETUP_FAILURE, response);
+
+        mRil.mConnectionSetupFailureRegistrants.notifyRegistrants(
+                new AsyncResult(null, response, null));
+    }
+
+    /**
+     * Fired by radio when ANBR is received form the network.
+     *
+     * @param indicationType Type of radio indication.
+     * @param qosSessionId QoS session ID is used to identify media stream such as audio or video.
+     * @param imsdirection Direction of this packet stream (e.g. uplink or downlink).
+     * @param bitsPerSecond The recommended bit rate for the UE
+     *        for a specific logical channel and a specific direction by the network.
+     */
+    public void notifyAnbr(int indicationType, int qosSessionId, int imsdirection,
+            int bitsPerSecond) {
+        mRil.processIndication(HAL_SERVICE_IMS, indicationType);
+
+        int[] response = new int[3];
+        response[0] = qosSessionId;
+        response[1] = imsdirection;
+        response[2] = bitsPerSecond;
+
+        if (RIL.RILJ_LOGD) mRil.unsljLogRet(RIL_UNSOL_NOTIFY_ANBR, response);
+
+        mRil.mNotifyAnbrRegistrants.notifyRegistrants(new AsyncResult(null, response, null));
+    }
+
+    /**
+     * Requests IMS stack to perform graceful IMS deregistration before radio performing
+     * network detach in the events of SIM remove, refresh or and so on. The radio waits for
+     * the IMS deregistration, which will be notified by telephony via
+     * {@link IRadioIms#updateImsRegistrationInfo()}, or a certain timeout interval to start
+     * the network detach procedure.
+     *
+     * @param indicationType Type of radio indication.
+     * @param reason the reason why the deregistration is triggered.
+     */
+    public void triggerImsDeregistration(int indicationType, int reason) {
+        mRil.processIndication(HAL_SERVICE_IMS, indicationType);
+
+        if (RIL.RILJ_LOGD) mRil.unsljLogRet(RIL_UNSOL_TRIGGER_IMS_DEREGISTRATION, reason);
+
+        int[] response = new int[1];
+        response[0] = RILUtils.convertHalDeregistrationReason(reason);
+
+        mRil.mTriggerImsDeregistrationRegistrants.notifyRegistrants(
+                new AsyncResult(null, response, null));
+    }
+}
diff --git a/src/java/com/android/internal/telephony/ImsResponse.java b/src/java/com/android/internal/telephony/ImsResponse.java
new file mode 100644
index 0000000..1adc000
--- /dev/null
+++ b/src/java/com/android/internal/telephony/ImsResponse.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import static android.telephony.TelephonyManager.HAL_SERVICE_IMS;
+import static android.telephony.ims.feature.MmTelFeature.ImsTrafficSessionCallbackWrapper.INVALID_TOKEN;
+
+import android.hardware.radio.RadioError;
+import android.hardware.radio.RadioResponseInfo;
+import android.hardware.radio.ims.IRadioImsResponse;
+import android.telephony.ims.feature.ConnectionFailureInfo;
+
+/**
+ * Interface declaring response functions to solicited radio requests for IMS APIs.
+ */
+public class ImsResponse extends IRadioImsResponse.Stub {
+    private final RIL mRil;
+
+    public ImsResponse(RIL ril) {
+        mRil = ril;
+    }
+
+    @Override
+    public String getInterfaceHash() {
+        return IRadioImsResponse.HASH;
+    }
+
+    @Override
+    public int getInterfaceVersion() {
+        return IRadioImsResponse.VERSION;
+    }
+
+    /**
+     * @param info Response info struct containing response type, serial no. and error.
+     */
+    public void setSrvccCallInfoResponse(RadioResponseInfo info) {
+        RadioResponse.responseVoid(HAL_SERVICE_IMS, mRil, info);
+    }
+
+    /**
+     * @param info Response info struct containing response type, serial no. and error.
+     */
+    public void updateImsRegistrationInfoResponse(RadioResponseInfo info) {
+        RadioResponse.responseVoid(HAL_SERVICE_IMS, mRil, info);
+    }
+
+    /**
+     * @param responseInfo Response info struct containing response type, serial no. and error.
+     * @param failureInfo Failure information.
+     */
+    public void startImsTrafficResponse(RadioResponseInfo responseInfo,
+            android.hardware.radio.ims.ConnectionFailureInfo failureInfo) {
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_IMS, responseInfo);
+
+        if (rr != null) {
+            Object[] response = { new Integer(INVALID_TOKEN), null };
+            if (responseInfo.error == RadioError.NONE) {
+                if (failureInfo != null) {
+                    response[1] = new ConnectionFailureInfo(
+                            RILUtils.convertHalConnectionFailureReason(failureInfo.failureReason),
+                            failureInfo.causeCode, failureInfo.waitTimeMillis);
+                }
+                RadioResponse.sendMessageResponse(rr.mResult, response);
+            }
+            mRil.processResponseDone(rr, responseInfo, response);
+        }
+    }
+
+    /**
+     * @param info Response info struct containing response type, serial no. and error.
+     */
+    public void stopImsTrafficResponse(RadioResponseInfo info) {
+        RadioResponse.responseVoid(HAL_SERVICE_IMS, mRil, info);
+    }
+
+    /**
+     * @param info Response info struct containing response type, serial no. and error.
+     */
+    public void triggerEpsFallbackResponse(RadioResponseInfo info) {
+        RadioResponse.responseVoid(HAL_SERVICE_IMS, mRil, info);
+    }
+
+    /**
+     * @param info Response info struct containing response type, serial no. and error.
+     */
+    public void sendAnbrQueryResponse(RadioResponseInfo info) {
+        RadioResponse.responseVoid(HAL_SERVICE_IMS, mRil, info);
+    }
+
+    /**
+     * @param info Response info struct containing response type, serial no. and error.
+     */
+    public void updateImsCallStatusResponse(RadioResponseInfo info) {
+        RadioResponse.responseVoid(HAL_SERVICE_IMS, mRil, info);
+    }
+}
diff --git a/src/java/com/android/internal/telephony/ImsSmsDispatcher.java b/src/java/com/android/internal/telephony/ImsSmsDispatcher.java
index 42fe7a7..c6a17a6 100644
--- a/src/java/com/android/internal/telephony/ImsSmsDispatcher.java
+++ b/src/java/com/android/internal/telephony/ImsSmsDispatcher.java
@@ -16,6 +16,7 @@
 
 package com.android.internal.telephony;
 
+import android.app.Activity;
 import android.content.Context;
 import android.os.Binder;
 import android.os.Message;
@@ -58,6 +59,7 @@
 
     private static final String TAG = "ImsSmsDispatcher";
     private static final int CONNECT_DELAY_MS = 5000; // 5 seconds;
+    public static final int MAX_SEND_RETRIES_OVER_IMS = MAX_SEND_RETRIES;
 
     /**
      * Creates FeatureConnector instances for ImsManager, used during testing to inject mock
@@ -170,10 +172,19 @@
                         mTrackers.remove(token);
                         break;
                     case ImsSmsImplBase.SEND_STATUS_ERROR_RETRY:
-                        if (tracker.mRetryCount < MAX_SEND_RETRIES) {
+                        int maxRetryCountOverIms = getMaxRetryCountOverIms();
+                        if (tracker.mRetryCount < getMaxSmsRetryCount()) {
+                            if (maxRetryCountOverIms < getMaxSmsRetryCount()
+                                    && tracker.mRetryCount >= maxRetryCountOverIms) {
+                                tracker.mRetryCount += 1;
+                                mTrackers.remove(token);
+                                fallbackToPstn(tracker);
+                                break;
+                            }
                             tracker.mRetryCount += 1;
                             sendMessageDelayed(
-                                    obtainMessage(EVENT_SEND_RETRY, tracker), SEND_RETRY_DELAY);
+                                    obtainMessage(EVENT_SEND_RETRY, tracker),
+                                    getSmsRetryDelayValue());
                         } else {
                             tracker.onFailed(mContext, reason, networkReasonCode);
                             mTrackers.remove(token);
@@ -193,6 +204,7 @@
                         SmsConstants.FORMAT_3GPP2.equals(getFormat()),
                         status == ImsSmsImplBase.SEND_STATUS_ERROR_FALLBACK,
                         reason,
+                        networkReasonCode,
                         tracker.mMessageId,
                         tracker.isFromDefaultSmsApplication(mContext),
                         tracker.getInterval());
@@ -248,6 +260,10 @@
                             mappedResult =
                                     ImsSmsImplBase.DELIVER_STATUS_ERROR_REQUEST_NOT_SUPPORTED;
                             break;
+                        case Activity.RESULT_OK:
+                            // class2 message saving to SIM operation is in progress, defer ack
+                            // until saving to SIM is success/failure
+                            return;
                         default:
                             mappedResult = ImsSmsImplBase.DELIVER_STATUS_ERROR_GENERIC;
                             break;
@@ -263,7 +279,7 @@
                     } catch (ImsException e) {
                         loge("Failed to acknowledgeSms(). Error: " + e.getMessage());
                     }
-                }, true /* ignoreClass */, true /* isOverIms */);
+                }, true /* ignoreClass */, true /* isOverIms */, token);
             } finally {
                 Binder.restoreCallingIdentity(identity);
             }
@@ -295,6 +311,9 @@
                             mImsManager = manager;
                             setListeners();
                             mIsImsServiceUp = true;
+
+                            /* set ImsManager */
+                            mSmsDispatchersController.setImsManager(mImsManager);
                         }
                     }
 
@@ -309,6 +328,9 @@
                         synchronized (mLock) {
                             mImsManager = null;
                             mIsImsServiceUp = false;
+
+                            /* unset ImsManager */
+                            mSmsDispatchersController.setImsManager(null);
                         }
                     }
                 }, this::post);
@@ -394,6 +416,81 @@
     }
 
     @Override
+    public int getMaxSmsRetryCount() {
+        int retryCount = MAX_SEND_RETRIES;
+        CarrierConfigManager mConfigManager;
+
+        mConfigManager = (CarrierConfigManager)  mContext.getSystemService(
+                Context.CARRIER_CONFIG_SERVICE);
+
+        if (mConfigManager != null) {
+            PersistableBundle carrierConfig = mConfigManager.getConfigForSubId(
+                    getSubId());
+
+            if (carrierConfig != null) {
+                retryCount = carrierConfig.getInt(
+                        CarrierConfigManager.ImsSms.KEY_SMS_MAX_RETRY_COUNT_INT);
+            }
+        }
+
+        Rlog.d(TAG, "Retry Count: " + retryCount);
+
+        return retryCount;
+    }
+
+    /**
+     * Returns the number of times SMS can be sent over IMS
+     *
+     * @return  retry count over Ims from  carrier configuration
+     */
+    @VisibleForTesting
+    public int getMaxRetryCountOverIms() {
+        int retryCountOverIms = MAX_SEND_RETRIES_OVER_IMS;
+        CarrierConfigManager mConfigManager;
+
+        mConfigManager = (CarrierConfigManager) mContext.getSystemService(Context
+                                                        .CARRIER_CONFIG_SERVICE);
+
+        if (mConfigManager != null) {
+            PersistableBundle carrierConfig = mConfigManager.getConfigForSubId(
+                    getSubId());
+
+
+            if (carrierConfig != null) {
+                retryCountOverIms = carrierConfig.getInt(
+                        CarrierConfigManager.ImsSms.KEY_SMS_MAX_RETRY_COUNT_OVER_IMS_INT);
+            }
+        }
+
+        Rlog.d(TAG, "Retry Count Over Ims: " + retryCountOverIms);
+
+        return retryCountOverIms;
+    }
+
+    @Override
+    public int getSmsRetryDelayValue() {
+        int retryDelay = SEND_RETRY_DELAY;
+        CarrierConfigManager mConfigManager;
+
+        mConfigManager = (CarrierConfigManager)  mContext.getSystemService(
+                Context.CARRIER_CONFIG_SERVICE);
+
+        if (mConfigManager != null) {
+            PersistableBundle carrierConfig = mConfigManager.getConfigForSubId(
+                    getSubId());
+
+            if (carrierConfig != null) {
+                retryDelay = carrierConfig.getInt(
+                        CarrierConfigManager.ImsSms.KEY_SMS_OVER_IMS_SEND_RETRY_DELAY_MILLIS_INT);
+            }
+        }
+
+        Rlog.d(TAG, "Retry delay: " + retryDelay);
+
+        return retryDelay;
+    }
+
+    @Override
     protected boolean shouldBlockSmsForEcbm() {
         // We should not block outgoing SMS during ECM on IMS. It only applies to outgoing CDMA
         // SMS.
@@ -416,10 +513,39 @@
     }
 
     @Override
+    protected SmsMessageBase.SubmitPduBase getSubmitPdu(String scAddr, String destAddr,
+            String message, boolean statusReportRequested, SmsHeader smsHeader, int priority,
+            int validityPeriod, int messageRef) {
+        return SMSDispatcherUtil.getSubmitPdu(isCdmaMo(), scAddr, destAddr, message,
+                statusReportRequested, smsHeader, priority, validityPeriod, messageRef);
+    }
+
+    @Override
+    protected SmsMessageBase.SubmitPduBase getSubmitPdu(String scAddr, String destAddr,
+            int destPort, byte[] message, boolean statusReportRequested, int messageRef) {
+        return SMSDispatcherUtil.getSubmitPdu(isCdmaMo(), scAddr, destAddr, destPort, message,
+                statusReportRequested, messageRef);
+    }
+
+    @Override
     protected TextEncodingDetails calculateLength(CharSequence messageBody, boolean use7bitOnly) {
         return SMSDispatcherUtil.calculateLength(isCdmaMo(), messageBody, use7bitOnly);
     }
 
+    /**
+     * Send the Memory Available Event to the ImsService
+     */
+    public void onMemoryAvailable() {
+        logd("onMemoryAvailable ");
+        int token = mNextToken.incrementAndGet();
+        try {
+            logd("onMemoryAvailable: token = " + token);
+            getImsManager().onMemoryAvailable(token);
+        } catch (ImsException e) {
+            loge("onMemoryAvailable failed: " + e.getMessage());
+        }
+    }
+
     @Override
     public void sendSms(SmsTracker tracker) {
         logd("sendSms: "
@@ -489,6 +615,7 @@
 
     @VisibleForTesting
     public void fallbackToPstn(SmsTracker tracker) {
+        tracker.mMessageRef = nextMessageRef();
         mSmsDispatchersController.sendRetrySms(tracker);
     }
 
diff --git a/src/java/com/android/internal/telephony/InboundSmsHandler.java b/src/java/com/android/internal/telephony/InboundSmsHandler.java
index 8f43dce..9166719 100644
--- a/src/java/com/android/internal/telephony/InboundSmsHandler.java
+++ b/src/java/com/android/internal/telephony/InboundSmsHandler.java
@@ -45,7 +45,6 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.pm.IPackageManager;
 import android.content.pm.PackageManager;
 import android.database.Cursor;
 import android.database.SQLException;
@@ -53,11 +52,10 @@
 import android.os.AsyncResult;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Looper;
 import android.os.Message;
 import android.os.PowerManager;
 import android.os.PowerWhitelistManager;
-import android.os.RemoteException;
-import android.os.ServiceManager;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.os.storage.StorageManager;
@@ -185,6 +183,7 @@
     /** BroadcastReceiver timed out waiting for an intent */
     public static final int EVENT_RECEIVER_TIMEOUT = 9;
 
+
     /** Wakelock release delay when returning to idle state. */
     private static final int WAKELOCK_TIMEOUT = 3000;
 
@@ -289,8 +288,8 @@
      * @param storageMonitor the SmsStorageMonitor to check for storage availability
      */
     protected InboundSmsHandler(String name, Context context, SmsStorageMonitor storageMonitor,
-            Phone phone) {
-        super(name);
+            Phone phone, Looper looper) {
+        super(name, looper);
 
         mContext = context;
         mStorageMonitor = storageMonitor;
@@ -501,7 +500,6 @@
                 case EVENT_RETURN_TO_IDLE:
                     // already in idle state; ignore
                     return HANDLED;
-
                 case EVENT_BROADCAST_COMPLETE:
                 case EVENT_START_ACCEPTING_SMS:
                 default:
@@ -540,7 +538,8 @@
 
                 case EVENT_INJECT_SMS:
                     // handle new injected SMS
-                    handleInjectSms((AsyncResult) msg.obj, msg.arg1 == 1 /* isOverIms */);
+                    handleInjectSms((AsyncResult) msg.obj, msg.arg1 == 1 /* isOverIms */,
+                            msg.arg2 /* token */);
                     sendMessage(EVENT_RETURN_TO_IDLE);
                     return HANDLED;
 
@@ -663,7 +662,6 @@
             }
         }
     }
-
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private void handleNewSms(AsyncResult ar) {
         if (ar.exception != null) {
@@ -674,7 +672,7 @@
         int result;
         try {
             SmsMessage sms = (SmsMessage) ar.result;
-            result = dispatchMessage(sms.mWrappedSmsMessage, SOURCE_NOT_INJECTED);
+            result = dispatchMessage(sms.mWrappedSmsMessage, SOURCE_NOT_INJECTED, 0 /*unused*/);
         } catch (RuntimeException ex) {
             loge("Exception dispatching message", ex);
             result = RESULT_SMS_DISPATCH_FAILURE;
@@ -693,7 +691,7 @@
      * @param ar is the AsyncResult that has the SMS PDU to be injected.
      */
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
-    private void handleInjectSms(AsyncResult ar, boolean isOverIms) {
+    private void handleInjectSms(AsyncResult ar, boolean isOverIms, int token) {
         int result;
         SmsDispatchersController.SmsInjectionCallback callback = null;
         try {
@@ -705,7 +703,7 @@
             } else {
                 @SmsSource int smsSource =
                         isOverIms ? SOURCE_INJECTED_FROM_IMS : SOURCE_INJECTED_FROM_UNKNOWN;
-                result = dispatchMessage(sms.mWrappedSmsMessage, smsSource);
+                result = dispatchMessage(sms.mWrappedSmsMessage, smsSource, token);
             }
         } catch (RuntimeException ex) {
             loge("Exception dispatching message", ex);
@@ -726,7 +724,7 @@
      * @return a result code from {@link android.provider.Telephony.Sms.Intents},
      *  or {@link Activity#RESULT_OK} for delayed acknowledgment to SMSC
      */
-    private int dispatchMessage(SmsMessageBase smsb, @SmsSource int smsSource) {
+    private int dispatchMessage(SmsMessageBase smsb, @SmsSource int smsSource, int token) {
         // If sms is null, there was a parsing error.
         if (smsb == null) {
             loge("dispatchSmsMessage: message is null");
@@ -740,20 +738,7 @@
             return Intents.RESULT_SMS_HANDLED;
         }
 
-        // onlyCore indicates if the device is in cryptkeeper
-        boolean onlyCore = false;
-        try {
-            onlyCore = IPackageManager.Stub.asInterface(ServiceManager.getService("package"))
-                    .isOnlyCoreApps();
-        } catch (RemoteException e) {
-        }
-        if (onlyCore) {
-            // Device is unable to receive SMS in encrypted state
-            log("Received a short message in encrypted state. Rejecting.");
-            return Intents.RESULT_SMS_RECEIVED_WHILE_ENCRYPTED;
-        }
-
-        int result = dispatchMessageRadioSpecific(smsb, smsSource);
+        int result = dispatchMessageRadioSpecific(smsb, smsSource, token);
 
         // In case of error, add to metrics. This is not required in case of success, as the
         // data will be tracked when the message is processed (processMessagePart).
@@ -775,7 +760,7 @@
      *  or {@link Activity#RESULT_OK} for delayed acknowledgment to SMSC
      */
     protected abstract int dispatchMessageRadioSpecific(SmsMessageBase smsb,
-            @SmsSource int smsSource);
+            @SmsSource int smsSource, int token);
 
     /**
      * Send an acknowledge message to the SMSC.
@@ -875,7 +860,6 @@
      * <code>RESULT_SMS_DISPATCH_FAILURE</code><br>
      * <code>RESULT_SMS_NULL_PDU</code><br>
      * <code>RESULT_SMS_NULL_MESSAGE</code><br>
-     * <code>RESULT_SMS_RECEIVED_WHILE_ENCRYPTED</code><br>
      * <code>RESULT_SMS_DATABASE_ERROR</code><br>
      * <code>RESULT_SMS_INVALID_URI</code><br>
      */
@@ -1159,7 +1143,7 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private void showNewMessageNotification() {
         // Do not show the notification on non-FBE devices.
-        if (!StorageManager.isFileEncryptedNativeOrEmulated()) {
+        if (!StorageManager.isFileEncrypted()) {
             return;
         }
         log("Show new message notification.");
@@ -1453,12 +1437,14 @@
             intent.putExtra("messageId", messageId);
         }
 
+        UserHandle userHandle = null;
         if (destPort == -1) {
             intent.setAction(Intents.SMS_DELIVER_ACTION);
             // Direct the intent to only the default SMS app. If we can't find a default SMS app
             // then sent it to all broadcast receivers.
-            // We are deliberately delivering to the primary user's default SMS App.
-            ComponentName componentName = SmsApplication.getDefaultSmsApplication(mContext, true);
+            userHandle = TelephonyUtils.getSubscriptionUserHandle(mContext, subId);
+            ComponentName componentName = SmsApplication.getDefaultSmsApplicationAsUser(mContext,
+                    true, userHandle);
             if (componentName != null) {
                 // Deliver SMS message only to this receiver.
                 intent.setComponent(componentName);
@@ -1482,9 +1468,12 @@
             intent.setComponent(null);
         }
 
+        if (userHandle == null) {
+            userHandle = UserHandle.SYSTEM;
+        }
         Bundle options = handleSmsWhitelisting(intent.getComponent(), isClass0);
         dispatchIntent(intent, android.Manifest.permission.RECEIVE_SMS,
-                AppOpsManager.OPSTR_RECEIVE_SMS, options, resultReceiver, UserHandle.SYSTEM, subId);
+                AppOpsManager.OPSTR_RECEIVE_SMS, options, resultReceiver, userHandle, subId);
     }
 
     /**
diff --git a/src/java/com/android/internal/telephony/LocaleTracker.java b/src/java/com/android/internal/telephony/LocaleTracker.java
old mode 100755
new mode 100644
diff --git a/src/java/com/android/internal/telephony/MessagingIndication.java b/src/java/com/android/internal/telephony/MessagingIndication.java
index 96e74cc..5814e3d 100644
--- a/src/java/com/android/internal/telephony/MessagingIndication.java
+++ b/src/java/com/android/internal/telephony/MessagingIndication.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_MESSAGING;
+
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_RESPONSE_CDMA_NEW_SMS;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS;
@@ -47,7 +49,7 @@
      */
     public void cdmaNewSms(int indicationType,
             android.hardware.radio.messaging.CdmaSmsMessage msg) {
-        mRil.processIndication(RIL.MESSAGING_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MESSAGING, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_CDMA_NEW_SMS);
 
@@ -63,7 +65,7 @@
      * @param indicationType Type of radio indication
      */
     public void cdmaRuimSmsStorageFull(int indicationType) {
-        mRil.processIndication(RIL.MESSAGING_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MESSAGING, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL);
 
@@ -82,7 +84,7 @@
      *        BTS as coded in 3GPP 23.041 Section 9.4.2.2
      */
     public void newBroadcastSms(int indicationType, byte[] data) {
-        mRil.processIndication(RIL.MESSAGING_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MESSAGING, indicationType);
 
         if (mRil.isLogOrTrace()) {
             mRil.unsljLogvRet(
@@ -101,7 +103,7 @@
      *        The PDU starts with the SMSC address per TS 27.005 (+CMT:)
      */
     public void newSms(int indicationType, byte[] pdu) {
-        mRil.processIndication(RIL.MESSAGING_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MESSAGING, indicationType);
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_NEW_SMS);
 
         SmsMessageBase smsb = com.android.internal.telephony.gsm.SmsMessage.createFromPdu(pdu);
@@ -117,7 +119,7 @@
      * @param recordNumber Record number on the SIM
      */
     public void newSmsOnSim(int indicationType, int recordNumber) {
-        mRil.processIndication(RIL.MESSAGING_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MESSAGING, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM);
 
@@ -133,7 +135,7 @@
      *        The PDU starts with the SMSC address per TS 27.005 (+CMT:)
      */
     public void newSmsStatusReport(int indicationType, byte[] pdu) {
-        mRil.processIndication(RIL.MESSAGING_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MESSAGING, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT);
 
@@ -149,7 +151,7 @@
      * @param indicationType Type of radio indication
      */
     public void simSmsStorageFull(int indicationType) {
-        mRil.processIndication(RIL.MESSAGING_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MESSAGING, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_SIM_SMS_STORAGE_FULL);
 
diff --git a/src/java/com/android/internal/telephony/MessagingResponse.java b/src/java/com/android/internal/telephony/MessagingResponse.java
index 3dc1d1a..19211e1 100644
--- a/src/java/com/android/internal/telephony/MessagingResponse.java
+++ b/src/java/com/android/internal/telephony/MessagingResponse.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_MESSAGING;
+
 import android.hardware.radio.RadioError;
 import android.hardware.radio.RadioResponseInfo;
 import android.hardware.radio.messaging.IRadioMessagingResponse;
@@ -36,7 +38,7 @@
 
     private void responseSms(RadioResponseInfo responseInfo,
             android.hardware.radio.messaging.SendSmsResult sms) {
-        RILRequest rr = mRil.processResponse(RIL.MESSAGING_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_MESSAGING, responseInfo);
 
         if (rr != null) {
             long messageId = RIL.getOutgoingSmsMessageId(rr.mResult);
@@ -62,35 +64,35 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void acknowledgeIncomingGsmSmsWithPduResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void acknowledgeLastIncomingCdmaSmsResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void acknowledgeLastIncomingGsmSmsResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void deleteSmsOnRuimResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void deleteSmsOnSimResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
@@ -99,7 +101,7 @@
      */
     public void getCdmaBroadcastConfigResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.messaging.CdmaBroadcastSmsConfigInfo[] configs) {
-        RILRequest rr = mRil.processResponse(RIL.MESSAGING_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_MESSAGING, responseInfo);
 
         if (rr != null) {
             int[] ret;
@@ -148,7 +150,7 @@
      */
     public void getGsmBroadcastConfigResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.messaging.GsmBroadcastSmsConfigInfo[] configs) {
-        RILRequest rr = mRil.processResponse(RIL.MESSAGING_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_MESSAGING, responseInfo);
 
         if (rr != null) {
             ArrayList<SmsBroadcastConfigInfo> ret = new ArrayList<>();
@@ -168,14 +170,14 @@
      * @param smsc Short Message Service Center address on the device
      */
     public void getSmscAddressResponse(RadioResponseInfo responseInfo, String smsc) {
-        RadioResponse.responseString(RIL.MESSAGING_SERVICE, mRil, responseInfo, smsc);
+        RadioResponse.responseString(HAL_SERVICE_MESSAGING, mRil, responseInfo, smsc);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void reportSmsMemoryStatusResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
@@ -227,35 +229,35 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setCdmaBroadcastActivationResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setCdmaBroadcastConfigResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setGsmBroadcastActivationResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setGsmBroadcastConfigResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setSmscAddressResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MESSAGING_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MESSAGING, mRil, responseInfo);
     }
 
     /**
@@ -263,7 +265,7 @@
      * @param index record index where the CDMA SMS message is stored
      */
     public void writeSmsToRuimResponse(RadioResponseInfo responseInfo, int index) {
-        RadioResponse.responseInts(RIL.MESSAGING_SERVICE, mRil, responseInfo, index);
+        RadioResponse.responseInts(HAL_SERVICE_MESSAGING, mRil, responseInfo, index);
     }
 
     /**
@@ -271,7 +273,7 @@
      * @param index record index where the message is stored
      */
     public void writeSmsToSimResponse(RadioResponseInfo responseInfo, int index) {
-        RadioResponse.responseInts(RIL.MESSAGING_SERVICE, mRil, responseInfo, index);
+        RadioResponse.responseInts(HAL_SERVICE_MESSAGING, mRil, responseInfo, index);
     }
 
     @Override
diff --git a/src/java/com/android/internal/telephony/MockModem.java b/src/java/com/android/internal/telephony/MockModem.java
index 4266a75..a20e748 100644
--- a/src/java/com/android/internal/telephony/MockModem.java
+++ b/src/java/com/android/internal/telephony/MockModem.java
@@ -16,6 +16,14 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_DATA;
+import static android.telephony.TelephonyManager.HAL_SERVICE_IMS;
+import static android.telephony.TelephonyManager.HAL_SERVICE_MESSAGING;
+import static android.telephony.TelephonyManager.HAL_SERVICE_MODEM;
+import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
+import static android.telephony.TelephonyManager.HAL_SERVICE_SIM;
+import static android.telephony.TelephonyManager.HAL_SERVICE_VOICE;
+
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -34,6 +42,7 @@
     private static final String BIND_IRADIODATA = "android.telephony.mockmodem.iradiodata";
     private static final String BIND_IRADIONETWORK = "android.telephony.mockmodem.iradionetwork";
     private static final String BIND_IRADIOVOICE = "android.telephony.mockmodem.iradiovoice";
+    private static final String BIND_IRADIOIMS = "android.telephony.mockmodem.iradioims";
     private static final String BIND_IRADIOCONFIG = "android.telephony.mockmodem.iradioconfig";
     private static final String PHONE_ID = "phone_id";
 
@@ -42,7 +51,7 @@
     static final int RADIOCONFIG_SERVICE = RIL.MAX_SERVICE_IDX + 1;
 
     static final int BINDER_RETRY_MILLIS = 3 * 100;
-    static final int BINDER_MAX_RETRY = 3;
+    static final int BINDER_MAX_RETRY = 10;
 
     private Context mContext;
     private String mServiceName;
@@ -54,6 +63,7 @@
     private IBinder mDataBinder;
     private IBinder mNetworkBinder;
     private IBinder mVoiceBinder;
+    private IBinder mImsBinder;
     private IBinder mConfigBinder;
     private ServiceConnection mModemServiceConnection;
     private ServiceConnection mSimServiceConnection;
@@ -61,9 +71,11 @@
     private ServiceConnection mDataServiceConnection;
     private ServiceConnection mNetworkServiceConnection;
     private ServiceConnection mVoiceServiceConnection;
+    private ServiceConnection mImsServiceConnection;
     private ServiceConnection mConfigServiceConnection;
 
     private byte mPhoneId;
+    private String mTag;
 
     MockModem(Context context, String serviceName) {
         this(context, serviceName, 0);
@@ -71,6 +83,7 @@
 
     MockModem(Context context, String serviceName, int phoneId) {
         mPhoneId = (byte) phoneId;
+        mTag = TAG + "-" + mPhoneId;
         mContext = context;
         String[] componentInfo = serviceName.split("/", 2);
         mPackageName = componentInfo[0];
@@ -86,20 +99,22 @@
 
         @Override
         public void onServiceConnected(ComponentName name, IBinder binder) {
-            Rlog.d(TAG, "IRadio " + getModuleName(mService) + "  - onServiceConnected");
+            Rlog.d(mTag, "IRadio " + getModuleName(mService) + "  - onServiceConnected");
 
-            if (mService == RIL.MODEM_SERVICE) {
+            if (mService == HAL_SERVICE_MODEM) {
                 mModemBinder = binder;
-            } else if (mService == RIL.SIM_SERVICE) {
+            } else if (mService == HAL_SERVICE_SIM) {
                 mSimBinder = binder;
-            } else if (mService == RIL.MESSAGING_SERVICE) {
+            } else if (mService == HAL_SERVICE_MESSAGING) {
                 mMessagingBinder = binder;
-            } else if (mService == RIL.DATA_SERVICE) {
+            } else if (mService == HAL_SERVICE_DATA) {
                 mDataBinder = binder;
-            } else if (mService == RIL.NETWORK_SERVICE) {
+            } else if (mService == HAL_SERVICE_NETWORK) {
                 mNetworkBinder = binder;
-            } else if (mService == RIL.VOICE_SERVICE) {
+            } else if (mService == HAL_SERVICE_VOICE) {
                 mVoiceBinder = binder;
+            } else if (mService == HAL_SERVICE_IMS) {
+                mImsBinder = binder;
             } else if (mService == RADIOCONFIG_SERVICE) {
                 mConfigBinder = binder;
             }
@@ -107,20 +122,22 @@
 
         @Override
         public void onServiceDisconnected(ComponentName name) {
-            Rlog.d(TAG, "IRadio " + getModuleName(mService) + "  - onServiceDisconnected");
+            Rlog.d(mTag, "IRadio " + getModuleName(mService) + "  - onServiceDisconnected");
 
-            if (mService == RIL.MODEM_SERVICE) {
+            if (mService == HAL_SERVICE_MODEM) {
                 mModemBinder = null;
-            } else if (mService == RIL.SIM_SERVICE) {
+            } else if (mService == HAL_SERVICE_SIM) {
                 mSimBinder = null;
-            } else if (mService == RIL.MESSAGING_SERVICE) {
+            } else if (mService == HAL_SERVICE_MESSAGING) {
                 mMessagingBinder = null;
-            } else if (mService == RIL.DATA_SERVICE) {
+            } else if (mService == HAL_SERVICE_DATA) {
                 mDataBinder = null;
-            } else if (mService == RIL.NETWORK_SERVICE) {
+            } else if (mService == HAL_SERVICE_NETWORK) {
                 mNetworkBinder = null;
-            } else if (mService == RIL.VOICE_SERVICE) {
+            } else if (mService == HAL_SERVICE_VOICE) {
                 mVoiceBinder = null;
+            } else if (mService == HAL_SERVICE_IMS) {
+                mImsBinder = null;
             } else if (mService == RADIOCONFIG_SERVICE) {
                 mConfigBinder = null;
             }
@@ -138,7 +155,7 @@
 
         Intent intent = new Intent();
         intent.setComponent(new ComponentName(mPackageName, mServiceName));
-        intent.setAction(actionName);
+        intent.setAction(actionName + phoneId);
         intent.putExtra(PHONE_ID, phoneId);
 
         status = mContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
@@ -148,18 +165,20 @@
     /** waitForBinder */
     public IBinder getServiceBinder(int service) {
         switch (service) {
-            case RIL.MODEM_SERVICE:
+            case HAL_SERVICE_MODEM:
                 return mModemBinder;
-            case RIL.SIM_SERVICE:
+            case HAL_SERVICE_SIM:
                 return mSimBinder;
-            case RIL.MESSAGING_SERVICE:
+            case HAL_SERVICE_MESSAGING:
                 return mMessagingBinder;
-            case RIL.DATA_SERVICE:
+            case HAL_SERVICE_DATA:
                 return mDataBinder;
-            case RIL.NETWORK_SERVICE:
+            case HAL_SERVICE_NETWORK:
                 return mNetworkBinder;
-            case RIL.VOICE_SERVICE:
+            case HAL_SERVICE_VOICE:
                 return mVoiceBinder;
+            case HAL_SERVICE_IMS:
+                return mImsBinder;
             case RADIOCONFIG_SERVICE:
                 return mConfigBinder;
             default:
@@ -183,95 +202,109 @@
                 boolean status =
                         bindModuleToMockModemService(BIND_IRADIOCONFIG, mConfigServiceConnection);
                 if (!status) {
-                    Rlog.d(TAG, "IRadio Config bind fail");
+                    Rlog.d(mTag, "IRadio Config bind fail");
                     mConfigServiceConnection = null;
                 }
             } else {
-                Rlog.d(TAG, "IRadio Config is bound");
+                Rlog.d(mTag, "IRadio Config is bound");
             }
-        } else if (service == RIL.MODEM_SERVICE) {
+        } else if (service == HAL_SERVICE_MODEM) {
             if (mModemBinder == null) {
-                mModemServiceConnection = new MockModemConnection(RIL.MODEM_SERVICE);
+                mModemServiceConnection = new MockModemConnection(HAL_SERVICE_MODEM);
 
                 boolean status =
                         bindModuleToMockModemService(
                                 mPhoneId, BIND_IRADIOMODEM, mModemServiceConnection);
                 if (!status) {
-                    Rlog.d(TAG, "IRadio Modem bind fail");
+                    Rlog.d(mTag, "IRadio Modem bind fail");
                     mModemServiceConnection = null;
                 }
             } else {
-                Rlog.d(TAG, "IRadio Modem is bound");
+                Rlog.d(mTag, "IRadio Modem is bound");
             }
-        } else if (service == RIL.SIM_SERVICE) {
+        } else if (service == HAL_SERVICE_SIM) {
             if (mSimBinder == null) {
-                mSimServiceConnection = new MockModemConnection(RIL.SIM_SERVICE);
+                mSimServiceConnection = new MockModemConnection(HAL_SERVICE_SIM);
 
                 boolean status =
                         bindModuleToMockModemService(
                                 mPhoneId, BIND_IRADIOSIM, mSimServiceConnection);
                 if (!status) {
-                    Rlog.d(TAG, "IRadio Sim bind fail");
+                    Rlog.d(mTag, "IRadio Sim bind fail");
                     mSimServiceConnection = null;
                 }
             } else {
-                Rlog.d(TAG, "IRadio Sim is bound");
+                Rlog.d(mTag, "IRadio Sim is bound");
             }
-        } else if (service == RIL.MESSAGING_SERVICE) {
+        } else if (service == HAL_SERVICE_MESSAGING) {
             if (mMessagingBinder == null) {
-                mMessagingServiceConnection = new MockModemConnection(RIL.MESSAGING_SERVICE);
+                mMessagingServiceConnection = new MockModemConnection(HAL_SERVICE_MESSAGING);
 
                 boolean status =
                         bindModuleToMockModemService(
                                 mPhoneId, BIND_IRADIOMESSAGING, mMessagingServiceConnection);
                 if (!status) {
-                    Rlog.d(TAG, "IRadio Messaging bind fail");
+                    Rlog.d(mTag, "IRadio Messaging bind fail");
                     mMessagingServiceConnection = null;
                 }
             } else {
-                Rlog.d(TAG, "IRadio Messaging is bound");
+                Rlog.d(mTag, "IRadio Messaging is bound");
             }
-        } else if (service == RIL.DATA_SERVICE) {
+        } else if (service == HAL_SERVICE_DATA) {
             if (mDataBinder == null) {
-                mDataServiceConnection = new MockModemConnection(RIL.DATA_SERVICE);
+                mDataServiceConnection = new MockModemConnection(HAL_SERVICE_DATA);
 
                 boolean status =
                         bindModuleToMockModemService(
                                 mPhoneId, BIND_IRADIODATA, mDataServiceConnection);
                 if (!status) {
-                    Rlog.d(TAG, "IRadio Data bind fail");
+                    Rlog.d(mTag, "IRadio Data bind fail");
                     mDataServiceConnection = null;
                 }
             } else {
-                Rlog.d(TAG, "IRadio Data is bound");
+                Rlog.d(mTag, "IRadio Data is bound");
             }
-        } else if (service == RIL.NETWORK_SERVICE) {
+        } else if (service == HAL_SERVICE_NETWORK) {
             if (mNetworkBinder == null) {
-                mNetworkServiceConnection = new MockModemConnection(RIL.NETWORK_SERVICE);
+                mNetworkServiceConnection = new MockModemConnection(HAL_SERVICE_NETWORK);
 
                 boolean status =
                         bindModuleToMockModemService(
                                 mPhoneId, BIND_IRADIONETWORK, mNetworkServiceConnection);
                 if (!status) {
-                    Rlog.d(TAG, "IRadio Network bind fail");
+                    Rlog.d(mTag, "IRadio Network bind fail");
                     mNetworkServiceConnection = null;
                 }
             } else {
-                Rlog.d(TAG, "IRadio Network is bound");
+                Rlog.d(mTag, "IRadio Network is bound");
             }
-        } else if (service == RIL.VOICE_SERVICE) {
+        } else if (service == HAL_SERVICE_VOICE) {
             if (mVoiceBinder == null) {
-                mVoiceServiceConnection = new MockModemConnection(RIL.VOICE_SERVICE);
+                mVoiceServiceConnection = new MockModemConnection(HAL_SERVICE_VOICE);
 
                 boolean status =
                         bindModuleToMockModemService(
                                 mPhoneId, BIND_IRADIOVOICE, mVoiceServiceConnection);
                 if (!status) {
-                    Rlog.d(TAG, "IRadio Voice bind fail");
+                    Rlog.d(mTag, "IRadio Voice bind fail");
                     mVoiceServiceConnection = null;
                 }
             } else {
-                Rlog.d(TAG, "IRadio Voice is bound");
+                Rlog.d(mTag, "IRadio Voice is bound");
+            }
+        } else if (service == HAL_SERVICE_IMS) {
+            if (mImsBinder == null) {
+                mImsServiceConnection = new MockModemConnection(HAL_SERVICE_IMS);
+
+                boolean status =
+                        bindModuleToMockModemService(
+                                mPhoneId, BIND_IRADIOIMS, mImsServiceConnection);
+                if (!status) {
+                    Rlog.d(TAG, "IRadio Ims bind fail");
+                    mImsServiceConnection = null;
+                }
+            } else {
+                Rlog.d(TAG, "IRadio Ims is bound");
             }
         }
     }
@@ -284,49 +317,56 @@
                 mContext.unbindService(mConfigServiceConnection);
                 mConfigServiceConnection = null;
                 mConfigBinder = null;
-                Rlog.d(TAG, "unbind IRadio Config");
+                Rlog.d(mTag, "unbind IRadio Config");
             }
-        } else if (service == RIL.MODEM_SERVICE) {
+        } else if (service == HAL_SERVICE_MODEM) {
             if (mModemServiceConnection != null) {
                 mContext.unbindService(mModemServiceConnection);
                 mModemServiceConnection = null;
                 mModemBinder = null;
-                Rlog.d(TAG, "unbind IRadio Modem");
+                Rlog.d(mTag, "unbind IRadio Modem");
             }
-        } else if (service == RIL.SIM_SERVICE) {
+        } else if (service == HAL_SERVICE_SIM) {
             if (mSimServiceConnection != null) {
                 mContext.unbindService(mSimServiceConnection);
                 mSimServiceConnection = null;
                 mSimBinder = null;
-                Rlog.d(TAG, "unbind IRadio Sim");
+                Rlog.d(mTag, "unbind IRadio Sim");
             }
-        } else if (service == RIL.MESSAGING_SERVICE) {
+        } else if (service == HAL_SERVICE_MESSAGING) {
             if (mMessagingServiceConnection != null) {
                 mContext.unbindService(mMessagingServiceConnection);
                 mMessagingServiceConnection = null;
                 mMessagingBinder = null;
-                Rlog.d(TAG, "unbind IRadio Messaging");
+                Rlog.d(mTag, "unbind IRadio Messaging");
             }
-        } else if (service == RIL.DATA_SERVICE) {
+        } else if (service == HAL_SERVICE_DATA) {
             if (mDataServiceConnection != null) {
                 mContext.unbindService(mDataServiceConnection);
                 mDataServiceConnection = null;
                 mDataBinder = null;
-                Rlog.d(TAG, "unbind IRadio Data");
+                Rlog.d(mTag, "unbind IRadio Data");
             }
-        } else if (service == RIL.NETWORK_SERVICE) {
+        } else if (service == HAL_SERVICE_NETWORK) {
             if (mNetworkServiceConnection != null) {
                 mContext.unbindService(mNetworkServiceConnection);
                 mNetworkServiceConnection = null;
                 mNetworkBinder = null;
-                Rlog.d(TAG, "unbind IRadio Network");
+                Rlog.d(mTag, "unbind IRadio Network");
             }
-        } else if (service == RIL.VOICE_SERVICE) {
+        } else if (service == HAL_SERVICE_VOICE) {
             if (mVoiceServiceConnection != null) {
                 mContext.unbindService(mVoiceServiceConnection);
                 mVoiceServiceConnection = null;
                 mVoiceBinder = null;
-                Rlog.d(TAG, "unbind IRadio Voice");
+                Rlog.d(mTag, "unbind IRadio Voice");
+            }
+        } else if (service == HAL_SERVICE_IMS) {
+            if (mImsServiceConnection != null) {
+                mContext.unbindService(mImsServiceConnection);
+                mImsServiceConnection = null;
+                mImsBinder = null;
+                Rlog.d(TAG, "unbind IRadio Ims");
             }
         }
     }
@@ -337,18 +377,20 @@
 
     private String getModuleName(int service) {
         switch (service) {
-            case RIL.MODEM_SERVICE:
+            case HAL_SERVICE_MODEM:
                 return "modem";
-            case RIL.SIM_SERVICE:
+            case HAL_SERVICE_SIM:
                 return "sim";
-            case RIL.MESSAGING_SERVICE:
+            case HAL_SERVICE_MESSAGING:
                 return "messaging";
-            case RIL.DATA_SERVICE:
+            case HAL_SERVICE_DATA:
                 return "data";
-            case RIL.NETWORK_SERVICE:
+            case HAL_SERVICE_NETWORK:
                 return "network";
-            case RIL.VOICE_SERVICE:
+            case HAL_SERVICE_VOICE:
                 return "voice";
+            case HAL_SERVICE_IMS:
+                return "ims";
             case RADIOCONFIG_SERVICE:
                 return "config";
             default:
diff --git a/src/java/com/android/internal/telephony/ModemIndication.java b/src/java/com/android/internal/telephony/ModemIndication.java
index d05e0f6..0ee40bb 100644
--- a/src/java/com/android/internal/telephony/ModemIndication.java
+++ b/src/java/com/android/internal/telephony/ModemIndication.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_MODEM;
+
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_HARDWARE_CONFIG_CHANGED;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_MODEM_RESTART;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_RADIO_CAPABILITY;
@@ -44,7 +46,7 @@
      */
     public void hardwareConfigChanged(int indicationType,
             android.hardware.radio.modem.HardwareConfig[] configs) {
-        mRil.processIndication(RIL.MODEM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MODEM, indicationType);
 
         ArrayList<HardwareConfig> response = RILUtils.convertHalHardwareConfigList(configs);
 
@@ -62,7 +64,7 @@
      *        restart" that explains the cause of the modem restart
      */
     public void modemReset(int indicationType, String reason) {
-        mRil.processIndication(RIL.MODEM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MODEM, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_MODEM_RESTART, reason);
 
@@ -78,7 +80,7 @@
      */
     public void radioCapabilityIndication(int indicationType,
             android.hardware.radio.modem.RadioCapability radioCapability) {
-        mRil.processIndication(RIL.MODEM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MODEM, indicationType);
 
         RadioCapability response = RILUtils.convertHalRadioCapability(radioCapability, mRil);
 
@@ -94,7 +96,7 @@
      * @param radioState Current radio state
      */
     public void radioStateChanged(int indicationType, int radioState) {
-        mRil.processIndication(RIL.MODEM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MODEM, indicationType);
 
         int state = RILUtils.convertHalRadioState(radioState);
         if (mRil.isLogOrTrace()) {
@@ -110,7 +112,7 @@
      * @param indicationType Type of radio indication
      */
     public void rilConnected(int indicationType) {
-        mRil.processIndication(RIL.MODEM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_MODEM, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RIL_CONNECTED);
 
diff --git a/src/java/com/android/internal/telephony/ModemResponse.java b/src/java/com/android/internal/telephony/ModemResponse.java
index 6e44ddc..bd04d16 100644
--- a/src/java/com/android/internal/telephony/ModemResponse.java
+++ b/src/java/com/android/internal/telephony/ModemResponse.java
@@ -16,9 +16,12 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_MODEM;
+
 import android.hardware.radio.RadioError;
 import android.hardware.radio.RadioResponseInfo;
 import android.hardware.radio.modem.IRadioModemResponse;
+import android.hardware.radio.modem.ImeiInfo;
 import android.os.SystemClock;
 import android.telephony.ActivityStatsTechSpecificInfo;
 import android.telephony.AnomalyReporter;
@@ -51,7 +54,7 @@
      * @param responseInfo Response info struct containing response type, serial number and error.
      */
     public void enableModemResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MODEM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MODEM, mRil, responseInfo);
     }
 
     /**
@@ -59,7 +62,7 @@
      * @param version String containing version string for log reporting
      */
     public void getBasebandVersionResponse(RadioResponseInfo responseInfo, String version) {
-        RadioResponse.responseString(RIL.MODEM_SERVICE, mRil, responseInfo, version);
+        RadioResponse.responseString(HAL_SERVICE_MODEM, mRil, responseInfo, version);
     }
 
     /**
@@ -72,7 +75,21 @@
     public void getDeviceIdentityResponse(RadioResponseInfo responseInfo, String imei,
             String imeisv, String esn, String meid) {
         RadioResponse.responseStrings(
-                RIL.MODEM_SERVICE, mRil, responseInfo, imei, imeisv, esn, meid);
+                HAL_SERVICE_MODEM, mRil, responseInfo, imei, imeisv, esn, meid);
+    }
+
+    /**
+     * @param responseInfo Response info struct containing response type, serial no. and error
+     * @param imeiInfo object containing ImeiType, device IMEI and IMEISV
+     */
+    public void getImeiResponse(RadioResponseInfo responseInfo, ImeiInfo imeiInfo) {
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_MODEM, responseInfo);
+        if (rr != null) {
+            if (responseInfo.error == RadioError.NONE) {
+                RadioResponse.sendMessageResponse(rr.mResult, imeiInfo);
+            }
+            mRil.processResponseDone(rr, responseInfo, imeiInfo);
+        }
     }
 
     /**
@@ -81,7 +98,7 @@
      */
     public void getHardwareConfigResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.modem.HardwareConfig[] config) {
-        RILRequest rr = mRil.processResponse(RIL.MODEM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_MODEM, responseInfo);
 
         if (rr != null) {
             ArrayList<HardwareConfig> ret = RILUtils.convertHalHardwareConfigList(config);
@@ -98,7 +115,7 @@
      */
     public void getModemActivityInfoResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.modem.ActivityStatsInfo activityInfo) {
-        RILRequest rr = mRil.processResponse(RIL.MODEM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_MODEM, responseInfo);
 
         if (rr != null) {
             ModemActivityInfo ret = null;
@@ -141,7 +158,7 @@
      * @param isEnabled whether the modem stack is enabled.
      */
     public void getModemStackStatusResponse(RadioResponseInfo responseInfo, boolean isEnabled) {
-        RILRequest rr = mRil.processResponse(RIL.MODEM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_MODEM, responseInfo);
 
         if (rr != null) {
             if (responseInfo.error == RadioError.NONE) {
@@ -157,7 +174,7 @@
      */
     public void getRadioCapabilityResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.modem.RadioCapability radioCapability) {
-        RILRequest rr = mRil.processResponse(RIL.MODEM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_MODEM, responseInfo);
 
         if (rr != null) {
             RadioCapability ret = RILUtils.convertHalRadioCapability(radioCapability, mRil);
@@ -179,42 +196,42 @@
      * @param result String containing the contents of the NV item
      */
     public void nvReadItemResponse(RadioResponseInfo responseInfo, String result) {
-        RadioResponse.responseString(RIL.MODEM_SERVICE, mRil, responseInfo, result);
+        RadioResponse.responseString(HAL_SERVICE_MODEM, mRil, responseInfo, result);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void nvResetConfigResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MODEM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MODEM, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void nvWriteCdmaPrlResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MODEM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MODEM, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void nvWriteItemResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MODEM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MODEM, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void requestShutdownResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MODEM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MODEM, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void sendDeviceStateResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MODEM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MODEM, mRil, responseInfo);
     }
 
     /**
@@ -223,7 +240,7 @@
      */
     public void setRadioCapabilityResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.modem.RadioCapability radioCapability) {
-        RILRequest rr = mRil.processResponse(RIL.MODEM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_MODEM, responseInfo);
 
         if (rr != null) {
             RadioCapability ret = RILUtils.convertHalRadioCapability(radioCapability, mRil);
@@ -238,7 +255,7 @@
      * @param responseInfo Response info struct containing response type, serial no. and error.
      */
     public void setRadioPowerResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.MODEM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_MODEM, mRil, responseInfo);
         mRil.mLastRadioPowerResult = responseInfo.error;
         if (responseInfo.error == RadioError.RF_HARDWARE_ISSUE) {
             AnomalyReporter.reportAnomaly(
diff --git a/src/java/com/android/internal/telephony/MultiSimSettingController.java b/src/java/com/android/internal/telephony/MultiSimSettingController.java
index f3b91df..f188ceb 100644
--- a/src/java/com/android/internal/telephony/MultiSimSettingController.java
+++ b/src/java/com/android/internal/telephony/MultiSimSettingController.java
@@ -33,10 +33,8 @@
 import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.app.PendingIntent;
-import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
-import android.content.IntentFilter;
 import android.os.AsyncResult;
 import android.os.Handler;
 import android.os.Looper;
@@ -54,6 +52,8 @@
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.data.DataSettingsManager.DataSettingsManagerCallback;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.util.ArrayUtils;
 
 import java.lang.annotation.Retention;
@@ -83,7 +83,6 @@
     private static final int EVENT_SUBSCRIPTION_INFO_CHANGED         = 4;
     private static final int EVENT_SUBSCRIPTION_GROUP_CHANGED        = 5;
     private static final int EVENT_DEFAULT_DATA_SUBSCRIPTION_CHANGED = 6;
-    private static final int EVENT_CARRIER_CONFIG_CHANGED            = 7;
     private static final int EVENT_MULTI_SIM_CONFIG_CHANGED          = 8;
     @VisibleForTesting
     public static final int EVENT_RADIO_STATE_CHANGED                = 9;
@@ -122,6 +121,8 @@
 
     protected final Context mContext;
     protected final SubscriptionController mSubController;
+    private final SubscriptionManagerService mSubscriptionManagerService;
+
     // Keep a record of active primary (non-opportunistic) subscription list.
     @NonNull private List<Integer> mPrimarySubList = new ArrayList<>();
 
@@ -158,19 +159,6 @@
 
     private static final String SETTING_USER_PREF_DATA_SUB = "user_preferred_data_sub";
 
-    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            if (CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) {
-                int phoneId = intent.getIntExtra(CarrierConfigManager.EXTRA_SLOT_INDEX,
-                        SubscriptionManager.INVALID_SIM_SLOT_INDEX);
-                int subId = intent.getIntExtra(CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX,
-                        SubscriptionManager.INVALID_SUBSCRIPTION_ID);
-                notifyCarrierConfigChanged(phoneId, subId);
-            }
-        }
-    };
-
     private static class DataSettingsControllerCallback extends DataSettingsManagerCallback {
         private final Phone mPhone;
 
@@ -233,6 +221,7 @@
     public MultiSimSettingController(Context context, SubscriptionController sc) {
         mContext = context;
         mSubController = sc;
+        mSubscriptionManagerService = SubscriptionManagerService.getInstance();
 
         // Initialize mCarrierConfigLoadedSubIds and register to listen to carrier config change.
         final int phoneCount = ((TelephonyManager) mContext.getSystemService(
@@ -245,8 +234,12 @@
 
         mIsAskEverytimeSupportedForSms = mContext.getResources()
                 .getBoolean(com.android.internal.R.bool.config_sms_ask_every_time_support);
-        context.registerReceiver(mIntentReceiver, new IntentFilter(
-                CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));
+
+        CarrierConfigManager ccm = mContext.getSystemService(CarrierConfigManager.class);
+        // Listener callback is executed on handler thread to directly handle config change
+        ccm.registerCarrierConfigChangeListener(this::post,
+                (slotIndex, subId, carrierId, specificCarrierId) ->
+                        onCarrierConfigChanged(slotIndex, subId));
     }
 
     /**
@@ -325,11 +318,6 @@
             case EVENT_DEFAULT_DATA_SUBSCRIPTION_CHANGED:
                 onDefaultDataSettingChanged();
                 break;
-            case EVENT_CARRIER_CONFIG_CHANGED:
-                int phoneId = msg.arg1;
-                int subId = msg.arg2;
-                onCarrierConfigChanged(phoneId, subId);
-                break;
             case EVENT_MULTI_SIM_CONFIG_CHANGED:
                 int activeModems = (int) ((AsyncResult) msg.obj).result;
                 onMultiSimConfigChanged(activeModems);
@@ -358,12 +346,26 @@
         // Make sure MOBILE_DATA of subscriptions in same group are synced.
         setUserDataEnabledForGroup(subId, enable);
 
+        SubscriptionInfo subInfo = null;
+        int defaultDataSubId;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            subInfo = mSubscriptionManagerService.getSubscriptionInfo(subId);
+            defaultDataSubId = mSubscriptionManagerService.getDefaultDataSubId();
+        } else {
+            subInfo = mSubController.getSubscriptionInfo(subId);
+            defaultDataSubId = mSubController.getDefaultDataSubId();
+        }
+
         // If user is enabling a non-default non-opportunistic subscription, make it default.
-        if (mSubController.getDefaultDataSubId() != subId && !mSubController.isOpportunistic(subId)
-                && enable && mSubController.isActiveSubId(subId) && setDefaultData) {
-             android.provider.Settings.Global.putInt(mContext.getContentResolver(),
-                 SETTING_USER_PREF_DATA_SUB, subId);
-            mSubController.setDefaultDataSubId(subId);
+        if (defaultDataSubId != subId && subInfo != null && !subInfo.isOpportunistic() && enable
+                && subInfo.isActive() && setDefaultData) {
+            android.provider.Settings.Global.putInt(mContext.getContentResolver(),
+                    SETTING_USER_PREF_DATA_SUB, subId);
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                mSubscriptionManagerService.setDefaultDataSubId(subId);
+            } else {
+                mSubController.setDefaultDataSubId(subId);
+            }
         }
     }
 
@@ -374,8 +376,13 @@
         if (DBG) log("onRoamingDataEnabled");
         setRoamingDataEnabledForGroup(subId, enable);
 
-        // Also inform SubscriptionController as it keeps another copy of user setting.
-        mSubController.setDataRoaming(enable ? 1 : 0, subId);
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            // Also inform SubscriptionController as it keeps another copy of user setting.
+            mSubscriptionManagerService.setDataRoaming(enable ? 1 : 0, subId);
+        } else {
+            // Also inform SubscriptionController as it keeps another copy of user setting.
+            mSubController.setDataRoaming(enable ? 1 : 0, subId);
+        }
     }
 
     /**
@@ -420,14 +427,6 @@
         reEvaluateAll();
     }
 
-    /**
-     * Called when carrier config changes on any phone.
-     */
-    @VisibleForTesting
-    public void notifyCarrierConfigChanged(int phoneId, int subId) {
-        obtainMessage(EVENT_CARRIER_CONFIG_CHANGED, phoneId, subId).sendToTarget();
-    }
-
     private void onCarrierConfigChanged(int phoneId, int subId) {
         log("onCarrierConfigChanged phoneId " + phoneId + " subId " + subId);
         if (!SubscriptionManager.isValidPhoneId(phoneId)) {
@@ -439,14 +438,12 @@
         // being specified in it. So here we do additional check to make sur we don't miss the
         // subId.
         if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
-            int[] subIds = mSubController.getSubId(phoneId);
-            if (!ArrayUtils.isEmpty(subIds)) {
-                CarrierConfigManager cm = (CarrierConfigManager) mContext.getSystemService(
-                        mContext.CARRIER_CONFIG_SERVICE);
-                if (cm != null && cm.getConfigForSubId(subIds[0]) != null) {
-                    loge("onCarrierConfigChanged with invalid subId while subd "
-                            + subIds[0] + " is active and its config is loaded");
-                    subId = subIds[0];
+            subId = SubscriptionManager.getSubscriptionId(phoneId);
+            if (SubscriptionManager.isValidSubscriptionId(subId)) {
+                CarrierConfigManager cm = mContext.getSystemService(CarrierConfigManager.class);
+                if (cm != null && cm.getConfigForSubId(subId) != null) {
+                    loge("onCarrierConfigChanged with invalid subId while subId "
+                            + subId + " is active and its config is loaded");
                 }
             }
         }
@@ -460,7 +457,12 @@
      */
     @VisibleForTesting
     public boolean isCarrierConfigLoadedForAllSub() {
-        int[] activeSubIds = mSubController.getActiveSubIdList(false);
+        int[] activeSubIds;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            activeSubIds = mSubscriptionManagerService.getActiveSubIdList(false);
+        } else {
+            activeSubIds = mSubController.getActiveSubIdList(false);
+        }
         for (int activeSubId : activeSubIds) {
             boolean isLoaded = false;
             for (int configLoadedSub : mCarrierConfigLoadedSubIds) {
@@ -527,20 +529,29 @@
     private void onSubscriptionGroupChanged(ParcelUuid groupUuid) {
         if (DBG) log("onSubscriptionGroupChanged");
 
-        List<SubscriptionInfo> infoList = mSubController.getSubscriptionsInGroup(
-                groupUuid, mContext.getOpPackageName(), mContext.getAttributionTag());
-        if (infoList == null || infoList.isEmpty()) return;
+        List<SubscriptionInfo> infoList;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            infoList = mSubscriptionManagerService.getSubscriptionsInGroup(
+                    groupUuid, mContext.getOpPackageName(), mContext.getAttributionTag());
+            if (infoList == null || infoList.isEmpty()) return;
+
+        } else {
+            infoList = mSubController.getSubscriptionsInGroup(
+                    groupUuid, mContext.getOpPackageName(), mContext.getAttributionTag());
+            if (infoList == null || infoList.isEmpty()) return;
+        }
 
         // Get a reference subscription to copy settings from.
         // TODO: the reference sub should be passed in from external caller.
         int refSubId = infoList.get(0).getSubscriptionId();
         for (SubscriptionInfo info : infoList) {
             int subId = info.getSubscriptionId();
-            if (mSubController.isActiveSubId(subId) && !mSubController.isOpportunistic(subId)) {
+            if (info.isActive() && !info.isOpportunistic()) {
                 refSubId = subId;
                 break;
             }
         }
+
         if (DBG) log("refSubId is " + refSubId);
 
         boolean enable = false;
@@ -553,8 +564,14 @@
                     mContext, Settings.Global.MOBILE_DATA, INVALID_SUBSCRIPTION_ID, enable);
         }
         boolean setDefaultData = true;
-        List<SubscriptionInfo> activeSubList = mSubController.getActiveSubscriptionInfoList(
-                mContext.getOpPackageName(), mContext.getAttributionTag());
+        List<SubscriptionInfo> activeSubList;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            activeSubList = mSubscriptionManagerService.getActiveSubscriptionInfoList(
+                    mContext.getOpPackageName(), mContext.getAttributionTag());
+        } else {
+            activeSubList = mSubController.getActiveSubscriptionInfoList(
+                    mContext.getOpPackageName(), mContext.getAttributionTag());
+        }
         for (SubscriptionInfo activeInfo : activeSubList) {
             if (!(groupUuid.equals(activeInfo.getGroupUuid()))) {
                 // Do not set refSubId as defaultDataSubId if there are other active
@@ -577,8 +594,12 @@
             onRoamingDataEnabled(refSubId, enable);
         }
 
-        // Sync settings in subscription database..
-        mSubController.syncGroupedSetting(refSubId);
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            mSubscriptionManagerService.syncGroupedSetting(refSubId);
+        } else {
+            // Sync settings in subscription database..
+            mSubController.syncGroupedSetting(refSubId);
+        }
     }
 
     /**
@@ -600,21 +621,38 @@
 
         if (!isReadyToReevaluate()) return;
 
-        List<SubscriptionInfo> activeSubInfos = mSubController
-                .getActiveSubscriptionInfoList(mContext.getOpPackageName(),
-                        mContext.getAttributionTag());
+        List<SubscriptionInfo> activeSubInfos;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            activeSubInfos = mSubscriptionManagerService.getActiveSubscriptionInfoList(
+                    mContext.getOpPackageName(), mContext.getAttributionTag());
 
-        if (ArrayUtils.isEmpty(activeSubInfos)) {
-            mPrimarySubList.clear();
-            if (DBG) log("[updateDefaultValues] No active sub. Setting default to INVALID sub.");
-            mSubController.setDefaultDataSubId(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
-            mSubController.setDefaultVoiceSubId(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
-            mSubController.setDefaultSmsSubId(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
-            return;
+            if (ArrayUtils.isEmpty(activeSubInfos)) {
+                mPrimarySubList.clear();
+                if (DBG) log("updateDefaults: No active sub. Setting default to INVALID sub.");
+                mSubscriptionManagerService.setDefaultDataSubId(
+                        SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+                mSubscriptionManagerService.setDefaultVoiceSubId(
+                        SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+                mSubscriptionManagerService.setDefaultSmsSubId(
+                        SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+                return;
+            }
+        } else {
+            activeSubInfos = mSubController.getActiveSubscriptionInfoList(
+                    mContext.getOpPackageName(), mContext.getAttributionTag());
+
+            if (ArrayUtils.isEmpty(activeSubInfos)) {
+                mPrimarySubList.clear();
+                if (DBG) log("updateDefaultValues: No active sub. Setting default to INVALID sub.");
+                mSubController.setDefaultDataSubId(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+                mSubController.setDefaultVoiceSubId(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+                mSubController.setDefaultSmsSubId(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+                return;
+            }
         }
 
         int change = updatePrimarySubListAndGetChangeType(activeSubInfos);
-        if (DBG) log("[updateDefaultValues] change: " + change);
+        if (DBG) log("updateDefaultValues: change: " + change);
         if (change == PRIMARY_SUB_NO_CHANGE) return;
 
         // If there's only one primary subscription active, we trigger PREFERRED_PICK_DIALOG
@@ -626,34 +664,63 @@
                 || ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE))
                 .getActiveModemCount() == 1)) {
             int subId = mPrimarySubList.get(0);
-            if (DBG) log("[updateDefaultValues] to only primary sub " + subId);
-            mSubController.setDefaultDataSubId(subId);
-            mSubController.setDefaultVoiceSubId(subId);
-            mSubController.setDefaultSmsSubId(subId);
+            if (DBG) log("updateDefaultValues: to only primary sub " + subId);
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                mSubscriptionManagerService.setDefaultDataSubId(subId);
+                mSubscriptionManagerService.setDefaultVoiceSubId(subId);
+                mSubscriptionManagerService.setDefaultSmsSubId(subId);
+            } else {
+                mSubController.setDefaultDataSubId(subId);
+                mSubController.setDefaultVoiceSubId(subId);
+                mSubController.setDefaultSmsSubId(subId);
+            }
             sendDefaultSubConfirmedNotification(subId);
             return;
         }
 
-        if (DBG) log("[updateDefaultValues] records: " + mPrimarySubList);
+        if (DBG) log("updateDefaultValues: records: " + mPrimarySubList);
 
-        // Update default data subscription.
-        if (DBG) log("[updateDefaultValues] Update default data subscription");
-        boolean dataSelected = updateDefaultValue(mPrimarySubList,
-                mSubController.getDefaultDataSubId(),
-                (newValue -> mSubController.setDefaultDataSubId(newValue)));
+        boolean dataSelected, voiceSelected, smsSelected;
 
-        // Update default voice subscription.
-        if (DBG) log("[updateDefaultValues] Update default voice subscription");
-        boolean voiceSelected = updateDefaultValue(mPrimarySubList,
-                mSubController.getDefaultVoiceSubId(),
-                (newValue -> mSubController.setDefaultVoiceSubId(newValue)));
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            // Update default data subscription.
+            if (DBG) log("updateDefaultValues: Update default data subscription");
+            dataSelected = updateDefaultValue(mPrimarySubList,
+                    mSubscriptionManagerService.getDefaultDataSubId(),
+                    mSubscriptionManagerService::setDefaultDataSubId);
 
-        // Update default sms subscription.
-        if (DBG) log("[updateDefaultValues] Update default sms subscription");
-        boolean smsSelected = updateDefaultValue(mPrimarySubList,
-                mSubController.getDefaultSmsSubId(),
-                (newValue -> mSubController.setDefaultSmsSubId(newValue)),
-                mIsAskEverytimeSupportedForSms);
+            // Update default voice subscription.
+            if (DBG) log("updateDefaultValues: Update default voice subscription");
+            voiceSelected = updateDefaultValue(mPrimarySubList,
+                    mSubscriptionManagerService.getDefaultVoiceSubId(),
+                    mSubscriptionManagerService::setDefaultVoiceSubId);
+
+            // Update default sms subscription.
+            if (DBG) log("updateDefaultValues: Update default sms subscription");
+            smsSelected = updateDefaultValue(mPrimarySubList,
+                    mSubscriptionManagerService.getDefaultSmsSubId(),
+                    mSubscriptionManagerService::setDefaultSmsSubId,
+                    mIsAskEverytimeSupportedForSms);
+        } else {
+            // Update default data subscription.
+            if (DBG) log("updateDefaultValues: Update default data subscription");
+            dataSelected = updateDefaultValue(mPrimarySubList,
+                    mSubController.getDefaultDataSubId(),
+                    mSubController::setDefaultDataSubId);
+
+            // Update default voice subscription.
+            if (DBG) log("updateDefaultValues: Update default voice subscription");
+            voiceSelected = updateDefaultValue(mPrimarySubList,
+                    mSubController.getDefaultVoiceSubId(),
+                    mSubController::setDefaultVoiceSubId);
+
+            // Update default sms subscription.
+            if (DBG) log("updateDefaultValues: Update default sms subscription");
+            smsSelected = updateDefaultValue(mPrimarySubList,
+                    mSubController.getDefaultSmsSubId(),
+                    mSubController::setDefaultSmsSubId,
+                    mIsAskEverytimeSupportedForSms);
+        }
 
         boolean autoFallbackEnabled = mContext.getResources().getBoolean(
                 com.android.internal.R.bool.config_voice_data_sms_auto_fallback);
@@ -704,7 +771,14 @@
             // any previous primary subscription becomes inactive, we consider it
             for (int subId : prevPrimarySubList) {
                 if (mPrimarySubList.contains(subId)) continue;
-                if (!mSubController.isActiveSubId(subId)) {
+                SubscriptionInfo subInfo = null;
+                if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                    subInfo = mSubscriptionManagerService.getSubscriptionInfo(subId);
+                } else {
+                    subInfo = mSubController.getSubscriptionInfo(subId);
+                }
+
+                if (subInfo == null || !subInfo.isActive()) {
                     for (int currentSubId : mPrimarySubList) {
                         if (areSubscriptionsInSameGroup(currentSubId, subId)) {
                             return PRIMARY_SUB_REMOVED_IN_GROUP;
@@ -712,10 +786,10 @@
                     }
                     return PRIMARY_SUB_REMOVED;
                 }
-                if (!mSubController.isOpportunistic(subId)) {
+                if (!subInfo.isOpportunistic()) {
                     // Should never happen.
-                    loge("[updatePrimarySubListAndGetChangeType]: missing active primary subId "
-                            + subId);
+                    loge("[updatePrimarySubListAndGetChangeType]: missing active primary "
+                            + "subId " + subId);
                 }
             }
             return PRIMARY_SUB_MARKED_OPPT;
@@ -814,9 +888,18 @@
             // If a dual CDMA SIM combination warning is needed.
             if (phone != null && phone.isCdmaSubscriptionAppPresent()) {
                 cdmaPhoneCount++;
-                String simName = mSubController.getActiveSubscriptionInfo(
-                        subId, mContext.getOpPackageName(), mContext.getAttributionTag())
-                        .getDisplayName().toString();
+                String simName = null;
+                if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                    SubscriptionInfoInternal subInfo = mSubscriptionManagerService
+                            .getSubscriptionInfoInternal(subId);
+                    if (subInfo != null) {
+                        simName = subInfo.getDisplayName();
+                    }
+                } else {
+                    simName = mSubController.getActiveSubscriptionInfo(
+                            subId, mContext.getOpPackageName(), mContext.getAttributionTag())
+                            .getDisplayName().toString();
+                }
                 if (TextUtils.isEmpty(simName)) {
                     // Fall back to carrier name.
                     simName = phone.getCarrierName();
@@ -841,12 +924,25 @@
     protected void disableDataForNonDefaultNonOpportunisticSubscriptions() {
         if (!isReadyToReevaluate()) return;
 
-        int defaultDataSub = mSubController.getDefaultDataSubId();
+        int defaultDataSub;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            defaultDataSub = mSubscriptionManagerService.getDefaultDataSubId();
+        } else {
+            defaultDataSub = mSubController.getDefaultDataSubId();
+        }
 
         for (Phone phone : PhoneFactory.getPhones()) {
+            boolean isOpportunistic;
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                SubscriptionInfoInternal subInfo = mSubscriptionManagerService
+                        .getSubscriptionInfoInternal(phone.getSubId());
+                isOpportunistic = subInfo != null && subInfo.isOpportunistic();
+            } else {
+                isOpportunistic = mSubController.isOpportunistic(phone.getSubId());
+            }
             if (phone.getSubId() != defaultDataSub
                     && SubscriptionManager.isValidSubscriptionId(phone.getSubId())
-                    && !mSubController.isOpportunistic(phone.getSubId())
+                    && !isOpportunistic
                     && phone.isUserDataEnabled()
                     && !areSubscriptionsInSameGroup(defaultDataSub, phone.getSubId())) {
                 log("setting data to false on " + phone.getSubId());
@@ -862,9 +958,19 @@
                 || !SubscriptionManager.isUsableSubscriptionId(subId2)) return false;
         if (subId1 == subId2) return true;
 
-        ParcelUuid groupUuid1 = mSubController.getGroupUuid(subId1);
-        ParcelUuid groupUuid2 = mSubController.getGroupUuid(subId2);
-        return groupUuid1 != null && groupUuid1.equals(groupUuid2);
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo1 =
+                    mSubscriptionManagerService.getSubscriptionInfoInternal(subId1);
+            SubscriptionInfoInternal subInfo2 =
+                    mSubscriptionManagerService.getSubscriptionInfoInternal(subId2);
+            return subInfo1 != null && subInfo2 != null
+                    && !TextUtils.isEmpty(subInfo1.getGroupUuid())
+                    && subInfo1.getGroupUuid().equals(subInfo2.getGroupUuid());
+        } else {
+            ParcelUuid groupUuid1 = mSubController.getGroupUuid(subId1);
+            ParcelUuid groupUuid2 = mSubController.getGroupUuid(subId2);
+            return groupUuid1 != null && groupUuid1.equals(groupUuid2);
+        }
     }
 
     /**
@@ -873,18 +979,35 @@
      */
     protected void setUserDataEnabledForGroup(int subId, boolean enable) {
         log("setUserDataEnabledForGroup subId " + subId + " enable " + enable);
-        List<SubscriptionInfo> infoList = mSubController.getSubscriptionsInGroup(
-                mSubController.getGroupUuid(subId), mContext.getOpPackageName(),
-                mContext.getAttributionTag());
+        List<SubscriptionInfo> infoList = null;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = mSubscriptionManagerService
+                    .getSubscriptionInfoInternal(subId);
+            if (subInfo != null && !subInfo.getGroupUuid().isEmpty()) {
+                infoList = mSubscriptionManagerService.getSubscriptionsInGroup(
+                        ParcelUuid.fromString(subInfo.getGroupUuid()), mContext.getOpPackageName(),
+                        mContext.getAttributionTag());
+            }
+        } else {
+            infoList = mSubController.getSubscriptionsInGroup(
+                    mSubController.getGroupUuid(subId), mContext.getOpPackageName(),
+                    mContext.getAttributionTag());
+        }
 
         if (infoList == null) return;
 
         for (SubscriptionInfo info : infoList) {
             int currentSubId = info.getSubscriptionId();
             // TODO: simplify when setUserDataEnabled becomes singleton
-            if (mSubController.isActiveSubId(currentSubId)) {
+            if (info.isActive()) {
                 // For active subscription, call setUserDataEnabled through DataSettingsManager.
-                Phone phone = PhoneFactory.getPhone(mSubController.getPhoneId(currentSubId));
+                Phone phone;
+                if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                    phone = PhoneFactory.getPhone(mSubscriptionManagerService
+                            .getPhoneId(currentSubId));
+                } else {
+                    phone = PhoneFactory.getPhone(mSubController.getPhoneId(currentSubId));
+                }
                 // If enable is true and it's not opportunistic subscription, we don't enable it,
                 // as there can't be two
                 if (phone != null) {
@@ -905,11 +1028,19 @@
      * are synced.
      */
     private void setRoamingDataEnabledForGroup(int subId, boolean enable) {
-        SubscriptionController subController = SubscriptionController.getInstance();
-        List<SubscriptionInfo> infoList = subController.getSubscriptionsInGroup(
-                mSubController.getGroupUuid(subId), mContext.getOpPackageName(),
-                mContext.getAttributionTag());
-
+        List<SubscriptionInfo> infoList;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = mSubscriptionManagerService
+                    .getSubscriptionInfoInternal(subId);
+            if (subInfo == null || subInfo.getGroupUuid().isEmpty()) return;
+            infoList = SubscriptionManagerService.getInstance().getSubscriptionsInGroup(
+                    ParcelUuid.fromString(subInfo.getGroupUuid()), mContext.getOpPackageName(),
+                    mContext.getAttributionTag());
+        } else {
+            infoList = SubscriptionController.getInstance().getSubscriptionsInGroup(
+                    mSubController.getGroupUuid(subId), mContext.getOpPackageName(),
+                    mContext.getAttributionTag());
+        }
         if (infoList == null) return;
 
         for (SubscriptionInfo info : infoList) {
@@ -964,14 +1095,23 @@
     private void deactivateGroupedOpportunisticSubscriptionIfNeeded() {
         if (!SubscriptionInfoUpdater.isSubInfoInitialized()) return;
 
-        List<SubscriptionInfo> opptSubList = mSubController.getOpportunisticSubscriptions(
-                mContext.getOpPackageName(), mContext.getAttributionTag());
+        List<SubscriptionInfo> opptSubList;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            opptSubList = mSubscriptionManagerService.getAllSubInfoList(
+                    mContext.getOpPackageName(), mContext.getAttributionTag()).stream()
+                    .filter(SubscriptionInfo::isOpportunistic)
+                    .collect(Collectors.toList());
+
+        } else {
+            opptSubList = mSubController.getOpportunisticSubscriptions(
+                    mContext.getOpPackageName(), mContext.getAttributionTag());
+        }
 
         if (ArrayUtils.isEmpty(opptSubList)) return;
 
         for (SubscriptionInfo info : opptSubList) {
-            if (info.isGroupDisabled() && mSubController.isActiveSubId(info.getSubscriptionId())) {
-                log("[deactivateGroupedOpptSubIfNeeded] "
+            if (info.isGroupDisabled() && info.isActive()) {
+                log("deactivateGroupedOpportunisticSubscriptionIfNeeded: "
                         + "Deactivating grouped opportunistic subscription "
                         + info.getSubscriptionId());
                 deactivateSubscription(info);
@@ -1001,6 +1141,59 @@
     // would be selected as preferred voice/data/sms SIM.
     private void updateUserPreferences(List<Integer> primarySubList, boolean dataSelected,
             boolean voiceSelected, boolean smsSelected) {
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+
+            // In Single SIM case or if there are no activated subs available, no need to update.
+            // EXIT.
+            if ((primarySubList.isEmpty()) || (mSubscriptionManagerService
+                    .getActiveSubInfoCountMax() == 1)) {
+                return;
+            }
+
+            if (!isRadioAvailableOnAllSubs()) {
+                log("Radio is in Invalid state, Ignore Updating User Preference!!!");
+                return;
+            }
+            final int defaultDataSubId = mSubscriptionManagerService.getDefaultDataSubId();
+
+            if (DBG) {
+                log("updateUserPreferences:  dds = " + defaultDataSubId + " voice = "
+                        + mSubscriptionManagerService.getDefaultVoiceSubId()
+                        + " sms = " + mSubscriptionManagerService.getDefaultSmsSubId());
+            }
+
+            int autoDefaultSubId = primarySubList.get(0);
+
+            if ((primarySubList.size() == 1) && !smsSelected) {
+                mSubscriptionManagerService.setDefaultSmsSubId(autoDefaultSubId);
+            }
+
+            if ((primarySubList.size() == 1) && !voiceSelected) {
+                mSubscriptionManagerService.setDefaultVoiceSubId(autoDefaultSubId);
+            }
+
+            int userPrefDataSubId = getUserPrefDataSubIdFromDB();
+
+            log("User pref subId = " + userPrefDataSubId + " current dds " + defaultDataSubId
+                    + " next active subId " + autoDefaultSubId);
+
+            // If earlier user selected DDS is now available, set that as DDS subId.
+            if (primarySubList.contains(userPrefDataSubId)
+                    && SubscriptionManager.isValidSubscriptionId(userPrefDataSubId)
+                    && (defaultDataSubId != userPrefDataSubId)) {
+                mSubscriptionManagerService.setDefaultDataSubId(userPrefDataSubId);
+            } else if (!dataSelected) {
+                mSubscriptionManagerService.setDefaultDataSubId(autoDefaultSubId);
+            }
+
+            if (DBG) {
+                log("updateUserPreferences: after dds = "
+                        + mSubscriptionManagerService.getDefaultDataSubId() + " voice = "
+                        + mSubscriptionManagerService.getDefaultVoiceSubId() + " sms = "
+                        + mSubscriptionManagerService.getDefaultSmsSubId());
+            }
+            return;
+        }
         // In Single SIM case or if there are no activated subs available, no need to update. EXIT.
         if ((primarySubList.isEmpty()) || (mSubController.getActiveSubInfoCountMax() == 1)) return;
 
@@ -1039,9 +1232,11 @@
         }
 
 
-        if (DBG) log("updateUserPreferences: after dds = " + mSubController.getDefaultDataSubId() +
-                " voice = " + mSubController.getDefaultVoiceSubId() + " sms = " +
-                 mSubController.getDefaultSmsSubId());
+        if (DBG) {
+            log("updateUserPreferences: after dds = " + mSubController.getDefaultDataSubId()
+                    + " voice = " + mSubController.getDefaultVoiceSubId() + " sms = "
+                    + mSubController.getDefaultSmsSubId());
+        }
     }
 
     private int getUserPrefDataSubIdFromDB() {
diff --git a/src/java/com/android/internal/telephony/NetworkIndication.java b/src/java/com/android/internal/telephony/NetworkIndication.java
index c9ebfd5..909006e 100644
--- a/src/java/com/android/internal/telephony/NetworkIndication.java
+++ b/src/java/com/android/internal/telephony/NetworkIndication.java
@@ -16,10 +16,12 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
 import static android.telephony.TelephonyManager.UNKNOWN_CARRIER_ID;
 
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CDMA_PRL_CHANGED;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CELL_INFO_LIST;
+import static com.android.internal.telephony.RILConstants.RIL_UNSOL_EMERGENCY_NETWORK_SCAN_RESULT;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_LCEDATA_RECV;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_NETWORK_SCAN_RESULT;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_NITZ_TIME_RECEIVED;
@@ -39,6 +41,7 @@
 import android.telephony.BarringInfo;
 import android.telephony.CellIdentity;
 import android.telephony.CellInfo;
+import android.telephony.EmergencyRegResult;
 import android.telephony.LinkCapacityEstimate;
 import android.telephony.NetworkRegistrationInfo;
 import android.telephony.PhysicalChannelConfig;
@@ -72,7 +75,7 @@
     public void barringInfoChanged(int indicationType,
             android.hardware.radio.network.CellIdentity cellIdentity,
             android.hardware.radio.network.BarringInfo[] barringInfos) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         if (cellIdentity == null || barringInfos == null) {
             reportAnomaly(UUID.fromString("645b16bb-c930-4c1c-9c5d-568696542e05"),
@@ -84,7 +87,7 @@
         BarringInfo cbi = new BarringInfo(RILUtils.convertHalCellIdentity(cellIdentity),
                 RILUtils.convertHalBarringInfoList(barringInfos));
 
-        mRil.mBarringInfoChangedRegistrants.notifyRegistrants(new AsyncResult(null, cbi, null));
+        mRil.notifyBarringInfoChanged(cbi);
     }
 
     /**
@@ -93,7 +96,7 @@
      * @param version PRL version after PRL changes
      */
     public void cdmaPrlChanged(int indicationType, int version) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         int[] response = new int[]{version};
 
@@ -109,7 +112,7 @@
      */
     public void cellInfoList(int indicationType,
             android.hardware.radio.network.CellInfo[] records) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
         ArrayList<CellInfo> response = RILUtils.convertHalCellInfoList(records);
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_CELL_INFO_LIST, response);
         mRil.mRilCellInfoListRegistrants.notifyRegistrants(new AsyncResult(null, response, null));
@@ -122,7 +125,7 @@
      */
     public void currentLinkCapacityEstimate(int indicationType,
             android.hardware.radio.network.LinkCapacityEstimate lce) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         List<LinkCapacityEstimate> response = RILUtils.convertHalLceData(lce);
 
@@ -140,7 +143,7 @@
      */
     public void currentPhysicalChannelConfigs(int indicationType,
             android.hardware.radio.network.PhysicalChannelConfig[] configs) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
         List<PhysicalChannelConfig> response = new ArrayList<>(configs.length);
         try {
             for (android.hardware.radio.network.PhysicalChannelConfig config : configs) {
@@ -191,7 +194,7 @@
      */
     public void currentSignalStrength(int indicationType,
             android.hardware.radio.network.SignalStrength signalStrength) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         SignalStrength ssInitial = RILUtils.convertHalSignalStrength(signalStrength);
 
@@ -209,7 +212,7 @@
      * @param indicationType Type of radio indication
      */
     public void imsNetworkStateChanged(int indicationType) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED);
 
@@ -223,7 +226,7 @@
      */
     public void networkScanResult(int indicationType,
             android.hardware.radio.network.NetworkScanResult result) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         ArrayList<CellInfo> cellInfos = RILUtils.convertHalCellInfoList(result.networkInfos);
         NetworkScanResult nsr = new NetworkScanResult(result.status, result.error, cellInfos);
@@ -236,7 +239,7 @@
      * @param indicationType Type of radio indication
      */
     public void networkStateChanged(int indicationType) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED);
 
@@ -256,7 +259,7 @@
      */
     public void nitzTimeReceived(int indicationType, String nitzTime,
         @ElapsedRealtimeLong long receivedTimeMs, long ageMs) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_NITZ_TIME_RECEIVED, nitzTime);
 
@@ -303,7 +306,7 @@
     public void registrationFailed(int indicationType,
             android.hardware.radio.network.CellIdentity cellIdentity, String chosenPlmn,
             @NetworkRegistrationInfo.Domain int domain, int causeCode, int additionalCauseCode) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
         CellIdentity ci = RILUtils.convertHalCellIdentity(cellIdentity);
         if (ci == null || TextUtils.isEmpty(chosenPlmn)
                 || (domain & NetworkRegistrationInfo.DOMAIN_CS_PS) == 0
@@ -328,7 +331,7 @@
      * @param state Bitmask of restricted state as defined by PhoneRestrictedState
      */
     public void restrictedStateChanged(int indicationType, int state) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogvRet(RIL_UNSOL_RESTRICTED_STATE_CHANGED, state);
 
@@ -344,7 +347,7 @@
      */
     public void suppSvcNotify(int indicationType,
             android.hardware.radio.network.SuppSvcNotification suppSvcNotification) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         SuppServiceNotification notification = new SuppServiceNotification();
         notification.notificationType = suppSvcNotification.isMT ? 1 : 0;
@@ -368,7 +371,7 @@
      * @param rat Current new voice rat
      */
     public void voiceRadioTechChanged(int indicationType, int rat) {
-        mRil.processIndication(RIL.NETWORK_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
 
         int[] response = new int[] {rat};
 
@@ -380,6 +383,25 @@
                 new AsyncResult(null, response, null));
     }
 
+    /**
+     * Emergency Scan Results.
+     * @param indicationType Type of radio indication
+     * @param result the result of the Emergency Network Scan
+     */
+    public void emergencyNetworkScanResult(int indicationType,
+            android.hardware.radio.network.EmergencyRegResult result) {
+        mRil.processIndication(HAL_SERVICE_NETWORK, indicationType);
+
+        EmergencyRegResult response = RILUtils.convertHalEmergencyRegResult(result);
+
+        if (mRil.isLogOrTrace()) {
+            mRil.unsljLogRet(RIL_UNSOL_EMERGENCY_NETWORK_SCAN_RESULT, response);
+        }
+
+        mRil.mEmergencyNetworkScanRegistrants.notifyRegistrants(
+                new AsyncResult(null, response, null));
+    }
+
     @Override
     public String getInterfaceHash() {
         return IRadioNetworkIndication.HASH;
diff --git a/src/java/com/android/internal/telephony/NetworkResponse.java b/src/java/com/android/internal/telephony/NetworkResponse.java
index d9f70fd..b1eb926 100644
--- a/src/java/com/android/internal/telephony/NetworkResponse.java
+++ b/src/java/com/android/internal/telephony/NetworkResponse.java
@@ -16,12 +16,15 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
+
 import android.hardware.radio.RadioError;
 import android.hardware.radio.RadioResponseInfo;
 import android.hardware.radio.network.IRadioNetworkResponse;
 import android.os.AsyncResult;
 import android.telephony.BarringInfo;
 import android.telephony.CellInfo;
+import android.telephony.EmergencyRegResult;
 import android.telephony.LinkCapacityEstimate;
 import android.telephony.RadioAccessSpecifier;
 import android.telephony.SignalStrength;
@@ -57,7 +60,7 @@
             int halRadioAccessFamilyBitmap) {
         int networkTypeBitmask = RILUtils.convertHalNetworkTypeBitMask(halRadioAccessFamilyBitmap);
         mRil.mAllowedNetworkTypesBitmask = networkTypeBitmask;
-        RadioResponse.responseInts(RIL.NETWORK_SERVICE, mRil, responseInfo, networkTypeBitmask);
+        RadioResponse.responseInts(HAL_SERVICE_NETWORK, mRil, responseInfo, networkTypeBitmask);
     }
 
     /**
@@ -65,7 +68,7 @@
      * @param bandModes List of RadioBandMode listing supported modes
      */
     public void getAvailableBandModesResponse(RadioResponseInfo responseInfo, int[] bandModes) {
-        RadioResponse.responseIntArrayList(RIL.NETWORK_SERVICE, mRil, responseInfo,
+        RadioResponse.responseIntArrayList(HAL_SERVICE_NETWORK, mRil, responseInfo,
                 RILUtils.primitiveArrayToArrayList(bandModes));
     }
 
@@ -75,7 +78,7 @@
      */
     public void getAvailableNetworksResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.network.OperatorInfo[] networkInfos) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
 
         if (rr != null) {
             ArrayList<OperatorInfo> ret = new ArrayList<>();
@@ -98,7 +101,7 @@
     public void getBarringInfoResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.network.CellIdentity cellIdentity,
             android.hardware.radio.network.BarringInfo[] barringInfos) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
 
         if (rr != null) {
             BarringInfo bi = new BarringInfo(RILUtils.convertHalCellIdentity(cellIdentity),
@@ -118,7 +121,7 @@
      * @param type CdmaRoamingType defined in types.hal
      */
     public void getCdmaRoamingPreferenceResponse(RadioResponseInfo responseInfo, int type) {
-        RadioResponse.responseInts(RIL.NETWORK_SERVICE, mRil, responseInfo, type);
+        RadioResponse.responseInts(HAL_SERVICE_NETWORK, mRil, responseInfo, type);
     }
 
     /**
@@ -127,7 +130,7 @@
      */
     public void getCellInfoListResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.network.CellInfo[] cellInfo) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
 
         if (rr != null) {
             ArrayList<CellInfo> ret = RILUtils.convertHalCellInfoList(cellInfo);
@@ -144,7 +147,7 @@
      */
     public void getDataRegistrationStateResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.network.RegStateResult dataRegResponse) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
 
         if (rr != null) {
             if (responseInfo.error == RadioError.NONE) {
@@ -161,7 +164,7 @@
      */
     public void getImsRegistrationStateResponse(RadioResponseInfo responseInfo,
             boolean isRegistered, int ratFamily) {
-        RadioResponse.responseInts(RIL.NETWORK_SERVICE, mRil, responseInfo, isRegistered ? 1 : 0,
+        RadioResponse.responseInts(HAL_SERVICE_NETWORK, mRil, responseInfo, isRegistered ? 1 : 0,
                 ratFamily == android.hardware.radio.RadioTechnologyFamily.THREE_GPP
                         ? PhoneConstants.PHONE_TYPE_GSM : PhoneConstants.PHONE_TYPE_CDMA);
     }
@@ -171,7 +174,7 @@
      * @param selection false for automatic selection, true for manual selection
      */
     public void getNetworkSelectionModeResponse(RadioResponseInfo responseInfo, boolean selection) {
-        RadioResponse.responseInts(RIL.NETWORK_SERVICE, mRil, responseInfo, selection ? 1 : 0);
+        RadioResponse.responseInts(HAL_SERVICE_NETWORK, mRil, responseInfo, selection ? 1 : 0);
     }
 
     /**
@@ -183,7 +186,7 @@
     public void getOperatorResponse(RadioResponseInfo responseInfo, String longName,
             String shortName, String numeric) {
         RadioResponse.responseStrings(
-                RIL.NETWORK_SERVICE, mRil, responseInfo, longName, shortName, numeric);
+                HAL_SERVICE_NETWORK, mRil, responseInfo, longName, shortName, numeric);
     }
 
     /**
@@ -192,7 +195,7 @@
      */
     public void getSignalStrengthResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.network.SignalStrength signalStrength) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
 
         if (rr != null) {
             SignalStrength ret = RILUtils.convertHalSignalStrength(signalStrength);
@@ -209,7 +212,7 @@
      */
     public void getSystemSelectionChannelsResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.network.RadioAccessSpecifier[] halSpecifiers) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
 
         if (rr != null) {
             ArrayList<RadioAccessSpecifier> specifiers = new ArrayList<>();
@@ -229,7 +232,7 @@
      * @param rat Current voice RAT
      */
     public void getVoiceRadioTechnologyResponse(RadioResponseInfo responseInfo, int rat) {
-        RadioResponse.responseInts(RIL.NETWORK_SERVICE, mRil, responseInfo, rat);
+        RadioResponse.responseInts(HAL_SERVICE_NETWORK, mRil, responseInfo, rat);
     }
 
     /**
@@ -238,7 +241,7 @@
      */
     public void getVoiceRegistrationStateResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.network.RegStateResult voiceRegResponse) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
         if (rr != null) {
             if (responseInfo.error == RadioError.NONE) {
                 RadioResponse.sendMessageResponse(rr.mResult, voiceRegResponse);
@@ -254,7 +257,7 @@
      */
     public void isNrDualConnectivityEnabledResponse(RadioResponseInfo responseInfo,
             boolean isEnabled) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
 
         if (rr != null) {
             if (responseInfo.error == RadioError.NONE) {
@@ -270,7 +273,7 @@
      */
     public void pullLceDataResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.network.LceDataInfo lceInfo) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
 
         if (rr != null) {
             List<LinkCapacityEstimate> ret = RILUtils.convertHalLceData(lceInfo);
@@ -285,105 +288,105 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setAllowedNetworkTypesBitmapResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setBandModeResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setBarringPasswordResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setCdmaRoamingPreferenceResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setCellInfoListRateResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setIndicationFilterResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setLinkCapacityReportingCriteriaResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setLocationUpdatesResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setNetworkSelectionModeAutomaticResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setNetworkSelectionModeManualResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setNrDualConnectivityStateResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setSignalStrengthReportingCriteriaResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setSuppServiceNotificationsResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial number and error.
      */
     public void setSystemSelectionChannelsResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void startNetworkScanResponse(RadioResponseInfo responseInfo) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
         if (rr != null) {
             NetworkScanResult nsr = null;
             if (responseInfo.error == RadioError.NONE) {
@@ -399,7 +402,7 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void stopNetworkScanResponse(RadioResponseInfo responseInfo) {
-        RILRequest rr = mRil.processResponse(RIL.NETWORK_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
         if (rr != null) {
             NetworkScanResult nsr = null;
             if (responseInfo.error == RadioError.NONE) {
@@ -417,14 +420,14 @@
      */
     public void supplyNetworkDepersonalizationResponse(RadioResponseInfo responseInfo,
             int retriesRemaining) {
-        RadioResponse.responseInts(RIL.NETWORK_SERVICE, mRil, responseInfo, retriesRemaining);
+        RadioResponse.responseInts(HAL_SERVICE_NETWORK, mRil, responseInfo, retriesRemaining);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setUsageSettingResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.NETWORK_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     /**
@@ -433,7 +436,91 @@
      */
     public void getUsageSettingResponse(RadioResponseInfo responseInfo,
             /* @TelephonyManager.UsageSetting */ int usageSetting) {
-        RadioResponse.responseInts(RIL.NETWORK_SERVICE, mRil, responseInfo, usageSetting);
+        RadioResponse.responseInts(HAL_SERVICE_NETWORK, mRil, responseInfo, usageSetting);
+    }
+
+    /**
+     * @param responseInfo Response info struct containing response type, serial no. and error
+     * @param regState the current registration state of the modem.
+     */
+    public void setEmergencyModeResponse(RadioResponseInfo responseInfo,
+            android.hardware.radio.network.EmergencyRegResult regState) {
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
+
+        if (rr != null) {
+            EmergencyRegResult response = RILUtils.convertHalEmergencyRegResult(regState);
+            if (responseInfo.error == RadioError.NONE) {
+                RadioResponse.sendMessageResponse(rr.mResult, response);
+            }
+            mRil.processResponseDone(rr, responseInfo, response);
+        }
+    }
+
+    /**
+     * @param responseInfo Response info struct containing response type, serial no. and error
+     */
+    public void triggerEmergencyNetworkScanResponse(RadioResponseInfo responseInfo) {
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
+    }
+
+    /**
+     * @param responseInfo Response info struct containing response type, serial no. and error
+     */
+    public void exitEmergencyModeResponse(RadioResponseInfo responseInfo) {
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
+    }
+
+    /**
+     * @param responseInfo Response info struct containing response type, serial no. and error
+     */
+    public void cancelEmergencyNetworkScanResponse(RadioResponseInfo responseInfo) {
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
+    }
+
+    /**
+     * @param responseInfo Response info struct containing response type, serial no. and error
+     */
+    public void setNullCipherAndIntegrityEnabledResponse(RadioResponseInfo responseInfo) {
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
+    }
+
+    /**
+     * @param responseInfo Response info struct containing response type, serial no. and error.
+     * @param isEnabled Indicates whether null cipher and integrity is enabled, indicating
+     *                  potentially unencrypted communication
+     */
+    public void isNullCipherAndIntegrityEnabledResponse(RadioResponseInfo responseInfo,
+                    boolean isEnabled) {
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
+
+        if (rr != null) {
+            if (responseInfo.error == RadioError.NONE) {
+                RadioResponse.sendMessageResponse(rr.mResult, isEnabled);
+            }
+            mRil.processResponseDone(rr, responseInfo, isEnabled);
+        }
+    }
+
+    /**
+     * @param responseInfo Response info struct containing response type, serial no. and error.
+     * @param isEnabled Indicates whether N1 mode is enabled or not.
+     */
+    public void isN1ModeEnabledResponse(RadioResponseInfo responseInfo, boolean isEnabled) {
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_NETWORK, responseInfo);
+
+        if (rr != null) {
+            if (responseInfo.error == RadioError.NONE) {
+                RadioResponse.sendMessageResponse(rr.mResult, isEnabled);
+            }
+            mRil.processResponseDone(rr, responseInfo, isEnabled);
+        }
+    }
+
+    /**
+     * @param responseInfo Response info struct containing response type, serial no. and error.
+     */
+    public void setN1ModeEnabledResponse(RadioResponseInfo responseInfo) {
+        RadioResponse.responseVoid(HAL_SERVICE_NETWORK, mRil, responseInfo);
     }
 
     @Override
@@ -445,4 +532,5 @@
     public int getInterfaceVersion() {
         return IRadioNetworkResponse.VERSION;
     }
+
 }
diff --git a/src/java/com/android/internal/telephony/NetworkScanRequestTracker.java b/src/java/com/android/internal/telephony/NetworkScanRequestTracker.java
index ea68cba..b15dc59 100644
--- a/src/java/com/android/internal/telephony/NetworkScanRequestTracker.java
+++ b/src/java/com/android/internal/telephony/NetworkScanRequestTracker.java
@@ -42,6 +42,8 @@
 import android.telephony.TelephonyScanManager;
 import android.util.Log;
 
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
+
 import java.util.Collection;
 import java.util.List;
 import java.util.Set;
@@ -195,6 +197,13 @@
     public static Set<String> getAllowedMccMncsForLocationRestrictedScan(Context context) {
         final long token = Binder.clearCallingIdentity();
         try {
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                return SubscriptionManagerService.getInstance()
+                        .getAvailableSubscriptionInfoList(context.getOpPackageName(),
+                                context.getAttributionTag()).stream()
+                        .flatMap(NetworkScanRequestTracker::getAllowableMccMncsFromSubscriptionInfo)
+                        .collect(Collectors.toSet());
+            }
             return SubscriptionController.getInstance()
                     .getAvailableSubscriptionInfoList(context.getOpPackageName(),
                             context.getAttributionTag()).stream()
diff --git a/src/java/com/android/internal/telephony/NetworkTypeController.java b/src/java/com/android/internal/telephony/NetworkTypeController.java
index f39c79b..4fd3054 100644
--- a/src/java/com/android/internal/telephony/NetworkTypeController.java
+++ b/src/java/com/android/internal/telephony/NetworkTypeController.java
@@ -16,6 +16,7 @@
 
 package com.android.internal.telephony;
 
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.content.BroadcastReceiver;
 import android.content.Context;
@@ -39,6 +40,7 @@
 import android.text.TextUtils;
 
 import com.android.internal.telephony.data.DataNetworkController.DataNetworkControllerCallback;
+import com.android.internal.telephony.data.DataUtils;
 import com.android.internal.telephony.util.ArrayUtils;
 import com.android.internal.util.IState;
 import com.android.internal.util.IndentingPrintWriter;
@@ -52,6 +54,7 @@
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -83,30 +86,35 @@
     /** Stop all timers and go to current state. */
     public static final int EVENT_UPDATE = 0;
     /** Quit after processing all existing messages. */
-    public static final int EVENT_QUIT = 1;
-    private static final int EVENT_DATA_RAT_CHANGED = 2;
-    private static final int EVENT_NR_STATE_CHANGED = 3;
-    private static final int EVENT_NR_FREQUENCY_CHANGED = 4;
-    private static final int EVENT_PHYSICAL_LINK_STATUS_CHANGED = 5;
-    private static final int EVENT_PHYSICAL_CHANNEL_CONFIG_NOTIF_CHANGED = 6;
-    private static final int EVENT_CARRIER_CONFIG_CHANGED = 7;
-    private static final int EVENT_PRIMARY_TIMER_EXPIRED = 8;
-    private static final int EVENT_SECONDARY_TIMER_EXPIRED = 9;
-    private static final int EVENT_RADIO_OFF_OR_UNAVAILABLE = 10;
-    private static final int EVENT_PREFERRED_NETWORK_MODE_CHANGED = 11;
-    private static final int EVENT_INITIALIZE = 12;
-    private static final int EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED = 13;
-    private static final int EVENT_BANDWIDTH_CHANGED = 15;
-    private static final int EVENT_UPDATE_NR_ADVANCED_STATE = 16;
-    private static final int EVENT_DEVICE_IDLE_MODE_CHANGED = 17;
+    private static final int EVENT_QUIT = 1;
+    /** Initialize all events. */
+    private static final int EVENT_INITIALIZE = 2;
+    /** Event for service state changed (data rat, bandwidth, NR state, NR frequency, etc). */
+    private static final int EVENT_SERVICE_STATE_CHANGED = 3;
+    /** Event for physical link status changed. */
+    private static final int EVENT_PHYSICAL_LINK_STATUS_CHANGED = 4;
+    /** Event for physical channel config indications turned on/off. */
+    private static final int EVENT_PHYSICAL_CHANNEL_CONFIG_NOTIF_CHANGED = 5;
+    /** Event for carrier configs changed. */
+    private static final int EVENT_CARRIER_CONFIG_CHANGED = 6;
+    /** Event for primary timer expired. If a secondary timer exists, it will begin afterwards. */
+    private static final int EVENT_PRIMARY_TIMER_EXPIRED = 7;
+    /** Event for secondary timer expired. */
+    private static final int EVENT_SECONDARY_TIMER_EXPIRED = 8;
+    /** Event for radio off or unavailable. */
+    private static final int EVENT_RADIO_OFF_OR_UNAVAILABLE = 9;
+    /** Event for preferred network mode changed. */
+    private static final int EVENT_PREFERRED_NETWORK_MODE_CHANGED = 10;
+    /** Event for physical channel configs changed. */
+    private static final int EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED = 11;
+    /** Event for device idle mode changed, when device goes to deep sleep and pauses all timers. */
+    private static final int EVENT_DEVICE_IDLE_MODE_CHANGED = 12;
 
     private static final String[] sEvents = new String[EVENT_DEVICE_IDLE_MODE_CHANGED + 1];
     static {
         sEvents[EVENT_UPDATE] = "EVENT_UPDATE";
         sEvents[EVENT_QUIT] = "EVENT_QUIT";
-        sEvents[EVENT_DATA_RAT_CHANGED] = "EVENT_DATA_RAT_CHANGED";
-        sEvents[EVENT_NR_STATE_CHANGED] = "EVENT_NR_STATE_CHANGED";
-        sEvents[EVENT_NR_FREQUENCY_CHANGED] = "EVENT_NR_FREQUENCY_CHANGED";
+        sEvents[EVENT_SERVICE_STATE_CHANGED] = "EVENT_SERVICE_STATE_CHANGED";
         sEvents[EVENT_PHYSICAL_LINK_STATUS_CHANGED] = "EVENT_PHYSICAL_LINK_STATUS_CHANGED";
         sEvents[EVENT_PHYSICAL_CHANNEL_CONFIG_NOTIF_CHANGED] =
                 "EVENT_PHYSICAL_CHANNEL_CONFIG_NOTIF_CHANGED";
@@ -117,14 +125,12 @@
         sEvents[EVENT_PREFERRED_NETWORK_MODE_CHANGED] = "EVENT_PREFERRED_NETWORK_MODE_CHANGED";
         sEvents[EVENT_INITIALIZE] = "EVENT_INITIALIZE";
         sEvents[EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED] = "EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED";
-        sEvents[EVENT_BANDWIDTH_CHANGED] = "EVENT_BANDWIDTH_CHANGED";
-        sEvents[EVENT_UPDATE_NR_ADVANCED_STATE] = "EVENT_UPDATE_NR_ADVANCED_STATE";
         sEvents[EVENT_DEVICE_IDLE_MODE_CHANGED] = "EVENT_DEVICE_IDLE_MODE_CHANGED";
     }
 
-    private final Phone mPhone;
-    private final DisplayInfoController mDisplayInfoController;
-    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
+    private final @NonNull Phone mPhone;
+    private final @NonNull DisplayInfoController mDisplayInfoController;
+    private final @NonNull BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
             switch (intent.getAction()) {
@@ -143,20 +149,20 @@
         }
     };
 
-    private Map<String, OverrideTimerRule> mOverrideTimerRules = new HashMap<>();
-    private String mLteEnhancedPattern = "";
-    private int mOverrideNetworkType;
+    private @NonNull Map<String, OverrideTimerRule> mOverrideTimerRules = new HashMap<>();
+    private @NonNull String mLteEnhancedPattern = "";
+    private @Annotation.OverrideNetworkType int mOverrideNetworkType;
     private boolean mIsPhysicalChannelConfigOn;
     private boolean mIsPrimaryTimerActive;
     private boolean mIsSecondaryTimerActive;
-    private boolean mIsTimerResetEnabledForLegacyStateRRCIdle;
+    private boolean mIsTimerResetEnabledForLegacyStateRrcIdle;
     private int mLtePlusThresholdBandwidth;
     private int mNrAdvancedThresholdBandwidth;
     private boolean mIncludeLteForNrAdvancedThresholdBandwidth;
-    private int[] mAdditionalNrAdvancedBandsList;
-    private String mPrimaryTimerState;
-    private String mSecondaryTimerState;
-    private String mPreviousState;
+    private @NonNull int[] mAdditionalNrAdvancedBandsList;
+    private @NonNull String mPrimaryTimerState;
+    private @NonNull String mSecondaryTimerState;
+    private @NonNull String mPreviousState;
     private @LinkStatus int mPhysicalLinkStatus;
     private boolean mIsPhysicalChannelConfig16Supported;
     private boolean mIsNrAdvancedAllowedByPco = false;
@@ -168,6 +174,10 @@
     private @Nullable DataNetworkControllerCallback mNrAdvancedCapableByPcoChangedCallback = null;
     private @Nullable DataNetworkControllerCallback mNrPhysicalLinkStatusChangedCallback = null;
 
+    // Cached copies below to prevent race conditions
+    private @NonNull ServiceState mServiceState;
+    private @Nullable List<PhysicalChannelConfig> mPhysicalChannelConfigs;
+
     /**
      * NetworkTypeController constructor.
      *
@@ -180,13 +190,22 @@
         mDisplayInfoController = displayInfoController;
         mOverrideNetworkType = TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE;
         mIsPhysicalChannelConfigOn = true;
-        addState(mDefaultState);
-        addState(mLegacyState, mDefaultState);
-        addState(mIdleState, mDefaultState);
-        addState(mLteConnectedState, mDefaultState);
-        addState(mNrConnectedState, mDefaultState);
-        setInitialState(mDefaultState);
+        mPrimaryTimerState = "";
+        mSecondaryTimerState = "";
+        mPreviousState = "";
+        DefaultState defaultState = new DefaultState();
+        addState(defaultState);
+        addState(mLegacyState, defaultState);
+        addState(mIdleState, defaultState);
+        addState(mLteConnectedState, defaultState);
+        addState(mNrConnectedState, defaultState);
+        addState(mNrConnectedAdvancedState, defaultState);
+        setInitialState(defaultState);
         start();
+
+        mServiceState = mPhone.getServiceStateTracker().getServiceState();
+        mPhysicalChannelConfigs = mPhone.getServiceStateTracker().getPhysicalChannelConfigList();
+
         sendMessage(EVENT_INITIALIZE);
     }
 
@@ -199,10 +218,21 @@
     }
 
     /**
-     * @return True if either the primary or secondary 5G hysteresis timer is active,
-     * and false if neither are.
+     * @return The current data network type, used to create TelephonyDisplayInfo in
+     * DisplayInfoController.
      */
-    public boolean is5GHysteresisActive() {
+    public @Annotation.NetworkType int getDataNetworkType() {
+        NetworkRegistrationInfo nri = mServiceState.getNetworkRegistrationInfo(
+                NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+        return nri == null ? TelephonyManager.NETWORK_TYPE_UNKNOWN
+                : nri.getAccessNetworkTechnology();
+    }
+
+    /**
+     * @return {@code true} if either the primary or secondary 5G icon timers are active,
+     * and {@code false} if neither are.
+     */
+    public boolean areAnyTimersActive() {
         return mIsPrimaryTimerActive || mIsSecondaryTimerActive;
     }
 
@@ -213,18 +243,11 @@
                 EVENT_PREFERRED_NETWORK_MODE_CHANGED, null);
         mPhone.registerForPhysicalChannelConfig(getHandler(),
                 EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED, null);
-        mPhone.getServiceStateTracker().registerForDataRegStateOrRatChanged(
-                AccessNetworkConstants.TRANSPORT_TYPE_WWAN, getHandler(),
-                EVENT_DATA_RAT_CHANGED, null);
-        mPhone.getServiceStateTracker().registerForBandwidthChanged(
-                getHandler(), EVENT_BANDWIDTH_CHANGED, null);
+        mPhone.getServiceStateTracker().registerForServiceStateChanged(getHandler(),
+                EVENT_SERVICE_STATE_CHANGED, null);
         mIsPhysicalChannelConfig16Supported = mPhone.getContext().getSystemService(
                 TelephonyManager.class).isRadioInterfaceCapabilitySupported(
                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
-        mPhone.getServiceStateTracker().registerForNrStateChanged(getHandler(),
-                EVENT_NR_STATE_CHANGED, null);
-        mPhone.getServiceStateTracker().registerForNrFrequencyChanged(getHandler(),
-                EVENT_NR_FREQUENCY_CHANGED, null);
         mPhone.getDeviceStateMonitor().registerForPhysicalChannelConfigNotifChanged(getHandler(),
                 EVENT_PHYSICAL_CHANNEL_CONFIG_NOTIF_CHANGED, null);
         IntentFilter filter = new IntentFilter();
@@ -236,119 +259,80 @@
     private void unRegisterForAllEvents() {
         mPhone.unregisterForRadioOffOrNotAvailable(getHandler());
         mPhone.unregisterForPreferredNetworkTypeChanged(getHandler());
-        mPhone.getServiceStateTracker().unregisterForDataRegStateOrRatChanged(
-                AccessNetworkConstants.TRANSPORT_TYPE_WWAN, getHandler());
-        mPhone.getServiceStateTracker().unregisterForNrStateChanged(getHandler());
-        mPhone.getServiceStateTracker().unregisterForNrFrequencyChanged(getHandler());
+        mPhone.getServiceStateTracker().unregisterForServiceStateChanged(getHandler());
         mPhone.getDeviceStateMonitor().unregisterForPhysicalChannelConfigNotifChanged(getHandler());
         mPhone.getContext().unregisterReceiver(mIntentReceiver);
     }
 
     private void parseCarrierConfigs() {
-        String nrIconConfiguration = CarrierConfigManager.getDefaultConfig().getString(
-                CarrierConfigManager.KEY_5G_ICON_CONFIGURATION_STRING);
-        String overrideTimerRule = CarrierConfigManager.getDefaultConfig().getString(
-                CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING);
-        String overrideSecondaryTimerRule = CarrierConfigManager.getDefaultConfig().getString(
-                CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING);
-        mLteEnhancedPattern = CarrierConfigManager.getDefaultConfig().getString(
-                CarrierConfigManager.KEY_SHOW_CARRIER_DATA_ICON_PATTERN_STRING);
-        mIsTimerResetEnabledForLegacyStateRRCIdle =
-                CarrierConfigManager.getDefaultConfig().getBoolean(
-                        CarrierConfigManager.KEY_NR_TIMERS_RESET_IF_NON_ENDC_AND_RRC_IDLE_BOOL);
-        mLtePlusThresholdBandwidth = CarrierConfigManager.getDefaultConfig().getInt(
-                CarrierConfigManager.KEY_LTE_PLUS_THRESHOLD_BANDWIDTH_KHZ_INT);
-        mNrAdvancedThresholdBandwidth = CarrierConfigManager.getDefaultConfig().getInt(
-                CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT);
-        mIncludeLteForNrAdvancedThresholdBandwidth = CarrierConfigManager.getDefaultConfig()
-                .getBoolean(CarrierConfigManager
-                        .KEY_INCLUDE_LTE_FOR_NR_ADVANCED_THRESHOLD_BANDWIDTH_BOOL);
-        mEnableNrAdvancedWhileRoaming = CarrierConfigManager.getDefaultConfig().getBoolean(
-                CarrierConfigManager.KEY_ENABLE_NR_ADVANCED_WHILE_ROAMING_BOOL);
-
-        CarrierConfigManager configManager = (CarrierConfigManager) mPhone.getContext()
-                .getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        PersistableBundle config = CarrierConfigManager.getDefaultConfig();
+        CarrierConfigManager configManager =
+                mPhone.getContext().getSystemService(CarrierConfigManager.class);
         if (configManager != null) {
             PersistableBundle b = configManager.getConfigForSubId(mPhone.getSubId());
             if (b != null) {
-                if (b.getString(CarrierConfigManager.KEY_5G_ICON_CONFIGURATION_STRING) != null) {
-                    nrIconConfiguration = b.getString(
-                            CarrierConfigManager.KEY_5G_ICON_CONFIGURATION_STRING);
-                }
-                if (b.getString(CarrierConfigManager
-                        .KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING) != null) {
-                    overrideTimerRule = b.getString(
-                            CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING);
-                }
-                if (b.getString(CarrierConfigManager
-                        .KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING) != null) {
-                    overrideSecondaryTimerRule = b.getString(
-                            CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING);
-                }
-                if (b.getString(CarrierConfigManager
-                        .KEY_SHOW_CARRIER_DATA_ICON_PATTERN_STRING) != null) {
-                    mLteEnhancedPattern = b.getString(
-                            CarrierConfigManager.KEY_SHOW_CARRIER_DATA_ICON_PATTERN_STRING);
-                }
-                mIsTimerResetEnabledForLegacyStateRRCIdle = b.getBoolean(
-                        CarrierConfigManager.KEY_NR_TIMERS_RESET_IF_NON_ENDC_AND_RRC_IDLE_BOOL);
-                mLtePlusThresholdBandwidth = b.getInt(
-                        CarrierConfigManager.KEY_LTE_PLUS_THRESHOLD_BANDWIDTH_KHZ_INT,
-                        mLtePlusThresholdBandwidth);
-                mNrAdvancedThresholdBandwidth = b.getInt(
-                        CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT,
-                        mNrAdvancedThresholdBandwidth);
-                mIncludeLteForNrAdvancedThresholdBandwidth = b.getBoolean(CarrierConfigManager
-                        .KEY_INCLUDE_LTE_FOR_NR_ADVANCED_THRESHOLD_BANDWIDTH_BOOL,
-                        mIncludeLteForNrAdvancedThresholdBandwidth);
-                mAdditionalNrAdvancedBandsList = b.getIntArray(
-                        CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY);
-                mNrAdvancedCapablePcoId = b.getInt(
-                        CarrierConfigManager.KEY_NR_ADVANCED_CAPABLE_PCO_ID_INT);
-                if (mNrAdvancedCapablePcoId > 0 && mNrAdvancedCapableByPcoChangedCallback == null) {
-                    mNrAdvancedCapableByPcoChangedCallback =
-                            new DataNetworkControllerCallback(getHandler()::post) {
-                        @Override
-                        public void onNrAdvancedCapableByPcoChanged(
-                                boolean nrAdvancedCapable) {
-                            log("mIsNrAdvancedAllowedByPco=" + nrAdvancedCapable);
-                            mIsNrAdvancedAllowedByPco = nrAdvancedCapable;
-                            sendMessage(EVENT_UPDATE_NR_ADVANCED_STATE);
-                        }
-                    };
-                    mPhone.getDataNetworkController().registerDataNetworkControllerCallback(
-                            mNrAdvancedCapableByPcoChangedCallback);
-                } else if (mNrAdvancedCapablePcoId == 0
-                        && mNrAdvancedCapableByPcoChangedCallback != null) {
-                    mPhone.getDataNetworkController().unregisterDataNetworkControllerCallback(
-                            mNrAdvancedCapableByPcoChangedCallback);
-                    mNrAdvancedCapableByPcoChangedCallback = null;
-                }
-                mEnableNrAdvancedWhileRoaming = b.getBoolean(
-                        CarrierConfigManager.KEY_ENABLE_NR_ADVANCED_WHILE_ROAMING_BOOL);
-                mIsUsingUserDataForRrcDetection = b.getBoolean(
-                        CarrierConfigManager.KEY_LTE_ENDC_USING_USER_DATA_FOR_RRC_DETECTION_BOOL);
-                if (!mIsPhysicalChannelConfig16Supported || mIsUsingUserDataForRrcDetection) {
-                    if (mNrPhysicalLinkStatusChangedCallback == null) {
-                        mNrPhysicalLinkStatusChangedCallback =
-                                new DataNetworkControllerCallback(getHandler()::post) {
-                            @Override
-                            public void onPhysicalLinkStatusChanged(
-                                    @LinkStatus int status) {
-                                sendMessage(obtainMessage(
-                                        EVENT_PHYSICAL_LINK_STATUS_CHANGED,
-                                        new AsyncResult(null, status, null)));
-                            }};
-                        mPhone.getDataNetworkController().registerDataNetworkControllerCallback(
-                                mNrPhysicalLinkStatusChangedCallback);
-                    }
-                } else if (mNrPhysicalLinkStatusChangedCallback != null) {
-                    mPhone.getDataNetworkController().unregisterDataNetworkControllerCallback(
-                            mNrPhysicalLinkStatusChangedCallback);
-                    mNrPhysicalLinkStatusChangedCallback = null;
-                }
+                config = b;
             }
         }
+        mLteEnhancedPattern = config.getString(
+                CarrierConfigManager.KEY_SHOW_CARRIER_DATA_ICON_PATTERN_STRING);
+        mIsTimerResetEnabledForLegacyStateRrcIdle = config.getBoolean(
+                CarrierConfigManager.KEY_NR_TIMERS_RESET_IF_NON_ENDC_AND_RRC_IDLE_BOOL);
+        mLtePlusThresholdBandwidth = config.getInt(
+                CarrierConfigManager.KEY_LTE_PLUS_THRESHOLD_BANDWIDTH_KHZ_INT);
+        mNrAdvancedThresholdBandwidth = config.getInt(
+                CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT);
+        mIncludeLteForNrAdvancedThresholdBandwidth = config.getBoolean(
+                CarrierConfigManager.KEY_INCLUDE_LTE_FOR_NR_ADVANCED_THRESHOLD_BANDWIDTH_BOOL);
+        mEnableNrAdvancedWhileRoaming = config.getBoolean(
+                CarrierConfigManager.KEY_ENABLE_NR_ADVANCED_WHILE_ROAMING_BOOL);
+        mAdditionalNrAdvancedBandsList = config.getIntArray(
+                CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY);
+        mNrAdvancedCapablePcoId = config.getInt(
+                CarrierConfigManager.KEY_NR_ADVANCED_CAPABLE_PCO_ID_INT);
+        if (mNrAdvancedCapablePcoId > 0 && mNrAdvancedCapableByPcoChangedCallback == null) {
+            mNrAdvancedCapableByPcoChangedCallback =
+                    new DataNetworkControllerCallback(getHandler()::post) {
+                        @Override
+                        public void onNrAdvancedCapableByPcoChanged(boolean nrAdvancedCapable) {
+                            log("mIsNrAdvancedAllowedByPco=" + nrAdvancedCapable);
+                            mIsNrAdvancedAllowedByPco = nrAdvancedCapable;
+                            sendMessage(EVENT_UPDATE);
+                        }
+                    };
+            mPhone.getDataNetworkController().registerDataNetworkControllerCallback(
+                    mNrAdvancedCapableByPcoChangedCallback);
+        } else if (mNrAdvancedCapablePcoId == 0 && mNrAdvancedCapableByPcoChangedCallback != null) {
+            mPhone.getDataNetworkController().unregisterDataNetworkControllerCallback(
+                    mNrAdvancedCapableByPcoChangedCallback);
+            mNrAdvancedCapableByPcoChangedCallback = null;
+        }
+        mIsUsingUserDataForRrcDetection = config.getBoolean(
+                CarrierConfigManager.KEY_LTE_ENDC_USING_USER_DATA_FOR_RRC_DETECTION_BOOL);
+        if (!isUsingPhysicalChannelConfigForRrcDetection()) {
+            if (mNrPhysicalLinkStatusChangedCallback == null) {
+                mNrPhysicalLinkStatusChangedCallback =
+                        new DataNetworkControllerCallback(getHandler()::post) {
+                            @Override
+                            public void onPhysicalLinkStatusChanged(@LinkStatus int status) {
+                                sendMessage(obtainMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
+                                        new AsyncResult(null, status, null)));
+                            }
+                        };
+                mPhone.getDataNetworkController().registerDataNetworkControllerCallback(
+                        mNrPhysicalLinkStatusChangedCallback);
+            }
+        } else if (mNrPhysicalLinkStatusChangedCallback != null) {
+            mPhone.getDataNetworkController().unregisterDataNetworkControllerCallback(
+                    mNrPhysicalLinkStatusChangedCallback);
+            mNrPhysicalLinkStatusChangedCallback = null;
+        }
+        String nrIconConfiguration = config.getString(
+                CarrierConfigManager.KEY_5G_ICON_CONFIGURATION_STRING);
+        String overrideTimerRule = config.getString(
+                CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING);
+        String overrideSecondaryTimerRule = config.getString(
+                CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING);
         createTimerRules(nrIconConfiguration, overrideTimerRule, overrideSecondaryTimerRule);
     }
 
@@ -357,7 +341,7 @@
         if (!TextUtils.isEmpty(icons)) {
             // Format: "STATE:ICON,STATE2:ICON2"
             for (String pair : icons.trim().split(",")) {
-                String[] kv = (pair.trim().toLowerCase()).split(":");
+                String[] kv = (pair.trim().toLowerCase(Locale.ROOT)).split(":");
                 if (kv.length != 2) {
                     if (DBG) loge("Invalid 5G icon configuration, config = " + pair);
                     continue;
@@ -384,7 +368,7 @@
         if (!TextUtils.isEmpty(timers)) {
             // Format: "FROM_STATE,TO_STATE,DURATION;FROM_STATE_2,TO_STATE_2,DURATION_2"
             for (String triple : timers.trim().split(";")) {
-                String[] kv = (triple.trim().toLowerCase()).split(",");
+                String[] kv = (triple.trim().toLowerCase(Locale.ROOT)).split(",");
                 if (kv.length != 3) {
                     if (DBG) loge("Invalid 5G icon timer configuration, config = " + triple);
                     continue;
@@ -410,7 +394,7 @@
         if (!TextUtils.isEmpty(secondaryTimers)) {
             // Format: "PRIMARY_STATE,TO_STATE,DURATION;PRIMARY_STATE_2,TO_STATE_2,DURATION_2"
             for (String triple : secondaryTimers.trim().split(";")) {
-                String[] kv = (triple.trim().toLowerCase()).split(",");
+                String[] kv = (triple.trim().toLowerCase(Locale.ROOT)).split(",");
                 if (kv.length != 3) {
                     if (DBG) {
                         loge("Invalid 5G icon secondary timer configuration, config = " + triple);
@@ -452,7 +436,7 @@
         int displayNetworkType = TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE;
         int dataNetworkType = getDataNetworkType();
         boolean nrNsa = isLte(dataNetworkType)
-                && mPhone.getServiceState().getNrState() != NetworkRegistrationInfo.NR_STATE_NONE;
+                && mServiceState.getNrState() != NetworkRegistrationInfo.NR_STATE_NONE;
         boolean nrSa = dataNetworkType == TelephonyManager.NETWORK_TYPE_NR;
 
         // NR display is not accurate when physical channel config notifications are off
@@ -483,7 +467,7 @@
                 keys.add(STATE_CONNECTED_NR_ADVANCED);
             }
         } else {
-            switch (mPhone.getServiceState().getNrState()) {
+            switch (mServiceState.getNrState()) {
                 case NetworkRegistrationInfo.NR_STATE_CONNECTED:
                     if (isNrAdvanced()) {
                         keys.add(STATE_CONNECTED_NR_ADVANCED);
@@ -513,9 +497,9 @@
     private @Annotation.OverrideNetworkType int getLteDisplayType() {
         int value = TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE;
         if ((getDataNetworkType() == TelephonyManager.NETWORK_TYPE_LTE_CA
-                || mPhone.getServiceState().isUsingCarrierAggregation())
-                && (IntStream.of(mPhone.getServiceState().getCellBandwidths()).sum()
-                        > mLtePlusThresholdBandwidth)) {
+                || mServiceState.isUsingCarrierAggregation())
+                && IntStream.of(mServiceState.getCellBandwidths()).sum()
+                > mLtePlusThresholdBandwidth) {
             value = TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA;
         }
         if (isLteEnhancedAvailable()) {
@@ -529,8 +513,8 @@
             return false;
         }
         Pattern stringPattern = Pattern.compile(mLteEnhancedPattern);
-        for (String opName : new String[] {mPhone.getServiceState().getOperatorAlphaLongRaw(),
-                mPhone.getServiceState().getOperatorAlphaShortRaw()}) {
+        for (String opName : new String[] {mServiceState.getOperatorAlphaLongRaw(),
+                mServiceState.getOperatorAlphaShortRaw()}) {
             if (!TextUtils.isEmpty(opName)) {
                 Matcher matcher = stringPattern.matcher(opName);
                 if (matcher.find()) {
@@ -550,8 +534,6 @@
             if (DBG) log("DefaultState: process " + getEventName(msg.what));
             switch (msg.what) {
                 case EVENT_UPDATE:
-                case EVENT_PREFERRED_NETWORK_MODE_CHANGED:
-                    if (DBG) log("Reset timers since preferred network mode changed.");
                     resetAllTimers();
                     transitionToCurrentState();
                     break;
@@ -568,20 +550,10 @@
                     registerForAllEvents();
                     parseCarrierConfigs();
                     break;
-                case EVENT_DATA_RAT_CHANGED:
-                case EVENT_NR_STATE_CHANGED:
-                case EVENT_NR_FREQUENCY_CHANGED:
-                case EVENT_UPDATE_NR_ADVANCED_STATE:
-                    // ignored
-                    break;
-                case EVENT_BANDWIDTH_CHANGED:
-                    // Update in case of LTE/LTE+ switch
-                    updateOverrideNetworkType();
-                    break;
-                case EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED:
-                    if (isUsingPhysicalChannelConfigForRrcDetection()) {
-                        mPhysicalLinkStatus = getPhysicalLinkStatusFromPhysicalChannelConfig();
-                    }
+                case EVENT_SERVICE_STATE_CHANGED:
+                    mServiceState = mPhone.getServiceStateTracker().getServiceState();
+                    if (DBG) log("ServiceState updated: " + mServiceState);
+                    transitionToCurrentState();
                     break;
                 case EVENT_PHYSICAL_LINK_STATUS_CHANGED:
                     AsyncResult ar = (AsyncResult) msg.obj;
@@ -623,6 +595,20 @@
                     resetAllTimers();
                     transitionTo(mLegacyState);
                     break;
+                case EVENT_PREFERRED_NETWORK_MODE_CHANGED:
+                    if (DBG) log("Reset timers since preferred network mode changed.");
+                    resetAllTimers();
+                    transitionToCurrentState();
+                    break;
+                case EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED:
+                    mPhysicalChannelConfigs =
+                            mPhone.getServiceStateTracker().getPhysicalChannelConfigList();
+                    if (DBG) log("Physical channel configs updated: " + mPhysicalChannelConfigs);
+                    if (isUsingPhysicalChannelConfigForRrcDetection()) {
+                        mPhysicalLinkStatus = getPhysicalLinkStatusFromPhysicalChannelConfig();
+                    }
+                    transitionToCurrentState();
+                    break;
                 case EVENT_DEVICE_IDLE_MODE_CHANGED:
                     PowerManager pm = mPhone.getContext().getSystemService(PowerManager.class);
                     mIsDeviceIdleMode = pm.isDeviceIdleMode();
@@ -642,8 +628,6 @@
         }
     }
 
-    private final DefaultState mDefaultState = new DefaultState();
-
     /**
      * Device does not have NR available, due to any of the below reasons:
      * <ul>
@@ -671,11 +655,19 @@
         public boolean processMessage(Message msg) {
             if (DBG) log("LegacyState: process " + getEventName(msg.what));
             updateTimers();
-            int rat = getDataNetworkType();
             switch (msg.what) {
-                case EVENT_DATA_RAT_CHANGED:
+                case EVENT_SERVICE_STATE_CHANGED:
+                    mServiceState = mPhone.getServiceStateTracker().getServiceState();
+                    if (DBG) log("ServiceState updated: " + mServiceState);
+                    // fallthrough
+                case EVENT_UPDATE:
+                    int rat = getDataNetworkType();
                     if (rat == TelephonyManager.NETWORK_TYPE_NR || isLte(rat) && isNrConnected()) {
-                        transitionTo(mNrConnectedState);
+                        if (isNrAdvanced()) {
+                            transitionTo(mNrConnectedAdvancedState);
+                        } else {
+                            transitionTo(mNrConnectedState);
+                        }
                     } else if (isLte(rat) && isNrNotRestricted()) {
                         transitionWithTimerTo(isPhysicalLinkActive()
                                 ? mLteConnectedState : mIdleState);
@@ -688,35 +680,22 @@
                     }
                     mIsNrRestricted = isNrRestricted();
                     break;
-                case EVENT_NR_STATE_CHANGED:
-                    if (isNrConnected()) {
-                        transitionTo(mNrConnectedState);
-                    } else if (isLte(rat) && isNrNotRestricted()) {
-                        transitionWithTimerTo(isPhysicalLinkActive()
-                                ? mLteConnectedState : mIdleState);
-                    } else if (isLte(rat) && isNrRestricted()) {
-                        updateOverrideNetworkType();
-                    }
-                    mIsNrRestricted = isNrRestricted();
-                    break;
-                case EVENT_NR_FREQUENCY_CHANGED:
-                    // ignored
-                    break;
                 case EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED:
+                    mPhysicalChannelConfigs =
+                            mPhone.getServiceStateTracker().getPhysicalChannelConfigList();
+                    if (DBG) log("Physical channel configs updated: " + mPhysicalChannelConfigs);
                     if (isUsingPhysicalChannelConfigForRrcDetection()) {
                         mPhysicalLinkStatus = getPhysicalLinkStatusFromPhysicalChannelConfig();
-                        if (mIsTimerResetEnabledForLegacyStateRRCIdle && !isPhysicalLinkActive()) {
+                        if (mIsTimerResetEnabledForLegacyStateRrcIdle && !isPhysicalLinkActive()) {
                             if (DBG) log("Reset timers since timer reset is enabled for RRC idle.");
                             resetAllTimers();
                         }
                     }
-                    // Update in case of LTE/LTE+ switch
-                    updateOverrideNetworkType();
                     break;
                 case EVENT_PHYSICAL_LINK_STATUS_CHANGED:
                     AsyncResult ar = (AsyncResult) msg.obj;
                     mPhysicalLinkStatus = (int) ar.result;
-                    if (mIsTimerResetEnabledForLegacyStateRRCIdle && !isPhysicalLinkActive()) {
+                    if (mIsTimerResetEnabledForLegacyStateRrcIdle && !isPhysicalLinkActive()) {
                         if (DBG) log("Reset timers since timer reset is enabled for RRC idle.");
                         resetAllTimers();
                         updateOverrideNetworkType();
@@ -758,52 +737,52 @@
             if (DBG) log("IdleState: process " + getEventName(msg.what));
             updateTimers();
             switch (msg.what) {
-                case EVENT_DATA_RAT_CHANGED:
+                case EVENT_SERVICE_STATE_CHANGED:
+                    mServiceState = mPhone.getServiceStateTracker().getServiceState();
+                    if (DBG) log("ServiceState updated: " + mServiceState);
+                    // fallthrough
+                case EVENT_UPDATE:
                     int rat = getDataNetworkType();
-                    if (rat == TelephonyManager.NETWORK_TYPE_NR) {
-                        transitionTo(mNrConnectedState);
+                    if (rat == TelephonyManager.NETWORK_TYPE_NR
+                            || (isLte(rat) && isNrConnected())) {
+                        if (isNrAdvanced()) {
+                            transitionTo(mNrConnectedAdvancedState);
+                        } else {
+                            transitionTo(mNrConnectedState);
+                        }
                     } else if (!isLte(rat) || !isNrNotRestricted()) {
                         transitionWithTimerTo(mLegacyState);
-                    }
-                    break;
-                case EVENT_NR_STATE_CHANGED:
-                    if (isNrConnected()) {
-                        transitionTo(mNrConnectedState);
-                    } else if (!isNrNotRestricted()) {
-                        transitionWithTimerTo(mLegacyState);
-                    }
-                    break;
-                case EVENT_NR_FREQUENCY_CHANGED:
-                    // ignore
-                    break;
-                case EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED:
-                    if (isUsingPhysicalChannelConfigForRrcDetection()) {
-                        mPhysicalLinkStatus = getPhysicalLinkStatusFromPhysicalChannelConfig();
-                        if (isNrNotRestricted()) {
-                            // NOT_RESTRICTED_RRC_IDLE -> NOT_RESTRICTED_RRC_CON
-                            if (isPhysicalLinkActive()) {
-                                transitionWithTimerTo(mLteConnectedState);
-                                break;
-                            }
+                    } else {
+                        if (isPhysicalLinkActive()) {
+                            transitionWithTimerTo(mLteConnectedState);
                         } else {
-                            log("NR state changed. Sending EVENT_NR_STATE_CHANGED");
-                            sendMessage(EVENT_NR_STATE_CHANGED);
+                            // Update in case the override network type changed
+                            updateOverrideNetworkType();
                         }
                     }
-                    // Update in case of LTE/LTE+ switch
-                    updateOverrideNetworkType();
+                    break;
+                case EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED:
+                    mPhysicalChannelConfigs =
+                            mPhone.getServiceStateTracker().getPhysicalChannelConfigList();
+                    if (DBG) log("Physical channel configs updated: " + mPhysicalChannelConfigs);
+                    if (isUsingPhysicalChannelConfigForRrcDetection()) {
+                        mPhysicalLinkStatus = getPhysicalLinkStatusFromPhysicalChannelConfig();
+                        if (isPhysicalLinkActive()) {
+                            transitionWithTimerTo(mLteConnectedState);
+                        } else {
+                            log("Reevaluating state due to link status changed.");
+                            sendMessage(EVENT_UPDATE);
+                        }
+                    }
                     break;
                 case EVENT_PHYSICAL_LINK_STATUS_CHANGED:
                     AsyncResult ar = (AsyncResult) msg.obj;
                     mPhysicalLinkStatus = (int) ar.result;
-                    if (isNrNotRestricted()) {
-                        // NOT_RESTRICTED_RRC_IDLE -> NOT_RESTRICTED_RRC_CON
-                        if (isPhysicalLinkActive()) {
-                            transitionWithTimerTo(mLteConnectedState);
-                        }
+                    if (isPhysicalLinkActive()) {
+                        transitionWithTimerTo(mLteConnectedState);
                     } else {
-                        log("NR state changed. Sending EVENT_NR_STATE_CHANGED");
-                        sendMessage(EVENT_NR_STATE_CHANGED);
+                        log("Reevaluating state due to link status changed.");
+                        sendMessage(EVENT_UPDATE);
                     }
                     break;
                 default:
@@ -842,52 +821,52 @@
             if (DBG) log("LteConnectedState: process " + getEventName(msg.what));
             updateTimers();
             switch (msg.what) {
-                case EVENT_DATA_RAT_CHANGED:
+                case EVENT_SERVICE_STATE_CHANGED:
+                    mServiceState = mPhone.getServiceStateTracker().getServiceState();
+                    if (DBG) log("ServiceState updated: " + mServiceState);
+                    // fallthrough
+                case EVENT_UPDATE:
                     int rat = getDataNetworkType();
-                    if (rat == TelephonyManager.NETWORK_TYPE_NR) {
-                        transitionTo(mNrConnectedState);
+                    if (rat == TelephonyManager.NETWORK_TYPE_NR
+                            || (isLte(rat) && isNrConnected())) {
+                        if (isNrAdvanced()) {
+                            transitionTo(mNrConnectedAdvancedState);
+                        } else {
+                            transitionTo(mNrConnectedState);
+                        }
                     } else if (!isLte(rat) || !isNrNotRestricted()) {
                         transitionWithTimerTo(mLegacyState);
-                    }
-                    break;
-                case EVENT_NR_STATE_CHANGED:
-                    if (isNrConnected()) {
-                        transitionTo(mNrConnectedState);
-                    } else if (!isNrNotRestricted()) {
-                        transitionWithTimerTo(mLegacyState);
-                    }
-                    break;
-                case EVENT_NR_FREQUENCY_CHANGED:
-                    // ignore
-                    break;
-                case EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED:
-                    if (isUsingPhysicalChannelConfigForRrcDetection()) {
-                        mPhysicalLinkStatus = getPhysicalLinkStatusFromPhysicalChannelConfig();
-                        if (isNrNotRestricted()) {
-                            // NOT_RESTRICTED_RRC_CON -> NOT_RESTRICTED_RRC_IDLE
-                            if (!isPhysicalLinkActive()) {
-                                transitionWithTimerTo(mIdleState);
-                                break;
-                            }
+                    } else {
+                        if (!isPhysicalLinkActive()) {
+                            transitionWithTimerTo(mIdleState);
                         } else {
-                            log("NR state changed. Sending EVENT_NR_STATE_CHANGED");
-                            sendMessage(EVENT_NR_STATE_CHANGED);
+                            // Update in case the override network type changed
+                            updateOverrideNetworkType();
                         }
                     }
-                    // Update in case of LTE/LTE+ switch
-                    updateOverrideNetworkType();
+                    break;
+                case EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED:
+                    mPhysicalChannelConfigs =
+                            mPhone.getServiceStateTracker().getPhysicalChannelConfigList();
+                    if (DBG) log("Physical channel configs updated: " + mPhysicalChannelConfigs);
+                    if (isUsingPhysicalChannelConfigForRrcDetection()) {
+                        mPhysicalLinkStatus = getPhysicalLinkStatusFromPhysicalChannelConfig();
+                        if (!isPhysicalLinkActive()) {
+                            transitionWithTimerTo(mIdleState);
+                        } else {
+                            log("Reevaluating state due to link status changed.");
+                            sendMessage(EVENT_UPDATE);
+                        }
+                    }
                     break;
                 case EVENT_PHYSICAL_LINK_STATUS_CHANGED:
                     AsyncResult ar = (AsyncResult) msg.obj;
                     mPhysicalLinkStatus = (int) ar.result;
-                    if (isNrNotRestricted()) {
-                        // NOT_RESTRICTED_RRC_CON -> NOT_RESTRICTED_RRC_IDLE
-                        if (!isPhysicalLinkActive()) {
-                            transitionWithTimerTo(mIdleState);
-                        }
+                    if (!isPhysicalLinkActive()) {
+                        transitionWithTimerTo(mIdleState);
                     } else {
-                        log("NR state changed. Sending EVENT_NR_STATE_CHANGED");
-                        sendMessage(EVENT_NR_STATE_CHANGED);
+                        log("Reevaluating state due to link status changed.");
+                        sendMessage(EVENT_UPDATE);
                     }
                     break;
                 default:
@@ -911,28 +890,35 @@
      * Device is connected to 5G NR as the primary or secondary cell.
      */
     private final class NrConnectedState extends State {
-        private boolean mIsNrAdvanced = false;
-
         @Override
         public void enter() {
-            if (DBG) log("Entering NrConnectedState(" + getName() + ")");
+            if (DBG) log("Entering NrConnectedState");
             updateTimers();
             updateOverrideNetworkType();
             if (!mIsPrimaryTimerActive && !mIsSecondaryTimerActive) {
-                mIsNrAdvanced = isNrAdvanced();
                 mPreviousState = getName();
             }
         }
 
         @Override
         public boolean processMessage(Message msg) {
-            if (DBG) log("NrConnectedState(" + getName() + "): process " + getEventName(msg.what));
+            if (DBG) log("NrConnectedState: process " + getEventName(msg.what));
             updateTimers();
-            int rat = getDataNetworkType();
             switch (msg.what) {
-                case EVENT_DATA_RAT_CHANGED:
-                    if (rat == TelephonyManager.NETWORK_TYPE_NR || isLte(rat) && isNrConnected()) {
-                        updateOverrideNetworkType();
+                case EVENT_SERVICE_STATE_CHANGED:
+                    mServiceState = mPhone.getServiceStateTracker().getServiceState();
+                    if (DBG) log("ServiceState updated: " + mServiceState);
+                    // fallthrough
+                case EVENT_UPDATE:
+                    int rat = getDataNetworkType();
+                    if (rat == TelephonyManager.NETWORK_TYPE_NR
+                            || (isLte(rat) && isNrConnected())) {
+                        if (isNrAdvanced()) {
+                            transitionTo(mNrConnectedAdvancedState);
+                        } else {
+                            // Update in case the override network type changed
+                            updateOverrideNetworkType();
+                        }
                     } else if (isLte(rat) && isNrNotRestricted()) {
                         transitionWithTimerTo(isPhysicalLinkActive()
                                 ? mLteConnectedState : mIdleState);
@@ -940,34 +926,21 @@
                         transitionWithTimerTo(mLegacyState);
                     }
                     break;
-                case EVENT_NR_STATE_CHANGED:
-                    if (isLte(rat) && isNrNotRestricted()) {
-                        transitionWithTimerTo(isPhysicalLinkActive()
-                                ? mLteConnectedState : mIdleState);
-                    } else if (rat != TelephonyManager.NETWORK_TYPE_NR && !isNrConnected()) {
-                        transitionWithTimerTo(mLegacyState);
-                    }
-                    break;
-                case EVENT_UPDATE_NR_ADVANCED_STATE:
-                    updateNrAdvancedState();
-                    break;
-                case EVENT_NR_FREQUENCY_CHANGED:
                 case EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED:
+                    mPhysicalChannelConfigs =
+                            mPhone.getServiceStateTracker().getPhysicalChannelConfigList();
+                    if (DBG) log("Physical channel configs updated: " + mPhysicalChannelConfigs);
                     if (isUsingPhysicalChannelConfigForRrcDetection()) {
                         mPhysicalLinkStatus = getPhysicalLinkStatusFromPhysicalChannelConfig();
                     }
-                    updateNrAdvancedState();
+                    // Check NR advanced in case NR advanced bands were added
+                    if (isNrAdvanced()) {
+                        transitionTo(mNrConnectedAdvancedState);
+                    }
                     break;
                 case EVENT_PHYSICAL_LINK_STATUS_CHANGED:
                     AsyncResult ar = (AsyncResult) msg.obj;
                     mPhysicalLinkStatus = (int) ar.result;
-                    if (!isNrConnected()) {
-                        log("NR state changed. Sending EVENT_NR_STATE_CHANGED");
-                        sendMessage(EVENT_NR_STATE_CHANGED);
-                    }
-                    break;
-                case EVENT_BANDWIDTH_CHANGED:
-                    updateNrAdvancedState();
                     break;
                 default:
                     return NOT_HANDLED;
@@ -980,37 +953,89 @@
 
         @Override
         public String getName() {
-            return mIsNrAdvanced ? STATE_CONNECTED_NR_ADVANCED : STATE_CONNECTED;
-        }
-
-        private void updateNrAdvancedState() {
-            if (!isNrConnected() && getDataNetworkType() != TelephonyManager.NETWORK_TYPE_NR) {
-                log("NR state changed. Sending EVENT_NR_STATE_CHANGED");
-                sendMessage(EVENT_NR_STATE_CHANGED);
-                return;
-            }
-            boolean isNrAdvanced = isNrAdvanced();
-            if (isNrAdvanced != mIsNrAdvanced) {
-                if (!isNrAdvanced) {
-                    if (DBG) log("updateNrAdvancedState: CONNECTED_NR_ADVANCED -> CONNECTED");
-                    transitionWithTimerTo(mNrConnectedState, STATE_CONNECTED);
-                } else {
-                    if (DBG) log("updateNrAdvancedState: CONNECTED -> CONNECTED_NR_ADVANCED");
-                    transitionTo(mNrConnectedState);
-                }
-            }
-            mIsNrAdvanced = isNrAdvanced();
-            log("mIsNrAdvanced=" + mIsNrAdvanced);
+            return STATE_CONNECTED;
         }
     }
 
     private final NrConnectedState mNrConnectedState = new NrConnectedState();
 
-    private void transitionWithTimerTo(IState destState) {
-        transitionWithTimerTo(destState, destState.getName());
+    /**
+     * Device is connected to 5G NR as the primary cell and the data rate is higher than
+     * the generic 5G data rate.
+     */
+    private final class NrConnectedAdvancedState extends State {
+        @Override
+        public void enter() {
+            if (DBG) log("Entering NrConnectedAdvancedState");
+            updateTimers();
+            updateOverrideNetworkType();
+            if (!mIsPrimaryTimerActive && !mIsSecondaryTimerActive) {
+                mPreviousState = getName();
+            }
+        }
+
+        @Override
+        public boolean processMessage(Message msg) {
+            if (DBG) log("NrConnectedAdvancedState: process " + getEventName(msg.what));
+            updateTimers();
+            switch (msg.what) {
+                case EVENT_SERVICE_STATE_CHANGED:
+                    mServiceState = mPhone.getServiceStateTracker().getServiceState();
+                    if (DBG) log("ServiceState updated: " + mServiceState);
+                    // fallthrough
+                case EVENT_UPDATE:
+                    int rat = getDataNetworkType();
+                    if (rat == TelephonyManager.NETWORK_TYPE_NR
+                            || (isLte(rat) && isNrConnected())) {
+                        if (isNrAdvanced()) {
+                            // Update in case the override network type changed
+                            updateOverrideNetworkType();
+                        } else {
+                            transitionWithTimerTo(mNrConnectedState);
+                        }
+                    } else if (isLte(rat) && isNrNotRestricted()) {
+                        transitionWithTimerTo(isPhysicalLinkActive()
+                                ? mLteConnectedState : mIdleState);
+                    } else {
+                        transitionWithTimerTo(mLegacyState);
+                    }
+                    break;
+                case EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED:
+                    mPhysicalChannelConfigs =
+                            mPhone.getServiceStateTracker().getPhysicalChannelConfigList();
+                    if (DBG) log("Physical channel configs updated: " + mPhysicalChannelConfigs);
+                    if (isUsingPhysicalChannelConfigForRrcDetection()) {
+                        mPhysicalLinkStatus = getPhysicalLinkStatusFromPhysicalChannelConfig();
+                    }
+                    // Check NR advanced in case NR advanced bands were removed
+                    if (!isNrAdvanced()) {
+                        transitionWithTimerTo(mNrConnectedState);
+                    }
+                    break;
+                case EVENT_PHYSICAL_LINK_STATUS_CHANGED:
+                    AsyncResult ar = (AsyncResult) msg.obj;
+                    mPhysicalLinkStatus = (int) ar.result;
+                    break;
+                default:
+                    return NOT_HANDLED;
+            }
+            if (!mIsPrimaryTimerActive && !mIsSecondaryTimerActive) {
+                mPreviousState = getName();
+            }
+            return HANDLED;
+        }
+
+        @Override
+        public String getName() {
+            return STATE_CONNECTED_NR_ADVANCED;
+        }
     }
 
-    private void transitionWithTimerTo(IState destState, String destName) {
+    private final NrConnectedAdvancedState mNrConnectedAdvancedState =
+            new NrConnectedAdvancedState();
+
+    private void transitionWithTimerTo(IState destState) {
+        String destName = destState.getName();
         if (DBG) log("Transition with primary timer from " + mPreviousState + " to " + destName);
         OverrideTimerRule rule = mOverrideTimerRules.get(mPreviousState);
         if (!mIsDeviceIdleMode && rule != null && rule.getTimer(destName) > 0) {
@@ -1046,22 +1071,23 @@
     private void transitionToCurrentState() {
         int dataRat = getDataNetworkType();
         IState transitionState;
-        if (dataRat == TelephonyManager.NETWORK_TYPE_NR || isNrConnected()) {
-            transitionState = mNrConnectedState;
-            mPreviousState = isNrAdvanced() ? STATE_CONNECTED_NR_ADVANCED : STATE_CONNECTED;
+        if (dataRat == TelephonyManager.NETWORK_TYPE_NR || (isLte(dataRat) && isNrConnected())) {
+            if (isNrAdvanced()) {
+                transitionState = mNrConnectedAdvancedState;
+            } else {
+                transitionState = mNrConnectedState;
+            }
         } else if (isLte(dataRat) && isNrNotRestricted()) {
             if (isPhysicalLinkActive()) {
                 transitionState = mLteConnectedState;
-                mPreviousState = STATE_NOT_RESTRICTED_RRC_CON;
             } else {
                 transitionState = mIdleState;
-                mPreviousState = STATE_NOT_RESTRICTED_RRC_IDLE;
             }
         } else {
             transitionState = mLegacyState;
-            mPreviousState = isNrRestricted() ? STATE_RESTRICTED : STATE_LEGACY;
         }
         if (!transitionState.equals(getCurrentState())) {
+            mPreviousState = getCurrentState().getName();
             transitionTo(transitionState);
         } else {
             updateOverrideNetworkType();
@@ -1210,17 +1236,15 @@
     }
 
     private boolean isNrConnected() {
-        return mPhone.getServiceState().getNrState() == NetworkRegistrationInfo.NR_STATE_CONNECTED;
+        return mServiceState.getNrState() == NetworkRegistrationInfo.NR_STATE_CONNECTED;
     }
 
     private boolean isNrNotRestricted() {
-        return mPhone.getServiceState().getNrState()
-                == NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED;
+        return mServiceState.getNrState() == NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED;
     }
 
     private boolean isNrRestricted() {
-        return mPhone.getServiceState().getNrState()
-                == NetworkRegistrationInfo.NR_STATE_RESTRICTED;
+        return mServiceState.getNrState() == NetworkRegistrationInfo.NR_STATE_RESTRICTED;
     }
 
     /**
@@ -1235,7 +1259,7 @@
 
         // Check if NR advanced is enabled when the device is roaming. Some carriers disable it
         // while the device is roaming.
-        if (mPhone.getServiceState().getDataRoaming() && !mEnableNrAdvancedWhileRoaming) {
+        if (mServiceState.getDataRoaming() && !mEnableNrAdvancedWhileRoaming) {
             return false;
         }
 
@@ -1249,6 +1273,7 @@
                     .mapToInt(Integer::intValue)
                     .sum();
         }
+
         // Check if meeting minimum bandwidth requirement. For most carriers, there is no minimum
         // bandwidth requirement and mNrAdvancedThresholdBandwidth is 0.
         if (mNrAdvancedThresholdBandwidth > 0 && bandwidths < mNrAdvancedThresholdBandwidth) {
@@ -1261,18 +1286,15 @@
     }
 
     private boolean isNrMmwave() {
-        return mPhone.getServiceState().getNrFrequencyRange()
-                == ServiceState.FREQUENCY_RANGE_MMWAVE;
+        return mServiceState.getNrFrequencyRange() == ServiceState.FREQUENCY_RANGE_MMWAVE;
     }
 
     private boolean isAdditionalNrAdvancedBand() {
-        List<PhysicalChannelConfig> physicalChannelConfigList =
-                mPhone.getServiceStateTracker().getPhysicalChannelConfigList();
         if (ArrayUtils.isEmpty(mAdditionalNrAdvancedBandsList)
-                || physicalChannelConfigList == null) {
+                || mPhysicalChannelConfigs == null) {
             return false;
         }
-        for (PhysicalChannelConfig item : physicalChannelConfigList) {
+        for (PhysicalChannelConfig item : mPhysicalChannelConfigs) {
             if (item.getNetworkType() == TelephonyManager.NETWORK_TYPE_NR
                     && ArrayUtils.contains(mAdditionalNrAdvancedBandsList, item.getBand())) {
                 return true;
@@ -1291,19 +1313,10 @@
     }
 
     private int getPhysicalLinkStatusFromPhysicalChannelConfig() {
-        List<PhysicalChannelConfig> physicalChannelConfigList =
-                mPhone.getServiceStateTracker().getPhysicalChannelConfigList();
-        return (physicalChannelConfigList == null || physicalChannelConfigList.isEmpty())
+        return (mPhysicalChannelConfigs == null || mPhysicalChannelConfigs.isEmpty())
                 ? DataCallResponse.LINK_STATUS_DORMANT : DataCallResponse.LINK_STATUS_ACTIVE;
     }
 
-    private int getDataNetworkType() {
-        NetworkRegistrationInfo nri =  mPhone.getServiceState().getNetworkRegistrationInfo(
-                NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
-        return nri == null ? TelephonyManager.NETWORK_TYPE_UNKNOWN
-                : nri.getAccessNetworkTechnology();
-    }
-
     private String getEventName(int event) {
         try {
             return sEvents[event];
@@ -1350,16 +1363,16 @@
         pw.println("mIsPhysicalChannelConfigOn=" + mIsPhysicalChannelConfigOn);
         pw.println("mIsPrimaryTimerActive=" + mIsPrimaryTimerActive);
         pw.println("mIsSecondaryTimerActive=" + mIsSecondaryTimerActive);
-        pw.println("mIsTimerRestEnabledForLegacyStateRRCIdle="
-                + mIsTimerResetEnabledForLegacyStateRRCIdle);
+        pw.println("mIsTimerResetEnabledForLegacyStateRrcIdle="
+                + mIsTimerResetEnabledForLegacyStateRrcIdle);
         pw.println("mLtePlusThresholdBandwidth=" + mLtePlusThresholdBandwidth);
         pw.println("mNrAdvancedThresholdBandwidth=" + mNrAdvancedThresholdBandwidth);
+        pw.println("mAdditionalNrAdvancedBandsList="
+                + Arrays.toString(mAdditionalNrAdvancedBandsList));
         pw.println("mPrimaryTimerState=" + mPrimaryTimerState);
         pw.println("mSecondaryTimerState=" + mSecondaryTimerState);
         pw.println("mPreviousState=" + mPreviousState);
-        pw.println("mPhysicalLinkStatus=" + mPhysicalLinkStatus);
-        pw.println("mAdditionalNrAdvancedBandsList="
-                + Arrays.toString(mAdditionalNrAdvancedBandsList));
+        pw.println("mPhysicalLinkStatus=" + DataUtils.linkStatusToString(mPhysicalLinkStatus));
         pw.println("mIsPhysicalChannelConfig16Supported=" + mIsPhysicalChannelConfig16Supported);
         pw.println("mIsNrAdvancedAllowedByPco=" + mIsNrAdvancedAllowedByPco);
         pw.println("mNrAdvancedCapablePcoId=" + mNrAdvancedCapablePcoId);
diff --git a/src/java/com/android/internal/telephony/NitzSignal.java b/src/java/com/android/internal/telephony/NitzSignal.java
index 889fe95..2619f3d 100644
--- a/src/java/com/android/internal/telephony/NitzSignal.java
+++ b/src/java/com/android/internal/telephony/NitzSignal.java
@@ -19,7 +19,7 @@
 import android.annotation.DurationMillisLong;
 import android.annotation.ElapsedRealtimeLong;
 import android.annotation.NonNull;
-import android.os.TimestampedValue;
+import android.app.time.UnixEpochTime;
 
 import java.time.Duration;
 import java.util.Objects;
@@ -88,13 +88,12 @@
     }
 
     /**
-     * Creates a {@link android.os.TimestampedValue} containing the UTC time as the number of
-     * milliseconds since the start of the Unix epoch. The reference time is the time according to
-     * the elapsed realtime clock when that would have been the time, accounting for receipt time
-     * and age.
+     * Creates a {@link UnixEpochTime} containing the UTC time as the number of milliseconds since
+     * the start of the Unix epoch. The reference time is the time according to the elapsed realtime
+     * clock when that would have been the time, accounting for receipt time and age.
      */
-    public TimestampedValue<Long> createTimeSignal() {
-        return new TimestampedValue<>(
+    public UnixEpochTime createTimeSignal() {
+        return new UnixEpochTime(
                 getAgeAdjustedElapsedRealtimeMillis(),
                 getNitzData().getCurrentTimeInMillis());
     }
diff --git a/src/java/com/android/internal/telephony/Phone.java b/src/java/com/android/internal/telephony/Phone.java
index 36cf50c..a69e71e 100644
--- a/src/java/com/android/internal/telephony/Phone.java
+++ b/src/java/com/android/internal/telephony/Phone.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_RADIO;
+
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.BroadcastOptions;
@@ -24,6 +26,7 @@
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.res.Configuration;
+import android.hardware.radio.modem.ImeiInfo;
 import android.net.Uri;
 import android.os.AsyncResult;
 import android.os.Build;
@@ -35,16 +38,21 @@
 import android.os.RegistrantList;
 import android.os.SystemClock;
 import android.os.SystemProperties;
+import android.os.UserHandle;
+import android.os.UserManager;
 import android.os.WorkSource;
 import android.preference.PreferenceManager;
 import android.sysprop.TelephonyProperties;
 import android.telecom.VideoProfile;
 import android.telephony.AccessNetworkConstants;
+import android.telephony.Annotation.SrvccState;
 import android.telephony.CarrierConfigManager;
 import android.telephony.CarrierRestrictionRules;
+import android.telephony.CellBroadcastIdRange;
 import android.telephony.CellIdentity;
 import android.telephony.CellInfo;
 import android.telephony.ClientRequestStats;
+import android.telephony.DomainSelectionService;
 import android.telephony.ImsiEncryptionInfo;
 import android.telephony.LinkCapacityEstimate;
 import android.telephony.NetworkRegistrationInfo;
@@ -59,8 +67,10 @@
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyDisplayInfo;
 import android.telephony.TelephonyManager;
+import android.telephony.TelephonyManager.HalService;
 import android.telephony.emergency.EmergencyNumber;
 import android.telephony.ims.RegistrationManager;
+import android.telephony.ims.feature.MmTelFeature;
 import android.telephony.ims.stub.ImsRegistrationImplBase;
 import android.text.TextUtils;
 import android.util.LocalLog;
@@ -77,11 +87,15 @@
 import com.android.internal.telephony.data.DataNetworkController;
 import com.android.internal.telephony.data.DataSettingsManager;
 import com.android.internal.telephony.data.LinkBandwidthEstimator;
+import com.android.internal.telephony.emergency.EmergencyConstants;
 import com.android.internal.telephony.emergency.EmergencyNumberTracker;
+import com.android.internal.telephony.imsphone.ImsCallInfo;
 import com.android.internal.telephony.imsphone.ImsPhone;
 import com.android.internal.telephony.imsphone.ImsPhoneCall;
 import com.android.internal.telephony.metrics.SmsStats;
 import com.android.internal.telephony.metrics.VoiceCallSessionStats;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.test.SimulatedRadioControl;
 import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
 import com.android.internal.telephony.uicc.IccFileHandler;
@@ -229,8 +243,12 @@
     protected static final int EVENT_SUBSCRIPTIONS_CHANGED = 62;
     protected static final int EVENT_GET_USAGE_SETTING_DONE = 63;
     protected static final int EVENT_SET_USAGE_SETTING_DONE = 64;
+    protected static final int EVENT_IMS_DEREGISTRATION_TRIGGERED = 65;
+    protected static final int EVENT_SET_NULL_CIPHER_AND_INTEGRITY_DONE = 66;
+    protected static final int EVENT_GET_DEVICE_IMEI_DONE = 67;
+    protected static final int EVENT_TRIGGER_NOTIFY_ANBR = 68;
 
-    protected static final int EVENT_LAST = EVENT_SET_USAGE_SETTING_DONE;
+    protected static final int EVENT_LAST = EVENT_TRIGGER_NOTIFY_ANBR;
 
     // For shared prefs.
     private static final String GSM_ROAMING_LIST_OVERRIDE_PREFIX = "gsm_roaming_list_";
@@ -257,6 +275,9 @@
     // Integer used to let the calling application know that the we are ignoring auto mode switch.
     private static final int ALREADY_IN_AUTO_SELECTION = 1;
 
+    public static final String PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED =
+            "pref_null_cipher_and_integrity_enabled";
+
     /**
      * This method is invoked when the Phone exits Emergency Callback Mode.
      */
@@ -325,7 +346,7 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     protected AtomicReference<UiccCardApplication> mUiccApplication =
             new AtomicReference<UiccCardApplication>();
-    TelephonyTester mTelephonyTester;
+    private TelephonyTester mTelephonyTester;
     private String mName;
     private final String mActionDetached;
     private final String mActionAttached;
@@ -354,6 +375,12 @@
     private int mUsageSettingFromModem = SubscriptionManager.USAGE_SETTING_UNKNOWN;
     private boolean mIsUsageSettingSupported = true;
 
+    /**
+     * {@code true} if the new SubscriptionManagerService is enabled, otherwise the old
+     * SubscriptionController is used.
+     */
+    private boolean mIsSubscriptionManagerServiceEnabled = false;
+
     //IMS
     /**
      * {@link CallStateException} message text used to indicate that an IMS call has failed because
@@ -382,7 +409,7 @@
     public static final String EXTRA_KEY_ALERT_SHOW = "alertShow";
     public static final String EXTRA_KEY_NOTIFICATION_MESSAGE = "notificationMessage";
 
-    private final RegistrantList mPreciseCallStateRegistrants = new RegistrantList();
+    protected final RegistrantList mPreciseCallStateRegistrants = new RegistrantList();
 
     private final RegistrantList mHandoverRegistrants = new RegistrantList();
 
@@ -430,6 +457,8 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     protected final Context mContext;
 
+    protected SubscriptionManagerService mSubscriptionManagerService;
+
     /**
      * PhoneNotifier is an abstraction for all system-wide
      * state change notification. DefaultPhoneNotifier is
@@ -455,6 +484,10 @@
 
     protected LinkBandwidthEstimator mLinkBandwidthEstimator;
 
+    public static final int IMEI_TYPE_UNKNOWN = -1;
+    public static final int IMEI_TYPE_PRIMARY = ImeiInfo.ImeiType.PRIMARY;
+    public static final int IMEI_TYPE_SECONDARY = ImeiInfo.ImeiType.SECONDARY;
+
     public IccRecords getIccRecords() {
         return mIccRecords.get();
     }
@@ -557,10 +590,6 @@
                 .makeAppSmsManager(context);
         mLocalLog = new LocalLog(64);
 
-        if (TelephonyUtils.IS_DEBUGGABLE) {
-            mTelephonyTester = new TelephonyTester(this);
-        }
-
         setUnitTestMode(unitTestMode);
 
         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
@@ -596,10 +625,20 @@
         // Initialize SMS stats
         mSmsStats = new SmsStats(this);
 
+        mIsSubscriptionManagerServiceEnabled = mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_using_subscription_manager_service);
+        if (isSubscriptionManagerServiceEnabled()) {
+            mSubscriptionManagerService = SubscriptionManagerService.getInstance();
+        }
+
         if (getPhoneType() == PhoneConstants.PHONE_TYPE_IMS) {
             return;
         }
 
+        if (TelephonyUtils.IS_DEBUGGABLE) {
+            mTelephonyTester = new TelephonyTester(this);
+        }
+
         // Initialize device storage and outgoing SMS usage monitors for SMSDispatchers.
         mTelephonyComponentFactory = telephonyComponentFactory;
         mSmsStorageMonitor = mTelephonyComponentFactory.inject(SmsStorageMonitor.class.getName())
@@ -840,7 +879,11 @@
         return null;
     }
 
-    public void notifySrvccState(Call.SrvccState state) {
+    /**
+     * Notifies the change of the SRVCC state.
+     * @param state the new SRVCC state.
+     */
+    public void notifySrvccState(@SrvccState int state) {
     }
 
     public void registerForSilentRedial(Handler h, int what, Object obj) {
@@ -863,6 +906,9 @@
         Call.SrvccState srvccState = Call.SrvccState.NONE;
         if (ret != null && ret.length != 0) {
             int state = ret[0];
+            if (imsPhone != null) {
+                imsPhone.notifySrvccState(state);
+            }
             switch(state) {
                 case TelephonyManager.SRVCC_STATE_HANDOVER_STARTED:
                     srvccState = Call.SrvccState.STARTED;
@@ -875,11 +921,6 @@
                     break;
                 case TelephonyManager.SRVCC_STATE_HANDOVER_COMPLETED:
                     srvccState = Call.SrvccState.COMPLETED;
-                    if (imsPhone != null) {
-                        imsPhone.notifySrvccState(srvccState);
-                    } else {
-                        Rlog.d(LOG_TAG, "HANDOVER_COMPLETED: mImsPhone null");
-                    }
                     break;
                 case TelephonyManager.SRVCC_STATE_HANDOVER_FAILED:
                 case TelephonyManager.SRVCC_STATE_HANDOVER_CANCELED:
@@ -954,17 +995,6 @@
     }
 
     /**
-     * Subclasses of Phone probably want to replace this with a
-     * version scoped to their packages
-     */
-    protected void notifyPreciseCallStateChangedP() {
-        AsyncResult ar = new AsyncResult(null, this, null);
-        mPreciseCallStateRegistrants.notifyRegistrants(ar);
-
-        mNotifier.notifyPreciseCallState(this);
-    }
-
-    /**
      * Notifies when a Handover happens due to SRVCC or Silent Redial
      */
     public void registerForHandoverStateChanged(Handler h, int what, Object obj) {
@@ -1901,7 +1931,7 @@
     public boolean isRadioOffForThermalMitigation() {
         ServiceStateTracker sst = getServiceStateTracker();
         return sst != null && sst.getRadioPowerOffReasons()
-                .contains(Phone.RADIO_POWER_REASON_THERMAL);
+                .contains(TelephonyManager.RADIO_POWER_REASON_THERMAL);
     }
 
     /**
@@ -2363,17 +2393,29 @@
      * Loads the allowed network type from subscription database.
      */
     public void loadAllowedNetworksFromSubscriptionDatabase() {
-        // Try to load ALLOWED_NETWORK_TYPES from SIMINFO.
-        if (SubscriptionController.getInstance() == null) {
-            return;
+        String result = null;
+        if (isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = mSubscriptionManagerService
+                    .getSubscriptionInfoInternal(getSubId());
+            if (subInfo != null) {
+                result = subInfo.getAllowedNetworkTypesForReasons();
+            }
+        } else {
+            // Try to load ALLOWED_NETWORK_TYPES from SIMINFO.
+            if (SubscriptionController.getInstance() == null) {
+                return;
+            }
+
+            result = SubscriptionController.getInstance().getSubscriptionProperty(
+                    getSubId(),
+                    SubscriptionManager.ALLOWED_NETWORK_TYPES);
         }
 
-        String result = SubscriptionController.getInstance().getSubscriptionProperty(
-                getSubId(),
-                SubscriptionManager.ALLOWED_NETWORK_TYPES);
         // After fw load network type from DB, do unlock if subId is valid.
-        mIsAllowedNetworkTypesLoadedFromDb = SubscriptionManager.isValidSubscriptionId(getSubId());
-        if (result == null) {
+        mIsAllowedNetworkTypesLoadedFromDb = SubscriptionManager.isValidSubscriptionId(
+                getSubId());
+
+        if (TextUtils.isEmpty(result)) {
             return;
         }
 
@@ -2383,7 +2425,7 @@
         try {
             // Format: "REASON=VALUE,REASON2=VALUE2"
             for (String pair : result.trim().split(",")) {
-                String[] networkTypesValues = (pair.trim().toLowerCase()).split("=");
+                String[] networkTypesValues = (pair.trim().toLowerCase(Locale.ROOT)).split("=");
                 if (networkTypesValues.length != 2) {
                     Rlog.e(LOG_TAG, "Invalid ALLOWED_NETWORK_TYPES from DB, value = " + pair);
                     continue;
@@ -4041,6 +4083,9 @@
      */
     @UnsupportedAppUsage
     public int getSubId() {
+        if (isSubscriptionManagerServiceEnabled()) {
+            return mSubscriptionManagerService.getSubId(mPhoneId);
+        }
         if (SubscriptionController.getInstance() == null) {
             // TODO b/78359408 getInstance sometimes returns null in Treehugger tests, which causes
             // flakiness. Even though we haven't seen this crash in the wild we should keep this
@@ -4048,7 +4093,7 @@
             Rlog.e(LOG_TAG, "SubscriptionController.getInstance = null! Returning default subId");
             return SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
         }
-        return SubscriptionController.getInstance().getSubIdUsingPhoneId(mPhoneId);
+        return SubscriptionController.getInstance().getSubId(mPhoneId);
     }
 
     /**
@@ -4344,6 +4389,7 @@
         // When radio capability switch is done, query IMEI value and update it in Phone objects
         // to make it in sync with the IMEI value currently used by Logical-Modem.
         if (capabilitySwitched) {
+            mCi.getImei(obtainMessage(EVENT_GET_DEVICE_IMEI_DONE));
             mCi.getDeviceIdentity(obtainMessage(EVENT_GET_DEVICE_IDENTITY_DONE));
         }
     }
@@ -4362,7 +4408,16 @@
     }
 
     private int getResolvedUsageSetting(int subId) {
-        SubscriptionInfo subInfo = SubscriptionController.getInstance().getSubscriptionInfo(subId);
+        SubscriptionInfo subInfo = null;
+        if (isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfoInternal = mSubscriptionManagerService
+                    .getSubscriptionInfoInternal(subId);
+            if (subInfoInternal != null) {
+                subInfo = subInfoInternal.toSubscriptionInfo();
+            }
+        } else {
+            subInfo = SubscriptionController.getInstance().getSubscriptionInfo(subId);
+        }
 
         if (subInfo == null
                 || subInfo.getUsageSetting() == SubscriptionManager.USAGE_SETTING_UNKNOWN) {
@@ -4698,10 +4753,24 @@
      * Get the HAL version.
      *
      * @return the current HalVersion
+     *
+     * @deprecated Use {@link #getHalVersion(int service)} instead.
      */
+    @Deprecated
     public HalVersion getHalVersion() {
+        return getHalVersion(HAL_SERVICE_RADIO);
+    }
+
+    /**
+     * Get the HAL version with a specific service.
+     *
+     * @param service the service id to query
+     * @return the current HalVersion for a specific service
+     *
+     */
+    public HalVersion getHalVersion(@HalService int service) {
         if (mCi != null && mCi instanceof RIL) {
-            return ((RIL) mCi).getHalVersion();
+            return ((RIL) mCi).getHalVersion(service);
         }
         return RIL.RADIO_HAL_VERSION_UNKNOWN;
     }
@@ -4844,6 +4913,308 @@
         return mIsAllowedNetworkTypesLoadedFromDb;
     }
 
+    /**
+     * Returns the user's last setting for terminal-based call waiting
+     * @param forCsOnly indicates the caller expects the result for CS calls only
+     */
+    public int getTerminalBasedCallWaitingState(boolean forCsOnly) {
+        return CallWaitingController.TERMINAL_BASED_NOT_SUPPORTED;
+    }
+
+    /**
+     * Notifies the change of the user setting of the terminal-based call waiting service
+     * to IMS service.
+     */
+    public void setTerminalBasedCallWaitingStatus(int state) {
+    }
+
+    /**
+     * Notifies that the IMS service connected supports the terminal-based call waiting service
+     */
+    public void setTerminalBasedCallWaitingSupported(boolean supported) {
+    }
+
+    /**
+     * Notifies the NAS and RRC layers of the radio the type of upcoming IMS traffic.
+     *
+     * @param token A nonce to identify the request.
+     * @param trafficType IMS traffic type like registration, voice, video, SMS, emergency, and etc.
+     * @param accessNetworkType The type of the radio access network used.
+     * @param trafficDirection Indicates whether traffic is originated by mobile originated or
+     *        mobile terminated use case eg. MO/MT call/SMS etc.
+     * @param response is callback message.
+     */
+    public void startImsTraffic(int token,
+            @MmTelFeature.ImsTrafficType int trafficType,
+            @AccessNetworkConstants.RadioAccessNetworkType int accessNetworkType,
+            @MmTelFeature.ImsTrafficDirection int trafficDirection, Message response) {
+        mCi.startImsTraffic(token, trafficType, accessNetworkType, trafficDirection, response);
+    }
+
+    /**
+     * Notifies IMS traffic has been stopped.
+     *
+     * @param token The token assigned by startImsTraffic.
+     * @param response is callback message.
+     */
+    public void stopImsTraffic(int token, Message response) {
+        mCi.stopImsTraffic(token, response);
+    }
+
+    /**
+     * Register for notifications of connection setup failure
+     *
+     * @param h Handler for notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+    public void registerForConnectionSetupFailure(Handler h, int what, Object obj) {
+        mCi.registerForConnectionSetupFailure(h, what, obj);
+    }
+
+    /**
+     * Unregister for notifications of connection setup failure
+     *
+     * @param h Handler to be removed from the registrant list.
+     */
+    public void unregisterForConnectionSetupFailure(Handler h) {
+        mCi.unregisterForConnectionSetupFailure(h);
+    }
+
+    /**
+     * Triggers the UE initiated EPS fallback procedure.
+     *
+     * @param reason specifies the reason for EPS fallback.
+     * @param response is callback message.
+     */
+    public void triggerEpsFallback(@MmTelFeature.EpsFallbackReason int reason, Message response) {
+        mCi.triggerEpsFallback(reason, response);
+    }
+
+    /**
+     * Notifies the recommended bit rate for the indicated logical channel and direction.
+     *
+     * @param mediaType MediaType is used to identify media stream such as audio or video.
+     * @param direction Direction of this packet stream (e.g. uplink or downlink).
+     * @param bitsPerSecond The recommended bit rate for the UE for a specific logical channel and
+     *        a specific direction by NW.
+     */
+    public void triggerNotifyAnbr(int mediaType, int direction, int bitsPerSecond) {
+    }
+
+    /**
+     * Sets the emergency mode
+     *
+     * @param emcMode The radio emergency mode type.
+     * @param result Callback message.
+     */
+    public void setEmergencyMode(@EmergencyConstants.EmergencyMode int emcMode,
+            @Nullable Message result) {
+        mCi.setEmergencyMode(emcMode, result);
+    }
+
+    /**
+     * Triggers an emergency network scan.
+     *
+     * @param accessNetwork Contains the list of access network types to be prioritized
+     *        during emergency scan. The 1st entry has the highest priority.
+     * @param scanType Indicates the type of scans to be performed i.e. limited scan,
+     *        full service scan or any scan.
+     * @param result Callback message.
+     */
+    public void triggerEmergencyNetworkScan(
+            @NonNull @AccessNetworkConstants.RadioAccessNetworkType int[] accessNetwork,
+            @DomainSelectionService.EmergencyScanType int scanType, @Nullable Message result) {
+        mCi.triggerEmergencyNetworkScan(accessNetwork, scanType, result);
+    }
+
+    /**
+     * Cancels ongoing emergency network scan
+     * @param resetScan Indicates how the next {@link #triggerEmergencyNetworkScan} should work.
+     *        If {@code true}, then the modem shall start the new scan from the beginning,
+     *        otherwise the modem shall resume from the last search.
+     * @param result Callback message.
+     */
+    public void cancelEmergencyNetworkScan(boolean resetScan, @Nullable Message result) {
+        mCi.cancelEmergencyNetworkScan(resetScan, result);
+    }
+
+    /**
+     * Exits ongoing emergency mode
+     * @param result Callback message.
+     */
+    public void exitEmergencyMode(@Nullable Message result) {
+        mCi.exitEmergencyMode(result);
+    }
+
+    /**
+     * Registers for emergency network scan result.
+     *
+     * @param h Handler for notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+    public void registerForEmergencyNetworkScan(@NonNull Handler h,
+            int what, @Nullable Object obj) {
+        mCi.registerForEmergencyNetworkScan(h, what, obj);
+    }
+
+    /**
+     * Unregisters for emergency network scan result.
+     *
+     * @param h Handler to be removed from the registrant list.
+     */
+    public void unregisterForEmergencyNetworkScan(@NonNull Handler h) {
+        mCi.unregisterForEmergencyNetworkScan(h);
+    }
+
+    /**
+     * Notifies that IMS deregistration is triggered.
+     *
+     * @param reason the reason why the deregistration is triggered.
+     */
+    public void triggerImsDeregistration(
+            @ImsRegistrationImplBase.ImsDeregistrationReason int reason) {
+        if (mImsPhone != null) {
+            mImsPhone.triggerImsDeregistration(reason);
+        }
+    }
+
+    /**
+     * Registers for the domain selected for emergency calls.
+     *
+     * @param h Handler for notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+    public void registerForEmergencyDomainSelected(
+            @NonNull Handler h, int what, @Nullable Object obj) {
+    }
+
+    /**
+     * Unregisters for the domain selected for emergency calls.
+     *
+     * @param h Handler to be removed from the registrant list.
+     */
+    public void unregisterForEmergencyDomainSelected(@NonNull Handler h) {
+    }
+
+    /**
+     * Notifies the domain selected.
+     *
+     * @param transportType The preferred transport type.
+     */
+    public void notifyEmergencyDomainSelected(
+            @AccessNetworkConstants.TransportType int transportType) {
+    }
+
+    /**
+     * @return Telephony tester instance.
+     */
+    public @Nullable TelephonyTester getTelephonyTester() {
+        return mTelephonyTester;
+    }
+
+    /**
+     * @return {@code true} if the new {@link SubscriptionManagerService} is enabled, otherwise the
+     * old {@link SubscriptionController} is used.
+     */
+    public boolean isSubscriptionManagerServiceEnabled() {
+        return mIsSubscriptionManagerServiceEnabled;
+    }
+
+    /**
+     * @return UserHandle from phone sub id, or null if subscription is invalid.
+     */
+    public UserHandle getUserHandle() {
+        SubscriptionManager subManager = mContext.getSystemService(SubscriptionManager.class);
+        int subId = getSubId();
+        return subManager.isValidSubscriptionId(subId)
+                ? subManager.getSubscriptionUserHandle(subId)
+                : null;
+    }
+
+    /**
+     * Checks if the context user is a managed profile.
+     *
+     * Note that this applies specifically to <em>managed</em> profiles.
+     *
+     * @return whether the context user is a managed profile.
+     */
+    public boolean isManagedProfile() {
+        UserHandle userHandle = getUserHandle();
+        UserManager userManager = mContext.getSystemService(UserManager.class);
+        if (userHandle == null || userManager == null) return false;
+        return userManager.isManagedProfile(userHandle.getIdentifier());
+    }
+
+    /**
+     * @return global null cipher and integrity enabled preference
+     */
+    public boolean getNullCipherAndIntegrityEnabledPreference() {
+        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
+        return sp.getBoolean(PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, true);
+    }
+
+    /**
+     * @return whether or not this Phone interacts with a modem that supports the null cipher
+     * and integrity feature.
+     */
+    public boolean isNullCipherAndIntegritySupported() {
+        return false;
+    }
+
+    /**
+     * Override to implement handling of an update to the enablement of the null cipher and
+     * integrity preference.
+     * {@see #PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED}
+     */
+    public void handleNullCipherEnabledChange() {
+    }
+
+    /**
+     * Notifies the IMS call status to the modem.
+     *
+     * @param imsCallInfo The list of {@link ImsCallInfo}.
+     * @param response A callback to receive the response.
+     */
+    public void updateImsCallStatus(@NonNull List<ImsCallInfo> imsCallInfo, Message response) {
+        mCi.updateImsCallStatus(imsCallInfo, response);
+    }
+
+    /**
+     * Enables or disables N1 mode (access to 5G core network) in accordance with
+     * 3GPP TS 24.501 4.9.
+     * @param enable {@code true} to enable N1 mode, {@code false} to disable N1 mode.
+     * @param result Callback message to receive the result.
+     */
+    public void setN1ModeEnabled(boolean enable, Message result) {
+        mCi.setN1ModeEnabled(enable, result);
+    }
+
+    /**
+     * Check whether N1 mode (access to 5G core network) is enabled or not.
+     * @param result Callback message to receive the result.
+     */
+    public void isN1ModeEnabled(Message result) {
+        mCi.isN1ModeEnabled(result);
+    }
+
+    /**
+     * Return current cell broadcast ranges.
+     */
+    public List<CellBroadcastIdRange> getCellBroadcastIdRanges() {
+        return new ArrayList<>();
+    }
+
+    /**
+     * Set reception of cell broadcast messages with the list of the given ranges.
+     */
+    public void setCellBroadcastIdRanges(
+            @NonNull List<CellBroadcastIdRange> ranges, Consumer<Integer> callback) {
+        callback.accept(TelephonyManager.CELL_BROADCAST_RESULT_UNSUPPORTED);
+    }
+
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         pw.println("Phone: subId=" + getSubId());
         pw.println(" mPhoneId=" + mPhoneId);
@@ -4866,7 +5237,7 @@
         pw.println(" isDnsCheckDisabled()=" + isDnsCheckDisabled());
         pw.println(" getUnitTestMode()=" + getUnitTestMode());
         pw.println(" getState()=" + getState());
-        pw.println(" getIccSerialNumber()=" + getIccSerialNumber());
+        pw.println(" getIccSerialNumber()=" + Rlog.pii(LOG_TAG, getIccSerialNumber()));
         pw.println(" getIccRecordsLoaded()=" + getIccRecordsLoaded());
         pw.println(" getMessageWaitingIndicator()=" + getMessageWaitingIndicator());
         pw.println(" getCallForwardingIndicator()=" + getCallForwardingIndicator());
diff --git a/src/java/com/android/internal/telephony/PhoneConfigurationManager.java b/src/java/com/android/internal/telephony/PhoneConfigurationManager.java
index 05c325e..6f42872 100644
--- a/src/java/com/android/internal/telephony/PhoneConfigurationManager.java
+++ b/src/java/com/android/internal/telephony/PhoneConfigurationManager.java
@@ -35,6 +35,7 @@
 import android.util.Log;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.telephony.Rlog;
 
 import java.util.HashMap;
@@ -71,6 +72,7 @@
     private TelephonyManager mTelephonyManager;
     private static final RegistrantList sMultiSimConfigChangeRegistrants = new RegistrantList();
     private static final String ALLOW_MOCK_MODEM_PROPERTY = "persist.radio.allow_mock_modem";
+    private static final String BOOT_ALLOW_MOCK_MODEM_PROPERTY = "ro.boot.radio.allow_mock_modem";
     private static final boolean DEBUG = !"user".equals(Build.TYPE);
     /**
      * Init method to instantiate the object
@@ -122,6 +124,21 @@
         }
     }
 
+    // If virtual DSDA is enabled for this UE, then updates maxActiveVoiceSubscriptions to 2.
+    private PhoneCapability maybeUpdateMaxActiveVoiceSubscriptions(
+            final PhoneCapability staticCapability) {
+        boolean enableVirtualDsda = mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_enable_virtual_dsda);
+
+        if (staticCapability.getLogicalModemList().size() > 1 && enableVirtualDsda) {
+            return new PhoneCapability.Builder(staticCapability)
+                    .setMaxActiveVoiceSubscriptions(2)
+                    .build();
+        } else {
+            return staticCapability;
+        }
+    }
+
     /**
      * Static method to get instance.
      */
@@ -180,6 +197,8 @@
                     ar = (AsyncResult) msg.obj;
                     if (ar != null && ar.exception == null) {
                         mStaticCapability = (PhoneCapability) ar.result;
+                        mStaticCapability =
+                                maybeUpdateMaxActiveVoiceSubscriptions(mStaticCapability);
                         notifyCapabilityChanged();
                     } else {
                         log(msg.what + " failure. Not getting phone capability." + ar.exception);
@@ -366,7 +385,11 @@
             // eg if we are going from 2 phones to 1 phone, we need to deregister RIL for the
             // second phone. This loop does nothing if numOfActiveModems is increasing.
             for (int phoneId = numOfActiveModems; phoneId < oldNumOfActiveModems; phoneId++) {
-                SubscriptionController.getInstance().clearSubInfoRecord(phoneId);
+                if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                    SubscriptionManagerService.getInstance().markSubscriptionsInactive(phoneId);
+                } else {
+                    SubscriptionController.getInstance().clearSubInfoRecord(phoneId);
+                }
                 subInfoCleared = true;
                 mPhones[phoneId].mCi.onSlotActiveStatusChange(
                         SubscriptionManager.isValidPhoneId(phoneId));
@@ -400,8 +423,13 @@
                         + "setting VOICE & SMS subId to -1 (No Preference)");
 
                 //Set the default VOICE subId to -1 ("No Preference")
-                SubscriptionController.getInstance().setDefaultVoiceSubId(
-                        SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+                if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                    SubscriptionManagerService.getInstance().setDefaultVoiceSubId(
+                            SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+                } else {
+                    SubscriptionController.getInstance().setDefaultVoiceSubId(
+                            SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+                }
 
                 //TODO:: Set the default SMS sub to "No Preference". Tracking this bug (b/227386042)
             } else {
@@ -465,34 +493,46 @@
      * @return true if the modem service is set successfully, false otherwise.
      */
     public boolean setModemService(String serviceName) {
-        if (mRadioConfig == null || mPhones[0] == null) {
-            return false;
-        }
-
         log("setModemService: " + serviceName);
         boolean statusRadioConfig = false;
         boolean statusRil = false;
         final boolean isAllowed = SystemProperties.getBoolean(ALLOW_MOCK_MODEM_PROPERTY, false);
+        final boolean isAllowedForBoot =
+                SystemProperties.getBoolean(BOOT_ALLOW_MOCK_MODEM_PROPERTY, false);
 
-        // Check for ALLOW_MOCK_MODEM_PROPERTY on user builds
-        if (isAllowed || DEBUG) {
-            if (serviceName != null) {
+        // Check for ALLOW_MOCK_MODEM_PROPERTY and BOOT_ALLOW_MOCK_MODEM_PROPERTY on user builds
+        if (isAllowed || isAllowedForBoot || DEBUG) {
+            if (mRadioConfig != null) {
                 statusRadioConfig = mRadioConfig.setModemService(serviceName);
-
-                //TODO: consider multi-sim case (b/210073692)
-                statusRil = mPhones[0].mCi.setModemService(serviceName);
-            } else {
-                statusRadioConfig = mRadioConfig.setModemService(null);
-
-                //TODO: consider multi-sim case
-                statusRil = mPhones[0].mCi.setModemService(null);
             }
 
-            return statusRadioConfig && statusRil;
+            if (!statusRadioConfig) {
+                loge("setModemService: switching modem service for radioconfig fail");
+                return false;
+            }
+
+            for (int i = 0; i < getPhoneCount(); i++) {
+                if (mPhones[i] != null) {
+                    statusRil = mPhones[i].mCi.setModemService(serviceName);
+                }
+
+                if (!statusRil) {
+                    loge("setModemService: switch modem for radio " + i + " fail");
+
+                    // Disconnect the switched service
+                    mRadioConfig.setModemService(null);
+                    for (int t = 0; t < i; t++) {
+                        mPhones[t].mCi.setModemService(null);
+                    }
+                    return false;
+                }
+            }
         } else {
             loge("setModemService is not allowed");
             return false;
         }
+
+        return true;
     }
 
      /**
@@ -500,7 +540,6 @@
      * @return the service name of the connected service.
      */
     public String getModemService() {
-        //TODO: consider multi-sim case
         if (mPhones[0] == null) {
             return "";
         }
diff --git a/src/java/com/android/internal/telephony/PhoneFactory.java b/src/java/com/android/internal/telephony/PhoneFactory.java
index 3361b74..43aa154 100644
--- a/src/java/com/android/internal/telephony/PhoneFactory.java
+++ b/src/java/com/android/internal/telephony/PhoneFactory.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_RADIO;
+
 import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_CDMA;
 import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_CDMA_LTE;
 
@@ -50,6 +52,7 @@
 import com.android.internal.telephony.imsphone.ImsPhoneFactory;
 import com.android.internal.telephony.metrics.MetricsCollector;
 import com.android.internal.telephony.metrics.TelephonyMetrics;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.UiccController;
 import com.android.internal.telephony.util.NotificationChannelController;
 import com.android.internal.util.IndentingPrintWriter;
@@ -83,6 +86,7 @@
     private static IntentBroadcaster sIntentBroadcaster;
     private static @Nullable EuiccController sEuiccController;
     private static @Nullable EuiccCardController sEuiccCardController;
+    private static SubscriptionManagerService sSubscriptionManagerService;
 
     static private SubscriptionInfoUpdater sSubInfoRecordUpdater = null;
 
@@ -102,6 +106,8 @@
     private static MetricsCollector sMetricsCollector;
     private static RadioInterfaceCapabilityController sRadioHalCapabilities;
 
+    private static boolean sSubscriptionManagerServiceEnabled = false;
+
     //***** Class Methods
 
     public static void makeDefaultPhones(Context context) {
@@ -117,6 +123,10 @@
         synchronized (sLockProxyPhones) {
             if (!sMadeDefaults) {
                 sContext = context;
+
+                sSubscriptionManagerServiceEnabled = context.getResources().getBoolean(
+                        com.android.internal.R.bool.config_using_subscription_manager_service);
+
                 // create the telephony device controller.
                 TelephonyDevController.create();
 
@@ -179,7 +189,7 @@
 
                 if (numPhones > 0) {
                     final RadioConfig radioConfig = RadioConfig.make(context,
-                            sCommandsInterfaces[0].getHalVersion());
+                            sCommandsInterfaces[0].getHalVersion(HAL_SERVICE_RADIO));
                     sRadioHalCapabilities = RadioInterfaceCapabilityController.init(radioConfig,
                             sCommandsInterfaces[0]);
                 } else {
@@ -194,12 +204,24 @@
                 // call getInstance()
                 sUiccController = UiccController.make(context);
 
-                Rlog.i(LOG_TAG, "Creating SubscriptionController");
-                TelephonyComponentFactory.getInstance().inject(SubscriptionController.class.
-                        getName()).initSubscriptionController(context);
+
+                if (isSubscriptionManagerServiceEnabled()) {
+                    Rlog.i(LOG_TAG, "Creating SubscriptionManagerService");
+                    sSubscriptionManagerService = new SubscriptionManagerService(context,
+                            Looper.myLooper());
+                } else {
+                    Rlog.i(LOG_TAG, "Creating SubscriptionController");
+                    TelephonyComponentFactory.getInstance().inject(SubscriptionController.class
+                            .getName()).initSubscriptionController(context);
+                }
+
+                SubscriptionController sc = null;
+                if (!isSubscriptionManagerServiceEnabled()) {
+                    sc = SubscriptionController.getInstance();
+                }
+
                 TelephonyComponentFactory.getInstance().inject(MultiSimSettingController.class.
-                        getName()).initMultiSimSettingController(context,
-                        SubscriptionController.getInstance());
+                        getName()).initMultiSimSettingController(context, sc);
 
                 if (context.getPackageManager().hasSystemFeature(
                         PackageManager.FEATURE_TELEPHONY_EUICC)) {
@@ -231,13 +253,15 @@
 
                 sMadeDefaults = true;
 
-                Rlog.i(LOG_TAG, "Creating SubInfoRecordUpdater ");
-                HandlerThread pfhandlerThread = new HandlerThread("PhoneFactoryHandlerThread");
-                pfhandlerThread.start();
-                sSubInfoRecordUpdater = TelephonyComponentFactory.getInstance().inject(
-                        SubscriptionInfoUpdater.class.getName()).
-                        makeSubscriptionInfoUpdater(pfhandlerThread.
-                        getLooper(), context, SubscriptionController.getInstance());
+                if (!isSubscriptionManagerServiceEnabled()) {
+                    Rlog.i(LOG_TAG, "Creating SubInfoRecordUpdater ");
+                    HandlerThread pfhandlerThread = new HandlerThread("PhoneFactoryHandlerThread");
+                    pfhandlerThread.start();
+                    sSubInfoRecordUpdater = TelephonyComponentFactory.getInstance().inject(
+                            SubscriptionInfoUpdater.class.getName())
+                            .makeSubscriptionInfoUpdater(pfhandlerThread.getLooper(), context,
+                                    SubscriptionController.getInstance());
+                }
 
                 // Only bring up IMS if the device supports having an IMS stack.
                 if (context.getPackageManager().hasSystemFeature(
@@ -278,6 +302,14 @@
     }
 
     /**
+     * @return {@code true} if the new {@link SubscriptionManagerService} is enabled, otherwise the
+     * old {@link SubscriptionController} is used.
+     */
+    public static boolean isSubscriptionManagerServiceEnabled() {
+        return sSubscriptionManagerServiceEnabled;
+    }
+
+    /**
      * Upon single SIM to dual SIM switch or vice versa, we dynamically allocate or de-allocate
      * Phone and CommandInterface objects.
      * @param context
@@ -433,6 +465,9 @@
     /* Gets the default subscription */
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     public static int getDefaultSubscription() {
+        if (isSubscriptionManagerServiceEnabled()) {
+            return SubscriptionManagerService.getInstance().getDefaultSubId();
+        }
         return SubscriptionController.getInstance().getDefaultSubId();
     }
 
@@ -576,21 +611,36 @@
         pw.decreaseIndent();
         pw.println("++++++++++++++++++++++++++++++++");
 
-        pw.println("SubscriptionController:");
-        pw.increaseIndent();
-        try {
-            SubscriptionController.getInstance().dump(fd, pw, args);
-        } catch (Exception e) {
-            e.printStackTrace();
+        if (isSubscriptionManagerServiceEnabled()) {
+            SubscriptionManagerService.getInstance().dump(fd, pw, args);
+        } else {
+            pw.println("SubscriptionController:");
+            pw.increaseIndent();
+            try {
+                SubscriptionController.getInstance().dump(fd, pw, args);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+            pw.flush();
+            pw.decreaseIndent();
+            pw.println("++++++++++++++++++++++++++++++++");
+
+            pw.println("SubInfoRecordUpdater:");
+            pw.increaseIndent();
+            try {
+                sSubInfoRecordUpdater.dump(fd, pw, args);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
         }
         pw.flush();
         pw.decreaseIndent();
         pw.println("++++++++++++++++++++++++++++++++");
 
-        pw.println("SubInfoRecordUpdater:");
+        pw.println("sRadioHalCapabilities:");
         pw.increaseIndent();
         try {
-            sSubInfoRecordUpdater.dump(fd, pw, args);
+            sRadioHalCapabilities.dump(fd, pw, args);
         } catch (Exception e) {
             e.printStackTrace();
         }
diff --git a/src/java/com/android/internal/telephony/PhoneInternalInterface.java b/src/java/com/android/internal/telephony/PhoneInternalInterface.java
index 5b4d5e5..32c0c73 100644
--- a/src/java/com/android/internal/telephony/PhoneInternalInterface.java
+++ b/src/java/com/android/internal/telephony/PhoneInternalInterface.java
@@ -16,7 +16,6 @@
 
 package com.android.internal.telephony;
 
-import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.compat.annotation.UnsupportedAppUsage;
 import android.os.Build;
@@ -36,9 +35,9 @@
 
 import com.android.internal.telephony.PhoneConstants.DataState;
 
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.function.Consumer;
 
 /**
@@ -211,16 +210,6 @@
     static final String REASON_DATA_UNTHROTTLED = "dataUnthrottled";
     static final String REASON_TRAFFIC_DESCRIPTORS_UPDATED = "trafficDescriptorsUpdated";
 
-    // Reasons for Radio being powered off
-    int RADIO_POWER_REASON_USER = 0;
-    int RADIO_POWER_REASON_THERMAL = 1;
-    @Retention(RetentionPolicy.SOURCE)
-    @IntDef(prefix = {"RADIO_POWER_REASON_"},
-        value = {
-            RADIO_POWER_REASON_USER,
-            RADIO_POWER_REASON_THERMAL})
-    public @interface RadioPowerReason {}
-
     // Used for band mode selection methods
     static final int BM_UNSPECIFIED = RILConstants.BAND_MODE_UNSPECIFIED; // automatic
     static final int BM_EURO_BAND   = RILConstants.BAND_MODE_EURO;
@@ -593,8 +582,9 @@
      * getServiceState().getState() will not change immediately after this call.
      * registerForServiceStateChanged() to find out when the
      * request is complete. This will set the reason for radio power state as {@link
-     * #RADIO_POWER_REASON_USER}. This will not guarantee that the requested radio power state will
-     * actually be set. See {@link #setRadioPowerForReason(boolean, boolean, boolean, boolean, int)}
+     * android.telephony.TelephonyManager#RADIO_POWER_REASON_USER}. This will not guarantee that the
+     * requested radio power state will actually be set.
+     * See {@link #setRadioPowerForReason(boolean, boolean, boolean, boolean, int)}
      * for details.
      *
      * @param power true means "on", false means "off".
@@ -620,8 +610,9 @@
      * getServiceState().getState() will not change immediately after this call.
      * registerForServiceStateChanged() to find out when the
      * request is complete. This will set the reason for radio power state as {@link
-     * #RADIO_POWER_REASON_USER}. This will not guarantee that the requested radio power state will
-     * actually be set. See {@link #setRadioPowerForReason(boolean, boolean, boolean, boolean, int)}
+     * android.telephony.TelephonyManager#RADIO_POWER_REASON_USER}. This will not guarantee that the
+     * requested radio power state will actually be set.
+     * See {@link #setRadioPowerForReason(boolean, boolean, boolean, boolean, int)}
      * for details.
      *
      * @param power true means "on", false means "off".
@@ -638,7 +629,7 @@
     default void setRadioPower(boolean power, boolean forEmergencyCall,
             boolean isSelectedPhoneForEmergencyCall, boolean forceApply) {
         setRadioPowerForReason(power, forEmergencyCall, isSelectedPhoneForEmergencyCall, forceApply,
-                RADIO_POWER_REASON_USER);
+                TelephonyManager.RADIO_POWER_REASON_USER);
     }
 
     /**
@@ -656,11 +647,19 @@
      * @param power true means "on", false means "off".
      * @param reason RadioPowerReason constant defining the reason why the radio power was set.
      */
-    default void setRadioPowerForReason(boolean power, @RadioPowerReason int reason) {
+    default void setRadioPowerForReason(boolean power,
+            @TelephonyManager.RadioPowerReason int reason) {
         setRadioPowerForReason(power, false, false, false, reason);
     }
 
     /**
+     * @return reasons for powering off radio.
+     */
+    default Set<Integer> getRadioPowerOffReasons() {
+        return new HashSet<>();
+    }
+
+    /**
      * Sets the radio power on/off state with option to specify whether it's for emergency call
      * (off is sometimes called "airplane mode") and option to set the reason for setting the power
      * state. Current state can be gotten via {@link #getServiceState()}.
@@ -686,7 +685,7 @@
      */
     default void setRadioPowerForReason(boolean power, boolean forEmergencyCall,
             boolean isSelectedPhoneForEmergencyCall, boolean forceApply,
-            @RadioPowerReason int reason) {}
+            @TelephonyManager.RadioPowerReason int reason) {}
 
     /**
      * Get the line 1 phone number (MSISDN). For CDMA phones, the MDN is returned
@@ -1036,6 +1035,10 @@
     String getImei();
 
     /**
+     * Retrieves IMEI type for phones.
+     */
+    int getImeiType();
+    /**
      * Retrieves the IccPhoneBookInterfaceManager of the Phone
      */
     public IccPhoneBookInterfaceManager getIccPhoneBookInterfaceManager();
diff --git a/src/java/com/android/internal/telephony/PhoneNotifier.java b/src/java/com/android/internal/telephony/PhoneNotifier.java
index 701a157..2a439a3 100644
--- a/src/java/com/android/internal/telephony/PhoneNotifier.java
+++ b/src/java/com/android/internal/telephony/PhoneNotifier.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.compat.annotation.UnsupportedAppUsage;
+import android.telephony.Annotation;
 import android.telephony.Annotation.RadioPowerState;
 import android.telephony.Annotation.SrvccState;
 import android.telephony.BarringInfo;
@@ -33,6 +34,7 @@
 import android.telephony.TelephonyManager.DataEnabledReason;
 import android.telephony.emergency.EmergencyNumber;
 import android.telephony.ims.ImsReasonInfo;
+import android.telephony.ims.MediaQualityStatus;
 
 import java.util.List;
 
@@ -78,7 +80,10 @@
 
     void notifyCellInfo(Phone sender, List<CellInfo> cellInfo);
 
-    void notifyPreciseCallState(Phone sender);
+    /** Send a notification that precise call state changed. */
+    void notifyPreciseCallState(Phone sender, String[] imsCallIds,
+            @Annotation.ImsCallServiceType int[] imsCallServiceTypes,
+            @Annotation.ImsCallType int[] imsCallTypes);
 
     void notifyDisconnectCause(Phone sender, int cause, int preciseCause);
 
@@ -113,6 +118,9 @@
     /** Notify of a change to the call quality of an active foreground call. */
     void notifyCallQualityChanged(Phone sender, CallQuality callQuality, int callNetworkType);
 
+    /** Notify of a change to the media quality status of an active foreground call. */
+    void notifyMediaQualityStatusChanged(Phone sender, MediaQualityStatus status);
+
     /** Notify registration failed */
     void notifyRegistrationFailed(Phone sender, @NonNull CellIdentity cellIdentity,
             @NonNull String chosenPlmn, int domain, int causeCode, int additionalCauseCode);
diff --git a/src/java/com/android/internal/telephony/PhoneSubInfoController.java b/src/java/com/android/internal/telephony/PhoneSubInfoController.java
index b1dfa5f..fafa245 100644
--- a/src/java/com/android/internal/telephony/PhoneSubInfoController.java
+++ b/src/java/com/android/internal/telephony/PhoneSubInfoController.java
@@ -27,6 +27,7 @@
 import android.compat.annotation.UnsupportedAppUsage;
 import android.content.Context;
 import android.content.pm.PackageManager;
+import android.net.Uri;
 import android.os.Binder;
 import android.os.Build;
 import android.os.RemoteException;
@@ -36,9 +37,13 @@
 import android.telephony.PhoneNumberUtils;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyFrameworkInitializer;
+import android.text.TextUtils;
 import android.util.EventLog;
 
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.IsimRecords;
+import com.android.internal.telephony.uicc.SIMRecords;
 import com.android.internal.telephony.uicc.UiccCardApplication;
 import com.android.internal.telephony.uicc.UiccPort;
 import com.android.telephony.Rlog;
@@ -153,8 +158,13 @@
         long identity = Binder.clearCallingIdentity();
         boolean isActive;
         try {
-            isActive = SubscriptionController.getInstance().isActiveSubId(subId, callingPackage,
-                    callingFeatureId);
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                isActive = SubscriptionManagerService.getInstance().isActiveSubId(subId,
+                        callingPackage, callingFeatureId);
+            } else {
+                isActive = SubscriptionController.getInstance().isActiveSubId(subId, callingPackage,
+                        callingFeatureId);
+            }
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -168,6 +178,14 @@
             }
             identity = Binder.clearCallingIdentity();
             try {
+                if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                    SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
+                            .getSubscriptionInfoInternal(subId);
+                    if (subInfo != null && !TextUtils.isEmpty(subInfo.getImsi())) {
+                        return subInfo.getImsi();
+                    }
+                    return null;
+                }
                 return SubscriptionController.getInstance().getImsiPrivileged(subId);
             } finally {
                 Binder.restoreCallingIdentity(identity);
@@ -418,6 +436,29 @@
                 });
     }
 
+    /**
+     * Returns the USIM service table that fetched from EFUST elementary field that are loaded
+     * based on the appType.
+     */
+    public String getSimServiceTable(int subId, int appType) throws RemoteException {
+        return callPhoneMethodForSubIdWithPrivilegedCheck(subId, "getSimServiceTable",
+                (phone) -> {
+                    UiccPort uiccPort = phone.getUiccPort();
+                    if (uiccPort == null || uiccPort.getUiccProfile() == null) {
+                        loge("getSimServiceTable(): uiccPort or uiccProfile is null");
+                        return null;
+                    }
+                    UiccCardApplication uiccApp = uiccPort.getUiccProfile().getApplicationByType(
+                            appType);
+                    if (uiccApp == null) {
+                        loge("getSimServiceTable(): no app with specified apptype="
+                                + appType);
+                        return null;
+                    }
+                    return ((SIMRecords)uiccApp.getIccRecords()).getSimServiceTable();
+                });
+    }
+
     @Override
     public String getIccSimChallengeResponse(int subId, int appType, int authType, String data,
             String callingPackage, String callingFeatureId) throws RemoteException {
@@ -438,7 +479,9 @@
             }
 
             if (authType != UiccCardApplication.AUTH_CONTEXT_EAP_SIM
-                    && authType != UiccCardApplication.AUTH_CONTEXT_EAP_AKA) {
+                    && authType != UiccCardApplication.AUTH_CONTEXT_EAP_AKA
+                    && authType != UiccCardApplication.AUTH_CONTEXT_GBA_BOOTSTRAP
+                    && authType != UiccCardApplication.AUTHTYPE_GBA_NAF_KEY_EXTERNAL) {
                 loge("getIccSimChallengeResponse() unsupported authType: " + authType);
                 return null;
             }
@@ -491,7 +534,7 @@
             if (phone != null) {
                 return callMethodHelper.callMethod(phone);
             } else {
-                loge(message + " phone is null for Subscription:" + subId);
+                if (VDBG) loge(message + " phone is null for Subscription:" + subId);
                 return null;
             }
         } finally {
@@ -581,6 +624,37 @@
         }
     }
 
+    /**
+     * Returns SIP URI or tel URI of the Public Service Identity of the SM-SC fetched from
+     * EF_PSISMSC elementary field as defined in Section 4.5.9 (3GPP TS 31.102).
+     * @throws IllegalStateException in case if phone or UiccApplication is not available.
+     */
+    public Uri getSmscIdentity(int subId, int appType) throws RemoteException {
+        Uri smscIdentityUri = callPhoneMethodForSubIdWithPrivilegedCheck(subId, "getSmscIdentity",
+                (phone) -> {
+                    try {
+                        String smscIdentity = null;
+                        UiccPort uiccPort = phone.getUiccPort();
+                        UiccCardApplication uiccApp =
+                                uiccPort.getUiccProfile().getApplicationByType(
+                                        appType);
+                        smscIdentity = (uiccApp != null) ? uiccApp.getIccRecords().getSmscIdentity()
+                                : null;
+                        if (TextUtils.isEmpty(smscIdentity)) {
+                            return Uri.EMPTY;
+                        }
+                        return Uri.parse(smscIdentity);
+                    } catch (NullPointerException ex) {
+                        Rlog.e(TAG, "getSmscIdentity(): Exception = " + ex);
+                        return null;
+                    }
+                });
+        if (smscIdentityUri == null) {
+            throw new IllegalStateException("Telephony service error");
+        }
+        return smscIdentityUri;
+    }
+
     private void log(String s) {
         Rlog.d(TAG, s);
     }
diff --git a/src/java/com/android/internal/telephony/RIL.java b/src/java/com/android/internal/telephony/RIL.java
index 6de6652..51fa529 100644
--- a/src/java/com/android/internal/telephony/RIL.java
+++ b/src/java/com/android/internal/telephony/RIL.java
@@ -16,6 +16,15 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_DATA;
+import static android.telephony.TelephonyManager.HAL_SERVICE_IMS;
+import static android.telephony.TelephonyManager.HAL_SERVICE_MESSAGING;
+import static android.telephony.TelephonyManager.HAL_SERVICE_MODEM;
+import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
+import static android.telephony.TelephonyManager.HAL_SERVICE_RADIO;
+import static android.telephony.TelephonyManager.HAL_SERVICE_SIM;
+import static android.telephony.TelephonyManager.HAL_SERVICE_VOICE;
+
 import static com.android.internal.telephony.RILConstants.*;
 
 import android.annotation.NonNull;
@@ -45,7 +54,9 @@
 import android.os.WorkSource;
 import android.provider.Settings;
 import android.sysprop.TelephonyProperties;
+import android.telephony.AccessNetworkConstants;
 import android.telephony.AccessNetworkConstants.AccessNetworkType;
+import android.telephony.BarringInfo;
 import android.telephony.CarrierRestrictionRules;
 import android.telephony.CellInfo;
 import android.telephony.CellSignalStrengthCdma;
@@ -55,6 +66,7 @@
 import android.telephony.CellSignalStrengthTdscdma;
 import android.telephony.CellSignalStrengthWcdma;
 import android.telephony.ClientRequestStats;
+import android.telephony.DomainSelectionService;
 import android.telephony.ImsiEncryptionInfo;
 import android.telephony.ModemActivityInfo;
 import android.telephony.NeighboringCellInfo;
@@ -67,18 +79,24 @@
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyHistogram;
 import android.telephony.TelephonyManager;
+import android.telephony.TelephonyManager.HalService;
 import android.telephony.TelephonyManager.PrefNetworkMode;
 import android.telephony.data.DataProfile;
 import android.telephony.data.NetworkSliceInfo;
 import android.telephony.data.TrafficDescriptor;
 import android.telephony.emergency.EmergencyNumber;
+import android.telephony.ims.RegistrationManager;
+import android.telephony.ims.feature.ConnectionFailureInfo;
+import android.telephony.ims.stub.ImsRegistrationImplBase;
 import android.text.TextUtils;
 import android.util.SparseArray;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.cdma.CdmaInformationRecords;
 import com.android.internal.telephony.cdma.CdmaSmsBroadcastConfigInfo;
+import com.android.internal.telephony.emergency.EmergencyConstants;
 import com.android.internal.telephony.gsm.SmsBroadcastConfigInfo;
+import com.android.internal.telephony.imsphone.ImsCallInfo;
 import com.android.internal.telephony.metrics.ModemRestartStats;
 import com.android.internal.telephony.metrics.TelephonyMetrics;
 import com.android.internal.telephony.nano.TelephonyProto.SmsSession;
@@ -92,8 +110,10 @@
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Objects;
 import java.util.Set;
@@ -133,6 +153,9 @@
     private final ClientWakelockTracker mClientWakelockTracker = new ClientWakelockTracker();
 
     /** @hide */
+    public static final HalVersion RADIO_HAL_VERSION_UNSUPPORTED = HalVersion.UNSUPPORTED;
+
+    /** @hide */
     public static final HalVersion RADIO_HAL_VERSION_UNKNOWN = HalVersion.UNKNOWN;
 
     /** @hide */
@@ -159,8 +182,11 @@
     /** @hide */
     public static final HalVersion RADIO_HAL_VERSION_2_0 = new HalVersion(2, 0);
 
-    // IRadio version
-    private HalVersion mRadioVersion = RADIO_HAL_VERSION_UNKNOWN;
+    /** @hide */
+    public static final HalVersion RADIO_HAL_VERSION_2_1 = new HalVersion(2, 1);
+
+    // Hal version
+    private Map<Integer, HalVersion> mHalVersion = new HashMap<>();
 
     //***** Instance Variables
 
@@ -198,15 +224,9 @@
 
     private static final String PROPERTY_IS_VONR_ENABLED = "persist.radio.is_vonr_enabled_";
 
-    static final int RADIO_SERVICE = 0;
-    static final int DATA_SERVICE = 1;
-    static final int MESSAGING_SERVICE = 2;
-    static final int MODEM_SERVICE = 3;
-    static final int NETWORK_SERVICE = 4;
-    static final int SIM_SERVICE = 5;
-    static final int VOICE_SERVICE = 6;
-    static final int MIN_SERVICE_IDX = RADIO_SERVICE;
-    static final int MAX_SERVICE_IDX = VOICE_SERVICE;
+    public static final int MIN_SERVICE_IDX = HAL_SERVICE_RADIO;
+
+    public static final int MAX_SERVICE_IDX = HAL_SERVICE_IMS;
 
     /**
      * An array of sets that records if services are disabled in the HAL for a specific phone ID
@@ -233,6 +253,8 @@
     private volatile IRadio mRadioProxy = null;
     private DataResponse mDataResponse;
     private DataIndication mDataIndication;
+    private ImsResponse mImsResponse;
+    private ImsIndication mImsIndication;
     private MessagingResponse mMessagingResponse;
     private MessagingIndication mMessagingIndication;
     private ModemResponse mModemResponse;
@@ -250,6 +272,9 @@
     final RilHandler mRilHandler;
     private MockModem mMockModem;
 
+    // The last barring information received
+    private BarringInfo mLastBarringInfo = null;
+
     // Thread-safe HashMap to map from RIL_REQUEST_XXX constant to HalVersion.
     // This is for Radio HAL Fallback Compatibility feature. When a RIL request
     // is received, the HAL method from the mapping HalVersion here (if present),
@@ -413,7 +438,8 @@
         public void serviceDied(long cookie) {
             // Deal with service going away
             riljLog("serviceDied");
-            mRilHandler.sendMessage(mRilHandler.obtainMessage(EVENT_RADIO_PROXY_DEAD, RADIO_SERVICE,
+            mRilHandler.sendMessage(mRilHandler.obtainMessage(EVENT_RADIO_PROXY_DEAD,
+                    HAL_SERVICE_RADIO,
                     0 /* ignored arg2 */, cookie));
         }
     }
@@ -428,8 +454,11 @@
 
         public void linkToDeath(IBinder service) throws RemoteException {
             if (service != null) {
+                riljLog("Linked to death for service " + serviceToString(mService));
                 mBinder = service;
                 mBinder.linkToDeath(this, (int) mServiceCookies.get(mService).incrementAndGet());
+            } else {
+                riljLoge("Unable to link to death for service " + serviceToString(mService));
             }
         }
 
@@ -450,7 +479,7 @@
     }
 
     private synchronized void resetProxyAndRequestList(int service) {
-        if (service == RADIO_SERVICE) {
+        if (service == HAL_SERVICE_RADIO) {
             mRadioProxy = null;
         } else {
             mServiceProxies.get(service).clear();
@@ -467,7 +496,7 @@
         // Clear request list on close
         clearRequestList(RADIO_NOT_AVAILABLE, false);
 
-        if (service == RADIO_SERVICE) {
+        if (service == HAL_SERVICE_RADIO) {
             getRadioProxy(null);
         } else {
             getRadioServiceProxy(service, null);
@@ -496,13 +525,13 @@
             // Disable HIDL service
             if (mRadioProxy != null) {
                 riljLog("Disable HIDL service");
-                mDisabledRadioServices.get(RADIO_SERVICE).add(mPhoneId);
+                mDisabledRadioServices.get(HAL_SERVICE_RADIO).add(mPhoneId);
             }
 
             mMockModem.bindAllMockModemService();
 
             for (int service = MIN_SERVICE_IDX; service <= MAX_SERVICE_IDX; service++) {
-                if (service == RADIO_SERVICE) continue;
+                if (service == HAL_SERVICE_RADIO) continue;
 
                 int retryCount = 0;
                 IBinder binder;
@@ -537,14 +566,26 @@
         if ((serviceName == null) || (!serviceBound)) {
             if (serviceBound) riljLog("Unbinding to MockModemService");
 
-            if (mDisabledRadioServices.get(RADIO_SERVICE).contains(mPhoneId)) {
-                mDisabledRadioServices.get(RADIO_SERVICE).clear();
+            if (mDisabledRadioServices.get(HAL_SERVICE_RADIO).contains(mPhoneId)) {
+                mDisabledRadioServices.get(HAL_SERVICE_RADIO).clear();
             }
 
             if (mMockModem != null) {
-                mRadioVersion = RADIO_HAL_VERSION_UNKNOWN;
                 mMockModem = null;
                 for (int service = MIN_SERVICE_IDX; service <= MAX_SERVICE_IDX; service++) {
+                    if (service == HAL_SERVICE_RADIO) {
+                        if (isRadioVersion2_0()) {
+                            mHalVersion.put(service, RADIO_HAL_VERSION_2_0);
+                        } else {
+                            mHalVersion.put(service, RADIO_HAL_VERSION_UNKNOWN);
+                        }
+                    } else {
+                        if (isRadioServiceSupported(service)) {
+                            mHalVersion.put(service, RADIO_HAL_VERSION_UNKNOWN);
+                        } else {
+                            mHalVersion.put(service, RADIO_HAL_VERSION_UNSUPPORTED);
+                        }
+                    }
                     resetProxyAndRequestList(service);
                 }
             }
@@ -586,7 +627,10 @@
     /** Returns a {@link IRadio} instance or null if the service is not available. */
     @VisibleForTesting
     public synchronized IRadio getRadioProxy(Message result) {
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_2_0)) return null;
+        if (mHalVersion.containsKey(HAL_SERVICE_RADIO)
+                && mHalVersion.get(HAL_SERVICE_RADIO).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+            return null;
+        }
         if (!SubscriptionManager.isValidPhoneId(mPhoneId)) return null;
         if (!mIsCellularSupported) {
             if (RILJ_LOGV) riljLog("getRadioProxy: Not calling getService(): wifi-only");
@@ -603,14 +647,14 @@
         }
 
         try {
-            if (mDisabledRadioServices.get(RADIO_SERVICE).contains(mPhoneId)) {
+            if (mDisabledRadioServices.get(HAL_SERVICE_RADIO).contains(mPhoneId)) {
                 riljLoge("getRadioProxy: mRadioProxy for " + HIDL_SERVICE_NAME[mPhoneId]
                         + " is disabled");
             } else {
                 try {
                     mRadioProxy = android.hardware.radio.V1_6.IRadio.getService(
                             HIDL_SERVICE_NAME[mPhoneId], true);
-                    mRadioVersion = RADIO_HAL_VERSION_1_6;
+                    mHalVersion.put(HAL_SERVICE_RADIO, RADIO_HAL_VERSION_1_6);
                 } catch (NoSuchElementException e) {
                 }
 
@@ -618,7 +662,7 @@
                     try {
                         mRadioProxy = android.hardware.radio.V1_5.IRadio.getService(
                                 HIDL_SERVICE_NAME[mPhoneId], true);
-                        mRadioVersion = RADIO_HAL_VERSION_1_5;
+                        mHalVersion.put(HAL_SERVICE_RADIO, RADIO_HAL_VERSION_1_5);
                     } catch (NoSuchElementException e) {
                     }
                 }
@@ -627,7 +671,7 @@
                     try {
                         mRadioProxy = android.hardware.radio.V1_4.IRadio.getService(
                                 HIDL_SERVICE_NAME[mPhoneId], true);
-                        mRadioVersion = RADIO_HAL_VERSION_1_4;
+                        mHalVersion.put(HAL_SERVICE_RADIO, RADIO_HAL_VERSION_1_4);
                     } catch (NoSuchElementException e) {
                     }
                 }
@@ -636,7 +680,7 @@
                     try {
                         mRadioProxy = android.hardware.radio.V1_3.IRadio.getService(
                                 HIDL_SERVICE_NAME[mPhoneId], true);
-                        mRadioVersion = RADIO_HAL_VERSION_1_3;
+                        mHalVersion.put(HAL_SERVICE_RADIO, RADIO_HAL_VERSION_1_3);
                     } catch (NoSuchElementException e) {
                     }
                 }
@@ -645,7 +689,7 @@
                     try {
                         mRadioProxy = android.hardware.radio.V1_2.IRadio.getService(
                                 HIDL_SERVICE_NAME[mPhoneId], true);
-                        mRadioVersion = RADIO_HAL_VERSION_1_2;
+                        mHalVersion.put(HAL_SERVICE_RADIO, RADIO_HAL_VERSION_1_2);
                     } catch (NoSuchElementException e) {
                     }
                 }
@@ -654,7 +698,7 @@
                     try {
                         mRadioProxy = android.hardware.radio.V1_1.IRadio.getService(
                                 HIDL_SERVICE_NAME[mPhoneId], true);
-                        mRadioVersion = RADIO_HAL_VERSION_1_1;
+                        mHalVersion.put(HAL_SERVICE_RADIO, RADIO_HAL_VERSION_1_1);
                     } catch (NoSuchElementException e) {
                     }
                 }
@@ -663,7 +707,7 @@
                     try {
                         mRadioProxy = android.hardware.radio.V1_0.IRadio.getService(
                                 HIDL_SERVICE_NAME[mPhoneId], true);
-                        mRadioVersion = RADIO_HAL_VERSION_1_0;
+                        mHalVersion.put(HAL_SERVICE_RADIO, RADIO_HAL_VERSION_1_0);
                     } catch (NoSuchElementException e) {
                     }
                 }
@@ -672,11 +716,11 @@
                     if (!mIsRadioProxyInitialized) {
                         mIsRadioProxyInitialized = true;
                         mRadioProxy.linkToDeath(mRadioProxyDeathRecipient,
-                                mServiceCookies.get(RADIO_SERVICE).incrementAndGet());
+                                mServiceCookies.get(HAL_SERVICE_RADIO).incrementAndGet());
                         mRadioProxy.setResponseFunctions(mRadioResponse, mRadioIndication);
                     }
                 } else {
-                    mDisabledRadioServices.get(RADIO_SERVICE).add(mPhoneId);
+                    mDisabledRadioServices.get(HAL_SERVICE_RADIO).add(mPhoneId);
                     riljLoge("getRadioProxy: set mRadioProxy for "
                             + HIDL_SERVICE_NAME[mPhoneId] + " as disabled");
                 }
@@ -701,29 +745,32 @@
 
     /**
      * Returns a {@link RadioDataProxy}, {@link RadioMessagingProxy}, {@link RadioModemProxy},
-     * {@link RadioNetworkProxy}, {@link RadioSimProxy}, {@link RadioVoiceProxy}, or an empty {@link RadioServiceProxy}
-     * if the service is not available.
+     * {@link RadioNetworkProxy}, {@link RadioSimProxy}, {@link RadioVoiceProxy},
+     * {@link RadioImsProxy}, or null if the service is not available.
      */
     @NonNull
     public <T extends RadioServiceProxy> T getRadioServiceProxy(Class<T> serviceClass,
             Message result) {
         if (serviceClass == RadioDataProxy.class) {
-            return (T) getRadioServiceProxy(DATA_SERVICE, result);
+            return (T) getRadioServiceProxy(HAL_SERVICE_DATA, result);
         }
         if (serviceClass == RadioMessagingProxy.class) {
-            return (T) getRadioServiceProxy(MESSAGING_SERVICE, result);
+            return (T) getRadioServiceProxy(HAL_SERVICE_MESSAGING, result);
         }
         if (serviceClass == RadioModemProxy.class) {
-            return (T) getRadioServiceProxy(MODEM_SERVICE, result);
+            return (T) getRadioServiceProxy(HAL_SERVICE_MODEM, result);
         }
         if (serviceClass == RadioNetworkProxy.class) {
-            return (T) getRadioServiceProxy(NETWORK_SERVICE, result);
+            return (T) getRadioServiceProxy(HAL_SERVICE_NETWORK, result);
         }
         if (serviceClass == RadioSimProxy.class) {
-            return (T) getRadioServiceProxy(SIM_SERVICE, result);
+            return (T) getRadioServiceProxy(HAL_SERVICE_SIM, result);
         }
         if (serviceClass == RadioVoiceProxy.class) {
-            return (T) getRadioServiceProxy(VOICE_SERVICE, result);
+            return (T) getRadioServiceProxy(HAL_SERVICE_VOICE, result);
+        }
+        if (serviceClass == RadioImsProxy.class) {
+            return (T) getRadioServiceProxy(HAL_SERVICE_IMS, result);
         }
         riljLoge("getRadioServiceProxy: unrecognized " + serviceClass);
         return null;
@@ -731,12 +778,15 @@
 
     /**
      * Returns a {@link RadioServiceProxy}, which is empty if the service is not available.
-     * For RADIO_SERVICE, use {@link #getRadioProxy} instead, as this will always return null.
+     * For HAL_SERVICE_RADIO, use {@link #getRadioProxy} instead, as this will always return null.
      */
     @VisibleForTesting
     @NonNull
     public synchronized RadioServiceProxy getRadioServiceProxy(int service, Message result) {
         if (!SubscriptionManager.isValidPhoneId(mPhoneId)) return mServiceProxies.get(service);
+        if (service == HAL_SERVICE_IMS && !isRadioServiceSupported(service)) {
+            return mServiceProxies.get(service);
+        }
         if (!mIsCellularSupported) {
             if (RILJ_LOGV) riljLog("getRadioServiceProxy: Not calling getService(): wifi-only");
             if (result != null) {
@@ -753,168 +803,190 @@
         }
 
         try {
-            if (mDisabledRadioServices.get(service).contains(mPhoneId)) {
+            if (mMockModem == null && mDisabledRadioServices.get(service).contains(mPhoneId)) {
                 riljLoge("getRadioServiceProxy: " + serviceToString(service) + " for "
                         + HIDL_SERVICE_NAME[mPhoneId] + " is disabled");
             } else {
                 IBinder binder;
                 switch (service) {
-                    case DATA_SERVICE:
+                    case HAL_SERVICE_DATA:
                         if (mMockModem == null) {
                             binder = ServiceManager.waitForDeclaredService(
                                     android.hardware.radio.data.IRadioData.DESCRIPTOR + "/"
                                             + HIDL_SERVICE_NAME[mPhoneId]);
                         } else {
-                            binder = mMockModem.getServiceBinder(DATA_SERVICE);
+                            binder = mMockModem.getServiceBinder(HAL_SERVICE_DATA);
                         }
                         if (binder != null) {
-                            mRadioVersion = RADIO_HAL_VERSION_2_0;
-                            ((RadioDataProxy) serviceProxy).setAidl(mRadioVersion,
+                            mHalVersion.put(service, ((RadioDataProxy) serviceProxy).setAidl(
+                                    mHalVersion.get(service),
                                     android.hardware.radio.data.IRadioData.Stub.asInterface(
-                                            binder));
+                                            binder)));
                         }
                         break;
-                    case MESSAGING_SERVICE:
+                    case HAL_SERVICE_MESSAGING:
                         if (mMockModem == null) {
                             binder = ServiceManager.waitForDeclaredService(
                                     android.hardware.radio.messaging.IRadioMessaging.DESCRIPTOR
                                             + "/" + HIDL_SERVICE_NAME[mPhoneId]);
                         } else {
-                            binder = mMockModem.getServiceBinder(MESSAGING_SERVICE);
+                            binder = mMockModem.getServiceBinder(HAL_SERVICE_MESSAGING);
                         }
                         if (binder != null) {
-                            mRadioVersion = RADIO_HAL_VERSION_2_0;
-                            ((RadioMessagingProxy) serviceProxy).setAidl(mRadioVersion,
+                            mHalVersion.put(service, ((RadioMessagingProxy) serviceProxy).setAidl(
+                                    mHalVersion.get(service),
                                     android.hardware.radio.messaging.IRadioMessaging.Stub
-                                            .asInterface(binder));
+                                            .asInterface(binder)));
                         }
                         break;
-                    case MODEM_SERVICE:
+                    case HAL_SERVICE_MODEM:
                         if (mMockModem == null) {
                             binder = ServiceManager.waitForDeclaredService(
                                     android.hardware.radio.modem.IRadioModem.DESCRIPTOR + "/"
                                             + HIDL_SERVICE_NAME[mPhoneId]);
                         } else {
-                            binder = mMockModem.getServiceBinder(MODEM_SERVICE);
+                            binder = mMockModem.getServiceBinder(HAL_SERVICE_MODEM);
                         }
                         if (binder != null) {
-                            mRadioVersion = RADIO_HAL_VERSION_2_0;
-                            ((RadioModemProxy) serviceProxy).setAidl(mRadioVersion,
+                            mHalVersion.put(service, ((RadioModemProxy) serviceProxy).setAidl(
+                                    mHalVersion.get(service),
                                     android.hardware.radio.modem.IRadioModem.Stub
-                                            .asInterface(binder));
+                                            .asInterface(binder)));
                         }
                         break;
-                    case NETWORK_SERVICE:
+                    case HAL_SERVICE_NETWORK:
                         if (mMockModem == null) {
                             binder = ServiceManager.waitForDeclaredService(
                                     android.hardware.radio.network.IRadioNetwork.DESCRIPTOR + "/"
                                             + HIDL_SERVICE_NAME[mPhoneId]);
                         } else {
-                            binder = mMockModem.getServiceBinder(NETWORK_SERVICE);
+                            binder = mMockModem.getServiceBinder(HAL_SERVICE_NETWORK);
                         }
                         if (binder != null) {
-                            mRadioVersion = RADIO_HAL_VERSION_2_0;
-                            ((RadioNetworkProxy) serviceProxy).setAidl(mRadioVersion,
+                            mHalVersion.put(service, ((RadioNetworkProxy) serviceProxy).setAidl(
+                                    mHalVersion.get(service),
                                     android.hardware.radio.network.IRadioNetwork.Stub
-                                            .asInterface(binder));
+                                            .asInterface(binder)));
                         }
                         break;
-                    case SIM_SERVICE:
+                    case HAL_SERVICE_SIM:
                         if (mMockModem == null) {
                             binder = ServiceManager.waitForDeclaredService(
                                     android.hardware.radio.sim.IRadioSim.DESCRIPTOR + "/"
                                             + HIDL_SERVICE_NAME[mPhoneId]);
                         } else {
-                            binder = mMockModem.getServiceBinder(SIM_SERVICE);
+                            binder = mMockModem.getServiceBinder(HAL_SERVICE_SIM);
                         }
                         if (binder != null) {
-                            mRadioVersion = RADIO_HAL_VERSION_2_0;
-                            ((RadioSimProxy) serviceProxy).setAidl(mRadioVersion,
+                            mHalVersion.put(service, ((RadioSimProxy) serviceProxy).setAidl(
+                                    mHalVersion.get(service),
                                     android.hardware.radio.sim.IRadioSim.Stub
-                                            .asInterface(binder));
+                                            .asInterface(binder)));
                         }
                         break;
-                    case VOICE_SERVICE:
+                    case HAL_SERVICE_VOICE:
                         if (mMockModem == null) {
                             binder = ServiceManager.waitForDeclaredService(
                                     android.hardware.radio.voice.IRadioVoice.DESCRIPTOR + "/"
                                             + HIDL_SERVICE_NAME[mPhoneId]);
                         } else {
-                            binder = mMockModem.getServiceBinder(VOICE_SERVICE);
+                            binder = mMockModem.getServiceBinder(HAL_SERVICE_VOICE);
                         }
                         if (binder != null) {
-                            mRadioVersion = RADIO_HAL_VERSION_2_0;
-                            ((RadioVoiceProxy) serviceProxy).setAidl(mRadioVersion,
+                            mHalVersion.put(service, ((RadioVoiceProxy) serviceProxy).setAidl(
+                                    mHalVersion.get(service),
                                     android.hardware.radio.voice.IRadioVoice.Stub
-                                            .asInterface(binder));
+                                            .asInterface(binder)));
+                        }
+                        break;
+                    case HAL_SERVICE_IMS:
+                        if (mMockModem == null) {
+                            binder = ServiceManager.waitForDeclaredService(
+                                    android.hardware.radio.ims.IRadioIms.DESCRIPTOR + "/"
+                                            + HIDL_SERVICE_NAME[mPhoneId]);
+                        } else {
+                            binder = mMockModem.getServiceBinder(HAL_SERVICE_IMS);
+                        }
+                        if (binder != null) {
+                            mHalVersion.put(service, ((RadioImsProxy) serviceProxy).setAidl(
+                                    mHalVersion.get(service),
+                                    android.hardware.radio.ims.IRadioIms.Stub
+                                            .asInterface(binder)));
                         }
                         break;
                 }
 
-                if (serviceProxy.isEmpty() && mRadioVersion.less(RADIO_HAL_VERSION_2_0)) {
+                if (serviceProxy.isEmpty()
+                        && mHalVersion.get(service).less(RADIO_HAL_VERSION_2_0)) {
                     try {
-                        mRadioVersion = RADIO_HAL_VERSION_1_6;
-                        serviceProxy.setHidl(mRadioVersion,
+                        mHalVersion.put(service, RADIO_HAL_VERSION_1_6);
+                        serviceProxy.setHidl(mHalVersion.get(service),
                                 android.hardware.radio.V1_6.IRadio.getService(
                                         HIDL_SERVICE_NAME[mPhoneId], true));
                     } catch (NoSuchElementException e) {
                     }
                 }
 
-                if (serviceProxy.isEmpty() && mRadioVersion.less(RADIO_HAL_VERSION_2_0)) {
+                if (serviceProxy.isEmpty()
+                        && mHalVersion.get(service).less(RADIO_HAL_VERSION_2_0)) {
                     try {
-                        mRadioVersion = RADIO_HAL_VERSION_1_5;
-                        serviceProxy.setHidl(mRadioVersion,
+                        mHalVersion.put(service, RADIO_HAL_VERSION_1_5);
+                        serviceProxy.setHidl(mHalVersion.get(service),
                                 android.hardware.radio.V1_5.IRadio.getService(
                                         HIDL_SERVICE_NAME[mPhoneId], true));
                     } catch (NoSuchElementException e) {
                     }
                 }
 
-                if (serviceProxy.isEmpty() && mRadioVersion.less(RADIO_HAL_VERSION_2_0)) {
+                if (serviceProxy.isEmpty()
+                        && mHalVersion.get(service).less(RADIO_HAL_VERSION_2_0)) {
                     try {
-                        mRadioVersion = RADIO_HAL_VERSION_1_4;
-                        serviceProxy.setHidl(mRadioVersion,
+                        mHalVersion.put(service, RADIO_HAL_VERSION_1_4);
+                        serviceProxy.setHidl(mHalVersion.get(service),
                                 android.hardware.radio.V1_4.IRadio.getService(
                                         HIDL_SERVICE_NAME[mPhoneId], true));
                     } catch (NoSuchElementException e) {
                     }
                 }
 
-                if (serviceProxy.isEmpty() && mRadioVersion.less(RADIO_HAL_VERSION_2_0)) {
+                if (serviceProxy.isEmpty()
+                        && mHalVersion.get(service).less(RADIO_HAL_VERSION_2_0)) {
                     try {
-                        mRadioVersion = RADIO_HAL_VERSION_1_3;
-                        serviceProxy.setHidl(mRadioVersion,
+                        mHalVersion.put(service, RADIO_HAL_VERSION_1_3);
+                        serviceProxy.setHidl(mHalVersion.get(service),
                                 android.hardware.radio.V1_3.IRadio.getService(
                                         HIDL_SERVICE_NAME[mPhoneId], true));
                     } catch (NoSuchElementException e) {
                     }
                 }
 
-                if (serviceProxy.isEmpty() && mRadioVersion.less(RADIO_HAL_VERSION_2_0)) {
+                if (serviceProxy.isEmpty()
+                        && mHalVersion.get(service).less(RADIO_HAL_VERSION_2_0)) {
                     try {
-                        mRadioVersion = RADIO_HAL_VERSION_1_2;
-                        serviceProxy.setHidl(mRadioVersion,
+                        mHalVersion.put(service, RADIO_HAL_VERSION_1_2);
+                        serviceProxy.setHidl(mHalVersion.get(service),
                                 android.hardware.radio.V1_2.IRadio.getService(
                                         HIDL_SERVICE_NAME[mPhoneId], true));
                     } catch (NoSuchElementException e) {
                     }
                 }
 
-                if (serviceProxy.isEmpty() && mRadioVersion.less(RADIO_HAL_VERSION_2_0)) {
+                if (serviceProxy.isEmpty()
+                        && mHalVersion.get(service).less(RADIO_HAL_VERSION_2_0)) {
                     try {
-                        mRadioVersion = RADIO_HAL_VERSION_1_1;
-                        serviceProxy.setHidl(mRadioVersion,
+                        mHalVersion.put(service, RADIO_HAL_VERSION_1_1);
+                        serviceProxy.setHidl(mHalVersion.get(service),
                                 android.hardware.radio.V1_1.IRadio.getService(
                                         HIDL_SERVICE_NAME[mPhoneId], true));
                     } catch (NoSuchElementException e) {
                     }
                 }
 
-                if (serviceProxy.isEmpty() && mRadioVersion.less(RADIO_HAL_VERSION_2_0)) {
+                if (serviceProxy.isEmpty()
+                        && mHalVersion.get(service).less(RADIO_HAL_VERSION_2_0)) {
                     try {
-                        mRadioVersion = RADIO_HAL_VERSION_1_0;
-                        serviceProxy.setHidl(mRadioVersion,
+                        mHalVersion.put(service, RADIO_HAL_VERSION_1_0);
+                        serviceProxy.setHidl(mHalVersion.get(service),
                                 android.hardware.radio.V1_0.IRadio.getService(
                                         HIDL_SERVICE_NAME[mPhoneId], true));
                     } catch (NoSuchElementException e) {
@@ -924,45 +996,52 @@
                 if (!serviceProxy.isEmpty()) {
                     if (serviceProxy.isAidl()) {
                         switch (service) {
-                            case DATA_SERVICE:
+                            case HAL_SERVICE_DATA:
                                 mDeathRecipients.get(service).linkToDeath(
                                         ((RadioDataProxy) serviceProxy).getAidl().asBinder());
                                 ((RadioDataProxy) serviceProxy).getAidl().setResponseFunctions(
                                         mDataResponse, mDataIndication);
                                 break;
-                            case MESSAGING_SERVICE:
+                            case HAL_SERVICE_MESSAGING:
                                 mDeathRecipients.get(service).linkToDeath(
                                         ((RadioMessagingProxy) serviceProxy).getAidl().asBinder());
                                 ((RadioMessagingProxy) serviceProxy).getAidl().setResponseFunctions(
                                         mMessagingResponse, mMessagingIndication);
                                 break;
-                            case MODEM_SERVICE:
+                            case HAL_SERVICE_MODEM:
                                 mDeathRecipients.get(service).linkToDeath(
                                         ((RadioModemProxy) serviceProxy).getAidl().asBinder());
                                 ((RadioModemProxy) serviceProxy).getAidl().setResponseFunctions(
                                         mModemResponse, mModemIndication);
                                 break;
-                            case NETWORK_SERVICE:
+                            case HAL_SERVICE_NETWORK:
                                 mDeathRecipients.get(service).linkToDeath(
                                         ((RadioNetworkProxy) serviceProxy).getAidl().asBinder());
                                 ((RadioNetworkProxy) serviceProxy).getAidl().setResponseFunctions(
                                         mNetworkResponse, mNetworkIndication);
                                 break;
-                            case SIM_SERVICE:
+                            case HAL_SERVICE_SIM:
                                 mDeathRecipients.get(service).linkToDeath(
                                         ((RadioSimProxy) serviceProxy).getAidl().asBinder());
                                 ((RadioSimProxy) serviceProxy).getAidl().setResponseFunctions(
                                         mSimResponse, mSimIndication);
                                 break;
-                            case VOICE_SERVICE:
+                            case HAL_SERVICE_VOICE:
                                 mDeathRecipients.get(service).linkToDeath(
                                         ((RadioVoiceProxy) serviceProxy).getAidl().asBinder());
                                 ((RadioVoiceProxy) serviceProxy).getAidl().setResponseFunctions(
                                         mVoiceResponse, mVoiceIndication);
                                 break;
+                            case HAL_SERVICE_IMS:
+                                mDeathRecipients.get(service).linkToDeath(
+                                        ((RadioImsProxy) serviceProxy).getAidl().asBinder());
+                                ((RadioImsProxy) serviceProxy).getAidl().setResponseFunctions(
+                                        mImsResponse, mImsIndication);
+                                break;
                         }
                     } else {
-                        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+                        if (mHalVersion.get(service)
+                                .greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
                             throw new AssertionError("serviceProxy shouldn't be HIDL with HAL 2.0");
                         }
                         if (!mIsRadioProxyInitialized) {
@@ -975,6 +1054,7 @@
                     }
                 } else {
                     mDisabledRadioServices.get(service).add(mPhoneId);
+                    mHalVersion.put(service, RADIO_HAL_VERSION_UNKNOWN);
                     riljLoge("getRadioServiceProxy: set " + serviceToString(service) + " for "
                             + HIDL_SERVICE_NAME[mPhoneId] + " as disabled");
                 }
@@ -1003,7 +1083,7 @@
         for (int service = MIN_SERVICE_IDX; service <= MAX_SERVICE_IDX; service++) {
             if (active) {
                 // Try to connect to RIL services and set response functions.
-                if (service == RADIO_SERVICE) {
+                if (service == HAL_SERVICE_RADIO) {
                     getRadioProxy(null);
                 } else {
                     getRadioServiceProxy(service, null);
@@ -1044,7 +1124,11 @@
             mRadioBugDetector = new RadioBugDetector(context, mPhoneId);
         }
         try {
-            if (isRadioVersion2_0()) mRadioVersion = RADIO_HAL_VERSION_2_0;
+            if (isRadioVersion2_0()) {
+                mHalVersion.put(HAL_SERVICE_RADIO, RADIO_HAL_VERSION_2_0);
+            } else {
+                mHalVersion.put(HAL_SERVICE_RADIO, RADIO_HAL_VERSION_UNKNOWN);
+            }
         } catch (SecurityException ex) {
             /* TODO(b/211920208): instead of the following workaround (guessing if we're in a test
              * based on proxies being populated), mock ServiceManager to not throw
@@ -1060,6 +1144,8 @@
         mRadioIndication = new RadioIndication(this);
         mDataResponse = new DataResponse(this);
         mDataIndication = new DataIndication(this);
+        mImsResponse = new ImsResponse(this);
+        mImsIndication = new ImsIndication(this);
         mMessagingResponse = new MessagingResponse(this);
         mMessagingIndication = new MessagingIndication(this);
         mModemResponse = new ModemResponse(this);
@@ -1073,19 +1159,33 @@
         mRilHandler = new RilHandler();
         mRadioProxyDeathRecipient = new RadioProxyDeathRecipient();
         for (int service = MIN_SERVICE_IDX; service <= MAX_SERVICE_IDX; service++) {
-            if (service != RADIO_SERVICE) {
+            if (service != HAL_SERVICE_RADIO) {
+                try {
+                    if (isRadioServiceSupported(service)) {
+                        mHalVersion.put(service, RADIO_HAL_VERSION_UNKNOWN);
+                    } else {
+                        mHalVersion.put(service, RADIO_HAL_VERSION_UNSUPPORTED);
+                    }
+                } catch (SecurityException ex) {
+                    /* TODO(b/211920208): instead of the following workaround (guessing if
+                    * we're in a test based on proxies being populated), mock ServiceManager
+                    * to not throw SecurityException and return correct value based on what
+                    * HAL we're testing. */
+                    if (proxies == null) throw ex;
+                }
                 mDeathRecipients.put(service, new BinderServiceDeathRecipient(service));
             }
             mDisabledRadioServices.put(service, new HashSet<>());
             mServiceCookies.put(service, new AtomicLong(0));
         }
         if (proxies == null) {
-            mServiceProxies.put(DATA_SERVICE, new RadioDataProxy());
-            mServiceProxies.put(MESSAGING_SERVICE, new RadioMessagingProxy());
-            mServiceProxies.put(MODEM_SERVICE, new RadioModemProxy());
-            mServiceProxies.put(NETWORK_SERVICE, new RadioNetworkProxy());
-            mServiceProxies.put(SIM_SERVICE, new RadioSimProxy());
-            mServiceProxies.put(VOICE_SERVICE, new RadioVoiceProxy());
+            mServiceProxies.put(HAL_SERVICE_DATA, new RadioDataProxy());
+            mServiceProxies.put(HAL_SERVICE_MESSAGING, new RadioMessagingProxy());
+            mServiceProxies.put(HAL_SERVICE_MODEM, new RadioModemProxy());
+            mServiceProxies.put(HAL_SERVICE_NETWORK, new RadioNetworkProxy());
+            mServiceProxies.put(HAL_SERVICE_SIM, new RadioSimProxy());
+            mServiceProxies.put(HAL_SERVICE_VOICE, new RadioVoiceProxy());
+            mServiceProxies.put(HAL_SERVICE_IMS, new RadioImsProxy());
         } else {
             mServiceProxies = proxies;
         }
@@ -1114,7 +1214,7 @@
         // Set radio callback; needed to set RadioIndication callback (should be done after
         // wakelock stuff is initialized above as callbacks are received on separate binder threads)
         for (int service = MIN_SERVICE_IDX; service <= MAX_SERVICE_IDX; service++) {
-            if (service == RADIO_SERVICE) {
+            if (service == HAL_SERVICE_RADIO) {
                 getRadioProxy(null);
             } else {
                 if (proxies == null) {
@@ -1122,30 +1222,62 @@
                     getRadioServiceProxy(service, null);
                 }
             }
-        }
 
-        if (RILJ_LOGD) {
-            riljLog("Radio HAL version: " + mRadioVersion);
+            if (RILJ_LOGD) {
+                riljLog("HAL version of " + serviceToString(service)
+                        + ": " + mHalVersion.get(service));
+            }
         }
     }
 
     private boolean isRadioVersion2_0() {
-        final String[] serviceNames = new String[] {
-            android.hardware.radio.data.IRadioData.DESCRIPTOR,
-            android.hardware.radio.messaging.IRadioMessaging.DESCRIPTOR,
-            android.hardware.radio.modem.IRadioModem.DESCRIPTOR,
-            android.hardware.radio.network.IRadioNetwork.DESCRIPTOR,
-            android.hardware.radio.sim.IRadioSim.DESCRIPTOR,
-            android.hardware.radio.voice.IRadioVoice.DESCRIPTOR,
-        };
-        for (String serviceName : serviceNames) {
-            if (ServiceManager.isDeclared(serviceName + '/' + HIDL_SERVICE_NAME[mPhoneId])) {
+        for (int service = HAL_SERVICE_DATA; service <= MAX_SERVICE_IDX; service++) {
+            if (isRadioServiceSupported(service)) {
                 return true;
             }
         }
         return false;
     }
 
+    private boolean isRadioServiceSupported(int service) {
+        String serviceName = "";
+
+        if (service == HAL_SERVICE_RADIO) {
+            return true;
+        }
+
+        switch (service) {
+            case HAL_SERVICE_DATA:
+                serviceName = android.hardware.radio.data.IRadioData.DESCRIPTOR;
+                break;
+            case HAL_SERVICE_MESSAGING:
+                serviceName = android.hardware.radio.messaging.IRadioMessaging.DESCRIPTOR;
+                break;
+            case HAL_SERVICE_MODEM:
+                serviceName = android.hardware.radio.modem.IRadioModem.DESCRIPTOR;
+                break;
+            case HAL_SERVICE_NETWORK:
+                serviceName = android.hardware.radio.network.IRadioNetwork.DESCRIPTOR;
+                break;
+            case HAL_SERVICE_SIM:
+                serviceName = android.hardware.radio.sim.IRadioSim.DESCRIPTOR;
+                break;
+            case HAL_SERVICE_VOICE:
+                serviceName = android.hardware.radio.voice.IRadioVoice.DESCRIPTOR;
+                break;
+            case HAL_SERVICE_IMS:
+                serviceName = android.hardware.radio.ims.IRadioIms.DESCRIPTOR;
+                break;
+        }
+
+        if (!serviceName.equals("")
+                && ServiceManager.isDeclared(serviceName + '/' + HIDL_SERVICE_NAME[mPhoneId])) {
+            return true;
+        }
+
+        return false;
+    }
+
     private boolean isRadioBugDetectionEnabled() {
         return Settings.Global.getInt(mContext.getContentResolver(),
                 Settings.Global.ENABLE_RADIO_BUG_DETECTION, 1) != 0;
@@ -1205,7 +1337,7 @@
             try {
                 simProxy.getIccCardStatus(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "getIccCardStatus", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "getIccCardStatus", e);
             }
         }
     }
@@ -1252,7 +1384,7 @@
                 simProxy.supplyIccPinForApp(rr.mSerial, RILUtils.convertNullToEmptyString(pin),
                         RILUtils.convertNullToEmptyString(aid));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "supplyIccPinForApp", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "supplyIccPinForApp", e);
             }
         }
     }
@@ -1279,7 +1411,7 @@
                         RILUtils.convertNullToEmptyString(newPin),
                         RILUtils.convertNullToEmptyString(aid));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "supplyIccPukForApp", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "supplyIccPukForApp", e);
             }
         }
     }
@@ -1305,7 +1437,7 @@
                 simProxy.supplyIccPin2ForApp(rr.mSerial, RILUtils.convertNullToEmptyString(pin),
                         RILUtils.convertNullToEmptyString(aid));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "supplyIccPin2ForApp", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "supplyIccPin2ForApp", e);
             }
         }
     }
@@ -1332,7 +1464,7 @@
                         RILUtils.convertNullToEmptyString(newPin2),
                         RILUtils.convertNullToEmptyString(aid));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "supplyIccPuk2ForApp", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "supplyIccPuk2ForApp", e);
             }
         }
     }
@@ -1360,7 +1492,7 @@
                         RILUtils.convertNullToEmptyString(newPin),
                         RILUtils.convertNullToEmptyString(aid));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "changeIccPinForApp", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "changeIccPinForApp", e);
             }
         }
     }
@@ -1388,7 +1520,7 @@
                         RILUtils.convertNullToEmptyString(newPin2),
                         RILUtils.convertNullToEmptyString(aid));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "changeIccPin2ForApp", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "changeIccPin2ForApp", e);
             }
         }
     }
@@ -1410,7 +1542,7 @@
                         RILUtils.convertNullToEmptyString(netpin));
             } catch (RemoteException | RuntimeException e) {
                 handleRadioProxyExceptionForRR(
-                        NETWORK_SERVICE, "supplyNetworkDepersonalization", e);
+                        HAL_SERVICE_NETWORK, "supplyNetworkDepersonalization", e);
             }
         }
     }
@@ -1420,7 +1552,7 @@
             Message result) {
         RadioSimProxy simProxy = getRadioServiceProxy(RadioSimProxy.class, result);
         if (simProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_5)) {
+        if (mHalVersion.get(HAL_SERVICE_SIM).greaterOrEqual(RADIO_HAL_VERSION_1_5)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_ENTER_SIM_DEPERSONALIZATION, result,
                     mRILDefaultWorkSource);
 
@@ -1433,7 +1565,7 @@
                 simProxy.supplySimDepersonalization(rr.mSerial, persoType,
                         RILUtils.convertNullToEmptyString(controlKey));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "supplySimDepersonalization", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "supplySimDepersonalization", e);
             }
         } else {
             if (PersoSubState.PERSOSUBSTATE_SIM_NETWORK == persoType) {
@@ -1465,7 +1597,7 @@
             try {
                 voiceProxy.getCurrentCalls(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "getCurrentCalls", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "getCurrentCalls", e);
             }
         }
     }
@@ -1481,7 +1613,7 @@
     public void enableModem(boolean enable, Message result) {
         RadioModemProxy modemProxy = getRadioServiceProxy(RadioModemProxy.class, result);
         if (modemProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_3)) {
+        if (mHalVersion.get(HAL_SERVICE_MODEM).greaterOrEqual(RADIO_HAL_VERSION_1_3)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_ENABLE_MODEM, result, mRILDefaultWorkSource);
 
             if (RILJ_LOGD) {
@@ -1492,7 +1624,8 @@
             try {
                 modemProxy.enableModem(rr.mSerial, enable);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "enableModem", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM,
+                        "enableModem", e);
             }
         } else {
             if (RILJ_LOGV) riljLog("enableModem: not supported.");
@@ -1509,7 +1642,7 @@
             Message result) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_3)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_1_3)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_SET_SYSTEM_SELECTION_CHANNELS, result,
                     mRILDefaultWorkSource);
 
@@ -1521,7 +1654,8 @@
             try {
                 networkProxy.setSystemSelectionChannels(rr.mSerial, specifiers);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setSystemSelectionChannels", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
+                        "setSystemSelectionChannels", e);
             }
         } else {
             if (RILJ_LOGV) riljLog("setSystemSelectionChannels: not supported.");
@@ -1537,7 +1671,7 @@
     public void getSystemSelectionChannels(Message result) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_GET_SYSTEM_SELECTION_CHANNELS, result,
                     mRILDefaultWorkSource);
 
@@ -1549,7 +1683,8 @@
             try {
                 networkProxy.getSystemSelectionChannels(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getSystemSelectionChannels", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
+                        "getSystemSelectionChannels", e);
             }
         } else {
             if (RILJ_LOGV) riljLog("getSystemSelectionChannels: not supported.");
@@ -1565,7 +1700,7 @@
     public void getModemStatus(Message result) {
         RadioModemProxy modemProxy = getRadioServiceProxy(RadioModemProxy.class, result);
         if (modemProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_3)) {
+        if (mHalVersion.get(HAL_SERVICE_MODEM).greaterOrEqual(RADIO_HAL_VERSION_1_3)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_GET_MODEM_STATUS, result,
                     mRILDefaultWorkSource);
 
@@ -1576,7 +1711,7 @@
             try {
                 modemProxy.getModemStackStatus(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "getModemStatus", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "getModemStatus", e);
             }
         } else {
             if (RILJ_LOGV) riljLog("getModemStatus: not supported.");
@@ -1591,7 +1726,8 @@
     @Override
     public void dial(String address, boolean isEmergencyCall, EmergencyNumber emergencyNumberInfo,
             boolean hasKnownUserIntentEmergency, int clirMode, UUSInfo uusInfo, Message result) {
-        if (isEmergencyCall && mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_4)
+        if (isEmergencyCall
+                && mHalVersion.get(HAL_SERVICE_VOICE).greaterOrEqual(RADIO_HAL_VERSION_1_4)
                 && emergencyNumberInfo != null) {
             emergencyDial(address, emergencyNumberInfo, hasKnownUserIntentEmergency, clirMode,
                     uusInfo, result);
@@ -1609,7 +1745,7 @@
             try {
                 voiceProxy.dial(rr.mSerial, address, clirMode, uusInfo);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "dial", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "dial", e);
             }
         }
     }
@@ -1618,7 +1754,7 @@
             boolean hasKnownUserIntentEmergency, int clirMode, UUSInfo uusInfo, Message result) {
         RadioVoiceProxy voiceProxy = getRadioServiceProxy(RadioVoiceProxy.class, result);
         if (voiceProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_4)) {
+        if (mHalVersion.get(HAL_SERVICE_VOICE).greaterOrEqual(RADIO_HAL_VERSION_1_4)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_EMERGENCY_DIAL, result,
                     mRILDefaultWorkSource);
 
@@ -1631,7 +1767,7 @@
                 voiceProxy.emergencyDial(rr.mSerial, RILUtils.convertNullToEmptyString(address),
                         emergencyNumberInfo, hasKnownUserIntentEmergency, clirMode, uusInfo);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "emergencyDial", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "emergencyDial", e);
             }
         } else {
             riljLoge("emergencyDial is not supported with 1.4 below IRadio");
@@ -1656,7 +1792,7 @@
             try {
                 simProxy.getImsiForApp(rr.mSerial, RILUtils.convertNullToEmptyString(aid));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "getImsiForApp", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "getImsiForApp", e);
             }
         }
     }
@@ -1675,7 +1811,7 @@
             try {
                 voiceProxy.hangup(rr.mSerial, gsmIndex);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "hangup", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "hangup", e);
             }
         }
     }
@@ -1695,7 +1831,7 @@
             try {
                 voiceProxy.hangupWaitingOrBackground(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "hangupWaitingOrBackground", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "hangupWaitingOrBackground", e);
             }
         }
     }
@@ -1716,7 +1852,7 @@
                 voiceProxy.hangupForegroundResumeBackground(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
                 handleRadioProxyExceptionForRR(
-                        VOICE_SERVICE, "hangupForegroundResumeBackground", e);
+                        HAL_SERVICE_VOICE, "hangupForegroundResumeBackground", e);
             }
         }
     }
@@ -1735,7 +1871,8 @@
             try {
                 voiceProxy.switchWaitingOrHoldingAndActive(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "switchWaitingOrHoldingAndActive", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE,
+                        "switchWaitingOrHoldingAndActive", e);
             }
         }
     }
@@ -1753,7 +1890,7 @@
             try {
                 voiceProxy.conference(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "conference", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "conference", e);
             }
         }
     }
@@ -1771,7 +1908,7 @@
             try {
                 voiceProxy.rejectCall(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "rejectCall", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "rejectCall", e);
             }
         }
     }
@@ -1790,7 +1927,7 @@
             try {
                 voiceProxy.getLastCallFailCause(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "getLastCallFailCause", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "getLastCallFailCause", e);
             }
         }
     }
@@ -1809,7 +1946,7 @@
             try {
                 networkProxy.getSignalStrength(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getSignalStrength", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getSignalStrength", e);
             }
         }
     }
@@ -1833,7 +1970,7 @@
             try {
                 networkProxy.getVoiceRegistrationState(rr.mSerial, overrideHalVersion);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getVoiceRegistrationState", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getVoiceRegistrationState", e);
             }
         }
     }
@@ -1857,7 +1994,7 @@
             try {
                 networkProxy.getDataRegistrationState(rr.mSerial, overrideHalVersion);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getDataRegistrationState", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getDataRegistrationState", e);
             }
         }
     }
@@ -1875,7 +2012,7 @@
             try {
                 networkProxy.getOperator(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getOperator", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getOperator", e);
             }
         }
     }
@@ -1898,7 +2035,7 @@
                 modemProxy.setRadioPower(rr.mSerial, on, forEmergencyCall,
                         preferredForEmergencyCall);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "setRadioPower", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "setRadioPower", e);
             }
         }
     }
@@ -1917,7 +2054,7 @@
             try {
                 voiceProxy.sendDtmf(rr.mSerial, c + "");
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "sendDtmf", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "sendDtmf", e);
             }
         }
     }
@@ -1939,7 +2076,7 @@
                 mMetrics.writeRilSendSms(mPhoneId, rr.mSerial, SmsSession.Event.Tech.SMS_GSM,
                         SmsSession.Event.Format.SMS_FORMAT_3GPP, getOutgoingSmsMessageId(result));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "sendSMS", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "sendSMS", e);
             }
         }
     }
@@ -1980,7 +2117,7 @@
                 mMetrics.writeRilSendSms(mPhoneId, rr.mSerial, SmsSession.Event.Tech.SMS_GSM,
                         SmsSession.Event.Format.SMS_FORMAT_3GPP, getOutgoingSmsMessageId(result));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "sendSMSExpectMore", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "sendSMSExpectMore", e);
             }
         }
     }
@@ -2011,7 +2148,7 @@
                         isRoaming, allowRoaming, reason, linkProperties, pduSessionId, sliceInfo,
                         trafficDescriptor, matchAllRuleAllowed);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "setupDataCall", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "setupDataCall", e);
             }
         }
     }
@@ -2048,7 +2185,7 @@
                         RILUtils.convertNullToEmptyString(pin2),
                         RILUtils.convertNullToEmptyString(aid));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "iccIoForApp", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "iccIoForApp", e);
             }
         }
     }
@@ -2070,7 +2207,7 @@
             try {
                 voiceProxy.sendUssd(rr.mSerial, RILUtils.convertNullToEmptyString(ussd));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "sendUssd", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "sendUssd", e);
             }
         }
     }
@@ -2089,7 +2226,7 @@
             try {
                 voiceProxy.cancelPendingUssd(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "cancelPendingUssd", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "cancelPendingUssd", e);
             }
         }
     }
@@ -2107,7 +2244,7 @@
             try {
                 voiceProxy.getClir(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "getClir", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "getClir", e);
             }
         }
     }
@@ -2126,7 +2263,7 @@
             try {
                 voiceProxy.setClir(rr.mSerial, clirMode);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "setClir", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "setClir", e);
             }
         }
     }
@@ -2147,7 +2284,7 @@
             try {
                 voiceProxy.getCallForwardStatus(rr.mSerial, cfReason, serviceClass, number);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "getCallForwardStatus", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "getCallForwardStatus", e);
             }
         }
     }
@@ -2170,7 +2307,7 @@
                 voiceProxy.setCallForward(
                         rr.mSerial, action, cfReason, serviceClass, number, timeSeconds);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "setCallForward", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "setCallForward", e);
             }
         }
     }
@@ -2190,7 +2327,7 @@
             try {
                 voiceProxy.getCallWaiting(rr.mSerial, serviceClass);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "getCallWaiting", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "getCallWaiting", e);
             }
         }
     }
@@ -2210,7 +2347,7 @@
             try {
                 voiceProxy.setCallWaiting(rr.mSerial, enable, serviceClass);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "setCallWaiting", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "setCallWaiting", e);
             }
         }
     }
@@ -2231,7 +2368,7 @@
             try {
                 messagingProxy.acknowledgeLastIncomingGsmSms(rr.mSerial, success, cause);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE,
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING,
                         "acknowledgeLastIncomingGsmSms", e);
             }
         }
@@ -2251,7 +2388,7 @@
                 voiceProxy.acceptCall(rr.mSerial);
                 mMetrics.writeRilAnswer(mPhoneId, rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "acceptCall", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "acceptCall", e);
             }
         }
     }
@@ -2273,7 +2410,7 @@
                 dataProxy.deactivateDataCall(rr.mSerial, cid, reason);
                 mMetrics.writeRilDeactivateDataCall(mPhoneId, rr.mSerial, cid, reason);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "deactivateDataCall", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "deactivateDataCall", e);
             }
         }
     }
@@ -2304,7 +2441,7 @@
                         RILUtils.convertNullToEmptyString(password),
                         serviceClass, RILUtils.convertNullToEmptyString(appId));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "getFacilityLockForApp", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "getFacilityLockForApp", e);
             }
         }
     }
@@ -2335,7 +2472,7 @@
                         RILUtils.convertNullToEmptyString(password), serviceClass,
                         RILUtils.convertNullToEmptyString(appId));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "setFacilityLockForApp", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "setFacilityLockForApp", e);
             }
         }
     }
@@ -2360,7 +2497,7 @@
                         RILUtils.convertNullToEmptyString(oldPwd),
                         RILUtils.convertNullToEmptyString(newPwd));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "changeBarringPassword", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "changeBarringPassword", e);
             }
         }
     }
@@ -2379,7 +2516,7 @@
             try {
                 networkProxy.getNetworkSelectionMode(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getNetworkSelectionMode", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getNetworkSelectionMode", e);
             }
         }
     }
@@ -2399,7 +2536,7 @@
                 networkProxy.setNetworkSelectionModeAutomatic(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
                 handleRadioProxyExceptionForRR(
-                        NETWORK_SERVICE, "setNetworkSelectionModeAutomatic", e);
+                        HAL_SERVICE_NETWORK, "setNetworkSelectionModeAutomatic", e);
             }
         }
     }
@@ -2420,7 +2557,8 @@
                 networkProxy.setNetworkSelectionModeManual(rr.mSerial,
                         RILUtils.convertNullToEmptyString(operatorNumeric), ran);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setNetworkSelectionModeManual", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
+                        "setNetworkSelectionModeManual", e);
             }
         }
     }
@@ -2439,7 +2577,7 @@
             try {
                 networkProxy.getAvailableNetworks(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getAvailableNetworks", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getAvailableNetworks", e);
             }
         }
     }
@@ -2454,7 +2592,7 @@
     public void startNetworkScan(NetworkScanRequest networkScanRequest, Message result) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_1)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_1_1)) {
             HalVersion overrideHalVersion = getCompatVersion(RIL_REQUEST_START_NETWORK_SCAN);
             if (RILJ_LOGD) {
                 riljLog("startNetworkScan: overrideHalVersion=" + overrideHalVersion);
@@ -2471,7 +2609,7 @@
                 networkProxy.startNetworkScan(rr.mSerial, networkScanRequest, overrideHalVersion,
                         result);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "startNetworkScan", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "startNetworkScan", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "startNetworkScan: REQUEST_NOT_SUPPORTED");
@@ -2487,7 +2625,7 @@
     public void stopNetworkScan(Message result) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_1)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_1_1)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_STOP_NETWORK_SCAN, result,
                     mRILDefaultWorkSource);
 
@@ -2498,7 +2636,7 @@
             try {
                 networkProxy.stopNetworkScan(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "stopNetworkScan", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "stopNetworkScan", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "stopNetworkScan: REQUEST_NOT_SUPPORTED");
@@ -2524,7 +2662,7 @@
             try {
                 voiceProxy.startDtmf(rr.mSerial, c + "");
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "startDtmf", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "startDtmf", e);
             }
         }
     }
@@ -2542,7 +2680,7 @@
             try {
                 voiceProxy.stopDtmf(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "stopDtmf", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "stopDtmf", e);
             }
         }
     }
@@ -2562,7 +2700,7 @@
             try {
                 voiceProxy.separateConnection(rr.mSerial, gsmIndex);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "separateConnection", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "separateConnection", e);
             }
         }
     }
@@ -2581,7 +2719,7 @@
             try {
                 modemProxy.getBasebandVersion(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "getBasebandVersion", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "getBasebandVersion", e);
             }
         }
     }
@@ -2600,7 +2738,7 @@
             try {
                 voiceProxy.setMute(rr.mSerial, enableMute);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "setMute", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "setMute", e);
             }
         }
     }
@@ -2618,7 +2756,7 @@
             try {
                 voiceProxy.getMute(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "getMute", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "getMute", e);
             }
         }
     }
@@ -2636,7 +2774,7 @@
             try {
                 voiceProxy.getClip(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "getClip", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "getClip", e);
             }
         }
     }
@@ -2664,7 +2802,7 @@
             try {
                 dataProxy.getDataCallList(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "getDataCallList", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "getDataCallList", e);
             }
         }
     }
@@ -2695,7 +2833,8 @@
             try {
                 networkProxy.setSuppServiceNotifications(rr.mSerial, enable);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setSuppServiceNotifications", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
+                        "setSuppServiceNotifications", e);
             }
         }
     }
@@ -2718,7 +2857,7 @@
                         RILUtils.convertNullToEmptyString(smsc),
                         RILUtils.convertNullToEmptyString(pdu));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "writeSmsToSim", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "writeSmsToSim", e);
             }
         }
     }
@@ -2739,7 +2878,7 @@
             try {
                 messagingProxy.deleteSmsOnSim(rr.mSerial, index);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "deleteSmsOnSim", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "deleteSmsOnSim", e);
             }
         }
     }
@@ -2758,7 +2897,7 @@
             try {
                 networkProxy.setBandMode(rr.mSerial, bandMode);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setBandMode", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "setBandMode", e);
             }
         }
     }
@@ -2777,7 +2916,7 @@
             try {
                 networkProxy.getAvailableBandModes(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "queryAvailableBandMode", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "queryAvailableBandMode", e);
             }
         }
     }
@@ -2797,7 +2936,7 @@
             try {
                 simProxy.sendEnvelope(rr.mSerial, RILUtils.convertNullToEmptyString(contents));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "sendEnvelope", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "sendEnvelope", e);
             }
         }
     }
@@ -2819,7 +2958,7 @@
                 simProxy.sendTerminalResponseToSim(rr.mSerial,
                         RILUtils.convertNullToEmptyString(contents));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "sendTerminalResponse", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "sendTerminalResponse", e);
             }
         }
     }
@@ -2840,7 +2979,7 @@
                 simProxy.sendEnvelopeWithStatus(rr.mSerial,
                         RILUtils.convertNullToEmptyString(contents));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "sendEnvelopeWithStatus", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "sendEnvelopeWithStatus", e);
             }
         }
     }
@@ -2859,7 +2998,7 @@
             try {
                 voiceProxy.explicitCallTransfer(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "explicitCallTransfer", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "explicitCallTransfer", e);
             }
         }
     }
@@ -2881,7 +3020,7 @@
             try {
                 networkProxy.setPreferredNetworkTypeBitmap(rr.mSerial, mAllowedNetworkTypesBitmask);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setPreferredNetworkType", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "setPreferredNetworkType", e);
             }
         }
     }
@@ -2900,7 +3039,7 @@
             try {
                 networkProxy.getAllowedNetworkTypesBitmap(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getPreferredNetworkType", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getPreferredNetworkType", e);
             }
         }
     }
@@ -2910,7 +3049,7 @@
             @TelephonyManager.NetworkTypeBitMask int networkTypeBitmask, Message result) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (!networkProxy.isEmpty()) {
-            if (mRadioVersion.less(RADIO_HAL_VERSION_1_6)) {
+            if (mHalVersion.get(HAL_SERVICE_NETWORK).less(RADIO_HAL_VERSION_1_6)) {
                 // For older HAL, redirects the call to setPreferredNetworkType.
                 setPreferredNetworkType(
                         RadioAccessFamily.getNetworkTypeFromRaf(networkTypeBitmask), result);
@@ -2927,7 +3066,8 @@
             try {
                 networkProxy.setAllowedNetworkTypesBitmap(rr.mSerial, mAllowedNetworkTypesBitmask);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setAllowedNetworkTypeBitmask", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
+                        "setAllowedNetworkTypeBitmask", e);
             }
         }
     }
@@ -2946,7 +3086,8 @@
             try {
                 networkProxy.getAllowedNetworkTypesBitmap(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getAllowedNetworkTypeBitmask", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
+                        "getAllowedNetworkTypeBitmask", e);
             }
         }
     }
@@ -2966,7 +3107,7 @@
             try {
                 networkProxy.setLocationUpdates(rr.mSerial, enable);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setLocationUpdates", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "setLocationUpdates", e);
             }
         }
     }
@@ -2978,7 +3119,7 @@
     public void isNrDualConnectivityEnabled(Message result, WorkSource workSource) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_IS_NR_DUAL_CONNECTIVITY_ENABLED, result,
                     getDefaultWorkSourceIfInvalid(workSource));
 
@@ -2989,7 +3130,8 @@
             try {
                 networkProxy.isNrDualConnectivityEnabled(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "isNrDualConnectivityEnabled", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
+                        "isNrDualConnectivityEnabled", e);
             }
         } else {
             if (RILJ_LOGD) {
@@ -3019,7 +3161,7 @@
             WorkSource workSource) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_ENABLE_NR_DUAL_CONNECTIVITY, result,
                     getDefaultWorkSourceIfInvalid(workSource));
 
@@ -3031,7 +3173,7 @@
             try {
                 networkProxy.setNrDualConnectivityState(rr.mSerial, (byte) nrDualConnectivityState);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "enableNrDualConnectivity", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "enableNrDualConnectivity", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "enableNrDualConnectivity: REQUEST_NOT_SUPPORTED");
@@ -3057,7 +3199,7 @@
     @Override
     public void isVoNrEnabled(Message result, WorkSource workSource) {
 
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+        if (mHalVersion.get(HAL_SERVICE_VOICE).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
             RadioVoiceProxy voiceProxy = getRadioServiceProxy(RadioVoiceProxy.class, result);
             if (!voiceProxy.isEmpty()) {
                 RILRequest rr = obtainRequest(RIL_REQUEST_IS_VONR_ENABLED , result,
@@ -3070,7 +3212,7 @@
                 try {
                     voiceProxy.isVoNrEnabled(rr.mSerial);
                 } catch (RemoteException | RuntimeException e) {
-                    handleRadioProxyExceptionForRR(VOICE_SERVICE, "isVoNrEnabled", e);
+                    handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "isVoNrEnabled", e);
                 }
             }
         } else {
@@ -3090,7 +3232,7 @@
     public void setVoNrEnabled(boolean enabled, Message result, WorkSource workSource) {
         setVoNrEnabled(enabled);
 
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+        if (mHalVersion.get(HAL_SERVICE_VOICE).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
             RadioVoiceProxy voiceProxy = getRadioServiceProxy(RadioVoiceProxy.class, result);
             if (!voiceProxy.isEmpty()) {
                 RILRequest rr = obtainRequest(RIL_REQUEST_ENABLE_VONR, result,
@@ -3103,7 +3245,7 @@
                 try {
                     voiceProxy.setVoNrEnabled(rr.mSerial, enabled);
                 } catch (RemoteException | RuntimeException e) {
-                    handleRadioProxyExceptionForRR(VOICE_SERVICE, "setVoNrEnabled", e);
+                    handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "setVoNrEnabled", e);
                 }
             }
         } else {
@@ -3135,7 +3277,7 @@
             try {
                 simProxy.setCdmaSubscriptionSource(rr.mSerial, cdmaSubscription);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "setCdmaSubscriptionSource", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "setCdmaSubscriptionSource", e);
             }
         }
     }
@@ -3154,7 +3296,8 @@
             try {
                 networkProxy.getCdmaRoamingPreference(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "queryCdmaRoamingPreference", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
+                        "queryCdmaRoamingPreference", e);
             }
         }
     }
@@ -3174,7 +3317,7 @@
             try {
                 networkProxy.setCdmaRoamingPreference(rr.mSerial, cdmaRoamingType);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setCdmaRoamingPreference", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "setCdmaRoamingPreference", e);
             }
         }
     }
@@ -3193,7 +3336,7 @@
             try {
                 voiceProxy.getTtyMode(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "getTtyMode", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "getTtyMode", e);
             }
         }
     }
@@ -3212,7 +3355,7 @@
             try {
                 voiceProxy.setTtyMode(rr.mSerial, ttyMode);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "setTtyMode", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "setTtyMode", e);
             }
         }
     }
@@ -3232,7 +3375,7 @@
             try {
                 voiceProxy.setPreferredVoicePrivacy(rr.mSerial, enable);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "setPreferredVoicePrivacy", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "setPreferredVoicePrivacy", e);
             }
         }
     }
@@ -3251,7 +3394,7 @@
             try {
                 voiceProxy.getPreferredVoicePrivacy(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "getPreferredVoicePrivacy", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "getPreferredVoicePrivacy", e);
             }
         }
     }
@@ -3271,7 +3414,7 @@
                 voiceProxy.sendCdmaFeatureCode(rr.mSerial,
                         RILUtils.convertNullToEmptyString(featureCode));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "sendCdmaFeatureCode", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "sendCdmaFeatureCode", e);
             }
         }
     }
@@ -3292,7 +3435,7 @@
                 voiceProxy.sendBurstDtmf(rr.mSerial, RILUtils.convertNullToEmptyString(dtmfString),
                         on, off);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "sendBurstDtmf", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "sendBurstDtmf", e);
             }
         }
     }
@@ -3312,13 +3455,13 @@
 
             try {
                 messagingProxy.sendCdmaSmsExpectMore(rr.mSerial, pdu);
-                if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_5)) {
+                if (mHalVersion.get(HAL_SERVICE_MESSAGING).greaterOrEqual(RADIO_HAL_VERSION_1_5)) {
                     mMetrics.writeRilSendSms(mPhoneId, rr.mSerial, SmsSession.Event.Tech.SMS_CDMA,
                             SmsSession.Event.Format.SMS_FORMAT_3GPP2,
                             getOutgoingSmsMessageId(result));
                 }
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "sendCdmaSMSExpectMore", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "sendCdmaSMSExpectMore", e);
             }
         }
     }
@@ -3340,7 +3483,7 @@
                 mMetrics.writeRilSendSms(mPhoneId, rr.mSerial, SmsSession.Event.Tech.SMS_CDMA,
                         SmsSession.Event.Format.SMS_FORMAT_3GPP2, getOutgoingSmsMessageId(result));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "sendCdmaSms", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "sendCdmaSms", e);
             }
         }
     }
@@ -3361,7 +3504,7 @@
             try {
                 messagingProxy.acknowledgeLastIncomingCdmaSms(rr.mSerial, success, cause);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE,
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING,
                         "acknowledgeLastIncomingCdmaSms", e);
             }
         }
@@ -3382,7 +3525,7 @@
             try {
                 messagingProxy.getGsmBroadcastConfig(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "getGsmBroadcastConfig", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "getGsmBroadcastConfig", e);
             }
         }
     }
@@ -3406,7 +3549,7 @@
             try {
                 messagingProxy.setGsmBroadcastConfig(rr.mSerial, config);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "setGsmBroadcastConfig", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "setGsmBroadcastConfig", e);
             }
         }
     }
@@ -3427,7 +3570,8 @@
             try {
                 messagingProxy.setGsmBroadcastActivation(rr.mSerial, activate);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "setGsmBroadcastActivation", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING,
+                        "setGsmBroadcastActivation", e);
             }
         }
     }
@@ -3447,7 +3591,7 @@
             try {
                 messagingProxy.getCdmaBroadcastConfig(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "getCdmaBroadcastConfig", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "getCdmaBroadcastConfig", e);
             }
         }
     }
@@ -3471,7 +3615,7 @@
             try {
                 messagingProxy.setCdmaBroadcastConfig(rr.mSerial, configs);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "setCdmaBroadcastConfig", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "setCdmaBroadcastConfig", e);
             }
         }
     }
@@ -3492,7 +3636,8 @@
             try {
                 messagingProxy.setCdmaBroadcastActivation(rr.mSerial, activate);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "setCdmaBroadcastActivation", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING,
+                        "setCdmaBroadcastActivation", e);
             }
         }
     }
@@ -3511,7 +3656,7 @@
             try {
                 simProxy.getCdmaSubscription(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "getCdmaSubscription", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "getCdmaSubscription", e);
             }
         }
     }
@@ -3532,7 +3677,7 @@
             try {
                 messagingProxy.writeSmsToRuim(rr.mSerial, status, pdu);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "writeSmsToRuim", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "writeSmsToRuim", e);
             }
         }
     }
@@ -3553,7 +3698,7 @@
             try {
                 messagingProxy.deleteSmsOnRuim(rr.mSerial, index);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "deleteSmsOnRuim", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "deleteSmsOnRuim", e);
             }
         }
     }
@@ -3572,7 +3717,36 @@
             try {
                 modemProxy.getDeviceIdentity(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "getDeviceIdentity", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "getDeviceIdentity", e);
+            }
+        }
+    }
+
+    @Override
+    public void getImei(Message result) {
+        RadioModemProxy modemProxy = getRadioServiceProxy(RadioModemProxy.class, result);
+        if (!modemProxy.isEmpty() &&
+                mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_1)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_DEVICE_IMEI, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest));
+            }
+
+            try {
+                modemProxy.getImei(rr.mSerial);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "getImei", e);
+            }
+        }  else {
+            if (RILJ_LOGD) {
+                Rlog.e(RILJ_LOG_TAG, "getImei: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
             }
         }
     }
@@ -3591,7 +3765,7 @@
             try {
                 voiceProxy.exitEmergencyCallbackMode(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(VOICE_SERVICE, "exitEmergencyCallbackMode", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_VOICE, "exitEmergencyCallbackMode", e);
             }
         }
     }
@@ -3611,7 +3785,7 @@
             try {
                 messagingProxy.getSmscAddress(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "getSmscAddress", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "getSmscAddress", e);
             }
         }
     }
@@ -3633,7 +3807,7 @@
                 messagingProxy.setSmscAddress(rr.mSerial,
                         RILUtils.convertNullToEmptyString(address));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "setSmscAddress", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "setSmscAddress", e);
             }
         }
     }
@@ -3654,7 +3828,7 @@
             try {
                 messagingProxy.reportSmsMemoryStatus(rr.mSerial, available);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "reportSmsMemoryStatus", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "reportSmsMemoryStatus", e);
             }
         }
     }
@@ -3673,7 +3847,7 @@
             try {
                 simProxy.reportStkServiceIsRunning(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "reportStkServiceIsRunning", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "reportStkServiceIsRunning", e);
             }
         }
     }
@@ -3692,7 +3866,7 @@
             try {
                 simProxy.getCdmaSubscriptionSource(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "getCdmaSubscriptionSource", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "getCdmaSubscriptionSource", e);
             }
         }
     }
@@ -3714,7 +3888,7 @@
                 messagingProxy.acknowledgeIncomingGsmSmsWithPdu(rr.mSerial, success,
                         RILUtils.convertNullToEmptyString(ackPdu));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE,
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING,
                         "acknowledgeIncomingGsmSmsWithPdu", e);
             }
         }
@@ -3734,7 +3908,7 @@
             try {
                 networkProxy.getVoiceRadioTechnology(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getVoiceRadioTechnology", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getVoiceRadioTechnology", e);
             }
         }
     }
@@ -3753,7 +3927,7 @@
             try {
                 networkProxy.getCellInfoList(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getCellInfoList", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getCellInfoList", e);
             }
         }
     }
@@ -3773,7 +3947,7 @@
             try {
                 networkProxy.setCellInfoListRate(rr.mSerial, rateInMillis);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setCellInfoListRate", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "setCellInfoListRate", e);
             }
         }
     }
@@ -3793,7 +3967,7 @@
             try {
                 dataProxy.setInitialAttachApn(rr.mSerial, dataProfile, isRoaming);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "setInitialAttachApn", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "setInitialAttachApn", e);
             }
         }
     }
@@ -3812,7 +3986,7 @@
             try {
                 networkProxy.getImsRegistrationState(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getImsRegistrationState", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getImsRegistrationState", e);
             }
         }
     }
@@ -3835,7 +4009,7 @@
                 mMetrics.writeRilSendSms(mPhoneId, rr.mSerial, SmsSession.Event.Tech.SMS_IMS,
                         SmsSession.Event.Format.SMS_FORMAT_3GPP, getOutgoingSmsMessageId(result));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "sendImsGsmSms", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "sendImsGsmSms", e);
             }
         }
     }
@@ -3857,7 +4031,7 @@
                 mMetrics.writeRilSendSms(mPhoneId, rr.mSerial, SmsSession.Event.Tech.SMS_IMS,
                         SmsSession.Event.Format.SMS_FORMAT_3GPP2, getOutgoingSmsMessageId(result));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MESSAGING_SERVICE, "sendImsCdmaSms", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MESSAGING, "sendImsCdmaSms", e);
             }
         }
     }
@@ -3885,7 +4059,7 @@
                 simProxy.iccTransmitApduBasicChannel(
                         rr.mSerial, cla, instruction, p1, p2, p3, data);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "iccTransmitApduBasicChannel", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "iccTransmitApduBasicChannel", e);
             }
         }
     }
@@ -3910,7 +4084,7 @@
                 simProxy.iccOpenLogicalChannel(rr.mSerial, RILUtils.convertNullToEmptyString(aid),
                         p2);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "iccOpenLogicalChannel", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "iccOpenLogicalChannel", e);
             }
         }
     }
@@ -3930,7 +4104,7 @@
             try {
                 simProxy.iccCloseLogicalChannel(rr.mSerial, channel);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "iccCloseLogicalChannel", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "iccCloseLogicalChannel", e);
             }
         }
     }
@@ -3938,6 +4112,12 @@
     @Override
     public void iccTransmitApduLogicalChannel(int channel, int cla, int instruction, int p1, int p2,
             int p3, String data, Message result) {
+        iccTransmitApduLogicalChannel(channel, cla, instruction, p1, p2, p3, data, false, result);
+    }
+
+    @Override
+    public void iccTransmitApduLogicalChannel(int channel, int cla, int instruction, int p1, int p2,
+            int p3, String data, boolean isEs10Command, Message result) {
         if (channel <= 0) {
             throw new RuntimeException(
                     "Invalid channel in iccTransmitApduLogicalChannel: " + channel);
@@ -3954,6 +4134,7 @@
                             + String.format(" channel = %d", channel)
                             + String.format(" cla = 0x%02X ins = 0x%02X", cla, instruction)
                             + String.format(" p1 = 0x%02X p2 = 0x%02X p3 = 0x%02X", p1, p2, p3)
+                            + " isEs10Command = " + isEs10Command
                             + " data = " + data);
                 } else {
                     riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest));
@@ -3962,9 +4143,9 @@
 
             try {
                 simProxy.iccTransmitApduLogicalChannel(
-                        rr.mSerial, channel, cla, instruction, p1, p2, p3, data);
+                        rr.mSerial, channel, cla, instruction, p1, p2, p3, data, isEs10Command);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "iccTransmitApduLogicalChannel", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "iccTransmitApduLogicalChannel", e);
             }
         }
     }
@@ -3984,7 +4165,7 @@
             try {
                 modemProxy.nvReadItem(rr.mSerial, itemID);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "nvReadItem", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "nvReadItem", e);
             }
         }
     }
@@ -4005,7 +4186,7 @@
                 modemProxy.nvWriteItem(rr.mSerial, itemId,
                         RILUtils.convertNullToEmptyString(itemValue));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "nvWriteItem", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "nvWriteItem", e);
             }
         }
     }
@@ -4026,7 +4207,7 @@
             try {
                 modemProxy.nvWriteCdmaPrl(rr.mSerial, preferredRoamingList);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "nvWriteCdmaPrl", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "nvWriteCdmaPrl", e);
             }
         }
     }
@@ -4046,7 +4227,7 @@
             try {
                 modemProxy.nvResetConfig(rr.mSerial, resetType);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "nvResetConfig", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "nvResetConfig", e);
             }
         }
     }
@@ -4068,7 +4249,7 @@
             try {
                 simProxy.setUiccSubscription(rr.mSerial, slotId, appIndex, subId, subStatus);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "setUiccSubscription", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "setUiccSubscription", e);
             }
         }
     }
@@ -4083,7 +4264,7 @@
         // EID should be supported as long as HAL >= 1.2.
         //  - in HAL 1.2 we have EID through ATR
         //  - in later HAL versions we also have EID through slot / card status.
-        return mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_2);
+        return mHalVersion.get(HAL_SERVICE_RADIO).greaterOrEqual(RADIO_HAL_VERSION_1_2);
     }
 
     @Override
@@ -4100,7 +4281,7 @@
             try {
                 dataProxy.setDataAllowed(rr.mSerial, allowed);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "setDataAllowed", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "setDataAllowed", e);
             }
         }
     }
@@ -4120,7 +4301,7 @@
             try {
                 modemProxy.getHardwareConfig(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "getHardwareConfig", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "getHardwareConfig", e);
             }
         }
     }
@@ -4143,7 +4324,7 @@
                         RILUtils.convertNullToEmptyString(data),
                         RILUtils.convertNullToEmptyString(aid));
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "requestIccSimAuthentication", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "requestIccSimAuthentication", e);
             }
         }
     }
@@ -4166,7 +4347,7 @@
             try {
                 dataProxy.setDataProfile(rr.mSerial, dps, isRoaming);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "setDataProfile", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "setDataProfile", e);
             }
         }
     }
@@ -4184,7 +4365,7 @@
             try {
                 modemProxy.requestShutdown(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "requestShutdown", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "requestShutdown", e);
             }
         }
     }
@@ -4203,7 +4384,7 @@
             try {
                 modemProxy.getRadioCapability(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "getRadioCapability", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "getRadioCapability", e);
             }
         }
     }
@@ -4223,14 +4404,14 @@
             try {
                 modemProxy.setRadioCapability(rr.mSerial, rc);
             } catch (Exception e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "setRadioCapability", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "setRadioCapability", e);
             }
         }
     }
 
     @Override
     public void startLceService(int reportIntervalMs, boolean pullMode, Message result) {
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_2)) {
+        if (mHalVersion.get(HAL_SERVICE_RADIO).greaterOrEqual(RADIO_HAL_VERSION_1_2)) {
             // We have a 1.2 or later radio, so the LCE 1.0 LCE service control path is unused.
             // Instead the LCE functionality is always-on and provides unsolicited indications.
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "startLceService: REQUEST_NOT_SUPPORTED");
@@ -4254,14 +4435,14 @@
             try {
                 radioProxy.startLceService(rr.mSerial, reportIntervalMs, pullMode);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(RADIO_SERVICE, "startLceService", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_RADIO, "startLceService", e);
             }
         }
     }
 
     @Override
     public void stopLceService(Message result) {
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_2)) {
+        if (mHalVersion.get(HAL_SERVICE_RADIO).greaterOrEqual(RADIO_HAL_VERSION_1_2)) {
             // We have a 1.2 or later radio, so the LCE 1.0 LCE service control is unused.
             // Instead the LCE functionality is always-on and provides unsolicited indications.
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "stopLceService: REQUEST_NOT_SUPPORTED");
@@ -4284,7 +4465,7 @@
             try {
                 radioProxy.stopLceService(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(RADIO_SERVICE, "stopLceService", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_RADIO, "stopLceService", e);
             }
         }
     }
@@ -4303,7 +4484,7 @@
             long completionWindowMillis) {
         RadioDataProxy dataProxy = getRadioServiceProxy(RadioDataProxy.class, result);
         if (dataProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_DATA).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_SET_DATA_THROTTLING, result,
                     getDefaultWorkSourceIfInvalid(workSource));
 
@@ -4318,7 +4499,7 @@
                 dataProxy.setDataThrottling(rr.mSerial, (byte) dataThrottlingAction,
                         completionWindowMillis);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "setDataThrottling", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "setDataThrottling", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "setDataThrottling: REQUEST_NOT_SUPPORTED");
@@ -4343,7 +4524,7 @@
     @Deprecated
     @Override
     public void pullLceData(Message result) {
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_2)) {
+        if (mHalVersion.get(HAL_SERVICE_RADIO).greaterOrEqual(RADIO_HAL_VERSION_1_2)) {
             // We have a 1.2 or later radio, so the LCE 1.0 LCE service control path is unused.
             // Instead the LCE functionality is always-on and provides unsolicited indications.
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "pullLceData: REQUEST_NOT_SUPPORTED");
@@ -4366,7 +4547,7 @@
             try {
                 radioProxy.pullLceData(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(RADIO_SERVICE, "pullLceData", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_RADIO, "pullLceData", e);
             }
         }
     }
@@ -4388,7 +4569,7 @@
                         mRilHandler.obtainMessage(EVENT_BLOCKING_RESPONSE_TIMEOUT, rr.mSerial);
                 mRilHandler.sendMessageDelayed(msg, DEFAULT_BLOCKING_MESSAGE_RESPONSE_TIMEOUT_MS);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "getModemActivityInfo", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "getModemActivityInfo", e);
             }
         }
     }
@@ -4411,7 +4592,7 @@
             try {
                 simProxy.setAllowedCarriers(rr.mSerial, carrierRestrictionRules, result);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "setAllowedCarriers", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "setAllowedCarriers", e);
             }
         }
     }
@@ -4430,7 +4611,7 @@
             try {
                 simProxy.getAllowedCarriers(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "getAllowedCarriers", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "getAllowedCarriers", e);
             }
         }
     }
@@ -4450,7 +4631,7 @@
             try {
                 modemProxy.sendDeviceState(rr.mSerial, stateType, state);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(MODEM_SERVICE, "sendDeviceState", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_MODEM, "sendDeviceState", e);
             }
         }
     }
@@ -4470,7 +4651,7 @@
             try {
                 networkProxy.setIndicationFilter(rr.mSerial, filter);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setIndicationFilter", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "setIndicationFilter", e);
             }
         }
     }
@@ -4480,7 +4661,7 @@
             @NonNull List<SignalThresholdInfo> signalThresholdInfos, @Nullable Message result) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_2)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_1_2)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_SET_SIGNAL_STRENGTH_REPORTING_CRITERIA,
                     result, mRILDefaultWorkSource);
 
@@ -4491,7 +4672,7 @@
             try {
                 networkProxy.setSignalStrengthReportingCriteria(rr.mSerial, signalThresholdInfos);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE,
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
                         "setSignalStrengthReportingCriteria", e);
             }
         } else {
@@ -4505,7 +4686,7 @@
             Message result) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_2)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_1_2)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_SET_LINK_CAPACITY_REPORTING_CRITERIA, result,
                     mRILDefaultWorkSource);
 
@@ -4519,7 +4700,7 @@
                         ran);
             } catch (RemoteException | RuntimeException e) {
                 handleRadioProxyExceptionForRR(
-                        NETWORK_SERVICE, "setLinkCapacityReportingCriteria", e);
+                        HAL_SERVICE_NETWORK, "setLinkCapacityReportingCriteria", e);
             }
         } else {
             riljLoge("setLinkCapacityReportingCriteria ignored on IRadio version less than 1.2");
@@ -4541,7 +4722,7 @@
             try {
                 simProxy.setSimCardPower(rr.mSerial, state, result);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "setSimCardPower", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "setSimCardPower", e);
             }
         }
     }
@@ -4552,7 +4733,7 @@
         Objects.requireNonNull(imsiEncryptionInfo, "ImsiEncryptionInfo cannot be null.");
         RadioSimProxy simProxy = getRadioServiceProxy(RadioSimProxy.class, result);
         if (simProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_1)) {
+        if (mHalVersion.get(HAL_SERVICE_SIM).greaterOrEqual(RADIO_HAL_VERSION_1_1)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_SET_CARRIER_INFO_IMSI_ENCRYPTION, result,
                     mRILDefaultWorkSource);
             if (RILJ_LOGD) {
@@ -4562,7 +4743,8 @@
             try {
                 simProxy.setCarrierInfoForImsiEncryption(rr.mSerial, imsiEncryptionInfo);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "setCarrierInfoForImsiEncryption", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM,
+                        "setCarrierInfoForImsiEncryption", e);
             }
         } else {
             if (RILJ_LOGD) {
@@ -4582,7 +4764,7 @@
         Objects.requireNonNull(packetData, "KeepaliveRequest cannot be null.");
         RadioDataProxy dataProxy = getRadioServiceProxy(RadioDataProxy.class, result);
         if (dataProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_1)) {
+        if (mHalVersion.get(HAL_SERVICE_DATA).greaterOrEqual(RADIO_HAL_VERSION_1_1)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_START_KEEPALIVE, result,
                     mRILDefaultWorkSource);
 
@@ -4593,7 +4775,7 @@
             try {
                 dataProxy.startKeepalive(rr.mSerial, contextId, packetData, intervalMillis, result);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "startNattKeepalive", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "startNattKeepalive", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "startNattKeepalive: REQUEST_NOT_SUPPORTED");
@@ -4609,7 +4791,7 @@
     public void stopNattKeepalive(int sessionHandle, Message result) {
         RadioDataProxy dataProxy = getRadioServiceProxy(RadioDataProxy.class, result);
         if (dataProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_1)) {
+        if (mHalVersion.get(HAL_SERVICE_DATA).greaterOrEqual(RADIO_HAL_VERSION_1_1)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_STOP_KEEPALIVE, result,
                     mRILDefaultWorkSource);
 
@@ -4620,7 +4802,7 @@
             try {
                 dataProxy.stopKeepalive(rr.mSerial, sessionHandle);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "stopNattKeepalive", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "stopNattKeepalive", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "stopNattKeepalive: REQUEST_NOT_SUPPORTED");
@@ -4669,7 +4851,7 @@
     public void enableUiccApplications(boolean enable, Message result) {
         RadioSimProxy simProxy = getRadioServiceProxy(RadioSimProxy.class, result);
         if (simProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_5)) {
+        if (mHalVersion.get(HAL_SERVICE_SIM).greaterOrEqual(RADIO_HAL_VERSION_1_5)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_ENABLE_UICC_APPLICATIONS, result,
                     mRILDefaultWorkSource);
 
@@ -4680,7 +4862,7 @@
             try {
                 simProxy.enableUiccApplications(rr.mSerial, enable);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "enableUiccApplications", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "enableUiccApplications", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "enableUiccApplications: REQUEST_NOT_SUPPORTED");
@@ -4701,7 +4883,7 @@
     public void areUiccApplicationsEnabled(Message result) {
         RadioSimProxy simProxy = getRadioServiceProxy(RadioSimProxy.class, result);
         if (simProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_5)) {
+        if (mHalVersion.get(HAL_SERVICE_SIM).greaterOrEqual(RADIO_HAL_VERSION_1_5)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_GET_UICC_APPLICATIONS_ENABLEMENT, result,
                     mRILDefaultWorkSource);
 
@@ -4712,7 +4894,7 @@
             try {
                 simProxy.areUiccApplicationsEnabled(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "areUiccApplicationsEnabled", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "areUiccApplicationsEnabled", e);
             }
         } else {
             if (RILJ_LOGD) {
@@ -4732,7 +4914,7 @@
     @Override
     public boolean canToggleUiccApplicationsEnablement() {
         return !getRadioServiceProxy(RadioSimProxy.class, null).isEmpty()
-                && mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_5);
+                && mHalVersion.get(HAL_SERVICE_RADIO).greaterOrEqual(RADIO_HAL_VERSION_1_5);
     }
 
     @Override
@@ -4758,7 +4940,7 @@
                 voiceProxy.handleStkCallSetupRequestFromSim(rr.mSerial, accept);
             } catch (RemoteException | RuntimeException e) {
                 handleRadioProxyExceptionForRR(
-                        VOICE_SERVICE, "handleStkCallSetupRequestFromSim", e);
+                        HAL_SERVICE_VOICE, "handleStkCallSetupRequestFromSim", e);
             }
         }
     }
@@ -4770,7 +4952,7 @@
     public void getBarringInfo(Message result) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_5)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_1_5)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_GET_BARRING_INFO, result,
                     mRILDefaultWorkSource);
 
@@ -4781,7 +4963,7 @@
             try {
                 networkProxy.getBarringInfo(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getBarringInfo", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getBarringInfo", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "getBarringInfo: REQUEST_NOT_SUPPORTED");
@@ -4800,7 +4982,7 @@
     public void allocatePduSessionId(Message result) {
         RadioDataProxy dataProxy = getRadioServiceProxy(RadioDataProxy.class, result);
         if (dataProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_DATA).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_ALLOCATE_PDU_SESSION_ID, result,
                     mRILDefaultWorkSource);
             if (RILJ_LOGD) {
@@ -4810,7 +4992,7 @@
             try {
                 dataProxy.allocatePduSessionId(rr.mSerial);
             } catch (RemoteException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "allocatePduSessionId", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "allocatePduSessionId", e);
             }
         } else {
             AsyncResult.forMessage(result, null,
@@ -4826,7 +5008,7 @@
     public void releasePduSessionId(Message result, int pduSessionId) {
         RadioDataProxy dataProxy = getRadioServiceProxy(RadioDataProxy.class, result);
         if (dataProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_DATA).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_RELEASE_PDU_SESSION_ID, result,
                     mRILDefaultWorkSource);
             if (RILJ_LOGD) {
@@ -4836,7 +5018,7 @@
             try {
                 dataProxy.releasePduSessionId(rr.mSerial, pduSessionId);
             } catch (RemoteException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "releasePduSessionId", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "releasePduSessionId", e);
             }
         } else {
             AsyncResult.forMessage(result, null,
@@ -4852,7 +5034,7 @@
     public void startHandover(Message result, int callId) {
         RadioDataProxy dataProxy = getRadioServiceProxy(RadioDataProxy.class, result);
         if (dataProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_DATA).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_START_HANDOVER, result,
                     mRILDefaultWorkSource);
             if (RILJ_LOGD) {
@@ -4862,7 +5044,7 @@
             try {
                 dataProxy.startHandover(rr.mSerial, callId);
             } catch (RemoteException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "startHandover", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "startHandover", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "startHandover: REQUEST_NOT_SUPPORTED");
@@ -4881,7 +5063,7 @@
     public void cancelHandover(Message result, int callId) {
         RadioDataProxy dataProxy = getRadioServiceProxy(RadioDataProxy.class, result);
         if (dataProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_DATA).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_CANCEL_HANDOVER, result,
                     mRILDefaultWorkSource);
             if (RILJ_LOGD) {
@@ -4891,7 +5073,7 @@
             try {
                 dataProxy.cancelHandover(rr.mSerial, callId);
             } catch (RemoteException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "cancelHandover", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "cancelHandover", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "cancelHandover: REQUEST_NOT_SUPPORTED");
@@ -4908,7 +5090,7 @@
     public void getSlicingConfig(Message result) {
         RadioDataProxy dataProxy = getRadioServiceProxy(RadioDataProxy.class, result);
         if (dataProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_DATA).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_GET_SLICING_CONFIG, result,
                     mRILDefaultWorkSource);
 
@@ -4919,7 +5101,7 @@
             try {
                 dataProxy.getSlicingConfig(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(DATA_SERVICE, "getSlicingConfig", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_DATA, "getSlicingConfig", e);
             }
         } else {
             if (RILJ_LOGD) Rlog.d(RILJ_LOG_TAG, "getSlicingConfig: REQUEST_NOT_SUPPORTED");
@@ -4933,7 +5115,7 @@
     public void getSimPhonebookRecords(Message result) {
         RadioSimProxy simProxy = getRadioServiceProxy(RadioSimProxy.class, result);
         if (simProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_SIM).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_GET_SIM_PHONEBOOK_RECORDS, result,
                     mRILDefaultWorkSource);
 
@@ -4944,7 +5126,7 @@
             try {
                 simProxy.getSimPhonebookRecords(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "getSimPhonebookRecords", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "getSimPhonebookRecords", e);
             }
         } else {
             if (RILJ_LOGD) {
@@ -4962,7 +5144,7 @@
     public void getSimPhonebookCapacity(Message result) {
         RadioSimProxy simProxy = getRadioServiceProxy(RadioSimProxy.class, result);
         if (simProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_SIM).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_GET_SIM_PHONEBOOK_CAPACITY, result,
                     mRILDefaultWorkSource);
 
@@ -4973,7 +5155,7 @@
             try {
                 simProxy.getSimPhonebookCapacity(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "getSimPhonebookCapacity", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "getSimPhonebookCapacity", e);
             }
         } else {
             if (RILJ_LOGD) {
@@ -4991,7 +5173,7 @@
     public void updateSimPhonebookRecord(SimPhonebookRecord phonebookRecord, Message result) {
         RadioSimProxy simProxy = getRadioServiceProxy(RadioSimProxy.class, result);
         if (simProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
+        if (mHalVersion.get(HAL_SERVICE_SIM).greaterOrEqual(RADIO_HAL_VERSION_1_6)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_UPDATE_SIM_PHONEBOOK_RECORD, result,
                     mRILDefaultWorkSource);
 
@@ -5003,7 +5185,7 @@
             try {
                 simProxy.updateSimPhonebookRecords(rr.mSerial, phonebookRecord);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(SIM_SERVICE, "updateSimPhonebookRecords", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_SIM, "updateSimPhonebookRecords", e);
             }
         } else {
             if (RILJ_LOGD) {
@@ -5028,7 +5210,7 @@
             /* @TelephonyManager.UsageSetting */ int usageSetting) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_SET_USAGE_SETTING, result,
                     mRILDefaultWorkSource);
 
@@ -5039,7 +5221,7 @@
             try {
                 networkProxy.setUsageSetting(rr.mSerial, usageSetting);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "setUsageSetting", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "setUsageSetting", e);
             }
         } else {
             if (RILJ_LOGD) {
@@ -5062,7 +5244,7 @@
     public void getUsageSetting(Message result) {
         RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
         if (networkProxy.isEmpty()) return;
-        if (mRadioVersion.greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
             RILRequest rr = obtainRequest(RIL_REQUEST_GET_USAGE_SETTING, result,
                     mRILDefaultWorkSource);
 
@@ -5073,7 +5255,7 @@
             try {
                 networkProxy.getUsageSetting(rr.mSerial);
             } catch (RemoteException | RuntimeException e) {
-                handleRadioProxyExceptionForRR(NETWORK_SERVICE, "getUsageSetting", e);
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "getUsageSetting", e);
             }
         } else {
             if (RILJ_LOGD) {
@@ -5087,6 +5269,471 @@
         }
     }
 
+    @Override
+    public void setSrvccCallInfo(SrvccConnection[] srvccConnections, Message result) {
+        RadioImsProxy imsProxy = getRadioServiceProxy(RadioImsProxy.class, result);
+        if (imsProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_IMS).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_SET_SRVCC_CALL_INFO, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                // Do not log function arg for privacy
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest));
+            }
+
+            try {
+                imsProxy.setSrvccCallInfo(rr.mSerial,
+                        RILUtils.convertToHalSrvccCall(srvccConnections));
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_IMS, "setSrvccCallInfo", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "setSrvccCallInfo: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    @Override
+    public void updateImsRegistrationInfo(
+            @RegistrationManager.ImsRegistrationState int state,
+            @ImsRegistrationImplBase.ImsRegistrationTech int imsRadioTech,
+            @RegistrationManager.SuggestedAction int suggestedAction,
+            int capabilities, Message result) {
+        RadioImsProxy imsProxy = getRadioServiceProxy(RadioImsProxy.class, result);
+        if (imsProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_IMS).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_UPDATE_IMS_REGISTRATION_INFO, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest)
+                        + " state=" + state + ", radioTech=" + imsRadioTech
+                        + ", suggested=" + suggestedAction + ", cap=" + capabilities);
+            }
+
+            android.hardware.radio.ims.ImsRegistration registrationInfo =
+                    new android.hardware.radio.ims.ImsRegistration();
+            registrationInfo.regState = RILUtils.convertImsRegistrationState(state);
+            registrationInfo.accessNetworkType = RILUtils.convertImsRegistrationTech(imsRadioTech);
+            registrationInfo.suggestedAction = suggestedAction;
+            registrationInfo.capabilities = RILUtils.convertImsCapability(capabilities);
+
+            try {
+                imsProxy.updateImsRegistrationInfo(rr.mSerial, registrationInfo);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_IMS, "updateImsRegistrationInfo", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "updateImsRegistrationInfo: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    @Override
+    public void startImsTraffic(int token,
+            int trafficType, int accessNetworkType, int trafficDirection, Message result) {
+        RadioImsProxy imsProxy = getRadioServiceProxy(RadioImsProxy.class, result);
+        if (imsProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_IMS).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_START_IMS_TRAFFIC, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest)
+                        + "{" + token + ", " + trafficType + ", "
+                        + accessNetworkType + ", " + trafficDirection + "}");
+            }
+
+            try {
+                imsProxy.startImsTraffic(rr.mSerial, token,
+                        RILUtils.convertImsTrafficType(trafficType), accessNetworkType,
+                        RILUtils.convertImsTrafficDirection(trafficDirection));
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_IMS, "startImsTraffic", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "startImsTraffic: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    @Override
+    public void stopImsTraffic(int token, Message result) {
+        RadioImsProxy imsProxy = getRadioServiceProxy(RadioImsProxy.class, result);
+        if (imsProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_IMS).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_STOP_IMS_TRAFFIC, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest)
+                        + "{" + token + "}");
+            }
+
+            try {
+                imsProxy.stopImsTraffic(rr.mSerial, token);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_IMS, "stopImsTraffic", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "stopImsTraffic: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    @Override
+    public void triggerEpsFallback(int reason, Message result) {
+        RadioImsProxy imsProxy = getRadioServiceProxy(RadioImsProxy.class, result);
+        if (imsProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_IMS).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_TRIGGER_EPS_FALLBACK, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest)
+                        + " reason=" + reason);
+            }
+
+            try {
+                imsProxy.triggerEpsFallback(rr.mSerial, reason);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_IMS, "triggerEpsFallback", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "triggerEpsFallback: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    @Override
+    public void sendAnbrQuery(int mediaType, int direction, int bitsPerSecond,
+            Message result) {
+        RadioImsProxy imsProxy = getRadioServiceProxy(RadioImsProxy.class, result);
+        if (imsProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_IMS).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_SEND_ANBR_QUERY, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest));
+            }
+
+            try {
+                imsProxy.sendAnbrQuery(rr.mSerial, mediaType, direction, bitsPerSecond);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_IMS, "sendAnbrQuery", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "sendAnbrQuery: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setEmergencyMode(int emcMode, Message result) {
+        RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
+        if (networkProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_1)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_SET_EMERGENCY_MODE, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest)
+                        + " mode=" + EmergencyConstants.emergencyModeToString(emcMode));
+            }
+
+            try {
+                networkProxy.setEmergencyMode(rr.mSerial, emcMode);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "setEmergencyMode", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "setEmergencyMode: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void triggerEmergencyNetworkScan(
+            @NonNull @AccessNetworkConstants.RadioAccessNetworkType int[] accessNetwork,
+            @DomainSelectionService.EmergencyScanType int scanType, Message result) {
+        RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
+        if (networkProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_1)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_TRIGGER_EMERGENCY_NETWORK_SCAN, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest));
+            }
+
+            try {
+                networkProxy.triggerEmergencyNetworkScan(rr.mSerial,
+                        RILUtils.convertEmergencyNetworkScanTrigger(accessNetwork, scanType));
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
+                        "triggerEmergencyNetworkScan", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "triggerEmergencyNetworkScan: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void cancelEmergencyNetworkScan(boolean resetScan, Message result) {
+        RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
+        if (networkProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_1)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_CANCEL_EMERGENCY_NETWORK_SCAN, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest)
+                        + " resetScan=" + resetScan);
+            }
+
+            try {
+                networkProxy.cancelEmergencyNetworkScan(rr.mSerial, resetScan);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK,
+                        "cancelEmergencyNetworkScan", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "cancelEmergencyNetworkScan: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void exitEmergencyMode(Message result) {
+        RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
+        if (networkProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_1)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_EXIT_EMERGENCY_MODE, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest));
+            }
+
+            try {
+                networkProxy.exitEmergencyMode(rr.mSerial);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "exitEmergencyMode", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "exitEmergencyMode: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    /**
+     * Set if null ciphering / null integrity modes are permitted.
+     *
+     * @param result Callback message containing the success or failure status.
+     * @param enabled true if null ciphering / null integrity modes are permitted, false otherwise
+     */
+    @Override
+    public void setNullCipherAndIntegrityEnabled(boolean enabled, Message result) {
+        RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
+        if (networkProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_1)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_SET_NULL_CIPHER_AND_INTEGRITY_ENABLED, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest));
+            }
+
+            try {
+                networkProxy.setNullCipherAndIntegrityEnabled(rr.mSerial, enabled);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(
+                        HAL_SERVICE_NETWORK, "setNullCipherAndIntegrityEnabled", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "setNullCipherAndIntegrityEnabled: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void updateImsCallStatus(@NonNull List<ImsCallInfo> imsCallInfo, Message result) {
+        RadioImsProxy imsProxy = getRadioServiceProxy(RadioImsProxy.class, result);
+        if (imsProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_IMS).greaterOrEqual(RADIO_HAL_VERSION_2_0)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_UPDATE_IMS_CALL_STATUS, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest)
+                        + " " + imsCallInfo);
+            }
+            try {
+                imsProxy.updateImsCallStatus(rr.mSerial, RILUtils.convertImsCallInfo(imsCallInfo));
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_IMS, "updateImsCallStatus", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "updateImsCallStatus: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setN1ModeEnabled(boolean enable, Message result) {
+        RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
+        if (networkProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_1)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_SET_N1_MODE_ENABLED, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest)
+                        + " enable=" + enable);
+            }
+
+            try {
+                networkProxy.setN1ModeEnabled(rr.mSerial, enable);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "setN1ModeEnabled", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "setN1ModeEnabled: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void isN1ModeEnabled(Message result) {
+        RadioNetworkProxy networkProxy = getRadioServiceProxy(RadioNetworkProxy.class, result);
+        if (networkProxy.isEmpty()) return;
+        if (mHalVersion.get(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_1)) {
+            RILRequest rr = obtainRequest(RIL_REQUEST_IS_N1_MODE_ENABLED, result,
+                    mRILDefaultWorkSource);
+
+            if (RILJ_LOGD) {
+                riljLog(rr.serialString() + "> " + RILUtils.requestToString(rr.mRequest));
+            }
+
+            try {
+                networkProxy.isN1ModeEnabled(rr.mSerial);
+            } catch (RemoteException | RuntimeException e) {
+                handleRadioProxyExceptionForRR(HAL_SERVICE_NETWORK, "isN1ModeEnabled", e);
+            }
+        } else {
+            if (RILJ_LOGD) {
+                Rlog.d(RILJ_LOG_TAG, "isN1ModeEnabled: REQUEST_NOT_SUPPORTED");
+            }
+            if (result != null) {
+                AsyncResult.forMessage(result, null,
+                        CommandException.fromRilErrno(REQUEST_NOT_SUPPORTED));
+                result.sendToTarget();
+            }
+        }
+    }
+
     //***** Private Methods
     /**
      * This is a helper function to be called when an indication callback is called for any radio
@@ -5128,7 +5775,7 @@
      */
     @VisibleForTesting
     public RILRequest processResponse(RadioResponseInfo responseInfo) {
-        return processResponseInternal(RADIO_SERVICE, responseInfo.serial, responseInfo.error,
+        return processResponseInternal(HAL_SERVICE_RADIO, responseInfo.serial, responseInfo.error,
                 responseInfo.type);
     }
 
@@ -5142,7 +5789,7 @@
     @VisibleForTesting
     public RILRequest processResponse_1_6(
             android.hardware.radio.V1_6.RadioResponseInfo responseInfo) {
-        return processResponseInternal(RADIO_SERVICE, responseInfo.serial, responseInfo.error,
+        return processResponseInternal(HAL_SERVICE_RADIO, responseInfo.serial, responseInfo.error,
                 responseInfo.type);
     }
 
@@ -5154,7 +5801,6 @@
      * @param responseInfo RadioResponseInfo received in response callback
      * @return RILRequest corresponding to the response
      */
-    @VisibleForTesting
     public RILRequest processResponse(int service,
             android.hardware.radio.RadioResponseInfo responseInfo) {
         return processResponseInternal(service, responseInfo.serial, responseInfo.error,
@@ -5189,7 +5835,7 @@
                     + " ,error: " + error);
             return null;
         }
-        Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_NETWORK, "RIL", "" /* unused */, rr.mSerial);
+        Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_NETWORK, "RIL", rr.mSerial);
 
         // Time logging for RIL command and storing it in TelephonyHistogram.
         addToRilHistogram(rr);
@@ -5354,13 +6000,13 @@
         RILRequest rr = RILRequest.obtain(RIL_RESPONSE_ACKNOWLEDGEMENT, null,
                 mRILDefaultWorkSource);
         acquireWakeLock(rr, FOR_ACK_WAKELOCK);
-        if (service == RADIO_SERVICE) {
+        if (service == HAL_SERVICE_RADIO) {
             IRadio radioProxy = getRadioProxy(null);
             if (radioProxy != null) {
                 try {
                     radioProxy.responseAcknowledgement();
                 } catch (RemoteException | RuntimeException e) {
-                    handleRadioProxyExceptionForRR(RADIO_SERVICE, "sendAck", e);
+                    handleRadioProxyExceptionForRR(HAL_SERVICE_RADIO, "sendAck", e);
                     riljLoge("sendAck: " + e);
                 }
             } else {
@@ -5678,6 +6324,22 @@
                 sb.append("[").append(hwcfg).append("] ");
             }
             s = sb.toString();
+        } else if (req == RIL_REQUEST_START_IMS_TRAFFIC
+                || req == RIL_UNSOL_CONNECTION_SETUP_FAILURE) {
+            sb = new StringBuilder("{");
+            Object[] info = (Object[]) ret;
+            int token = (Integer) info[0];
+            sb.append(token).append(", ");
+            if (info[1] != null) {
+                ConnectionFailureInfo failureInfo = (ConnectionFailureInfo) info[1];
+                sb.append(failureInfo.getReason()).append(", ");
+                sb.append(failureInfo.getCauseCode()).append(", ");
+                sb.append(failureInfo.getWaitTimeMillis());
+            } else {
+                sb.append("null");
+            }
+            sb.append("}");
+            s = sb.toString();
         } else {
             // Check if toString() was overridden. Java classes created from HIDL have a built-in
             // toString() method, but AIDL classes only have it if the parcelable contains a
@@ -5855,6 +6517,13 @@
 
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         pw.println("RIL: " + this);
+        pw.println(" " + mServiceProxies.get(HAL_SERVICE_DATA));
+        pw.println(" " + mServiceProxies.get(HAL_SERVICE_MESSAGING));
+        pw.println(" " + mServiceProxies.get(HAL_SERVICE_MODEM));
+        pw.println(" " + mServiceProxies.get(HAL_SERVICE_NETWORK));
+        pw.println(" " + mServiceProxies.get(HAL_SERVICE_SIM));
+        pw.println(" " + mServiceProxies.get(HAL_SERVICE_VOICE));
+        pw.println(" " + mServiceProxies.get(HAL_SERVICE_IMS));
         pw.println(" mWakeLock=" + mWakeLock);
         pw.println(" mWakeLockTimeout=" + mWakeLockTimeout);
         synchronized (mRequestList) {
@@ -5930,31 +6599,53 @@
                 new CellSignalStrengthNr());
     }
 
-    /**
-     * Get the HAL version.
-     *
-     * @return the current HalVersion
-     */
-    public HalVersion getHalVersion() {
-        return mRadioVersion;
+    void notifyBarringInfoChanged(@NonNull BarringInfo barringInfo) {
+        mLastBarringInfo = barringInfo;
+        mBarringInfoChangedRegistrants.notifyRegistrants(new AsyncResult(null, barringInfo, null));
     }
 
-    private static String serviceToString(int service) {
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable BarringInfo getLastBarringInfo() {
+        return mLastBarringInfo;
+    }
+
+    /**
+     * Get the HAL version with a specific service.
+     *
+     * @param service the hal service id
+     * @return the current HalVersion
+     */
+    public HalVersion getHalVersion(int service) {
+        HalVersion halVersion = mHalVersion.get(service);
+        if (halVersion == null) {
+            if (isRadioServiceSupported(service)) {
+                halVersion = RADIO_HAL_VERSION_UNKNOWN;
+            } else {
+                halVersion = RADIO_HAL_VERSION_UNSUPPORTED;
+            }
+        }
+        return halVersion;
+    }
+
+    private static String serviceToString(@HalService int service) {
         switch (service) {
-            case RADIO_SERVICE:
+            case HAL_SERVICE_RADIO:
                 return "RADIO";
-            case DATA_SERVICE:
+            case HAL_SERVICE_DATA:
                 return "DATA";
-            case MESSAGING_SERVICE:
+            case HAL_SERVICE_MESSAGING:
                 return "MESSAGING";
-            case MODEM_SERVICE:
+            case HAL_SERVICE_MODEM:
                 return "MODEM";
-            case NETWORK_SERVICE:
+            case HAL_SERVICE_NETWORK:
                 return "NETWORK";
-            case SIM_SERVICE:
+            case HAL_SERVICE_SIM:
                 return "SIM";
-            case VOICE_SERVICE:
+            case HAL_SERVICE_VOICE:
                 return "VOICE";
+            case HAL_SERVICE_IMS:
+                return "IMS";
             default:
                 return "UNKNOWN:" + service;
         }
diff --git a/src/java/com/android/internal/telephony/RILUtils.java b/src/java/com/android/internal/telephony/RILUtils.java
index 61f1e82..95b0011 100644
--- a/src/java/com/android/internal/telephony/RILUtils.java
+++ b/src/java/com/android/internal/telephony/RILUtils.java
@@ -29,6 +29,7 @@
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_ALLOW_DATA;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_ANSWER;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_BASEBAND_VERSION;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_CANCEL_EMERGENCY_NETWORK_SCAN;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_CANCEL_HANDOVER;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_CANCEL_USSD;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_CDMA_BROADCAST_ACTIVATION;
@@ -74,6 +75,7 @@
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_ENTER_SIM_PUK;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_ENTER_SIM_PUK2;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_EXIT_EMERGENCY_MODE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_EXPLICIT_CALL_TRANSFER;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_GET_ACTIVITY_INFO;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_GET_ALLOWED_CARRIERS;
@@ -112,6 +114,7 @@
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_IMS_REGISTRATION_STATE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_IMS_SEND_SMS;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_ISIM_AUTHENTICATION;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_IS_N1_MODE_ENABLED;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_IS_NR_DUAL_CONNECTIVITY_ENABLED;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_IS_VONR_ENABLED;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_LAST_CALL_FAIL_CAUSE;
@@ -138,6 +141,7 @@
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_RESET_RADIO;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SCREEN_STATE;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SEND_ANBR_QUERY;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SEND_DEVICE_STATE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SEND_SMS;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SEND_SMS_EXPECT_MORE;
@@ -154,20 +158,24 @@
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_DATA_PROFILE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_DATA_THROTTLING;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_DC_RT_INFO_RATE;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_EMERGENCY_MODE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_FACILITY_LOCK;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_INITIAL_ATTACH_APN;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_LINK_CAPACITY_REPORTING_CRITERIA;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_LOCATION_UPDATES;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_LOGICAL_TO_PHYSICAL_SLOT_MAPPING;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_MUTE;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_N1_MODE_ENABLED;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_NULL_CIPHER_AND_INTEGRITY_ENABLED;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_PREFERRED_DATA_MODEM;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_RADIO_CAPABILITY;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_SIGNAL_STRENGTH_REPORTING_CRITERIA;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_SIM_CARD_POWER;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_SMSC_ADDRESS;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_SRVCC_CALL_INFO;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_SYSTEM_SELECTION_CHANNELS;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SET_TTY_MODE;
@@ -185,6 +193,7 @@
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SMS_ACKNOWLEDGE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_START_HANDOVER;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_START_IMS_TRAFFIC;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_START_KEEPALIVE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_START_LCE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_START_NETWORK_SCAN;
@@ -194,12 +203,17 @@
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_STK_SET_PROFILE;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_STOP_IMS_TRAFFIC;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_STOP_KEEPALIVE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_STOP_LCE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_STOP_NETWORK_SCAN;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SWITCH_DUAL_SIM_CONFIG;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_TRIGGER_EMERGENCY_NETWORK_SCAN;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_TRIGGER_EPS_FALLBACK;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_UDUB;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_UPDATE_IMS_CALL_STATUS;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_UPDATE_IMS_REGISTRATION_INFO;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_UPDATE_SIM_PHONEBOOK_RECORD;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_VOICE_RADIO_TECH;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_VOICE_REGISTRATION_STATE;
@@ -214,8 +228,10 @@
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CELL_INFO_LIST;
+import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CONNECTION_SETUP_FAILURE;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_DATA_CALL_LIST_CHANGED;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_DC_RT_INFO_CHANGED;
+import static com.android.internal.telephony.RILConstants.RIL_UNSOL_EMERGENCY_NETWORK_SCAN_RESULT;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_EMERGENCY_NUMBER_LIST;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
@@ -226,6 +242,7 @@
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_MODEM_RESTART;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_NETWORK_SCAN_RESULT;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_NITZ_TIME_RECEIVED;
+import static com.android.internal.telephony.RILConstants.RIL_UNSOL_NOTIFY_ANBR;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_OEM_HOOK_RAW;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_ON_SS;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_ON_USSD;
@@ -260,11 +277,13 @@
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_STK_PROACTIVE_COMMAND;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_STK_SESSION_END;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_SUPP_SVC_NOTIFICATION;
+import static com.android.internal.telephony.RILConstants.RIL_UNSOL_TRIGGER_IMS_DEREGISTRATION;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_UICC_APPLICATIONS_ENABLEMENT_CHANGED;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_UICC_SUBSCRIPTION_STATUS_CHANGED;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_UNTHROTTLE_APN;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_VOICE_RADIO_TECH_CHANGED;
 
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.net.InetAddresses;
 import android.net.LinkAddress;
@@ -298,8 +317,11 @@
 import android.telephony.CellSignalStrengthTdscdma;
 import android.telephony.CellSignalStrengthWcdma;
 import android.telephony.ClosedSubscriberGroupInfo;
+import android.telephony.DomainSelectionService;
+import android.telephony.EmergencyRegResult;
 import android.telephony.LinkCapacityEstimate;
 import android.telephony.ModemInfo;
+import android.telephony.NetworkRegistrationInfo;
 import android.telephony.PhoneCapability;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.PhysicalChannelConfig;
@@ -326,6 +348,11 @@
 import android.telephony.data.RouteSelectionDescriptor;
 import android.telephony.data.TrafficDescriptor;
 import android.telephony.data.UrspRule;
+import android.telephony.ims.RegistrationManager;
+import android.telephony.ims.feature.ConnectionFailureInfo;
+import android.telephony.ims.feature.MmTelFeature;
+import android.telephony.ims.stub.ImsRegistrationImplBase;
+import android.telephony.ims.stub.ImsRegistrationImplBase.ImsDeregistrationReason;
 import android.text.TextUtils;
 import android.util.ArraySet;
 import android.util.SparseArray;
@@ -339,6 +366,7 @@
 import com.android.internal.telephony.cdma.sms.SmsEnvelope;
 import com.android.internal.telephony.data.KeepaliveStatus;
 import com.android.internal.telephony.data.KeepaliveStatus.KeepaliveStatusCode;
+import com.android.internal.telephony.imsphone.ImsCallInfo;
 import com.android.internal.telephony.uicc.AdnCapacity;
 import com.android.internal.telephony.uicc.IccCardApplicationStatus;
 import com.android.internal.telephony.uicc.IccCardStatus;
@@ -346,6 +374,7 @@
 import com.android.internal.telephony.uicc.IccSlotPortMapping;
 import com.android.internal.telephony.uicc.IccSlotStatus;
 import com.android.internal.telephony.uicc.IccUtils;
+import com.android.internal.telephony.uicc.PortUtils;
 import com.android.internal.telephony.uicc.SimPhonebookRecord;
 import com.android.telephony.Rlog;
 
@@ -1441,8 +1470,9 @@
                     if (ComprehensionTlvTag.TEXT_STRING.value() == ctlv.getTag()) {
                         byte[] target = Arrays.copyOfRange(ctlv.getRawValue(), from,
                                 ctlv.getValueIndex() + ctlv.getLength());
-                        terminalResponse = terminalResponse.toLowerCase().replace(
-                                IccUtils.bytesToHexString(target).toLowerCase(), "********");
+                        terminalResponse = terminalResponse.toLowerCase(Locale.ROOT).replace(
+                                IccUtils.bytesToHexString(target).toLowerCase(Locale.ROOT),
+                                "********");
                     }
                     // The text string tag and the length field should also be hidden.
                     from = ctlv.getValueIndex() + ctlv.getLength();
@@ -1660,12 +1690,10 @@
         if ((networkTypeBitmask & TelephonyManager.NETWORK_TYPE_BITMASK_IWLAN) != 0) {
             raf |= android.hardware.radio.RadioAccessFamily.IWLAN;
         }
-        if ((networkTypeBitmask & TelephonyManager.NETWORK_TYPE_BITMASK_LTE) != 0) {
+        if ((networkTypeBitmask & TelephonyManager.NETWORK_TYPE_BITMASK_LTE) != 0
+                || (networkTypeBitmask & TelephonyManager.NETWORK_TYPE_BITMASK_LTE_CA) != 0) {
             raf |= android.hardware.radio.RadioAccessFamily.LTE;
         }
-        if ((networkTypeBitmask & TelephonyManager.NETWORK_TYPE_BITMASK_LTE_CA) != 0) {
-            raf |= android.hardware.radio.RadioAccessFamily.LTE_CA;
-        }
         if ((networkTypeBitmask & TelephonyManager.NETWORK_TYPE_BITMASK_NR) != 0) {
             raf |= android.hardware.radio.RadioAccessFamily.NR;
         }
@@ -1801,10 +1829,12 @@
      * @param p2 p2
      * @param p3 p3
      * @param data data
+     * @param radioHalVersion radio hal version
      * @return The converted SimApdu
      */
     public static android.hardware.radio.sim.SimApdu convertToHalSimApduAidl(int channel, int cla,
-            int instruction, int p1, int p2, int p3, String data) {
+            int instruction, int p1, int p2, int p3, String data, boolean isEs10Command,
+            HalVersion radioHalVersion) {
         android.hardware.radio.sim.SimApdu msg = new android.hardware.radio.sim.SimApdu();
         msg.sessionId = channel;
         msg.cla = cla;
@@ -1813,6 +1843,9 @@
         msg.p2 = p2;
         msg.p3 = p3;
         msg.data = convertNullToEmptyString(data);
+        if (radioHalVersion.greaterOrEqual(RIL.RADIO_HAL_VERSION_2_1)) {
+            msg.isEs10 = isEs10Command;
+        }
         return msg;
     }
 
@@ -3256,7 +3289,8 @@
             return new CellSignalStrengthNr(CellSignalStrengthNr.flip(ss.base.csiRsrp),
                     CellSignalStrengthNr.flip(ss.base.csiRsrq), ss.base.csiSinr,
                     ss.csiCqiTableIndex, ss.csiCqiReport, CellSignalStrengthNr.flip(ss.base.ssRsrp),
-                    CellSignalStrengthNr.flip(ss.base.ssRsrq), ss.base.ssSinr);
+                    CellSignalStrengthNr.flip(ss.base.ssRsrq), ss.base.ssSinr,
+                    CellInfo.UNAVAILABLE);
         }
         return null;
     }
@@ -3271,7 +3305,7 @@
         return new CellSignalStrengthNr(CellSignalStrengthNr.flip(ss.csiRsrp),
                 CellSignalStrengthNr.flip(ss.csiRsrq), ss.csiSinr, ss.csiCqiTableIndex,
                 primitiveArrayToArrayList(ss.csiCqiReport), CellSignalStrengthNr.flip(ss.ssRsrp),
-                CellSignalStrengthNr.flip(ss.ssRsrq), ss.ssSinr);
+                CellSignalStrengthNr.flip(ss.ssRsrq), ss.ssSinr, ss.timingAdvance);
     }
 
     private static ClosedSubscriberGroupInfo convertHalClosedSubscriberGroupInfo(
@@ -3836,7 +3870,7 @@
             case android.hardware.radio.data.Qos.nr:
                 android.hardware.radio.data.NrQos nr = qos.getNr();
                 return new NrQos(convertHalQosBandwidth(nr.downlink),
-                        convertHalQosBandwidth(nr.uplink), nr.qfi, nr.fiveQi,
+                        convertHalQosBandwidth(nr.uplink), nr.qosFlowIdentifier, nr.fiveQi,
                         nr.averagingWindowMs);
             default:
                 return null;
@@ -4331,6 +4365,7 @@
             android.hardware.radio.sim.CardStatus cardStatus) {
         IccCardStatus iccCardStatus = new IccCardStatus();
         iccCardStatus.setCardState(cardStatus.cardState);
+        iccCardStatus.setMultipleEnabledProfilesMode(cardStatus.supportedMepMode);
         iccCardStatus.setUniversalPinState(cardStatus.universalPinState);
         iccCardStatus.mGsmUmtsSubscriptionAppIndex = cardStatus.gsmUmtsSubscriptionAppIndex;
         iccCardStatus.mCdmaSubscriptionAppIndex = cardStatus.cdmaSubscriptionAppIndex;
@@ -4358,7 +4393,9 @@
         }
         IccSlotPortMapping slotPortMapping = new IccSlotPortMapping();
         slotPortMapping.mPhysicalSlotIndex = cardStatus.slotMap.physicalSlotId;
-        slotPortMapping.mPortIndex = cardStatus.slotMap.portId;
+        slotPortMapping.mPortIndex = PortUtils.convertFromHalPortIndex(
+                cardStatus.slotMap.physicalSlotId, cardStatus.slotMap.portId,
+                iccCardStatus.mCardState, iccCardStatus.mSupportedMepMode);
         iccCardStatus.mSlotPortMapping = slotPortMapping;
         return iccCardStatus;
     }
@@ -4474,6 +4511,7 @@
                 }
                 iccSlotStatus.atr = slotStatus.atr;
                 iccSlotStatus.eid = slotStatus.eid;
+                iccSlotStatus.setMultipleEnabledProfilesMode(slotStatus.supportedMepMode);
                 response.add(iccSlotStatus);
             }
             return response;
@@ -4541,7 +4579,8 @@
             int logicalSlotIdx = mapping.getLogicalSlotIndex();
             res[logicalSlotIdx] = new android.hardware.radio.config.SlotPortMapping();
             res[logicalSlotIdx].physicalSlotId = mapping.getPhysicalSlotIndex();
-            res[logicalSlotIdx].portId = mapping.getPortIndex();
+            res[logicalSlotIdx].portId = PortUtils.convertToHalPortIndex(
+                    mapping.getPhysicalSlotIndex(), mapping.getPortIndex());
         }
         return res;
     }
@@ -4586,10 +4625,199 @@
                 logicalModemList.add(new ModemInfo(modemInfo.modemId));
             }
         }
+        maxActiveVoiceCalls = maxActiveData;
         return new PhoneCapability(maxActiveVoiceCalls, maxActiveData, logicalModemList,
                 validationBeforeSwitchSupported, deviceNrCapabilities);
     }
 
+    /**
+     * Convert network scan type
+     * @param scanType The network scan type
+     * @return The converted EmergencyScanType
+     */
+    public static int convertEmergencyScanType(int scanType) {
+        switch (scanType) {
+            case DomainSelectionService.SCAN_TYPE_LIMITED_SERVICE:
+                return android.hardware.radio.network.EmergencyScanType.LIMITED_SERVICE;
+            case DomainSelectionService.SCAN_TYPE_FULL_SERVICE:
+                return android.hardware.radio.network.EmergencyScanType.FULL_SERVICE;
+            default:
+                return android.hardware.radio.network.EmergencyScanType.NO_PREFERENCE;
+        }
+    }
+
+    /**
+     * Convert to EmergencyNetworkScanTrigger
+     * @param accessNetwork The list of access network types
+     * @param scanType The network scan type
+     * @return The converted EmergencyNetworkScanTrigger
+     */
+    public static android.hardware.radio.network.EmergencyNetworkScanTrigger
+            convertEmergencyNetworkScanTrigger(@NonNull int[] accessNetwork, int scanType) {
+        int[] halAccessNetwork = new int[accessNetwork.length];
+        for (int i = 0; i < accessNetwork.length; i++) {
+            halAccessNetwork[i] = convertToHalAccessNetworkAidl(accessNetwork[i]);
+        }
+
+        android.hardware.radio.network.EmergencyNetworkScanTrigger scanRequest =
+                new android.hardware.radio.network.EmergencyNetworkScanTrigger();
+
+        scanRequest.accessNetwork = halAccessNetwork;
+        scanRequest.scanType = convertEmergencyScanType(scanType);
+        return scanRequest;
+    }
+
+    /**
+     * Convert EmergencyRegResult.aidl to EmergencyRegResult.
+     * @param halResult EmergencyRegResult.aidl in HAL.
+     * @return Converted EmergencyRegResult.
+     */
+    public static EmergencyRegResult convertHalEmergencyRegResult(
+            android.hardware.radio.network.EmergencyRegResult halResult) {
+        return new EmergencyRegResult(
+                halResult.accessNetwork,
+                convertHalRegState(halResult.regState),
+                halResult.emcDomain,
+                halResult.isVopsSupported,
+                halResult.isEmcBearerSupported,
+                halResult.nwProvidedEmc,
+                halResult.nwProvidedEmf,
+                halResult.mcc,
+                halResult.mnc,
+                getCountryCodeForMccMnc(halResult.mcc, halResult.mnc));
+    }
+
+    private static @NonNull String getCountryCodeForMccMnc(
+            @NonNull String mcc, @NonNull String mnc) {
+        if (TextUtils.isEmpty(mcc)) return "";
+        if (TextUtils.isEmpty(mnc)) mnc = "000";
+        String operatorNumeric = TextUtils.concat(mcc, mnc).toString();
+
+        MccTable.MccMnc mccMnc = MccTable.MccMnc.fromOperatorNumeric(operatorNumeric);
+        return MccTable.geoCountryCodeForMccMnc(mccMnc);
+    }
+
+    /**
+     * Convert RegResult.aidl to RegistrationState.
+     * @param halRegState RegResult in HAL.
+     * @return Converted RegistrationState.
+     */
+    public static @NetworkRegistrationInfo.RegistrationState int convertHalRegState(
+            int halRegState) {
+        switch (halRegState) {
+            case android.hardware.radio.network.RegState.NOT_REG_MT_NOT_SEARCHING_OP:
+            case android.hardware.radio.network.RegState.NOT_REG_MT_NOT_SEARCHING_OP_EM:
+                return NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING;
+            case android.hardware.radio.network.RegState.REG_HOME:
+                return NetworkRegistrationInfo.REGISTRATION_STATE_HOME;
+            case android.hardware.radio.network.RegState.NOT_REG_MT_SEARCHING_OP:
+            case android.hardware.radio.network.RegState.NOT_REG_MT_SEARCHING_OP_EM:
+                return NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_SEARCHING;
+            case android.hardware.radio.network.RegState.REG_DENIED:
+            case android.hardware.radio.network.RegState.REG_DENIED_EM:
+                return NetworkRegistrationInfo.REGISTRATION_STATE_DENIED;
+            case android.hardware.radio.network.RegState.UNKNOWN:
+            case android.hardware.radio.network.RegState.UNKNOWN_EM:
+                return NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN;
+            case android.hardware.radio.network.RegState.REG_ROAMING:
+                return NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING;
+            default:
+                return NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING;
+        }
+    }
+
+    /** Convert IMS deregistration reason */
+    public static @ImsDeregistrationReason int convertHalDeregistrationReason(int reason) {
+        switch (reason) {
+            case android.hardware.radio.ims.ImsDeregistrationReason.REASON_SIM_REMOVED:
+                return ImsRegistrationImplBase.REASON_SIM_REMOVED;
+            case android.hardware.radio.ims.ImsDeregistrationReason.REASON_SIM_REFRESH:
+                return ImsRegistrationImplBase.REASON_SIM_REFRESH;
+            case android.hardware.radio.ims.ImsDeregistrationReason
+                    .REASON_ALLOWED_NETWORK_TYPES_CHANGED:
+                return ImsRegistrationImplBase.REASON_ALLOWED_NETWORK_TYPES_CHANGED;
+            default:
+                return ImsRegistrationImplBase.REASON_UNKNOWN;
+        }
+    }
+
+    /**
+     * Convert the IMS traffic type.
+     * @param trafficType IMS traffic type like registration, voice, video, SMS, emergency, and etc.
+     * @return The converted IMS traffic type.
+     */
+    public static int convertImsTrafficType(@MmTelFeature.ImsTrafficType int trafficType) {
+        switch (trafficType) {
+            case MmTelFeature.IMS_TRAFFIC_TYPE_EMERGENCY:
+                return android.hardware.radio.ims.ImsTrafficType.EMERGENCY;
+            case MmTelFeature.IMS_TRAFFIC_TYPE_EMERGENCY_SMS:
+                return android.hardware.radio.ims.ImsTrafficType.EMERGENCY_SMS;
+            case MmTelFeature.IMS_TRAFFIC_TYPE_VOICE:
+                return android.hardware.radio.ims.ImsTrafficType.VOICE;
+            case MmTelFeature.IMS_TRAFFIC_TYPE_VIDEO:
+                return android.hardware.radio.ims.ImsTrafficType.VIDEO;
+            case MmTelFeature.IMS_TRAFFIC_TYPE_SMS:
+                return android.hardware.radio.ims.ImsTrafficType.SMS;
+            case MmTelFeature.IMS_TRAFFIC_TYPE_REGISTRATION:
+                return android.hardware.radio.ims.ImsTrafficType.REGISTRATION;
+        }
+        return android.hardware.radio.ims.ImsTrafficType.UT_XCAP;
+    }
+
+    /**
+     * Convert the IMS traffic direction.
+     * @param trafficDirection Indicates the traffic direction.
+     * @return The converted IMS traffic direction.
+     */
+    public static int convertImsTrafficDirection(
+            @MmTelFeature.ImsTrafficDirection int trafficDirection) {
+        switch (trafficDirection) {
+            case MmTelFeature.IMS_TRAFFIC_DIRECTION_INCOMING:
+                return android.hardware.radio.ims.ImsCall.Direction.INCOMING;
+            default:
+                return android.hardware.radio.ims.ImsCall.Direction.OUTGOING;
+        }
+    }
+
+    /**
+     * Convert the IMS connection failure reason.
+     * @param halReason  Specifies the reason that IMS connection failed.
+     * @return The converted IMS connection failure reason.
+     */
+    public static @ConnectionFailureInfo.FailureReason int convertHalConnectionFailureReason(
+            int halReason) {
+        switch (halReason) {
+            case android.hardware.radio.ims.ConnectionFailureInfo
+                    .ConnectionFailureReason.REASON_ACCESS_DENIED:
+                return ConnectionFailureInfo.REASON_ACCESS_DENIED;
+            case android.hardware.radio.ims.ConnectionFailureInfo
+                    .ConnectionFailureReason.REASON_NAS_FAILURE:
+                return ConnectionFailureInfo.REASON_NAS_FAILURE;
+            case android.hardware.radio.ims.ConnectionFailureInfo
+                    .ConnectionFailureReason.REASON_RACH_FAILURE:
+                return ConnectionFailureInfo.REASON_RACH_FAILURE;
+            case android.hardware.radio.ims.ConnectionFailureInfo
+                    .ConnectionFailureReason.REASON_RLC_FAILURE:
+                return ConnectionFailureInfo.REASON_RLC_FAILURE;
+            case android.hardware.radio.ims.ConnectionFailureInfo
+                    .ConnectionFailureReason.REASON_RRC_REJECT:
+                return ConnectionFailureInfo.REASON_RRC_REJECT;
+            case android.hardware.radio.ims.ConnectionFailureInfo
+                    .ConnectionFailureReason.REASON_RRC_TIMEOUT:
+                return ConnectionFailureInfo.REASON_RRC_TIMEOUT;
+            case android.hardware.radio.ims.ConnectionFailureInfo
+                    .ConnectionFailureReason.REASON_NO_SERVICE:
+                return ConnectionFailureInfo.REASON_NO_SERVICE;
+            case android.hardware.radio.ims.ConnectionFailureInfo
+                    .ConnectionFailureReason.REASON_PDN_NOT_AVAILABLE:
+                return ConnectionFailureInfo.REASON_PDN_NOT_AVAILABLE;
+            case android.hardware.radio.ims.ConnectionFailureInfo
+                    .ConnectionFailureReason.REASON_RF_BUSY:
+                return ConnectionFailureInfo.REASON_RF_BUSY;
+        }
+        return ConnectionFailureInfo.REASON_UNSPECIFIED;
+    }
+
     /** Append the data to the end of an ArrayList */
     public static void appendPrimitiveArrayToArrayList(byte[] src, ArrayList<Byte> dst) {
         for (byte b : src) {
@@ -5041,6 +5269,34 @@
                 return "SET_USAGE_SETTING";
             case RIL_REQUEST_GET_USAGE_SETTING:
                 return "GET_USAGE_SETTING";
+            case RIL_REQUEST_SET_EMERGENCY_MODE:
+                return "SET_EMERGENCY_MODE";
+            case RIL_REQUEST_TRIGGER_EMERGENCY_NETWORK_SCAN:
+                return "TRIGGER_EMERGENCY_NETWORK_SCAN";
+            case RIL_REQUEST_CANCEL_EMERGENCY_NETWORK_SCAN:
+                return "CANCEL_EMERGENCY_NETWORK_SCAN";
+            case RIL_REQUEST_EXIT_EMERGENCY_MODE:
+                return "EXIT_EMERGENCY_MODE";
+            case RIL_REQUEST_SET_SRVCC_CALL_INFO:
+                return "SET_SRVCC_CALL_INFO";
+            case RIL_REQUEST_UPDATE_IMS_REGISTRATION_INFO:
+                return "UPDATE_IMS_REGISTRATION_INFO";
+            case RIL_REQUEST_START_IMS_TRAFFIC:
+                return "START_IMS_TRAFFIC";
+            case RIL_REQUEST_STOP_IMS_TRAFFIC:
+                return "STOP_IMS_TRAFFIC";
+            case RIL_REQUEST_SEND_ANBR_QUERY:
+                return "SEND_ANBR_QUERY";
+            case RIL_REQUEST_TRIGGER_EPS_FALLBACK:
+                return "TRIGGER_EPS_FALLBACK";
+            case RIL_REQUEST_SET_NULL_CIPHER_AND_INTEGRITY_ENABLED:
+                return "SET_NULL_CIPHER_AND_INTEGRITY_ENABLED";
+            case RIL_REQUEST_UPDATE_IMS_CALL_STATUS:
+                return "UPDATE_IMS_CALL_STATUS";
+            case RIL_REQUEST_SET_N1_MODE_ENABLED:
+                return "SET_N1_MODE_ENABLED";
+            case RIL_REQUEST_IS_N1_MODE_ENABLED:
+                return "IS_N1_MODE_ENABLED";
             default:
                 return "<unknown request " + request + ">";
         }
@@ -5173,6 +5429,14 @@
                 return "UNSOL_REGISTRATION_FAILED";
             case RIL_UNSOL_BARRING_INFO_CHANGED:
                 return "UNSOL_BARRING_INFO_CHANGED";
+            case RIL_UNSOL_EMERGENCY_NETWORK_SCAN_RESULT:
+                return "UNSOL_EMERGENCY_NETWORK_SCAN_RESULT";
+            case RIL_UNSOL_CONNECTION_SETUP_FAILURE:
+                return "UNSOL_CONNECTION_SETUP_FAILURE";
+            case RIL_UNSOL_NOTIFY_ANBR:
+                return "UNSOL_NOTIFY_ANBR";
+            case RIL_UNSOL_TRIGGER_IMS_DEREGISTRATION:
+                return "UNSOL_TRIGGER_IMS_DEREGISTRATION";
             default:
                 return "<unknown response>";
         }
@@ -5321,6 +5585,261 @@
         return sb.toString();
     }
 
+    /**
+     * Converts the list of call information for Single Radio Voice Call Continuity(SRVCC).
+     *
+     * @param srvccConnections The list of call information for SRVCC.
+     * @return The converted list of call information.
+     */
+    public static android.hardware.radio.ims.SrvccCall[] convertToHalSrvccCall(
+            SrvccConnection[] srvccConnections) {
+        if (srvccConnections == null) {
+            return new android.hardware.radio.ims.SrvccCall[0];
+        }
+
+        int length = srvccConnections.length;
+        android.hardware.radio.ims.SrvccCall[] srvccCalls =
+                new android.hardware.radio.ims.SrvccCall[length];
+
+        for (int i = 0; i < length; i++) {
+            srvccCalls[i] = new android.hardware.radio.ims.SrvccCall();
+            srvccCalls[i].index = i + 1;
+            srvccCalls[i].callType = convertSrvccCallType(srvccConnections[i].getType());
+            srvccCalls[i].callState = convertCallState(srvccConnections[i].getState());
+            srvccCalls[i].callSubstate =
+                    convertSrvccCallSubState(srvccConnections[i].getSubState());
+            srvccCalls[i].ringbackToneType =
+                    convertSrvccCallRingbackToneType(srvccConnections[i].getRingbackToneType());
+            srvccCalls[i].isMpty = srvccConnections[i].isMultiParty();
+            srvccCalls[i].isMT = srvccConnections[i].isIncoming();
+            srvccCalls[i].number = TextUtils.emptyIfNull(srvccConnections[i].getNumber());
+            srvccCalls[i].numPresentation =
+                    convertPresentation(srvccConnections[i].getNumberPresentation());
+            srvccCalls[i].name = TextUtils.emptyIfNull(srvccConnections[i].getName());
+            srvccCalls[i].namePresentation =
+                    convertPresentation(srvccConnections[i].getNamePresentation());
+        }
+
+        return srvccCalls;
+    }
+
+    /**
+     * Converts the call type.
+     *
+     * @param type The call type.
+     * @return The converted call type.
+     */
+    public static int convertSrvccCallType(int type) {
+        switch (type) {
+            case  SrvccConnection.CALL_TYPE_NORMAL:
+                return android.hardware.radio.ims.SrvccCall.CallType.NORMAL;
+            case  SrvccConnection.CALL_TYPE_EMERGENCY:
+                return android.hardware.radio.ims.SrvccCall.CallType.EMERGENCY;
+            default:
+                throw new RuntimeException("illegal call type " + type);
+        }
+    }
+
+    /**
+     * Converts the call state.
+     *
+     * @param state The call state.
+     * @return The converted call state.
+     */
+    public static int convertCallState(Call.State state) {
+        switch (state) {
+            case ACTIVE: return android.hardware.radio.voice.Call.STATE_ACTIVE;
+            case HOLDING: return android.hardware.radio.voice.Call.STATE_HOLDING;
+            case DIALING: return android.hardware.radio.voice.Call.STATE_DIALING;
+            case ALERTING: return android.hardware.radio.voice.Call.STATE_ALERTING;
+            case INCOMING: return android.hardware.radio.voice.Call.STATE_INCOMING;
+            case WAITING: return android.hardware.radio.voice.Call.STATE_WAITING;
+            default:
+                throw new RuntimeException("illegal state " + state);
+        }
+    }
+
+    /**
+     * Converts the substate of a call.
+     *
+     * @param state The substate of a call.
+     * @return The converted substate.
+     */
+    public static int convertSrvccCallSubState(int state) {
+        switch (state) {
+            case SrvccConnection.SUBSTATE_NONE:
+                return android.hardware.radio.ims.SrvccCall.CallSubState.NONE;
+            case SrvccConnection.SUBSTATE_PREALERTING:
+                return android.hardware.radio.ims.SrvccCall.CallSubState.PREALERTING;
+            default:
+                throw new RuntimeException("illegal substate " + state);
+        }
+    }
+
+    /**
+     * Converts the ringback tone type.
+     *
+     * @param type The ringback tone type.
+     * @return The converted ringback tone type.
+     */
+    public static int convertSrvccCallRingbackToneType(int type) {
+        switch (type) {
+            case SrvccConnection.TONE_NONE:
+                return android.hardware.radio.ims.SrvccCall.ToneType.NONE;
+            case SrvccConnection.TONE_LOCAL:
+                return android.hardware.radio.ims.SrvccCall.ToneType.LOCAL;
+            case SrvccConnection.TONE_NETWORK:
+                return android.hardware.radio.ims.SrvccCall.ToneType.NETWORK;
+            default:
+                throw new RuntimeException("illegal ringback tone type " + type);
+        }
+    }
+
+    /**
+     * Converts the number presentation type for caller id display.
+     *
+     * @param presentation The number presentation type.
+     * @return The converted presentation type.
+     */
+    public static int convertPresentation(int presentation) {
+        switch (presentation) {
+            case PhoneConstants.PRESENTATION_ALLOWED:
+                return android.hardware.radio.voice.Call.PRESENTATION_ALLOWED;
+            case PhoneConstants.PRESENTATION_RESTRICTED:
+                return android.hardware.radio.voice.Call.PRESENTATION_RESTRICTED;
+            case PhoneConstants.PRESENTATION_UNKNOWN:
+                return android.hardware.radio.voice.Call.PRESENTATION_UNKNOWN;
+            case PhoneConstants.PRESENTATION_PAYPHONE:
+                return android.hardware.radio.voice.Call.PRESENTATION_PAYPHONE;
+            default:
+                throw new RuntimeException("illegal presentation " + presentation);
+        }
+    }
+
+    /**
+     * Converts IMS registration state.
+     *
+     * @param state The IMS registration state.
+     * @return The converted HAL IMS registration state.
+     */
+    public static int convertImsRegistrationState(int state) {
+        switch (state) {
+            case RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED:
+                return android.hardware.radio.ims.ImsRegistrationState.NOT_REGISTERED;
+            case RegistrationManager.REGISTRATION_STATE_REGISTERED:
+                return android.hardware.radio.ims.ImsRegistrationState.REGISTERED;
+            default:
+                throw new RuntimeException("illegal state " + state);
+        }
+    }
+
+    /**
+     * Converts IMS service radio technology.
+     *
+     * @param imsRadioTech The IMS service radio technology.
+     * @return The converted HAL access network type.
+     */
+
+    public static int convertImsRegistrationTech(
+            @ImsRegistrationImplBase.ImsRegistrationTech int imsRadioTech) {
+        switch (imsRadioTech) {
+            case ImsRegistrationImplBase.REGISTRATION_TECH_LTE:
+                return android.hardware.radio.AccessNetwork.EUTRAN;
+            case ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN:
+                return android.hardware.radio.AccessNetwork.IWLAN;
+            case ImsRegistrationImplBase.REGISTRATION_TECH_NR:
+                return android.hardware.radio.AccessNetwork.NGRAN;
+            case ImsRegistrationImplBase.REGISTRATION_TECH_3G:
+                return android.hardware.radio.AccessNetwork.UTRAN;
+            default:
+                return android.hardware.radio.AccessNetwork.UNKNOWN;
+        }
+    }
+
+    /**
+     * Converts IMS capabilities.
+     *
+     * @param capabilities The IMS capabilities.
+     * @return The converted HAL IMS capabilities.
+     */
+    public static int convertImsCapability(int capabilities) {
+        int halCapabilities = android.hardware.radio.ims.ImsRegistration.IMS_MMTEL_CAPABILITY_NONE;
+        if ((capabilities & CommandsInterface.IMS_MMTEL_CAPABILITY_VOICE) > 0) {
+            halCapabilities |=
+                    android.hardware.radio.ims.ImsRegistration.IMS_MMTEL_CAPABILITY_VOICE;
+        }
+        if ((capabilities & CommandsInterface.IMS_MMTEL_CAPABILITY_VIDEO) > 0) {
+            halCapabilities |=
+                    android.hardware.radio.ims.ImsRegistration.IMS_MMTEL_CAPABILITY_VIDEO;
+        }
+        if ((capabilities & CommandsInterface.IMS_MMTEL_CAPABILITY_SMS) > 0) {
+            halCapabilities |= android.hardware.radio.ims.ImsRegistration.IMS_MMTEL_CAPABILITY_SMS;
+        }
+        if ((capabilities & CommandsInterface.IMS_RCS_CAPABILITIES) > 0) {
+            halCapabilities |= android.hardware.radio.ims.ImsRegistration.IMS_RCS_CAPABILITIES;
+        }
+        return halCapabilities;
+    }
+
+    /** Converts the ImsCallInfo instances to HAL ImsCall instances. */
+    public static android.hardware.radio.ims.ImsCall[] convertImsCallInfo(
+            List<ImsCallInfo> imsCallInfos) {
+        if (imsCallInfos == null) {
+            return new android.hardware.radio.ims.ImsCall[0];
+        }
+
+        int length = 0;
+        for (int i = 0; i < imsCallInfos.size(); i++) {
+            if (imsCallInfos.get(i) != null) length++;
+        }
+        if (length == 0) {
+            return new android.hardware.radio.ims.ImsCall[0];
+        }
+
+        android.hardware.radio.ims.ImsCall[] halInfos =
+                new android.hardware.radio.ims.ImsCall[length];
+
+        int index = 0;
+        for (int i = 0; i < imsCallInfos.size(); i++) {
+            ImsCallInfo info = imsCallInfos.get(i);
+            if (info == null) continue;
+
+            halInfos[index] = new android.hardware.radio.ims.ImsCall();
+            halInfos[index].index = info.getIndex();
+            halInfos[index].callState = convertToHalImsCallState(info.getCallState());
+            halInfos[index].callType = info.isEmergencyCall()
+                    ? android.hardware.radio.ims.ImsCall.CallType.EMERGENCY
+                    : android.hardware.radio.ims.ImsCall.CallType.NORMAL;
+            halInfos[index].accessNetwork = convertToHalAccessNetworkAidl(info.getCallRadioTech());
+            halInfos[index].direction = info.isIncoming()
+                    ? android.hardware.radio.ims.ImsCall.Direction.INCOMING
+                    : android.hardware.radio.ims.ImsCall.Direction.OUTGOING;
+            halInfos[index].isHeldByRemote = info.isHeldByRemote();
+            index++;
+        }
+
+        return halInfos;
+    }
+
+    /**
+     * Converts the call state to HAL IMS call state.
+     *
+     * @param state The {@link Call.State}.
+     * @return The converted {@link android.hardware.radio.ims.ImsCall.CallState}.
+     */
+    private static int convertToHalImsCallState(Call.State state) {
+        switch (state) {
+            case ACTIVE: return android.hardware.radio.ims.ImsCall.CallState.ACTIVE;
+            case HOLDING: return android.hardware.radio.ims.ImsCall.CallState.HOLDING;
+            case DIALING: return android.hardware.radio.ims.ImsCall.CallState.DIALING;
+            case ALERTING: return android.hardware.radio.ims.ImsCall.CallState.ALERTING;
+            case INCOMING: return android.hardware.radio.ims.ImsCall.CallState.INCOMING;
+            case WAITING: return android.hardware.radio.ims.ImsCall.CallState.WAITING;
+            case DISCONNECTING: return android.hardware.radio.ims.ImsCall.CallState.DISCONNECTING;
+            default: return android.hardware.radio.ims.ImsCall.CallState.DISCONNECTED;
+        }
+    }
+
     private static void logd(String log) {
         Rlog.d("RILUtils", log);
     }
diff --git a/src/java/com/android/internal/telephony/RadioConfig.java b/src/java/com/android/internal/telephony/RadioConfig.java
index e455ecb..3e2be1d 100644
--- a/src/java/com/android/internal/telephony/RadioConfig.java
+++ b/src/java/com/android/internal/telephony/RadioConfig.java
@@ -358,7 +358,7 @@
 
             if (rr != null) {
                 Trace.asyncTraceForTrackEnd(
-                        Trace.TRACE_TAG_NETWORK, "RIL", "" /* unused */, rr.mSerial);
+                        Trace.TRACE_TAG_NETWORK, "RIL", rr.mSerial);
                 mRequestList.remove(serial);
             }
         }
@@ -642,4 +642,9 @@
     private static void loge(String log) {
         Rlog.e(TAG, log);
     }
+
+    @Override
+    public String toString() {
+        return "RadioConfig[" + "mRadioConfigProxy=" + mRadioConfigProxy + ']';
+    }
 }
diff --git a/src/java/com/android/internal/telephony/RadioConfigProxy.java b/src/java/com/android/internal/telephony/RadioConfigProxy.java
index a8601f1..edeb558 100644
--- a/src/java/com/android/internal/telephony/RadioConfigProxy.java
+++ b/src/java/com/android/internal/telephony/RadioConfigProxy.java
@@ -336,4 +336,11 @@
                     mRadioConfig.obtainMessage(RadioConfig.EVENT_AIDL_SERVICE_DEAD));
         }
     }
+
+    @Override
+    public String toString() {
+        return "RadioConfigProxy["
+                + "mRadioHalVersion=" + mRadioHalVersion
+                + ", mRadioConfigHalVersion=" + mRadioConfigHalVersion + ']';
+    }
 }
diff --git a/src/java/com/android/internal/telephony/RadioDataProxy.java b/src/java/com/android/internal/telephony/RadioDataProxy.java
index cbc762a..f110545 100644
--- a/src/java/com/android/internal/telephony/RadioDataProxy.java
+++ b/src/java/com/android/internal/telephony/RadioDataProxy.java
@@ -45,12 +45,33 @@
      * Set IRadioData as the AIDL implementation for RadioServiceProxy
      * @param halVersion Radio HAL version
      * @param data IRadioData implementation
+     *
+     * @return updated HAL version
      */
-    public void setAidl(HalVersion halVersion, android.hardware.radio.data.IRadioData data) {
+    public HalVersion setAidl(HalVersion halVersion, android.hardware.radio.data.IRadioData data) {
         mHalVersion = halVersion;
         mDataProxy = data;
         mIsAidl = true;
-        Rlog.d(TAG, "AIDL initialized");
+
+        try {
+            HalVersion newHalVersion;
+            int version = data.getInterfaceVersion();
+            switch(version) {
+                default:
+                    newHalVersion = RIL.RADIO_HAL_VERSION_2_0;
+                    break;
+            }
+            Rlog.d(TAG, "AIDL version=" + version + ", halVersion=" + newHalVersion);
+
+            if (mHalVersion.less(newHalVersion)) {
+                mHalVersion = newHalVersion;
+            }
+        } catch (RemoteException e) {
+            Rlog.e(TAG, "setAidl: " + e);
+        }
+
+        Rlog.d(TAG, "AIDL initialized mHalVersion=" + mHalVersion);
+        return mHalVersion;
     }
 
     /**
diff --git a/src/java/com/android/internal/telephony/RadioImsProxy.java b/src/java/com/android/internal/telephony/RadioImsProxy.java
new file mode 100644
index 0000000..3ad1b94
--- /dev/null
+++ b/src/java/com/android/internal/telephony/RadioImsProxy.java
@@ -0,0 +1,205 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import android.os.RemoteException;
+import android.telephony.Rlog;
+
+/**
+ * A holder for IRadioIms.
+ * Use getAidl to get IRadioIms and call the AIDL implementations of the HAL APIs.
+ */
+public class RadioImsProxy extends RadioServiceProxy {
+    private static final String TAG = "RadioImsProxy";
+    private volatile android.hardware.radio.ims.IRadioIms mImsProxy = null;
+
+    /**
+     * Sets IRadioIms as the AIDL implementation for RadioServiceProxy.
+     * @param halVersion Radio HAL version.
+     * @param ims IRadioIms implementation.
+     *
+     * @return updated HAL version.
+     */
+    public HalVersion setAidl(HalVersion halVersion, android.hardware.radio.ims.IRadioIms ims) {
+        mHalVersion = halVersion;
+        mImsProxy = ims;
+        mIsAidl = true;
+
+        try {
+            HalVersion newHalVersion;
+            int version = ims.getInterfaceVersion();
+            switch(version) {
+                default:
+                    newHalVersion = RIL.RADIO_HAL_VERSION_2_0;
+                    break;
+            }
+            Rlog.d(TAG, "AIDL version=" + version + ", halVersion=" + newHalVersion);
+
+            if (mHalVersion.less(newHalVersion)) {
+                mHalVersion = newHalVersion;
+            }
+        } catch (RemoteException e) {
+            Rlog.e(TAG, "setAidl: " + e);
+        }
+
+        Rlog.d(TAG, "AIDL initialized mHalVersion=" + mHalVersion);
+        return mHalVersion;
+    }
+
+    /**
+     * Gets the AIDL implementation of RadioImsProxy.
+     * @return IRadioIms implementation.
+     */
+    public android.hardware.radio.ims.IRadioIms getAidl() {
+        return mImsProxy;
+    }
+
+    /**
+     * Resets RadioImsProxy.
+     */
+    @Override
+    public void clear() {
+        super.clear();
+        mImsProxy = null;
+    }
+
+    /**
+     * Checks whether a RadioIms implementation exists.
+     * @return true if there is neither a HIDL nor AIDL implementation.
+     */
+    @Override
+    public boolean isEmpty() {
+        return mRadioProxy == null && mImsProxy == null;
+    }
+
+    /**
+     * No implementation in IRadioIms.
+     * @throws RemoteException.
+     */
+    @Override
+    public void responseAcknowledgement() throws RemoteException {
+        /* Currently, IRadioIms doesn't support the following response types:
+         * - RadioIndicationType.UNSOLICITED_ACK_EXP
+         * - RadioResponseType.SOLICITED_ACK_EXP */
+        // no-op
+    }
+
+    /**
+     * Calls IRadioIms#setSrvccCallInfo.
+     * @param serial Serial number of request.
+     * @param srvccCalls The list of call information.
+     * @throws RemoteException.
+     */
+    public void setSrvccCallInfo(int serial,
+            android.hardware.radio.ims.SrvccCall[] srvccCalls) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mImsProxy.setSrvccCallInfo(serial, srvccCalls);
+        }
+    }
+
+    /**
+     * Calls IRadioIms#updateImsRegistrationInfo.
+     * @param serial Serial number of request.
+     * @param registrationInfo The registration state information.
+     * @throws RemoteException.
+     */
+    public void updateImsRegistrationInfo(int serial,
+            android.hardware.radio.ims.ImsRegistration registrationInfo) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mImsProxy.updateImsRegistrationInfo(serial, registrationInfo);
+        }
+    }
+
+    /**
+     * Calls IRadioIms#startImsTraffic.
+     * @param serial Serial number of request.
+     * @param token A nonce to identify the request.
+     * @param trafficType IMS traffic type like registration, voice, video, SMS, emergency, and etc.
+     * @param accessNetworkType The type of underlying radio access network used.
+     * @param trafficDirection Indicates whether traffic is originated by mobile originated or
+     *        mobile terminated use case eg. MO/MT call/SMS etc.
+     * @throws RemoteException.
+     */
+    public void startImsTraffic(int serial, int token, int trafficType, int accessNetworkType,
+            int trafficDirection) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mImsProxy.startImsTraffic(serial,
+                    token, trafficType, accessNetworkType, trafficDirection);
+        }
+    }
+
+    /**
+     * Calls IRadioIms#stopImsTraffic.
+     * @param serial Serial number of request.
+     * @param token The token assigned by startImsTraffic.
+     * @throws RemoteException.
+     */
+    public void stopImsTraffic(int serial, int token)
+            throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mImsProxy.stopImsTraffic(serial, token);
+        }
+    }
+
+    /**
+     * Calls IRadioIms#triggerEpsFallback.
+     * @param serial Serial number of request.
+     * @param reason Specifies the reason for EPS fallback.
+     * @throws RemoteException.
+     */
+    public void triggerEpsFallback(int serial, int reason)
+            throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mImsProxy.triggerEpsFallback(serial, reason);
+        }
+    }
+
+    /**
+     * Calls IRadioIms#sendAnbrQuery.
+     * @param serial Serial number of request.
+     * @param mediaType Media type is used to identify media stream such as audio or video.
+     * @param direction Direction of this packet stream (e.g. uplink or downlink).
+     * @param bitsPerSecond The bit rate requested by the opponent UE.
+     * @throws RemoteException.
+     */
+    public void sendAnbrQuery(int serial, int mediaType, int direction, int bitsPerSecond)
+            throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mImsProxy.sendAnbrQuery(serial, mediaType, direction, bitsPerSecond);
+        }
+    }
+
+    /**
+     * Call IRadioIms#updateImsCallStatus
+     * @param serial Serial number of request.
+     * @param imsCalls The list of call status information.
+     * @throws RemoteException.
+     */
+    public void updateImsCallStatus(int serial,
+            android.hardware.radio.ims.ImsCall[] imsCalls) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mImsProxy.updateImsCallStatus(serial, imsCalls);
+        }
+    }
+}
diff --git a/src/java/com/android/internal/telephony/RadioIndication.java b/src/java/com/android/internal/telephony/RadioIndication.java
index d947d53..800511b 100644
--- a/src/java/com/android/internal/telephony/RadioIndication.java
+++ b/src/java/com/android/internal/telephony/RadioIndication.java
@@ -16,6 +16,7 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_RADIO;
 import static android.telephony.TelephonyManager.UNKNOWN_CARRIER_ID;
 
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CALL_RING;
@@ -136,7 +137,7 @@
      * @param radioState android.hardware.radio.V1_0.RadioState
      */
     public void radioStateChanged(int indicationType, int radioState) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         int state = RILUtils.convertHalRadioState(radioState);
         if (mRil.isLogOrTrace()) {
@@ -148,7 +149,7 @@
     }
 
     public void callStateChanged(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED);
 
@@ -160,7 +161,7 @@
      * @param indicationType RadioIndicationType
      */
     public void networkStateChanged(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED);
 
@@ -168,7 +169,7 @@
     }
 
     public void newSms(int indicationType, ArrayList<Byte> pdu) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         byte[] pduArray = RILUtils.arrayListToPrimitiveArray(pdu);
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_NEW_SMS);
@@ -181,7 +182,7 @@
     }
 
     public void newSmsStatusReport(int indicationType, ArrayList<Byte> pdu) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         byte[] pduArray = RILUtils.arrayListToPrimitiveArray(pdu);
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT);
@@ -192,7 +193,7 @@
     }
 
     public void newSmsOnSim(int indicationType, int recordNumber) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM);
 
@@ -202,7 +203,7 @@
     }
 
     public void onUssd(int indicationType, int ussdModeType, String msg) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogMore(RIL_UNSOL_ON_USSD, "" + ussdModeType);
 
@@ -216,7 +217,7 @@
     }
 
     public void nitzTimeReceived(int indicationType, String nitzTime, long receivedTime) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_NITZ_TIME_RECEIVED, nitzTime);
 
@@ -241,7 +242,7 @@
 
     public void currentSignalStrength(int indicationType,
                                       android.hardware.radio.V1_0.SignalStrength signalStrength) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         SignalStrength ssInitial = RILUtils.convertHalSignalStrength(signalStrength);
 
@@ -259,7 +260,7 @@
      */
     public void currentLinkCapacityEstimate(int indicationType,
                                             android.hardware.radio.V1_2.LinkCapacityEstimate lce) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         List<LinkCapacityEstimate> response = RILUtils.convertHalLceData(lce);
 
@@ -275,7 +276,7 @@
      */
     public void currentLinkCapacityEstimate_1_6(int indicationType,
             android.hardware.radio.V1_6.LinkCapacityEstimate lce) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         List<LinkCapacityEstimate> response = RILUtils.convertHalLceData(lce);
 
@@ -291,7 +292,7 @@
      */
     public void currentSignalStrength_1_2(int indicationType,
                                       android.hardware.radio.V1_2.SignalStrength signalStrength) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         SignalStrength ss = RILUtils.convertHalSignalStrength(signalStrength);
         // Note this is set to "verbose" because it happens frequently
@@ -308,7 +309,7 @@
     public void currentSignalStrength_1_4(int indicationType,
             android.hardware.radio.V1_4.SignalStrength signalStrength) {
 
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         SignalStrength ss = RILUtils.convertHalSignalStrength(signalStrength);
 
@@ -325,7 +326,7 @@
     public void currentSignalStrength_1_6(int indicationType,
             android.hardware.radio.V1_6.SignalStrength signalStrength) {
 
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         SignalStrength ss = RILUtils.convertHalSignalStrength(signalStrength);
 
@@ -341,7 +342,7 @@
      */
     public void currentPhysicalChannelConfigs_1_4(int indicationType,
             ArrayList<android.hardware.radio.V1_4.PhysicalChannelConfig> configs) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
         physicalChannelConfigsIndication(configs);
     }
 
@@ -350,7 +351,7 @@
      */
     public void currentPhysicalChannelConfigs_1_6(int indicationType,
             ArrayList<android.hardware.radio.V1_6.PhysicalChannelConfig> configs) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
         physicalChannelConfigsIndication(configs);
     }
 
@@ -359,7 +360,7 @@
      */
     public void currentPhysicalChannelConfigs(int indicationType,
             ArrayList<android.hardware.radio.V1_2.PhysicalChannelConfig> configs) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
         physicalChannelConfigsIndication(configs);
     }
 
@@ -368,7 +369,7 @@
      */
     public void currentEmergencyNumberList(int indicationType,
             ArrayList<android.hardware.radio.V1_4.EmergencyNumber> emergencyNumberList) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
         List<EmergencyNumber> response = new ArrayList<>(emergencyNumberList.size());
 
         for (android.hardware.radio.V1_4.EmergencyNumber emergencyNumberHal
@@ -422,7 +423,7 @@
     }
 
     public void suppSvcNotify(int indicationType, SuppSvcNotification suppSvcNotification) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         SuppServiceNotification notification = new SuppServiceNotification();
         notification.notificationType = suppSvcNotification.isMT ? 1 : 0;
@@ -441,7 +442,7 @@
     }
 
     public void stkSessionEnd(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_STK_SESSION_END);
 
@@ -451,7 +452,7 @@
     }
 
     public void stkProactiveCommand(int indicationType, String cmd) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_STK_PROACTIVE_COMMAND);
 
@@ -461,7 +462,7 @@
     }
 
     public void stkEventNotify(int indicationType, String cmd) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_STK_EVENT_NOTIFY);
 
@@ -471,7 +472,7 @@
     }
 
     public void stkCallSetup(int indicationType, long timeout) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_STK_CALL_SETUP, timeout);
 
@@ -481,7 +482,7 @@
     }
 
     public void simSmsStorageFull(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_SIM_SMS_STORAGE_FULL);
 
@@ -491,7 +492,7 @@
     }
 
     public void simRefresh(int indicationType, SimRefreshResult refreshResult) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         IccRefreshResponse response = new IccRefreshResponse();
         response.refreshResult = refreshResult.type;
@@ -504,7 +505,7 @@
     }
 
     public void callRing(int indicationType, boolean isGsm, CdmaSignalInfoRecord record) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         char response[] = null;
 
@@ -527,7 +528,7 @@
     }
 
     public void simStatusChanged(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED);
 
@@ -535,7 +536,7 @@
     }
 
     public void cdmaNewSms(int indicationType, CdmaSmsMessage msg) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_CDMA_NEW_SMS);
 
@@ -546,7 +547,7 @@
     }
 
     public void newBroadcastSms(int indicationType, ArrayList<Byte> data) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         byte[] response = RILUtils.arrayListToPrimitiveArray(data);
         if (mRil.isLogOrTrace()) {
@@ -560,7 +561,7 @@
     }
 
     public void cdmaRuimSmsStorageFull(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL);
 
@@ -570,7 +571,7 @@
     }
 
     public void restrictedStateChanged(int indicationType, int state) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogvRet(RIL_UNSOL_RESTRICTED_STATE_CHANGED, state);
 
@@ -580,7 +581,7 @@
     }
 
     public void enterEmergencyCallbackMode(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE);
 
@@ -590,7 +591,7 @@
     }
 
     public void cdmaCallWaiting(int indicationType, CdmaCallWaiting callWaitingRecord) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         // todo: create a CdmaCallWaitingNotification constructor that takes in these fields to make
         // sure no fields are missing
@@ -614,7 +615,7 @@
     }
 
     public void cdmaOtaProvisionStatus(int indicationType, int status) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         int response[] = new int[1];
         response[0] = status;
@@ -628,7 +629,7 @@
 
     public void cdmaInfoRec(int indicationType,
                             android.hardware.radio.V1_0.CdmaInformationRecords records) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         int numberOfInfoRecs = records.infoRec.size();
         for (int i = 0; i < numberOfInfoRecs; i++) {
@@ -724,7 +725,7 @@
     }
 
     public void indicateRingbackTone(int indicationType, boolean start) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogvRet(RIL_UNSOL_RINGBACK_TONE, start);
 
@@ -732,7 +733,7 @@
     }
 
     public void resendIncallMute(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESEND_INCALL_MUTE);
 
@@ -740,7 +741,7 @@
     }
 
     public void cdmaSubscriptionSourceChanged(int indicationType, int cdmaSource) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         int response[] = new int[1];
         response[0] = cdmaSource;
@@ -754,7 +755,7 @@
     }
 
     public void cdmaPrlChanged(int indicationType, int version) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         int response[] = new int[1];
         response[0] = version;
@@ -766,7 +767,7 @@
     }
 
     public void exitEmergencyCallbackMode(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE);
 
@@ -774,7 +775,7 @@
     }
 
     public void rilConnected(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RIL_CONNECTED);
 
@@ -787,7 +788,7 @@
     }
 
     public void voiceRadioTechChanged(int indicationType, int rat) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         int response[] = new int[1];
         response[0] = rat;
@@ -803,35 +804,35 @@
     /** Get unsolicited message for cellInfoList */
     public void cellInfoList(int indicationType,
             ArrayList<android.hardware.radio.V1_0.CellInfo> records) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
         responseCellInfoList(records);
     }
 
     /** Get unsolicited message for cellInfoList using HAL V1_2 */
     public void cellInfoList_1_2(int indicationType,
             ArrayList<android.hardware.radio.V1_2.CellInfo> records) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
         responseCellInfoList(records);
     }
 
     /** Get unsolicited message for cellInfoList using HAL V1_4 */
     public void cellInfoList_1_4(int indicationType,
             ArrayList<android.hardware.radio.V1_4.CellInfo> records) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
         responseCellInfoList(records);
     }
 
     /** Get unsolicited message for cellInfoList using HAL V1_5 */
     public void cellInfoList_1_5(int indicationType,
             ArrayList<android.hardware.radio.V1_5.CellInfo> records) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
         responseCellInfoList(records);
     }
 
     /** Get unsolicited message for cellInfoList using HAL V1_5 */
     public void cellInfoList_1_6(int indicationType,
             ArrayList<android.hardware.radio.V1_6.CellInfo> records) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
         responseCellInfoList(records);
     }
 
@@ -843,7 +844,7 @@
 
     /** Get unsolicited message for uicc applications enablement changes. */
     public void uiccApplicationsEnablementChanged(int indicationType, boolean enabled) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) {
             mRil.unsljLogRet(RIL_UNSOL_UICC_APPLICATIONS_ENABLEMENT_CHANGED, enabled);
@@ -883,7 +884,7 @@
     }
 
     public void imsNetworkStateChanged(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED);
 
@@ -891,7 +892,7 @@
     }
 
     public void subscriptionStatusChanged(int indicationType, boolean activate) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         int response[] = new int[1];
         response[0] = activate ? 1 : 0;
@@ -905,7 +906,7 @@
     }
 
     public void srvccStateNotify(int indicationType, int state) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         int response[] = new int[1];
         response[0] = state;
@@ -921,7 +922,7 @@
     public void hardwareConfigChanged(
             int indicationType,
             ArrayList<android.hardware.radio.V1_0.HardwareConfig> configs) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         ArrayList<HardwareConfig> response = RILUtils.convertHalHardwareConfigList(configs);
 
@@ -933,7 +934,7 @@
 
     public void radioCapabilityIndication(int indicationType,
                                           android.hardware.radio.V1_0.RadioCapability rc) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         RadioCapability response = RILUtils.convertHalRadioCapability(rc, mRil);
 
@@ -944,7 +945,7 @@
     }
 
     public void onSupplementaryServiceIndication(int indicationType, StkCcUnsolSsResult ss) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         int num;
         SsData ssData = new SsData();
@@ -992,7 +993,7 @@
     }
 
     public void stkCallControlAlphaNotify(int indicationType, String alpha) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_STK_CC_ALPHA_NOTIFY, alpha);
 
@@ -1002,7 +1003,7 @@
     }
 
     public void lceData(int indicationType, LceDataInfo lce) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         List<LinkCapacityEstimate> response = RILUtils.convertHalLceData(lce);
 
@@ -1014,7 +1015,7 @@
     }
 
     public void pcoData(int indicationType, PcoDataInfo pco) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         PcoData response = new PcoData(pco.cid, pco.bearerProto, pco.pcoId,
                 RILUtils.arrayListToPrimitiveArray(pco.contents));
@@ -1025,7 +1026,7 @@
     }
 
     public void modemReset(int indicationType, String reason) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_MODEM_RESTART, reason);
 
@@ -1038,7 +1039,7 @@
      * @param indicationType RadioIndicationType
      */
     public void carrierInfoForImsiEncryption(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) {
             mRil.unsljLogRet(RIL_UNSOL_CARRIER_INFO_IMSI_ENCRYPTION, null);
@@ -1055,7 +1056,7 @@
      */
     public void keepaliveStatus(
             int indicationType, android.hardware.radio.V1_1.KeepaliveStatus halStatus) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) {
             mRil.unsljLogRet(
@@ -1074,7 +1075,7 @@
      * @param indicationType RadioIndicationType
      */
     public void simPhonebookChanged(int indicationType) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) {
             mRil.unsljLog(RIL_UNSOL_RESPONSE_SIM_PHONEBOOK_CHANGED);
@@ -1091,7 +1092,7 @@
      */
     public void simPhonebookRecordsReceived(int indicationType, byte status,
             ArrayList<PhonebookRecordInfo> records) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         List<SimPhonebookRecord> simPhonebookRecords = new ArrayList<>();
 
@@ -1124,7 +1125,7 @@
             android.hardware.radio.V1_5.CellIdentity cellIdentity, String chosenPlmn,
             @NetworkRegistrationInfo.Domain int domain,
             int causeCode, int additionalCauseCode) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
         CellIdentity ci = RILUtils.convertHalCellIdentity(cellIdentity);
         if (ci == null
                 || TextUtils.isEmpty(chosenPlmn)
@@ -1155,7 +1156,7 @@
     public void barringInfoChanged(int indicationType,
             android.hardware.radio.V1_5.CellIdentity cellIdentity,
             ArrayList<android.hardware.radio.V1_5.BarringInfo> barringInfos) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (cellIdentity == null || barringInfos == null) {
             reportAnomaly(
@@ -1270,7 +1271,7 @@
 
     private void responseNetworkScan(int indicationType,
             android.hardware.radio.V1_1.NetworkScanResult result) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         ArrayList<CellInfo> cellInfos =
                 RILUtils.convertHalCellInfoList(new ArrayList<>(result.networkInfos));
@@ -1281,7 +1282,7 @@
 
     private void responseNetworkScan_1_2(int indicationType,
             android.hardware.radio.V1_2.NetworkScanResult result) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         ArrayList<CellInfo> cellInfos =
                 RILUtils.convertHalCellInfoList(new ArrayList<>(result.networkInfos));
@@ -1292,7 +1293,7 @@
 
     private void responseNetworkScan_1_4(int indicationType,
             android.hardware.radio.V1_4.NetworkScanResult result) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         ArrayList<CellInfo> cellInfos =
                 RILUtils.convertHalCellInfoList(new ArrayList<>(result.networkInfos));
@@ -1303,7 +1304,7 @@
 
     private void responseNetworkScan_1_5(int indicationType,
             android.hardware.radio.V1_5.NetworkScanResult result) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         ArrayList<CellInfo> cellInfos =
                 RILUtils.convertHalCellInfoList(new ArrayList<>(result.networkInfos));
@@ -1314,7 +1315,7 @@
 
     private void responseNetworkScan_1_6(int indicationType,
             android.hardware.radio.V1_6.NetworkScanResult result) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         ArrayList<CellInfo> cellInfos =
                 RILUtils.convertHalCellInfoList(new ArrayList<>(result.networkInfos));
@@ -1324,7 +1325,7 @@
     }
 
     private void responseDataCallListChanged(int indicationType, List<?> dcList) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_DATA_CALL_LIST_CHANGED, dcList);
 
@@ -1334,7 +1335,7 @@
     }
 
     private void responseApnUnthrottled(int indicationType, String apn) {
-        mRil.processIndication(RIL.RADIO_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_RADIO, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_UNTHROTTLE_APN, apn);
 
diff --git a/src/java/com/android/internal/telephony/RadioInterfaceCapabilityController.java b/src/java/com/android/internal/telephony/RadioInterfaceCapabilityController.java
index 04c6b54..bab4d12 100644
--- a/src/java/com/android/internal/telephony/RadioInterfaceCapabilityController.java
+++ b/src/java/com/android/internal/telephony/RadioInterfaceCapabilityController.java
@@ -28,6 +28,8 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.telephony.Rlog;
 
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
 import java.util.Collections;
 import java.util.Set;
 
@@ -164,6 +166,13 @@
         }
     }
 
+    /**
+     * Dump the fields of the instance
+     */
+    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        pw.println("mRadioConfig=" + mRadioConfig);
+    }
+
     private static void log(final String s) {
         Rlog.d(LOG_TAG, s);
     }
diff --git a/src/java/com/android/internal/telephony/RadioMessagingProxy.java b/src/java/com/android/internal/telephony/RadioMessagingProxy.java
index e68e957..4e9bc8c 100644
--- a/src/java/com/android/internal/telephony/RadioMessagingProxy.java
+++ b/src/java/com/android/internal/telephony/RadioMessagingProxy.java
@@ -36,13 +36,34 @@
      * Set IRadioMessaging as the AIDL implementation for RadioServiceProxy
      * @param halVersion Radio HAL version
      * @param messaging IRadioMessaging implementation
+     *
+     * @return updated HAL version
      */
-    public void setAidl(HalVersion halVersion,
+    public HalVersion setAidl(HalVersion halVersion,
             android.hardware.radio.messaging.IRadioMessaging messaging) {
         mHalVersion = halVersion;
         mMessagingProxy = messaging;
         mIsAidl = true;
-        Rlog.d(TAG, "AIDL initialized");
+
+        try {
+            HalVersion newHalVersion;
+            int version = messaging.getInterfaceVersion();
+            switch(version) {
+                default:
+                    newHalVersion = RIL.RADIO_HAL_VERSION_2_0;
+                    break;
+            }
+            Rlog.d(TAG, "AIDL version=" + version + ", halVersion=" + newHalVersion);
+
+            if (mHalVersion.less(newHalVersion)) {
+                mHalVersion = newHalVersion;
+            }
+        } catch (RemoteException e) {
+            Rlog.e(TAG, "setAidl: " + e);
+        }
+
+        Rlog.d(TAG, "AIDL initialized mHalVersion=" + mHalVersion);
+        return mHalVersion;
     }
 
     /**
diff --git a/src/java/com/android/internal/telephony/RadioModemProxy.java b/src/java/com/android/internal/telephony/RadioModemProxy.java
index 7aaf727..2628751 100644
--- a/src/java/com/android/internal/telephony/RadioModemProxy.java
+++ b/src/java/com/android/internal/telephony/RadioModemProxy.java
@@ -31,13 +31,34 @@
      * Set IRadioModem as the AIDL implementation for RadioServiceProxy
      * @param halVersion Radio HAL version
      * @param modem IRadioModem implementation
+     *
+     * @return updated HAL version
      */
-    public void setAidl(HalVersion halVersion,
+    public HalVersion setAidl(HalVersion halVersion,
             android.hardware.radio.modem.IRadioModem modem) {
         mHalVersion = halVersion;
         mModemProxy = modem;
         mIsAidl = true;
-        Rlog.d(TAG, "AIDL initialized");
+
+        try {
+            HalVersion newHalVersion;
+            int version = modem.getInterfaceVersion();
+            switch(version) {
+                default:
+                    newHalVersion = RIL.RADIO_HAL_VERSION_2_0;
+                    break;
+            }
+            Rlog.d(TAG, "AIDL version=" + version + ", halVersion=" + newHalVersion);
+
+            if (mHalVersion.less(newHalVersion)) {
+                mHalVersion = newHalVersion;
+            }
+        } catch (RemoteException e) {
+            Rlog.e(TAG, "setAidl: " + e);
+        }
+
+        Rlog.d(TAG, "AIDL initialized mHalVersion=" + mHalVersion);
+        return mHalVersion;
     }
 
     /**
@@ -110,6 +131,19 @@
     }
 
     /**
+     * Call IRadioModem#getImei
+     *
+     * @param serial Serial number of request
+     * @throws RemoteException
+     */
+    public void getImei(int serial) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mModemProxy.getImei(serial);
+        }
+    }
+
+    /**
      * Call IRadioModem#getHardwareConfig
      * @param serial Serial number of request
      * @throws RemoteException
diff --git a/src/java/com/android/internal/telephony/RadioNetworkProxy.java b/src/java/com/android/internal/telephony/RadioNetworkProxy.java
index b881035..4717ad6 100644
--- a/src/java/com/android/internal/telephony/RadioNetworkProxy.java
+++ b/src/java/com/android/internal/telephony/RadioNetworkProxy.java
@@ -65,13 +65,37 @@
      * Set IRadioNetwork as the AIDL implementation for RadioServiceProxy
      * @param halVersion Radio HAL version
      * @param network IRadioNetwork implementation
+     *
+     * @return updated HAL version
      */
-    public void setAidl(HalVersion halVersion,
+    public HalVersion setAidl(HalVersion halVersion,
             android.hardware.radio.network.IRadioNetwork network) {
         mHalVersion = halVersion;
         mNetworkProxy = network;
         mIsAidl = true;
-        Rlog.d(TAG, "AIDL initialized");
+
+        try {
+            HalVersion newHalVersion;
+            int version = network.getInterfaceVersion();
+            switch(version) {
+                case 2:
+                    newHalVersion = RIL.RADIO_HAL_VERSION_2_1;
+                    break;
+                default:
+                    newHalVersion = RIL.RADIO_HAL_VERSION_2_0;
+                    break;
+            }
+            Rlog.d(TAG, "AIDL version=" + version + ", halVersion=" + newHalVersion);
+
+            if (mHalVersion.less(newHalVersion)) {
+                mHalVersion = newHalVersion;
+            }
+        } catch (RemoteException e) {
+            Rlog.e(TAG, "setAidl: " + e);
+        }
+
+        Rlog.d(TAG, "AIDL initialized mHalVersion=" + mHalVersion);
+        return mHalVersion;
     }
 
     /**
@@ -828,4 +852,127 @@
         }
         // Only supported on AIDL.
     }
+
+    /**
+     * Set the Emergency Mode
+     *
+     * @param serial Serial number of the request.
+     * @param emcModeType Defines the radio emergency mode type.
+     * @throws RemoteException
+     */
+    public void setEmergencyMode(int serial, int emcModeType) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mNetworkProxy.setEmergencyMode(serial, emcModeType);
+        }
+        // Only supported on AIDL.
+    }
+
+    /**
+     * Triggers an Emergency network scan.
+     *
+     * @param serial Serial number of the request.
+     * @param scanRequest Contains the preferred networks and type of service to be scanned.
+     * @throws RemoteException
+     */
+    public void triggerEmergencyNetworkScan(int serial,
+            android.hardware.radio.network.EmergencyNetworkScanTrigger scanRequest)
+            throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mNetworkProxy.triggerEmergencyNetworkScan(serial, scanRequest);
+        }
+        // Only supported on AIDL.
+    }
+
+    /**
+     * Cancels ongoing Emergency network scan
+     *
+     * @param serial Serial number of the request.
+     * @param resetScan Indicates how the next {@link #triggerEmergencyNetworkScan} should work.
+     *        If {@code true}, then the modem shall start the new scan from the beginning,
+     *        otherwise the modem shall resume from the last search.
+     *
+     * @throws RemoteException
+     */
+    public void cancelEmergencyNetworkScan(int serial, boolean resetScan) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mNetworkProxy.cancelEmergencyNetworkScan(serial, resetScan);
+        }
+        // Only supported on AIDL.
+    }
+
+    /**
+     * Exits ongoing Emergency Mode
+     *
+     * @param serial Serial number of the request.
+     * @throws RemoteException
+     */
+    public void exitEmergencyMode(int serial) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mNetworkProxy.exitEmergencyMode(serial);
+        }
+        // Only supported on AIDL.
+    }
+
+    /**
+     * Set if null ciphering / null integrity is permitted.
+     *
+     * @param serial Serial number of the request.
+     * @param enabled true if null modes are allowed, false otherwise
+     * @throws RemoteException
+     */
+    public void setNullCipherAndIntegrityEnabled(int serial,
+            boolean enabled) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mNetworkProxy.setNullCipherAndIntegrityEnabled(serial, enabled);
+        }
+        // Only supported on AIDL.
+    }
+
+    /**
+     * Get if null ciphering / null integrity is permitted.
+     * @param serial Serial number of the request.
+     * @throws RemoteException
+     *
+     */
+    public void isNullCipherAndIntegrityEnabled(int serial) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mNetworkProxy.isNullCipherAndIntegrityEnabled(serial);
+        }
+        // Only supported on AIDL.
+    }
+
+    /**
+     * Checks whether N1 mode is enabled.
+     *
+     * @param serial Serial number of the request.
+     * @throws RemoteException
+     */
+    public void isN1ModeEnabled(int serial) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mNetworkProxy.isN1ModeEnabled(serial);
+        }
+        // Only supported on AIDL.
+    }
+
+    /**
+     * Enables or disables N1 mode.
+     *
+     * @param serial Serial number of request.
+     * @param enable Indicates whether to enable N1 mode or not.
+     * @throws RemoteException
+     */
+    public void setN1ModeEnabled(int serial, boolean enable) throws RemoteException {
+        if (isEmpty()) return;
+        if (isAidl()) {
+            mNetworkProxy.setN1ModeEnabled(serial, enable);
+        }
+        // Only supported on AIDL.
+    }
 }
diff --git a/src/java/com/android/internal/telephony/RadioResponse.java b/src/java/com/android/internal/telephony/RadioResponse.java
index 5265be5..0bc2958 100644
--- a/src/java/com/android/internal/telephony/RadioResponse.java
+++ b/src/java/com/android/internal/telephony/RadioResponse.java
@@ -2532,10 +2532,9 @@
             ArrayList<NeighboringCellInfo> ret = new ArrayList<NeighboringCellInfo>();
             NeighboringCellInfo cell;
 
-            int[] subId = SubscriptionManager.getSubId(mRil.mPhoneId);
-            int radioType =
-                    ((TelephonyManager) mRil.mContext.getSystemService(
-                            Context.TELEPHONY_SERVICE)).getDataNetworkType(subId[0]);
+            int radioType = ((TelephonyManager) mRil.mContext.getSystemService(
+                    Context.TELEPHONY_SERVICE)).getDataNetworkType(
+                            SubscriptionManager.getSubscriptionId(mRil.mPhoneId));
 
             if (radioType != TelephonyManager.NETWORK_TYPE_UNKNOWN) {
                 for (int i = 0; i < cells.size(); i++) {
diff --git a/src/java/com/android/internal/telephony/RadioServiceProxy.java b/src/java/com/android/internal/telephony/RadioServiceProxy.java
index 8650dd0..4257327 100644
--- a/src/java/com/android/internal/telephony/RadioServiceProxy.java
+++ b/src/java/com/android/internal/telephony/RadioServiceProxy.java
@@ -78,4 +78,9 @@
         if (isEmpty()) return;
         if (!isAidl()) mRadioProxy.responseAcknowledgement();
     }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + "[mHalVersion=" + mHalVersion + ']';
+    }
 }
diff --git a/src/java/com/android/internal/telephony/RadioSimProxy.java b/src/java/com/android/internal/telephony/RadioSimProxy.java
index da5b660..a305de9 100644
--- a/src/java/com/android/internal/telephony/RadioSimProxy.java
+++ b/src/java/com/android/internal/telephony/RadioSimProxy.java
@@ -41,12 +41,36 @@
      * Set IRadioSim as the AIDL implementation for RadioServiceProxy
      * @param halVersion Radio HAL version
      * @param sim IRadioSim implementation
+     *
+     * @return updated HAL version
      */
-    public void setAidl(HalVersion halVersion, android.hardware.radio.sim.IRadioSim sim) {
+    public HalVersion setAidl(HalVersion halVersion, android.hardware.radio.sim.IRadioSim sim) {
         mHalVersion = halVersion;
         mSimProxy = sim;
         mIsAidl = true;
-        Rlog.d(TAG, "AIDL initialized");
+
+        try {
+            HalVersion newHalVersion;
+            int version = sim.getInterfaceVersion();
+            switch(version) {
+                case 2:
+                    newHalVersion = RIL.RADIO_HAL_VERSION_2_1;
+                    break;
+                default:
+                    newHalVersion = RIL.RADIO_HAL_VERSION_2_0;
+                    break;
+            }
+            Rlog.d(TAG, "AIDL version=" + version + ", halVersion=" + newHalVersion);
+
+            if (mHalVersion.less(newHalVersion)) {
+                mHalVersion = newHalVersion;
+            }
+        } catch (RemoteException e) {
+            Rlog.e(TAG, "setAidl: " + e);
+        }
+
+        Rlog.d(TAG, "AIDL initialized mHalVersion=" + mHalVersion);
+        return mHalVersion;
     }
 
     /**
@@ -352,7 +376,8 @@
         if (isEmpty()) return;
         if (isAidl()) {
             mSimProxy.iccTransmitApduBasicChannel(serial,
-                    RILUtils.convertToHalSimApduAidl(0, cla, instruction, p1, p2, p3, data));
+                    RILUtils.convertToHalSimApduAidl(0, cla, instruction, p1, p2, p3, data,
+                            false, mHalVersion));
         } else {
             mRadioProxy.iccTransmitApduBasicChannel(serial,
                     RILUtils.convertToHalSimApdu(0, cla, instruction, p1, p2, p3, data));
@@ -373,10 +398,29 @@
      */
     public void iccTransmitApduLogicalChannel(int serial, int channel, int cla, int instruction,
             int p1, int p2, int p3, String data) throws RemoteException {
+        iccTransmitApduLogicalChannel(serial, channel, cla, instruction, p1, p2, p3, data, false);
+    }
+
+    /**
+     * Call IRadioSim#iccTransmitApduLogicalChannel
+     * @param serial Serial number of request
+     * @param channel Channel ID of the channel to use for communication
+     * @param cla Class of the command
+     * @param instruction Instruction of the command
+     * @param p1 P1 value of the command
+     * @param p2 P2 value of the command
+     * @param p3 P3 value of the command
+     * @param data Data to be sent
+     * @param isEs10Command APDU is an isEs10 command or not
+     * @throws RemoteException
+     */
+    public void iccTransmitApduLogicalChannel(int serial, int channel, int cla, int instruction,
+            int p1, int p2, int p3, String data, boolean isEs10Command) throws RemoteException {
         if (isEmpty()) return;
         if (isAidl()) {
             mSimProxy.iccTransmitApduLogicalChannel(serial,
-                    RILUtils.convertToHalSimApduAidl(channel, cla, instruction, p1, p2, p3, data));
+                    RILUtils.convertToHalSimApduAidl(channel, cla, instruction, p1, p2, p3, data,
+                            isEs10Command, mHalVersion));
         } else {
             mRadioProxy.iccTransmitApduLogicalChannel(serial,
                     RILUtils.convertToHalSimApdu(channel, cla, instruction, p1, p2, p3, data));
diff --git a/src/java/com/android/internal/telephony/RadioVoiceProxy.java b/src/java/com/android/internal/telephony/RadioVoiceProxy.java
index 6ac603b..07afaad 100644
--- a/src/java/com/android/internal/telephony/RadioVoiceProxy.java
+++ b/src/java/com/android/internal/telephony/RadioVoiceProxy.java
@@ -35,12 +35,34 @@
      * Set IRadioVoice as the AIDL implementation for RadioServiceProxy
      * @param halVersion Radio HAL version
      * @param voice IRadioVoice implementation
+     *
+     * @return updated HAL version
      */
-    public void setAidl(HalVersion halVersion, android.hardware.radio.voice.IRadioVoice voice) {
+    public HalVersion setAidl(HalVersion halVersion,
+            android.hardware.radio.voice.IRadioVoice voice) {
         mHalVersion = halVersion;
         mVoiceProxy = voice;
         mIsAidl = true;
-        Rlog.d(TAG, "AIDL initialized");
+
+        try {
+            HalVersion newHalVersion;
+            int version = voice.getInterfaceVersion();
+            switch(version) {
+                default:
+                    newHalVersion = RIL.RADIO_HAL_VERSION_2_0;
+                    break;
+            }
+            Rlog.d(TAG, "AIDL version=" + version + ", halVersion=" + newHalVersion);
+
+            if (mHalVersion.less(newHalVersion)) {
+                mHalVersion = newHalVersion;
+            }
+        } catch (RemoteException e) {
+            Rlog.e(TAG, "setAidl: " + e);
+        }
+
+        Rlog.d(TAG, "AIDL initialized mHalVersion=" + mHalVersion);
+        return mHalVersion;
     }
 
     /**
diff --git a/src/java/com/android/internal/telephony/SMSDispatcher.java b/src/java/com/android/internal/telephony/SMSDispatcher.java
index 5bc9d17..6746d7b 100644
--- a/src/java/com/android/internal/telephony/SMSDispatcher.java
+++ b/src/java/com/android/internal/telephony/SMSDispatcher.java
@@ -27,12 +27,17 @@
 import android.app.AlertDialog;
 import android.app.PendingIntent;
 import android.app.PendingIntent.CanceledException;
+import android.app.compat.CompatChanges;
+import android.compat.annotation.ChangeId;
+import android.compat.annotation.EnabledSince;
 import android.compat.annotation.UnsupportedAppUsage;
+import android.content.BroadcastReceiver;
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
@@ -60,6 +65,7 @@
 import android.telephony.PhoneNumberUtils;
 import android.telephony.ServiceState;
 import android.telephony.SmsManager;
+import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.text.Html;
 import android.text.Spanned;
@@ -80,6 +86,10 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.GsmAlphabet.TextEncodingDetails;
 import com.android.internal.telephony.cdma.sms.UserData;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
+import com.android.internal.telephony.uicc.IccRecords;
+import com.android.internal.telephony.util.TelephonyUtils;
 import com.android.telephony.Rlog;
 
 import java.io.FileDescriptor;
@@ -144,6 +154,23 @@
     protected static final int EVENT_ICC_CHANGED = 15;
     protected static final int EVENT_GET_IMS_SERVICE = 16;
 
+    /** Last TP - Message Reference value update to SIM */
+    private static final int EVENT_TPMR_SIM_UPDATE_RESPONSE = 17;
+
+    /** Handle SIM loaded  */
+    private static final int EVENT_SIM_LOADED = 18;
+
+    /**
+     * When this change is enabled, more specific values of SMS sending error code
+     * {@link SmsManager#Result} will be returned to the SMS Apps.
+     *
+     * Refer to {@link SMSDispatcher#rilErrorToSmsManagerResult} fore more details of the new values
+     * of SMS sending error code that will be returned.
+     */
+    @ChangeId
+    @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
+    static final long ADD_MORE_SMS_SENDING_ERROR_CODES = 250017070L;
+
     @UnsupportedAppUsage
     protected Phone mPhone;
     @UnsupportedAppUsage
@@ -155,6 +182,7 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     protected final TelephonyManager mTelephonyManager;
     protected final LocalLog mLocalLog = new LocalLog(16);
+    protected final LocalLog mSmsOutgoingErrorCodes = new LocalLog(10);
 
     /** Maximum number of times to retry sending a failed SMS. */
     protected static final int MAX_SEND_RETRIES = 3;
@@ -190,6 +218,9 @@
     @VisibleForTesting
     public int mCarrierMessagingTimeout = 10 * 60 * 1000; //10 minutes
 
+    /** Used for storing last TP - Message Reference used*/
+    private int mMessageRef = -1;
+
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     protected static int getNextConcatenatedRef() {
         sConcatenatedRef += 1;
@@ -215,10 +246,33 @@
                 com.android.internal.R.bool.config_sms_capable);
         mSmsSendDisabled = !mTelephonyManager.getSmsSendCapableForPhone(
                 mPhone.getPhoneId(), mSmsCapable);
+        IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(Intent.ACTION_SIM_STATE_CHANGED);
+        mContext.registerReceiver(mBroadcastReceiver, intentFilter);
         Rlog.d(TAG, "SMSDispatcher: ctor mSmsCapable=" + mSmsCapable + " format=" + getFormat()
                 + " mSmsSendDisabled=" + mSmsSendDisabled);
     }
 
+    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(final Context context, Intent intent) {
+            Rlog.d(TAG, "Received broadcast " + intent.getAction());
+            if (Intent.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
+                if (!intent.hasExtra(Intent.EXTRA_SIM_STATE)) {
+                    Rlog.d(TAG, "Extra not found in intent.");
+                } else {
+                    String simState = intent.getStringExtra(Intent.EXTRA_SIM_STATE);
+                    if (simState.equals(Intent.SIM_STATE_LOADED)) {
+                        Rlog.d(TAG, "SIM_STATE_CHANGED : SIM_LOADED");
+                        Message msg = obtainMessage(EVENT_SIM_LOADED);
+                        msg.arg1 = getSubId();
+                        sendMessage(msg);
+                    }
+                }
+            }
+        }
+    };
+
     /**
      * Observe the secure setting for updated premium sms determination rules
      */
@@ -262,6 +316,26 @@
     protected abstract String getFormat();
 
     /**
+     * Gets the maximum number of times the SMS can be retried upon Failure,
+     * from the {@link android.telephony.CarrierConfigManager}
+     *
+     * @return the default maximum number of times SMS can be sent
+     */
+    protected int getMaxSmsRetryCount() {
+        return MAX_SEND_RETRIES;
+    }
+
+    /**
+     * Gets the Time delay before next send attempt on a failed SMS,
+     * from the {@link android.telephony.CarrierConfigManager}
+     *
+     * @return the Time in miiliseconds for delay before next send attempt on a failed SMS
+     */
+    protected int getSmsRetryDelayValue() {
+        return SEND_RETRY_DELAY;
+    }
+
+    /**
      * Called when a status report is received. This should correspond to a previously successful
      * SEND.
      *
@@ -280,85 +354,166 @@
     @Override
     public void handleMessage(Message msg) {
         switch (msg.what) {
-        case EVENT_SEND_SMS_COMPLETE:
-            // An outbound SMS has been successfully transferred, or failed.
-            handleSendComplete((AsyncResult) msg.obj);
-            break;
+            case EVENT_SEND_SMS_COMPLETE:
+                // An outbound SMS has been successfully transferred, or failed.
+                handleSendComplete((AsyncResult) msg.obj);
+                break;
 
-        case EVENT_SEND_RETRY:
-            Rlog.d(TAG, "SMS retry..");
-            sendRetrySms((SmsTracker) msg.obj);
-            break;
+            case EVENT_SEND_RETRY:
+                Rlog.d(TAG, "SMS retry..");
+                sendRetrySms((SmsTracker) msg.obj);
+                break;
 
-        case EVENT_SEND_LIMIT_REACHED_CONFIRMATION:
-            handleReachSentLimit((SmsTracker[]) (msg.obj));
-            break;
+            case EVENT_SEND_LIMIT_REACHED_CONFIRMATION:
+                handleReachSentLimit((SmsTracker[]) (msg.obj));
+                break;
 
-        case EVENT_CONFIRM_SEND_TO_POSSIBLE_PREMIUM_SHORT_CODE:
-            handleConfirmShortCode(false, (SmsTracker[]) (msg.obj));
-            break;
+            case EVENT_CONFIRM_SEND_TO_POSSIBLE_PREMIUM_SHORT_CODE:
+                handleConfirmShortCode(false, (SmsTracker[]) (msg.obj));
+                break;
 
-        case EVENT_CONFIRM_SEND_TO_PREMIUM_SHORT_CODE:
-            handleConfirmShortCode(true, (SmsTracker[]) (msg.obj));
-            break;
+            case EVENT_CONFIRM_SEND_TO_PREMIUM_SHORT_CODE:
+                handleConfirmShortCode(true, (SmsTracker[]) (msg.obj));
+                break;
 
-        case EVENT_SEND_CONFIRMED_SMS:
-        {
-            SmsTracker[] trackers = (SmsTracker[]) msg.obj;
-            for (SmsTracker tracker : trackers) {
-                sendSms(tracker);
-            }
-            mPendingTrackerCount--;
-            break;
-        }
-
-        case EVENT_SENDING_NOT_ALLOWED:
-        {
-            SmsTracker[] trackers = (SmsTracker[]) msg.obj;
-            Rlog.d(TAG, "SMSDispatcher: EVENT_SENDING_NOT_ALLOWED - "
-                    + "sending SHORT_CODE_NEVER_ALLOWED error code.");
-            handleSmsTrackersFailure(
-                    trackers, SmsManager.RESULT_ERROR_SHORT_CODE_NEVER_ALLOWED, NO_ERROR_CODE);
-            break;
-        }
-
-        case EVENT_STOP_SENDING:
-        {
-            SmsTracker[] trackers = (SmsTracker[]) msg.obj;
-            int error;
-            if (msg.arg1 == ConfirmDialogListener.SHORT_CODE_MSG) {
-                if (msg.arg2 == ConfirmDialogListener.NEVER_ALLOW) {
-                    error = SmsManager.RESULT_ERROR_SHORT_CODE_NEVER_ALLOWED;
-                    Rlog.d(TAG, "SMSDispatcher: EVENT_STOP_SENDING - "
-                            + "sending SHORT_CODE_NEVER_ALLOWED error code.");
-                } else {
-                    error = SmsManager.RESULT_ERROR_SHORT_CODE_NOT_ALLOWED;
-                    Rlog.d(TAG, "SMSDispatcher: EVENT_STOP_SENDING - "
-                            + "sending SHORT_CODE_NOT_ALLOWED error code.");
+            case EVENT_SEND_CONFIRMED_SMS: {
+                SmsTracker[] trackers = (SmsTracker[]) msg.obj;
+                for (SmsTracker tracker : trackers) {
+                    sendSms(tracker);
                 }
-            } else if (msg.arg1 == ConfirmDialogListener.RATE_LIMIT) {
-                error = SmsManager.RESULT_ERROR_LIMIT_EXCEEDED;
-                Rlog.d(TAG, "SMSDispatcher: EVENT_STOP_SENDING - "
-                        + "sending LIMIT_EXCEEDED error code.");
-            } else {
-                error = SmsManager.RESULT_UNEXPECTED_EVENT_STOP_SENDING;
-                Rlog.e(TAG, "SMSDispatcher: EVENT_STOP_SENDING - unexpected cases.");
+                mPendingTrackerCount--;
+                break;
             }
 
-            handleSmsTrackersFailure(trackers, error, NO_ERROR_CODE);
-            mPendingTrackerCount--;
-            break;
-        }
+            case EVENT_SENDING_NOT_ALLOWED: {
+                SmsTracker[] trackers = (SmsTracker[]) msg.obj;
+                Rlog.d(TAG, "SMSDispatcher: EVENT_SENDING_NOT_ALLOWED - "
+                        + "sending SHORT_CODE_NEVER_ALLOWED error code.");
+                handleSmsTrackersFailure(
+                        trackers, SmsManager.RESULT_ERROR_SHORT_CODE_NEVER_ALLOWED, NO_ERROR_CODE);
+                break;
+            }
 
-        case EVENT_NEW_SMS_STATUS_REPORT:
-            handleStatusReport(msg.obj);
-            break;
+            case EVENT_STOP_SENDING: {
+                SmsTracker[] trackers = (SmsTracker[]) msg.obj;
+                int error;
+                if (msg.arg1 == ConfirmDialogListener.SHORT_CODE_MSG) {
+                    if (msg.arg2 == ConfirmDialogListener.NEVER_ALLOW) {
+                        error = SmsManager.RESULT_ERROR_SHORT_CODE_NEVER_ALLOWED;
+                        Rlog.d(TAG, "SMSDispatcher: EVENT_STOP_SENDING - "
+                                + "sending SHORT_CODE_NEVER_ALLOWED error code.");
+                    } else {
+                        error = SmsManager.RESULT_ERROR_SHORT_CODE_NOT_ALLOWED;
+                        Rlog.d(TAG, "SMSDispatcher: EVENT_STOP_SENDING - "
+                                + "sending SHORT_CODE_NOT_ALLOWED error code.");
+                    }
+                } else if (msg.arg1 == ConfirmDialogListener.RATE_LIMIT) {
+                    error = SmsManager.RESULT_ERROR_LIMIT_EXCEEDED;
+                    Rlog.d(TAG, "SMSDispatcher: EVENT_STOP_SENDING - "
+                            + "sending LIMIT_EXCEEDED error code.");
+                } else {
+                    error = SmsManager.RESULT_UNEXPECTED_EVENT_STOP_SENDING;
+                    Rlog.e(TAG, "SMSDispatcher: EVENT_STOP_SENDING - unexpected cases.");
+                }
 
-        default:
-            Rlog.e(TAG, "handleMessage() ignoring message of unexpected type " + msg.what);
+                handleSmsTrackersFailure(trackers, error, NO_ERROR_CODE);
+                mPendingTrackerCount--;
+                break;
+            }
+
+            case EVENT_NEW_SMS_STATUS_REPORT:
+                handleStatusReport(msg.obj);
+                break;
+            case EVENT_TPMR_SIM_UPDATE_RESPONSE:
+                handleMessageRefStatus(msg);
+                break;
+
+            case EVENT_SIM_LOADED:
+                /* sim TPMR value is given higher priority if both are non-negative number.
+                   Use case:
+                   if sim was used on another device and inserted in a new device,
+                   that device will start sending the next TPMR after reading from the SIM.
+                 */
+                mMessageRef = getTpmrValueFromSIM();
+                if (mMessageRef == -1) {
+                    if (mPhone.isSubscriptionManagerServiceEnabled()) {
+                        SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
+                                .getSubscriptionInfoInternal(msg.arg1);
+                        if (subInfo != null) {
+                            mMessageRef = subInfo.getLastUsedTPMessageReference();
+                        }
+                    } else {
+                        mMessageRef = SubscriptionController.getInstance().getMessageRef(msg.arg1);
+                    }
+                }
+                break;
+
+            default:
+                Rlog.e(TAG, "handleMessage() ignoring message of unexpected type " + msg.what);
         }
     }
 
+    private void handleMessageRefStatus(Message msg) {
+        AsyncResult ar = (AsyncResult) msg.obj;
+        if (ar.exception != null) {
+            Rlog.e(TAG, "Failed to update TP - Message reference value to SIM " + ar.exception);
+        } else {
+            Rlog.d(TAG, "TP - Message reference updated to SIM Successfully");
+        }
+    }
+
+    private void updateTPMessageReference() {
+        updateSIMLastTPMRValue(mMessageRef);
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                SubscriptionManagerService.getInstance()
+                        .setLastUsedTPMessageReference(getSubId(), mMessageRef);
+            } else {
+                SubscriptionController.getInstance().updateMessageRef(getSubId(), mMessageRef);
+            }
+        } catch (SecurityException e) {
+            Rlog.e(TAG, "Security Exception caused on messageRef updation to DB " + e.getMessage());
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    private void updateSIMLastTPMRValue(int messageRef) {
+        Message msg = obtainMessage(EVENT_TPMR_SIM_UPDATE_RESPONSE);
+        IccRecords iccRecords = getIccRecords();
+        if (iccRecords != null) {
+            iccRecords.setSmssTpmrValue(messageRef, msg);
+        }
+    }
+
+    private int getTpmrValueFromSIM() {
+        IccRecords iccRecords = getIccRecords();
+        if (iccRecords != null) {
+            return iccRecords.getSmssTpmrValue();
+        }
+        return -1;
+    }
+
+    private IccRecords getIccRecords() {
+        if (mPhone != null && mPhone.getIccRecords() != null) {
+            return mPhone.getIccRecords();
+        }
+        return null;
+    }
+
+    /**
+     *  Returns the next TP message Reference value incremented by 1 for every sms sent .
+     *  once a max of 255 is reached TP message Reference is reset to 0.
+     *
+     *  @return messageRef TP message Reference value
+     */
+    public int nextMessageRef() {
+        mMessageRef = (mMessageRef + 1) % 256;
+        updateTPMessageReference();
+        return mMessageRef;
+    }
+
     /**
      * Use the carrier messaging service to send a data or text SMS.
      */
@@ -569,26 +724,50 @@
         @Override
         public void onSendSmsComplete(int result, int messageRef) {
             Rlog.d(TAG, "onSendSmsComplete: result=" + result + " messageRef=" + messageRef);
-            if (mCallbackCalled) {
-                logWithLocalLog("onSendSmsComplete: unexpected call");
-                AnomalyReporter.reportAnomaly(sAnomalyUnexpectedCallback,
-                        "Unexpected onSendSmsComplete", mPhone.getCarrierId());
+            if (cleanupOnSendSmsComplete("onSendSmsComplete")) {
                 return;
             }
-            mCallbackCalled = true;
+
             final long identity = Binder.clearCallingIdentity();
             try {
-                mSmsSender.mCarrierMessagingServiceWrapper.disconnect();
                 processSendSmsResponse(mSmsSender.getSmsTracker(), result, messageRef);
-                mSmsSender.removeTimeout();
             } finally {
                 Binder.restoreCallingIdentity(identity);
             }
         }
 
+        /**
+         * This method should be called only once.
+         */
         @Override
         public void onSendMultipartSmsComplete(int result, int[] messageRefs) {
-            Rlog.e(TAG, "Unexpected onSendMultipartSmsComplete call with result: " + result);
+            Rlog.d(TAG, "onSendMultipartSmsComplete: result=" + result + " messageRefs="
+                    + Arrays.toString(messageRefs));
+            if (cleanupOnSendSmsComplete("onSendMultipartSmsComplete")) {
+                return;
+            }
+
+            final long identity = Binder.clearCallingIdentity();
+            try {
+                processSendMultipartSmsResponse(mSmsSender.getSmsTrackers(), result, messageRefs);
+            } finally {
+                Binder.restoreCallingIdentity(identity);
+            }
+        }
+
+        private boolean cleanupOnSendSmsComplete(String callingFunction) {
+            if (mCallbackCalled) {
+                logWithLocalLog(callingFunction + ": unexpected call");
+                AnomalyReporter.reportAnomaly(sAnomalyUnexpectedCallback,
+                        "Unexpected " + callingFunction, mPhone.getCarrierId());
+                return true;
+            }
+
+            mCallbackCalled = true;
+            mSmsSender.removeTimeout();
+            mSmsSender.mCarrierMessagingServiceWrapper.disconnect();
+
+            return false;
         }
 
         @Override
@@ -663,8 +842,7 @@
         }
 
         @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
-        void sendSmsByCarrierApp(String carrierPackageName,
-                                 MultipartSmsSenderCallback senderCallback) {
+        void sendSmsByCarrierApp(String carrierPackageName, SmsSenderCallback senderCallback) {
             super.sendSmsByCarrierApp(carrierPackageName, senderCallback);
         }
 
@@ -712,69 +890,6 @@
         }
     }
 
-    /**
-     * Callback for MultipartSmsSender from the carrier messaging service.
-     * Once the result is ready, the carrier messaging service connection is disposed.
-     */
-    private final class MultipartSmsSenderCallback implements CarrierMessagingCallback {
-        private final MultipartSmsSender mSmsSender;
-        private boolean mCallbackCalled = false;
-
-        MultipartSmsSenderCallback(MultipartSmsSender smsSender) {
-            mSmsSender = smsSender;
-        }
-
-        @Override
-        public void onSendSmsComplete(int result, int messageRef) {
-            Rlog.e(TAG, "Unexpected onSendSmsComplete call with result: " + result);
-        }
-
-        /**
-         * This method should be called only once.
-         */
-        @Override
-        public void onSendMultipartSmsComplete(int result, int[] messageRefs) {
-            Rlog.d(TAG, "onSendMultipartSmsComplete: result=" + result + " messageRefs="
-                    + Arrays.toString(messageRefs));
-            if (mCallbackCalled) {
-                logWithLocalLog("onSendMultipartSmsComplete: unexpected call");
-                AnomalyReporter.reportAnomaly(sAnomalyUnexpectedCallback,
-                        "Unexpected onSendMultipartSmsComplete", mPhone.getCarrierId());
-                return;
-            }
-            mCallbackCalled = true;
-            mSmsSender.removeTimeout();
-            mSmsSender.mCarrierMessagingServiceWrapper.disconnect();
-
-            if (mSmsSender.mTrackers == null) {
-                Rlog.e(TAG, "Unexpected onSendMultipartSmsComplete call with null trackers.");
-                return;
-            }
-
-            final long identity = Binder.clearCallingIdentity();
-            try {
-                processSendMultipartSmsResponse(mSmsSender.mTrackers, result, messageRefs);
-            } finally {
-                Binder.restoreCallingIdentity(identity);
-            }
-        }
-
-        @Override
-        public void onReceiveSmsComplete(int result) {
-            Rlog.e(TAG, "Unexpected onReceiveSmsComplete call with result: " + result);
-        }
-
-        @Override
-        public void onSendMmsComplete(int result, byte[] sendConfPdu) {
-            Rlog.e(TAG, "Unexpected onSendMmsComplete call with result: " + result);
-        }
-
-        @Override
-        public void onDownloadMmsComplete(int result) {
-            Rlog.e(TAG, "Unexpected onDownloadMmsComplete call with result: " + result);
-        }
-    }
-
     private void processSendMultipartSmsResponse(
             SmsTracker[] trackers, int result, int[] messageRefs) {
         if (trackers == null) {
@@ -919,7 +1034,7 @@
                 // This is retry after failure over IMS but voice is not available.
                 // Set retry to max allowed, so no retry is sent and cause
                 // SmsManager.RESULT_ERROR_GENERIC_FAILURE to be returned to app.
-                tracker.mRetryCount = MAX_SEND_RETRIES;
+                tracker.mRetryCount = getMaxSmsRetryCount();
 
                 Rlog.d(TAG, "handleSendComplete: Skipping retry: "
                         + " isIms()=" + isIms()
@@ -942,7 +1057,7 @@
                         tracker.isFromDefaultSmsApplication(mContext),
                         tracker.getInterval());
             } else if (error == SmsManager.RESULT_RIL_SMS_SEND_FAIL_RETRY
-                    && tracker.mRetryCount < MAX_SEND_RETRIES) {
+                    && tracker.mRetryCount < getMaxSmsRetryCount()) {
                 // Retry after a delay if needed.
                 // TODO: According to TS 23.040, 9.2.3.6, we should resend
                 //       with the same TP-MR as the failed message, and
@@ -954,7 +1069,7 @@
                 tracker.mRetryCount++;
                 int errorCode = (smsResponse != null) ? smsResponse.mErrorCode : NO_ERROR_CODE;
                 Message retryMsg = obtainMessage(EVENT_SEND_RETRY, tracker);
-                sendMessageDelayed(retryMsg, SEND_RETRY_DELAY);
+                sendMessageDelayed(retryMsg, getSmsRetryDelayValue());
                 mPhone.getSmsStats().onOutgoingSms(
                         tracker.mImsRetry > 0 /* isOverIms */,
                         SmsConstants.FORMAT_3GPP2.equals(getFormat()),
@@ -981,8 +1096,31 @@
     }
 
     @SmsManager.Result
-    private static int rilErrorToSmsManagerResult(CommandException.Error rilError,
+    private int rilErrorToSmsManagerResult(CommandException.Error rilError,
             SmsTracker tracker) {
+        mSmsOutgoingErrorCodes.log("rilError: " + rilError
+                + ", MessageId: " + SmsController.formatCrossStackMessageId(tracker.mMessageId));
+
+        ApplicationInfo appInfo = tracker.getAppInfo();
+        if (appInfo == null
+                || !CompatChanges.isChangeEnabled(ADD_MORE_SMS_SENDING_ERROR_CODES, appInfo.uid)) {
+            if (rilError == CommandException.Error.INVALID_RESPONSE
+                    || rilError == CommandException.Error.SIM_PIN2
+                    || rilError == CommandException.Error.SIM_PUK2
+                    || rilError == CommandException.Error.SUBSCRIPTION_NOT_AVAILABLE
+                    || rilError == CommandException.Error.SIM_ERR
+                    || rilError == CommandException.Error.INVALID_SIM_STATE
+                    || rilError == CommandException.Error.NO_SMS_TO_ACK
+                    || rilError == CommandException.Error.SIM_BUSY
+                    || rilError == CommandException.Error.SIM_FULL
+                    || rilError == CommandException.Error.NO_SUBSCRIPTION
+                    || rilError == CommandException.Error.NO_NETWORK_FOUND
+                    || rilError == CommandException.Error.DEVICE_IN_USE
+                    || rilError == CommandException.Error.ABORTED) {
+                return SmsManager.RESULT_ERROR_GENERIC_FAILURE;
+            }
+        }
+
         switch (rilError) {
             case RADIO_NOT_AVAILABLE:
                 return SmsManager.RESULT_RIL_RADIO_NOT_AVAILABLE;
@@ -1032,6 +1170,34 @@
                 return SmsManager.RESULT_RIL_ACCESS_BARRED;
             case BLOCKED_DUE_TO_CALL:
                 return SmsManager.RESULT_RIL_BLOCKED_DUE_TO_CALL;
+            case INVALID_SMSC_ADDRESS:
+                return SmsManager.RESULT_INVALID_SMSC_ADDRESS;
+            case INVALID_RESPONSE:
+                return SmsManager.RESULT_RIL_INVALID_RESPONSE;
+            case SIM_PIN2:
+                return SmsManager.RESULT_RIL_SIM_PIN2;
+            case SIM_PUK2:
+                return SmsManager.RESULT_RIL_SIM_PUK2;
+            case SUBSCRIPTION_NOT_AVAILABLE:
+                return SmsManager.RESULT_RIL_SUBSCRIPTION_NOT_AVAILABLE;
+            case SIM_ERR:
+                return SmsManager.RESULT_RIL_SIM_ERROR;
+            case INVALID_SIM_STATE:
+                return SmsManager.RESULT_RIL_INVALID_SIM_STATE;
+            case NO_SMS_TO_ACK:
+                return SmsManager.RESULT_RIL_NO_SMS_TO_ACK;
+            case SIM_BUSY:
+                return SmsManager.RESULT_RIL_SIM_BUSY;
+            case SIM_FULL:
+                return SmsManager.RESULT_RIL_SIM_FULL;
+            case NO_SUBSCRIPTION:
+                return SmsManager.RESULT_RIL_NO_SUBSCRIPTION;
+            case NO_NETWORK_FOUND:
+                return SmsManager.RESULT_RIL_NO_NETWORK_FOUND;
+            case DEVICE_IN_USE:
+                return SmsManager.RESULT_RIL_DEVICE_IN_USE;
+            case ABORTED:
+                return SmsManager.RESULT_RIL_ABORTED;
             default:
                 Rlog.d(TAG, "rilErrorToSmsManagerResult: " + rilError + " "
                         + SmsController.formatCrossStackMessageId(tracker.mMessageId));
@@ -1133,14 +1299,15 @@
     @UnsupportedAppUsage
     protected void sendData(String callingPackage, String destAddr, String scAddr, int destPort,
             byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent, boolean isForVvm) {
+        int messageRef = nextMessageRef();
         SmsMessageBase.SubmitPduBase pdu = getSubmitPdu(
-                scAddr, destAddr, destPort, data, (deliveryIntent != null));
+                scAddr, destAddr, destPort, data, (deliveryIntent != null), messageRef);
         if (pdu != null) {
             HashMap map = getSmsTrackerMap(destAddr, scAddr, destPort, data, pdu);
             SmsTracker tracker = getSmsTracker(callingPackage, map, sentIntent, deliveryIntent,
                     getFormat(), null /*messageUri*/, false /*expectMore*/,
                     null /*fullMessageText*/, false /*isText*/,
-                    true /*persistMessage*/, isForVvm, 0L /* messageId */);
+                    true /*persistMessage*/, isForVvm, 0L /* messageId */, messageRef);
 
             if (!sendSmsByCarrierApp(true /* isDataSms */, tracker)) {
                 sendSubmitPdu(tracker);
@@ -1252,18 +1419,131 @@
      *                 Used for logging and diagnostics purposes. The id may be NULL.
      */
     public void sendText(String destAddr, String scAddr, String text,
+            PendingIntent sentIntent, PendingIntent deliveryIntent, Uri messageUri,
+            String callingPkg, boolean persistMessage, int priority,
+            boolean expectMore, int validityPeriod, boolean isForVvm,
+            long messageId) {
+        sendText(destAddr, scAddr, text, sentIntent, deliveryIntent, messageUri, callingPkg,
+                persistMessage, priority, expectMore, validityPeriod, isForVvm, messageId, false);
+    }
+
+    /**
+     * Send a text based SMS.
+     *
+     * @param destAddr the address to send the message to
+     * @param scAddr is the service center address or null to use
+     *  the current default SMSC
+     * @param text the body of the message to send
+     * @param sentIntent if not NULL this <code>PendingIntent</code> is
+     *  broadcast when the message is successfully sent, or failed.
+     *  The result code will be <code>Activity.RESULT_OK<code> for success,
+     *  or one of these errors:<br>
+     *  <code>SmsManager.RESULT_ERROR_GENERIC_FAILURE</code><br>
+     *  <code>SmsManager.RESULT_ERROR_RADIO_OFF</code><br>
+     *  <code>SmsManager.RESULT_ERROR_NULL_PDU</code><br>
+     *  <code>SmsManager.RESULT_ERROR_NO_SERVICE</code><br>
+     *  <code>SmsManager.RESULT_ERROR_LIMIT_EXCEEDED</code><br>
+     *  <code>SmsManager.RESULT_ERROR_FDN_CHECK_FAILURE</code><br>
+     *  <code>SmsManager.RESULT_ERROR_SHORT_CODE_NOT_ALLOWED</code><br>
+     *  <code>SmsManager.RESULT_ERROR_SHORT_CODE_NEVER_ALLOWED</code><br>
+     *  <code>SmsManager.RESULT_RADIO_NOT_AVAILABLE</code><br>
+     *  <code>SmsManager.RESULT_NETWORK_REJECT</code><br>
+     *  <code>SmsManager.RESULT_INVALID_ARGUMENTS</code><br>
+     *  <code>SmsManager.RESULT_INVALID_STATE</code><br>
+     *  <code>SmsManager.RESULT_NO_MEMORY</code><br>
+     *  <code>SmsManager.RESULT_INVALID_SMS_FORMAT</code><br>
+     *  <code>SmsManager.RESULT_SYSTEM_ERROR</code><br>
+     *  <code>SmsManager.RESULT_MODEM_ERROR</code><br>
+     *  <code>SmsManager.RESULT_NETWORK_ERROR</code><br>
+     *  <code>SmsManager.RESULT_ENCODING_ERROR</code><br>
+     *  <code>SmsManager.RESULT_INVALID_SMSC_ADDRESS</code><br>
+     *  <code>SmsManager.RESULT_OPERATION_NOT_ALLOWED</code><br>
+     *  <code>SmsManager.RESULT_INTERNAL_ERROR</code><br>
+     *  <code>SmsManager.RESULT_NO_RESOURCES</code><br>
+     *  <code>SmsManager.RESULT_CANCELLED</code><br>
+     *  <code>SmsManager.RESULT_REQUEST_NOT_SUPPORTED</code><br>
+     *  <code>SmsManager.RESULT_NO_BLUETOOTH_SERVICE</code><br>
+     *  <code>SmsManager.RESULT_INVALID_BLUETOOTH_ADDRESS</code><br>
+     *  <code>SmsManager.RESULT_BLUETOOTH_DISCONNECTED</code><br>
+     *  <code>SmsManager.RESULT_UNEXPECTED_EVENT_STOP_SENDING</code><br>
+     *  <code>SmsManager.RESULT_SMS_BLOCKED_DURING_EMERGENCY</code><br>
+     *  <code>SmsManager.RESULT_SMS_SEND_RETRY_FAILED</code><br>
+     *  <code>SmsManager.RESULT_REMOTE_EXCEPTION</code><br>
+     *  <code>SmsManager.RESULT_NO_DEFAULT_SMS_APP</code><br>
+     *  <code>SmsManager.RESULT_RIL_RADIO_NOT_AVAILABLE</code><br>
+     *  <code>SmsManager.RESULT_RIL_SMS_SEND_FAIL_RETRY</code><br>
+     *  <code>SmsManager.RESULT_RIL_NETWORK_REJECT</code><br>
+     *  <code>SmsManager.RESULT_RIL_INVALID_STATE</code><br>
+     *  <code>SmsManager.RESULT_RIL_INVALID_ARGUMENTS</code><br>
+     *  <code>SmsManager.RESULT_RIL_NO_MEMORY</code><br>
+     *  <code>SmsManager.RESULT_RIL_REQUEST_RATE_LIMITED</code><br>
+     *  <code>SmsManager.RESULT_RIL_INVALID_SMS_FORMAT</code><br>
+     *  <code>SmsManager.RESULT_RIL_SYSTEM_ERR</code><br>
+     *  <code>SmsManager.RESULT_RIL_ENCODING_ERR</code><br>
+     *  <code>SmsManager.RESULT_RIL_INVALID_SMSC_ADDRESS</code><br>
+     *  <code>SmsManager.RESULT_RIL_MODEM_ERR</code><br>
+     *  <code>SmsManager.RESULT_RIL_NETWORK_ERR</code><br>
+     *  <code>SmsManager.RESULT_RIL_INTERNAL_ERR</code><br>
+     *  <code>SmsManager.RESULT_RIL_REQUEST_NOT_SUPPORTED</code><br>
+     *  <code>SmsManager.RESULT_RIL_INVALID_MODEM_STATE</code><br>
+     *  <code>SmsManager.RESULT_RIL_NETWORK_NOT_READY</code><br>
+     *  <code>SmsManager.RESULT_RIL_OPERATION_NOT_ALLOWED</code><br>
+     *  <code>SmsManager.RESULT_RIL_NO_RESOURCES</code><br>
+     *  <code>SmsManager.RESULT_RIL_CANCELLED</code><br>
+     *  <code>SmsManager.RESULT_RIL_SIM_ABSENT</code><br>
+     *  <code>SmsManager.RESULT_RIL_SIMULTANEOUS_SMS_AND_CALL_NOT_ALLOWED</code><br>
+     *  <code>SmsManager.RESULT_RIL_ACCESS_BARRED</code><br>
+     *  <code>SmsManager.RESULT_RIL_BLOCKED_DUE_TO_CALL</code><br>
+     *  For <code>SmsManager.RESULT_ERROR_GENERIC_FAILURE</code> or any of the RESULT_RIL errors,
+     *  the sentIntent may include the extra "errorCode" containing a radio technology specific
+     *  value, generally only useful for troubleshooting.<br>
+     *  The per-application based SMS control checks sentIntent. If sentIntent
+     *  is NULL the caller will be checked against all unknown applications,
+     *  which cause smaller number of SMS to be sent in checking period.
+     * @param deliveryIntent if not NULL this <code>PendingIntent</code> is
+     *  broadcast when the message is delivered to the recipient.  The
+     * @param messageUri optional URI of the message if it is already stored in the system
+     * @param callingPkg the calling package name
+     * @param persistMessage whether to save the sent message into SMS DB for a
+     *  non-default SMS app.
+     *
+     * @param priority Priority level of the message
+     *  Refer specification See 3GPP2 C.S0015-B, v2.0, table 4.5.9-1
+     *  ---------------------------------
+     *  PRIORITY      | Level of Priority
+     *  ---------------------------------
+     *      '00'      |     Normal
+     *      '01'      |     Interactive
+     *      '10'      |     Urgent
+     *      '11'      |     Emergency
+     *  ----------------------------------
+     *  Any Other values included Negative considered as Invalid Priority Indicator of the message.
+     * @param expectMore is a boolean to indicate the sending messages through same link or not.
+     * @param validityPeriod Validity Period of the message in mins.
+     *  Refer specification 3GPP TS 23.040 V6.8.1 section 9.2.3.12.1.
+     *  Validity Period(Minimum) -> 5 mins
+     *  Validity Period(Maximum) -> 635040 mins(i.e.63 weeks).
+     *  Any Other values included Negative considered as Invalid Validity Period of the message.
+     * @param messageId An id that uniquely identifies the message requested to be sent.
+     *                 Used for logging and diagnostics purposes. The id may be NULL.
+     * @param skipShortCodeCheck Skip check for short code type destination address.
+     */
+    public void sendText(String destAddr, String scAddr, String text,
                          PendingIntent sentIntent, PendingIntent deliveryIntent, Uri messageUri,
                          String callingPkg, boolean persistMessage, int priority,
                          boolean expectMore, int validityPeriod, boolean isForVvm,
-                         long messageId) {
+                         long messageId, boolean skipShortCodeCheck) {
         Rlog.d(TAG, "sendText id: " + SmsController.formatCrossStackMessageId(messageId));
+        int messageRef = nextMessageRef();
         SmsMessageBase.SubmitPduBase pdu = getSubmitPdu(
-                scAddr, destAddr, text, (deliveryIntent != null), null, priority, validityPeriod);
+                scAddr, destAddr, text, (deliveryIntent != null), null, priority, validityPeriod,
+                messageRef);
         if (pdu != null) {
             HashMap map = getSmsTrackerMap(destAddr, scAddr, text, pdu);
             SmsTracker tracker = getSmsTracker(callingPkg, map, sentIntent, deliveryIntent,
                     getFormat(), messageUri, expectMore, text, true /*isText*/,
-                    persistMessage, priority, validityPeriod, isForVvm, messageId);
+                    persistMessage, priority, validityPeriod, isForVvm, messageId, messageRef,
+                    skipShortCodeCheck);
 
             if (!sendSmsByCarrierApp(false /* isDataSms */, tracker)) {
                 sendSubmitPdu(tracker);
@@ -1319,6 +1599,14 @@
     protected abstract SmsMessageBase.SubmitPduBase getSubmitPdu(String scAddr, String destAddr,
             int destPort, byte[] message, boolean statusReportRequested);
 
+    protected abstract SmsMessageBase.SubmitPduBase getSubmitPdu(String scAddr, String destAddr,
+            String message, boolean statusReportRequested, SmsHeader smsHeader,
+            int priority, int validityPeriod, int messageRef);
+
+
+    protected abstract SmsMessageBase.SubmitPduBase getSubmitPdu(String scAddr, String destAddr,
+            int destPort, byte[] message, boolean statusReportRequested, int messageRef);
+
     /**
      * Calculate the number of septets needed to encode the message. This function should only be
      * called for individual segments of multipart message.
@@ -1494,12 +1782,13 @@
             if (deliveryIntents != null && deliveryIntents.size() > i) {
                 deliveryIntent = deliveryIntents.get(i);
             }
-
+            int messageRef = nextMessageRef();
             trackers[i] =
                 getNewSubmitPduTracker(callingPkg, destAddr, scAddr, parts.get(i), smsHeader,
                         encoding, sentIntent, deliveryIntent, (i == (msgCount - 1)),
                         unsentPartCount, anyPartFailed, messageUri,
-                        fullMessageText, priority, expectMore, validityPeriod, messageId);
+                        fullMessageText, priority, expectMore, validityPeriod, messageId,
+                        messageRef);
             if (trackers[i] == null) {
                 triggerSentIntentForFailure(sentIntents);
                 return;
@@ -1509,12 +1798,10 @@
 
         String carrierPackage = getCarrierAppPackageName();
         if (carrierPackage != null) {
-            Rlog.d(TAG, "Found carrier package " + carrierPackage
-                    + " "
+            Rlog.d(TAG, "Found carrier package " + carrierPackage + " "
                     + SmsController.formatCrossStackMessageId(getMultiTrackermessageId(trackers)));
             MultipartSmsSender smsSender = new MultipartSmsSender(parts, trackers);
-            smsSender.sendSmsByCarrierApp(carrierPackage,
-                    new MultipartSmsSenderCallback(smsSender));
+            smsSender.sendSmsByCarrierApp(carrierPackage, new SmsSenderCallback(smsSender));
         } else {
             Rlog.v(TAG, "No carrier package. "
                     + SmsController.formatCrossStackMessageId(getMultiTrackermessageId(trackers)));
@@ -1537,7 +1824,7 @@
             PendingIntent sentIntent, PendingIntent deliveryIntent, boolean lastPart,
             AtomicInteger unsentPartCount, AtomicBoolean anyPartFailed, Uri messageUri,
             String fullMessageText, int priority, boolean expectMore, int validityPeriod,
-            long messageId) {
+            long messageId, int messageRef) {
         if (isCdmaMo()) {
             UserData uData = new UserData();
             uData.payloadStr = message;
@@ -1567,7 +1854,7 @@
                         getFormat(), unsentPartCount, anyPartFailed, messageUri, smsHeader,
                         (!lastPart || expectMore), fullMessageText, true /*isText*/,
                         true /*persistMessage*/, priority, validityPeriod, false /* isForVvm */,
-                        messageId);
+                        messageId, messageRef, false);
             } else {
                 Rlog.e(TAG, "CdmaSMSDispatcher.getNewSubmitPduTracker(): getSubmitPdu() returned "
                         + "null " + SmsController.formatCrossStackMessageId(messageId));
@@ -1578,15 +1865,14 @@
                     com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(scAddress,
                             destinationAddress, message, deliveryIntent != null,
                             SmsHeader.toByteArray(smsHeader), encoding, smsHeader.languageTable,
-                            smsHeader.languageShiftTable, validityPeriod);
+                            smsHeader.languageShiftTable, validityPeriod, messageRef);
             if (pdu != null) {
-                HashMap map =  getSmsTrackerMap(destinationAddress, scAddress,
-                        message, pdu);
+                HashMap map = getSmsTrackerMap(destinationAddress, scAddress, message, pdu);
                 return getSmsTracker(callingPackage, map, sentIntent,
                         deliveryIntent, getFormat(), unsentPartCount, anyPartFailed, messageUri,
                         smsHeader, (!lastPart || expectMore), fullMessageText, true /*isText*/,
                         false /*persistMessage*/, priority, validityPeriod, false /* isForVvm */,
-                        messageId);
+                        messageId, messageRef, false);
             } else {
                 Rlog.e(TAG, "GsmSMSDispatcher.getNewSubmitPduTracker(): getSubmitPdu() returned "
                         + "null " + SmsController.formatCrossStackMessageId(messageId));
@@ -1744,7 +2030,8 @@
      */
     boolean checkDestination(SmsTracker[] trackers) {
         if (mContext.checkCallingOrSelfPermission(SEND_SMS_NO_CONFIRMATION)
-                == PackageManager.PERMISSION_GRANTED || trackers[0].mIsForVvm) {
+                == PackageManager.PERMISSION_GRANTED || trackers[0].mIsForVvm
+                || trackers[0].mSkipShortCodeDestAddrCheck) {
             return true;            // app is pre-approved to send to short codes
         } else {
             int rule = mPremiumSmsRule.get();
@@ -1785,6 +2072,12 @@
                                                 trackers[0].mDestAddress, networkCountryIso));
             }
 
+            if (smsCategory != SmsManager.SMS_CATEGORY_NOT_SHORT_CODE) {
+                int xmlVersion = mSmsDispatchersController.getUsageMonitor()
+                        .getShortCodeXmlFileVersion();
+                mPhone.getSmsStats().onOutgoingShortCodeSms(smsCategory, xmlVersion);
+            }
+
             if (smsCategory == SmsManager.SMS_CATEGORY_NOT_SHORT_CODE
                     || smsCategory == SmsManager.SMS_CATEGORY_FREE_SHORT_CODE
                     || smsCategory == SmsManager.SMS_CATEGORY_STANDARD_SHORT_CODE) {
@@ -2082,6 +2375,7 @@
         private Boolean mIsFromDefaultSmsApplication;
 
         private int mCarrierId;
+        private boolean mSkipShortCodeDestAddrCheck;
         // SMS anomaly uuid -- unexpected error from RIL
         private final UUID mAnomalyUnexpectedErrorFromRilUUID =
                 UUID.fromString("43043600-ea7a-44d2-9ae6-a58567ac7886");
@@ -2091,7 +2385,8 @@
                 AtomicInteger unsentPartCount, AtomicBoolean anyPartFailed, Uri messageUri,
                 SmsHeader smsHeader, boolean expectMore, String fullMessageText, int subId,
                 boolean isText, boolean persistMessage, int userId, int priority,
-                int validityPeriod, boolean isForVvm, long messageId, int carrierId) {
+                int validityPeriod, boolean isForVvm, long messageId, int carrierId,
+                int messageRef, boolean skipShortCodeDestAddrCheck) {
             mData = data;
             mSentIntent = sentIntent;
             mDeliveryIntent = deliveryIntent;
@@ -2102,7 +2397,7 @@
             mExpectMore = expectMore;
             mImsRetry = 0;
             mUsesImsServiceForIms = false;
-            mMessageRef = 0;
+            mMessageRef = messageRef;
             mUnsentPartCount = unsentPartCount;
             mAnyPartFailed = anyPartFailed;
             mMessageUri = messageUri;
@@ -2117,6 +2412,7 @@
             mIsForVvm = isForVvm;
             mMessageId = messageId;
             mCarrierId = carrierId;
+            mSkipShortCodeDestAddrCheck = skipShortCodeDestAddrCheck;
         }
 
         public HashMap<String, Object> getData() {
@@ -2131,12 +2427,21 @@
             return mAppInfo != null ? mAppInfo.packageName : null;
         }
 
+        /**
+         * Get the calling Application Info
+         * @return Application Info
+         */
+        public ApplicationInfo getAppInfo() {
+            return mAppInfo == null ? null : mAppInfo.applicationInfo;
+        }
+
         /** Return if the SMS was originated from the default SMS application. */
         public boolean isFromDefaultSmsApplication(Context context) {
             if (mIsFromDefaultSmsApplication == null) {
+                UserHandle userHandle = TelephonyUtils.getSubscriptionUserHandle(context, mSubId);
                 // Perform a lazy initialization, due to the cost of the operation.
-                mIsFromDefaultSmsApplication =
-                        SmsApplication.isDefaultSmsApplication(context, getAppPackageName());
+                mIsFromDefaultSmsApplication = SmsApplication.isDefaultSmsApplicationAsUser(context,
+                                    getAppPackageName(), userHandle);
             }
             return mIsFromDefaultSmsApplication;
         }
@@ -2384,7 +2689,7 @@
             AtomicInteger unsentPartCount, AtomicBoolean anyPartFailed, Uri messageUri,
             SmsHeader smsHeader, boolean expectMore, String fullMessageText, boolean isText,
             boolean persistMessage, int priority, int validityPeriod, boolean isForVvm,
-            long messageId) {
+            long messageId, int messageRef, boolean skipShortCodeCheck) {
         // Get package info via packagemanager
         UserHandle callingUser = UserHandle.getUserHandleForUid(Binder.getCallingUid());
         final int userId = callingUser.getIdentifier();
@@ -2401,28 +2706,30 @@
         return new SmsTracker(data, sentIntent, deliveryIntent, appInfo, destAddr, format,
                 unsentPartCount, anyPartFailed, messageUri, smsHeader, expectMore,
                 fullMessageText, getSubId(), isText, persistMessage, userId, priority,
-                validityPeriod, isForVvm, messageId, mPhone.getCarrierId());
+                validityPeriod, isForVvm, messageId, mPhone.getCarrierId(), messageRef,
+                skipShortCodeCheck);
     }
 
     protected SmsTracker getSmsTracker(String callingPackage, HashMap<String, Object> data,
             PendingIntent sentIntent, PendingIntent deliveryIntent, String format, Uri messageUri,
             boolean expectMore, String fullMessageText, boolean isText, boolean persistMessage,
-            boolean isForVvm, long messageId) {
+            boolean isForVvm, long messageId, int messageRef) {
         return getSmsTracker(callingPackage, data, sentIntent, deliveryIntent, format,
                 null/*unsentPartCount*/, null/*anyPartFailed*/, messageUri, null/*smsHeader*/,
                 expectMore, fullMessageText, isText, persistMessage,
                 SMS_MESSAGE_PRIORITY_NOT_SPECIFIED, SMS_MESSAGE_PERIOD_NOT_SPECIFIED, isForVvm,
-                messageId);
+                messageId, messageRef, false);
     }
 
     protected SmsTracker getSmsTracker(String callingPackage, HashMap<String, Object> data,
             PendingIntent sentIntent, PendingIntent deliveryIntent, String format, Uri messageUri,
             boolean expectMore, String fullMessageText, boolean isText, boolean persistMessage,
-            int priority, int validityPeriod, boolean isForVvm, long messageId) {
+            int priority, int validityPeriod, boolean isForVvm, long messageId, int messageRef,
+            boolean skipShortCodeCheck) {
         return getSmsTracker(callingPackage, data, sentIntent, deliveryIntent, format,
                 null/*unsentPartCount*/, null/*anyPartFailed*/, messageUri, null/*smsHeader*/,
                 expectMore, fullMessageText, isText, persistMessage, priority, validityPeriod,
-                isForVvm, messageId);
+                isForVvm, messageId, messageRef, skipShortCodeCheck);
     }
 
     protected HashMap<String, Object> getSmsTrackerMap(String destAddr, String scAddr,
@@ -2591,7 +2898,7 @@
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     protected int getSubId() {
-        return SubscriptionController.getInstance().getSubIdUsingPhoneId(mPhone.getPhoneId());
+        return SubscriptionManager.getSubscriptionId(mPhone.getPhoneId());
     }
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@@ -2641,10 +2948,17 @@
         IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, "  ");
         pw.println(TAG);
         pw.increaseIndent();
+
         pw.println("mLocalLog:");
         pw.increaseIndent();
         mLocalLog.dump(fd, pw, args);
         pw.decreaseIndent();
+
+        pw.println("mSmsOutgoingErrorCodes:");
+        pw.increaseIndent();
+        mSmsOutgoingErrorCodes.dump(fd, pw, args);
+        pw.decreaseIndent();
+
         pw.decreaseIndent();
     }
 }
diff --git a/src/java/com/android/internal/telephony/ServiceStateTracker.java b/src/java/com/android/internal/telephony/ServiceStateTracker.java
old mode 100755
new mode 100644
index 0c73169..2a76a84
--- a/src/java/com/android/internal/telephony/ServiceStateTracker.java
+++ b/src/java/com/android/internal/telephony/ServiceStateTracker.java
@@ -97,6 +97,8 @@
 import com.android.internal.telephony.imsphone.ImsPhone;
 import com.android.internal.telephony.metrics.ServiceStateStats;
 import com.android.internal.telephony.metrics.TelephonyMetrics;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppState;
 import com.android.internal.telephony.uicc.IccCardStatus.CardState;
 import com.android.internal.telephony.uicc.IccRecords;
@@ -145,7 +147,7 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private UiccController mUiccController = null;
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
-    private UiccCardApplication mUiccApplcation = null;
+    private UiccCardApplication mUiccApplication = null;
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private IccRecords mIccRecords = null;
 
@@ -181,13 +183,12 @@
 
     private final Set<Integer> mRadioPowerOffReasons = new HashSet();
 
-    // TODO - this should not be public, right now used externally GsmConnetion.
+    // TODO - this should not be public, right now used externally GsmConnection.
     public RestrictedState mRestrictedState;
 
     /**
-     * A unique identifier to track requests associated with a poll
-     * and ignore stale responses.  The value is a count-down of
-     * expected responses in this pollingContext.
+     * A unique identifier to track requests associated with a poll and ignore stale responses.
+     * The value is a count-down of expected responses in this pollingContext.
      */
     @VisibleForTesting
     public int[] mPollingContext;
@@ -216,7 +217,6 @@
     private RegistrantList mNrStateChangedRegistrants = new RegistrantList();
     private RegistrantList mNrFrequencyChangedRegistrants = new RegistrantList();
     private RegistrantList mCssIndicatorChangedRegistrants = new RegistrantList();
-    private final RegistrantList mBandwidthChangedRegistrants = new RegistrantList();
     private final RegistrantList mAirplaneModeChangedRegistrants = new RegistrantList();
     private final RegistrantList mAreaCodeChangedRegistrants = new RegistrantList();
 
@@ -309,7 +309,7 @@
     // Show PLMN only and only if this bit is set.
     public static final int CARRIER_NAME_DISPLAY_BITMASK_SHOW_PLMN = 1 << 1;
 
-    private List<Message> mPendingCellInfoRequests = new LinkedList<Message>();
+    private List<Message> mPendingCellInfoRequests = new LinkedList<>();
     // @GuardedBy("mPendingCellInfoRequests")
     private boolean mIsPendingCellInfoRequest = false;
 
@@ -320,8 +320,6 @@
     private CarrierDisplayNameResolver mCdnr;
 
     private boolean mImsRegistrationOnOff = false;
-    /** Radio is disabled by carrier. Radio power will not be override if this field is set */
-    private boolean mRadioDisabledByCarrier = false;
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private boolean mDeviceShuttingDown = false;
     /** Keep track of SPN display rules, so we only broadcast intent if something changes. */
@@ -348,11 +346,11 @@
     private SubscriptionManager mSubscriptionManager;
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private SubscriptionController mSubscriptionController;
+    private SubscriptionManagerService mSubscriptionManagerService;
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private final SstSubscriptionsChangedListener mOnSubscriptionsChangedListener =
         new SstSubscriptionsChangedListener();
 
-
     private final RatRatcheter mRatRatcheter;
 
     private final LocaleTracker mLocaleTracker;
@@ -365,6 +363,7 @@
     private final LocalLog mCdnrLogs = new LocalLog(64);
 
     private Pattern mOperatorNameStringPattern;
+    private PersistableBundle mCarrierConfig;
 
     private class SstSubscriptionsChangedListener extends OnSubscriptionsChangedListener {
 
@@ -652,15 +651,21 @@
         mCi.registerForCellInfoList(this, EVENT_UNSOL_CELL_INFO_LIST, null);
         mCi.registerForPhysicalChannelConfiguration(this, EVENT_PHYSICAL_CHANNEL_CONFIG, null);
 
-        mSubscriptionController = SubscriptionController.getInstance();
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            mSubscriptionManagerService = SubscriptionManagerService.getInstance();
+        } else {
+            mSubscriptionController = SubscriptionController.getInstance();
+        }
+
         mSubscriptionManager = SubscriptionManager.from(phone.getContext());
         mSubscriptionManager.addOnSubscriptionsChangedListener(
                 new android.os.HandlerExecutor(this), mOnSubscriptionsChangedListener);
         mRestrictedState = new RestrictedState();
+        mCarrierConfig = getCarrierConfig();
 
         mAccessNetworksManager = mPhone.getAccessNetworksManager();
         mOutOfServiceSS = new ServiceState();
-        mOutOfServiceSS.setOutOfService(mAccessNetworksManager.isInLegacyMode(), false);
+        mOutOfServiceSS.setOutOfService(false);
 
         for (int transportType : mAccessNetworksManager.getAvailableTransports()) {
             mRegStateManagers.append(transportType, new NetworkRegistrationManager(
@@ -683,12 +688,11 @@
                 Settings.Global.ENABLE_CELLULAR_ON_BOOT, 1);
         mDesiredPowerState = (enableCellularOnBoot > 0) && ! (airplaneMode > 0);
         if (!mDesiredPowerState) {
-            mRadioPowerOffReasons.add(Phone.RADIO_POWER_REASON_USER);
+            mRadioPowerOffReasons.add(TelephonyManager.RADIO_POWER_REASON_USER);
         }
         mRadioPowerLog.log("init : airplane mode = " + airplaneMode + " enableCellularOnBoot = " +
                 enableCellularOnBoot);
 
-
         mPhone.getCarrierActionAgent().registerForCarrierAction(CARRIER_ACTION_SET_RADIO_ENABLED,
                 this, EVENT_RADIO_POWER_FROM_CARRIER, null, false);
 
@@ -762,9 +766,9 @@
         }
 
         mSS = new ServiceState();
-        mSS.setOutOfService(mAccessNetworksManager.isInLegacyMode(), false);
+        mSS.setOutOfService(false);
         mNewSS = new ServiceState();
-        mNewSS.setOutOfService(mAccessNetworksManager.isInLegacyMode(), false);
+        mNewSS.setOutOfService(false);
         mLastCellInfoReqTime = 0;
         mLastCellInfoList = null;
         mStartedGprsRegCheck = false;
@@ -867,7 +871,10 @@
     public boolean getDesiredPowerState() {
         return mDesiredPowerState;
     }
-    public boolean getPowerStateFromCarrier() { return !mRadioDisabledByCarrier; }
+
+    public boolean getPowerStateFromCarrier() {
+        return !mRadioPowerOffReasons.contains(TelephonyManager.RADIO_POWER_REASON_CARRIER);
+    }
 
     public List<PhysicalChannelConfig> getPhysicalChannelConfigList() {
         return mLastPhysicalChannelConfigList;
@@ -901,7 +908,7 @@
         if (nrs != null) {
             int rat = ServiceState.networkTypeToRilRadioTechnology(
                     nrs.getAccessNetworkTechnology());
-            int drs = regCodeToServiceState(nrs.getInitialRegistrationState());
+            int drs = regCodeToServiceState(nrs.getNetworkRegistrationState());
             return new Pair<>(drs, rat);
         }
         return null;
@@ -1057,7 +1064,7 @@
      * @return the current reasons for which the radio is off.
      */
     public Set<Integer> getRadioPowerOffReasons() {
-        return mRadioPowerOffReasons;
+        return Set.copyOf(mRadioPowerOffReasons);
     }
 
     /**
@@ -1083,14 +1090,14 @@
     public void setRadioPower(boolean power, boolean forEmergencyCall,
             boolean isSelectedPhoneForEmergencyCall, boolean forceApply) {
         setRadioPowerForReason(power, forEmergencyCall, isSelectedPhoneForEmergencyCall, forceApply,
-                Phone.RADIO_POWER_REASON_USER);
+                TelephonyManager.RADIO_POWER_REASON_USER);
     }
 
     /**
      * Turn on or off radio power with option to specify whether it's for emergency call and specify
      * a reason for setting the power state.
-     * More details check {@link PhoneInternalInterface#setRadioPower(
-     * boolean, boolean, boolean, boolean, int)}.
+     * More details check {@link
+     * PhoneInternalInterface#setRadioPowerForReason(boolean, boolean, boolean, boolean, int)}.
      */
     public void setRadioPowerForReason(boolean power, boolean forEmergencyCall,
             boolean isSelectedPhoneForEmergencyCall, boolean forceApply, int reason) {
@@ -1124,23 +1131,6 @@
     }
 
     /**
-     * Radio power set from carrier action. if set to false means carrier desire to turn radio off
-     * and radio wont be re-enabled unless carrier explicitly turn it back on.
-     * @param enable indicate if radio power is enabled or disabled from carrier action.
-     */
-    public void setRadioPowerFromCarrier(boolean enable) {
-        boolean disableByCarrier = !enable;
-        if (mRadioDisabledByCarrier == disableByCarrier) {
-            log("setRadioPowerFromCarrier mRadioDisabledByCarrier is already "
-                    + disableByCarrier + " Do nothing.");
-            return;
-        }
-
-        mRadioDisabledByCarrier = disableByCarrier;
-        setPowerStateToDesired();
-    }
-
-    /**
      * These two flags manage the behavior of the cell lock -- the
      * lock should be held if either flag is true.  The intention is
      * to allow temporary acquisition of the lock to get a single
@@ -1213,8 +1203,8 @@
                     mCdnr.updateEfFromUsim(null /* Usim */);
                 }
                 onUpdateIccAvailability();
-                if (mUiccApplcation == null
-                        || mUiccApplcation.getState() != AppState.APPSTATE_READY) {
+                if (mUiccApplication == null
+                        || mUiccApplication.getState() != AppState.APPSTATE_READY) {
                     mIsSimReady = false;
                     updateSpnDisplay();
                 }
@@ -1637,7 +1627,8 @@
                 if (ar.exception == null) {
                     boolean enable = (boolean) ar.result;
                     if (DBG) log("EVENT_RADIO_POWER_FROM_CARRIER: " + enable);
-                    setRadioPowerFromCarrier(enable);
+                    setRadioPowerForReason(enable, false, false, false,
+                            TelephonyManager.RADIO_POWER_REASON_CARRIER);
                 }
                 break;
 
@@ -1666,6 +1657,7 @@
                     // Notify NR frequency, NR connection status or bandwidths changed.
                     if (hasChanged) {
                         mPhone.notifyServiceStateChanged(mPhone.getServiceState());
+                        mServiceStateChangedRegistrants.notifyRegistrants();
                         TelephonyMetrics.getInstance().writeServiceStateChanged(
                                 mPhone.getPhoneId(), mSS);
                         mPhone.getVoiceCallSessionStats().onServiceStateChanged(mSS);
@@ -2081,16 +2073,20 @@
         if (physicalChannelConfigs != null) {
             for (PhysicalChannelConfig config : physicalChannelConfigs) {
                 if (isNrPhysicalChannelConfig(config) && isInternetPhysicalChannelConfig(config)) {
-                    // Update the NR frequency range if there is an internet data connection
+                    // Update the NR frequency range if there is an active internet data connection
                     // associated with this NR physical channel channel config.
-                    newFrequencyRange = ServiceState.getBetterNRFrequencyRange(
-                            newFrequencyRange, config.getFrequencyRange());
-                    break;
+                    // If there are multiple valid configs, use the highest frequency range value.
+                    newFrequencyRange = Math.max(newFrequencyRange, config.getFrequencyRange());
                 }
             }
         }
 
         boolean hasChanged = newFrequencyRange != ss.getNrFrequencyRange();
+        if (hasChanged) {
+            log(String.format("NR frequency range changed from %s to %s.",
+                    ServiceState.frequencyRangeToString(ss.getNrFrequencyRange()),
+                    ServiceState.frequencyRangeToString(newFrequencyRange)));
+        }
         ss.setNrFrequencyRange(newFrequencyRange);
         return hasChanged;
     }
@@ -2121,6 +2117,11 @@
         }
 
         boolean hasChanged = newNrState != oldNrState;
+        if (hasChanged) {
+            log(String.format("NR state changed from %s to %s.",
+                    NetworkRegistrationInfo.nrStateToString(oldNrState),
+                    NetworkRegistrationInfo.nrStateToString(newNrState)));
+        }
         regInfo.setNrState(newNrState);
         ss.addNetworkRegistrationInfo(regInfo);
         return hasChanged;
@@ -2160,14 +2161,14 @@
         if (wlanPsRegState != null
                 && wlanPsRegState.getAccessNetworkTechnology()
                 == TelephonyManager.NETWORK_TYPE_IWLAN
-                && wlanPsRegState.getInitialRegistrationState()
+                && wlanPsRegState.getNetworkRegistrationState()
                 == NetworkRegistrationInfo.REGISTRATION_STATE_HOME
                 && isIwlanPreferred) {
             serviceState.setDataRegState(ServiceState.STATE_IN_SERVICE);
         } else if (wwanPsRegState != null) {
             // If the device is not camped on IWLAN, then we use cellular PS registration state
             // to compute reg state and rat.
-            int regState = wwanPsRegState.getInitialRegistrationState();
+            int regState = wwanPsRegState.getNetworkRegistrationState();
             serviceState.setDataRegState(regCodeToServiceState(regState));
         }
         if (DBG) {
@@ -2183,10 +2184,8 @@
                 VoiceSpecificRegistrationInfo voiceSpecificStates =
                         networkRegState.getVoiceSpecificInfo();
 
-                int registrationState = networkRegState.getInitialRegistrationState();
+                int registrationState = networkRegState.getNetworkRegistrationState();
                 int cssIndicator = voiceSpecificStates.cssSupported ? 1 : 0;
-                int newVoiceRat = ServiceState.networkTypeToRilRadioTechnology(
-                        networkRegState.getAccessNetworkTechnology());
                 mNewSS.setVoiceRegState(regCodeToServiceState(registrationState));
                 mNewSS.setCssIndicator(cssIndicator);
                 mNewSS.addNetworkRegistrationInfo(networkRegState);
@@ -2219,7 +2218,7 @@
                                     && !isRoamIndForHomeSystem(roamingIndicator);
                     mNewSS.setVoiceRoaming(cdmaRoaming);
                     mRoamingIndicator = roamingIndicator;
-                    mIsInPrl = (systemIsInPrl == 0) ? false : true;
+                    mIsInPrl = systemIsInPrl != 0;
                     mDefaultRoamingIndicator = defaultRoamingIndicator;
 
                     int systemId = 0;
@@ -2265,7 +2264,7 @@
                 mNewSS.addNetworkRegistrationInfo(networkRegState);
                 DataSpecificRegistrationInfo dataSpecificStates =
                         networkRegState.getDataSpecificInfo();
-                int registrationState = networkRegState.getInitialRegistrationState();
+                int registrationState = networkRegState.getNetworkRegistrationState();
                 int serviceState = regCodeToServiceState(registrationState);
                 int newDataRat = ServiceState.networkTypeToRilRadioTechnology(
                         networkRegState.getAccessNetworkTechnology());
@@ -2596,8 +2595,7 @@
      */
     private boolean isRoamIndForHomeSystem(int roamInd) {
         // retrieve the carrier-specified list of ERIs for home system
-        final PersistableBundle config = getCarrierConfig();
-        int[] homeRoamIndicators = config.getIntArray(CarrierConfigManager
+        int[] homeRoamIndicators = mCarrierConfig.getIntArray(CarrierConfigManager
                     .KEY_CDMA_ENHANCED_ROAMING_INDICATOR_FOR_HOME_NETWORK_INT_ARRAY);
 
         log("isRoamIndForHomeSystem: homeRoamIndicators=" + Arrays.toString(homeRoamIndicators));
@@ -2626,8 +2624,6 @@
      */
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     protected void updateRoamingState() {
-        PersistableBundle bundle = getCarrierConfig();
-
         if (mPhone.isPhoneTypeGsm()) {
             /**
              * Since the roaming state of gsm service (from +CREG) and
@@ -2652,14 +2648,14 @@
                 roaming = false;
             }
 
-            if (alwaysOnHomeNetwork(bundle)) {
+            if (alwaysOnHomeNetwork(mCarrierConfig)) {
                 log("updateRoamingState: carrier config override always on home network");
                 roaming = false;
-            } else if (isNonRoamingInGsmNetwork(bundle, mNewSS.getOperatorNumeric())) {
+            } else if (isNonRoamingInGsmNetwork(mCarrierConfig, mNewSS.getOperatorNumeric())) {
                 log("updateRoamingState: carrier config override set non roaming:"
                         + mNewSS.getOperatorNumeric());
                 roaming = false;
-            } else if (isRoamingInGsmNetwork(bundle, mNewSS.getOperatorNumeric())) {
+            } else if (isRoamingInGsmNetwork(mCarrierConfig, mNewSS.getOperatorNumeric())) {
                 log("updateRoamingState: carrier config override set roaming:"
                         + mNewSS.getOperatorNumeric());
                 roaming = true;
@@ -2669,16 +2665,16 @@
         } else {
             String systemId = Integer.toString(mNewSS.getCdmaSystemId());
 
-            if (alwaysOnHomeNetwork(bundle)) {
+            if (alwaysOnHomeNetwork(mCarrierConfig)) {
                 log("updateRoamingState: carrier config override always on home network");
                 setRoamingOff();
-            } else if (isNonRoamingInGsmNetwork(bundle, mNewSS.getOperatorNumeric())
-                    || isNonRoamingInCdmaNetwork(bundle, systemId)) {
+            } else if (isNonRoamingInGsmNetwork(mCarrierConfig, mNewSS.getOperatorNumeric())
+                    || isNonRoamingInCdmaNetwork(mCarrierConfig, systemId)) {
                 log("updateRoamingState: carrier config override set non-roaming:"
                         + mNewSS.getOperatorNumeric() + ", " + systemId);
                 setRoamingOff();
-            } else if (isRoamingInGsmNetwork(bundle, mNewSS.getOperatorNumeric())
-                    || isRoamingInCdmaNetwork(bundle, systemId)) {
+            } else if (isRoamingInGsmNetwork(mCarrierConfig, mNewSS.getOperatorNumeric())
+                    || isRoamingInCdmaNetwork(mCarrierConfig, systemId)) {
                 log("updateRoamingState: carrier config override set roaming:"
                         + mNewSS.getOperatorNumeric() + ", " + systemId);
                 setRoamingOn();
@@ -2709,17 +2705,14 @@
         if (!mPhone.isPhoneTypeGsm() && !mSS.getRoaming()) {
             boolean hasBrandOverride = mUiccController.getUiccPort(getPhoneId()) != null
                     && mUiccController.getUiccPort(getPhoneId()).getOperatorBrandOverride() != null;
-            if (!hasBrandOverride) {
-                PersistableBundle config = getCarrierConfig();
-                if (config.getBoolean(
+            if (!hasBrandOverride && mCarrierConfig.getBoolean(
                         CarrierConfigManager.KEY_CDMA_HOME_REGISTERED_PLMN_NAME_OVERRIDE_BOOL)) {
-                    String operator = config.getString(
-                            CarrierConfigManager.KEY_CDMA_HOME_REGISTERED_PLMN_NAME_STRING);
-                    log("updateOperatorNameFromCarrierConfig: changing from "
-                            + mSS.getOperatorAlpha() + " to " + operator);
-                    // override long and short operator name, keeping numeric the same
-                    mSS.setOperatorName(operator, operator, mSS.getOperatorNumeric());
-                }
+                String operator = mCarrierConfig.getString(
+                        CarrierConfigManager.KEY_CDMA_HOME_REGISTERED_PLMN_NAME_STRING);
+                log("updateOperatorNameFromCarrierConfig: changing from "
+                        + mSS.getOperatorAlpha() + " to " + operator);
+                // override long and short operator name, keeping numeric the same
+                mSS.setOperatorName(operator, operator, mSS.getOperatorNumeric());
             }
         }
     }
@@ -2756,9 +2749,16 @@
             SubscriptionManager.putPhoneIdAndSubIdExtra(intent, mPhone.getPhoneId());
             mPhone.getContext().sendStickyBroadcastAsUser(intent, UserHandle.ALL);
 
-            if (!mSubscriptionController.setPlmnSpn(mPhone.getPhoneId(),
-                    data.shouldShowPlmn(), data.getPlmn(), data.shouldShowSpn(), data.getSpn())) {
-                mSpnUpdatePending = true;
+            if (mPhone.isSubscriptionManagerServiceEnabled()) {
+                mSubscriptionManagerService.setCarrierName(mPhone.getSubId(),
+                        TextUtils.emptyIfNull(getCarrierName(data.shouldShowPlmn(), data.getPlmn(),
+                                data.shouldShowSpn(), data.getSpn())));
+            } else {
+                if (!mSubscriptionController.setPlmnSpn(mPhone.getPhoneId(),
+                        data.shouldShowPlmn(), data.getPlmn(), data.shouldShowSpn(),
+                        data.getSpn())) {
+                    mSpnUpdatePending = true;
+                }
             }
         }
 
@@ -2770,6 +2770,26 @@
         mCurPlmn = data.getPlmn();
     }
 
+    @NonNull
+    private String getCarrierName(boolean showPlmn, String plmn, boolean showSpn, String spn) {
+        String carrierName = "";
+        if (showPlmn) {
+            carrierName = plmn;
+            if (showSpn) {
+                // Need to show both plmn and spn if both are not same.
+                if (!Objects.equals(spn, plmn)) {
+                    String separator = mPhone.getContext().getString(
+                            com.android.internal.R.string.kg_text_message_separator).toString();
+                    carrierName = new StringBuilder().append(carrierName).append(separator)
+                            .append(spn).toString();
+                }
+            }
+        } else if (showSpn) {
+            carrierName = spn;
+        }
+        return carrierName;
+    }
+
     private void updateSpnDisplayCdnr() {
         log("updateSpnDisplayCdnr+");
         CarrierDisplayNameData data = mCdnr.getCarrierDisplayNameData();
@@ -2780,8 +2800,8 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     @VisibleForTesting
     public void updateSpnDisplay() {
-        PersistableBundle config = getCarrierConfig();
-        if (config.getBoolean(CarrierConfigManager.KEY_ENABLE_CARRIER_DISPLAY_NAME_RESOLVER_BOOL)) {
+        if (mCarrierConfig.getBoolean(
+                CarrierConfigManager.KEY_ENABLE_CARRIER_DISPLAY_NAME_RESOLVER_BOOL)) {
             updateSpnDisplayCdnr();
         } else {
             updateSpnDisplayLegacy();
@@ -2794,8 +2814,8 @@
         String spn = null;
         String dataSpn = null;
         boolean showSpn = false;
-        String plmn = null;
-        boolean showPlmn = false;
+        String plmn;
+        boolean showPlmn;
 
         String wfcVoiceSpnFormat = null;
         String wfcDataSpnFormat = null;
@@ -2813,20 +2833,17 @@
             //
             // 2) Show PLMN + Wi-Fi Calling if there is no valid SPN in case 1
 
-            int voiceIdx = 0;
-            int dataIdx = 0;
-            int flightModeIdx = -1;
-            boolean useRootLocale = false;
+            int voiceIdx;
+            int dataIdx;
+            int flightModeIdx;
+            boolean useRootLocale;
 
-            PersistableBundle bundle = getCarrierConfig();
-
-            voiceIdx = bundle.getInt(CarrierConfigManager.KEY_WFC_SPN_FORMAT_IDX_INT);
-            dataIdx = bundle.getInt(
-                    CarrierConfigManager.KEY_WFC_DATA_SPN_FORMAT_IDX_INT);
-            flightModeIdx = bundle.getInt(
+            voiceIdx = mCarrierConfig.getInt(CarrierConfigManager.KEY_WFC_SPN_FORMAT_IDX_INT);
+            dataIdx = mCarrierConfig.getInt(CarrierConfigManager.KEY_WFC_DATA_SPN_FORMAT_IDX_INT);
+            flightModeIdx = mCarrierConfig.getInt(
                     CarrierConfigManager.KEY_WFC_FLIGHT_MODE_SPN_FORMAT_IDX_INT);
             useRootLocale =
-                    bundle.getBoolean(CarrierConfigManager.KEY_WFC_SPN_USE_ROOT_LOCALE);
+                    mCarrierConfig.getBoolean(CarrierConfigManager.KEY_WFC_SPN_USE_ROOT_LOCALE);
 
             String[] wfcSpnFormats = SubscriptionManager.getResourcesForSubId(mPhone.getContext(),
                     mPhone.getSubId(), useRootLocale)
@@ -2857,21 +2874,19 @@
                 && (mPhone.getImsPhone() != null)
                 && (mPhone.getImsPhone().getImsRegistrationTech()
                 == ImsRegistrationImplBase.REGISTRATION_TECH_CROSS_SIM)) {
-            // In Cros SIM Calling mode show SPN or PLMN + Cross SIM Calling
+            // In Cross SIM Calling mode show SPN or PLMN + Cross SIM Calling
             //
             // 1) Show SPN + Cross SIM Calling If SIM has SPN and SPN display condition
             //    is satisfied or SPN override is enabled for this carrier
             //
             // 2) Show PLMN + Cross SIM Calling if there is no valid SPN in case 1
-            PersistableBundle bundle = getCarrierConfig();
             int crossSimSpnFormatIdx =
-                    bundle.getInt(CarrierConfigManager.KEY_CROSS_SIM_SPN_FORMAT_INT);
+                    mCarrierConfig.getInt(CarrierConfigManager.KEY_CROSS_SIM_SPN_FORMAT_INT);
             boolean useRootLocale =
-                    bundle.getBoolean(CarrierConfigManager.KEY_WFC_SPN_USE_ROOT_LOCALE);
+                    mCarrierConfig.getBoolean(CarrierConfigManager.KEY_WFC_SPN_USE_ROOT_LOCALE);
 
             String[] crossSimSpnFormats = SubscriptionManager.getResourcesForSubId(
-                    mPhone.getContext(),
-                    mPhone.getSubId(), useRootLocale)
+                    mPhone.getContext(), mPhone.getSubId(), useRootLocale)
                     .getStringArray(R.array.crossSimSpnFormats);
 
             if (crossSimSpnFormatIdx < 0 || crossSimSpnFormatIdx >= crossSimSpnFormats.length) {
@@ -2901,7 +2916,6 @@
             //    EXTRA_SHOW_PLMN = true
             //    EXTRA_PLMN = null
 
-            IccRecords iccRecords = mIccRecords;
             int rule = getCarrierNameDisplayBitmask(mSS);
             boolean noService = false;
             if (combinedRegState == ServiceState.STATE_OUT_OF_SERVICE
@@ -2917,8 +2931,7 @@
                 } else {
                     // No service at all
                     plmn = Resources.getSystem()
-                            .getText(
-                                com.android.internal.R.string.lockscreen_carrier_default)
+                            .getText(com.android.internal.R.string.lockscreen_carrier_default)
                             .toString();
                     noService = true;
                 }
@@ -2961,8 +2974,7 @@
                 } else if (!TextUtils.isEmpty(plmn)) {
                     // Show PLMN + Cross-SIM Calling if there is no valid SPN in the above case
                     String originalPlmn = plmn.trim();
-                    PersistableBundle config = getCarrierConfig();
-                    if (mIccRecords != null && config.getBoolean(
+                    if (mIccRecords != null && mCarrierConfig.getBoolean(
                             CarrierConfigManager.KEY_WFC_CARRIER_NAME_OVERRIDE_BY_PNN_BOOL)) {
                         originalPlmn = mIccRecords.getPnnHomeName();
                     }
@@ -2987,8 +2999,7 @@
                 // Show PLMN + Wi-Fi Calling if there is no valid SPN in the above case
                 String originalPlmn = plmn.trim();
 
-                PersistableBundle config = getCarrierConfig();
-                if (mIccRecords != null && config.getBoolean(
+                if (mIccRecords != null && mCarrierConfig.getBoolean(
                         CarrierConfigManager.KEY_WFC_CARRIER_NAME_OVERRIDE_BY_PNN_BOOL)) {
                     originalPlmn = mIccRecords.getPnnHomeName();
                 }
@@ -3071,12 +3082,12 @@
     protected void setPowerStateToDesired(boolean forEmergencyCall,
             boolean isSelectedPhoneForEmergencyCall, boolean forceApply) {
         if (DBG) {
-            String tmpLog = "setPowerStateToDesired: mDeviceShuttingDown=" + mDeviceShuttingDown +
-                    ", mDesiredPowerState=" + mDesiredPowerState +
-                    ", getRadioState=" + mCi.getRadioState() +
-                    ", mRadioDisabledByCarrier=" + mRadioDisabledByCarrier +
-                    ", IMS reg state=" + mImsRegistrationOnOff +
-                    ", pending radio off=" + hasMessages(EVENT_POWER_OFF_RADIO_IMS_DEREG_TIMEOUT);
+            String tmpLog = "setPowerStateToDesired: mDeviceShuttingDown=" + mDeviceShuttingDown
+                    + ", mDesiredPowerState=" + mDesiredPowerState
+                    + ", getRadioState=" + mCi.getRadioState()
+                    + ", mRadioPowerOffReasons=" + mRadioPowerOffReasons
+                    + ", IMS reg state=" + mImsRegistrationOnOff
+                    + ", pending radio off=" + hasMessages(EVENT_POWER_OFF_RADIO_IMS_DEREG_TIMEOUT);
             log(tmpLog);
             mRadioPowerLog.log(tmpLog);
         }
@@ -3088,10 +3099,10 @@
         }
 
         // If we want it on and it's off, turn it on
-        if (mDesiredPowerState && !mRadioDisabledByCarrier
+        if (mDesiredPowerState && mRadioPowerOffReasons.isEmpty()
                 && (forceApply || mCi.getRadioState() == TelephonyManager.RADIO_POWER_OFF)) {
             mCi.setRadioPower(true, forEmergencyCall, isSelectedPhoneForEmergencyCall, null);
-        } else if ((!mDesiredPowerState || mRadioDisabledByCarrier) && mCi.getRadioState()
+        } else if ((!mDesiredPowerState || !mRadioPowerOffReasons.isEmpty()) && mCi.getRadioState()
                 == TelephonyManager.RADIO_POWER_ON) {
             if (DBG) log("setPowerStateToDesired: powerOffRadioSafely()");
             powerOffRadioSafely();
@@ -3108,7 +3119,6 @@
 
     /**
      * Cancel the EVENT_POWER_OFF_RADIO_DELAYED event if it is currently pending to be completed.
-     * @return true if there was a pending timeout message in the queue, false otherwise.
      */
     private void cancelDelayRadioOffWaitingForImsDeregTimeout() {
         if (hasMessages(EVENT_POWER_OFF_RADIO_IMS_DEREG_TIMEOUT)) {
@@ -3117,21 +3127,6 @@
         }
     }
 
-    /**
-     * Start a timer to turn off the radio if IMS does not move to deregistered after the
-     * radio power off event occurred. If this event already exists in the message queue, then
-     * ignore the new request and use the existing one.
-     */
-    private void startDelayRadioOffWaitingForImsDeregTimeout() {
-        if (hasMessages(EVENT_POWER_OFF_RADIO_IMS_DEREG_TIMEOUT)) {
-            if (DBG) log("startDelayRadioOffWaitingForImsDeregTimeout: timer exists, ignoring");
-            return;
-        }
-        if (DBG) log("startDelayRadioOffWaitingForImsDeregTimeout: starting timer");
-        sendEmptyMessageDelayed(EVENT_POWER_OFF_RADIO_IMS_DEREG_TIMEOUT,
-                getRadioPowerOffDelayTimeoutForImsRegistration());
-    }
-
     protected void onUpdateIccAvailability() {
         if (mUiccController == null ) {
             return;
@@ -3139,7 +3134,7 @@
 
         UiccCardApplication newUiccApplication = getUiccCardApplication();
 
-        if (mUiccApplcation != newUiccApplication) {
+        if (mUiccApplication != newUiccApplication) {
 
             // Remove the EF records that come from UICC
             if (mIccRecords instanceof SIMRecords) {
@@ -3148,26 +3143,26 @@
                 mCdnr.updateEfFromRuim(null /* ruim */);
             }
 
-            if (mUiccApplcation != null) {
+            if (mUiccApplication != null) {
                 log("Removing stale icc objects.");
-                mUiccApplcation.unregisterForReady(this);
+                mUiccApplication.unregisterForReady(this);
                 if (mIccRecords != null) {
                     mIccRecords.unregisterForRecordsLoaded(this);
                 }
                 mIccRecords = null;
-                mUiccApplcation = null;
+                mUiccApplication = null;
             }
             if (newUiccApplication != null) {
                 log("New card found");
-                mUiccApplcation = newUiccApplication;
-                mIccRecords = mUiccApplcation.getIccRecords();
+                mUiccApplication = newUiccApplication;
+                mIccRecords = mUiccApplication.getIccRecords();
                 if (mPhone.isPhoneTypeGsm()) {
-                    mUiccApplcation.registerForReady(this, EVENT_SIM_READY, null);
+                    mUiccApplication.registerForReady(this, EVENT_SIM_READY, null);
                     if (mIccRecords != null) {
                         mIccRecords.registerForRecordsLoaded(this, EVENT_SIM_RECORDS_LOADED, null);
                     }
                 } else if (mIsSubscriptionFromRuim) {
-                    mUiccApplcation.registerForReady(this, EVENT_RUIM_READY, null);
+                    mUiccApplication.registerForReady(this, EVENT_RUIM_READY, null);
                     if (mIccRecords != null) {
                         mIccRecords.registerForRecordsLoaded(this, EVENT_RUIM_RECORDS_LOADED, null);
                     }
@@ -3254,7 +3249,6 @@
                 + " mImsRegistrationOnOff=" + mImsRegistrationOnOff
                 + "}");
 
-
         if (mImsRegistrationOnOff && !registered) {
             // moving to deregistered, only send this event if we need to re-evaluate
             if (getRadioPowerOffDelayTimeoutForImsRegistration() > 0) {
@@ -3267,6 +3261,9 @@
             }
         }
         mImsRegistrationOnOff = registered;
+
+        // It's possible ServiceState changes did not trigger SPN display update; we update it here.
+        updateSpnDisplay();
     }
 
     public void onImsCapabilityChanged() {
@@ -3303,7 +3300,7 @@
                 nri = mNewSS.getNetworkRegistrationInfo(
                         NetworkRegistrationInfo.DOMAIN_PS,
                         AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
-                mNewSS.setOutOfService(mAccessNetworksManager.isInLegacyMode(), false);
+                mNewSS.setOutOfService(false);
                 // Add the IWLAN registration info back to service state.
                 if (nri != null) {
                     mNewSS.addNetworkRegistrationInfo(nri);
@@ -3320,7 +3317,7 @@
                 nri = mNewSS.getNetworkRegistrationInfo(
                         NetworkRegistrationInfo.DOMAIN_PS,
                         AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
-                mNewSS.setOutOfService(mAccessNetworksManager.isInLegacyMode(), true);
+                mNewSS.setOutOfService(true);
                 // Add the IWLAN registration info back to service state.
                 if (nri != null) {
                     mNewSS.addNetworkRegistrationInfo(nri);
@@ -3415,8 +3412,8 @@
         updateNrFrequencyRangeFromPhysicalChannelConfigs(mLastPhysicalChannelConfigList, mNewSS);
         updateNrStateFromPhysicalChannelConfigs(mLastPhysicalChannelConfigList, mNewSS);
 
-        if (TelephonyUtils.IS_DEBUGGABLE && mPhone.mTelephonyTester != null) {
-            mPhone.mTelephonyTester.overrideServiceState(mNewSS);
+        if (TelephonyUtils.IS_DEBUGGABLE && mPhone.getTelephonyTester() != null) {
+            mPhone.getTelephonyTester().overrideServiceState(mNewSS);
         }
 
         NetworkRegistrationInfo networkRegState = mNewSS.getNetworkRegistrationInfo(
@@ -3424,9 +3421,9 @@
         setPhyCellInfoFromCellIdentity(mNewSS, networkRegState.getCellIdentity());
 
         if (DBG) {
-            log("Poll ServiceState done: "
-                    + " oldSS=[" + mSS + "] newSS=[" + mNewSS + "]"
-                    + " oldMaxDataCalls=" + mMaxDataCalls
+            log("Poll ServiceState done: oldSS=" + mSS);
+            log("Poll ServiceState done: newSS=" + mNewSS);
+            log("Poll ServiceState done: oldMaxDataCalls=" + mMaxDataCalls
                     + " mNewMaxDataCalls=" + mNewMaxDataCalls
                     + " oldReasonDataDenied=" + mReasonDataDenied
                     + " mNewReasonDataDenied=" + mNewReasonDataDenied);
@@ -3447,14 +3444,10 @@
                 mSS.getState() == ServiceState.STATE_POWER_OFF
                         && mNewSS.getState() != ServiceState.STATE_POWER_OFF;
 
-        SparseBooleanArray hasDataAttached = new SparseBooleanArray(
-                mAccessNetworksManager.getAvailableTransports().length);
-        SparseBooleanArray hasDataDetached = new SparseBooleanArray(
-                mAccessNetworksManager.getAvailableTransports().length);
-        SparseBooleanArray hasRilDataRadioTechnologyChanged = new SparseBooleanArray(
-                mAccessNetworksManager.getAvailableTransports().length);
-        SparseBooleanArray hasDataRegStateChanged = new SparseBooleanArray(
-                mAccessNetworksManager.getAvailableTransports().length);
+        SparseBooleanArray hasDataAttached = new SparseBooleanArray();
+        SparseBooleanArray hasDataDetached = new SparseBooleanArray();
+        SparseBooleanArray hasRilDataRadioTechnologyChanged = new SparseBooleanArray();
+        SparseBooleanArray hasDataRegStateChanged = new SparseBooleanArray();
         boolean anyDataRegChanged = false;
         boolean anyDataRatChanged = false;
         boolean hasAlphaRawChanged =
@@ -3492,9 +3485,9 @@
                 anyDataRatChanged = true;
             }
 
-            int oldRegState = oldNrs != null ? oldNrs.getInitialRegistrationState()
+            int oldRegState = oldNrs != null ? oldNrs.getNetworkRegistrationState()
                     : NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN;
-            int newRegState = newNrs != null ? newNrs.getInitialRegistrationState()
+            int newRegState = newNrs != null ? newNrs.getNetworkRegistrationState()
                     : NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN;
             hasDataRegStateChanged.put(transport, oldRegState != newRegState);
             if (oldRegState != newRegState) {
@@ -3643,7 +3636,7 @@
         ServiceState oldMergedSS = new ServiceState(mPhone.getServiceState());
         mSS = new ServiceState(mNewSS);
 
-        mNewSS.setOutOfService(mAccessNetworksManager.isInLegacyMode(), false);
+        mNewSS.setOutOfService(false);
 
         mCellIdentity = primaryCellIdentity;
         if (mSS.getState() == ServiceState.STATE_IN_SERVICE && primaryCellIdentity != null) {
@@ -3689,10 +3682,6 @@
             mCssIndicatorChangedRegistrants.notifyRegistrants();
         }
 
-        if (hasBandwidthChanged) {
-            mBandwidthChangedRegistrants.notifyRegistrants();
-        }
-
         if (hasRejectCauseChanged) {
             setNotification(CS_REJECT_CAUSE_ENABLED);
         }
@@ -3941,8 +3930,9 @@
                 }
             }
 
-            if (mUiccApplcation != null && mUiccApplcation.getState() == AppState.APPSTATE_READY &&
-                    mIccRecords != null && getCombinedRegState(mSS) == ServiceState.STATE_IN_SERVICE
+            if (mUiccApplication != null && mUiccApplication.getState() == AppState.APPSTATE_READY
+                    && mIccRecords != null
+                    && getCombinedRegState(mSS) == ServiceState.STATE_IN_SERVICE
                     && !ServiceState.isPsOnlyTech(mSS.getRilVoiceRadioTechnology())) {
                 // SIM is found on the device. If ERI roaming is OFF, and SID/NID matches
                 // one configured in SIM, use operator name from CSIM record. Note that ERI, SID,
@@ -3973,10 +3963,9 @@
         }
 
         String carrierName = mIccRecords != null ? mIccRecords.getServiceProviderName() : "";
-        PersistableBundle config = getCarrierConfig();
-        if (config.getBoolean(CarrierConfigManager.KEY_CARRIER_NAME_OVERRIDE_BOOL)
+        if (mCarrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_NAME_OVERRIDE_BOOL)
                 || TextUtils.isEmpty(carrierName)) {
-            return config.getString(CarrierConfigManager.KEY_CARRIER_NAME_STRING);
+            return mCarrierConfig.getString(CarrierConfigManager.KEY_CARRIER_NAME_STRING);
         }
 
         return carrierName;
@@ -3995,7 +3984,6 @@
      */
     @CarrierNameDisplayBitmask
     public int getCarrierNameDisplayBitmask(ServiceState ss) {
-        PersistableBundle config = getCarrierConfig();
         if (!TextUtils.isEmpty(getOperatorBrandOverride())) {
             // If the operator has been overridden, all PLMNs will be considered HOME PLMNs, only
             // show SPN.
@@ -4005,7 +3993,7 @@
             // This is a hack from IccRecords#getServiceProviderName().
             return CARRIER_NAME_DISPLAY_BITMASK_SHOW_PLMN;
         } else {
-            boolean useRoamingFromServiceState = config.getBoolean(
+            boolean useRoamingFromServiceState = mCarrierConfig.getBoolean(
                     CarrierConfigManager.KEY_SPN_DISPLAY_RULE_USE_ROAMING_FROM_SERVICE_STATE_BOOL);
             int carrierDisplayNameConditionFromSim =
                     mIccRecords == null ? 0 : mIccRecords.getCarrierNameDisplayCondition();
@@ -4225,8 +4213,7 @@
     private boolean isOperatorConsideredNonRoaming(ServiceState s) {
         String operatorNumeric = s.getOperatorNumeric();
 
-        PersistableBundle config = getCarrierConfig();
-        String[] numericArray = config.getStringArray(
+        String[] numericArray = mCarrierConfig.getStringArray(
                 CarrierConfigManager.KEY_NON_ROAMING_OPERATOR_STRING_ARRAY);
 
         if (ArrayUtils.isEmpty(numericArray) || operatorNumeric == null) {
@@ -4243,8 +4230,7 @@
 
     private boolean isOperatorConsideredRoaming(ServiceState s) {
         String operatorNumeric = s.getOperatorNumeric();
-        PersistableBundle config = getCarrierConfig();
-        String[] numericArray = config.getStringArray(
+        String[] numericArray = mCarrierConfig.getStringArray(
                 CarrierConfigManager.KEY_ROAMING_OPERATOR_STRING_ARRAY);
         if (ArrayUtils.isEmpty(numericArray) || operatorNumeric == null) {
             return false;
@@ -4277,12 +4263,12 @@
                     ((state & RILConstants.RIL_RESTRICTED_STATE_CS_EMERGENCY) != 0) ||
                             ((state & RILConstants.RIL_RESTRICTED_STATE_CS_ALL) != 0) );
             //ignore the normal call and data restricted state before SIM READY
-            if (mUiccApplcation != null && mUiccApplcation.getState() == AppState.APPSTATE_READY) {
+            if (mUiccApplication != null
+                    && mUiccApplication.getState() == AppState.APPSTATE_READY) {
                 newRs.setCsNormalRestricted(
                         ((state & RILConstants.RIL_RESTRICTED_STATE_CS_NORMAL) != 0) ||
                                 ((state & RILConstants.RIL_RESTRICTED_STATE_CS_ALL) != 0) );
-                newRs.setPsRestricted(
-                        (state & RILConstants.RIL_RESTRICTED_STATE_PS_ALL)!= 0);
+                newRs.setPsRestricted((state & RILConstants.RIL_RESTRICTED_STATE_PS_ALL) != 0);
             }
 
             if (DBG) log("onRestrictedStateChanged: new rs "+ newRs);
@@ -4496,14 +4482,23 @@
         }
         Context context = mPhone.getContext();
 
-        SubscriptionInfo info = mSubscriptionController
-                .getActiveSubscriptionInfo(mPhone.getSubId(), context.getOpPackageName(),
-                        context.getAttributionTag());
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = mSubscriptionManagerService
+                    .getSubscriptionInfoInternal(mPhone.getSubId());
+            if (subInfo == null || !subInfo.isVisible()) {
+                log("cannot setNotification on invisible subid mSubId=" + mSubId);
+                return;
+            }
+        } else {
+            SubscriptionInfo info = mSubscriptionController
+                    .getActiveSubscriptionInfo(mPhone.getSubId(), context.getOpPackageName(),
+                            context.getAttributionTag());
 
-        //if subscription is part of a group and non-primary, suppress all notifications
-        if (info == null || (info.isOpportunistic() && info.getGroupUuid() != null)) {
-            log("cannot setNotification on invisible subid mSubId=" + mSubId);
-            return;
+            //if subscription is part of a group and non-primary, suppress all notifications
+            if (info == null || (info.isOpportunistic() && info.getGroupUuid() != null)) {
+                log("cannot setNotification on invisible subid mSubId=" + mSubId);
+                return;
+            }
         }
 
         // Needed because sprout RIL sends these when they shouldn't?
@@ -4514,10 +4509,8 @@
             return;
         }
 
-        boolean autoCancelCsRejectNotification = false;
-
-        PersistableBundle bundle = getCarrierConfig();
-        boolean disableVoiceBarringNotification = bundle.getBoolean(
+        boolean autoCancelCsRejectNotification;
+        boolean disableVoiceBarringNotification = mCarrierConfig.getBoolean(
                 CarrierConfigManager.KEY_DISABLE_VOICE_BARRING_NOTIFICATION_BOOL, false);
         if (disableVoiceBarringNotification && (notifyType == CS_ENABLED
                 || notifyType == CS_NORMAL_ENABLED
@@ -4525,7 +4518,7 @@
             if (DBG) log("Voice/emergency call barred notification disabled");
             return;
         }
-        autoCancelCsRejectNotification = bundle.getBoolean(
+        autoCancelCsRejectNotification = mCarrierConfig.getBoolean(
                 CarrierConfigManager.KEY_AUTO_CANCEL_CS_REJECT_NOTIFICATION, false);
 
         CharSequence details = "";
@@ -4535,7 +4528,7 @@
 
         final boolean multipleSubscriptions = (((TelephonyManager) mPhone.getContext()
                   .getSystemService(Context.TELEPHONY_SERVICE)).getPhoneCount() > 1);
-        final int simNumber = mSubscriptionController.getSlotIndex(mSubId) + 1;
+        int simNumber = SubscriptionManager.getSlotIndex(mSubId) + 1;
 
         switch (notifyType) {
             case PS_ENABLED:
@@ -4946,9 +4939,10 @@
      *
      * @param h handler to notify
      * @param what what code of message when delivered
+     * @param userobj the user obj that will be passed back when notify
      */
-    public void registerForServiceStateChanged(Handler h, int what) {
-        mServiceStateChangedRegistrants.addUnique(h, what, null);
+    public void registerForServiceStateChanged(Handler h, int what, Object userobj) {
+        mServiceStateChangedRegistrants.addUnique(h, what, userobj);
     }
 
     /**
@@ -5018,8 +5012,8 @@
     }
 
     private void onCarrierConfigChanged() {
-        PersistableBundle config = getCarrierConfig();
-        log("CarrierConfigChange " + config);
+        mCarrierConfig = getCarrierConfig();
+        log("CarrierConfigChange " + mCarrierConfig);
 
         // Load the ERI based on carrier config. Carrier might have their specific ERI.
         if (mEriManager != null) {
@@ -5027,8 +5021,8 @@
             mCdnr.updateEfForEri(getOperatorNameFromEri());
         }
 
-        updateOperatorNamePattern(config);
-        mCdnr.updateEfFromCarrierConfig(config);
+        updateOperatorNamePattern(mCarrierConfig);
+        mCdnr.updateEfFromCarrierConfig(mCarrierConfig);
         mPhone.notifyCallForwardingIndicator();
 
         // Sometimes the network registration information comes before carrier config is ready.
@@ -5086,8 +5080,8 @@
     // Determine if the Icc card exists
     private boolean iccCardExists() {
         boolean iccCardExist = false;
-        if (mUiccApplcation != null) {
-            iccCardExist = mUiccApplcation.getState() != AppState.APPSTATE_UNKNOWN;
+        if (mUiccApplication != null) {
+            iccCardExist = mUiccApplication.getState() != AppState.APPSTATE_UNKNOWN;
         }
         return iccCardExist;
     }
@@ -5290,7 +5284,7 @@
         pw.println(" mImsRegistrationOnOff=" + mImsRegistrationOnOff);
         pw.println(" pending radio off event="
                 + hasMessages(EVENT_POWER_OFF_RADIO_IMS_DEREG_TIMEOUT));
-        pw.println(" mRadioDisabledByCarrier" + mRadioDisabledByCarrier);
+        pw.println(" mRadioPowerOffReasons=" + mRadioPowerOffReasons);
         pw.println(" mDeviceShuttingDown=" + mDeviceShuttingDown);
         pw.println(" mSpnUpdatePending=" + mSpnUpdatePending);
         pw.println(" mCellInfoMinIntervalMs=" + mCellInfoMinIntervalMs);
@@ -5514,14 +5508,8 @@
         return mPhone.getPhoneId();
     }
 
-    /* Reset Service state when IWLAN is enabled as polling in airplane mode
-     * causes state to go to OUT_OF_SERVICE state instead of STATE_OFF
-     */
-
-
     /**
-     * This method adds IWLAN registration info for legacy mode devices camped on IWLAN. It also
-     * makes some adjustments when the device camps on IWLAN in airplane mode.
+     * This method makes some adjustments when the device camps on IWLAN in airplane mode.
      */
     private void processIwlanRegistrationInfo() {
         if (mCi.getRadioState() == TelephonyManager.RADIO_POWER_OFF) {
@@ -5535,7 +5523,7 @@
             }
             // operator info should be kept in SS
             String operator = mNewSS.getOperatorAlphaLong();
-            mNewSS.setOutOfService(mAccessNetworksManager.isInLegacyMode(), true);
+            mNewSS.setOutOfService(true);
             if (resetIwlanRatVal) {
                 mNewSS.setDataRegState(ServiceState.STATE_IN_SERVICE);
                 NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
@@ -5545,17 +5533,6 @@
                         .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
                         .build();
                 mNewSS.addNetworkRegistrationInfo(nri);
-                if (mAccessNetworksManager.isInLegacyMode()) {
-                    // If in legacy mode, simulate the behavior that IWLAN registration info
-                    // is reported through WWAN transport.
-                    nri = new NetworkRegistrationInfo.Builder()
-                            .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
-                            .setDomain(NetworkRegistrationInfo.DOMAIN_PS)
-                            .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_IWLAN)
-                            .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
-                            .build();
-                    mNewSS.addNetworkRegistrationInfo(nri);
-                }
                 mNewSS.setOperatorAlphaLong(operator);
                 // Since it's in airplane mode, cellular must be out of service. The only possible
                 // transport for data to go through is the IWLAN transport. Setting this to true
@@ -5565,31 +5542,6 @@
             }
             return;
         }
-
-        // If the device operates in legacy mode and camps on IWLAN, modem reports IWLAN as a RAT
-        // through WWAN registration info. To be consistent with the behavior with AP-assisted mode,
-        // we manually make a WLAN registration info for clients to consume. In this scenario,
-        // both WWAN and WLAN registration info are the IWLAN registration info and that's the
-        // unfortunate limitation we have when the device operates in legacy mode. In AP-assisted
-        // mode, the WWAN registration will correctly report the actual cellular registration info
-        // when the device camps on IWLAN.
-        if (mAccessNetworksManager.isInLegacyMode()) {
-            NetworkRegistrationInfo wwanNri = mNewSS.getNetworkRegistrationInfo(
-                    NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
-            if (wwanNri != null && wwanNri.getAccessNetworkTechnology()
-                    == TelephonyManager.NETWORK_TYPE_IWLAN) {
-                NetworkRegistrationInfo wlanNri = new NetworkRegistrationInfo.Builder()
-                        .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WLAN)
-                        .setDomain(NetworkRegistrationInfo.DOMAIN_PS)
-                        .setRegistrationState(wwanNri.getInitialRegistrationState())
-                        .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_IWLAN)
-                        .setRejectCause(wwanNri.getRejectCause())
-                        .setEmergencyOnly(wwanNri.isEmergencyEnabled())
-                        .setAvailableServices(wwanNri.getAvailableServices())
-                        .build();
-                mNewSS.addNetworkRegistrationInfo(wlanNri);
-            }
-        }
     }
 
     /**
@@ -5847,25 +5799,6 @@
     }
 
     /**
-     * Registers for cell bandwidth changed.
-     * @param h handler to notify
-     * @param what what code of message when delivered
-     * @param obj placed in Message.obj
-     */
-    public void registerForBandwidthChanged(Handler h, int what, Object obj) {
-        Registrant r = new Registrant(h, what, obj);
-        mBandwidthChangedRegistrants.add(r);
-    }
-
-    /**
-     * Unregisters for cell bandwidth changed.
-     * @param h handler to notify
-     */
-    public void unregisterForBandwidthChanged(Handler h) {
-        mBandwidthChangedRegistrants.remove(h);
-    }
-
-    /**
      * Get the NR data connection context ids.
      *
      * @return data connection context ids.
diff --git a/src/java/com/android/internal/telephony/SignalStrengthController.java b/src/java/com/android/internal/telephony/SignalStrengthController.java
index d950a9d..0c32f83 100644
--- a/src/java/com/android/internal/telephony/SignalStrengthController.java
+++ b/src/java/com/android/internal/telephony/SignalStrengthController.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
+
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.content.BroadcastReceiver;
@@ -46,6 +48,7 @@
 import android.util.Pair;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.util.ArrayUtils;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.telephony.Rlog;
@@ -357,9 +360,16 @@
                 || (curTime - mSignalStrengthUpdatedTime > SIGNAL_STRENGTH_REFRESH_THRESHOLD_IN_MS);
         if (!isStale) return false;
 
-        List<SubscriptionInfo> subInfoList = SubscriptionController.getInstance()
-                .getActiveSubscriptionInfoList(mPhone.getContext().getOpPackageName(),
-                        mPhone.getContext().getAttributionTag());
+        List<SubscriptionInfo> subInfoList;
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            subInfoList = SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
+                    mPhone.getContext().getOpPackageName(),
+                    mPhone.getContext().getAttributionTag());
+        } else {
+            subInfoList = SubscriptionController.getInstance()
+                    .getActiveSubscriptionInfoList(mPhone.getContext().getOpPackageName(),
+                            mPhone.getContext().getAttributionTag());
+        }
 
         if (!ArrayUtils.isEmpty(subInfoList)) {
             for (SubscriptionInfo info : subInfoList) {
@@ -423,7 +433,7 @@
                             (lteMeasurementEnabled & CellSignalStrengthLte.USE_RSRP) != 0));
         }
 
-        if (mPhone.getHalVersion().greaterOrEqual(RIL.RADIO_HAL_VERSION_1_5)) {
+        if (mPhone.getHalVersion(HAL_SERVICE_NETWORK).greaterOrEqual(RIL.RADIO_HAL_VERSION_1_5)) {
             int[] lteRsrqThresholds = mCarrierConfig.getIntArray(
                     CarrierConfigManager.KEY_LTE_RSRQ_THRESHOLDS_INT_ARRAY);
             if (lteRsrqThresholds != null) {
@@ -480,6 +490,18 @@
                                 AccessNetworkConstants.AccessNetworkType.NGRAN,
                                 (nrMeasurementEnabled & CellSignalStrengthNr.USE_SSSINR) != 0));
             }
+
+            int[] wcdmaEcnoThresholds = mCarrierConfig.getIntArray(
+                    CarrierConfigManager.KEY_WCDMA_ECNO_THRESHOLDS_INT_ARRAY);
+            if (wcdmaEcnoThresholds != null) {
+                signalThresholdInfos.add(
+                        createSignalThresholdsInfo(
+                                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_ECNO,
+                                wcdmaEcnoThresholds,
+                                AccessNetworkConstants.AccessNetworkType.UTRAN,
+                                false));
+            }
+
         }
 
         consolidatedAndSetReportingCriteria(signalThresholdInfos);
@@ -513,7 +535,7 @@
                         AccessNetworkConstants.AccessNetworkType.CDMA2000,
                         true));
 
-        if (mPhone.getHalVersion().greaterOrEqual(RIL.RADIO_HAL_VERSION_1_5)) {
+        if (mPhone.getHalVersion(HAL_SERVICE_NETWORK).greaterOrEqual(RIL.RADIO_HAL_VERSION_1_5)) {
             signalThresholdInfos.add(
                     createSignalThresholdsInfo(
                             SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ,
@@ -546,6 +568,12 @@
                             AccessNetworkThresholds.NGRAN_SSSINR,
                             AccessNetworkConstants.AccessNetworkType.NGRAN,
                             false));
+            signalThresholdInfos.add(
+                    createSignalThresholdsInfo(
+                            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_ECNO,
+                            AccessNetworkThresholds.UTRAN_ECNO,
+                            AccessNetworkConstants.AccessNetworkType.UTRAN,
+                            false));
         }
 
         consolidatedAndSetReportingCriteria(signalThresholdInfos);
@@ -574,12 +602,14 @@
                             measurementType,
                             mPhone.getSubId(),
                             mPhone.isDeviceIdle());
+            int hysteresisDb = getMinimumHysteresisDb(isEnabledForAppRequest, ran, measurementType,
+                    consolidatedThresholds);
             consolidatedSignalThresholdInfos.add(
                     new SignalThresholdInfo.Builder()
                             .setRadioAccessNetworkType(ran)
                             .setSignalMeasurementType(measurementType)
                             .setHysteresisMs(REPORTING_HYSTERESIS_MILLIS)
-                            .setHysteresisDb(REPORTING_HYSTERESIS_DB)
+                            .setHysteresisDb(hysteresisDb)
                             .setThresholds(consolidatedThresholds, true /*isSystem*/)
                             .setIsEnabled(isEnabledForSystem || isEnabledForAppRequest)
                             .build());
@@ -590,6 +620,126 @@
                         + consolidatedSignalThresholdInfos);
     }
 
+    /**
+     * Return the minimum hysteresis dB from all available sources:
+     * - system default
+     * - value set by client through API
+     * - threshold delta
+     */
+    @VisibleForTesting
+    public int getMinimumHysteresisDb(boolean isEnabledForAppRequest, int ran, int measurementType,
+              final int[] consolidatedThresholdList) {
+
+        int currHysteresisDb = getHysteresisDbFromCarrierConfig(ran, measurementType);
+
+        if (isEnabledForAppRequest) {
+            // Get minimum hysteresisDb at api
+            int apiHysteresisDb =
+                    getHysteresisDbFromSignalThresholdInfoRequests(ran, measurementType);
+
+            // Choose minimum of hysteresisDb between api Vs current system/cc value set
+            currHysteresisDb = Math.min(currHysteresisDb, apiHysteresisDb);
+
+            // Hal Req: choose hysteresis db value to be smaller of smallest of threshold delta
+            currHysteresisDb =  computeHysteresisDbOnSmallestThresholdDelta(
+                    currHysteresisDb, consolidatedThresholdList);
+        }
+        return currHysteresisDb;
+    }
+
+    /**
+     * Get the hysteresis db value from Signal Requests
+     * Note: Based on the current use case, there does not exist multile App signal threshold info
+     * requests with hysteresis db value, so this logic picks the latest hysteresis db value set.
+     *
+     * TODO(b/262655157): Support Multiple App Hysteresis DB value customisation
+     */
+    private int getHysteresisDbFromSignalThresholdInfoRequests(
+            @AccessNetworkConstants.RadioAccessNetworkType int ran,
+            @SignalThresholdInfo.SignalMeasurementType int measurement) {
+        int apiHysteresisDb = REPORTING_HYSTERESIS_DB;
+        for (SignalRequestRecord record : mSignalRequestRecords) {
+            for (SignalThresholdInfo info : record.mRequest.getSignalThresholdInfos()) {
+                if (isRanAndSignalMeasurementTypeMatch(ran, measurement, info)) {
+                    if (info.getHysteresisDb() >= 0) {
+                        apiHysteresisDb = info.getHysteresisDb();
+                    }
+                }
+            }
+        }
+        return apiHysteresisDb;
+    }
+
+    private int getHysteresisDbFromCarrierConfig(int ran, int measurement) {
+        int configHysteresisDb = REPORTING_HYSTERESIS_DB;
+        String configKey = null;
+
+        switch (ran) {
+            case AccessNetworkConstants.AccessNetworkType.GERAN:
+                if (measurement == SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI) {
+                    configKey = CarrierConfigManager.KEY_GERAN_RSSI_HYSTERESIS_DB_INT;
+                }
+                break;
+            case AccessNetworkConstants.AccessNetworkType.UTRAN:
+                if (measurement == SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP) {
+                    configKey = CarrierConfigManager.KEY_UTRAN_RSCP_HYSTERESIS_DB_INT;
+                } else if (measurement == SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_ECNO) {
+                    configKey = CarrierConfigManager.KEY_UTRAN_ECNO_HYSTERESIS_DB_INT;
+                }
+                break;
+            case AccessNetworkConstants.AccessNetworkType.EUTRAN:
+                if (measurement == SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP) {
+                    configKey = CarrierConfigManager.KEY_EUTRAN_RSRP_HYSTERESIS_DB_INT;
+                } else if (measurement == SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ) {
+                    configKey = CarrierConfigManager.KEY_EUTRAN_RSRQ_HYSTERESIS_DB_INT;
+                } else if (measurement == SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR) {
+                    configKey = CarrierConfigManager.KEY_EUTRAN_RSSNR_HYSTERESIS_DB_INT;
+                }
+                break;
+            case AccessNetworkConstants.AccessNetworkType.NGRAN:
+                if (measurement == SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP) {
+                    configKey = CarrierConfigManager.KEY_NGRAN_SSRSRP_HYSTERESIS_DB_INT;
+                } else if (measurement == SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ) {
+                    configKey = CarrierConfigManager.KEY_NGRAN_SSRSRQ_HYSTERESIS_DB_INT;
+                } else if (measurement == SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR) {
+                    configKey = CarrierConfigManager.KEY_NGRAN_SSSINR_HYSTERESIS_DB_INT;
+                }
+                break;
+            default:
+                localLog("No matching configuration");
+        }
+        if (configKey != null) {
+            configHysteresisDb = mCarrierConfig.getInt(configKey, REPORTING_HYSTERESIS_DB);
+        }
+        return configHysteresisDb >= SignalThresholdInfo.HYSTERESIS_DB_MINIMUM
+                ? configHysteresisDb : REPORTING_HYSTERESIS_DB;
+    }
+
+    /**
+     * This method computes the hysteresis db value between smaller of the smallest Threshold Delta
+     * and system / cc / api hysteresis db value determined.
+     *
+     * @param currMinHysteresisDb  smaller value between system / cc / api hysteresis db value
+     * @param signalThresholdInfoArray consolidated threshold info with App request consolidated.
+     * @return current minimum hysteresis db value computed between above params.
+     *
+     */
+    private int computeHysteresisDbOnSmallestThresholdDelta(
+            int currMinHysteresisDb, final int[] signalThresholdInfoArray) {
+        int index = 0;
+        if (signalThresholdInfoArray.length > 1) {
+            while (index != signalThresholdInfoArray.length - 1) {
+                if (signalThresholdInfoArray[index + 1] - signalThresholdInfoArray[index]
+                        < currMinHysteresisDb) {
+                    currMinHysteresisDb =
+                            signalThresholdInfoArray[index + 1] - signalThresholdInfoArray[index];
+                }
+                index++;
+            }
+        }
+        return currMinHysteresisDb;
+    }
+
     void setSignalStrengthDefaultValues() {
         mSignalStrength = new SignalStrength();
         mSignalStrengthUpdatedTime = System.currentTimeMillis();
@@ -1108,6 +1258,16 @@
                 15, /* SIGNAL_STRENGTH_GOOD */
                 30  /* SIGNAL_STRENGTH_GREAT */
         };
+
+        /**
+         * List of dBm thresholds for UTRAN {@link AccessNetworkConstants.AccessNetworkType} ECNO
+         */
+        public static final int[] UTRAN_ECNO = new int[]{
+                -24, /* SIGNAL_STRENGTH_POOR */
+                -14, /* SIGNAL_STRENGTH_MODERATE */
+                -6, /* SIGNAL_STRENGTH_GOOD */
+                1  /* SIGNAL_STRENGTH_GREAT */
+        };
     }
 
     private static void log(String msg) {
diff --git a/src/java/com/android/internal/telephony/SimIndication.java b/src/java/com/android/internal/telephony/SimIndication.java
index 20f89da..d74a9b1 100644
--- a/src/java/com/android/internal/telephony/SimIndication.java
+++ b/src/java/com/android/internal/telephony/SimIndication.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_SIM;
+
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CARRIER_INFO_IMSI_ENCRYPTION;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_RESPONSE_SIM_PHONEBOOK_CHANGED;
@@ -53,7 +55,7 @@
      * @param indicationType Type of radio indication
      */
     public void carrierInfoForImsiEncryption(int indicationType) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         if (mRil.isLogOrTrace()) {
             mRil.unsljLogRet(RIL_UNSOL_CARRIER_INFO_IMSI_ENCRYPTION, null);
@@ -69,7 +71,7 @@
      * @param cdmaSource New CdmaSubscriptionSource
      */
     public void cdmaSubscriptionSourceChanged(int indicationType, int cdmaSource) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         int[] response = new int[]{cdmaSource};
         if (mRil.isLogOrTrace()) {
@@ -85,7 +87,7 @@
      * @param indicationType Type of radio indication
      */
     public void simPhonebookChanged(int indicationType) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         if (mRil.isLogOrTrace()) {
             mRil.unsljLog(RIL_UNSOL_RESPONSE_SIM_PHONEBOOK_CHANGED);
@@ -102,7 +104,7 @@
      */
     public void simPhonebookRecordsReceived(int indicationType, byte status,
             android.hardware.radio.sim.PhonebookRecordInfo[] records) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         List<SimPhonebookRecord> simPhonebookRecords = new ArrayList<>();
 
@@ -127,7 +129,7 @@
      */
     public void simRefresh(int indicationType,
             android.hardware.radio.sim.SimRefreshResult refreshResult) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         IccRefreshResponse response = new IccRefreshResponse();
         response.refreshResult = refreshResult.type;
@@ -144,7 +146,7 @@
      * @param indicationType Type of radio indication
      */
     public void simStatusChanged(int indicationType) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED);
 
@@ -159,7 +161,7 @@
      *        Refer to TS 102.223 section 9.4 for command types
      */
     public void stkEventNotify(int indicationType, String cmd) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_STK_EVENT_NOTIFY);
 
@@ -175,7 +177,7 @@
      *        Refer to TS 102.223 section 9.4 for command types
      */
     public void stkProactiveCommand(int indicationType, String cmd) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_STK_PROACTIVE_COMMAND);
 
@@ -189,7 +191,7 @@
      * @param indicationType Type of radio indication
      */
     public void stkSessionEnd(int indicationType) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_STK_SESSION_END);
 
@@ -204,7 +206,7 @@
      * @param activate false for subscription deactivated, true for subscription activated
      */
     public void subscriptionStatusChanged(int indicationType, boolean activate) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         int[] response = new int[]{activate ? 1 : 0};
 
@@ -222,7 +224,7 @@
      * @param enabled Whether uiccApplications are enabled or disabled
      */
     public void uiccApplicationsEnablementChanged(int indicationType, boolean enabled) {
-        mRil.processIndication(RIL.SIM_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_SIM, indicationType);
 
         if (mRil.isLogOrTrace()) {
             mRil.unsljLogRet(RIL_UNSOL_UICC_APPLICATIONS_ENABLEMENT_CHANGED, enabled);
diff --git a/src/java/com/android/internal/telephony/SimResponse.java b/src/java/com/android/internal/telephony/SimResponse.java
index b0099fb..5f05c58 100644
--- a/src/java/com/android/internal/telephony/SimResponse.java
+++ b/src/java/com/android/internal/telephony/SimResponse.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_SIM;
+
 import android.hardware.radio.RadioError;
 import android.hardware.radio.RadioResponseInfo;
 import android.hardware.radio.sim.IRadioSimResponse;
@@ -41,7 +43,7 @@
 
     private void responseIccIo(RadioResponseInfo responseInfo,
             android.hardware.radio.sim.IccIoResult result) {
-        RILRequest rr = mRil.processResponse(RIL.SIM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_SIM, responseInfo);
 
         if (rr != null) {
             IccIoResult ret = new IccIoResult(result.sw1, result.sw2, result.simResponse);
@@ -68,7 +70,7 @@
      */
     public void areUiccApplicationsEnabledResponse(RadioResponseInfo responseInfo,
             boolean enabled) {
-        RILRequest rr = mRil.processResponse(RIL.SIM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_SIM, responseInfo);
 
         if (rr != null) {
             if (responseInfo.error == RadioError.NONE) {
@@ -83,7 +85,7 @@
      * @param remainingAttempts Number of retries remaining, must be equal to -1 if unknown.
      */
     public void changeIccPin2ForAppResponse(RadioResponseInfo responseInfo, int remainingAttempts) {
-        RadioResponse.responseInts(RIL.SIM_SERVICE, mRil, responseInfo, remainingAttempts);
+        RadioResponse.responseInts(HAL_SERVICE_SIM, mRil, responseInfo, remainingAttempts);
     }
 
     /**
@@ -91,14 +93,14 @@
      * @param remainingAttempts Number of retries remaining, must be equal to -1 if unknown.
      */
     public void changeIccPinForAppResponse(RadioResponseInfo responseInfo, int remainingAttempts) {
-        RadioResponse.responseInts(RIL.SIM_SERVICE, mRil, responseInfo, remainingAttempts);
+        RadioResponse.responseInts(HAL_SERVICE_SIM, mRil, responseInfo, remainingAttempts);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error.
      */
     public void enableUiccApplicationsResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.SIM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_SIM, mRil, responseInfo);
     }
 
     /**
@@ -109,7 +111,7 @@
     public void getAllowedCarriersResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.sim.CarrierRestrictions carrierRestrictions,
             int multiSimPolicy) {
-        RILRequest rr = mRil.processResponse(RIL.SIM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_SIM, responseInfo);
         if (rr == null) {
             return;
         }
@@ -154,7 +156,7 @@
     public void getCdmaSubscriptionResponse(RadioResponseInfo responseInfo, String mdn,
             String hSid, String hNid, String min, String prl) {
         RadioResponse.responseStrings(
-                RIL.SIM_SERVICE, mRil, responseInfo, mdn, hSid, hNid, min, prl);
+                HAL_SERVICE_SIM, mRil, responseInfo, mdn, hSid, hNid, min, prl);
     }
 
     /**
@@ -162,7 +164,7 @@
      * @param source CDMA subscription source
      */
     public void getCdmaSubscriptionSourceResponse(RadioResponseInfo responseInfo, int source) {
-        RadioResponse.responseInts(RIL.SIM_SERVICE, mRil, responseInfo, source);
+        RadioResponse.responseInts(HAL_SERVICE_SIM, mRil, responseInfo, source);
     }
 
     /**
@@ -171,7 +173,7 @@
      *        specified barring facility is active. "0" means "disabled for all"
      */
     public void getFacilityLockForAppResponse(RadioResponseInfo responseInfo, int response) {
-        RadioResponse.responseInts(RIL.SIM_SERVICE, mRil, responseInfo, response);
+        RadioResponse.responseInts(HAL_SERVICE_SIM, mRil, responseInfo, response);
     }
 
     /**
@@ -180,7 +182,7 @@
      */
     public void getIccCardStatusResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.sim.CardStatus cardStatus) {
-        RILRequest rr = mRil.processResponse(RIL.SIM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_SIM, responseInfo);
 
         if (rr != null) {
             IccCardStatus iccCardStatus = RILUtils.convertHalCardStatus(cardStatus);
@@ -197,7 +199,7 @@
      * @param imsi String containing the IMSI
      */
     public void getImsiForAppResponse(RadioResponseInfo responseInfo, String imsi) {
-        RadioResponse.responseString(RIL.SIM_SERVICE, mRil, responseInfo, imsi);
+        RadioResponse.responseString(HAL_SERVICE_SIM, mRil, responseInfo, imsi);
     }
 
     /**
@@ -207,7 +209,7 @@
     public void getSimPhonebookCapacityResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.sim.PhonebookCapacity pbCapacity) {
         AdnCapacity capacity = RILUtils.convertHalPhonebookCapacity(pbCapacity);
-        RILRequest rr = mRil.processResponse(RIL.SIM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_SIM, responseInfo);
         if (rr != null) {
             if (responseInfo.error == RadioError.NONE) {
                 RadioResponse.sendMessageResponse(rr.mResult, capacity);
@@ -220,14 +222,14 @@
      * @param responseInfo Response info struct containing response type, serial no. and error.
      */
     public void getSimPhonebookRecordsResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.SIM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_SIM, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void iccCloseLogicalChannelResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.SIM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_SIM, mRil, responseInfo);
     }
 
     /**
@@ -252,7 +254,7 @@
         for (int i = 0; i < selectResponse.length; i++) {
             arr.add((int) selectResponse[i]);
         }
-        RadioResponse.responseIntArrayList(RIL.SIM_SERVICE, mRil, responseInfo, arr);
+        RadioResponse.responseIntArrayList(HAL_SERVICE_SIM, mRil, responseInfo, arr);
     }
 
     /**
@@ -277,7 +279,7 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void reportStkServiceIsRunningResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.SIM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_SIM, mRil, responseInfo);
     }
 
     /**
@@ -286,7 +288,7 @@
      */
     public void requestIccSimAuthenticationResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.sim.IccIoResult iccIo) {
-        RILRequest rr = mRil.processResponse(RIL.SIM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_SIM, responseInfo);
 
         if (rr != null) {
             IccIoResult ret = new IccIoResult(iccIo.sw1, iccIo.sw2,
@@ -315,7 +317,7 @@
      *        string starting with first byte of response
      */
     public void sendEnvelopeResponse(RadioResponseInfo responseInfo, String commandResponse) {
-        RadioResponse.responseString(RIL.SIM_SERVICE, mRil, responseInfo, commandResponse);
+        RadioResponse.responseString(HAL_SERVICE_SIM, mRil, responseInfo, commandResponse);
     }
 
     /**
@@ -331,7 +333,7 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void sendTerminalResponseToSimResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.SIM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_SIM, mRil, responseInfo);
     }
 
     /**
@@ -339,7 +341,7 @@
      */
     public void setAllowedCarriersResponse(RadioResponseInfo responseInfo) {
         int ret = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
-        RILRequest rr = mRil.processResponse(RIL.SIM_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_SIM, responseInfo);
         if (rr != null) {
             mRil.riljLog("setAllowedCarriersResponse - error = " + responseInfo.error);
 
@@ -355,14 +357,14 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setCarrierInfoForImsiEncryptionResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.SIM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_SIM, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setCdmaSubscriptionSourceResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.SIM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_SIM, mRil, responseInfo);
     }
 
     /**
@@ -370,21 +372,21 @@
      * @param retry 0 is the number of retries remaining, or -1 if unknown
      */
     public void setFacilityLockForAppResponse(RadioResponseInfo responseInfo, int retry) {
-        RadioResponse.responseInts(RIL.SIM_SERVICE, mRil, responseInfo, retry);
+        RadioResponse.responseInts(HAL_SERVICE_SIM, mRil, responseInfo, retry);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setSimCardPowerResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.SIM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_SIM, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setUiccSubscriptionResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.SIM_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_SIM, mRil, responseInfo);
     }
 
     /**
@@ -392,7 +394,7 @@
      * @param remainingAttempts Number of retries remaining, must be equal to -1 if unknown.
      */
     public void supplyIccPin2ForAppResponse(RadioResponseInfo responseInfo, int remainingAttempts) {
-        RadioResponse.responseInts(RIL.SIM_SERVICE, mRil, responseInfo, remainingAttempts);
+        RadioResponse.responseInts(HAL_SERVICE_SIM, mRil, responseInfo, remainingAttempts);
     }
 
     /**
@@ -400,7 +402,7 @@
      * @param remainingAttempts Number of retries remaining, must be equal to -1 if unknown.
      */
     public void supplyIccPinForAppResponse(RadioResponseInfo responseInfo, int remainingAttempts) {
-        RadioResponse.responseInts(RIL.SIM_SERVICE, mRil, responseInfo, remainingAttempts);
+        RadioResponse.responseInts(HAL_SERVICE_SIM, mRil, responseInfo, remainingAttempts);
     }
 
     /**
@@ -408,7 +410,7 @@
      * @param remainingAttempts Number of retries remaining, must be equal to -1 if unknown.
      */
     public void supplyIccPuk2ForAppResponse(RadioResponseInfo responseInfo, int remainingAttempts) {
-        RadioResponse.responseInts(RIL.SIM_SERVICE, mRil, responseInfo, remainingAttempts);
+        RadioResponse.responseInts(HAL_SERVICE_SIM, mRil, responseInfo, remainingAttempts);
     }
 
     /**
@@ -416,7 +418,7 @@
      * @param remainingAttempts Number of retries remaining, must be equal to -1 if unknown.
      */
     public void supplyIccPukForAppResponse(RadioResponseInfo responseInfo, int remainingAttempts) {
-        RadioResponse.responseInts(RIL.SIM_SERVICE, mRil, responseInfo, remainingAttempts);
+        RadioResponse.responseInts(HAL_SERVICE_SIM, mRil, responseInfo, remainingAttempts);
     }
 
     /**
@@ -428,7 +430,7 @@
     public void supplySimDepersonalizationResponse(RadioResponseInfo responseInfo, int persoType,
             int remainingRetries) {
         RadioResponse.responseInts(
-                RIL.SIM_SERVICE, mRil, responseInfo, persoType, remainingRetries);
+                HAL_SERVICE_SIM, mRil, responseInfo, persoType, remainingRetries);
     }
 
     /**
@@ -437,7 +439,7 @@
      */
     public void updateSimPhonebookRecordsResponse(RadioResponseInfo responseInfo,
             int updatedRecordIndex) {
-        RadioResponse.responseInts(RIL.SIM_SERVICE, mRil, responseInfo, updatedRecordIndex);
+        RadioResponse.responseInts(HAL_SERVICE_SIM, mRil, responseInfo, updatedRecordIndex);
     }
 
     @Override
diff --git a/src/java/com/android/internal/telephony/SmsBroadcastUndelivered.java b/src/java/com/android/internal/telephony/SmsBroadcastUndelivered.java
index f5d9e9e..2f39958 100644
--- a/src/java/com/android/internal/telephony/SmsBroadcastUndelivered.java
+++ b/src/java/com/android/internal/telephony/SmsBroadcastUndelivered.java
@@ -34,6 +34,7 @@
 import com.android.internal.telephony.cdma.CdmaInboundSmsHandler;
 import com.android.internal.telephony.gsm.GsmInboundSmsHandler;
 import com.android.internal.telephony.metrics.TelephonyMetrics;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.telephony.Rlog;
 
 import java.util.HashMap;
@@ -261,8 +262,13 @@
     private static void broadcastSms(InboundSmsTracker tracker) {
         InboundSmsHandler handler;
         int subId = tracker.getSubId();
-        // TODO consider other subs in this subId's group as well
-        int phoneId = SubscriptionController.getInstance().getPhoneId(subId);
+        int phoneId;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            phoneId = SubscriptionManagerService.getInstance().getPhoneId(subId);
+        } else {
+            // TODO consider other subs in this subId's group as well
+            phoneId = SubscriptionController.getInstance().getPhoneId(subId);
+        }
         if (!SubscriptionManager.isValidPhoneId(phoneId)) {
             Rlog.e(TAG, "broadcastSms: ignoring message; no phone found for subId " + subId);
             return;
diff --git a/src/java/com/android/internal/telephony/SmsController.java b/src/java/com/android/internal/telephony/SmsController.java
index 9f79161..c33dbe2 100644
--- a/src/java/com/android/internal/telephony/SmsController.java
+++ b/src/java/com/android/internal/telephony/SmsController.java
@@ -18,6 +18,8 @@
 
 package com.android.internal.telephony;
 
+import static android.content.pm.PackageManager.PERMISSION_GRANTED;
+
 import static com.android.internal.telephony.util.TelephonyUtils.checkDumpPermission;
 
 import android.annotation.Nullable;
@@ -26,12 +28,14 @@
 import android.app.PendingIntent;
 import android.compat.annotation.UnsupportedAppUsage;
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.os.BaseBundle;
 import android.os.Binder;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.TelephonyServiceManager.ServiceRegisterer;
+import android.os.UserHandle;
 import android.provider.Telephony.Sms.Intents;
 import android.telephony.CarrierConfigManager;
 import android.telephony.SmsManager;
@@ -40,6 +44,8 @@
 import android.telephony.TelephonyFrameworkInitializer;
 import android.telephony.TelephonyManager;
 
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.telephony.Rlog;
 
@@ -47,6 +53,7 @@
 import java.io.PrintWriter;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
+import java.util.Locale;
 
 /**
  * Implements the ISmsImplBase interface used in the SmsManager API.
@@ -56,7 +63,8 @@
 
     private final Context mContext;
 
-    protected SmsController(Context context) {
+    @VisibleForTesting
+    public SmsController(Context context) {
         mContext = context;
         ServiceRegisterer smsServiceRegisterer = TelephonyFrameworkInitializer
                 .getTelephonyServiceManager()
@@ -151,6 +159,22 @@
         if (callingPackage == null) {
             callingPackage = getCallingPackage();
         }
+        Rlog.d(LOG_TAG, "sendDataForSubscriber caller=" + callingPackage);
+
+        // Check if user is associated with the subscription
+        if (!TelephonyPermissions.checkSubscriptionAssociatedWithUser(mContext, subId,
+                Binder.getCallingUserHandle())) {
+            // TODO(b/258629881): Display error dialog.
+            sendErrorInPendingIntent(sentIntent, SmsManager.RESULT_USER_NOT_ALLOWED);
+            return;
+        }
+
+        // Perform FDN check
+        if (isNumberBlockedByFDN(subId, destAddr, callingPackage)) {
+            sendErrorInPendingIntent(sentIntent, SmsManager.RESULT_ERROR_FDN_CHECK_FAILURE);
+            return;
+        }
+
         IccSmsInterfaceManager iccSmsIntMgr = getIccSmsInterfaceManager(subId);
         if (iccSmsIntMgr != null) {
             iccSmsIntMgr.sendData(callingPackage, callingAttributionTag, destAddr, scAddr, destPort,
@@ -186,14 +210,68 @@
             String callingAttributionTag, String destAddr, String scAddr, String text,
             PendingIntent sentIntent, PendingIntent deliveryIntent,
             boolean persistMessageForNonDefaultSmsApp, long messageId) {
+        sendTextForSubscriber(subId, callingPackage, callingAttributionTag, destAddr, scAddr,
+                text, sentIntent, deliveryIntent, persistMessageForNonDefaultSmsApp, messageId,
+                false, false);
+
+    }
+
+    /**
+     * @param subId Subscription Id
+     * @param callingAttributionTag the attribution tag of the caller
+     * @param destAddr the address to send the message to
+     * @param scAddr is the service center address or null to use
+     *  the current default SMSC
+     * @param text the body of the message to send
+     * @param sentIntent if not NULL this <code>PendingIntent</code> is
+     *  broadcast when the message is successfully sent, or failed.
+     *  The result code will be <code>Activity.RESULT_OK</code> for success, or relevant errors
+     *  the sentIntent may include the extra "errorCode" containing a radio technology specific
+     *  value, generally only useful for troubleshooting.
+     * @param deliveryIntent if not NULL this <code>PendingIntent</code> is
+     *  broadcast when the message is delivered to the recipient.  The
+     *  raw pdu of the status report is in the extended data ("pdu").
+     * @param skipFdnCheck if set to true, FDN check must be skipped .This is set in case of STK sms
+     *
+     * @hide
+     */
+    public void sendTextForSubscriber(int subId, String callingPackage,
+            String callingAttributionTag, String destAddr, String scAddr, String text,
+            PendingIntent sentIntent, PendingIntent deliveryIntent,
+            boolean persistMessageForNonDefaultSmsApp, long messageId, boolean skipFdnCheck,
+            boolean skipShortCodeCheck) {
         if (callingPackage == null) {
             callingPackage = getCallingPackage();
         }
+        Rlog.d(LOG_TAG, "sendTextForSubscriber caller=" + callingPackage);
+
+        if (skipFdnCheck || skipShortCodeCheck) {
+            if (mContext.checkCallingOrSelfPermission(
+                    android.Manifest.permission.MODIFY_PHONE_STATE)
+                    != PackageManager.PERMISSION_GRANTED) {
+                throw new SecurityException("Requires MODIFY_PHONE_STATE permission.");
+            }
+        }
         if (!getSmsPermissions(subId).checkCallingCanSendText(persistMessageForNonDefaultSmsApp,
                 callingPackage, callingAttributionTag, "Sending SMS message")) {
             sendErrorInPendingIntent(sentIntent, SmsManager.RESULT_ERROR_GENERIC_FAILURE);
             return;
         }
+
+        // Check if user is associated with the subscription
+        if (!TelephonyPermissions.checkSubscriptionAssociatedWithUser(mContext, subId,
+                Binder.getCallingUserHandle())) {
+            // TODO(b/258629881): Display error dialog.
+            sendErrorInPendingIntent(sentIntent, SmsManager.RESULT_USER_NOT_ALLOWED);
+            return;
+        }
+
+        // Perform FDN check
+        if (!skipFdnCheck && isNumberBlockedByFDN(subId, destAddr, callingPackage)) {
+            sendErrorInPendingIntent(sentIntent, SmsManager.RESULT_ERROR_FDN_CHECK_FAILURE);
+            return;
+        }
+
         long token = Binder.clearCallingIdentity();
         SubscriptionInfo info;
         try {
@@ -201,11 +279,12 @@
         } finally {
             Binder.restoreCallingIdentity(token);
         }
+
         if (isBluetoothSubscription(info)) {
             sendBluetoothText(info, destAddr, text, sentIntent, deliveryIntent);
         } else {
             sendIccText(subId, callingPackage, destAddr, scAddr, text, sentIntent, deliveryIntent,
-                    persistMessageForNonDefaultSmsApp, messageId);
+                    persistMessageForNonDefaultSmsApp, messageId, skipShortCodeCheck);
         }
     }
 
@@ -222,16 +301,17 @@
 
     private void sendIccText(int subId, String callingPackage, String destAddr,
             String scAddr, String text, PendingIntent sentIntent, PendingIntent deliveryIntent,
-            boolean persistMessageForNonDefaultSmsApp, long messageId) {
+            boolean persistMessageForNonDefaultSmsApp, long messageId, boolean skipShortCodeCheck) {
         Rlog.d(LOG_TAG, "sendTextForSubscriber iccSmsIntMgr"
-                + " Subscription: " + subId + " id: " + messageId);
+                + " Subscription: " + subId + " " + formatCrossStackMessageId(messageId));
         IccSmsInterfaceManager iccSmsIntMgr = getIccSmsInterfaceManager(subId);
         if (iccSmsIntMgr != null) {
             iccSmsIntMgr.sendText(callingPackage, destAddr, scAddr, text, sentIntent,
-                    deliveryIntent, persistMessageForNonDefaultSmsApp, messageId);
+                    deliveryIntent, persistMessageForNonDefaultSmsApp, messageId,
+                    skipShortCodeCheck);
         } else {
             Rlog.e(LOG_TAG, "sendTextForSubscriber iccSmsIntMgr is null for"
-                    + " Subscription: " + subId + " id: " + messageId);
+                    + " Subscription: " + subId + " " + formatCrossStackMessageId(messageId));
             sendErrorInPendingIntent(sentIntent, SmsManager.RESULT_ERROR_GENERIC_FAILURE);
         }
     }
@@ -259,6 +339,22 @@
         if (callingPackage == null) {
             callingPackage = getCallingPackage();
         }
+        Rlog.d(LOG_TAG, "sendTextForSubscriberWithOptions caller=" + callingPackage);
+
+        // Check if user is associated with the subscription
+        if (!TelephonyPermissions.checkSubscriptionAssociatedWithUser(mContext, subId,
+                Binder.getCallingUserHandle())) {
+            // TODO(b/258629881): Display error dialog.
+            sendErrorInPendingIntent(sentIntent, SmsManager.RESULT_USER_NOT_ALLOWED);
+            return;
+        }
+
+        // Perform FDN check
+        if (isNumberBlockedByFDN(subId, destAddr, callingPackage)) {
+            sendErrorInPendingIntent(sentIntent, SmsManager.RESULT_ERROR_FDN_CHECK_FAILURE);
+            return;
+        }
+
         IccSmsInterfaceManager iccSmsIntMgr = getIccSmsInterfaceManager(subId);
         if (iccSmsIntMgr != null) {
             iccSmsIntMgr.sendTextWithOptions(callingPackage, callingAttributionTag, destAddr,
@@ -281,6 +377,22 @@
         if (getCallingPackage() != null) {
             callingPackage = getCallingPackage();
         }
+        Rlog.d(LOG_TAG, "sendMultipartTextForSubscriber caller=" + callingPackage);
+
+        // Check if user is associated with the subscription
+        if (!TelephonyPermissions.checkSubscriptionAssociatedWithUser(mContext, subId,
+                Binder.getCallingUserHandle())) {
+            // TODO(b/258629881): Display error dialog.
+            sendErrorInPendingIntents(sentIntents, SmsManager.RESULT_USER_NOT_ALLOWED);
+            return;
+        }
+
+        // Perform FDN check
+        if (isNumberBlockedByFDN(subId, destAddr, callingPackage)) {
+            sendErrorInPendingIntents(sentIntents, SmsManager.RESULT_ERROR_FDN_CHECK_FAILURE);
+            return;
+        }
+
         IccSmsInterfaceManager iccSmsIntMgr = getIccSmsInterfaceManager(subId);
         if (iccSmsIntMgr != null) {
             iccSmsIntMgr.sendMultipartText(callingPackage, callingAttributionTag, destAddr, scAddr,
@@ -288,7 +400,7 @@
                     messageId);
         } else {
             Rlog.e(LOG_TAG, "sendMultipartTextForSubscriber iccSmsIntMgr is null for"
-                    + " Subscription: " + subId + " id: " + messageId);
+                    + " Subscription: " + subId + " " + formatCrossStackMessageId(messageId));
             sendErrorInPendingIntents(sentIntents, SmsManager.RESULT_ERROR_GENERIC_FAILURE);
         }
     }
@@ -301,6 +413,22 @@
         if (callingPackage == null) {
             callingPackage = getCallingPackage();
         }
+        Rlog.d(LOG_TAG, "sendMultipartTextForSubscriberWithOptions caller=" + callingPackage);
+
+        // Check if user is associated with the subscription
+        if (!TelephonyPermissions.checkSubscriptionAssociatedWithUser(mContext, subId,
+                Binder.getCallingUserHandle())) {
+            // TODO(b/258629881): Display error dialog.
+            sendErrorInPendingIntents(sentIntents, SmsManager.RESULT_USER_NOT_ALLOWED);
+            return;
+        }
+
+        // Perform FDN check
+        if (isNumberBlockedByFDN(subId, destAddr, callingPackage)) {
+            sendErrorInPendingIntents(sentIntents, SmsManager.RESULT_ERROR_FDN_CHECK_FAILURE);
+            return;
+        }
+
         IccSmsInterfaceManager iccSmsIntMgr = getIccSmsInterfaceManager(subId);
         if (iccSmsIntMgr != null) {
             iccSmsIntMgr.sendMultipartTextWithOptions(callingPackage, callingAttributionTag,
@@ -409,7 +537,7 @@
         // Don't show the SMS SIM Pick activity if it is not foreground.
         boolean isCallingProcessForeground = am != null
                 && am.getUidImportance(Binder.getCallingUid())
-                        == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
+                == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
         if (!isCallingProcessForeground) {
             Rlog.d(LOG_TAG, "isSmsSimPickActivityNeeded: calling process not foreground. "
                     + "Suppressing activity.");
@@ -480,18 +608,32 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     @Override
     public int getPreferredSmsSubscription() {
-        // If there is a default, choose that one.
-        int defaultSubId = SubscriptionController.getInstance().getDefaultSmsSubId();
+        int defaultSubId;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            // If there is a default, choose that one.
+            defaultSubId = SubscriptionManagerService.getInstance().getDefaultSmsSubId();
+        } else {
+            // If there is a default, choose that one.
+            defaultSubId = SubscriptionController.getInstance().getDefaultSmsSubId();
+        }
         if (SubscriptionManager.isValidSubscriptionId(defaultSubId)) {
             return defaultSubId;
         }
         // No default, if there is only one sub active, choose that as the "preferred" sub id.
         long token = Binder.clearCallingIdentity();
         try {
-            int[] activeSubs = SubscriptionController.getInstance()
-                    .getActiveSubIdList(true /*visibleOnly*/);
-            if (activeSubs.length == 1) {
-                return activeSubs[0];
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                int[] activeSubs = SubscriptionManagerService.getInstance()
+                        .getActiveSubIdList(true /*visibleOnly*/);
+                if (activeSubs.length == 1) {
+                    return activeSubs[0];
+                }
+            } else {
+                int[] activeSubs = SubscriptionController.getInstance()
+                        .getActiveSubIdList(true /*visibleOnly*/);
+                if (activeSubs.length == 1) {
+                    return activeSubs[0];
+                }
             }
         } finally {
             Binder.restoreCallingIdentity(token);
@@ -519,6 +661,8 @@
             throw new SecurityException("sendStoredText: Package " + callingPkg
                     + "does not belong to " + Binder.getCallingUid());
         }
+        Rlog.d(LOG_TAG, "sendStoredText caller=" + callingPkg);
+
         if (iccSmsIntMgr != null) {
             iccSmsIntMgr.sendStoredText(callingPkg, callingAttributionTag, messageUri, scAddress,
                     sentIntent, deliveryIntent);
@@ -537,6 +681,8 @@
             throw new SecurityException("sendStoredMultipartText: Package " + callingPkg
                     + " does not belong to " + Binder.getCallingUid());
         }
+        Rlog.d(LOG_TAG, "sendStoredMultipartText caller=" + callingPkg);
+
         if (iccSmsIntMgr != null) {
             iccSmsIntMgr.sendStoredMultipartText(callingPkg, callingAttributionTag, messageUri,
                     scAddress, sentIntents, deliveryIntents);
@@ -690,6 +836,58 @@
     }
 
     @Override
+    public void setStorageMonitorMemoryStatusOverride(int subId, boolean isStorageAvailable) {
+        Phone phone = getPhone(subId);
+        Context context;
+        if (phone != null) {
+            context = phone.getContext();
+        } else {
+            Rlog.e(LOG_TAG, "Phone Object is Null");
+            return;
+        }
+        // If it doesn't have modify phone state permission
+        // a SecurityException will be thrown.
+        if (context.checkPermission(android.Manifest
+                        .permission.MODIFY_PHONE_STATE, Binder.getCallingPid(),
+                        Binder.getCallingUid()) != PERMISSION_GRANTED) {
+            throw new SecurityException(
+                    "setStorageMonitorMemoryStatusOverride needs MODIFY_PHONE_STATE");
+        }
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            phone.mSmsStorageMonitor.sendMemoryStatusOverride(isStorageAvailable);
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    @Override
+    public void clearStorageMonitorMemoryStatusOverride(int subId) {
+        Phone phone = getPhone(subId);
+        Context context;
+        if (phone != null) {
+            context = phone.getContext();
+        } else {
+            Rlog.e(LOG_TAG, "Phone Object is Null");
+            return;
+        }
+        // If it doesn't have modify phone state permission
+        // a SecurityException will be thrown.
+        if (context.checkPermission(android.Manifest
+                        .permission.MODIFY_PHONE_STATE, Binder.getCallingPid(),
+                        Binder.getCallingUid()) != PERMISSION_GRANTED) {
+            throw new SecurityException(
+                    "clearStorageMonitorMemoryStatusOverride needs MODIFY_PHONE_STATE");
+        }
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            phone.mSmsStorageMonitor.clearMemoryStatusOverride();
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    @Override
     public int checkSmsShortCodeDestination(int subId, String callingPackage,
             String callingFeatureId, String destAddress, String countryIso) {
         if (callingPackage == null) {
@@ -714,6 +912,23 @@
     public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
             String callingAttributionTag, int subId, String number, int port, String text,
             PendingIntent sentIntent) {
+        Rlog.d(LOG_TAG, "sendVisualVoicemailSmsForSubscriber caller=" + callingPackage);
+
+        // Do not send non-emergency SMS in ECBM as it forces device to exit ECBM.
+        if(getPhone(subId).isInEcm()) {
+            Rlog.d(LOG_TAG, "sendVisualVoicemailSmsForSubscriber: Do not send non-emergency "
+                    + "SMS in ECBM as it forces device to exit ECBM.");
+            return;
+        }
+
+        // Check if user is associated with the subscription
+        if (!TelephonyPermissions.checkSubscriptionAssociatedWithUser(mContext, subId,
+                Binder.getCallingUserHandle())) {
+            // TODO(b/258629881): Display error dialog.
+            sendErrorInPendingIntent(sentIntent, SmsManager.RESULT_USER_NOT_ALLOWED);
+            return;
+        }
+
         if (port == 0) {
             sendTextForSubscriberWithSelfPermissionsInternal(subId, callingPackage,
                     callingAttributionTag, number, null, text, sentIntent, null, false,
@@ -856,4 +1071,49 @@
     public static String formatCrossStackMessageId(long id) {
         return "{x-message-id:" + id + "}";
     }
-}
+
+    /**
+     * The following function checks if destination address or smsc is blocked due to FDN.
+     * @param subId subscription ID
+     * @param destAddr destination address of the message
+     * @return true if either destAddr or smscAddr is blocked due to FDN.
+     */
+    @VisibleForTesting
+    public boolean isNumberBlockedByFDN(int subId, String destAddr, String callingPackage) {
+        int phoneId = SubscriptionManager.getPhoneId(subId);
+        if (!FdnUtils.isFdnEnabled(phoneId)) {
+            return false;
+        }
+
+        // Skip FDN check for emergency numbers
+        TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
+        if (tm.isEmergencyNumber(destAddr)) {
+            return false;
+        }
+
+        // Check if destAddr is present in FDN list
+        String defaultCountryIso = tm.getSimCountryIso().toUpperCase(Locale.ENGLISH);
+        if (FdnUtils.isNumberBlockedByFDN(phoneId, destAddr, defaultCountryIso)) {
+            return true;
+        }
+
+        // Get SMSC address for this subscription
+        IccSmsInterfaceManager iccSmsIntMgr = getIccSmsInterfaceManager(subId);
+        String smscAddr;
+        if (iccSmsIntMgr != null) {
+            long identity = Binder.clearCallingIdentity();
+            try {
+                smscAddr =  iccSmsIntMgr.getSmscAddressFromIccEf(callingPackage);
+            } finally {
+                Binder.restoreCallingIdentity(identity);
+            }
+        } else {
+            Rlog.e(LOG_TAG, "getSmscAddressFromIccEfForSubscriber iccSmsIntMgr is null"
+                    + " for Subscription: " + subId);
+            return true;
+        }
+
+        // Check if smscAddr is present in FDN list
+        return FdnUtils.isNumberBlockedByFDN(phoneId, smscAddr, defaultCountryIso);
+    }
+}
\ No newline at end of file
diff --git a/src/java/com/android/internal/telephony/SmsDispatchersController.java b/src/java/com/android/internal/telephony/SmsDispatchersController.java
index 53556ac..e434801 100644
--- a/src/java/com/android/internal/telephony/SmsDispatchersController.java
+++ b/src/java/com/android/internal/telephony/SmsDispatchersController.java
@@ -20,6 +20,8 @@
 import static com.android.internal.telephony.cdma.sms.BearerData.ERROR_NONE;
 import static com.android.internal.telephony.cdma.sms.BearerData.ERROR_TEMPORARY;
 
+import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.app.Activity;
 import android.app.PendingIntent;
 import android.app.PendingIntent.CanceledException;
@@ -29,19 +31,29 @@
 import android.content.IntentFilter;
 import android.net.Uri;
 import android.os.AsyncResult;
+import android.os.Binder;
 import android.os.Handler;
+import android.os.Looper;
 import android.os.Message;
 import android.os.UserManager;
 import android.provider.Telephony.Sms;
 import android.provider.Telephony.Sms.Intents;
+import android.telephony.Annotation.DisconnectCauses;
+import android.telephony.DomainSelectionService;
+import android.telephony.NetworkRegistrationInfo;
 import android.telephony.ServiceState;
 import android.telephony.SmsManager;
 import android.telephony.SmsMessage;
+import android.telephony.TelephonyManager;
 
 import com.android.ims.ImsManager;
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.cdma.CdmaInboundSmsHandler;
 import com.android.internal.telephony.cdma.CdmaSMSDispatcher;
+import com.android.internal.telephony.domainselection.DomainSelectionConnection;
+import com.android.internal.telephony.domainselection.DomainSelectionResolver;
+import com.android.internal.telephony.domainselection.EmergencySmsDomainSelectionConnection;
+import com.android.internal.telephony.domainselection.SmsDomainSelectionConnection;
 import com.android.internal.telephony.gsm.GsmInboundSmsHandler;
 import com.android.internal.telephony.gsm.GsmSMSDispatcher;
 import com.android.telephony.Rlog;
@@ -50,6 +62,8 @@
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
 
 /**
  *
@@ -115,6 +129,195 @@
             new HashMap<>();
 
     /**
+     * Testing interface for injecting mock DomainSelectionConnection and a flag to indicate
+     * whether the domain selection is supported.
+     */
+    @VisibleForTesting
+    public interface DomainSelectionResolverProxy {
+        /**
+         * Returns a {@link DomainSelectionConnection} created using the specified
+         * context and callback.
+         *
+         * @param phone The {@link Phone} instance.
+         * @param selectorType The domain selector type to identify the domain selection connection.
+         *                     A {@link DomainSelectionService#SELECTOR_TYPE_SMS} is used for SMS.
+         * @param isEmergency A flag to indicate whether this connection is
+         *                    for an emergency SMS or not.
+         */
+        @Nullable DomainSelectionConnection getDomainSelectionConnection(Phone phone,
+                @DomainSelectionService.SelectorType int selectorType, boolean isEmergency);
+
+        /**
+         * Checks if the device supports the domain selection service to route the call / SMS /
+         * supplementary services to the appropriate domain.
+         *
+         * @return {@code true} if the domain selection is supported on the device,
+         *         {@code false} otherwise.
+         */
+        boolean isDomainSelectionSupported();
+    }
+
+    private DomainSelectionResolverProxy mDomainSelectionResolverProxy =
+            new DomainSelectionResolverProxy() {
+                @Override
+                @Nullable
+                public DomainSelectionConnection getDomainSelectionConnection(Phone phone,
+                        @DomainSelectionService.SelectorType int selectorType,
+                        boolean isEmergency) {
+                    try {
+                        return DomainSelectionResolver.getInstance().getDomainSelectionConnection(
+                                phone, selectorType, isEmergency);
+                    } catch (IllegalStateException e) {
+                        // In general, DomainSelectionResolver is always initialized by TeleService,
+                        // but if it's not initialized (like in unit tests),
+                        // it returns null to perform the legacy behavior in this case.
+                        return null;
+                    }
+                }
+
+                @Override
+                public boolean isDomainSelectionSupported() {
+                    return DomainSelectionResolver.getInstance().isDomainSelectionSupported();
+                }
+            };
+
+    /** Stores the sending SMS information for a pending request. */
+    private class PendingRequest {
+        public static final int TYPE_DATA = 1;
+        public static final int TYPE_TEXT = 2;
+        public static final int TYPE_MULTIPART_TEXT = 3;
+        public static final int TYPE_RETRY_SMS = 4;
+
+        public final int type;
+        public final SMSDispatcher.SmsTracker tracker;
+        public final String callingPackage;
+        public final String destAddr;
+        public final String scAddr;
+        public final ArrayList<PendingIntent> sentIntents;
+        public final ArrayList<PendingIntent> deliveryIntents;
+        public final boolean isForVvm;
+        // sendData specific
+        public final byte[] data;
+        public final int destPort;
+        // sendText / sendMultipartText specific
+        public final ArrayList<String> texts;
+        public final Uri messageUri;
+        public final boolean persistMessage;
+        public final int priority;
+        public final boolean expectMore;
+        public final int validityPeriod;
+        public final long messageId;
+        public final boolean skipShortCodeCheck;
+
+        PendingRequest(int type, SMSDispatcher.SmsTracker tracker, String callingPackage,
+                String destAddr, String scAddr, ArrayList<PendingIntent> sentIntents,
+                ArrayList<PendingIntent> deliveryIntents, boolean isForVvm, byte[] data,
+                int destPort, ArrayList<String> texts, Uri messageUri, boolean persistMessage,
+                int priority, boolean expectMore, int validityPeriod, long messageId,
+                boolean skipShortCodeCheck) {
+            this.type = type;
+            this.tracker = tracker;
+            this.callingPackage = callingPackage;
+            this.destAddr = destAddr;
+            this.scAddr = scAddr;
+            this.sentIntents = sentIntents;
+            this.deliveryIntents = deliveryIntents;
+            this.isForVvm = isForVvm;
+
+            this.data = data;
+            this.destPort = destPort;
+
+            this.texts = texts;
+            this.messageUri = messageUri;
+            this.persistMessage = persistMessage;
+            this.priority = priority;
+            this.expectMore = expectMore;
+            this.validityPeriod = validityPeriod;
+            this.messageId = messageId;
+            this.skipShortCodeCheck = skipShortCodeCheck;
+        }
+    }
+
+    /**
+     * Manages the {@link DomainSelectionConnection} instance and its related information.
+     */
+    @VisibleForTesting
+    protected class DomainSelectionConnectionHolder
+            implements DomainSelectionConnection.DomainSelectionConnectionCallback {
+        private final boolean mEmergency;
+        // Manages the pending request while selecting a proper domain.
+        private final List<PendingRequest> mPendingRequests = new ArrayList<>();
+        // Manages the domain selection connections: MO SMS or emergency SMS.
+        private DomainSelectionConnection mConnection;
+
+        DomainSelectionConnectionHolder(boolean emergency) {
+            mEmergency = emergency;
+        }
+
+        /**
+         * Returns a {@link DomainSelectionConnection} instance.
+         */
+        public DomainSelectionConnection getConnection() {
+            return mConnection;
+        }
+
+        /**
+         * Returns a list of {@link PendingRequest} that was added
+         * while the domain selection is performed.
+         */
+        public List<PendingRequest> getPendingRequests() {
+            return mPendingRequests;
+        }
+
+        /**
+         * Checks whether or not the domain selection is requested.
+         * If there is no pending request, the domain selection request is needed to
+         * select a proper domain for MO SMS.
+         */
+        public boolean isDomainSelectionRequested() {
+            return !mPendingRequests.isEmpty();
+        }
+
+        /**
+         * Checks whether or not this holder is for an emergency SMS.
+         */
+        public boolean isEmergency() {
+            return mEmergency;
+        }
+
+        /**
+         * Clears all pending requests.
+         */
+        public void clearAllRequests() {
+            mPendingRequests.clear();
+        }
+
+        /**
+         * Add a new pending request.
+         */
+        public void addRequest(@NonNull PendingRequest request) {
+            mPendingRequests.add(request);
+        }
+
+        /**
+         * Sets a {@link DomainSelectionConnection} instance.
+         */
+        public void setConnection(DomainSelectionConnection connection) {
+            mConnection = connection;
+        }
+
+
+        @Override
+        public void onSelectionTerminated(@DisconnectCauses int cause) {
+            notifyDomainSelectionTerminated(this);
+        }
+    }
+
+    /** Manages the domain selection connections: MO SMS or emergency SMS. */
+    private DomainSelectionConnectionHolder mDscHolder;
+    private DomainSelectionConnectionHolder mEmergencyDscHolder;
+
+    /**
      * Puts a delivery pending tracker to the map based on the format.
      *
      * @param tracker the tracker awaiting a delivery status report.
@@ -129,6 +332,14 @@
 
     public SmsDispatchersController(Phone phone, SmsStorageMonitor storageMonitor,
             SmsUsageMonitor usageMonitor) {
+        this(phone, storageMonitor, usageMonitor, phone.getLooper());
+    }
+
+    @VisibleForTesting
+    public SmsDispatchersController(Phone phone, SmsStorageMonitor storageMonitor,
+            SmsUsageMonitor usageMonitor, Looper looper) {
+        super(looper);
+
         Rlog.d(TAG, "SmsDispatchersController created");
 
         mContext = phone.getContext();
@@ -141,9 +352,9 @@
         mImsSmsDispatcher = new ImsSmsDispatcher(phone, this, ImsManager::getConnector);
         mCdmaDispatcher = new CdmaSMSDispatcher(phone, this);
         mGsmInboundSmsHandler = GsmInboundSmsHandler.makeInboundSmsHandler(phone.getContext(),
-                storageMonitor, phone);
+                storageMonitor, phone, looper);
         mCdmaInboundSmsHandler = CdmaInboundSmsHandler.makeInboundSmsHandler(phone.getContext(),
-                storageMonitor, phone, (CdmaSMSDispatcher) mCdmaDispatcher);
+                storageMonitor, phone, (CdmaSMSDispatcher) mCdmaDispatcher, looper);
         mGsmDispatcher = new GsmSMSDispatcher(phone, this, mGsmInboundSmsHandler);
         SmsBroadcastUndelivered.initialize(phone.getContext(),
                 mGsmInboundSmsHandler, mCdmaInboundSmsHandler);
@@ -188,6 +399,9 @@
         mCdmaDispatcher.dispose();
         mGsmInboundSmsHandler.dispose();
         mCdmaInboundSmsHandler.dispose();
+        // Cancels the domain selection request if it's still in progress.
+        finishDomainSelection(mDscHolder);
+        finishDomainSelection(mEmergencyDscHolder);
     }
 
     /**
@@ -242,6 +456,21 @@
         }
     }
 
+    private String getSmscAddressFromUSIM(String callingPkg) {
+        IccSmsInterfaceManager iccSmsIntMgr = mPhone.getIccSmsInterfaceManager();
+        if (iccSmsIntMgr != null) {
+            long identity = Binder.clearCallingIdentity();
+            try {
+                return iccSmsIntMgr.getSmscAddressFromIccEf(callingPkg);
+            } finally {
+                Binder.restoreCallingIdentity(identity);
+            }
+        } else {
+            Rlog.d(TAG, "getSmscAddressFromIccEf iccSmsIntMgr is null");
+        }
+        return null;
+    }
+
     private void reevaluateTimerStatus() {
         long currentTime = System.currentTimeMillis();
 
@@ -390,7 +619,12 @@
         // SMS pdus when the phone is camping on CDMA(3gpp2) network and vice versa.
         android.telephony.SmsMessage msg =
                 android.telephony.SmsMessage.createFromPdu(pdu, format);
-        injectSmsPdu(msg, format, callback, false /* ignoreClass */, isOverIms);
+        injectSmsPdu(msg, format, callback, false /* ignoreClass */, isOverIms, 0 /* unused */);
+    }
+
+    @VisibleForTesting
+    public void setImsSmsDispatcher(ImsSmsDispatcher imsSmsDispatcher) {
+        mImsSmsDispatcher = imsSmsDispatcher;
     }
 
     /**
@@ -405,7 +639,7 @@
      */
     @VisibleForTesting
     public void injectSmsPdu(SmsMessage msg, String format, SmsInjectionCallback callback,
-            boolean ignoreClass, boolean isOverIms) {
+            boolean ignoreClass, boolean isOverIms, int token) {
         Rlog.d(TAG, "SmsDispatchersController:injectSmsPdu");
         try {
             if (msg == null) {
@@ -427,7 +661,7 @@
                 Rlog.i(TAG, "SmsDispatchersController:injectSmsText Sending msg=" + msg
                         + ", format=" + format + "to mGsmInboundSmsHandler");
                 mGsmInboundSmsHandler.sendMessage(
-                        InboundSmsHandler.EVENT_INJECT_SMS, isOverIms ? 1 : 0, 0, ar);
+                        InboundSmsHandler.EVENT_INJECT_SMS, isOverIms ? 1 : 0, token, ar);
             } else if (format.equals(SmsConstants.FORMAT_3GPP2)) {
                 Rlog.i(TAG, "SmsDispatchersController:injectSmsText Sending msg=" + msg
                         + ", format=" + format + "to mCdmaInboundSmsHandler");
@@ -445,20 +679,60 @@
     }
 
     /**
+     * sets ImsManager object.
+     *
+     * @param imsManager holds a valid object or a null for setting
+     */
+    public boolean setImsManager(ImsManager imsManager) {
+        if (mGsmInboundSmsHandler != null) {
+            mGsmInboundSmsHandler.setImsManager(imsManager);
+            return true;
+        }
+        return false;
+    }
+
+    /**
      * Retry the message along to the radio.
      *
      * @param tracker holds the SMS message to send
      */
     public void sendRetrySms(SMSDispatcher.SmsTracker tracker) {
-        String oldFormat = tracker.mFormat;
         boolean retryUsingImsService = false;
 
-        if (!tracker.mUsesImsServiceForIms && mImsSmsDispatcher.isAvailable()) {
-            // If this tracker has not been handled by ImsSmsDispatcher yet and IMS Service is
-            // available now, retry this failed tracker using IMS Service.
-            retryUsingImsService = true;
+        if (!tracker.mUsesImsServiceForIms) {
+            if (mDomainSelectionResolverProxy.isDomainSelectionSupported()) {
+                DomainSelectionConnectionHolder holder = getDomainSelectionConnection(false);
+
+                // If the DomainSelectionConnection is not available,
+                // fallback to the legacy implementation.
+                if (holder != null && holder.getConnection() != null) {
+                    sendSmsUsingDomainSelection(holder,
+                            new PendingRequest(PendingRequest.TYPE_RETRY_SMS, tracker,
+                                    null, null, null, null, null, false, null, 0, null, null, false,
+                                    0, false, 0, 0L, false),
+                            "sendRetrySms");
+                    return;
+                }
+            }
+
+            if (mImsSmsDispatcher.isAvailable()) {
+                // If this tracker has not been handled by ImsSmsDispatcher yet and IMS Service is
+                // available now, retry this failed tracker using IMS Service.
+                retryUsingImsService = true;
+            }
         }
 
+        sendRetrySms(tracker, retryUsingImsService);
+    }
+
+    /**
+     * Retry the message along to the radio.
+     *
+     * @param tracker holds the SMS message to send
+     * @param retryUsingImsService a flag to indicate whether the retry SMS can use the ImsService
+     */
+    public void sendRetrySms(SMSDispatcher.SmsTracker tracker, boolean retryUsingImsService) {
+        String oldFormat = tracker.mFormat;
         // If retryUsingImsService is true, newFormat will be IMS SMS format. Otherwise, newFormat
         // will be based on voice technology.
         String newFormat =
@@ -503,7 +777,8 @@
                             scAddr, destAddr, text, (tracker.mDeliveryIntent != null), null);
                 } else {
                     pdu = com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(
-                            scAddr, destAddr, text, (tracker.mDeliveryIntent != null), null);
+                            scAddr, destAddr, text, (tracker.mDeliveryIntent != null), null,
+                            0, 0, 0, -1, tracker.mMessageRef);
                 }
             } else if (map.containsKey("data")) {
                 byte[] data = (byte[]) map.get("data");
@@ -518,7 +793,7 @@
                 } else {
                     pdu = com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(
                             scAddr, destAddr, destPort.intValue(), data,
-                            (tracker.mDeliveryIntent != null));
+                            (tracker.mDeliveryIntent != null), tracker.mMessageRef);
                 }
             }
 
@@ -544,6 +819,23 @@
     }
 
     /**
+     * Memory Available Event
+     * @param result callback message
+     */
+    public void reportSmsMemoryStatus(Message result) {
+        Rlog.d(TAG, "reportSmsMemoryStatus: ");
+        try {
+            mImsSmsDispatcher.onMemoryAvailable();
+            AsyncResult.forMessage(result, null, null);
+            result.sendToTarget();
+        } catch (Exception e) {
+            Rlog.e(TAG, "reportSmsMemoryStatus Failed ", e);
+            AsyncResult.forMessage(result, null, e);
+            result.sendToTarget();
+        }
+    }
+
+    /**
      * SMS over IMS is supported if IMS is registered and SMS is supported on IMS.
      *
      * @return true if SMS over IMS is supported via an IMS Service or mIms is true for the older
@@ -589,6 +881,347 @@
         return (mCdmaDispatcher.getFormat().equals(format));
     }
 
+    /** Sets a proxy interface for accessing the methods of {@link DomainSelectionResolver}. */
+    @VisibleForTesting
+    public void setDomainSelectionResolverProxy(@NonNull DomainSelectionResolverProxy proxy) {
+        mDomainSelectionResolverProxy = proxy;
+    }
+
+    /**
+     * Determines whether or not to use CDMA format for MO SMS when the domain selection uses.
+     * If the domain is {@link NetworkRegistrationInfo#DOMAIN_PS}, then format is based on
+     * IMS SMS format, otherwise format is based on current phone type.
+     *
+     * @return {@code true} if CDMA format should be used for MO SMS, {@code false} otherwise.
+     */
+    private boolean isCdmaMo(@NetworkRegistrationInfo.Domain int domain) {
+        if (domain != NetworkRegistrationInfo.DOMAIN_PS) {
+            // IMS is not registered, use voice technology to determine SMS format.
+            return (PhoneConstants.PHONE_TYPE_CDMA == mPhone.getPhoneType());
+        }
+        // IMS is registered with SMS support
+        return isCdmaFormat(mImsSmsDispatcher.getFormat());
+    }
+
+    /**
+     * Returns a {@link DomainSelectionConnectionHolder} according to the flag specified.
+     *
+     * @param emergency The flag to indicate that the domain selection is for an emergency SMS.
+     * @return A {@link DomainSelectionConnectionHolder} instance or null.
+     */
+    @VisibleForTesting
+    @Nullable
+    protected DomainSelectionConnectionHolder getDomainSelectionConnectionHolder(
+            boolean emergency) {
+        return emergency ? mEmergencyDscHolder : mDscHolder;
+    }
+
+    /**
+     * Returns a {@link DomainSelectionConnectionHolder} if the domain selection supports,
+     * return null otherwise.
+     *
+     * @param emergency The flag to indicate that the domain selection is for an emergency SMS.
+     * @return A {@link DomainSelectionConnectionHolder} that grabs the
+     *         {@link DomainSelectionConnection} and its related information to use the domain
+     *         selection architecture.
+     */
+    private DomainSelectionConnectionHolder getDomainSelectionConnection(boolean emergency) {
+        DomainSelectionConnectionHolder holder = getDomainSelectionConnectionHolder(emergency);
+        DomainSelectionConnection connection = (holder != null) ? holder.getConnection() : null;
+        boolean created = false;
+
+        if (connection == null) {
+            connection = mDomainSelectionResolverProxy.getDomainSelectionConnection(
+                    mPhone, DomainSelectionService.SELECTOR_TYPE_SMS, emergency);
+
+            if (connection == null) {
+                // Domain selection architecture is not supported.
+                // Use the legacy architecture.
+                return null;
+            }
+
+            created = true;
+        }
+
+        if (holder == null) {
+            holder = new DomainSelectionConnectionHolder(emergency);
+
+            if (emergency) {
+                mEmergencyDscHolder = holder;
+            } else {
+                mDscHolder = holder;
+            }
+        }
+
+        holder.setConnection(connection);
+
+        return holder;
+    }
+
+    /**
+     * Requests the domain selection for MO SMS.
+     *
+     * @param holder The {@link DomainSelectionConnectionHolder} that contains the
+     *               {@link DomainSelectionConnection} and its related information.
+     */
+    private void requestDomainSelection(@NonNull DomainSelectionConnectionHolder holder) {
+        DomainSelectionService.SelectionAttributes attr =
+                new DomainSelectionService.SelectionAttributes.Builder(mPhone.getPhoneId(),
+                        mPhone.getSubId(), DomainSelectionService.SELECTOR_TYPE_SMS)
+                .setEmergency(holder.isEmergency())
+                .build();
+
+        if (holder.isEmergency()) {
+            EmergencySmsDomainSelectionConnection emergencyConnection =
+                    (EmergencySmsDomainSelectionConnection) holder.getConnection();
+            CompletableFuture<Integer> future =
+                    emergencyConnection.requestDomainSelection(attr, holder);
+            future.thenAcceptAsync((domain) -> {
+                if (VDBG) {
+                    logd("requestDomainSelection(emergency): domain="
+                            + DomainSelectionService.getDomainName(domain));
+                }
+                sendAllPendingRequests(holder, domain);
+                finishDomainSelection(holder);
+            }, this::post);
+        } else {
+            SmsDomainSelectionConnection connection =
+                    (SmsDomainSelectionConnection) holder.getConnection();
+            CompletableFuture<Integer> future = connection.requestDomainSelection(attr, holder);
+            future.thenAcceptAsync((domain) -> {
+                if (VDBG) {
+                    logd("requestDomainSelection: domain="
+                            + DomainSelectionService.getDomainName(domain));
+                }
+                sendAllPendingRequests(holder, domain);
+                finishDomainSelection(holder);
+            }, this::post);
+        }
+    }
+
+    /**
+     * Sends a SMS after selecting the domain via the domain selection service.
+     *
+     * @param holder The {@link DomainSelectionConnectionHolder} that contains the
+     *               {@link DomainSelectionConnection} and its related information.
+     * @param request The {@link PendingRequest} that stores the SMS request
+     *                (data, text, multipart text) to be sent.
+     * @param logTag The log tag to display which method called this method.
+     */
+    private void sendSmsUsingDomainSelection(@NonNull DomainSelectionConnectionHolder holder,
+            @NonNull PendingRequest request, @NonNull String logTag) {
+        boolean isDomainSelectionRequested = holder.isDomainSelectionRequested();
+        // The domain selection is in progress so waits for the result of
+        // the domain selection by adding this request to the pending list.
+        holder.addRequest(request);
+
+        if (!isDomainSelectionRequested) {
+            if (VDBG) {
+                logd("requestDomainSelection: " + logTag);
+            }
+            requestDomainSelection(holder);
+        }
+    }
+
+    /**
+     * Finishes the domain selection for MO SMS.
+     *
+     * @param holder The {@link DomainSelectionConnectionHolder} object that is being finished.
+     */
+    private void finishDomainSelection(DomainSelectionConnectionHolder holder) {
+        DomainSelectionConnection connection = (holder != null) ? holder.getConnection() : null;
+
+        if (connection != null) {
+            // After this method is called, the domain selection service will clean up
+            // its resources and finish the procedure that are related to the current domain
+            // selection request.
+            connection.finishSelection();
+        }
+
+        if (holder != null) {
+            final List<PendingRequest> pendingRequests = holder.getPendingRequests();
+
+            logd("finishDomainSelection: pendingRequests=" + pendingRequests.size());
+
+            for (PendingRequest r : pendingRequests) {
+                triggerSentIntentForFailure(r.sentIntents);
+            }
+
+            holder.clearAllRequests();
+            holder.setConnection(null);
+        }
+    }
+
+    /**
+     * Notifies the application that MO SMS is not sent by the error of domain selection.
+     *
+     * @param holder The {@link DomainSelectionConnectionHolder} object that is being terminated.
+     */
+    private void notifyDomainSelectionTerminated(@NonNull DomainSelectionConnectionHolder holder) {
+        final List<PendingRequest> pendingRequests = holder.getPendingRequests();
+
+        logd("notifyDomainSelectionTerminated: pendingRequests=" + pendingRequests.size());
+
+        for (PendingRequest r : pendingRequests) {
+            triggerSentIntentForFailure(r.sentIntents);
+        }
+
+        holder.setConnection(null);
+        holder.clearAllRequests();
+    }
+
+    /**
+     * Sends all pending requests for MO SMS.
+     *
+     * @param holder The {@link DomainSelectionConnectionHolder} object that all the pending
+     *               requests are handled.
+     * @param domain The domain where the SMS is being sent, which can be one of the following:
+     *               - {@link NetworkRegistrationInfo#DOMAIN_PS}
+     *               - {@link NetworkRegistrationInfo#DOMAIN_CS}
+     */
+    private void sendAllPendingRequests(@NonNull DomainSelectionConnectionHolder holder,
+            @NetworkRegistrationInfo.Domain int domain) {
+        final List<PendingRequest> pendingRequests = holder.getPendingRequests();
+
+        if (VDBG) {
+            logd("sendAllPendingRequests: domain=" + DomainSelectionService.getDomainName(domain)
+                    + ", size=" + pendingRequests.size());
+        }
+
+        for (PendingRequest r : pendingRequests) {
+            switch (r.type) {
+                case PendingRequest.TYPE_DATA:
+                    sendData(domain, r);
+                    break;
+                case PendingRequest.TYPE_TEXT:
+                    sendText(domain, r);
+                    break;
+                case PendingRequest.TYPE_MULTIPART_TEXT:
+                    sendMultipartText(domain, r);
+                    break;
+                case PendingRequest.TYPE_RETRY_SMS:
+                    sendRetrySms(r.tracker, (domain == NetworkRegistrationInfo.DOMAIN_PS));
+                    break;
+                default:
+                    // Not reachable.
+                    break;
+            }
+        }
+
+        holder.clearAllRequests();
+    }
+
+    /**
+     * Sends a data based SMS to a specific application port.
+     *
+     * @param domain The domain where the SMS is being sent, which can be one of the following:
+     *               - {@link NetworkRegistrationInfo#DOMAIN_PS}
+     *               - {@link NetworkRegistrationInfo#DOMAIN_CS}
+     * @param request The pending request for MO SMS.
+     */
+    private void sendData(@NetworkRegistrationInfo.Domain int domain,
+            @NonNull PendingRequest request) {
+        if (domain == NetworkRegistrationInfo.DOMAIN_PS) {
+            mImsSmsDispatcher.sendData(request.callingPackage, request.destAddr, request.scAddr,
+                    request.destPort, request.data, request.sentIntents.get(0),
+                    request.deliveryIntents.get(0), request.isForVvm);
+        } else if (isCdmaMo(domain)) {
+            mCdmaDispatcher.sendData(request.callingPackage, request.destAddr, request.scAddr,
+                    request.destPort, request.data, request.sentIntents.get(0),
+                    request.deliveryIntents.get(0), request.isForVvm);
+        } else {
+            mGsmDispatcher.sendData(request.callingPackage, request.destAddr, request.scAddr,
+                    request.destPort, request.data, request.sentIntents.get(0),
+                    request.deliveryIntents.get(0), request.isForVvm);
+        }
+    }
+
+    /**
+     * Sends a text based SMS.
+     *
+     * @param domain The domain where the SMS is being sent, which can be one of the following:
+     *               - {@link NetworkRegistrationInfo#DOMAIN_PS}
+     *               - {@link NetworkRegistrationInfo#DOMAIN_CS}
+     * @param request The pending request for MO SMS.
+     */
+    private void sendText(@NetworkRegistrationInfo.Domain int domain,
+            @NonNull PendingRequest request) {
+        if (domain == NetworkRegistrationInfo.DOMAIN_PS) {
+            mImsSmsDispatcher.sendText(request.destAddr, request.scAddr, request.texts.get(0),
+                    request.sentIntents.get(0), request.deliveryIntents.get(0),
+                    request.messageUri, request.callingPackage, request.persistMessage,
+                    request.priority, false /*request.expectMore*/, request.validityPeriod,
+                    request.isForVvm, request.messageId, request.skipShortCodeCheck);
+        } else {
+            if (isCdmaMo(domain)) {
+                mCdmaDispatcher.sendText(request.destAddr, request.scAddr, request.texts.get(0),
+                        request.sentIntents.get(0), request.deliveryIntents.get(0),
+                        request.messageUri, request.callingPackage, request.persistMessage,
+                        request.priority, request.expectMore, request.validityPeriod,
+                        request.isForVvm, request.messageId, request.skipShortCodeCheck);
+            } else {
+                mGsmDispatcher.sendText(request.destAddr, request.scAddr, request.texts.get(0),
+                        request.sentIntents.get(0), request.deliveryIntents.get(0),
+                        request.messageUri, request.callingPackage, request.persistMessage,
+                        request.priority, request.expectMore, request.validityPeriod,
+                        request.isForVvm, request.messageId, request.skipShortCodeCheck);
+            }
+        }
+    }
+
+    /**
+     * Sends a multi-part text based SMS.
+     *
+     * @param domain The domain where the SMS is being sent, which can be one of the following:
+     *               - {@link NetworkRegistrationInfo#DOMAIN_PS}
+     *               - {@link NetworkRegistrationInfo#DOMAIN_CS}
+     * @param request The pending request for MO SMS.
+     */
+    private void sendMultipartText(@NetworkRegistrationInfo.Domain int domain,
+            @NonNull PendingRequest request) {
+        if (domain == NetworkRegistrationInfo.DOMAIN_PS) {
+            mImsSmsDispatcher.sendMultipartText(request.destAddr, request.scAddr, request.texts,
+                    request.sentIntents, request.deliveryIntents, request.messageUri,
+                    request.callingPackage, request.persistMessage, request.priority,
+                    false /*request.expectMore*/, request.validityPeriod, request.messageId);
+        } else {
+            if (isCdmaMo(domain)) {
+                mCdmaDispatcher.sendMultipartText(request.destAddr, request.scAddr, request.texts,
+                        request.sentIntents, request.deliveryIntents, request.messageUri,
+                        request.callingPackage, request.persistMessage, request.priority,
+                        request.expectMore, request.validityPeriod, request.messageId);
+            } else {
+                mGsmDispatcher.sendMultipartText(request.destAddr, request.scAddr, request.texts,
+                        request.sentIntents, request.deliveryIntents, request.messageUri,
+                        request.callingPackage, request.persistMessage, request.priority,
+                        request.expectMore, request.validityPeriod, request.messageId);
+            }
+        }
+    }
+
+    private void triggerSentIntentForFailure(@NonNull PendingIntent sentIntent) {
+        try {
+            sentIntent.send(SmsManager.RESULT_ERROR_GENERIC_FAILURE);
+        } catch (CanceledException e) {
+            logd("Intent has been canceled!");
+        }
+    }
+
+    private void triggerSentIntentForFailure(@NonNull List<PendingIntent> sentIntents) {
+        for (PendingIntent sentIntent : sentIntents) {
+            triggerSentIntentForFailure(sentIntent);
+        }
+    }
+
+    /**
+     * Creates an ArrayList object from any object.
+     */
+    private static <T> ArrayList<T> asArrayList(T object) {
+        ArrayList<T> list = new ArrayList<>();
+        list.add(object);
+        return list;
+    }
+
     /**
      * Send a data based SMS to a specific application port.
      *
@@ -670,6 +1303,26 @@
      */
     protected void sendData(String callingPackage, String destAddr, String scAddr, int destPort,
             byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent, boolean isForVvm) {
+        if (scAddr == null) {
+            scAddr = getSmscAddressFromUSIM(callingPackage);
+        }
+
+        if (mDomainSelectionResolverProxy.isDomainSelectionSupported()) {
+            DomainSelectionConnectionHolder holder = getDomainSelectionConnection(false);
+
+            // If the DomainSelectionConnection is not available,
+            // fallback to the legacy implementation.
+            if (holder != null && holder.getConnection() != null) {
+                sendSmsUsingDomainSelection(holder,
+                        new PendingRequest(PendingRequest.TYPE_DATA, null, callingPackage,
+                                destAddr, scAddr, asArrayList(sentIntent),
+                                asArrayList(deliveryIntent), isForVvm, data, destPort, null, null,
+                                false, 0, false, 0, 0L, false),
+                        "sendData");
+                return;
+            }
+        }
+
         if (mImsSmsDispatcher.isAvailable()) {
             mImsSmsDispatcher.sendData(callingPackage, destAddr, scAddr, destPort, data, sentIntent,
                     deliveryIntent, isForVvm);
@@ -783,19 +1436,148 @@
             PendingIntent deliveryIntent, Uri messageUri, String callingPkg, boolean persistMessage,
             int priority, boolean expectMore, int validityPeriod, boolean isForVvm,
             long messageId) {
+        sendText(destAddr, scAddr, text, sentIntent, deliveryIntent, messageUri, callingPkg,
+                persistMessage, priority, expectMore, validityPeriod, isForVvm, messageId, false);
+    }
+
+    /**
+     * Send a text based SMS.
+     *
+     * @param destAddr the address to send the message to
+     * @param scAddr is the service center address or null to use
+     *  the current default SMSC
+     * @param text the body of the message to send
+     * @param sentIntent if not NULL this <code>PendingIntent</code> is
+     *  broadcast when the message is successfully sent, or failed.
+     *  The result code will be <code>Activity.RESULT_OK<code> for success,
+     *  or one of these errors:<br>
+     *  <code>SmsManager.RESULT_ERROR_GENERIC_FAILURE</code><br>
+     *  <code>SmsManager.RESULT_ERROR_RADIO_OFF</code><br>
+     *  <code>SmsManager.RESULT_ERROR_NULL_PDU</code><br>
+     *  <code>SmsManager.RESULT_ERROR_NO_SERVICE</code><br>
+     *  <code>SmsManager.RESULT_ERROR_LIMIT_EXCEEDED</code><br>
+     *  <code>SmsManager.RESULT_ERROR_FDN_CHECK_FAILURE</code><br>
+     *  <code>SmsManager.RESULT_ERROR_SHORT_CODE_NOT_ALLOWED</code><br>
+     *  <code>SmsManager.RESULT_ERROR_SHORT_CODE_NEVER_ALLOWED</code><br>
+     *  <code>SmsManager.RESULT_RADIO_NOT_AVAILABLE</code><br>
+     *  <code>SmsManager.RESULT_NETWORK_REJECT</code><br>
+     *  <code>SmsManager.RESULT_INVALID_ARGUMENTS</code><br>
+     *  <code>SmsManager.RESULT_INVALID_STATE</code><br>
+     *  <code>SmsManager.RESULT_NO_MEMORY</code><br>
+     *  <code>SmsManager.RESULT_INVALID_SMS_FORMAT</code><br>
+     *  <code>SmsManager.RESULT_SYSTEM_ERROR</code><br>
+     *  <code>SmsManager.RESULT_MODEM_ERROR</code><br>
+     *  <code>SmsManager.RESULT_NETWORK_ERROR</code><br>
+     *  <code>SmsManager.RESULT_ENCODING_ERROR</code><br>
+     *  <code>SmsManager.RESULT_INVALID_SMSC_ADDRESS</code><br>
+     *  <code>SmsManager.RESULT_OPERATION_NOT_ALLOWED</code><br>
+     *  <code>SmsManager.RESULT_INTERNAL_ERROR</code><br>
+     *  <code>SmsManager.RESULT_NO_RESOURCES</code><br>
+     *  <code>SmsManager.RESULT_CANCELLED</code><br>
+     *  <code>SmsManager.RESULT_REQUEST_NOT_SUPPORTED</code><br>
+     *  <code>SmsManager.RESULT_NO_BLUETOOTH_SERVICE</code><br>
+     *  <code>SmsManager.RESULT_INVALID_BLUETOOTH_ADDRESS</code><br>
+     *  <code>SmsManager.RESULT_BLUETOOTH_DISCONNECTED</code><br>
+     *  <code>SmsManager.RESULT_UNEXPECTED_EVENT_STOP_SENDING</code><br>
+     *  <code>SmsManager.RESULT_SMS_BLOCKED_DURING_EMERGENCY</code><br>
+     *  <code>SmsManager.RESULT_SMS_SEND_RETRY_FAILED</code><br>
+     *  <code>SmsManager.RESULT_REMOTE_EXCEPTION</code><br>
+     *  <code>SmsManager.RESULT_NO_DEFAULT_SMS_APP</code><br>
+     *  <code>SmsManager.RESULT_RIL_RADIO_NOT_AVAILABLE</code><br>
+     *  <code>SmsManager.RESULT_RIL_SMS_SEND_FAIL_RETRY</code><br>
+     *  <code>SmsManager.RESULT_RIL_NETWORK_REJECT</code><br>
+     *  <code>SmsManager.RESULT_RIL_INVALID_STATE</code><br>
+     *  <code>SmsManager.RESULT_RIL_INVALID_ARGUMENTS</code><br>
+     *  <code>SmsManager.RESULT_RIL_NO_MEMORY</code><br>
+     *  <code>SmsManager.RESULT_RIL_REQUEST_RATE_LIMITED</code><br>
+     *  <code>SmsManager.RESULT_RIL_INVALID_SMS_FORMAT</code><br>
+     *  <code>SmsManager.RESULT_RIL_SYSTEM_ERR</code><br>
+     *  <code>SmsManager.RESULT_RIL_ENCODING_ERR</code><br>
+     *  <code>SmsManager.RESULT_RIL_INVALID_SMSC_ADDRESS</code><br>
+     *  <code>SmsManager.RESULT_RIL_MODEM_ERR</code><br>
+     *  <code>SmsManager.RESULT_RIL_NETWORK_ERR</code><br>
+     *  <code>SmsManager.RESULT_RIL_INTERNAL_ERR</code><br>
+     *  <code>SmsManager.RESULT_RIL_REQUEST_NOT_SUPPORTED</code><br>
+     *  <code>SmsManager.RESULT_RIL_INVALID_MODEM_STATE</code><br>
+     *  <code>SmsManager.RESULT_RIL_NETWORK_NOT_READY</code><br>
+     *  <code>SmsManager.RESULT_RIL_OPERATION_NOT_ALLOWED</code><br>
+     *  <code>SmsManager.RESULT_RIL_NO_RESOURCES</code><br>
+     *  <code>SmsManager.RESULT_RIL_CANCELLED</code><br>
+     *  <code>SmsManager.RESULT_RIL_SIM_ABSENT</code><br>
+     *  <code>SmsManager.RESULT_RIL_SIMULTANEOUS_SMS_AND_CALL_NOT_ALLOWED</code><br>
+     *  <code>SmsManager.RESULT_RIL_ACCESS_BARRED</code><br>
+     *  <code>SmsManager.RESULT_RIL_BLOCKED_DUE_TO_CALL</code><br>
+     *  For <code>SmsManager.RESULT_ERROR_GENERIC_FAILURE</code> or any of the RESULT_RIL errors,
+     *  the sentIntent may include the extra "errorCode" containing a radio technology specific
+     *  value, generally only useful for troubleshooting.<br>
+     *  The per-application based SMS control checks sentIntent. If sentIntent
+     *  is NULL the caller will be checked against all unknown applications,
+     *  which cause smaller number of SMS to be sent in checking period.
+     * @param deliveryIntent if not NULL this <code>PendingIntent</code> is
+     *  broadcast when the message is delivered to the recipient.  The
+     * @param messageUri optional URI of the message if it is already stored in the system
+     * @param callingPkg the calling package name
+     * @param persistMessage whether to save the sent message into SMS DB for a
+     *  non-default SMS app.
+     * @param priority Priority level of the message
+     *  Refer specification See 3GPP2 C.S0015-B, v2.0, table 4.5.9-1
+     *  ---------------------------------
+     *  PRIORITY      | Level of Priority
+     *  ---------------------------------
+     *      '00'      |     Normal
+     *      '01'      |     Interactive
+     *      '10'      |     Urgent
+     *      '11'      |     Emergency
+     *  ----------------------------------
+     *  Any Other values included Negative considered as Invalid Priority Indicator of the message.
+     * @param expectMore is a boolean to indicate the sending messages through same link or not.
+     * @param validityPeriod Validity Period of the message in mins.
+     *  Refer specification 3GPP TS 23.040 V6.8.1 section 9.2.3.12.1.
+     *  Validity Period(Minimum) -> 5 mins
+     *  Validity Period(Maximum) -> 635040 mins(i.e.63 weeks).
+     *  Any Other values included Negative considered as Invalid Validity Period of the message.
+     * @param skipShortCodeCheck Skip check for short code type destination address.
+     */
+    public void sendText(String destAddr, String scAddr, String text, PendingIntent sentIntent,
+            PendingIntent deliveryIntent, Uri messageUri, String callingPkg, boolean persistMessage,
+            int priority, boolean expectMore, int validityPeriod, boolean isForVvm,
+            long messageId, boolean skipShortCodeCheck) {
+        if (scAddr == null) {
+            scAddr = getSmscAddressFromUSIM(callingPkg);
+        }
+
+        if (mDomainSelectionResolverProxy.isDomainSelectionSupported()) {
+            TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
+            boolean isEmergency = tm.isEmergencyNumber(destAddr);
+            DomainSelectionConnectionHolder holder = getDomainSelectionConnection(isEmergency);
+
+            // If the DomainSelectionConnection is not available,
+            // fallback to the legacy implementation.
+            if (holder != null && holder.getConnection() != null) {
+                sendSmsUsingDomainSelection(holder,
+                        new PendingRequest(PendingRequest.TYPE_TEXT, null, callingPkg,
+                                destAddr, scAddr, asArrayList(sentIntent),
+                                asArrayList(deliveryIntent), isForVvm, null, 0, asArrayList(text),
+                                messageUri, persistMessage, priority, expectMore, validityPeriod,
+                                messageId, skipShortCodeCheck),
+                        "sendText");
+                return;
+            }
+        }
+
         if (mImsSmsDispatcher.isAvailable() || mImsSmsDispatcher.isEmergencySmsSupport(destAddr)) {
             mImsSmsDispatcher.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent,
                     messageUri, callingPkg, persistMessage, priority, false /*expectMore*/,
-                    validityPeriod, isForVvm, messageId);
+                    validityPeriod, isForVvm, messageId, skipShortCodeCheck);
         } else {
             if (isCdmaMo()) {
                 mCdmaDispatcher.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent,
                         messageUri, callingPkg, persistMessage, priority, expectMore,
-                        validityPeriod, isForVvm, messageId);
+                        validityPeriod, isForVvm, messageId, skipShortCodeCheck);
             } else {
                 mGsmDispatcher.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent,
                         messageUri, callingPkg, persistMessage, priority, expectMore,
-                        validityPeriod, isForVvm, messageId);
+                        validityPeriod, isForVvm, messageId, skipShortCodeCheck);
             }
         }
     }
@@ -909,6 +1691,26 @@
             ArrayList<PendingIntent> deliveryIntents, Uri messageUri, String callingPkg,
             boolean persistMessage, int priority, boolean expectMore, int validityPeriod,
             long messageId) {
+        if (scAddr == null) {
+            scAddr = getSmscAddressFromUSIM(callingPkg);
+        }
+
+        if (mDomainSelectionResolverProxy.isDomainSelectionSupported()) {
+            DomainSelectionConnectionHolder holder = getDomainSelectionConnection(false);
+
+            // If the DomainSelectionConnection is not available,
+            // fallback to the legacy implementation.
+            if (holder != null && holder.getConnection() != null) {
+                sendSmsUsingDomainSelection(holder,
+                        new PendingRequest(PendingRequest.TYPE_MULTIPART_TEXT, null,
+                                callingPkg, destAddr, scAddr, sentIntents, deliveryIntents, false,
+                                null, 0, parts, messageUri, persistMessage, priority, expectMore,
+                                validityPeriod, messageId, false),
+                        "sendMultipartText");
+                return;
+            }
+        }
+
         if (mImsSmsDispatcher.isAvailable()) {
             mImsSmsDispatcher.sendMultipartText(destAddr, scAddr, parts, sentIntents,
                     deliveryIntents, messageUri, callingPkg, persistMessage, priority,
@@ -1065,4 +1867,8 @@
     private void logd(String msg) {
         Rlog.d(TAG, msg);
     }
+
+    private void logi(String s) {
+        Rlog.i(TAG + " [" + mPhone.getPhoneId() + "]", s);
+    }
 }
diff --git a/src/java/com/android/internal/telephony/SmsPermissions.java b/src/java/com/android/internal/telephony/SmsPermissions.java
index 44751ac..f6af69d 100644
--- a/src/java/com/android/internal/telephony/SmsPermissions.java
+++ b/src/java/com/android/internal/telephony/SmsPermissions.java
@@ -23,9 +23,11 @@
 import android.content.Intent;
 import android.os.Binder;
 import android.os.Build;
+import android.os.UserHandle;
 import android.service.carrier.CarrierMessagingService;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.util.TelephonyUtils;
 import com.android.telephony.Rlog;
 
 /**
@@ -132,7 +134,15 @@
      */
     public boolean checkCallingOrSelfCanGetSmscAddress(String callingPackage, String message) {
         // Allow it to the default SMS app always.
-        if (!isCallerDefaultSmsPackage(callingPackage)) {
+        boolean isDefaultSmsPackage;
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            isDefaultSmsPackage = isCallerDefaultSmsPackage(callingPackage);
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+
+        if (!isDefaultSmsPackage) {
             TelephonyPermissions
                         .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
                                 mContext, mPhone.getSubId(), message);
@@ -151,7 +161,15 @@
      */
     public boolean checkCallingOrSelfCanSetSmscAddress(String callingPackage, String message) {
         // Allow it to the default SMS app always.
-        if (!isCallerDefaultSmsPackage(callingPackage)) {
+        boolean isDefaultSmsPackage;
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            isDefaultSmsPackage = isCallerDefaultSmsPackage(callingPackage);
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+
+        if (!isDefaultSmsPackage) {
             // Allow it with MODIFY_PHONE_STATE or Carrier Privileges
             TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
                     mContext, mPhone.getSubId(), message);
@@ -163,7 +181,9 @@
     @VisibleForTesting
     public boolean isCallerDefaultSmsPackage(String packageName) {
         if (packageNameMatchesCallingUid(packageName)) {
-            return SmsApplication.isDefaultSmsApplication(mContext, packageName);
+            UserHandle userHandle = TelephonyUtils.getSubscriptionUserHandle(mContext,
+                    mPhone.getSubId());
+            return SmsApplication.isDefaultSmsApplicationAsUser(mContext, packageName, userHandle);
         }
         return false;
     }
diff --git a/src/java/com/android/internal/telephony/SmsStorageMonitor.java b/src/java/com/android/internal/telephony/SmsStorageMonitor.java
old mode 100755
new mode 100644
index 2375c73..2736f7a
--- a/src/java/com/android/internal/telephony/SmsStorageMonitor.java
+++ b/src/java/com/android/internal/telephony/SmsStorageMonitor.java
@@ -18,18 +18,22 @@
 
 import android.compat.annotation.UnsupportedAppUsage;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.res.Resources;
 import android.os.AsyncResult;
 import android.os.Build;
 import android.os.Handler;
 import android.os.Message;
 import android.os.PowerManager;
+import android.os.UserHandle;
 import android.provider.Telephony.Sms.Intents;
 import android.telephony.SubscriptionManager;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.util.TelephonyUtils;
 import com.android.telephony.Rlog;
 
 /**
@@ -40,7 +44,7 @@
  * dual-mode devices that require support for both 3GPP and 3GPP2 format messages.
  */
 public class SmsStorageMonitor extends Handler {
-    private static final String TAG = "SmsStorageMonitor";
+    private static final String TAG = "SmsStorageMonitor1";
 
     /** Maximum number of times to retry memory status reporting */
     private static final int MAX_RETRIES = 1;
@@ -83,6 +87,10 @@
     final CommandsInterface mCi;
     boolean mStorageAvailable = true;
 
+    boolean mInitialStorageAvailableStatus = true;
+
+    private boolean mMemoryStatusOverrideFlag = false;
+
     /**
      * Hold the wake lock for 5 seconds, which should be enough time for
      * any receiver(s) to grab its own wake lock.
@@ -111,6 +119,31 @@
         mContext.registerReceiver(mResultReceiver, filter);
     }
 
+    /**
+     * Overriding of the Memory Status by the TestApi and send the event to Handler to test
+     * the RP-SMMA feature
+     * @param isStorageAvailable boolean value specifies the MemoryStatus to be
+     * sent to Handler
+     */
+    public void sendMemoryStatusOverride(boolean isStorageAvailable) {
+        if (!mMemoryStatusOverrideFlag) {
+            mInitialStorageAvailableStatus = mStorageAvailable;
+            mMemoryStatusOverrideFlag = true;
+        }
+        mStorageAvailable = isStorageAvailable;
+        if (isStorageAvailable) {
+            sendMessage(obtainMessage(EVENT_REPORT_MEMORY_STATUS));
+        }
+    }
+
+    /**
+     * reset Memory Status change made by {@link #sendMemoryStatusOverride}
+     */
+    public void clearMemoryStatusOverride() {
+        mStorageAvailable = mInitialStorageAvailableStatus;
+        mMemoryStatusOverrideFlag = false;
+    }
+
     @VisibleForTesting
     public void setMaxRetries(int maxCount) {
         mMaxRetryCount = maxCount;
@@ -209,6 +242,18 @@
 
     private void sendMemoryStatusReport(boolean isAvailable) {
         mIsWaitingResponse = true;
+        Resources r = mContext.getResources();
+        if (r.getBoolean(com.android.internal.R.bool.config_smma_notification_supported_over_ims)) {
+            IccSmsInterfaceManager smsIfcMngr = mPhone.getIccSmsInterfaceManager();
+            if (smsIfcMngr != null) {
+                Rlog.d(TAG, "sendMemoryStatusReport: smsIfcMngr is available");
+                if (smsIfcMngr.mDispatchersController.isIms() && isAvailable) {
+                    smsIfcMngr.mDispatchersController.reportSmsMemoryStatus(
+                            obtainMessage(EVENT_REPORT_MEMORY_STATUS_DONE));
+                    return;
+                }
+            }
+        }
         mCi.reportSmsMemoryStatus(isAvailable, obtainMessage(EVENT_REPORT_MEMORY_STATUS_DONE));
     }
 
@@ -223,9 +268,14 @@
      * that SIM storage for SMS messages is full.
      */
     private void handleIccFull() {
+        UserHandle userHandle = TelephonyUtils.getSubscriptionUserHandle(mContext,
+                mPhone.getSubId());
+        ComponentName componentName = SmsApplication.getDefaultSimFullApplicationAsUser(mContext,
+                false, userHandle);
+
         // broadcast SIM_FULL intent
         Intent intent = new Intent(Intents.SIM_FULL_ACTION);
-        intent.setComponent(SmsApplication.getDefaultSimFullApplication(mContext, false));
+        intent.setComponent(componentName);
         mWakeLock.acquire(WAKE_LOCK_TIMEOUT);
         SubscriptionManager.putPhoneIdAndSubIdExtra(intent, mPhone.getPhoneId());
         mContext.sendBroadcast(intent, android.Manifest.permission.RECEIVE_SMS);
diff --git a/src/java/com/android/internal/telephony/SmsUsageMonitor.java b/src/java/com/android/internal/telephony/SmsUsageMonitor.java
index 8bcdc07..8e4ac60 100644
--- a/src/java/com/android/internal/telephony/SmsUsageMonitor.java
+++ b/src/java/com/android/internal/telephony/SmsUsageMonitor.java
@@ -43,6 +43,7 @@
 import org.xmlpull.v1.XmlPullParserException;
 import org.xmlpull.v1.XmlSerializer;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -73,6 +74,8 @@
 
     private static final String SHORT_CODE_PATH = "/data/misc/sms/codes";
 
+    private static final String SHORT_CODE_VERSION_PATH = "/data/misc/sms/metadata/version";
+
     /** Default checking period for SMS sent without user permission. */
     private static final int DEFAULT_SMS_CHECK_PERIOD = 60000;      // 1 minute
 
@@ -128,6 +131,8 @@
     /** Last modified time for pattern file */
     private long mPatternFileLastModified = 0;
 
+    private int mPatternFileVersion = -1;
+
     private RoleManager mRoleManager;
 
     /** Directory for per-app SMS permission XML file. */
@@ -415,9 +420,11 @@
                     if (mPatternFile.exists()) {
                         if (DBG) Rlog.d(TAG, "Loading SMS Short Code patterns from file");
                         mCurrentPatternMatcher = getPatternMatcherFromFile(countryIso);
+                        mPatternFileVersion = getPatternFileVersionFromFile();
                     } else {
                         if (DBG) Rlog.d(TAG, "Loading SMS Short Code patterns from resource");
                         mCurrentPatternMatcher = getPatternMatcherFromResource(countryIso);
+                        mPatternFileVersion = -1;
                     }
                     mCurrentCountry = countryIso;
                 }
@@ -655,6 +662,37 @@
         return false;
     }
 
+    private int getPatternFileVersionFromFile() {
+        File versionFile = new File(SHORT_CODE_VERSION_PATH);
+        if (versionFile.exists()) {
+            BufferedReader reader = null;
+            try {
+                reader = new BufferedReader(new FileReader(versionFile));
+                String version = reader.readLine();
+                if (version != null) {
+                    return Integer.parseInt(version);
+                }
+            } catch (IOException e) {
+                Rlog.e(TAG, "File reader exception reading short code "
+                        + "pattern file version", e);
+            } finally {
+                try {
+                    if (reader != null) {
+                        reader.close();
+                    }
+                } catch (IOException e) {
+                    Rlog.e(TAG, "File reader exception closing short code "
+                            + "pattern file version reader", e);
+                }
+            }
+        }
+        return -1;
+    }
+
+    public int getShortCodeXmlFileVersion() {
+        return mPatternFileVersion;
+    }
+
     private static void log(String msg) {
         Rlog.d(TAG, msg);
     }
diff --git a/src/java/com/android/internal/telephony/SrvccConnection.java b/src/java/com/android/internal/telephony/SrvccConnection.java
new file mode 100644
index 0000000..b831a59
--- /dev/null
+++ b/src/java/com/android/internal/telephony/SrvccConnection.java
@@ -0,0 +1,235 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_ACTIVE;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_ALERTING;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_DIALING;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_HOLDING;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_INCOMING;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_INCOMING_SETUP;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_WAITING;
+
+import android.net.Uri;
+import android.telephony.Annotation.PreciseCallStates;
+import android.telephony.ims.ImsCallProfile;
+import android.telephony.ims.ImsStreamMediaProfile;
+import android.text.TextUtils;
+
+import com.android.ims.internal.ConferenceParticipant;
+import com.android.internal.telephony.imsphone.ImsPhoneConnection;
+
+/**
+ * Connection information for SRVCC
+ */
+public class SrvccConnection {
+    private static final String TAG = "SrvccConnection";
+
+    public static final int CALL_TYPE_NORMAL = 0;
+    public static final int CALL_TYPE_EMERGENCY = 1;
+
+    public static final int SUBSTATE_NONE = 0;
+    /** Pre-alerting state. Applicable for MT calls only */
+    public static final int SUBSTATE_PREALERTING = 1;
+
+    public static final int TONE_NONE = 0;
+    public static final int TONE_LOCAL = 1;
+    public static final int TONE_NETWORK = 2;
+
+    /** Values are CALL_TYPE_ */
+    private int mType = CALL_TYPE_NORMAL;
+
+    /** Values are Call.State */
+    private Call.State mState;
+
+    /** Values are SUBSTATE_ */
+    private int mSubstate = SUBSTATE_NONE;
+
+    /** Values are TONE_ */
+    private int mRingbackToneType = TONE_NONE;
+
+    /** true if it is a multi-party call */
+    private boolean mIsMpty = false;
+
+    /** true if it is a mobile terminated call */
+    private boolean mIsMT;
+
+    /** Remote party nummber */
+    private String mNumber;
+
+    /** Values are PhoneConstants.PRESENTATION_ */
+    private int mNumPresentation;
+
+    /** Remote party name */
+    private String mName;
+
+    /** Values are PhoneConstants.PRESENTATION_ */
+    private int mNamePresentation;
+
+    public SrvccConnection(ImsCallProfile profile,
+            ImsPhoneConnection c, @PreciseCallStates int preciseCallState) {
+        mState = toCallState(preciseCallState);
+        if (mState == Call.State.ALERTING) {
+            mRingbackToneType = isLocalTone(profile) ? TONE_LOCAL : TONE_NETWORK;
+        }
+        if (preciseCallState == PRECISE_CALL_STATE_INCOMING_SETUP) {
+            mSubstate = SUBSTATE_PREALERTING;
+        }
+
+        if (c == null) {
+            initialize(profile);
+        } else {
+            initialize(c);
+        }
+    }
+
+    public SrvccConnection(ConferenceParticipant cp, @PreciseCallStates int preciseCallState) {
+        mState = toCallState(preciseCallState);
+        mIsMT = cp.getCallDirection() == android.telecom.Call.Details.DIRECTION_INCOMING;
+        mNumber = getParticipantAddress(cp.getHandle());
+        mNumPresentation = cp.getParticipantPresentation();
+        if (mNumPresentation == PhoneConstants.PRESENTATION_RESTRICTED) {
+            mNumber = "";
+        }
+        mName = cp.getDisplayName();
+        if (!TextUtils.isEmpty(mName)) {
+            mNamePresentation = PhoneConstants.PRESENTATION_ALLOWED;
+        } else {
+            mNamePresentation = PhoneConstants.PRESENTATION_UNKNOWN;
+        }
+        mIsMpty = true;
+    }
+
+    private static String getParticipantAddress(Uri address) {
+        if (address == null) {
+            return null;
+        }
+
+        String number = address.getSchemeSpecificPart();
+        if (TextUtils.isEmpty(number)) {
+            return null;
+        }
+
+        String[] numberParts = number.split("[@;:]");
+        if (numberParts.length == 0) return null;
+
+        return numberParts[0];
+    }
+
+    // MT call in alerting or prealerting state
+    private void initialize(ImsCallProfile profile) {
+        mIsMT = true;
+        mNumber = profile.getCallExtra(ImsCallProfile.EXTRA_OI);
+        mName = profile.getCallExtra(ImsCallProfile.EXTRA_CNA);
+        mNumPresentation = ImsCallProfile.OIRToPresentation(
+                profile.getCallExtraInt(ImsCallProfile.EXTRA_OIR));
+        mNamePresentation = ImsCallProfile.OIRToPresentation(
+                profile.getCallExtraInt(ImsCallProfile.EXTRA_CNAP));
+    }
+
+    private void initialize(ImsPhoneConnection c) {
+        if (c.isEmergencyCall()) {
+            mType = CALL_TYPE_EMERGENCY;
+        }
+        mIsMT = c.isIncoming();
+        mNumber = c.getAddress();
+        mNumPresentation = c.getNumberPresentation();
+        mName = c.getCnapName();
+        mNamePresentation = c.getCnapNamePresentation();
+    }
+
+    private boolean isLocalTone(ImsCallProfile profile) {
+        if (profile == null) return false;
+
+        ImsStreamMediaProfile mediaProfile = profile.getMediaProfile();
+        if (mediaProfile == null) return false;
+
+        boolean shouldPlayRingback =
+                (mediaProfile.getAudioDirection() == ImsStreamMediaProfile.DIRECTION_INACTIVE)
+                        ? true : false;
+        return shouldPlayRingback;
+    }
+
+    private static Call.State toCallState(int preciseCallState) {
+        switch (preciseCallState) {
+            case PRECISE_CALL_STATE_ACTIVE: return Call.State.ACTIVE;
+            case PRECISE_CALL_STATE_HOLDING: return Call.State.HOLDING;
+            case PRECISE_CALL_STATE_DIALING: return Call.State.DIALING;
+            case PRECISE_CALL_STATE_ALERTING: return Call.State.ALERTING;
+            case PRECISE_CALL_STATE_INCOMING: return Call.State.INCOMING;
+            case PRECISE_CALL_STATE_WAITING: return Call.State.WAITING;
+            case PRECISE_CALL_STATE_INCOMING_SETUP: return Call.State.INCOMING;
+            default:
+        }
+        return Call.State.DISCONNECTED;
+    }
+
+    /** Returns the type of the call */
+    public int getType() {
+        return mType;
+    }
+
+    /** Returns the state */
+    public Call.State getState() {
+        return mState;
+    }
+
+    /** Updates the state */
+    public void setState(Call.State state) {
+        mState = state;
+    }
+
+    /** Returns the sub state */
+    public int getSubState() {
+        return mSubstate;
+    }
+
+    /** Returns the ringback tone type */
+    public int getRingbackToneType() {
+        return mRingbackToneType;
+    }
+
+    /** true if it is a multi-party call */
+    public boolean isMultiParty() {
+        return mIsMpty;
+    }
+
+    /** true if it is a mobile terminated call */
+    public boolean isIncoming() {
+        return mIsMT;
+    }
+
+    /** Returns the remote party nummber */
+    public String getNumber() {
+        return mNumber;
+    }
+
+    /** Returns the number presentation */
+    public int getNumberPresentation() {
+        return mNumPresentation;
+    }
+
+    /** Returns the remote party name */
+    public String getName() {
+        return mName;
+    }
+
+    /** Returns the name presentation */
+    public int getNamePresentation() {
+        return mNamePresentation;
+    }
+}
diff --git a/src/java/com/android/internal/telephony/SubscriptionController.java b/src/java/com/android/internal/telephony/SubscriptionController.java
index 088cc67..af759ea 100644
--- a/src/java/com/android/internal/telephony/SubscriptionController.java
+++ b/src/java/com/android/internal/telephony/SubscriptionController.java
@@ -37,6 +37,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
+import android.content.res.Resources;
 import android.database.ContentObserver;
 import android.database.Cursor;
 import android.net.Uri;
@@ -74,9 +75,8 @@
 import android.util.Log;
 
 import com.android.ims.ImsManager;
+import com.android.internal.R;
 import com.android.internal.annotations.VisibleForTesting;
-import com.android.internal.telephony.IccCardConstants.State;
-import com.android.internal.telephony.data.DataEnabledOverride;
 import com.android.internal.telephony.data.PhoneSwitcher;
 import com.android.internal.telephony.metrics.TelephonyMetrics;
 import com.android.internal.telephony.uicc.IccUtils;
@@ -166,7 +166,7 @@
     protected UiccController mUiccController;
 
     /**
-     * Apps targeting on Android T and beyond will get an empty list if there is no access to device
+     * Apps targeting on Android T and beyond will get exception if there is no access to device
      * identifiers nor has carrier privileges when calling
      * SubscriptionManager#getSubscriptionsInGroup.
      */
@@ -310,11 +310,12 @@
             SubscriptionManager.WFC_IMS_ROAMING_ENABLED,
             SubscriptionManager.DATA_ROAMING,
             SubscriptionManager.DISPLAY_NAME,
-            SubscriptionManager.DATA_ENABLED_OVERRIDE_RULES,
+            SubscriptionManager.ENABLED_MOBILE_DATA_POLICIES,
             SubscriptionManager.UICC_APPLICATIONS_ENABLED,
             SubscriptionManager.IMS_RCS_UCE_ENABLED,
             SubscriptionManager.CROSS_SIM_CALLING_ENABLED,
-            SubscriptionManager.NR_ADVANCED_CALLING_ENABLED
+            SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
+            SubscriptionManager.USER_HANDLE
     ));
 
     public static SubscriptionController init(Context c) {
@@ -330,8 +331,11 @@
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     public static SubscriptionController getInstance() {
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            throw new RuntimeException("getInstance should not be called.");
+        }
         if (sInstance == null) {
-           Log.wtf(LOG_TAG, "getInstance null");
+            Log.wtf(LOG_TAG, "getInstance null");
         }
 
         return sInstance;
@@ -465,6 +469,11 @@
                 Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
     }
 
+    private void enforceManageSubscriptionUserAssociation(String message) {
+        mContext.enforceCallingOrSelfPermission(
+                Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION, message);
+    }
+
     /**
      * Returns whether the {@code callingPackage} has access to subscriber identifiers on the
      * specified {@code subId} using the provided {@code message} in any resulting
@@ -498,17 +507,6 @@
     }
 
     /**
-     * Broadcast when SubscriptionInfo has changed
-     * FIXME: Hopefully removed if the API council accepts SubscriptionInfoListener
-     */
-     private void broadcastSimInfoContentChanged() {
-        Intent intent = new Intent(TelephonyIntents.ACTION_SUBINFO_CONTENT_CHANGE);
-        mContext.sendBroadcast(intent);
-        intent = new Intent(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED);
-        mContext.sendBroadcast(intent);
-     }
-
-    /**
      * Notify the changed of subscription info.
      */
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@@ -519,9 +517,6 @@
         if (DBG) logd("notifySubscriptionInfoChanged:");
         trm.notifySubscriptionInfoChanged();
 
-        // FIXME: Remove if listener technique accepted.
-        broadcastSimInfoContentChanged();
-
         MultiSimSettingController.getInstance().notifySubscriptionInfoChanged();
         TelephonyMetrics metrics = TelephonyMetrics.getInstance();
         List<SubscriptionInfo> subInfos;
@@ -537,33 +532,34 @@
 
     /**
      * New SubInfoRecord instance and fill in detail info
-     * @param cursor
+     * @param cursor The database cursor
      * @return the query result of desired SubInfoRecord
      */
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private SubscriptionInfo getSubInfoRecord(Cursor cursor) {
+        SubscriptionInfo.Builder builder = new SubscriptionInfo.Builder();
         int id = cursor.getInt(cursor.getColumnIndexOrThrow(
                 SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID));
-        String iccId = cursor.getString(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.ICC_ID));
-        int simSlotIndex = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.SIM_SLOT_INDEX));
-        String displayName = cursor.getString(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.DISPLAY_NAME));
-        String carrierName = cursor.getString(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.CARRIER_NAME));
-        int nameSource = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.NAME_SOURCE));
-        int iconTint = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.HUE));
-        String number = cursor.getString(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.NUMBER));
-        int dataRoaming = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.DATA_ROAMING));
-        String mcc = cursor.getString(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.MCC_STRING));
-        String mnc = cursor.getString(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.MNC_STRING));
+        builder.setId(id)
+                .setIccId(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.ICC_ID)))
+                .setSimSlotIndex(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.SIM_SLOT_INDEX)))
+                .setDisplayName(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.DISPLAY_NAME)))
+                .setCarrierName(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.CARRIER_NAME)))
+                .setDisplayNameSource(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.NAME_SOURCE)))
+                .setIconTint(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.HUE)))
+                .setDataRoaming(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.DATA_ROAMING)))
+                .setMcc(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.MCC_STRING)))
+                .setMnc(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.MNC_STRING)));
+
         String ehplmnsRaw = cursor.getString(cursor.getColumnIndexOrThrow(
                 SubscriptionManager.EHPLMNS));
         String hplmnsRaw = cursor.getString(cursor.getColumnIndexOrThrow(
@@ -571,77 +567,63 @@
         String[] ehplmns = ehplmnsRaw == null ? null : ehplmnsRaw.split(",");
         String[] hplmns = hplmnsRaw == null ? null : hplmnsRaw.split(",");
 
-        // cardId is the private ICCID/EID string, also known as the card string
-        String cardId = cursor.getString(cursor.getColumnIndexOrThrow(
+        builder.setEhplmns(ehplmns).setHplmns(hplmns);
+
+
+        // CARD_ID is the private ICCID/EID string, also known as the card string
+        String cardString = cursor.getString(cursor.getColumnIndexOrThrow(
                 SubscriptionManager.CARD_ID));
-        String countryIso = cursor.getString(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.ISO_COUNTRY_CODE));
+        builder.setCardString(cardString);
         // publicCardId is the publicly exposed int card ID
-        int publicCardId = mUiccController.convertToPublicCardId(cardId);
+        int publicCardId = mUiccController.convertToPublicCardId(cardString);
+        builder.setCardId(publicCardId);
+
+        builder.setCountryIso(cursor.getString(cursor.getColumnIndexOrThrow(
+                SubscriptionManager.ISO_COUNTRY_CODE)))
+                .setCarrierId(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.CARRIER_ID)));
+
         boolean isEmbedded = cursor.getInt(cursor.getColumnIndexOrThrow(
                 SubscriptionManager.IS_EMBEDDED)) == 1;
-        int carrierId = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.CARRIER_ID));
-        UiccAccessRule[] accessRules;
+        builder.setEmbedded(isEmbedded);
         if (isEmbedded) {
-            accessRules = UiccAccessRule.decodeRules(cursor.getBlob(
-                    cursor.getColumnIndexOrThrow(SubscriptionManager.ACCESS_RULES)));
-        } else {
-            accessRules = null;
+            builder.setNativeAccessRules(UiccAccessRule.decodeRules(cursor.getBlob(
+                            cursor.getColumnIndexOrThrow(SubscriptionManager.ACCESS_RULES))));
         }
-        UiccAccessRule[] carrierConfigAccessRules = UiccAccessRule.decodeRules(cursor.getBlob(
-            cursor.getColumnIndexOrThrow(SubscriptionManager.ACCESS_RULES_FROM_CARRIER_CONFIGS)));
-        boolean isOpportunistic = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.IS_OPPORTUNISTIC)) == 1;
-        String groupUUID = cursor.getString(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.GROUP_UUID));
-        int profileClass = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.PROFILE_CLASS));
-        int portIndex = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.PORT_INDEX));
-        int subType = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.SUBSCRIPTION_TYPE));
-        String groupOwner = getOptionalStringFromCursor(cursor, SubscriptionManager.GROUP_OWNER,
-                /*defaultVal*/ null);
-        boolean areUiccApplicationsEnabled = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.UICC_APPLICATIONS_ENABLED)) == 1;
 
-        int usageSetting = cursor.getInt(cursor.getColumnIndexOrThrow(
-                SubscriptionManager.USAGE_SETTING));
-
-        if (VDBG) {
-            String iccIdToPrint = SubscriptionInfo.givePrintableIccid(iccId);
-            String cardIdToPrint = SubscriptionInfo.givePrintableIccid(cardId);
-            logd("[getSubInfoRecord] id:" + id + " iccid:" + iccIdToPrint + " simSlotIndex:"
-                    + simSlotIndex + " carrierid:" + carrierId + " displayName:" + displayName
-                    + " nameSource:" + nameSource + " iconTint:" + iconTint
-                    + " dataRoaming:" + dataRoaming + " mcc:" + mcc + " mnc:" + mnc
-                    + " countIso:" + countryIso + " isEmbedded:"
-                    + isEmbedded + " accessRules:" + Arrays.toString(accessRules)
-                    + " carrierConfigAccessRules: " + Arrays.toString(carrierConfigAccessRules)
-                    + " cardId:" + cardIdToPrint + " portIndex:" + portIndex
-                    + " publicCardId:" + publicCardId
-                    + " isOpportunistic:" + isOpportunistic + " groupUUID:" + groupUUID
-                    + " profileClass:" + profileClass + " subscriptionType: " + subType
-                    + " areUiccApplicationsEnabled: " + areUiccApplicationsEnabled
-                    + " usageSetting: " + usageSetting);
-        }
+        builder.setCarrierConfigAccessRules(UiccAccessRule.decodeRules(cursor.getBlob(
+                cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.ACCESS_RULES_FROM_CARRIER_CONFIGS))))
+                .setOpportunistic(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.IS_OPPORTUNISTIC)) == 1)
+                .setGroupUuid(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.GROUP_UUID)))
+                .setProfileClass(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.PROFILE_CLASS)))
+                .setPortIndex(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.PORT_INDEX)))
+                .setType(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.SUBSCRIPTION_TYPE)))
+                .setGroupOwner(getOptionalStringFromCursor(cursor, SubscriptionManager.GROUP_OWNER,
+                        /*defaultVal*/ null))
+                .setUiccApplicationsEnabled(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.UICC_APPLICATIONS_ENABLED)) == 1)
+                .setUsageSetting(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SubscriptionManager.USAGE_SETTING)));
 
         // If line1number has been set to a different number, use it instead.
+        String number = cursor.getString(cursor.getColumnIndexOrThrow(
+                SubscriptionManager.NUMBER));
         String line1Number = mTelephonyManager.getLine1Number(id);
         if (!TextUtils.isEmpty(line1Number) && !line1Number.equals(number)) {
             number = line1Number;
         }
+        builder.setNumber(number);
+
         // FIXME(b/210771052): constructing a complete SubscriptionInfo requires a port index,
         // but the port index isn't available here. Should it actually be part of SubscriptionInfo?
-        SubscriptionInfo info = new SubscriptionInfo(id, iccId, simSlotIndex, displayName,
-                carrierName, nameSource, iconTint, number, dataRoaming, /* icon= */ null,
-                mcc, mnc, countryIso, isEmbedded, accessRules, cardId, publicCardId,
-                isOpportunistic, groupUUID, /* isGroupDisabled= */ false , carrierId, profileClass,
-                subType, groupOwner, carrierConfigAccessRules, areUiccApplicationsEnabled,
-                portIndex, usageSetting);
-        info.setAssociatedPlmns(ehplmns, hplmns);
-        return info;
+
+        return builder.build();
     }
 
     private String getOptionalStringFromCursor(Cursor cursor, String column, String defaultVal) {
@@ -840,13 +822,14 @@
                 for (SubscriptionInfo si : subList) {
                     if (iccId.equals(si.getIccId())) {
                         if (DBG)
-                            logd("[getActiveSubInfoUsingIccId]+ iccId=" + iccId + " subInfo=" + si);
+                            logd("[getActiveSubInfoUsingIccId]+ iccId="
+                                    + Rlog.pii(LOG_TAG, iccId) + " subInfo=" + si);
                         return si;
                     }
                 }
             }
             if (DBG) {
-                logd("[getActiveSubInfoUsingIccId]+ iccId=" + iccId
+                logd("[getActiveSubInfoUsingIccId]+ iccId=" + Rlog.pii(LOG_TAG, iccId)
                         + " subList=" + subList + " subInfo=null");
             }
         } finally {
@@ -1074,7 +1057,6 @@
      * @param callingFeatureId The feature in the package
      * @return all SIM count in database, include what was inserted before
      */
-    @Override
     public int getAllSubInfoCount(String callingPackage, String callingFeatureId) {
         if (DBG) logd("[getAllSubInfoCount]+");
 
@@ -1327,17 +1309,6 @@
     }
 
     /**
-     * Add a new SubInfoRecord to subinfo database if needed
-     * @param iccId the IccId of the SIM card
-     * @param slotIndex the slot which the SIM is inserted
-     * @return 0 if success, < 0 on error.
-     */
-    @Override
-    public int addSubInfoRecord(String iccId, int slotIndex) {
-        return addSubInfo(iccId, null, slotIndex, SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
-    }
-
-    /**
      * Add a new subscription info record, if needed.
      * @param uniqueId This is the unique identifier for the subscription within the specific
      *                 subscription type.
@@ -1540,7 +1511,7 @@
                 notifySubscriptionInfoChanged();
             } else {  // Handle Local SIM devices
                 // Set Display name after sub id is set above so as to get valid simCarrierName
-                int subId = getSubIdUsingPhoneId(slotIndex);
+                int subId = getSubId(slotIndex);
                 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
                     if (DBG) {
                         logdl("[addSubInfoRecord]- getSubId failed invalid subId = " + subId);
@@ -1554,9 +1525,9 @@
                     if (!TextUtils.isEmpty(simCarrierName)) {
                         nameToSet = simCarrierName;
                     } else {
-                        nameToSet = "CARD " + Integer.toString(slotIndex + 1);
+                        Resources r = Resources.getSystem();
+                        nameToSet = r.getString(R.string.default_card_name, (slotIndex + 1));
                     }
-
                     ContentValues value = new ContentValues();
                     value.put(SubscriptionManager.DISPLAY_NAME, nameToSet);
                     resolver.update(SubscriptionManager.getUriForSubscriptionId(subId), value,
@@ -1815,7 +1786,7 @@
     public boolean setPlmnSpn(int slotIndex, boolean showPlmn, String plmn, boolean showSpn,
                               String spn) {
         synchronized (mLock) {
-            int subId = getSubIdUsingPhoneId(slotIndex);
+            int subId = getSubId(slotIndex);
             if (mContext.getPackageManager().resolveContentProvider(
                     SubscriptionManager.CONTENT_URI.getAuthority(), 0) == null ||
                     !SubscriptionManager.isValidSubscriptionId(subId)) {
@@ -1889,12 +1860,14 @@
 
     /**
      * Set SIM color tint by simInfo index
-     * @param tint the tint color of the SIM
+     *
      * @param subId the unique SubInfoRecord index in database
+     * @param tint the tint color of the SIM
+     *
      * @return the number of records updated
      */
     @Override
-    public int setIconTint(int tint, int subId) {
+    public int setIconTint(int subId, int tint) {
         if (DBG) logd("[setIconTint]+ tint:" + tint + " subId:" + subId);
 
         enforceModifyPhoneState("setIconTint");
@@ -1955,7 +1928,7 @@
 
         String spn;
 
-        switch (subInfo.getNameSource()) {
+        switch (subInfo.getDisplayNameSource()) {
             case SubscriptionManager.NAME_SOURCE_SIM_PNN:
                 String pnn = phone.getPlmn();
                 return !TextUtils.isEmpty(pnn);
@@ -2023,7 +1996,7 @@
             // if there is no sub in the db, return 0 since subId does not exist in db
             if (allSubInfo == null || allSubInfo.isEmpty()) return 0;
             for (SubscriptionInfo subInfo : allSubInfo) {
-                int subInfoNameSource = subInfo.getNameSource();
+                int subInfoNameSource = subInfo.getDisplayNameSource();
                 boolean isHigherPriority = (getNameSourcePriority(subInfoNameSource)
                         > getNameSourcePriority(nameSource));
                 boolean isEqualPriorityAndName = (getNameSourcePriority(subInfoNameSource)
@@ -2045,7 +2018,9 @@
                 if (TextUtils.isEmpty(nameToSet)) {
                     if (nameSource == SubscriptionManager.NAME_SOURCE_USER_INPUT
                             && SubscriptionManager.isValidSlotIndex(getSlotIndex(subId))) {
-                        nameToSet = "CARD " + (getSlotIndex(subId) + 1);
+                        Resources r = Resources.getSystem();
+                        nameToSet = r.getString(R.string.default_card_name,
+                                (getSlotIndex(subId) + 1));
                     } else {
                         nameToSet = mContext.getString(SubscriptionManager.DEFAULT_NAME_RES);
                     }
@@ -2339,10 +2314,11 @@
             case SubscriptionManager.IMS_RCS_UCE_ENABLED:
             case SubscriptionManager.CROSS_SIM_CALLING_ENABLED:
             case SubscriptionManager.NR_ADVANCED_CALLING_ENABLED:
+            case SubscriptionManager.USER_HANDLE:
                 values.put(propKey, cursor.getInt(columnIndex));
                 break;
             case SubscriptionManager.DISPLAY_NAME:
-            case SubscriptionManager.DATA_ENABLED_OVERRIDE_RULES:
+            case SubscriptionManager.ENABLED_MOBILE_DATA_POLICIES:
                 values.put(propKey, cursor.getString(columnIndex));
                 break;
             default:
@@ -2644,13 +2620,13 @@
     }
 
     /**
-     * Return the subId for specified slot Id.
+     * Return the subIds for specified slot Id.
      * @deprecated
      */
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     @Override
     @Deprecated
-    public int[] getSubId(int slotIndex) {
+    public int[] getSubIds(int slotIndex) {
         if (VDBG) printStackTrace("[getSubId]+ slotIndex=" + slotIndex);
 
         // Map default slotIndex to the current default subId.
@@ -2744,7 +2720,6 @@
     /**
      * @return the number of records cleared
      */
-    @Override
     public int clearSubInfo() {
         enforceModifyPhoneState("clearSubInfo");
 
@@ -3044,9 +3019,8 @@
 
     // FIXME: We need we should not be assuming phoneId == slotIndex as it will not be true
     // when there are multiple subscriptions per sim and probably for other reasons.
-    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
-    public int getSubIdUsingPhoneId(int phoneId) {
-        int[] subIds = getSubId(phoneId);
+    public int getSubId(int phoneId) {
+        int[] subIds = getSubIds(phoneId);
         if (subIds == null || subIds.length == 0) {
             return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
         }
@@ -3190,46 +3164,6 @@
     }
 
     /**
-     * Get the SIM state for the slot index.
-     * For Remote-SIMs, this method returns {@link #IccCardConstants.State.UNKNOWN}
-     * @return SIM state as the ordinal of {@See IccCardConstants.State}
-     */
-    @Override
-    public int getSimStateForSlotIndex(int slotIndex) {
-        State simState;
-        String err;
-        if (slotIndex < 0) {
-            simState = IccCardConstants.State.UNKNOWN;
-            err = "invalid slotIndex";
-        } else {
-            Phone phone = null;
-            try {
-                phone = PhoneFactory.getPhone(slotIndex);
-            } catch (IllegalStateException e) {
-                // ignore
-            }
-            if (phone == null) {
-                simState = IccCardConstants.State.UNKNOWN;
-                err = "phone == null";
-            } else {
-                IccCard icc = phone.getIccCard();
-                if (icc == null) {
-                    simState = IccCardConstants.State.UNKNOWN;
-                    err = "icc == null";
-                } else {
-                    simState = icc.getState();
-                    err = "";
-                }
-            }
-        }
-        if (VDBG) {
-            logd("getSimStateForSlotIndex: " + err + " simState=" + simState
-                    + " ordinal=" + simState.ordinal() + " slotIndex=" + slotIndex);
-        }
-        return simState.ordinal();
-    }
-
-    /**
      * Store properties associated with SubscriptionInfo in database
      * @param subId Subscription Id of Subscription
      * @param propKey Column name in database associated with SubscriptionInfo
@@ -3286,6 +3220,7 @@
             case SubscriptionManager.VOIMS_OPT_IN_STATUS:
             case SubscriptionManager.NR_ADVANCED_CALLING_ENABLED:
             case SubscriptionManager.USAGE_SETTING:
+            case SubscriptionManager.USER_HANDLE:
                 value.put(propKey, Integer.parseInt(propValue));
                 break;
             case SubscriptionManager.ALLOWED_NETWORK_TYPES:
@@ -3373,7 +3308,7 @@
                         case SubscriptionManager.CROSS_SIM_CALLING_ENABLED:
                         case SubscriptionManager.IS_OPPORTUNISTIC:
                         case SubscriptionManager.GROUP_UUID:
-                        case SubscriptionManager.DATA_ENABLED_OVERRIDE_RULES:
+                        case SubscriptionManager.ENABLED_MOBILE_DATA_POLICIES:
                         case SubscriptionManager.ALLOWED_NETWORK_TYPES:
                         case SubscriptionManager.D2D_STATUS_SHARING:
                         case SubscriptionManager.VOIMS_OPT_IN_STATUS:
@@ -3382,6 +3317,7 @@
                         case SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER:
                         case SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS:
                         case SubscriptionManager.USAGE_SETTING:
+                        case SubscriptionManager.USER_HANDLE:
                             resultValue = cursor.getString(0);
                             break;
                         default:
@@ -3426,12 +3362,7 @@
             pw.println(" defaultDataSubId=" + getDefaultDataSubId());
             pw.println(" defaultVoiceSubId=" + getDefaultVoiceSubId());
             pw.println(" defaultSmsSubId=" + getDefaultSmsSubId());
-
-            pw.println(" defaultDataPhoneId=" + SubscriptionManager
-                    .from(mContext).getDefaultDataPhoneId());
             pw.println(" defaultVoicePhoneId=" + SubscriptionManager.getDefaultVoicePhoneId());
-            pw.println(" defaultSmsPhoneId=" + SubscriptionManager
-                    .from(mContext).getDefaultSmsPhoneId());
             pw.flush();
 
             for (Entry<Integer, ArrayList<Integer>> entry : mSlotIndexToSubIds.entrySet()) {
@@ -4027,20 +3958,22 @@
         }
 
         // If the calling app neither has carrier privileges nor READ_PHONE_STATE and access to
-        // device identifiers, it will return an empty list.
+        // device identifiers, it will throw a securityException.
         if (CompatChanges.isChangeEnabled(
                 REQUIRE_DEVICE_IDENTIFIERS_FOR_GROUP_UUID, Binder.getCallingUid())) {
             try {
                 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mContext,
                         callingPackage, callingFeatureId, "getSubscriptionsInGroup")) {
                     EventLog.writeEvent(0x534e4554, "213902861", Binder.getCallingUid());
-                    return new ArrayList<>();
+                    throw new SecurityException("Need to have carrier privileges or access to "
+                            + "device identifiers to call getSubscriptionsInGroup");
                 }
             } catch (SecurityException e) {
                 EventLog.writeEvent(0x534e4554, "213902861", Binder.getCallingUid());
-                return new ArrayList<>();
+                throw e;
             }
         }
+
         return subInfoList.stream().filter(info -> {
             if (!groupUuid.equals(info.getGroupUuid())) return false;
             int subId = info.getSubscriptionId();
@@ -4061,7 +3994,7 @@
      * @return true if sub/group is the same, false otherwise
      */
     public boolean checkPhoneIdAndIccIdMatch(int phoneId, String iccid) {
-        int subId = getSubIdUsingPhoneId(phoneId);
+        int subId = getSubId(phoneId);
         if (!SubscriptionManager.isUsableSubIdValue(subId)) return false;
         ParcelUuid groupUuid = getGroupUuid(subId);
         List<SubscriptionInfo> subInfoList;
@@ -4186,7 +4119,10 @@
         }
 
         // Can't find the existing SIM.
-        if (slotInfo == null) return false;
+        if (slotInfo == null) {
+            loge("Can't find the existing SIM.");
+            return false;
+        }
 
         // this for physical slot which has only one port
         if (enable && !slotInfo.getPorts().stream().findFirst().get().isActive()) {
@@ -4331,7 +4267,7 @@
                         Settings.Global.ENABLED_SUBSCRIPTION_FOR_SLOT + physicalSlotIndex);
             } catch (Settings.SettingNotFoundException e) {
                 // Value never set. Return whether it's currently active.
-                subId = getSubIdUsingPhoneId(logicalSlotIndex);
+                subId = getSubId(logicalSlotIndex);
             }
 
             return subId;
@@ -4436,16 +4372,16 @@
         if (hasIdentifierAccess && hasPhoneNumberAccess) {
             return subInfo;
         }
-        SubscriptionInfo result = new SubscriptionInfo(subInfo);
+        SubscriptionInfo.Builder result = new SubscriptionInfo.Builder(subInfo);
         if (!hasIdentifierAccess) {
-            result.clearIccId();
-            result.clearCardString();
-            result.clearGroupUuid();
+            result.setIccId(null);
+            result.setCardString(null);
+            result.setGroupUuid(null);
         }
         if (!hasPhoneNumberAccess) {
-            result.clearNumber();
+            result.setNumber(null);
         }
-        return result;
+        return result.build();
     }
 
     private synchronized boolean addToSubIdList(int slotIndex, int subId, int subscriptionType) {
@@ -4520,20 +4456,23 @@
 
             mCacheOpportunisticSubInfoList = subList;
 
-            for (SubscriptionInfo info : mCacheOpportunisticSubInfoList) {
+            for (int i = 0; i < mCacheOpportunisticSubInfoList.size(); i++) {
+                SubscriptionInfo info = mCacheOpportunisticSubInfoList.get(i);
                 if (shouldDisableSubGroup(info.getGroupUuid())) {
-                    info.setGroupDisabled(true);
+                    SubscriptionInfo.Builder builder = new SubscriptionInfo.Builder(info);
+                    builder.setGroupDisabled(true);
+                    mCacheOpportunisticSubInfoList.set(i, builder.build());
                 }
             }
 
             if (DBG_CACHE) {
                 if (!mCacheOpportunisticSubInfoList.isEmpty()) {
                     for (SubscriptionInfo si : mCacheOpportunisticSubInfoList) {
-                        logd("[refreshCachedOpptSubscriptionInfoList] Setting Cached info="
-                                + si);
+                        logd("[refreshCachedOpportunisticSubscriptionInfoList] Setting Cached "
+                                + "info=" + si);
                     }
                 } else {
-                    logdl("[refreshCachedOpptSubscriptionInfoList]- no info return");
+                    logdl("[refreshCachedOpportunisticSubscriptionInfoList]- no info return");
                 }
             }
 
@@ -4558,19 +4497,19 @@
     }
 
     /**
-     * Set allowing mobile data during voice call.
+     * Set enabled mobile data policies.
      *
      * @param subId Subscription index
-     * @param rules Data enabled override rules in string format. See {@link DataEnabledOverride}
-     * for details.
+     * @param policies Mobile data policies in string format.
+     *                 See {@link TelephonyManager.MobileDataPolicy} for details.
      * @return {@code true} if settings changed, otherwise {@code false}.
      */
-    public boolean setDataEnabledOverrideRules(int subId, @NonNull String rules) {
-        if (DBG) logd("[setDataEnabledOverrideRules]+ rules:" + rules + " subId:" + subId);
+    public boolean setEnabledMobileDataPolicies(int subId, @NonNull String policies) {
+        if (DBG) logd("[setEnabledMobileDataPolicies]+ policies:" + policies + " subId:" + subId);
 
         validateSubId(subId);
         ContentValues value = new ContentValues(1);
-        value.put(SubscriptionManager.DATA_ENABLED_OVERRIDE_RULES, rules);
+        value.put(SubscriptionManager.ENABLED_MOBILE_DATA_POLICIES, policies);
 
         boolean result = updateDatabase(value, subId, true) > 0;
 
@@ -4584,15 +4523,16 @@
     }
 
     /**
-     * Get data enabled override rules.
+     * Get enabled mobile data policies.
      *
      * @param subId Subscription index
-     * @return Data enabled override rules in string
+     * @return Enabled mobile data policies joined by "," (ie. "1,2") or an empty string if no
+     * policies are enabled.
      */
     @NonNull
-    public String getDataEnabledOverrideRules(int subId) {
+    public String getEnabledMobileDataPolicies(int subId) {
         return TelephonyUtils.emptyIfNull(getSubscriptionProperty(subId,
-                SubscriptionManager.DATA_ENABLED_OVERRIDE_RULES));
+                SubscriptionManager.ENABLED_MOBILE_DATA_POLICIES));
     }
 
     /**
@@ -4714,7 +4654,7 @@
      * Sets the phone number for the given {@code subId}.
      *
      * <p>The only accepted {@code source} is {@link
-     * SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER}.
+     * SubscriptionManager#PHONE_NUMBER_SOURCE_CARRIER}.
      */
     @Override
     public void setPhoneNumber(int subId, int source, String number,
@@ -4781,6 +4721,229 @@
     }
 
     /**
+     * Querying last used TP - MessageRef for particular subId from SIMInfo table.
+     * @return messageRef
+     */
+    public int getMessageRef(int subId) {
+        try (Cursor cursor = mContext.getContentResolver().query(SubscriptionManager.CONTENT_URI,
+                new String[]{SubscriptionManager.TP_MESSAGE_REF},
+                SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID + "=\"" + subId
+                        + "\"", null, null)) {
+            try {
+                if (cursor != null && cursor.moveToFirst()) {
+                    do {
+                        return cursor.getInt(cursor.getColumnIndexOrThrow(
+                                SubscriptionManager.TP_MESSAGE_REF));
+                    } while (cursor.moveToNext());
+                } else {
+                    if (DBG) logd("Valid row not present in db");
+                }
+            } catch (Exception e) {
+                if (DBG) logd("Query failed " + e.getMessage());
+            } finally {
+                if (cursor != null) {
+                    cursor.close();
+                }
+            }
+        }
+        return -1;
+    }
+
+    /**
+     * Update the TP - Message Reference value for every SMS Sent
+     * @param messageRef
+     * @param subId
+     */
+    public void updateMessageRef(int subId, int messageRef) {
+        TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
+                mContext, subId, mContext.getOpPackageName());
+
+        if (mContext == null) {
+            logel("[updateMessageRef] mContext is null");
+            return;
+        }
+        try {
+            if (SubscriptionManager.CONTENT_URI != null) {
+                ContentValues values = new ContentValues(1);
+                values.put(SubscriptionManager.TP_MESSAGE_REF, messageRef);
+                mContext.getContentResolver().update(SubscriptionManager.CONTENT_URI, values,
+                        SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID + "=\"" + subId
+                                + "\"", null);
+            } else {
+                if (DBG) logd("TP - Message reference value not updated to DB");
+            }
+        } finally {
+            if (DBG) logd("TP - Message reference updated to DB Successfully :" + messageRef);
+        }
+    }
+
+    /**
+     * Set UserHandle for this subscription
+     *
+     * @param userHandle the userHandle associated with the subscription
+     * Pass {@code null} user handle to clear the association
+     * @param subId the unique SubscriptionInfo index in database
+     * @return the number of records updated.
+     *
+     * @throws SecurityException if doesn't have required permission.
+     * @throws IllegalArgumentException if subId is invalid.
+     */
+    @Override
+    public int setSubscriptionUserHandle(@Nullable UserHandle userHandle, int subId) {
+        enforceManageSubscriptionUserAssociation("setSubscriptionUserHandle");
+
+        if (userHandle == null) {
+            userHandle = UserHandle.of(UserHandle.USER_NULL);
+        }
+
+        long token = Binder.clearCallingIdentity();
+        try {
+            int ret = setSubscriptionProperty(subId, SubscriptionManager.USER_HANDLE,
+                    String.valueOf(userHandle.getIdentifier()));
+            // ret is the number of records updated in the DB
+            if (ret != 0) {
+                notifySubscriptionInfoChanged();
+            } else {
+                throw new IllegalArgumentException("[setSubscriptionUserHandle]: Invalid subId: "
+                        + subId);
+            }
+            return ret;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Get UserHandle of this subscription.
+     *
+     * @param subId the unique SubscriptionInfo index in database
+     * @return userHandle associated with this subscription
+     * or {@code null} if subscription is not associated with any user.
+     *
+     * @throws SecurityException if doesn't have required permission.
+     * @throws IllegalArgumentException if subId is invalid.
+     */
+    @Override
+    @Nullable
+    public UserHandle getSubscriptionUserHandle(int subId) {
+        enforceManageSubscriptionUserAssociation("getSubscriptionUserHandle");
+
+        if (!mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_enable_get_subscription_user_handle)) {
+            return null;
+        }
+
+        long token = Binder.clearCallingIdentity();
+        try {
+            String userHandleStr = getSubscriptionProperty(subId, SubscriptionManager.USER_HANDLE);
+            if (userHandleStr == null) {
+                throw new IllegalArgumentException("[getSubscriptionUserHandle]: Invalid subId: "
+                        + subId);
+            }
+            UserHandle userHandle = UserHandle.of(Integer.parseInt(userHandleStr));
+            if (userHandle.getIdentifier() == UserHandle.USER_NULL) {
+                return null;
+            }
+            return userHandle;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Check if subscription and user are associated with each other.
+     *
+     * @param subscriptionId the subId of the subscription
+     * @param userHandle user handle of the user
+     * @return {@code true} if subscription is associated with user
+     * {code true} if there are no subscriptions on device
+     * else {@code false} if subscription is not associated with user.
+     *
+     * @throws SecurityException if the caller doesn't have permissions required.
+     * @throws IllegalStateException if subscription service is not available.
+     *
+     */
+    @Override
+    public boolean isSubscriptionAssociatedWithUser(int subscriptionId,
+            @NonNull UserHandle userHandle) {
+        enforceManageSubscriptionUserAssociation("isSubscriptionAssociatedWithUser");
+
+        long token = Binder.clearCallingIdentity();
+        try {
+            // Return true if there are no subscriptions on the device.
+            List<SubscriptionInfo> subInfoList = getAllSubInfoList(
+                    mContext.getOpPackageName(), mContext.getAttributionTag());
+            if (subInfoList == null || subInfoList.isEmpty()) {
+                return true;
+            }
+
+            // Get list of subscriptions associated with this user.
+            List<SubscriptionInfo> associatedSubscriptionsList =
+                    getSubscriptionInfoListAssociatedWithUser(userHandle);
+            if (associatedSubscriptionsList.isEmpty()) {
+                return false;
+            }
+
+            // Return true if required subscription is present in associated subscriptions list.
+            for (SubscriptionInfo subInfo: associatedSubscriptionsList) {
+                if (subInfo.getSubscriptionId() == subscriptionId){
+                    return true;
+                }
+            }
+            return false;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Get list of subscriptions associated with user.
+     *
+     * If user handle is associated with some subscriptions, return subscriptionsAssociatedWithUser
+     * else return all the subscriptions which are not associated with any user.
+     *
+     * @param userHandle user handle of the user
+     * @return list of subscriptionInfo associated with the user.
+     *
+     * @throws SecurityException if the caller doesn't have permissions required.
+     * @throws IllegalStateException if subscription service is not available.
+     *
+     */
+    @Override
+    public @NonNull List<SubscriptionInfo> getSubscriptionInfoListAssociatedWithUser(
+            @NonNull UserHandle userHandle) {
+        enforceManageSubscriptionUserAssociation("getActiveSubscriptionInfoListAssociatedWithUser");
+
+        long token = Binder.clearCallingIdentity();
+        try {
+            List<SubscriptionInfo> subInfoList =  getAllSubInfoList(
+                    mContext.getOpPackageName(), mContext.getAttributionTag());
+            if (subInfoList == null || subInfoList.isEmpty()) {
+                return new ArrayList<>();
+            }
+
+            List<SubscriptionInfo> subscriptionsAssociatedWithUser = new ArrayList<>();
+            List<SubscriptionInfo> subscriptionsWithNoAssociation = new ArrayList<>();
+            for (SubscriptionInfo subInfo : subInfoList) {
+                int subId = subInfo.getSubscriptionId();
+                UserHandle subIdUserHandle = getSubscriptionUserHandle(subId);
+                if (userHandle.equals(subIdUserHandle)) {
+                    // Store subscriptions whose user handle matches with required user handle.
+                    subscriptionsAssociatedWithUser.add(subInfo);
+                } else if (subIdUserHandle == null) {
+                    // Store subscriptions whose user handle is set to null.
+                    subscriptionsWithNoAssociation.add(subInfo);
+                }
+            }
+
+            return subscriptionsAssociatedWithUser.isEmpty() ?
+                    subscriptionsWithNoAssociation : subscriptionsAssociatedWithUser;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
      * @hide
      */
     private void setGlobalSetting(String name, int value) {
diff --git a/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java b/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
index 60f9a62..1681d14 100644
--- a/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
+++ b/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
@@ -46,7 +46,6 @@
 import android.telephony.SubscriptionManager;
 import android.telephony.SubscriptionManager.UsageSetting;
 import android.telephony.TelephonyManager;
-import android.telephony.TelephonyManager.SimState;
 import android.telephony.UiccAccessRule;
 import android.telephony.euicc.EuiccManager;
 import android.text.TextUtils;
@@ -612,7 +611,8 @@
 
         /**
          * The sim loading sequence will be
-         *  1. ACTION_SUBINFO_CONTENT_CHANGE happens through updateSubscriptionInfoByIccId() above.
+         *  1. OnSubscriptionsChangedListener is called through updateSubscriptionInfoByIccId()
+         *  above.
          *  2. ACTION_SIM_STATE_CHANGED/ACTION_SIM_CARD_STATE_CHANGED
          *  /ACTION_SIM_APPLICATION_STATE_CHANGED
          *  3. ACTION_SUBSCRIPTION_CARRIER_IDENTITY_CHANGED
@@ -821,7 +821,7 @@
         // If SIM is not absent, insert new record or update existing record.
         if (!ICCID_STRING_FOR_NO_SIM.equals(sIccId[phoneId]) && sIccId[phoneId] != null) {
             logd("updateSubscriptionInfoByIccId: adding subscription info record: iccid: "
-                    + sIccId[phoneId] + ", phoneId:" + phoneId);
+                    + Rlog.pii(LOG_TAG, sIccId[phoneId]) + ", phoneId:" + phoneId);
             mSubscriptionManager.addSubscriptionInfoRecord(sIccId[phoneId], phoneId);
         }
 
@@ -1017,7 +1017,7 @@
                 mSubscriptionController.insertEmptySubInfoRecord(
                         embeddedProfile.getIccid(), SubscriptionManager.SIM_NOT_INSERTED);
             } else {
-                nameSource = existingSubscriptions.get(index).getNameSource();
+                nameSource = existingSubscriptions.get(index).getDisplayNameSource();
                 prevCarrierId = existingSubscriptions.get(index).getCarrierId();
                 existingSubscriptions.remove(index);
             }
@@ -1165,7 +1165,7 @@
             return;
         }
 
-        int currentSubId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
+        int currentSubId = mSubscriptionController.getSubId(phoneId);
         if (!SubscriptionManager.isValidSubscriptionId(currentSubId)
                 || currentSubId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
             if (DBG) logd("No subscription is active for phone being updated");
@@ -1300,9 +1300,9 @@
             if (slot != null) {
                 i.putExtra(PhoneConstants.PORT_KEY, slot.getPortIndexFromPhoneId(phoneId));
             }
-            logd("Broadcasting intent ACTION_SIM_CARD_STATE_CHANGED " + simStateString(state)
-                    + " for phone: " + phoneId + " slot: " + slotId + " port: "
-                    + slot.getPortIndexFromPhoneId(phoneId));
+            logd("Broadcasting intent ACTION_SIM_CARD_STATE_CHANGED "
+                    + TelephonyManager.simStateToString(state) + " for phone: " + phoneId
+                    + " slot: " + slotId + " port: " + slot.getPortIndexFromPhoneId(phoneId));
             sContext.sendBroadcast(i, Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
             TelephonyMetrics.getInstance().updateSimState(phoneId, state);
         }
@@ -1332,51 +1332,14 @@
             if (slot != null) {
                 i.putExtra(PhoneConstants.PORT_KEY, slot.getPortIndexFromPhoneId(phoneId));
             }
-            logd("Broadcasting intent ACTION_SIM_APPLICATION_STATE_CHANGED " + simStateString(state)
-                    + " for phone: " + phoneId + " slot: " + slotId + "port: "
-                    + slot.getPortIndexFromPhoneId(phoneId));
+            logd("Broadcasting intent ACTION_SIM_APPLICATION_STATE_CHANGED "
+                    + TelephonyManager.simStateToString(state) + " for phone: " + phoneId
+                    + " slot: " + slotId + "port: " + slot.getPortIndexFromPhoneId(phoneId));
             sContext.sendBroadcast(i, Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
             TelephonyMetrics.getInstance().updateSimState(phoneId, state);
         }
     }
 
-    /**
-     * Convert SIM state into string
-     *
-     * @param state SIM state
-     * @return SIM state in string format
-     */
-    public static String simStateString(@SimState int state) {
-        switch (state) {
-            case TelephonyManager.SIM_STATE_UNKNOWN:
-                return "UNKNOWN";
-            case TelephonyManager.SIM_STATE_ABSENT:
-                return "ABSENT";
-            case TelephonyManager.SIM_STATE_PIN_REQUIRED:
-                return "PIN_REQUIRED";
-            case TelephonyManager.SIM_STATE_PUK_REQUIRED:
-                return "PUK_REQUIRED";
-            case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
-                return "NETWORK_LOCKED";
-            case TelephonyManager.SIM_STATE_READY:
-                return "READY";
-            case TelephonyManager.SIM_STATE_NOT_READY:
-                return "NOT_READY";
-            case TelephonyManager.SIM_STATE_PERM_DISABLED:
-                return "PERM_DISABLED";
-            case TelephonyManager.SIM_STATE_CARD_IO_ERROR:
-                return "CARD_IO_ERROR";
-            case TelephonyManager.SIM_STATE_CARD_RESTRICTED:
-                return "CARD_RESTRICTED";
-            case TelephonyManager.SIM_STATE_LOADED:
-                return "LOADED";
-            case TelephonyManager.SIM_STATE_PRESENT:
-                return "PRESENT";
-            default:
-                return "INVALID";
-        }
-    }
-
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private static void logd(String message) {
         Rlog.d(LOG_TAG, message);
diff --git a/src/java/com/android/internal/telephony/TelephonyTester.java b/src/java/com/android/internal/telephony/TelephonyTester.java
index 40bb212..b9e04c8 100644
--- a/src/java/com/android/internal/telephony/TelephonyTester.java
+++ b/src/java/com/android/internal/telephony/TelephonyTester.java
@@ -16,6 +16,7 @@
 
 package com.android.internal.telephony;
 
+import android.annotation.NonNull;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -62,7 +63,6 @@
  *
  */
 public class TelephonyTester {
-    private static final String LOG_TAG = "TelephonyTester";
     private static final boolean DBG = true;
 
     /**
@@ -144,6 +144,7 @@
             "com.android.internal.telephony.TestServiceState";
 
     private static final String EXTRA_ACTION = "action";
+    private static final String EXTRA_PHONE_ID = "phone_id";
     private static final String EXTRA_VOICE_RAT = "voice_rat";
     private static final String EXTRA_DATA_RAT = "data_rat";
     private static final String EXTRA_VOICE_REG_STATE = "voice_reg_state";
@@ -157,6 +158,8 @@
 
     private static final String ACTION_RESET = "reset";
 
+    private String mLogTag;
+
     private static List<ImsExternalCallState> mImsExternalCallStates = null;
 
     private Intent mServiceStateTestIntent;
@@ -198,11 +201,7 @@
                     sendTestSuppServiceNotification(intent);
                 } else if (action.equals(ACTION_TEST_SERVICE_STATE)) {
                     log("handle test service state changed intent");
-                    // Trigger the service state update. The replacement will be done in
-                    // overrideServiceState().
-                    mServiceStateTestIntent = intent;
-                    mPhone.getServiceStateTracker().sendEmptyMessage(
-                            ServiceStateTracker.EVENT_NETWORK_STATE_CHANGED);
+                    setServiceStateTestIntent(intent);
                 } else if (action.equals(ACTION_TEST_IMS_E_CALL)) {
                     log("handle test IMS ecall intent");
                     testImsECall();
@@ -216,7 +215,7 @@
                     if (DBG) log("onReceive: unknown action=" + action);
                 }
             } catch (BadParcelableException e) {
-                Rlog.w(LOG_TAG, e);
+                Rlog.w(mLogTag, e);
             }
         }
     };
@@ -225,6 +224,7 @@
         mPhone = phone;
 
         if (TelephonyUtils.IS_DEBUGGABLE) {
+            mLogTag = "TelephonyTester-" + mPhone.getPhoneId();
             IntentFilter filter = new IntentFilter();
 
             filter.addAction(mPhone.getActionDetached());
@@ -261,8 +261,8 @@
         }
     }
 
-    private static void log(String s) {
-        Rlog.d(LOG_TAG, s);
+    private void log(String s) {
+        Rlog.d(mLogTag, s);
     }
 
     private void handleSuppServiceFailedIntent(Intent intent) {
@@ -385,8 +385,25 @@
         }
     }
 
+    /**
+     * Set the service state test intent.
+     *
+     * @param intent The service state test intent.
+     */
+    public void setServiceStateTestIntent(@NonNull Intent intent) {
+        mServiceStateTestIntent = intent;
+        // Trigger the service state update. The replacement will be done in
+        // overrideServiceState().
+        mPhone.getServiceStateTracker().sendEmptyMessage(
+                ServiceStateTracker.EVENT_NETWORK_STATE_CHANGED);
+    }
+
     void overrideServiceState(ServiceState ss) {
         if (mServiceStateTestIntent == null || ss == null) return;
+        if (mPhone.getPhoneId() != mServiceStateTestIntent.getIntExtra(
+                EXTRA_PHONE_ID, mPhone.getPhoneId())) {
+            return;
+        }
         if (mServiceStateTestIntent.hasExtra(EXTRA_ACTION)
                 && ACTION_RESET.equals(mServiceStateTestIntent.getStringExtra(EXTRA_ACTION))) {
             log("Service state override reset");
@@ -394,13 +411,36 @@
         }
 
         if (mServiceStateTestIntent.hasExtra(EXTRA_VOICE_REG_STATE)) {
+            int state = mServiceStateTestIntent.getIntExtra(EXTRA_DATA_REG_STATE,
+                    ServiceState.STATE_OUT_OF_SERVICE);
             ss.setVoiceRegState(mServiceStateTestIntent.getIntExtra(EXTRA_VOICE_REG_STATE,
                     ServiceState.STATE_OUT_OF_SERVICE));
+            NetworkRegistrationInfo nri = ss.getNetworkRegistrationInfo(
+                    NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+            NetworkRegistrationInfo.Builder builder = new NetworkRegistrationInfo.Builder(nri);
+            if (state == ServiceState.STATE_IN_SERVICE) {
+                builder.setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
+            } else {
+                builder.setRegistrationState(
+                        NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING);
+            }
+            ss.addNetworkRegistrationInfo(builder.build());
             log("Override voice service state with " + ss.getState());
         }
         if (mServiceStateTestIntent.hasExtra(EXTRA_DATA_REG_STATE)) {
-            ss.setDataRegState(mServiceStateTestIntent.getIntExtra(EXTRA_DATA_REG_STATE,
-                    ServiceState.STATE_OUT_OF_SERVICE));
+            int state = mServiceStateTestIntent.getIntExtra(EXTRA_DATA_REG_STATE,
+                    ServiceState.STATE_OUT_OF_SERVICE);
+            ss.setDataRegState(state);
+            NetworkRegistrationInfo nri = ss.getNetworkRegistrationInfo(
+                    NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+            NetworkRegistrationInfo.Builder builder = new NetworkRegistrationInfo.Builder(nri);
+            if (state == ServiceState.STATE_IN_SERVICE) {
+                builder.setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
+            } else {
+                builder.setRegistrationState(
+                        NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING);
+            }
+            ss.addNetworkRegistrationInfo(builder.build());
             log("Override data service state with " + ss.getDataRegistrationState());
         }
         if (mServiceStateTestIntent.hasExtra(EXTRA_OPERATOR)) {
diff --git a/src/java/com/android/internal/telephony/UiccPhoneBookController.java b/src/java/com/android/internal/telephony/UiccPhoneBookController.java
index 5b55c35..8b8457c 100644
--- a/src/java/com/android/internal/telephony/UiccPhoneBookController.java
+++ b/src/java/com/android/internal/telephony/UiccPhoneBookController.java
@@ -19,11 +19,12 @@
 package com.android.internal.telephony;
 
 import android.compat.annotation.UnsupportedAppUsage;
+import android.content.ContentValues;
 import android.os.Build;
 import android.os.TelephonyServiceManager.ServiceRegisterer;
 import android.telephony.TelephonyFrameworkInitializer;
-import android.content.ContentValues;
 
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.AdnCapacity;
 import com.android.internal.telephony.uicc.AdnRecord;
 import com.android.telephony.Rlog;
@@ -146,7 +147,12 @@
     private IccPhoneBookInterfaceManager
             getIccPhoneBookInterfaceManager(int subId) {
 
-        int phoneId = SubscriptionController.getInstance().getPhoneId(subId);
+        int phoneId;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            phoneId = SubscriptionManagerService.getInstance().getPhoneId(subId);
+        } else {
+            phoneId = SubscriptionController.getInstance().getPhoneId(subId);
+        }
         try {
             return PhoneFactory.getPhone(phoneId).getIccPhoneBookInterfaceManager();
         } catch (NullPointerException e) {
diff --git a/src/java/com/android/internal/telephony/VoiceIndication.java b/src/java/com/android/internal/telephony/VoiceIndication.java
index 984d2a0..9720bb7 100644
--- a/src/java/com/android/internal/telephony/VoiceIndication.java
+++ b/src/java/com/android/internal/telephony/VoiceIndication.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_VOICE;
+
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CALL_RING;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CDMA_CALL_WAITING;
 import static com.android.internal.telephony.RILConstants.RIL_UNSOL_CDMA_INFO_REC;
@@ -63,7 +65,7 @@
      */
     public void callRing(int indicationType, boolean isGsm,
             android.hardware.radio.voice.CdmaSignalInfoRecord record) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         char[] response = null;
 
@@ -90,7 +92,7 @@
      * @param indicationType Type of radio indication
      */
     public void callStateChanged(int indicationType) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED);
 
@@ -104,7 +106,7 @@
      */
     public void cdmaCallWaiting(int indicationType,
             android.hardware.radio.voice.CdmaCallWaiting callWaitingRecord) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         // TODO: create a CdmaCallWaitingNotification constructor that takes in these fields to make
         // sure no fields are missing
@@ -134,7 +136,7 @@
      */
     public void cdmaInfoRec(int indicationType,
             android.hardware.radio.voice.CdmaInformationRecord[] records) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         for (int i = 0; i < records.length; i++) {
             android.hardware.radio.voice.CdmaInformationRecord record = records[i];
@@ -233,7 +235,7 @@
      * @param status CDMA OTA provision status
      */
     public void cdmaOtaProvisionStatus(int indicationType, int status) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         int[] response = new int[] {status};
 
@@ -252,7 +254,7 @@
      */
     public void currentEmergencyNumberList(int indicationType,
             android.hardware.radio.voice.EmergencyNumber[] emergencyNumberList) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         List<EmergencyNumber> response = new ArrayList<>(emergencyNumberList.length);
         for (android.hardware.radio.voice.EmergencyNumber enHal : emergencyNumberList) {
@@ -279,7 +281,7 @@
      * @param indicationType Type of radio indication
      */
     public void enterEmergencyCallbackMode(int indicationType) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE);
 
@@ -294,7 +296,7 @@
      * @param indicationType Type of radio indication
      */
     public void exitEmergencyCallbackMode(int indicationType) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE);
 
@@ -307,7 +309,7 @@
      * @param start true = start play ringback tone, false = stop playing ringback tone
      */
     public void indicateRingbackTone(int indicationType, boolean start) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogvRet(RIL_UNSOL_RINGBACK_TONE, start);
 
@@ -322,7 +324,7 @@
      */
     public void onSupplementaryServiceIndication(int indicationType,
             android.hardware.radio.voice.StkCcUnsolSsResult ss) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         int num;
         SsData ssData = new SsData();
@@ -374,7 +376,7 @@
      * @param msg Message string in UTF-8, if applicable
      */
     public void onUssd(int indicationType, int ussdModeType, String msg) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogMore(RIL_UNSOL_ON_USSD, "" + ussdModeType);
 
@@ -390,7 +392,7 @@
      * @param indicationType Type of radio indication
      */
     public void resendIncallMute(int indicationType) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLog(RIL_UNSOL_RESEND_INCALL_MUTE);
 
@@ -403,7 +405,7 @@
      * @param state New SRVCC State
      */
     public void srvccStateNotify(int indicationType, int state) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         int[] response = new int[] {state};
 
@@ -419,7 +421,7 @@
      * @param alpha ALPHA string from UICC in UTF-8 format
      */
     public void stkCallControlAlphaNotify(int indicationType, String alpha) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_STK_CC_ALPHA_NOTIFY, alpha);
 
@@ -434,7 +436,7 @@
      * @param timeout Timeout value in milliseconds for setting up voice call
      */
     public void stkCallSetup(int indicationType, long timeout) {
-        mRil.processIndication(RIL.VOICE_SERVICE, indicationType);
+        mRil.processIndication(HAL_SERVICE_VOICE, indicationType);
 
         if (mRil.isLogOrTrace()) mRil.unsljLogRet(RIL_UNSOL_STK_CALL_SETUP, timeout);
 
diff --git a/src/java/com/android/internal/telephony/VoiceResponse.java b/src/java/com/android/internal/telephony/VoiceResponse.java
index b1ba9d9..d1e060e 100644
--- a/src/java/com/android/internal/telephony/VoiceResponse.java
+++ b/src/java/com/android/internal/telephony/VoiceResponse.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_VOICE;
+
 import android.hardware.radio.RadioError;
 import android.hardware.radio.RadioResponseInfo;
 import android.hardware.radio.voice.IRadioVoiceResponse;
@@ -47,49 +49,49 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void acceptCallResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void cancelPendingUssdResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void conferenceResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void dialResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void emergencyDialResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void exitEmergencyCallbackModeResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void explicitCallTransferResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
@@ -99,7 +101,7 @@
      */
     public void getCallForwardStatusResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.voice.CallForwardInfo[] callForwardInfos) {
-        RILRequest rr = mRil.processResponse(RIL.VOICE_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_VOICE, responseInfo);
         if (rr != null) {
             CallForwardInfo[] ret = new CallForwardInfo[callForwardInfos.length];
             for (int i = 0; i < callForwardInfos.length; i++) {
@@ -129,7 +131,7 @@
     public void getCallWaitingResponse(RadioResponseInfo responseInfo, boolean enable,
             int serviceClass) {
         RadioResponse.responseInts(
-                RIL.VOICE_SERVICE, mRil, responseInfo, enable ? 1 : 0, serviceClass);
+                HAL_SERVICE_VOICE, mRil, responseInfo, enable ? 1 : 0, serviceClass);
     }
 
     /**
@@ -137,7 +139,7 @@
      * @param status indicates CLIP status
      */
     public void getClipResponse(RadioResponseInfo responseInfo, int status) {
-        RadioResponse.responseInts(RIL.VOICE_SERVICE, mRil, responseInfo, status);
+        RadioResponse.responseInts(HAL_SERVICE_VOICE, mRil, responseInfo, status);
     }
 
     /**
@@ -146,7 +148,7 @@
      * @param m is "m" parameter from TS 27.007 7.7
      */
     public void getClirResponse(RadioResponseInfo responseInfo, int n, int m) {
-        RadioResponse.responseInts(RIL.VOICE_SERVICE, mRil, responseInfo, n, m);
+        RadioResponse.responseInts(HAL_SERVICE_VOICE, mRil, responseInfo, n, m);
     }
 
     /**
@@ -155,7 +157,7 @@
      */
     public void getCurrentCallsResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.voice.Call[] calls) {
-        RILRequest rr = mRil.processResponse(RIL.VOICE_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_VOICE, responseInfo);
 
         if (rr != null) {
             int num = calls.length;
@@ -198,7 +200,7 @@
      */
     public void getLastCallFailCauseResponse(RadioResponseInfo responseInfo,
             android.hardware.radio.voice.LastCallFailCauseInfo fcInfo) {
-        RILRequest rr = mRil.processResponse(RIL.VOICE_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_VOICE, responseInfo);
 
         if (rr != null) {
             LastCallFailCause ret = new LastCallFailCause();
@@ -216,7 +218,7 @@
      * @param enable true for "mute enabled" and false for "mute disabled"
      */
     public void getMuteResponse(RadioResponseInfo responseInfo, boolean enable) {
-        RadioResponse.responseInts(RIL.VOICE_SERVICE, mRil, responseInfo, enable ? 1 : 0);
+        RadioResponse.responseInts(HAL_SERVICE_VOICE, mRil, responseInfo, enable ? 1 : 0);
     }
 
     /**
@@ -225,7 +227,7 @@
      *        true for Enhanced Privacy Mode (Private Long Code Mask)
      */
     public void getPreferredVoicePrivacyResponse(RadioResponseInfo responseInfo, boolean enable) {
-        RadioResponse.responseInts(RIL.VOICE_SERVICE, mRil, responseInfo, enable ? 1 : 0);
+        RadioResponse.responseInts(HAL_SERVICE_VOICE, mRil, responseInfo, enable ? 1 : 0);
     }
 
     /**
@@ -233,35 +235,35 @@
      * @param mode TTY mode
      */
     public void getTtyModeResponse(RadioResponseInfo responseInfo, int mode) {
-        RadioResponse.responseInts(RIL.VOICE_SERVICE, mRil, responseInfo, mode);
+        RadioResponse.responseInts(HAL_SERVICE_VOICE, mRil, responseInfo, mode);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void handleStkCallSetupRequestFromSimResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void hangupConnectionResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void hangupForegroundResumeBackgroundResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void hangupWaitingOrBackgroundResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
@@ -269,7 +271,7 @@
      * @param enable true for "vonr enabled" and false for "vonr disabled"
      */
     public void isVoNrEnabledResponse(RadioResponseInfo responseInfo, boolean enable) {
-        RILRequest rr = mRil.processResponse(RIL.VOICE_SERVICE, responseInfo);
+        RILRequest rr = mRil.processResponse(HAL_SERVICE_VOICE, responseInfo);
 
         if (rr != null) {
             if (responseInfo.error == RadioError.NONE) {
@@ -283,112 +285,112 @@
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void rejectCallResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void sendBurstDtmfResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void sendCdmaFeatureCodeResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void sendDtmfResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void sendUssdResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void separateConnectionResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setCallForwardResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setCallWaitingResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setClirResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setMuteResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setPreferredVoicePrivacyResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setTtyModeResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void setVoNrEnabledResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void startDtmfResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void stopDtmfResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     /**
      * @param responseInfo Response info struct containing response type, serial no. and error
      */
     public void switchWaitingOrHoldingAndActiveResponse(RadioResponseInfo responseInfo) {
-        RadioResponse.responseVoid(RIL.VOICE_SERVICE, mRil, responseInfo);
+        RadioResponse.responseVoid(HAL_SERVICE_VOICE, mRil, responseInfo);
     }
 
     @Override
diff --git a/src/java/com/android/internal/telephony/WapPushOverSms.java b/src/java/com/android/internal/telephony/WapPushOverSms.java
old mode 100755
new mode 100644
index d6f69e2..d6ea4ad
--- a/src/java/com/android/internal/telephony/WapPushOverSms.java
+++ b/src/java/com/android/internal/telephony/WapPushOverSms.java
@@ -46,6 +46,7 @@
 import android.text.TextUtils;
 
 import com.android.internal.telephony.uicc.IccUtils;
+import com.android.internal.telephony.util.TelephonyUtils;
 import com.android.telephony.Rlog;
 
 import com.google.android.mms.pdu.GenericPdu;
@@ -245,9 +246,10 @@
                 System.arraycopy(pdu, dataIndex, intentData, 0, intentData.length);
             }
 
-            int[] subIds = SubscriptionManager.getSubId(phoneId);
-            int subId = (subIds != null) && (subIds.length > 0) ? subIds[0]
-                    : SmsManager.getDefaultSmsSubscriptionId();
+            int subId = SubscriptionManager.getSubscriptionId(phoneId);
+            if (!SubscriptionManager.isValidSubscriptionId(subId)) {
+                subId = SmsManager.getDefaultSmsSubscriptionId();
+            }
 
             // Continue if PDU parsing fails: the default messaging app may successfully parse the
             // same PDU.
@@ -391,7 +393,10 @@
 
         // Direct the intent to only the default MMS app. If we can't find a default MMS app
         // then sent it to all broadcast receivers.
-        ComponentName componentName = SmsApplication.getDefaultMmsApplication(mContext, true);
+        UserHandle userHandle = TelephonyUtils.getSubscriptionUserHandle(mContext, subId);
+        ComponentName componentName = SmsApplication.getDefaultMmsApplicationAsUser(mContext,
+                true, userHandle);
+
         Bundle options = null;
         if (componentName != null) {
             // Deliver MMS message only to this receiver
@@ -409,9 +414,12 @@
             options = bopts.toBundle();
         }
 
+        if (userHandle == null) {
+            userHandle = UserHandle.SYSTEM;
+        }
         handler.dispatchIntent(intent, getPermissionForType(result.mimeType),
                 getAppOpsStringPermissionForIntent(result.mimeType), options, receiver,
-                UserHandle.SYSTEM, subId);
+                userHandle, subId);
         return Activity.RESULT_OK;
     }
 
diff --git a/src/java/com/android/internal/telephony/WspTypeDecoder.java b/src/java/com/android/internal/telephony/WspTypeDecoder.java
old mode 100755
new mode 100644
diff --git a/src/java/com/android/internal/telephony/cat/AppInterface.java b/src/java/com/android/internal/telephony/cat/AppInterface.java
old mode 100755
new mode 100644
diff --git a/src/java/com/android/internal/telephony/cat/CatCmdMessage.java b/src/java/com/android/internal/telephony/cat/CatCmdMessage.java
index 3d21270..4447c07 100644
--- a/src/java/com/android/internal/telephony/cat/CatCmdMessage.java
+++ b/src/java/com/android/internal/telephony/cat/CatCmdMessage.java
@@ -40,6 +40,7 @@
     private ToneSettings mToneSettings = null;
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private CallSettings mCallSettings = null;
+    private SMSSettings mSMSSettings =  null;
     private SetupEventListSettings mSetupEventListSettings = null;
     private boolean mLoadIconFailed = false;
 
@@ -61,6 +62,14 @@
         public TextMessage callMsg;
     }
 
+    /**
+     * Container for SEND SMS  command settings.
+     */
+    public class SMSSettings {
+        public TextMessage smsText;
+        public TextMessage destAddr;
+    }
+
     public class SetupEventListSettings {
         @UnsupportedAppUsage
         public int[] eventList;
@@ -84,57 +93,69 @@
         mCmdDet = cmdParams.mCmdDet;
         mLoadIconFailed =  cmdParams.mLoadIconFailed;
         switch(getCmdType()) {
-        case SET_UP_MENU:
-        case SELECT_ITEM:
-            mMenu = ((SelectItemParams) cmdParams).mMenu;
-            break;
-        case DISPLAY_TEXT:
-        case SET_UP_IDLE_MODE_TEXT:
-        case SEND_DTMF:
-        case SEND_SMS:
-        case REFRESH:
-        case RUN_AT:
-        case SEND_SS:
-        case SEND_USSD:
-            mTextMsg = ((DisplayTextParams) cmdParams).mTextMsg;
-            break;
-        case GET_INPUT:
-        case GET_INKEY:
-            mInput = ((GetInputParams) cmdParams).mInput;
-            break;
-        case LAUNCH_BROWSER:
-            mTextMsg = ((LaunchBrowserParams) cmdParams).mConfirmMsg;
-            mBrowserSettings = new BrowserSettings();
-            mBrowserSettings.url = ((LaunchBrowserParams) cmdParams).mUrl;
-            mBrowserSettings.mode = ((LaunchBrowserParams) cmdParams).mMode;
-            break;
-        case PLAY_TONE:
-            PlayToneParams params = (PlayToneParams) cmdParams;
-            mToneSettings = params.mSettings;
-            mTextMsg = params.mTextMsg;
-            break;
-        case GET_CHANNEL_STATUS:
-            mTextMsg = ((CallSetupParams) cmdParams).mConfirmMsg;
-            break;
-        case SET_UP_CALL:
-            mCallSettings = new CallSettings();
-            mCallSettings.confirmMsg = ((CallSetupParams) cmdParams).mConfirmMsg;
-            mCallSettings.callMsg = ((CallSetupParams) cmdParams).mCallMsg;
-            break;
-        case OPEN_CHANNEL:
-        case CLOSE_CHANNEL:
-        case RECEIVE_DATA:
-        case SEND_DATA:
-            BIPClientParams param = (BIPClientParams) cmdParams;
-            mTextMsg = param.mTextMsg;
-            break;
-        case SET_UP_EVENT_LIST:
-            mSetupEventListSettings = new SetupEventListSettings();
-            mSetupEventListSettings.eventList = ((SetEventListParams) cmdParams).mEventInfo;
-            break;
-        case PROVIDE_LOCAL_INFORMATION:
-        default:
-            break;
+            case SET_UP_MENU:
+            case SELECT_ITEM:
+                mMenu = ((SelectItemParams) cmdParams).mMenu;
+                break;
+            case SEND_SMS:
+                /* If cmdParams  is an instanceof SendSMSParams , then it means config value
+                 * config_stk_sms_send_support is true and the SMS should be sent by framework
+                 */
+                if (cmdParams instanceof SendSMSParams) {
+                    mSMSSettings = new SMSSettings();
+                    mSMSSettings.smsText = ((SendSMSParams) cmdParams).mTextSmsMsg;
+                    mSMSSettings.destAddr = ((SendSMSParams) cmdParams).mDestAddress;
+                    mTextMsg = ((SendSMSParams) cmdParams).mDisplayText.mTextMsg;
+                } else {
+                    mTextMsg = ((DisplayTextParams) cmdParams).mTextMsg;
+                }
+                break;
+            case DISPLAY_TEXT:
+            case SET_UP_IDLE_MODE_TEXT:
+            case SEND_DTMF:
+            case REFRESH:
+            case RUN_AT:
+            case SEND_SS:
+            case SEND_USSD:
+                mTextMsg = ((DisplayTextParams) cmdParams).mTextMsg;
+                break;
+            case GET_INPUT:
+            case GET_INKEY:
+                mInput = ((GetInputParams) cmdParams).mInput;
+                break;
+            case LAUNCH_BROWSER:
+                mTextMsg = ((LaunchBrowserParams) cmdParams).mConfirmMsg;
+                mBrowserSettings = new BrowserSettings();
+                mBrowserSettings.url = ((LaunchBrowserParams) cmdParams).mUrl;
+                mBrowserSettings.mode = ((LaunchBrowserParams) cmdParams).mMode;
+                break;
+            case PLAY_TONE:
+                PlayToneParams params = (PlayToneParams) cmdParams;
+                mToneSettings = params.mSettings;
+                mTextMsg = params.mTextMsg;
+                break;
+            case GET_CHANNEL_STATUS:
+                mTextMsg = ((CallSetupParams) cmdParams).mConfirmMsg;
+                break;
+            case SET_UP_CALL:
+                mCallSettings = new CallSettings();
+                mCallSettings.confirmMsg = ((CallSetupParams) cmdParams).mConfirmMsg;
+                mCallSettings.callMsg = ((CallSetupParams) cmdParams).mCallMsg;
+                break;
+            case OPEN_CHANNEL:
+            case CLOSE_CHANNEL:
+            case RECEIVE_DATA:
+            case SEND_DATA:
+                BIPClientParams param = (BIPClientParams) cmdParams;
+                mTextMsg = param.mTextMsg;
+                break;
+            case SET_UP_EVENT_LIST:
+                mSetupEventListSettings = new SetupEventListSettings();
+                mSetupEventListSettings.eventList = ((SetEventListParams) cmdParams).mEventInfo;
+                break;
+            case PROVIDE_LOCAL_INFORMATION:
+            default:
+                break;
         }
     }
 
@@ -145,29 +166,34 @@
         mInput = in.readParcelable(Input.class.getClassLoader());
         mLoadIconFailed = (in.readByte() == 1);
         switch (getCmdType()) {
-        case LAUNCH_BROWSER:
-            mBrowserSettings = new BrowserSettings();
-            mBrowserSettings.url = in.readString();
-            mBrowserSettings.mode = LaunchBrowserMode.values()[in.readInt()];
-            break;
-        case PLAY_TONE:
-            mToneSettings = in.readParcelable(ToneSettings.class.getClassLoader());
-            break;
-        case SET_UP_CALL:
-            mCallSettings = new CallSettings();
-            mCallSettings.confirmMsg = in.readParcelable(TextMessage.class.getClassLoader());
-            mCallSettings.callMsg = in.readParcelable(TextMessage.class.getClassLoader());
-            break;
-        case SET_UP_EVENT_LIST:
-            mSetupEventListSettings = new SetupEventListSettings();
-            int length = in.readInt();
-            mSetupEventListSettings.eventList = new int[length];
-            for (int i = 0; i < length; i++) {
-                mSetupEventListSettings.eventList[i] = in.readInt();
-            }
-            break;
-        default:
-            break;
+            case LAUNCH_BROWSER:
+                mBrowserSettings = new BrowserSettings();
+                mBrowserSettings.url = in.readString();
+                mBrowserSettings.mode = LaunchBrowserMode.values()[in.readInt()];
+                break;
+            case PLAY_TONE:
+                mToneSettings = in.readParcelable(ToneSettings.class.getClassLoader());
+                break;
+            case SET_UP_CALL:
+                mCallSettings = new CallSettings();
+                mCallSettings.confirmMsg = in.readParcelable(TextMessage.class.getClassLoader());
+                mCallSettings.callMsg = in.readParcelable(TextMessage.class.getClassLoader());
+                break;
+            case SET_UP_EVENT_LIST:
+                mSetupEventListSettings = new SetupEventListSettings();
+                int length = in.readInt();
+                mSetupEventListSettings.eventList = new int[length];
+                for (int i = 0; i < length; i++) {
+                    mSetupEventListSettings.eventList[i] = in.readInt();
+                }
+                break;
+            case SEND_SMS:
+                mSMSSettings = new SMSSettings();
+                mSMSSettings.smsText = in.readParcelable(SendSMSParams.class.getClassLoader());
+                mSMSSettings.destAddr = in.readParcelable(SendSMSParams.class.getClassLoader());
+                break;
+            default:
+                break;
         }
     }
 
@@ -178,23 +204,29 @@
         dest.writeParcelable(mMenu, 0);
         dest.writeParcelable(mInput, 0);
         dest.writeByte((byte) (mLoadIconFailed ? 1 : 0));
-        switch(getCmdType()) {
-        case LAUNCH_BROWSER:
-            dest.writeString(mBrowserSettings.url);
-            dest.writeInt(mBrowserSettings.mode.ordinal());
-            break;
-        case PLAY_TONE:
-            dest.writeParcelable(mToneSettings, 0);
-            break;
-        case SET_UP_CALL:
-            dest.writeParcelable(mCallSettings.confirmMsg, 0);
-            dest.writeParcelable(mCallSettings.callMsg, 0);
-            break;
-        case SET_UP_EVENT_LIST:
-            dest.writeIntArray(mSetupEventListSettings.eventList);
-            break;
-        default:
-            break;
+        switch (getCmdType()) {
+            case LAUNCH_BROWSER:
+                dest.writeString(mBrowserSettings.url);
+                dest.writeInt(mBrowserSettings.mode.ordinal());
+                break;
+            case PLAY_TONE:
+                dest.writeParcelable(mToneSettings, 0);
+                break;
+            case SET_UP_CALL:
+                dest.writeParcelable(mCallSettings.confirmMsg, 0);
+                dest.writeParcelable(mCallSettings.callMsg, 0);
+                break;
+            case SET_UP_EVENT_LIST:
+                dest.writeIntArray(mSetupEventListSettings.eventList);
+                break;
+            case SEND_SMS:
+                if (mSMSSettings != null) {
+                    dest.writeParcelable(mSMSSettings.smsText, 0);
+                    dest.writeParcelable(mSMSSettings.destAddr, 0);
+                }
+                break;
+            default:
+                break;
         }
     }
 
diff --git a/src/java/com/android/internal/telephony/cat/CatService.java b/src/java/com/android/internal/telephony/cat/CatService.java
index 9ed5eb9..31b997f 100644
--- a/src/java/com/android/internal/telephony/cat/CatService.java
+++ b/src/java/com/android/internal/telephony/cat/CatService.java
@@ -20,25 +20,37 @@
 import static com.android.internal.telephony.cat.CatCmdMessage.SetupEventListConstants.LANGUAGE_SELECTION_EVENT;
 import static com.android.internal.telephony.cat.CatCmdMessage.SetupEventListConstants.USER_ACTIVITY_EVENT;
 
+import android.app.Activity;
 import android.app.ActivityManager;
+import android.app.PendingIntent;
 import android.app.backup.BackupManager;
 import android.compat.annotation.UnsupportedAppUsage;
+import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.res.Resources.NotFoundException;
 import android.os.AsyncResult;
 import android.os.Build;
 import android.os.Handler;
+import android.os.HandlerThread;
 import android.os.LocaleList;
+import android.os.Looper;
 import android.os.Message;
 import android.os.RemoteException;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.PhoneFactory;
+import com.android.internal.telephony.ProxyController;
+import com.android.internal.telephony.SmsController;
 import com.android.internal.telephony.SubscriptionController;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.IccCardStatus.CardState;
 import com.android.internal.telephony.uicc.IccFileHandler;
 import com.android.internal.telephony.uicc.IccRecords;
@@ -138,12 +150,21 @@
 
     static final String STK_DEFAULT = "Default Message";
 
+    private static final String SMS_DELIVERY_ACTION =
+            "com.android.internal.telephony.cat.SMS_DELIVERY_ACTION";
+    private static final String SMS_SENT_ACTION =
+            "com.android.internal.telephony.cat.SMS_SENT_ACTION";
+
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private int mSlotId;
+    private static HandlerThread sCatServiceThread;
 
     /* For multisim catservice should not be singleton */
     private CatService(CommandsInterface ci, UiccCardApplication ca, IccRecords ir,
-            Context context, IccFileHandler fh, UiccProfile uiccProfile, int slotId) {
+            Context context, IccFileHandler fh, UiccProfile uiccProfile, int slotId,
+            Looper looper) {
+        //creating new thread to avoid deadlock conditions with the framework thread.
+        super(looper);
         if (ci == null || ca == null || ir == null || context == null || fh == null
                 || uiccProfile == null) {
             throw new NullPointerException(
@@ -187,6 +208,10 @@
 
         CatLog.d(this, "Running CAT service on Slotid: " + mSlotId +
                 ". STK app installed:" + mStkAppInstalled);
+
+        SmsBroadcastReceiver smsBroadcastReceiver = new SmsBroadcastReceiver();
+        mContext.registerReceiver(smsBroadcastReceiver, new IntentFilter(SMS_DELIVERY_ACTION));
+        mContext.registerReceiver(smsBroadcastReceiver, new IntentFilter(SMS_SENT_ACTION));
     }
 
     /**
@@ -200,6 +225,10 @@
      */
     public static CatService getInstance(CommandsInterface ci,
             Context context, UiccProfile uiccProfile, int slotId) {
+        if (sCatServiceThread == null) {
+            sCatServiceThread = new HandlerThread("CatServiceThread");
+            sCatServiceThread.start();
+        }
         UiccCardApplication ca = null;
         IccFileHandler fh = null;
         IccRecords ir = null;
@@ -227,8 +256,8 @@
                         || uiccProfile == null) {
                     return null;
                 }
-
-                sInstance[slotId] = new CatService(ci, ca, ir, context, fh, uiccProfile, slotId);
+                sInstance[slotId] = new CatService(ci, ca, ir, context, fh, uiccProfile, slotId,
+                        sCatServiceThread.getLooper());
             } else if ((ir != null) && (mIccRecords != ir)) {
                 if (mIccRecords != null) {
                     mIccRecords.unregisterForRecordsLoaded(sInstance[slotId]);
@@ -447,8 +476,49 @@
                     ((DisplayTextParams)cmdParams).mTextMsg.text = null;
                 }
                 break;
-            case SEND_DTMF:
             case SEND_SMS:
+                /* If cmdParams  is an instanceof SendSMSParams , then it means config value
+                 * config_stk_sms_send_support is true and the SMS should be sent by framework
+                 */
+                if (cmdParams instanceof SendSMSParams) {
+                    String text = null, destAddr = null;
+                    if (((SendSMSParams) cmdParams).mTextSmsMsg != null) {
+                        text = ((SendSMSParams) cmdParams).mTextSmsMsg.text;
+                    }
+                    if (((SendSMSParams) cmdParams).mDestAddress != null) {
+                        destAddr = ((SendSMSParams) cmdParams).mDestAddress.text;
+                    }
+                    if (text != null && destAddr != null) {
+                        ProxyController proxyController = ProxyController.getInstance(mContext);
+                        SubscriptionManager subscriptionManager = (SubscriptionManager)
+                                mContext.getSystemService(
+                                        Context.TELEPHONY_SUBSCRIPTION_SERVICE);
+                        SubscriptionInfo subInfo =
+                                subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(
+                                        mSlotId);
+                        if (subInfo != null) {
+                            sendStkSms(text, destAddr, subInfo.getSubscriptionId(), cmdParams,
+                                    proxyController);
+                        } else {
+                            sendTerminalResponse(cmdParams.mCmdDet,
+                                    ResultCode.CMD_DATA_NOT_UNDERSTOOD, false, 0x00, null);
+                            CatLog.d(this, "Subscription info is null");
+                        }
+                    } else {
+                        sendTerminalResponse(cmdParams.mCmdDet, ResultCode.CMD_DATA_NOT_UNDERSTOOD,
+                                false, 0x00, null);
+                        CatLog.d(this, "Sms text or Destination Address is null");
+                    }
+                } else {
+                    if ((((DisplayTextParams) cmdParams).mTextMsg.text != null)
+                            && (((DisplayTextParams) cmdParams).mTextMsg.text.equals(
+                            STK_DEFAULT))) {
+                        message = mContext.getText(com.android.internal.R.string.sending);
+                        ((DisplayTextParams) cmdParams).mTextMsg.text = message.toString();
+                    }
+                }
+                break;
+            case SEND_DTMF:
             case SEND_SS:
             case SEND_USSD:
                 if ((((DisplayTextParams)cmdParams).mTextMsg.text != null)
@@ -536,6 +606,61 @@
         broadcastCatCmdIntent(cmdMsg);
     }
 
+    /**
+     * Used to send STK based sms via CATService
+     * @param text The message body
+     * @param destAddr The destination Address
+     * @param subId Subscription Id
+     * @param cmdParams Send SMS Command Params
+     * @param proxyController ProxyController
+     * @hide
+     */
+    public void sendStkSms(String text, String destAddr, int subId, CommandParams cmdParams,
+            ProxyController proxyController) {
+        PendingIntent sentPendingIntent = PendingIntent.getBroadcast(mContext, 0,
+                new Intent(SMS_SENT_ACTION).putExtra("cmdDetails",
+                        cmdParams.mCmdDet), PendingIntent.FLAG_MUTABLE);
+        PendingIntent deliveryPendingIntent = PendingIntent.getBroadcast(mContext, 0,
+                new Intent(SMS_DELIVERY_ACTION).putExtra("cmdDetails",
+                        cmdParams.mCmdDet), PendingIntent.FLAG_MUTABLE);
+        SmsController smsController = proxyController.getSmsController();
+        smsController.sendTextForSubscriber(subId, mContext.getOpPackageName(),
+                mContext.getAttributionTag(), destAddr, null, text, sentPendingIntent,
+                deliveryPendingIntent, false, 0L, true, true);
+    }
+
+    private class SmsBroadcastReceiver extends BroadcastReceiver {
+
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            CommandDetails commandDetails = (CommandDetails) intent.getExtra("cmdDetails");
+            if (intent.getAction().equals(SMS_SENT_ACTION)) {
+                int resultCode = getResultCode();
+                switch (resultCode) {
+                    case Activity.RESULT_OK:
+                        break;
+                    default:
+                        //ToDO handle Error cases bug : b/243123292
+                        CatLog.d(this, "Error sending STK SMS : " + resultCode);
+                        sendTerminalResponse(commandDetails, ResultCode.SMS_RP_ERROR, true,
+                                ResultCode.NETWORK_CRNTLY_UNABLE_TO_PROCESS.value(), null);
+                }
+            }
+            if (intent.getAction().equals(SMS_DELIVERY_ACTION)) {
+                int resultCode = getResultCode();
+                switch (resultCode) {
+                    case Activity.RESULT_OK:
+                        sendTerminalResponse(commandDetails, ResultCode.OK, false, 0, null);
+                        break;
+                    default:
+                        //ToDO handle Error cases bug: b/243123292
+                        CatLog.d(this, "Error delivering STK SMS : " + resultCode);
+                        sendTerminalResponse(commandDetails, ResultCode.SMS_RP_ERROR, true,
+                                ResultCode.TERMINAL_CRNTLY_UNABLE_TO_PROCESS.value(), null);
+                }
+            }
+        }
+    }
 
     private void broadcastCatCmdIntent(CatCmdMessage cmdMsg) {
         Intent intent = new Intent(AppInterface.CAT_CMD_ACTION);
@@ -808,9 +933,16 @@
     //TODO Need to take care for MSIM
     public static AppInterface getInstance() {
         int slotId = PhoneConstants.DEFAULT_SLOT_INDEX;
-        SubscriptionController sControl = SubscriptionController.getInstance();
-        if (sControl != null) {
-            slotId = sControl.getSlotIndex(sControl.getDefaultSubId());
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            if (SubscriptionManagerService.getInstance() != null) {
+                slotId = SubscriptionManagerService.getInstance().getSlotIndex(
+                        SubscriptionManagerService.getInstance().getDefaultSubId());
+            }
+        } else {
+            SubscriptionController sControl = SubscriptionController.getInstance();
+            if (sControl != null) {
+                slotId = sControl.getSlotIndex(sControl.getDefaultSubId());
+            }
         }
         return getInstance(null, null, null, slotId);
     }
diff --git a/src/java/com/android/internal/telephony/cat/CommandParams.java b/src/java/com/android/internal/telephony/cat/CommandParams.java
old mode 100755
new mode 100644
index b9de4d1..8530ee2
--- a/src/java/com/android/internal/telephony/cat/CommandParams.java
+++ b/src/java/com/android/internal/telephony/cat/CommandParams.java
@@ -22,16 +22,16 @@
 
 /**
  * Container class for proactive command parameters.
- *
+ * @hide
  */
-class CommandParams {
+public class CommandParams {
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     CommandDetails mCmdDet;
     // Variable to track if an optional icon load has failed.
     boolean mLoadIconFailed = false;
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
-    CommandParams(CommandDetails cmdDet) {
+    public CommandParams(CommandDetails cmdDet) {
         mCmdDet = cmdDet;
     }
 
diff --git a/src/java/com/android/internal/telephony/cat/CommandParamsFactory.java b/src/java/com/android/internal/telephony/cat/CommandParamsFactory.java
index 7fbebfa..65f3c4a 100644
--- a/src/java/com/android/internal/telephony/cat/CommandParamsFactory.java
+++ b/src/java/com/android/internal/telephony/cat/CommandParamsFactory.java
@@ -29,6 +29,7 @@
 import android.os.Build;
 import android.os.Handler;
 import android.os.Message;
+import android.telephony.SmsMessage;
 import android.text.TextUtils;
 
 import com.android.internal.telephony.GsmAlphabet;
@@ -40,9 +41,9 @@
 /**
  * Factory class, used for decoding raw byte arrays, received from baseband,
  * into a CommandParams object.
- *
+ * @hide
  */
-class CommandParamsFactory extends Handler {
+public class CommandParamsFactory extends Handler {
     private static CommandParamsFactory sInstance = null;
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private IconLoader mIconLoader;
@@ -53,6 +54,7 @@
     private String mSavedLanguage;
     private String mRequestedLanguage;
     private boolean mNoAlphaUsrCnf = false;
+    private boolean mStkSmsSendViaTelephony = false;
 
     // constants
     static final int MSG_ID_LOAD_ICON_DONE = 1;
@@ -86,7 +88,15 @@
     private static final int MAX_GSM7_DEFAULT_CHARS = 239;
     private static final int MAX_UCS2_CHARS = 118;
 
-    static synchronized CommandParamsFactory getInstance(RilMessageDecoder caller,
+    /**
+     * Returns a singleton instance of CommandParamsFactory
+     * @param caller Class used for queuing raw ril messages, decoding them into
+     *               CommandParams objects and sending the result back to the CAT Service.
+     * @param fh IccFileHandler Object
+     * @param context The Context
+     * @return CommandParamsFactory instance
+     */
+    public static synchronized CommandParamsFactory getInstance(RilMessageDecoder caller,
             IccFileHandler fh, Context context) {
         if (sInstance != null) {
             return sInstance;
@@ -106,6 +116,12 @@
         } catch (NotFoundException e) {
             mNoAlphaUsrCnf = false;
         }
+        try {
+            mStkSmsSendViaTelephony = context.getResources().getBoolean(
+                    com.android.internal.R.bool.config_stk_sms_send_support);
+        } catch (NotFoundException e) {
+            mStkSmsSendViaTelephony = false;
+        }
     }
 
     private CommandDetails processCommandDetails(List<ComprehensionTlv> ctlvs) {
@@ -187,8 +203,14 @@
                 case GET_INPUT:
                     cmdPending = processGetInput(cmdDet, ctlvs);
                     break;
-                case SEND_DTMF:
                 case SEND_SMS:
+                    if (mStkSmsSendViaTelephony) {
+                        cmdPending = processSMSEventNotify(cmdDet, ctlvs);
+                    } else {
+                        cmdPending = processEventNotify(cmdDet, ctlvs);
+                    }
+                    break;
+                case SEND_DTMF:
                 case REFRESH:
                 case RUN_AT:
                 case SEND_SS:
@@ -735,6 +757,62 @@
         return false;
     }
 
+
+    /**
+     * Processes SMS_EVENT_NOTIFY message from baseband.
+     *
+     * Method extracts values such as Alpha Id,Icon Id,Sms Tpdu etc from the ComprehensionTlv,
+     * in order to create the CommandParams i.e. SendSMSParams.
+     *
+     * @param cmdDet Command Details container object.
+     * @param ctlvs List of ComprehensionTlv objects following Command Details
+     *        object and Device Identities object within the proactive command
+     * @return true if the command is processing is pending and additional
+     *         asynchronous processing is required.
+     * @hide
+     */
+    public boolean processSMSEventNotify(CommandDetails cmdDet,
+            List<ComprehensionTlv> ctlvs) throws ResultException {
+        CatLog.d(this, "processSMSEventNotify");
+
+        TextMessage textMsg = new TextMessage();
+        IconId iconId = null;
+
+        ComprehensionTlv ctlv = searchForTag(ComprehensionTlvTag.ALPHA_ID,
+                ctlvs);
+        /* Retrieves alpha identifier from an Alpha Identifier COMPREHENSION-TLV object.
+         *
+         * String corresponding to the alpha identifier is obtained and saved as part of
+         * the DisplayTextParams.
+         */
+        textMsg.text = ValueParser.retrieveAlphaId(ctlv, mNoAlphaUsrCnf);
+
+        ctlv = searchForTag(ComprehensionTlvTag.ICON_ID, ctlvs);
+        if (ctlv != null) {
+            // Retrieves icon id from the Icon Identifier COMPREHENSION-TLV object
+            iconId = ValueParser.retrieveIconId(ctlv);
+            textMsg.iconSelfExplanatory = iconId.selfExplanatory;
+        }
+
+        textMsg.responseNeeded = false;
+        DisplayTextParams displayTextParams = new DisplayTextParams(cmdDet, textMsg);
+        ComprehensionTlv ctlvTpdu = searchForTag(ComprehensionTlvTag.SMS_TPDU,
+                ctlvs);
+        // Retrieves smsMessage from the SMS TPDU COMPREHENSION-TLV object
+        SmsMessage smsMessage = ValueParser.retrieveTpduAsSmsMessage(ctlvTpdu);
+        if (smsMessage != null) {
+            TextMessage smsText = new TextMessage();
+            // Obtains the sms message content.
+            smsText.text = smsMessage.getMessageBody();
+            TextMessage destAddr = new TextMessage();
+            // Obtains the destination Address.
+            destAddr.text = smsMessage.getRecipientAddress();
+            mCmdParams = new SendSMSParams(cmdDet, smsText, destAddr, displayTextParams);
+            return false;
+        }
+        return true;
+    }
+
     /**
      * Processes SET_UP_EVENT_LIST proactive command from the SIM card.
      *
diff --git a/src/java/com/android/internal/telephony/cat/ComprehensionTlv.java b/src/java/com/android/internal/telephony/cat/ComprehensionTlv.java
index 5542b65..416c669 100644
--- a/src/java/com/android/internal/telephony/cat/ComprehensionTlv.java
+++ b/src/java/com/android/internal/telephony/cat/ComprehensionTlv.java
@@ -50,7 +50,7 @@
      * @param data Byte array containing the value
      * @param valueIndex Index in data at which the value starts
      */
-    protected ComprehensionTlv(int tag, boolean cr, int length, byte[] data,
+    public ComprehensionTlv(int tag, boolean cr, int length, byte[] data,
             int valueIndex) {
         mTag = tag;
         mCr = cr;
diff --git a/src/java/com/android/internal/telephony/cat/RilMessageDecoder.java b/src/java/com/android/internal/telephony/cat/RilMessageDecoder.java
old mode 100755
new mode 100644
index c25b59e..4b10cae
--- a/src/java/com/android/internal/telephony/cat/RilMessageDecoder.java
+++ b/src/java/com/android/internal/telephony/cat/RilMessageDecoder.java
@@ -32,8 +32,9 @@
 /**
  * Class used for queuing raw ril messages, decoding them into CommanParams
  * objects and sending the result back to the CAT Service.
+ * @hide
  */
-class RilMessageDecoder extends StateMachine {
+public class RilMessageDecoder extends StateMachine {
 
     // constants
     private static final int CMD_START = 1;
diff --git a/src/java/com/android/internal/telephony/cat/SendSMSParams.java b/src/java/com/android/internal/telephony/cat/SendSMSParams.java
new file mode 100644
index 0000000..f5108f0
--- /dev/null
+++ b/src/java/com/android/internal/telephony/cat/SendSMSParams.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.cat;
+
+class SendSMSParams extends CommandParams {
+
+    TextMessage mTextSmsMsg;
+    TextMessage mDestAddress;
+    DisplayTextParams mDisplayText;
+
+    SendSMSParams(CommandDetails cmdDet, TextMessage textMsg, TextMessage destAddress,
+            DisplayTextParams displayText) {
+        super(cmdDet);
+        mTextSmsMsg = textMsg;
+        mDestAddress = destAddress;
+        mDisplayText = displayText;
+    }
+
+}
diff --git a/src/java/com/android/internal/telephony/cat/ValueParser.java b/src/java/com/android/internal/telephony/cat/ValueParser.java
index 7c09136..bd17f48 100644
--- a/src/java/com/android/internal/telephony/cat/ValueParser.java
+++ b/src/java/com/android/internal/telephony/cat/ValueParser.java
@@ -18,16 +18,24 @@
 
 import android.compat.annotation.UnsupportedAppUsage;
 import android.os.Build;
+import android.telephony.SmsMessage;
 
 import com.android.internal.telephony.GsmAlphabet;
 import com.android.internal.telephony.cat.Duration.TimeUnit;
 import com.android.internal.telephony.uicc.IccUtils;
 
+import java.io.ByteArrayOutputStream;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
-abstract class ValueParser {
+
+/**
+ *  Util class that parses different entities from the ctlvs ComprehensionTlv List
+ * @hide
+ */
+public abstract class ValueParser {
 
     /**
      * Search for a Command Details object from a list.
@@ -352,4 +360,42 @@
             throw new ResultException(ResultCode.CMD_DATA_NOT_UNDERSTOOD);
         }
     }
+
+    /**
+     * Retrieve's the tpdu from the ctlv and creates the SmsMessage from pdu.
+     * @param ctlv  ComprehensionTlv value
+     * @return message SmsMessage to retrieve the destAddress and Text
+     * @throws ResultException
+     * @hide
+     */
+    public static SmsMessage retrieveTpduAsSmsMessage(ComprehensionTlv ctlv)
+            throws ResultException {
+        if (ctlv != null) {
+            byte[] rawValue = ctlv.getRawValue();
+            int valueIndex = ctlv.getValueIndex();
+            int length = ctlv.getLength();
+            if (length != 0) {
+                try {
+                    byte[] pdu = Arrays.copyOfRange(rawValue, valueIndex, (valueIndex + length));
+                    ByteArrayOutputStream bo = new ByteArrayOutputStream(pdu.length + 1);
+                    /* Framework's TPdu Parser expects the TPdu be prepended with SC-Address.
+                     * else the parser will throw an exception. So prepending TPdu with 0,
+                     * which indicates that there is no SC address and its length is 0.
+                     * This way Parser will skip parsing for SC-Address
+                     */
+                    bo.write(0x00);
+                    bo.write(pdu, 0, pdu.length);
+                    byte[] frameworkPdu = bo.toByteArray();
+                    //ToDO handle for 3GPP2 format bug: b/243123533
+                    SmsMessage message = SmsMessage.createFromPdu(frameworkPdu,
+                            SmsMessage.FORMAT_3GPP);
+                    return message;
+                } catch (IndexOutOfBoundsException e) {
+                    throw new ResultException(ResultCode.CMD_DATA_NOT_UNDERSTOOD);
+                }
+            }
+        }
+        return null;
+    }
+
 }
diff --git a/src/java/com/android/internal/telephony/cdma/CdmaInboundSmsHandler.java b/src/java/com/android/internal/telephony/cdma/CdmaInboundSmsHandler.java
index eb5f866..784c974 100644
--- a/src/java/com/android/internal/telephony/cdma/CdmaInboundSmsHandler.java
+++ b/src/java/com/android/internal/telephony/cdma/CdmaInboundSmsHandler.java
@@ -21,6 +21,7 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.res.Resources;
+import android.os.Looper;
 import android.os.Message;
 import android.os.RemoteCallback;
 import android.os.SystemProperties;
@@ -77,8 +78,8 @@
      * Create a new inbound SMS handler for CDMA.
      */
     private CdmaInboundSmsHandler(Context context, SmsStorageMonitor storageMonitor,
-            Phone phone, CdmaSMSDispatcher smsDispatcher) {
-        super("CdmaInboundSmsHandler", context, storageMonitor, phone);
+            Phone phone, CdmaSMSDispatcher smsDispatcher, Looper looper) {
+        super("CdmaInboundSmsHandler", context, storageMonitor, phone, looper);
         mSmsDispatcher = smsDispatcher;
         phone.mCi.setOnNewCdmaSms(getHandler(), EVENT_NEW_SMS, null);
 
@@ -169,9 +170,10 @@
      * Wait for state machine to enter startup state. We can't send any messages until then.
      */
     public static CdmaInboundSmsHandler makeInboundSmsHandler(Context context,
-            SmsStorageMonitor storageMonitor, Phone phone, CdmaSMSDispatcher smsDispatcher) {
+            SmsStorageMonitor storageMonitor, Phone phone, CdmaSMSDispatcher smsDispatcher,
+            Looper looper) {
         CdmaInboundSmsHandler handler = new CdmaInboundSmsHandler(context, storageMonitor,
-                phone, smsDispatcher);
+                phone, smsDispatcher, looper);
         handler.start();
         return handler;
     }
@@ -194,7 +196,8 @@
      * @return true if the message was handled here; false to continue processing
      */
     @Override
-    protected int dispatchMessageRadioSpecific(SmsMessageBase smsb, @SmsSource int smsSource) {
+    protected int dispatchMessageRadioSpecific(SmsMessageBase smsb, @SmsSource int smsSource,
+            int token) {
         SmsMessage sms = (SmsMessage) smsb;
         boolean isBroadcastType = (SmsEnvelope.MESSAGE_TYPE_BROADCAST == sms.getMessageType());
 
diff --git a/src/java/com/android/internal/telephony/cdma/CdmaSMSDispatcher.java b/src/java/com/android/internal/telephony/cdma/CdmaSMSDispatcher.java
index 5a830a8..ebc6342 100644
--- a/src/java/com/android/internal/telephony/cdma/CdmaSMSDispatcher.java
+++ b/src/java/com/android/internal/telephony/cdma/CdmaSMSDispatcher.java
@@ -93,6 +93,21 @@
     }
 
     @Override
+    protected SmsMessageBase.SubmitPduBase getSubmitPdu(String scAddr, String destAddr,
+            String message, boolean statusReportRequested, SmsHeader smsHeader, int priority,
+            int validityPeriod, int messageRef) {
+        return SMSDispatcherUtil.getSubmitPduCdma(scAddr, destAddr, message,
+                statusReportRequested, smsHeader, priority);
+    }
+
+    @Override
+    protected SmsMessageBase.SubmitPduBase getSubmitPdu(String scAddr, String destAddr,
+            int destPort, byte[] message, boolean statusReportRequested, int messageRef) {
+        return SMSDispatcherUtil.getSubmitPduCdma(scAddr, destAddr, destPort, message,
+                statusReportRequested);
+    }
+
+    @Override
     protected TextEncodingDetails calculateLength(CharSequence messageBody, boolean use7bitOnly) {
         return SMSDispatcherUtil.calculateLengthCdma(messageBody, use7bitOnly);
     }
diff --git a/src/java/com/android/internal/telephony/cdma/CdmaSmsBroadcastConfigInfo.java b/src/java/com/android/internal/telephony/cdma/CdmaSmsBroadcastConfigInfo.java
old mode 100755
new mode 100644
index b31df59..24ee56d
--- a/src/java/com/android/internal/telephony/cdma/CdmaSmsBroadcastConfigInfo.java
+++ b/src/java/com/android/internal/telephony/cdma/CdmaSmsBroadcastConfigInfo.java
@@ -17,6 +17,8 @@
 
 package com.android.internal.telephony.cdma;
 
+import java.util.Objects;
+
 /**
  * CdmaSmsBroadcastConfigInfo defines one configuration of Cdma Broadcast
  * Message to be received by the ME
@@ -84,4 +86,22 @@
             mFromServiceCategory + ", " + mToServiceCategory + "] " +
             (isSelected() ? "ENABLED" : "DISABLED");
     }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(mFromServiceCategory, mToServiceCategory, mLanguage, mSelected);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (!(obj instanceof CdmaSmsBroadcastConfigInfo)) {
+            return false;
+        }
+
+        CdmaSmsBroadcastConfigInfo other = (CdmaSmsBroadcastConfigInfo) obj;
+
+        return mFromServiceCategory == other.mFromServiceCategory
+                && mToServiceCategory == other.mToServiceCategory
+                && mLanguage == other.mLanguage && mSelected == other.mSelected;
+    }
 }
diff --git a/src/java/com/android/internal/telephony/data/AccessNetworksManager.java b/src/java/com/android/internal/telephony/data/AccessNetworksManager.java
index d034abd..a6b37a3 100644
--- a/src/java/com/android/internal/telephony/data/AccessNetworksManager.java
+++ b/src/java/com/android/internal/telephony/data/AccessNetworksManager.java
@@ -19,7 +19,6 @@
 import android.annotation.CallbackExecutor;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.annotation.StringDef;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
@@ -27,14 +26,15 @@
 import android.content.IntentFilter;
 import android.content.ServiceConnection;
 import android.content.pm.PackageManager;
+import android.os.AsyncResult;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Looper;
+import android.os.Message;
 import android.os.PersistableBundle;
 import android.os.Registrant;
 import android.os.RegistrantList;
 import android.os.RemoteException;
-import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.telephony.AccessNetworkConstants;
 import android.telephony.AccessNetworkConstants.AccessNetworkType;
@@ -56,14 +56,11 @@
 import android.util.SparseArray;
 
 import com.android.internal.telephony.Phone;
-import com.android.internal.telephony.RIL;
 import com.android.internal.telephony.SlidingWindowEventCounter;
 import com.android.telephony.Rlog;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -81,35 +78,9 @@
  */
 public class AccessNetworksManager extends Handler {
     private static final boolean DBG = false;
-    public static final String SYSTEM_PROPERTIES_IWLAN_OPERATION_MODE =
-            "ro.telephony.iwlan_operation_mode";
 
-    @Retention(RetentionPolicy.SOURCE)
-    @StringDef(prefix = {"IWLAN_OPERATION_MODE_"},
-            value = {
-                    IWLAN_OPERATION_MODE_DEFAULT,
-                    IWLAN_OPERATION_MODE_LEGACY,
-                    IWLAN_OPERATION_MODE_AP_ASSISTED})
-    public @interface IwlanOperationMode {}
-
-    /**
-     * IWLAN default mode. On device that has IRadio 1.4 or above, it means
-     * {@link #IWLAN_OPERATION_MODE_AP_ASSISTED}. On device that has IRadio 1.3 or below, it means
-     * {@link #IWLAN_OPERATION_MODE_LEGACY}.
-     */
-    public static final String IWLAN_OPERATION_MODE_DEFAULT = "default";
-
-    /**
-     * IWLAN legacy mode. IWLAN is completely handled by the modem, and when the device is on
-     * IWLAN, modem reports IWLAN as a RAT.
-     */
-    public static final String IWLAN_OPERATION_MODE_LEGACY = "legacy";
-
-    /**
-     * IWLAN application processor assisted mode. IWLAN is handled by the bound IWLAN data service
-     * and network service separately.
-     */
-    public static final String IWLAN_OPERATION_MODE_AP_ASSISTED = "AP-assisted";
+    /** Event to guide a transport type for initial data connection of emergency data network. */
+    private static final int EVENT_GUIDE_TRANSPORT_TYPE_FOR_EMERGENCY = 1;
 
     /**
      * The counters to detect frequent QNS attempt to change preferred network transport by ApnType.
@@ -210,6 +181,19 @@
         }
     }
 
+    @Override
+    public void handleMessage(@NonNull Message msg) {
+        switch (msg.what) {
+            case EVENT_GUIDE_TRANSPORT_TYPE_FOR_EMERGENCY:
+                AsyncResult ar = (AsyncResult) msg.obj;
+                int transport = (int) ar.result;
+                onEmergencyDataNetworkPreferredTransportChanged(transport);
+                break;
+            default:
+                loge("Unexpected event " + msg.what);
+        }
+    }
+
     private class AccessNetworksManagerDeathRecipient implements IBinder.DeathRecipient {
         @Override
         public void binderDied() {
@@ -332,6 +316,20 @@
         }
     }
 
+    private void onEmergencyDataNetworkPreferredTransportChanged(
+            @AccessNetworkConstants.TransportType int transportType) {
+        try {
+            logl("onEmergencyDataNetworkPreferredTransportChanged: "
+                    + AccessNetworkConstants.transportTypeToString(transportType));
+            if (mIQualifiedNetworksService != null) {
+                mIQualifiedNetworksService.reportEmergencyDataNetworkPreferredTransportChanged(
+                        mPhone.getPhoneId(), transportType);
+            }
+        } catch (Exception ex) {
+            loge("onEmergencyDataNetworkPreferredTransportChanged: ", ex);
+        }
+    }
+
     /**
      * Access networks manager callback. This should be only used by {@link DataNetworkController}.
      */
@@ -366,28 +364,19 @@
                 Context.CARRIER_CONFIG_SERVICE);
         mLogTag = "ANM-" + mPhone.getPhoneId();
         mApnTypeToQnsChangeNetworkCounter = new SparseArray<>();
-
-        if (isInLegacyMode()) {
-            log("operates in legacy mode.");
-            // For legacy mode, WWAN is the only transport to handle all data connections, even
-            // the IWLAN ones.
-            mAvailableTransports = new int[]{AccessNetworkConstants.TRANSPORT_TYPE_WWAN};
-        } else {
-            log("operates in AP-assisted mode.");
-            mAvailableTransports = new int[]{AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
-                    AccessNetworkConstants.TRANSPORT_TYPE_WLAN};
-            IntentFilter intentFilter = new IntentFilter();
-            intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
-            try {
-                Context contextAsUser = phone.getContext().createPackageContextAsUser(
-                        phone.getContext().getPackageName(), 0, UserHandle.ALL);
-                contextAsUser.registerReceiver(mConfigChangedReceiver, intentFilter,
-                        null /* broadcastPermission */, null);
-            } catch (PackageManager.NameNotFoundException e) {
-                loge("Package name not found: ", e);
-            }
-            bindQualifiedNetworksService();
+        mAvailableTransports = new int[]{AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
+                AccessNetworkConstants.TRANSPORT_TYPE_WLAN};
+        IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
+        try {
+            Context contextAsUser = phone.getContext().createPackageContextAsUser(
+                    phone.getContext().getPackageName(), 0, UserHandle.ALL);
+            contextAsUser.registerReceiver(mConfigChangedReceiver, intentFilter,
+                    null /* broadcastPermission */, null);
+        } catch (PackageManager.NameNotFoundException e) {
+            loge("Package name not found: ", e);
         }
+        bindQualifiedNetworksService();
 
         // Using post to delay the registering because data retry manager and data config
         // manager instances are created later than access networks manager.
@@ -415,6 +404,8 @@
                             mApnTypeToQnsChangeNetworkCounter.clear();
                         }
                     });
+            mPhone.registerForEmergencyDomainSelected(
+                    this, EVENT_GUIDE_TRANSPORT_TYPE_FOR_EMERGENCY, null);
         });
     }
 
@@ -490,8 +481,7 @@
     /**
      * Get the qualified network service package.
      *
-     * @return package name of the qualified networks service package. Return empty string when in
-     * legacy mode (i.e. Dedicated IWLAN data/network service is not supported).
+     * @return package name of the qualified networks service package.
      */
     private String getQualifiedNetworksServicePackageName() {
         // Read package name from the resource
@@ -568,30 +558,9 @@
     }
 
     /**
-     * @return {@code true} if the device operates in legacy mode, otherwise {@code false}.
+     * @return The available transports.
      */
-    public boolean isInLegacyMode() {
-        // Get IWLAN operation mode from the system property. If the system property is configured
-        // to default or not configured, the mode is tied to IRadio version. For 1.4 or above, it's
-        // AP-assisted mode, for 1.3 or below, it's legacy mode.
-        String mode = SystemProperties.get(SYSTEM_PROPERTIES_IWLAN_OPERATION_MODE);
-
-        if (mode.equals(IWLAN_OPERATION_MODE_AP_ASSISTED)) {
-            return false;
-        } else if (mode.equals(IWLAN_OPERATION_MODE_LEGACY)) {
-            return true;
-        }
-
-        return mPhone.getHalVersion().less(RIL.RADIO_HAL_VERSION_1_4);
-    }
-
-    /**
-     * @return The available transports. Note that on legacy devices, the only available transport
-     * would be WWAN only. If the device is configured as AP-assisted mode, the available transport
-     * will always be WWAN and WLAN (even if the device is not camped on IWLAN).
-     * See {@link #isInLegacyMode()} for mode details.
-     */
-    public synchronized @NonNull int[] getAvailableTransports() {
+    public @NonNull int[] getAvailableTransports() {
         return mAvailableTransports;
     }
 
@@ -626,11 +595,6 @@
      * @return The preferred transport.
      */
     public @TransportType int getPreferredTransport(@ApnType int apnType) {
-        // In legacy mode, always preferred on cellular.
-        if (isInLegacyMode()) {
-            return AccessNetworkConstants.TRANSPORT_TYPE_WWAN;
-        }
-
         return mPreferredTransports.get(apnType) == null
                 ? AccessNetworkConstants.TRANSPORT_TYPE_WWAN : mPreferredTransports.get(apnType);
     }
@@ -733,9 +697,6 @@
         }
 
         pw.decreaseIndent();
-        pw.println("isInLegacy=" + isInLegacyMode());
-        pw.println("IWLAN operation mode="
-                + SystemProperties.get(SYSTEM_PROPERTIES_IWLAN_OPERATION_MODE));
         pw.println("Local logs=");
         pw.increaseIndent();
         mLocalLog.dump(fd, pw, args);
diff --git a/src/java/com/android/internal/telephony/data/CellularNetworkValidator.java b/src/java/com/android/internal/telephony/data/CellularNetworkValidator.java
index c63676f..aa830ae 100644
--- a/src/java/com/android/internal/telephony/data/CellularNetworkValidator.java
+++ b/src/java/com/android/internal/telephony/data/CellularNetworkValidator.java
@@ -17,7 +17,7 @@
 package com.android.internal.telephony.data;
 
 import static android.telephony.AccessNetworkConstants.TRANSPORT_TYPE_WWAN;
-import static android.telephony.CarrierConfigManager.KEY_DATA_SWITCH_VALIDATION_MIN_GAP_LONG;
+import static android.telephony.CarrierConfigManager.KEY_DATA_SWITCH_VALIDATION_MIN_INTERVAL_MILLIS_LONG;
 import static android.telephony.NetworkRegistrationInfo.DOMAIN_PS;
 
 import android.content.Context;
@@ -43,6 +43,8 @@
 import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.metrics.TelephonyMetrics;
 import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 
 import java.util.Comparator;
 import java.util.HashMap;
@@ -164,9 +166,17 @@
 
         private String getValidationNetworkIdentity(int subId) {
             if (!SubscriptionManager.isUsableSubscriptionId(subId)) return null;
-            SubscriptionController subController = SubscriptionController.getInstance();
-            if (subController == null) return null;
-            Phone phone = PhoneFactory.getPhone(subController.getPhoneId(subId));
+            Phone phone;
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                if (SubscriptionManagerService.getInstance() == null) return null;
+                phone = PhoneFactory.getPhone(SubscriptionManagerService.getInstance()
+                        .getPhoneId(subId));
+            } else {
+                SubscriptionController subController = SubscriptionController.getInstance();
+                if (subController == null) return null;
+                phone = PhoneFactory.getPhone(subController.getPhoneId(subId));
+            }
+
             if (phone == null || phone.getServiceState() == null) return null;
 
             NetworkRegistrationInfo regInfo = phone.getServiceState().getNetworkRegistrationInfo(
@@ -192,7 +202,7 @@
             if (configManager != null) {
                 PersistableBundle b = configManager.getConfigForSubId(subId);
                 if (b != null) {
-                    ttl = b.getLong(KEY_DATA_SWITCH_VALIDATION_MIN_GAP_LONG);
+                    ttl = b.getLong(KEY_DATA_SWITCH_VALIDATION_MIN_INTERVAL_MILLIS_LONG);
                 }
             }
             // Ttl can't be bigger than one day for now.
@@ -257,10 +267,20 @@
         // If it's already validating the same subscription, do nothing.
         if (subId == mSubId) return;
 
-        if (!SubscriptionController.getInstance().isActiveSubId(subId)) {
-            logd("Failed to start validation. Inactive subId " + subId);
-            callback.onValidationDone(false, subId);
-            return;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
+                    .getSubscriptionInfoInternal(subId);
+            if (subInfo == null || !subInfo.isActive()) {
+                logd("Failed to start validation. Inactive subId " + subId);
+                callback.onValidationDone(false, subId);
+                return;
+            }
+        } else {
+            if (!SubscriptionController.getInstance().isActiveSubId(subId)) {
+                logd("Failed to start validation. Inactive subId " + subId);
+                callback.onValidationDone(false, subId);
+                return;
+            }
         }
 
         if (isValidating()) {
diff --git a/src/java/com/android/internal/telephony/data/DataConfigManager.java b/src/java/com/android/internal/telephony/data/DataConfigManager.java
index 71dde93..731aac7 100644
--- a/src/java/com/android/internal/telephony/data/DataConfigManager.java
+++ b/src/java/com/android/internal/telephony/data/DataConfigManager.java
@@ -19,10 +19,6 @@
 import android.annotation.CallbackExecutor;
 import android.annotation.NonNull;
 import android.annotation.StringDef;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
 import android.content.res.Resources;
 import android.net.LinkProperties;
 import android.net.NetworkCapabilities;
@@ -76,6 +72,12 @@
     /** The default timeout in ms for data network stuck in a transit state. */
     private static final int DEFAULT_NETWORK_TRANSIT_STATE_TIMEOUT_MS = 300000;
 
+    /** Default time threshold in ms to define a internet connection status to be stable. */
+    public static int DEFAULT_AUTO_DATA_SWITCH_STABILITY_TIME_MS = 10000;
+
+    /** The max number of retries when a pre-switching validation fails. */
+    public static int DEFAULT_AUTO_DATA_SWITCH_MAX_RETRY = 7;
+
     /** Event for carrier config changed. */
     private static final int EVENT_CARRIER_CONFIG_CHANGED = 1;
 
@@ -218,6 +220,12 @@
             "anomaly_network_handover_timeout";
     /** DeviceConfig key of anomaly report: True for enabling APN config invalidity detection */
     private static final String KEY_ANOMALY_APN_CONFIG_ENABLED = "anomaly_apn_config_enabled";
+    /** DeviceConfig key of the time threshold in ms for defining a network status to be stable. **/
+    private static final String KEY_AUTO_DATA_SWITCH_AVAILABILITY_STABILITY_TIME_THRESHOLD =
+            "auto_data_switch_availability_stability_time_threshold";
+    /** DeviceConfig key of the maximum number of retries when a validation for switching failed.**/
+    private static final String KEY_AUTO_DATA_SWITCH_VALIDATION_MAX_RETRY =
+            "auto_data_switch_validation_max_retry";
 
     /** Anomaly report thresholds for frequent setup data call failure. */
     private EventFrequency mSetupDataCallAnomalyReportThreshold;
@@ -260,6 +268,18 @@
      */
     private boolean mIsApnConfigAnomalyReportEnabled;
 
+    /**
+     * Time threshold in ms to define a internet connection status to be stable(e.g. out of service,
+     * in service, wifi is the default active network.etc), while -1 indicates auto switch feature
+     * disabled.
+     */
+    private long mAutoDataSwitchAvailabilityStabilityTimeThreshold;
+
+    /**
+     * The maximum number of retries when a pre-switching validation fails.
+     */
+    private int mAutoDataSwitchValidationMaxRetry;
+
     private @NonNull final Phone mPhone;
     private @NonNull final String mLogTag;
 
@@ -314,22 +334,13 @@
         log("DataConfigManager created.");
 
         mCarrierConfigManager = mPhone.getContext().getSystemService(CarrierConfigManager.class);
-
-        // Register for carrier configs update
-        IntentFilter filter = new IntentFilter();
-        filter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
-        mPhone.getContext().registerReceiver(new BroadcastReceiver() {
-            @Override
-            public void onReceive(Context context, Intent intent) {
-                if (intent.getAction().equals(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)) {
-                    if (mPhone.getPhoneId() == intent.getIntExtra(
-                            CarrierConfigManager.EXTRA_SLOT_INDEX,
-                            SubscriptionManager.INVALID_SIM_SLOT_INDEX)) {
+        // Callback send msg to handler thread, so callback itself can be executed in binder thread.
+        mCarrierConfigManager.registerCarrierConfigChangeListener(Runnable::run,
+                (slotIndex, subId, carrierId, specificCarrierId) -> {
+                    if (slotIndex == mPhone.getPhoneId()) {
                         sendEmptyMessage(EVENT_CARRIER_CONFIG_CHANGED);
                     }
-                }
-            }
-        }, filter, null, mPhone);
+                });
 
         // Register for device config update
         DeviceConfig.addOnPropertiesChangedListener(
@@ -427,6 +438,12 @@
                 KEY_ANOMALY_NETWORK_HANDOVER_TIMEOUT, DEFAULT_NETWORK_TRANSIT_STATE_TIMEOUT_MS);
         mIsApnConfigAnomalyReportEnabled = properties.getBoolean(
                 KEY_ANOMALY_APN_CONFIG_ENABLED, false);
+        mAutoDataSwitchAvailabilityStabilityTimeThreshold = properties.getInt(
+                KEY_AUTO_DATA_SWITCH_AVAILABILITY_STABILITY_TIME_THRESHOLD,
+                DEFAULT_AUTO_DATA_SWITCH_STABILITY_TIME_MS);
+        mAutoDataSwitchValidationMaxRetry = properties.getInt(
+                KEY_AUTO_DATA_SWITCH_VALIDATION_MAX_RETRY,
+                DEFAULT_AUTO_DATA_SWITCH_MAX_RETRY);
     }
 
     /**
@@ -919,6 +936,22 @@
     }
 
     /**
+     * @return The maximum number of retries when a validation for switching failed.
+     */
+    public int getAutoDataSwitchValidationMaxRetry() {
+        return mAutoDataSwitchValidationMaxRetry;
+    }
+
+    /**
+     * @return Time threshold in ms to define a internet connection status to be stable
+     * (e.g. out of service, in service, wifi is the default active network.etc), while -1 indicates
+     * auto switch feature disabled.
+     */
+    public long getAutoDataSwitchAvailabilityStabilityTimeThreshold() {
+        return mAutoDataSwitchAvailabilityStabilityTimeThreshold;
+    }
+
+    /**
      * Get the TCP config string, used by {@link LinkProperties#setTcpBufferSizes(String)}.
      * The config string will have the following form, with values in bytes:
      * "read_min,read_default,read_max,write_min,write_default,write_max"
@@ -1298,6 +1331,9 @@
         pw.println("mNetworkDisconnectingTimeout=" + mNetworkDisconnectingTimeout);
         pw.println("mNetworkHandoverTimeout=" + mNetworkHandoverTimeout);
         pw.println("mIsApnConfigAnomalyReportEnabled=" + mIsApnConfigAnomalyReportEnabled);
+        pw.println("mAutoDataSwitchAvailabilityStabilityTimeThreshold="
+                + mAutoDataSwitchAvailabilityStabilityTimeThreshold);
+        pw.println("mAutoDataSwitchValidationMaxRetry=" + mAutoDataSwitchValidationMaxRetry);
         pw.println("Metered APN types=" + mMeteredApnTypes.stream()
                 .map(ApnSetting::getApnTypeString).collect(Collectors.joining(",")));
         pw.println("Roaming metered APN types=" + mRoamingMeteredApnTypes.stream()
diff --git a/src/java/com/android/internal/telephony/data/DataEnabledOverride.java b/src/java/com/android/internal/telephony/data/DataEnabledOverride.java
deleted file mode 100644
index e639ba6..0000000
--- a/src/java/com/android/internal/telephony/data/DataEnabledOverride.java
+++ /dev/null
@@ -1,460 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.internal.telephony.data;
-
-import android.annotation.IntDef;
-import android.annotation.NonNull;
-import android.telephony.Annotation.ApnType;
-import android.telephony.SubscriptionManager;
-import android.telephony.TelephonyManager;
-import android.telephony.data.ApnSetting;
-import android.text.TextUtils;
-import android.util.ArrayMap;
-import android.util.Log;
-
-import com.android.internal.annotations.VisibleForTesting;
-import com.android.internal.telephony.Phone;
-import com.android.internal.telephony.PhoneConstants;
-import com.android.internal.telephony.PhoneFactory;
-import com.android.internal.telephony.SubscriptionController;
-import com.android.internal.telephony.data.DataEnabledOverride.OverrideConditions.Condition;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-
-/**
- * This class represents the rules for overriding data enabled settings in different conditions.
- * When data is disabled by the user, data can still be turned on temporarily when conditions
- * satisfy any rule here.
- */
-public class DataEnabledOverride {
-
-    private final Set<OverrideRule> mRules = new HashSet<>();
-
-    /**
-     * The rule for allowing data during voice call.
-     */
-    private static final OverrideRule OVERRIDE_RULE_ALLOW_DATA_DURING_VOICE_CALL =
-            new OverrideRule(ApnSetting.TYPE_ALL, OverrideConditions.CONDITION_IN_VOICE_CALL
-                    | OverrideConditions.CONDITION_NON_DEFAULT
-                    | OverrideConditions.CONDITION_DEFAULT_DATA_ENABLED
-                    | OverrideConditions.CONDITION_DSDS_ENABLED);
-
-    /**
-     * The rule for always allowing mms. Without adding any condition to the rule, any condition can
-     * satisfy this rule for mms.
-     */
-    private static final OverrideRule OVERRIDE_RULE_ALWAYS_ALLOW_MMS =
-            new OverrideRule(ApnSetting.TYPE_MMS, OverrideConditions.CONDITION_UNCONDITIONALLY);
-
-    /**
-     * Data enabled override rule
-     */
-    private static class OverrideRule {
-        /**
-         * APN type of the rule. The rule is APN type specific. The override is applicable to the
-         * specified APN type as well. For now we only support one APN type per rule. Can be
-         * expanded to multiple APN types in the future.
-         */
-        private final @ApnType int mApnType;
-
-        /** The required conditions for overriding */
-        private final OverrideConditions mRequiredConditions;
-
-        /**
-         * Constructor
-         *
-         * @param rule The override rule string. For example, {@code mms=nonDefault} or
-         * {@code default=voiceCall & nonDefault}
-         */
-        OverrideRule(@NonNull String rule) {
-            String[] tokens = rule.trim().split("\\s*=\\s*");
-            if (tokens.length != 2) {
-                throw new IllegalArgumentException("Invalid data enabled override rule format: "
-                        + rule);
-            }
-
-            if (TextUtils.isEmpty(tokens[0])) {
-                throw new IllegalArgumentException("APN type can't be empty");
-            }
-
-            mApnType = ApnSetting.getApnTypesBitmaskFromString(tokens[0]);
-            if (mApnType == ApnSetting.TYPE_NONE) {
-                throw new IllegalArgumentException("Invalid APN type. Rule=" + rule);
-            }
-
-            mRequiredConditions = new OverrideConditions(tokens[1]);
-        }
-
-        /**
-         * Constructor
-         *
-         * @param apnType APN type of the rule
-         * @param requiredConditions The required conditions for the rule
-         */
-        private OverrideRule(int apnType, int requiredConditions) {
-            mApnType = apnType;
-            mRequiredConditions = new OverrideConditions(requiredConditions);
-        }
-
-        /**
-         * Check if this rule can be satisfied by the given APN type and provided conditions.
-         *
-         * @param apnType APN type to check
-         * @param providedConditions The provided conditions to check
-         * @return {@code true} if satisfied
-         */
-        boolean isSatisfiedByConditions(@ApnType int apnType, @Condition int providedConditions) {
-            return (mApnType == apnType || mApnType == ApnSetting.TYPE_ALL)
-                    && mRequiredConditions.allMet(providedConditions);
-        }
-
-        @Override
-        public String toString() {
-            return ApnSetting.getApnTypeString(mApnType) + "=" + mRequiredConditions;
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            if (this == o) return true;
-            if (o == null || getClass() != o.getClass()) return false;
-            OverrideRule that = (OverrideRule) o;
-            return mApnType == that.mApnType
-                    && Objects.equals(mRequiredConditions, that.mRequiredConditions);
-        }
-
-        @Override
-        public int hashCode() {
-            return Objects.hash(mApnType, mRequiredConditions);
-        }
-    }
-
-    /**
-     * Represent the conditions for overriding data enabled settings
-     */
-    static class OverrideConditions {
-        // Possible values for data enabled override condition. Note these flags are bitmasks.
-        /** Unconditionally override enabled settings */
-        static final int CONDITION_UNCONDITIONALLY = 0;
-
-        /** Enable data only on subscription that is not user selected default data subscription */
-        static final int CONDITION_NON_DEFAULT = 1 << 0;
-
-        /** Enable data only when device has ongoing voice call */
-        static final int CONDITION_IN_VOICE_CALL = 1 << 1;
-
-        /** Enable data only when default data is on */
-        static final int CONDITION_DEFAULT_DATA_ENABLED = 1 << 2;
-
-        /** Enable data only when device is in DSDS mode */
-        static final int CONDITION_DSDS_ENABLED = 1 << 3;
-
-        /** Enable data unconditionally in string format */
-        static final String CONDITION_UNCONDITIONALLY_STRING = "unconditionally";
-
-        /** Enable data only on subscription that is not default in string format */
-        static final String CONDITION_NON_DEFAULT_STRING = "nonDefault";
-
-        /** Enable data only when device has ongoing voice call in string format */
-        static final String CONDITION_VOICE_CALL_STRING = "inVoiceCall";
-
-        /** Enable data only when default data is on in string format */
-        static final String CONDITION_DEFAULT_DATA_ENABLED_STRING = "DefaultDataOn";
-
-        /** Enable data only when device is in DSDS mode in string format */
-        static final String CONDITION_DSDS_ENABLED_STRING = "dsdsEnabled";
-
-        /** @hide */
-        @IntDef(flag = true, prefix = { "OVERRIDE_CONDITION_" }, value = {
-                CONDITION_NON_DEFAULT,
-                CONDITION_IN_VOICE_CALL,
-                CONDITION_DEFAULT_DATA_ENABLED,
-                CONDITION_DSDS_ENABLED
-        })
-        @Retention(RetentionPolicy.SOURCE)
-        public @interface Condition {}
-
-        private static final Map<Integer, String> OVERRIDE_CONDITION_INT_MAP = new ArrayMap<>();
-        private static final Map<String, Integer> OVERRIDE_CONDITION_STRING_MAP = new ArrayMap<>();
-
-        static {
-            OVERRIDE_CONDITION_INT_MAP.put(CONDITION_NON_DEFAULT,
-                    CONDITION_NON_DEFAULT_STRING);
-            OVERRIDE_CONDITION_INT_MAP.put(CONDITION_IN_VOICE_CALL,
-                    CONDITION_VOICE_CALL_STRING);
-            OVERRIDE_CONDITION_INT_MAP.put(CONDITION_DEFAULT_DATA_ENABLED,
-                    CONDITION_DEFAULT_DATA_ENABLED_STRING);
-            OVERRIDE_CONDITION_INT_MAP.put(CONDITION_DSDS_ENABLED,
-                    CONDITION_DSDS_ENABLED_STRING);
-
-            OVERRIDE_CONDITION_STRING_MAP.put(CONDITION_UNCONDITIONALLY_STRING,
-                    CONDITION_UNCONDITIONALLY);
-            OVERRIDE_CONDITION_STRING_MAP.put(CONDITION_NON_DEFAULT_STRING,
-                    CONDITION_NON_DEFAULT);
-            OVERRIDE_CONDITION_STRING_MAP.put(CONDITION_VOICE_CALL_STRING,
-                    CONDITION_IN_VOICE_CALL);
-            OVERRIDE_CONDITION_STRING_MAP.put(CONDITION_DEFAULT_DATA_ENABLED_STRING,
-                    CONDITION_DEFAULT_DATA_ENABLED);
-            OVERRIDE_CONDITION_STRING_MAP.put(CONDITION_DSDS_ENABLED_STRING,
-                    CONDITION_DSDS_ENABLED);
-        }
-
-        private final @Condition int mConditions;
-
-        /**
-         * Conditions for overriding data enabled setting
-         *
-         * @param conditions Conditions in string format
-         */
-        OverrideConditions(@NonNull String conditions) {
-            mConditions = getBitmaskFromString(conditions);
-        }
-
-        /**
-         * Conditions for overriding data enabled setting
-         *
-         * @param conditions Conditions in bitmask
-         */
-        OverrideConditions(@Condition int conditions) {
-            mConditions = conditions;
-        }
-
-        private static String getStringFromBitmask(@Condition int conditions) {
-            if (conditions == CONDITION_UNCONDITIONALLY) {
-                return CONDITION_UNCONDITIONALLY_STRING;
-            }
-            List<String> conditionsStrings = new ArrayList<>();
-            for (Integer condition : OVERRIDE_CONDITION_INT_MAP.keySet()) {
-                if ((conditions & condition) == condition) {
-                    conditionsStrings.add(OVERRIDE_CONDITION_INT_MAP.get(condition));
-                }
-            }
-            return TextUtils.join("&", conditionsStrings);
-        }
-
-        private static @Condition int getBitmaskFromString(@NonNull String str) {
-            if (TextUtils.isEmpty(str)) {
-                throw new IllegalArgumentException("Empty rule string");
-            }
-
-            String[] conditionStrings = str.trim().split("\\s*&\\s*");
-            int bitmask = 0;
-
-            for (String conditionStr : conditionStrings) {
-                if (!TextUtils.isEmpty(conditionStr)) {
-                    if (!OVERRIDE_CONDITION_STRING_MAP.containsKey(conditionStr)) {
-                        throw new IllegalArgumentException("Invalid conditions: " + str);
-                    }
-                    bitmask |= OVERRIDE_CONDITION_STRING_MAP.get(conditionStr);
-                }
-            }
-
-            return bitmask;
-        }
-
-        /**
-         * Check if provided conditions can meet all conditions in the rule.
-         *
-         * @param providedConditions The provided conditions
-         * @return {@code true} if all conditions are met.
-         */
-        boolean allMet(@Condition int providedConditions) {
-            return (providedConditions & mConditions) == mConditions;
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            if (this == o) return true;
-            if (o == null || getClass() != o.getClass()) return false;
-            OverrideConditions that = (OverrideConditions) o;
-            return mConditions == that.mConditions;
-        }
-
-        @Override
-        public int hashCode() {
-            return Objects.hash(mConditions);
-        }
-
-        @Override
-        public String toString() {
-            return getStringFromBitmask(mConditions);
-        }
-    }
-
-    /**
-     * Constructor
-     *
-     * @param rules Data enabled override rules
-     */
-    public DataEnabledOverride(@NonNull String rules) {
-        updateRules(rules);
-    }
-
-    /**
-     * Update the data enabled override rules.
-     *
-     * @param newRules New override rules
-     */
-    @VisibleForTesting
-    public void updateRules(@NonNull String newRules) {
-        mRules.clear();
-        String[] rulesString = newRules.trim().split("\\s*,\\s*");
-        for (String rule : rulesString) {
-            if (!TextUtils.isEmpty(rule)) {
-                mRules.add(new OverrideRule(rule));
-            }
-        }
-    }
-
-    /**
-     * Set always allowing MMS
-     *
-     * @param allow {@code true} if always allowing, otherwise {@code false}.
-     */
-    public void setAlwaysAllowMms(boolean allow) {
-        if (allow) {
-            mRules.add(OVERRIDE_RULE_ALWAYS_ALLOW_MMS);
-        } else {
-            mRules.remove(OVERRIDE_RULE_ALWAYS_ALLOW_MMS);
-        }
-    }
-
-    /**
-     * Set allowing mobile data during voice call. This is used for allowing data on the non-default
-     * data SIM. When a voice call is placed on the non-default data SIM on DSDS devices, users will
-     * not be able to use mobile data. By calling this API, data will be temporarily enabled on the
-     * non-default data SIM during the life cycle of the voice call.
-     *
-     * @param allow {@code true} if allowing using data during voice call, {@code false} if
-     * disallowed.
-     */
-    public void setDataAllowedInVoiceCall(boolean allow) {
-        if (allow) {
-            mRules.add(OVERRIDE_RULE_ALLOW_DATA_DURING_VOICE_CALL);
-        } else {
-            mRules.remove(OVERRIDE_RULE_ALLOW_DATA_DURING_VOICE_CALL);
-        }
-    }
-
-    /**
-     * Check if data is allowed during voice call.
-     *
-     * @return {@code true} if data is allowed during voice call.
-     */
-    public boolean isDataAllowedInVoiceCall() {
-        return mRules.contains(OVERRIDE_RULE_ALLOW_DATA_DURING_VOICE_CALL);
-    }
-
-    public boolean isMmsAlwaysAllowed() {
-        return mRules.contains(OVERRIDE_RULE_ALWAYS_ALLOW_MMS);
-    }
-
-    private boolean canSatisfyAnyRule(@ApnType int apnType,
-                                      @Condition int providedConditions) {
-        for (OverrideRule rule : mRules) {
-            if (rule.isSatisfiedByConditions(apnType, providedConditions)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    private @Condition int getCurrentConditions(Phone phone) {
-        int conditions = 0;
-
-        if (phone != null) {
-            // Check if the device is on voice call
-            if (phone.getState() != PhoneConstants.State.IDLE) {
-                conditions |= OverrideConditions.CONDITION_IN_VOICE_CALL;
-            }
-
-            int defaultDataSubId = SubscriptionController.getInstance().getDefaultDataSubId();
-
-            if (phone.getSubId() != defaultDataSubId) {
-                conditions |= OverrideConditions.CONDITION_NON_DEFAULT;
-            }
-
-            if (defaultDataSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
-                int phoneId = SubscriptionController.getInstance().getPhoneId(defaultDataSubId);
-                try {
-                    Phone defaultDataPhone = PhoneFactory.getPhone(phoneId);
-                    if (defaultDataPhone != null && defaultDataPhone.isUserDataEnabled()) {
-                        conditions |= OverrideConditions.CONDITION_DEFAULT_DATA_ENABLED;
-                    }
-                } catch (IllegalStateException e) {
-                    //ignore the exception and do not add the condition
-                    Log.d("DataEnabledOverride", e.getMessage());
-                }
-            }
-
-            if (TelephonyManager.from(phone.getContext()).isMultiSimEnabled()) {
-                conditions |= OverrideConditions.CONDITION_DSDS_ENABLED;
-            }
-        }
-
-        return conditions;
-    }
-
-    /**
-     * Check for given APN type if we should enable data.
-     *
-     * @param phone Phone object
-     * @param apnType APN type
-     * @return {@code true} if data should be enabled for the current condition.
-     */
-    public boolean shouldOverrideDataEnabledSettings(Phone phone, @ApnType int apnType) {
-        return canSatisfyAnyRule(apnType, getCurrentConditions(phone));
-    }
-
-    /**
-     * Get data enabled override rules.
-     *
-     * @return Get data enabled override rules in string format
-     */
-    @NonNull
-    public String getRules() {
-        List<String> ruleStrings = new ArrayList<>();
-        for (OverrideRule rule : mRules) {
-            ruleStrings.add(rule.toString());
-        }
-        return TextUtils.join(",", ruleStrings);
-    }
-
-    @Override
-    public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
-        DataEnabledOverride that = (DataEnabledOverride) o;
-        return mRules.equals(that.mRules);
-    }
-
-    @Override
-    public int hashCode() {
-        return Objects.hash(mRules);
-    }
-
-    @Override
-    public String toString() {
-        return "DataEnabledOverride: [rules=\"" + getRules() + "\"]";
-    }
-}
diff --git a/src/java/com/android/internal/telephony/data/DataNetwork.java b/src/java/com/android/internal/telephony/data/DataNetwork.java
index bb887c9..ec34e7d 100644
--- a/src/java/com/android/internal/telephony/data/DataNetwork.java
+++ b/src/java/com/android/internal/telephony/data/DataNetwork.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony.data;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_DATA;
+
 import android.annotation.CallbackExecutor;
 import android.annotation.IntDef;
 import android.annotation.NonNull;
@@ -264,8 +266,9 @@
     private static final int DEFAULT_INTERNET_NETWORK_SCORE = 50;
     private static final int OTHER_NETWORK_SCORE = 45;
 
-    @IntDef(prefix = {"DEACTIVATION_REASON_"},
+    @IntDef(prefix = {"TEAR_DOWN_REASON_"},
             value = {
+                    TEAR_DOWN_REASON_NONE,
                     TEAR_DOWN_REASON_CONNECTIVITY_SERVICE_UNWANTED,
                     TEAR_DOWN_REASON_SIM_REMOVAL,
                     TEAR_DOWN_REASON_AIRPLANE_MODE_ON,
@@ -298,6 +301,9 @@
             })
     public @interface TearDownReason {}
 
+    /** Data network was not torn down. */
+    public static final int TEAR_DOWN_REASON_NONE = 0;
+
     /** Data network tear down requested by connectivity service. */
     public static final int TEAR_DOWN_REASON_CONNECTIVITY_SERVICE_UNWANTED = 1;
 
@@ -375,7 +381,7 @@
     /** Data network tear down due to data profile not preferred. */
     public static final int TEAR_DOWN_REASON_DATA_PROFILE_NOT_PREFERRED = 26;
 
-    /** Data network tear down due to not allowed by policy. */
+    /** Data network tear down due to handover not allowed by policy. */
     public static final int TEAR_DOWN_REASON_NOT_ALLOWED_BY_POLICY = 27;
 
     /** Data network tear down due to illegal state. */
@@ -545,6 +551,10 @@
     /** Data network controller. */
     private final @NonNull DataNetworkController mDataNetworkController;
 
+    /** Data network controller callback. */
+    private final @NonNull DataNetworkController.DataNetworkControllerCallback
+            mDataNetworkControllerCallback;
+
     /** Data config manager. */
     private final @NonNull DataConfigManager mDataConfigManager;
 
@@ -624,6 +634,11 @@
     private @DataFailureCause int mFailCause = DataFailCause.NONE;
 
     /**
+     * The tear down reason if the data call is voluntarily deactivated, not due to failure.
+     */
+    private @TearDownReason int mTearDownReason = TEAR_DOWN_REASON_NONE;
+
+    /**
      * The retry delay in milliseconds from setup data failure.
      */
     private long mRetryDelayMillis = DataCallResponse.RETRY_DURATION_UNDEFINED;
@@ -783,9 +798,10 @@
          *
          * @param dataNetwork The data network.
          * @param cause The disconnect cause.
+         * @param tearDownReason The reason the network was torn down
          */
         public abstract void onDisconnected(@NonNull DataNetwork dataNetwork,
-                @DataFailureCause int cause);
+                @DataFailureCause int cause, @TearDownReason int tearDownReason);
 
         /**
          * Called when handover between IWLAN and cellular network succeeded.
@@ -875,12 +891,14 @@
         mAccessNetworksManager = phone.getAccessNetworksManager();
         mVcnManager = mPhone.getContext().getSystemService(VcnManager.class);
         mDataNetworkController = phone.getDataNetworkController();
+        mDataNetworkControllerCallback = new DataNetworkController.DataNetworkControllerCallback(
+                getHandler()::post) {
+            @Override
+            public void onSubscriptionPlanOverride() {
+                sendMessage(EVENT_SUBSCRIPTION_PLAN_OVERRIDE);
+            }};
         mDataNetworkController.registerDataNetworkControllerCallback(
-                new DataNetworkController.DataNetworkControllerCallback(getHandler()::post) {
-                    @Override
-                    public void onSubscriptionPlanOverride() {
-                        sendMessage(EVENT_SUBSCRIPTION_PLAN_OVERRIDE);
-                    }});
+                mDataNetworkControllerCallback);
         mDataConfigManager = mDataNetworkController.getDataConfigManager();
         mDataCallSessionStats = new DataCallSessionStats(mPhone);
         mDataNetworkCallback = callback;
@@ -896,8 +914,9 @@
         mDataAllowedReason = dataAllowedReason;
         dataProfile.setLastSetupTimestamp(SystemClock.elapsedRealtime());
         mAttachedNetworkRequestList.addAll(networkRequestList);
-        mCid.put(AccessNetworkConstants.TRANSPORT_TYPE_WWAN, INVALID_CID);
-        mCid.put(AccessNetworkConstants.TRANSPORT_TYPE_WLAN, INVALID_CID);
+        for (int transportType : mAccessNetworksManager.getAvailableTransports()) {
+            mCid.put(transportType, INVALID_CID);
+        }
         mTcpBufferSizes = mDataConfigManager.getDefaultTcpConfigString();
         mTelephonyDisplayInfo = mPhone.getDisplayInfoController().getTelephonyDisplayInfo();
 
@@ -1024,7 +1043,7 @@
             mPhone.getDisplayInfoController().registerForTelephonyDisplayInfoChanged(
                     getHandler(), EVENT_DISPLAY_INFO_CHANGED, null);
             mPhone.getServiceStateTracker().registerForServiceStateChanged(getHandler(),
-                    EVENT_SERVICE_STATE_CHANGED);
+                    EVENT_SERVICE_STATE_CHANGED, null);
             for (int transport : mAccessNetworksManager.getAvailableTransports()) {
                 mDataServiceManagers.get(transport)
                         .registerForDataCallListChanged(getHandler(), EVENT_DATA_STATE_CHANGED);
@@ -1561,7 +1580,7 @@
 
             if (mEverConnected) {
                 mDataNetworkCallback.invokeFromExecutor(() -> mDataNetworkCallback
-                        .onDisconnected(DataNetwork.this, mFailCause));
+                        .onDisconnected(DataNetwork.this, mFailCause, mTearDownReason));
                 if (mTransport == AccessNetworkConstants.TRANSPORT_TYPE_WWAN) {
                     unregisterForWwanEvents();
                 }
@@ -1572,6 +1591,8 @@
             }
             notifyPreciseDataConnectionState();
             mNetworkAgent.unregister();
+            mDataNetworkController.unregisterDataNetworkControllerCallback(
+                    mDataNetworkControllerCallback);
             mDataCallSessionStats.onDataCallDisconnected(mFailCause);
 
             if (mTransport == AccessNetworkConstants.TRANSPORT_TYPE_WLAN
@@ -2371,14 +2392,16 @@
         logl("onSetupResponse: resultCode=" + DataServiceCallback.resultCodeToString(resultCode)
                 + ", response=" + response);
         mFailCause = getFailCauseFromDataCallResponse(resultCode, response);
-        validateDataCallResponse(response);
+        validateDataCallResponse(response, true /*isSetupResponse*/);
         if (mFailCause == DataFailCause.NONE) {
-            if (mDataNetworkController.isNetworkInterfaceExisting(response.getInterfaceName())) {
-                logl("Interface " + response.getInterfaceName() + " already existing. Silently "
-                        + "tear down now.");
+            DataNetwork dataNetwork = mDataNetworkController.getDataNetworkByInterface(
+                    response.getInterfaceName());
+            if (dataNetwork != null) {
+                logl("Interface " + response.getInterfaceName() + " has been already used by "
+                        + dataNetwork + ". Silently tear down now.");
                 // If this is a pre-5G data setup, that means APN database has some problems. For
                 // example, different APN settings have the same APN name.
-                if (response.getTrafficDescriptors().isEmpty()) {
+                if (response.getTrafficDescriptors().isEmpty() && dataNetwork.isConnected()) {
                     reportAnomaly("Duplicate network interface " + response.getInterfaceName()
                             + " detected.", "62f66e7e-8d71-45de-a57b-dc5c78223fd5");
                 }
@@ -2441,8 +2464,10 @@
      * If the {@link DataCallResponse} contains invalid info, triggers an anomaly report.
      *
      * @param response The response to be validated
+     * @param isSetupResponse {@code true} if the response is for initial data call setup
      */
-    private void validateDataCallResponse(@Nullable DataCallResponse response) {
+    private void validateDataCallResponse(@Nullable DataCallResponse response,
+            boolean isSetupResponse) {
         if (response == null
                 || response.getLinkStatus() == DataCallResponse.LINK_STATUS_INACTIVE) return;
         int failCause = response.getCause();
@@ -2462,9 +2487,11 @@
                 reportAnomaly("Invalid DataCallResponse detected",
                         "1f273e9d-b09c-46eb-ad1c-421d01f61164");
             }
+            // Check IP for initial setup response
             NetworkRegistrationInfo nri = getNetworkRegistrationInfo();
-            if (mDataProfile.getApnSetting() != null && nri != null && nri.isInService()) {
-                boolean isRoaming = nri.getInitialRegistrationState()
+            if (isSetupResponse
+                    && mDataProfile.getApnSetting() != null && nri != null && nri.isInService()) {
+                boolean isRoaming = nri.getNetworkRegistrationState()
                         == NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING;
                 int protocol = isRoaming ? mDataProfile.getApnSetting().getRoamingProtocol()
                         : mDataProfile.getApnSetting().getProtocol();
@@ -2511,9 +2538,9 @@
             log("Remove network since deactivate request returned an error.");
             mFailCause = DataFailCause.RADIO_NOT_AVAILABLE;
             transitionTo(mDisconnectedState);
-        } else if (mPhone.getHalVersion().less(RIL.RADIO_HAL_VERSION_2_0)) {
+        } else if (mPhone.getHalVersion(HAL_SERVICE_DATA).less(RIL.RADIO_HAL_VERSION_2_0)) {
             log("Remove network on deactivate data response on old HAL "
-                    + mPhone.getHalVersion());
+                    + mPhone.getHalVersion(HAL_SERVICE_DATA));
             mFailCause = DataFailCause.LOST_CONNECTION;
             transitionTo(mDisconnectedState);
         }
@@ -2528,6 +2555,7 @@
         if (getCurrentState() == null || isDisconnected()) {
             return;
         }
+        mTearDownReason = reason;
         sendMessage(obtainMessage(EVENT_TEAR_DOWN_NETWORK, reason));
     }
 
@@ -2622,7 +2650,7 @@
         if (response != null) {
             if (!response.equals(mDataCallResponse)) {
                 log("onDataStateChanged: " + response);
-                validateDataCallResponse(response);
+                validateDataCallResponse(response, false /*isSetupResponse*/);
                 mDataCallResponse = response;
                 if (response.getLinkStatus() != DataCallResponse.LINK_STATUS_INACTIVE) {
                     updateDataNetwork(response);
@@ -3200,7 +3228,7 @@
         logl("onHandoverResponse: resultCode=" + DataServiceCallback.resultCodeToString(resultCode)
                 + ", response=" + response);
         mFailCause = getFailCauseFromDataCallResponse(resultCode, response);
-        validateDataCallResponse(response);
+        validateDataCallResponse(response, false /*isSetupResponse*/);
         if (mFailCause == DataFailCause.NONE) {
             // Handover succeeded.
 
@@ -3353,6 +3381,8 @@
      */
     public static @NonNull String tearDownReasonToString(@TearDownReason int reason) {
         switch (reason) {
+            case TEAR_DOWN_REASON_NONE:
+                return "NONE";
             case TEAR_DOWN_REASON_CONNECTIVITY_SERVICE_UNWANTED:
                 return "CONNECTIVITY_SERVICE_UNWANTED";
             case TEAR_DOWN_REASON_SIM_REMOVAL:
diff --git a/src/java/com/android/internal/telephony/data/DataNetworkController.java b/src/java/com/android/internal/telephony/data/DataNetworkController.java
index 9eee448..39edee3 100644
--- a/src/java/com/android/internal/telephony/data/DataNetworkController.java
+++ b/src/java/com/android/internal/telephony/data/DataNetworkController.java
@@ -82,7 +82,6 @@
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.SlidingWindowEventCounter;
-import com.android.internal.telephony.SubscriptionInfoUpdater;
 import com.android.internal.telephony.TelephonyComponentFactory;
 import com.android.internal.telephony.data.AccessNetworksManager.AccessNetworksManagerCallback;
 import com.android.internal.telephony.data.DataConfigManager.DataConfigManagerCallback;
@@ -780,13 +779,10 @@
         log("DataNetworkController created.");
 
         mAccessNetworksManager = phone.getAccessNetworksManager();
-        mDataServiceManagers.put(AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
-                new DataServiceManager(mPhone, looper, AccessNetworkConstants.TRANSPORT_TYPE_WWAN));
-        if (!mAccessNetworksManager.isInLegacyMode()) {
-            mDataServiceManagers.put(AccessNetworkConstants.TRANSPORT_TYPE_WLAN,
-                    new DataServiceManager(mPhone, looper,
-                            AccessNetworkConstants.TRANSPORT_TYPE_WLAN));
+        for (int transport : mAccessNetworksManager.getAvailableTransports()) {
+            mDataServiceManagers.put(transport, new DataServiceManager(mPhone, looper, transport));
         }
+
         mDataConfigManager = new DataConfigManager(mPhone, looper);
 
         // ========== Anomaly counters ==========
@@ -969,7 +965,7 @@
         });
 
         mPhone.getServiceStateTracker().registerForServiceStateChanged(this,
-                EVENT_SERVICE_STATE_CHANGED);
+                EVENT_SERVICE_STATE_CHANGED, null);
         mDataConfigManager.registerCallback(new DataConfigManagerCallback(this::post) {
             @Override
             public void onCarrierConfigChanged() {
@@ -989,12 +985,10 @@
         mDataServiceManagers.get(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
                 .registerForServiceBindingChanged(this, EVENT_DATA_SERVICE_BINDING_CHANGED);
 
-        if (!mAccessNetworksManager.isInLegacyMode()) {
-            mPhone.getServiceStateTracker().registerForServiceStateChanged(this,
-                    EVENT_SERVICE_STATE_CHANGED);
-            mDataServiceManagers.get(AccessNetworkConstants.TRANSPORT_TYPE_WLAN)
-                    .registerForServiceBindingChanged(this, EVENT_DATA_SERVICE_BINDING_CHANGED);
-        }
+        mPhone.getServiceStateTracker().registerForServiceStateChanged(this,
+                EVENT_SERVICE_STATE_CHANGED, null);
+        mDataServiceManagers.get(AccessNetworkConstants.TRANSPORT_TYPE_WLAN)
+                .registerForServiceBindingChanged(this, EVENT_DATA_SERVICE_BINDING_CHANGED);
 
         mPhone.getContext().getSystemService(TelephonyRegistryManager.class)
                 .addOnSubscriptionsChangedListener(new OnSubscriptionsChangedListener() {
@@ -1567,7 +1561,8 @@
 
             if (!mDataSettingsManager.isDataEnabled()
                     && networkRequest.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)
-                    && mDataSettingsManager.isMmsAlwaysAllowed()) {
+                    && mDataSettingsManager.isMobileDataPolicyEnabled(TelephonyManager
+                    .MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED)) {
                 // We reach here when data is disabled, but MMS always-allowed is enabled.
                 // (Note that isDataEnabled(ApnSetting.TYPE_MMS) returns true in this case, so it
                 // would not generate any soft disallowed reason. We need to explicitly handle it.)
@@ -1855,7 +1850,7 @@
             }
         }
 
-        log("Evaluated " + dataNetwork + ", " + evaluation.toString());
+        log("Evaluated " + dataNetwork + ", " + evaluation);
         return evaluation;
     }
 
@@ -2049,7 +2044,7 @@
                     return DataNetwork.TEAR_DOWN_REASON_ONLY_ALLOWED_SINGLE_NETWORK;
             }
         }
-        return 0;
+        return DataNetwork.TEAR_DOWN_REASON_NONE;
     }
 
     /**
@@ -2149,16 +2144,19 @@
     }
 
     /**
-     * Check if there are existing networks having the same interface name.
+     * Get data network by interface name.
      *
-     * @param interfaceName The interface name to check.
-     * @return {@code true} if the existing network has the same interface name.
+     * @param interfaceName The network interface name.
+     * @return The data network if found.
      */
-    public boolean isNetworkInterfaceExisting(@NonNull String interfaceName) {
+    @Nullable
+    public DataNetwork getDataNetworkByInterface(@NonNull String interfaceName) {
         return mDataNetworkList.stream()
                 .filter(dataNetwork -> !dataNetwork.isDisconnecting())
-                .anyMatch(dataNetwork -> interfaceName.equals(
-                        dataNetwork.getLinkProperties().getInterfaceName()));
+                .filter(dataNetwork -> interfaceName.equals(
+                        dataNetwork.getLinkProperties().getInterfaceName()))
+                .findFirst()
+                .orElse(null);
     }
 
     /**
@@ -2327,7 +2325,7 @@
         log("onCarrierConfigUpdated: config is "
                 + (mDataConfigManager.isConfigCarrierSpecific() ? "" : "not ")
                 + "carrier specific. mSimState="
-                + SubscriptionInfoUpdater.simStateString(mSimState));
+                + TelephonyManager.simStateToString(mSimState));
         updateNetworkRequestsPriority();
         onReevaluateUnsatisfiedNetworkRequests(DataEvaluationReason.DATA_CONFIG_CHANGED);
     }
@@ -2488,9 +2486,9 @@
 
                     @Override
                     public void onDisconnected(@NonNull DataNetwork dataNetwork,
-                            @DataFailureCause int cause) {
+                            @DataFailureCause int cause, @TearDownReason int tearDownReason) {
                         DataNetworkController.this.onDataNetworkDisconnected(
-                                dataNetwork, cause);
+                                dataNetwork, cause, tearDownReason);
                     }
 
                     @Override
@@ -2812,11 +2810,13 @@
      *
      * @param dataNetwork The data network.
      * @param cause The disconnect cause.
+     * @param tearDownReason The reason the network was torn down
      */
     private void onDataNetworkDisconnected(@NonNull DataNetwork dataNetwork,
-            @DataFailureCause int cause) {
+            @DataFailureCause int cause, @TearDownReason int tearDownReason) {
         logl("onDataNetworkDisconnected: " + dataNetwork + ", cause="
-                + DataFailCause.toString(cause) + "(" + cause + ")");
+                + DataFailCause.toString(cause) + "(" + cause + "), tearDownReason="
+                + DataNetwork.tearDownReasonToString(tearDownReason));
         mDataNetworkList.remove(dataNetwork);
         mPendingImsDeregDataNetworks.remove(dataNetwork);
         mDataRetryManager.cancelPendingHandoverRetry(dataNetwork);
@@ -2837,11 +2837,13 @@
                     () -> callback.onAnyDataNetworkExistingChanged(mAnyDataNetworkExisting)));
         }
 
+        // Immediately reestablish on target transport if network was torn down due to policy
+        long delayMillis = tearDownReason == DataNetwork.TEAR_DOWN_REASON_HANDOVER_NOT_ALLOWED
+                ? 0 : mDataConfigManager.getRetrySetupAfterDisconnectMillis();
         // Sometimes network was unsolicitedly reported lost for reasons. We should re-evaluate
         // and see if data network can be re-established again.
         sendMessageDelayed(obtainMessage(EVENT_REEVALUATE_UNSATISFIED_NETWORK_REQUESTS,
-                DataEvaluationReason.RETRY_AFTER_DISCONNECTED),
-                mDataConfigManager.getRetrySetupAfterDisconnectMillis());
+                        DataEvaluationReason.RETRY_AFTER_DISCONNECTED), delayMillis);
     }
 
     /**
@@ -2985,7 +2987,7 @@
      * @param simState SIM state. (Note this is mixed with card state and application state.)
      */
     private void onSimStateChanged(@SimState int simState) {
-        log("onSimStateChanged: state=" + SubscriptionInfoUpdater.simStateString(simState));
+        log("onSimStateChanged: state=" + TelephonyManager.simStateToString(simState));
         if (mSimState != simState) {
             mSimState = simState;
             if (simState == TelephonyManager.SIM_STATE_ABSENT) {
@@ -3168,6 +3170,12 @@
             log("Found more network requests that can be satisfied. " + networkRequestList);
             dataNetwork.attachNetworkRequests(networkRequestList);
         }
+
+        if (dataNetwork.getNetworkCapabilities().hasCapability(
+                NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
+            // Update because DataNetwork#isInternetSupported might have changed with capabilities.
+            updateOverallInternetDataState();
+        }
     }
 
     /**
@@ -3318,9 +3326,9 @@
     }
 
     /**
-     * Update the internet data network state. For now only {@link TelephonyManager#DATA_CONNECTED}
-     * , {@link TelephonyManager#DATA_SUSPENDED}, and
-     * {@link TelephonyManager#DATA_DISCONNECTED} are supported.
+     * Update the internet data network state. For now only {@link TelephonyManager#DATA_CONNECTED},
+     * {@link TelephonyManager#DATA_SUSPENDED}, and {@link TelephonyManager#DATA_DISCONNECTED}
+     * are supported.
      */
     private void updateOverallInternetDataState() {
         boolean anyInternetConnected = mDataNetworkList.stream()
@@ -3588,8 +3596,8 @@
 
     /**
      * Get the internet data network state. Note that this is the best effort if more than one
-     * data network supports internet. For now only {@link TelephonyManager#DATA_CONNECTED}
-     * , {@link TelephonyManager#DATA_SUSPENDED}, and {@link TelephonyManager#DATA_DISCONNECTED}
+     * data network supports internet. For now only {@link TelephonyManager#DATA_CONNECTED},
+     * {@link TelephonyManager#DATA_SUSPENDED}, and {@link TelephonyManager#DATA_DISCONNECTED}
      * are supported.
      *
      * @return The data network state.
@@ -3699,7 +3707,7 @@
         pw.println("mImsDataNetworkState="
                 + TelephonyUtils.dataStateToString(mImsDataNetworkState));
         pw.println("mDataServiceBound=" + mDataServiceBound);
-        pw.println("mSimState=" + SubscriptionInfoUpdater.simStateString(mSimState));
+        pw.println("mSimState=" + TelephonyManager.simStateToString(mSimState));
         pw.println("mDataNetworkControllerCallbacks=" + mDataNetworkControllerCallbacks);
         pw.println("Subscription plans:");
         pw.increaseIndent();
diff --git a/src/java/com/android/internal/telephony/data/DataRetryManager.java b/src/java/com/android/internal/telephony/data/DataRetryManager.java
index 0beea1f..c2b3b9a 100644
--- a/src/java/com/android/internal/telephony/data/DataRetryManager.java
+++ b/src/java/com/android/internal/telephony/data/DataRetryManager.java
@@ -958,10 +958,9 @@
                 DataRetryManager.this.onCarrierConfigUpdated();
             }
         });
-        mDataServiceManagers.get(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
-                .registerForApnUnthrottled(this, EVENT_DATA_PROFILE_UNTHROTTLED);
-        if (!mPhone.getAccessNetworksManager().isInLegacyMode()) {
-            mDataServiceManagers.get(AccessNetworkConstants.TRANSPORT_TYPE_WLAN)
+
+        for (int transport : mPhone.getAccessNetworksManager().getAvailableTransports()) {
+            mDataServiceManagers.get(transport)
                     .registerForApnUnthrottled(this, EVENT_DATA_PROFILE_UNTHROTTLED);
         }
         mDataProfileManager.registerCallback(new DataProfileManagerCallback(this::post) {
diff --git a/src/java/com/android/internal/telephony/data/DataSettingsManager.java b/src/java/com/android/internal/telephony/data/DataSettingsManager.java
index 24fee82..014c2bb 100644
--- a/src/java/com/android/internal/telephony/data/DataSettingsManager.java
+++ b/src/java/com/android/internal/telephony/data/DataSettingsManager.java
@@ -19,7 +19,6 @@
 import android.annotation.CallbackExecutor;
 import android.annotation.NonNull;
 import android.content.ContentResolver;
-import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.Handler;
 import android.os.Looper;
@@ -32,9 +31,11 @@
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
 import android.telephony.TelephonyManager;
+import android.telephony.TelephonyManager.MobileDataPolicy;
 import android.telephony.TelephonyRegistryManager;
 import android.telephony.data.ApnSetting;
 import android.telephony.data.ApnSetting.ApnType;
+import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.ArraySet;
 import android.util.IndentingPrintWriter;
@@ -42,13 +43,20 @@
 
 import com.android.internal.telephony.GlobalSettingsHelper;
 import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.SettingsObserver;
 import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.data.DataConfigManager.DataConfigManagerCallback;
+import com.android.internal.telephony.metrics.DeviceTelephonyPropertiesStats;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
+import com.android.internal.telephony.util.TelephonyUtils;
 import com.android.telephony.Rlog;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.Executor;
@@ -59,6 +67,9 @@
  * data roaming settings, etc...
  */
 public class DataSettingsManager extends Handler {
+    /** Invalid mobile data policy **/
+    private static final int INVALID_MOBILE_DATA_POLICY = -1;
+
     /** Event for call state changed. */
     private static final int EVENT_CALL_STATE_CHANGED = 2;
     /** Event for subscriptions updated. */
@@ -67,10 +78,9 @@
     private static final int EVENT_SET_DATA_ENABLED_FOR_REASON = 5;
     /** Event for set data roaming enabled. */
     private static final int EVENT_SET_DATA_ROAMING_ENABLED = 6;
-    /** Event for set always allow MMS data. */
-    private static final int EVENT_SET_ALWAYS_ALLOW_MMS_DATA = 7;
-    /** Event for set allow data during voice call. */
-    private static final int EVENT_SET_ALLOW_DATA_DURING_VOICE_CALL = 8;
+    /** Event for set mobile data policy. */
+    private static final int EVENT_SET_MOBILE_DATA_POLICY = 7;
+
     /** Event for device provisioned changed. */
     private static final int EVENT_PROVISIONED_CHANGED = 9;
     /** Event for provisioning data enabled setting changed. */
@@ -83,8 +93,8 @@
     private final SettingsObserver mSettingsObserver;
     private final String mLogTag;
     private final LocalLog mLocalLog = new LocalLog(128);
+    private Set<Integer> mEnabledMobileDataPolicy = new HashSet<>();
     private int mSubId;
-    private DataEnabledOverride mDataEnabledOverride;
 
     /** Data config manager */
     private final @NonNull DataConfigManager mDataConfigManager;
@@ -171,7 +181,7 @@
         mResolver = mPhone.getContext().getContentResolver();
         registerCallback(callback);
         mDataConfigManager = dataNetworkController.getDataConfigManager();
-        mDataEnabledOverride = getDataEnabledOverride();
+        refreshEnabledMobileDataPolicy();
         mSettingsObserver = new SettingsObserver(mPhone.getContext(), this);
         mDataEnabledSettings.put(TelephonyManager.DATA_ENABLED_REASON_POLICY, true);
         mDataEnabledSettings.put(TelephonyManager.DATA_ENABLED_REASON_CARRIER, true);
@@ -191,7 +201,7 @@
             }
             case EVENT_SUBSCRIPTIONS_CHANGED: {
                 mSubId = (int) msg.obj;
-                mDataEnabledOverride = getDataEnabledOverride();
+                refreshEnabledMobileDataPolicy();
                 updateDataEnabledAndNotify(TelephonyManager.DATA_ENABLED_REASON_USER);
                 mPhone.notifyUserMobileDataStateChanged(isUserDataEnabled());
                 break;
@@ -225,34 +235,10 @@
                 setDataRoamingFromUserAction();
                 break;
             }
-            case EVENT_SET_ALWAYS_ALLOW_MMS_DATA: {
-                boolean alwaysAllow = (boolean) msg.obj;
-                if (alwaysAllow == isMmsAlwaysAllowed()) {
-                    break;
-                }
-                logl("AlwaysAllowMmsData changed to " + alwaysAllow);
-                mDataEnabledOverride.setAlwaysAllowMms(alwaysAllow);
-                if (SubscriptionController.getInstance()
-                        .setDataEnabledOverrideRules(mSubId, mDataEnabledOverride.getRules())) {
-                    updateDataEnabledAndNotify(TelephonyManager.DATA_ENABLED_REASON_OVERRIDE);
-                    notifyDataEnabledOverrideChanged(alwaysAllow,
-                            TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED);
-                }
-                break;
-            }
-            case EVENT_SET_ALLOW_DATA_DURING_VOICE_CALL: {
-                boolean allow = (boolean) msg.obj;
-                if (allow == isDataAllowedInVoiceCall()) {
-                    break;
-                }
-                logl("AllowDataDuringVoiceCall changed to " + allow);
-                mDataEnabledOverride.setDataAllowedInVoiceCall(allow);
-                if (SubscriptionController.getInstance()
-                        .setDataEnabledOverrideRules(mSubId, mDataEnabledOverride.getRules())) {
-                    updateDataEnabledAndNotify(TelephonyManager.DATA_ENABLED_REASON_OVERRIDE);
-                    notifyDataEnabledOverrideChanged(allow, TelephonyManager
-                            .MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL);
-                }
+            case EVENT_SET_MOBILE_DATA_POLICY: {
+                int mobileDataPolicy = msg.arg1;
+                boolean enable = msg.arg2 == 1;
+                onSetMobileDataPolicy(mobileDataPolicy, enable);
                 break;
             }
             case EVENT_PROVISIONED_CHANGED:
@@ -399,9 +385,9 @@
             return isProvisioningDataEnabled();
         } else {
             boolean userDataEnabled = isUserDataEnabled();
-            // Check if we should temporarily enable data in certain conditions.
-            boolean isDataEnabledOverridden = mDataEnabledOverride
-                    .shouldOverrideDataEnabledSettings(mPhone, apnType);
+            // Check if we should temporarily enable data based on mobile data policy.
+            boolean isDataEnabledOverridden = isDataEnabledOverriddenForApn(apnType);
+
 
             return ((userDataEnabled || isDataEnabledOverridden)
                     && mDataEnabledSettings.get(TelephonyManager.DATA_ENABLED_REASON_POLICY)
@@ -410,9 +396,16 @@
         }
     }
 
-    private static boolean isStandAloneOpportunistic(int subId, Context context) {
+    private boolean isStandAloneOpportunistic(int subId) {
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
+                    .getSubscriptionInfoInternal(subId);
+            return subInfo != null && subInfo.isOpportunistic()
+                    && TextUtils.isEmpty(subInfo.getGroupUuid());
+        }
         SubscriptionInfo info = SubscriptionController.getInstance().getActiveSubscriptionInfo(
-                subId, context.getOpPackageName(), context.getAttributionTag());
+                subId, mPhone.getContext().getOpPackageName(),
+                mPhone.getContext().getAttributionTag());
         return (info != null) && info.isOpportunistic() && info.getGroupUuid() == null;
     }
 
@@ -423,7 +416,7 @@
      */
     private void setUserDataEnabled(boolean enabled, String callingPackage) {
         // Can't disable data for stand alone opportunistic subscription.
-        if (isStandAloneOpportunistic(mSubId, mPhone.getContext()) && !enabled) return;
+        if (isStandAloneOpportunistic(mSubId) && !enabled) return;
         boolean changed = GlobalSettingsHelper.setInt(mPhone.getContext(),
                 Settings.Global.MOBILE_DATA, mSubId, (enabled ? 1 : 0));
         log("Set user data enabled to " + enabled + ", changed=" + changed + ", callingPackage="
@@ -445,7 +438,7 @@
         }
 
         // User data should always be true for opportunistic subscription.
-        if (isStandAloneOpportunistic(mSubId, mPhone.getContext())) return true;
+        if (isStandAloneOpportunistic(mSubId)) return true;
 
         boolean defaultVal = TelephonyProperties.mobile_data().orElse(true);
 
@@ -590,44 +583,86 @@
         sp.putBoolean(key, true).commit();
     }
 
-    private @NonNull DataEnabledOverride getDataEnabledOverride() {
-        return new DataEnabledOverride(SubscriptionController.getInstance()
-                .getDataEnabledOverrideRules(mSubId));
+    /** Refresh the enabled mobile data policies from Telephony database */
+    private void refreshEnabledMobileDataPolicy() {
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
+                    .getSubscriptionInfoInternal(mSubId);
+            if (subInfo != null) {
+                mEnabledMobileDataPolicy = getMobileDataPolicyEnabled(
+                        subInfo.getEnabledMobileDataPolicies());
+            }
+        } else {
+            mEnabledMobileDataPolicy = getMobileDataPolicyEnabled(SubscriptionController
+                    .getInstance().getEnabledMobileDataPolicies(mSubId));
+        }
     }
 
     /**
-     * Set whether to always allow the MMS data connection.
-     * @param alwaysAllow {@code true} if MMS data is always allowed and {@code false} otherwise.
+     * @return {@code true} If the mobile data policy is enabled
      */
-    public void setAlwaysAllowMmsData(boolean alwaysAllow) {
-        obtainMessage(EVENT_SET_ALWAYS_ALLOW_MMS_DATA, alwaysAllow).sendToTarget();
+    public boolean isMobileDataPolicyEnabled(@MobileDataPolicy int mobileDataPolicy) {
+        return mEnabledMobileDataPolicy.contains(mobileDataPolicy);
     }
 
     /**
-     * Check whether MMS is always allowed.
-     * @return {@code true} if MMS is always allowed and {@code false} otherwise.
+     * Set mobile data policy enabled status
+     * @param mobileDataPolicy The mobile data policy to set
+     * @param enable {@code true} to enable the policy; {@code false} to disable.
      */
-    public boolean isMmsAlwaysAllowed() {
-        return mDataEnabledOverride.isMmsAlwaysAllowed();
+    public void setMobileDataPolicy(@MobileDataPolicy int mobileDataPolicy, boolean enable) {
+        obtainMessage(EVENT_SET_MOBILE_DATA_POLICY, mobileDataPolicy, enable ? 1 : 0)
+                .sendToTarget();
     }
 
     /**
-     * Set whether to allow mobile data during voice call. This is used for allowing data on the
-     * non-default data SIM. When a voice call is placed on the non-default data SIM on DSDS
-     * devices, users will not be able to use mobile data. By calling this API, data will be
-     * temporarily enabled on the non-default data SIM during the life cycle of the voice call.
-     * @param allow {@code true} if data is allowed during a voice call and {@code false} otherwise.
+     * Store data mobile policy to Telephony database.
+     *
+     * @param mobileDataPolicy The mobile data policy that overrides user data enabled setting.
+     * @param enable {@code true} to enable the policy; {@code false} to remove the policy.
      */
-    public void setAllowDataDuringVoiceCall(boolean allow) {
-        obtainMessage(EVENT_SET_ALLOW_DATA_DURING_VOICE_CALL, allow).sendToTarget();
+    private void onSetMobileDataPolicy(@MobileDataPolicy int mobileDataPolicy, boolean enable) {
+        if (enable == isMobileDataPolicyEnabled(mobileDataPolicy)) {
+            return;
+        }
+        metricsRecordSetMobileDataPolicy(mobileDataPolicy);
+
+        if (enable) {
+            mEnabledMobileDataPolicy.add(mobileDataPolicy);
+        } else {
+            mEnabledMobileDataPolicy.remove(mobileDataPolicy);
+        }
+
+        String enabledMobileDataPolicies = mEnabledMobileDataPolicy.stream().map(String::valueOf)
+                .collect(Collectors.joining(","));
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionManagerService.getInstance().setEnabledMobileDataPolicies(mSubId,
+                    enabledMobileDataPolicies);
+            logl(TelephonyUtils.mobileDataPolicyToString(mobileDataPolicy) + " changed to "
+                    + enable);
+            updateDataEnabledAndNotify(TelephonyManager.DATA_ENABLED_REASON_OVERRIDE);
+            notifyDataEnabledOverrideChanged(enable, mobileDataPolicy);
+        } else {
+            if (SubscriptionController.getInstance().setEnabledMobileDataPolicies(
+                    mSubId, enabledMobileDataPolicies)) {
+                logl(TelephonyUtils.mobileDataPolicyToString(mobileDataPolicy) + " changed to "
+                        + enable);
+                updateDataEnabledAndNotify(TelephonyManager.DATA_ENABLED_REASON_OVERRIDE);
+                notifyDataEnabledOverrideChanged(enable, mobileDataPolicy);
+            } else {
+                loge("onSetMobileDataPolicy: failed to set " + enabledMobileDataPolicies);
+            }
+        }
     }
 
     /**
-     * Check whether data is allowed during a voice call.
-     * @return {@code true} if data is allowed during voice call and {@code false} otherwise.
+     * Record the number of times a mobile data policy is toggled to metrics.
+     * @param mobileDataPolicy The mobile data policy that's toggled
      */
-    public boolean isDataAllowedInVoiceCall() {
-        return mDataEnabledOverride.isDataAllowedInVoiceCall();
+    private void metricsRecordSetMobileDataPolicy(@MobileDataPolicy int mobileDataPolicy) {
+        if (mobileDataPolicy == TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH) {
+            DeviceTelephonyPropertiesStats.recordAutoDataSwitchFeatureToggle();
+        }
     }
 
     /**
@@ -656,6 +691,98 @@
     }
 
     /**
+     * Return the parsed mobile data policies.
+     *
+     * @param policies New mobile data policies in String format.
+     * @return A Set of parsed mobile data policies.
+     */
+    public @NonNull @MobileDataPolicy Set<Integer> getMobileDataPolicyEnabled(
+            @NonNull String policies) {
+        Set<Integer> mobileDataPolicies = new HashSet<>();
+        String[] rulesString = policies.trim().split("\\s*,\\s*");
+        for (String rule : rulesString) {
+            if (!TextUtils.isEmpty(rule)) {
+                int parsedDataPolicy = parsePolicyFrom(rule);
+                if (parsedDataPolicy != INVALID_MOBILE_DATA_POLICY) {
+                    mobileDataPolicies.add(parsedDataPolicy);
+                }
+            }
+        }
+        return mobileDataPolicies;
+    }
+
+    /**
+     * Parse a mobile data policy retrieved from Telephony db.
+     * If the policy is in legacy format, convert it into the corresponding mobile data policy.
+     *
+     * @param policy Mobile data policy to be parsed from.
+     * @return Parsed mobile data policy. {@link #INVALID_MOBILE_DATA_POLICY} if string can't be
+     * parsed into a mobile data policy.
+     */
+    private @MobileDataPolicy int parsePolicyFrom(@NonNull String policy) {
+        int dataPolicy;
+        try {
+            // parse as new override policy
+            dataPolicy = Integer.parseInt(policy);
+        } catch (NumberFormatException e) {
+            dataPolicy = INVALID_MOBILE_DATA_POLICY;
+            loge("parsePolicyFrom: invalid mobile data policy format: "  + policy);
+        }
+        return dataPolicy;
+    }
+
+    /**
+     * Check if data enabled is temporarily overridden in certain conditions.
+     *
+     * @param apnType The APN type to check.
+     * @return {@code true} if data enabled should be overridden.
+     */
+    private boolean isDataEnabledOverriddenForApn(@ApnType int apnType) {
+        boolean overridden = false;
+
+        // mobile data policy : MMS always allowed
+        if (isMobileDataPolicyEnabled(TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED)) {
+            overridden = apnType == ApnSetting.TYPE_MMS;
+        }
+
+        boolean isNonDds;
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            isNonDds = mPhone.getSubId() != SubscriptionManagerService.getInstance()
+                    .getDefaultDataSubId();
+        } else {
+            isNonDds = mPhone.getSubId() != SubscriptionController.getInstance()
+                    .getDefaultDataSubId();
+        }
+
+        // mobile data policy : data during call
+        if (isMobileDataPolicyEnabled(TelephonyManager
+                .MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL)) {
+            overridden = isNonDds && mPhone.getState() != PhoneConstants.State.IDLE;
+        }
+
+        // mobile data policy : auto data switch
+        if (isMobileDataPolicyEnabled(TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH)) {
+            Phone defaultDataPhone;
+            if (mPhone.isSubscriptionManagerServiceEnabled()) {
+                // check user enabled data on the default data phone
+                defaultDataPhone = PhoneFactory.getPhone(SubscriptionManagerService.getInstance()
+                        .getPhoneId(SubscriptionManagerService.getInstance()
+                                .getDefaultDataSubId()));
+            } else {
+                // check user enabled data on the default data phone
+                defaultDataPhone = PhoneFactory.getPhone(SubscriptionController.getInstance()
+                        .getPhoneId(SubscriptionController.getInstance().getDefaultDataSubId()));
+            }
+            if (defaultDataPhone == null) {
+                loge("isDataEnabledOverriddenForApn: unexpected defaultDataPhone is null");
+            } else {
+                overridden = isNonDds && defaultDataPhone.isUserDataEnabled();
+            }
+        }
+        return overridden;
+    }
+
+    /**
      * Register the callback for receiving information from {@link DataSettingsManager}.
      *
      * @param callback The callback.
@@ -697,7 +824,8 @@
                 + ", isProvisioningDataEnabled=" + isProvisioningDataEnabled()
                 + ", mIsDataEnabled=" + mIsDataEnabled
                 + ", mDataEnabledSettings=" + mDataEnabledSettings
-                + ", mDataEnabledOverride=" + mDataEnabledOverride
+                + ", mEnabledMobileDataPolicy=" + mEnabledMobileDataPolicy.stream()
+                .map(TelephonyUtils::mobileDataPolicyToString).collect(Collectors.joining(","))
                 + "]";
     }
 
@@ -753,7 +881,8 @@
                 .map(entry ->
                         dataEnabledChangedReasonToString(entry.getKey()) + "=" + entry.getValue())
                 .collect(Collectors.joining(", ")));
-        pw.println("mDataEnabledOverride=" + mDataEnabledOverride);
+        pw.println("mEnabledMobileDataPolicy=" + mEnabledMobileDataPolicy.stream()
+                .map(TelephonyUtils::mobileDataPolicyToString).collect(Collectors.joining(",")));
         pw.println("Local logs:");
         pw.increaseIndent();
         mLocalLog.dump(fd, pw, args);
diff --git a/src/java/com/android/internal/telephony/data/DataStallRecoveryManager.java b/src/java/com/android/internal/telephony/data/DataStallRecoveryManager.java
index e467815..6632c67 100644
--- a/src/java/com/android/internal/telephony/data/DataStallRecoveryManager.java
+++ b/src/java/com/android/internal/telephony/data/DataStallRecoveryManager.java
@@ -146,11 +146,13 @@
     private final @NonNull DataServiceManager mWwanDataServiceManager;
 
     /** The data stall recovery action. */
-    private @RecoveryAction int mRecovryAction;
+    private @RecoveryAction int mRecoveryAction;
     /** The elapsed real time of last recovery attempted */
     private @ElapsedRealtimeLong long mTimeLastRecoveryStartMs;
     /** Whether current network is good or not */
     private boolean mIsValidNetwork;
+    /** Whether data stall recovery is triggered or not */
+    private boolean mRecoveryTriggered = false;
     /** Whether data stall happened or not. */
     private boolean mDataStalled;
     /** Whether the result of last action(RADIO_RESTART) reported. */
@@ -352,6 +354,7 @@
      */
     private void reset() {
         mIsValidNetwork = true;
+        mRecoveryTriggered = false;
         mIsAttemptedAllSteps = false;
         mRadioStateChangedDuringDataStall = false;
         mIsAirPlaneModeEnableDuringDataStall = false;
@@ -359,7 +362,7 @@
         cancelNetworkCheckTimer();
         mTimeLastRecoveryStartMs = 0;
         mLastAction = RECOVERY_ACTION_GET_DATA_CALL_LIST;
-        mRecovryAction = RECOVERY_ACTION_GET_DATA_CALL_LIST;
+        mRecoveryAction = RECOVERY_ACTION_GET_DATA_CALL_LIST;
     }
 
     /**
@@ -373,15 +376,12 @@
         setNetworkValidationState(isValid);
         if (isValid) {
             reset();
-        } else {
-            if (mIsValidNetwork || isRecoveryAlreadyStarted()) {
-                mIsValidNetwork = false;
-                if (isRecoveryNeeded(true)) {
-                    log("trigger data stall recovery");
-                    mTimeLastRecoveryStartMs = SystemClock.elapsedRealtime();
-                    sendMessage(obtainMessage(EVENT_DO_RECOVERY));
-                }
-            }
+        } else if (isRecoveryNeeded(true)) {
+            // Set the network as invalid, because recovery is needed
+            mIsValidNetwork = false;
+            log("trigger data stall recovery");
+            mTimeLastRecoveryStartMs = SystemClock.elapsedRealtime();
+            sendMessage(obtainMessage(EVENT_DO_RECOVERY));
         }
     }
 
@@ -402,8 +402,8 @@
     @VisibleForTesting
     @RecoveryAction
     public int getRecoveryAction() {
-        log("getRecoveryAction: " + recoveryActionToString(mRecovryAction));
-        return mRecovryAction;
+        log("getRecoveryAction: " + recoveryActionToString(mRecoveryAction));
+        return mRecoveryAction;
     }
 
     /**
@@ -413,24 +413,24 @@
      */
     @VisibleForTesting
     public void setRecoveryAction(@RecoveryAction int action) {
-        mRecovryAction = action;
+        mRecoveryAction = action;
 
         // Check if the mobile data enabled is TRUE, it means that the mobile data setting changed
         // from DISABLED to ENABLED, we will set the next recovery action to
         // RECOVERY_ACTION_RADIO_RESTART due to already did the RECOVERY_ACTION_CLEANUP.
         if (mMobileDataChangedToEnabledDuringDataStall
-                && mRecovryAction < RECOVERY_ACTION_RADIO_RESTART) {
-            mRecovryAction = RECOVERY_ACTION_RADIO_RESTART;
+                && mRecoveryAction < RECOVERY_ACTION_RADIO_RESTART) {
+            mRecoveryAction = RECOVERY_ACTION_RADIO_RESTART;
         }
         // Check if the radio state changed from off to on, it means that the modem already
         // did the radio restart, we will set the next action to RECOVERY_ACTION_RESET_MODEM.
         if (mRadioStateChangedDuringDataStall
                 && mRadioPowerState == TelephonyManager.RADIO_POWER_ON) {
-            mRecovryAction = RECOVERY_ACTION_RESET_MODEM;
+            mRecoveryAction = RECOVERY_ACTION_RESET_MODEM;
         }
         // To check the flag from DataConfigManager if we need to skip the step.
-        if (shouldSkipRecoveryAction(mRecovryAction)) {
-            switch (mRecovryAction) {
+        if (shouldSkipRecoveryAction(mRecoveryAction)) {
+            switch (mRecoveryAction) {
                 case RECOVERY_ACTION_GET_DATA_CALL_LIST:
                     setRecoveryAction(RECOVERY_ACTION_CLEANUP);
                     break;
@@ -446,7 +446,7 @@
             }
         }
 
-        log("setRecoveryAction: " + recoveryActionToString(mRecovryAction));
+        log("setRecoveryAction: " + recoveryActionToString(mRecoveryAction));
     }
 
     /**
@@ -455,7 +455,7 @@
      * @return {@code true} if recovery already started, {@code false} recovery not started.
      */
     private boolean isRecoveryAlreadyStarted() {
-        return getRecoveryAction() != RECOVERY_ACTION_GET_DATA_CALL_LIST;
+        return getRecoveryAction() != RECOVERY_ACTION_GET_DATA_CALL_LIST || mRecoveryTriggered;
     }
 
     /**
@@ -542,6 +542,12 @@
     private boolean isRecoveryNeeded(boolean isNeedToCheckTimer) {
         logv("enter: isRecoveryNeeded()");
 
+        // Skip if network is invalid and recovery was not started yet
+        if (!mIsValidNetwork && !isRecoveryAlreadyStarted()) {
+            logl("skip when network still remains invalid and recovery was not started yet");
+            return false;
+        }
+
         // Skip recovery if we have already attempted all steps.
         if (mIsAttemptedAllSteps) {
             logl("skip retrying continue recovery action");
@@ -577,7 +583,6 @@
             logl("skip data stall recovery as data not connected");
             return false;
         }
-
         return true;
     }
 
@@ -587,39 +592,55 @@
      * @param isValid true for validation passed & false for validation failed
      */
     private void setNetworkValidationState(boolean isValid) {
+        boolean isLogNeeded = false;
+        int timeDuration = 0;
+        boolean isFirstDataStall = false;
+        boolean isFirstValidationAfterDoRecovery = false;
+        @RecoveredReason int reason = getRecoveredReason(isValid);
         // Validation status is true and was not data stall.
         if (isValid && !mDataStalled) {
             return;
         }
 
         if (!mDataStalled) {
+            // First data stall
+            isLogNeeded = true;
             mDataStalled = true;
-            mDataStallStartMs = SystemClock.elapsedRealtime();
-            logl("data stall: start time = " + DataUtils.elapsedTimeToString(mDataStallStartMs));
-            return;
-        }
-
-        if (!mLastActionReported) {
-            @RecoveredReason int reason = getRecoveredReason(isValid);
-            int timeDuration = (int) (SystemClock.elapsedRealtime() - mDataStallStartMs);
-            logl(
-                    "data stall: lastaction = "
-                            + recoveryActionToString(mLastAction)
-                            + ", isRecovered = "
-                            + isValid
-                            + ", reason = "
-                            + recoveredReasonToString(reason)
-                            + ", TimeDuration = "
-                            + timeDuration);
-            DataStallRecoveryStats.onDataStallEvent(
-                    mLastAction, mPhone, isValid, timeDuration, reason);
+            isFirstDataStall = true;
+        } else if (!mLastActionReported) {
+            // When the first validation status appears, enter this block.
+            isLogNeeded = true;
+            timeDuration = (int) (SystemClock.elapsedRealtime() - mDataStallStartMs);
             mLastActionReported = true;
+            isFirstValidationAfterDoRecovery = true;
         }
 
         if (isValid) {
+            // When the validation passed(mobile data resume), enter this block.
+            isLogNeeded = true;
+            timeDuration = (int) (SystemClock.elapsedRealtime() - mDataStallStartMs);
             mLastActionReported = false;
             mDataStalled = false;
         }
+
+        if (isLogNeeded) {
+            DataStallRecoveryStats.onDataStallEvent(
+                    mLastAction, mPhone, isValid, timeDuration, reason,
+                    isFirstValidationAfterDoRecovery);
+            logl(
+                    "data stall: "
+                    + (isFirstDataStall == true ? "start" : isValid == false ? "in process" : "end")
+                    + ", lastaction="
+                    + recoveryActionToString(mLastAction)
+                    + ", isRecovered="
+                    + isValid
+                    + ", reason="
+                    + recoveredReasonToString(reason)
+                    + ", isFirstValidationAfterDoRecovery="
+                    + isFirstValidationAfterDoRecovery
+                    + ", TimeDuration="
+                    + timeDuration);
+        }
     }
 
     /**
@@ -652,6 +673,7 @@
     private void doRecovery() {
         @RecoveryAction final int recoveryAction = getRecoveryAction();
         final int signalStrength = mPhone.getSignalStrength().getLevel();
+        mRecoveryTriggered = true;
 
         // DSRM used sendMessageDelayed to process the next event EVENT_DO_RECOVERY, so it need
         // to check the condition if DSRM need to process the recovery action.
diff --git a/src/java/com/android/internal/telephony/data/DataUtils.java b/src/java/com/android/internal/telephony/data/DataUtils.java
index 897f412..fc0c4f1 100644
--- a/src/java/com/android/internal/telephony/data/DataUtils.java
+++ b/src/java/com/android/internal/telephony/data/DataUtils.java
@@ -41,6 +41,7 @@
 import android.util.ArrayMap;
 
 import com.android.internal.telephony.data.DataNetworkController.NetworkRequestList;
+import com.android.telephony.Rlog;
 
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -61,6 +62,7 @@
     /** The time format for converting time to readable string. */
     private static final SimpleDateFormat TIME_FORMAT =
             new SimpleDateFormat("HH:mm:ss.SSS", Locale.US);
+    private static final String TAG = "DataUtils";
 
     /**
      * Get the network capability from the string.
@@ -164,6 +166,7 @@
             case NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH:
                 return "PRIORITIZE_BANDWIDTH";
             default:
+                loge("Unknown network capability(" + netCap + ")");
                 return "Unknown(" + netCap + ")";
         }
     }
@@ -209,7 +212,9 @@
         switch (status) {
             case NetworkAgent.VALIDATION_STATUS_VALID: return "VALID";
             case NetworkAgent.VALIDATION_STATUS_NOT_VALID: return "INVALID";
-            default: return "UNKNOWN(" + status + ")";
+            default:
+                loge("Unknown validation status(" + status + ")");
+                return "UNKNOWN(" + status + ")";
         }
     }
 
@@ -366,6 +371,7 @@
             case ImsFeature.FEATURE_MMTEL: return "MMTEL";
             case ImsFeature.FEATURE_RCS: return "RCS";
             default:
+                loge("Unknown IMS feature(" + imsFeature + ")");
                 return "Unknown(" + imsFeature + ")";
         }
     }
@@ -468,7 +474,9 @@
             case DataCallResponse.LINK_STATUS_INACTIVE: return "INACTIVE";
             case DataCallResponse.LINK_STATUS_ACTIVE: return "ACTIVE";
             case DataCallResponse.LINK_STATUS_DORMANT: return "DORMANT";
-            default: return "UNKNOWN(" + linkStatus + ")";
+            default:
+                loge("Unknown link status(" + linkStatus + ")");
+                return "UNKNOWN(" + linkStatus + ")";
         }
     }
 
@@ -505,7 +513,13 @@
             case TelephonyManager.DATA_ACTIVITY_OUT: return "OUT";
             case TelephonyManager.DATA_ACTIVITY_INOUT: return "INOUT";
             case TelephonyManager.DATA_ACTIVITY_DORMANT: return "DORMANT";
-            default: return "UNKNOWN(" + dataActivity + ")";
+            default:
+                loge("Unknown data activity(" + dataActivity + ")");
+                return "UNKNOWN(" + dataActivity + ")";
         }
     }
+
+    private static void loge(String msg) {
+        Rlog.e(TAG, msg);
+    }
 }
diff --git a/src/java/com/android/internal/telephony/data/PhoneSwitcher.java b/src/java/com/android/internal/telephony/data/PhoneSwitcher.java
index ee8d157..fe63ada 100644
--- a/src/java/com/android/internal/telephony/data/PhoneSwitcher.java
+++ b/src/java/com/android/internal/telephony/data/PhoneSwitcher.java
@@ -32,6 +32,9 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
 import android.compat.annotation.UnsupportedAppUsage;
 import android.content.BroadcastReceiver;
 import android.content.Context;
@@ -47,6 +50,7 @@
 import android.net.TelephonyNetworkSpecifier;
 import android.os.AsyncResult;
 import android.os.Build;
+import android.os.Bundle;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
@@ -54,22 +58,23 @@
 import android.os.Registrant;
 import android.os.RegistrantList;
 import android.os.RemoteException;
-import android.provider.DeviceConfig;
+import android.provider.Settings;
+import android.telephony.AccessNetworkConstants;
 import android.telephony.CarrierConfigManager;
+import android.telephony.NetworkRegistrationInfo;
 import android.telephony.PhoneCapability;
 import android.telephony.PhoneStateListener;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.telephony.TelephonyRegistryManager;
-import android.telephony.data.ApnSetting;
 import android.telephony.ims.ImsReasonInfo;
 import android.telephony.ims.ImsRegistrationAttributes;
 import android.telephony.ims.RegistrationManager;
 import android.telephony.ims.stub.ImsRegistrationImplBase;
-import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.LocalLog;
+import android.util.Log;
 
 import com.android.ims.ImsException;
 import com.android.ims.ImsManager;
@@ -90,6 +95,9 @@
 import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent;
 import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.DataSwitch;
 import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.OnDemandDataSwitch;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
+import com.android.internal.telephony.util.NotificationChannelController;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.telephony.Rlog;
 
@@ -112,12 +120,23 @@
  */
 public class PhoneSwitcher extends Handler {
     private static final String LOG_TAG = "PhoneSwitcher";
-    /** DeviceConfig key of the time threshold in ms for defining a network status to be stable. **/
-    private static final String KEY_AUTO_DATA_SWITCH_AVAILABILITY_STABILITY_TIME_THRESHOLD =
-            "auto_data_switch_availability_stability_time_threshold";
-    protected static final boolean VDBG = false;
+    protected static final boolean VDBG = Rlog.isLoggable(LOG_TAG, Log.VERBOSE);
 
-    private static final int DEFAULT_NETWORK_CHANGE_TIMEOUT_MS = 5000;
+    /** Fragment "key" argument passed thru {@link #SETTINGS_EXTRA_SHOW_FRAGMENT_ARGUMENTS} */
+    private static final String SETTINGS_EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
+    /**
+     * When starting this activity, this extra can also be specified to supply a Bundle of arguments
+     * to pass to that fragment when it is instantiated during the initial creation of the activity.
+     */
+    private static final String SETTINGS_EXTRA_SHOW_FRAGMENT_ARGUMENTS =
+            ":settings:show_fragment_args";
+    /** The res Id of the auto data switch fragment in settings. **/
+    private static final String AUTO_DATA_SWITCH_SETTING_R_ID = "auto_data_switch";
+    /** Notification tag **/
+    private static final String AUTO_DATA_SWITCH_NOTIFICATION_TAG = "auto_data_switch";
+    /** Notification ID **/
+    private static final int AUTO_DATA_SWITCH_NOTIFICATION_ID = 1;
+
     private static final int MODEM_COMMAND_RETRY_PERIOD_MS     = 5000;
     // After the emergency call ends, wait for a few seconds to see if we enter ECBM before starting
     // the countdown to remove the emergency DDS override.
@@ -188,6 +207,7 @@
     private final @NonNull NetworkRequestList mNetworkRequestList = new NetworkRequestList();
     protected final RegistrantList mActivePhoneRegistrants;
     protected final SubscriptionController mSubscriptionController;
+    private final SubscriptionManagerService mSubscriptionManagerService;
     protected final Context mContext;
     private final LocalLog mLocalLog;
     protected PhoneState[] mPhoneStates;
@@ -196,7 +216,10 @@
     @VisibleForTesting
     protected final CellularNetworkValidator mValidator;
     private int mPendingSwitchSubId = INVALID_SUBSCRIPTION_ID;
-    private int mLastAutoSelectedSwitchReason = -1;
+    /** The reason for the last time changing preferred data sub **/
+    private int mLastSwitchPreferredDataReason = -1;
+    /** {@code true} if we've displayed the notification the first time auto switch occurs **/
+    private boolean mDisplayedAutoSwitchNotification = false;
     private boolean mPendingSwitchNeedValidation;
     @VisibleForTesting
     public final CellularNetworkValidator.ValidationCallback mValidationCallback =
@@ -236,6 +259,9 @@
     // its value will be DEFAULT_SUBSCRIPTION_ID.
     private int mAutoSelectedDataSubId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
 
+    /** The count of consecutive auto switch validation failure **/
+    private int mAutoSwitchRetryFailedCount = 0;
+
     // The phone ID that has an active voice call. If set, and its mobile data setting is on,
     // it will become the mPreferredDataPhoneId.
     protected int mPhoneIdInVoiceCall = SubscriptionManager.INVALID_PHONE_INDEX;
@@ -266,6 +292,16 @@
 
     private ISetOpportunisticDataCallback mSetOpptSubCallback;
 
+    /** Data config manager callback for updating device config. **/
+    private final DataConfigManager.DataConfigManagerCallback mDataConfigManagerCallback =
+            new DataConfigManager.DataConfigManagerCallback(this::post) {
+        @Override
+        public void onDeviceConfigChanged() {
+            log("onDeviceConfigChanged");
+            PhoneSwitcher.this.updateConfig();
+        }
+    };
+
     private static final int EVENT_PRIMARY_DATA_SUB_CHANGED       = 101;
     protected static final int EVENT_SUBSCRIPTION_CHANGED           = 102;
     private static final int EVENT_REQUEST_NETWORK                = 103;
@@ -282,14 +318,12 @@
     // mEmergencyOverride, start the countdown to remove the override using the message
     // EVENT_REMOVE_DDS_EMERGENCY_OVERRIDE. The only exception to this is if the device moves to
     // ECBM, which is detected by EVENT_EMERGENCY_TOGGLE.
-    @VisibleForTesting
-    public static final int EVENT_PRECISE_CALL_STATE_CHANGED      = 109;
+    private static final int EVENT_PRECISE_CALL_STATE_CHANGED     = 109;
     private static final int EVENT_NETWORK_VALIDATION_DONE        = 110;
-    private static final int EVENT_REMOVE_DEFAULT_NETWORK_CHANGE_CALLBACK = 111;
+    private static final int EVENT_EVALUATE_AUTO_SWITCH           = 111;
     private static final int EVENT_MODEM_COMMAND_DONE             = 112;
     private static final int EVENT_MODEM_COMMAND_RETRY            = 113;
-    @VisibleForTesting
-    public static final int EVENT_DATA_ENABLED_CHANGED            = 114;
+    private static final int EVENT_SERVICE_STATE_CHANGED          = 114;
     // An emergency call is about to be originated and requires the DDS to be overridden.
     // Uses EVENT_PRECISE_CALL_STATE_CHANGED message to start countdown to finish override defined
     // in mEmergencyOverride. If EVENT_PRECISE_CALL_STATE_CHANGED does not come in
@@ -298,13 +332,11 @@
     // If it exists, remove the current mEmergencyOverride DDS override.
     private static final int EVENT_REMOVE_DDS_EMERGENCY_OVERRIDE  = 116;
     // If it exists, remove the current mEmergencyOverride DDS override.
-    @VisibleForTesting
-    public static final int EVENT_MULTI_SIM_CONFIG_CHANGED        = 117;
+    private static final int EVENT_MULTI_SIM_CONFIG_CHANGED       = 117;
     private static final int EVENT_NETWORK_AVAILABLE              = 118;
     private static final int EVENT_PROCESS_SIM_STATE_CHANGE       = 119;
-    @VisibleForTesting
-    public static final int EVENT_IMS_RADIO_TECH_CHANGED          = 120;
-    public static final int EVENT_DEVICE_CONFIG_CHANGED           = 121;
+    private static final int EVENT_IMS_RADIO_TECH_CHANGED         = 120;
+    private static final int EVENT_MEETS_AUTO_DATA_SWITCH_STATE   = 121;
 
     // List of events triggers re-evaluations
     private static final String EVALUATION_REASON_RADIO_ON = "EVENT_RADIO_ON";
@@ -324,8 +356,6 @@
     // Default timeout value of network validation in millisecond.
     private final static int DEFAULT_VALIDATION_EXPIRATION_TIME = 2000;
 
-    private Boolean mHasRegisteredDefaultNetworkChangeCallback = false;
-
     private ConnectivityManager mConnectivityManager;
     private int mImsRegistrationTech = REGISTRATION_TECH_NONE;
 
@@ -338,6 +368,12 @@
      */
     private long mAutoDataSwitchAvailabilityStabilityTimeThreshold = -1;
 
+    /**
+     * The maximum number of retries when a validation for switching failed.
+     */
+    private int mAutoDataSwitchValidationMaxRetry =
+            DataConfigManager.DEFAULT_AUTO_DATA_SWITCH_MAX_RETRY;
+
     /** Data settings manager callback. Key is the phone id. */
     private final @NonNull Map<Integer, DataSettingsManagerCallback> mDataSettingsManagerCallbacks =
             new ArrayMap<>();
@@ -345,18 +381,37 @@
     private class DefaultNetworkCallback extends ConnectivityManager.NetworkCallback {
         public int mExpectedSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
         public int mSwitchReason = TelephonyEvent.DataSwitch.Reason.DATA_SWITCH_REASON_UNKNOWN;
+        public boolean isDefaultNetworkOnCellular = false;
         @Override
         public void onCapabilitiesChanged(Network network,
                 NetworkCapabilities networkCapabilities) {
-            if (networkCapabilities.hasTransport(TRANSPORT_CELLULAR)
-                    && SubscriptionManager.isValidSubscriptionId(mExpectedSubId)
-                    && mExpectedSubId == getSubIdFromNetworkSpecifier(
-                            networkCapabilities.getNetworkSpecifier())) {
-                logDataSwitchEvent(
-                        mExpectedSubId,
-                        TelephonyEvent.EventState.EVENT_STATE_END,
-                        mSwitchReason);
-                removeDefaultNetworkChangeCallback();
+            if (networkCapabilities.hasTransport(TRANSPORT_CELLULAR)) {
+                isDefaultNetworkOnCellular = true;
+                if (SubscriptionManager.isValidSubscriptionId(mExpectedSubId)
+                        && mExpectedSubId == getSubIdFromNetworkSpecifier(
+                        networkCapabilities.getNetworkSpecifier())) {
+                    logDataSwitchEvent(
+                            mExpectedSubId,
+                            TelephonyEvent.EventState.EVENT_STATE_END,
+                            mSwitchReason);
+                    mExpectedSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+                    mSwitchReason = TelephonyEvent.DataSwitch.Reason.DATA_SWITCH_REASON_UNKNOWN;
+                }
+            } else {
+                if (isDefaultNetworkOnCellular) {
+                    // non-cellular transport is active
+                    isDefaultNetworkOnCellular = false;
+                    log("default network is active on non cellular");
+                    evaluateIfAutoSwitchIsNeeded();
+                }
+            }
+        }
+
+        @Override
+        public void onLost(Network network) {
+            // try find an active sub to switch to
+            if (!hasMessages(EVENT_EVALUATE_AUTO_SWITCH)) {
+                sendEmptyMessage(EVENT_EVALUATE_AUTO_SWITCH);
             }
         }
     }
@@ -415,43 +470,21 @@
         return sPhoneSwitcher;
     }
 
-    /**
-     * Whether this phone IMS registration is on its original network. This result impacts
-     * whether we want to do DDS switch to the phone having voice call.
-     * If it's registered on IWLAN or cross SIM in multi-SIM case, return false. Otherwise,
-     * return true.
-     */
-    private boolean isImsOnOriginalNetwork(Phone phone) {
-        if (phone == null) return false;
-        int phoneId = phone.getPhoneId();
-        if (!SubscriptionManager.isValidPhoneId(phoneId)) return false;
-
-        int imsRegTech = mImsRegTechProvider.get(mContext, phoneId);
-        // If IMS is registered on IWLAN or cross SIM, return false.
-        boolean isOnOriginalNetwork = (imsRegTech != REGISTRATION_TECH_IWLAN)
-                && (imsRegTech != REGISTRATION_TECH_CROSS_SIM);
-        if (!isOnOriginalNetwork) {
-            log("IMS call on IWLAN or cross SIM. Call will be ignored for DDS switch");
-        }
-        return isOnOriginalNetwork;
-    }
-
-    private boolean isPhoneInVoiceCallChanged() {
+    private boolean updatesIfPhoneInVoiceCallChanged() {
         int oldPhoneIdInVoiceCall = mPhoneIdInVoiceCall;
         // If there's no active call, the value will become INVALID_PHONE_INDEX
         // and internet data will be switched back to system selected or user selected
         // subscription.
         mPhoneIdInVoiceCall = SubscriptionManager.INVALID_PHONE_INDEX;
         for (Phone phone : PhoneFactory.getPhones()) {
-            if (isPhoneInVoiceCall(phone) || (isPhoneInVoiceCall(phone.getImsPhone())
-                    && isImsOnOriginalNetwork(phone))) {
+            if (isPhoneInVoiceCall(phone) || isPhoneInVoiceCall(phone.getImsPhone())) {
                 mPhoneIdInVoiceCall = phone.getPhoneId();
                 break;
             }
         }
 
         if (mPhoneIdInVoiceCall != oldPhoneIdInVoiceCall) {
-            log("isPhoneInVoiceCallChanged from phoneId " + oldPhoneIdInVoiceCall
+            logl("isPhoneInVoiceCallChanged from phoneId " + oldPhoneIdInVoiceCall
                     + " to phoneId " + mPhoneIdInVoiceCall);
             return true;
         } else {
@@ -478,13 +511,13 @@
         }
     }
 
-    private void evaluateIfDataSwitchIsNeeded(String reason) {
-        if (onEvaluate(REQUESTS_UNCHANGED, reason)) {
+    private void evaluateIfImmediateDataSwitchIsNeeded(String evaluationReason, int switchReason) {
+        if (onEvaluate(REQUESTS_UNCHANGED, evaluationReason)) {
             logDataSwitchEvent(mPreferredDataSubId.get(),
                     TelephonyEvent.EventState.EVENT_STATE_START,
-                    DataSwitch.Reason.DATA_SWITCH_REASON_IN_CALL);
+                    switchReason);
             registerDefaultNetworkChangeCallback(mPreferredDataSubId.get(),
-                    DataSwitch.Reason.DATA_SWITCH_REASON_IN_CALL);
+                    switchReason);
         }
     }
 
@@ -498,7 +531,14 @@
         mMaxDataAttachModemCount = maxActivePhones;
         mLocalLog = new LocalLog(MAX_LOCAL_LOG_LINES);
 
-        mSubscriptionController = SubscriptionController.getInstance();
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            mSubscriptionManagerService = SubscriptionManagerService.getInstance();
+            mSubscriptionController = null;
+        } else {
+            mSubscriptionController = SubscriptionController.getInstance();
+            mSubscriptionManagerService = null;
+        }
+
         mRadioConfig = RadioConfig.getInstance();
         mValidator = CellularNetworkValidator.getInstance();
 
@@ -508,9 +548,9 @@
         mContext.registerReceiver(mSimStateIntentReceiver, filter);
 
         mActivePhoneRegistrants = new RegistrantList();
-        for (int i = 0; i < mActiveModemCount; i++) {
-            mPhoneStates[i] = new PhoneState();
-            Phone phone = PhoneFactory.getPhone(i);
+        for (int phoneId = 0; phoneId < mActiveModemCount; phoneId++) {
+            mPhoneStates[phoneId] = new PhoneState();
+            Phone phone = PhoneFactory.getPhone(phoneId);
             if (phone != null) {
                 phone.registerForEmergencyCallToggle(
                         this, EVENT_EMERGENCY_TOGGLE, null);
@@ -521,18 +561,19 @@
                     phone.getImsPhone().registerForPreciseCallStateChanged(
                             this, EVENT_PRECISE_CALL_STATE_CHANGED, null);
                 }
-                mDataSettingsManagerCallbacks.computeIfAbsent(phone.getPhoneId(),
+                mDataSettingsManagerCallbacks.computeIfAbsent(phoneId,
                         v -> new DataSettingsManagerCallback(this::post) {
                             @Override
                             public void onDataEnabledChanged(boolean enabled,
                                     @TelephonyManager.DataEnabledChangedReason int reason,
                                     @NonNull String callingPackage) {
-                                evaluateIfDataSwitchIsNeeded("EVENT_DATA_ENABLED_CHANGED");
+                                PhoneSwitcher.this.onDataEnabledChanged();
                             }});
                 phone.getDataSettingsManager().registerCallback(
-                        mDataSettingsManagerCallbacks.get(phone.getPhoneId()));
-
-                registerForImsRadioTechChange(context, i);
+                        mDataSettingsManagerCallbacks.get(phoneId));
+                phone.getServiceStateTracker().registerForServiceStateChanged(this,
+                        EVENT_SERVICE_STATE_CHANGED, phoneId);
+                registerForImsRadioTechChange(context, phoneId);
             }
             Set<CommandException.Error> ddsFailure = new HashSet<CommandException.Error>();
             mCurrentDdsSwitchFailure.add(ddsFailure);
@@ -556,6 +597,8 @@
         PhoneConfigurationManager.registerForMultiSimConfigChange(
                 this, EVENT_MULTI_SIM_CONFIG_CHANGED, null);
 
+        mConnectivityManager.registerDefaultNetworkCallback(mDefaultNetworkCallback, this);
+
         final NetworkCapabilities.Builder builder = new NetworkCapabilities.Builder()
                 .addTransportType(TRANSPORT_CELLULAR)
                 .addCapability(NetworkCapabilities.NET_CAPABILITY_MMS)
@@ -587,20 +630,9 @@
         // we want to see all requests
         networkFactory.registerIgnoringScore();
 
-        // Register for device config update
-        DeviceConfig.addOnPropertiesChangedListener(
-                DeviceConfig.NAMESPACE_TELEPHONY, this::post,
-                properties -> {
-                    if (TextUtils.equals(DeviceConfig.NAMESPACE_TELEPHONY,
-                            properties.getNamespace())) {
-                        sendEmptyMessage(EVENT_DEVICE_CONFIG_CHANGED);
-                    }
-                });
-        updateDeviceConfig();
-
         updateHalCommandToUse();
 
-        log("PhoneSwitcher started");
+        logl("PhoneSwitcher started");
     }
 
     private final BroadcastReceiver mDefaultDataChangedReceiver = new BroadcastReceiver() {
@@ -620,7 +652,7 @@
                         TelephonyManager.SIM_STATE_UNKNOWN);
                 int slotIndex = intent.getIntExtra(SubscriptionManager.EXTRA_SLOT_INDEX,
                         SubscriptionManager.INVALID_SIM_SLOT_INDEX);
-                log("mSimStateIntentReceiver: slotIndex = " + slotIndex + " state = " + state);
+                logl("mSimStateIntentReceiver: slotIndex = " + slotIndex + " state = " + state);
                 obtainMessage(EVENT_PROCESS_SIM_STATE_CHANGE, slotIndex, state).sendToTarget();
             }
         }
@@ -631,14 +663,21 @@
             return false;
         }
 
-        SubscriptionInfo info = SubscriptionController.getInstance()
-                .getActiveSubscriptionInfoForSimSlotIndex(slotIndex,
-                mContext.getOpPackageName(), null);
+        SubscriptionInfo info;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            info = mSubscriptionManagerService
+                    .getActiveSubscriptionInfoForSimSlotIndex(slotIndex,
+                            mContext.getOpPackageName(), mContext.getAttributionTag());
+        } else {
+            info = mSubscriptionController
+                    .getActiveSubscriptionInfoForSimSlotIndex(slotIndex,
+                            mContext.getOpPackageName(), null);
+        }
         boolean uiccAppsEnabled = info != null && info.areUiccApplicationsEnabled();
 
         IccCard iccCard = PhoneFactory.getPhone(slotIndex).getIccCard();
         if (!iccCard.isEmptyProfile() && uiccAppsEnabled) {
-            log("isSimApplicationReady: SIM is ready for slotIndex: " + slotIndex);
+            logl("isSimApplicationReady: SIM is ready for slotIndex: " + slotIndex);
             return true;
         } else {
             return false;
@@ -661,14 +700,22 @@
                 onEvaluate(REQUESTS_UNCHANGED, "subChanged");
                 break;
             }
+            case EVENT_SERVICE_STATE_CHANGED: {
+                AsyncResult ar = (AsyncResult) msg.obj;
+                final int phoneId = (int) ar.userObj;
+                onServiceStateChanged(phoneId);
+                break;
+            }
+            case EVENT_MEETS_AUTO_DATA_SWITCH_STATE: {
+                final int targetSubId = msg.arg1;
+                final boolean needValidation = (boolean) msg.obj;
+                validate(targetSubId, needValidation,
+                        DataSwitch.Reason.DATA_SWITCH_REASON_AUTO, null);
+                break;
+            }
             case EVENT_PRIMARY_DATA_SUB_CHANGED: {
-                if (onEvaluate(REQUESTS_UNCHANGED, "primary data subId changed")) {
-                    logDataSwitchEvent(mPreferredDataSubId.get(),
-                            TelephonyEvent.EventState.EVENT_STATE_START,
-                            DataSwitch.Reason.DATA_SWITCH_REASON_MANUAL);
-                    registerDefaultNetworkChangeCallback(mPreferredDataSubId.get(),
-                            DataSwitch.Reason.DATA_SWITCH_REASON_MANUAL);
-                }
+                evaluateIfImmediateDataSwitchIsNeeded("primary data sub changed",
+                        DataSwitch.Reason.DATA_SWITCH_REASON_MANUAL);
                 break;
             }
             case EVENT_REQUEST_NETWORK: {
@@ -682,7 +729,7 @@
             case EVENT_EMERGENCY_TOGGLE: {
                 boolean isInEcm = isInEmergencyCallbackMode();
                 if (mEmergencyOverride != null) {
-                    log("Emergency override - ecbm status = " + isInEcm);
+                    logl("Emergency override - ecbm status = " + isInEcm);
                     if (isInEcm) {
                         // The device has gone into ECBM. Wait until it's out.
                         removeMessages(EVENT_REMOVE_DDS_EMERGENCY_OVERRIDE);
@@ -696,6 +743,9 @@
                 onEvaluate(REQUESTS_CHANGED, "emergencyToggle");
                 break;
             }
+            case EVENT_EVALUATE_AUTO_SWITCH:
+                evaluateIfAutoSwitchIsNeeded();
+                break;
             case EVENT_RADIO_CAPABILITY_CHANGED: {
                 final int phoneId = msg.arg1;
                 sendRilCommands(phoneId);
@@ -714,24 +764,25 @@
                 onEvaluate(REQUESTS_UNCHANGED, EVALUATION_REASON_RADIO_ON);
                 break;
             }
-            case EVENT_IMS_RADIO_TECH_CHANGED:
+            case EVENT_IMS_RADIO_TECH_CHANGED: {
                 // register for radio tech change to listen to radio tech handover in case previous
                 // attempt was not successful
                 registerForImsRadioTechChange();
-                // If the phoneId in voice call didn't change, do nothing.
-                if (!isPhoneInVoiceCallChanged()) {
-                    break;
+                // if voice call state changes or in voice call didn't change
+                // but RAT changes(e.g. Iwlan -> cross sim), reevaluate for data switch.
+                if (updatesIfPhoneInVoiceCallChanged() || isAnyVoiceCallActiveOnDevice()) {
+                    evaluateIfImmediateDataSwitchIsNeeded("Ims radio tech changed",
+                            DataSwitch.Reason.DATA_SWITCH_REASON_IN_CALL);
                 }
-                evaluateIfDataSwitchIsNeeded("EVENT_IMS_RADIO_TECH_CHANGED");
                 break;
-
+            }
             case EVENT_PRECISE_CALL_STATE_CHANGED: {
                 // register for radio tech change to listen to radio tech handover in case previous
                 // attempt was not successful
                 registerForImsRadioTechChange();
 
                 // If the phoneId in voice call didn't change, do nothing.
-                if (!isPhoneInVoiceCallChanged()) {
+                if (!updatesIfPhoneInVoiceCallChanged()) {
                     break;
                 }
 
@@ -760,13 +811,16 @@
                         mEmergencyOverride.mPendingOriginatingCall = false;
                     }
                 }
-                evaluateIfDataSwitchIsNeeded("EVENT_PRECISE_CALL_STATE_CHANGED");
+                // Always update data modem via data during call code path, because
+                // mAutoSelectedDataSubId doesn't know about any data switch due to voice call
+                evaluateIfImmediateDataSwitchIsNeeded("precise call state changed",
+                        DataSwitch.Reason.DATA_SWITCH_REASON_IN_CALL);
+                if (!isAnyVoiceCallActiveOnDevice()) {
+                    // consider auto switch on hang up all voice call
+                    evaluateIfAutoSwitchIsNeeded();
+                }
                 break;
             }
-
-            case EVENT_DATA_ENABLED_CHANGED:
-                evaluateIfDataSwitchIsNeeded("EVENT_DATA_ENABLED_CHANGED");
-                break;
             case EVENT_NETWORK_VALIDATION_DONE: {
                 int subId = msg.arg1;
                 boolean passed = (msg.arg2 == 1);
@@ -779,10 +833,6 @@
                 onNetworkAvailable(subId, network);
                 break;
             }
-            case EVENT_REMOVE_DEFAULT_NETWORK_CHANGE_CALLBACK: {
-                removeDefaultNetworkChangeCallback();
-                break;
-            }
             case EVENT_MODEM_COMMAND_DONE: {
                 AsyncResult ar = (AsyncResult) msg.obj;
                 onDdsSwitchResponse(ar);
@@ -791,10 +841,10 @@
             case EVENT_MODEM_COMMAND_RETRY: {
                 int phoneId = (int) msg.obj;
                 if (isPhoneIdValidForRetry(phoneId)) {
-                    log("EVENT_MODEM_COMMAND_RETRY: resend modem command on phone " + phoneId);
+                    logl("EVENT_MODEM_COMMAND_RETRY: resend modem command on phone " + phoneId);
                     sendRilCommands(phoneId);
                 } else {
-                    log("EVENT_MODEM_COMMAND_RETRY: skip retry as DDS sub changed");
+                    logl("EVENT_MODEM_COMMAND_RETRY: skip retry as DDS sub changed");
                     mCurrentDdsSwitchFailure.get(phoneId).clear();
                 }
                 break;
@@ -806,7 +856,7 @@
                     // being overridden, ignore. We should not try to switch DDS while already
                     // waiting for SUPL.
                     if (mEmergencyOverride.mPhoneId != req.mPhoneId) {
-                        log("emergency override requested for phone id " + req.mPhoneId + " when "
+                        logl("emergency override requested for phone id " + req.mPhoneId + " when "
                                 + "there is already an override in place for phone id "
                                 + mEmergencyOverride.mPhoneId + ". Ignoring.");
                         if (req.isCallbackAvailable()) {
@@ -826,7 +876,7 @@
                     mEmergencyOverride = req;
                 }
 
-                log("new emergency override - " + mEmergencyOverride);
+                logl("new emergency override - " + mEmergencyOverride);
                 // a new request has been created, remove any previous override complete scheduled.
                 removeMessages(EVENT_REMOVE_DDS_EMERGENCY_OVERRIDE);
                 Message msg2 = obtainMessage(EVENT_REMOVE_DDS_EMERGENCY_OVERRIDE);
@@ -844,7 +894,7 @@
                 break;
             }
             case EVENT_REMOVE_DDS_EMERGENCY_OVERRIDE: {
-                log("Emergency override removed - " + mEmergencyOverride);
+                logl("Emergency override removed - " + mEmergencyOverride);
                 mEmergencyOverride = null;
                 onEvaluate(REQUESTS_UNCHANGED, "emer_rm_override_dds");
                 break;
@@ -859,7 +909,7 @@
                 int simState = (int) msg.arg2;
 
                 if (!SubscriptionManager.isValidSlotIndex(slotIndex)) {
-                    log("EVENT_PROCESS_SIM_STATE_CHANGE: skip processing due to invalid slotId: "
+                    logl("EVENT_PROCESS_SIM_STATE_CHANGE: skip processing due to invalid slotId: "
                             + slotIndex);
                 } else if (mCurrentDdsSwitchFailure.get(slotIndex).contains(
                         CommandException.Error.INVALID_SIM_STATE)
@@ -867,23 +917,38 @@
                         && isSimApplicationReady(slotIndex)) {
                     sendRilCommands(slotIndex);
                 }
-                break;
-            }
-            case EVENT_DEVICE_CONFIG_CHANGED: {
-                updateDeviceConfig();
+
+                registerConfigChange();
                 break;
             }
         }
     }
 
-    /** Update local properties from {@link DeviceConfig} */
-    private void updateDeviceConfig() {
-        DeviceConfig.Properties properties = //read all telephony properties
-                DeviceConfig.getProperties(DeviceConfig.NAMESPACE_TELEPHONY);
+    /**
+     * Register for device config change on the primary data phone.
+     */
+    private void registerConfigChange() {
+        Phone phone = getPhoneBySubId(mPrimaryDataSubId);
+        if (phone != null) {
+            DataConfigManager dataConfig = phone.getDataNetworkController().getDataConfigManager();
+            dataConfig.registerCallback(mDataConfigManagerCallback);
+            updateConfig();
+            sendEmptyMessage(EVENT_EVALUATE_AUTO_SWITCH);
+        }
+    }
 
-        mAutoDataSwitchAvailabilityStabilityTimeThreshold = properties.getInt(
-                KEY_AUTO_DATA_SWITCH_AVAILABILITY_STABILITY_TIME_THRESHOLD, -1);
-
+    /**
+     * Update data config.
+     */
+    private void updateConfig() {
+        Phone phone = getPhoneBySubId(mPrimaryDataSubId);
+        if (phone != null) {
+            DataConfigManager dataConfig = phone.getDataNetworkController().getDataConfigManager();
+            mAutoDataSwitchAvailabilityStabilityTimeThreshold =
+                    dataConfig.getAutoDataSwitchAvailabilityStabilityTimeThreshold();
+            mAutoDataSwitchValidationMaxRetry =
+                    dataConfig.getAutoDataSwitchValidationMaxRetry();
+        }
     }
 
     private synchronized void onMultiSimConfigChanged(int activeModemCount) {
@@ -920,11 +985,13 @@
                         public void onDataEnabledChanged(boolean enabled,
                                 @TelephonyManager.DataEnabledChangedReason int reason,
                                 @NonNull String callingPackage) {
-                            evaluateIfDataSwitchIsNeeded("EVENT_DATA_ENABLED_CHANGED");
+                            PhoneSwitcher.this.onDataEnabledChanged();
                         }
                     });
             phone.getDataSettingsManager().registerCallback(
                     mDataSettingsManagerCallbacks.get(phone.getPhoneId()));
+            phone.getServiceStateTracker().registerForServiceStateChanged(this,
+                    EVENT_SERVICE_STATE_CHANGED, phoneId);
 
             Set<CommandException.Error> ddsFailure = new HashSet<CommandException.Error>();
             mCurrentDdsSwitchFailure.add(ddsFailure);
@@ -932,6 +999,23 @@
         }
     }
 
+    /**
+     * Called when
+     * 1. user changed mobile data settings
+     * 2. OR user changed auto data switch feature
+     */
+    private void onDataEnabledChanged() {
+        logl("user changed data related settings");
+        if (isAnyVoiceCallActiveOnDevice()) {
+            // user changed data related settings during call, switch or turn off immediately
+            evaluateIfImmediateDataSwitchIsNeeded(
+                    "user changed data settings during call",
+                    DataSwitch.Reason.DATA_SWITCH_REASON_IN_CALL);
+        } else {
+            evaluateIfAutoSwitchIsNeeded();
+        }
+    }
+
     private boolean isInEmergencyCallbackMode() {
         for (Phone p : PhoneFactory.getPhones()) {
             if (p == null) continue;
@@ -987,21 +1071,9 @@
         }
     }
 
-    private void removeDefaultNetworkChangeCallback() {
-        removeMessages(EVENT_REMOVE_DEFAULT_NETWORK_CHANGE_CALLBACK);
-        mDefaultNetworkCallback.mExpectedSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
-        mDefaultNetworkCallback.mSwitchReason =
-                TelephonyEvent.DataSwitch.Reason.DATA_SWITCH_REASON_UNKNOWN;
-        mConnectivityManager.unregisterNetworkCallback(mDefaultNetworkCallback);
-    }
-
     private void registerDefaultNetworkChangeCallback(int expectedSubId, int reason) {
         mDefaultNetworkCallback.mExpectedSubId = expectedSubId;
         mDefaultNetworkCallback.mSwitchReason = reason;
-        mConnectivityManager.registerDefaultNetworkCallback(mDefaultNetworkCallback, this);
-        sendMessageDelayed(
-                obtainMessage(EVENT_REMOVE_DEFAULT_NETWORK_CHANGE_CALLBACK),
-                DEFAULT_NETWORK_CHANGE_TIMEOUT_MS);
     }
 
     private void collectRequestNetworkMetrics(NetworkRequest networkRequest) {
@@ -1028,6 +1100,201 @@
         }
     }
 
+    /**
+     * Called when service state changed.
+     */
+    private void onServiceStateChanged(int phoneId) {
+        Phone phone = findPhoneById(phoneId);
+        if (phone != null) {
+            int newRegState = phone.getServiceState()
+                    .getNetworkRegistrationInfo(
+                            NetworkRegistrationInfo.DOMAIN_PS,
+                            AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
+                    .getRegistrationState();
+            if (newRegState != mPhoneStates[phoneId].dataRegState) {
+                mPhoneStates[phoneId].dataRegState = newRegState;
+                logl("onServiceStateChanged: phoneId:" + phoneId + " dataReg-> "
+                        + NetworkRegistrationInfo.registrationStateToString(newRegState));
+                if (!hasMessages(EVENT_EVALUATE_AUTO_SWITCH)) {
+                    sendEmptyMessage(EVENT_EVALUATE_AUTO_SWITCH);
+                }
+            }
+        }
+    }
+
+    /**
+     * Evaluate if auto switch is suitable at the moment.
+     */
+    private void evaluateIfAutoSwitchIsNeeded() {
+        // auto data switch feature is disabled from server
+        if (mAutoDataSwitchAvailabilityStabilityTimeThreshold < 0) return;
+        // check is valid DSDS
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            if (!isActiveSubId(mPrimaryDataSubId) || mSubscriptionManagerService
+                    .getActiveSubIdList(true).length <= 1) {
+                return;
+            }
+        } else {
+            if (!isActiveSubId(mPrimaryDataSubId)
+                    || mSubscriptionController.getActiveSubIdList(true).length <= 1) {
+                return;
+            }
+        }
+
+        Phone primaryDataPhone = getPhoneBySubId(mPrimaryDataSubId);
+        if (primaryDataPhone == null) {
+            loge("evaluateIfAutoSwitchIsNeeded: cannot find primary data phone. subId="
+                    + mPrimaryDataSubId);
+            return;
+        }
+
+        int primaryPhoneId = primaryDataPhone.getPhoneId();
+        log("evaluateIfAutoSwitchIsNeeded: primaryPhoneId: " + primaryPhoneId
+                + " preferredPhoneId: " + mPreferredDataPhoneId);
+        Phone secondaryDataPhone;
+
+        if (mPreferredDataPhoneId == primaryPhoneId) {
+            // on primary data sub
+
+            int candidateSubId = getAutoSwitchTargetSubIdIfExists();
+            if (candidateSubId != INVALID_SUBSCRIPTION_ID) {
+                startAutoDataSwitchStabilityCheck(candidateSubId, true);
+            } else {
+                cancelPendingAutoDataSwitch();
+            }
+        } else if ((secondaryDataPhone = findPhoneById(mPreferredDataPhoneId)) != null) {
+            // on secondary data sub
+
+            if (!primaryDataPhone.isUserDataEnabled()
+                    || !secondaryDataPhone.isDataAllowed()) {
+                // immediately switch back if user setting changes
+                mAutoSelectedDataSubId = DEFAULT_SUBSCRIPTION_ID;
+                evaluateIfImmediateDataSwitchIsNeeded("User disabled data settings",
+                        DataSwitch.Reason.DATA_SWITCH_REASON_MANUAL);
+                return;
+            }
+
+            NetworkCapabilities defaultNetworkCapabilities = mConnectivityManager
+                    .getNetworkCapabilities(mConnectivityManager.getActiveNetwork());
+            if (defaultNetworkCapabilities != null && !defaultNetworkCapabilities
+                    .hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
+                log("evaluateIfAutoSwitchIsNeeded: "
+                        + "Default network is active on non-cellular transport");
+                startAutoDataSwitchStabilityCheck(DEFAULT_SUBSCRIPTION_ID, false);
+                return;
+            }
+
+            if (mPhoneStates[secondaryDataPhone.getPhoneId()].dataRegState
+                    != NetworkRegistrationInfo.REGISTRATION_STATE_HOME) {
+                // secondary phone lost its HOME availability
+                startAutoDataSwitchStabilityCheck(DEFAULT_SUBSCRIPTION_ID, false);
+                return;
+            }
+
+            if (isInService(mPhoneStates[primaryPhoneId])) {
+                // primary becomes available
+                startAutoDataSwitchStabilityCheck(DEFAULT_SUBSCRIPTION_ID, true);
+                return;
+            }
+
+            // cancel any previous attempts of switching back to primary
+            cancelPendingAutoDataSwitch();
+        }
+    }
+
+    /**
+     * @param phoneState The phone state to check
+     * @return {@code true} if the phone state is considered in service.
+     */
+    private boolean isInService(@NonNull PhoneState phoneState) {
+        return phoneState.dataRegState == NetworkRegistrationInfo.REGISTRATION_STATE_HOME
+                || phoneState.dataRegState == NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING;
+    }
+
+    /**
+     * Called when the current environment suits auto data switch.
+     * Start pre-switch validation if the current environment suits auto data switch for
+     * {@link #mAutoDataSwitchAvailabilityStabilityTimeThreshold} MS.
+     * @param targetSubId the target sub Id.
+     * @param needValidation {@code true} if validation is needed.
+     */
+    private void startAutoDataSwitchStabilityCheck(int targetSubId, boolean needValidation) {
+        log("startAutoDataSwitchStabilityCheck: targetSubId=" + targetSubId
+                + " needValidation=" + needValidation);
+        if (!hasMessages(EVENT_MEETS_AUTO_DATA_SWITCH_STATE, needValidation)) {
+            sendMessageDelayed(obtainMessage(EVENT_MEETS_AUTO_DATA_SWITCH_STATE, targetSubId,
+                            0/*placeholder*/,
+                            needValidation),
+                    mAutoDataSwitchAvailabilityStabilityTimeThreshold);
+        }
+    }
+
+    /**
+     * Cancel any auto switch attempts when the current environment is not suitable for auto switch.
+     */
+    private void cancelPendingAutoDataSwitch() {
+        mAutoSwitchRetryFailedCount = 0;
+        removeMessages(EVENT_MEETS_AUTO_DATA_SWITCH_STATE);
+        if (mValidator.isValidating()) {
+            mValidator.stopValidation();
+
+            removeMessages(EVENT_NETWORK_VALIDATION_DONE);
+            removeMessages(EVENT_NETWORK_AVAILABLE);
+            mPendingSwitchSubId = INVALID_SUBSCRIPTION_ID;
+            mPendingSwitchNeedValidation = false;
+        }
+    }
+
+    /**
+     * Called when consider switching from primary default data sub to another data sub.
+     * @return the target subId if a suitable candidate is found, otherwise return
+     * {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID}
+     */
+    private int getAutoSwitchTargetSubIdIfExists() {
+        Phone primaryDataPhone = getPhoneBySubId(mPrimaryDataSubId);
+        if (primaryDataPhone == null) {
+            log("getAutoSwitchTargetSubId: no sim loaded");
+            return INVALID_SUBSCRIPTION_ID;
+        }
+
+        int primaryPhoneId = primaryDataPhone.getPhoneId();
+
+        if (!primaryDataPhone.isUserDataEnabled()) {
+            log("getAutoSwitchTargetSubId: user disabled data");
+            return INVALID_SUBSCRIPTION_ID;
+        }
+
+        NetworkCapabilities defaultNetworkCapabilities = mConnectivityManager
+                .getNetworkCapabilities(mConnectivityManager.getActiveNetwork());
+        if (defaultNetworkCapabilities != null && !defaultNetworkCapabilities
+                .hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
+            // Exists other active default transport
+            log("getAutoSwitchTargetSubId: Default network is active on non-cellular transport");
+            return INVALID_SUBSCRIPTION_ID;
+        }
+
+        // check whether primary and secondary signal status worth switching
+        if (isInService(mPhoneStates[primaryPhoneId])) {
+            log("getAutoSwitchTargetSubId: primary is in service");
+            return INVALID_SUBSCRIPTION_ID;
+        }
+        for (int phoneId = 0; phoneId < mPhoneStates.length; phoneId++) {
+            if (phoneId != primaryPhoneId) {
+                // the alternative phone must have HOME availability
+                if (mPhoneStates[phoneId].dataRegState
+                        == NetworkRegistrationInfo.REGISTRATION_STATE_HOME) {
+                    log("getAutoSwitchTargetSubId: found phone " + phoneId + " in HOME service");
+                    Phone secondaryDataPhone = findPhoneById(phoneId);
+                    if (secondaryDataPhone != null && // check auto switch feature enabled
+                            secondaryDataPhone.isDataAllowed()) {
+                        return secondaryDataPhone.getSubId();
+                    }
+                }
+            }
+        }
+        return INVALID_SUBSCRIPTION_ID;
+    }
+
     private TelephonyManager getTm() {
         return (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
     }
@@ -1050,11 +1317,17 @@
         boolean diffDetected = mHalCommandToUse != HAL_COMMAND_PREFERRED_DATA && requestsChanged;
 
         // Check if user setting of default non-opportunistic data sub is changed.
-        final int primaryDataSubId = mSubscriptionController.getDefaultDataSubId();
+        int primaryDataSubId;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            primaryDataSubId = mSubscriptionManagerService.getDefaultDataSubId();
+        } else {
+            primaryDataSubId = mSubscriptionController.getDefaultDataSubId();
+        }
         if (primaryDataSubId != mPrimaryDataSubId) {
             sb.append(" mPrimaryDataSubId ").append(mPrimaryDataSubId).append("->")
                 .append(primaryDataSubId);
             mPrimaryDataSubId = primaryDataSubId;
+            mLastSwitchPreferredDataReason = DataSwitch.Reason.DATA_SWITCH_REASON_MANUAL;
         }
 
         // Check to see if there is any active subscription on any phone
@@ -1062,13 +1335,16 @@
 
         // Check if phoneId to subId mapping is changed.
         for (int i = 0; i < mActiveModemCount; i++) {
-            int sub = mSubscriptionController.getSubIdUsingPhoneId(i);
+            int sub = SubscriptionManager.getSubscriptionId(i);
 
             if (SubscriptionManager.isValidSubscriptionId(sub)) hasAnyActiveSubscription = true;
 
             if (sub != mPhoneSubscriptions[i]) {
                 sb.append(" phone[").append(i).append("] ").append(mPhoneSubscriptions[i]);
                 sb.append("->").append(sub);
+                if (mAutoSelectedDataSubId == mPhoneSubscriptions[i]) {
+                    mAutoSelectedDataSubId = DEFAULT_SUBSCRIPTION_ID;
+                }
                 mPhoneSubscriptions[i] = sub;
                 diffDetected = true;
             }
@@ -1094,11 +1370,11 @@
         if (hasAnyActiveSubscription) updatePreferredDataPhoneId();
 
         if (oldPreferredDataPhoneId != mPreferredDataPhoneId) {
-            sb.append(" preferred phoneId ").append(oldPreferredDataPhoneId)
+            sb.append(" preferred data phoneId ").append(oldPreferredDataPhoneId)
                     .append("->").append(mPreferredDataPhoneId);
             diffDetected = true;
         } else if (oldPreferredDataSubId != mPreferredDataSubId.get()) {
-            log("SIM refresh, notify dds change");
+            logl("SIM refresh, notify dds change");
             // Inform connectivity about the active data phone
             notifyPreferredDataSubIdChanged();
         }
@@ -1107,7 +1383,7 @@
         // DDS are out of sync after APM, AP should force DDS when radio on. long term solution
         // should be having API to query preferred data modem to detect the out-of-sync scenarios.
         if (diffDetected || EVALUATION_REASON_RADIO_ON.equals(reason)) {
-            log("evaluating due to " + sb.toString());
+            logl("evaluating due to " + sb);
             if (mHalCommandToUse == HAL_COMMAND_PREFERRED_DATA) {
                 // With HAL_COMMAND_PREFERRED_DATA, all phones are assumed to allow PS attach.
                 // So marking all phone as active, and the phone with mPreferredDataPhoneId
@@ -1145,7 +1421,7 @@
                     }
 
                     if (newActivePhones.size() < mMaxDataAttachModemCount
-                            && newActivePhones.contains(mPreferredDataPhoneId)
+                            && !newActivePhones.contains(mPreferredDataPhoneId)
                             && SubscriptionManager.isUsableSubIdValue(mPreferredDataPhoneId)) {
                         newActivePhones.add(mPreferredDataPhoneId);
                     }
@@ -1178,6 +1454,8 @@
 
     protected static class PhoneState {
         public volatile boolean active = false;
+        public @NetworkRegistrationInfo.RegistrationState int dataRegState =
+                NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING;
         public long lastRequested = 0;
     }
 
@@ -1195,7 +1473,7 @@
         PhoneState state = mPhoneStates[phoneId];
         if (state.active == active) return;
         state.active = active;
-        log((active ? "activate " : "deactivate ") + phoneId);
+        logl((active ? "activate " : "deactivate ") + phoneId);
         state.lastRequested = System.currentTimeMillis();
         sendRilCommands(phoneId);
     }
@@ -1242,7 +1520,7 @@
 
     protected void sendRilCommands(int phoneId) {
         if (!SubscriptionManager.isValidPhoneId(phoneId)) {
-            log("sendRilCommands: skip dds switch due to invalid phoneId=" + phoneId);
+            logl("sendRilCommands: skip dds switch due to invalid phoneId=" + phoneId);
             return;
         }
 
@@ -1254,7 +1532,7 @@
             }
         } else if (phoneId == mPreferredDataPhoneId) {
             // Only setPreferredDataModem if the phoneId equals to current mPreferredDataPhoneId
-            log("sendRilCommands: setPreferredDataModem - phoneId: " + phoneId);
+            logl("sendRilCommands: setPreferredDataModem - phoneId: " + phoneId);
             mRadioConfig.setPreferredDataModem(mPreferredDataPhoneId, message);
         }
     }
@@ -1264,7 +1542,7 @@
                 .getNumberOfModemsWithSimultaneousDataConnections();
         if (mMaxDataAttachModemCount != newMaxDataAttachModemCount) {
             mMaxDataAttachModemCount = newMaxDataAttachModemCount;
-            log("Max active phones changed to " + mMaxDataAttachModemCount);
+            logl("Max active phones changed to " + mMaxDataAttachModemCount);
             onEvaluate(REQUESTS_UNCHANGED, "phoneCfgChanged");
         }
     }
@@ -1314,66 +1592,86 @@
         return INVALID_SUBSCRIPTION_ID;
     }
 
-    private int getSubIdForDefaultNetworkRequests() {
-        if (mSubscriptionController.isActiveSubId(mAutoSelectedDataSubId)) {
-            return mAutoSelectedDataSubId;
+    private boolean isActiveSubId(int subId) {
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = mSubscriptionManagerService
+                    .getSubscriptionInfoInternal(subId);
+            return subInfo != null && subInfo.isActive();
         } else {
-            return mPrimaryDataSubId;
+            return mSubscriptionController.isActiveSubId(subId);
         }
     }
 
     // This updates mPreferredDataPhoneId which decides which phone should handle default network
     // requests.
     protected void updatePreferredDataPhoneId() {
-        Phone voicePhone = findPhoneById(mPhoneIdInVoiceCall);
-        boolean isDataEnabled = false;
-        if (voicePhone != null) {
-            isDataEnabled = voicePhone.getDataSettingsManager()
-                    .isDataEnabled(ApnSetting.TYPE_DEFAULT);
-        }
-
         if (mEmergencyOverride != null && findPhoneById(mEmergencyOverride.mPhoneId) != null) {
             // Override DDS for emergency even if user data is not enabled, since it is an
             // emergency.
             // TODO: Provide a notification to the user that metered data is currently being
             // used during this period.
-            log("updatePreferredDataPhoneId: preferred data overridden for emergency."
+            logl("updatePreferredDataPhoneId: preferred data overridden for emergency."
                     + " phoneId = " + mEmergencyOverride.mPhoneId);
             mPreferredDataPhoneId = mEmergencyOverride.mPhoneId;
-        } else if (isDataEnabled) {
-            // If a phone is in call and user enabled its mobile data, we
-            // should switch internet connection to it. Because the other modem
-            // will lose data connection anyway.
-            // TODO: validate network first.
-            mPreferredDataPhoneId = mPhoneIdInVoiceCall;
+            mLastSwitchPreferredDataReason = DataSwitch.Reason.DATA_SWITCH_REASON_UNKNOWN;
         } else {
-            int subId = getSubIdForDefaultNetworkRequests();
-            int phoneId = SubscriptionManager.INVALID_PHONE_INDEX;
-
-            if (SubscriptionManager.isUsableSubIdValue(subId)) {
-                for (int i = 0; i < mActiveModemCount; i++) {
-                    if (mPhoneSubscriptions[i] == subId) {
-                        phoneId = i;
-                        break;
-                    }
+            int imsRegTech = mImsRegTechProvider.get(mContext, mPhoneIdInVoiceCall);
+            if (isAnyVoiceCallActiveOnDevice() && imsRegTech != REGISTRATION_TECH_IWLAN) {
+                if (imsRegTech != REGISTRATION_TECH_CROSS_SIM) {
+                    mPreferredDataPhoneId = shouldSwitchDataDueToInCall()
+                            ? mPhoneIdInVoiceCall : getFallbackDataPhoneIdForInternetRequests();
+                } else {
+                    logl("IMS call on cross-SIM, skip switching data to phone "
+                            + mPhoneIdInVoiceCall);
                 }
+            } else {
+                mPreferredDataPhoneId = getFallbackDataPhoneIdForInternetRequests();
             }
-
-            mPreferredDataPhoneId = phoneId;
         }
 
-        mPreferredDataSubId.set(
-                mSubscriptionController.getSubIdUsingPhoneId(mPreferredDataPhoneId));
+        mPreferredDataSubId.set(SubscriptionManager.getSubscriptionId(mPreferredDataPhoneId));
+    }
+
+    /**
+     * @return the default data phone Id (or auto selected phone Id in auto data switch/CBRS case)
+     */
+    private int getFallbackDataPhoneIdForInternetRequests() {
+        int fallbackSubId = isActiveSubId(mAutoSelectedDataSubId)
+                ? mAutoSelectedDataSubId : mPrimaryDataSubId;
+
+        if (SubscriptionManager.isUsableSubIdValue(fallbackSubId)) {
+            for (int phoneId = 0; phoneId < mActiveModemCount; phoneId++) {
+                if (mPhoneSubscriptions[phoneId] == fallbackSubId) {
+                    return phoneId;
+                }
+            }
+        }
+        return SubscriptionManager.INVALID_PHONE_INDEX;
+    }
+
+    /**
+     * If a phone is in call and user enabled its mobile data and auto data switch feature, we
+     * should switch internet connection to it because the other modem will lose data connection
+     * anyway.
+     * @return {@code true} if should switch data to the phone in voice call
+     */
+    private boolean shouldSwitchDataDueToInCall() {
+        Phone voicePhone = findPhoneById(mPhoneIdInVoiceCall);
+        Phone defaultDataPhone = getPhoneBySubId(mPrimaryDataSubId);
+        return defaultDataPhone != null // check user enabled data
+                && defaultDataPhone.isUserDataEnabled()
+                && voicePhone != null // check user enabled voice during call feature
+                && voicePhone.isDataAllowed();
     }
 
     protected void transitionToEmergencyPhone() {
         if (mActiveModemCount <= 0) {
-            log("No phones: unable to reset preferred phone for emergency");
+            logl("No phones: unable to reset preferred phone for emergency");
             return;
         }
 
         if (mPreferredDataPhoneId != DEFAULT_EMERGENCY_PHONE_ID) {
-            log("No active subscriptions: resetting preferred phone to 0 for emergency");
+            logl("No active subscriptions: resetting preferred phone to 0 for emergency");
             mPreferredDataPhoneId = DEFAULT_EMERGENCY_PHONE_ID;
         }
 
@@ -1383,6 +1681,14 @@
         }
     }
 
+    private Phone getPhoneBySubId(int subId) {
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            return findPhoneById(mSubscriptionManagerService.getPhoneId(subId));
+        } else {
+            return findPhoneById(mSubscriptionController.getPhoneId(subId));
+        }
+    }
+
     private Phone findPhoneById(final int phoneId) {
         if (!SubscriptionManager.isValidPhoneId(phoneId)) {
             return null;
@@ -1394,15 +1700,16 @@
             TelephonyNetworkRequest networkRequest, int phoneId) {
         if (!SubscriptionManager.isValidPhoneId(phoneId)) return false;
 
+        int subId = SubscriptionManager.getSubscriptionId(phoneId);
+
         // In any case, if phone state is inactive, don't apply the network request.
-        if (!isPhoneActive(phoneId) || (
-                mSubscriptionController.getSubIdUsingPhoneId(phoneId) == INVALID_SUBSCRIPTION_ID
+        if (!isPhoneActive(phoneId) || (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID
                 && !isEmergencyNetworkRequest(networkRequest))) {
             return false;
         }
 
         NetworkRequest netRequest = networkRequest.getNativeNetworkRequest();
-        int subId = getSubIdFromNetworkSpecifier(netRequest.getNetworkSpecifier());
+        subId = getSubIdFromNetworkSpecifier(netRequest.getNetworkSpecifier());
 
         //if this phone is an emergency networkRequest
         //and subId is not specified that is invalid or default
@@ -1473,10 +1780,12 @@
      */
     private void validate(int subId, boolean needValidation, int switchReason,
             @Nullable ISetOpportunisticDataCallback callback) {
+        logl("Validate subId " + subId + " due to " + switchReasonToString(switchReason)
+                + " needValidation=" + needValidation);
         int subIdToValidate = (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID)
                 ? mPrimaryDataSubId : subId;
-        if (!mSubscriptionController.isActiveSubId(subIdToValidate)) {
-            log("Can't switch data to inactive subId " + subIdToValidate);
+        if (!isActiveSubId(subIdToValidate)) {
+            logl("Can't switch data to inactive subId " + subIdToValidate);
             if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
                 // the default data sub is not selected yet, store the intent of switching to
                 // default subId once it becomes available.
@@ -1504,7 +1813,7 @@
             return;
         }
 
-        mLastAutoSelectedSwitchReason = switchReason;
+        mLastSwitchPreferredDataReason = switchReason;
         logDataSwitchEvent(subIdToValidate,
                 TelephonyEvent.EventState.EVENT_STATE_START,
                 switchReason);
@@ -1551,7 +1860,7 @@
         try {
             callback.onComplete(result);
         } catch (RemoteException exception) {
-            log("RemoteException " + exception);
+            logl("RemoteException " + exception);
         }
     }
 
@@ -1564,18 +1873,25 @@
     private void setAutoSelectedDataSubIdInternal(int subId) {
         if (mAutoSelectedDataSubId != subId) {
             mAutoSelectedDataSubId = subId;
-            onEvaluate(REQUESTS_UNCHANGED, switchReasonToString(mLastAutoSelectedSwitchReason));
+            onEvaluate(REQUESTS_UNCHANGED, switchReasonToString(mLastSwitchPreferredDataReason));
         }
     }
 
     private void confirmSwitch(int subId, boolean confirm) {
-        log("confirmSwitch: subId " + subId + (confirm ? " confirmed." : " cancelled."));
+        logl("confirmSwitch: subId " + subId + (confirm ? " confirmed." : " cancelled."));
         int resultForCallBack;
-        if (!mSubscriptionController.isActiveSubId(subId)) {
-            log("confirmSwitch: subId " + subId + " is no longer active");
+        if (!isActiveSubId(subId)) {
+            logl("confirmSwitch: subId " + subId + " is no longer active");
             resultForCallBack = SET_OPPORTUNISTIC_SUB_INACTIVE_SUBSCRIPTION;
+            mAutoSwitchRetryFailedCount = 0;
         } else if (!confirm) {
             resultForCallBack = SET_OPPORTUNISTIC_SUB_VALIDATION_FAILED;
+
+            // retry for auto data switch validation failure
+            if (mLastSwitchPreferredDataReason == DataSwitch.Reason.DATA_SWITCH_REASON_AUTO) {
+                scheduleAutoSwitchRetryEvaluation();
+                mAutoSwitchRetryFailedCount++;
+            }
         } else {
             if (subId == mPrimaryDataSubId) {
                 setAutoSelectedDataSubIdInternal(DEFAULT_SUBSCRIPTION_ID);
@@ -1583,6 +1899,7 @@
                 setAutoSelectedDataSubIdInternal(subId);
             }
             resultForCallBack = SET_OPPORTUNISTIC_SUB_SUCCESS;
+            mAutoSwitchRetryFailedCount = 0;
         }
 
         // Trigger callback if needed
@@ -1591,6 +1908,23 @@
         mPendingSwitchSubId = INVALID_SUBSCRIPTION_ID;
     }
 
+    /**
+     * Schedule auto data switch evaluation retry if haven't reached the max retry count.
+     */
+    private void scheduleAutoSwitchRetryEvaluation() {
+        if (mAutoSwitchRetryFailedCount < mAutoDataSwitchValidationMaxRetry) {
+            if (!hasMessages(EVENT_EVALUATE_AUTO_SWITCH)) {
+                sendMessageDelayed(obtainMessage(EVENT_EVALUATE_AUTO_SWITCH),
+                        mAutoDataSwitchAvailabilityStabilityTimeThreshold
+                                << mAutoSwitchRetryFailedCount);
+            }
+        } else {
+            logl("scheduleAutoSwitchEvaluation: reached max auto switch retry count "
+                    + mAutoDataSwitchValidationMaxRetry);
+            mAutoSwitchRetryFailedCount = 0;
+        }
+    }
+
     private void onNetworkAvailable(int subId, Network network) {
         log("onNetworkAvailable: on subId " + subId);
         // Do nothing unless pending switch matches target subId and it doesn't require
@@ -1603,7 +1937,7 @@
     }
 
     private void onValidationDone(int subId, boolean passed) {
-        log("onValidationDone: " + (passed ? "passed" : "failed") + " on subId " + subId);
+        logl("onValidationDone: " + (passed ? "passed" : "failed") + " on subId " + subId);
         if (mPendingSwitchSubId == INVALID_SUBSCRIPTION_ID || mPendingSwitchSubId != subId) return;
 
         // If validation failed and mPendingSwitch.mNeedValidation is false, we still confirm
@@ -1629,7 +1963,7 @@
      */
     public void trySetOpportunisticDataSubscription(int subId, boolean needValidation,
             ISetOpportunisticDataCallback callback) {
-        log("Try set opportunistic data subscription to subId " + subId
+        logl("Try set opportunistic data subscription to subId " + subId
                 + (needValidation ? " with " : " without ") + "validation");
         PhoneSwitcher.this.obtainMessage(EVENT_OPPT_DATA_SUB_CHANGED,
                 subId, needValidation ? 1 : 0, callback).sendToTarget();
@@ -1654,13 +1988,34 @@
         return mPreferredDataPhoneId;
     }
 
+    /**
+     * Log debug messages and also log into the local log.
+     * @param l debug messages
+     */
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
-    protected void log(String l) {
-        Rlog.d(LOG_TAG, l);
+    protected void logl(String l) {
+        log(l);
         mLocalLog.log(l);
     }
 
     /**
+     * Log debug messages.
+     * @param s debug messages
+     */
+    private void log(@NonNull String s) {
+        Rlog.d(LOG_TAG, s);
+    }
+
+    /**
+     * Log debug error messages.
+     * @param s debug messages
+     */
+    private void loge(@NonNull String s) {
+        Rlog.e(LOG_TAG, s);
+    }
+
+
+    /**
      * Convert data switch reason into string.
      *
      * @param reason The switch reason.
@@ -1676,6 +2031,8 @@
                 return "IN_CALL";
             case TelephonyEvent.DataSwitch.Reason.DATA_SWITCH_REASON_CBRS:
                 return "CBRS";
+            case TelephonyEvent.DataSwitch.Reason.DATA_SWITCH_REASON_AUTO:
+                return "AUTO";
             default: return "UNKNOWN(" + reason + ")";
         }
     }
@@ -1706,7 +2063,7 @@
      * @param reason The switching reason.
      */
     private void logDataSwitchEvent(int subId, int state, int reason) {
-        log("Data switch event. subId=" + subId + ", state=" + switchStateToString(state)
+        logl("Data switch event. subId=" + subId + ", state=" + switchStateToString(state)
                 + ", reason=" + switchReasonToString(reason));
         DataSwitch dataSwitch = new DataSwitch();
         dataSwitch.state = state;
@@ -1720,7 +2077,7 @@
     protected void notifyPreferredDataSubIdChanged() {
         TelephonyRegistryManager telephonyRegistryManager = (TelephonyRegistryManager) mContext
                 .getSystemService(Context.TELEPHONY_REGISTRY_SERVICE);
-        log("notifyPreferredDataSubIdChanged to " + mPreferredDataSubId.get());
+        logl("notifyPreferredDataSubIdChanged to " + mPreferredDataSubId.get());
         telephonyRegistryManager.notifyActiveDataSubIdChanged(mPreferredDataSubId.get());
     }
 
@@ -1752,15 +2109,23 @@
         for (int i = 0; i < mActiveModemCount; i++) {
             PhoneState ps = mPhoneStates[i];
             c.setTimeInMillis(ps.lastRequested);
-            pw.println("PhoneId(" + i + ") active=" + ps.active + ", lastRequest=" +
-                    (ps.lastRequested == 0 ? "never" :
+            pw.println("PhoneId(" + i + ") active=" + ps.active + ", dataRegState="
+                    + NetworkRegistrationInfo.registrationStateToString(ps.dataRegState)
+                    + ", lastRequest="
+                    + (ps.lastRequested == 0 ? "never" :
                      String.format("%tm-%td %tH:%tM:%tS.%tL", c, c, c, c, c, c)));
         }
         pw.println("mPreferredDataPhoneId=" + mPreferredDataPhoneId);
         pw.println("mPreferredDataSubId=" + mPreferredDataSubId.get());
-        pw.println("DefaultDataSubId=" + mSubscriptionController.getDefaultDataSubId());
-        pw.println("DefaultDataPhoneId=" + mSubscriptionController.getPhoneId(
-                mSubscriptionController.getDefaultDataSubId()));
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            pw.println("DefaultDataSubId=" + mSubscriptionManagerService.getDefaultDataSubId());
+            pw.println("DefaultDataPhoneId=" + mSubscriptionManagerService.getPhoneId(
+                    mSubscriptionManagerService.getDefaultDataSubId()));
+        } else {
+            pw.println("DefaultDataSubId=" + mSubscriptionController.getDefaultDataSubId());
+            pw.println("DefaultDataPhoneId=" + mSubscriptionController.getPhoneId(
+                    mSubscriptionController.getDefaultDataSubId()));
+        }
         pw.println("mPrimaryDataSubId=" + mPrimaryDataSubId);
         pw.println("mAutoSelectedDataSubId=" + mAutoSelectedDataSubId);
         pw.println("mIsRegisteredForImsRadioTechChange=" + mIsRegisteredForImsRadioTechChange);
@@ -1771,6 +2136,10 @@
         pw.println("mCurrentDdsSwitchFailure=" + mCurrentDdsSwitchFailure);
         pw.println("mAutoDataSwitchAvailabilityStabilityTimeThreshold="
                 + mAutoDataSwitchAvailabilityStabilityTimeThreshold);
+        pw.println("mAutoDataSwitchValidationMaxRetry=" + mAutoDataSwitchValidationMaxRetry);
+        pw.println("mLastSwitchPreferredDataReason="
+                + switchReasonToString(mLastSwitchPreferredDataReason));
+        pw.println("mDisplayedAutoSwitchNotification=" + mDisplayedAutoSwitchNotification);
         pw.println("Local logs:");
         pw.increaseIndent();
         mLocalLog.dump(fd, pw, args);
@@ -1788,49 +2157,118 @@
         boolean commandSuccess = ar != null && ar.exception == null;
         int phoneId = (int) ar.userObj;
         if (mEmergencyOverride != null) {
-            log("Emergency override result sent = " + commandSuccess);
+            logl("Emergency override result sent = " + commandSuccess);
             mEmergencyOverride.sendOverrideCompleteCallbackResultAndClear(commandSuccess);
             // Do not retry , as we do not allow changes in onEvaluate during an emergency
             // call. When the call ends, we will start the countdown to remove the override.
         } else if (!commandSuccess) {
-            log("onDdsSwitchResponse: DDS switch failed. with exception " + ar.exception);
+            logl("onDdsSwitchResponse: DDS switch failed. with exception " + ar.exception);
             if (ar.exception instanceof CommandException) {
                 CommandException.Error error = ((CommandException)
                         (ar.exception)).getCommandError();
                 mCurrentDdsSwitchFailure.get(phoneId).add(error);
                 if (error == CommandException.Error.OP_NOT_ALLOWED_DURING_VOICE_CALL) {
-                    log("onDdsSwitchResponse: Wait for call end indication");
+                    logl("onDdsSwitchResponse: Wait for call end indication");
                     return;
                 } else if (error == CommandException.Error.INVALID_SIM_STATE) {
                     /* If there is a attach failure due to sim not ready then
                     hold the retry until sim gets ready */
-                    log("onDdsSwitchResponse: Wait for SIM to get READY");
+                    logl("onDdsSwitchResponse: Wait for SIM to get READY");
                     return;
                 }
             }
-            log("onDdsSwitchResponse: Scheduling DDS switch retry");
+            logl("onDdsSwitchResponse: Scheduling DDS switch retry");
             sendMessageDelayed(Message.obtain(this, EVENT_MODEM_COMMAND_RETRY,
                         phoneId), MODEM_COMMAND_RETRY_PERIOD_MS);
             return;
         }
-        if (commandSuccess) log("onDdsSwitchResponse: DDS switch success on phoneId = " + phoneId);
+        if (commandSuccess) logl("onDdsSwitchResponse: DDS switch success on phoneId = " + phoneId);
         mCurrentDdsSwitchFailure.get(phoneId).clear();
         // Notify all registrants
         mActivePhoneRegistrants.notifyRegistrants();
         notifyPreferredDataSubIdChanged();
+        displayAutoDataSwitchNotification();
+    }
+
+    /**
+     * Display a notification the first time auto data switch occurs.
+     */
+    private void displayAutoDataSwitchNotification() {
+        NotificationManager notificationManager = (NotificationManager)
+                mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+
+        if (mDisplayedAutoSwitchNotification) {
+            // cancel posted notification if any exist
+            log("displayAutoDataSwitchNotification: canceling any notifications for subId "
+                    + mAutoSelectedDataSubId);
+            notificationManager.cancel(AUTO_DATA_SWITCH_NOTIFICATION_TAG,
+                    AUTO_DATA_SWITCH_NOTIFICATION_ID);
+            return;
+        }
+        // proceed only the first time auto data switch occurs, which includes data during call
+        if (mLastSwitchPreferredDataReason != DataSwitch.Reason.DATA_SWITCH_REASON_AUTO) {
+            log("displayAutoDataSwitchNotification: Ignore DDS switch due to "
+                    + switchReasonToString(mLastSwitchPreferredDataReason));
+            return;
+        }
+        SubscriptionInfo subInfo;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            subInfo = mSubscriptionManagerService.getSubscriptionInfo(mAutoSelectedDataSubId);
+        } else {
+            subInfo = mSubscriptionController.getSubscriptionInfo(mAutoSelectedDataSubId);
+        }
+        if (subInfo == null || subInfo.isOpportunistic()) {
+            loge("displayAutoDataSwitchNotification: mAutoSelectedDataSubId="
+                    + mAutoSelectedDataSubId + " unexpected subInfo " + subInfo);
+            return;
+        }
+        logl("displayAutoDataSwitchNotification: display for subId=" + mAutoSelectedDataSubId);
+        // "Mobile network settings" screen / dialog
+        Intent intent = new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS);
+        final Bundle fragmentArgs = new Bundle();
+        // Special contract for Settings to highlight permission row
+        fragmentArgs.putString(SETTINGS_EXTRA_FRAGMENT_ARG_KEY, AUTO_DATA_SWITCH_SETTING_R_ID);
+        intent.putExtra(Settings.EXTRA_SUB_ID, mAutoSelectedDataSubId);
+        intent.putExtra(SETTINGS_EXTRA_SHOW_FRAGMENT_ARGUMENTS, fragmentArgs);
+        PendingIntent contentIntent = PendingIntent.getActivity(
+                mContext, mAutoSelectedDataSubId, intent, PendingIntent.FLAG_IMMUTABLE);
+
+        CharSequence activeCarrierName = subInfo.getDisplayName();
+        CharSequence contentTitle = mContext.getString(
+                com.android.internal.R.string.auto_data_switch_title, activeCarrierName);
+        CharSequence contentText = mContext.getText(
+                com.android.internal.R.string.auto_data_switch_content);
+
+        final Notification notif = new Notification.Builder(mContext)
+                .setContentTitle(contentTitle)
+                .setContentText(contentText)
+                .setSmallIcon(android.R.drawable.stat_sys_warning)
+                .setColor(mContext.getResources().getColor(
+                        com.android.internal.R.color.system_notification_accent_color))
+                .setChannelId(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS)
+                .setContentIntent(contentIntent)
+                .setStyle(new Notification.BigTextStyle().bigText(contentText))
+                .build();
+        notificationManager.notify(AUTO_DATA_SWITCH_NOTIFICATION_TAG,
+                AUTO_DATA_SWITCH_NOTIFICATION_ID, notif);
+        mDisplayedAutoSwitchNotification = true;
     }
 
     private boolean isPhoneIdValidForRetry(int phoneId) {
-        int phoneIdForRequest = INVALID_PHONE_INDEX;
-        int ddsPhoneId = mSubscriptionController.getPhoneId(
-                mSubscriptionController.getDefaultDataSubId());
+        int ddsPhoneId;
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            ddsPhoneId = mSubscriptionManagerService.getPhoneId(
+                    mSubscriptionManagerService.getDefaultDataSubId());
+        } else {
+            ddsPhoneId = mSubscriptionController.getPhoneId(
+                    mSubscriptionController.getDefaultDataSubId());
+        }
         if (ddsPhoneId != INVALID_PHONE_INDEX && ddsPhoneId == phoneId) {
             return true;
         } else {
             if (mNetworkRequestList.isEmpty()) return false;
             for (TelephonyNetworkRequest networkRequest : mNetworkRequestList) {
-                phoneIdForRequest = phoneIdForRequest(networkRequest);
-                if (phoneIdForRequest == phoneId) {
+                if (phoneIdForRequest(networkRequest) == phoneId) {
                     return true;
                 }
             }
diff --git a/src/java/com/android/internal/telephony/data/TelephonyNetworkAgent.java b/src/java/com/android/internal/telephony/data/TelephonyNetworkAgent.java
index 78c555c..5ed09e6 100644
--- a/src/java/com/android/internal/telephony/data/TelephonyNetworkAgent.java
+++ b/src/java/com/android/internal/telephony/data/TelephonyNetworkAgent.java
@@ -50,6 +50,9 @@
     private final String mLogTag;
     private final LocalLog mLocalLog = new LocalLog(128);
 
+    /** Max unregister network agent delay. */
+    private static final int NETWORK_AGENT_TEARDOWN_DELAY_MS = 5_000;
+
     /** The parent data network. */
     private final @NonNull DataNetwork mDataNetwork;
 
@@ -296,14 +299,15 @@
 
     /**
      * Abandon the network agent. This is used for telephony to re-create the network agent when
-     * immutable capabilities got changed, where telephony calls {@link NetworkAgent#unregister()}
-     * and then create another network agent with new capabilities. Abandon this network agent
-     * allowing it ignore the subsequent {@link #onNetworkUnwanted()} invocation caused by
-     * {@link NetworkAgent#unregister()}.
+     * immutable capabilities got changed, where telephony calls
+     * {@link NetworkAgent#unregisterAfterReplacement} and then create another network agent with
+     * new capabilities. Abandon this network agent allowing it ignore the subsequent
+     * {@link #onNetworkUnwanted()} invocation caused by
+     * {@link NetworkAgent#unregisterAfterReplacement}.
      */
     public void abandon() {
         mAbandoned = true;
-        unregister();
+        unregisterAfterReplacement(NETWORK_AGENT_TEARDOWN_DELAY_MS);
     }
 
     /**
diff --git a/src/java/com/android/internal/telephony/data/TelephonyNetworkFactory.java b/src/java/com/android/internal/telephony/data/TelephonyNetworkFactory.java
index e64dd9b..2cdf807 100644
--- a/src/java/com/android/internal/telephony/data/TelephonyNetworkFactory.java
+++ b/src/java/com/android/internal/telephony/data/TelephonyNetworkFactory.java
@@ -29,7 +29,6 @@
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.Phone;
-import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.metrics.NetworkRequestsStats;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.telephony.Rlog;
@@ -63,7 +62,6 @@
     private static final int EVENT_NETWORK_RELEASE                  = 4;
 
     private final PhoneSwitcher mPhoneSwitcher;
-    private final SubscriptionController mSubscriptionController;
     private final LocalLog mLocalLog = new LocalLog(REQUEST_LOG_SIZE);
 
     // Key: network request. Value: the transport of the network request applies to,
@@ -86,10 +84,9 @@
         mPhone = phone;
         mInternalHandler = new InternalHandler(looper);
 
-        mSubscriptionController = SubscriptionController.getInstance();
         mAccessNetworksManager = mPhone.getAccessNetworksManager();
 
-        setCapabilityFilter(makeNetworkFilter(mSubscriptionController, mPhone.getPhoneId()));
+        setCapabilityFilter(makeNetworkFilterByPhoneId(mPhone.getPhoneId()));
         setScoreFilter(TELEPHONY_NETWORK_SCORE);
 
         mPhoneSwitcher = PhoneSwitcher.getInstance();
@@ -113,10 +110,8 @@
                 }
             };
 
-    private NetworkCapabilities makeNetworkFilter(SubscriptionController subscriptionController,
-            int phoneId) {
-        final int subscriptionId = subscriptionController.getSubIdUsingPhoneId(phoneId);
-        return makeNetworkFilter(subscriptionId);
+    private NetworkCapabilities makeNetworkFilterByPhoneId(int phoneId) {
+        return makeNetworkFilter(SubscriptionManager.getSubscriptionId(phoneId));
     }
 
     /**
@@ -238,8 +233,7 @@
     // watch for phone->subId changes, reapply new filter and let
     // that flow through to apply/revoke of requests
     private void onSubIdChange() {
-        final int newSubscriptionId = mSubscriptionController.getSubIdUsingPhoneId(
-                mPhone.getPhoneId());
+        int newSubscriptionId = SubscriptionManager.getSubscriptionId(mPhone.getPhoneId());
         if (mSubscriptionId != newSubscriptionId) {
             if (DBG) logl("onSubIdChange " + mSubscriptionId + "->" + newSubscriptionId);
             mSubscriptionId = newSubscriptionId;
diff --git a/src/java/com/android/internal/telephony/domainselection/DomainSelectionConnection.java b/src/java/com/android/internal/telephony/domainselection/DomainSelectionConnection.java
new file mode 100644
index 0000000..e10c199
--- /dev/null
+++ b/src/java/com/android/internal/telephony/domainselection/DomainSelectionConnection.java
@@ -0,0 +1,441 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.os.AsyncResult;
+import android.os.CancellationSignal;
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.os.Looper;
+import android.os.Message;
+import android.telephony.AccessNetworkConstants.RadioAccessNetworkType;
+import android.telephony.Annotation.DisconnectCauses;
+import android.telephony.DomainSelectionService;
+import android.telephony.DomainSelectionService.EmergencyScanType;
+import android.telephony.DomainSelector;
+import android.telephony.EmergencyRegResult;
+import android.telephony.NetworkRegistrationInfo;
+import android.telephony.TransportSelectorCallback;
+import android.telephony.WwanSelectorCallback;
+import android.util.LocalLog;
+import android.util.Log;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.infra.AndroidFuture;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.util.TelephonyUtils;
+
+import java.io.PrintWriter;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import java.util.function.Consumer;
+
+
+/**
+ * Manages the information of request and the callback binder.
+ */
+public class DomainSelectionConnection {
+
+    private static final boolean DBG = TelephonyUtils.IS_DEBUGGABLE;
+
+    protected static final int EVENT_EMERGENCY_NETWORK_SCAN_RESULT = 1;
+    protected static final int EVENT_QUALIFIED_NETWORKS_CHANGED = 2;
+
+    /** Callback to receive responses from DomainSelectionConnection. */
+    public interface DomainSelectionConnectionCallback {
+        /**
+         * Notifies that selection has terminated because there is no decision that can be made
+         * or a timeout has occurred. The call should be terminated when this method is called.
+         *
+         * @param cause Indicates the reason.
+         */
+        void onSelectionTerminated(@DisconnectCauses int cause);
+    }
+
+    /** An internal class implementing {@link TransportSelectorCallback} interface. */
+    private final class TransportSelectorCallbackWrapper implements TransportSelectorCallback {
+        @Override
+        public void onCreated(@NonNull DomainSelector selector) {
+            mDomainSelector = selector;
+            DomainSelectionConnection.this.onCreated();
+        }
+
+        @Override
+        public void onWlanSelected() {
+            DomainSelectionConnection.this.onWlanSelected();
+        }
+
+        @Override
+        public @NonNull WwanSelectorCallback onWwanSelected() {
+            if (mWwanSelectorCallback == null) {
+                mWwanSelectorCallback = new WwanSelectorCallbackWrapper();
+            }
+            DomainSelectionConnection.this.onWwanSelected();
+            return mWwanSelectorCallback;
+        }
+
+        @Override
+        public void onWwanSelected(final Consumer<WwanSelectorCallback> consumer) {
+            if (mWwanSelectorCallback == null) {
+                mWwanSelectorCallback = new WwanSelectorCallbackWrapper();
+            }
+            if (mWwanSelectedExecutor == null) {
+                mWwanSelectedExecutor = Executors.newSingleThreadExecutor();
+            }
+            mWwanSelectedExecutor.execute(() -> {
+                DomainSelectionConnection.this.onWwanSelected();
+                consumer.accept(mWwanSelectorCallback);
+            });
+        }
+
+        @Override
+        public void onSelectionTerminated(int cause) {
+            DomainSelectionConnection.this.onSelectionTerminated(cause);
+            dispose();
+        }
+    }
+
+    /** An internal class implementing {@link WwanSelectorCallback} interface. */
+    private final class WwanSelectorCallbackWrapper
+            implements WwanSelectorCallback, CancellationSignal.OnCancelListener {
+        @Override
+        public void onRequestEmergencyNetworkScan(@NonNull List<Integer> preferredNetworks,
+                @EmergencyScanType int scanType, @NonNull CancellationSignal signal,
+                @NonNull Consumer<EmergencyRegResult> consumer) {
+            if (signal != null) signal.setOnCancelListener(this);
+            mResultCallback = consumer;
+            initHandler();
+            DomainSelectionConnection.this.onRequestEmergencyNetworkScan(
+                    preferredNetworks.stream().mapToInt(Integer::intValue).toArray(), scanType);
+        }
+
+        @Override
+        public void onDomainSelected(@NetworkRegistrationInfo.Domain int domain) {
+            DomainSelectionConnection.this.onDomainSelected(domain);
+        }
+
+        @Override
+        public void onCancel() {
+            DomainSelectionConnection.this.onCancel();
+        }
+    }
+
+    protected final class DomainSelectionConnectionHandler extends Handler {
+        DomainSelectionConnectionHandler(Looper looper) {
+            super(looper);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            AsyncResult ar;
+            switch (msg.what) {
+                case EVENT_EMERGENCY_NETWORK_SCAN_RESULT:
+                    mIsWaitingForScanResult = false;
+                    if (mResultCallback == null) break;
+                    ar = (AsyncResult) msg.obj;
+                    EmergencyRegResult regResult = (EmergencyRegResult) ar.result;
+                    if (DBG) logd("EVENT_EMERGENCY_NETWORK_SCAN_RESULT result=" + regResult);
+                    CompletableFuture.runAsync(
+                            () -> mResultCallback.accept(regResult),
+                            mController.getDomainSelectionServiceExecutor()).join();
+                    break;
+                case EVENT_QUALIFIED_NETWORKS_CHANGED:
+                    onQualifiedNetworksChanged();
+                    break;
+                default:
+                    loge("handleMessage unexpected msg=" + msg.what);
+                    break;
+            }
+        }
+    }
+
+    protected String mTag = "DomainSelectionConnection";
+
+    private final LocalLog mLocalLog = new LocalLog(30);
+    private final @NonNull TransportSelectorCallback mTransportSelectorCallback;
+
+    /**
+     * Controls the communication between {@link DomainSelectionConnection} and
+     * {@link DomainSelectionService}.
+     */
+    private final @NonNull DomainSelectionController mController;
+    /** Indicates whether the requested service is for emergency services. */
+    private final boolean mIsEmergency;
+
+    /** Interface to receive the request to trigger emergency network scan and selected domain. */
+    private @Nullable WwanSelectorCallback mWwanSelectorCallback;
+    /** Interface to return the result of emergency network scan. */
+    private @Nullable Consumer<EmergencyRegResult> mResultCallback;
+    /** Interface to the {@link DomainSelector} created for this service. */
+    private @Nullable DomainSelector mDomainSelector;
+
+    /** The slot requested this connection. */
+    protected @NonNull Phone mPhone;
+    /** The requested domain selector type. */
+    private @DomainSelectionService.SelectorType int mSelectorType;
+
+    /** The attributes required to determine the domain. */
+    private @Nullable DomainSelectionService.SelectionAttributes mSelectionAttributes;
+
+    private @Nullable Looper mLooper;
+    protected @Nullable DomainSelectionConnectionHandler mHandler;
+    private boolean mRegisteredRegistrant;
+    private boolean mIsWaitingForScanResult;
+
+    private @NonNull AndroidFuture<Integer> mOnComplete;
+
+    private @Nullable Executor mWwanSelectedExecutor;
+
+    /**
+     * Creates an instance.
+     *
+     * @param phone For which this service is requested.
+     * @param selectorType Indicates the type of the requested service.
+     * @param isEmergency Indicates whether this request is for emergency service.
+     * @param controller The controller to communicate with the domain selection service.
+     */
+    public DomainSelectionConnection(@NonNull Phone phone,
+            @DomainSelectionService.SelectorType int selectorType, boolean isEmergency,
+            @NonNull DomainSelectionController controller) {
+        mController = controller;
+        mPhone = phone;
+        mSelectorType = selectorType;
+        mIsEmergency = isEmergency;
+
+        mTransportSelectorCallback = new TransportSelectorCallbackWrapper();
+        mOnComplete = new AndroidFuture<>();
+    }
+
+    /**
+     * Returns the attributes required to determine the domain for a telephony service.
+     *
+     * @return The attributes required to determine the domain.
+     */
+    public @Nullable DomainSelectionService.SelectionAttributes getSelectionAttributes() {
+        return mSelectionAttributes;
+    }
+
+    /**
+     * Returns the interface for the callbacks.
+     *
+     * @return The {@link TransportSelectorCallback} interface.
+     */
+    @VisibleForTesting
+    public @NonNull TransportSelectorCallback getTransportSelectorCallback() {
+        return mTransportSelectorCallback;
+    }
+
+    /**
+     * Returns the {@link CompletableFuture} to receive the selected domain.
+     *
+     * @return The callback to receive response.
+     */
+    public @NonNull CompletableFuture<Integer> getCompletableFuture() {
+        return mOnComplete;
+    }
+
+    /**
+     * Returs the {@link Phone} which requested this connection.
+     *
+     * @return The {@link Phone} instance.
+     */
+    public @NonNull Phone getPhone() {
+        return mPhone;
+    }
+
+    /**
+     * Requests the domain selection servic to select a domain.
+     *
+     * @param attr The attributes required to determine the domain.
+     */
+    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PROTECTED)
+    public void selectDomain(@NonNull DomainSelectionService.SelectionAttributes attr) {
+        mSelectionAttributes = attr;
+        mController.selectDomain(attr, getTransportSelectorCallback());
+    }
+
+    /**
+     * Notifies that {@link DomainSelector} instance has been created for the selection request.
+     */
+    public void onCreated() {
+        // Can be overridden if required
+    }
+
+    /**
+     * Notifies that WLAN transport has been selected.
+     */
+    public void onWlanSelected() {
+        // Can be overridden.
+    }
+
+    /**
+     * Notifies that WWAN transport has been selected.
+     */
+    public void onWwanSelected() {
+        // Can be overridden.
+    }
+
+    /**
+     * Notifies that selection has terminated because there is no decision that can be made
+     * or a timeout has occurred. The call should be terminated when this method is called.
+     *
+     * @param cause Indicates the reason.
+     */
+    public void onSelectionTerminated(@DisconnectCauses int cause) {
+        // Can be overridden.
+    }
+
+    /**
+     * Requests the emergency network scan.
+     *
+     * @param preferredNetworks The ordered list of preferred networks to scan.
+     * @param scanType Indicates the scan preference, such as full service or limited service.
+     */
+    public void onRequestEmergencyNetworkScan(
+            @NonNull @RadioAccessNetworkType int[] preferredNetworks,
+            @EmergencyScanType int scanType) {
+        // Can be overridden if required
+        if (!mRegisteredRegistrant) {
+            mPhone.registerForEmergencyNetworkScan(mHandler,
+                    EVENT_EMERGENCY_NETWORK_SCAN_RESULT, null);
+            mRegisteredRegistrant = true;
+        }
+        mIsWaitingForScanResult = true;
+        mPhone.triggerEmergencyNetworkScan(preferredNetworks, scanType, null);
+    }
+
+    /**
+     * Notifies the domain selected.
+     * @param domain The selected domain.
+     */
+    public void onDomainSelected(@NetworkRegistrationInfo.Domain int domain) {
+        // Can be overridden if required
+        CompletableFuture<Integer> future = getCompletableFuture();
+        future.complete(domain);
+    }
+
+    /**
+     * Notifies that the emergency network scan is canceled.
+     */
+    public void onCancel() {
+        // Can be overridden if required
+        onCancel(false);
+    }
+
+    private void onCancel(boolean resetScan) {
+        mIsWaitingForScanResult = false;
+        mPhone.cancelEmergencyNetworkScan(resetScan, null);
+    }
+
+    /**
+     * Cancels an ongoing selection operation. It is up to the {@link DomainSelectionService}
+     * to clean up all ongoing operations with the framework.
+     */
+    public void cancelSelection() {
+        if (mDomainSelector == null) return;
+        mDomainSelector.cancelSelection();
+        dispose();
+    }
+
+    /**
+     * Requests the domain selection service to reselect a domain.
+     *
+     * @param attr The attributes required to determine the domain.
+     * @return The callback to receive the response.
+     */
+    public @NonNull CompletableFuture<Integer> reselectDomain(
+            @NonNull DomainSelectionService.SelectionAttributes attr) {
+        mSelectionAttributes = attr;
+        if (mDomainSelector == null) return null;
+        mOnComplete = new AndroidFuture<>();
+        mDomainSelector.reselectDomain(attr);
+        return mOnComplete;
+    }
+
+    /**
+     * Finishes the selection procedure and cleans everything up.
+     */
+    public void finishSelection() {
+        if (mDomainSelector == null) return;
+        mDomainSelector.finishSelection();
+        dispose();
+    }
+
+    /** Indicates that the service connection has been removed. */
+    public void onServiceDisconnected() {
+        // Can be overridden.
+        dispose();
+    }
+
+    private void dispose() {
+        if (mRegisteredRegistrant) {
+            mPhone.unregisterForEmergencyNetworkScan(mHandler);
+            mRegisteredRegistrant = false;
+        }
+        if (mIsWaitingForScanResult) onCancel(true);
+        mController.removeConnection(this);
+        if (mLooper != null) mLooper.quitSafely();
+        mLooper = null;
+        mHandler = null;
+    }
+
+    protected void initHandler() {
+        if (mLooper == null) {
+            HandlerThread handlerThread = new HandlerThread(mTag);
+            handlerThread.start();
+            mLooper = handlerThread.getLooper();
+        }
+        if (mHandler == null) mHandler = new DomainSelectionConnectionHandler(mLooper);
+    }
+
+    /**
+     * Notifies the change of qualified networks.
+     */
+    protected void onQualifiedNetworksChanged() {
+        if (mIsEmergency
+                && (mSelectorType == DomainSelectionService.SELECTOR_TYPE_CALLING)) {
+            // DomainSelectionConnection for emergency calls shall override this.
+            throw new IllegalStateException("DomainSelectionConnection for emergency calls"
+                    + " should override onQualifiedNetworksChanged()");
+        }
+    }
+
+    /**
+     * Dumps local log.
+     */
+    public void dump(@NonNull PrintWriter printWriter) {
+        mLocalLog.dump(printWriter);
+    }
+
+    protected void logd(String msg) {
+        Log.d(mTag, msg);
+    }
+
+    protected void logi(String msg) {
+        Log.i(mTag, msg);
+        mLocalLog.log(msg);
+    }
+
+    protected void loge(String msg) {
+        Log.e(mTag, msg);
+        mLocalLog.log(msg);
+    }
+}
diff --git a/src/java/com/android/internal/telephony/domainselection/DomainSelectionController.java b/src/java/com/android/internal/telephony/domainselection/DomainSelectionController.java
new file mode 100644
index 0000000..52c9960
--- /dev/null
+++ b/src/java/com/android/internal/telephony/domainselection/DomainSelectionController.java
@@ -0,0 +1,299 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import static android.telephony.DomainSelectionService.SELECTOR_TYPE_CALLING;
+import static android.telephony.DomainSelectionService.SELECTOR_TYPE_SMS;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.Context;
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.os.Looper;
+import android.os.Message;
+import android.telephony.BarringInfo;
+import android.telephony.DomainSelectionService;
+import android.telephony.ServiceState;
+import android.telephony.TelephonyManager;
+import android.telephony.TransportSelectorCallback;
+import android.util.LocalLog;
+import android.util.Log;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.util.TelephonyUtils;
+
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.concurrent.Executor;
+
+/**
+ * Manages the connection to {@link DomainSelectionService}.
+ */
+public class DomainSelectionController {
+    private static final String TAG = "DomainSelectionController";
+    private static final boolean DBG = TelephonyUtils.IS_DEBUGGABLE;
+
+    private static final int EVENT_SERVICE_STATE_CHANGED = 1;
+    private static final int EVENT_BARRING_INFO_CHANGED = 2;
+
+    private final HandlerThread mHandlerThread =
+            new HandlerThread("DomainSelectionControllerHandler");
+
+    private final DomainSelectionService mDomainSelectionService;
+    private final Handler mHandler;
+    // Only added or removed, never accessed on purpose.
+    private final LocalLog mLocalLog = new LocalLog(30);
+
+    protected final Object mLock = new Object();
+    protected final Context mContext;
+
+    protected final int[] mConnectionCounts;
+    private final ArrayList<DomainSelectionConnection> mConnections = new ArrayList<>();
+
+    private final class DomainSelectionControllerHandler extends Handler {
+        DomainSelectionControllerHandler(Looper looper) {
+            super(looper);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            AsyncResult ar;
+            switch (msg.what) {
+                case EVENT_SERVICE_STATE_CHANGED:
+                    ar = (AsyncResult) msg.obj;
+                    updateServiceState((Phone) ar.userObj, (ServiceState) ar.result);
+                    break;
+                case EVENT_BARRING_INFO_CHANGED:
+                    ar = (AsyncResult) msg.obj;
+                    updateBarringInfo((Phone) ar.userObj, (BarringInfo) ar.result);
+                    break;
+                default:
+                    loge("unexpected event=" + msg.what);
+                    break;
+            }
+        }
+    }
+
+    /**
+     * Creates an instance.
+     *
+     * @param context Context object from hosting application.
+     * @param service The {@link DomainSelectionService} instance.
+     */
+    public DomainSelectionController(@NonNull Context context,
+            @NonNull DomainSelectionService service) {
+        this(context, service, null);
+    }
+
+    /**
+     * Creates an instance.
+     *
+     * @param context Context object from hosting application.
+     * @param service The {@link DomainSelectionService} instance.
+     * @param looper Handles event messages.
+     */
+    @VisibleForTesting
+    public DomainSelectionController(@NonNull Context context,
+            @NonNull DomainSelectionService service, @Nullable Looper looper) {
+        mContext = context;
+        mDomainSelectionService = service;
+
+        if (looper == null) {
+            mHandlerThread.start();
+            looper = mHandlerThread.getLooper();
+        }
+        mHandler = new DomainSelectionControllerHandler(looper);
+
+        int numPhones = TelephonyManager.getDefault().getActiveModemCount();
+        mConnectionCounts = new int[numPhones];
+        for (int i = 0; i < numPhones; i++) {
+            mConnectionCounts[i] = 0;
+        }
+    }
+
+    /**
+     * Returns a {@link DomainSelectionConnection} instance.
+     *
+     * @param phone Indicates who requests the service.
+     * @param selectorType Indicates the selector type requested.
+     * @param isEmergency Indicates whether this is for emergency service.
+     * @return A {@link DomainSelectiionConnection} instance for the requested service.
+     *         Returns {@code null} if the requested service is not supported.
+     */
+    public @Nullable DomainSelectionConnection getDomainSelectionConnection(
+            @NonNull Phone phone,
+            @DomainSelectionService.SelectorType int selectorType,
+            boolean isEmergency) {
+        DomainSelectionConnection c = null;
+
+        if (selectorType == SELECTOR_TYPE_CALLING) {
+            if (isEmergency) {
+                c = new EmergencyCallDomainSelectionConnection(phone, this);
+            } else {
+                c = new NormalCallDomainSelectionConnection(phone, this);
+            }
+        } else if (selectorType == SELECTOR_TYPE_SMS) {
+            if (isEmergency) {
+                c = new EmergencySmsDomainSelectionConnection(phone, this);
+            } else {
+                c = new SmsDomainSelectionConnection(phone, this);
+            }
+        }
+
+        addConnection(c);
+        return c;
+    }
+
+    private void addConnection(@Nullable DomainSelectionConnection c) {
+        if (c == null) return;
+        mConnections.add(c);
+        registerForStateChange(c);
+    }
+
+    /**
+     * Releases resources for this connection.
+     */
+    public void removeConnection(@Nullable DomainSelectionConnection c) {
+        if (c == null) return;
+        mConnections.remove(c);
+        unregisterForStateChange(c);
+    }
+
+    /**
+     * Requests the domain selection.
+     *
+     * @param attr Attributetes required to determine the domain.
+     * @param callback A callback to receive the response.
+     */
+    public void selectDomain(@NonNull DomainSelectionService.SelectionAttributes attr,
+            @NonNull TransportSelectorCallback callback) {
+        if (attr == null || callback == null) return;
+        if (DBG) logd("selectDomain");
+
+        Executor e = mDomainSelectionService.getCachedExecutor();
+        e.execute(() -> mDomainSelectionService.onDomainSelection(attr, callback));
+    }
+
+    /**
+     * Notifies the change in {@link ServiceState} for a specific slot.
+     *
+     * @param phone {@link Phone} which the state changed.
+     * @param serviceState Updated {@link ServiceState}.
+     */
+    private void updateServiceState(Phone phone, ServiceState serviceState) {
+        if (phone == null || serviceState == null) return;
+        if (DBG) logd("updateServiceState phoneId=" + phone.getPhoneId());
+
+        Executor e = mDomainSelectionService.getCachedExecutor();
+        e.execute(() -> mDomainSelectionService.onServiceStateUpdated(
+                phone.getPhoneId(), phone.getSubId(), serviceState));
+    }
+
+    /**
+     * Notifies the change in {@link BarringInfo} for a specific slot.
+     *
+     * @param phone {@link Phone} which the state changed.
+     * @param info Updated {@link BarringInfo}.
+     */
+    private void updateBarringInfo(Phone phone, BarringInfo info) {
+        if (phone == null || info == null) return;
+        if (DBG) logd("updateBarringInfo phoneId=" + phone.getPhoneId());
+
+        Executor e = mDomainSelectionService.getCachedExecutor();
+        e.execute(() -> mDomainSelectionService.onBarringInfoUpdated(
+                phone.getPhoneId(), phone.getSubId(), info));
+    }
+
+    /**
+     * Registers for the notification of {@link ServiceState} and {@link BarringInfo}.
+     *
+     * @param c {@link DomainSelectionConnection} for which the registration is requested.
+     */
+    private void registerForStateChange(DomainSelectionConnection c) {
+        Phone phone = c.getPhone();
+        int count = mConnectionCounts[phone.getPhoneId()];
+        if (count < 0) count = 0;
+
+        mConnectionCounts[phone.getPhoneId()] = count + 1;
+        if (count > 0) return;
+
+        phone.registerForServiceStateChanged(mHandler, EVENT_SERVICE_STATE_CHANGED, phone);
+        phone.mCi.registerForBarringInfoChanged(mHandler, EVENT_BARRING_INFO_CHANGED, phone);
+
+        updateServiceState(phone, phone.getServiceStateTracker().getServiceState());
+        updateBarringInfo(phone, phone.mCi.getLastBarringInfo());
+    }
+
+    /**
+     * Unregisters for the notification of {@link ServiceState} and {@link BarringInfo}.
+     *
+     * @param c {@link DomainSelectionConnection} for which the unregistration is requested.
+     */
+    private void unregisterForStateChange(DomainSelectionConnection c) {
+        Phone phone = c.getPhone();
+        int count = mConnectionCounts[phone.getPhoneId()];
+        if (count < 1) count = 1;
+
+        mConnectionCounts[phone.getPhoneId()] = count - 1;
+        if (count > 1) return;
+
+        phone.unregisterForServiceStateChanged(mHandler);
+        phone.mCi.unregisterForBarringInfoChanged(mHandler);
+    }
+
+    /**
+     * Notifies the {@link DomainSelectionConnection} instances registered
+     * of the service disconnection.
+     */
+    private void notifyServiceDisconnected() {
+        for (DomainSelectionConnection c : mConnections) {
+            c.onServiceDisconnected();
+        }
+    }
+
+    /**
+     * Gets the {@link Executor} which executes methods of {@link DomainSelectionService.}
+     * @return {@link Executor} instance.
+     */
+    public @NonNull Executor getDomainSelectionServiceExecutor() {
+        return mDomainSelectionService.getCachedExecutor();
+    }
+
+    /**
+     * Dumps logcal log
+     */
+    public void dump(@NonNull PrintWriter printWriter) {
+        mLocalLog.dump(printWriter);
+    }
+
+    private void logd(String msg) {
+        Log.d(TAG, msg);
+    }
+
+    private void logi(String msg) {
+        Log.i(TAG, msg);
+        mLocalLog.log(msg);
+    }
+
+    private void loge(String msg) {
+        Log.e(TAG, msg);
+        mLocalLog.log(msg);
+    }
+}
diff --git a/src/java/com/android/internal/telephony/domainselection/DomainSelectionResolver.java b/src/java/com/android/internal/telephony/domainselection/DomainSelectionResolver.java
new file mode 100644
index 0000000..cbb74fa
--- /dev/null
+++ b/src/java/com/android/internal/telephony/domainselection/DomainSelectionResolver.java
@@ -0,0 +1,207 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
+
+import static com.android.internal.telephony.RIL.RADIO_HAL_VERSION_2_1;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.Context;
+import android.telephony.DomainSelectionService;
+import android.util.IndentingPrintWriter;
+import android.util.LocalLog;
+import android.util.Log;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneFactory;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+
+/**
+ * This class is an entry point to provide whether the AOSP domain selection is supported or not,
+ * and bind the {@link DomainSelectionController} with the given {@link DomainSelectionService} to
+ * provide a specific {@link DomainSelectionConnection} object for communicating with each domain
+ * selector.
+ */
+public class DomainSelectionResolver {
+    private static final String TAG = DomainSelectionResolver.class.getSimpleName();
+    private static DomainSelectionResolver sInstance = null;
+
+    /**
+     * Creates the DomainSelectionResolver singleton instance.
+     *
+     * @param context The context of the application.
+     * @param deviceConfigEnabled The flag to indicate whether or not the device supports
+     *                            the domain selection service or not.
+     */
+    public static void make(Context context, boolean deviceConfigEnabled) {
+        if (sInstance == null) {
+            sInstance = new DomainSelectionResolver(context, deviceConfigEnabled);
+        }
+    }
+
+    /**
+     * Returns the singleton instance of DomainSelectionResolver.
+     *
+     * @return A {@link DomainSelectionResolver} instance.
+     */
+    public static DomainSelectionResolver getInstance() {
+        if (sInstance == null) {
+            throw new IllegalStateException("DomainSelectionResolver is not ready!");
+        }
+        return sInstance;
+    }
+
+    /**
+     * Sets a {@link DomainSelectionResolver} for injecting mock DomainSelectionResolver.
+     *
+     * @param resolver A {@link DomainSelectionResolver} instance to test.
+     */
+    @VisibleForTesting
+    public static void setDomainSelectionResolver(DomainSelectionResolver resolver) {
+        sInstance = resolver;
+    }
+
+    /**
+     * Testing interface for injecting mock DomainSelectionController.
+     */
+    @VisibleForTesting
+    public interface DomainSelectionControllerFactory {
+        /**
+         * Returns a {@link DomainSelectionController} created using the specified
+         * context and {@link DomainSelectionService} instance.
+         */
+        DomainSelectionController create(@NonNull Context context,
+                @NonNull DomainSelectionService service);
+    }
+
+    private DomainSelectionControllerFactory mDomainSelectionControllerFactory =
+            new DomainSelectionControllerFactory() {
+        @Override
+        public DomainSelectionController create(@NonNull Context context,
+                @NonNull DomainSelectionService service) {
+            return new DomainSelectionController(context, service);
+        }
+    };
+
+    // Persistent Logging
+    private final LocalLog mEventLog = new LocalLog(10);
+    private final Context mContext;
+    // The flag to indicate whether the device supports the domain selection service or not.
+    private final boolean mDeviceConfigEnabled;
+    // DomainSelectionController, which are bound to DomainSelectionService.
+    private DomainSelectionController mController;
+
+    public DomainSelectionResolver(Context context, boolean deviceConfigEnabled) {
+        mContext = context;
+        mDeviceConfigEnabled = deviceConfigEnabled;
+        logi("DomainSelectionResolver created: device-config=" + deviceConfigEnabled);
+    }
+
+    /**
+     * Checks if the device supports the domain selection service to route the call / SMS /
+     * supplementary services to the appropriate domain.
+     * This checks the device-config and Radio HAL version for supporting the domain selection.
+     * The domain selection requires the Radio HAL version greater than or equal to 2.1.
+     *
+     * @return {@code true} if the domain selection is supported on the device,
+     *         {@code false} otherwise.
+     */
+    public boolean isDomainSelectionSupported() {
+        return mDeviceConfigEnabled && PhoneFactory.getDefaultPhone()
+                .getHalVersion(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_1);
+    }
+
+    /**
+     * Returns a {@link DomainSelectionConnection} instance.
+     *
+     * @param phone The Phone instance for witch this request is.
+     * @param selectorType Indicates the selector type requested.
+     * @param isEmergency Indicates whether this is for emergency service.
+     * @throws IllegalStateException If the {@link DomainSelectionController} is not created
+     *         because {@link #initialize} method is not called even if the domain selection is
+     *         supported.
+     * @return A {@link DomainSelectionConnection} instance if the device supports
+     *         AOSP domain selection and IMS is available or {@code null} otherwise.
+     */
+    public @Nullable DomainSelectionConnection getDomainSelectionConnection(Phone phone,
+            @DomainSelectionService.SelectorType int selectorType, boolean isEmergency) {
+        if (mController == null) {
+            // If the caller calls this method without checking whether the domain selection
+            // is supported or not, this exception will be thrown.
+            throw new IllegalStateException("DomainSelection is not supported!");
+        }
+
+        if (phone == null || !phone.isImsAvailable()) {
+            // If ImsPhone is null or the binder of ImsService is not available,
+            // CS domain is used for the telephony services.
+            return null;
+        }
+
+        return mController.getDomainSelectionConnection(phone, selectorType, isEmergency);
+    }
+
+    /** Sets a factory interface for creating {@link DomainSelectionController} instance. */
+    @VisibleForTesting
+    public void setDomainSelectionControllerFactory(DomainSelectionControllerFactory factory) {
+        mDomainSelectionControllerFactory = factory;
+    }
+
+    /**
+     * Needs to be called after the constructor to create a {@link DomainSelectionController} that
+     * is bound to the given {@link DomainSelectionService}.
+     *
+     * @param service A {@link DomainSelectionService} to be bound.
+     */
+    public void initialize(@NonNull DomainSelectionService service) {
+        logi("Initialize.");
+        mController = mDomainSelectionControllerFactory.create(mContext, service);
+    }
+
+    /**
+     * Dumps this instance into a readable format for dumpsys usage.
+     */
+    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        IndentingPrintWriter ipw = new IndentingPrintWriter(pw, "  ");
+        ipw.println("Resolver:");
+        ipw.increaseIndent();
+        ipw.println("Event Log:");
+        ipw.increaseIndent();
+        mEventLog.dump(ipw);
+        ipw.decreaseIndent();
+        ipw.decreaseIndent();
+
+        ipw.println("Controller:");
+        ipw.increaseIndent();
+        DomainSelectionController controller = mController;
+        if (controller == null) {
+            ipw.println("no active controller");
+        } else {
+            controller.dump(ipw);
+        }
+        ipw.decreaseIndent();
+    }
+
+    private void logi(String s) {
+        Log.i(TAG, s);
+        mEventLog.log(s);
+    }
+}
diff --git a/src/java/com/android/internal/telephony/domainselection/EmergencyCallDomainSelectionConnection.java b/src/java/com/android/internal/telephony/domainselection/EmergencyCallDomainSelectionConnection.java
new file mode 100644
index 0000000..2646c48
--- /dev/null
+++ b/src/java/com/android/internal/telephony/domainselection/EmergencyCallDomainSelectionConnection.java
@@ -0,0 +1,197 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import static android.telephony.AccessNetworkConstants.TRANSPORT_TYPE_INVALID;
+import static android.telephony.AccessNetworkConstants.TRANSPORT_TYPE_WLAN;
+import static android.telephony.AccessNetworkConstants.TRANSPORT_TYPE_WWAN;
+import static android.telephony.DomainSelectionService.SELECTOR_TYPE_CALLING;
+import static android.telephony.NetworkRegistrationInfo.DOMAIN_PS;
+
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_WLAN;
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_WWAN;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.telephony.AccessNetworkConstants.TransportType;
+import android.telephony.Annotation.DisconnectCauses;
+import android.telephony.Annotation.NetCapability;
+import android.telephony.DomainSelectionService;
+import android.telephony.EmergencyRegResult;
+import android.telephony.NetworkRegistrationInfo;
+import android.telephony.data.ApnSetting;
+import android.telephony.ims.ImsReasonInfo;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.data.AccessNetworksManager;
+import com.android.internal.telephony.emergency.EmergencyStateTracker;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Manages the information of request and the callback binder for emergency calling.
+ */
+public class EmergencyCallDomainSelectionConnection extends DomainSelectionConnection {
+
+    private static final boolean DBG = false;
+
+    private @NonNull EmergencyStateTracker mEmergencyStateTracker = null;
+    private @Nullable DomainSelectionConnectionCallback mCallback;
+    private @TransportType int mPreferredTransportType = TRANSPORT_TYPE_INVALID;
+
+    /**
+     * Create an instance.
+     *
+     * @param phone For which this service is requested.
+     * @param controller The controller to communicate with the domain selection service.
+     */
+    public EmergencyCallDomainSelectionConnection(@NonNull Phone phone,
+            @NonNull DomainSelectionController controller) {
+        this(phone, controller, EmergencyStateTracker.getInstance());
+    }
+
+    /**
+     * Create an instance.
+     *
+     * @param phone For which this service is requested.
+     * @param controller The controller to communicate with the domain selection service.
+     * @param tracker The {@link EmergencyStateTracker} instance.
+     */
+    @VisibleForTesting
+    public EmergencyCallDomainSelectionConnection(@NonNull Phone phone,
+            @NonNull DomainSelectionController controller, @NonNull EmergencyStateTracker tracker) {
+        super(phone, SELECTOR_TYPE_CALLING, true, controller);
+        mTag = "EmergencyCallDomainSelectionConnection";
+
+        mEmergencyStateTracker = tracker;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void onWlanSelected() {
+        mEmergencyStateTracker.onEmergencyTransportChanged(MODE_EMERGENCY_WLAN);
+        AccessNetworksManager anm = mPhone.getAccessNetworksManager();
+        if (anm.getPreferredTransport(ApnSetting.TYPE_EMERGENCY) != TRANSPORT_TYPE_WLAN) {
+            changePreferredTransport(TRANSPORT_TYPE_WLAN);
+            return;
+        }
+
+        CompletableFuture<Integer> future = getCompletableFuture();
+        if (future != null) future.complete(DOMAIN_PS);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void onWwanSelected() {
+        mEmergencyStateTracker.onEmergencyTransportChanged(MODE_EMERGENCY_WWAN);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void onSelectionTerminated(@DisconnectCauses int cause) {
+        if (mCallback != null) mCallback.onSelectionTerminated(cause);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void onDomainSelected(@NetworkRegistrationInfo.Domain int domain) {
+        if (domain == DOMAIN_PS) {
+            AccessNetworksManager anm = mPhone.getAccessNetworksManager();
+            if (anm.getPreferredTransport(ApnSetting.TYPE_EMERGENCY) != TRANSPORT_TYPE_WWAN) {
+                changePreferredTransport(TRANSPORT_TYPE_WWAN);
+                return;
+            }
+        }
+        super.onDomainSelected(domain);
+    }
+
+    /**
+     * Request a domain for emergency call.
+     *
+     * @param attr The attributes required to determine the domain.
+     * @param callback A callback to receive the response.
+     * @return the callback to receive the response.
+     */
+    public @NonNull CompletableFuture<Integer> createEmergencyConnection(
+            @NonNull DomainSelectionService.SelectionAttributes attr,
+            @NonNull DomainSelectionConnectionCallback callback) {
+        mCallback = callback;
+        selectDomain(attr);
+        return getCompletableFuture();
+    }
+
+    private void changePreferredTransport(@TransportType int transportType) {
+        initHandler();
+        mPreferredTransportType = transportType;
+        AccessNetworksManager anm = mPhone.getAccessNetworksManager();
+        anm.registerForQualifiedNetworksChanged(mHandler, EVENT_QUALIFIED_NETWORKS_CHANGED);
+        mPhone.notifyEmergencyDomainSelected(transportType);
+    }
+
+    private AccessNetworksManager.AccessNetworksManagerCallback mPreferredTransportCallback =
+            new AccessNetworksManager.AccessNetworksManagerCallback(Runnable::run) {
+        @Override
+        public void onPreferredTransportChanged(@NetCapability int capability) {
+        }
+    };
+
+    /** {@inheritDoc} */
+    @Override
+    protected void onQualifiedNetworksChanged() {
+        AccessNetworksManager anm = mPhone.getAccessNetworksManager();
+        int preferredTransport = anm.getPreferredTransport(ApnSetting.TYPE_EMERGENCY);
+        if (preferredTransport == mPreferredTransportType) {
+            CompletableFuture<Integer> future = getCompletableFuture();
+            if (future != null) future.complete(DOMAIN_PS);
+            anm.unregisterForQualifiedNetworksChanged(mHandler);
+        }
+    }
+
+    /**
+     * Returns the attributes required to determine the domain for a telephony service.
+     *
+     * @param slotId The slot identifier.
+     * @param subId The subscription identifier.
+     * @param exited {@code true} if the request caused the device to move out of airplane mode.
+     * @param callId The call identifier.
+     * @param number The dialed number.
+     * @param callFailCause The reason why the last CS attempt failed.
+     * @param imsReasonInfo The reason why the last PS attempt failed.
+     * @param emergencyRegResult The current registration result for emergency services.
+     * @return The attributes required to determine the domain.
+     */
+    public static @NonNull DomainSelectionService.SelectionAttributes getSelectionAttributes(
+            int slotId, int subId, boolean exited,
+            @NonNull String callId, @NonNull String number, int callFailCause,
+            @Nullable ImsReasonInfo imsReasonInfo,
+            @Nullable EmergencyRegResult emergencyRegResult) {
+        DomainSelectionService.SelectionAttributes.Builder builder =
+                new DomainSelectionService.SelectionAttributes.Builder(
+                        slotId, subId, SELECTOR_TYPE_CALLING)
+                .setEmergency(true)
+                .setExitedFromAirplaneMode(exited)
+                .setCallId(callId)
+                .setNumber(number)
+                .setCsDisconnectCause(callFailCause);
+
+        if (imsReasonInfo != null) builder.setPsDisconnectCause(imsReasonInfo);
+        if (emergencyRegResult != null) builder.setEmergencyRegResult(emergencyRegResult);
+
+        return builder.build();
+    }
+}
diff --git a/src/java/com/android/internal/telephony/domainselection/EmergencySmsDomainSelectionConnection.java b/src/java/com/android/internal/telephony/domainselection/EmergencySmsDomainSelectionConnection.java
new file mode 100644
index 0000000..bf8ccca
--- /dev/null
+++ b/src/java/com/android/internal/telephony/domainselection/EmergencySmsDomainSelectionConnection.java
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_WLAN;
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_WWAN;
+
+import android.annotation.NonNull;
+import android.telephony.AccessNetworkConstants;
+import android.telephony.AccessNetworkConstants.TransportType;
+import android.telephony.NetworkRegistrationInfo;
+import android.telephony.data.ApnSetting;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.data.AccessNetworksManager;
+import com.android.internal.telephony.emergency.EmergencyStateTracker;
+
+/**
+ * Manages the information of request and the callback binder for an emergency SMS.
+ */
+public class EmergencySmsDomainSelectionConnection extends SmsDomainSelectionConnection {
+    private final Object mLock = new Object();
+    private @NonNull EmergencyStateTracker mEmergencyStateTracker;
+    private @TransportType int mPreferredTransportType =
+            AccessNetworkConstants.TRANSPORT_TYPE_INVALID;
+
+    public EmergencySmsDomainSelectionConnection(
+            Phone phone, DomainSelectionController controller) {
+        this(phone, controller, EmergencyStateTracker.getInstance());
+    }
+
+    @VisibleForTesting
+    public EmergencySmsDomainSelectionConnection(Phone phone,
+            DomainSelectionController controller, EmergencyStateTracker tracker) {
+        super(phone, controller, true);
+        mTag = "DomainSelectionConnection-EmergencySMS";
+        mEmergencyStateTracker = tracker;
+    }
+
+    @Override
+    public void onWlanSelected() {
+        synchronized (mLock) {
+            if (mPreferredTransportType != AccessNetworkConstants.TRANSPORT_TYPE_INVALID) {
+                logi("Domain selection completion is in progress");
+                return;
+            }
+
+            mEmergencyStateTracker.onEmergencyTransportChanged(MODE_EMERGENCY_WLAN);
+
+            // Change the transport type if the current preferred transport type for an emergency
+            // is not {@link AccessNetworkConstants#TRANSPORT_TYPE_WLAN}.
+            AccessNetworksManager anm = mPhone.getAccessNetworksManager();
+            if (anm.getPreferredTransport(ApnSetting.TYPE_EMERGENCY)
+                    != AccessNetworkConstants.TRANSPORT_TYPE_WLAN) {
+                changePreferredTransport(AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
+                // The {@link #onDomainSlected()} will be called after the preferred transport
+                // is successfully changed and notified from the {@link AccessNetworksManager}.
+                return;
+            }
+
+            super.onWlanSelected();
+        }
+    }
+
+    @Override
+    public void onWwanSelected() {
+        mEmergencyStateTracker.onEmergencyTransportChanged(MODE_EMERGENCY_WWAN);
+    }
+
+    @Override
+    public void onDomainSelected(@NetworkRegistrationInfo.Domain int domain) {
+        synchronized (mLock) {
+            if (mPreferredTransportType != AccessNetworkConstants.TRANSPORT_TYPE_INVALID) {
+                logi("Domain selection completion is in progress");
+                return;
+            }
+
+            if (domain == NetworkRegistrationInfo.DOMAIN_PS) {
+                // Change the transport type if the current preferred transport type for
+                // an emergency is not {@link AccessNetworkConstants#TRANSPORT_TYPE_WWAN}.
+                AccessNetworksManager anm = mPhone.getAccessNetworksManager();
+                if (anm.getPreferredTransport(ApnSetting.TYPE_EMERGENCY)
+                        != AccessNetworkConstants.TRANSPORT_TYPE_WWAN) {
+                    changePreferredTransport(AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+                    // The {@link #onDomainSlected()} will be called after the preferred transport
+                    // is successfully changed and notified from the {@link AccessNetworksManager}.
+                    return;
+                }
+            }
+
+            super.onDomainSelected(domain);
+        }
+    }
+
+    @Override
+    public void finishSelection() {
+        AccessNetworksManager anm = mPhone.getAccessNetworksManager();
+
+        synchronized (mLock) {
+            if (mPreferredTransportType != AccessNetworkConstants.TRANSPORT_TYPE_INVALID) {
+                mPreferredTransportType = AccessNetworkConstants.TRANSPORT_TYPE_INVALID;
+                anm.unregisterForQualifiedNetworksChanged(mHandler);
+            }
+        }
+
+        super.finishSelection();
+    }
+
+    @Override
+    protected void onQualifiedNetworksChanged() {
+        AccessNetworksManager anm = mPhone.getAccessNetworksManager();
+        int preferredTransportType = anm.getPreferredTransport(ApnSetting.TYPE_EMERGENCY);
+
+        synchronized (mLock) {
+            if (preferredTransportType == mPreferredTransportType) {
+                mPreferredTransportType = AccessNetworkConstants.TRANSPORT_TYPE_INVALID;
+                super.onDomainSelected(NetworkRegistrationInfo.DOMAIN_PS);
+                anm.unregisterForQualifiedNetworksChanged(mHandler);
+            }
+        }
+    }
+
+    private void changePreferredTransport(@TransportType int transportType) {
+        logi("Change preferred transport: " + transportType);
+        initHandler();
+        mPreferredTransportType = transportType;
+        AccessNetworksManager anm = mPhone.getAccessNetworksManager();
+        anm.registerForQualifiedNetworksChanged(mHandler, EVENT_QUALIFIED_NETWORKS_CHANGED);
+        mPhone.notifyEmergencyDomainSelected(transportType);
+    }
+}
diff --git a/src/java/com/android/internal/telephony/domainselection/NormalCallDomainSelectionConnection.java b/src/java/com/android/internal/telephony/domainselection/NormalCallDomainSelectionConnection.java
new file mode 100644
index 0000000..516d6b9
--- /dev/null
+++ b/src/java/com/android/internal/telephony/domainselection/NormalCallDomainSelectionConnection.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import static android.telephony.DomainSelectionService.SELECTOR_TYPE_CALLING;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.telephony.AccessNetworkConstants.RadioAccessNetworkType;
+import android.telephony.Annotation.DisconnectCauses;
+import android.telephony.DomainSelectionService;
+import android.telephony.DomainSelectionService.EmergencyScanType;
+import android.telephony.NetworkRegistrationInfo;
+import android.telephony.ims.ImsReasonInfo;
+
+import com.android.internal.telephony.Phone;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Manages the information of request and the callback binder for normal calling.
+ */
+public class NormalCallDomainSelectionConnection extends DomainSelectionConnection {
+
+    private static final boolean DBG = false;
+
+    private @Nullable DomainSelectionConnectionCallback mCallback;
+
+    /**
+     * Create an instance.
+     *
+     * @param phone For which this service is requested.
+     * @param controller The controller to communicate with the domain selection service.
+     */
+    public NormalCallDomainSelectionConnection(@NonNull Phone phone,
+            @NonNull DomainSelectionController controller) {
+        super(phone, SELECTOR_TYPE_CALLING, false, controller);
+        mTag = "NormalCallDomainSelectionConnection";
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void onWlanSelected() {
+        CompletableFuture<Integer> future = getCompletableFuture();
+        future.complete(NetworkRegistrationInfo.DOMAIN_PS);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void onWwanSelected() {
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void onSelectionTerminated(@DisconnectCauses int cause) {
+        if (mCallback != null) mCallback.onSelectionTerminated(cause);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void onRequestEmergencyNetworkScan(@RadioAccessNetworkType int[] preferredNetworks,
+            @EmergencyScanType int scanType) {
+        // Not expected with normal calling.
+        // Override to prevent abnormal behavior.
+    }
+
+    /**
+     * Request a domain for normal call.
+     *
+     * @param attr The attributes required to determine the domain.
+     * @param callback A callback to receive the response.
+     * @return A {@link CompletableFuture} callback to receive the result.
+     */
+    public CompletableFuture<Integer> createNormalConnection(
+            @NonNull DomainSelectionService.SelectionAttributes attr,
+            @NonNull DomainSelectionConnectionCallback callback) {
+        mCallback = callback;
+        selectDomain(attr);
+        return getCompletableFuture();
+    }
+
+    /**
+     * Returns the attributes required to determine the domain for a normal call.
+     *
+     * @param slotId The slot identifier.
+     * @param subId The subscription identifier.
+     * @param callId The call identifier.
+     * @param number The dialed number.
+     * @param isVideoCall flag for video call.
+     * @param callFailCause The reason why the last CS attempt failed.
+     * @param imsReasonInfo The reason why the last PS attempt failed.
+     * @return The attributes required to determine the domain.
+     */
+    public static @NonNull DomainSelectionService.SelectionAttributes getSelectionAttributes(
+            int slotId, int subId, @NonNull String callId, @NonNull String number,
+            boolean isVideoCall, int callFailCause, @Nullable ImsReasonInfo imsReasonInfo) {
+
+        DomainSelectionService.SelectionAttributes.Builder builder =
+                new DomainSelectionService.SelectionAttributes.Builder(
+                        slotId, subId, SELECTOR_TYPE_CALLING)
+                        .setEmergency(false)
+                        .setCallId(callId)
+                        .setNumber(number)
+                        .setCsDisconnectCause(callFailCause)
+                        .setVideoCall(isVideoCall);
+
+        if (imsReasonInfo != null) {
+            builder.setPsDisconnectCause(imsReasonInfo);
+        }
+        return builder.build();
+    }
+}
diff --git a/src/java/com/android/internal/telephony/domainselection/SmsDomainSelectionConnection.java b/src/java/com/android/internal/telephony/domainselection/SmsDomainSelectionConnection.java
new file mode 100644
index 0000000..36a7b17
--- /dev/null
+++ b/src/java/com/android/internal/telephony/domainselection/SmsDomainSelectionConnection.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import static android.telephony.DomainSelectionService.SELECTOR_TYPE_SMS;
+
+import android.annotation.NonNull;
+import android.telephony.Annotation.DisconnectCauses;
+import android.telephony.DomainSelectionService;
+import android.telephony.NetworkRegistrationInfo;
+
+import com.android.internal.telephony.Phone;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Manages the information of request and the callback binder for SMS.
+ */
+public class SmsDomainSelectionConnection extends DomainSelectionConnection {
+    private DomainSelectionConnectionCallback mCallback;
+
+    public SmsDomainSelectionConnection(Phone phone, DomainSelectionController controller) {
+        this(phone, controller, false);
+        mTag = "DomainSelectionConnection-SMS";
+    }
+
+    protected SmsDomainSelectionConnection(Phone phone, DomainSelectionController controller,
+            boolean isEmergency) {
+        super(phone, SELECTOR_TYPE_SMS, isEmergency, controller);
+    }
+
+    @Override
+    public void onWlanSelected() {
+        super.onDomainSelected(NetworkRegistrationInfo.DOMAIN_PS);
+    }
+
+    @Override
+    public void onSelectionTerminated(@DisconnectCauses int cause) {
+        if (mCallback != null) mCallback.onSelectionTerminated(cause);
+    }
+
+    @Override
+    public void finishSelection() {
+        CompletableFuture<Integer> future = getCompletableFuture();
+
+        if (future != null && !future.isDone()) {
+            cancelSelection();
+        } else {
+            super.finishSelection();
+        }
+    }
+
+    /**
+     * Requests a domain selection for SMS.
+     *
+     * @param attr The attributes required to determine the domain.
+     * @param callback A callback to notify an error of the domain selection.
+     * @return A {@link CompletableFuture} to get the selected domain
+     *         {@link NetworkRegistrationInfo#DOMAIN_PS} or
+     *         {@link NetworkRegistrationInfo#DOMAIN_CS}.
+     */
+    public @NonNull CompletableFuture<Integer> requestDomainSelection(
+            @NonNull DomainSelectionService.SelectionAttributes attr,
+            @NonNull DomainSelectionConnectionCallback callback) {
+        mCallback = callback;
+        selectDomain(attr);
+        return getCompletableFuture();
+    }
+}
diff --git a/src/java/com/android/internal/telephony/emergency/EmergencyConstants.java b/src/java/com/android/internal/telephony/emergency/EmergencyConstants.java
new file mode 100644
index 0000000..6caf5ab
--- /dev/null
+++ b/src/java/com/android/internal/telephony/emergency/EmergencyConstants.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.emergency;
+
+import android.annotation.IntDef;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * Define the constants for emergency call domain selection.
+ */
+public class EmergencyConstants {
+
+    @Retention(RetentionPolicy.SOURCE)
+    @IntDef(prefix = {"MODE_EMERGENCY_"},
+            value = {
+                    MODE_EMERGENCY_NONE,
+                    MODE_EMERGENCY_WWAN,
+                    MODE_EMERGENCY_WLAN,
+                    MODE_EMERGENCY_CALLBACK,
+            })
+    public @interface EmergencyMode {}
+
+    /**
+     * Default value.
+     */
+    public static final int MODE_EMERGENCY_NONE = 0;
+    /**
+     * Mode Type Emergency WWAN, indicates that the current domain selected for the Emergency call
+     * is cellular.
+     */
+    public static final int MODE_EMERGENCY_WWAN = 1;
+    /**
+     * Mode Type Emergency WLAN, indicates that the current domain selected for the Emergency call
+     * is WLAN/WIFI.
+     */
+    public static final int MODE_EMERGENCY_WLAN = 2;
+    /**
+     * Mode Type Emergency Callback, indicates that the current mode set request is for Emergency
+     * callback.
+     */
+    public static final int MODE_EMERGENCY_CALLBACK = 3;
+
+    /** Converts the {@link EmergencyMode} to String */
+    public static String emergencyModeToString(int emcMode) {
+        switch (emcMode) {
+            case MODE_EMERGENCY_NONE: return "NONE";
+            case MODE_EMERGENCY_WWAN: return "WWAN";
+            case MODE_EMERGENCY_WLAN: return "WLAN";
+            case MODE_EMERGENCY_CALLBACK: return "CALLBACK";
+            default: return "UNKNOWN(" + emcMode + ")";
+        }
+    }
+}
diff --git a/src/java/com/android/internal/telephony/emergency/EmergencyNumberTracker.java b/src/java/com/android/internal/telephony/emergency/EmergencyNumberTracker.java
index 276d82a..011a51f 100644
--- a/src/java/com/android/internal/telephony/emergency/EmergencyNumberTracker.java
+++ b/src/java/com/android/internal/telephony/emergency/EmergencyNumberTracker.java
@@ -16,10 +16,14 @@
 
 package com.android.internal.telephony.emergency;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_VOICE;
+
+import android.annotation.NonNull;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.res.Resources;
 import android.os.AsyncResult;
 import android.os.Environment;
 import android.os.Handler;
@@ -28,6 +32,7 @@
 import android.os.PersistableBundle;
 import android.os.SystemProperties;
 import android.telephony.CarrierConfigManager;
+import android.telephony.CellIdentity;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.ServiceState;
 import android.telephony.SubscriptionManager;
@@ -36,6 +41,8 @@
 import android.telephony.emergency.EmergencyNumber.EmergencyCallRouting;
 import android.telephony.emergency.EmergencyNumber.EmergencyServiceCategories;
 import android.text.TextUtils;
+import android.util.ArrayMap;
+import android.util.ArraySet;
 import android.util.LocalLog;
 
 import com.android.internal.annotations.VisibleForTesting;
@@ -48,6 +55,7 @@
 import com.android.internal.telephony.ServiceStateTracker;
 import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.metrics.TelephonyMetrics;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.phone.ecc.nano.ProtobufEccData;
 import com.android.phone.ecc.nano.ProtobufEccData.EccInfo;
@@ -67,6 +75,9 @@
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
 import java.util.zip.GZIPInputStream;
 
 /**
@@ -95,9 +106,18 @@
 
     private final CommandsInterface mCi;
     private final Phone mPhone;
+    private int mPhoneId;
     private String mCountryIso;
     private String mLastKnownEmergencyCountryIso = "";
     private int mCurrentDatabaseVersion = INVALID_DATABASE_VERSION;
+    private boolean mIsHalVersionLessThan1Dot4 = false;
+    private Resources mResources = null;
+    /**
+     * Used for storing all specific mnc's along with the list of emergency numbers
+     * for which normal routing should be supported.
+     */
+    private Map<String, Set<String>> mNormalRoutedNumbers = new ArrayMap<>();
+
     /**
      * Indicates if the country iso is set by another subscription.
      * @hide
@@ -164,8 +184,10 @@
     public EmergencyNumberTracker(Phone phone, CommandsInterface ci) {
         mPhone = phone;
         mCi = ci;
+        mResources = mPhone.getContext().getResources();
 
         if (mPhone != null) {
+            mPhoneId = phone.getPhoneId();
             CarrierConfigManager configMgr = (CarrierConfigManager)
                     mPhone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
             if (configMgr != null) {
@@ -185,6 +207,9 @@
             filter.addAction(TelephonyManager.ACTION_NETWORK_COUNTRY_CHANGED);
 
             mPhone.getContext().registerReceiver(mIntentReceiver, filter);
+
+            mIsHalVersionLessThan1Dot4 = mPhone.getHalVersion(HAL_SERVICE_VOICE)
+                                                 .lessOrEqual(new HalVersion(1, 3));
         } else {
             loge("mPhone is null.");
         }
@@ -269,7 +294,12 @@
     @VisibleForTesting
     public boolean isSimAbsent() {
         for (Phone phone: PhoneFactory.getPhones()) {
-            int slotId = SubscriptionController.getInstance().getSlotIndex(phone.getSubId());
+            int slotId;
+            if (phone.isSubscriptionManagerServiceEnabled()) {
+                slotId = SubscriptionManagerService.getInstance().getSlotIndex(phone.getSubId());
+            } else {
+                slotId = SubscriptionController.getInstance().getSlotIndex(phone.getSubId());
+            }
             // If slot id is invalid, it means that there is no sim card.
             if (slotId != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
                 // If there is at least one sim active, sim is not absent; it returns false
@@ -284,7 +314,7 @@
         // If country iso has been cached when listener is set, don't need to cache the initial
         // country iso and initial database.
         if (mCountryIso == null) {
-            String countryForDatabaseCache = getInitialCountryIso().toLowerCase();
+            String countryForDatabaseCache = getInitialCountryIso().toLowerCase(Locale.ROOT);
             updateEmergencyCountryIso(countryForDatabaseCache);
             // Use the last known country to cache the database in APM
             if (TextUtils.isEmpty(countryForDatabaseCache)
@@ -404,7 +434,8 @@
                 EVENT_OVERRIDE_OTA_EMERGENCY_NUMBER_DB_FILE_PATH, null).sendToTarget();
     }
 
-    private EmergencyNumber convertEmergencyNumberFromEccInfo(EccInfo eccInfo, String countryIso) {
+    private EmergencyNumber convertEmergencyNumberFromEccInfo(EccInfo eccInfo, String countryIso,
+            int emergencyCallRouting) {
         String phoneNumber = eccInfo.phoneNumber.trim();
         if (phoneNumber.isEmpty()) {
             loge("EccInfo has empty phone number.");
@@ -445,13 +476,65 @@
                     // Ignores unknown types.
             }
         }
-        return new EmergencyNumber(phoneNumber, countryIso, "", emergencyServiceCategoryBitmask,
-                new ArrayList<String>(), EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
-                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+        return new EmergencyNumber(phoneNumber, countryIso, "",
+                emergencyServiceCategoryBitmask, new ArrayList<String>(),
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE, emergencyCallRouting);
+    }
+
+    /**
+     * Get routing type of emergency numbers from DB. Update mnc's list with numbers that are
+     * to supported as normal routing type in the respective mnc's.
+     */
+    private int getRoutingInfoFromDB(EccInfo eccInfo,
+            Map<String, Set<String>> normalRoutedNumbers) {
+        int emergencyCallRouting;
+        switch(eccInfo.routing)
+        {
+            case EccInfo.Routing.NORMAL :
+                emergencyCallRouting = EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL;
+                break;
+            case EccInfo.Routing.EMERGENCY :
+                emergencyCallRouting = EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY;
+                break;
+            default:
+                emergencyCallRouting = EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN;
+        }
+        String phoneNumber = eccInfo.phoneNumber.trim();
+        if (phoneNumber.isEmpty()) {
+            loge("EccInfo has empty phone number.");
+            return emergencyCallRouting;
+        }
+
+        if (eccInfo.routing == EccInfo.Routing.NORMAL) {
+            emergencyCallRouting = EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL;
+
+            if (((eccInfo.normalRoutingMncs).length != 0)
+                    && (eccInfo.normalRoutingMncs[0].length() > 0)) {
+                emergencyCallRouting = EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN;
+
+                for (String routingMnc : eccInfo.normalRoutingMncs) {
+                    boolean mncExist = normalRoutedNumbers.containsKey(routingMnc);
+                    Set phoneNumberList;
+                    if (!mncExist) {
+                        phoneNumberList = new ArraySet<String>();
+                        phoneNumberList.add(phoneNumber);
+                        normalRoutedNumbers.put(routingMnc, phoneNumberList);
+                    } else {
+                        phoneNumberList = normalRoutedNumbers.get(routingMnc);
+                        if (!phoneNumberList.contains(phoneNumber)) {
+                            phoneNumberList.add(phoneNumber);
+                        }
+                    }
+                }
+                logd("Normal routed mncs with phoneNumbers:" + normalRoutedNumbers);
+            }
+        }
+        return emergencyCallRouting;
     }
 
     private void cacheEmergencyDatabaseByCountry(String countryIso) {
         int assetsDatabaseVersion;
+        Map<String, Set<String>> assetNormalRoutedNumbers = new ArrayMap<>();
 
         // Read the Asset emergency number database
         List<EmergencyNumber> updatedAssetEmergencyNumberList = new ArrayList<>();
@@ -465,10 +548,15 @@
             logd(countryIso + " asset emergency database is loaded. Ver: " + assetsDatabaseVersion
                     + " Phone Id: " + mPhone.getPhoneId());
             for (ProtobufEccData.CountryInfo countryEccInfo : allEccMessages.countries) {
-                if (countryEccInfo.isoCode.equals(countryIso.toUpperCase())) {
+                if (countryEccInfo.isoCode.equals(countryIso.toUpperCase(Locale.ROOT))) {
                     for (ProtobufEccData.EccInfo eccInfo : countryEccInfo.eccs) {
+                        int emergencyCallRouting = EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN;
+                        if (!shouldEmergencyNumberRoutingFromDbBeIgnored()) {
+                            emergencyCallRouting = getRoutingInfoFromDB(eccInfo,
+                                    assetNormalRoutedNumbers);
+                        }
                         updatedAssetEmergencyNumberList.add(convertEmergencyNumberFromEccInfo(
-                                eccInfo, countryIso));
+                                eccInfo, countryIso, emergencyCallRouting));
                     }
                 }
             }
@@ -489,6 +577,8 @@
             logd("Using Asset Emergency database. Version: " + assetsDatabaseVersion);
             mCurrentDatabaseVersion = assetsDatabaseVersion;
             mEmergencyNumberListFromDatabase = updatedAssetEmergencyNumberList;
+            mNormalRoutedNumbers.clear();
+            mNormalRoutedNumbers = assetNormalRoutedNumbers;
         } else {
             logd("Using Ota Emergency database. Version: " + otaDatabaseVersion);
         }
@@ -497,6 +587,7 @@
     private int cacheOtaEmergencyNumberDatabase() {
         ProtobufEccData.AllInfo allEccMessages = null;
         int otaDatabaseVersion = INVALID_DATABASE_VERSION;
+        Map<String, Set<String>> otaNormalRoutedNumbers = new ArrayMap<>();
 
         // Read the OTA emergency number database
         List<EmergencyNumber> updatedOtaEmergencyNumberList = new ArrayList<>();
@@ -525,10 +616,15 @@
             logd(countryIso + " ota emergency database is loaded. Ver: " + otaDatabaseVersion);
             otaDatabaseVersion = allEccMessages.revision;
             for (ProtobufEccData.CountryInfo countryEccInfo : allEccMessages.countries) {
-                if (countryEccInfo.isoCode.equals(countryIso.toUpperCase())) {
+                if (countryEccInfo.isoCode.equals(countryIso.toUpperCase(Locale.ROOT))) {
                     for (ProtobufEccData.EccInfo eccInfo : countryEccInfo.eccs) {
+                        int emergencyCallRouting = EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN;
+                        if (!shouldEmergencyNumberRoutingFromDbBeIgnored()) {
+                            emergencyCallRouting = getRoutingInfoFromDB(eccInfo,
+                                    otaNormalRoutedNumbers);
+                        }
                         updatedOtaEmergencyNumberList.add(convertEmergencyNumberFromEccInfo(
-                                eccInfo, countryIso));
+                                eccInfo, countryIso, emergencyCallRouting));
                     }
                 }
             }
@@ -543,6 +639,8 @@
                 && mCurrentDatabaseVersion < otaDatabaseVersion) {
             mCurrentDatabaseVersion = otaDatabaseVersion;
             mEmergencyNumberListFromDatabase = updatedOtaEmergencyNumberList;
+            mNormalRoutedNumbers.clear();
+            mNormalRoutedNumbers = otaNormalRoutedNumbers;
         }
         return otaDatabaseVersion;
     }
@@ -591,7 +689,7 @@
     private void updateEmergencyNumberListDatabaseAndNotify(String countryIso) {
         logd("updateEmergencyNumberListDatabaseAndNotify(): receiving countryIso: "
                 + countryIso);
-        updateEmergencyCountryIso(countryIso.toLowerCase());
+        updateEmergencyCountryIso(countryIso.toLowerCase(Locale.ROOT));
         // Use cached country iso in APM to load emergency number database.
         if (TextUtils.isEmpty(countryIso) && isAirplaneModeEnabled()) {
             countryIso = getCountryIsoForCachingDatabase();
@@ -687,7 +785,11 @@
         }
         mergedEmergencyNumberList.addAll(mEmergencyNumberListWithPrefix);
         mergedEmergencyNumberList.addAll(mEmergencyNumberListFromTestMode);
-        EmergencyNumber.mergeSameNumbersInEmergencyNumberList(mergedEmergencyNumberList);
+        if (shouldDeterminingOfUrnsAndCategoriesWhileMergingIgnored()) {
+            EmergencyNumber.mergeSameNumbersInEmergencyNumberList(mergedEmergencyNumberList);
+        } else {
+            EmergencyNumber.mergeSameNumbersInEmergencyNumberList(mergedEmergencyNumberList, true);
+        }
         mEmergencyNumberList = mergedEmergencyNumberList;
     }
 
@@ -698,11 +800,90 @@
      *         indication not support from the HAL.
      */
     public List<EmergencyNumber> getEmergencyNumberList() {
+        List<EmergencyNumber> completeEmergencyNumberList;
         if (!mEmergencyNumberListFromRadio.isEmpty()) {
-            return Collections.unmodifiableList(mEmergencyNumberList);
+            completeEmergencyNumberList = Collections.unmodifiableList(mEmergencyNumberList);
         } else {
-            return getEmergencyNumberListFromEccListDatabaseAndTest();
+            completeEmergencyNumberList = getEmergencyNumberListFromEccListDatabaseAndTest();
         }
+        if (shouldAdjustForRouting()) {
+            return adjustRoutingForEmergencyNumbers(completeEmergencyNumberList);
+        } else {
+            return completeEmergencyNumberList;
+        }
+    }
+
+    /**
+     * Util function to check whether routing type and mnc value in emergency number needs
+     * to be adjusted for the current network mnc.
+     */
+    private boolean shouldAdjustForRouting() {
+        if (!shouldEmergencyNumberRoutingFromDbBeIgnored() && !mNormalRoutedNumbers.isEmpty()) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Adjust emergency numbers with mnc and routing type based on the current network mnc.
+     */
+    private List<EmergencyNumber> adjustRoutingForEmergencyNumbers(
+            List<EmergencyNumber> emergencyNumbers) {
+        CellIdentity cellIdentity = mPhone.getCurrentCellIdentity();
+        if (cellIdentity != null) {
+            String networkMnc = cellIdentity.getMncString();
+            Set<String> normalRoutedPhoneNumbers = mNormalRoutedNumbers.get(networkMnc);
+            Set<String> normalRoutedPhoneNumbersWithPrefix = new ArraySet<String>();
+
+            if (normalRoutedPhoneNumbers != null && !normalRoutedPhoneNumbers.isEmpty()) {
+                for (String num : normalRoutedPhoneNumbers) {
+                    Set<String> phoneNumbersWithPrefix = addPrefixToEmergencyNumber(num);
+                    if (phoneNumbersWithPrefix != null && !phoneNumbersWithPrefix.isEmpty()) {
+                        normalRoutedPhoneNumbersWithPrefix.addAll(phoneNumbersWithPrefix);
+                    }
+                }
+            }
+            List<EmergencyNumber> adjustedEmergencyNumberList = new ArrayList<>();
+            int routing;
+            String mnc;
+            for (EmergencyNumber num : emergencyNumbers) {
+                routing = num.getEmergencyCallRouting();
+                mnc = num.getMnc();
+                if (num.isFromSources(EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE)) {
+                    if ((normalRoutedPhoneNumbers != null
+                            && normalRoutedPhoneNumbers.contains(num.getNumber()))
+                            || normalRoutedPhoneNumbersWithPrefix.contains(num.getNumber())) {
+                        routing = EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL;
+                        mnc = networkMnc;
+                        logd("adjustRoutingForEmergencyNumbers for number" + num.getNumber());
+                    } else if (routing == EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN) {
+                        routing = EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY;
+                    }
+                }
+                adjustedEmergencyNumberList.add(new EmergencyNumber(num.getNumber(),
+                        num.getCountryIso(), mnc,
+                        num.getEmergencyServiceCategoryBitmask(),
+                        num.getEmergencyUrns(), num.getEmergencyNumberSourceBitmask(),
+                        routing));
+            }
+            return adjustedEmergencyNumberList;
+        } else {
+            return emergencyNumbers;
+        }
+    }
+
+
+    /**
+     * Util function to add prefix to the given emergency number.
+     */
+    private Set<String> addPrefixToEmergencyNumber(String number) {
+        Set<String> phoneNumbersWithPrefix = new ArraySet<String>();
+        for (String prefix : mEmergencyNumberPrefix) {
+            if (!number.startsWith(prefix)) {
+                phoneNumbersWithPrefix.add(prefix + number);
+            }
+        }
+        return phoneNumbersWithPrefix;
     }
 
     /**
@@ -710,7 +891,7 @@
      *
      * @return {@code true} if it is; {@code false} otherwise.
      */
-    public boolean isEmergencyNumber(String number, boolean exactMatch) {
+    public boolean isEmergencyNumber(String number) {
         if (number == null) {
             return false;
         }
@@ -726,31 +907,14 @@
 
         if (!mEmergencyNumberListFromRadio.isEmpty()) {
             for (EmergencyNumber num : mEmergencyNumberList) {
-                // According to com.android.i18n.phonenumbers.ShortNumberInfo, in
-                // these countries, if extra digits are added to an emergency number,
-                // it no longer connects to the emergency service.
-                String countryIso = getLastKnownEmergencyCountryIso();
-                if (countryIso.equals("br") || countryIso.equals("cl")
-                        || countryIso.equals("ni")) {
-                    exactMatch = true;
-                } else {
-                    exactMatch = false || exactMatch;
-                }
-                if (exactMatch) {
-                    if (num.getNumber().equals(number)) {
-                        logd("Found in mEmergencyNumberList [exact match] ");
-                        return true;
-                    }
-                } else {
-                    if (number.startsWith(num.getNumber())) {
-                        logd("Found in mEmergencyNumberList [not exact match] ");
-                        return true;
-                    }
+                if (num.getNumber().equals(number)) {
+                    logd("Found in mEmergencyNumberList");
+                    return true;
                 }
             }
             return false;
         } else {
-            boolean inEccList = isEmergencyNumberFromEccList(number, exactMatch);
+            boolean inEccList = isEmergencyNumberFromEccList(number);
             boolean inEmergencyNumberDb = isEmergencyNumberFromDatabase(number);
             boolean inEmergencyNumberTestList = isEmergencyNumberForTest(number);
             logd("Search results - inRilEccList:" + inEccList
@@ -855,22 +1019,11 @@
      */
     private List<EmergencyNumber> getEmergencyNumberListFromEccList() {
         List<EmergencyNumber> emergencyNumberList = new ArrayList<>();
-        int slotId = SubscriptionController.getInstance().getSlotIndex(mPhone.getSubId());
 
-        String ecclist = (slotId <= 0) ? "ril.ecclist" : ("ril.ecclist" + slotId);
-        String emergencyNumbers = SystemProperties.get(ecclist, "");
-        if (TextUtils.isEmpty(emergencyNumbers)) {
-            // then read-only ecclist property since old RIL only uses this
-            emergencyNumbers = SystemProperties.get("ro.ril.ecclist");
+        if (mIsHalVersionLessThan1Dot4) {
+            emergencyNumberList.addAll(getEmergencyNumberListFromEccListForHalv1_3());
         }
-        if (!TextUtils.isEmpty(emergencyNumbers)) {
-            // searches through the comma-separated list for a match,
-            // return true if one is found.
-            for (String emergencyNum : emergencyNumbers.split(",")) {
-                emergencyNumberList.add(getLabeledEmergencyNumberForEcclist(emergencyNum));
-            }
-        }
-        emergencyNumbers = ((isSimAbsent()) ? "112,911,000,08,110,118,119,999" : "112,911");
+        String emergencyNumbers = ((isSimAbsent()) ? "112,911,000,08,110,118,119,999" : "112,911");
         for (String emergencyNum : emergencyNumbers.split(",")) {
             emergencyNumberList.add(getLabeledEmergencyNumberForEcclist(emergencyNum));
         }
@@ -881,20 +1034,50 @@
         return emergencyNumberList;
     }
 
+    private String getEmergencyNumberListForHalv1_3() {
+        int slotId;
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            slotId = SubscriptionManagerService.getInstance().getSlotIndex(mPhone.getSubId());
+        } else {
+            slotId = SubscriptionController.getInstance().getSlotIndex(mPhone.getSubId());
+        }
+
+        String ecclist = (slotId <= 0) ? "ril.ecclist" : ("ril.ecclist" + slotId);
+        String emergencyNumbers = SystemProperties.get(ecclist, "");
+
+        if (TextUtils.isEmpty(emergencyNumbers)) {
+            // then read-only ecclist property since old RIL only uses this
+            emergencyNumbers = SystemProperties.get("ro.ril.ecclist");
+        }
+        logd(ecclist + " emergencyNumbers: " + emergencyNumbers);
+        return emergencyNumbers;
+    }
+
+    private List<EmergencyNumber> getEmergencyNumberListFromEccListForHalv1_3() {
+        List<EmergencyNumber> emergencyNumberList = new ArrayList<>();
+        String emergencyNumbers = getEmergencyNumberListForHalv1_3();
+
+        if (!TextUtils.isEmpty(emergencyNumbers)) {
+            for (String emergencyNum : emergencyNumbers.split(",")) {
+                emergencyNumberList.add(getLabeledEmergencyNumberForEcclist(emergencyNum));
+            }
+        }
+        return emergencyNumberList;
+    }
+
     private List<EmergencyNumber> getEmergencyNumberListWithPrefix(
             List<EmergencyNumber> emergencyNumberList) {
         List<EmergencyNumber> emergencyNumberListWithPrefix = new ArrayList<>();
         if (emergencyNumberList != null) {
             for (EmergencyNumber num : emergencyNumberList) {
-                for (String prefix : mEmergencyNumberPrefix) {
-                    // If an emergency number has started with the prefix,
-                    // no need to apply the prefix.
-                    if (!num.getNumber().startsWith(prefix)) {
+                Set<String> phoneNumbersWithPrefix = addPrefixToEmergencyNumber(num.getNumber());
+                if (phoneNumbersWithPrefix != null && !phoneNumbersWithPrefix.isEmpty()) {
+                    for (String numberWithPrefix : phoneNumbersWithPrefix) {
                         emergencyNumberListWithPrefix.add(new EmergencyNumber(
-                            prefix + num.getNumber(), num.getCountryIso(),
-                            num.getMnc(), num.getEmergencyServiceCategoryBitmask(),
-                            num.getEmergencyUrns(), num.getEmergencyNumberSourceBitmask(),
-                            num.getEmergencyCallRouting()));
+                                numberWithPrefix, num.getCountryIso(),
+                                num.getMnc(), num.getEmergencyServiceCategoryBitmask(),
+                                num.getEmergencyUrns(), num.getEmergencyNumberSourceBitmask(),
+                                num.getEmergencyCallRouting()));
                     }
                 }
             }
@@ -913,7 +1096,7 @@
     }
 
     private boolean isEmergencyNumberFromDatabase(String number) {
-        if (!mPhone.getHalVersion().greaterOrEqual(new HalVersion(1, 4))) {
+        if (mEmergencyNumberListFromDatabase.isEmpty()) {
             return false;
         }
         number = PhoneNumberUtils.stripSeparators(number);
@@ -936,10 +1119,10 @@
         number = PhoneNumberUtils.stripSeparators(number);
         for (EmergencyNumber num : mEmergencyNumberListFromDatabase) {
             if (num.getNumber().equals(number)) {
-                return new EmergencyNumber(number, getLastKnownEmergencyCountryIso().toLowerCase(),
-                        "", num.getEmergencyServiceCategoryBitmask(),
+                return new EmergencyNumber(number, getLastKnownEmergencyCountryIso()
+                        .toLowerCase(Locale.ROOT), "", num.getEmergencyServiceCategoryBitmask(),
                         new ArrayList<String>(), EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
-                        EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+                        num.getEmergencyCallRouting());
             }
         }
         return new EmergencyNumber(number, "", "",
@@ -952,7 +1135,7 @@
      * Back-up old logics for {@link PhoneNumberUtils#isEmergencyNumberInternal} for legacy
      * and deprecate purpose.
      */
-    private boolean isEmergencyNumberFromEccList(String number, boolean useExactMatch) {
+    private boolean isEmergencyNumberFromEccList(String number) {
         // If the number passed in is null, just return false:
         if (number == null) return false;
 
@@ -976,57 +1159,14 @@
         /// @}
 
         String emergencyNumbers = "";
-        int slotId = SubscriptionController.getInstance().getSlotIndex(mPhone.getSubId());
-
-        String ecclist = null;
         String countryIso = getLastKnownEmergencyCountryIso();
+        logd("country:" + countryIso);
 
-        if (!mPhone.getHalVersion().greaterOrEqual(new HalVersion(1, 4))) {
-            //only use ril ecc list for older devices with HAL < 1.4
-            // check read-write ecclist property first
-            ecclist = (slotId <= 0) ? "ril.ecclist" : ("ril.ecclist" + slotId);
-            emergencyNumbers = SystemProperties.get(ecclist, "");
-
-            logd("slotId:" + slotId + " country:" + countryIso + " emergencyNumbers: "
-                + emergencyNumbers);
-
-            if (TextUtils.isEmpty(emergencyNumbers)) {
-                // then read-only ecclist property since old RIL only uses this
-                emergencyNumbers = SystemProperties.get("ro.ril.ecclist");
-            }
+        if (mIsHalVersionLessThan1Dot4) {
+            emergencyNumbers = getEmergencyNumberListForHalv1_3();
 
             if (!TextUtils.isEmpty(emergencyNumbers)) {
-                // searches through the comma-separated list for a match,
-                // return true if one is found.
-                for (String emergencyNum : emergencyNumbers.split(",")) {
-                    // According to com.android.i18n.phonenumbers.ShortNumberInfo, in
-                    // these countries, if extra digits are added to an emergency number,
-                    // it no longer connects to the emergency service.
-                    if (useExactMatch || countryIso.equals("br") || countryIso.equals("cl")
-                        || countryIso.equals("ni")) {
-                        if (number.equals(emergencyNum)) {
-                            return true;
-                        } else {
-                            for (String prefix : mEmergencyNumberPrefix) {
-                                if (number.equals(prefix + emergencyNum)) {
-                                    return true;
-                                }
-                            }
-                        }
-                    } else {
-                        if (number.startsWith(emergencyNum)) {
-                            return true;
-                        } else {
-                            for (String prefix : mEmergencyNumberPrefix) {
-                                if (number.startsWith(prefix + emergencyNum)) {
-                                    return true;
-                                }
-                            }
-                        }
-                    }
-                }
-                // no matches found against the list!
-                return false;
+                return isEmergencyNumberFromEccListForHalv1_3(number, emergencyNumbers);
             }
         }
 
@@ -1038,63 +1178,57 @@
         emergencyNumbers = ((isSimAbsent()) ? "112,911,000,08,110,118,119,999" : "112,911");
 
         for (String emergencyNum : emergencyNumbers.split(",")) {
-            if (useExactMatch) {
-                if (number.equals(emergencyNum)) {
-                    return true;
-                } else {
-                    for (String prefix : mEmergencyNumberPrefix) {
-                        if (number.equals(prefix + emergencyNum)) {
-                            return true;
-                        }
-                    }
-                }
+            if (number.equals(emergencyNum)) {
+                return true;
             } else {
-                if (number.startsWith(emergencyNum)) {
-                    return true;
-                } else {
-                    for (String prefix : mEmergencyNumberPrefix) {
-                        if (number.equals(prefix + emergencyNum)) {
-                            return true;
-                        }
+                for (String prefix : mEmergencyNumberPrefix) {
+                    if (number.equals(prefix + emergencyNum)) {
+                        return true;
                     }
                 }
             }
         }
 
-        if(isSimAbsent()) {
+        if (isSimAbsent()) {
             // No ecclist system property, so use our own list.
             if (countryIso != null) {
                 ShortNumberInfo info = ShortNumberInfo.getInstance();
-                if (useExactMatch) {
-                    if (info.isEmergencyNumber(number, countryIso.toUpperCase())) {
-                        return true;
-                    } else {
-                        for (String prefix : mEmergencyNumberPrefix) {
-                            if (info.isEmergencyNumber(prefix + number, countryIso.toUpperCase())) {
-                                return true;
-                            }
-                        }
-                    }
-                    return false;
+                if (info.isEmergencyNumber(number, countryIso.toUpperCase(Locale.ROOT))) {
+                    return true;
                 } else {
-                    if (info.connectsToEmergencyNumber(number, countryIso.toUpperCase())) {
-                        return true;
-                    } else {
-                        for (String prefix : mEmergencyNumberPrefix) {
-                            if (info.connectsToEmergencyNumber(prefix + number,
-                                    countryIso.toUpperCase())) {
-                                return true;
-                            }
+                    for (String prefix : mEmergencyNumberPrefix) {
+                        if (info.isEmergencyNumber(prefix + number,
+                                countryIso.toUpperCase(Locale.ROOT))) {
+                            return true;
                         }
                     }
-                    return false;
                 }
+                return false;
             }
         }
 
         return false;
     }
 
+    private boolean isEmergencyNumberFromEccListForHalv1_3(@NonNull String number,
+            @NonNull String emergencyNumbers) {
+        // searches through the comma-separated list for a match,
+        // return true if one is found.
+        for (String emergencyNum : emergencyNumbers.split(",")) {
+            if (number.equals(emergencyNum)) {
+                return true;
+            } else {
+                for (String prefix : mEmergencyNumberPrefix) {
+                    if (number.equals(prefix + emergencyNum)) {
+                        return true;
+                    }
+                }
+            }
+        }
+        // no matches found against the list!
+        return false;
+    }
+
     /**
      * Execute command for updating emergency number for test mode.
      */
@@ -1107,7 +1241,7 @@
      */
     private void updateEmergencyNumberListTestModeAndNotify(int action, EmergencyNumber num) {
         if (action == ADD_EMERGENCY_NUMBER_TEST_MODE) {
-            if (!isEmergencyNumber(num.getNumber(), true)) {
+            if (!isEmergencyNumber(num.getNumber())) {
                 mEmergencyNumberListFromTestMode.add(num);
             }
         } else if (action == RESET_EMERGENCY_NUMBER_TEST_MODE) {
@@ -1134,7 +1268,7 @@
 
     private List<EmergencyNumber> getEmergencyNumberListFromEccListDatabaseAndTest() {
         List<EmergencyNumber> mergedEmergencyNumberList = getEmergencyNumberListFromEccList();
-        if (mPhone.getHalVersion().greaterOrEqual(new HalVersion(1, 4))) {
+        if (!mEmergencyNumberListFromDatabase.isEmpty()) {
             loge("getEmergencyNumberListFromEccListDatabaseAndTest: radio indication is"
                     + " unavailable in 1.4 HAL.");
             mergedEmergencyNumberList.addAll(mEmergencyNumberListFromDatabase);
@@ -1142,7 +1276,12 @@
                     mEmergencyNumberListFromDatabase));
         }
         mergedEmergencyNumberList.addAll(getEmergencyNumberListTestMode());
-        EmergencyNumber.mergeSameNumbersInEmergencyNumberList(mergedEmergencyNumberList);
+
+        if (shouldDeterminingOfUrnsAndCategoriesWhileMergingIgnored()) {
+            EmergencyNumber.mergeSameNumbersInEmergencyNumberList(mergedEmergencyNumberList);
+        } else {
+            EmergencyNumber.mergeSameNumbersInEmergencyNumberList(mergedEmergencyNumberList, true);
+        }
         return mergedEmergencyNumberList;
     }
 
@@ -1158,16 +1297,16 @@
         return new ArrayList<>(mEmergencyNumberListFromRadio);
     }
 
-    private static void logd(String str) {
-        Rlog.d(TAG, str);
+    private void logd(String str) {
+        Rlog.d(TAG, "[" + mPhoneId + "]" + str);
     }
 
-    private static void logw(String str) {
-        Rlog.w(TAG, str);
+    private void logw(String str) {
+        Rlog.w(TAG, "[" + mPhoneId + "]" + str);
     }
 
-    private static void loge(String str) {
-        Rlog.e(TAG, str);
+    private void loge(String str) {
+        Rlog.e(TAG, "[" + mPhoneId + "]" +  str);
     }
 
     private void writeUpdatedEmergencyNumberListMetrics(
@@ -1182,6 +1321,41 @@
     }
 
     /**
+     * @return {@code true} if emergency numbers sourced from modem/config should be ignored.
+     * {@code false} if emergency numbers sourced from modem/config should not be ignored.
+     */
+    @VisibleForTesting
+    public boolean shouldModemConfigEmergencyNumbersBeIgnored() {
+        return mResources.getBoolean(com.android.internal.R.bool
+                .ignore_modem_config_emergency_numbers);
+    }
+
+    /**
+     * @return {@code true} if emergency number routing from the android emergency number
+     * database should be ignored.
+     * {@code false} if emergency number routing from the android emergency number database
+     * should not be ignored.
+     */
+    @VisibleForTesting
+    public boolean shouldEmergencyNumberRoutingFromDbBeIgnored() {
+        return mResources.getBoolean(com.android.internal.R.bool
+                .ignore_emergency_number_routing_from_db);
+    }
+
+
+    /**
+     * @return {@code true} if determining of Urns & Service Categories while merging duplicate
+     * numbers should be ignored.
+     * {@code false} if determining of Urns & Service Categories while merging duplicate
+     * numbers should not be ignored.
+     */
+    @VisibleForTesting
+    public boolean shouldDeterminingOfUrnsAndCategoriesWhileMergingIgnored() {
+        // TODO: Device config
+        return false;
+    }
+
+    /**
      * Dump Emergency Number List info in the tracking
      *
      * @param fd FileDescriptor
@@ -1190,7 +1364,7 @@
      */
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, "  ");
-        ipw.println(" Hal Version:" + mPhone.getHalVersion());
+        ipw.println(" Hal Version:" + mPhone.getHalVersion(HAL_SERVICE_VOICE));
         ipw.println(" ========================================= ");
 
         ipw.println(" Country Iso:" + getEmergencyCountryIso());
@@ -1229,10 +1403,10 @@
         ipw.decreaseIndent();
         ipw.println(" ========================================= ");
 
-        int slotId = SubscriptionController.getInstance().getSlotIndex(mPhone.getSubId());
-        String ecclist = (slotId <= 0) ? "ril.ecclist" : ("ril.ecclist" + slotId);
-        ipw.println(" ril.ecclist: " + SystemProperties.get(ecclist, ""));
-        ipw.println(" ========================================= ");
+        if (mIsHalVersionLessThan1Dot4) {
+            getEmergencyNumberListForHalv1_3();
+            ipw.println(" ========================================= ");
+        }
 
         ipw.println("Emergency Number List for Phone" + "(" + mPhone.getPhoneId() + ")");
         ipw.increaseIndent();
diff --git a/src/java/com/android/internal/telephony/emergency/EmergencyStateTracker.java b/src/java/com/android/internal/telephony/emergency/EmergencyStateTracker.java
new file mode 100644
index 0000000..b4f4554
--- /dev/null
+++ b/src/java/com/android/internal/telephony/emergency/EmergencyStateTracker.java
@@ -0,0 +1,560 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.emergency;
+
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_NONE;
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_WWAN;
+
+import android.annotation.NonNull;
+import android.content.Context;
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.provider.Settings;
+import android.telephony.CarrierConfigManager;
+import android.telephony.DisconnectCause;
+import android.telephony.EmergencyRegResult;
+import android.telephony.ServiceState;
+import android.telephony.TelephonyManager;
+import android.telephony.emergency.EmergencyNumber;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.Call;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.PhoneFactory;
+import com.android.internal.telephony.RIL;
+import com.android.internal.telephony.data.PhoneSwitcher;
+import com.android.telephony.Rlog;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Consumer;
+
+/**
+ * Tracks the emergency call state and notifies listeners of changes to the emergency mode.
+ */
+public class EmergencyStateTracker {
+
+    private static final String TAG = "EmergencyStateTracker";
+
+    // Timeout before we continue with the emergency call without waiting for DDS switch response
+    // from the modem.
+    private static final int DEFAULT_DATA_SWITCH_TIMEOUT_MS = 1000;
+
+    private static EmergencyStateTracker INSTANCE = null;
+
+    private final Context mContext;
+    private final Handler mHandler;
+    private @EmergencyConstants.EmergencyMode int mEmergencyMode = MODE_EMERGENCY_NONE;
+    private Phone mPhone;
+    private RadioOnHelper mRadioOnHelper;
+    private CompletableFuture<Integer> mOnCompleted = null;
+    /** Tracks emergency calls by callId that have reached {@link Call.State.Active}.*/
+    private Set<String> mActiveEmergencyCalls = new HashSet();
+    private boolean mIsSuplDdsSwitchRequiredForEmergencyCall;
+    private EmergencyRegResult mLastEmergencyRegResult;
+    private boolean mIsInEmergencyCall;
+    private boolean mIsTestEmergencyNumber;
+
+    /** PhoneFactory Dependencies for testing. */
+    @VisibleForTesting
+    public interface PhoneFactoryProxy {
+        Phone[] getPhones();
+    }
+
+    private PhoneFactoryProxy mPhoneFactoryProxy = new PhoneFactoryProxy() {
+        @Override
+        public Phone[] getPhones() {
+            return PhoneFactory.getPhones();
+        }
+    };
+
+    /** PhoneSwitcher dependencies for testing. */
+    @VisibleForTesting
+    public interface PhoneSwitcherProxy {
+
+        PhoneSwitcher getPhoneSwitcher();
+    }
+
+    private PhoneSwitcherProxy mPhoneSwitcherProxy = new PhoneSwitcherProxy() {
+        @Override
+        public PhoneSwitcher getPhoneSwitcher() {
+            return PhoneSwitcher.getInstance();
+        }
+    };
+
+    /**
+     * TelephonyManager dependencies for testing.
+     */
+    @VisibleForTesting
+    public interface TelephonyManagerProxy {
+        int getPhoneCount();
+    }
+
+    private TelephonyManagerProxy mTelephonyManagerProxy;
+
+    private static class TelephonyManagerProxyImpl implements TelephonyManagerProxy {
+        private final TelephonyManager mTelephonyManager;
+
+
+        TelephonyManagerProxyImpl(Context context) {
+            mTelephonyManager = new TelephonyManager(context);
+        }
+
+        @Override
+        public int getPhoneCount() {
+            return mTelephonyManager.getPhoneCount();
+        }
+    }
+
+    /**
+     * Return the handler for testing.
+     */
+    @VisibleForTesting
+    public Handler getHandler() {
+        return mHandler;
+    }
+
+    @VisibleForTesting
+    public static final int MSG_SET_EMERGENCY_MODE_DONE = 1;
+    @VisibleForTesting
+    public static final int MSG_EXIT_EMERGENCY_MODE_DONE = 2;
+
+    private final class MyHandler extends Handler {
+
+        MyHandler(Looper looper) {
+            super(looper);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            AsyncResult ar;
+
+            switch (msg.what) {
+            case MSG_SET_EMERGENCY_MODE_DONE:
+                Rlog.v(TAG, "MSG_SET_EMERGENCY_MODE_DONE");
+                ar = (AsyncResult) msg.obj;
+                if (ar.exception == null) {
+                    mLastEmergencyRegResult = (EmergencyRegResult) ar.result;
+                } else {
+                    Rlog.w(TAG, "LastEmergencyRegResult not set. AsyncResult.exception: "
+                            + ar.exception);
+                }
+                setIsInEmergencyCall(true);
+                mOnCompleted.complete(DisconnectCause.NOT_DISCONNECTED);
+                break;
+
+            case MSG_EXIT_EMERGENCY_MODE_DONE:
+                Rlog.v(TAG, "MSG_EXIT_EMERGENCY_MODE_DONE");
+                setIsInEmergencyCall(false);
+                break;
+
+            default:
+                break;
+            }
+        }
+    }
+
+    /**
+     * Creates the EmergencyStateTracker singleton instance.
+     *
+     * @param context                                 The context of the application.
+     * @param isSuplDdsSwitchRequiredForEmergencyCall Whether gnss supl requires default data for
+     *                                                emergency call.
+     */
+    public static void make(Context context, boolean isSuplDdsSwitchRequiredForEmergencyCall) {
+        if (INSTANCE == null) {
+            INSTANCE = new EmergencyStateTracker(context, Looper.myLooper(),
+                    isSuplDdsSwitchRequiredForEmergencyCall);
+        }
+    }
+
+    /**
+     * Returns the singleton instance of EmergencyStateTracker.
+     *
+     * @return {@link EmergencyStateTracker} instance.
+     */
+    public static EmergencyStateTracker getInstance() {
+        if (INSTANCE == null) {
+            throw new IllegalStateException("EmergencyStateTracker is not ready!");
+        }
+        return INSTANCE;
+    }
+
+    /**
+     * Initializes EmergencyStateTracker.
+     */
+    private EmergencyStateTracker(Context context, Looper looper,
+            boolean isSuplDdsSwitchRequiredForEmergencyCall) {
+        mContext = context;
+        mHandler = new MyHandler(looper);
+        mIsSuplDdsSwitchRequiredForEmergencyCall = isSuplDdsSwitchRequiredForEmergencyCall;
+        mTelephonyManagerProxy = new TelephonyManagerProxyImpl(context);
+    }
+
+    /**
+     * Initializes EmergencyStateTracker with injections for testing.
+     *
+     * @param context                                 The context of the application.
+     * @param looper                                  The {@link Looper} of the application.
+     * @param isSuplDdsSwitchRequiredForEmergencyCall Whether gnss supl requires default data for
+     *                                                emergency call.
+     * @param phoneFactoryProxy                       The {@link PhoneFactoryProxy} to be injected.
+     * @param phoneSwitcherProxy                      The {@link PhoneSwitcherProxy} to be injected.
+     * @param telephonyManagerProxy                   The {@link TelephonyManagerProxy} to be
+     *                                                injected.
+     * @param radioOnHelper                           The {@link RadioOnHelper} to be injected.
+     */
+    @VisibleForTesting
+    public EmergencyStateTracker(Context context, Looper looper,
+            boolean isSuplDdsSwitchRequiredForEmergencyCall, PhoneFactoryProxy phoneFactoryProxy,
+            PhoneSwitcherProxy phoneSwitcherProxy, TelephonyManagerProxy telephonyManagerProxy,
+            RadioOnHelper radioOnHelper) {
+        mContext = context;
+        mHandler = new MyHandler(looper);
+        mIsSuplDdsSwitchRequiredForEmergencyCall = isSuplDdsSwitchRequiredForEmergencyCall;
+        mPhoneFactoryProxy = phoneFactoryProxy;
+        mPhoneSwitcherProxy = phoneSwitcherProxy;
+        mTelephonyManagerProxy = telephonyManagerProxy;
+        mRadioOnHelper = radioOnHelper;
+    }
+
+    /**
+     * Starts the process of an emergency call.
+     *
+     * <p>
+     * Handles turning on radio and switching DDS.
+     *
+     * @param phone                 the {@code Phone} on which to process the emergency call.
+     * @param callId                the call id on which to process the emergency call.
+     * @param isTestEmergencyNumber whether this is a test emergency number.
+     * @return a {@code CompletableFuture} that results in {@code DisconnectCause.NOT_DISCONNECTED}
+     *         if emergency call successfully started.
+     */
+    public CompletableFuture<Integer> startEmergencyCall(Phone phone, String callId,
+            boolean isTestEmergencyNumber) {
+        Rlog.i(TAG, "startEmergencyCall");
+
+        if (mPhone != null) {
+            Rlog.e(TAG, "startEmergencyCall failed. Existing emergency call in progress.");
+            // Create new future to return as to not interfere with any uncompleted futures.
+            CompletableFuture<Integer> future = new CompletableFuture<>();
+            future.complete(DisconnectCause.ERROR_UNSPECIFIED);
+            return future;
+        }
+        mPhone = phone;
+        mIsTestEmergencyNumber = isTestEmergencyNumber;
+        mLastEmergencyRegResult = null;
+        mOnCompleted = new CompletableFuture<>();
+
+        final boolean isAirplaneModeOn = isAirplaneModeOn(mContext);
+        boolean needToTurnOnRadio = !isRadioOn() || isAirplaneModeOn;
+
+        if (needToTurnOnRadio) {
+            if (mRadioOnHelper == null) {
+                mRadioOnHelper = new RadioOnHelper(mContext);
+            }
+
+            mRadioOnHelper.triggerRadioOnAndListen(new RadioOnStateListener.Callback() {
+                @Override
+                public void onComplete(RadioOnStateListener listener, boolean isRadioReady) {
+                    if (!isRadioReady) {
+                        // Could not turn radio on
+                        Rlog.e(TAG, "Failed to turn on radio.");
+                        mOnCompleted.complete(DisconnectCause.POWER_OFF);
+                        mPhone = null;
+                    } else {
+                        delayDialAndSetEmergencyMode(phone);
+                    }
+                }
+
+                @Override
+                public boolean isOkToCall(Phone phone, int serviceState) {
+                    // We currently only look to make sure that the radio is on before dialing. We
+                    // should be able to make emergency calls at any time after the radio has been
+                    // powered on and isn't in the UNAVAILABLE state, even if it is reporting the
+                    // OUT_OF_SERVICE state.
+                    return phone.getServiceStateTracker().isRadioOn();
+                }
+            }, !isTestEmergencyNumber, phone, isTestEmergencyNumber);
+        } else {
+            delayDialAndSetEmergencyMode(phone);
+        }
+
+        return mOnCompleted;
+    }
+
+    private void delayDialAndSetEmergencyMode(Phone phone) {
+        delayDialForDdsSwitch(phone, result -> {
+            Rlog.i(TAG, "delayDialForDdsSwitch: result = " + result);
+            if (!result) {
+                // DDS Switch timed out/failed, but continue with call as it may still succeed.
+                Rlog.e(TAG, "DDS Switch failed.");
+            }
+            // Once radio is on and DDS switched, must call setEmergencyMode() before selecting
+            // emergency domain. EmergencyRegResult is required to determine domain and this is the
+            // only API that can receive it before starting domain selection. Once domain selection
+            // is finished, the actual emergency mode will be set when onEmergencyTransportChanged()
+            // is called.
+            setEmergencyMode(MODE_EMERGENCY_WWAN);
+        });
+    }
+
+    /**
+     * Triggers modem to set new emergency mode.
+     *
+     * @param mode the new emergency mode
+     */
+    private void setEmergencyMode(@EmergencyConstants.EmergencyMode int mode) {
+        Rlog.i(TAG, "setEmergencyMode from " + mEmergencyMode + " to " + mode);
+
+        if (mEmergencyMode == mode) {
+            return;
+        }
+        mEmergencyMode = mode;
+
+        if (mIsTestEmergencyNumber) {
+            Rlog.d(TAG, "IsTestEmergencyNumber true. Skipping setting emergency mode on modem.");
+            return;
+        }
+        mPhone.setEmergencyMode(mode, mHandler.obtainMessage(MSG_SET_EMERGENCY_MODE_DONE));
+    }
+
+    /**
+     * Notifies external app listeners of emergency mode changes.
+     *
+     * @param callActive whether there is an active emergency call.
+     */
+    private void setIsInEmergencyCall(boolean callActive) {
+        mIsInEmergencyCall = callActive;
+    }
+
+    /**
+     * Checks if there is an ongoing emergency call.
+     *
+     * @return true if in emergency call
+     */
+    public boolean isInEmergencyCall() {
+        return mIsInEmergencyCall;
+    }
+
+    /**
+     * Triggers modem to exit emergency mode.
+     */
+    private void exitEmergencyMode() {
+        Rlog.i(TAG, "exitEmergencyMode");
+
+        mEmergencyMode = MODE_EMERGENCY_NONE;
+
+        mPhone.exitEmergencyMode(mHandler.obtainMessage(MSG_EXIT_EMERGENCY_MODE_DONE));
+    }
+
+    /**
+     * Ends emergency call.
+     *
+     * @param callId the call id on which to end the emergency call.
+     */
+    public void endCall(String callId) {
+        mActiveEmergencyCalls.remove(callId);
+        exitEmergencyMode();
+        mPhone = null;
+    }
+
+    /** Returns last {@link EmergencyRegResult} as set by {@code setEmergencyMode()}. */
+    public EmergencyRegResult getEmergencyRegResult() {
+        return mLastEmergencyRegResult;
+    };
+
+    /**
+     * Handles emergency transport change by setting new emergency mode.
+     *
+     * @param mode the new emergency mode
+     */
+    public void onEmergencyTransportChanged(@EmergencyConstants.EmergencyMode int mode) {
+        setEmergencyMode(mode);
+    }
+
+    /**
+     * Handles emergency call state change.
+     *
+     * @param state the new call state
+     * @param callId the callId whose state has changed
+     */
+    public void onEmergencyCallStateChanged(Call.State state, String callId) {
+        if (state == Call.State.ACTIVE) {
+            mActiveEmergencyCalls.add(callId);
+        }
+    }
+
+    /** Returns {@code true} if any phones from PhoneFactory have radio on. */
+    private boolean isRadioOn() {
+        boolean result = false;
+        for (Phone phone : mPhoneFactoryProxy.getPhones()) {
+            result |= phone.isRadioOn();
+        }
+        return result;
+    }
+
+    /** Returns {@code true} if airplane mode is on. */
+    private boolean isAirplaneModeOn(Context context) {
+        return Settings.Global.getInt(context.getContentResolver(),
+                Settings.Global.AIRPLANE_MODE_ON, 0) > 0;
+    }
+
+    /**
+     * If needed, block until the default data is switched for outgoing emergency call, or
+     * timeout expires.
+     *
+     * @param phone            The Phone to switch the DDS on.
+     * @param completeConsumer The consumer to call once the default data subscription has been
+     *                         switched, provides {@code true} result if the switch happened
+     *                         successfully or {@code false} if the operation timed out/failed.
+     */
+    @VisibleForTesting
+    public void delayDialForDdsSwitch(Phone phone, Consumer<Boolean> completeConsumer) {
+        if (phone == null) {
+            // Do not block indefinitely.
+            completeConsumer.accept(false);
+        }
+        try {
+            // Waiting for PhoneSwitcher to complete the operation.
+            CompletableFuture<Boolean> future = possiblyOverrideDefaultDataForEmergencyCall(phone);
+            // In the case that there is an issue or bug in PhoneSwitcher logic, do not wait
+            // indefinitely for the future to complete. Instead, set a timeout that will complete
+            // the future as to not block the outgoing call indefinitely.
+            CompletableFuture<Boolean> timeout = new CompletableFuture<>();
+            mHandler.postDelayed(() -> timeout.complete(false), DEFAULT_DATA_SWITCH_TIMEOUT_MS);
+            // Also ensure that the Consumer is completed on the main thread.
+            CompletableFuture<Void> unused = future.acceptEitherAsync(timeout, completeConsumer,
+                    phone.getContext().getMainExecutor());
+        } catch (Exception e) {
+            Rlog.w(TAG, "delayDialForDdsSwitch - exception= " + e.getMessage());
+        }
+    }
+
+    /**
+     * If needed, block until Default Data subscription is switched for outgoing emergency call.
+     *
+     * <p>
+     * In some cases, we need to try to switch the Default Data subscription before placing the
+     * emergency call on DSDS devices. This includes the following situation: - The modem does not
+     * support processing GNSS SUPL requests on the non-default data subscription. For some carriers
+     * that do not provide a control plane fallback mechanism, the SUPL request will be dropped and
+     * we will not be able to get the user's location for the emergency call. In this case, we need
+     * to swap default data temporarily.
+     *
+     * @param phone Evaluates whether or not the default data should be moved to the phone
+     *              specified. Should not be null.
+     */
+    private CompletableFuture<Boolean> possiblyOverrideDefaultDataForEmergencyCall(
+            @NonNull Phone phone) {
+        int phoneCount = mTelephonyManagerProxy.getPhoneCount();
+        // Do not override DDS if this is a single SIM device.
+        if (phoneCount <= PhoneConstants.MAX_PHONE_COUNT_SINGLE_SIM) {
+            return CompletableFuture.completedFuture(Boolean.TRUE);
+        }
+
+        // Do not switch Default data if this device supports emergency SUPL on non-DDS.
+        if (!mIsSuplDdsSwitchRequiredForEmergencyCall) {
+            Rlog.d(TAG, "possiblyOverrideDefaultDataForEmergencyCall: not switching DDS, does not "
+                    + "require DDS switch.");
+            return CompletableFuture.completedFuture(Boolean.TRUE);
+        }
+
+        CarrierConfigManager cfgManager = (CarrierConfigManager) phone.getContext()
+                .getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        if (cfgManager == null) {
+            // For some reason CarrierConfigManager is unavailable. Do not block emergency call.
+            Rlog.w(TAG, "possiblyOverrideDefaultDataForEmergencyCall: couldn't get"
+                    + "CarrierConfigManager");
+            return CompletableFuture.completedFuture(Boolean.TRUE);
+        }
+
+        // Only override default data if we are IN_SERVICE already.
+        if (!isAvailableForEmergencyCalls(phone)) {
+            Rlog.d(TAG, "possiblyOverrideDefaultDataForEmergencyCall: not switching DDS");
+            return CompletableFuture.completedFuture(Boolean.TRUE);
+        }
+
+        // Only override default data if we are not roaming, we do not want to switch onto a network
+        // that only supports data plane only (if we do not know).
+        boolean isRoaming = phone.getServiceState().getVoiceRoaming();
+        // In some roaming conditions, we know the roaming network doesn't support control plane
+        // fallback even though the home operator does. For these operators we will need to do a DDS
+        // switch anyway to make sure the SUPL request doesn't fail.
+        boolean roamingNetworkSupportsControlPlaneFallback = true;
+        String[] dataPlaneRoamPlmns = cfgManager.getConfigForSubId(phone.getSubId()).getStringArray(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_DATA_PLANE_ONLY_ROAMING_PLMN_STRING_ARRAY);
+        if (dataPlaneRoamPlmns != null && Arrays.asList(dataPlaneRoamPlmns)
+                .contains(phone.getServiceState().getOperatorNumeric())) {
+            roamingNetworkSupportsControlPlaneFallback = false;
+        }
+        if (isRoaming && roamingNetworkSupportsControlPlaneFallback) {
+            Rlog.d(TAG, "possiblyOverrideDefaultDataForEmergencyCall: roaming network is assumed "
+                    + "to support CP fallback, not switching DDS.");
+            return CompletableFuture.completedFuture(Boolean.TRUE);
+        }
+        // Do not try to swap default data if we support CS fallback or it is assumed that the
+        // roaming network supports control plane fallback, we do not want to introduce a lag in
+        // emergency call setup time if possible.
+        final boolean supportsCpFallback = cfgManager.getConfigForSubId(phone.getSubId()).getInt(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_CONTROL_PLANE_SUPPORT_INT,
+                CarrierConfigManager.Gps.SUPL_EMERGENCY_MODE_TYPE_CP_ONLY)
+                != CarrierConfigManager.Gps.SUPL_EMERGENCY_MODE_TYPE_DP_ONLY;
+        if (supportsCpFallback && roamingNetworkSupportsControlPlaneFallback) {
+            Rlog.d(TAG, "possiblyOverrideDefaultDataForEmergencyCall: not switching DDS, carrier "
+                    + "supports CP fallback.");
+            return CompletableFuture.completedFuture(Boolean.TRUE);
+        }
+
+        // Get extension time, may be 0 for some carriers that support ECBM as well. Use
+        // CarrierConfig default if format fails.
+        int extensionTime = 0;
+        try {
+            extensionTime = Integer.parseInt(cfgManager.getConfigForSubId(phone.getSubId())
+                    .getString(CarrierConfigManager.Gps.KEY_ES_EXTENSION_SEC_STRING, "0"));
+        } catch (NumberFormatException e) {
+            // Just use default.
+        }
+        CompletableFuture<Boolean> modemResultFuture = new CompletableFuture<>();
+        try {
+            Rlog.d(TAG, "possiblyOverrideDefaultDataForEmergencyCall: overriding DDS for "
+                    + extensionTime + "seconds");
+            mPhoneSwitcherProxy.getPhoneSwitcher().overrideDefaultDataForEmergency(
+                    phone.getPhoneId(), extensionTime, modemResultFuture);
+            // Catch all exceptions, we want to continue with emergency call if possible.
+        } catch (Exception e) {
+            Rlog.w(TAG,
+                    "possiblyOverrideDefaultDataForEmergencyCall: exception = " + e.getMessage());
+            modemResultFuture = CompletableFuture.completedFuture(Boolean.FALSE);
+        }
+        return modemResultFuture;
+    }
+
+    /**
+     * Returns true if the state of the Phone is IN_SERVICE or available for emergency calling only.
+     */
+    private boolean isAvailableForEmergencyCalls(Phone phone) {
+        return ServiceState.STATE_IN_SERVICE == phone.getServiceState().getState()
+                || phone.getServiceState().isEmergencyOnly();
+    }
+}
diff --git a/src/java/com/android/internal/telephony/emergency/RadioOnHelper.java b/src/java/com/android/internal/telephony/emergency/RadioOnHelper.java
new file mode 100644
index 0000000..827e495
--- /dev/null
+++ b/src/java/com/android/internal/telephony/emergency/RadioOnHelper.java
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.emergency;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.UserHandle;
+import android.provider.Settings;
+import android.telephony.TelephonyManager;
+
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneFactory;
+import com.android.telephony.Rlog;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Helper class that implements special behavior related to emergency calls or making phone calls
+ * when the radio is in the POWER_OFF STATE. Specifically, this class handles the case of the user
+ * trying to dial an emergency number while the radio is off (i.e. the device is in airplane mode)
+ * or a normal number while the radio is off (because of the device is on Bluetooth), by turning the
+ * radio back on, waiting for it to come up, and then retrying the call.
+ */
+public class RadioOnHelper implements RadioOnStateListener.Callback {
+
+    private static final String TAG = "RadioOnStateListener";
+
+    private final Context mContext;
+    private RadioOnStateListener.Callback mCallback;
+    private List<RadioOnStateListener> mListeners;
+    private List<RadioOnStateListener> mInProgressListeners;
+    private boolean mIsRadioOnCallingEnabled;
+
+    public RadioOnHelper(Context context) {
+        mContext = context;
+        mInProgressListeners = new ArrayList<>(2);
+    }
+
+    private void setupListeners() {
+        if (mListeners == null) {
+            mListeners = new ArrayList<>(2);
+        }
+        int activeModems = TelephonyManager.from(mContext).getActiveModemCount();
+        // Add new listeners if active modem count increased.
+        while (mListeners.size() < activeModems) {
+            mListeners.add(new RadioOnStateListener());
+        }
+        // Clean up listeners if active modem count decreased.
+        while (mListeners.size() > activeModems) {
+            mListeners.get(mListeners.size() - 1).cleanup();
+            mListeners.remove(mListeners.size() - 1);
+        }
+    }
+
+    /**
+     * Starts the "turn on radio" sequence. This is the (single) external API of the RadioOnHelper
+     * class.
+     *
+     * This method kicks off the following sequence:
+     * - Power on the radio for each Phone
+     * - Listen for radio events telling us the radio has come up.
+     * - Retry if we've gone a significant amount of time without any response from the radio.
+     * - Finally, clean up any leftover state.
+     *
+     * This method is safe to call from any thread, since it simply posts a message to the
+     * RadioOnHelper's handler (thus ensuring that the rest of the sequence is entirely serialized,
+     * and runs on the main looper.)
+     */
+    public void triggerRadioOnAndListen(RadioOnStateListener.Callback callback,
+            boolean forEmergencyCall, Phone phoneForEmergencyCall, boolean isTestEmergencyNumber) {
+        setupListeners();
+        mCallback = callback;
+        mInProgressListeners.clear();
+        mIsRadioOnCallingEnabled = false;
+        for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) {
+            Phone phone = PhoneFactory.getPhone(i);
+            if (phone == null) {
+                continue;
+            }
+
+            mInProgressListeners.add(mListeners.get(i));
+            mListeners.get(i).waitForRadioOn(phone, this, forEmergencyCall, forEmergencyCall
+                    && phone == phoneForEmergencyCall);
+        }
+        powerOnRadio(forEmergencyCall, phoneForEmergencyCall, isTestEmergencyNumber);
+    }
+
+    /**
+     * Attempt to power on the radio (i.e. take the device out of airplane mode). We'll eventually
+     * get an onServiceStateChanged() callback when the radio successfully comes up.
+     */
+    private void powerOnRadio(boolean forEmergencyCall, Phone phoneForEmergencyCall,
+            boolean isTestEmergencyNumber) {
+
+        // Always try to turn on the radio here independent of APM setting - if we got here in the
+        // first place, the radio is off independent of APM setting.
+        for (Phone phone : PhoneFactory.getPhones()) {
+            Rlog.d(TAG, "powerOnRadio, enabling Radio");
+            if (isTestEmergencyNumber) {
+                phone.setRadioPowerOnForTestEmergencyCall(phone == phoneForEmergencyCall);
+            } else {
+                phone.setRadioPower(true, forEmergencyCall, phone == phoneForEmergencyCall,
+                        false);
+            }
+        }
+
+        // If airplane mode is on, we turn it off the same way that the Settings activity turns it
+        // off to keep the setting in sync.
+        if (Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.AIRPLANE_MODE_ON, 0) > 0) {
+            Rlog.d(TAG, "==> Turning off airplane mode for emergency call.");
+
+            // Change the system setting
+            Settings.Global.putInt(mContext.getContentResolver(),
+                    Settings.Global.AIRPLANE_MODE_ON, 0);
+
+            // Post the broadcast intend for change in airplane mode TODO: We really should not be
+            // in charge of sending this broadcast. If changing the setting is sufficient to trigger
+            // all of the rest of the logic, then that should also trigger the broadcast intent.
+            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
+            intent.putExtra("state", false);
+            mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
+        }
+    }
+
+    /**
+     * This method is called from multiple Listeners on the Main Looper. Synchronization is not
+     * necessary.
+     */
+    @Override
+    public void onComplete(RadioOnStateListener listener, boolean isRadioReady) {
+        mIsRadioOnCallingEnabled |= isRadioReady;
+        mInProgressListeners.remove(listener);
+        if (mCallback != null && mInProgressListeners.isEmpty()) {
+            mCallback.onComplete(null, mIsRadioOnCallingEnabled);
+        }
+    }
+
+    @Override
+    public boolean isOkToCall(Phone phone, int serviceState) {
+        return (mCallback == null) ? false : mCallback.isOkToCall(phone, serviceState);
+    }
+}
diff --git a/src/java/com/android/internal/telephony/emergency/RadioOnStateListener.java b/src/java/com/android/internal/telephony/emergency/RadioOnStateListener.java
new file mode 100644
index 0000000..01eaaa6
--- /dev/null
+++ b/src/java/com/android/internal/telephony/emergency/RadioOnStateListener.java
@@ -0,0 +1,403 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.emergency;
+
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.telephony.ServiceState;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.os.SomeArgs;
+import com.android.internal.telephony.Phone;
+import com.android.telephony.Rlog;
+
+import java.util.Locale;
+
+/**
+ * Helper class that listens to a Phone's radio state and sends an onComplete callback when we
+ * return true for isOkToCall.
+ */
+public class RadioOnStateListener {
+
+    public interface Callback {
+        /**
+         * Receives the result of the RadioOnStateListener's attempt to turn on the radio.
+         */
+        void onComplete(RadioOnStateListener listener, boolean isRadioReady);
+
+        /**
+         * Given the Phone and the new service state of that phone, return whether or not this phone
+         * is ok to call. If it is, onComplete will be called shortly after.
+         */
+        boolean isOkToCall(Phone phone, int serviceState);
+    }
+
+    private static final String TAG = "RadioOnStateListener";
+
+    // Number of times to retry the call, and time between retry attempts.
+    // not final for testing
+    private static int MAX_NUM_RETRIES = 5;
+    // not final for testing
+    private static long TIME_BETWEEN_RETRIES_MILLIS = 5000; // msec
+
+    // Handler message codes; see handleMessage()
+    private static final int MSG_START_SEQUENCE = 1;
+    @VisibleForTesting
+    public static final int MSG_SERVICE_STATE_CHANGED = 2;
+    private static final int MSG_RETRY_TIMEOUT = 3;
+    @VisibleForTesting
+    public static final int MSG_RADIO_ON = 4;
+    public static final int MSG_RADIO_OFF_OR_NOT_AVAILABLE = 5;
+
+    private final Handler mHandler = new Handler(Looper.getMainLooper()) {
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case MSG_START_SEQUENCE:
+                    SomeArgs args = (SomeArgs) msg.obj;
+                    try {
+                        Phone phone = (Phone) args.arg1;
+                        RadioOnStateListener.Callback callback =
+                                (RadioOnStateListener.Callback) args.arg2;
+                        boolean forEmergencyCall = (boolean) args.arg3;
+                        boolean isSelectedPhoneForEmergencyCall = (boolean) args.arg4;
+                        startSequenceInternal(phone, callback, forEmergencyCall,
+                                isSelectedPhoneForEmergencyCall);
+                    } finally {
+                        args.recycle();
+                    }
+                    break;
+                case MSG_SERVICE_STATE_CHANGED:
+                    onServiceStateChanged((ServiceState) ((AsyncResult) msg.obj).result);
+                    break;
+                case MSG_RADIO_ON:
+                    onRadioOn();
+                    break;
+                case MSG_RADIO_OFF_OR_NOT_AVAILABLE:
+                    registerForRadioOn();
+                    break;
+                case MSG_RETRY_TIMEOUT:
+                    onRetryTimeout();
+                    break;
+                default:
+                    Rlog.w(TAG, String.format(Locale.getDefault(),
+                        "handleMessage: unexpected message: %d.", msg.what));
+                    break;
+            }
+        }
+    };
+
+    private Callback mCallback; // The callback to notify upon completion.
+    private Phone mPhone; // The phone that will attempt to place the call.
+    private boolean mForEmergencyCall; // Whether radio is being turned on for emergency call.
+    // Whether this phone is selected to place emergency call. Can be true only if
+    // mForEmergencyCall is true.
+    private boolean mSelectedPhoneForEmergencyCall;
+    private int mNumRetriesSoFar;
+
+    /**
+     * Starts the "wait for radio" sequence. This is the (single) external API of the
+     * RadioOnStateListener class.
+     *
+     * This method kicks off the following sequence:
+     * - Listen for the service state change event telling us the radio has come up.
+     * - Retry if we've gone {@link #TIME_BETWEEN_RETRIES_MILLIS} without any response from the
+     *   radio.
+     * - Finally, clean up any leftover state.
+     *
+     * This method is safe to call from any thread, since it simply posts a message to the
+     * RadioOnStateListener's handler (thus ensuring that the rest of the sequence is entirely
+     * serialized, and runs only on the handler thread.)
+     */
+    public void waitForRadioOn(Phone phone, Callback callback,
+            boolean forEmergencyCall, boolean isSelectedPhoneForEmergencyCall) {
+        Rlog.d(TAG, "waitForRadioOn: Phone " + phone.getPhoneId());
+
+        if (mPhone != null) {
+            // If there already is an ongoing request, ignore the new one!
+            return;
+        }
+
+        SomeArgs args = SomeArgs.obtain();
+        args.arg1 = phone;
+        args.arg2 = callback;
+        args.arg3 = forEmergencyCall;
+        args.arg4 = isSelectedPhoneForEmergencyCall;
+        mHandler.obtainMessage(MSG_START_SEQUENCE, args).sendToTarget();
+    }
+
+    /**
+     * Actual implementation of waitForRadioOn(), guaranteed to run on the handler thread.
+     *
+     * @see #waitForRadioOn
+     */
+    private void startSequenceInternal(Phone phone, Callback callback,
+            boolean forEmergencyCall, boolean isSelectedPhoneForEmergencyCall) {
+        Rlog.d(TAG, "startSequenceInternal: Phone " + phone.getPhoneId());
+
+        // First of all, clean up any state left over from a prior RadioOn call sequence. This
+        // ensures that we'll behave sanely if another startTurnOnRadioSequence() comes in while
+        // we're already in the middle of the sequence.
+        cleanup();
+
+        mPhone = phone;
+        mCallback = callback;
+        mForEmergencyCall = forEmergencyCall;
+        mSelectedPhoneForEmergencyCall = isSelectedPhoneForEmergencyCall;
+
+        registerForServiceStateChanged();
+        // Register for RADIO_OFF to handle cases where emergency call is dialed before
+        // we receive UNSOL_RESPONSE_RADIO_STATE_CHANGED with RADIO_OFF.
+        registerForRadioOff();
+        // Next step: when the SERVICE_STATE_CHANGED event comes in, we'll retry the call; see
+        // onServiceStateChanged(). But also, just in case, start a timer to make sure we'll retry
+        // the call even if the SERVICE_STATE_CHANGED event never comes in for some reason.
+        startRetryTimer();
+    }
+
+    /**
+     * Handles the SERVICE_STATE_CHANGED event. This event tells us that the radio state has changed
+     * and is probably coming up. We can now check to see if the conditions are met to place the
+     * call with {@link Callback#isOkToCall}
+     */
+    private void onServiceStateChanged(ServiceState state) {
+        if (mPhone == null) {
+            return;
+        }
+        Rlog.d(TAG, String.format("onServiceStateChanged(), new state = %s, Phone = %s", state,
+                mPhone.getPhoneId()));
+
+        // Possible service states:
+        // - STATE_IN_SERVICE        // Normal operation
+        // - STATE_OUT_OF_SERVICE    // Still searching for an operator to register to,
+        //                           // or no radio signal
+        // - STATE_EMERGENCY_ONLY    // Only emergency numbers are allowed; currently not used
+        // - STATE_POWER_OFF         // Radio is explicitly powered off (airplane mode)
+
+        if (isOkToCall(state.getState())) {
+            // Woo hoo! It's OK to actually place the call.
+            Rlog.d(TAG, "onServiceStateChanged: ok to call!");
+
+            onComplete(true);
+            cleanup();
+        } else {
+            // The service state changed, but we're still not ready to call yet.
+            Rlog.d(TAG, "onServiceStateChanged: not ready to call yet, keep waiting.");
+        }
+    }
+
+    private void onRadioOn() {
+        if (mPhone == null) {
+            return;
+        }
+        ServiceState state = mPhone.getServiceState();
+        Rlog.d(TAG, String.format("onRadioOn, state = %s, Phone = %s", state, mPhone.getPhoneId()));
+        if (isOkToCall(state.getState())) {
+            onComplete(true);
+            cleanup();
+        } else {
+            Rlog.d(TAG, "onRadioOn: not ready to call yet, keep waiting.");
+        }
+    }
+
+    /**
+     * Callback to see if it is okay to call yet, given the current conditions.
+     */
+    private boolean isOkToCall(int serviceState) {
+        return (mCallback == null) ? false : mCallback.isOkToCall(mPhone, serviceState);
+    }
+
+    /**
+     * Handles the retry timer expiring.
+     */
+    private void onRetryTimeout() {
+        if (mPhone == null) {
+            return;
+        }
+        int serviceState = mPhone.getServiceState().getState();
+        Rlog.d(TAG,
+                String.format(Locale.getDefault(),
+                        "onRetryTimeout():  phone state = %s, service state = %d, retries = %d.",
+                        mPhone.getState(), serviceState, mNumRetriesSoFar));
+
+        // - If we're actually in a call, we've succeeded.
+        // - Otherwise, if the radio is now on, that means we successfully got out of airplane mode
+        //   but somehow didn't get the service state change event. In that case, try to place the
+        //   call.
+        // - If the radio is still powered off, try powering it on again.
+
+        if (isOkToCall(serviceState)) {
+            Rlog.d(TAG, "onRetryTimeout: Radio is on. Cleaning up.");
+
+            // Woo hoo -- we successfully got out of airplane mode.
+            onComplete(true);
+            cleanup();
+        } else {
+            // Uh oh; we've waited the full TIME_BETWEEN_RETRIES_MILLIS and the radio is still not
+            // powered-on. Try again.
+
+            mNumRetriesSoFar++;
+            Rlog.d(TAG, "mNumRetriesSoFar is now " + mNumRetriesSoFar);
+
+            if (mNumRetriesSoFar > MAX_NUM_RETRIES) {
+                Rlog.w(TAG, "Hit MAX_NUM_RETRIES; giving up.");
+                cleanup();
+            } else {
+                Rlog.d(TAG, "Trying (again) to turn on the radio.");
+                mPhone.setRadioPower(true, mForEmergencyCall, mSelectedPhoneForEmergencyCall,
+                        false);
+                startRetryTimer();
+            }
+        }
+    }
+
+    /**
+     * Clean up when done with the whole sequence: either after successfully turning on the radio,
+     * or after bailing out because of too many failures.
+     *
+     * The exact cleanup steps are:
+     * - Notify callback if we still hadn't sent it a response.
+     * - Double-check that we're not still registered for any telephony events
+     * - Clean up any extraneous handler messages (like retry timeouts) still in the queue
+     *
+     * Basically this method guarantees that there will be no more activity from the
+     * RadioOnStateListener until someone kicks off the whole sequence again with another call to
+     * {@link #waitForRadioOn}
+     *
+     * TODO: Do the work for the comment below: Note we don't call this method simply after a
+     * successful call to placeCall(), since it's still possible the call will disconnect very
+     * quickly with an OUT_OF_SERVICE error.
+     */
+    public void cleanup() {
+        Rlog.d(TAG, "cleanup()");
+
+        // This will send a failure call back if callback has yet to be invoked. If the callback was
+        // already invoked, it's a no-op.
+        onComplete(false);
+
+        unregisterForServiceStateChanged();
+        unregisterForRadioOff();
+        unregisterForRadioOn();
+        cancelRetryTimer();
+
+        // Used for unregisterForServiceStateChanged() so we null it out here instead.
+        mPhone = null;
+        mNumRetriesSoFar = 0;
+    }
+
+    private void startRetryTimer() {
+        cancelRetryTimer();
+        mHandler.sendEmptyMessageDelayed(MSG_RETRY_TIMEOUT, TIME_BETWEEN_RETRIES_MILLIS);
+    }
+
+    private void cancelRetryTimer() {
+        mHandler.removeMessages(MSG_RETRY_TIMEOUT);
+    }
+
+    private void registerForServiceStateChanged() {
+        // Unregister first, just to make sure we never register ourselves twice. (We need this
+        // because Phone.registerForServiceStateChanged() does not prevent multiple registration of
+        // the same handler.)
+        unregisterForServiceStateChanged();
+        mPhone.registerForServiceStateChanged(mHandler, MSG_SERVICE_STATE_CHANGED, null);
+    }
+
+    private void unregisterForServiceStateChanged() {
+        // This method is safe to call even if we haven't set mPhone yet.
+        if (mPhone != null) {
+            mPhone.unregisterForServiceStateChanged(mHandler); // Safe even if unnecessary
+        }
+        mHandler.removeMessages(MSG_SERVICE_STATE_CHANGED); // Clean up any pending messages too
+    }
+
+    private void registerForRadioOff() {
+        mPhone.mCi.registerForOffOrNotAvailable(mHandler, MSG_RADIO_OFF_OR_NOT_AVAILABLE, null);
+    }
+
+    private void unregisterForRadioOff() {
+        // This method is safe to call even if we haven't set mPhone yet.
+        if (mPhone != null) {
+            mPhone.mCi.unregisterForOffOrNotAvailable(mHandler); // Safe even if unnecessary
+        }
+        mHandler.removeMessages(MSG_RADIO_OFF_OR_NOT_AVAILABLE); // Clean up any pending messages
+    }
+
+    private void registerForRadioOn() {
+        unregisterForRadioOff();
+        mPhone.mCi.registerForOn(mHandler, MSG_RADIO_ON, null);
+    }
+
+    private void unregisterForRadioOn() {
+        // This method is safe to call even if we haven't set mPhone yet.
+        if (mPhone != null) {
+            mPhone.mCi.unregisterForOn(mHandler); // Safe even if unnecessary
+        }
+        mHandler.removeMessages(MSG_RADIO_ON); // Clean up any pending messages too
+    }
+
+    private void onComplete(boolean isRadioReady) {
+        if (mCallback != null) {
+            Callback tempCallback = mCallback;
+            mCallback = null;
+            tempCallback.onComplete(this, isRadioReady);
+        }
+    }
+
+    @VisibleForTesting
+    public Handler getHandler() {
+        return mHandler;
+    }
+
+    @VisibleForTesting
+    public void setMaxNumRetries(int retries) {
+        MAX_NUM_RETRIES = retries;
+    }
+
+    @VisibleForTesting
+    public void setTimeBetweenRetriesMillis(long timeMs) {
+        TIME_BETWEEN_RETRIES_MILLIS = timeMs;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o)
+            return true;
+        if (o == null || !getClass().equals(o.getClass()))
+            return false;
+
+        RadioOnStateListener that = (RadioOnStateListener) o;
+
+        if (mNumRetriesSoFar != that.mNumRetriesSoFar) {
+            return false;
+        }
+        if (mCallback != null ? !mCallback.equals(that.mCallback) : that.mCallback != null) {
+            return false;
+        }
+        return mPhone != null ? mPhone.equals(that.mPhone) : that.mPhone == null;
+    }
+
+    @Override
+    public int hashCode() {
+        int hash = 7;
+        hash = 31 * hash + mNumRetriesSoFar;
+        hash = 31 * hash + (mCallback == null ? 0 : mCallback.hashCode());
+        hash = 31 * hash + (mPhone == null ? 0 : mPhone.hashCode());
+        return hash;
+    }
+}
diff --git a/src/java/com/android/internal/telephony/euicc/EuiccCardController.java b/src/java/com/android/internal/telephony/euicc/EuiccCardController.java
index f337141..a63ae1d 100644
--- a/src/java/com/android/internal/telephony/euicc/EuiccCardController.java
+++ b/src/java/com/android/internal/telephony/euicc/EuiccCardController.java
@@ -39,7 +39,9 @@
 import android.util.Log;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.SubscriptionController;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.UiccCard;
 import com.android.internal.telephony.uicc.UiccController;
 import com.android.internal.telephony.uicc.UiccPort;
@@ -51,6 +53,7 @@
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.util.List;
 
 /** Backing implementation of {@link EuiccCardManager}. */
 public class EuiccCardController extends IEuiccCardController.Stub {
@@ -413,7 +416,7 @@
         // if there is no iccid enabled on this port, return null.
         if (TextUtils.isEmpty(iccId)) {
             try {
-                callback.onComplete(EuiccCardManager.RESULT_PROFILE_NOT_FOUND, null);
+                callback.onComplete(EuiccCardManager.RESULT_PROFILE_DOES_NOT_EXIST, null);
             } catch (RemoteException exception) {
                 loge("getEnabledProfile callback failure.", exception);
             }
@@ -649,9 +652,14 @@
             @Override
             public void onResult(Void result) {
                 Log.i(TAG, "Request subscription info list refresh after delete.");
-                SubscriptionController.getInstance()
-                        .requestEmbeddedSubscriptionInfoListRefresh(
-                                mUiccController.convertToPublicCardId(cardId));
+                if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                    SubscriptionManagerService.getInstance().updateEmbeddedSubscriptions(
+                            List.of(mUiccController.convertToPublicCardId(cardId)), null);
+                } else {
+                    SubscriptionController.getInstance()
+                            .requestEmbeddedSubscriptionInfoListRefresh(
+                                    mUiccController.convertToPublicCardId(cardId));
+                }
                 try {
                     callback.onComplete(EuiccCardManager.RESULT_OK);
                 } catch (RemoteException exception) {
@@ -701,9 +709,14 @@
             @Override
             public void onResult(Void result) {
                 Log.i(TAG, "Request subscription info list refresh after reset memory.");
-                SubscriptionController.getInstance()
-                        .requestEmbeddedSubscriptionInfoListRefresh(
-                                mUiccController.convertToPublicCardId(cardId));
+                if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                    SubscriptionManagerService.getInstance().updateEmbeddedSubscriptions(
+                            List.of(mUiccController.convertToPublicCardId(cardId)), null);
+                } else {
+                    SubscriptionController.getInstance()
+                            .requestEmbeddedSubscriptionInfoListRefresh(
+                                    mUiccController.convertToPublicCardId(cardId));
+                }
                 try {
                     callback.onComplete(EuiccCardManager.RESULT_OK);
                 } catch (RemoteException exception) {
@@ -1190,9 +1203,14 @@
             @Override
             public void onResult(byte[] result) {
                 Log.i(TAG, "Request subscription info list refresh after install.");
-                SubscriptionController.getInstance()
-                        .requestEmbeddedSubscriptionInfoListRefresh(
-                                mUiccController.convertToPublicCardId(cardId));
+                if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                    SubscriptionManagerService.getInstance().updateEmbeddedSubscriptions(
+                            List.of(mUiccController.convertToPublicCardId(cardId)), null);
+                } else {
+                    SubscriptionController.getInstance()
+                            .requestEmbeddedSubscriptionInfoListRefresh(
+                                    mUiccController.convertToPublicCardId(cardId));
+                }
                 try {
                     callback.onComplete(EuiccCardManager.RESULT_OK, result);
                 } catch (RemoteException exception) {
diff --git a/src/java/com/android/internal/telephony/euicc/EuiccConnector.java b/src/java/com/android/internal/telephony/euicc/EuiccConnector.java
index 974acf9..7d46a9a 100644
--- a/src/java/com/android/internal/telephony/euicc/EuiccConnector.java
+++ b/src/java/com/android/internal/telephony/euicc/EuiccConnector.java
@@ -225,6 +225,8 @@
     static class GetMetadataRequest {
         DownloadableSubscription mSubscription;
         boolean mForceDeactivateSim;
+        boolean mSwitchAfterDownload;
+        int mPortIndex;
         GetMetadataCommandCallback mCallback;
     }
 
@@ -389,6 +391,9 @@
         mSm = (SubscriptionManager)
                 context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
 
+        // TODO(b/239277548): Disable debug logging after analysing this bug.
+        setDbg(true);
+
         // Unavailable/Available both monitor for package changes and update mSelectedComponent but
         // do not need to adjust the binding.
         mUnavailableState = new UnavailableState();
@@ -444,13 +449,15 @@
 
     /** Asynchronously fetch metadata for the given downloadable subscription. */
     @VisibleForTesting(visibility = PACKAGE)
-    public void getDownloadableSubscriptionMetadata(int cardId,
-            DownloadableSubscription subscription,
+    public void getDownloadableSubscriptionMetadata(int cardId, int portIndex,
+            DownloadableSubscription subscription, boolean switchAfterDownload,
             boolean forceDeactivateSim, GetMetadataCommandCallback callback) {
         GetMetadataRequest request =
                 new GetMetadataRequest();
         request.mSubscription = subscription;
         request.mForceDeactivateSim = forceDeactivateSim;
+        request.mSwitchAfterDownload = switchAfterDownload;
+        request.mPortIndex = portIndex;
         request.mCallback = callback;
         sendMessage(CMD_GET_DOWNLOADABLE_SUBSCRIPTION_METADATA, cardId, 0 /* arg2 */, request);
     }
@@ -749,7 +756,9 @@
                         case CMD_GET_DOWNLOADABLE_SUBSCRIPTION_METADATA: {
                             GetMetadataRequest request = (GetMetadataRequest) message.obj;
                             mEuiccService.getDownloadableSubscriptionMetadata(slotId,
+                                    request.mPortIndex,
                                     request.mSubscription,
+                                    request.mSwitchAfterDownload,
                                     request.mForceDeactivateSim,
                                     new IGetDownloadableSubscriptionMetadataCallback.Stub() {
                                         @Override
diff --git a/src/java/com/android/internal/telephony/euicc/EuiccController.java b/src/java/com/android/internal/telephony/euicc/EuiccController.java
index 28039b7..a3d3056 100644
--- a/src/java/com/android/internal/telephony/euicc/EuiccController.java
+++ b/src/java/com/android/internal/telephony/euicc/EuiccController.java
@@ -61,6 +61,7 @@
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.euicc.EuiccConnector.OtaStatusChangedCallback;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.IccUtils;
 import com.android.internal.telephony.uicc.UiccController;
 import com.android.internal.telephony.uicc.UiccPort;
@@ -385,6 +386,7 @@
 
     void getDownloadableSubscriptionMetadata(int cardId, DownloadableSubscription subscription,
             boolean forceDeactivateSim, String callingPackage, PendingIntent callbackIntent) {
+        Log.d(TAG, " getDownloadableSubscriptionMetadata callingPackage: " + callingPackage);
         if (!callerCanWriteEmbeddedSubscriptions()) {
             throw new SecurityException("Must have WRITE_EMBEDDED_SUBSCRIPTIONS to get metadata");
         }
@@ -392,7 +394,8 @@
         long token = Binder.clearCallingIdentity();
         try {
             mConnector.getDownloadableSubscriptionMetadata(cardId,
-                    subscription, forceDeactivateSim,
+                    TelephonyManager.DEFAULT_PORT_INDEX, subscription,
+                    false /* switchAfterDownload */, forceDeactivateSim,
                     new GetMetadataCommandCallback(
                             token, subscription, callingPackage, callbackIntent));
         } finally {
@@ -601,8 +604,8 @@
             if (!isConsentNeededToResolvePortIndex
                     && canManageSubscriptionOnTargetSim(cardId, callingPackage, true,
                     portIndex)) {
-                mConnector.getDownloadableSubscriptionMetadata(cardId, subscription,
-                    forceDeactivateSim,
+                mConnector.getDownloadableSubscriptionMetadata(cardId, portIndex,
+                        subscription, switchAfterDownload, forceDeactivateSim,
                     new DownloadSubscriptionGetMetadataCommandCallback(token, subscription,
                         switchAfterDownload, callingPackage, forceDeactivateSim,
                         callbackIntent, false /* withUserConsent */, portIndex));
@@ -713,7 +716,8 @@
         Log.d(TAG, " downloadSubscriptionPrivilegedCheckMetadata cardId: " + cardId
                 + " switchAfterDownload: " + switchAfterDownload + " portIndex: " + portIndex
                 + " forceDeactivateSim: " + forceDeactivateSim);
-        mConnector.getDownloadableSubscriptionMetadata(cardId, subscription, forceDeactivateSim,
+        mConnector.getDownloadableSubscriptionMetadata(cardId, portIndex,
+                subscription, switchAfterDownload, forceDeactivateSim,
                 new DownloadSubscriptionGetMetadataCommandCallback(callingToken, subscription,
                         switchAfterDownload, callingPackage, forceDeactivateSim, callbackIntent,
                         true /* withUserConsent */, portIndex));
@@ -862,6 +866,7 @@
 
     void getDefaultDownloadableSubscriptionList(int cardId,
             boolean forceDeactivateSim, String callingPackage, PendingIntent callbackIntent) {
+        Log.d(TAG, " getDefaultDownloadableSubscriptionList callingPackage: " + callingPackage);
         if (!callerCanWriteEmbeddedSubscriptions()) {
             throw new SecurityException(
                     "Must have WRITE_EMBEDDED_SUBSCRIPTIONS to get default list");
@@ -1147,7 +1152,8 @@
      * Returns the resolved portIndex or {@link TelephonyManager#INVALID_PORT_INDEX} if calling
      * cannot manage any active subscription.
      */
-    private int getResolvedPortIndexForDisableSubscription(int cardId, String callingPackage,
+    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
+    public int getResolvedPortIndexForDisableSubscription(int cardId, String callingPackage,
             boolean callerCanWriteEmbeddedSubscriptions) {
         List<SubscriptionInfo> subInfoList = mSubscriptionManager
                 .getActiveSubscriptionInfoList(/* userVisibleOnly */false);
@@ -1175,7 +1181,8 @@
      * Returns the resolved portIndex or {@link TelephonyManager#INVALID_PORT_INDEX} if no port
      * is available without user consent.
      */
-    private int getResolvedPortIndexForSubscriptionSwitch(int cardId) {
+    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
+    public int getResolvedPortIndexForSubscriptionSwitch(int cardId) {
         int slotIndex = getSlotIndexFromCardId(cardId);
         // Euicc Slot
         UiccSlot slot = UiccController.getInstance().getUiccSlot(slotIndex);
@@ -1585,9 +1592,15 @@
     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
     public void refreshSubscriptionsAndSendResult(
             PendingIntent callbackIntent, int resultCode, Intent extrasIntent) {
-        SubscriptionController.getInstance()
-                .requestEmbeddedSubscriptionInfoListRefresh(
-                        () -> sendResult(callbackIntent, resultCode, extrasIntent));
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionManagerService.getInstance().updateEmbeddedSubscriptions(
+                    List.of(mTelephonyManager.getCardIdForDefaultEuicc()),
+                    () -> sendResult(callbackIntent, resultCode, extrasIntent));
+        } else {
+            SubscriptionController.getInstance()
+                    .requestEmbeddedSubscriptionInfoListRefresh(
+                            () -> sendResult(callbackIntent, resultCode, extrasIntent));
+        }
     }
 
     /** Dispatch the given callback intent with the given result code and data. */
diff --git a/src/java/com/android/internal/telephony/gsm/GsmInboundSmsHandler.java b/src/java/com/android/internal/telephony/gsm/GsmInboundSmsHandler.java
index 0abd4ab..907f158 100644
--- a/src/java/com/android/internal/telephony/gsm/GsmInboundSmsHandler.java
+++ b/src/java/com/android/internal/telephony/gsm/GsmInboundSmsHandler.java
@@ -24,10 +24,12 @@
 import android.content.IntentFilter;
 import android.os.AsyncResult;
 import android.os.Build;
+import android.os.Looper;
 import android.os.Message;
 import android.os.SystemProperties;
 import android.provider.Telephony.Sms.Intents;
 
+import com.android.ims.ImsManager;
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.InboundSmsHandler;
 import com.android.internal.telephony.Phone;
@@ -43,7 +45,7 @@
  */
 public class GsmInboundSmsHandler extends InboundSmsHandler {
 
-    private static BroadcastReceiver sTestBroadcastReceiver;
+    private BroadcastReceiver mTestBroadcastReceiver;
     /** Handler for SMS-PP data download messages to UICC. */
     private final UsimDataDownloadHandler mDataDownloadHandler;
 
@@ -56,18 +58,18 @@
      * Create a new GSM inbound SMS handler.
      */
     private GsmInboundSmsHandler(Context context, SmsStorageMonitor storageMonitor,
-            Phone phone) {
-        super("GsmInboundSmsHandler", context, storageMonitor, phone);
+            Phone phone, Looper looper) {
+        super("GsmInboundSmsHandler", context, storageMonitor, phone, looper);
         phone.mCi.setOnNewGsmSms(getHandler(), EVENT_NEW_SMS, null);
         mDataDownloadHandler = new UsimDataDownloadHandler(phone.mCi, phone.getPhoneId());
         mCellBroadcastServiceManager.enable();
 
         if (TEST_MODE) {
-            if (sTestBroadcastReceiver == null) {
-                sTestBroadcastReceiver = new GsmCbTestBroadcastReceiver();
+            if (mTestBroadcastReceiver == null) {
+                mTestBroadcastReceiver = new GsmCbTestBroadcastReceiver();
                 IntentFilter filter = new IntentFilter();
                 filter.addAction(TEST_ACTION);
-                context.registerReceiver(sTestBroadcastReceiver, filter,
+                context.registerReceiver(mTestBroadcastReceiver, filter,
                         Context.RECEIVER_EXPORTED);
             }
         }
@@ -127,8 +129,9 @@
      * Wait for state machine to enter startup state. We can't send any messages until then.
      */
     public static GsmInboundSmsHandler makeInboundSmsHandler(Context context,
-            SmsStorageMonitor storageMonitor, Phone phone) {
-        GsmInboundSmsHandler handler = new GsmInboundSmsHandler(context, storageMonitor, phone);
+            SmsStorageMonitor storageMonitor, Phone phone, Looper looper) {
+        GsmInboundSmsHandler handler =
+                new GsmInboundSmsHandler(context, storageMonitor, phone, looper);
         handler.start();
         return handler;
     }
@@ -153,7 +156,8 @@
      * or {@link Activity#RESULT_OK} for delayed acknowledgment to SMSC
      */
     @Override
-    protected int dispatchMessageRadioSpecific(SmsMessageBase smsb, @SmsSource int smsSource) {
+    protected int dispatchMessageRadioSpecific(SmsMessageBase smsb, @SmsSource int smsSource,
+            int token) {
         SmsMessage sms = (SmsMessage) smsb;
 
         if (sms.isTypeZero()) {
@@ -177,7 +181,7 @@
         // Send SMS-PP data download messages to UICC. See 3GPP TS 31.111 section 7.1.1.
         if (sms.isUsimDataDownload()) {
             UsimServiceTable ust = mPhone.getUsimServiceTable();
-            return mDataDownloadHandler.handleUsimDataDownload(ust, sms, smsSource);
+            return mDataDownloadHandler.handleUsimDataDownload(ust, sms, smsSource, token);
         }
 
         boolean handled = false;
@@ -268,4 +272,15 @@
                 android.telephony.SmsMessage.FORMAT_3GPP);
         mPhone.getSmsStats().onIncomingSmsVoicemail(false /* is3gpp2 */, smsSource);
     }
+
+    /**
+     * sets ImsManager object.
+     */
+    public boolean setImsManager(ImsManager imsManager) {
+        if (mDataDownloadHandler != null) {
+            mDataDownloadHandler.setImsManager(imsManager);
+            return true;
+        }
+        return false;
+    }
 }
diff --git a/src/java/com/android/internal/telephony/gsm/GsmMmiCode.java b/src/java/com/android/internal/telephony/gsm/GsmMmiCode.java
index 8e5fb00..9de3ee9 100644
--- a/src/java/com/android/internal/telephony/gsm/GsmMmiCode.java
+++ b/src/java/com/android/internal/telephony/gsm/GsmMmiCode.java
@@ -49,6 +49,7 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.CallForwardInfo;
 import com.android.internal.telephony.CallStateException;
+import com.android.internal.telephony.CallWaitingController;
 import com.android.internal.telephony.CommandException;
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.GsmCdmaPhone;
@@ -61,6 +62,7 @@
 import com.android.internal.telephony.util.ArrayUtils;
 import com.android.telephony.Rlog;
 
+import java.util.ArrayList;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -455,7 +457,7 @@
         return "";
     }
 
-    private String getActionStringFromReqType(SsData.RequestType rType) {
+    private static String getActionStringFromReqType(SsData.RequestType rType) {
         switch (rType) {
             case SS_ACTIVATION:
                 return ACTION_ACTIVATE;
@@ -520,6 +522,40 @@
         }
     }
 
+    public static SsData.ServiceType cfReasonToServiceType(int commandInterfaceCFReason) {
+        switch (commandInterfaceCFReason) {
+            case CommandsInterface.CF_REASON_UNCONDITIONAL:
+                return  SsData.ServiceType.SS_CFU;
+            case CommandsInterface.CF_REASON_BUSY:
+                return SsData.ServiceType.SS_CF_BUSY;
+            case CommandsInterface.CF_REASON_NO_REPLY:
+                return SsData.ServiceType.SS_CF_NO_REPLY;
+            case CommandsInterface.CF_REASON_NOT_REACHABLE:
+                return SsData.ServiceType.SS_CF_NOT_REACHABLE;
+            case CommandsInterface.CF_REASON_ALL:
+                return SsData.ServiceType.SS_CF_ALL;
+            case CommandsInterface.CF_REASON_ALL_CONDITIONAL:
+                return SsData.ServiceType.SS_CF_ALL_CONDITIONAL;
+            default:
+                return null;
+        }
+    }
+
+    public static SsData.RequestType cfActionToRequestType(int commandInterfaceCFAction) {
+        switch (commandInterfaceCFAction) {
+            case CommandsInterface.CF_ACTION_DISABLE:
+                return SsData.RequestType.SS_DEACTIVATION;
+            case CommandsInterface.CF_ACTION_ENABLE:
+                return SsData.RequestType.SS_ACTIVATION;
+            case CommandsInterface.CF_ACTION_REGISTRATION:
+                return SsData.RequestType.SS_REGISTRATION;
+            case CommandsInterface.CF_ACTION_ERASURE:
+                return SsData.RequestType.SS_ERASURE;
+            default:
+                return null;
+        }
+    }
+
     @UnsupportedAppUsage
     private static int
     siToServiceClass(String si) {
@@ -623,6 +659,29 @@
         }
     }
 
+    public static SsData.ServiceType cbFacilityToServiceType(String commandInterfaceCBFacility) {
+        switch(commandInterfaceCBFacility) {
+            case CommandsInterface.CB_FACILITY_BAOC:
+                return SsData.ServiceType.SS_BAOC;
+            case CommandsInterface.CB_FACILITY_BAOIC:
+                return SsData.ServiceType.SS_BAOIC;
+            case CommandsInterface.CB_FACILITY_BAOICxH:
+                return SsData.ServiceType.SS_BAOIC_EXC_HOME;
+            case CommandsInterface.CB_FACILITY_BAIC:
+                return SsData.ServiceType.SS_BAIC;
+            case CommandsInterface.CB_FACILITY_BAICr:
+                return SsData.ServiceType.SS_BAIC_ROAMING;
+            case CommandsInterface.CB_FACILITY_BA_ALL:
+                return SsData.ServiceType.SS_ALL_BARRING;
+            case CommandsInterface.CB_FACILITY_BA_MO:
+                return SsData.ServiceType.SS_OUTGOING_BARRING;
+            case CommandsInterface.CB_FACILITY_BA_MT:
+                return SsData.ServiceType.SS_INCOMING_BARRING;
+            default:
+                return null;
+        }
+    }
+
     //***** Constructor
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@@ -892,6 +951,17 @@
         return CommandsInterface.CLIR_DEFAULT;
     }
 
+    public static SsData.RequestType clirModeToRequestType(int commandInterfaceCLIRMode) {
+        switch (commandInterfaceCLIRMode) {
+            case CommandsInterface.CLIR_SUPPRESSION:
+                return SsData.RequestType.SS_ACTIVATION;
+            case CommandsInterface.CLIR_INVOCATION:
+                return SsData.RequestType.SS_DEACTIVATION;
+            default:
+                return null;
+        }
+    }
+
     /**
      * Returns true if the Service Code is FAC to dial as a normal call.
      *
@@ -1099,11 +1169,25 @@
                 int serviceClass = siToServiceClass(mSia);
 
                 if (isActivate() || isDeactivate()) {
+                    if (serviceClass == SERVICE_CLASS_NONE
+                            || (serviceClass & SERVICE_CLASS_VOICE) == SERVICE_CLASS_VOICE) {
+                        if (mPhone.getTerminalBasedCallWaitingState(true)
+                                != CallWaitingController.TERMINAL_BASED_NOT_SUPPORTED) {
+                            mPhone.setCallWaiting(isActivate(), serviceClass,
+                                    obtainMessage(EVENT_SET_COMPLETE, this));
+                            return;
+                        }
+                    }
                     mPhone.mCi.setCallWaiting(isActivate(), serviceClass,
                             obtainMessage(EVENT_SET_COMPLETE, this));
                 } else if (isInterrogate()) {
-                    mPhone.mCi.queryCallWaiting(serviceClass,
-                            obtainMessage(EVENT_QUERY_COMPLETE, this));
+                    if (mPhone.getTerminalBasedCallWaitingState(true)
+                            != CallWaitingController.TERMINAL_BASED_NOT_SUPPORTED) {
+                        mPhone.getCallWaiting(obtainMessage(EVENT_QUERY_COMPLETE, this));
+                    } else {
+                        mPhone.mCi.queryCallWaiting(serviceClass,
+                                obtainMessage(EVENT_QUERY_COMPLETE, this));
+                    }
                 } else {
                     throw new RuntimeException ("Invalid or Unsupported MMI Code");
                 }
@@ -1834,6 +1918,120 @@
         return this.mCallbackReceiver;
     }
 
+    /**
+     * Returns list of control strings for a supplementary service request
+     * as defined in TS 22.030 6.5
+     * @param requestType request type associated with the supplementary service
+     * @param serviceType supplementary service type
+     * @return list of control strings associated with the supplementary service.
+     */
+    public static ArrayList<String> getControlStrings(SsData.RequestType requestType,
+            SsData.ServiceType serviceType) {
+        ArrayList<String> controlStrings = new ArrayList<>();
+        if (requestType == null || serviceType == null) {
+            return controlStrings;
+        }
+
+        String actionStr = getActionStringFromReqType(requestType);
+        switch (serviceType) {
+            case SS_CFU:
+                controlStrings.add(actionStr + SC_CFU);
+                controlStrings.add(actionStr + SC_CF_All);
+                break;
+            case SS_CF_BUSY:
+                controlStrings.add(actionStr + SC_CFB);
+                controlStrings.add(actionStr + SC_CF_All_Conditional);
+                controlStrings.add(actionStr + SC_CF_All);
+                break;
+            case SS_CF_NO_REPLY:
+                controlStrings.add(actionStr + SC_CFNRy);
+                controlStrings.add(actionStr + SC_CF_All_Conditional);
+                controlStrings.add(actionStr + SC_CF_All);
+                break;
+            case SS_CF_NOT_REACHABLE:
+                controlStrings.add(actionStr + SC_CFNR);
+                controlStrings.add(actionStr + SC_CF_All_Conditional);
+                controlStrings.add(actionStr + SC_CF_All);
+                break;
+            case SS_CF_ALL:
+                controlStrings.add(actionStr + SC_CF_All);
+                break;
+            case SS_CF_ALL_CONDITIONAL:
+                controlStrings.add(actionStr + SC_CF_All_Conditional);
+                controlStrings.add(actionStr + SC_CF_All);
+                break;
+            case SS_CLIP:
+                controlStrings.add(actionStr + SC_CLIP);
+                break;
+            case SS_CLIR:
+                controlStrings.add(actionStr + SC_CLIR);
+                break;
+            case SS_WAIT:
+                controlStrings.add(actionStr + SC_WAIT);
+                break;
+            case SS_BAOC:
+                controlStrings.add(actionStr + SC_BAOC);
+                controlStrings.add(actionStr + SC_BA_MO);
+                controlStrings.add(actionStr + SC_BA_ALL);
+                break;
+            case SS_BAOIC:
+                controlStrings.add(actionStr + SC_BAOIC);
+                controlStrings.add(actionStr + SC_BA_MO);
+                controlStrings.add(actionStr + SC_BA_ALL);
+                break;
+            case SS_BAOIC_EXC_HOME:
+                controlStrings.add(actionStr + SC_BAOICxH);
+                controlStrings.add(actionStr + SC_BA_MO);
+                controlStrings.add(actionStr + SC_BA_ALL);
+                break;
+            case SS_BAIC:
+                controlStrings.add(actionStr + SC_BAIC);
+                controlStrings.add(actionStr + SC_BA_MT);
+                controlStrings.add(actionStr + SC_BA_ALL);
+                break;
+            case SS_BAIC_ROAMING:
+                controlStrings.add(actionStr + SC_BAICr);
+                controlStrings.add(actionStr + SC_BA_MT);
+                controlStrings.add(actionStr + SC_BA_ALL);
+                break;
+            case SS_ALL_BARRING:
+                controlStrings.add(actionStr + SC_BA_ALL);
+                break;
+            case SS_OUTGOING_BARRING:
+                controlStrings.add(actionStr + SC_BA_MO);
+                controlStrings.add(actionStr + SC_BA_ALL);
+                break;
+            case SS_INCOMING_BARRING:
+                controlStrings.add(actionStr + SC_BA_MT);
+                controlStrings.add(actionStr + SC_BA_ALL);
+                break;
+        }
+       return controlStrings;
+    }
+
+    /**
+     * Returns control strings for registration of new password as per TS 22.030 6.5.4
+     * @param requestType request type associated with the supplementary service
+     * @param serviceType supplementary service type
+     * @return list of control strings for new password registration.
+     */
+    public static ArrayList<String> getControlStringsForPwd(SsData.RequestType requestType,
+            SsData.ServiceType serviceType) {
+        ArrayList<String> controlStrings = new ArrayList<>();
+        if (requestType == null || serviceType == null) {
+            return controlStrings;
+        }
+
+        controlStrings = getControlStrings(SsData.RequestType.SS_ACTIVATION, serviceType);
+        String actionStr = getActionStringFromReqType(requestType);
+        ArrayList<String> controlStringsPwd = new ArrayList<>();
+        for(String controlString : controlStrings) {
+            // Prepend each control string with **SC_PWD
+            controlStringsPwd.add(actionStr + SC_PWD + controlString);
+        }
+        return controlStringsPwd;
+    }
+
     /***
      * TODO: It would be nice to have a method here that can take in a dialstring and
      * figure out if there is an MMI code embedded within it.  This code would replace
diff --git a/src/java/com/android/internal/telephony/gsm/GsmSMSDispatcher.java b/src/java/com/android/internal/telephony/gsm/GsmSMSDispatcher.java
index 1f237b2..5b1f36d 100644
--- a/src/java/com/android/internal/telephony/gsm/GsmSMSDispatcher.java
+++ b/src/java/com/android/internal/telephony/gsm/GsmSMSDispatcher.java
@@ -120,6 +120,21 @@
     }
 
     @Override
+    protected SmsMessageBase.SubmitPduBase getSubmitPdu(String scAddr, String destAddr,
+            String message, boolean statusReportRequested, SmsHeader smsHeader, int priority,
+            int validityPeriod, int messageRef) {
+        return SMSDispatcherUtil.getSubmitPduGsm(scAddr, destAddr, message, statusReportRequested,
+                validityPeriod, messageRef);
+    }
+
+    @Override
+    protected SmsMessageBase.SubmitPduBase getSubmitPdu(String scAddr, String destAddr,
+            int destPort, byte[] message, boolean statusReportRequested, int messageRef) {
+        return SMSDispatcherUtil.getSubmitPduGsm(scAddr, destAddr, destPort, message,
+                statusReportRequested, messageRef);
+    }
+
+    @Override
     protected TextEncodingDetails calculateLength(CharSequence messageBody, boolean use7bitOnly) {
         return SMSDispatcherUtil.calculateLengthGsm(messageBody, use7bitOnly);
     }
diff --git a/src/java/com/android/internal/telephony/gsm/UsimDataDownloadHandler.java b/src/java/com/android/internal/telephony/gsm/UsimDataDownloadHandler.java
index ed819c1..bae56d1 100644
--- a/src/java/com/android/internal/telephony/gsm/UsimDataDownloadHandler.java
+++ b/src/java/com/android/internal/telephony/gsm/UsimDataDownloadHandler.java
@@ -17,13 +17,19 @@
 package com.android.internal.telephony.gsm;
 
 import android.app.Activity;
+import android.content.res.Resources;
+import android.content.res.Resources.NotFoundException;
 import android.os.AsyncResult;
 import android.os.Handler;
 import android.os.Message;
 import android.provider.Telephony.Sms.Intents;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.SmsManager;
+import android.telephony.ims.stub.ImsSmsImplBase;
 
+import com.android.ims.ImsException;
+import com.android.ims.ImsManager;
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.InboundSmsHandler;
 import com.android.internal.telephony.PhoneFactory;
@@ -61,10 +67,14 @@
 
     private final CommandsInterface mCi;
     private final int mPhoneId;
+    private ImsManager mImsManager;
+    Resources mResource;
 
     public UsimDataDownloadHandler(CommandsInterface commandsInterface, int phoneId) {
         mCi = commandsInterface;
         mPhoneId = phoneId;
+        mImsManager = null; // will get initialized when ImsManager connection is ready
+        mResource = Resources.getSystem();
     }
 
     /**
@@ -79,7 +89,7 @@
      * @return {@code Activity.RESULT_OK} on success; {@code RESULT_SMS_GENERIC_ERROR} on failure
      */
     int handleUsimDataDownload(UsimServiceTable ust, SmsMessage smsMessage,
-            @InboundSmsHandler.SmsSource int smsSource) {
+            @InboundSmsHandler.SmsSource int smsSource, int token) {
         // If we receive an SMS-PP message before the UsimServiceTable has been loaded,
         // assume that the data download service is not present. This is very unlikely to
         // happen because the IMS connection will not be established until after the ISIM
@@ -87,7 +97,7 @@
         if (ust != null && ust.isAvailable(
                 UsimServiceTable.UsimService.DATA_DL_VIA_SMS_PP)) {
             Rlog.d(TAG, "Received SMS-PP data download, sending to UICC.");
-            return startDataDownload(smsMessage, smsSource);
+            return startDataDownload(smsMessage, smsSource, token);
         } else {
             Rlog.d(TAG, "DATA_DL_VIA_SMS_PP service not available, storing message to UICC.");
             String smsc = IccUtils.bytesToHexString(
@@ -95,7 +105,8 @@
                             smsMessage.getServiceCenterAddress()));
             mCi.writeSmsToSim(SmsManager.STATUS_ON_ICC_UNREAD, smsc,
                     IccUtils.bytesToHexString(smsMessage.getPdu()),
-                    obtainMessage(EVENT_WRITE_SMS_COMPLETE));
+                    obtainMessage(EVENT_WRITE_SMS_COMPLETE,
+                            new int[]{ smsSource, smsMessage.mMessageRef, token }));
             addUsimDataDownloadToMetrics(false, smsSource);
             return Activity.RESULT_OK;  // acknowledge after response from write to USIM
         }
@@ -111,9 +122,9 @@
      * @return {@code Activity.RESULT_OK} on success; {@code RESULT_SMS_GENERIC_ERROR} on failure
      */
     public int startDataDownload(SmsMessage smsMessage,
-            @InboundSmsHandler.SmsSource int smsSource) {
+            @InboundSmsHandler.SmsSource int smsSource, int token) {
         if (sendMessage(obtainMessage(EVENT_START_DATA_DOWNLOAD,
-                smsSource, 0 /* unused */, smsMessage))) {
+                smsSource, token, smsMessage))) {
             return Activity.RESULT_OK;  // we will send SMS ACK/ERROR based on UICC response
         } else {
             Rlog.e(TAG, "startDataDownload failed to send message to start data download.");
@@ -122,7 +133,7 @@
     }
 
     private void handleDataDownload(SmsMessage smsMessage,
-            @InboundSmsHandler.SmsSource int smsSource) {
+            @InboundSmsHandler.SmsSource int smsSource, int token) {
         int dcs = smsMessage.getDataCodingScheme();
         int pid = smsMessage.getProtocolIdentifier();
         byte[] pdu = smsMessage.getPdu();           // includes SC address
@@ -139,6 +150,7 @@
 
         byte[] envelope = new byte[totalLength];
         int index = 0;
+        Rlog.d(TAG, "smsSource: " + smsSource + "Token: " + token);
 
         // SMS-PP download tag and length (assumed to be < 256 bytes).
         envelope[index++] = (byte) BER_SMS_PP_DOWNLOAD_TAG;
@@ -173,14 +185,16 @@
         // Verify that we calculated the payload size correctly.
         if (index != envelope.length) {
             Rlog.e(TAG, "startDataDownload() calculated incorrect envelope length, aborting.");
-            acknowledgeSmsWithError(CommandsInterface.GSM_SMS_FAIL_CAUSE_UNSPECIFIED_ERROR);
+            acknowledgeSmsWithError(CommandsInterface.GSM_SMS_FAIL_CAUSE_UNSPECIFIED_ERROR,
+                    smsSource, token, smsMessage.mMessageRef);
             addUsimDataDownloadToMetrics(false, smsSource);
             return;
         }
 
         String encodedEnvelope = IccUtils.bytesToHexString(envelope);
         mCi.sendEnvelopeWithStatus(encodedEnvelope, obtainMessage(
-                EVENT_SEND_ENVELOPE_RESPONSE, new int[]{ dcs, pid }));
+                EVENT_SEND_ENVELOPE_RESPONSE, new int[]{ dcs, pid, smsSource,
+                    smsMessage.mMessageRef, token }));
 
         addUsimDataDownloadToMetrics(true, smsSource);
     }
@@ -211,7 +225,8 @@
      * @param response UICC response encoded as hexadecimal digits. First two bytes are the
      *  UICC SW1 and SW2 status bytes.
      */
-    private void sendSmsAckForEnvelopeResponse(IccIoResult response, int dcs, int pid) {
+    private void sendSmsAckForEnvelopeResponse(IccIoResult response, int dcs, int pid,
+            int smsSource, int token, int messageRef) {
         int sw1 = response.sw1;
         int sw2 = response.sw2;
 
@@ -221,7 +236,8 @@
             success = true;
         } else if (sw1 == 0x93 && sw2 == 0x00) {
             Rlog.e(TAG, "USIM data download failed: Toolkit busy");
-            acknowledgeSmsWithError(CommandsInterface.GSM_SMS_FAIL_CAUSE_USIM_APP_TOOLKIT_BUSY);
+            acknowledgeSmsWithError(CommandsInterface.GSM_SMS_FAIL_CAUSE_USIM_APP_TOOLKIT_BUSY,
+                    smsSource, token, messageRef);
             return;
         } else if (sw1 == 0x62 || sw1 == 0x63) {
             Rlog.e(TAG, "USIM data download failed: " + response.toString());
@@ -234,10 +250,11 @@
         byte[] responseBytes = response.payload;
         if (responseBytes == null || responseBytes.length == 0) {
             if (success) {
-                mCi.acknowledgeLastIncomingGsmSms(true, 0, null);
+                acknowledgeSmsWithSuccess(0, smsSource, token, messageRef);
             } else {
                 acknowledgeSmsWithError(
-                        CommandsInterface.GSM_SMS_FAIL_CAUSE_USIM_DATA_DOWNLOAD_ERROR);
+                        CommandsInterface.GSM_SMS_FAIL_CAUSE_USIM_DATA_DOWNLOAD_ERROR, smsSource,
+                        token, messageRef);
             }
             return;
         }
@@ -268,12 +285,32 @@
 
         System.arraycopy(responseBytes, 0, smsAckPdu, index, responseBytes.length);
 
-        mCi.acknowledgeIncomingGsmSmsWithPdu(success,
-                IccUtils.bytesToHexString(smsAckPdu), null);
+        if (smsSource == InboundSmsHandler.SOURCE_INJECTED_FROM_IMS && ackViaIms()) {
+            acknowledgeImsSms(token, messageRef, true, smsAckPdu);
+        } else {
+            mCi.acknowledgeIncomingGsmSmsWithPdu(success,
+                    IccUtils.bytesToHexString(smsAckPdu), null);
+        }
     }
 
-    private void acknowledgeSmsWithError(int cause) {
-        mCi.acknowledgeLastIncomingGsmSms(false, cause, null);
+    private void acknowledgeSmsWithSuccess(int cause, int smsSource, int token, int messageRef) {
+        Rlog.d(TAG, "acknowledgeSmsWithSuccess- cause: " + cause + " smsSource: " + smsSource
+                + " token: " + token + " messageRef: " + messageRef);
+        if (smsSource == InboundSmsHandler.SOURCE_INJECTED_FROM_IMS && ackViaIms()) {
+            acknowledgeImsSms(token, messageRef, true, null);
+        } else {
+            mCi.acknowledgeLastIncomingGsmSms(true, cause, null);
+        }
+    }
+
+    private void acknowledgeSmsWithError(int cause, int smsSource, int token, int messageRef) {
+        Rlog.d(TAG, "acknowledgeSmsWithError- cause: " + cause + " smsSource: " + smsSource
+                + " token: " + token + " messageRef: " + messageRef);
+        if (smsSource == InboundSmsHandler.SOURCE_INJECTED_FROM_IMS && ackViaIms()) {
+            acknowledgeImsSms(token, messageRef, false, null);
+        } else {
+            mCi.acknowledgeLastIncomingGsmSms(false, cause, null);
+        }
     }
 
     /**
@@ -300,6 +337,45 @@
     }
 
     /**
+     * Route resposes via ImsManager based on config
+     */
+    private boolean ackViaIms() {
+        boolean isViaIms;
+
+        try {
+            isViaIms = mResource.getBoolean(
+                    com.android.internal.R.bool.config_smppsim_response_via_ims);
+        } catch (NotFoundException e) {
+            isViaIms = false;
+        }
+
+        Rlog.d(TAG, "ackViaIms : " + isViaIms);
+        return isViaIms;
+    }
+
+    /**
+     * Acknowledges IMS SMS and delivers the result based on the envelope or SIM saving respose
+     * received from SIM for SMS-PP Data.
+     */
+    private void acknowledgeImsSms(int token, int messageRef, boolean success, byte[] pdu) {
+        int result = success ? ImsSmsImplBase.DELIVER_STATUS_OK :
+                    ImsSmsImplBase.DELIVER_STATUS_ERROR_GENERIC;
+        Rlog.d(TAG, "sending result via acknowledgeImsSms: " + result + " token: " + token);
+
+        try {
+            if (mImsManager != null) {
+                if (pdu != null && pdu.length > 0) {
+                    mImsManager.acknowledgeSms(token, messageRef, result, pdu);
+                } else {
+                    mImsManager.acknowledgeSms(token, messageRef, result);
+                }
+            }
+        } catch (ImsException e) {
+            Rlog.e(TAG, "Failed to acknowledgeSms(). Error: " + e.getMessage());
+        }
+    }
+
+    /**
      * Handle UICC envelope response and send SMS acknowledgement.
      *
      * @param msg the message to handle
@@ -307,35 +383,60 @@
     @Override
     public void handleMessage(Message msg) {
         AsyncResult ar;
+        int smsSource = InboundSmsHandler.SOURCE_INJECTED_FROM_UNKNOWN;
+        int token = 0;
+        int messageRef = 0;
+        int[] responseInfo;
 
         switch (msg.what) {
             case EVENT_START_DATA_DOWNLOAD:
-                handleDataDownload((SmsMessage) msg.obj, msg.arg1 /* smsSource */);
+                Rlog.d(TAG, "EVENT_START_DATA_DOWNLOAD");
+                handleDataDownload((SmsMessage) msg.obj, msg.arg1 /* smsSource */,
+                        msg.arg2 /* token */);
                 break;
 
             case EVENT_SEND_ENVELOPE_RESPONSE:
                 ar = (AsyncResult) msg.obj;
 
+                responseInfo = (int[]) ar.userObj;
+                smsSource = responseInfo[2];
+                messageRef = responseInfo[3];
+                token = responseInfo[4];
+
+                Rlog.d(TAG, "Received EVENT_SEND_ENVELOPE_RESPONSE from source : " + smsSource);
+
                 if (ar.exception != null) {
                     Rlog.e(TAG, "UICC Send Envelope failure, exception: " + ar.exception);
+
                     acknowledgeSmsWithError(
-                            CommandsInterface.GSM_SMS_FAIL_CAUSE_USIM_DATA_DOWNLOAD_ERROR);
+                            CommandsInterface.GSM_SMS_FAIL_CAUSE_USIM_DATA_DOWNLOAD_ERROR,
+                            smsSource, token, messageRef);
                     return;
                 }
 
-                int[] dcsPid = (int[]) ar.userObj;
-                sendSmsAckForEnvelopeResponse((IccIoResult) ar.result, dcsPid[0], dcsPid[1]);
+                Rlog.d(TAG, "Successful in sending envelope response");
+                sendSmsAckForEnvelopeResponse((IccIoResult) ar.result, responseInfo[0],
+                            responseInfo[1], smsSource, token, messageRef);
                 break;
 
             case EVENT_WRITE_SMS_COMPLETE:
                 ar = (AsyncResult) msg.obj;
+
+                responseInfo = (int[]) ar.userObj;
+                smsSource = responseInfo[0];
+                messageRef = responseInfo[1];
+                token = responseInfo[2];
+
+                Rlog.d(TAG, "Received EVENT_WRITE_SMS_COMPLETE from source : " + smsSource);
+
                 if (ar.exception == null) {
                     Rlog.d(TAG, "Successfully wrote SMS-PP message to UICC");
-                    mCi.acknowledgeLastIncomingGsmSms(true, 0, null);
+                    acknowledgeSmsWithSuccess(0, smsSource, token, messageRef);
                 } else {
                     Rlog.d(TAG, "Failed to write SMS-PP message to UICC", ar.exception);
-                    mCi.acknowledgeLastIncomingGsmSms(false,
-                            CommandsInterface.GSM_SMS_FAIL_CAUSE_UNSPECIFIED_ERROR, null);
+                    acknowledgeSmsWithError(
+                            CommandsInterface.GSM_SMS_FAIL_CAUSE_UNSPECIFIED_ERROR,
+                            smsSource, token, messageRef);
                 }
                 break;
 
@@ -343,4 +444,23 @@
                 Rlog.e(TAG, "Ignoring unexpected message, what=" + msg.what);
         }
     }
+
+    /**
+     * Called when ImsManager connection is ready. ImsManager object will be used to send ACK to IMS
+     * which doesn't use RIL interface.
+     * @param imsManager object
+     */
+    public void setImsManager(ImsManager imsManager) {
+        mImsManager = imsManager;
+    }
+
+    /**
+     * Called to set mocked object of type Resources during unit testing of this file.
+     * @param resource object
+     */
+    @VisibleForTesting
+    public void setResourcesForTest(Resources resource) {
+        mResource = resource;
+        Rlog.d(TAG, "setResourcesForTest");
+    }
 }
diff --git a/src/java/com/android/internal/telephony/gsm/UsimPhoneBookManager.java b/src/java/com/android/internal/telephony/gsm/UsimPhoneBookManager.java
old mode 100755
new mode 100644
index e594ab6..48be16c
--- a/src/java/com/android/internal/telephony/gsm/UsimPhoneBookManager.java
+++ b/src/java/com/android/internal/telephony/gsm/UsimPhoneBookManager.java
@@ -32,6 +32,7 @@
 import com.android.telephony.Rlog;
 
 import java.util.ArrayList;
+import java.util.Locale;
 
 /**
  * This class implements reading and parsing USIM records.
@@ -233,7 +234,7 @@
 
             int emailEfid = email.getEfid();
             log("EF_EMAIL exists in PBR. efid = 0x" +
-                    Integer.toHexString(emailEfid).toUpperCase());
+                    Integer.toHexString(emailEfid).toUpperCase(Locale.ROOT));
 
             /**
              * Make sure this EF_EMAIL was never read earlier. Sometimes two PBR record points
@@ -348,7 +349,7 @@
                 emailList = new ArrayList<String>();
             }
             log("Adding email #" + i + " list to index 0x" +
-                    Integer.toHexString(index).toUpperCase());
+                    Integer.toHexString(index).toUpperCase(Locale.ROOT));
             emailList.add(email);
             mEmailsForAdnRec.put(index, emailList);
         }
@@ -402,7 +403,7 @@
                 }
                 emailList.add(email);
                 log("Adding email list to index 0x" +
-                        Integer.toHexString(index).toUpperCase());
+                        Integer.toHexString(index).toUpperCase(Locale.ROOT));
                 mEmailsForAdnRec.put(index, emailList);
             }
         }
@@ -446,8 +447,9 @@
             System.arraycopy(emailList.toArray(), 0, emails, 0, emailList.size());
             rec.setEmails(emails);
             log("Adding email list to ADN (0x" +
-                    Integer.toHexString(mPhoneBookRecords.get(i).getEfid()).toUpperCase() +
-                    ") record #" + mPhoneBookRecords.get(i).getRecId());
+                    Integer.toHexString(mPhoneBookRecords.get(i).getEfid())
+                            .toUpperCase(Locale.ROOT) + ") record #"
+                    + mPhoneBookRecords.get(i).getRecId());
             mPhoneBookRecords.set(i, rec);
         }
     }
diff --git a/src/java/com/android/internal/telephony/ims/ImsEnablementTracker.java b/src/java/com/android/internal/telephony/ims/ImsEnablementTracker.java
new file mode 100644
index 0000000..732478d
--- /dev/null
+++ b/src/java/com/android/internal/telephony/ims/ImsEnablementTracker.java
@@ -0,0 +1,800 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.ims;
+
+import android.content.ComponentName;
+import android.os.Handler;
+import android.os.IBinder;
+import android.os.Looper;
+import android.os.Message;
+import android.os.RemoteException;
+import android.telephony.ims.aidl.IImsServiceController;
+import android.util.Log;
+import android.util.SparseArray;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.State;
+import com.android.internal.util.StateMachine;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * This class will abstract away all the new enablement logic and take the reset/enable/disable
+ * IMS commands as inputs.
+ * The IMS commands will call enableIms or disableIms to match the enablement state only when
+ * it changes.
+ */
+public class ImsEnablementTracker {
+    private static final String LOG_TAG = "ImsEnablementTracker";
+    private static final long REQUEST_THROTTLE_TIME_MS = 1 * 1000; // 1 seconds
+
+    private static final int COMMAND_NONE_MSG = 0;
+    // Indicate that the enableIms command has been received.
+    private static final int COMMAND_ENABLE_MSG = 1;
+    // Indicate that the disableIms command has been received.
+    private static final int COMMAND_DISABLE_MSG = 2;
+    // Indicate that the resetIms command has been received.
+    private static final int COMMAND_RESET_MSG = 3;
+    // Indicate that the internal enable message with delay has been received.
+    @VisibleForTesting
+    protected static final int COMMAND_ENABLING_DONE = 4;
+    // Indicate that the internal disable message with delay has been received.
+    @VisibleForTesting
+    protected static final int COMMAND_DISABLING_DONE = 5;
+    // Indicate that the internal reset message with delay has been received.
+    @VisibleForTesting
+    protected static final int COMMAND_RESETTING_DONE = 6;
+    // The ImsServiceController binder is connected.
+    private static final int COMMAND_CONNECTED_MSG = 7;
+    // The ImsServiceController binder is disconnected.
+    private static final int COMMAND_DISCONNECTED_MSG = 8;
+    // The subId is changed to INVALID_SUBSCRIPTION_ID.
+    private static final int COMMAND_INVALID_SUBID_MSG = 9;
+
+    private static final Map<Integer, String> EVENT_DESCRIPTION = new HashMap<>();
+    static {
+        EVENT_DESCRIPTION.put(COMMAND_NONE_MSG, "COMMAND_NONE_MSG");
+        EVENT_DESCRIPTION.put(COMMAND_ENABLE_MSG, "COMMAND_ENABLE_MSG");
+        EVENT_DESCRIPTION.put(COMMAND_DISABLE_MSG, "COMMAND_DISABLE_MSG");
+        EVENT_DESCRIPTION.put(COMMAND_RESET_MSG, "COMMAND_RESET_MSG");
+        EVENT_DESCRIPTION.put(COMMAND_ENABLING_DONE, "COMMAND_ENABLING_DONE");
+        EVENT_DESCRIPTION.put(COMMAND_DISABLING_DONE, "COMMAND_DISABLING_DONE");
+        EVENT_DESCRIPTION.put(COMMAND_RESETTING_DONE, "COMMAND_RESETTING_DONE");
+        EVENT_DESCRIPTION.put(COMMAND_CONNECTED_MSG, "COMMAND_CONNECTED_MSG");
+        EVENT_DESCRIPTION.put(COMMAND_DISCONNECTED_MSG, "COMMAND_DISCONNECTED_MSG");
+        EVENT_DESCRIPTION.put(COMMAND_INVALID_SUBID_MSG, "COMMAND_INVALID_SUBID_MSG");
+    }
+
+    @VisibleForTesting
+    protected static final int STATE_IMS_DISCONNECTED = 0;
+    @VisibleForTesting
+    protected static final int STATE_IMS_DEFAULT = 1;
+    @VisibleForTesting
+    protected static final int STATE_IMS_ENABLED = 2;
+    @VisibleForTesting
+    protected static final int STATE_IMS_DISABLING = 3;
+    @VisibleForTesting
+    protected static final int STATE_IMS_DISABLED = 4;
+    @VisibleForTesting
+    protected static final int STATE_IMS_ENABLING = 5;
+    @VisibleForTesting
+    protected static final int STATE_IMS_RESETTING = 6;
+
+    protected final Object mLock = new Object();
+    private IImsServiceController mIImsServiceController;
+    private long mLastImsOperationTimeMs = 0L;
+    private final ComponentName mComponentName;
+    private final SparseArray<ImsEnablementTrackerStateMachine> mStateMachines;
+
+    private final Looper mLooper;
+    private final int mState;
+
+    /**
+     * Provides Ims Enablement Tracker State Machine responsible for ims enable/disable command
+     * interactions with Ims service controller binder.
+     * The enable/disable/reset ims commands have a time interval of at least 1 second between
+     * processing each command.
+     * For example, the enableIms command is received and the binder's enableIms is called.
+     * After that, if the disableIms command is received, the binder's disableIms will be
+     * called after 1 second.
+     * A time of 1 second uses {@link Handler#sendMessageDelayed(Message, long)},
+     * and the enabled, disabled and reset states are responsible for waiting for
+     * that delay message.
+     */
+    class ImsEnablementTrackerStateMachine extends StateMachine {
+        /**
+         * The initial state of this class and waiting for an ims commands.
+         */
+        @VisibleForTesting
+        public final Default mDefault;
+        /**
+         * Indicates that {@link IImsServiceController#enableIms(int, int)} has been called and
+         * waiting for an ims commands.
+         * Common transitions are to
+         * {@link #mDisabling} state when the disable command is received
+         * or {@link #mResetting} state when the reset command is received.
+         * or {@link #mDisconnected} if the binder is disconnected.
+         */
+        @VisibleForTesting
+        public final Enabled mEnabled;
+        /**
+         * Indicates that the state waiting for a disableIms message.
+         * Common transitions are to
+         * {@link #mEnabled} when the enable command is received.
+         * or {@link #mResetting} when the reset command is received.
+         * or {@link #mDisabled} the previous binder API call has passed 1 second, and if
+         * {@link IImsServiceController#disableIms(int, int)} called.
+         * or {@link #mDisabling} received a disableIms message and the previous binder API call
+         * has not passed 1 second.Then send a disableIms message with delay.
+         * or {@link #mDisconnected} if the binder is disconnected.
+         */
+        @VisibleForTesting
+        public final Disabling mDisabling;
+        /**
+         * Indicates that {@link IImsServiceController#disableIms(int, int)} has been called and
+         * waiting for an ims commands.
+         * Common transitions are to
+         * {@link #mEnabling} state when the enable command is received.
+         * or {@link #mDisconnected} if the binder is disconnected.
+         */
+        @VisibleForTesting
+        public final Disabled mDisabled;
+        /**
+         * Indicates that the state waiting for an enableIms message.
+         * Common transitions are to
+         * {@link #mEnabled} the previous binder API call has passed 1 second, and
+         * {@link IImsServiceController#enableIms(int, int)} called.
+         * or {@link #mDisabled} when the disable command is received.
+         * or {@link #mEnabling} received an enableIms message and the previous binder API call
+         * has not passed 1 second.Then send an enableIms message with delay.
+         * or {@link #mDisconnected} if the binder is disconnected.
+         */
+        @VisibleForTesting
+        public final Enabling mEnabling;
+        /**
+         * Indicates that the state waiting for a resetIms message.
+         * Common transitions are to
+         * {@link #mDisabling} state when the disable command is received
+         * or {@link #mResetting} received a resetIms message and the previous binder API call
+         * has not passed 1 second.Then send a resetIms message with delay.
+         * or {@link #mEnabling} when the resetIms message is received and if
+         * {@link IImsServiceController#disableIms(int, int)} call is successful. And send an enable
+         * message with delay.
+         * or {@link #mDisconnected} if the binder is disconnected.
+         */
+        @VisibleForTesting
+        public final Resetting mResetting;
+        /**
+         * Indicates that {@link IImsServiceController} has not been set.
+         * Common transition is to
+         * {@link #mDefault} state when the binder is set.
+         * or {@link #mDisabling} If the disable command is received while the binder is
+         * disconnected
+         * or {@link #mEnabling} If the enable command is received while the binder is
+         * disconnected
+         */
+        @VisibleForTesting
+        public final Disconnected mDisconnected;
+
+        @VisibleForTesting
+        public int mSlotId;
+        @VisibleForTesting
+        public int mSubId;
+
+        private final int mPhoneId;
+
+        ImsEnablementTrackerStateMachine(String name, Looper looper, int state, int slotId) {
+            super(name, looper);
+            mPhoneId = slotId;
+            mDefault = new Default();
+            mEnabled = new Enabled();
+            mDisabling = new Disabling();
+            mDisabled = new Disabled();
+            mEnabling = new Enabling();
+            mResetting = new Resetting();
+            mDisconnected = new Disconnected();
+
+            addState(mDefault);
+            addState(mEnabled);
+            addState(mDisabling);
+            addState(mDisabled);
+            addState(mEnabling);
+            addState(mResetting);
+            addState(mDisconnected);
+
+            setInitialState(getState(state));
+        }
+
+        public void clearAllMessage() {
+            Log.d(LOG_TAG, "clearAllMessage");
+            removeMessages(COMMAND_ENABLE_MSG);
+            removeMessages(COMMAND_DISABLE_MSG);
+            removeMessages(COMMAND_RESET_MSG);
+            removeMessages(COMMAND_ENABLING_DONE);
+            removeMessages(COMMAND_DISABLING_DONE);
+            removeMessages(COMMAND_RESETTING_DONE);
+        }
+
+        public void serviceBinderConnected() {
+            clearAllMessage();
+            sendMessage(COMMAND_CONNECTED_MSG);
+        }
+
+        public void serviceBinderDisconnected() {
+            clearAllMessage();
+            sendMessage(COMMAND_DISCONNECTED_MSG);
+        }
+
+        @VisibleForTesting
+        public boolean isState(int state) {
+            if (state == mDefault.mStateNo) {
+                return (getCurrentState() == mDefault) ? true : false;
+            } else if (state == mEnabled.mStateNo) {
+                return (getCurrentState() == mEnabled) ? true : false;
+            } else if (state == mDisabling.mStateNo) {
+                return (getCurrentState() == mDisabling) ? true : false;
+            } else if (state == mDisabled.mStateNo) {
+                return (getCurrentState() == mDisabled) ? true : false;
+            } else if (state == mEnabling.mStateNo) {
+                return (getCurrentState() == mEnabling) ? true : false;
+            } else if (state == mResetting.mStateNo) {
+                return (getCurrentState() == mResetting) ? true : false;
+            }
+            return false;
+        }
+
+        private State getState(int state) {
+            switch (state) {
+                case ImsEnablementTracker.STATE_IMS_DEFAULT:
+                    return mDefault;
+                case ImsEnablementTracker.STATE_IMS_ENABLED:
+                    return mEnabled;
+                case ImsEnablementTracker.STATE_IMS_DISABLING:
+                    return mDisabling;
+                case ImsEnablementTracker.STATE_IMS_DISABLED:
+                    return mDisabled;
+                case ImsEnablementTracker.STATE_IMS_ENABLING:
+                    return mEnabling;
+                case ImsEnablementTracker.STATE_IMS_RESETTING:
+                    return mResetting;
+                default:
+                    return mDisconnected;
+            }
+        }
+
+        class Default extends State {
+            public int mStateNo = STATE_IMS_DEFAULT;
+            @Override
+            public void enter() {
+                Log.d(LOG_TAG, "Default state:enter");
+            }
+
+            @Override
+            public void exit() {
+                Log.d(LOG_TAG, "Default state:exit");
+            }
+
+            @Override
+            public boolean processMessage(Message message) {
+                Log.d(LOG_TAG, "[" + mPhoneId + "]Default state:processMessage. msg.what="
+                        + EVENT_DESCRIPTION.get(message.what) + ",component:" + mComponentName);
+                switch (message.what) {
+                    // When enableIms() is called, enableIms of binder is call and the state
+                    // change to the enabled state.
+                    case COMMAND_ENABLE_MSG:
+                        sendEnableIms(message.arg1, message.arg2);
+                        transitionTo(mEnabled);
+                        return HANDLED;
+                    // When disableIms() is called, disableIms of binder is call and the state
+                    // change to the disabled state.
+                    case COMMAND_DISABLE_MSG:
+                        sendDisableIms(message.arg1, message.arg2);
+                        transitionTo(mDisabled);
+                        return HANDLED;
+                    case COMMAND_DISCONNECTED_MSG:
+                        transitionTo(mDisconnected);
+                        return HANDLED;
+                    default:
+                        return NOT_HANDLED;
+                }
+            }
+        }
+
+        class Enabled extends State {
+            public int mStateNo = STATE_IMS_ENABLED;
+            @Override
+            public void enter() {
+                Log.d(LOG_TAG, "Enabled state:enter");
+            }
+
+            @Override
+            public void exit() {
+                Log.d(LOG_TAG, "Enabled state:exit");
+            }
+
+            @Override
+            public boolean processMessage(Message message) {
+                Log.d(LOG_TAG, "[" + mPhoneId + "]Enabled state:processMessage. msg.what="
+                        + EVENT_DESCRIPTION.get(message.what) + ",component:" + mComponentName);
+                mSlotId = message.arg1;
+                mSubId = message.arg2;
+                switch (message.what) {
+                    // the disableIms() is called.
+                    case COMMAND_DISABLE_MSG:
+                        transitionTo(mDisabling);
+                        return HANDLED;
+                    // the resetIms() is called.
+                    case COMMAND_RESET_MSG:
+                        transitionTo(mResetting);
+                        return HANDLED;
+                    case COMMAND_DISCONNECTED_MSG:
+                        transitionTo(mDisconnected);
+                        return HANDLED;
+                    case COMMAND_INVALID_SUBID_MSG:
+                        clearAllMessage();
+                        transitionTo(mDefault);
+                        return HANDLED;
+                    default:
+                        return NOT_HANDLED;
+                }
+            }
+        }
+
+        class Disabling extends State {
+            public int mStateNo = STATE_IMS_DISABLING;
+            @Override
+            public void enter() {
+                Log.d(LOG_TAG, "Disabling state:enter");
+                sendMessageDelayed(COMMAND_DISABLING_DONE, mSlotId, mSubId,
+                        getRemainThrottleTime());
+            }
+
+            @Override
+            public void exit() {
+                Log.d(LOG_TAG, "Disabling state:exit");
+            }
+
+            @Override
+            public boolean processMessage(Message message) {
+                Log.d(LOG_TAG, "[" + mPhoneId + "]Disabling state:processMessage. msg.what="
+                        + EVENT_DESCRIPTION.get(message.what) + ",component:" + mComponentName);
+                mSlotId = message.arg1;
+                mSubId = message.arg2;
+                switch (message.what) {
+                    // In the enabled state, disableIms() is called, but the throttle timer has
+                    // not expired, so a delay_disable message is sent.
+                    // At this point enableIms() was called, so it cancels the message and just
+                    // changes the state to the enabled.
+                    case COMMAND_ENABLE_MSG:
+                        clearAllMessage();
+                        transitionTo(mEnabled);
+                        return HANDLED;
+                    case COMMAND_DISABLING_DONE:
+                        // If the disable command is received before disableIms is processed,
+                        // it will be ignored because the disable command processing is in progress.
+                        removeMessages(COMMAND_DISABLE_MSG);
+                        sendDisableIms(mSlotId, mSubId);
+                        transitionTo(mDisabled);
+                        return HANDLED;
+                    case COMMAND_RESET_MSG:
+                        clearAllMessage();
+                        transitionTo(mResetting);
+                        return HANDLED;
+                    case COMMAND_DISCONNECTED_MSG:
+                        transitionTo(mDisconnected);
+                        return HANDLED;
+                    case COMMAND_INVALID_SUBID_MSG:
+                        clearAllMessage();
+                        transitionTo(mDefault);
+                        return HANDLED;
+                    default:
+                        return NOT_HANDLED;
+                }
+            }
+        }
+
+        class Disabled extends State {
+            public int mStateNo = STATE_IMS_DISABLED;
+            @Override
+            public void enter() {
+                Log.d(LOG_TAG, "Disabled state:enter");
+            }
+
+            @Override
+            public void exit() {
+                Log.d(LOG_TAG, "Disabled state:exit");
+            }
+
+            @Override
+            public boolean processMessage(Message message) {
+                Log.d(LOG_TAG, "[" + mPhoneId + "]Disabled state:processMessage. msg.what="
+                        + EVENT_DESCRIPTION.get(message.what) + ",component:" + mComponentName);
+                mSlotId = message.arg1;
+                mSubId = message.arg2;
+                switch (message.what) {
+                    case COMMAND_ENABLE_MSG:
+                        transitionTo(mEnabling);
+                        return HANDLED;
+                    case COMMAND_DISCONNECTED_MSG:
+                        transitionTo(mDisconnected);
+                        return HANDLED;
+                    case COMMAND_INVALID_SUBID_MSG:
+                        clearAllMessage();
+                        transitionTo(mDefault);
+                        return HANDLED;
+                    default:
+                        return NOT_HANDLED;
+                }
+            }
+        }
+
+        class Enabling extends State {
+            public int mStateNo = STATE_IMS_ENABLING;
+            @Override
+            public void enter() {
+                Log.d(LOG_TAG, "Enabling state:enter");
+                sendMessageDelayed(COMMAND_ENABLING_DONE, mSlotId, mSubId, getRemainThrottleTime());
+            }
+
+            @Override
+            public void exit() {
+                Log.d(LOG_TAG, "Enabling state:exit");
+            }
+
+            @Override
+            public boolean processMessage(Message message) {
+                Log.d(LOG_TAG, "[" + mPhoneId + "]Enabling state:processMessage. msg.what="
+                        + EVENT_DESCRIPTION.get(message.what) + ",component:" + mComponentName);
+                mSlotId = message.arg1;
+                mSubId = message.arg2;
+                switch (message.what) {
+                    case COMMAND_DISABLE_MSG:
+                        clearAllMessage();
+                        transitionTo(mDisabled);
+                        return HANDLED;
+                    case COMMAND_ENABLING_DONE:
+                        // If the enable command is received before enableIms is processed,
+                        // it will be ignored because the enable command processing is in progress.
+                        removeMessages(COMMAND_ENABLE_MSG);
+                        sendEnableIms(message.arg1, message.arg2);
+                        transitionTo(mEnabled);
+                        return HANDLED;
+                    case COMMAND_DISCONNECTED_MSG:
+                        transitionTo(mDisconnected);
+                        return HANDLED;
+                    case COMMAND_INVALID_SUBID_MSG:
+                        clearAllMessage();
+                        transitionTo(mDefault);
+                        return HANDLED;
+                    default:
+                        return NOT_HANDLED;
+                }
+            }
+        }
+
+        class Resetting extends State {
+            public int mStateNo = STATE_IMS_RESETTING;
+            @Override
+            public void enter() {
+                Log.d(LOG_TAG, "Resetting state:enter");
+                sendMessageDelayed(COMMAND_RESETTING_DONE, mSlotId, mSubId,
+                        getRemainThrottleTime());
+            }
+
+            @Override
+            public void exit() {
+                Log.d(LOG_TAG, "Resetting state:exit");
+            }
+
+            @Override
+            public boolean processMessage(Message message) {
+                Log.d(LOG_TAG, "[" + mPhoneId + "]Resetting state:processMessage. msg.what="
+                        + EVENT_DESCRIPTION.get(message.what) + ",component:" + mComponentName);
+                mSlotId = message.arg1;
+                mSubId = message.arg2;
+                switch (message.what) {
+                    case COMMAND_DISABLE_MSG:
+                        clearAllMessage();
+                        transitionTo(mDisabling);
+                        return HANDLED;
+                    case COMMAND_RESETTING_DONE:
+                        // If the reset command is received before disableIms is processed,
+                        // it will be ignored because the reset command processing is in progress.
+                        removeMessages(COMMAND_RESET_MSG);
+                        sendDisableIms(mSlotId, mSubId);
+                        transitionTo(mEnabling);
+                        return HANDLED;
+                    case COMMAND_DISCONNECTED_MSG:
+                        transitionTo(mDisconnected);
+                        return HANDLED;
+                    case COMMAND_INVALID_SUBID_MSG:
+                        clearAllMessage();
+                        transitionTo(mDefault);
+                        return HANDLED;
+                    default:
+                        return NOT_HANDLED;
+                }
+            }
+        }
+
+        class Disconnected extends State {
+            public int mStateNo = STATE_IMS_DISCONNECTED;
+            private int mLastMsg = COMMAND_NONE_MSG;
+            @Override
+            public void enter() {
+                Log.d(LOG_TAG, "Disconnected state:enter");
+                clearAllMessage();
+            }
+
+            @Override
+            public void exit() {
+                Log.d(LOG_TAG, "Disconnected state:exit");
+                mLastMsg = COMMAND_NONE_MSG;
+            }
+
+            @Override
+            public boolean processMessage(Message message) {
+                Log.d(LOG_TAG, "[" + mPhoneId + "]Disconnected state:processMessage. msg.what="
+                        + EVENT_DESCRIPTION.get(message.what) + ",component:" + mComponentName);
+                switch (message.what) {
+                    case COMMAND_CONNECTED_MSG:
+                        clearAllMessage();
+                        transitionTo(mDefault);
+                        if (mLastMsg != COMMAND_NONE_MSG) {
+                            sendMessageDelayed(mLastMsg, mSlotId, mSubId, 0);
+                        }
+                        return HANDLED;
+                    case COMMAND_ENABLE_MSG:
+                    case COMMAND_DISABLE_MSG:
+                    case COMMAND_RESET_MSG:
+                        mLastMsg = message.what;
+                        mSlotId = message.arg1;
+                        mSubId = message.arg2;
+                        return HANDLED;
+                    default:
+                        return NOT_HANDLED;
+                }
+            }
+        }
+    }
+
+    public ImsEnablementTracker(Looper looper, ComponentName componentName) {
+        mIImsServiceController = null;
+        mStateMachines = new SparseArray<>();
+        mLooper = looper;
+        mState = ImsEnablementTracker.STATE_IMS_DISCONNECTED;
+        mComponentName = componentName;
+    }
+
+    @VisibleForTesting
+    public ImsEnablementTracker(Looper looper, IImsServiceController controller, int state,
+            int numSlots) {
+        mIImsServiceController = controller;
+        mStateMachines = new SparseArray<>();
+        mLooper = looper;
+        mState = state;
+        mComponentName = null;
+        ImsEnablementTrackerStateMachine enablementStateMachine = null;
+        for (int i = 0; i < numSlots; i++) {
+            enablementStateMachine = new ImsEnablementTrackerStateMachine("ImsEnablementTracker",
+                    mLooper, mState, i);
+            mStateMachines.put(i, enablementStateMachine);
+        }
+    }
+
+    /**
+     * Set the number of SIM slots.
+     * @param numOfSlots the number of SIM slots.
+     */
+    public void setNumOfSlots(int numOfSlots) {
+        int oldNumSlots = mStateMachines.size();
+        Log.d(LOG_TAG, "set the slots: old[" + oldNumSlots + "], new[" + numOfSlots + "],"
+                + "component:" + mComponentName);
+        if (numOfSlots == oldNumSlots) {
+            return;
+        }
+        ImsEnablementTrackerStateMachine enablementStateMachine = null;
+        if (oldNumSlots < numOfSlots) {
+            for (int i = oldNumSlots; i < numOfSlots; i++) {
+                enablementStateMachine = new ImsEnablementTrackerStateMachine(
+                        "ImsEnablementTracker", mLooper, mState, i);
+                enablementStateMachine.start();
+                mStateMachines.put(i, enablementStateMachine);
+            }
+        } else if (oldNumSlots > numOfSlots) {
+            for (int i = (oldNumSlots - 1); i > (numOfSlots - 1); i--) {
+                enablementStateMachine = mStateMachines.get(i);
+                mStateMachines.remove(i);
+                enablementStateMachine.quitNow();
+            }
+        }
+    }
+
+    /**
+     * This API is for testing purposes only and is used to start a state machine.
+     */
+    @VisibleForTesting
+    public void startStateMachineAsConnected(int slotId) {
+        mStateMachines.get(slotId).start();
+        mStateMachines.get(slotId).sendMessage(COMMAND_CONNECTED_MSG);
+    }
+
+    @VisibleForTesting
+    public Handler getHandler(int slotId) {
+        return mStateMachines.get(slotId).getHandler();
+    }
+
+    /**
+     * Check that the current state and the input state are the same.
+     * @param state the input state.
+     * @return true if the current state and input state are the same or false.
+     */
+    @VisibleForTesting
+    public boolean isState(int slotId, int state) {
+        return mStateMachines.get(slotId).isState(state);
+    }
+
+    /**
+     * Notify the state machine that the subId has changed to invalid.
+     * @param slotId subscription id
+     */
+    public void subIdChangedToInvalid(int slotId) {
+        Log.d(LOG_TAG, "[" + slotId + "] subId changed to invalid, component:" + mComponentName);
+        ImsEnablementTrackerStateMachine stateMachine = mStateMachines.get(slotId);
+        if (stateMachine != null) {
+            stateMachine.sendMessage(COMMAND_INVALID_SUBID_MSG, slotId);
+        } else {
+            Log.w(LOG_TAG, "There is no state machine associated with this slotId.");
+        }
+    }
+
+    /**
+     * Notify ImsService to enable IMS for the framework. This will trigger IMS registration and
+     * trigger ImsFeature status updates.
+     * @param slotId slot id
+     * @param subId subscription id
+     */
+    public void enableIms(int slotId, int subId) {
+        Log.d(LOG_TAG, "[" + slotId + "][" + subId + "]enableIms, component:" + mComponentName);
+        ImsEnablementTrackerStateMachine stateMachine = mStateMachines.get(slotId);
+        if (stateMachine != null) {
+            stateMachine.sendMessage(COMMAND_ENABLE_MSG, slotId, subId);
+        } else {
+            Log.w(LOG_TAG, "There is no state machine associated with this slotId.");
+        }
+    }
+
+    /**
+     * Notify ImsService to disable IMS for the framework. This will trigger IMS de-registration and
+     * trigger ImsFeature capability status to become false.
+     * @param slotId slot id
+     * @param subId subscription id
+     */
+    public void disableIms(int slotId, int subId) {
+        Log.d(LOG_TAG, "[" + slotId + "][" + subId + "]disableIms, component:" + mComponentName);
+        ImsEnablementTrackerStateMachine stateMachine = mStateMachines.get(slotId);
+        if (stateMachine != null) {
+            stateMachine.sendMessage(COMMAND_DISABLE_MSG, slotId, subId);
+        } else {
+            Log.w(LOG_TAG, "There is no state machine associated with this slotId.");
+        }
+    }
+
+    /**
+     * Notify ImsService to disable IMS for the framework if current state is enabled.
+     * And notify ImsService back to enable IMS for the framework.
+     * @param slotId slot id
+     * @param subId subscription id
+     */
+    public void resetIms(int slotId, int subId) {
+        Log.d(LOG_TAG, "[" + slotId + "][" + subId + "]resetIms, component:" + mComponentName);
+        ImsEnablementTrackerStateMachine stateMachine = mStateMachines.get(slotId);
+        if (stateMachine != null) {
+            stateMachine.sendMessage(COMMAND_RESET_MSG, slotId, subId);
+        } else {
+            Log.w(LOG_TAG, "There is no state machine associated with this slotId.");
+        }
+    }
+
+    /**
+     * Sets the IImsServiceController instance.
+     */
+    protected void setServiceController(IBinder serviceController) {
+        synchronized (mLock) {
+            mIImsServiceController = IImsServiceController.Stub.asInterface(serviceController);
+            Log.d(LOG_TAG, "setServiceController with Binder:" + mIImsServiceController
+                    + ", component:" + mComponentName);
+            ImsEnablementTrackerStateMachine stateMachine = null;
+            for (int i = 0; i < mStateMachines.size(); i++) {
+                stateMachine = mStateMachines.get(i);
+                if (stateMachine == null) {
+                    Log.w(LOG_TAG, "There is no state machine associated with"
+                            + "the slotId[" + i + "]");
+                    continue;
+                }
+                if (isServiceControllerAvailable()) {
+                    stateMachine.serviceBinderConnected();
+                } else {
+                    stateMachine.serviceBinderDisconnected();
+                }
+            }
+        }
+    }
+
+    @VisibleForTesting
+    protected long getLastOperationTimeMillis() {
+        return mLastImsOperationTimeMs;
+    }
+
+    /**
+     * Get remaining throttle time value
+     * @return remaining throttle time value
+     */
+    @VisibleForTesting
+    public long getRemainThrottleTime() {
+        long remainTime = REQUEST_THROTTLE_TIME_MS - (System.currentTimeMillis()
+                - getLastOperationTimeMillis());
+        Log.d(LOG_TAG, "getRemainThrottleTime:" + remainTime);
+        if (remainTime < 0) {
+            return 0;
+        }
+        return remainTime;
+    }
+
+    /**
+     * Check to see if the service controller is available.
+     * @return true if available, false otherwise
+     */
+    private boolean isServiceControllerAvailable() {
+        if (mIImsServiceController != null) {
+            return true;
+        }
+        Log.d(LOG_TAG, "isServiceControllerAvailable : binder is not alive");
+        return false;
+    }
+
+    private void sendEnableIms(int slotId, int subId) {
+        try {
+            synchronized (mLock) {
+                if (isServiceControllerAvailable()) {
+                    Log.d(LOG_TAG, "[" + slotId + "][" + subId + "]sendEnableIms,"
+                            + "componentName[" + mComponentName + "]");
+                    mIImsServiceController.enableIms(slotId, subId);
+                    mLastImsOperationTimeMs = System.currentTimeMillis();
+                }
+            }
+        } catch (RemoteException e) {
+            Log.w(LOG_TAG, "Couldn't enable IMS: " + e.getMessage());
+        }
+    }
+
+    private void sendDisableIms(int slotId, int subId) {
+        try {
+            synchronized (mLock) {
+                if (isServiceControllerAvailable()) {
+                    Log.d(LOG_TAG, "[" + slotId + "][" + subId + "]sendDisableIms,"
+                            + "componentName[" + mComponentName + "]");
+                    mIImsServiceController.disableIms(slotId, subId);
+                    mLastImsOperationTimeMs = System.currentTimeMillis();
+                }
+            }
+        } catch (RemoteException e) {
+            Log.w(LOG_TAG, "Couldn't disable IMS: " + e.getMessage());
+        }
+    }
+}
diff --git a/src/java/com/android/internal/telephony/ims/ImsResolver.java b/src/java/com/android/internal/telephony/ims/ImsResolver.java
index c3331d9..f5f3ce7 100644
--- a/src/java/com/android/internal/telephony/ims/ImsResolver.java
+++ b/src/java/com/android/internal/telephony/ims/ImsResolver.java
@@ -312,7 +312,8 @@
     @VisibleForTesting
     public interface SubscriptionManagerProxy {
         /**
-         * Mock-able interface for {@link SubscriptionManager#getSubId(int)} used for testing.
+         * Mock-able interface for {@link SubscriptionManager#getSubscriptionId(int)} used for
+         * testing.
          */
         int getSubId(int slotId);
         /**
@@ -346,12 +347,7 @@
     private SubscriptionManagerProxy mSubscriptionManagerProxy = new SubscriptionManagerProxy() {
         @Override
         public int getSubId(int slotId) {
-            int[] subIds = SubscriptionManager.getSubId(slotId);
-            if (subIds != null) {
-                // This is done in all other places getSubId is used.
-                return subIds[0];
-            }
-            return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+            return SubscriptionManager.getSubscriptionId(slotId);
         }
 
         @Override
@@ -742,6 +738,15 @@
     }
 
     /**
+     * Notify ImsService to disable IMS for the framework.
+     * And notify ImsService back to enable IMS for the framework.
+     */
+    public void resetIms(int slotId) {
+        getImsServiceControllers(slotId).forEach(
+                (controller) -> controller.resetIms(slotId, getSubId(slotId)));
+    }
+
+    /**
      * Returns the ImsRegistration structure associated with the slotId and feature specified.
      */
     public @Nullable IImsRegistration getImsRegistration(int slotId, int feature) {
diff --git a/src/java/com/android/internal/telephony/ims/ImsServiceController.java b/src/java/com/android/internal/telephony/ims/ImsServiceController.java
index 92e7d71..3572158 100644
--- a/src/java/com/android/internal/telephony/ims/ImsServiceController.java
+++ b/src/java/com/android/internal/telephony/ims/ImsServiceController.java
@@ -241,6 +241,7 @@
     private Set<ImsFeatureConfiguration.FeatureSlotPair> mImsFeatures;
     private SparseIntArray mSlotIdToSubIdMap;
     private IImsServiceController mIImsServiceController;
+    private final ImsEnablementTracker mImsEnablementTracker;
     // The Capabilities bitmask of the connected ImsService (see ImsService#ImsServiceCapability).
     private long mServiceCapabilities;
     private ImsServiceConnection mImsServiceConnection;
@@ -332,7 +333,7 @@
         mPermissionManager = (LegacyPermissionManager) mContext.getSystemService(
                 Context.LEGACY_PERMISSION_SERVICE);
         mRepo = repo;
-
+        mImsEnablementTracker = new ImsEnablementTracker(mHandlerThread.getLooper(), componentName);
         mPackageManager = mContext.getPackageManager();
         if (mPackageManager != null) {
             mChangedPackages = mPackageManager.getChangedPackages(mLastSequenceNumber);
@@ -359,6 +360,7 @@
                 mRestartImsServiceRunnable);
         mPermissionManager = null;
         mRepo = repo;
+        mImsEnablementTracker = new ImsEnablementTracker(handler.getLooper(), componentName);
     }
 
     /**
@@ -378,6 +380,8 @@
                 sanitizeFeatureConfig(imsFeatureSet);
                 mImsFeatures = imsFeatureSet;
                 mSlotIdToSubIdMap = slotIdToSubIdMap;
+                // Set the number of slots that support the feature
+                mImsEnablementTracker.setNumOfSlots(mSlotIdToSubIdMap.size());
                 grantPermissionsToService();
                 Intent imsServiceIntent = new Intent(getServiceInterface()).setComponent(
                         mComponentName);
@@ -464,6 +468,14 @@
                             + newSubId);
                     Log.i(LOG_TAG, "subId changed for slot: " + slotID + ", " + oldSubId + " -> "
                             + newSubId);
+                    if (newSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+                        /* An INVALID subId can also be set in bind(), however
+                        the ImsEnablementTracker will move into the DEFAULT state, so we only
+                        need to track changes in subId that result in requiring we move
+                        the state machine back to DEFAULT.
+                         */
+                        mImsEnablementTracker.subIdChangedToInvalid(slotID);
+                    }
                 }
             }
             mSlotIdToSubIdMap = slotIdToSubIdMap;
@@ -553,15 +565,7 @@
      * trigger ImsFeature status updates.
      */
     public void enableIms(int slotId, int subId) {
-        try {
-            synchronized (mLock) {
-                if (isServiceControllerAvailable()) {
-                    mIImsServiceController.enableIms(slotId, subId);
-                }
-            }
-        } catch (RemoteException e) {
-            Log.w(LOG_TAG, "Couldn't enable IMS: " + e.getMessage());
-        }
+        mImsEnablementTracker.enableIms(slotId, subId);
     }
 
     /**
@@ -569,15 +573,15 @@
      * trigger ImsFeature capability status to become false.
      */
     public void disableIms(int slotId, int subId) {
-        try {
-            synchronized (mLock) {
-                if (isServiceControllerAvailable()) {
-                    mIImsServiceController.disableIms(slotId, subId);
-                }
-            }
-        } catch (RemoteException e) {
-            Log.w(LOG_TAG, "Couldn't disable IMS: " + e.getMessage());
-        }
+        mImsEnablementTracker.disableIms(slotId, subId);
+    }
+
+    /**
+     * Notify ImsService to disable IMS for the framework.
+     * And notify ImsService back to enable IMS for the framework
+     */
+    public void resetIms(int slotId, int subId) {
+        mImsEnablementTracker.resetIms(slotId, subId);
     }
 
     /**
@@ -651,6 +655,7 @@
      */
     protected void setServiceController(IBinder serviceController) {
         mIImsServiceController = IImsServiceController.Stub.asInterface(serviceController);
+        mImsEnablementTracker.setServiceController(serviceController);
     }
 
     /**
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsCallInfo.java b/src/java/com/android/internal/telephony/imsphone/ImsCallInfo.java
new file mode 100644
index 0000000..79ab9c5
--- /dev/null
+++ b/src/java/com/android/internal/telephony/imsphone/ImsCallInfo.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.imsphone;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.telephony.AccessNetworkConstants;
+import android.telephony.ServiceState;
+
+import com.android.internal.telephony.Call;
+
+/**
+ * Contains call state to be notified to modem.
+ */
+public class ImsCallInfo {
+
+    private final int mIndex;
+    private @Nullable ImsPhoneConnection mConnection = null;
+    private Call.State mState = Call.State.IDLE;
+    private boolean mIsHeldByRemote = false;
+
+    public ImsCallInfo(int index) {
+        mIndex = index;
+    }
+
+    /** Clears the call state. */
+    public void reset() {
+        mConnection = null;
+        mState = Call.State.IDLE;
+        mIsHeldByRemote = false;
+    }
+
+    /**
+     * Updates the state of the IMS call.
+     *
+     * @param c The instance of {@link ImsPhoneConnection}.
+     */
+    public void update(@NonNull ImsPhoneConnection c) {
+        mConnection = c;
+        mState = c.getState();
+    }
+
+    /**
+     * Updates the state of the IMS call.
+     *
+     * @param c The instance of {@link ImsPhoneConnection}.
+     * @param holdReceived {@code true} if the remote party held the call.
+     * @param resumeReceived {@code true} if the remote party resumed the call.
+     */
+    public boolean update(@NonNull ImsPhoneConnection c,
+            boolean holdReceived, boolean resumeReceived) {
+        Call.State state = c.getState();
+        boolean changed = mState != state;
+        mState = state;
+
+        if (holdReceived && !mIsHeldByRemote) {
+            changed = true;
+            mIsHeldByRemote = true;
+        } else if (resumeReceived && mIsHeldByRemote) {
+            changed = true;
+            mIsHeldByRemote = false;
+        }
+
+        return changed;
+    }
+
+    /** Called when clearing orphaned connection. */
+    public void onDisconnect() {
+        mState = Call.State.DISCONNECTED;
+    }
+
+    /** @return the call index. */
+    public int getIndex() {
+        return mIndex;
+    }
+
+    /** @return the call state. */
+    public Call.State getCallState() {
+        return mState;
+    }
+
+    /** @return whether the remote party is holding the call. */
+    public boolean isHeldByRemote() {
+        return mIsHeldByRemote;
+    }
+
+    /** @return {@code true} if the call is an incoming call. */
+    public boolean isIncoming() {
+        return mConnection.isIncoming();
+    }
+
+    /** @return {@code true} if the call is an emergency call. */
+    public boolean isEmergencyCall() {
+        return mConnection.isEmergencyCall();
+    }
+
+    /** @return the radio technology used for current connection. */
+    public @AccessNetworkConstants.RadioAccessNetworkType int getCallRadioTech() {
+        return ServiceState.rilRadioTechnologyToAccessNetworkType(mConnection.getCallRadioTech());
+    }
+
+    @Override
+    public String toString() {
+        return "[ id=" + mIndex + ", state=" + mState
+                + ", isMT=" + isIncoming() + ", heldByRemote=" + mIsHeldByRemote + " ]";
+    }
+}
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsCallInfoTracker.java b/src/java/com/android/internal/telephony/imsphone/ImsCallInfoTracker.java
new file mode 100644
index 0000000..5783e48
--- /dev/null
+++ b/src/java/com/android/internal/telephony/imsphone/ImsCallInfoTracker.java
@@ -0,0 +1,205 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.imsphone;
+
+import static com.android.internal.telephony.Call.State.DISCONNECTED;
+import static com.android.internal.telephony.Call.State.IDLE;
+
+import android.annotation.NonNull;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.Call;
+import com.android.internal.telephony.Connection;
+import com.android.internal.telephony.Phone;
+import com.android.telephony.Rlog;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Contains the state of all IMS calls.
+ */
+public class ImsCallInfoTracker {
+    private static final String LOG_TAG = "ImsCallInfoTracker";
+    private static final boolean DBG = false;
+
+    private final Phone mPhone;
+    private final List<ImsCallInfo> mQueue = new ArrayList<>();
+    private int mNextIndex = 1;
+
+    private final Map<Connection, ImsCallInfo> mImsCallInfo = new HashMap<>();
+
+    public ImsCallInfoTracker(Phone phone) {
+        mPhone = phone;
+    }
+
+    /**
+     * Adds a new instance of the IMS call.
+     *
+     * @param c The instance of {@link ImsPhoneConnection}.
+     */
+    public void addImsCallStatus(@NonNull ImsPhoneConnection c) {
+        if (DBG) Rlog.d(LOG_TAG, "addImsCallStatus");
+
+        synchronized (mImsCallInfo) {
+            if (mQueue.isEmpty()) {
+                mQueue.add(new ImsCallInfo(mNextIndex++));
+            }
+
+            Iterator<ImsCallInfo> it = mQueue.iterator();
+            ImsCallInfo imsCallInfo = it.next();
+            mQueue.remove(imsCallInfo);
+
+            imsCallInfo.update(c);
+            mImsCallInfo.put(c, imsCallInfo);
+
+            notifyImsCallStatus();
+
+            if (DBG) dump();
+        }
+    }
+
+    /**
+     * Updates the list of IMS calls.
+     *
+     * @param c The instance of {@link ImsPhoneConnection}.
+     */
+    public void updateImsCallStatus(@NonNull ImsPhoneConnection c) {
+        updateImsCallStatus(c, false, false);
+    }
+
+    /**
+     * Updates the list of IMS calls.
+     *
+     * @param c The instance of {@link ImsPhoneConnection}.
+     * @param holdReceived {@code true} if the remote party held the call.
+     * @param resumeReceived {@code true} if the remote party resumed the call.
+     */
+    public void updateImsCallStatus(@NonNull ImsPhoneConnection c,
+            boolean holdReceived, boolean resumeReceived) {
+        if (DBG) {
+            Rlog.d(LOG_TAG, "updateImsCallStatus holdReceived=" + holdReceived
+                    + ", resumeReceived=" + resumeReceived);
+        }
+
+        synchronized (mImsCallInfo) {
+            ImsCallInfo info = mImsCallInfo.get(c);
+
+            if (info == null) {
+                // This happens when the user tries to hangup the call after handover has completed.
+                return;
+            }
+
+            boolean changed = info.update(c, holdReceived, resumeReceived);
+
+            if (changed) notifyImsCallStatus();
+
+            Call.State state = c.getState();
+
+            if (DBG) Rlog.d(LOG_TAG, "updateImsCallStatus state=" + state);
+            // Call is disconnected. There are 2 cases in disconnected state:
+            // if silent redial, state == IDLE, otherwise, state == DISCONNECTED.
+            if (state == DISCONNECTED || state == IDLE) {
+                // clear the disconnected call
+                mImsCallInfo.remove(c);
+                info.reset();
+                if (info.getIndex() < (mNextIndex - 1)) {
+                    mQueue.add(info);
+                    sort(mQueue);
+                } else {
+                    mNextIndex--;
+                }
+            }
+
+            if (DBG) dump();
+        }
+    }
+
+    /** Clears all orphaned IMS call information. */
+    public void clearAllOrphanedConnections() {
+        if (DBG) Rlog.d(LOG_TAG, "clearAllOrphanedConnections");
+
+        Collection<ImsCallInfo> infos = mImsCallInfo.values();
+        infos.stream().forEach(info -> { info.onDisconnect(); });
+        notifyImsCallStatus();
+        clearAllCallInfo();
+
+        if (DBG) dump();
+    }
+
+    /** Notifies that SRVCC has completed. */
+    public void notifySrvccCompleted() {
+        if (DBG) Rlog.d(LOG_TAG, "notifySrvccCompleted");
+
+        clearAllCallInfo();
+        notifyImsCallStatus();
+
+        if (DBG) dump();
+    }
+
+    private void clearAllCallInfo() {
+        try {
+            Collection<ImsCallInfo> infos = mImsCallInfo.values();
+            infos.stream().forEach(info -> { info.reset(); });
+            mImsCallInfo.clear();
+            mQueue.clear();
+            mNextIndex = 1;
+        } catch (UnsupportedOperationException e) {
+            Rlog.e(LOG_TAG, "e=" + e);
+        }
+    }
+
+    private void notifyImsCallStatus() {
+        Collection<ImsCallInfo> infos = mImsCallInfo.values();
+        ArrayList<ImsCallInfo> imsCallInfo = new ArrayList<ImsCallInfo>(infos);
+        sort(imsCallInfo);
+        mPhone.updateImsCallStatus(imsCallInfo, null);
+    }
+
+    /**
+     * Sorts the list of IMS calls by the call index.
+     *
+     * @param infos The list of IMS calls.
+     */
+    @VisibleForTesting
+    public static void sort(List<ImsCallInfo> infos) {
+        Collections.sort(infos, new Comparator<ImsCallInfo>() {
+            @Override
+            public int compare(ImsCallInfo l, ImsCallInfo r) {
+                if (l.getIndex() > r.getIndex()) {
+                    return 1;
+                } else if (l.getIndex() < r.getIndex()) {
+                    return -1;
+                }
+                return 0;
+            }
+        });
+    }
+
+    private void dump() {
+        Collection<ImsCallInfo> infos = mImsCallInfo.values();
+        ArrayList<ImsCallInfo> imsCallInfo = new ArrayList<ImsCallInfo>(infos);
+        sort(imsCallInfo);
+        Rlog.d(LOG_TAG, "imsCallInfos=" + imsCallInfo);
+    }
+}
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsPhone.java b/src/java/com/android/internal/telephony/imsphone/ImsPhone.java
index 71e85f3..31190a0 100644
--- a/src/java/com/android/internal/telephony/imsphone/ImsPhone.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsPhone.java
@@ -19,6 +19,12 @@
 import static android.provider.Telephony.SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS;
 import static android.telephony.ims.ImsManager.EXTRA_WFC_REGISTRATION_FAILURE_MESSAGE;
 import static android.telephony.ims.ImsManager.EXTRA_WFC_REGISTRATION_FAILURE_TITLE;
+import static android.telephony.ims.RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED;
+import static android.telephony.ims.RegistrationManager.REGISTRATION_STATE_REGISTERED;
+import static android.telephony.ims.RegistrationManager.SUGGESTED_ACTION_NONE;
+import static android.telephony.ims.RegistrationManager.SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK;
+import static android.telephony.ims.RegistrationManager.SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK_WITH_TIMEOUT;
+import static android.telephony.ims.stub.ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
 
 import static com.android.internal.telephony.CommandsInterface.CB_FACILITY_BAIC;
 import static com.android.internal.telephony.CommandsInterface.CB_FACILITY_BAICr;
@@ -42,6 +48,7 @@
 import static com.android.internal.telephony.CommandsInterface.SERVICE_CLASS_NONE;
 import static com.android.internal.telephony.CommandsInterface.SERVICE_CLASS_VOICE;
 
+import android.annotation.NonNull;
 import android.app.Activity;
 import android.app.Notification;
 import android.app.NotificationManager;
@@ -79,9 +86,12 @@
 import android.telephony.ims.ImsCallForwardInfo;
 import android.telephony.ims.ImsCallProfile;
 import android.telephony.ims.ImsReasonInfo;
+import android.telephony.ims.ImsRegistrationAttributes;
 import android.telephony.ims.ImsSsData;
 import android.telephony.ims.ImsSsInfo;
 import android.telephony.ims.RegistrationManager;
+import android.telephony.ims.feature.MmTelFeature;
+import android.telephony.ims.stub.ImsRegistrationImplBase;
 import android.telephony.ims.stub.ImsUtImplBase;
 import android.text.TextUtils;
 import android.util.LocalLog;
@@ -116,6 +126,7 @@
 import com.android.internal.telephony.metrics.TelephonyMetrics;
 import com.android.internal.telephony.metrics.VoiceCallSessionStats;
 import com.android.internal.telephony.nano.TelephonyProto.ImsConnectionState;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
 import com.android.internal.telephony.uicc.IccRecords;
 import com.android.internal.telephony.util.NotificationChannelController;
 import com.android.internal.telephony.util.TelephonyUtils;
@@ -288,6 +299,16 @@
 
     private ImsStats mImsStats;
 
+    private int mImsRegistrationState;
+    // The access network type where IMS is registered
+    private @ImsRegistrationImplBase.ImsRegistrationTech int mImsRegistrationTech =
+            REGISTRATION_TECH_NONE;
+    private @RegistrationManager.SuggestedAction int mImsRegistrationSuggestedAction;
+    private @ImsRegistrationImplBase.ImsRegistrationTech int mImsDeregistrationTech =
+            REGISTRATION_TECH_NONE;
+    private int mImsRegistrationCapabilities;
+    private boolean mNotifiedRegisteredState;
+
     // A runnable which is used to automatically exit from Ecm after a period of time.
     private Runnable mExitEcmRunnable = new Runnable() {
         @Override
@@ -457,11 +478,7 @@
         mCT.registerPhoneStateListener(mExternalCallTracker);
         mExternalCallTracker.setCallPuller(mCT);
 
-        boolean legacyMode = true;
-        if (mDefaultPhone.getAccessNetworksManager() != null) {
-            legacyMode = mDefaultPhone.getAccessNetworksManager().isInLegacyMode();
-        }
-        mSS.setOutOfService(legacyMode, false);
+        mSS.setOutOfService(false);
 
         mPhoneId = mDefaultPhone.getPhoneId();
 
@@ -1583,7 +1600,7 @@
     }
 
     @Override
-    public void notifySrvccState(Call.SrvccState state) {
+    public void notifySrvccState(int state) {
         mCT.notifySrvccState(state);
     }
 
@@ -2370,7 +2387,7 @@
     /**
      * Update roaming state and WFC mode in the following situations:
      *     1) voice is in service.
-     *     2) data is in service and it is not IWLAN (if in legacy mode).
+     *     2) data is in service.
      * @param ss non-null ServiceState
      */
     private void updateRoamingState(ServiceState ss) {
@@ -2391,15 +2408,7 @@
             logi("updateRoamingState: we are not IN_SERVICE, ignoring roaming change.");
             return;
         }
-        // We ignore roaming changes when moving to IWLAN because it always sets the roaming
-        // mode to home and masks the actual cellular roaming status if voice is not registered. If
-        // we just moved to IWLAN because WFC roaming mode is IWLAN preferred and WFC home mode is
-        // cell preferred, we can get into a condition where the modem keeps bouncing between
-        // IWLAN->cell->IWLAN->cell...
-        if (isCsNotInServiceAndPsWwanReportingWlan(ss)) {
-            logi("updateRoamingState: IWLAN masking roaming, ignore roaming change.");
-            return;
-        }
+
         if (mCT.getState() == PhoneConstants.State.IDLE) {
             if (DBG) logd("updateRoamingState now: " + newRoamingState);
             mLastKnownRoamingState = newRoamingState;
@@ -2418,30 +2427,6 @@
         }
     }
 
-    /**
-     * In legacy mode, data registration will report IWLAN when we are using WLAN for data,
-     * effectively masking the true roaming state of the device if voice is not registered.
-     *
-     * @return true if we are reporting not in service for CS domain over WWAN transport and WLAN
-     * for PS domain over WWAN transport.
-     */
-    private boolean isCsNotInServiceAndPsWwanReportingWlan(ServiceState ss) {
-        // We can not get into this condition if we are in AP-Assisted mode.
-        if (mDefaultPhone.getAccessNetworksManager() == null
-                || !mDefaultPhone.getAccessNetworksManager().isInLegacyMode()) {
-            return false;
-        }
-        NetworkRegistrationInfo csInfo = ss.getNetworkRegistrationInfo(
-                NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
-        NetworkRegistrationInfo psInfo = ss.getNetworkRegistrationInfo(
-                NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
-        // We will return roaming state correctly if the CS domain is in service because
-        // ss.getRoaming() returns isVoiceRoaming||isDataRoaming result and isDataRoaming==false
-        // when the modem reports IWLAN RAT.
-        return psInfo != null && csInfo != null && !csInfo.isInService()
-                && psInfo.getAccessNetworkTechnology() == TelephonyManager.NETWORK_TYPE_IWLAN;
-    }
-
     public RegistrationManager.RegistrationCallback getImsMmTelRegistrationCallback() {
         return mImsMmTelRegistrationHelper.getCallback();
     }
@@ -2452,12 +2437,18 @@
     public void resetImsRegistrationState() {
         if (DBG) logd("resetImsRegistrationState");
         mImsMmTelRegistrationHelper.reset();
+        int subId = getSubId();
+        if (SubscriptionManager.isValidSubscriptionId(subId)) {
+            updateImsRegistrationInfo(REGISTRATION_STATE_NOT_REGISTERED,
+                    REGISTRATION_TECH_NONE, SUGGESTED_ACTION_NONE);
+        }
     }
 
     private ImsRegistrationCallbackHelper.ImsRegistrationUpdate mMmTelRegistrationUpdate = new
             ImsRegistrationCallbackHelper.ImsRegistrationUpdate() {
         @Override
-        public void handleImsRegistered(int imsRadioTech) {
+        public void handleImsRegistered(@NonNull ImsRegistrationAttributes attributes) {
+            int imsRadioTech = attributes.getTransportType();
             if (DBG) {
                 logd("handleImsRegistered: onImsMmTelConnected imsRadioTech="
                         + AccessNetworkConstants.transportTypeToString(imsRadioTech));
@@ -2468,6 +2459,8 @@
             getDefaultPhone().setImsRegistrationState(true);
             mMetrics.writeOnImsConnectionState(mPhoneId, ImsConnectionState.State.CONNECTED, null);
             mImsStats.onImsRegistered(imsRadioTech);
+            updateImsRegistrationInfo(REGISTRATION_STATE_REGISTERED,
+                    attributes.getRegistrationTechnology(), SUGGESTED_ACTION_NONE);
         }
 
         @Override
@@ -2486,10 +2479,13 @@
         }
 
         @Override
-        public void handleImsUnregistered(ImsReasonInfo imsReasonInfo) {
+        public void handleImsUnregistered(ImsReasonInfo imsReasonInfo,
+                @RegistrationManager.SuggestedAction int suggestedAction,
+                @ImsRegistrationImplBase.ImsRegistrationTech int imsRadioTech) {
             if (DBG) {
                 logd("handleImsUnregistered: onImsMmTelDisconnected imsReasonInfo="
-                        + imsReasonInfo);
+                        + imsReasonInfo + ", suggestedAction=" + suggestedAction
+                        + ", disconnectedRadioTech=" + imsRadioTech);
             }
             mRegLocalLog.log("handleImsUnregistered: onImsMmTelDisconnected imsRadioTech="
                     + imsReasonInfo);
@@ -2499,6 +2495,16 @@
             mMetrics.writeOnImsConnectionState(mPhoneId, ImsConnectionState.State.DISCONNECTED,
                     imsReasonInfo);
             mImsStats.onImsUnregistered(imsReasonInfo);
+            mImsRegistrationTech = REGISTRATION_TECH_NONE;
+            int suggestedModemAction = SUGGESTED_ACTION_NONE;
+            if (imsReasonInfo.getCode() == ImsReasonInfo.CODE_REGISTRATION_ERROR) {
+                if ((suggestedAction == SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK)
+                        || (suggestedAction == SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK_WITH_TIMEOUT)) {
+                    suggestedModemAction = suggestedAction;
+                }
+            }
+            updateImsRegistrationInfo(REGISTRATION_STATE_NOT_REGISTERED,
+                    imsRadioTech, suggestedModemAction);
         }
 
         @Override
@@ -2524,15 +2530,30 @@
             // IMS callbacks are sent back to telephony after SIM state changed.
             return;
         }
-        SubscriptionController subController = SubscriptionController.getInstance();
-        String countryIso = getCountryIso(subController, subId);
-        // Format the number as one more defense to reject garbage values:
-        // phoneNumber will become null.
-        phoneNumber = PhoneNumberUtils.formatNumberToE164(phoneNumber, countryIso);
-        if (phoneNumber == null) {
-            return;
+
+        if (isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = mSubscriptionManagerService
+                    .getSubscriptionInfoInternal(subId);
+            if (subInfo != null) {
+                phoneNumber = PhoneNumberUtils.formatNumberToE164(phoneNumber,
+                        subInfo.getCountryIso());
+                if (phoneNumber == null) {
+                    return;
+                }
+                mSubscriptionManagerService.setNumberFromIms(subId, phoneNumber);
+            }
+        } else {
+            SubscriptionController subController = SubscriptionController.getInstance();
+            String countryIso = getCountryIso(subController, subId);
+            // Format the number as one more defense to reject garbage values:
+            // phoneNumber will become null.
+            phoneNumber = PhoneNumberUtils.formatNumberToE164(phoneNumber, countryIso);
+            if (phoneNumber == null) {
+                return;
+            }
+            subController.setSubscriptionProperty(subId, COLUMN_PHONE_NUMBER_SOURCE_IMS,
+                    phoneNumber);
         }
-        subController.setSubscriptionProperty(subId, COLUMN_PHONE_NUMBER_SOURCE_IMS, phoneNumber);
     }
 
     private static String getCountryIso(SubscriptionController subController, int subId) {
@@ -2616,6 +2637,131 @@
         return mLastKnownRoamingState;
     }
 
+    /**
+     * Update IMS registration information to modem.
+     *
+     * @param capabilities indicate MMTEL capability such as VOICE, VIDEO and SMS.
+     */
+    public void updateImsRegistrationInfo(int capabilities) {
+        if (mImsRegistrationState == REGISTRATION_STATE_REGISTERED) {
+            if (mNotifiedRegisteredState && (capabilities == mImsRegistrationCapabilities)) {
+                // Duplicated notification, no change in capabilities.
+                return;
+            }
+
+            mImsRegistrationCapabilities = capabilities;
+            if (capabilities == 0) {
+                // Ignore this as this usually happens just before onUnregistered callback.
+                // We can notify modem when onUnregistered() flow occurs.
+                return;
+            }
+
+            mDefaultPhone.mCi.updateImsRegistrationInfo(mImsRegistrationState,
+                    mImsRegistrationTech, 0, capabilities, null);
+            mNotifiedRegisteredState = true;
+        }
+    }
+
+    /**
+     * Update IMS registration info
+     *
+     * @param regState indicates IMS registration state.
+     * @param imsRadioTech indicates the type of the radio access network where IMS is registered.
+     * @param suggestedAction indicates the suggested action for the radio to perform.
+     */
+    private void updateImsRegistrationInfo(
+            @RegistrationManager.ImsRegistrationState int regState,
+            @ImsRegistrationImplBase.ImsRegistrationTech int imsRadioTech,
+            @RegistrationManager.SuggestedAction int suggestedAction) {
+
+        if (regState == mImsRegistrationState) {
+            if ((regState == REGISTRATION_STATE_REGISTERED && imsRadioTech == mImsRegistrationTech)
+                    || (regState == REGISTRATION_STATE_NOT_REGISTERED
+                            && suggestedAction == mImsRegistrationSuggestedAction
+                            && imsRadioTech == mImsDeregistrationTech)) {
+                // Filter duplicate notification.
+                return;
+            }
+        }
+
+        if (regState == REGISTRATION_STATE_NOT_REGISTERED) {
+            mDefaultPhone.mCi.updateImsRegistrationInfo(regState,
+                    imsRadioTech, suggestedAction, 0, null);
+        } else if (mImsRegistrationState == REGISTRATION_STATE_REGISTERED) {
+            // This happens when radio tech is changed while in REGISTERED state.
+            if (mImsRegistrationCapabilities > 0) {
+                // Capability has been updated. Notify REGISTRATION_STATE_REGISTERED.
+                mDefaultPhone.mCi.updateImsRegistrationInfo(regState, imsRadioTech, 0,
+                        mImsRegistrationCapabilities, null);
+                mImsRegistrationTech = imsRadioTech;
+                mNotifiedRegisteredState = true;
+                return;
+            }
+        }
+
+        mImsRegistrationState = regState;
+        mImsRegistrationTech = imsRadioTech;
+        mImsRegistrationSuggestedAction = suggestedAction;
+        if (regState == REGISTRATION_STATE_NOT_REGISTERED) {
+            mImsDeregistrationTech = imsRadioTech;
+        } else {
+            mImsDeregistrationTech = REGISTRATION_TECH_NONE;
+        }
+        mImsRegistrationCapabilities = 0;
+        // REGISTRATION_STATE_REGISTERED will be notified when the capability is updated.
+        mNotifiedRegisteredState = false;
+    }
+
+    @Override
+    public void setTerminalBasedCallWaitingStatus(int state) {
+        mCT.setTerminalBasedCallWaitingStatus(state);
+    }
+
+    @Override
+    public void triggerEpsFallback(@MmTelFeature.EpsFallbackReason int reason, Message response) {
+        mDefaultPhone.triggerEpsFallback(reason, response);
+    }
+
+    @Override
+    public void startImsTraffic(int token,
+            @MmTelFeature.ImsTrafficType int trafficType,
+            @AccessNetworkConstants.RadioAccessNetworkType int accessNetworkType,
+            @MmTelFeature.ImsTrafficDirection int trafficDirection, Message response) {
+        mDefaultPhone.startImsTraffic(token, trafficType,
+                accessNetworkType, trafficDirection, response);
+    }
+
+    @Override
+    public void stopImsTraffic(int token, Message response) {
+        mDefaultPhone.stopImsTraffic(token, response);
+    }
+
+    @Override
+    public void registerForConnectionSetupFailure(Handler h, int what, Object obj) {
+        mDefaultPhone.registerForConnectionSetupFailure(h, what, obj);
+    }
+
+    @Override
+    public void unregisterForConnectionSetupFailure(Handler h) {
+        mDefaultPhone.unregisterForConnectionSetupFailure(h);
+    }
+
+    @Override
+    public void triggerImsDeregistration(
+            @ImsRegistrationImplBase.ImsDeregistrationReason int reason) {
+        mCT.triggerImsDeregistration(reason);
+    }
+
+    @Override
+    public void updateImsCallStatus(List<ImsCallInfo> imsCallInfo, Message response) {
+        mDefaultPhone.updateImsCallStatus(imsCallInfo, response);
+    }
+
+    @Override
+    public void triggerNotifyAnbr(int mediaType, int direction, int bitsPerSecond) {
+        mCT.triggerNotifyAnbr(mediaType, direction, bitsPerSecond);
+    }
+
     @Override
     public void dump(FileDescriptor fd, PrintWriter printWriter, String[] args) {
         IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, "  ");
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsPhoneBase.java b/src/java/com/android/internal/telephony/imsphone/ImsPhoneBase.java
index b5c7da1..1426ac8 100644
--- a/src/java/com/android/internal/telephony/imsphone/ImsPhoneBase.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsPhoneBase.java
@@ -24,11 +24,13 @@
 import android.sysprop.TelephonyProperties;
 import android.telephony.Annotation.DataActivityType;
 import android.telephony.CallQuality;
+import android.telephony.CallState;
 import android.telephony.NetworkScanRequest;
 import android.telephony.ServiceState;
 import android.telephony.SignalStrength;
 import android.telephony.TelephonyManager;
 import android.telephony.ims.ImsReasonInfo;
+import android.telephony.ims.MediaQualityStatus;
 import android.util.Pair;
 
 import com.android.internal.annotations.VisibleForTesting;
@@ -138,6 +140,10 @@
         mNotifier.notifyCallQualityChanged(this, callQuality, callNetworkType);
     }
 
+    public void onMediaQualityStatusChanged(MediaQualityStatus status) {
+        mNotifier.notifyMediaQualityStatusChanged(this, status);
+    }
+
     @Override
     public ServiceState getServiceState() {
         // FIXME: we may need to provide this when data connectivity is lost
@@ -192,7 +198,39 @@
      */
     public void notifyPreciseCallStateChanged() {
         /* we'd love it if this was package-scoped*/
-        super.notifyPreciseCallStateChangedP();
+        AsyncResult ar = new AsyncResult(null, this, null);
+        mPreciseCallStateRegistrants.notifyRegistrants(ar);
+
+        ImsPhoneCall ringingCall = (ImsPhoneCall) getRingingCall();
+        ImsPhoneCall foregroundCall = (ImsPhoneCall) getForegroundCall();
+        ImsPhoneCall backgroundCall = (ImsPhoneCall) getBackgroundCall();
+
+        if (ringingCall != null && foregroundCall != null && backgroundCall != null) {
+            //Array for IMS call session ID of RINGING/FOREGROUND/BACKGROUND call
+            String[] imsCallIds = new String[CallState.CALL_CLASSIFICATION_MAX];
+            //Array for IMS call service type of RINGING/FOREGROUND/BACKGROUND call
+            int[] imsCallServiceTypes = new int[CallState.CALL_CLASSIFICATION_MAX];
+            //Array for IMS call type of RINGING/FOREGROUND/BACKGROUND call
+            int[] imsCallTypes = new int[CallState.CALL_CLASSIFICATION_MAX];
+            imsCallIds[CallState.CALL_CLASSIFICATION_RINGING] =
+                    ringingCall.getCallSessionId();
+            imsCallIds[CallState.CALL_CLASSIFICATION_FOREGROUND] =
+                    foregroundCall.getCallSessionId();
+            imsCallIds[CallState.CALL_CLASSIFICATION_BACKGROUND] =
+                    backgroundCall.getCallSessionId();
+            imsCallServiceTypes[CallState.CALL_CLASSIFICATION_RINGING] =
+                    ringingCall.getServiceType();
+            imsCallServiceTypes[CallState.CALL_CLASSIFICATION_FOREGROUND] =
+                    foregroundCall.getServiceType();
+            imsCallServiceTypes[CallState.CALL_CLASSIFICATION_BACKGROUND] =
+                    backgroundCall.getServiceType();
+            imsCallTypes[CallState.CALL_CLASSIFICATION_RINGING] = ringingCall.getCallType();
+            imsCallTypes[CallState.CALL_CLASSIFICATION_FOREGROUND] =
+                    foregroundCall.getCallType();
+            imsCallTypes[CallState.CALL_CLASSIFICATION_BACKGROUND] =
+                    backgroundCall.getCallType();
+            mNotifier.notifyPreciseCallState(this, imsCallIds, imsCallServiceTypes, imsCallTypes);
+        }
     }
 
     public void notifyDisconnect(Connection cn) {
@@ -300,6 +338,11 @@
     }
 
     @Override
+    public int getImeiType() {
+        return Phone.IMEI_TYPE_UNKNOWN;
+    }
+
+    @Override
     public String getEsn() {
         Rlog.e(LOG_TAG, "[VoltePhone] getEsn() is a CDMA method");
         return "0";
@@ -530,4 +573,14 @@
             notifyPhoneStateChanged();
         }
     }
+
+    @Override
+    public int getTerminalBasedCallWaitingState(boolean forCsOnly) {
+        return getDefaultPhone().getTerminalBasedCallWaitingState(forCsOnly);
+    }
+
+    @Override
+    public void setTerminalBasedCallWaitingSupported(boolean supported) {
+        getDefaultPhone().setTerminalBasedCallWaitingSupported(supported);
+    }
 }
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsPhoneCall.java b/src/java/com/android/internal/telephony/imsphone/ImsPhoneCall.java
index 98cc441..7a6adce 100644
--- a/src/java/com/android/internal/telephony/imsphone/ImsPhoneCall.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsPhoneCall.java
@@ -19,6 +19,8 @@
 import android.compat.annotation.UnsupportedAppUsage;
 import android.os.Build;
 import android.telephony.DisconnectCause;
+import android.telephony.ims.ImsCallProfile;
+import android.telephony.ims.ImsCallSession;
 import android.telephony.ims.ImsStreamMediaProfile;
 import android.util.Log;
 
@@ -327,6 +329,41 @@
         return (connection == null) ? null : connection.getImsCall();
     }
 
+    /**
+     * Retrieves the {@link ImsCallSession#getCallId()} for the current {@link ImsPhoneCall}.
+     */
+    @VisibleForTesting
+    public String getCallSessionId() {
+        return ((getImsCall() == null) ? null : getImsCall().getSession()) == null
+                ? null : getImsCall().getSession().getCallId();
+    }
+
+    /**
+     * Retrieves the service type in {@link ImsCallProfile} for the current {@link ImsPhoneCall}.
+     */
+    @VisibleForTesting
+    public int getServiceType() {
+        if (getFirstConnection() == null) {
+            return ImsCallProfile.SERVICE_TYPE_NONE;
+        } else {
+            return getFirstConnection().isEmergencyCall()
+                    ? ImsCallProfile.SERVICE_TYPE_EMERGENCY : ImsCallProfile.SERVICE_TYPE_NORMAL;
+        }
+    }
+
+    /**
+     * Retrieves the call type in {@link ImsCallProfile} for the current {@link ImsPhoneCall}.
+     */
+    @VisibleForTesting
+    public int getCallType() {
+        if (getImsCall() == null) {
+            return ImsCallProfile.CALL_TYPE_NONE;
+        } else {
+            return getImsCall().isVideoCall()
+                    ? ImsCallProfile.CALL_TYPE_VT : ImsCallProfile.CALL_TYPE_VOICE;
+        }
+    }
+
     /*package*/ static boolean isLocalTone(ImsCall imsCall) {
         if ((imsCall == null) || (imsCall.getCallProfile() == null)
                 || (imsCall.getCallProfile().mMediaProfile == null)) {
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsPhoneCallTracker.java b/src/java/com/android/internal/telephony/imsphone/ImsPhoneCallTracker.java
index 9b69cc9..6840f4b 100644
--- a/src/java/com/android/internal/telephony/imsphone/ImsPhoneCallTracker.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsPhoneCallTracker.java
@@ -16,12 +16,32 @@
 
 package com.android.internal.telephony.imsphone;
 
+import static android.telephony.CarrierConfigManager.ImsVoice.ALERTING_SRVCC_SUPPORT;
+import static android.telephony.CarrierConfigManager.ImsVoice.BASIC_SRVCC_SUPPORT;
+import static android.telephony.CarrierConfigManager.ImsVoice.MIDCALL_SRVCC_SUPPORT;
+import static android.telephony.CarrierConfigManager.ImsVoice.PREALERTING_SRVCC_SUPPORT;
 import static android.telephony.CarrierConfigManager.USSD_OVER_CS_PREFERRED;
 import static android.telephony.CarrierConfigManager.USSD_OVER_IMS_ONLY;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_ACTIVE;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_ALERTING;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_DIALING;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_HOLDING;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_INCOMING;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_INCOMING_SETUP;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_WAITING;
+import static android.telephony.ims.ImsService.CAPABILITY_TERMINAL_BASED_CALL_WAITING;
+import static android.telephony.ims.feature.ConnectionFailureInfo.REASON_UNSPECIFIED;
+import static android.telephony.ims.feature.MmTelFeature.ImsTrafficSessionCallbackWrapper.INVALID_TOKEN;
 
 import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
+import static com.android.internal.telephony.CallWaitingController.TERMINAL_BASED_ACTIVATED;
+import static com.android.internal.telephony.CallWaitingController.TERMINAL_BASED_NOT_SUPPORTED;
+import static com.android.internal.telephony.CommandsInterface.IMS_MMTEL_CAPABILITY_SMS;
+import static com.android.internal.telephony.CommandsInterface.IMS_MMTEL_CAPABILITY_VIDEO;
+import static com.android.internal.telephony.CommandsInterface.IMS_MMTEL_CAPABILITY_VOICE;
 import static com.android.internal.telephony.Phone.CS_FALLBACK;
 
+import android.Manifest;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.usage.NetworkStatsManager;
@@ -56,6 +76,7 @@
 import android.telecom.Connection.VideoProvider;
 import android.telecom.TelecomManager;
 import android.telecom.VideoProfile;
+import android.telephony.AccessNetworkConstants;
 import android.telephony.CallQuality;
 import android.telephony.CarrierConfigManager;
 import android.telephony.DisconnectCause;
@@ -74,9 +95,16 @@
 import android.telephony.ims.ImsReasonInfo;
 import android.telephony.ims.ImsStreamMediaProfile;
 import android.telephony.ims.ImsSuppServiceNotification;
+import android.telephony.ims.MediaQualityStatus;
+import android.telephony.ims.MediaThreshold;
 import android.telephony.ims.ProvisioningManager;
 import android.telephony.ims.RtpHeaderExtension;
 import android.telephony.ims.RtpHeaderExtensionType;
+import android.telephony.ims.SrvccCall;
+import android.telephony.ims.aidl.IImsCallSessionListener;
+import android.telephony.ims.aidl.IImsTrafficSessionCallback;
+import android.telephony.ims.aidl.ISrvccStartedCallback;
+import android.telephony.ims.feature.ConnectionFailureInfo;
 import android.telephony.ims.feature.ImsFeature;
 import android.telephony.ims.feature.MmTelFeature;
 import android.telephony.ims.stub.ImsRegistrationImplBase;
@@ -114,6 +142,7 @@
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.ServiceStateTracker;
+import com.android.internal.telephony.SrvccConnection;
 import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.d2d.RtpTransport;
 import com.android.internal.telephony.data.DataSettingsManager;
@@ -124,6 +153,8 @@
 import com.android.internal.telephony.metrics.TelephonyMetrics;
 import com.android.internal.telephony.nano.TelephonyProto.TelephonyCallSession;
 import com.android.internal.telephony.nano.TelephonyProto.TelephonyCallSession.Event.ImsCommand;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.util.TelephonyUtils;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.telephony.Rlog;
@@ -131,8 +162,10 @@
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
@@ -143,11 +176,14 @@
 import java.util.concurrent.CompletionException;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executor;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Consumer;
+import java.util.function.Supplier;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 /**
  * {@hide}
@@ -178,6 +214,20 @@
         SharedPreferences getDefaultSharedPreferences(Context context);
     }
 
+    private static class ImsTrafficSession {
+        private final @MmTelFeature.ImsTrafficType int mTrafficType;
+        private final @MmTelFeature.ImsTrafficDirection int mTrafficDirection;
+        private final @NonNull IImsTrafficSessionCallback mCallback;
+
+        ImsTrafficSession(@MmTelFeature.ImsTrafficType int trafficType,
+                @MmTelFeature.ImsTrafficDirection int trafficDirection,
+                @NonNull IImsTrafficSessionCallback callback) {
+            mTrafficType = trafficType;
+            mTrafficDirection = trafficDirection;
+            mCallback = callback;
+        }
+    }
+
     private static final boolean DBG = true;
 
     // When true, dumps the state of ImsPhoneCallTracker after changes to foreground and background
@@ -208,16 +258,18 @@
     private final MmTelFeatureListener mMmTelFeatureListener = new MmTelFeatureListener();
     private class MmTelFeatureListener extends MmTelFeature.Listener {
 
-        private void processIncomingCall(IImsCallSession c, Bundle extras) {
+        private IImsCallSessionListener processIncomingCall(@NonNull IImsCallSession c,
+                @Nullable String callId, @Nullable Bundle extras) {
             if (DBG) log("processIncomingCall: incoming call intent");
 
             if (extras == null) extras = new Bundle();
-            if (mImsManager == null) return;
+            if (mImsManager == null) return null;
 
             try {
+                IImsCallSessionListener iimsCallSessionListener;
                 // Network initiated USSD will be treated by mImsUssdListener
                 boolean isUssd = extras.getBoolean(MmTelFeature.EXTRA_IS_USSD, false);
-                // For compatibility purposes with older vendor implmentations.
+                // For compatibility purposes with older vendor implementations.
                 isUssd |= extras.getBoolean(ImsManager.EXTRA_USSD, false);
                 if (isUssd) {
                     if (DBG) log("processIncomingCall: USSD");
@@ -226,11 +278,14 @@
                     if (mUssdSession != null) {
                         mUssdSession.accept(ImsCallProfile.CALL_TYPE_VOICE);
                     }
-                    return;
+                    if (callId != null) mUssdSession.getCallSession().setCallId(callId);
+                    iimsCallSessionListener = (IImsCallSessionListener) mUssdSession
+                            .getCallSession().getIImsCallSessionListenerProxy();
+                    return iimsCallSessionListener;
                 }
 
                 boolean isUnknown = extras.getBoolean(MmTelFeature.EXTRA_IS_UNKNOWN_CALL, false);
-                // For compatibility purposes with older vendor implmentations.
+                // For compatibility purposes with older vendor implementations.
                 isUnknown |= extras.getBoolean(ImsManager.EXTRA_IS_UNKNOWN_CALL, false);
                 if (DBG) {
                     log("processIncomingCall: isUnknown = " + isUnknown
@@ -240,6 +295,9 @@
 
                 // Normal MT/Unknown call
                 ImsCall imsCall = mImsManager.takeCall(c, mImsCallListener);
+                if (callId != null) imsCall.getCallSession().setCallId(callId);
+                iimsCallSessionListener = (IImsCallSessionListener) imsCall
+                        .getCallSession().getIImsCallSessionListenerProxy();
                 ImsPhoneConnection conn = new ImsPhoneConnection(mPhone, imsCall,
                         ImsPhoneCallTracker.this,
                         (isUnknown ? mForegroundCall : mRingingCall), isUnknown);
@@ -262,13 +320,13 @@
                 if ((c != null) && (c.getCallProfile() != null)
                         && (c.getCallProfile().getCallExtras() != null)
                         && (c.getCallProfile().getCallExtras()
-                          .containsKey(ImsCallProfile.EXTRA_CALL_DISCONNECT_CAUSE))) {
+                        .containsKey(ImsCallProfile.EXTRA_CALL_DISCONNECT_CAUSE))) {
                     String error = c.getCallProfile()
                             .getCallExtra(ImsCallProfile.EXTRA_CALL_DISCONNECT_CAUSE, null);
                     if (error != null) {
                         try {
                             int cause = getDisconnectCauseFromReasonInfo(
-                                        new ImsReasonInfo(Integer.parseInt(error), 0, null),
+                                    new ImsReasonInfo(Integer.parseInt(error), 0, null),
                                     conn.getState());
                             if (cause == DisconnectCause.INCOMING_AUTO_REJECTED) {
                                 conn.setDisconnectCause(cause);
@@ -313,18 +371,20 @@
 
                 updatePhoneState();
                 mPhone.notifyPreciseCallStateChanged();
+                mImsCallInfoTracker.addImsCallStatus(conn);
+                return iimsCallSessionListener;
             } catch (ImsException | RemoteException e) {
                 loge("processIncomingCall: exception " + e);
                 mOperationLocalLog.log("onIncomingCall: exception processing: "  + e);
+                return null;
             }
         }
 
         @Override
-        public void onIncomingCall(IImsCallSession c, Bundle extras) {
-            // we want to ensure we block this binder thread until incoming call setup completes
-            // as to avoid race conditions where the ImsService tries to update the state of the
-            // call before the listeners have been attached.
-            executeAndWait(()-> processIncomingCall(c, extras));
+        @Nullable
+        public IImsCallSessionListener onIncomingCall(
+                @NonNull IImsCallSession c, @Nullable String callId, @Nullable Bundle extras) {
+            return executeAndWaitForReturn(()-> processIncomingCall(c, callId, extras));
         }
 
         @Override
@@ -339,6 +399,102 @@
             }, mExecutor);
         }
 
+        @Override
+        public void onAudioModeIsVoipChanged(int imsAudioHandler) {
+            TelephonyUtils.runWithCleanCallingIdentity(()-> {
+                ImsCall imsCall = null;
+                if (mForegroundCall.hasConnections()) {
+                    imsCall = mForegroundCall.getImsCall();
+                } else if (mBackgroundCall.hasConnections()) {
+                    imsCall = mBackgroundCall.getImsCall();
+                } else if (mRingingCall.hasConnections()) {
+                    imsCall = mRingingCall.getImsCall();
+                } else if (mHandoverCall.hasConnections()) {
+                    imsCall = mHandoverCall.getImsCall();
+                } else {
+                    Rlog.e(LOG_TAG, "onAudioModeIsVoipChanged: no Call");
+                }
+
+                if (imsCall != null) {
+                    ImsPhoneConnection conn = findConnection(imsCall);
+                    if (conn != null) {
+                        conn.onAudioModeIsVoipChanged(imsAudioHandler);
+                    }
+                } else {
+                    Rlog.e(LOG_TAG, "onAudioModeIsVoipChanged: no ImsCall");
+                }
+            }, mExecutor);
+        }
+
+        @Override
+        public void onTriggerEpsFallback(@MmTelFeature.EpsFallbackReason int reason) {
+            TelephonyUtils.runWithCleanCallingIdentity(()-> {
+                if (DBG) log("onTriggerEpsFallback reason=" + reason);
+                mPhone.triggerEpsFallback(reason, null);
+            }, mExecutor);
+        }
+
+        @Override
+        public void onStartImsTrafficSession(int token,
+                @MmTelFeature.ImsTrafficType int trafficType,
+                @AccessNetworkConstants.RadioAccessNetworkType int accessNetworkType,
+                @MmTelFeature.ImsTrafficDirection int trafficDirection,
+                IImsTrafficSessionCallback callback) {
+            registerImsTrafficSession(token, trafficType, trafficDirection, callback);
+            TelephonyUtils.runWithCleanCallingIdentity(()-> {
+                if (DBG) {
+                    log("onStartImsTrafficSession token=" + token + ", traffic=" + trafficType
+                            + ", networkType=" + accessNetworkType
+                            + ", direction=" + trafficDirection);
+                }
+                mPhone.startImsTraffic(token, trafficType, accessNetworkType, trafficDirection,
+                        obtainMessage(EVENT_START_IMS_TRAFFIC_DONE, callback));
+            }, mExecutor);
+        }
+
+        @Override
+        public void onModifyImsTrafficSession(int token,
+                @AccessNetworkConstants.RadioAccessNetworkType int accessNetworkType) {
+            ImsTrafficSession session = getImsTrafficSession(token);
+            if (session == null) {
+                loge("onModifyImsTrafficSession unknown session, token=" + token);
+                return;
+            }
+            TelephonyUtils.runWithCleanCallingIdentity(()-> {
+                if (DBG) {
+                    log("onModifyImsTrafficSession token=" + token
+                            + ", networkType=" + accessNetworkType);
+                }
+                mPhone.startImsTraffic(token, session.mTrafficType,
+                        accessNetworkType, session.mTrafficDirection,
+                        obtainMessage(EVENT_START_IMS_TRAFFIC_DONE, session.mCallback));
+            }, mExecutor);
+        }
+
+        @Override
+        public void onStopImsTrafficSession(int token) {
+            unregisterImsTrafficSession(token);
+            if (token == INVALID_TOKEN) return;
+            TelephonyUtils.runWithCleanCallingIdentity(()-> {
+                if (DBG) {
+                    log("onStopImsTrafficSession token=" + token);
+                }
+                mPhone.stopImsTraffic(token, null);
+            }, mExecutor);
+        }
+
+        @Override
+        public void onMediaQualityStatusChanged(MediaQualityStatus status) {
+            TelephonyUtils.runWithCleanCallingIdentity(()-> {
+                if (mPhone != null && mPhone.mDefaultPhone != null) {
+                    if (DBG) log("onMediaQualityStatusChanged " + status);
+                    mPhone.onMediaQualityStatusChanged(status);
+                } else {
+                    loge("onMediaQualityStatusChanged: null phone");
+                }
+            }, mExecutor);
+        }
+
         /**
          * Schedule the given Runnable on mExecutor and block this thread until it finishes.
          * @param r The Runnable to run.
@@ -351,6 +507,24 @@
                 logw("Binder - exception: " + e.getMessage());
             }
         }
+
+        /**
+         * Schedule the given Runnable on mExecutor and block this thread until it finishes.
+         * @param r The Runnable to run.
+         */
+        private <T> T executeAndWaitForReturn(Supplier<T> r) {
+
+            CompletableFuture<T> future = CompletableFuture.supplyAsync(
+                    () -> TelephonyUtils.runWithCleanCallingIdentity(r), mExecutor);
+
+            try {
+                return future.get();
+            } catch (ExecutionException | InterruptedException e) {
+                Log.w(LOG_TAG, "ImsPhoneCallTracker : executeAndWaitForReturn exception: "
+                        + e.getMessage());
+                return null;
+            }
+        }
     }
 
     /**
@@ -482,6 +656,9 @@
     private static final int EVENT_ANSWER_WAITING_CALL = 30;
     private static final int EVENT_RESUME_NOW_FOREGROUND_CALL = 31;
     private static final int EVENT_REDIAL_WITHOUT_RTT = 32;
+    private static final int EVENT_START_IMS_TRAFFIC_DONE = 33;
+    private static final int EVENT_CONNECTION_SETUP_FAILURE = 34;
+    private static final int EVENT_NEW_ACTIVE_CALL_STARTED = 35;
 
     private static final int TIMEOUT_HANGUP_PENDINGMO = 500;
 
@@ -553,6 +730,13 @@
         }
     }
 
+    private class SrvccStartedCallback extends ISrvccStartedCallback.Stub {
+        @Override
+        public void onSrvccCallNotified(List<SrvccCall> profiles) {
+            handleSrvccConnectionInfo(profiles);
+        }
+    }
+
     private volatile NetworkStats mVtDataUsageSnapshot = null;
     private volatile NetworkStats mVtDataUsageUidSnapshot = null;
     private final VtDataUsageProvider mVtDataUsageProvider = new VtDataUsageProvider();
@@ -607,14 +791,22 @@
     private boolean mSupportCepOnPeer = true;
     private boolean mSupportD2DUsingRtp = false;
     private boolean mSupportSdpForRtpHeaderExtensions = false;
+    private int mThresholdRtpPacketLoss;
+    private int mThresholdRtpJitter;
+    private long mThresholdRtpInactivityTime;
+    private final List<Integer> mSrvccTypeSupported = new ArrayList<>();
+    private final SrvccStartedCallback mSrvccStartedCallback = new SrvccStartedCallback();
     // Tracks the state of our background/foreground calls while a call hold/swap operation is
     // in progress. Values listed above.
     private HoldSwapState mHoldSwitchingState = HoldSwapState.INACTIVE;
+    private MediaThreshold mMediaThreshold;
 
     private String mLastDialString = null;
     private ImsDialArgs mLastDialArgs = null;
     private Executor mExecutor = Runnable::run;
 
+    private final ImsCallInfoTracker mImsCallInfoTracker;
+
     /**
      * Listeners to changes in the phone state.  Intended for use by other interested IMS components
      * without the need to register a full blown {@link android.telephony.PhoneStateListener}.
@@ -986,6 +1178,9 @@
     // Used for important operational related events for logging.
     private final LocalLog mOperationLocalLog = new LocalLog(64);
 
+    private final ConcurrentHashMap<Integer, ImsTrafficSession> mImsTrafficSessions =
+            new ConcurrentHashMap<>();
+
     /**
      * Container to ease passing around a tuple of two objects. This object provides a sensible
      * implementation of equals(), returning true/false using equals() for one object (Integer)
@@ -1034,6 +1229,7 @@
             return (first == null ? 0 : first.hashCode());
         }
     }
+
     //***** Events
 
 
@@ -1095,10 +1291,15 @@
                             postDelayed(mConnectorRunnable, CONNECTOR_RETRY_DELAY_MS);
                         }
                         stopListeningForCalls();
+                        stopAllImsTrafficTypes();
                     }
                 }, executor);
         // It can take some time for ITelephony to get published, so defer connecting.
         post(mConnectorRunnable);
+
+        mImsCallInfoTracker = new ImsCallInfoTracker(phone);
+
+        mPhone.registerForConnectionSetupFailure(this, EVENT_CONNECTION_SETUP_FAILURE, null);
     }
 
     /**
@@ -1175,6 +1376,8 @@
         // For compatibility with apps that still use deprecated intent
         sendImsServiceStateIntent(ImsManager.ACTION_IMS_SERVICE_UP);
         mCurrentlyConnectedSubId = Optional.of(subId);
+
+        initializeTerminalBasedCallWaiting();
     }
 
     /**
@@ -1235,6 +1438,7 @@
             mUtInterface = null;
         }
         mCurrentlyConnectedSubId = Optional.empty();
+        mMediaThreshold = null;
         resetImsCapabilities();
         hangupAllOrphanedConnections(DisconnectCause.LOST_SIGNAL);
         // For compatibility with apps that still use deprecated intent
@@ -1275,6 +1479,7 @@
         // above. Remove all references to it.
         mPendingMO = null;
         updatePhoneState();
+        mImsCallInfoTracker.clearAllOrphanedConnections();
     }
 
     /**
@@ -1315,6 +1520,8 @@
                 (NetworkStatsManager) mPhone.getContext().getSystemService(
                         Context.NETWORK_STATS_SERVICE);
         statsManager.unregisterNetworkStatsProvider(mVtDataUsageProvider);
+
+        mPhone.unregisterForConnectionSetupFailure(this);
     }
 
     @Override
@@ -1506,7 +1713,7 @@
             mLastDialString = dialString;
             mLastDialArgs = dialArgs;
             mPendingMO = new ImsPhoneConnection(mPhone, dialString, this, mForegroundCall,
-                    isEmergencyNumber, isWpsCall);
+                    isEmergencyNumber, isWpsCall, dialArgs);
             mOperationLocalLog.log("dial requested. connId=" + System.identityHashCode(mPendingMO));
             if (isEmergencyNumber && dialArgs != null && dialArgs.intentExtras != null) {
                 Rlog.i(LOG_TAG, "dial ims emergency dialer: " + dialArgs.intentExtras.getBoolean(
@@ -1617,10 +1824,20 @@
         // Check for changes due to carrier config.
         maybeConfigureRtpHeaderExtensions();
 
-        if (!SubscriptionController.getInstance().isActiveSubId(subId)) {
-            loge("updateCarrierConfiguration: skipping notification to ImsService, non"
-                    + "active subId = " + subId);
-            return;
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
+                    .getSubscriptionInfoInternal(subId);
+            if (subInfo == null || !subInfo.isActive()) {
+                loge("updateCarrierConfiguration: skipping notification to ImsService, non"
+                        + "active subId = " + subId);
+                return;
+            }
+        } else {
+            if (!SubscriptionController.getInstance().isActiveSubId(subId)) {
+                loge("updateCarrierConfiguration: skipping notification to ImsService, non"
+                        + "active subId = " + subId);
+                return;
+            }
         }
 
         Phone defaultPhone = getPhone().getDefaultPhone();
@@ -1647,6 +1864,8 @@
         logi("updateCarrierConfiguration: Updating ImsService configs.");
         mCarrierConfigLoadedForSubscription = true;
         updateImsServiceConfig();
+        updateMediaThreshold(
+                mThresholdRtpPacketLoss, mThresholdRtpJitter, mThresholdRtpInactivityTime);
     }
 
     /**
@@ -1695,6 +1914,13 @@
         mSupportSdpForRtpHeaderExtensions = carrierConfig.getBoolean(
                 CarrierConfigManager
                         .KEY_SUPPORTS_SDP_NEGOTIATION_OF_D2D_RTP_HEADER_EXTENSIONS_BOOL);
+        mThresholdRtpPacketLoss = carrierConfig.getInt(
+                CarrierConfigManager.ImsVoice.KEY_VOICE_RTP_PACKET_LOSS_RATE_THRESHOLD_INT);
+        mThresholdRtpInactivityTime = carrierConfig.getLong(
+                CarrierConfigManager.ImsVoice
+                        .KEY_VOICE_RTP_INACTIVITY_TIME_THRESHOLD_MILLIS_LONG);
+        mThresholdRtpJitter = carrierConfig.getInt(
+                CarrierConfigManager.ImsVoice.KEY_VOICE_RTP_JITTER_THRESHOLD_MILLIS_INT);
 
         if (mPhone.getContext().getResources().getBoolean(
                 com.android.internal.R.bool.config_allow_ussd_over_ims)) {
@@ -1741,6 +1967,41 @@
         } else {
             log("No carrier ImsReasonInfo mappings defined.");
         }
+
+        mSrvccTypeSupported.clear();
+        int[] srvccType =
+                carrierConfig.getIntArray(CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY);
+        if (srvccType != null && srvccType.length > 0) {
+            mSrvccTypeSupported.addAll(
+                    Arrays.stream(srvccType).boxed().collect(Collectors.toList()));
+        }
+    }
+
+    private void updateMediaThreshold(
+            int thresholdPacketLoss, int thresholdJitter, long thresholdInactivityTime) {
+        if (!MediaThreshold.isValidRtpInactivityTimeMillis(thresholdInactivityTime)
+                && !MediaThreshold.isValidJitterMillis(thresholdJitter)
+                && !MediaThreshold.isValidRtpPacketLossRate(thresholdPacketLoss)) {
+            logi("There is no valid RTP threshold value");
+            return;
+        }
+        int[] thPacketLosses = {thresholdPacketLoss};
+        long[] thInactivityTimesMillis = {thresholdInactivityTime};
+        int[] thJitters = {thresholdJitter};
+        MediaThreshold threshold = new MediaThreshold.Builder()
+                .setThresholdsRtpPacketLossRate(thPacketLosses)
+                .setThresholdsRtpInactivityTimeMillis(thInactivityTimesMillis)
+                .setThresholdsRtpJitterMillis(thJitters).build();
+        if (mMediaThreshold == null || !mMediaThreshold.equals(threshold)) {
+            logi("setMediaThreshold :" + threshold);
+            try {
+                mImsManager.setMediaThreshold(MediaQualityStatus.MEDIA_SESSION_TYPE_AUDIO,
+                        threshold);
+                mMediaThreshold = threshold;
+            } catch (ImsException e) {
+                loge("setMediaThreshold Failed: " + e);
+            }
+        }
     }
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@@ -1864,6 +2125,7 @@
             setVideoCallProvider(conn, imsCall);
             conn.setAllowAddCallDuringVideoCall(mAllowAddCallDuringVideoCall);
             conn.setAllowHoldingVideoCall(mAllowHoldingVideoCall);
+            mImsCallInfoTracker.addImsCallStatus(conn);
         } catch (ImsException e) {
             loge("dialInternal : " + e);
             mOperationLocalLog.log("dialInternal exception: " + e);
@@ -2585,6 +2847,7 @@
                 + System.identityHashCode(conn));
 
         call.onHangupLocal();
+        mImsCallInfoTracker.updateImsCallStatus(conn);
 
         try {
             if (imsCall != null) {
@@ -2791,8 +3054,15 @@
         // Do not log operations that do not change the state
         mOperationLocalLog.log("processCallStateChange: state=" + state + " cause=" + cause
                 + " connId=" + System.identityHashCode(conn));
-
+        boolean noActiveCall = false;
+        if (mForegroundCall.getState() != ImsPhoneCall.State.ACTIVE
+                && mBackgroundCall.getState() != ImsPhoneCall.State.ACTIVE) {
+            noActiveCall = true;
+        }
         changed = conn.update(imsCall, state);
+        if (noActiveCall && changed && state == ImsPhoneCall.State.ACTIVE) {
+            sendMessage(obtainMessage(EVENT_NEW_ACTIVE_CALL_STARTED));
+        }
         if (state == ImsPhoneCall.State.DISCONNECTED) {
             changed = conn.onDisconnect(cause) || changed;
             //detach the disconnected connections
@@ -2822,6 +3092,7 @@
         }
 
         if (changed) {
+            mImsCallInfoTracker.updateImsCallStatus(conn);
             if (conn.getCall() == mHandoverCall) return;
             updatePhoneState();
             mPhone.notifyPreciseCallStateChanged();
@@ -2853,7 +3124,7 @@
     @VisibleForTesting
     public void addReasonCodeRemapping(Integer fromCode, String message, Integer toCode) {
         if (message != null) {
-            message = message.toLowerCase();
+            message = message.toLowerCase(Locale.ROOT);
         }
         mImsReasonCodeMap.put(new ImsReasonInfoKeyPair(fromCode, message), toCode);
     }
@@ -2874,7 +3145,7 @@
         if (reason == null) {
             reason = "";
         } else {
-            reason = reason.toLowerCase();
+            reason = reason.toLowerCase(Locale.ROOT);
         }
         log("maybeRemapReasonCode : fromCode = " + reasonInfo.getCode() + " ; message = "
                 + reason);
@@ -3217,6 +3488,13 @@
                 eccCategory = imsCall.getCallProfile().getEmergencyServiceCategories();
             }
 
+            if (reasonInfo.getCode() == ImsReasonInfo.CODE_SIP_ALTERNATE_EMERGENCY_CALL) {
+                ImsPhoneConnection conn = findConnection(imsCall);
+                if (conn != null) {
+                    conn.setNonDetectableEmergencyCallInfo(eccCategory);
+                }
+            }
+
             if (mHoldSwitchingState == HoldSwapState.HOLDING_TO_ANSWER_INCOMING) {
                 // If we put a call on hold to answer an incoming call, we should reset the
                 // variables that keep track of the switch here.
@@ -3243,6 +3521,7 @@
                         && isForegroundHigherPriority()) {
                     mForegroundCall.detach(mPendingMO);
                     removeConnection(mPendingMO);
+                    mImsCallInfoTracker.updateImsCallStatus(mPendingMO);
                     mPendingMO.finalize();
                     mPendingMO = null;
                     // if we need to perform CSFB of call, hang up any background call
@@ -3274,6 +3553,7 @@
                 if (conn != null) {
                     mForegroundCall.detach(conn);
                     removeConnection(conn);
+                    mImsCallInfoTracker.updateImsCallStatus(conn);
                 }
                 updatePhoneState();
                 mPhone.initiateSilentRedial(reasonInfo.getExtraCode() ==
@@ -3693,6 +3973,7 @@
                     mOnHoldToneStarted = false;
                 }
                 conn.onConnectionEvent(android.telecom.Connection.EVENT_CALL_REMOTELY_UNHELD, null);
+                mImsCallInfoTracker.updateImsCallStatus(conn, false, true);
             }
 
             boolean useVideoPauseWorkaround = mPhone.getContext().getResources().getBoolean(
@@ -4102,6 +4383,28 @@
                 conn.receivedRtpHeaderExtensions(rtpHeaderExtensionData);
             }
         }
+
+        /**
+         * Access Network Bitrate Recommendation Query (ANBRQ), see 3GPP TS 26.114.
+         * This API triggers radio to send ANBRQ message to the access network to query the desired
+         * bitrate.
+         *
+         * @param imsCall The ImsCall the data was received on.
+         * @param mediaType MediaType is used to identify media stream such as audio or video.
+         * @param direction Direction of this packet stream (e.g. uplink or downlink).
+         * @param bitsPerSecond This value is the bitrate requested by the other party UE through
+         *        RTP CMR, RTCPAPP or TMMBR, and ImsStack converts this value to the MAC bitrate
+         *        (defined in TS36.321, range: 0 ~ 8000 kbit/s).
+         */
+        @Override
+        public void onCallSessionSendAnbrQuery(ImsCall imsCall, int mediaType, int direction,
+                int bitsPerSecond) {
+            if (DBG) {
+                log("onCallSessionSendAnbrQuery mediaType=" + mediaType + ", direction="
+                    + direction + ", bitPerSecond=" + bitsPerSecond);
+            }
+            handleSendAnbrQuery(mediaType, direction, bitsPerSecond);
+        }
     };
 
     /**
@@ -4239,7 +4542,8 @@
             configChangedIntent.putExtra(ImsConfig.EXTRA_CHANGED_ITEM, item);
             configChangedIntent.putExtra(ImsConfig.EXTRA_NEW_VALUE, value);
             if (mPhone != null && mPhone.getContext() != null) {
-                mPhone.getContext().sendBroadcast(configChangedIntent);
+                mPhone.getContext().sendBroadcast(
+                        configChangedIntent, Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
             }
         }
     };
@@ -4319,21 +4623,56 @@
      * Notify of a change to SRVCC state
      * @param state the new SRVCC state.
      */
-    public void notifySrvccState(Call.SrvccState state) {
+    public void notifySrvccState(int state) {
         if (DBG) log("notifySrvccState state=" + state);
 
-        mSrvccState = state;
+        if (mImsManager != null) {
+            try {
+                if (state == TelephonyManager.SRVCC_STATE_HANDOVER_STARTED) {
+                    mImsManager.notifySrvccStarted(mSrvccStartedCallback);
+                } else if (state == TelephonyManager.SRVCC_STATE_HANDOVER_COMPLETED) {
+                    mImsManager.notifySrvccCompleted();
+                } else if (state == TelephonyManager.SRVCC_STATE_HANDOVER_FAILED) {
+                    mImsManager.notifySrvccFailed();
+                } else if (state == TelephonyManager.SRVCC_STATE_HANDOVER_CANCELED) {
+                    mImsManager.notifySrvccCanceled();
+                }
+            } catch (ImsException e) {
+                loge("notifySrvccState : exception " + e);
+            }
+        }
 
-        if (mSrvccState == Call.SrvccState.COMPLETED) {
-            // If the dialing call had ringback, ensure it stops now, otherwise it'll keep playing
-            // afer the SRVCC completes.
-            mForegroundCall.maybeStopRingback();
+        switch(state) {
+            case TelephonyManager.SRVCC_STATE_HANDOVER_STARTED:
+                mSrvccState = Call.SrvccState.STARTED;
+                break;
 
-            resetState();
-            transferHandoverConnections(mForegroundCall);
-            transferHandoverConnections(mBackgroundCall);
-            transferHandoverConnections(mRingingCall);
-            updatePhoneState();
+            case TelephonyManager.SRVCC_STATE_HANDOVER_COMPLETED:
+                mSrvccState = Call.SrvccState.COMPLETED;
+
+                // If the dialing call had ringback, ensure it stops now,
+                // otherwise it'll keep playing afer the SRVCC completes.
+                mForegroundCall.maybeStopRingback();
+
+                resetState();
+                transferHandoverConnections(mForegroundCall);
+                transferHandoverConnections(mBackgroundCall);
+                transferHandoverConnections(mRingingCall);
+                updatePhoneState();
+                mImsCallInfoTracker.notifySrvccCompleted();
+                break;
+
+            case TelephonyManager.SRVCC_STATE_HANDOVER_FAILED:
+                mSrvccState = Call.SrvccState.FAILED;
+                break;
+
+            case TelephonyManager.SRVCC_STATE_HANDOVER_CANCELED:
+                mSrvccState = Call.SrvccState.CANCELED;
+                break;
+
+            default:
+                //ignore invalid state
+                return;
         }
     }
 
@@ -4464,6 +4803,7 @@
                 try {
                     ImsFeature.Capabilities capabilities = (ImsFeature.Capabilities) args.arg1;
                     handleFeatureCapabilityChanged(capabilities);
+                    updateImsRegistrationInfo();
                 } finally {
                     args.recycle();
                 }
@@ -4546,6 +4886,55 @@
                 }
                 break;
             }
+
+            case EVENT_START_IMS_TRAFFIC_DONE: // fallthrough
+            case EVENT_CONNECTION_SETUP_FAILURE: {
+                ar = (AsyncResult) msg.obj;
+                // Not-null with EVENT_START_IMS_TRAFFIC_DONE
+                IImsTrafficSessionCallback callback = (IImsTrafficSessionCallback) ar.userObj;
+                try {
+                    if (ar.exception == null) {
+                        Object[] result = (Object[]) ar.result;
+                        if (result != null && result.length > 1) {
+                            if (callback == null) {
+                                //EVENT_CONNECTION_SETUP_FAILURE
+                                ImsTrafficSession session =
+                                        getImsTrafficSession((int) result[0]);
+                                if (session != null) callback = session.mCallback;
+                            }
+                            if (callback == null) break;
+
+                            if (result[1] == null) callback.onReady();
+                            else callback.onError((ConnectionFailureInfo) result[1]);
+                            break;
+                        }
+                    }
+                    if (callback != null) {
+                        callback.onError(new ConnectionFailureInfo(REASON_UNSPECIFIED, 0, -1));
+                    }
+                } catch (RemoteException e) {
+                    Rlog.e(LOG_TAG, "Exception: " + e);
+                }
+                break;
+            }
+
+            case EVENT_NEW_ACTIVE_CALL_STARTED: {
+                try {
+                    MediaQualityStatus status = mImsManager
+                            .queryMediaQualityStatus(MediaQualityStatus.MEDIA_SESSION_TYPE_AUDIO);
+                    if (status != null) {
+                        if (mPhone != null && mPhone.mDefaultPhone != null) {
+                            if (DBG) log("notify media quality status: " + status);
+                            mPhone.onMediaQualityStatusChanged(status);
+                        } else {
+                            loge("onMediaQualityStatusChanged: null phone");
+                        }
+                    }
+                } catch (ImsException e) {
+                    Rlog.e(LOG_TAG, "Exception in queryMediaQualityStatus: " + e);
+                }
+                break;
+            }
         }
     }
 
@@ -4720,6 +5109,7 @@
                         + mSupportSdpForRtpHeaderExtensions);
             }
         }
+        pw.println(" mSrvccTypeSupported=" + mSrvccTypeSupported);
         pw.println(" Event Log:");
         pw.increaseIndent();
         mOperationLocalLog.dump(pw);
@@ -5364,6 +5754,7 @@
                 mOnHoldToneId = System.identityHashCode(conn);
             }
             conn.onConnectionEvent(android.telecom.Connection.EVENT_CALL_REMOTELY_HELD, null);
+            mImsCallInfoTracker.updateImsCallStatus(conn, true, false);
 
             boolean useVideoPauseWorkaround = mPhone.getContext().getResources().getBoolean(
                     com.android.internal.R.bool.config_useVideoPauseWorkaround);
@@ -5491,4 +5882,241 @@
         }
         return false;
     }
+
+    private void initializeTerminalBasedCallWaiting() {
+        boolean capable = false;
+        if (mImsManager != null) {
+            try {
+                capable = mImsManager.isCapable(CAPABILITY_TERMINAL_BASED_CALL_WAITING);
+            } catch (ImsException e) {
+                loge("initializeTerminalBasedCallWaiting : exception " + e);
+            }
+        }
+        logi("initializeTerminalBasedCallWaiting capable=" + capable);
+        mPhone.setTerminalBasedCallWaitingSupported(capable);
+
+        if (capable) {
+            setTerminalBasedCallWaitingStatus(mPhone.getTerminalBasedCallWaitingState(false));
+        }
+    }
+
+    /**
+     * Notifies the change of the user setting of the terminal-based call waiting service
+     * to IMS service.
+     */
+    public void setTerminalBasedCallWaitingStatus(int state) {
+        if (state == TERMINAL_BASED_NOT_SUPPORTED) return;
+        if (mImsManager != null) {
+            try {
+                log("setTerminalBasedCallWaitingStatus state=" + state);
+                mImsManager.setTerminalBasedCallWaitingStatus(
+                        state == TERMINAL_BASED_ACTIVATED);
+            } catch (ImsException e) {
+                loge("setTerminalBasedCallWaitingStatus : exception " + e);
+            }
+        }
+    }
+
+    /** Send the list of SrvccConnection instances to the radio */
+    public void handleSrvccConnectionInfo(List<SrvccCall> profileList) {
+        mPhone.getDefaultPhone().mCi.setSrvccCallInfo(
+                convertToSrvccConnectionInfo(profileList), null);
+    }
+
+    /** Converts SrvccCall to SrvccConnection. */
+    @VisibleForTesting
+    public SrvccConnection[] convertToSrvccConnectionInfo(List<SrvccCall> profileList) {
+        if (mSrvccTypeSupported.isEmpty() || profileList == null || profileList.isEmpty()) {
+            return null;
+        }
+
+        List<SrvccConnection> connList = new ArrayList<>();
+        for (SrvccCall profile : profileList) {
+            if (isCallProfileSupported(profile)) {
+                addConnection(connList,
+                        profile, findConnection(profile.getCallId()));
+            }
+        }
+
+        if (connList.isEmpty()) return null;
+        return connList.toArray(new SrvccConnection[0]);
+    }
+
+    /** Send the mediaType, direction, bitrate for ANBR Query to the radio */
+    public void handleSendAnbrQuery(int mediaType, int direction, int bitsPerSecond) {
+        if (DBG) log("handleSendAnbrQuery - mediaType=" + mediaType);
+        mPhone.getDefaultPhone().mCi.sendAnbrQuery(mediaType, direction, bitsPerSecond, null);
+    }
+
+
+    /**
+     * Notifies the recommended bit rate for the indicated logical channel and direction.
+     *
+     * @param mediaType MediaType is used to identify media stream such as audio or video.
+     * @param direction Direction of this packet stream (e.g. uplink or downlink).
+     * @param bitsPerSecond The recommended bit rate for the UE for a specific logical channel and
+     *        a specific direction by NW.
+     */
+    public void triggerNotifyAnbr(int mediaType, int direction, int bitsPerSecond) {
+        ImsCall activeCall = mForegroundCall.getFirstConnection().getImsCall();
+
+        if (activeCall != null) {
+            if (DBG) log("triggerNotifyAnbr - mediaType=" + mediaType);
+            activeCall.callSessionNotifyAnbr(mediaType, direction, bitsPerSecond);
+        }
+    }
+
+    private boolean isCallProfileSupported(SrvccCall profile) {
+        if (profile == null) return false;
+
+        switch(profile.getPreciseCallState()) {
+            case PRECISE_CALL_STATE_ACTIVE:
+                return mSrvccTypeSupported.contains(BASIC_SRVCC_SUPPORT);
+            case PRECISE_CALL_STATE_HOLDING:
+                return mSrvccTypeSupported.contains(MIDCALL_SRVCC_SUPPORT);
+            case PRECISE_CALL_STATE_DIALING:
+                return mSrvccTypeSupported.contains(PREALERTING_SRVCC_SUPPORT);
+            case PRECISE_CALL_STATE_ALERTING:
+                return mSrvccTypeSupported.contains(ALERTING_SRVCC_SUPPORT);
+            case PRECISE_CALL_STATE_INCOMING:
+                return mSrvccTypeSupported.contains(ALERTING_SRVCC_SUPPORT);
+            case PRECISE_CALL_STATE_WAITING:
+                return mSrvccTypeSupported.contains(ALERTING_SRVCC_SUPPORT);
+            case PRECISE_CALL_STATE_INCOMING_SETUP:
+                return mSrvccTypeSupported.contains(PREALERTING_SRVCC_SUPPORT);
+            default:
+                break;
+        }
+        return false;
+    }
+
+    private synchronized ImsPhoneConnection findConnection(String callId) {
+        for (ImsPhoneConnection c : mConnections) {
+            ImsCall imsCall = c.getImsCall();
+            if (imsCall == null) continue;
+            ImsCallSession session = imsCall.getCallSession();
+            if (session == null) continue;
+
+            if (TextUtils.equals(session.getCallId(), callId)) {
+                return c;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Update the list of SrvccConnection with the given SrvccCall and ImsPhoneconnection.
+     *
+     * @param destList the list of SrvccConnection the new connection will be added
+     * @param profile the SrvccCall of the connection to be added
+     * @param c the ImsPhoneConnection of the connection to be added
+     */
+    private void addConnection(
+            List<SrvccConnection> destList, SrvccCall profile, ImsPhoneConnection c) {
+        if (destList == null) return;
+        if (profile == null) return;
+
+        int preciseCallState = profile.getPreciseCallState();
+        if (!isAlive(preciseCallState)) return;
+
+        List<ConferenceParticipant> participants = getConferenceParticipants(c);
+        if (participants != null) {
+            for (ConferenceParticipant cp : participants) {
+                if (cp.getState() == android.telecom.Connection.STATE_DISCONNECTED) {
+                    Rlog.i(LOG_TAG, "addConnection participant is disconnected");
+                    continue;
+                }
+                SrvccConnection srvccConnection = new SrvccConnection(cp, preciseCallState);
+                destList.add(srvccConnection);
+            }
+        } else {
+            SrvccConnection srvccConnection =
+                    new SrvccConnection(profile.getImsCallProfile(), c, preciseCallState);
+            destList.add(srvccConnection);
+        }
+    }
+
+    private List<ConferenceParticipant> getConferenceParticipants(ImsPhoneConnection c) {
+        if (!mSrvccTypeSupported.contains(MIDCALL_SRVCC_SUPPORT)) return null;
+
+        ImsCall imsCall = c.getImsCall();
+        if (imsCall == null) return null;
+
+        List<ConferenceParticipant> participants = imsCall.getConferenceParticipants();
+        if (participants == null || participants.isEmpty()) return null;
+        return participants;
+    }
+
+    private static boolean isAlive(int preciseCallState) {
+        switch (preciseCallState) {
+            case PRECISE_CALL_STATE_ACTIVE: return true;
+            case PRECISE_CALL_STATE_HOLDING: return true;
+            case PRECISE_CALL_STATE_DIALING: return true;
+            case PRECISE_CALL_STATE_ALERTING: return true;
+            case PRECISE_CALL_STATE_INCOMING: return true;
+            case PRECISE_CALL_STATE_WAITING: return true;
+            case PRECISE_CALL_STATE_INCOMING_SETUP: return true;
+            default:
+        }
+        return false;
+    }
+
+    /**
+     * Notifies that radio triggered IMS deregistration.
+     * @param reason the reason why the deregistration is triggered.
+     */
+    public void triggerImsDeregistration(
+            @ImsRegistrationImplBase.ImsDeregistrationReason int reason) {
+        if (mImsManager == null) return;
+        try {
+            mImsManager.triggerDeregistration(reason);
+        } catch (ImsException e) {
+            loge("triggerImsDeregistration: exception " + e);
+        }
+    }
+
+    private void updateImsRegistrationInfo() {
+        int capabilities = 0;
+
+        if (mMmTelCapabilities.isCapable(
+                MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE)) {
+            capabilities |= IMS_MMTEL_CAPABILITY_VOICE;
+        }
+        if (mMmTelCapabilities.isCapable(
+                MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO)) {
+            capabilities |= IMS_MMTEL_CAPABILITY_VIDEO;
+        }
+        if (mMmTelCapabilities.isCapable(
+                MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_SMS)) {
+            capabilities |= IMS_MMTEL_CAPABILITY_SMS;
+        }
+
+        mPhone.updateImsRegistrationInfo(capabilities);
+    }
+
+    private void registerImsTrafficSession(int token,
+                @MmTelFeature.ImsTrafficType int trafficType,
+                @MmTelFeature.ImsTrafficDirection int trafficDirection,
+                @NonNull IImsTrafficSessionCallback callback) {
+        mImsTrafficSessions.put(Integer.valueOf(token),
+                new ImsTrafficSession(trafficType, trafficDirection, callback));
+    }
+
+    private void unregisterImsTrafficSession(int token) {
+        mImsTrafficSessions.remove(Integer.valueOf(token));
+    }
+
+    private ImsTrafficSession getImsTrafficSession(int token) {
+        return mImsTrafficSessions.get(Integer.valueOf(token));
+    }
+
+    private void stopAllImsTrafficTypes() {
+        boolean isEmpty = mImsTrafficSessions.isEmpty();
+        logi("stopAllImsTrafficTypes empty=" + isEmpty);
+
+        if (isEmpty) return;
+
+        mImsTrafficSessions.forEachKey(1, token -> mPhone.stopImsTraffic(token, null));
+        mImsTrafficSessions.clear();
+    }
 }
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsPhoneCommandInterface.java b/src/java/com/android/internal/telephony/imsphone/ImsPhoneCommandInterface.java
index 14952b7..33e78f6 100644
--- a/src/java/com/android/internal/telephony/imsphone/ImsPhoneCommandInterface.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsPhoneCommandInterface.java
@@ -511,6 +511,10 @@
     }
 
     @Override
+    public void getImei(Message response) {
+    }
+
+    @Override
     public void getCDMASubscription(Message response) {
     }
 
@@ -609,6 +613,12 @@
     public void iccTransmitApduLogicalChannel(int channel, int cla, int instruction,
                                               int p1, int p2, int p3, String data,
                                               Message response) {}
+
+    @Override
+    public void iccTransmitApduLogicalChannel(int channel, int cla, int instruction,
+                                              int p1, int p2, int p3, String data,
+                                              boolean isEs10Command, Message response) {}
+
     @Override
     public void iccTransmitApduBasicChannel(int cla, int instruction, int p1, int p2,
                                             int p3, String data, Message response) {}
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsPhoneConnection.java b/src/java/com/android/internal/telephony/imsphone/ImsPhoneConnection.java
old mode 100755
new mode 100644
index 68de4a3..581b6ff
--- a/src/java/com/android/internal/telephony/imsphone/ImsPhoneConnection.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsPhoneConnection.java
@@ -244,7 +244,8 @@
 
     /** This is an MO call, created when dialing */
     public ImsPhoneConnection(Phone phone, String dialString, ImsPhoneCallTracker ct,
-            ImsPhoneCall parent, boolean isEmergency, boolean isWpsCall) {
+            ImsPhoneCall parent, boolean isEmergency, boolean isWpsCall,
+            ImsPhone.ImsDialArgs dialArgs) {
         super(PhoneConstants.PHONE_TYPE_IMS);
         createWakeLock(phone.getContext());
         acquireWakeLock();
@@ -272,6 +273,13 @@
         mIsEmergency = isEmergency;
         if (isEmergency) {
             setEmergencyCallInfo(mOwner);
+
+            if (getEmergencyNumberInfo() == null) {
+                // There was no emergency number info found for this call, however it is
+                // still marked as an emergency number. This may happen if it was a redialed
+                // non-detectable emergency call from IMS.
+                setNonDetectableEmergencyCallInfo(dialArgs.eccCategory);
+            }
         }
 
         mIsWpsCall = isWpsCall;
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsPhoneMmiCode.java b/src/java/com/android/internal/telephony/imsphone/ImsPhoneMmiCode.java
index 27a8697..a033edd 100644
--- a/src/java/com/android/internal/telephony/imsphone/ImsPhoneMmiCode.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsPhoneMmiCode.java
@@ -58,6 +58,7 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.CallForwardInfo;
 import com.android.internal.telephony.CallStateException;
+import com.android.internal.telephony.CallWaitingController;
 import com.android.internal.telephony.CommandException;
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.MmiCode;
@@ -1096,10 +1097,25 @@
                 int serviceClass = siToServiceClass(mSia);
 
                 if (isActivate() || isDeactivate()) {
+                    if (serviceClass == SERVICE_CLASS_NONE
+                            || (serviceClass & SERVICE_CLASS_VOICE) == SERVICE_CLASS_VOICE) {
+                        if (mPhone.getTerminalBasedCallWaitingState(false)
+                                != CallWaitingController.TERMINAL_BASED_NOT_SUPPORTED) {
+                            mPhone.getDefaultPhone().setCallWaiting(isActivate(), serviceClass,
+                                    obtainMessage(EVENT_SET_COMPLETE, this));
+                            return;
+                        }
+                    }
                     mPhone.setCallWaiting(isActivate(), serviceClass,
                             obtainMessage(EVENT_SET_COMPLETE, this));
                 } else if (isInterrogate()) {
-                    mPhone.getCallWaiting(obtainMessage(EVENT_QUERY_COMPLETE, this));
+                    if (mPhone.getTerminalBasedCallWaitingState(false)
+                            != CallWaitingController.TERMINAL_BASED_NOT_SUPPORTED) {
+                        mPhone.getDefaultPhone()
+                                .getCallWaiting(obtainMessage(EVENT_QUERY_COMPLETE, this));
+                    } else {
+                        mPhone.getCallWaiting(obtainMessage(EVENT_QUERY_COMPLETE, this));
+                    }
                 } else {
                     throw new RuntimeException ("Invalid or Unsupported MMI Code");
                 }
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsRegistrationCallbackHelper.java b/src/java/com/android/internal/telephony/imsphone/ImsRegistrationCallbackHelper.java
index 115f6fe..9452e2a 100644
--- a/src/java/com/android/internal/telephony/imsphone/ImsRegistrationCallbackHelper.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsRegistrationCallbackHelper.java
@@ -20,8 +20,10 @@
 import android.annotation.NonNull;
 import android.net.Uri;
 import android.telephony.ims.ImsReasonInfo;
+import android.telephony.ims.ImsRegistrationAttributes;
 import android.telephony.ims.RegistrationManager;
 import android.telephony.ims.aidl.IImsRegistrationCallback;
+import android.telephony.ims.stub.ImsRegistrationImplBase;
 import android.util.Log;
 
 import java.util.concurrent.Executor;
@@ -40,7 +42,7 @@
         /**
          * Handle the callback when IMS is registered.
          */
-        void handleImsRegistered(int imsRadioTech);
+        void handleImsRegistered(@NonNull ImsRegistrationAttributes attributes);
 
         /**
          * Handle the callback when IMS is registering.
@@ -50,7 +52,9 @@
         /**
          * Handle the callback when IMS is unregistered.
          */
-        void handleImsUnregistered(ImsReasonInfo imsReasonInfo);
+        void handleImsUnregistered(ImsReasonInfo imsReasonInfo,
+                @RegistrationManager.SuggestedAction int suggestedAction,
+                @ImsRegistrationImplBase.ImsRegistrationTech int imsRadioTech);
 
         /**
          * Handle the callback when the list of subscriber {@link Uri}s associated with this IMS
@@ -66,9 +70,9 @@
     private final RegistrationManager.RegistrationCallback mImsRegistrationCallback =
             new RegistrationManager.RegistrationCallback() {
                 @Override
-                public void onRegistered(int imsRadioTech) {
+                public void onRegistered(@NonNull ImsRegistrationAttributes attributes) {
                     updateRegistrationState(RegistrationManager.REGISTRATION_STATE_REGISTERED);
-                    mImsRegistrationUpdate.handleImsRegistered(imsRadioTech);
+                    mImsRegistrationUpdate.handleImsRegistered(attributes);
                 }
 
                 @Override
@@ -79,8 +83,17 @@
 
                 @Override
                 public void onUnregistered(ImsReasonInfo imsReasonInfo) {
+                    onUnregistered(imsReasonInfo, RegistrationManager.SUGGESTED_ACTION_NONE,
+                            ImsRegistrationImplBase.REGISTRATION_TECH_NONE);
+                }
+
+                @Override
+                public void onUnregistered(ImsReasonInfo imsReasonInfo,
+                        @RegistrationManager.SuggestedAction int suggestedAction,
+                        @ImsRegistrationImplBase.ImsRegistrationTech int imsRadioTech) {
                     updateRegistrationState(RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED);
-                    mImsRegistrationUpdate.handleImsUnregistered(imsReasonInfo);
+                    mImsRegistrationUpdate.handleImsUnregistered(imsReasonInfo, suggestedAction,
+                            imsRadioTech);
                 }
 
                 @Override
diff --git a/src/java/com/android/internal/telephony/metrics/DataCallSessionStats.java b/src/java/com/android/internal/telephony/metrics/DataCallSessionStats.java
index bfa081b..9af6627 100644
--- a/src/java/com/android/internal/telephony/metrics/DataCallSessionStats.java
+++ b/src/java/com/android/internal/telephony/metrics/DataCallSessionStats.java
@@ -29,7 +29,9 @@
 import android.telephony.Annotation.NetworkType;
 import android.telephony.DataFailCause;
 import android.telephony.ServiceState;
+import android.telephony.SubscriptionInfo;
 import android.telephony.TelephonyManager;
+import android.telephony.data.ApnSetting;
 import android.telephony.data.ApnSetting.ProtocolType;
 import android.telephony.data.DataCallResponse;
 import android.telephony.data.DataService;
@@ -41,6 +43,8 @@
 import com.android.internal.telephony.ServiceStateTracker;
 import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.nano.PersistAtomsProto.DataCallSession;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.telephony.Rlog;
 
 import java.util.Arrays;
@@ -257,6 +261,22 @@
     private void endDataCallSession() {
         mDataCallSession.oosAtEnd = getIsOos();
         mDataCallSession.ongoing = false;
+        // set if this data call is established for internet on the non-Dds
+        SubscriptionInfo subInfo;
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            subInfo = SubscriptionManagerService.getInstance()
+                    .getSubscriptionInfo(mPhone.getSubId());
+        } else {
+            subInfo = SubscriptionController.getInstance()
+                    .getSubscriptionInfo(mPhone.getSubId());
+        }
+        if (mPhone.getSubId() != SubscriptionController.getInstance().getDefaultDataSubId()
+                && ((mDataCallSession.apnTypeBitmask & ApnSetting.TYPE_DEFAULT)
+                == ApnSetting.TYPE_DEFAULT)
+                && subInfo != null && !subInfo.isOpportunistic()) {
+            mDataCallSession.isNonDds = true;
+        }
+
         // store for the data call list event, after DataCall is disconnected and entered into
         // inactive mode
         PhoneFactory.getMetricsCollector().unregisterOngoingDataCallStat(this);
@@ -292,6 +312,7 @@
                 call.handoverFailureCauses.length);
         copy.handoverFailureRat = Arrays.copyOf(call.handoverFailureRat,
                 call.handoverFailureRat.length);
+        copy.isNonDds = call.isNonDds;
         return copy;
     }
 
@@ -316,6 +337,7 @@
         proto.ongoing = true;
         proto.handoverFailureCauses = new int[0];
         proto.handoverFailureRat = new int[0];
+        proto.isNonDds = false;
         return proto;
     }
 
@@ -323,12 +345,17 @@
         ServiceStateTracker serviceStateTracker = mPhone.getServiceStateTracker();
         ServiceState serviceState =
                 serviceStateTracker != null ? serviceStateTracker.getServiceState() : null;
-        return serviceState != null ? serviceState.getRoaming() : false;
+        return serviceState != null && serviceState.getRoaming();
     }
 
     private boolean getIsOpportunistic() {
+        if (mPhone.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
+                    .getSubscriptionInfoInternal(mPhone.getSubId());
+            return subInfo != null && subInfo.isOpportunistic();
+        }
         SubscriptionController subController = SubscriptionController.getInstance();
-        return subController != null ? subController.isOpportunistic(mPhone.getSubId()) : false;
+        return subController != null && subController.isOpportunistic(mPhone.getSubId());
     }
 
     private boolean getIsOos() {
@@ -336,8 +363,7 @@
         ServiceState serviceState =
                 serviceStateTracker != null ? serviceStateTracker.getServiceState() : null;
         return serviceState != null
-                ? serviceState.getDataRegistrationState() == ServiceState.STATE_OUT_OF_SERVICE
-                : false;
+                && serviceState.getDataRegistrationState() == ServiceState.STATE_OUT_OF_SERVICE;
     }
 
     private void logi(String format, Object... args) {
diff --git a/src/java/com/android/internal/telephony/metrics/DataStallRecoveryStats.java b/src/java/com/android/internal/telephony/metrics/DataStallRecoveryStats.java
index 77e758b..38db78d 100644
--- a/src/java/com/android/internal/telephony/metrics/DataStallRecoveryStats.java
+++ b/src/java/com/android/internal/telephony/metrics/DataStallRecoveryStats.java
@@ -16,16 +16,22 @@
 
 package com.android.internal.telephony.metrics;
 
+import android.telephony.AccessNetworkConstants;
 import android.telephony.Annotation.NetworkType;
+import android.telephony.CellSignalStrength;
+import android.telephony.NetworkRegistrationInfo;
 import android.telephony.ServiceState;
 import android.telephony.TelephonyManager;
 
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.ServiceStateTracker;
 import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.TelephonyStatsLog;
 import com.android.internal.telephony.data.DataStallRecoveryManager;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 
 /** Generates metrics related to data stall recovery events per phone ID for the pushed atom. */
 public class DataStallRecoveryStats {
@@ -48,13 +54,15 @@
      * @param isRecovered The data stall symptom recovered or not.
      * @param durationMillis The duration from data stall symptom occurred.
      * @param reason The recovered(data resume) reason.
+     * @param isFirstValidation The validation status if it's the first come after recovery.
      */
     public static void onDataStallEvent(
             @DataStallRecoveryManager.RecoveryAction int recoveryAction,
             Phone phone,
             boolean isRecovered,
             int durationMillis,
-            @DataStallRecoveryManager.RecoveredReason int reason) {
+            @DataStallRecoveryManager.RecoveredReason int reason,
+            boolean isFirstValidation) {
         if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_IMS) {
             phone = phone.getDefaultPhone();
         }
@@ -74,6 +82,38 @@
             recoveryAction = RECOVERY_ACTION_RESET_MODEM_MAPPING;
         }
 
+        // collect info of the other device in case of DSDS
+        int otherSignalStrength = CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
+        // the number returned here matches the NetworkRegistrationState enum we have
+        int otherNetworkRegState = NetworkRegistrationInfo
+                .REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING;
+        for (Phone otherPhone : PhoneFactory.getPhones()) {
+            if (otherPhone.getPhoneId() == phone.getPhoneId()) continue;
+            if (!getIsOpportunistic(otherPhone)) {
+                otherSignalStrength = otherPhone.getSignalStrength().getLevel();
+                NetworkRegistrationInfo regInfo = otherPhone.getServiceState()
+                        .getNetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS,
+                                AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+                if (regInfo != null) {
+                    otherNetworkRegState = regInfo.getRegistrationState();
+                }
+                break;
+            }
+        }
+
+        // the number returned here matches the NetworkRegistrationState enum we have
+        int phoneNetworkRegState = NetworkRegistrationInfo
+                .REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING;
+
+        NetworkRegistrationInfo phoneRegInfo = phone.getServiceState()
+                        .getNetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS,
+                                AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+        if (phoneRegInfo != null) {
+            phoneNetworkRegState = phoneRegInfo.getRegistrationState();
+        }
+
+        int phoneId = phone.getPhoneId();
+
         TelephonyStatsLog.write(
                 TelephonyStatsLog.DATA_STALL_RECOVERY_REPORTED,
                 carrierId,
@@ -85,7 +125,12 @@
                 band,
                 isRecovered,
                 durationMillis,
-                reason);
+                reason,
+                otherSignalStrength,
+                otherNetworkRegState,
+                phoneNetworkRegState,
+                isFirstValidation,
+                phoneId);
     }
 
     /** Returns the RAT used for data (including IWLAN). */
@@ -99,7 +144,12 @@
     }
 
     private static boolean getIsOpportunistic(Phone phone) {
+        if (phone.isSubscriptionManagerServiceEnabled()) {
+            SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
+                    .getSubscriptionInfoInternal(phone.getSubId());
+            return subInfo != null && subInfo.isOpportunistic();
+        }
         SubscriptionController subController = SubscriptionController.getInstance();
-        return subController != null ? subController.isOpportunistic(phone.getSubId()) : false;
+        return subController != null && subController.isOpportunistic(phone.getSubId());
     }
 }
diff --git a/src/java/com/android/internal/telephony/metrics/DeviceTelephonyPropertiesStats.java b/src/java/com/android/internal/telephony/metrics/DeviceTelephonyPropertiesStats.java
new file mode 100644
index 0000000..51fe20c
--- /dev/null
+++ b/src/java/com/android/internal/telephony/metrics/DeviceTelephonyPropertiesStats.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.metrics;
+
+import com.android.internal.telephony.PhoneFactory;
+
+/** Metrics for the telephony related properties on the device. */
+public class DeviceTelephonyPropertiesStats {
+    private static final String TAG = DeviceTelephonyPropertiesStats.class.getSimpleName();
+
+    /**
+     * Record whenever the auto data switch feature is toggled.
+     */
+    public static void recordAutoDataSwitchFeatureToggle() {
+        PersistAtomsStorage storage = PhoneFactory.getMetricsCollector().getAtomsStorage();
+        storage.recordToggledAutoDataSwitch();
+    }
+}
diff --git a/src/java/com/android/internal/telephony/metrics/MetricsCollector.java b/src/java/com/android/internal/telephony/metrics/MetricsCollector.java
index 2b4019c..360eda8 100644
--- a/src/java/com/android/internal/telephony/metrics/MetricsCollector.java
+++ b/src/java/com/android/internal/telephony/metrics/MetricsCollector.java
@@ -16,10 +16,6 @@
 
 package com.android.internal.telephony.metrics;
 
-import static android.text.format.DateUtils.HOUR_IN_MILLIS;
-import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
-import static android.text.format.DateUtils.SECOND_IN_MILLIS;
-
 import static com.android.internal.telephony.TelephonyStatsLog.CARRIER_ID_TABLE_VERSION;
 import static com.android.internal.telephony.TelephonyStatsLog.CELLULAR_DATA_SERVICE_SWITCH;
 import static com.android.internal.telephony.TelephonyStatsLog.CELLULAR_SERVICE_STATE;
@@ -33,6 +29,7 @@
 import static com.android.internal.telephony.TelephonyStatsLog.IMS_REGISTRATION_STATS;
 import static com.android.internal.telephony.TelephonyStatsLog.IMS_REGISTRATION_TERMINATION;
 import static com.android.internal.telephony.TelephonyStatsLog.INCOMING_SMS;
+import static com.android.internal.telephony.TelephonyStatsLog.OUTGOING_SHORT_CODE_SMS;
 import static com.android.internal.telephony.TelephonyStatsLog.OUTGOING_SMS;
 import static com.android.internal.telephony.TelephonyStatsLog.PER_SIM_STATUS;
 import static com.android.internal.telephony.TelephonyStatsLog.PRESENCE_NOTIFY_EVENT;
@@ -52,6 +49,8 @@
 
 import android.app.StatsManager;
 import android.content.Context;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
 import android.util.StatsEvent;
 
 import com.android.internal.annotations.VisibleForTesting;
@@ -71,6 +70,7 @@
 import com.android.internal.telephony.nano.PersistAtomsProto.ImsRegistrationTermination;
 import com.android.internal.telephony.nano.PersistAtomsProto.IncomingSms;
 import com.android.internal.telephony.nano.PersistAtomsProto.NetworkRequestsV2;
+import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingShortCodeSms;
 import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingSms;
 import com.android.internal.telephony.nano.PersistAtomsProto.PresenceNotifyEvent;
 import com.android.internal.telephony.nano.PersistAtomsProto.RcsAcsProvisioningStats;
@@ -85,6 +85,7 @@
 import com.android.internal.util.ConcurrentUtils;
 import com.android.telephony.Rlog;
 
+import java.time.Duration;
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.List;
@@ -104,6 +105,10 @@
     /** Disables various restrictions to ease debugging during development. */
     private static final boolean DBG = false; // STOPSHIP if true
 
+    private static final long MILLIS_PER_HOUR = Duration.ofHours(1).toMillis();
+    private static final long MILLIS_PER_MINUTE = Duration.ofMinutes(1).toMillis();
+    private static final long MILLIS_PER_SECOND = Duration.ofSeconds(1).toMillis();
+
     /**
      * Sets atom pull cool down to 23 hours to help enforcing privacy requirement.
      *
@@ -111,7 +116,7 @@
      * that occur once a day.
      */
     private static final long MIN_COOLDOWN_MILLIS =
-            DBG ? 10L * SECOND_IN_MILLIS : 23L * HOUR_IN_MILLIS;
+            DBG ? 10L * MILLIS_PER_SECOND : 23L * MILLIS_PER_HOUR;
 
     /**
      * Buckets with less than these many calls will be dropped.
@@ -122,7 +127,7 @@
 
     /** Bucket size in milliseconds to round call durations into. */
     private static final long DURATION_BUCKET_MILLIS =
-            DBG ? 2L * SECOND_IN_MILLIS : 5L * MINUTE_IN_MILLIS;
+            DBG ? 2L * MILLIS_PER_SECOND : 5L * MILLIS_PER_MINUTE;
 
     private final PersistAtomsStorage mStorage;
     private final StatsManager mStatsManager;
@@ -141,6 +146,7 @@
         mStorage = storage;
         mStatsManager = (StatsManager) context.getSystemService(Context.STATS_MANAGER);
         if (mStatsManager != null) {
+            // Most (but not all) of these are subject to cooldown specified by MIN_COOLDOWN_MILLIS.
             registerAtom(CELLULAR_DATA_SERVICE_SWITCH);
             registerAtom(CELLULAR_SERVICE_STATE);
             registerAtom(SIM_SLOT_STATE);
@@ -169,6 +175,7 @@
             registerAtom(PRESENCE_NOTIFY_EVENT);
             registerAtom(GBA_EVENT);
             registerAtom(PER_SIM_STATUS);
+            registerAtom(OUTGOING_SHORT_CODE_SMS);
             Rlog.d(TAG, "registered");
         } else {
             Rlog.e(TAG, "could not get StatsManager, atoms not registered");
@@ -243,6 +250,8 @@
                 return pullGbaEvent(data);
             case PER_SIM_STATUS:
                 return pullPerSimStatus(data);
+            case OUTGOING_SHORT_CODE_SMS:
+                return pullOutgoingShortCodeSms(data);
             default:
                 Rlog.e(TAG, String.format("unexpected atom ID %d", atomTag));
                 return StatsManager.PULL_SKIP;
@@ -469,13 +478,22 @@
         }
     }
 
-    private static int pullDeviceTelephonyProperties(List<StatsEvent> data) {
+    private int pullDeviceTelephonyProperties(List<StatsEvent> data) {
         Phone[] phones = getPhonesIfAny();
         if (phones.length == 0) {
             return StatsManager.PULL_SKIP;
         }
+        boolean isAutoDataSwitchOn = Arrays.stream(phones)
+                .anyMatch(phone ->
+                        phone.getSubId() != SubscriptionManager.getDefaultDataSubscriptionId()
+                                && phone.getDataSettingsManager().isMobileDataPolicyEnabled(
+                        TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH));
+        boolean hasDedicatedManagedProfileSub = Arrays.stream(phones)
+                .anyMatch(Phone::isManagedProfile);
 
-        data.add(TelephonyStatsLog.buildStatsEvent(DEVICE_TELEPHONY_PROPERTIES, true));
+        data.add(TelephonyStatsLog.buildStatsEvent(DEVICE_TELEPHONY_PROPERTIES, true,
+                isAutoDataSwitchOn, mStorage.getAutoDataSwitchToggleCount(),
+                hasDedicatedManagedProfileSub));
         return StatsManager.PULL_SUCCESS;
     }
 
@@ -684,6 +702,19 @@
         return result;
     }
 
+    private int pullOutgoingShortCodeSms(List<StatsEvent> data) {
+        OutgoingShortCodeSms[] outgoingShortCodeSmsList = mStorage
+                .getOutgoingShortCodeSms(MIN_COOLDOWN_MILLIS);
+        if (outgoingShortCodeSmsList != null) {
+            // Outgoing short code SMS list is already shuffled when SMS were inserted
+            Arrays.stream(outgoingShortCodeSmsList).forEach(sms -> data.add(buildStatsEvent(sms)));
+            return StatsManager.PULL_SUCCESS;
+        } else {
+            Rlog.w(TAG, "OUTGOING_SHORT_CODE_SMS pull too frequent, skipping");
+            return StatsManager.PULL_SKIP;
+        }
+    }
+
     /** Registers a pulled atom ID {@code atomId}. */
     private void registerAtom(int atomId) {
         mStatsManager.setPullAtomCallback(atomId, /* metadata= */ null,
@@ -712,8 +743,9 @@
                 state.simSlotIndex,
                 state.isMultiSim,
                 state.carrierId,
-                (int) (round(state.totalTimeMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS),
-                state.isEmergencyOnly);
+                roundAndConvertMillisToSeconds(state.totalTimeMillis),
+                state.isEmergencyOnly,
+                state.isInternetPdnUp);
     }
 
     private static StatsEvent buildStatsEvent(VoiceCallRatUsage usage) {
@@ -721,7 +753,7 @@
                 VOICE_CALL_RAT_USAGE,
                 usage.carrierId,
                 usage.rat,
-                round(usage.totalDurationMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS,
+                roundAndConvertMillisToSeconds(usage.totalDurationMillis),
                 usage.callCount);
     }
 
@@ -784,7 +816,8 @@
                 sms.isEsim,
                 sms.carrierId,
                 sms.messageId,
-                sms.count);
+                sms.count,
+                sms.isManagedProfile);
     }
 
     private static StatsEvent buildStatsEvent(OutgoingSms sms) {
@@ -804,7 +837,10 @@
                 sms.messageId,
                 sms.retryId,
                 sms.intervalMillis,
-                sms.count);
+                sms.count,
+                sms.sendErrorCode,
+                sms.networkErrorCode,
+                sms.isManagedProfile);
     }
 
     private static StatsEvent buildStatsEvent(DataCallSession dataCallSession) {
@@ -826,11 +862,13 @@
                 dataCallSession.failureCause,
                 dataCallSession.suggestedRetryMillis,
                 dataCallSession.deactivateReason,
-                round(dataCallSession.durationMinutes, DURATION_BUCKET_MILLIS / MINUTE_IN_MILLIS),
+                roundAndConvertMillisToMinutes(
+                        dataCallSession.durationMinutes * MILLIS_PER_MINUTE),
                 dataCallSession.ongoing,
                 dataCallSession.bandAtEnd,
                 dataCallSession.handoverFailureCauses,
-                dataCallSession.handoverFailureRat);
+                dataCallSession.handoverFailureRat,
+                dataCallSession.isNonDds);
     }
 
     private static StatsEvent buildStatsEvent(ImsRegistrationStats stats) {
@@ -839,19 +877,15 @@
                 stats.carrierId,
                 stats.simSlotIndex,
                 stats.rat,
-                (int) (round(stats.registeredMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS),
-                (int) (round(stats.voiceCapableMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS),
-                (int)
-                        (round(stats.voiceAvailableMillis, DURATION_BUCKET_MILLIS)
-                                / SECOND_IN_MILLIS),
-                (int) (round(stats.smsCapableMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS),
-                (int) (round(stats.smsAvailableMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS),
-                (int) (round(stats.videoCapableMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS),
-                (int)
-                        (round(stats.videoAvailableMillis, DURATION_BUCKET_MILLIS)
-                                / SECOND_IN_MILLIS),
-                (int) (round(stats.utCapableMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS),
-                (int) (round(stats.utAvailableMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS));
+                roundAndConvertMillisToSeconds(stats.registeredMillis),
+                roundAndConvertMillisToSeconds(stats.voiceCapableMillis),
+                roundAndConvertMillisToSeconds(stats.voiceAvailableMillis),
+                roundAndConvertMillisToSeconds(stats.smsCapableMillis),
+                roundAndConvertMillisToSeconds(stats.smsAvailableMillis),
+                roundAndConvertMillisToSeconds(stats.videoCapableMillis),
+                roundAndConvertMillisToSeconds(stats.videoAvailableMillis),
+                roundAndConvertMillisToSeconds(stats.utCapableMillis),
+                roundAndConvertMillisToSeconds(stats.utAvailableMillis));
     }
 
     private static StatsEvent buildStatsEvent(ImsRegistrationTermination termination) {
@@ -882,7 +916,7 @@
                 stats.slotId,
                 stats.featureTagName,
                 stats.registrationTech,
-                (int) (round(stats.registeredMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS));
+                roundAndConvertMillisToSeconds(stats.registeredMillis));
     }
 
     private static StatsEvent buildStatsEvent(RcsClientProvisioningStats stats) {
@@ -903,7 +937,7 @@
                 stats.responseType,
                 stats.isSingleRegistrationEnabled,
                 stats.count,
-                (int) (round(stats.stateTimerMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS));
+                roundAndConvertMillisToSeconds(stats.stateTimerMillis));
     }
 
     private static StatsEvent buildStatsEvent(SipDelegateStats stats) {
@@ -912,7 +946,7 @@
                 stats.dimension,
                 stats.carrierId,
                 stats.slotId,
-                (int) (round(stats.uptimeMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS),
+                roundAndConvertMillisToSeconds(stats.uptimeMillis),
                 stats.destroyReason);
     }
 
@@ -924,7 +958,7 @@
                 stats.featureTagName,
                 stats.sipTransportDeniedReason,
                 stats.sipTransportDeregisteredReason,
-                (int) (round(stats.associatedMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS));
+                roundAndConvertMillisToSeconds(stats.associatedMillis));
     }
 
     private static StatsEvent buildStatsEvent(SipMessageResponse stats) {
@@ -984,7 +1018,7 @@
                 stats.serviceIdName,
                 stats.serviceIdVersion,
                 stats.registrationTech,
-                (int) (round(stats.publishedMillis, DURATION_BUCKET_MILLIS) / SECOND_IN_MILLIS));
+                roundAndConvertMillisToSeconds(stats.publishedMillis));
     }
 
     private static StatsEvent buildStatsEvent(UceEventStats stats) {
@@ -1022,6 +1056,14 @@
                 stats.count);
     }
 
+    private static StatsEvent buildStatsEvent(OutgoingShortCodeSms shortCodeSms) {
+        return TelephonyStatsLog.buildStatsEvent(
+                OUTGOING_SHORT_CODE_SMS,
+                shortCodeSms.category,
+                shortCodeSms.xmlVersion,
+                shortCodeSms.shortCodeSmsCount);
+    }
+
     /** Returns all phones in {@link PhoneFactory}, or an empty array if phones not made yet. */
     private static Phone[] getPhonesIfAny() {
         try {
@@ -1032,8 +1074,21 @@
         }
     }
 
-    /** Returns the value rounded to the bucket. */
-    private static long round(long value, long bucket) {
-        return bucket == 0 ? value : ((value + bucket / 2) / bucket) * bucket;
+    /**
+     * Rounds the duration and converts it from milliseconds to seconds.
+     */
+    private static int roundAndConvertMillisToSeconds(long valueMillis) {
+        long roundedValueMillis = Math.round((double) valueMillis / DURATION_BUCKET_MILLIS)
+                * DURATION_BUCKET_MILLIS;
+        return (int) (roundedValueMillis / MILLIS_PER_SECOND);
+    }
+
+    /**
+     * Rounds the duration and converts it from milliseconds to minutes.
+     */
+    private static int roundAndConvertMillisToMinutes(long valueMillis) {
+        long roundedValueMillis = Math.round((double) valueMillis / DURATION_BUCKET_MILLIS)
+                * DURATION_BUCKET_MILLIS;
+        return (int) (roundedValueMillis / MILLIS_PER_MINUTE);
     }
 }
diff --git a/src/java/com/android/internal/telephony/metrics/PerSimStatus.java b/src/java/com/android/internal/telephony/metrics/PerSimStatus.java
index 4b3e350..0b55815 100644
--- a/src/java/com/android/internal/telephony/metrics/PerSimStatus.java
+++ b/src/java/com/android/internal/telephony/metrics/PerSimStatus.java
@@ -18,9 +18,6 @@
 
 import static android.provider.Telephony.Carriers.CONTENT_URI;
 import static android.telephony.PhoneNumberUtils.areSamePhoneNumber;
-import static android.telephony.SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER;
-import static android.telephony.SubscriptionManager.PHONE_NUMBER_SOURCE_IMS;
-import static android.telephony.SubscriptionManager.PHONE_NUMBER_SOURCE_UICC;
 
 import static com.android.internal.telephony.TelephonyStatsLog.PER_SIM_STATUS__SIM_VOLTAGE_CLASS__VOLTAGE_CLASS_A;
 import static com.android.internal.telephony.TelephonyStatsLog.PER_SIM_STATUS__SIM_VOLTAGE_CLASS__VOLTAGE_CLASS_B;
@@ -36,6 +33,7 @@
 import android.net.Uri;
 import android.provider.Telephony;
 import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.telephony.data.ApnSetting;
 import android.telephony.ims.ImsManager;
@@ -46,6 +44,8 @@
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.SubscriptionController;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.UiccController;
 import com.android.internal.telephony.uicc.UiccSlot;
 
@@ -172,25 +172,43 @@
      */
     @Nullable
     private static int[] getNumberIds(Phone phone) {
-        SubscriptionController subscriptionController = SubscriptionController.getInstance();
-        if (subscriptionController == null) {
-            return null;
+        String countryIso = "";
+        String[] numbersFromAllSources;
+
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            if (SubscriptionManagerService.getInstance() == null) return null;
+            SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
+                    .getSubscriptionInfoInternal(phone.getSubId());
+            if (subInfo != null) {
+                countryIso = subInfo.getCountryIso();
+            }
+            numbersFromAllSources = new String[]{
+                    SubscriptionManagerService.getInstance().getPhoneNumber(phone.getSubId(),
+                            SubscriptionManager.PHONE_NUMBER_SOURCE_UICC, null, null),
+                    SubscriptionManagerService.getInstance().getPhoneNumber(phone.getSubId(),
+                            SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER, null, null),
+                    SubscriptionManagerService.getInstance().getPhoneNumber(phone.getSubId(),
+                            SubscriptionManager.PHONE_NUMBER_SOURCE_IMS, null, null)
+            };
+        } else {
+            SubscriptionController subscriptionController = SubscriptionController.getInstance();
+            if (subscriptionController == null) {
+                return null;
+            }
+            int subId = phone.getSubId();
+            countryIso = Optional.ofNullable(subscriptionController.getSubscriptionInfo(subId))
+                    .map(SubscriptionInfo::getCountryIso)
+                    .orElse("");
+            // numbersFromAllSources[] - phone numbers from each sources:
+            numbersFromAllSources = new String[]{
+                    subscriptionController.getPhoneNumber(subId,
+                            SubscriptionManager.PHONE_NUMBER_SOURCE_UICC, null, null), // 0
+                    subscriptionController.getPhoneNumber(subId,
+                            SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER, null, null), // 1
+                    subscriptionController.getPhoneNumber(subId,
+                            SubscriptionManager.PHONE_NUMBER_SOURCE_IMS, null, null), // 2
+            };
         }
-        int subId = phone.getSubId();
-        String countryIso =
-                Optional.ofNullable(subscriptionController.getSubscriptionInfo(subId))
-                        .map(SubscriptionInfo::getCountryIso)
-                        .orElse("");
-        // numbersFromAllSources[] - phone numbers from each sources:
-        String[] numbersFromAllSources =
-                new String[] {
-                    subscriptionController.getPhoneNumber(
-                            subId, PHONE_NUMBER_SOURCE_UICC, null, null), // 0
-                    subscriptionController.getPhoneNumber(
-                            subId, PHONE_NUMBER_SOURCE_CARRIER, null, null), // 1
-                    subscriptionController.getPhoneNumber(
-                            subId, PHONE_NUMBER_SOURCE_IMS, null, null), // 2
-                };
         int[] numberIds = new int[numbersFromAllSources.length]; // default value 0
         for (int i = 0, idForNextUniqueNumber = 1; i < numberIds.length; i++) {
             if (TextUtils.isEmpty(numbersFromAllSources[i])) {
diff --git a/src/java/com/android/internal/telephony/metrics/PersistAtomsStorage.java b/src/java/com/android/internal/telephony/metrics/PersistAtomsStorage.java
index eef88d2..f92071f 100644
--- a/src/java/com/android/internal/telephony/metrics/PersistAtomsStorage.java
+++ b/src/java/com/android/internal/telephony/metrics/PersistAtomsStorage.java
@@ -42,6 +42,7 @@
 import com.android.internal.telephony.nano.PersistAtomsProto.ImsRegistrationTermination;
 import com.android.internal.telephony.nano.PersistAtomsProto.IncomingSms;
 import com.android.internal.telephony.nano.PersistAtomsProto.NetworkRequestsV2;
+import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingShortCodeSms;
 import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingSms;
 import com.android.internal.telephony.nano.PersistAtomsProto.PersistAtoms;
 import com.android.internal.telephony.nano.PersistAtomsProto.PresenceNotifyEvent;
@@ -159,6 +160,10 @@
     /** Maximum number of GBA Event to store between pulls. */
     private final int mMaxNumGbaEventStats;
 
+
+     /** Maximum number of outgoing short code sms to store between pulls. */
+    private final int mMaxOutgoingShortCodeSms;
+
     /** Stores persist atoms and persist states of the puller. */
     @VisibleForTesting protected PersistAtoms mAtoms;
 
@@ -207,6 +212,7 @@
             mMaxNumUceEventStats = 5;
             mMaxNumPresenceNotifyEventStats = 10;
             mMaxNumGbaEventStats = 5;
+            mMaxOutgoingShortCodeSms = 5;
         } else {
             mMaxNumVoiceCallSessions = 50;
             mMaxNumSms = 25;
@@ -229,6 +235,7 @@
             mMaxNumUceEventStats = 25;
             mMaxNumPresenceNotifyEventStats = 50;
             mMaxNumGbaEventStats = 10;
+            mMaxOutgoingShortCodeSms = 10;
         }
 
         mAtoms = loadAtomsFromFile();
@@ -431,6 +438,14 @@
         }
     }
 
+    /**
+     * Store the number of times auto data switch feature is toggled.
+     */
+    public synchronized void recordToggledAutoDataSwitch() {
+        mAtoms.autoDataSwitchToggleCount++;
+        saveAtomsToFile(SAVE_TO_FILE_DELAY_FOR_UPDATE_MILLIS);
+    }
+
     /** Adds a new {@link NetworkRequestsV2} to the storage. */
     public synchronized void addNetworkRequestsV2(NetworkRequestsV2 networkRequests) {
         NetworkRequestsV2 existingMetrics = find(networkRequests);
@@ -655,6 +670,18 @@
         }
     }
 
+    /** Adds an outgoing short code sms to the storage. */
+    public synchronized void addOutgoingShortCodeSms(OutgoingShortCodeSms shortCodeSms) {
+        OutgoingShortCodeSms existingOutgoingShortCodeSms = find(shortCodeSms);
+        if (existingOutgoingShortCodeSms != null) {
+            existingOutgoingShortCodeSms.shortCodeSmsCount += 1;
+        } else {
+            mAtoms.outgoingShortCodeSms = insertAtRandomPlace(mAtoms.outgoingShortCodeSms,
+                    shortCodeSms, mMaxOutgoingShortCodeSms);
+        }
+        saveAtomsToFile(SAVE_TO_FILE_DELAY_FOR_UPDATE_MILLIS);
+    }
+
     /**
      * Returns and clears the voice call sessions if last pulled longer than {@code
      * minIntervalMillis} ago, otherwise returns {@code null}.
@@ -865,6 +892,16 @@
         }
     }
 
+    /** @return the number of times auto data switch mobile data policy is toggled. */
+    public synchronized int getAutoDataSwitchToggleCount() {
+        int count = mAtoms.autoDataSwitchToggleCount;
+        if (count > 0) {
+            mAtoms.autoDataSwitchToggleCount = 0;
+            saveAtomsToFile(SAVE_TO_FILE_DELAY_FOR_GET_MILLIS);
+        }
+        return count;
+    }
+
     /**
      * Returns and clears the ImsRegistrationFeatureTagStats if last pulled longer than
      * {@code minIntervalMillis} ago, otherwise returns {@code null}.
@@ -1174,6 +1211,24 @@
         return bitmask;
     }
 
+    /**
+     * Returns and clears the OutgoingShortCodeSms if last pulled longer than {@code
+     * minIntervalMillis} ago, otherwise returns {@code null}.
+     */
+    @Nullable
+    public synchronized OutgoingShortCodeSms[] getOutgoingShortCodeSms(long minIntervalMillis) {
+        if ((getWallTimeMillis() - mAtoms.outgoingShortCodeSmsPullTimestampMillis)
+                > minIntervalMillis) {
+            mAtoms.outgoingShortCodeSmsPullTimestampMillis = getWallTimeMillis();
+            OutgoingShortCodeSms[] previousOutgoingShortCodeSms = mAtoms.outgoingShortCodeSms;
+            mAtoms.outgoingShortCodeSms = new OutgoingShortCodeSms[0];
+            saveAtomsToFile(SAVE_TO_FILE_DELAY_FOR_GET_MILLIS);
+            return previousOutgoingShortCodeSms;
+        } else {
+            return null;
+        }
+    }
+
     /** Saves {@link PersistAtoms} to a file in private storage immediately. */
     public synchronized void flushAtoms() {
         saveAtomsToFile(0);
@@ -1309,6 +1364,8 @@
                             atoms.unmeteredNetworks,
                             UnmeteredNetworks.class
                     );
+            atoms.outgoingShortCodeSms = sanitizeAtoms(atoms.outgoingShortCodeSms,
+                    OutgoingShortCodeSms.class, mMaxOutgoingShortCodeSms);
 
             // out of caution, sanitize also the timestamps
             atoms.voiceCallRatUsagePullTimestampMillis =
@@ -1357,6 +1414,8 @@
                     sanitizeTimestamp(atoms.presenceNotifyEventPullTimestampMillis);
             atoms.gbaEventPullTimestampMillis =
                     sanitizeTimestamp(atoms.gbaEventPullTimestampMillis);
+            atoms.outgoingShortCodeSmsPullTimestampMillis =
+                    sanitizeTimestamp(atoms.outgoingShortCodeSmsPullTimestampMillis);
 
             return atoms;
         } catch (NoSuchFileException e) {
@@ -1407,7 +1466,8 @@
                     && state.simSlotIndex == key.simSlotIndex
                     && state.isMultiSim == key.isMultiSim
                     && state.carrierId == key.carrierId
-                    && state.isEmergencyOnly == key.isEmergencyOnly) {
+                    && state.isEmergencyOnly == key.isEmergencyOnly
+                    && state.isInternetPdnUp == key.isInternetPdnUp) {
                 return state;
             }
         }
@@ -1725,6 +1785,20 @@
     }
 
     /**
+     * Returns OutgoingShortCodeSms atom that has same category, xmlVersion as the given one,
+     * or {@code null} if it does not exist.
+     */
+    private @Nullable OutgoingShortCodeSms find(OutgoingShortCodeSms key) {
+        for (OutgoingShortCodeSms shortCodeSms : mAtoms.outgoingShortCodeSms) {
+            if (shortCodeSms.category == key.category
+                    && shortCodeSms.xmlVersion == key.xmlVersion) {
+                return shortCodeSms;
+            }
+        }
+        return null;
+    }
+
+    /**
      * Inserts a new element in a random position in an array with a maximum size.
      *
      * <p>If the array is full, merge with existing item if possible or replace one item randomly.
@@ -1969,6 +2043,7 @@
         atoms.uceEventStatsPullTimestampMillis = currentTime;
         atoms.presenceNotifyEventPullTimestampMillis = currentTime;
         atoms.gbaEventPullTimestampMillis = currentTime;
+        atoms.outgoingShortCodeSmsPullTimestampMillis = currentTime;
 
         Rlog.d(TAG, "created new PersistAtoms");
         return atoms;
@@ -1979,4 +2054,4 @@
         // Epoch time in UTC, preserved across reboots, but can be adjusted e.g. by the user or NTP
         return System.currentTimeMillis();
     }
-}
+}
\ No newline at end of file
diff --git a/src/java/com/android/internal/telephony/metrics/RcsStats.java b/src/java/com/android/internal/telephony/metrics/RcsStats.java
index 4a1ff0d..8d24def 100644
--- a/src/java/com/android/internal/telephony/metrics/RcsStats.java
+++ b/src/java/com/android/internal/telephony/metrics/RcsStats.java
@@ -85,6 +85,7 @@
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Random;
 import java.util.Set;
@@ -129,48 +130,51 @@
     private static final Map<String, Integer> FEATURE_TAGS = new HashMap<>();
 
     static {
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_STANDALONE_MSG.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_STANDALONE_MSG.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_STANDALONE_MSG);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_CHAT_IM.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_CHAT_IM.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_CHAT_IM);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_CHAT_SESSION.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_CHAT_SESSION.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_CHAT_SESSION);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_FILE_TRANSFER.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_FILE_TRANSFER.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_FILE_TRANSFER);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_FILE_TRANSFER_VIA_SMS.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_FILE_TRANSFER_VIA_SMS.trim()
+                        .toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_FILE_TRANSFER_VIA_SMS);
         FEATURE_TAGS.put(
-                FeatureTags.FEATURE_TAG_CALL_COMPOSER_ENRICHED_CALLING.trim().toLowerCase(),
+                FeatureTags.FEATURE_TAG_CALL_COMPOSER_ENRICHED_CALLING.trim()
+                        .toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_CALL_COMPOSER_ENRICHED_CALLING);
         FEATURE_TAGS.put(
-                FeatureTags.FEATURE_TAG_CALL_COMPOSER_VIA_TELEPHONY.trim().toLowerCase(),
+                FeatureTags.FEATURE_TAG_CALL_COMPOSER_VIA_TELEPHONY.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_CALL_COMPOSER_VIA_TELEPHONY);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_POST_CALL.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_POST_CALL.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_POST_CALL);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_SHARED_MAP.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_SHARED_MAP.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_SHARED_MAP);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_SHARED_SKETCH.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_SHARED_SKETCH.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_SHARED_SKETCH);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_GEO_PUSH.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_GEO_PUSH.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_GEO_PUSH);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_GEO_PUSH_VIA_SMS.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_GEO_PUSH_VIA_SMS.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_GEO_PUSH_VIA_SMS);
         FEATURE_TAGS.put(
-                FeatureTags.FEATURE_TAG_CHATBOT_COMMUNICATION_USING_SESSION.trim().toLowerCase(),
+                FeatureTags.FEATURE_TAG_CHATBOT_COMMUNICATION_USING_SESSION.trim()
+                        .toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_CHATBOT_COMMUNICATION_USING_SESSION);
         String FeatureTag = FeatureTags.FEATURE_TAG_CHATBOT_COMMUNICATION_USING_STANDALONE_MSG;
-        FEATURE_TAGS.put(FeatureTag.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTag.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_CHATBOT_COMMUNICATION_USING_STANDALONE_MSG);
         FEATURE_TAGS.put(
-                FeatureTags.FEATURE_TAG_CHATBOT_VERSION_SUPPORTED.trim().toLowerCase(),
+                FeatureTags.FEATURE_TAG_CHATBOT_VERSION_SUPPORTED.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_CHATBOT_VERSION_SUPPORTED);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_CHATBOT_ROLE.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_CHATBOT_ROLE.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_CHATBOT_ROLE);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_MMTEL.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_MMTEL.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_MMTEL);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_VIDEO.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_VIDEO.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_VIDEO);
-        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_PRESENCE.trim().toLowerCase(),
+        FEATURE_TAGS.put(FeatureTags.FEATURE_TAG_PRESENCE.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_PRESENCE);
     }
 
@@ -183,34 +187,42 @@
     private static final Map<String, Integer> SERVICE_IDS = new HashMap<>();
 
     static {
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_MMTEL.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_MMTEL.trim().toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_MMTEL);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CHAT_V1.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CHAT_V1.trim().toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_CHAT_V1);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CHAT_V2.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CHAT_V2.trim().toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_CHAT_V2);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_FT.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_FT.trim().toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_FT);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_FT_OVER_SMS.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_FT_OVER_SMS.trim()
+                        .toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_FT_OVER_SMS);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_GEO_PUSH.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_GEO_PUSH.trim().toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_GEO_PUSH);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_GEO_PUSH_VIA_SMS.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_GEO_PUSH_VIA_SMS.trim()
+                        .toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_GEO_PUSH_VIA_SMS);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CALL_COMPOSER.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CALL_COMPOSER.trim()
+                        .toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_CALL_COMPOSER);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_POST_CALL.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_POST_CALL.trim()
+                        .toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_POST_CALL);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_SHARED_MAP.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_SHARED_MAP.trim()
+                        .toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_SHARED_MAP);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_SHARED_SKETCH.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_SHARED_SKETCH.trim()
+                        .toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_SHARED_SKETCH);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CHATBOT.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CHATBOT.trim().toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_CHATBOT);
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CHATBOT_STANDALONE.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CHATBOT_STANDALONE.trim()
+                        .toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_CHATBOT_STANDALONE
         );
-        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CHATBOT_ROLE.trim().toLowerCase(),
+        SERVICE_IDS.put(RcsContactPresenceTuple.SERVICE_ID_CHATBOT_ROLE.trim()
+                        .toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_CHATBOT_ROLE);
     }
 
@@ -221,33 +233,33 @@
     private static final Map<String, Integer> MESSAGE_TYPE = new HashMap<>();
 
     static {
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_INVITE.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_INVITE.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_INVITE);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_ACK.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_ACK.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_ACK);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_OPTIONS.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_OPTIONS.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_OPTIONS);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_BYE.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_BYE.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_BYE);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_CANCEL.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_CANCEL.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_CANCEL);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_REGISTER.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_REGISTER.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_REGISTER);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_PRACK.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_PRACK.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_PRACK);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_SUBSCRIBE.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_SUBSCRIBE.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_SUBSCRIBE);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_NOTIFY.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_NOTIFY.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_NOTIFY);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_PUBLISH.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_PUBLISH.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_PUBLISH);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_INFO.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_INFO.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_INFO);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_REFER.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_REFER.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_REFER);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_MESSAGE.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_MESSAGE.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_MESSAGE);
-        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_UPDATE.trim().toLowerCase(),
+        MESSAGE_TYPE.put(SIP_REQUEST_MESSAGE_TYPE_UPDATE.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_UPDATE);
     }
 
@@ -1564,39 +1576,34 @@
 
     @VisibleForTesting
     protected int getSubId(int slotId) {
-        final int[] subIds = SubscriptionManager.getSubId(slotId);
-        int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
-        if (subIds != null && subIds.length >= 1) {
-            subId = subIds[0];
-        }
-        return subId;
+        return SubscriptionManager.getSubscriptionId(slotId);
     }
 
     /** Get a enum value from pre-defined feature tag name list */
     @VisibleForTesting
     public int convertTagNameToValue(@NonNull String tagName) {
-        return FEATURE_TAGS.getOrDefault(tagName.trim().toLowerCase(),
+        return FEATURE_TAGS.getOrDefault(tagName.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.IMS_FEATURE_TAG_CUSTOM);
     }
 
     /** Get a enum value from pre-defined service id list */
     @VisibleForTesting
     public int convertServiceIdToValue(@NonNull String serviceId) {
-        return SERVICE_IDS.getOrDefault(serviceId.trim().toLowerCase(),
+        return SERVICE_IDS.getOrDefault(serviceId.trim().toLowerCase(Locale.ROOT),
                 IMS_REGISTRATION_SERVICE_DESC_STATS__SERVICE_ID_NAME__SERVICE_ID_CUSTOM);
     }
 
     /** Get a enum value from pre-defined message type list */
     @VisibleForTesting
     public int convertMessageTypeToValue(@NonNull String messageType) {
-        return MESSAGE_TYPE.getOrDefault(messageType.trim().toLowerCase(),
+        return MESSAGE_TYPE.getOrDefault(messageType.trim().toLowerCase(Locale.ROOT),
                 TelephonyProtoEnums.SIP_REQUEST_CUSTOM);
     }
 
     /** Get a enum value from pre-defined reason list */
     @VisibleForTesting
     public int convertPresenceNotifyReason(@NonNull String reason) {
-        return NOTIFY_REASONS.getOrDefault(reason.trim().toLowerCase(),
+        return NOTIFY_REASONS.getOrDefault(reason.trim().toLowerCase(Locale.ROOT),
                 PRESENCE_NOTIFY_EVENT__REASON__REASON_CUSTOM);
     }
 
diff --git a/src/java/com/android/internal/telephony/metrics/ServiceStateStats.java b/src/java/com/android/internal/telephony/metrics/ServiceStateStats.java
index 9d9088e..5b78685 100644
--- a/src/java/com/android/internal/telephony/metrics/ServiceStateStats.java
+++ b/src/java/com/android/internal/telephony/metrics/ServiceStateStats.java
@@ -15,7 +15,13 @@
  */
 
 package com.android.internal.telephony.metrics;
+import static android.telephony.TelephonyManager.DATA_CONNECTED;
 
+import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_CS;
+import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_IMS;
+import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_UNKNOWN;
+
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.os.SystemClock;
 import android.telephony.AccessNetworkConstants;
@@ -24,24 +30,24 @@
 import android.telephony.NetworkRegistrationInfo;
 import android.telephony.ServiceState;
 import android.telephony.TelephonyManager;
-
-import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_CS;
-import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_IMS;
-import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_UNKNOWN;
+import android.telephony.data.DataProfile;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.ServiceStateTracker;
+import com.android.internal.telephony.data.DataNetworkController;
+import com.android.internal.telephony.data.DataNetworkController.DataNetworkControllerCallback;
 import com.android.internal.telephony.imsphone.ImsPhone;
 import com.android.internal.telephony.nano.PersistAtomsProto.CellularDataServiceSwitch;
 import com.android.internal.telephony.nano.PersistAtomsProto.CellularServiceState;
 import com.android.telephony.Rlog;
 
+import java.util.List;
 import java.util.concurrent.atomic.AtomicReference;
 
 /** Tracks service state duration and switch metrics for each phone. */
-public class ServiceStateStats {
+public class ServiceStateStats extends DataNetworkControllerCallback {
     private static final String TAG = ServiceStateStats.class.getSimpleName();
 
     private final AtomicReference<TimestampedServiceState> mLastState =
@@ -50,6 +56,7 @@
     private final PersistAtomsStorage mStorage;
 
     public ServiceStateStats(Phone phone) {
+        super(Runnable::run);
         mPhone = phone;
         mStorage = PhoneFactory.getMetricsCollector().getAtomsStorage();
     }
@@ -80,6 +87,21 @@
         addServiceState(lastState, now);
     }
 
+    /** Registers for internet pdn connected callback. */
+    public void registerDataNetworkControllerCallback() {
+        mPhone.getDataNetworkController().registerDataNetworkControllerCallback(this);
+    }
+
+    /** Updates service state when internet pdn gets connected. */
+    public void onInternetDataNetworkConnected(@NonNull List<DataProfile> dataProfiles) {
+        onInternetDataNetworkChanged(true);
+    }
+
+    /** Updates service state when internet pdn gets disconnected. */
+    public void onInternetDataNetworkDisconnected() {
+        onInternetDataNetworkChanged(false);
+    }
+
     /** Updates the current service state. */
     public void onServiceStateChanged(ServiceState serviceState) {
         final long now = getTimeMillis();
@@ -97,7 +119,7 @@
             newState.isMultiSim = SimSlotState.isMultiSim();
             newState.carrierId = mPhone.getCarrierId();
             newState.isEmergencyOnly = isEmergencyOnly(serviceState);
-
+            newState.isInternetPdnUp = isInternetPdnUp(mPhone);
             TimestampedServiceState prevState =
                     mLastState.getAndSet(new TimestampedServiceState(newState, now));
             addServiceStateAndSwitch(
@@ -224,6 +246,7 @@
         copy.carrierId = state.carrierId;
         copy.totalTimeMillis = state.totalTimeMillis;
         copy.isEmergencyOnly = state.isEmergencyOnly;
+        copy.isInternetPdnUp = state.isInternetPdnUp;
         return copy;
     }
 
@@ -310,6 +333,29 @@
                 || nrState == NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED;
     }
 
+    private static boolean isInternetPdnUp(Phone phone) {
+        DataNetworkController dataNetworkController = phone.getDataNetworkController();
+        if (dataNetworkController != null) {
+            return dataNetworkController.getInternetDataNetworkState() == DATA_CONNECTED;
+        }
+        return false;
+    }
+
+    private void onInternetDataNetworkChanged(boolean internetPdnUp) {
+        final long now = getTimeMillis();
+        TimestampedServiceState lastState =
+                mLastState.getAndUpdate(
+                        state -> {
+                            if (state.mServiceState == null) {
+                                return new TimestampedServiceState(null, now);
+                            }
+                            CellularServiceState newServiceState = copyOf(state.mServiceState);
+                            newServiceState.isInternetPdnUp = internetPdnUp;
+                            return new TimestampedServiceState(newServiceState, now);
+                        });
+        addServiceState(lastState, now);
+    }
+
     @VisibleForTesting
     protected long getTimeMillis() {
         return SystemClock.elapsedRealtime();
diff --git a/src/java/com/android/internal/telephony/metrics/SmsStats.java b/src/java/com/android/internal/telephony/metrics/SmsStats.java
index 48826fd..2f1e6a7 100644
--- a/src/java/com/android/internal/telephony/metrics/SmsStats.java
+++ b/src/java/com/android/internal/telephony/metrics/SmsStats.java
@@ -58,6 +58,7 @@
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.ServiceStateTracker;
 import com.android.internal.telephony.nano.PersistAtomsProto.IncomingSms;
+import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingShortCodeSms;
 import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingSms;
 import com.android.telephony.Rlog;
 
@@ -155,46 +156,62 @@
 
     /** Create a new atom when an outgoing SMS is sent. */
     public void onOutgoingSms(boolean isOverIms, boolean is3gpp2, boolean fallbackToCs,
-            @SmsManager.Result int errorCode, long messageId, boolean isFromDefaultApp,
+            @SmsManager.Result int sendErrorCode, long messageId, boolean isFromDefaultApp,
             long intervalMillis) {
-        onOutgoingSms(isOverIms, is3gpp2, fallbackToCs, errorCode, NO_ERROR_CODE,
+        onOutgoingSms(isOverIms, is3gpp2, fallbackToCs, sendErrorCode, NO_ERROR_CODE,
                 messageId, isFromDefaultApp, intervalMillis);
     }
 
     /** Create a new atom when an outgoing SMS is sent. */
     public void onOutgoingSms(boolean isOverIms, boolean is3gpp2, boolean fallbackToCs,
-            @SmsManager.Result int errorCode, int radioSpecificErrorCode, long messageId,
+            @SmsManager.Result int sendErrorCode, int networkErrorCode, long messageId,
             boolean isFromDefaultApp, long intervalMillis) {
         OutgoingSms proto =
                 getOutgoingDefaultProto(is3gpp2, isOverIms, messageId, isFromDefaultApp,
                         intervalMillis);
 
+        // The field errorCode is used for up-to-Android-13 devices. From Android 14, sendErrorCode
+        // and networkErrorCode will be used. The field errorCode will be deprecated when most
+        // devices use Android 14 or higher versions.
         if (isOverIms) {
             // Populate error code and result for IMS case
-            proto.errorCode = errorCode;
+            proto.errorCode = sendErrorCode;
             if (fallbackToCs) {
                 proto.sendResult = OUTGOING_SMS__SEND_RESULT__SMS_SEND_RESULT_ERROR_FALLBACK;
-            } else if (errorCode == SmsManager.RESULT_RIL_SMS_SEND_FAIL_RETRY) {
+            } else if (sendErrorCode == SmsManager.RESULT_RIL_SMS_SEND_FAIL_RETRY) {
                 proto.sendResult = OUTGOING_SMS__SEND_RESULT__SMS_SEND_RESULT_ERROR_RETRY;
-            } else if (errorCode != SmsManager.RESULT_ERROR_NONE) {
+            } else if (sendErrorCode != SmsManager.RESULT_ERROR_NONE) {
                 proto.sendResult = OUTGOING_SMS__SEND_RESULT__SMS_SEND_RESULT_ERROR;
             }
         } else {
             // Populate error code and result for CS case
-            if (errorCode == SmsManager.RESULT_RIL_SMS_SEND_FAIL_RETRY) {
+            if (sendErrorCode == SmsManager.RESULT_RIL_SMS_SEND_FAIL_RETRY) {
                 proto.sendResult = OUTGOING_SMS__SEND_RESULT__SMS_SEND_RESULT_ERROR_RETRY;
-            } else if (errorCode != SmsManager.RESULT_ERROR_NONE) {
+            } else if (sendErrorCode != SmsManager.RESULT_ERROR_NONE) {
                 proto.sendResult = OUTGOING_SMS__SEND_RESULT__SMS_SEND_RESULT_ERROR;
             }
-            proto.errorCode = radioSpecificErrorCode;
-            if (errorCode == SmsManager.RESULT_RIL_RADIO_NOT_AVAILABLE
-                    && radioSpecificErrorCode == NO_ERROR_CODE) {
+            proto.errorCode = networkErrorCode;
+            if (sendErrorCode == SmsManager.RESULT_RIL_RADIO_NOT_AVAILABLE
+                    && networkErrorCode == NO_ERROR_CODE) {
                 proto.errorCode = is3gpp2 ? NO_NETWORK_ERROR_3GPP2 : NO_NETWORK_ERROR_3GPP;
             }
         }
+
+        proto.sendErrorCode = sendErrorCode;
+        proto.networkErrorCode = networkErrorCode;
+
         mAtomsStorage.addOutgoingSms(proto);
     }
 
+    /** Create a new atom when user attempted to send an outgoing short code sms. */
+    public void onOutgoingShortCodeSms(int category, int xmlVersion) {
+        OutgoingShortCodeSms proto = new OutgoingShortCodeSms();
+        proto.category = category;
+        proto.xmlVersion = xmlVersion;
+        proto.shortCodeSmsCount = 1;
+        mAtomsStorage.addOutgoingShortCodeSms(proto);
+    }
+
     /** Creates a proto for a normal single-part {@code IncomingSms} with default values. */
     private IncomingSms getIncomingDefaultProto(boolean is3gpp2,
             @InboundSmsHandler.SmsSource int smsSource) {
@@ -216,6 +233,7 @@
         // SMS messages (e.g. those handled by OS or error cases).
         proto.messageId = RANDOM.nextLong();
         proto.count = 1;
+        proto.isManagedProfile = mPhone.isManagedProfile();
         return proto;
     }
 
@@ -241,6 +259,7 @@
         proto.retryId = 0;
         proto.intervalMillis = intervalMillis;
         proto.count = 1;
+        proto.isManagedProfile = mPhone.isManagedProfile();
         return proto;
     }
 
diff --git a/src/java/com/android/internal/telephony/metrics/VoiceCallSessionStats.java b/src/java/com/android/internal/telephony/metrics/VoiceCallSessionStats.java
index 7d68ae1..9234bfb 100644
--- a/src/java/com/android/internal/telephony/metrics/VoiceCallSessionStats.java
+++ b/src/java/com/android/internal/telephony/metrics/VoiceCallSessionStats.java
@@ -446,7 +446,7 @@
         proto.srvccFailureCount = 0L;
         proto.srvccCancellationCount = 0L;
         proto.rttEnabled = false;
-        proto.isEmergency = conn.isEmergencyCall();
+        proto.isEmergency = conn.isEmergencyCall() || conn.isNetworkIdentifiedEmergencyCall();
         proto.isRoaming = serviceState != null ? serviceState.getVoiceRoaming() : false;
         proto.isMultiparty = conn.isMultiparty();
         proto.lastKnownRat = rat;
diff --git a/src/java/com/android/internal/telephony/nitz/NitzStateMachineImpl.java b/src/java/com/android/internal/telephony/nitz/NitzStateMachineImpl.java
index 8115127..8b34933 100644
--- a/src/java/com/android/internal/telephony/nitz/NitzStateMachineImpl.java
+++ b/src/java/com/android/internal/telephony/nitz/NitzStateMachineImpl.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.app.time.UnixEpochTime;
 import android.app.timedetector.TelephonyTimeSuggestion;
 import android.app.timezonedetector.TelephonyTimeZoneSuggestion;
 import android.content.Context;
@@ -372,7 +373,7 @@
                 builder.addDebugInfo("Clearing time suggestion"
                         + " reason=" + reason);
             } else {
-                TimestampedValue<Long> newNitzTime = nitzSignal.createTimeSignal();
+                UnixEpochTime newNitzTime = nitzSignal.createTimeSignal();
                 builder.setUnixEpochTime(newNitzTime);
                 builder.addDebugInfo("Sending new time suggestion"
                         + " nitzSignal=" + nitzSignal
diff --git a/src/java/com/android/internal/telephony/nitz/TimeServiceHelperImpl.java b/src/java/com/android/internal/telephony/nitz/TimeServiceHelperImpl.java
index 9c7aac9..74b30f8 100644
--- a/src/java/com/android/internal/telephony/nitz/TimeServiceHelperImpl.java
+++ b/src/java/com/android/internal/telephony/nitz/TimeServiceHelperImpl.java
@@ -18,13 +18,13 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.app.time.UnixEpochTime;
 import android.app.timedetector.TelephonyTimeSuggestion;
 import android.app.timedetector.TimeDetector;
 import android.app.timezonedetector.TelephonyTimeZoneSuggestion;
 import android.app.timezonedetector.TimeZoneDetector;
 import android.content.Context;
 import android.os.SystemClock;
-import android.os.TimestampedValue;
 import android.util.LocalLog;
 
 import com.android.internal.telephony.Phone;
@@ -69,8 +69,9 @@
         Objects.requireNonNull(timeSuggestion);
 
         if (timeSuggestion.getUnixEpochTime() != null) {
-            TimestampedValue<Long> unixEpochTime = timeSuggestion.getUnixEpochTime();
-            TelephonyMetrics.getInstance().writeNITZEvent(mSlotIndex, unixEpochTime.getValue());
+            UnixEpochTime unixEpochTime = timeSuggestion.getUnixEpochTime();
+            TelephonyMetrics.getInstance().writeNITZEvent(
+                    mSlotIndex, unixEpochTime.getUnixEpochTimeMillis());
         }
         mTimeDetector.suggestTelephonyTime(timeSuggestion);
     }
diff --git a/src/java/com/android/internal/telephony/subscription/SubscriptionDatabaseManager.java b/src/java/com/android/internal/telephony/subscription/SubscriptionDatabaseManager.java
new file mode 100644
index 0000000..199d2d4
--- /dev/null
+++ b/src/java/com/android/internal/telephony/subscription/SubscriptionDatabaseManager.java
@@ -0,0 +1,2019 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.subscription;
+
+import android.annotation.CallbackExecutor;
+import android.annotation.ColorInt;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.UserIdInt;
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.ParcelUuid;
+import android.provider.Telephony;
+import android.provider.Telephony.SimInfo;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.SubscriptionManager.DataRoamingMode;
+import android.telephony.SubscriptionManager.DeviceToDeviceStatusSharingPreference;
+import android.telephony.SubscriptionManager.ProfileClass;
+import android.telephony.SubscriptionManager.SimDisplayNameSource;
+import android.telephony.SubscriptionManager.SubscriptionType;
+import android.telephony.SubscriptionManager.UsageSetting;
+import android.telephony.TelephonyManager;
+import android.telephony.UiccAccessRule;
+import android.telephony.ims.ImsMmTelManager;
+import android.text.TextUtils;
+import android.util.Base64;
+import android.util.IndentingPrintWriter;
+import android.util.LocalLog;
+
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.uicc.UiccController;
+import com.android.internal.util.function.TriConsumer;
+import com.android.telephony.Rlog;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+import java.util.AbstractMap;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.Executor;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+
+/**
+ * The subscription database manager is the wrapper of {@link SimInfo}
+ * table. It's a full memory cache of the entire subscription database, and the update can be
+ * asynchronously or synchronously. The database's cache allows multi threads to read
+ * simultaneously, if no write is ongoing.
+ *
+ * Note that from Android 14, directly writing into the subscription database through content
+ * resolver with {@link SimInfo#CONTENT_URI} will cause cache/db out of sync. All the read/write
+ * to the database should go through {@link SubscriptionManagerService}.
+ */
+public class SubscriptionDatabaseManager extends Handler {
+    private static final String LOG_TAG = "SDM";
+
+    /** Whether enabling verbose debugging message or not. */
+    private static final boolean VDBG = false;
+
+    /** Invalid database row index. */
+    private static final int INVALID_ROW_INDEX = -1;
+
+    /** The mapping from {@link SimInfo} table to {@link SubscriptionInfoInternal} get methods. */
+    private static final Map<String, Function<SubscriptionInfoInternal, ?>>
+            // TODO: Support SimInfo.COLUMN_CB_XXX which are still used by wear.
+            SUBSCRIPTION_GET_METHOD_MAP = Map.ofEntries(
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_UNIQUE_KEY_SUBSCRIPTION_ID,
+                    SubscriptionInfoInternal::getSubscriptionId),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ICC_ID,
+                    SubscriptionInfoInternal::getIccId),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_SIM_SLOT_INDEX,
+                    SubscriptionInfoInternal::getSimSlotIndex),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_DISPLAY_NAME,
+                    SubscriptionInfoInternal::getDisplayName),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_CARRIER_NAME,
+                    SubscriptionInfoInternal::getCarrierName),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_NAME_SOURCE,
+                    SubscriptionInfoInternal::getDisplayNameSource),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_COLOR,
+                    SubscriptionInfoInternal::getIconTint),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_NUMBER,
+                    SubscriptionInfoInternal::getNumber),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_DATA_ROAMING,
+                    SubscriptionInfoInternal::getDataRoaming),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_MCC_STRING,
+                    SubscriptionInfoInternal::getMcc),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_MNC_STRING,
+                    SubscriptionInfoInternal::getMnc),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_EHPLMNS,
+                    SubscriptionInfoInternal::getEhplmns),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_HPLMNS,
+                    SubscriptionInfoInternal::getHplmns),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_IS_EMBEDDED,
+                    SubscriptionInfoInternal::getEmbedded),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_CARD_ID,
+                    SubscriptionInfoInternal::getCardString),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ACCESS_RULES,
+                    SubscriptionInfoInternal::getNativeAccessRules),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ACCESS_RULES_FROM_CARRIER_CONFIGS,
+                    SubscriptionInfoInternal::getCarrierConfigAccessRules),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_IS_REMOVABLE,
+                    SubscriptionInfoInternal::getRemovableEmbedded),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED,
+                    SubscriptionInfoInternal::getEnhanced4GModeEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_VT_IMS_ENABLED,
+                    SubscriptionInfoInternal::getVideoTelephonyEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_WFC_IMS_ENABLED,
+                    SubscriptionInfoInternal::getWifiCallingEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_WFC_IMS_MODE,
+                    SubscriptionInfoInternal::getWifiCallingMode),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_WFC_IMS_ROAMING_MODE,
+                    SubscriptionInfoInternal::getWifiCallingModeForRoaming),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_WFC_IMS_ROAMING_ENABLED,
+                    SubscriptionInfoInternal::getWifiCallingEnabledForRoaming),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_IS_OPPORTUNISTIC,
+                    SubscriptionInfoInternal::getOpportunistic),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_GROUP_UUID,
+                    SubscriptionInfoInternal::getGroupUuid),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ISO_COUNTRY_CODE,
+                    SubscriptionInfoInternal::getCountryIso),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_CARRIER_ID,
+                    SubscriptionInfoInternal::getCarrierId),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_PROFILE_CLASS,
+                    SubscriptionInfoInternal::getProfileClass),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_SUBSCRIPTION_TYPE,
+                    SubscriptionInfoInternal::getSubscriptionType),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_GROUP_OWNER,
+                    SubscriptionInfoInternal::getGroupOwner),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES,
+                    SubscriptionInfoInternal::getEnabledMobileDataPolicies),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_IMSI,
+                    SubscriptionInfoInternal::getImsi),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_UICC_APPLICATIONS_ENABLED,
+                    SubscriptionInfoInternal::getUiccApplicationsEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_IMS_RCS_UCE_ENABLED,
+                    SubscriptionInfoInternal::getRcsUceEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED,
+                    SubscriptionInfoInternal::getCrossSimCallingEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_RCS_CONFIG,
+                    SubscriptionInfoInternal::getRcsConfig),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ALLOWED_NETWORK_TYPES_FOR_REASONS,
+                    SubscriptionInfoInternal::getAllowedNetworkTypesForReasons),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_D2D_STATUS_SHARING,
+                    SubscriptionInfoInternal::getDeviceToDeviceStatusSharingPreference),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_VOIMS_OPT_IN_STATUS,
+                    SubscriptionInfoInternal::getVoImsOptInEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS,
+                    SubscriptionInfoInternal::getDeviceToDeviceStatusSharingContacts),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED,
+                    SubscriptionInfoInternal::getNrAdvancedCallingEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER,
+                    SubscriptionInfoInternal::getNumberFromCarrier),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS,
+                    SubscriptionInfoInternal::getNumberFromIms),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_PORT_INDEX,
+                    SubscriptionInfoInternal::getPortIndex),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_USAGE_SETTING,
+                    SubscriptionInfoInternal::getUsageSetting),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_TP_MESSAGE_REF,
+                    SubscriptionInfoInternal::getLastUsedTPMessageReference),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_USER_HANDLE,
+                    SubscriptionInfoInternal::getUserId)
+    );
+
+    /**
+     * The mapping from columns in {@link android.provider.Telephony.SimInfo} table to
+     * {@link SubscriptionDatabaseManager} setting integer methods.
+     */
+    private static final Map<String, TriConsumer<SubscriptionDatabaseManager, Integer, Integer>>
+            SUBSCRIPTION_SET_INTEGER_METHOD_MAP = Map.ofEntries(
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_SIM_SLOT_INDEX,
+                    SubscriptionDatabaseManager::setSimSlotIndex),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_NAME_SOURCE,
+                    SubscriptionDatabaseManager::setDisplayNameSource),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_COLOR,
+                    SubscriptionDatabaseManager::setIconTint),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_DATA_ROAMING,
+                    SubscriptionDatabaseManager::setDataRoaming),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_IS_EMBEDDED,
+                    SubscriptionDatabaseManager::setEmbedded),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_IS_REMOVABLE,
+                    SubscriptionDatabaseManager::setRemovableEmbedded),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED,
+                    SubscriptionDatabaseManager::setEnhanced4GModeEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_VT_IMS_ENABLED,
+                    SubscriptionDatabaseManager::setVideoTelephonyEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_WFC_IMS_ENABLED,
+                    SubscriptionDatabaseManager::setWifiCallingEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_WFC_IMS_MODE,
+                    SubscriptionDatabaseManager::setWifiCallingMode),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_WFC_IMS_ROAMING_MODE,
+                    SubscriptionDatabaseManager::setWifiCallingModeForRoaming),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_WFC_IMS_ROAMING_ENABLED,
+                    SubscriptionDatabaseManager::setWifiCallingEnabledForRoaming),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_IS_OPPORTUNISTIC,
+                    SubscriptionDatabaseManager::setOpportunistic),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_CARRIER_ID,
+                    SubscriptionDatabaseManager::setCarrierId),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_PROFILE_CLASS,
+                    SubscriptionDatabaseManager::setProfileClass),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_SUBSCRIPTION_TYPE,
+                    SubscriptionDatabaseManager::setSubscriptionType),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_UICC_APPLICATIONS_ENABLED,
+                    SubscriptionDatabaseManager::setUiccApplicationsEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_IMS_RCS_UCE_ENABLED,
+                    SubscriptionDatabaseManager::setRcsUceEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED,
+                    SubscriptionDatabaseManager::setCrossSimCallingEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_D2D_STATUS_SHARING,
+                    SubscriptionDatabaseManager::setDeviceToDeviceStatusSharingPreference),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_VOIMS_OPT_IN_STATUS,
+                    SubscriptionDatabaseManager::setVoImsOptInEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED,
+                    SubscriptionDatabaseManager::setNrAdvancedCallingEnabled),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_PORT_INDEX,
+                    SubscriptionDatabaseManager::setPortIndex),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_USAGE_SETTING,
+                    SubscriptionDatabaseManager::setUsageSetting),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_TP_MESSAGE_REF,
+                    SubscriptionDatabaseManager::setLastUsedTPMessageReference),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_USER_HANDLE,
+                    SubscriptionDatabaseManager::setUserId)
+    );
+
+    /**
+     * The mapping from columns in {@link android.provider.Telephony.SimInfo} table to
+     * {@link SubscriptionDatabaseManager} setting string methods.
+     */
+    private static final Map<String, TriConsumer<SubscriptionDatabaseManager, Integer, String>>
+            SUBSCRIPTION_SET_STRING_METHOD_MAP = Map.ofEntries(
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ICC_ID,
+                    SubscriptionDatabaseManager::setIccId),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_DISPLAY_NAME,
+                    SubscriptionDatabaseManager::setDisplayName),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_CARRIER_NAME,
+                    SubscriptionDatabaseManager::setCarrierName),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_NUMBER,
+                    SubscriptionDatabaseManager::setNumber),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_MCC_STRING,
+                    SubscriptionDatabaseManager::setMcc),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_MNC_STRING,
+                    SubscriptionDatabaseManager::setMnc),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_EHPLMNS,
+                    SubscriptionDatabaseManager::setEhplmns),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_HPLMNS,
+                    SubscriptionDatabaseManager::setHplmns),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_CARD_ID,
+                    SubscriptionDatabaseManager::setCardString),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_GROUP_UUID,
+                    SubscriptionDatabaseManager::setGroupUuid),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ISO_COUNTRY_CODE,
+                    SubscriptionDatabaseManager::setCountryIso),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_GROUP_OWNER,
+                    SubscriptionDatabaseManager::setGroupOwner),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES,
+                    SubscriptionDatabaseManager::setEnabledMobileDataPolicies),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_IMSI,
+                    SubscriptionDatabaseManager::setImsi),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ALLOWED_NETWORK_TYPES_FOR_REASONS,
+                    SubscriptionDatabaseManager::setAllowedNetworkTypesForReasons),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS,
+                    SubscriptionDatabaseManager::setDeviceToDeviceStatusSharingContacts),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER,
+                    SubscriptionDatabaseManager::setNumberFromCarrier),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS,
+                    SubscriptionDatabaseManager::setNumberFromIms)
+    );
+
+    /**
+     * The mapping from columns in {@link android.provider.Telephony.SimInfo} table to
+     * {@link SubscriptionDatabaseManager} setting byte array methods.
+     */
+    private static final Map<String, TriConsumer<SubscriptionDatabaseManager, Integer, byte[]>>
+            SUBSCRIPTION_SET_BYTE_ARRAY_METHOD_MAP = Map.ofEntries(
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ACCESS_RULES,
+                    SubscriptionDatabaseManager::setNativeAccessRules),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_ACCESS_RULES_FROM_CARRIER_CONFIGS,
+                    SubscriptionDatabaseManager::setCarrierConfigAccessRules),
+            new AbstractMap.SimpleImmutableEntry<>(
+                    SimInfo.COLUMN_RCS_CONFIG,
+                    SubscriptionDatabaseManager::setRcsConfig)
+    );
+
+    /**
+     * The columns that should be in-sync between the subscriptions in the same group. Changing
+     * the value in those fields will automatically apply to the rest of the subscriptions in the
+     * group.
+     *
+     * @see SubscriptionManager#getSubscriptionsInGroup(ParcelUuid)
+     */
+    private static final Set<String> GROUP_SHARING_COLUMNS = Set.of(
+            SimInfo.COLUMN_DISPLAY_NAME,
+            SimInfo.COLUMN_NAME_SOURCE,
+            SimInfo.COLUMN_COLOR,
+            SimInfo.COLUMN_DATA_ROAMING,
+            SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED,
+            SimInfo.COLUMN_VT_IMS_ENABLED,
+            SimInfo.COLUMN_WFC_IMS_ENABLED,
+            SimInfo.COLUMN_WFC_IMS_MODE,
+            SimInfo.COLUMN_WFC_IMS_ROAMING_MODE,
+            SimInfo.COLUMN_WFC_IMS_ROAMING_ENABLED,
+            SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES,
+            SimInfo.COLUMN_UICC_APPLICATIONS_ENABLED,
+            SimInfo.COLUMN_IMS_RCS_UCE_ENABLED,
+            SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED,
+            SimInfo.COLUMN_RCS_CONFIG,
+            SimInfo.COLUMN_D2D_STATUS_SHARING,
+            SimInfo.COLUMN_VOIMS_OPT_IN_STATUS,
+            SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS,
+            SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED,
+            SimInfo.COLUMN_USER_HANDLE
+    );
+
+    /**
+     * The deprecated columns that do not have corresponding set methods in
+     * {@link SubscriptionDatabaseManager}.
+     */
+    private static final Set<String> DEPRECATED_DATABASE_COLUMNS = Set.of(
+            SimInfo.COLUMN_DISPLAY_NUMBER_FORMAT,
+            SimInfo.COLUMN_MCC,
+            SimInfo.COLUMN_MNC,
+            SimInfo.COLUMN_SIM_PROVISIONING_STATUS,
+            SimInfo.COLUMN_CB_EXTREME_THREAT_ALERT,
+            SimInfo.COLUMN_CB_SEVERE_THREAT_ALERT,
+            SimInfo.COLUMN_CB_AMBER_ALERT,
+            SimInfo.COLUMN_CB_EMERGENCY_ALERT,
+            SimInfo.COLUMN_CB_ALERT_SOUND_DURATION,
+            SimInfo.COLUMN_CB_ALERT_REMINDER_INTERVAL,
+            SimInfo.COLUMN_CB_ALERT_VIBRATE,
+            SimInfo.COLUMN_CB_ALERT_SPEECH,
+            SimInfo.COLUMN_CB_ETWS_TEST_ALERT,
+            SimInfo.COLUMN_CB_CHANNEL_50_ALERT,
+            SimInfo.COLUMN_CB_CMAS_TEST_ALERT,
+            SimInfo.COLUMN_CB_OPT_OUT_DIALOG,
+            SimInfo.COLUMN_IS_METERED,
+            SimInfo.COLUMN_DATA_ENABLED_OVERRIDE_RULES,
+            SimInfo.COLUMN_ALLOWED_NETWORK_TYPES
+    );
+
+    /** The context */
+    @NonNull
+    private final Context mContext;
+
+    /** The callback used for passing events back to {@link SubscriptionManagerService}. */
+    @NonNull
+    private final SubscriptionDatabaseManagerCallback mCallback;
+
+    /** UICC controller */
+    private final UiccController mUiccController;
+
+    /**
+     * The read/write lock to protect the entire database access. Using a Re-entrant read lock as
+     * much more read requests are expected than the write requests. All the access to
+     * {@link #mAllSubscriptionInfoInternalCache} needs to be protected by this lock.
+     */
+    @NonNull
+    private final ReadWriteLock mReadWriteLock = new ReentrantReadWriteLock();
+
+    /** Indicating whether access the database asynchronously or not. */
+    private final boolean mAsyncMode;
+
+    /** Local log for most important debug messages. */
+    @NonNull
+    private final LocalLog mLocalLog = new LocalLog(128);
+
+    /**
+     * The entire subscription database, including subscriptions from inserted, previously inserted
+     * SIMs. This is the full memory cache of the subscription database. The key is the subscription
+     * id. Note all the access to this map needs to be protected by the re-entrant lock
+     * {@link #mReadWriteLock}.
+     *
+     * @see SimInfo
+     */
+    @GuardedBy("mReadWriteLock")
+    @NonNull
+    private final Map<Integer, SubscriptionInfoInternal> mAllSubscriptionInfoInternalCache =
+            new HashMap<>(16);
+
+    /** Whether database has been loaded into the cache after boot up. */
+    private boolean mDatabaseLoaded = false;
+
+    /**
+     * This is the callback used for listening events from {@link SubscriptionDatabaseManager}.
+     */
+    public abstract static class SubscriptionDatabaseManagerCallback {
+        /** The executor of the callback. */
+        private final @NonNull Executor mExecutor;
+
+        /**
+         * Constructor
+         *
+         * @param executor The executor of the callback.
+         */
+        public SubscriptionDatabaseManagerCallback(@NonNull @CallbackExecutor Executor executor) {
+            mExecutor = executor;
+        }
+
+        /**
+         * @return The executor of the callback.
+         */
+        @VisibleForTesting
+        public @NonNull Executor getExecutor() {
+            return mExecutor;
+        }
+
+        /**
+         * Invoke the callback from executor.
+         *
+         * @param runnable The callback method to invoke.
+         */
+        public void invokeFromExecutor(@NonNull Runnable runnable) {
+            mExecutor.execute(runnable);
+        }
+
+        /**
+         * Called when database has been loaded into the cache.
+         */
+        public abstract void onDatabaseLoaded();
+
+        /**
+         * Called when subscription changed.
+         *
+         * @param subId The subscription id.
+         */
+        public abstract void onSubscriptionChanged(int subId);
+
+        /**
+         * Called when {@link SubscriptionInfoInternal#areUiccApplicationsEnabled()}
+         * changed.
+         *
+         * @param subId The subscription id.
+         */
+        public abstract void onUiccApplicationsEnabled(int subId);
+    }
+
+    /**
+     * The constructor.
+     *
+     * @param context The context.
+     * @param looper Looper for the handler.
+     * @param callback Subscription database callback.
+     */
+    public SubscriptionDatabaseManager(@NonNull Context context, @NonNull Looper looper,
+            @NonNull SubscriptionDatabaseManagerCallback callback) {
+        super(looper);
+        log("Created SubscriptionDatabaseManager.");
+        mContext = context;
+        mCallback = callback;
+        mUiccController = UiccController.getInstance();
+        mAsyncMode = mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_subscription_database_async_update);
+        loadFromDatabase();
+    }
+
+    /**
+     * Helper method to get specific field from {@link SubscriptionInfoInternal} by the database
+     * column name. {@link SubscriptionInfoInternal} represent one single record in the
+     * {@link SimInfo} table. So every column has a corresponding get method in
+     * {@link SubscriptionInfoInternal} (except for unused or deprecated columns).
+     *
+     * @param subInfo The subscription info.
+     * @param columnName The database column name.
+     *
+     * @return The corresponding value from {@link SubscriptionInfoInternal}.
+     *
+     * @throws IllegalArgumentException if {@code columnName} is invalid.
+     *
+     * @see android.provider.Telephony.SimInfo for all the columns.
+     */
+    @NonNull
+    private static Object getSubscriptionInfoFieldByColumnName(
+            @NonNull SubscriptionInfoInternal subInfo, @NonNull String columnName) {
+        if (SUBSCRIPTION_GET_METHOD_MAP.containsKey(columnName)) {
+            return SUBSCRIPTION_GET_METHOD_MAP.get(columnName).apply(subInfo);
+        }
+        throw new IllegalArgumentException("Invalid column name " + columnName);
+    }
+
+    /**
+     * Get a specific field from the subscription database by {@code subId} and {@code columnName}.
+     *
+     * @param subId The subscription id.
+     * @param columnName The database column name.
+     *
+     * @return The value from subscription database.
+     *
+     * @throws IllegalArgumentException if {@code subId} or {@code columnName} is invalid.
+     *
+     * @see android.provider.Telephony.SimInfo for all the columns.
+     */
+    @NonNull
+    public Object getSubscriptionProperty(int subId, @NonNull String columnName) {
+        SubscriptionInfoInternal subInfo = getSubscriptionInfoInternal(subId);
+        if (subInfo == null) {
+            throw new IllegalArgumentException("Invalid subId " + subId);
+        }
+
+        return getSubscriptionInfoFieldByColumnName(subInfo, columnName);
+    }
+
+    /**
+     * Set a field in the subscription database. Note not all fields are supported.
+     *
+     * @param subId Subscription Id of Subscription.
+     * @param columnName Column name in the database. Note not all fields are supported.
+     * @param value Value to store in the database.
+     *
+     * @throws IllegalArgumentException if {@code subId} or {@code columnName} is invalid, or
+     * {@code value} cannot be converted to the corresponding database column format.
+     * @throws NumberFormatException if a string value cannot be converted to integer.
+     * @throws ClassCastException if {@code value} cannot be casted to the required type.
+     *
+     * @see android.provider.Telephony.SimInfo for all the columns.
+     */
+    public void setSubscriptionProperty(int subId, @NonNull String columnName,
+            @NonNull Object value) {
+        if (SUBSCRIPTION_SET_INTEGER_METHOD_MAP.containsKey(columnName)) {
+            // For integer type columns, accepting both integer and string that can be converted to
+            // integer.
+            int intValue;
+            if (value instanceof String) {
+                intValue = Integer.parseInt((String) value);
+            } else if (value instanceof Integer) {
+                intValue = (int) value;
+            } else {
+                throw new ClassCastException("columnName=" + columnName + ", cannot cast "
+                        + value.getClass() + " to integer.");
+            }
+            SUBSCRIPTION_SET_INTEGER_METHOD_MAP.get(columnName).accept(this, subId, intValue);
+        } else if (SUBSCRIPTION_SET_STRING_METHOD_MAP.containsKey(columnName)) {
+            // For string type columns. Will throw exception if value is not string type.
+            SUBSCRIPTION_SET_STRING_METHOD_MAP.get(columnName).accept(this, subId, (String) value);
+        } else if (SUBSCRIPTION_SET_BYTE_ARRAY_METHOD_MAP.containsKey(columnName)) {
+            // For byte array type columns, accepting both byte[] and string that can be converted
+            // to byte[] using base 64 encoding/decoding.
+            byte[] byteArrayValue;
+            if (value instanceof String) {
+                byteArrayValue = Base64.decode((String) value, Base64.DEFAULT);
+            } else if (value instanceof byte[]) {
+                byteArrayValue = (byte[]) value;
+            } else {
+                throw new ClassCastException("columnName=" + columnName + ", cannot cast "
+                        + value.getClass() + " to byte[].");
+            }
+            SUBSCRIPTION_SET_BYTE_ARRAY_METHOD_MAP.get(columnName).accept(
+                    this, subId, byteArrayValue);
+        } else {
+            throw new IllegalArgumentException("Does not support set " + columnName + ".");
+        }
+    }
+
+    /**
+     * Comparing the old/new {@link SubscriptionInfoInternal} and create content values for database
+     * update. If any field in the new subscription info is different from the old one, then each
+     * delta will be added into the {@link ContentValues}.
+     *
+     * @param oldSubInfo The old {@link SubscriptionInfoInternal}.
+     * @param newSubInfo The new {@link SubscriptionInfoInternal}.
+     *
+     * @return The delta content values for database update.
+     */
+    @NonNull
+    private ContentValues createDeltaContentValues(@Nullable SubscriptionInfoInternal oldSubInfo,
+            @NonNull SubscriptionInfoInternal newSubInfo) {
+        ContentValues deltaContentValues = new ContentValues();
+
+        for (String columnName : Telephony.SimInfo.getAllColumns()) {
+            if (DEPRECATED_DATABASE_COLUMNS.contains(columnName)) continue;
+            // subId is generated by the database. Cannot be updated.
+            if (columnName.equals(SimInfo.COLUMN_UNIQUE_KEY_SUBSCRIPTION_ID)) continue;
+            Object newValue = getSubscriptionInfoFieldByColumnName(newSubInfo, columnName);
+            if (newValue != null) {
+                Object oldValue = null;
+                if (oldSubInfo != null) {
+                    oldValue = getSubscriptionInfoFieldByColumnName(oldSubInfo, columnName);
+                }
+                // Some columns need special handling. We need to convert them into a format that
+                // is accepted by the database.
+                if (!Objects.equals(oldValue, newValue)) {
+                    deltaContentValues.putObject(columnName, newValue);
+                }
+            }
+        }
+        return deltaContentValues;
+    }
+
+    /**
+     * Synchronously insert a new record into the database. This operation is synchronous because
+     * we need to convert the inserted row index into the subscription id.
+     *
+     * @param contentValues The fields of the subscription to be inserted into the database.
+     *
+     * @return The row index of the new record. {@link #INVALID_ROW_INDEX} if insertion failed.
+     */
+    private int insertNewRecordIntoDatabaseSync(@NonNull ContentValues contentValues) {
+        Objects.requireNonNull(contentValues);
+        Uri uri = mContext.getContentResolver().insert(SimInfo.CONTENT_URI, contentValues);
+        if (uri != null && uri.getLastPathSegment() != null) {
+            int subId = Integer.parseInt(uri.getLastPathSegment());
+            if (SubscriptionManager.isValidSubscriptionId(subId)) {
+                logl("insertNewRecordIntoDatabaseSync: Successfully added subscription. subId="
+                        + uri.getLastPathSegment());
+                return subId;
+            }
+        }
+
+        logel("insertNewRecordIntoDatabaseSync: Failed to insert subscription into database. "
+                + "contentValues=" + contentValues);
+        return INVALID_ROW_INDEX;
+    }
+
+    /**
+     * Insert a new subscription info. The subscription info must have subscription id
+     * {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID}. Note this is a slow method, so be
+     * cautious to call this method.
+     *
+     * @param subInfo The subscription info to update.
+     *
+     * @return The new subscription id. {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} (-1) if
+     * insertion fails.
+     */
+    public int insertSubscriptionInfo(@NonNull SubscriptionInfoInternal subInfo) {
+        Objects.requireNonNull(subInfo);
+        // A new subscription to be inserted must have invalid subscription id.
+        if (SubscriptionManager.isValidSubscriptionId(subInfo.getSubscriptionId())) {
+            throw new RuntimeException("insertSubscriptionInfo: Not a new subscription to "
+                    + "insert. subInfo=" + subInfo);
+        }
+
+        if (!mDatabaseLoaded) {
+            throw new IllegalStateException("Database has not been loaded. Can't insert new "
+                    + "record at this point.");
+        }
+
+        int subId;
+        // Grab the write lock so no other threads can read or write the cache.
+        mReadWriteLock.writeLock().lock();
+        try {
+            // Synchronously insert into the database. Note this should be the only synchronous
+            // write operation performed by the subscription database manager. The reason is that
+            // we need to get the sub id for cache update.
+            subId = insertNewRecordIntoDatabaseSync(createDeltaContentValues(null, subInfo));
+            if (subId > 0) {
+                mAllSubscriptionInfoInternalCache.put(subId, new SubscriptionInfoInternal
+                        .Builder(subInfo)
+                        .setId(subId).build());
+            } else {
+                logel("insertSubscriptionInfo: Failed to insert a new subscription. subInfo="
+                        + subInfo);
+            }
+        } finally {
+            mReadWriteLock.writeLock().unlock();
+        }
+
+        mCallback.invokeFromExecutor(() -> mCallback.onSubscriptionChanged(subId));
+        return subId;
+    }
+
+    /**
+     * Remove a subscription record from the database.
+     *
+     * @param subId The subscription id of the subscription to be deleted.
+     *
+     * @throws IllegalArgumentException If {@code subId} is invalid.
+     */
+    public void removeSubscriptionInfo(int subId) {
+        if (!mAllSubscriptionInfoInternalCache.containsKey(subId)) {
+            throw new IllegalArgumentException("subId " + subId + " is invalid.");
+        }
+
+        mReadWriteLock.writeLock().lock();
+        try {
+            if (mContext.getContentResolver().delete(SimInfo.CONTENT_URI,
+                    SimInfo.COLUMN_UNIQUE_KEY_SUBSCRIPTION_ID + "=?",
+                    new String[]{Integer.toString(subId)}) > 0) {
+                mAllSubscriptionInfoInternalCache.remove(subId);
+            } else {
+                logel("Failed to remove subscription with subId=" + subId);
+            }
+        } finally {
+            mReadWriteLock.writeLock().unlock();
+        }
+
+        mCallback.invokeFromExecutor(() -> mCallback.onSubscriptionChanged(subId));
+    }
+
+    /**
+     * Update a subscription in the database (synchronously or asynchronously).
+     *
+     * @param subId The subscription id of the subscription to be updated.
+     * @param contentValues The fields to be update.
+     *
+     * @return The number of rows updated. Note if the database is configured as asynchronously
+     * update, then this will be always 1.
+     */
+    private int updateDatabase(int subId, @NonNull ContentValues contentValues) {
+        logv("updateDatabase: prepare to update sub " + subId);
+
+        if (!mDatabaseLoaded) {
+            logel("updateDatabase: Database has not been loaded. Can't update database at this "
+                    + "point. contentValues=" + contentValues);
+            return 0;
+        }
+
+        if (mAsyncMode) {
+            // Perform the update in the handler thread asynchronously.
+            post(() -> {
+                mContext.getContentResolver().update(Uri.withAppendedPath(
+                        SimInfo.CONTENT_URI, String.valueOf(subId)), contentValues, null, null);
+                logv("updateDatabase: async updated subscription in the database."
+                            + " subId=" + subId + ", contentValues= " + contentValues.getValues());
+            });
+            return 1;
+        } else {
+            logv("updateDatabase: sync updated subscription in the database."
+                    + " subId=" + subId + ", contentValues= " + contentValues.getValues());
+
+            return mContext.getContentResolver().update(Uri.withAppendedPath(
+                    SimInfo.CONTENT_URI, String.valueOf(subId)), contentValues, null, null);
+        }
+    }
+
+    /**
+     * Update a certain field of subscription in the database. Also update the subscription cache
+     * {@link #mAllSubscriptionInfoInternalCache}.
+     *
+     * @param subId The subscription id.
+     * @param columnName The database column name from the database table {@link SimInfo}.
+     * @param newValue The new value to update the subscription info cache
+     * {@link #mAllSubscriptionInfoInternalCache}.
+     * @param builderSetMethod The {@link SubscriptionInfo.Builder} method to set a specific field
+     * when constructing the new {@link SubscriptionInfo}. This should be one of the
+     * SubscriptionInfoInternal.Builder.setXxxx method.
+     * @param <T> The type of newValue for subscription cache update.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    private <T> void writeDatabaseAndCacheHelper(int subId, @NonNull String columnName,
+            @Nullable T newValue,
+            BiFunction<SubscriptionInfoInternal.Builder, T, SubscriptionInfoInternal.Builder>
+                    builderSetMethod) {
+        ContentValues contentValues = new ContentValues();
+
+        // Grab the write lock so no other threads can read or write the cache.
+        mReadWriteLock.writeLock().lock();
+        try {
+            final SubscriptionInfoInternal oldSubInfo =
+                    mAllSubscriptionInfoInternalCache.get(subId);
+            if (oldSubInfo == null) {
+                logel("Subscription doesn't exist. subId=" + subId + ", columnName=" + columnName);
+                throw new IllegalArgumentException("Subscription doesn't exist. subId=" + subId
+                        + ", columnName=" + columnName);
+            }
+
+            // Check if writing this field should automatically write to the rest of subscriptions
+            // in the same group.
+            final boolean syncToGroup = GROUP_SHARING_COLUMNS.contains(columnName);
+
+            mAllSubscriptionInfoInternalCache.forEach((id, subInfo) -> {
+                if (id == subId || (syncToGroup && !oldSubInfo.getGroupUuid().isEmpty()
+                        && oldSubInfo.getGroupUuid().equals(subInfo.getGroupUuid()))) {
+                    // Check if the new value is different from the old value in the cache.
+                    if (!Objects.equals(getSubscriptionInfoFieldByColumnName(subInfo, columnName),
+                            newValue)) {
+                        logv("writeDatabaseAndCacheHelper: subId=" + subId + ",columnName="
+                                + columnName + ", newValue=" + newValue);
+                        // If the value is different, then we need to update the cache. Since all
+                        // fields in SubscriptionInfo are final, we need to create a new
+                        // SubscriptionInfo.
+                        SubscriptionInfoInternal.Builder builder = new SubscriptionInfoInternal
+                                .Builder(subInfo);
+
+                        // Apply the new value to the builder. This line is equivalent to
+                        // builder.setXxxxxx(newValue);
+                        builder = builderSetMethod.apply(builder, newValue);
+
+                        // Prepare the content value for update.
+                        contentValues.putObject(columnName, newValue);
+                        if (updateDatabase(id, contentValues) > 0) {
+                            // Update the subscription database cache.
+                            mAllSubscriptionInfoInternalCache.put(id, builder.build());
+                            mCallback.invokeFromExecutor(()
+                                    -> mCallback.onSubscriptionChanged(subId));
+                        }
+                    }
+                }
+            });
+        } finally {
+            mReadWriteLock.writeLock().unlock();
+        }
+    }
+
+    /**
+     * Update the database with the {@link SubscriptionInfoInternal}, and also update the cache.
+     *
+     * @param newSubInfo The new {@link SubscriptionInfoInternal}.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void updateSubscription(@NonNull SubscriptionInfoInternal newSubInfo) {
+        Objects.requireNonNull(newSubInfo);
+
+        // Grab the write lock so no other threads can read or write the cache.
+        mReadWriteLock.writeLock().lock();
+        try {
+            int subId = newSubInfo.getSubscriptionId();
+            SubscriptionInfoInternal oldSubInfo = mAllSubscriptionInfoInternalCache.get(
+                    newSubInfo.getSubscriptionId());
+            if (oldSubInfo == null) {
+                throw new IllegalArgumentException("updateSubscription: subscription does not "
+                        + "exist. subId=" + subId);
+            }
+            if (oldSubInfo.equals(newSubInfo)) return;
+
+            if (updateDatabase(subId, createDeltaContentValues(oldSubInfo, newSubInfo)) > 0) {
+                mAllSubscriptionInfoInternalCache.put(subId, newSubInfo);
+                mCallback.invokeFromExecutor(() -> mCallback.onSubscriptionChanged(subId));
+                if (oldSubInfo.areUiccApplicationsEnabled()
+                        != newSubInfo.areUiccApplicationsEnabled()) {
+                    mCallback.invokeFromExecutor(() -> mCallback.onUiccApplicationsEnabled(subId));
+                }
+            }
+        } finally {
+            mReadWriteLock.writeLock().unlock();
+        }
+    }
+
+    /**
+     * Set the ICCID of the SIM that is associated with the subscription.
+     *
+     * @param subId Subscription id.
+     * @param iccId The ICCID of the SIM that is associated with this subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setIccId(int subId, @NonNull String iccId) {
+        Objects.requireNonNull(iccId);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_ICC_ID, iccId,
+                SubscriptionInfoInternal.Builder::setIccId);
+    }
+
+    /**
+     * Set the SIM index of the slot that currently contains the subscription. Set to
+     * {@link SubscriptionManager#INVALID_SIM_SLOT_INDEX} if the subscription is inactive.
+     *
+     * @param subId Subscription id.
+     * @param simSlotIndex The SIM slot index.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setSimSlotIndex(int subId, int simSlotIndex) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_SIM_SLOT_INDEX, simSlotIndex,
+                SubscriptionInfoInternal.Builder::setSimSlotIndex);
+    }
+
+    /**
+     * Set the name displayed to the user that identifies this subscription. This name is used
+     * in Settings page and can be renamed by the user.
+     *
+     * @param subId Subscription id.
+     * @param displayName The display name.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setDisplayName(int subId, @NonNull String displayName) {
+        Objects.requireNonNull(displayName);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_DISPLAY_NAME, displayName,
+                SubscriptionInfoInternal.Builder::setDisplayName);
+    }
+
+    /**
+     * Set the name displayed to the user that identifies subscription provider name. This name
+     * is the SPN displayed in status bar and many other places. Can't be renamed by the user.
+     *
+     * @param subId Subscription id.
+     * @param carrierName The carrier name.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setCarrierName(int subId, @NonNull String carrierName) {
+        Objects.requireNonNull(carrierName);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_CARRIER_NAME, carrierName,
+                SubscriptionInfoInternal.Builder::setCarrierName);
+    }
+
+    /**
+     * Set the source of the display name.
+     *
+     * @param subId Subscription id.
+     * @param displayNameSource The source of the display name.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     *
+     * @see SubscriptionInfo#getDisplayName()
+     */
+    public void setDisplayNameSource(int subId, @SimDisplayNameSource int displayNameSource) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_NAME_SOURCE, displayNameSource,
+                SubscriptionInfoInternal.Builder::setDisplayNameSource);
+    }
+
+    /**
+     * Set the color to be used for tinting the icon when displaying to the user.
+     *
+     * @param subId Subscription id.
+     * @param iconTint The color to be used for tinting the icon when displaying to the user.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setIconTint(int subId, @ColorInt int iconTint) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_COLOR, iconTint,
+                SubscriptionInfoInternal.Builder::setIconTint);
+    }
+
+    /**
+     * Set the number presented to the user identify this subscription.
+     *
+     * @param subId Subscription id.
+     * @param number the number presented to the user identify this subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setNumber(int subId, @NonNull String number) {
+        Objects.requireNonNull(number);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_NUMBER, number,
+                SubscriptionInfoInternal.Builder::setNumber);
+    }
+
+    /**
+     * Set whether user enables data roaming for this subscription or not.
+     *
+     * @param subId Subscription id.
+     * @param dataRoaming Data roaming mode. Either
+     * {@link SubscriptionManager#DATA_ROAMING_ENABLE} or
+     * {@link SubscriptionManager#DATA_ROAMING_DISABLE}
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setDataRoaming(int subId, @DataRoamingMode int dataRoaming) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_DATA_ROAMING, dataRoaming,
+                SubscriptionInfoInternal.Builder::setDataRoaming);
+    }
+
+    /**
+     * Set the mobile country code.
+     *
+     * @param subId Subscription id.
+     * @param mcc The mobile country code.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setMcc(int subId, @NonNull String mcc) {
+        Objects.requireNonNull(mcc);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_MCC_STRING, mcc,
+                SubscriptionInfoInternal.Builder::setMcc);
+    }
+
+    /**
+     * Set the mobile network code.
+     *
+     * @param subId Subscription id.
+     * @param mnc Mobile network code.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setMnc(int subId, @NonNull String mnc) {
+        Objects.requireNonNull(mnc);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_MNC_STRING, mnc,
+                SubscriptionInfoInternal.Builder::setMnc);
+    }
+
+    /**
+     * Set EHPLMNs associated with the subscription.
+     *
+     * @param subId Subscription id.
+     * @param ehplmns EHPLMNs associated with the subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setEhplmns(int subId, @NonNull String ehplmns) {
+        Objects.requireNonNull(ehplmns);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_EHPLMNS, ehplmns,
+                SubscriptionInfoInternal.Builder::setEhplmns);
+    }
+
+    /**
+     * Set HPLMNs associated with the subscription.
+     *
+     * @param subId Subscription id.
+     * @param hplmns HPLMNs associated with the subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setHplmns(int subId, @NonNull String hplmns) {
+        Objects.requireNonNull(hplmns);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_HPLMNS, hplmns,
+                SubscriptionInfoInternal.Builder::setHplmns);
+    }
+
+    /**
+     * Set whether the subscription is from eSIM or not.
+     *
+     * @param subId Subscription id.
+     * @param isEmbedded if the subscription is from eSIM.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setEmbedded(int subId, int isEmbedded) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_IS_EMBEDDED, isEmbedded,
+                SubscriptionInfoInternal.Builder::setEmbedded);
+    }
+
+    /**
+     * Set whether the subscription is from eSIM or not.
+     *
+     * @param subId Subscription id.
+     * @param isEmbedded {@code true} if the subscription is from eSIM.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setEmbedded(int subId, boolean isEmbedded) {
+        setEmbedded(subId, isEmbedded ? 1 : 0);
+    }
+
+    /**
+     * Set the card string of the SIM card. This is usually the ICCID or EID.
+     *
+     * @param subId Subscription id.
+     * @param cardString The card string of the SIM card.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     *
+     * @see SubscriptionInfo#getCardString()
+     */
+    public void setCardString(int subId, @NonNull String cardString) {
+        Objects.requireNonNull(cardString);
+        // Also update the public card id.
+        setCardId(subId, mUiccController.convertToPublicCardId(cardString));
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_CARD_ID, cardString,
+                SubscriptionInfoInternal.Builder::setCardString);
+    }
+
+    /**
+     * Set the card id. This is the non-PII card id converted from
+     * {@link SubscriptionInfoInternal#getCardString()}. This field only exists in
+     * {@link SubscriptionInfo}, but not the database.
+     *
+     * @param subId Subscription id.
+     * @param cardId The card id.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setCardId(int subId, int cardId) {
+        // card id does not have a corresponding SimInfo column. So we only update the cache.
+
+        // Grab the write lock so no other threads can read or write the cache.
+        mReadWriteLock.writeLock().lock();
+        try {
+            SubscriptionInfoInternal subInfoCache = mAllSubscriptionInfoInternalCache.get(subId);
+            if (subInfoCache == null) {
+                throw new IllegalArgumentException("Subscription doesn't exist. subId=" + subId
+                        + ", columnName=cardId");
+            }
+            mAllSubscriptionInfoInternalCache.put(subId,
+                    new SubscriptionInfoInternal.Builder(subInfoCache)
+                            .setCardId(cardId).build());
+        } finally {
+            mReadWriteLock.writeLock().unlock();
+        }
+    }
+
+    /**
+     * Set the native access rules for this subscription, if it is embedded and defines any.
+     * This does not include access rules for non-embedded subscriptions.
+     *
+     * @param subId Subscription id.
+     * @param nativeAccessRules The native access rules for this subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setNativeAccessRules(int subId, @NonNull byte[] nativeAccessRules) {
+        Objects.requireNonNull(nativeAccessRules);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_ACCESS_RULES, nativeAccessRules,
+                SubscriptionInfoInternal.Builder::setNativeAccessRules);
+    }
+
+    /**
+     * Set the carrier certificates for this subscription that are saved in carrier configs.
+     * This does not include access rules from the Uicc, whether embedded or non-embedded.
+     *
+     * @param subId Subscription id.
+     * @param carrierConfigAccessRules The carrier certificates for this subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setCarrierConfigAccessRules(int subId, @NonNull byte[] carrierConfigAccessRules) {
+        Objects.requireNonNull(carrierConfigAccessRules);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_ACCESS_RULES_FROM_CARRIER_CONFIGS,
+                carrierConfigAccessRules,
+                SubscriptionInfoInternal.Builder::setCarrierConfigAccessRules);
+    }
+
+    /**
+     * Set the carrier certificates for this subscription that are saved in carrier configs.
+     * This does not include access rules from the Uicc, whether embedded or non-embedded.
+     *
+     * @param subId Subscription id.
+     * @param carrierConfigAccessRules The carrier certificates for this subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setCarrierConfigAccessRules(int subId,
+            @NonNull UiccAccessRule[] carrierConfigAccessRules) {
+        Objects.requireNonNull(carrierConfigAccessRules);
+        byte[] carrierConfigAccessRulesBytes = UiccAccessRule.encodeRules(carrierConfigAccessRules);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_ACCESS_RULES_FROM_CARRIER_CONFIGS,
+                carrierConfigAccessRulesBytes,
+                SubscriptionInfoInternal.Builder::setCarrierConfigAccessRules);
+    }
+
+    /**
+     * Set whether an embedded subscription is on a removable card. Such subscriptions are
+     * marked inaccessible as soon as the current card is removed. Otherwise, they will remain
+     * accessible unless explicitly deleted. Only meaningful for embedded subscription.
+     *
+     * @param subId Subscription id.
+     * @param isRemovableEmbedded if the subscription is from the removable embedded SIM.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setRemovableEmbedded(int subId, int isRemovableEmbedded) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_IS_REMOVABLE, isRemovableEmbedded,
+                SubscriptionInfoInternal.Builder::setRemovableEmbedded);
+    }
+
+    /**
+     * Set whether enhanced 4G mode is enabled by the user or not.
+     *
+     * @param subId Subscription id.
+     * @param isEnhanced4GModeEnabled whether enhanced 4G mode is enabled by the user or not.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setEnhanced4GModeEnabled(int subId, int isEnhanced4GModeEnabled) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED,
+                isEnhanced4GModeEnabled,
+                SubscriptionInfoInternal.Builder::setEnhanced4GModeEnabled);
+    }
+
+    /**
+     * Set whether video telephony is enabled by the user or not.
+     *
+     * @param subId Subscription id.
+     * @param isVideoTelephonyEnabled whether video telephony is enabled by the user or not.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setVideoTelephonyEnabled(int subId, int isVideoTelephonyEnabled) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_VT_IMS_ENABLED, isVideoTelephonyEnabled,
+                SubscriptionInfoInternal.Builder::setVideoTelephonyEnabled);
+    }
+
+    /**
+     * Set whether Wi-Fi calling is enabled by the user or not when the device is not roaming.
+     *
+     * @param subId Subscription id.
+     * @param isWifiCallingEnabled whether Wi-Fi calling is enabled by the user or not when the
+     * device is not roaming.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setWifiCallingEnabled(int subId, int isWifiCallingEnabled) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_WFC_IMS_ENABLED, isWifiCallingEnabled,
+                SubscriptionInfoInternal.Builder::setWifiCallingEnabled);
+    }
+
+    /**
+     * Set Wi-Fi calling mode when the device is not roaming.
+     *
+     * @param subId Subscription id.
+     * @param wifiCallingMode Wi-Fi calling mode when the device is not roaming.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setWifiCallingMode(int subId,
+            @ImsMmTelManager.WiFiCallingMode int wifiCallingMode) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_WFC_IMS_MODE, wifiCallingMode,
+                SubscriptionInfoInternal.Builder::setWifiCallingMode);
+    }
+
+    /**
+     * Set Wi-Fi calling mode when the device is roaming.
+     *
+     * @param subId Subscription id.
+     * @param wifiCallingModeForRoaming Wi-Fi calling mode when the device is roaming.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setWifiCallingModeForRoaming(int subId,
+            @ImsMmTelManager.WiFiCallingMode int wifiCallingModeForRoaming) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_WFC_IMS_ROAMING_MODE,
+                wifiCallingModeForRoaming,
+                SubscriptionInfoInternal.Builder::setWifiCallingModeForRoaming);
+    }
+
+    /**
+     * Set whether Wi-Fi calling is enabled by the user or not when the device is roaming.
+     *
+     * @param subId Subscription id.
+     * @param isWifiCallingEnabledForRoaming whether Wi-Fi calling is enabled by the user or not
+     * when the device is roaming.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setWifiCallingEnabledForRoaming(int subId, int isWifiCallingEnabledForRoaming) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_WFC_IMS_ROAMING_ENABLED,
+                isWifiCallingEnabledForRoaming,
+                SubscriptionInfoInternal.Builder::setWifiCallingEnabledForRoaming);
+    }
+
+    /**
+     * Set whether the subscription is opportunistic or not.
+     *
+     * @param subId Subscription id.
+     * @param isOpportunistic if the subscription is opportunistic.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setOpportunistic(int subId, boolean isOpportunistic) {
+        setOpportunistic(subId, isOpportunistic ? 1 : 0);
+    }
+
+    /**
+     * Set whether the subscription is opportunistic or not.
+     *
+     * @param subId Subscription id.
+     * @param isOpportunistic if the subscription is opportunistic.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setOpportunistic(int subId, int isOpportunistic) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_IS_OPPORTUNISTIC, isOpportunistic,
+                SubscriptionInfoInternal.Builder::setOpportunistic);
+    }
+
+    /**
+     * Set the group UUID of the subscription group.
+     *
+     * @param subId Subscription id.
+     * @param groupUuid The group UUID.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     *
+     * @see SubscriptionInfo#getGroupUuid()
+     */
+    public void setGroupUuid(int subId, @NonNull String groupUuid) {
+        Objects.requireNonNull(groupUuid);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_GROUP_UUID, groupUuid,
+                SubscriptionInfoInternal.Builder::setGroupUuid);
+    }
+
+    /**
+     * Set the ISO Country code for the subscription's provider.
+     *
+     * @param subId Subscription id.
+     * @param countryIso The ISO country code for the subscription's provider.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setCountryIso(int subId, @NonNull String countryIso) {
+        Objects.requireNonNull(countryIso);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_ISO_COUNTRY_CODE, countryIso,
+                SubscriptionInfoInternal.Builder::setCountryIso);
+    }
+
+    /**
+     * Set the subscription carrier id.
+     *
+     * @param subId Subscription id.
+     * @param carrierId The carrier id.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     *
+     * @see TelephonyManager#getSimCarrierId()
+     */
+    public void setCarrierId(int subId, int carrierId) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_CARRIER_ID, carrierId,
+                SubscriptionInfoInternal.Builder::setCarrierId);
+    }
+
+    /**
+     * Set the profile class populated from the profile metadata if present.
+     *
+     * @param subId Subscription id.
+     * @param profileClass the profile class populated from the profile metadata if present.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     *
+     * @see SubscriptionInfo#getProfileClass()
+     */
+    public void setProfileClass(int subId, @ProfileClass int profileClass) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_PROFILE_CLASS, profileClass,
+                SubscriptionInfoInternal.Builder::setProfileClass);
+    }
+
+    /**
+     * Set the subscription type.
+     *
+     * @param subId Subscription id.
+     * @param type Subscription type.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setSubscriptionType(int subId, @SubscriptionType int type) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_SUBSCRIPTION_TYPE, type,
+                SubscriptionInfoInternal.Builder::setType);
+    }
+
+    /**
+     * Set the owner package of group the subscription belongs to.
+     *
+     * @param subId Subscription id.
+     * @param groupOwner Owner package of group the subscription belongs to.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setGroupOwner(int subId, @NonNull String groupOwner) {
+        Objects.requireNonNull(groupOwner);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_GROUP_OWNER, groupOwner,
+                SubscriptionInfoInternal.Builder::setGroupOwner);
+    }
+
+    /**
+     * Set the enabled mobile data policies.
+     *
+     * @param subId Subscription id.
+     * @param enabledMobileDataPolicies The enabled mobile data policies.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setEnabledMobileDataPolicies(int subId, @NonNull String enabledMobileDataPolicies) {
+        Objects.requireNonNull(enabledMobileDataPolicies);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES,
+                enabledMobileDataPolicies,
+                SubscriptionInfoInternal.Builder::setEnabledMobileDataPolicies);
+    }
+
+    /**
+     * Set the IMSI (International Mobile Subscriber Identity) of the subscription.
+     *
+     * @param subId Subscription id.
+     * @param imsi The IMSI.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setImsi(int subId, @NonNull String imsi) {
+        Objects.requireNonNull(imsi);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_IMSI, imsi,
+                SubscriptionInfoInternal.Builder::setImsi);
+    }
+
+    /**
+     * Set whether Uicc applications are configured to enable or not.
+     *
+     * @param subId Subscription id.
+     * @param areUiccApplicationsEnabled if Uicc applications are configured to enable.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setUiccApplicationsEnabled(int subId, boolean areUiccApplicationsEnabled) {
+        setUiccApplicationsEnabled(subId, areUiccApplicationsEnabled ? 1 : 0);
+    }
+
+    /**
+     * Set whether Uicc applications are configured to enable or not.
+     *
+     * @param subId Subscription id.
+     * @param areUiccApplicationsEnabled if Uicc applications are configured to enable.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setUiccApplicationsEnabled(int subId, int areUiccApplicationsEnabled) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_UICC_APPLICATIONS_ENABLED,
+                areUiccApplicationsEnabled,
+                SubscriptionInfoInternal.Builder::setUiccApplicationsEnabled);
+    }
+
+    /**
+     * Set whether the user has enabled IMS RCS User Capability Exchange (UCE) for this
+     * subscription.
+     *
+     * @param subId Subscription id.
+     * @param isRcsUceEnabled If the user enabled RCS UCE for this subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setRcsUceEnabled(int subId, int isRcsUceEnabled) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_IMS_RCS_UCE_ENABLED, isRcsUceEnabled,
+                SubscriptionInfoInternal.Builder::setRcsUceEnabled);
+    }
+
+    /**
+     * Set whether the user has enabled cross SIM calling for this subscription.
+     *
+     * @param subId Subscription id.
+     * @param isCrossSimCallingEnabled If the user enabled cross SIM calling for this
+     * subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setCrossSimCallingEnabled(int subId, int isCrossSimCallingEnabled) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED,
+                isCrossSimCallingEnabled,
+                SubscriptionInfoInternal.Builder::setCrossSimCallingEnabled);
+    }
+
+    /**
+     * Set the RCS config for this subscription.
+     *
+     * @param subId Subscription id.
+     * @param rcsConfig The RCS config for this subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setRcsConfig(int subId, @NonNull byte[] rcsConfig) {
+        Objects.requireNonNull(rcsConfig);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_RCS_CONFIG, rcsConfig,
+                SubscriptionInfoInternal.Builder::setRcsConfig);
+    }
+
+    /**
+     * Set the allowed network types for reasons.
+     *
+     * @param subId Subscription id.
+     * @param allowedNetworkTypesForReasons The allowed network types for reasons in string
+     * format. The format is "[reason]=[network types bitmask], [reason]=[network types bitmask],
+     * ...". For example, "user=1239287394, thermal=298791239, carrier=3456812312".
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setAllowedNetworkTypesForReasons(int subId,
+            @NonNull String allowedNetworkTypesForReasons) {
+        Objects.requireNonNull(allowedNetworkTypesForReasons);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_ALLOWED_NETWORK_TYPES_FOR_REASONS,
+                allowedNetworkTypesForReasons,
+                SubscriptionInfoInternal.Builder::setAllowedNetworkTypesForReasons);
+    }
+
+    /**
+     * Set device to device sharing status.
+     *
+     * @param subId Subscription id.
+     * @param deviceToDeviceStatusSharingPreference Device to device sharing status.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setDeviceToDeviceStatusSharingPreference(int subId,
+            @DeviceToDeviceStatusSharingPreference int deviceToDeviceStatusSharingPreference) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_D2D_STATUS_SHARING,
+                deviceToDeviceStatusSharingPreference,
+                SubscriptionInfoInternal.Builder::setDeviceToDeviceStatusSharingPreference);
+    }
+
+    /**
+     * Set whether the user has opted-in voice over IMS.
+     *
+     * @param subId Subscription id.
+     * @param isVoImsOptInEnabled Whether the user has opted-in voice over IMS.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setVoImsOptInEnabled(int subId, int isVoImsOptInEnabled) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_VOIMS_OPT_IN_STATUS, isVoImsOptInEnabled,
+                SubscriptionInfoInternal.Builder::setVoImsOptInEnabled);
+    }
+
+    /**
+     * Set contacts information that allow device to device sharing.
+     *
+     * @param subId Subscription id.
+     * @param deviceToDeviceStatusSharingContacts contacts information that allow device to
+     * device sharing.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setDeviceToDeviceStatusSharingContacts(int subId,
+            @NonNull String deviceToDeviceStatusSharingContacts) {
+        Objects.requireNonNull(deviceToDeviceStatusSharingContacts);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS,
+                deviceToDeviceStatusSharingContacts,
+                SubscriptionInfoInternal.Builder::setDeviceToDeviceStatusSharingContacts);
+    }
+
+    /**
+     * Set whether the user has enabled NR advanced calling.
+     *
+     * @param subId Subscription id.
+     * @param isNrAdvancedCallingEnabled Whether the user has enabled NR advanced calling.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setNrAdvancedCallingEnabled(int subId, int isNrAdvancedCallingEnabled) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED,
+                isNrAdvancedCallingEnabled,
+                SubscriptionInfoInternal.Builder::setNrAdvancedCallingEnabled);
+    }
+
+    /**
+     * Set the phone number retrieved from carrier.
+     *
+     * @param subId Subscription id.
+     * @param numberFromCarrier The phone number retrieved from carrier.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setNumberFromCarrier(int subId, @NonNull String numberFromCarrier) {
+        Objects.requireNonNull(numberFromCarrier);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER,
+                numberFromCarrier, SubscriptionInfoInternal.Builder::setNumberFromCarrier);
+    }
+
+    /**
+     * Set the phone number retrieved from IMS.
+     *
+     * @param subId Subscription id.
+     * @param numberFromIms The phone number retrieved from IMS.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setNumberFromIms(int subId, @NonNull String numberFromIms) {
+        Objects.requireNonNull(numberFromIms);
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS,
+                numberFromIms, SubscriptionInfoInternal.Builder::setNumberFromIms);
+    }
+
+    /**
+     * Set the port index of the Uicc card.
+     *
+     * @param subId Subscription id.
+     * @param portIndex The port index of the Uicc card.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setPortIndex(int subId, int portIndex) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_PORT_INDEX, portIndex,
+                SubscriptionInfoInternal.Builder::setPortIndex);
+    }
+
+    /**
+     * Set subscription's preferred usage setting.
+     *
+     * @param subId Subscription id.
+     * @param usageSetting Subscription's preferred usage setting.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setUsageSetting(int subId, @UsageSetting int usageSetting) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_USAGE_SETTING, usageSetting,
+                SubscriptionInfoInternal.Builder::setUsageSetting);
+    }
+
+    /**
+     * Set last used TP message reference.
+     *
+     * @param subId Subscription id.
+     * @param lastUsedTPMessageReference Last used TP message reference.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setLastUsedTPMessageReference(int subId, int lastUsedTPMessageReference) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_TP_MESSAGE_REF,
+                lastUsedTPMessageReference,
+                SubscriptionInfoInternal.Builder::setLastUsedTPMessageReference);
+    }
+
+    /**
+     * Set the user id associated with this subscription.
+     *
+     * @param subId Subscription id.
+     * @param userId The user id associated with this subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void setUserId(int subId, @UserIdInt int userId) {
+        writeDatabaseAndCacheHelper(subId, SimInfo.COLUMN_USER_HANDLE, userId,
+                SubscriptionInfoInternal.Builder::setUserId);
+    }
+
+    /**
+     * Load the entire database into the cache.
+     */
+    private void loadFromDatabase() {
+        // Perform the task in the handler thread.
+        Runnable r = () -> {
+            try (Cursor cursor = mContext.getContentResolver().query(
+                    SimInfo.CONTENT_URI, null, null, null, null)) {
+                mReadWriteLock.writeLock().lock();
+                try {
+                    mAllSubscriptionInfoInternalCache.clear();
+                    while (cursor != null && cursor.moveToNext()) {
+                        SubscriptionInfoInternal subInfo = createSubscriptionInfoFromCursor(cursor);
+                        if (subInfo != null) {
+                            mAllSubscriptionInfoInternalCache
+                                    .put(subInfo.getSubscriptionId(), subInfo);
+                        }
+                    }
+                    mDatabaseLoaded = true;
+                    mCallback.invokeFromExecutor(mCallback::onDatabaseLoaded);
+                    log("Loaded " + mAllSubscriptionInfoInternalCache.size()
+                            + " records from the subscription database.");
+                } finally {
+                    mReadWriteLock.writeLock().unlock();
+                }
+            }
+        };
+
+        if (mAsyncMode) {
+            // Load the database asynchronously.
+            post(r);
+        } else {
+            // Load the database synchronously.
+            r.run();
+        }
+    }
+
+    /**
+     * Build the {@link SubscriptionInfoInternal} from database.
+     *
+     * @param cursor  The cursor of the database.
+     *
+     * @return The subscription info from a single database record.
+     */
+    @Nullable
+    private SubscriptionInfoInternal createSubscriptionInfoFromCursor(@NonNull Cursor cursor) {
+        SubscriptionInfoInternal.Builder builder = new SubscriptionInfoInternal.Builder();
+        int id = cursor.getInt(cursor.getColumnIndexOrThrow(
+                SimInfo.COLUMN_UNIQUE_KEY_SUBSCRIPTION_ID));
+        builder.setId(id)
+                .setIccId(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_ICC_ID))))
+                .setSimSlotIndex(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_SIM_SLOT_INDEX)))
+                .setDisplayName(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_DISPLAY_NAME))))
+                .setCarrierName(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_CARRIER_NAME))))
+                .setDisplayNameSource(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_NAME_SOURCE)))
+                .setIconTint(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_COLOR)))
+                .setNumber(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_NUMBER))))
+                .setDataRoaming(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_DATA_ROAMING)))
+                .setMcc(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_MCC_STRING))))
+                .setMnc(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_MNC_STRING))))
+                .setEhplmns(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_EHPLMNS))))
+                .setHplmns(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_HPLMNS))))
+                .setEmbedded(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_IS_EMBEDDED)));
+
+        String cardString = TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                SimInfo.COLUMN_CARD_ID)));
+        builder.setCardString(cardString);
+        // publicCardId is the publicly exposed int card ID
+        int publicCardId = mUiccController.convertToPublicCardId(cardString);
+
+        byte[] rules = cursor.getBlob(cursor.getColumnIndexOrThrow(SimInfo.COLUMN_ACCESS_RULES));
+        if (rules != null) {
+            builder.setNativeAccessRules(rules);
+        }
+
+        rules = cursor.getBlob(cursor.getColumnIndexOrThrow(
+                SimInfo.COLUMN_ACCESS_RULES_FROM_CARRIER_CONFIGS));
+        if (rules != null) {
+            builder.setCarrierConfigAccessRules(rules);
+        }
+
+        byte[] config = cursor.getBlob(cursor.getColumnIndexOrThrow(SimInfo.COLUMN_RCS_CONFIG));
+        if (config != null) {
+            builder.setRcsConfig(config);
+        }
+
+        builder.setCardId(publicCardId)
+                .setRemovableEmbedded(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_IS_REMOVABLE)))
+                .setEnhanced4GModeEnabled(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED)))
+                .setVideoTelephonyEnabled(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_VT_IMS_ENABLED)))
+                .setWifiCallingEnabled(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_WFC_IMS_ENABLED)))
+                .setWifiCallingMode(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_WFC_IMS_MODE)))
+                .setWifiCallingModeForRoaming(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_WFC_IMS_ROAMING_MODE)))
+                .setWifiCallingEnabledForRoaming(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_WFC_IMS_ROAMING_ENABLED)))
+                .setOpportunistic(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_IS_OPPORTUNISTIC)))
+                .setGroupUuid(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_GROUP_UUID))))
+                .setCountryIso(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_ISO_COUNTRY_CODE))))
+                .setCarrierId(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_CARRIER_ID)))
+                .setProfileClass(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_PROFILE_CLASS)))
+                .setType(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_SUBSCRIPTION_TYPE)))
+                .setGroupOwner(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_GROUP_OWNER))))
+                .setEnabledMobileDataPolicies(TextUtils.emptyIfNull(
+                        cursor.getString(cursor.getColumnIndexOrThrow(
+                                SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES))))
+                .setImsi(TextUtils.emptyIfNull(cursor.getString(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_IMSI))))
+                .setUiccApplicationsEnabled(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_UICC_APPLICATIONS_ENABLED)))
+                .setRcsUceEnabled(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_IMS_RCS_UCE_ENABLED)))
+                .setCrossSimCallingEnabled(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED)))
+                .setAllowedNetworkTypesForReasons(TextUtils.emptyIfNull(
+                        cursor.getString(cursor.getColumnIndexOrThrow(
+                                SimInfo.COLUMN_ALLOWED_NETWORK_TYPES_FOR_REASONS))))
+                .setDeviceToDeviceStatusSharingPreference(cursor.getInt(
+                        cursor.getColumnIndexOrThrow(
+                                SimInfo.COLUMN_D2D_STATUS_SHARING)))
+                .setVoImsOptInEnabled(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_VOIMS_OPT_IN_STATUS)))
+                .setDeviceToDeviceStatusSharingContacts(TextUtils.emptyIfNull(cursor.getString(
+                        cursor.getColumnIndexOrThrow(
+                                SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS))))
+                .setNrAdvancedCallingEnabled(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED)))
+                .setNumberFromCarrier(TextUtils.emptyIfNull(cursor.getString(
+                        cursor.getColumnIndexOrThrow(
+                                SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER))))
+                .setNumberFromIms(TextUtils.emptyIfNull(cursor.getString(
+                        cursor.getColumnIndexOrThrow(
+                                SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS))))
+                .setPortIndex(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_PORT_INDEX)))
+                .setUsageSetting(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_USAGE_SETTING)))
+                .setLastUsedTPMessageReference(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_TP_MESSAGE_REF)))
+                .setUserId(cursor.getInt(cursor.getColumnIndexOrThrow(
+                        SimInfo.COLUMN_USER_HANDLE)));
+
+        return builder.build();
+    }
+
+    /**
+     * Sync the group sharing fields from reference subscription to the rest of the subscriptions in
+     * the same group. For example, if user enables wifi calling, the same setting should be applied
+     * to all subscriptions in the same group.
+     *
+     * @param subId The subscription id of reference subscription.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     */
+    public void syncToGroup(int subId) {
+        if (!mAllSubscriptionInfoInternalCache.containsKey(subId)) {
+            throw new IllegalArgumentException("Invalid subId " + subId);
+        }
+
+        for (String column : GROUP_SHARING_COLUMNS) {
+            // Get the value from the reference subscription, and set to itself again.
+            // writeDatabaseAndCacheHelper() will automatically sync to the rest of the group.
+            setSubscriptionProperty(subId, column, getSubscriptionProperty(subId, column));
+        }
+    }
+
+    /**
+     * Get the subscription info by subscription id.
+     *
+     * @param subId The subscription id.
+     *
+     * @return The subscription info. {@code null} if not found.
+     */
+    @Nullable
+    public SubscriptionInfoInternal getSubscriptionInfoInternal(int subId) {
+        mReadWriteLock.readLock().lock();
+        try {
+            return mAllSubscriptionInfoInternalCache.get(subId);
+        } finally {
+            mReadWriteLock.readLock().unlock();
+        }
+    }
+
+    /**
+     * @return All subscription infos in the database.
+     */
+    @NonNull
+    public List<SubscriptionInfoInternal> getAllSubscriptions() {
+        mReadWriteLock.readLock().lock();
+        try {
+            return new ArrayList<>(mAllSubscriptionInfoInternalCache.values());
+        } finally {
+            mReadWriteLock.readLock().unlock();
+        }
+    }
+
+    /**
+     * Get subscription info by ICCID.
+     *
+     * @param iccId The ICCID of the SIM card.
+     * @return The subscription info if found. {@code null} if not found.
+     */
+    @Nullable
+    public SubscriptionInfoInternal getSubscriptionInfoInternalByIccId(@NonNull String iccId) {
+        mReadWriteLock.readLock().lock();
+        try {
+            return mAllSubscriptionInfoInternalCache.values().stream()
+                    .filter(subInfo -> subInfo.getIccId().equals(iccId))
+                    .findFirst()
+                    .orElse(null);
+        } finally {
+            mReadWriteLock.readLock().unlock();
+        }
+    }
+
+    /**
+     * @return {@code true} if the database has been loaded into the cache.
+     */
+    public boolean isDatabaseLoaded() {
+        return mDatabaseLoaded;
+    }
+    /**
+     * Log debug messages.
+     *
+     * @param s debug messages
+     */
+    private void log(@NonNull String s) {
+        Rlog.d(LOG_TAG, s);
+    }
+
+    /**
+     * Log error messages.
+     *
+     * @param s error messages
+     */
+    private void loge(@NonNull String s) {
+        Rlog.e(LOG_TAG, s);
+    }
+
+    /**
+     * Log verbose messages.
+     *
+     * @param s debug messages.
+     */
+    private void logv(@NonNull String s) {
+        if (VDBG) Rlog.v(LOG_TAG, s);
+    }
+
+    /**
+     * Log error messages and also log into the local log.
+     *
+     * @param s debug messages
+     */
+    private void logel(@NonNull String s) {
+        loge(s);
+        mLocalLog.log(s);
+    }
+
+    /**
+     * Log debug messages and also log into the local log.
+     *
+     * @param s debug messages
+     */
+    private void logl(@NonNull String s) {
+        log(s);
+        mLocalLog.log(s);
+    }
+
+    /**
+     * Dump the state of {@link SubscriptionManagerService}.
+     *
+     * @param fd File descriptor
+     * @param printWriter Print writer
+     * @param args Arguments
+     */
+    public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter printWriter,
+            @NonNull String[] args) {
+        IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, "  ");
+        pw.println(SubscriptionManagerService.class.getSimpleName() + ":");
+        pw.increaseIndent();
+        pw.println("All subscriptions:");
+        pw.increaseIndent();
+        mAllSubscriptionInfoInternalCache.forEach((subId, subInfo) -> pw.println(subInfo));
+        pw.decreaseIndent();
+        pw.decreaseIndent();
+    }
+}
diff --git a/src/java/com/android/internal/telephony/subscription/SubscriptionInfoInternal.java b/src/java/com/android/internal/telephony/subscription/SubscriptionInfoInternal.java
new file mode 100644
index 0000000..b0262b0
--- /dev/null
+++ b/src/java/com/android/internal/telephony/subscription/SubscriptionInfoInternal.java
@@ -0,0 +1,2271 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.subscription;
+
+import android.annotation.ColorInt;
+import android.annotation.NonNull;
+import android.annotation.UserIdInt;
+import android.os.UserHandle;
+import android.provider.Telephony.SimInfo;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.SubscriptionManager.DeviceToDeviceStatusSharingPreference;
+import android.telephony.SubscriptionManager.ProfileClass;
+import android.telephony.SubscriptionManager.SimDisplayNameSource;
+import android.telephony.SubscriptionManager.SubscriptionType;
+import android.telephony.SubscriptionManager.UsageSetting;
+import android.telephony.TelephonyManager;
+import android.telephony.UiccAccessRule;
+import android.telephony.ims.ImsMmTelManager;
+import android.text.TextUtils;
+
+import com.android.internal.telephony.uicc.IccUtils;
+import com.android.internal.telephony.util.TelephonyUtils;
+import com.android.telephony.Rlog;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * The class represents a single row of {@link SimInfo} table. All columns (excepts unused columns)
+ * in the database have a corresponding field in this class.
+ *
+ * The difference between {@link SubscriptionInfo} and this class is that {@link SubscriptionInfo}
+ * is a subset of this class. This is intended to solve the problem that some database fields
+ * required higher permission like
+ * {@link android.Manifest.permission#READ_PRIVILEGED_PHONE_STATE} to access while
+ * {@link SubscriptionManager#getActiveSubscriptionIdList()} only requires
+ * {@link android.Manifest.permission#READ_PHONE_STATE} to access. Sometimes blanking out fields in
+ * {@link SubscriptionInfo} creates ambiguity for clients hard to distinguish between insufficient
+ * permission versus true failure.
+ *
+ * Also the fields in this class match the format used in database. For example, boolean values
+ * are stored as integer, or string arrays are stored as a single comma separated string.
+ */
+public class SubscriptionInfoInternal {
+    /**
+     * Subscription Identifier, this is a device unique number
+     * and not an index into an array
+     */
+    private final int mId;
+
+    /**
+     * The ICCID of the SIM that is associated with this subscription, empty if unknown.
+     */
+    @NonNull
+    private final String mIccId;
+
+    /**
+     * The index of the SIM slot that currently contains the subscription and not necessarily unique
+     * and maybe {@link SubscriptionManager#INVALID_SIM_SLOT_INDEX} if unknown or the subscription
+     * is inactive.
+     */
+    private final int mSimSlotIndex;
+
+    /**
+     * The name displayed to the user that identifies this subscription. This name is used
+     * in Settings page and can be renamed by the user.
+     */
+    @NonNull
+    private final String mDisplayName;
+
+    /**
+     * The name displayed to the user that identifies subscription provider name. This name is the
+     * SPN displayed in status bar and many other places. Can't be renamed by the user.
+     */
+    @NonNull
+    private final String mCarrierName;
+
+    /**
+     * The source of the {@link #mDisplayName}.
+     */
+    @SimDisplayNameSource
+    private final int mDisplayNameSource;
+
+    /**
+     * The color to be used for tinting the icon when displaying to the user.
+     */
+    @ColorInt
+    private final int mIconTint;
+
+    /**
+     * The number presented to the user identify this subscription.
+     */
+    @NonNull
+    private final String mNumber;
+
+    /**
+     * Whether user enables data roaming for this subscription or not. Either
+     * {@link SubscriptionManager#DATA_ROAMING_ENABLE} or
+     * {@link SubscriptionManager#DATA_ROAMING_DISABLE}.
+     */
+    private final int mDataRoaming;
+
+    /**
+     * Mobile Country Code.
+     */
+    @NonNull
+    private final String mMcc;
+
+    /**
+     * Mobile Network Code.
+     */
+    @NonNull
+    private final String mMnc;
+
+    /**
+     * EHPLMNs associated with the subscription.
+     */
+    @NonNull
+    private final String mEhplmns;
+
+    /**
+     * HPLMNs associated with the subscription.
+     */
+    @NonNull
+    private final String mHplmns;
+
+    /**
+     * Whether the subscription is from eSIM. It is intended to use integer to fit the database
+     * format.
+     */
+    private final int mIsEmbedded;
+
+    /**
+     * The string ID of the SIM card. It is the ICCID of the active profile for a UICC card and the
+     * EID for an eUICC card.
+     */
+    @NonNull
+    private final String mCardString;
+
+    /**
+     * The access rules for this subscription, if it is embedded and defines any. This does not
+     * include access rules for non-embedded subscriptions.
+     */
+    @NonNull
+    private final byte[] mNativeAccessRules;
+
+    /**
+     * The carrier certificates for this subscription that are saved in carrier configs.
+     * This does not include access rules from the Uicc, whether embedded or non-embedded.
+     */
+    @NonNull
+    private final byte[] mCarrierConfigAccessRules;
+
+    /**
+     * Whether an embedded subscription is on a removable card. Such subscriptions are marked
+     * inaccessible as soon as the current card is removed. Otherwise, they will remain accessible
+     * unless explicitly deleted. Only meaningful when {@link #getEmbedded()} is {@code 1}. It
+     * is intended to use integer to fit the database format.
+     */
+    private final int mIsRemovableEmbedded;
+
+    /**
+     * Whether enhanced 4G mode is enabled by the user or not. It is intended to use integer to fit
+     * the database format.
+     */
+    private final int mIsEnhanced4GModeEnabled;
+
+    /**
+     * Whether video telephony is enabled by the user or not. It is intended to use integer to fit
+     * the database format.
+     */
+    private final int mIsVideoTelephonyEnabled;
+
+    /**
+     * Whether Wi-Fi calling is enabled by the user or not when the device is not roaming. It is
+     * intended to use integer to fit the database format.
+     */
+    private final int mIsWifiCallingEnabled;
+
+    /**
+     * Wi-Fi calling mode when the device is not roaming.
+     */
+    @ImsMmTelManager.WiFiCallingMode
+    private final int mWifiCallingMode;
+
+    /**
+     * Wi-Fi calling mode when the device is roaming.
+     */
+    @ImsMmTelManager.WiFiCallingMode
+    private final int mWifiCallingModeForRoaming;
+
+    /**
+     * Whether Wi-Fi calling is enabled by the user or not when the device is roaming. It is
+     * intended to use integer to fit the database format.
+     */
+    private final int mIsWifiCallingEnabledForRoaming;
+
+    /**
+     * Whether the subscription is opportunistic. It is intended to use integer to fit the database
+     * format.
+     */
+    private final int mIsOpportunistic;
+
+    /**
+     * A UUID assigned to the subscription group in string format.
+     *
+     * @see SubscriptionManager#createSubscriptionGroup(List)
+     */
+    @NonNull
+    private final String mGroupUuid;
+
+    /**
+     * ISO Country code for the subscription's provider.
+     */
+    @NonNull
+    private final String mCountryIso;
+
+    /**
+     * The subscription carrier id.
+     *
+     * @see TelephonyManager#getSimCarrierId()
+     */
+    private final int mCarrierId;
+
+    /**
+     * The profile class populated from the profile metadata if present. Otherwise,
+     * the profile class defaults to {@link SubscriptionManager#PROFILE_CLASS_UNSET} if there is no
+     * profile metadata or the subscription is not on an eUICC ({@link #getEmbedded} returns
+     * {@code 0}).
+     */
+    @ProfileClass
+    private final int mProfileClass;
+
+    /**
+     * Type of the subscription.
+     */
+    @SubscriptionType
+    private final int mType;
+
+    /**
+     * A package name that specifies who created the group. Empty if not available.
+     */
+    @NonNull
+    private final String mGroupOwner;
+
+    /**
+     * The enabled mobile data policies in string format.
+     */
+    @NonNull
+    private final String mEnabledMobileDataPolicies;
+
+    /**
+     * The IMSI (International Mobile Subscriber Identity) of the subscription.
+     */
+    @NonNull
+    private final String mImsi;
+
+    /**
+     * Whether uicc applications are configured to enable or disable.
+     * By default it's true. It is intended to use integer to fit the database format.
+     */
+    private final int mAreUiccApplicationsEnabled;
+
+    /**
+     * Whether the user has enabled IMS RCS User Capability Exchange (UCE) for this subscription.
+     * It is intended to use integer to fit the database format.
+     */
+    private final int mIsRcsUceEnabled;
+
+    /**
+     * Whether the user has enabled cross SIM calling for this subscription. It is intended to
+     * use integer to fit the database format.
+     */
+    private final int mIsCrossSimCallingEnabled;
+
+    /**
+     * The RCS configuration.
+     */
+    @NonNull
+    private final byte[] mRcsConfig;
+
+    /**
+     * The allowed network types for reasons in string format. The format is
+     * "[reason]=[network types bitmask], [reason]=[network types bitmask], ..."
+     *
+     * For example, "user=1239287394, thermal=298791239, carrier=3456812312".
+     */
+    @NonNull
+    private final String mAllowedNetworkTypesForReasons;
+
+    /**
+     * Device to device sharing status.
+     */
+    @DeviceToDeviceStatusSharingPreference
+    private final int mDeviceToDeviceStatusSharingPreference;
+
+    /**
+     * Whether the user has opted-in voice over IMS. It is intended to use integer to fit the
+     * database format.
+     */
+    private final int mIsVoImsOptInEnabled;
+
+    /**
+     * Contacts information that allow device to device sharing.
+     */
+    @NonNull
+    private final String mDeviceToDeviceStatusSharingContacts;
+
+    /**
+     * Whether the user has enabled NR advanced calling. It is intended to use integer to fit the
+     * database format.
+     */
+    private final int mIsNrAdvancedCallingEnabled;
+
+    /**
+     * The phone number retrieved from carrier.
+     */
+    @NonNull
+    private final String mNumberFromCarrier;
+
+    /**
+     * The phone number retrieved from IMS.
+     */
+    @NonNull
+    private final String mNumberFromIms;
+
+    /**
+     * The port index of the Uicc card.
+     */
+    private final int mPortIndex;
+
+    /**
+     * Subscription's preferred usage setting.
+     */
+    @UsageSetting
+    private final int mUsageSetting;
+
+    /**
+     * Last used TP message reference.
+     */
+    private final int mLastUsedTPMessageReference;
+
+    /**
+     * The user id associated with this subscription.
+     */
+    private final int mUserId;
+
+    // Below are the fields that do not exist in the SimInfo table.
+    /**
+     * The card ID of the SIM card. This maps uniquely to {@link #mCardString}.
+     */
+    private final int mCardId;
+
+    /**
+     * Whether group of the subscription is disabled. This is only useful if it's a grouped
+     * opportunistic subscription. In this case, if all primary (non-opportunistic) subscriptions
+     * in the group are deactivated (unplugged pSIM or deactivated eSIM profile), we should disable
+     * this opportunistic subscription. It is intended to use integer to fit the database format.
+     */
+    private final boolean mIsGroupDisabled;
+
+    /**
+     * Constructor from builder.
+     *
+     * @param builder Builder of {@link SubscriptionInfoInternal}.
+     */
+    private SubscriptionInfoInternal(@NonNull Builder builder) {
+        this.mId = builder.mId;
+        this.mIccId = builder.mIccId;
+        this.mSimSlotIndex = builder.mSimSlotIndex;
+        this.mDisplayName = builder.mDisplayName;
+        this.mCarrierName = builder.mCarrierName;
+        this.mDisplayNameSource = builder.mDisplayNameSource;
+        this.mIconTint = builder.mIconTint;
+        this.mNumber = builder.mNumber;
+        this.mDataRoaming = builder.mDataRoaming;
+        this.mMcc = builder.mMcc;
+        this.mMnc = builder.mMnc;
+        this.mEhplmns = builder.mEhplmns;
+        this.mHplmns = builder.mHplmns;
+        this.mIsEmbedded = builder.mIsEmbedded;
+        this.mCardString = builder.mCardString;
+        this.mNativeAccessRules = builder.mNativeAccessRules;
+        this.mCarrierConfigAccessRules = builder.mCarrierConfigAccessRules;
+        this.mIsRemovableEmbedded = builder.mIsRemovableEmbedded;
+        this.mIsEnhanced4GModeEnabled = builder.mIsEnhanced4GModeEnabled;
+        this.mIsVideoTelephonyEnabled = builder.mIsVideoTelephonyEnabled;
+        this.mIsWifiCallingEnabled = builder.mIsWifiCallingEnabled;
+        this.mWifiCallingMode = builder.mWifiCallingMode;
+        this.mWifiCallingModeForRoaming = builder.mWifiCallingModeForRoaming;
+        this.mIsWifiCallingEnabledForRoaming = builder.mIsWifiCallingEnabledForRoaming;
+        this.mIsOpportunistic = builder.mIsOpportunistic;
+        this.mGroupUuid = builder.mGroupUuid;
+        this.mCountryIso = builder.mCountryIso;
+        this.mCarrierId = builder.mCarrierId;
+        this.mProfileClass = builder.mProfileClass;
+        this.mType = builder.mType;
+        this.mGroupOwner = builder.mGroupOwner;
+        this.mEnabledMobileDataPolicies = builder.mEnabledMobileDataPolicies;
+        this.mImsi = builder.mImsi;
+        this.mAreUiccApplicationsEnabled = builder.mAreUiccApplicationsEnabled;
+        this.mIsRcsUceEnabled = builder.mIsRcsUceEnabled;
+        this.mIsCrossSimCallingEnabled = builder.mIsCrossSimCallingEnabled;
+        this.mRcsConfig = builder.mRcsConfig;
+        this.mAllowedNetworkTypesForReasons = builder.mAllowedNetworkTypesForReasons;
+        this.mDeviceToDeviceStatusSharingPreference =
+                builder.mDeviceToDeviceStatusSharingPreference;
+        this.mIsVoImsOptInEnabled = builder.mIsVoImsOptInEnabled;
+        this.mDeviceToDeviceStatusSharingContacts = builder.mDeviceToDeviceStatusSharingContacts;
+        this.mIsNrAdvancedCallingEnabled = builder.mIsNrAdvancedCallingEnabled;
+        this.mNumberFromCarrier = builder.mNumberFromCarrier;
+        this.mNumberFromIms = builder.mNumberFromIms;
+        this.mPortIndex = builder.mPortIndex;
+        this.mUsageSetting = builder.mUsageSetting;
+        this.mLastUsedTPMessageReference = builder.mLastUsedTPMessageReference;
+        this.mUserId = builder.mUserId;
+
+        // Below are the fields that do not exist in the SimInfo table.
+        this.mCardId = builder.mCardId;
+        this.mIsGroupDisabled = builder.mIsGroupDisabled;
+    }
+
+    /**
+     * @return The subscription ID.
+     */
+    public int getSubscriptionId() {
+        return mId;
+    }
+
+    /**
+     * Returns the ICC ID.
+     *
+     * @return the ICC ID, or an empty string if one of these requirements is not met
+     */
+    @NonNull
+    public String getIccId() {
+        return mIccId;
+    }
+
+    /**
+     * @return The index of the SIM slot that currently contains the subscription and not
+     * necessarily unique and maybe {@link SubscriptionManager#INVALID_SIM_SLOT_INDEX} if unknown or
+     * the subscription is inactive.
+     */
+    public int getSimSlotIndex() {
+        return mSimSlotIndex;
+    }
+
+    /**
+     * @return The name displayed to the user that identifies this subscription. This name is
+     * used in Settings page and can be renamed by the user.
+     *
+     * @see #getCarrierName()
+     */
+    @NonNull
+    public String getDisplayName() {
+        return mDisplayName;
+    }
+
+    /**
+     * @return The name displayed to the user that identifies subscription provider name. This name
+     * is the SPN displayed in status bar and many other places. Can't be renamed by the user.
+     *
+     * @see #getDisplayName()
+     */
+    @NonNull
+    public String getCarrierName() {
+        return mCarrierName;
+    }
+
+    /**
+     * @return The source of the {@link #getDisplayName()}.
+     */
+    @SimDisplayNameSource
+    public int getDisplayNameSource() {
+        return mDisplayNameSource;
+    }
+
+    /**
+     * A highlight color to use in displaying information about this {@code PhoneAccount}.
+     *
+     * @return A hexadecimal color value.
+     */
+    @ColorInt
+    public int getIconTint() {
+        return mIconTint;
+    }
+
+    /**
+     * @return the number of this subscription.
+     */
+    public String getNumber() {
+        return mNumber;
+    }
+
+    /**
+     * Whether user enables data roaming for this subscription or not. Either
+     * {@link SubscriptionManager#DATA_ROAMING_ENABLE} or
+     * {@link SubscriptionManager#DATA_ROAMING_DISABLE}.
+     */
+    public int getDataRoaming() {
+        return mDataRoaming;
+    }
+
+    /**
+     * @return The mobile country code.
+     */
+    @NonNull
+    public String getMcc() {
+        return mMcc;
+    }
+
+    /**
+     * @return The mobile network code.
+     */
+    @NonNull
+    public String getMnc() {
+        return mMnc;
+    }
+
+    /**
+     * @return Extended home PLMNs associated with this subscription.
+     */
+    @NonNull
+    public String getEhplmns() {
+        return mEhplmns;
+    }
+
+    /**
+     * @return Home PLMNs associated with this subscription.
+     */
+    @NonNull
+    public String getHplmns() {
+        return mHplmns;
+    }
+
+    /**
+     * @return {@code true} if the subscription is from eSIM.
+     */
+    public boolean isEmbedded() {
+        return mIsEmbedded != 0;
+    }
+
+    /**
+     * @return {@code 1} if the subscription is from eSIM.
+     */
+    public int getEmbedded() {
+        return mIsEmbedded;
+    }
+
+    /**
+     * Returns the card string of the SIM card which contains the subscription.
+     *
+     * @return The card string of the SIM card which contains the subscription. The card string is
+     * the ICCID for UICCs or the EID for
+     * eUICCs.
+     */
+    @NonNull
+    public String getCardString() {
+        return mCardString;
+    }
+
+    /**
+     * @return The access rules for this subscription, if it is embedded and defines any. This
+     * does not include access rules for non-embedded subscriptions. This is the raw string
+     * stored in the database.
+     */
+    @NonNull
+    public byte[] getNativeAccessRules() {
+        return mNativeAccessRules;
+    }
+
+    /**
+     * @return The carrier certificates for this subscription that are saved in carrier configs.
+     * This does not include access rules from the Uicc, whether embedded or non-embedded. This
+     * is the raw string stored in the database.
+     */
+    public byte[] getCarrierConfigAccessRules() {
+        return mCarrierConfigAccessRules;
+    }
+
+    /**
+     * @return {@code 1} if an embedded subscription is on a removable card. Such subscriptions are
+     * marked inaccessible as soon as the current card is removed. Otherwise, they will remain
+     * accessible unless explicitly deleted. Only meaningful when {@link #getEmbedded()} is 1.
+     */
+    public boolean isRemovableEmbedded() {
+        return mIsRemovableEmbedded != 0;
+    }
+
+    /**
+     * @return {@code 1} if an embedded subscription is on a removable card. Such subscriptions are
+     * marked inaccessible as soon as the current card is removed. Otherwise, they will remain
+     * accessible unless explicitly deleted. Only meaningful when {@link #getEmbedded()} is 1.
+     */
+    public int getRemovableEmbedded() {
+        return mIsRemovableEmbedded;
+    }
+
+    /**
+     * @return {@code true} if enhanced 4G mode is enabled by the user or not.
+     */
+    public boolean isEnhanced4GModeEnabled() {
+        return mIsEnhanced4GModeEnabled == 1;
+    }
+
+    /**
+     * @return {@code 1} if enhanced 4G mode is enabled by the user or not. {@code 0} if disabled.
+     * {@code -1} if the user did not change any setting.
+     */
+    public int getEnhanced4GModeEnabled() {
+        return mIsEnhanced4GModeEnabled;
+    }
+
+    /**
+     * @return {@code true} if video telephony is enabled by the user or not.
+     */
+    public boolean isVideoTelephonyEnabled() {
+        return mIsVideoTelephonyEnabled != 0;
+    }
+
+    /**
+     * @return {@code 1} if video telephony is enabled by the user or not.
+     */
+    public int getVideoTelephonyEnabled() {
+        return mIsVideoTelephonyEnabled;
+    }
+
+    /**
+     * @return {@code true} if Wi-Fi calling is enabled by the user or not when the device is not
+     * roaming.
+     */
+    public boolean isWifiCallingEnabled() {
+        return mIsWifiCallingEnabled == 1;
+    }
+
+    /**
+     * @return {@code 1} if Wi-Fi calling is enabled by the user or not when the device is not
+     * roaming. {@code 0} if disabled. {@code -1} if the user did not change any setting.
+     */
+    public int getWifiCallingEnabled() {
+        return mIsWifiCallingEnabled;
+    }
+
+    /**
+     * @return Wi-Fi calling mode when the device is not roaming.
+     */
+    @ImsMmTelManager.WiFiCallingMode
+    public int getWifiCallingMode() {
+        return mWifiCallingMode;
+    }
+
+    /**
+     * @return Wi-Fi calling mode when the device is roaming.
+     */
+    @ImsMmTelManager.WiFiCallingMode
+    public int getWifiCallingModeForRoaming() {
+        return mWifiCallingModeForRoaming;
+    }
+
+    /**
+     * @return {@code true} if Wi-Fi calling is enabled by the user or not when the device is
+     * roaming. {@code 0} if disabled. {@code -1} if the user did not change any setting.
+     */
+    public boolean isWifiCallingEnabledForRoaming() {
+        return mIsWifiCallingEnabledForRoaming == 1;
+    }
+
+    /**
+     * @return {@code 1} if Wi-Fi calling is enabled by the user or not when the device is roaming.
+     */
+    public int getWifiCallingEnabledForRoaming() {
+        return mIsWifiCallingEnabledForRoaming;
+    }
+
+    /**
+     * An opportunistic subscription connects to a network that is
+     * limited in functionality and / or coverage.
+     *
+     * @return {@code true} if subscription is opportunistic.
+     */
+    public boolean isOpportunistic() {
+        return mIsOpportunistic != 0;
+    }
+
+    /**
+     * An opportunistic subscription connects to a network that is
+     * limited in functionality and / or coverage.
+     *
+     * @return {@code 1} if subscription is opportunistic.
+     */
+    public int getOpportunistic() {
+        return mIsOpportunistic;
+    }
+
+    /**
+     * Used in scenarios where different subscriptions are bundled as a group.
+     * It's typically a primary and an opportunistic subscription. (see {@link #getOpportunistic()})
+     * Such that those subscriptions will have some affiliated behaviors such as opportunistic
+     * subscription may be invisible to the user.
+     *
+     * @return Group UUID in string format.
+     */
+    @NonNull
+    public String getGroupUuid() {
+        return mGroupUuid;
+    }
+
+    /**
+     * @return The ISO country code. Empty if not available.
+     */
+    public String getCountryIso() {
+        return mCountryIso;
+    }
+
+    /**
+     * @return The carrier id of this subscription carrier.
+     *
+     * @see TelephonyManager#getSimCarrierId()
+     */
+    public int getCarrierId() {
+        return mCarrierId;
+    }
+
+    /**
+     * @return The profile class populated from the profile metadata if present. Otherwise,
+     * the profile class defaults to {@link SubscriptionManager#PROFILE_CLASS_UNSET} if there is no
+     * profile metadata or the subscription is not on an eUICC ({@link #getEmbedded} return
+     * {@code 0}).
+     */
+    @ProfileClass
+    public int getProfileClass() {
+        return mProfileClass;
+    }
+
+    /**
+     * This method returns the type of a subscription. It can be
+     * {@link SubscriptionManager#SUBSCRIPTION_TYPE_LOCAL_SIM} or
+     * {@link SubscriptionManager#SUBSCRIPTION_TYPE_REMOTE_SIM}.
+     *
+     * @return The type of the subscription.
+     */
+    @SubscriptionType
+    public int getSubscriptionType() {
+        return mType;
+    }
+
+    /**
+     * @return The owner package of group the subscription belongs to.
+     */
+    @NonNull
+    public String getGroupOwner() {
+        return mGroupOwner;
+    }
+
+    /**
+     * @return The enabled mobile data policies in string format.
+     *
+     * @see com.android.internal.telephony.data.DataSettingsManager#getMobileDataPolicyEnabled
+     */
+    @NonNull
+    public String getEnabledMobileDataPolicies() {
+        return mEnabledMobileDataPolicies;
+    }
+
+    /**
+     * @return The IMSI (International Mobile Subscriber Identity) of the subscription.
+     */
+    @NonNull
+    public String getImsi() {
+        return mImsi;
+    }
+
+    /**
+     * @return {@code true} if Uicc applications are set to be enabled or disabled.
+     */
+    public boolean areUiccApplicationsEnabled() {
+        return mAreUiccApplicationsEnabled != 0;
+    }
+
+    /**
+     * @return {@code 1} if Uicc applications are set to be enabled or disabled.
+     */
+    public int getUiccApplicationsEnabled() {
+        return mAreUiccApplicationsEnabled;
+    }
+
+    /**
+     * @return {@code true} if the user has enabled IMS RCS User Capability Exchange (UCE) for this
+     * subscription.
+     */
+    public boolean isRcsUceEnabled() {
+        return mIsRcsUceEnabled != 0;
+    }
+
+    /**
+     * @return {@code 1} if the user has enabled IMS RCS User Capability Exchange (UCE) for this
+     * subscription.
+     */
+    public int getRcsUceEnabled() {
+        return mIsRcsUceEnabled;
+    }
+
+    /**
+     * @return {@code true} if the user has enabled cross SIM calling for this subscription.
+     */
+    public boolean isCrossSimCallingEnabled() {
+        return mIsCrossSimCallingEnabled != 0;
+    }
+
+    /**
+     * @return {@code 1} if the user has enabled cross SIM calling for this subscription.
+     */
+    public int getCrossSimCallingEnabled() {
+        return mIsCrossSimCallingEnabled;
+    }
+
+    /**
+     * @return The RCS configuration.
+     */
+    @NonNull
+    public byte[] getRcsConfig() {
+        return mRcsConfig;
+    }
+
+    /**
+     * The allowed network types for reasons in string format. The format is
+     * "[reason]=[network types bitmask], [reason]=[network types bitmask], ..."
+     *
+     * For example, "user=1239287394, thermal=298791239, carrier=3456812312".
+     */
+    @NonNull
+    public String getAllowedNetworkTypesForReasons() {
+        return mAllowedNetworkTypesForReasons;
+    }
+
+    /**
+     * @return Device to device sharing status.
+     */
+    @DeviceToDeviceStatusSharingPreference
+    public int getDeviceToDeviceStatusSharingPreference() {
+        return mDeviceToDeviceStatusSharingPreference;
+    }
+
+    /**
+     * @return {@code true} if the user has opted-in voice over IMS.
+     */
+    public boolean isVoImsOptInEnabled() {
+        return mIsVoImsOptInEnabled != 0;
+    }
+
+    /**
+     * @return {@code 1} if the user has opted-in voice over IMS.
+     */
+    public int getVoImsOptInEnabled() {
+        return mIsVoImsOptInEnabled;
+    }
+
+    /**
+     * @return Contacts information that allow device to device sharing.
+     */
+    @NonNull
+    public String getDeviceToDeviceStatusSharingContacts() {
+        return mDeviceToDeviceStatusSharingContacts;
+    }
+
+    /**
+     * @return {@code true} if the user has enabled NR advanced calling.
+     */
+    public boolean isNrAdvancedCallingEnabled() {
+        return mIsNrAdvancedCallingEnabled == 1;
+    }
+
+    /**
+     * @return {@code 1} if the user has enabled NR advanced calling. {code 0} if disabled.
+     * {code -1} if the user did not change any setting.
+     */
+    public int getNrAdvancedCallingEnabled() {
+        return mIsNrAdvancedCallingEnabled;
+    }
+
+    /**
+     * @return Get the phone number retrieved from carrier.
+     */
+    @NonNull
+    public String getNumberFromCarrier() {
+        return mNumberFromCarrier;
+    }
+
+    /**
+     * @return Get the phone number retrieved from IMS.
+     */
+    @NonNull
+    public String getNumberFromIms() {
+        return mNumberFromIms;
+    }
+
+    /**
+     * @return The port index of the SIM card which contains the subscription.
+     */
+    public int getPortIndex() {
+        return mPortIndex;
+    }
+
+    /**
+     * Get the usage setting for this subscription.
+     *
+     * @return The usage setting used for this subscription.
+     */
+    @UsageSetting
+    public int getUsageSetting() {
+        return mUsageSetting;
+    }
+
+    /**
+     * @return Last used TP message reference.
+     */
+    public int getLastUsedTPMessageReference() {
+        return mLastUsedTPMessageReference;
+    }
+
+    /**
+     * @return The user id associated with this subscription.
+     */
+    @UserIdInt
+    public int getUserId() {
+        return mUserId;
+    }
+
+    // Below are the fields that do not exist in SimInfo table.
+    /**
+     * @return The card ID of the SIM card which contains the subscription.
+     *
+     * @see android.telephony.UiccCardInfo#getCardId().
+     */
+    public int getCardId() {
+        return mCardId;
+    }
+
+    /**
+     * @return {@code true} if the group of the subscription is disabled. This is only useful if
+     * it's a grouped opportunistic subscription. In this case, if all primary (non-opportunistic)
+     * subscriptions in the group are deactivated (unplugged pSIM or deactivated eSIM profile), we
+     * should disable this opportunistic subscription.
+     */
+    public boolean isGroupDisabled() {
+        return mIsGroupDisabled;
+    }
+
+    /**
+     * @return {@code true} if the subscription is from the actively used SIM.
+     */
+    public boolean isActive() {
+        return mSimSlotIndex >= 0 || mType == SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM;
+    }
+
+    /**
+     * @return {@code true} if the subscription is visible to the user.
+     */
+    public boolean isVisible() {
+        return !isOpportunistic() || TextUtils.isEmpty(mGroupUuid);
+    }
+
+    /** @return converted {@link SubscriptionInfo}. */
+    @NonNull
+    public SubscriptionInfo toSubscriptionInfo() {
+        return new SubscriptionInfo.Builder()
+                .setId(mId)
+                .setIccId(mIccId)
+                .setSimSlotIndex(mSimSlotIndex)
+                .setDisplayName(mDisplayName)
+                .setCarrierName(mCarrierName)
+                .setDisplayNameSource(mDisplayNameSource)
+                .setIconTint(mIconTint)
+                .setNumber(mNumber)
+                .setDataRoaming(mDataRoaming)
+                .setMcc(mMcc)
+                .setMnc(mMnc)
+                .setEhplmns(TextUtils.isEmpty(mEhplmns) ? null : mEhplmns.split(","))
+                .setHplmns(TextUtils.isEmpty(mHplmns) ? null : mHplmns.split(","))
+                .setCountryIso(mCountryIso)
+                .setEmbedded(mIsEmbedded != 0)
+                .setNativeAccessRules(mNativeAccessRules.length == 0
+                        ? null : UiccAccessRule.decodeRules(mNativeAccessRules))
+                .setCardString(mCardString)
+                .setCardId(mCardId)
+                .setOpportunistic(mIsOpportunistic != 0)
+                .setGroupUuid(mGroupUuid)
+                .setGroupDisabled(mIsGroupDisabled)
+                .setCarrierId(mCarrierId)
+                .setProfileClass(mProfileClass)
+                .setType(mType)
+                .setGroupOwner(mGroupOwner)
+                .setCarrierConfigAccessRules(mCarrierConfigAccessRules.length == 0
+                        ? null : UiccAccessRule.decodeRules(mCarrierConfigAccessRules))
+                .setUiccApplicationsEnabled(mAreUiccApplicationsEnabled != 0)
+                .setPortIndex(mPortIndex)
+                .setUsageSetting(mUsageSetting)
+                .build();
+    }
+
+    /**
+     * Get ID stripped PII information on user build.
+     *
+     * @param id The PII id.
+     *
+     * @return The stripped string.
+     */
+    public static String givePrintableId(String id) {
+        String idToPrint = null;
+        if (id != null) {
+            int len = id.length();
+            if (len > 6 && !TelephonyUtils.IS_DEBUGGABLE) {
+                idToPrint = id.substring(0, len - 6) + Rlog.pii(false, id.substring(len - 6));
+            } else {
+                idToPrint = id;
+            }
+        }
+        return idToPrint;
+    }
+
+    @Override
+    public String toString() {
+        return "[SubscriptionInfoInternal: id=" + mId
+                + " iccId=" + givePrintableId(mIccId)
+                + " simSlotIndex=" + mSimSlotIndex
+                + " portIndex=" + mPortIndex
+                + " isEmbedded=" + mIsEmbedded
+                + " isRemovableEmbedded=" + mIsRemovableEmbedded
+                + " carrierId=" + mCarrierId
+                + " displayName=" + mDisplayName
+                + " carrierName=" + mCarrierName
+                + " isOpportunistic=" + mIsOpportunistic
+                + " groupUuid=" + mGroupUuid
+                + " groupOwner=" + mGroupOwner
+                + " displayNameSource="
+                + SubscriptionManager.displayNameSourceToString(mDisplayNameSource)
+                + " iconTint=" + mIconTint
+                + " number=" + Rlog.pii(TelephonyUtils.IS_DEBUGGABLE, mNumber)
+                + " dataRoaming=" + mDataRoaming
+                + " mcc=" + mMcc
+                + " mnc=" + mMnc
+                + " ehplmns=" + mEhplmns
+                + " hplmns=" + mHplmns
+                + " cardString=" + givePrintableId(mCardString)
+                + " cardId=" + mCardId
+                + " nativeAccessRules=" + IccUtils.bytesToHexString(mNativeAccessRules)
+                + " carrierConfigAccessRules=" + IccUtils.bytesToHexString(
+                        mCarrierConfigAccessRules)
+                + " countryIso=" + mCountryIso
+                + " profileClass=" + mProfileClass
+                + " type=" + SubscriptionManager.subscriptionTypeToString(mType)
+                + " areUiccApplicationsEnabled=" + mAreUiccApplicationsEnabled
+                + " usageSetting=" + SubscriptionManager.usageSettingToString(mUsageSetting)
+                + " isEnhanced4GModeEnabled=" + mIsEnhanced4GModeEnabled
+                + " isVideoTelephonyEnabled=" + mIsVideoTelephonyEnabled
+                + " isWifiCallingEnabled=" + mIsWifiCallingEnabled
+                + " isWifiCallingEnabledForRoaming=" + mIsWifiCallingEnabledForRoaming
+                + " wifiCallingMode=" + ImsMmTelManager.wifiCallingModeToString(mWifiCallingMode)
+                + " wifiCallingModeForRoaming="
+                + ImsMmTelManager.wifiCallingModeToString(mWifiCallingModeForRoaming)
+                + " enabledMobileDataPolicies=" + mEnabledMobileDataPolicies
+                + " imsi=" + givePrintableId(mImsi)
+                + " rcsUceEnabled=" + mIsRcsUceEnabled
+                + " crossSimCallingEnabled=" + mIsCrossSimCallingEnabled
+                + " rcsConfig=" + IccUtils.bytesToHexString(mRcsConfig)
+                + " allowedNetworkTypesForReasons=" + mAllowedNetworkTypesForReasons
+                + " deviceToDeviceStatusSharingPreference=" + mDeviceToDeviceStatusSharingPreference
+                + " isVoImsOptInEnabled=" + mIsVoImsOptInEnabled
+                + " deviceToDeviceStatusSharingContacts=" + mDeviceToDeviceStatusSharingContacts
+                + " numberFromCarrier=" + mNumberFromCarrier
+                + " numberFromIms=" + mNumberFromIms
+                + " userId=" + mUserId
+                + " isGroupDisabled=" + mIsGroupDisabled
+                + "]";
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        SubscriptionInfoInternal that = (SubscriptionInfoInternal) o;
+        return mId == that.mId && mSimSlotIndex == that.mSimSlotIndex
+                && mDisplayNameSource == that.mDisplayNameSource && mIconTint == that.mIconTint
+                && mDataRoaming == that.mDataRoaming && mIsEmbedded == that.mIsEmbedded
+                && mIsRemovableEmbedded == that.mIsRemovableEmbedded
+                && mIsEnhanced4GModeEnabled == that.mIsEnhanced4GModeEnabled
+                && mIsVideoTelephonyEnabled == that.mIsVideoTelephonyEnabled
+                && mIsWifiCallingEnabled == that.mIsWifiCallingEnabled
+                && mWifiCallingMode == that.mWifiCallingMode
+                && mWifiCallingModeForRoaming == that.mWifiCallingModeForRoaming
+                && mIsWifiCallingEnabledForRoaming == that.mIsWifiCallingEnabledForRoaming
+                && mIsOpportunistic == that.mIsOpportunistic && mCarrierId == that.mCarrierId
+                && mProfileClass == that.mProfileClass && mType == that.mType
+                && mAreUiccApplicationsEnabled == that.mAreUiccApplicationsEnabled
+                && mIsRcsUceEnabled == that.mIsRcsUceEnabled
+                && mIsCrossSimCallingEnabled == that.mIsCrossSimCallingEnabled
+                && mDeviceToDeviceStatusSharingPreference
+                == that.mDeviceToDeviceStatusSharingPreference
+                && mIsVoImsOptInEnabled == that.mIsVoImsOptInEnabled
+                && mIsNrAdvancedCallingEnabled == that.mIsNrAdvancedCallingEnabled
+                && mPortIndex == that.mPortIndex && mUsageSetting == that.mUsageSetting
+                && mLastUsedTPMessageReference == that.mLastUsedTPMessageReference
+                && mUserId == that.mUserId && mCardId == that.mCardId
+                && mIsGroupDisabled == that.mIsGroupDisabled && mIccId.equals(that.mIccId)
+                && mDisplayName.equals(that.mDisplayName) && mCarrierName.equals(that.mCarrierName)
+                && mNumber.equals(that.mNumber) && mMcc.equals(that.mMcc) && mMnc.equals(that.mMnc)
+                && mEhplmns.equals(that.mEhplmns) && mHplmns.equals(that.mHplmns)
+                && mCardString.equals(
+                that.mCardString) && Arrays.equals(mNativeAccessRules,
+                that.mNativeAccessRules) && Arrays.equals(mCarrierConfigAccessRules,
+                that.mCarrierConfigAccessRules) && mGroupUuid.equals(that.mGroupUuid)
+                && mCountryIso.equals(that.mCountryIso) && mGroupOwner.equals(that.mGroupOwner)
+                && mEnabledMobileDataPolicies.equals(that.mEnabledMobileDataPolicies)
+                && mImsi.equals(
+                that.mImsi) && Arrays.equals(mRcsConfig, that.mRcsConfig)
+                && mAllowedNetworkTypesForReasons.equals(that.mAllowedNetworkTypesForReasons)
+                && mDeviceToDeviceStatusSharingContacts.equals(
+                that.mDeviceToDeviceStatusSharingContacts) && mNumberFromCarrier.equals(
+                that.mNumberFromCarrier) && mNumberFromIms.equals(that.mNumberFromIms);
+    }
+
+    @Override
+    public int hashCode() {
+        int result = Objects.hash(mId, mIccId, mSimSlotIndex, mDisplayName, mCarrierName,
+                mDisplayNameSource, mIconTint, mNumber, mDataRoaming, mMcc, mMnc, mEhplmns, mHplmns,
+                mIsEmbedded, mCardString, mIsRemovableEmbedded, mIsEnhanced4GModeEnabled,
+                mIsVideoTelephonyEnabled, mIsWifiCallingEnabled, mWifiCallingMode,
+                mWifiCallingModeForRoaming, mIsWifiCallingEnabledForRoaming, mIsOpportunistic,
+                mGroupUuid, mCountryIso, mCarrierId, mProfileClass, mType, mGroupOwner,
+                mEnabledMobileDataPolicies, mImsi, mAreUiccApplicationsEnabled, mIsRcsUceEnabled,
+                mIsCrossSimCallingEnabled, mAllowedNetworkTypesForReasons,
+                mDeviceToDeviceStatusSharingPreference, mIsVoImsOptInEnabled,
+                mDeviceToDeviceStatusSharingContacts, mIsNrAdvancedCallingEnabled,
+                mNumberFromCarrier,
+                mNumberFromIms, mPortIndex, mUsageSetting, mLastUsedTPMessageReference, mUserId,
+                mCardId, mIsGroupDisabled);
+        result = 31 * result + Arrays.hashCode(mNativeAccessRules);
+        result = 31 * result + Arrays.hashCode(mCarrierConfigAccessRules);
+        result = 31 * result + Arrays.hashCode(mRcsConfig);
+        return result;
+    }
+
+    /**
+     * The builder class of {@link SubscriptionInfoInternal}.
+     */
+    public static class Builder {
+        /**
+         * The subscription id.
+         */
+        private int mId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+
+        /**
+         * The ICCID of the SIM that is associated with this subscription, empty if unknown.
+         */
+        @NonNull
+        private String mIccId = "";
+
+        /**
+         * The index of the SIM slot that currently contains the subscription and not necessarily
+         * unique and maybe {@link SubscriptionManager#INVALID_SIM_SLOT_INDEX} if unknown or the
+         * subscription is inactive.
+         */
+        private int mSimSlotIndex = SubscriptionManager.INVALID_SIM_SLOT_INDEX;
+
+        /**
+         * The name displayed to the user that identifies this subscription. This name is used
+         * in Settings page and can be renamed by the user.
+         */
+        @NonNull
+        private String mDisplayName = "";
+
+        /**
+         * The name displayed to the user that identifies subscription provider name. This name
+         * is the SPN displayed in status bar and many other places. Can't be renamed by the user.
+         */
+        @NonNull
+        private String mCarrierName = "";
+
+        /**
+         * The source of the display name.
+         */
+        @SimDisplayNameSource
+        private int mDisplayNameSource = SubscriptionManager.NAME_SOURCE_UNKNOWN;
+
+        /**
+         * The color to be used for tinting the icon when displaying to the user.
+         */
+        private int mIconTint = 0;
+
+        /**
+         * The number presented to the user identify this subscription.
+         */
+        @NonNull
+        private String mNumber = "";
+
+        /**
+         * Whether user enables data roaming for this subscription or not. Either
+         * {@link SubscriptionManager#DATA_ROAMING_ENABLE} or
+         * {@link SubscriptionManager#DATA_ROAMING_DISABLE}.
+         */
+        private int mDataRoaming = SubscriptionManager.DATA_ROAMING_DISABLE;
+
+        /**
+         * The mobile country code.
+         */
+        @NonNull
+        private String mMcc = "";
+
+        /**
+         * The mobile network code.
+         */
+        @NonNull
+        private String mMnc = "";
+
+        /**
+         * EHPLMNs associated with the subscription.
+         */
+        @NonNull
+        private String mEhplmns = "";
+
+        /**
+         * HPLMNs associated with the subscription.
+         */
+        @NonNull
+        private String mHplmns = "";
+
+        /**
+         * Whether the subscription is from eSIM.
+         */
+        private int mIsEmbedded = 0;
+
+        /**
+         * The card string of the SIM card.
+         */
+        @NonNull
+        private String mCardString = "";
+
+        /**
+         * The native access rules for this subscription, if it is embedded and defines any. This
+         * does not include access rules for non-embedded subscriptions.
+         */
+        @NonNull
+        private byte[] mNativeAccessRules = new byte[0];
+
+        /**
+         * The carrier certificates for this subscription that are saved in carrier configs.
+         * This does not include access rules from the Uicc, whether embedded or non-embedded.
+         */
+        @NonNull
+        private byte[] mCarrierConfigAccessRules = new byte[0];
+
+        /**
+         * Whether an embedded subscription is on a removable card. Such subscriptions are marked
+         * inaccessible as soon as the current card is removed. Otherwise, they will remain
+         * accessible unless explicitly deleted. Only meaningful when {@link #getEmbedded()} is
+         * {@code 1}.
+         */
+        private int mIsRemovableEmbedded = 0;
+
+        /**
+         * Whether enhanced 4G mode is enabled by the user or not.
+         */
+        private int mIsEnhanced4GModeEnabled = 0;
+
+        /**
+         * Whether video telephony is enabled by the user or not.
+         */
+        private int mIsVideoTelephonyEnabled = 0;
+
+        /**
+         * Whether Wi-Fi calling is enabled by the user or not when the device is not roaming.
+         */
+        private int mIsWifiCallingEnabled = 0;
+
+        /**
+         * Wi-Fi calling mode when the device is not roaming.
+         */
+        @ImsMmTelManager.WiFiCallingMode
+        private int mWifiCallingMode = ImsMmTelManager.WIFI_MODE_UNKNOWN;
+
+        /**
+         * Wi-Fi calling mode when the device is roaming.
+         */
+        @ImsMmTelManager.WiFiCallingMode
+        private int mWifiCallingModeForRoaming = ImsMmTelManager.WIFI_MODE_UNKNOWN;
+
+        /**
+         * Whether Wi-Fi calling is enabled by the user or not when the device is roaming.
+         */
+        private int mIsWifiCallingEnabledForRoaming = 0;
+
+        /**
+         * Whether the subscription is opportunistic or not.
+         */
+        private int mIsOpportunistic = 0;
+
+        /**
+         * The group UUID of the subscription group in string format.
+         */
+        @NonNull
+        private String mGroupUuid = "";
+
+        /**
+         * The ISO Country code for the subscription's provider.
+         */
+        @NonNull
+        private String mCountryIso = "";
+
+        /**
+         * The carrier id.
+         *
+         * @see TelephonyManager#getSimCarrierId()
+         */
+        private int mCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
+
+        /**
+         * The profile class populated from the profile metadata if present. Otherwise, the profile
+         * class defaults to {@link SubscriptionManager#PROFILE_CLASS_UNSET} if there is no profile
+         * metadata or the subscription is not on an eUICC ({@link #getEmbedded} returns
+         * {@code 0}).
+         */
+        @ProfileClass
+        private int mProfileClass = SubscriptionManager.PROFILE_CLASS_UNSET;
+
+        /**
+         * The subscription type.
+         */
+        @SubscriptionType
+        private int mType = SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM;
+
+        /**
+         * The owner package of group the subscription belongs to.
+         */
+        @NonNull
+        private String mGroupOwner = "";
+
+        /**
+         * The enabled mobile data policies in string format.
+         */
+        @NonNull
+        private String mEnabledMobileDataPolicies = "";
+
+        /**
+         * The IMSI (International Mobile Subscriber Identity) of the subscription.
+         */
+        @NonNull
+        private String mImsi = "";
+
+        /**
+         * Whether Uicc applications are configured to enable or not.
+         */
+        private int mAreUiccApplicationsEnabled = 0;
+
+        /**
+         * Whether the user has enabled IMS RCS User Capability Exchange (UCE) for this
+         * subscription.
+         */
+        private int mIsRcsUceEnabled = 0;
+
+        /**
+         * Whether the user has enabled cross SIM calling for this subscription.
+         */
+        private int mIsCrossSimCallingEnabled = 0;
+
+        /**
+         * The RCS configuration.
+         */
+        private byte[] mRcsConfig = new byte[0];
+
+        /**
+         * The allowed network types for reasons in string format. The format is
+         * "[reason]=[network types bitmask], [reason]=[network types bitmask], ..."
+         *
+         * For example, "user=1239287394, thermal=298791239, carrier=3456812312".
+         */
+        private String mAllowedNetworkTypesForReasons = "";
+
+        /**
+         * Device to device sharing status.
+         */
+        @DeviceToDeviceStatusSharingPreference
+        private int mDeviceToDeviceStatusSharingPreference =
+                SubscriptionManager.D2D_SHARING_DISABLED;
+
+        /**
+         * Whether the user has opted-in voice over IMS.
+         */
+        private int mIsVoImsOptInEnabled = 0;
+
+        /**
+         * Contacts information that allow device to device sharing.
+         */
+        @NonNull
+        private String mDeviceToDeviceStatusSharingContacts = "";
+
+        /**
+         * Whether the user has enabled NR advanced calling.
+         */
+        private int mIsNrAdvancedCallingEnabled = -1;
+
+        /**
+         * The phone number retrieved from carrier.
+         */
+        @NonNull
+        private String mNumberFromCarrier = "";
+
+        /**
+         * The phone number retrieved from IMS.
+         */
+        @NonNull
+        private String mNumberFromIms = "";
+
+        /**
+         * the port index of the Uicc card.
+         */
+        private int mPortIndex = TelephonyManager.INVALID_PORT_INDEX;
+
+        /**
+         * Subscription's preferred usage setting.
+         */
+        @UsageSetting
+        private int mUsageSetting = SubscriptionManager.USAGE_SETTING_UNKNOWN;
+
+        /**
+         * Last used TP message reference.
+         */
+        private int mLastUsedTPMessageReference = -1;
+
+        /**
+         * The user id associated with this subscription.
+         */
+        private int mUserId = UserHandle.USER_NULL;
+
+        // The following fields do not exist in the SimInfo table.
+        /**
+         * The card ID of the SIM card which contains the subscription.
+         */
+        private int mCardId = TelephonyManager.UNINITIALIZED_CARD_ID;
+
+        /**
+         * Whether group of the subscription is disabled. This is only useful if it's a grouped
+         * opportunistic subscription. In this case, if all primary (non-opportunistic)
+         * subscriptions in the group are deactivated (unplugged pSIM or deactivated eSIM profile),
+         * we should disable this opportunistic subscription.
+         */
+        private boolean mIsGroupDisabled;
+
+        /**
+         * Default constructor.
+         */
+        public Builder() {
+        }
+
+        /**
+         * Constructor from {@link SubscriptionInfoInternal}.
+         *
+         * @param info The subscription info.
+         */
+        public Builder(@NonNull SubscriptionInfoInternal info) {
+            mId = info.mId;
+            mIccId = info.mIccId;
+            mSimSlotIndex = info.mSimSlotIndex;
+            mDisplayName = info.mDisplayName;
+            mCarrierName = info.mCarrierName;
+            mDisplayNameSource = info.mDisplayNameSource;
+            mIconTint = info.mIconTint;
+            mNumber = info.mNumber;
+            mDataRoaming = info.mDataRoaming;
+            mMcc = info.mMcc;
+            mMnc = info.mMnc;
+            mEhplmns = info.mEhplmns;
+            mHplmns = info.mHplmns;
+            mIsEmbedded = info.mIsEmbedded;
+            mCardString = info.mCardString;
+            mNativeAccessRules = info.mNativeAccessRules;
+            mCarrierConfigAccessRules = info.mCarrierConfigAccessRules;
+            mIsRemovableEmbedded = info.mIsRemovableEmbedded;
+            mIsEnhanced4GModeEnabled = info.mIsEnhanced4GModeEnabled;
+            mIsVideoTelephonyEnabled = info.mIsVideoTelephonyEnabled;
+            mIsWifiCallingEnabled = info.mIsWifiCallingEnabled;
+            mWifiCallingMode = info.mWifiCallingMode;
+            mWifiCallingModeForRoaming = info.mWifiCallingModeForRoaming;
+            mIsWifiCallingEnabledForRoaming = info.mIsWifiCallingEnabledForRoaming;
+            mIsOpportunistic = info.mIsOpportunistic;
+            mGroupUuid = info.mGroupUuid;
+            mCountryIso = info.mCountryIso;
+            mCarrierId = info.mCarrierId;
+            mProfileClass = info.mProfileClass;
+            mType = info.mType;
+            mGroupOwner = info.mGroupOwner;
+            mEnabledMobileDataPolicies = info.mEnabledMobileDataPolicies;
+            mImsi = info.mImsi;
+            mAreUiccApplicationsEnabled = info.mAreUiccApplicationsEnabled;
+            mIsRcsUceEnabled = info.mIsRcsUceEnabled;
+            mIsCrossSimCallingEnabled = info.mIsCrossSimCallingEnabled;
+            mRcsConfig = info.mRcsConfig;
+            mAllowedNetworkTypesForReasons = info.mAllowedNetworkTypesForReasons;
+            mDeviceToDeviceStatusSharingPreference = info.mDeviceToDeviceStatusSharingPreference;
+            mIsVoImsOptInEnabled = info.mIsVoImsOptInEnabled;
+            mDeviceToDeviceStatusSharingContacts = info.mDeviceToDeviceStatusSharingContacts;
+            mIsNrAdvancedCallingEnabled = info.mIsNrAdvancedCallingEnabled;
+            mNumberFromCarrier = info.mNumberFromCarrier;
+            mNumberFromIms = info.mNumberFromIms;
+            mPortIndex = info.mPortIndex;
+            mUsageSetting = info.mUsageSetting;
+            mLastUsedTPMessageReference = info.getLastUsedTPMessageReference();
+            mUserId = info.mUserId;
+            // Below are the fields that do not exist in the SimInfo table.
+            mCardId = info.mCardId;
+            mIsGroupDisabled = info.mIsGroupDisabled;
+        }
+
+        /**
+         * Set the subscription id.
+         *
+         * @param id The subscription id.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setId(int id) {
+            mId = id;
+            return this;
+        }
+
+        /**
+         * Set the ICCID of the SIM that is associated with this subscription.
+         *
+         * @param iccId The ICCID of the SIM that is associated with this subscription.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setIccId(@NonNull String iccId) {
+            Objects.requireNonNull(iccId);
+            mIccId = iccId;
+            return this;
+        }
+
+        /**
+         * Set the SIM index of the slot that currently contains the subscription. Set to
+         * {@link SubscriptionManager#INVALID_SIM_SLOT_INDEX} if the subscription is inactive.
+         *
+         * @param simSlotIndex The SIM slot index.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setSimSlotIndex(int simSlotIndex) {
+            mSimSlotIndex = simSlotIndex;
+            return this;
+        }
+
+        /**
+         * The name displayed to the user that identifies this subscription. This name is used
+         * in Settings page and can be renamed by the user.
+         *
+         * @param displayName The display name.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setDisplayName(@NonNull String displayName) {
+            Objects.requireNonNull(displayName);
+            mDisplayName = displayName;
+            return this;
+        }
+
+        /**
+         * The name displayed to the user that identifies subscription provider name. This name
+         * is the SPN displayed in status bar and many other places. Can't be renamed by the user.
+         *
+         * @param carrierName The carrier name.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setCarrierName(@NonNull String carrierName) {
+            Objects.requireNonNull(carrierName);
+            mCarrierName = carrierName;
+            return this;
+        }
+
+        /**
+         * Set the source of the display name.
+         *
+         * @param displayNameSource The source of the display name.
+         * @return The builder.
+         *
+         * @see SubscriptionInfoInternal#getDisplayName()
+         */
+        @NonNull
+        public Builder setDisplayNameSource(@SimDisplayNameSource int displayNameSource) {
+            mDisplayNameSource = displayNameSource;
+            return this;
+        }
+
+        /**
+         * Set the color to be used for tinting the icon when displaying to the user.
+         *
+         * @param iconTint The color to be used for tinting the icon when displaying to the user.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setIconTint(int iconTint) {
+            mIconTint = iconTint;
+            return this;
+        }
+
+        /**
+         * Set the number presented to the user identify this subscription.
+         *
+         * @param number the number presented to the user identify this subscription.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setNumber(@NonNull String number) {
+            Objects.requireNonNull(number);
+            mNumber = number;
+            return this;
+        }
+
+        /**
+         * Set whether user enables data roaming for this subscription or not.
+         *
+         * @param dataRoaming Data roaming mode. Either
+         * {@link SubscriptionManager#DATA_ROAMING_ENABLE} or
+         * {@link SubscriptionManager#DATA_ROAMING_DISABLE}
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setDataRoaming(int dataRoaming) {
+            mDataRoaming = dataRoaming;
+            return this;
+        }
+
+        /**
+         * Set the mobile country code.
+         *
+         * @param mcc The mobile country code.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setMcc(@NonNull String mcc) {
+            Objects.requireNonNull(mcc);
+            mMcc = mcc;
+            return this;
+        }
+
+        /**
+         * Set the mobile network code.
+         *
+         * @param mnc Mobile network code.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setMnc(@NonNull String mnc) {
+            Objects.requireNonNull(mnc);
+            mMnc = mnc;
+            return this;
+        }
+
+        /**
+         * Set EHPLMNs associated with the subscription.
+         *
+         * @param ehplmns EHPLMNs associated with the subscription.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setEhplmns(@NonNull String ehplmns) {
+            Objects.requireNonNull(ehplmns);
+            mEhplmns = ehplmns;
+            return this;
+        }
+
+        /**
+         * Set HPLMNs associated with the subscription.
+         *
+         * @param hplmns HPLMNs associated with the subscription.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setHplmns(@NonNull String hplmns) {
+            Objects.requireNonNull(hplmns);
+            mHplmns = hplmns;
+            return this;
+        }
+
+        /**
+         * Set whether the subscription is from eSIM or not.
+         *
+         * @param isEmbedded {@code 1} if the subscription is from eSIM.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setEmbedded(int isEmbedded) {
+            mIsEmbedded = isEmbedded;
+            return this;
+        }
+
+        /**
+         * Set the card string of the SIM card.
+         *
+         * @param cardString The card string of the SIM card.
+         *
+         * @return The builder.
+         *
+         * @see #getCardString()
+         */
+        @NonNull
+        public Builder setCardString(@NonNull String cardString) {
+            Objects.requireNonNull(cardString);
+            mCardString = cardString;
+            return this;
+        }
+
+        /**
+         * Set the native access rules for this subscription, if it is embedded and defines any.
+         * This does not include access rules for non-embedded subscriptions.
+         *
+         * @param nativeAccessRules The native access rules for this subscription.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setNativeAccessRules(@NonNull byte[] nativeAccessRules) {
+            Objects.requireNonNull(nativeAccessRules);
+            mNativeAccessRules = nativeAccessRules;
+            return this;
+        }
+
+        /**
+         * Set the native access rules for this subscription, if it is embedded and defines any.
+         * This does not include access rules for non-embedded subscriptions.
+         *
+         * @param nativeAccessRules The native access rules for this subscription.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setNativeAccessRules(@NonNull List<UiccAccessRule> nativeAccessRules) {
+            Objects.requireNonNull(nativeAccessRules);
+            if (!nativeAccessRules.isEmpty()) {
+                mNativeAccessRules = UiccAccessRule.encodeRules(
+                        nativeAccessRules.toArray(new UiccAccessRule[0]));
+            }
+            return this;
+        }
+
+        /**
+         * Set the carrier certificates for this subscription that are saved in carrier configs.
+         * This does not include access rules from the Uicc, whether embedded or non-embedded.
+         *
+         * @param carrierConfigAccessRules The carrier certificates for this subscription.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setCarrierConfigAccessRules(@NonNull byte[] carrierConfigAccessRules) {
+            Objects.requireNonNull(carrierConfigAccessRules);
+            mCarrierConfigAccessRules = carrierConfigAccessRules;
+            return this;
+        }
+
+        /**
+         * Set whether an embedded subscription is on a removable card. Such subscriptions are
+         * marked inaccessible as soon as the current card is removed. Otherwise, they will remain
+         * accessible unless explicitly deleted. Only meaningful when {@link #getEmbedded()} is
+         * {@code 1}.
+         *
+         * @param isRemovableEmbedded {@code true} if the subscription is from the removable
+         * embedded SIM.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setRemovableEmbedded(boolean isRemovableEmbedded) {
+            mIsRemovableEmbedded = isRemovableEmbedded ? 1 : 0;
+            return this;
+        }
+
+        /**
+         * Set whether an embedded subscription is on a removable card. Such subscriptions are
+         * marked inaccessible as soon as the current card is removed. Otherwise, they will remain
+         * accessible unless explicitly deleted. Only meaningful when {@link #getEmbedded()} is
+         * {@code 1}.
+         *
+         * @param isRemovableEmbedded {@code 1} if the subscription is from the removable
+         * embedded SIM.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setRemovableEmbedded(int isRemovableEmbedded) {
+            mIsRemovableEmbedded = isRemovableEmbedded;
+            return this;
+        }
+
+        /**
+         * Set whether enhanced 4G mode is enabled by the user or not.
+         *
+         * @param isEnhanced4GModeEnabled whether enhanced 4G mode is enabled by the user or not.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setEnhanced4GModeEnabled(int isEnhanced4GModeEnabled) {
+            mIsEnhanced4GModeEnabled = isEnhanced4GModeEnabled;
+            return this;
+        }
+
+        /**
+         * Set whether video telephony is enabled by the user or not.
+         *
+         * @param isVideoTelephonyEnabled whether video telephony is enabled by the user or not.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setVideoTelephonyEnabled(int isVideoTelephonyEnabled) {
+            mIsVideoTelephonyEnabled = isVideoTelephonyEnabled;
+            return this;
+        }
+
+        /**
+         * Set whether Wi-Fi calling is enabled by the user or not when the device is not roaming.
+         *
+         * @param isWifiCallingEnabled whether Wi-Fi calling is enabled by the user or not when
+         * the device is not roaming.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setWifiCallingEnabled(int isWifiCallingEnabled) {
+            mIsWifiCallingEnabled = isWifiCallingEnabled;
+            return this;
+        }
+
+        /**
+         * Set Wi-Fi calling mode when the device is not roaming.
+         *
+         * @param wifiCallingMode Wi-Fi calling mode when the device is not roaming.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setWifiCallingMode(@ImsMmTelManager.WiFiCallingMode int wifiCallingMode) {
+            mWifiCallingMode = wifiCallingMode;
+            return this;
+        }
+
+        /**
+         * Set Wi-Fi calling mode when the device is roaming.
+         *
+         * @param wifiCallingModeForRoaming Wi-Fi calling mode when the device is roaming.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setWifiCallingModeForRoaming(
+                @ImsMmTelManager.WiFiCallingMode int wifiCallingModeForRoaming) {
+            mWifiCallingModeForRoaming = wifiCallingModeForRoaming;
+            return this;
+        }
+
+        /**
+         * Set whether Wi-Fi calling is enabled by the user or not when the device is roaming.
+         *
+         * @param wifiCallingEnabledForRoaming whether Wi-Fi calling is enabled by the user or not
+         * when the device is roaming.
+         *
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setWifiCallingEnabledForRoaming(int wifiCallingEnabledForRoaming) {
+            mIsWifiCallingEnabledForRoaming = wifiCallingEnabledForRoaming;
+            return this;
+        }
+
+        /**
+         * Set whether the subscription is opportunistic or not.
+         *
+         * @param isOpportunistic {@code 1} if the subscription is opportunistic.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setOpportunistic(int isOpportunistic) {
+            mIsOpportunistic = isOpportunistic;
+            return this;
+        }
+
+        /**
+         * Set the group UUID of the subscription group.
+         *
+         * @param groupUuid The group UUID.
+         * @return The builder.
+         *
+         */
+        @NonNull
+        public Builder setGroupUuid(@NonNull String groupUuid) {
+            Objects.requireNonNull(groupUuid);
+            mGroupUuid = groupUuid;
+            return this;
+        }
+
+        /**
+         * Set the ISO country code for the subscription's provider.
+         *
+         * @param countryIso The ISO country code for the subscription's provider.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setCountryIso(@NonNull String countryIso) {
+            Objects.requireNonNull(countryIso);
+            mCountryIso = countryIso;
+            return this;
+        }
+
+        /**
+         * Set the subscription carrier id.
+         *
+         * @param carrierId The carrier id.
+         * @return The builder
+         *
+         * @see TelephonyManager#getSimCarrierId()
+         */
+        @NonNull
+        public Builder setCarrierId(int carrierId) {
+            mCarrierId = carrierId;
+            return this;
+        }
+
+        /**
+         * Set the profile class populated from the profile metadata if present.
+         *
+         * @param profileClass the profile class populated from the profile metadata if present.
+         * @return The builder
+         *
+         * @see #getProfileClass()
+         */
+        @NonNull
+        public Builder setProfileClass(@ProfileClass int profileClass) {
+            mProfileClass = profileClass;
+            return this;
+        }
+
+        /**
+         * Set the subscription type.
+         *
+         * @param type Subscription type.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setType(@SubscriptionType int type) {
+            mType = type;
+            return this;
+        }
+
+        /**
+         * Set the owner package of group the subscription belongs to.
+         *
+         * @param groupOwner Owner package of group the subscription belongs to.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setGroupOwner(@NonNull String groupOwner) {
+            Objects.requireNonNull(groupOwner);
+            mGroupOwner = groupOwner;
+            return this;
+        }
+
+        /**
+         * Set the enabled mobile data policies.
+         *
+         * @param enabledMobileDataPolicies The enabled mobile data policies.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setEnabledMobileDataPolicies(@NonNull String enabledMobileDataPolicies) {
+            Objects.requireNonNull(enabledMobileDataPolicies);
+            mEnabledMobileDataPolicies = enabledMobileDataPolicies;
+            return this;
+        }
+
+        /**
+         * Set the IMSI (International Mobile Subscriber Identity) of the subscription.
+         *
+         * @param imsi The IMSI.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setImsi(@NonNull String imsi) {
+            Objects.requireNonNull(imsi);
+            mImsi = imsi;
+            return this;
+        }
+
+        /**
+         * Set whether Uicc applications are configured to enable or not.
+         *
+         * @param areUiccApplicationsEnabled {@code 1} if Uicc applications are configured to
+         * enable.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setUiccApplicationsEnabled(int areUiccApplicationsEnabled) {
+            mAreUiccApplicationsEnabled = areUiccApplicationsEnabled;
+            return this;
+        }
+
+        /**
+         * Set whether the user has enabled IMS RCS User Capability Exchange (UCE) for this
+         * subscription.
+         *
+         * @param isRcsUceEnabled If the user enabled RCS UCE for this subscription.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setRcsUceEnabled(int isRcsUceEnabled) {
+            mIsRcsUceEnabled = isRcsUceEnabled;
+            return this;
+        }
+
+        /**
+         * Set whether the user has enabled cross SIM calling for this subscription.
+         *
+         * @param isCrossSimCallingEnabled If the user enabled cross SIM calling for this
+         * subscription.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setCrossSimCallingEnabled(int isCrossSimCallingEnabled) {
+            mIsCrossSimCallingEnabled = isCrossSimCallingEnabled;
+            return this;
+        }
+
+        /**
+         * Set the RCS config for this subscription.
+         *
+         * @param rcsConfig The RCS config for this subscription.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setRcsConfig(byte[] rcsConfig) {
+            Objects.requireNonNull(rcsConfig);
+            mRcsConfig = rcsConfig;
+            return this;
+        }
+
+        /**
+         * Set the allowed network types for reasons.
+         *
+         * @param allowedNetworkTypesForReasons The allowed network types for reasons in string
+         * format. The format is
+         * "[reason]=[network types bitmask], [reason]=[network types bitmask], ..."
+         *
+         * For example, "user=1239287394, thermal=298791239, carrier=3456812312".
+         *
+         * @return The builder.
+         */
+        public Builder setAllowedNetworkTypesForReasons(
+                @NonNull String allowedNetworkTypesForReasons) {
+            Objects.requireNonNull(allowedNetworkTypesForReasons);
+            mAllowedNetworkTypesForReasons = allowedNetworkTypesForReasons;
+            return this;
+        }
+
+        /**
+         * Set device to device sharing status.
+         *
+         * @param deviceToDeviceStatusSharingPreference Device to device sharing status.
+         * @return The builder.
+         */
+        public Builder setDeviceToDeviceStatusSharingPreference(
+                @DeviceToDeviceStatusSharingPreference int deviceToDeviceStatusSharingPreference) {
+            mDeviceToDeviceStatusSharingPreference = deviceToDeviceStatusSharingPreference;
+            return this;
+        }
+
+        /**
+         * Set whether the user has opted-in voice over IMS.
+         *
+         * @param isVoImsOptInEnabled Whether the user has opted-in voice over IMS.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setVoImsOptInEnabled(int isVoImsOptInEnabled) {
+            mIsVoImsOptInEnabled = isVoImsOptInEnabled;
+            return this;
+        }
+
+        /**
+         * Set contacts information that allow device to device sharing.
+         *
+         * @param deviceToDeviceStatusSharingContacts contacts information that allow device to
+         * device sharing.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setDeviceToDeviceStatusSharingContacts(
+                @NonNull String deviceToDeviceStatusSharingContacts) {
+            Objects.requireNonNull(deviceToDeviceStatusSharingContacts);
+            mDeviceToDeviceStatusSharingContacts = deviceToDeviceStatusSharingContacts;
+            return this;
+        }
+
+        /**
+         * Set whether the user has enabled NR advanced calling.
+         *
+         * @param isNrAdvancedCallingEnabled Whether the user has enabled NR advanced calling.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setNrAdvancedCallingEnabled(int isNrAdvancedCallingEnabled) {
+            mIsNrAdvancedCallingEnabled = isNrAdvancedCallingEnabled;
+            return this;
+        }
+
+        /**
+         * Set the phone number retrieved from carrier.
+         *
+         * @param numberFromCarrier The phone number retrieved from carrier.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setNumberFromCarrier(@NonNull String numberFromCarrier) {
+            Objects.requireNonNull(numberFromCarrier);
+            mNumberFromCarrier = numberFromCarrier;
+            return this;
+        }
+
+        /**
+         * Set the phone number retrieved from IMS.
+         *
+         * @param numberFromIms The phone number retrieved from IMS.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setNumberFromIms(@NonNull String numberFromIms) {
+            Objects.requireNonNull(numberFromIms);
+            mNumberFromIms = numberFromIms;
+            return this;
+        }
+
+        /**
+         * Set the port index of the Uicc card.
+         *
+         * @param portIndex The port index of the Uicc card.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setPortIndex(int portIndex) {
+            mPortIndex = portIndex;
+            return this;
+        }
+
+        /**
+         * Set subscription's preferred usage setting.
+         *
+         * @param usageSetting Subscription's preferred usage setting.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setUsageSetting(@UsageSetting int usageSetting) {
+            mUsageSetting = usageSetting;
+            return this;
+        }
+
+        /**
+         * Set last used TP message reference.
+         *
+         * @param lastUsedTPMessageReference Last used TP message reference.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setLastUsedTPMessageReference(
+                int lastUsedTPMessageReference) {
+            mLastUsedTPMessageReference = lastUsedTPMessageReference;
+            return this;
+        }
+
+        /**
+         * Set the user id associated with this subscription.
+         *
+         * @param userId The user id associated with this subscription.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setUserId(@UserIdInt int userId) {
+            mUserId = userId;
+            return this;
+        }
+
+        // Below are the fields that do not exist in the SimInfo table.
+        /**
+         * Set the card ID of the SIM card which contains the subscription.
+         *
+         * @param cardId The card ID of the SIM card which contains the subscription.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setCardId(int cardId) {
+            mCardId = cardId;
+            return this;
+        }
+
+        /**
+         * Whether group of the subscription is disabled. This is only useful if it's a grouped
+         * opportunistic subscription. In this case, if all primary (non-opportunistic)
+         * subscriptions in the group are deactivated (unplugged pSIM or deactivated eSIM profile),
+         * we should disable this opportunistic subscription.
+         *
+         * @param isGroupDisabled {@code 1} if group of the subscription is disabled.
+         * @return The builder.
+         */
+        @NonNull
+        public Builder setGroupDisabled(boolean isGroupDisabled) {
+            mIsGroupDisabled = isGroupDisabled;
+            return this;
+        }
+
+        /**
+         * Build the {@link SubscriptionInfoInternal}.
+         *
+         * @return The {@link SubscriptionInfoInternal} instance.
+         */
+        public SubscriptionInfoInternal build() {
+            return new SubscriptionInfoInternal(this);
+        }
+    }
+}
diff --git a/src/java/com/android/internal/telephony/subscription/SubscriptionManagerService.java b/src/java/com/android/internal/telephony/subscription/SubscriptionManagerService.java
new file mode 100644
index 0000000..27007f1
--- /dev/null
+++ b/src/java/com/android/internal/telephony/subscription/SubscriptionManagerService.java
@@ -0,0 +1,3638 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.subscription;
+
+import android.Manifest;
+import android.annotation.CallbackExecutor;
+import android.annotation.ColorInt;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.RequiresPermission;
+import android.app.AppOpsManager;
+import android.app.PendingIntent;
+import android.app.compat.CompatChanges;
+import android.compat.annotation.ChangeId;
+import android.compat.annotation.EnabledSince;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.res.Resources;
+import android.os.Binder;
+import android.os.Build;
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.os.Looper;
+import android.os.ParcelUuid;
+import android.os.PersistableBundle;
+import android.os.RemoteException;
+import android.os.TelephonyServiceManager;
+import android.os.UserHandle;
+import android.provider.Settings;
+import android.provider.Telephony.SimInfo;
+import android.service.carrier.CarrierIdentifier;
+import android.service.euicc.EuiccProfileInfo;
+import android.service.euicc.EuiccService;
+import android.service.euicc.GetEuiccProfileInfoListResult;
+import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
+import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.SubscriptionManager.DataRoamingMode;
+import android.telephony.SubscriptionManager.DeviceToDeviceStatusSharingPreference;
+import android.telephony.SubscriptionManager.PhoneNumberSource;
+import android.telephony.SubscriptionManager.SimDisplayNameSource;
+import android.telephony.SubscriptionManager.SubscriptionType;
+import android.telephony.SubscriptionManager.UsageSetting;
+import android.telephony.TelephonyFrameworkInitializer;
+import android.telephony.TelephonyManager;
+import android.telephony.TelephonyManager.SimState;
+import android.telephony.TelephonyRegistryManager;
+import android.telephony.UiccAccessRule;
+import android.telephony.euicc.EuiccManager;
+import android.text.TextUtils;
+import android.util.ArraySet;
+import android.util.Base64;
+import android.util.EventLog;
+import android.util.IndentingPrintWriter;
+import android.util.LocalLog;
+
+import com.android.internal.R;
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.CarrierResolver;
+import com.android.internal.telephony.ISetOpportunisticDataCallback;
+import com.android.internal.telephony.ISub;
+import com.android.internal.telephony.IccCard;
+import com.android.internal.telephony.MultiSimSettingController;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneFactory;
+import com.android.internal.telephony.TelephonyIntents;
+import com.android.internal.telephony.TelephonyPermissions;
+import com.android.internal.telephony.data.PhoneSwitcher;
+import com.android.internal.telephony.euicc.EuiccController;
+import com.android.internal.telephony.subscription.SubscriptionDatabaseManager.SubscriptionDatabaseManagerCallback;
+import com.android.internal.telephony.uicc.IccUtils;
+import com.android.internal.telephony.uicc.UiccCard;
+import com.android.internal.telephony.uicc.UiccController;
+import com.android.internal.telephony.uicc.UiccPort;
+import com.android.internal.telephony.uicc.UiccSlot;
+import com.android.internal.telephony.util.ArrayUtils;
+import com.android.telephony.Rlog;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executor;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+/**
+ * The subscription manager service is the backend service of {@link SubscriptionManager}.
+ * The service handles all SIM subscription related requests from clients.
+ */
+public class SubscriptionManagerService extends ISub.Stub {
+    private static final String LOG_TAG = "SMSVC";
+
+    /** Whether enabling verbose debugging message or not. */
+    private static final boolean VDBG = false;
+
+    /**
+     * The columns in {@link SimInfo} table that can be directly accessed through
+     * {@link #getSubscriptionProperty(int, String, String, String)} or
+     * {@link #setSubscriptionProperty(int, String, String)}. Usually those fields are not
+     * sensitive. Mostly they are related to user settings, for example, wifi calling
+     * user settings, cross sim calling user settings, etc...Those fields are protected with
+     * {@link Manifest.permission#READ_PHONE_STATE} permission only.
+     *
+     * For sensitive fields, they usually requires special methods to access. For example,
+     * {@link #getSubscriptionUserHandle(int)} or {@link #getPhoneNumber(int, int, String, String)}
+     * that requires higher permission to access.
+     */
+    private static final Set<String> DIRECT_ACCESS_SUBSCRIPTION_COLUMNS = Set.of(
+            SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED,
+            SimInfo.COLUMN_VT_IMS_ENABLED,
+            SimInfo.COLUMN_WFC_IMS_ENABLED,
+            SimInfo.COLUMN_WFC_IMS_MODE,
+            SimInfo.COLUMN_WFC_IMS_ROAMING_MODE,
+            SimInfo.COLUMN_WFC_IMS_ROAMING_ENABLED,
+            SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES,
+            SimInfo.COLUMN_IMS_RCS_UCE_ENABLED,
+            SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED,
+            SimInfo.COLUMN_RCS_CONFIG,
+            SimInfo.COLUMN_ALLOWED_NETWORK_TYPES_FOR_REASONS,
+            SimInfo.COLUMN_D2D_STATUS_SHARING,
+            SimInfo.COLUMN_VOIMS_OPT_IN_STATUS,
+            SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS,
+            SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED
+    );
+
+    /**
+     * Apps targeting on Android T and beyond will get exception if there is no access to device
+     * identifiers nor has carrier privileges when calling
+     * {@link SubscriptionManager#getSubscriptionsInGroup}.
+     */
+    @ChangeId
+    @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
+    public static final long REQUIRE_DEVICE_IDENTIFIERS_FOR_GROUP_UUID = 213902861L;
+
+    /** Instance of subscription manager service. */
+    @NonNull
+    private static SubscriptionManagerService sInstance;
+
+    /** The context */
+    @NonNull
+    private final Context mContext;
+
+    /** App Ops manager instance. */
+    @NonNull
+    private final AppOpsManager mAppOpsManager;
+
+    /** Telephony manager instance. */
+    @NonNull
+    private final TelephonyManager mTelephonyManager;
+
+    /** Subscription manager instance. */
+    @NonNull
+    private final SubscriptionManager mSubscriptionManager;
+
+    /**
+     * Euicc manager instance. Will be null if the device does not support
+     * {@link PackageManager#FEATURE_TELEPHONY_EUICC}.
+     */
+    @Nullable
+    private final EuiccManager mEuiccManager;
+
+    /** Uicc controller instance. */
+    @NonNull
+    private final UiccController mUiccController;
+
+    /**
+     * Euicc controller instance. Will be null if the device does not support
+     * {@link PackageManager#FEATURE_TELEPHONY_EUICC}.
+     */
+    @Nullable
+    private EuiccController mEuiccController;
+
+    /** The main handler of subscription manager service. */
+    @NonNull
+    private final Handler mHandler;
+
+    /** Local log for most important debug messages. */
+    @NonNull
+    private final LocalLog mLocalLog = new LocalLog(128);
+
+    /** The subscription database manager. */
+    @NonNull
+    private final SubscriptionDatabaseManager mSubscriptionDatabaseManager;
+
+    /** The slot index subscription id map. Key is the slot index, and the value is sub id. */
+    @NonNull
+    private final WatchedMap<Integer, Integer> mSlotIndexToSubId = new WatchedMap<>();
+
+    /** Subscription manager service callbacks. */
+    @NonNull
+    private final Set<SubscriptionManagerServiceCallback> mSubscriptionManagerServiceCallbacks =
+            new ArraySet<>();
+
+    /**
+     * Default sub id. Derived from {@link #mDefaultVoiceSubId} and {@link #mDefaultDataSubId},
+     * depending on device capability.
+     */
+    @NonNull
+    private final WatchedInt mDefaultSubId;
+
+    /** Default voice subscription id. */
+    @NonNull
+    private final WatchedInt mDefaultVoiceSubId;
+
+    /** Default data subscription id. */
+    @NonNull
+    private final WatchedInt mDefaultDataSubId;
+
+    /** Default sms subscription id. */
+    @NonNull
+    private final WatchedInt mDefaultSmsSubId;
+
+    /** Sim state per logical SIM slot index. */
+    @NonNull
+    private final int[] mSimState;
+
+    /**
+     * Watched map that automatically invalidate cache in {@link SubscriptionManager}.
+     */
+    private static class WatchedMap<K, V> extends ConcurrentHashMap<K, V> {
+        @Override
+        public void clear() {
+            super.clear();
+            SubscriptionManager.invalidateSubscriptionManagerServiceCaches();
+        }
+
+        @Override
+        public V put(K key, V value) {
+            V oldValue = super.put(key, value);
+            if (!Objects.equals(oldValue, value)) {
+                SubscriptionManager.invalidateSubscriptionManagerServiceCaches();
+            }
+            return oldValue;
+        }
+
+        @Override
+        public V remove(Object key) {
+            V oldValue = super.remove(key);
+            if (oldValue != null) {
+                SubscriptionManager.invalidateSubscriptionManagerServiceCaches();
+            }
+            return oldValue;
+        }
+    }
+
+    /**
+     * Watched integer.
+     */
+    public static class WatchedInt {
+        protected int mValue;
+
+        /**
+         * Constructor.
+         *
+         * @param initialValue The initial value.
+         */
+        public WatchedInt(int initialValue) {
+            mValue = initialValue;
+        }
+
+        /**
+         * @return The value.
+         */
+        public int get() {
+            return mValue;
+        }
+
+        /**
+         * Set the value.
+         *
+         * @param newValue The new value.
+         *
+         * @return {@code true} if {@code newValue} is different from the existing value.
+         */
+        public boolean set(int newValue) {
+            if (mValue != newValue) {
+                mValue = newValue;
+                SubscriptionManager.invalidateSubscriptionManagerServiceCaches();
+                return true;
+            }
+            return false;
+        }
+    }
+
+    /**
+     * This is the callback used for listening events from {@link SubscriptionManagerService}.
+     */
+    public static class SubscriptionManagerServiceCallback {
+        /** The executor of the callback. */
+        @NonNull
+        private final Executor mExecutor;
+
+        /**
+         * Constructor
+         *
+         * @param executor The executor of the callback.
+         */
+        public SubscriptionManagerServiceCallback(@NonNull @CallbackExecutor Executor executor) {
+            mExecutor = executor;
+        }
+
+        /**
+         * @return The executor of the callback.
+         */
+        @NonNull
+        @VisibleForTesting
+        public Executor getExecutor() {
+            return mExecutor;
+        }
+
+        /**
+         * Invoke the callback from executor.
+         *
+         * @param runnable The callback method to invoke.
+         */
+        public void invokeFromExecutor(@NonNull Runnable runnable) {
+            mExecutor.execute(runnable);
+        }
+
+        /**
+         * Called when subscription changed.
+         *
+         * @param subId The subscription id.
+         */
+        public void onSubscriptionChanged(int subId) {}
+
+        /**
+         * Called when {@link SubscriptionInfoInternal#areUiccApplicationsEnabled()} changed.
+         *
+         * @param subId The subscription id.
+         */
+        public void onUiccApplicationsEnabled(int subId) {}
+    }
+
+    /**
+     * The constructor
+     *
+     * @param context The context
+     * @param looper The looper for the handler.
+     */
+    public SubscriptionManagerService(@NonNull Context context, @NonNull Looper looper) {
+        sInstance = this;
+        mContext = context;
+        mTelephonyManager = context.getSystemService(TelephonyManager.class);
+        mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
+        mEuiccManager = context.getSystemService(EuiccManager.class);
+        mAppOpsManager = context.getSystemService(AppOpsManager.class);
+
+        mUiccController = UiccController.getInstance();
+        mHandler = new Handler(looper);
+        TelephonyServiceManager.ServiceRegisterer subscriptionServiceRegisterer =
+                TelephonyFrameworkInitializer
+                        .getTelephonyServiceManager()
+                        .getSubscriptionServiceRegisterer();
+        if (subscriptionServiceRegisterer.get() == null) {
+            subscriptionServiceRegisterer.register(this);
+        }
+
+        mDefaultVoiceSubId = new WatchedInt(Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.MULTI_SIM_VOICE_CALL_SUBSCRIPTION,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID)) {
+            @Override
+            public boolean set(int newValue) {
+                int oldValue = mValue;
+                if (super.set(newValue)) {
+                    logl("Default voice sub changed from " + oldValue + " to " + newValue);
+                    Settings.Global.putInt(mContext.getContentResolver(),
+                            Settings.Global.MULTI_SIM_VOICE_CALL_SUBSCRIPTION, newValue);
+                    return true;
+                }
+                return false;
+            }
+        };
+
+        mDefaultDataSubId = new WatchedInt(Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.MULTI_SIM_DATA_CALL_SUBSCRIPTION,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID)) {
+            @Override
+            public boolean set(int newValue) {
+                int oldValue = mValue;
+                if (super.set(newValue)) {
+                    logl("Default data sub changed from " + oldValue + " to " + newValue);
+                    Settings.Global.putInt(mContext.getContentResolver(),
+                            Settings.Global.MULTI_SIM_DATA_CALL_SUBSCRIPTION, newValue);
+                    return true;
+                }
+                return false;
+            }
+        };
+
+        mDefaultSmsSubId = new WatchedInt(Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.MULTI_SIM_SMS_SUBSCRIPTION,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID)) {
+            @Override
+            public boolean set(int newValue) {
+                int oldValue = mValue;
+                if (super.set(newValue)) {
+                    logl("Default voice sms changed from " + oldValue + " to " + newValue);
+                    Settings.Global.putInt(mContext.getContentResolver(),
+                            Settings.Global.MULTI_SIM_SMS_SUBSCRIPTION, newValue);
+                    return true;
+                }
+                return false;
+            }
+        };
+
+        mDefaultSubId = new WatchedInt(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+
+        mSimState = new int[mTelephonyManager.getSupportedModemCount()];
+        Arrays.fill(mSimState, TelephonyManager.SIM_STATE_UNKNOWN);
+
+        // Create a separate thread for subscription database manager. The database will be updated
+        // from a different thread.
+        HandlerThread handlerThread = new HandlerThread(LOG_TAG);
+        handlerThread.start();
+        mSubscriptionDatabaseManager = new SubscriptionDatabaseManager(context,
+                handlerThread.getLooper(), new SubscriptionDatabaseManagerCallback(mHandler::post) {
+                    /**
+                     * Called when database has been loaded into the cache.
+                     */
+                    @Override
+                    public void onDatabaseLoaded() {
+                        log("Subscription database has been loaded.");
+                        for (int phoneId = 0; phoneId < mTelephonyManager.getActiveModemCount()
+                                ; phoneId++) {
+                            markSubscriptionsInactive(phoneId);
+                        }
+                    }
+
+                    /**
+                     * Called when subscription changed.
+                     *
+                     * @param subId The subscription id.
+                     */
+                    @Override
+                    public void onSubscriptionChanged(int subId) {
+                        mSubscriptionManagerServiceCallbacks.forEach(
+                                callback -> callback.invokeFromExecutor(
+                                        () -> callback.onSubscriptionChanged(subId)));
+
+                        MultiSimSettingController.getInstance().notifySubscriptionInfoChanged();
+
+                        TelephonyRegistryManager telephonyRegistryManager =
+                                mContext.getSystemService(TelephonyRegistryManager.class);
+                        if (telephonyRegistryManager != null) {
+                            telephonyRegistryManager.notifySubscriptionInfoChanged();
+                        }
+
+                        SubscriptionInfoInternal subInfo =
+                                mSubscriptionDatabaseManager.getSubscriptionInfoInternal(subId);
+                        if (subInfo != null && subInfo.isOpportunistic()
+                                && telephonyRegistryManager != null) {
+                            telephonyRegistryManager.notifyOpportunisticSubscriptionInfoChanged();
+                        }
+
+                        // TODO: Call TelephonyMetrics.updateActiveSubscriptionInfoList when active
+                        //  subscription changes.
+                    }
+
+                    /**
+                     * Called when {@link SubscriptionInfoInternal#areUiccApplicationsEnabled()}
+                     * changed.
+                     *
+                     * @param subId The subscription id.
+                     */
+                    @Override
+                    public void onUiccApplicationsEnabled(int subId) {
+                        log("onUiccApplicationsEnabled: subId=" + subId);
+                        mSubscriptionManagerServiceCallbacks.forEach(
+                                callback -> callback.invokeFromExecutor(
+                                        () -> callback.onUiccApplicationsEnabled(subId)));
+                    }
+                });
+
+        updateDefaultSubId();
+
+        mHandler.post(() -> {
+            // EuiccController is created after SubscriptionManagerService. So we need to get
+            // the instance later in the handler.
+            if (mContext.getPackageManager().hasSystemFeature(
+                    PackageManager.FEATURE_TELEPHONY_EUICC)) {
+                mEuiccController = EuiccController.get();
+            }
+        });
+    }
+
+    /**
+     * @return The singleton instance of {@link SubscriptionManagerService}.
+     */
+    @NonNull
+    public static SubscriptionManagerService getInstance() {
+        return sInstance;
+    }
+
+    /**
+     * Check if the calling package can manage the subscription group.
+     *
+     * @param groupUuid a UUID assigned to the subscription group.
+     * @param callingPackage the package making the IPC.
+     *
+     * @return {@code true} if calling package is the owner of or has carrier privileges for all
+     * subscriptions in the group.
+     */
+    private boolean canPackageManageGroup(@NonNull ParcelUuid groupUuid,
+            @NonNull String callingPackage) {
+        if (groupUuid == null) {
+            throw new IllegalArgumentException("Invalid groupUuid");
+        }
+
+        if (TextUtils.isEmpty(callingPackage)) {
+            throw new IllegalArgumentException("Empty callingPackage");
+        }
+
+        List<SubscriptionInfo> infoList;
+
+        // Getting all subscriptions in the group.
+        infoList = mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                .filter(subInfo -> subInfo.getGroupUuid().equals(groupUuid.toString()))
+                .map(SubscriptionInfoInternal::toSubscriptionInfo)
+                .collect(Collectors.toList());
+
+        // If the group does not exist, then by default the UUID is up for grabs so no need to
+        // restrict management of a group (that someone may be attempting to create).
+        if (ArrayUtils.isEmpty(infoList)) {
+            return true;
+        }
+
+        // If the calling package is the group owner, skip carrier permission check and return
+        // true as it was done before.
+        if (callingPackage.equals(infoList.get(0).getGroupOwner())) return true;
+
+        // Check carrier privilege for all subscriptions in the group.
+        return (checkCarrierPrivilegeOnSubList(infoList.stream()
+                .mapToInt(SubscriptionInfo::getSubscriptionId).toArray(), callingPackage));
+    }
+
+    /**
+     * Helper function to check if the caller has carrier privilege permissions on a list of subId.
+     * The check can either be processed against access rules on currently active SIM cards, or
+     * the access rules we keep in our database for currently inactive SIMs.
+     *
+     * @param subIdList List of subscription ids.
+     * @param callingPackage The package making the call.
+     *
+     * @throws IllegalArgumentException if the some subId is invalid or doesn't exist.
+     *
+     * @return {@code true} if checking passes on all subId, {@code false} otherwise.
+     */
+    private boolean checkCarrierPrivilegeOnSubList(@NonNull int[] subIdList,
+            @NonNull String callingPackage) {
+        for (int subId : subIdList) {
+            SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                    .getSubscriptionInfoInternal(subId);
+            if (subInfo == null) {
+                loge("checkCarrierPrivilegeOnSubList: subId " + subId + " does not exist.");
+                return false;
+            }
+
+            if (subInfo.isActive()) {
+                if (!mTelephonyManager.hasCarrierPrivileges(subId)) {
+                    loge("checkCarrierPrivilegeOnSubList: Does not have carrier privilege on sub "
+                            + subId);
+                    return false;
+                }
+            } else {
+                if (!mSubscriptionManager.canManageSubscription(subInfo.toSubscriptionInfo(),
+                        callingPackage)) {
+                    loge("checkCarrierPrivilegeOnSubList: cannot manage sub " + subId);
+                    return false;
+                }
+            }
+        }
+
+        return true;
+    }
+
+    /**
+     * Sync the settings from specified subscription to all grouped subscriptions.
+     *
+     * @param subId The subscription id of the referenced subscription.
+     */
+    public void syncGroupedSetting(int subId) {
+        mHandler.post(() -> {
+            SubscriptionInfoInternal reference = mSubscriptionDatabaseManager
+                    .getSubscriptionInfoInternal(subId);
+            if (reference == null) {
+                loge("syncSettings: Can't find subscription info for sub " + subId);
+                return;
+            }
+
+            mSubscriptionDatabaseManager.syncToGroup(subId);
+        });
+    }
+
+    /**
+     * Check whether the {@code callingPackage} has access to the phone number on the specified
+     * {@code subId} or not.
+     *
+     * @param subId The subscription id.
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     * @param message Message to include in the exception or NoteOp.
+     *
+     * @return {@code true} if the caller has phone number access.
+     */
+    private boolean hasPhoneNumberAccess(int subId, @NonNull String callingPackage,
+            @Nullable String callingFeatureId, @Nullable String message) {
+        try {
+            return TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(mContext, subId,
+                    callingPackage, callingFeatureId, message);
+        } catch (SecurityException e) {
+            return false;
+        }
+    }
+
+    /**
+     * Check whether the {@code callingPackage} has access to subscriber identifiers on the
+     * specified {@code subId} or not.
+     *
+     * @param subId The subscription id.
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     * @param message Message to include in the exception or NoteOp.
+     * @param reportFailure Indicates if failure should be reported.
+     *
+     * @return {@code true} if the caller has identifier access.
+     */
+    private boolean hasSubscriberIdentifierAccess(int subId, @NonNull String callingPackage,
+            @Nullable String callingFeatureId, @Nullable String message, boolean reportFailure) {
+        try {
+            return TelephonyPermissions.checkCallingOrSelfReadSubscriberIdentifiers(mContext, subId,
+                    callingPackage, callingFeatureId, message, reportFailure);
+        } catch (SecurityException e) {
+            // A SecurityException indicates that the calling package is targeting at least the
+            // minimum level that enforces identifier access restrictions and the new access
+            // requirements are not met.
+            return false;
+        }
+    }
+
+    /**
+     * Conditionally removes identifiers from the provided {@link SubscriptionInfo} if the {@code
+     * callingPackage} does not meet the access requirements for identifiers and returns the
+     * potentially modified object.
+     *
+     * <p>
+     * If the caller does not have {@link Manifest.permission#READ_PHONE_NUMBERS} permission,
+     * {@link SubscriptionInfo#getNumber()} will return empty string.
+     * If the caller does not have {@link Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER},
+     * {@link SubscriptionInfo#getIccId()} and {@link SubscriptionInfo#getCardString()} will return
+     * empty string, and {@link SubscriptionInfo#getGroupUuid()} will return {@code null}.
+     *
+     * @param subInfo The subscription info.
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     * @param message Message to include in the exception or NoteOp.
+     *
+     * @return The modified {@link SubscriptionInfo} depending on caller's permission.
+     */
+    @NonNull
+    private SubscriptionInfo conditionallyRemoveIdentifiers(@NonNull SubscriptionInfo subInfo,
+            @NonNull String callingPackage, @Nullable String callingFeatureId,
+            @Nullable String message) {
+        int subId = subInfo.getSubscriptionId();
+        boolean hasIdentifierAccess = hasSubscriberIdentifierAccess(subId, callingPackage,
+                callingFeatureId, message, true);
+        boolean hasPhoneNumberAccess = hasPhoneNumberAccess(subId, callingPackage,
+                callingFeatureId, message);
+
+        if (hasIdentifierAccess && hasPhoneNumberAccess) {
+            return subInfo;
+        }
+
+        SubscriptionInfo.Builder result = new SubscriptionInfo.Builder(subInfo);
+        if (!hasIdentifierAccess) {
+            result.setIccId(null);
+            result.setCardString(null);
+            result.setGroupUuid(null);
+        }
+
+        if (!hasPhoneNumberAccess) {
+            result.setNumber(null);
+        }
+        return result.build();
+    }
+
+    /**
+     * @return The list of ICCIDs from the inserted physical SIMs.
+     */
+    @NonNull
+    private List<String> getIccIdsOfInsertedPhysicalSims() {
+        List<String> iccidList = new ArrayList<>();
+        UiccSlot[] uiccSlots = mUiccController.getUiccSlots();
+        if (uiccSlots == null) return iccidList;
+
+        for (UiccSlot uiccSlot : uiccSlots) {
+            if (uiccSlot != null && uiccSlot.getCardState() != null
+                    && uiccSlot.getCardState().isCardPresent() && !uiccSlot.isEuicc()) {
+                // Non euicc slots will have single port, so use default port index.
+                String iccId = uiccSlot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
+                if (!TextUtils.isEmpty(iccId)) {
+                    iccidList.add(IccUtils.stripTrailingFs(iccId));
+                }
+            }
+        }
+
+        return iccidList;
+    }
+
+    /**
+     * Set the subscription carrier id.
+     *
+     * @param subId Subscription id.
+     * @param carrierId The carrier id.
+     *
+     * @throws IllegalArgumentException if {@code subId} is invalid or the subscription does not
+     * exist.
+     *
+     * @see TelephonyManager#getSimCarrierId()
+     */
+    public void setCarrierId(int subId, int carrierId) {
+        // This can throw IllegalArgumentException if the subscription does not exist.
+        try {
+            mSubscriptionDatabaseManager.setCarrierId(subId, carrierId);
+        } catch (IllegalArgumentException e) {
+            loge("setCarrierId: invalid subId=" + subId);
+        }
+    }
+
+    /**
+     * Set MCC/MNC by subscription id.
+     *
+     * @param mccMnc MCC/MNC associated with the subscription.
+     * @param subId The subscription id.
+     */
+    public void setMccMnc(int subId, @NonNull String mccMnc) {
+        // This can throw IllegalArgumentException if the subscription does not exist.
+        try {
+            mSubscriptionDatabaseManager.setMcc(subId, mccMnc.substring(0, 3));
+            mSubscriptionDatabaseManager.setMnc(subId, mccMnc.substring(3));
+        } catch (IllegalArgumentException e) {
+            loge("setMccMnc: invalid subId=" + subId);
+        }
+    }
+
+    /**
+     * Set ISO country code by subscription id.
+     *
+     * @param iso ISO country code associated with the subscription.
+     * @param subId The subscription id.
+     */
+    public void setCountryIso(int subId, @NonNull String iso) {
+        // This can throw IllegalArgumentException if the subscription does not exist.
+        try {
+            mSubscriptionDatabaseManager.setCountryIso(subId, iso);
+        } catch (IllegalArgumentException e) {
+            loge("setCountryIso: invalid subId=" + subId);
+        }
+    }
+
+    /**
+     * Set the name displayed to the user that identifies subscription provider name. This name
+     * is the SPN displayed in status bar and many other places. Can't be renamed by the user.
+     *
+     * @param subId Subscription id.
+     * @param carrierName The carrier name.
+     */
+    public void setCarrierName(int subId, @NonNull String carrierName) {
+        // This can throw IllegalArgumentException if the subscription does not exist.
+        try {
+            mSubscriptionDatabaseManager.setCarrierName(subId, carrierName);
+        } catch (IllegalArgumentException e) {
+            loge("setCarrierName: invalid subId=" + subId);
+        }
+    }
+
+    /**
+     * Set last used TP message reference.
+     *
+     * @param subId Subscription id.
+     * @param lastUsedTPMessageReference Last used TP message reference.
+     */
+    public void setLastUsedTPMessageReference(int subId, int lastUsedTPMessageReference) {
+        // This can throw IllegalArgumentException if the subscription does not exist.
+        try {
+            mSubscriptionDatabaseManager.setLastUsedTPMessageReference(
+                    subId, lastUsedTPMessageReference);
+        } catch (IllegalArgumentException e) {
+            loge("setLastUsedTPMessageReference: invalid subId=" + subId);
+        }
+    }
+
+    /**
+     * Set the enabled mobile data policies.
+     *
+     * @param subId Subscription id.
+     * @param enabledMobileDataPolicies The enabled mobile data policies.
+     */
+    public void setEnabledMobileDataPolicies(int subId, @NonNull String enabledMobileDataPolicies) {
+        // This can throw IllegalArgumentException if the subscription does not exist.
+        try {
+            mSubscriptionDatabaseManager.setEnabledMobileDataPolicies(
+                    subId, enabledMobileDataPolicies);
+        } catch (IllegalArgumentException e) {
+            loge("setEnabledMobileDataPolicies: invalid subId=" + subId);
+        }
+    }
+
+    /**
+     * Set the phone number retrieved from IMS.
+     *
+     * @param subId Subscription id.
+     * @param numberFromIms The phone number retrieved from IMS.
+     */
+    public void setNumberFromIms(int subId, @NonNull String numberFromIms) {
+        // This can throw IllegalArgumentException if the subscription does not exist.
+        try {
+            mSubscriptionDatabaseManager.setNumberFromIms(subId, numberFromIms);
+        } catch (IllegalArgumentException e) {
+            loge("setNumberFromIms: invalid subId=" + subId);
+        }
+    }
+
+    /**
+     * Mark all subscriptions on this SIM slot index inactive.
+     *
+     * @param simSlotIndex The logical SIM slot index (i.e. phone id).
+     */
+    public void markSubscriptionsInactive(int simSlotIndex) {
+        mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                .filter(subInfo -> subInfo.getSimSlotIndex() == simSlotIndex)
+                .forEach(subInfo -> {
+                    mSubscriptionDatabaseManager.setSimSlotIndex(subInfo.getSubscriptionId(),
+                            SubscriptionManager.INVALID_SIM_SLOT_INDEX);
+                    mSlotIndexToSubId.remove(simSlotIndex);
+                });
+    }
+
+    /**
+     * This is only for internal use and the returned priority is arbitrary. The idea is to give a
+     * higher value to name source that has higher priority to override other name sources.
+     *
+     * @param nameSource Source of display name.
+     *
+     * @return The priority. Higher value means higher priority.
+     */
+    private static int getNameSourcePriority(@SimDisplayNameSource int nameSource) {
+        int index = Arrays.asList(
+                SubscriptionManager.NAME_SOURCE_UNKNOWN,
+                SubscriptionManager.NAME_SOURCE_CARRIER_ID,
+                SubscriptionManager.NAME_SOURCE_SIM_PNN,
+                SubscriptionManager.NAME_SOURCE_SIM_SPN,
+                SubscriptionManager.NAME_SOURCE_CARRIER,
+                SubscriptionManager.NAME_SOURCE_USER_INPUT // user has highest priority.
+        ).indexOf(nameSource);
+        return Math.max(0, index);
+    }
+
+    /**
+     * Get the embedded profile port index by ICCID.
+     *
+     * @param iccId The ICCID.
+     * @return The port index.
+     */
+    private int getEmbeddedProfilePortIndex(String iccId) {
+        UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
+        for (UiccSlot slot : slots) {
+            if (slot != null && slot.isEuicc()
+                    && slot.getPortIndexFromIccId(iccId) != TelephonyManager.INVALID_PORT_INDEX) {
+                return slot.getPortIndexFromIccId(iccId);
+            }
+        }
+        return TelephonyManager.INVALID_PORT_INDEX;
+    }
+
+    /**
+     * Pull the embedded subscription from {@link EuiccController} for the eUICC with the given list
+     * of card IDs {@code cardIds}.
+     *
+     * @param cardIds The card ids of the embedded subscriptions.
+     * @param callback Callback to be called upon completion.
+     */
+    public void updateEmbeddedSubscriptions(@NonNull List<Integer> cardIds,
+            @Nullable Runnable callback) {
+        mHandler.post(() -> {
+            // Do nothing if eUICCs are disabled. (Previous entries may remain in the cache, but
+            // they are filtered out of list calls as long as EuiccManager.isEnabled returns false).
+            if (mEuiccManager == null || !mEuiccManager.isEnabled()) {
+                loge("updateEmbeddedSubscriptions: eUICC not enabled");
+                if (callback != null) {
+                    callback.run();
+                }
+                return;
+            }
+
+            log("updateEmbeddedSubscriptions: start to get euicc profiles.");
+            for (int cardId : cardIds) {
+                GetEuiccProfileInfoListResult result = mEuiccController
+                        .blockingGetEuiccProfileInfoList(cardId);
+                log("updateEmbeddedSubscriptions: cardId=" + cardId + ", result=" + result);
+
+                if (result.getResult() != EuiccService.RESULT_OK) {
+                    loge("Failed to get euicc profile info. result="
+                            + EuiccService.resultToString(result.getResult()));
+                    continue;
+                }
+
+                if (result.getProfiles() == null || result.getProfiles().isEmpty()) {
+                    loge("No profiles returned.");
+                    continue;
+                }
+
+                final boolean isRemovable = result.getIsRemovable();
+
+                for (EuiccProfileInfo embeddedProfile : result.getProfiles()) {
+                    SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                            .getSubscriptionInfoInternalByIccId(embeddedProfile.getIccid());
+
+                    // The subscription does not exist in the database. Insert a new one here.
+                    if (subInfo == null) {
+                        subInfo = new SubscriptionInfoInternal.Builder()
+                                .setIccId(embeddedProfile.getIccid())
+                                .build();
+                        int subId = mSubscriptionDatabaseManager.insertSubscriptionInfo(subInfo);
+                        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                                .setId(subId).build();
+                    }
+
+                    int nameSource = subInfo.getDisplayNameSource();
+                    int carrierId = subInfo.getCarrierId();
+
+                    SubscriptionInfoInternal.Builder builder = new SubscriptionInfoInternal
+                            .Builder(subInfo);
+
+                    builder.setEmbedded(1);
+
+                    List<UiccAccessRule> ruleList = embeddedProfile.getUiccAccessRules();
+                    if (ruleList != null && !ruleList.isEmpty()) {
+                        builder.setNativeAccessRules(embeddedProfile.getUiccAccessRules());
+                    }
+                    builder.setRemovableEmbedded(isRemovable);
+
+                    // override DISPLAY_NAME if the priority of existing nameSource is <= carrier
+                    if (getNameSourcePriority(nameSource) <= getNameSourcePriority(
+                            SubscriptionManager.NAME_SOURCE_CARRIER)) {
+                        builder.setDisplayName(embeddedProfile.getNickname());
+                        builder.setDisplayNameSource(SubscriptionManager.NAME_SOURCE_CARRIER);
+                    }
+                    builder.setProfileClass(embeddedProfile.getProfileClass());
+                    builder.setPortIndex(getEmbeddedProfilePortIndex(embeddedProfile.getIccid()));
+
+                    CarrierIdentifier cid = embeddedProfile.getCarrierIdentifier();
+                    if (cid != null) {
+                        // Due to the limited subscription information, carrier id identified here
+                        // might not be accurate compared with CarrierResolver. Only update carrier
+                        // id if there is no valid carrier id present.
+                        if (carrierId == TelephonyManager.UNKNOWN_CARRIER_ID) {
+                            builder.setCarrierId(CarrierResolver
+                                    .getCarrierIdFromIdentifier(mContext, cid));
+                        }
+                        String mcc = cid.getMcc();
+                        String mnc = cid.getMnc();
+                        builder.setMcc(mcc);
+                        builder.setMnc(mnc);
+                    }
+                    // If cardId = unsupported or un-initialized, we have no reason to update DB.
+                    // Additionally, if the device does not support cardId for default eUICC, the
+                    // CARD_ID field should not contain the EID
+                    if (cardId >= 0 && mUiccController.getCardIdForDefaultEuicc()
+                            != TelephonyManager.UNSUPPORTED_CARD_ID) {
+                        builder.setCardString(mUiccController.convertToCardString(cardId));
+                    }
+
+                    subInfo = builder.build();
+                    log("updateEmbeddedSubscriptions: update subscription " + subInfo);
+                    mSubscriptionDatabaseManager.updateSubscription(subInfo);
+                }
+            }
+        });
+        log("updateEmbeddedSubscriptions: Finished embedded subscription update.");
+        if (callback != null) {
+            callback.run();
+        }
+    }
+
+    /**
+     * Check if the SIM application is enabled on the card or not.
+     *
+     * @param phoneId The phone id.
+     *
+     * @return {@code true} if the application is enabled.
+     */
+    public boolean areUiccAppsEnabledOnCard(int phoneId) {
+        // When uicc apps are disabled(supported in IRadio 1.5), we will still get IccId from
+        // cardStatus (since IRadio 1.2). And upon cardStatus change we'll receive another
+        // handleSimNotReady so this will be evaluated again.
+        UiccSlot slot = mUiccController.getUiccSlotForPhone(phoneId);
+        if (slot == null) return false;
+        UiccPort port = mUiccController.getUiccPort(phoneId);
+        String iccId = (port == null) ? null : port.getIccId();
+        if (iccId == null) {
+            return false;
+        }
+
+        SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                .getSubscriptionInfoInternalByIccId(IccUtils.stripTrailingFs(iccId));
+        return subInfo != null && subInfo.areUiccApplicationsEnabled();
+    }
+
+    /**
+     * Get ICCID by phone id.
+     *
+     * @param phoneId The phone id (i.e. Logical SIM slot index.)
+     *
+     * @return The ICCID. Empty string if not available.
+     */
+    @NonNull
+    private String getIccId(int phoneId) {
+        UiccPort port = mUiccController.getUiccPort(phoneId);
+        return (port == null) ? "" : TextUtils.emptyIfNull(
+                IccUtils.stripTrailingFs(port.getIccId()));
+    }
+
+    /**
+     * Update the subscriptions on the logical SIM slot index (i.e. phone id).
+     *
+     * @param phoneId The phone id (i.e. Logical SIM slot index)
+     */
+    private void updateSubscriptions(int phoneId) {
+        int simState = mSimState[phoneId];
+        log("updateSubscriptions: phoneId=" + phoneId + ", simState="
+                + TelephonyManager.simStateToString(simState));
+        if (simState == TelephonyManager.SIM_STATE_ABSENT) {
+            if (mSlotIndexToSubId.containsKey(phoneId)) {
+                // Re-enable the SIM when it's removed, so it will be in enabled state when it gets
+                // re-inserted again. (pre-U behavior)
+                mSubscriptionDatabaseManager.setUiccApplicationsEnabled(
+                        mSlotIndexToSubId.get(phoneId), true);
+                // When sim is absent, set the port index to invalid port index. (pre-U behavior)
+                mSubscriptionDatabaseManager.setPortIndex(mSlotIndexToSubId.get(phoneId),
+                        TelephonyManager.INVALID_PORT_INDEX);
+            }
+        } else if (simState == TelephonyManager.SIM_STATE_NOT_READY) {
+            // Check if this is the final state. Only update the subscription if NOT_READY is a
+            // final state.
+            IccCard iccCard = PhoneFactory.getPhone(phoneId).getIccCard();
+            if (!iccCard.isEmptyProfile() && areUiccAppsEnabledOnCard(phoneId)) {
+                log("updateSubscriptions: SIM_STATE_NOT_READY is not a final state. Will update "
+                        + "subscription later.");
+                return;
+            }
+        }
+
+        String iccId = getIccId(phoneId);
+        // Loop through all the subscriptions. If we found any ICCID matched, apply the right
+        // logical index to that.
+        int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+        for (SubscriptionInfoInternal subInfo
+                : mSubscriptionDatabaseManager.getAllSubscriptions()) {
+            int simSlotIndex = SubscriptionManager.INVALID_SIM_SLOT_INDEX;
+            if (!TextUtils.isEmpty(iccId) && subInfo.getIccId().equals(iccId)) {
+                subId = subInfo.getSubscriptionId();
+                simSlotIndex = phoneId;
+                mSlotIndexToSubId.put(simSlotIndex, subId);
+                logl("updateSubscriptions: Found sub " + subInfo.getSubscriptionId()
+                        + ", phoneId=" + phoneId);
+            }
+            mSubscriptionDatabaseManager.setSimSlotIndex(subInfo.getSubscriptionId(), simSlotIndex);
+        }
+
+        if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+            logl("updateSubscriptions: Did not find any subscription. phoneId=" + phoneId);
+            mSlotIndexToSubId.remove(phoneId);
+        }
+
+        if (!TextUtils.isEmpty(iccId)) {
+            // Check if the subscription already existed.
+            SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                    .getSubscriptionInfoInternalByIccId(iccId);
+            if (subInfo == null) {
+                // This is a new SIM card. Insert a new record.
+                subId = mSubscriptionDatabaseManager.insertSubscriptionInfo(
+                        new SubscriptionInfoInternal.Builder()
+                                .setIccId(iccId)
+                                .setSimSlotIndex(phoneId)
+                                .build());
+                logl("updateSubscriptions: Inserted a new subscription. subId=" + subId
+                        + ", phoneId=" + phoneId);
+            } else {
+                subId = subInfo.getSubscriptionId();
+            }
+
+            // Update the SIM slot index. This will make the subscription active.
+            mSubscriptionDatabaseManager.setSimSlotIndex(subId, phoneId);
+
+            // Update the card id.
+            UiccCard card = mUiccController.getUiccCardForPhone(phoneId);
+            if (card != null) {
+                String cardId = card.getCardId();
+                if (cardId != null) {
+                    mSubscriptionDatabaseManager.setCardString(subId, cardId);
+                }
+            }
+
+            // Update the port index.
+            UiccSlot slot = mUiccController.getUiccSlotForPhone(phoneId);
+            if (slot != null && !slot.isEuicc()) {
+                int portIndex = slot.getPortIndexFromIccId(iccId);
+                mSubscriptionDatabaseManager.setPortIndex(subId, portIndex);
+            }
+        }
+
+        updateDefaultSubIds();
+    }
+
+    /**
+     * Calculate the usage setting based on the carrier request.
+     *
+     * @param currentUsageSetting the current setting in the subscription DB.
+     * @param preferredUsageSetting provided by the carrier config.
+     *
+     * @return the calculated usage setting.
+     */
+    @VisibleForTesting
+    @UsageSetting public int calculateUsageSetting(@UsageSetting int currentUsageSetting,
+            @UsageSetting int preferredUsageSetting) {
+        int[] supportedUsageSettings;
+
+        //  Load the resources to provide the device capability
+        try {
+            supportedUsageSettings = mContext.getResources().getIntArray(
+                    com.android.internal.R.array.config_supported_cellular_usage_settings);
+            // If usage settings are not supported, return the default setting, which is UNKNOWN.
+            if (supportedUsageSettings == null
+                    || supportedUsageSettings.length < 1) return currentUsageSetting;
+        } catch (Resources.NotFoundException nfe) {
+            loge("calculateUsageSetting: Failed to load usage setting resources!");
+            return currentUsageSetting;
+        }
+
+        // If the current setting is invalid, including the first time the value is set,
+        // update it to default (this will trigger a change in the DB).
+        if (currentUsageSetting < SubscriptionManager.USAGE_SETTING_DEFAULT
+                || currentUsageSetting > SubscriptionManager.USAGE_SETTING_DATA_CENTRIC) {
+            log("calculateUsageSetting: Updating usage setting for current subscription");
+            currentUsageSetting = SubscriptionManager.USAGE_SETTING_DEFAULT;
+        }
+
+        // Range check the inputs, and on failure, make no changes
+        if (preferredUsageSetting < SubscriptionManager.USAGE_SETTING_DEFAULT
+                || preferredUsageSetting > SubscriptionManager.USAGE_SETTING_DATA_CENTRIC) {
+            loge("calculateUsageSetting: Invalid usage setting!" + preferredUsageSetting);
+            return currentUsageSetting;
+        }
+
+        // Default is always allowed
+        if (preferredUsageSetting == SubscriptionManager.USAGE_SETTING_DEFAULT) {
+            return preferredUsageSetting;
+        }
+
+        // Forced setting must be explicitly supported
+        for (int supportedUsageSetting : supportedUsageSettings) {
+            if (preferredUsageSetting == supportedUsageSetting) return preferredUsageSetting;
+        }
+
+        // If the preferred setting is not possible, just keep the current setting.
+        return currentUsageSetting;
+    }
+
+    /**
+     * Called by CarrierConfigLoader to update the subscription before sending a broadcast.
+     */
+    public void updateSubscriptionByCarrierConfig(int phoneId, @NonNull String configPackageName,
+            @NonNull PersistableBundle config, @NonNull Runnable callback) {
+        mHandler.post(() -> {
+            updateSubscriptionByCarrierConfigInternal(phoneId, configPackageName, config);
+            callback.run();
+        });
+    }
+
+    private void updateSubscriptionByCarrierConfigInternal(int phoneId,
+            @NonNull String configPackageName, @NonNull PersistableBundle config) {
+        log("updateSubscriptionByCarrierConfig: phoneId=" + phoneId + ", configPackageName="
+                + configPackageName);
+        if (!SubscriptionManager.isValidPhoneId(phoneId)
+                || TextUtils.isEmpty(configPackageName) || config == null) {
+            loge("updateSubscriptionByCarrierConfig: Failed to update the subscription. phoneId="
+                    + phoneId + " configPackageName=" + configPackageName + " config="
+                        + ((config == null) ? "null" : config.hashCode()));
+            return;
+        }
+
+        if (!mSlotIndexToSubId.containsKey(phoneId)) {
+            log("updateSubscriptionByCarrierConfig: No subscription is active for phone being "
+                    + "updated.");
+            return;
+        }
+
+        int subId = mSlotIndexToSubId.get(phoneId);
+
+        SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                .getSubscriptionInfoInternal(subId);
+        if (subInfo == null) {
+            loge("updateSubscriptionByCarrierConfig: Couldn't retrieve subscription info for "
+                    + "current subscription. subId=" + subId);
+            return;
+        }
+
+        ParcelUuid groupUuid;
+
+        // carrier certificates are not subscription-specific, so we want to load them even if
+        // this current package is not a CarrierServicePackage
+        String[] certs = config.getStringArray(
+                CarrierConfigManager.KEY_CARRIER_CERTIFICATE_STRING_ARRAY);
+        UiccAccessRule[] carrierConfigAccessRules = UiccAccessRule.decodeRulesFromCarrierConfig(
+                certs);
+        if (carrierConfigAccessRules != null) {
+            mSubscriptionDatabaseManager.setCarrierConfigAccessRules(
+                    subId, carrierConfigAccessRules);
+        }
+
+        boolean isOpportunistic = config.getBoolean(
+                CarrierConfigManager.KEY_IS_OPPORTUNISTIC_SUBSCRIPTION_BOOL,
+                subInfo.isOpportunistic());
+        mSubscriptionDatabaseManager.setOpportunistic(subId, isOpportunistic);
+
+        String groupUuidString = config.getString(
+                CarrierConfigManager.KEY_SUBSCRIPTION_GROUP_UUID_STRING, "");
+        String oldGroupUuidString = subInfo.getGroupUuid();
+        if (!TextUtils.isEmpty(groupUuidString)) {
+            try {
+                // Update via a UUID Structure to ensure consistent formatting
+                groupUuid = ParcelUuid.fromString(groupUuidString);
+                if (groupUuidString.equals(CarrierConfigManager.REMOVE_GROUP_UUID_STRING)) {
+                    // Remove the group UUID.
+                    mSubscriptionDatabaseManager.setGroupUuid(subId, "");
+                } else if (canPackageManageGroup(groupUuid, configPackageName)) {
+                    mSubscriptionDatabaseManager.setGroupUuid(subId, groupUuidString);
+                    mSubscriptionDatabaseManager.setGroupOwner(subId, configPackageName);
+                    log("updateSubscriptionByCarrierConfig: Group added for sub " + subId);
+                } else {
+                    loge("updateSubscriptionByCarrierConfig: configPackageName "
+                            + configPackageName + " doesn't own groupUuid " + groupUuid);
+                }
+
+                if (!groupUuidString.equals(oldGroupUuidString)) {
+                    MultiSimSettingController.getInstance()
+                            .notifySubscriptionGroupChanged(groupUuid);
+                }
+            } catch (IllegalArgumentException e) {
+                loge("updateSubscriptionByCarrierConfig: Invalid Group UUID="
+                        + groupUuidString);
+            }
+        }
+
+        final int preferredUsageSetting = config.getInt(
+                CarrierConfigManager.KEY_CELLULAR_USAGE_SETTING_INT,
+                SubscriptionManager.USAGE_SETTING_UNKNOWN);
+
+        int newUsageSetting = calculateUsageSetting(
+                subInfo.getUsageSetting(), preferredUsageSetting);
+
+        if (newUsageSetting != subInfo.getUsageSetting()) {
+            mSubscriptionDatabaseManager.setUsageSetting(subId, newUsageSetting);
+            log("updateSubscriptionByCarrierConfig: UsageSetting changed,"
+                    + " oldSetting=" + SubscriptionManager.usageSettingToString(
+                            subInfo.getUsageSetting())
+                    + " preferredSetting=" + SubscriptionManager.usageSettingToString(
+                            preferredUsageSetting)
+                    + " newSetting=" + SubscriptionManager.usageSettingToString(newUsageSetting));
+        }
+    }
+
+    /**
+     * Get all subscription info records from SIMs that are inserted now or previously inserted.
+     *
+     * <p>
+     * If the caller does not have {@link Manifest.permission#READ_PHONE_NUMBERS} permission,
+     * {@link SubscriptionInfo#getNumber()} will return empty string.
+     * If the caller does not have {@link Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER},
+     * {@link SubscriptionInfo#getIccId()} and {@link SubscriptionInfo#getCardString()} will return
+     * empty string, and {@link SubscriptionInfo#getGroupUuid()} will return {@code null}.
+     *
+     * <p>
+     * The carrier app will only get the list of subscriptions that it has carrier privilege on,
+     * but will have non-stripped {@link SubscriptionInfo} in the list.
+     *
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return List of all {@link SubscriptionInfo} records from SIMs that are inserted or
+     * previously inserted. Sorted by {@link SubscriptionInfo#getSimSlotIndex()}, then
+     * {@link SubscriptionInfo#getSubscriptionId()}.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @NonNull
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_STATE,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public List<SubscriptionInfo> getAllSubInfoList(@NonNull String callingPackage,
+            @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        // Check if the caller has READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or carrier
+        // privilege on any active subscription. The carrier app will get full subscription infos
+        // on the subs it has carrier privilege.
+        if (!TelephonyPermissions.checkReadPhoneStateOnAnyActiveSub(mContext,
+                Binder.getCallingPid(), Binder.getCallingUid(), callingPackage, callingFeatureId,
+                "getAllSubInfoList")) {
+            throw new SecurityException("Need READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or "
+                    + "carrier privilege");
+        }
+
+        return mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                // callers have READ_PHONE_STATE or READ_PRIVILEGED_PHONE_STATE can get a full
+                // list. Carrier apps can only get the subscriptions they have privileged.
+                .filter(subInfo -> TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
+                        mContext, subInfo.getSubscriptionId(), callingPackage, callingFeatureId,
+                        "getAllSubInfoList"))
+                // Remove the identifier if the caller does not have sufficient permission.
+                // carrier apps will get full subscription info on the subscriptions associated
+                // to them.
+                .map(subInfo -> conditionallyRemoveIdentifiers(subInfo.toSubscriptionInfo(),
+                        callingPackage, callingFeatureId, "getAllSubInfoList"))
+                .sorted(Comparator.comparing(SubscriptionInfo::getSimSlotIndex)
+                        .thenComparing(SubscriptionInfo::getSubscriptionId))
+                .collect(Collectors.toList());
+    }
+
+    /**
+     * Get the active {@link SubscriptionInfo} with the subscription id key.
+     *
+     * @param subId The unique {@link SubscriptionInfo} key in database
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return The subscription info.
+     *
+     * @throws SecurityException if the caller does not have required permissions.
+     */
+    @Override
+    @Nullable
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_STATE,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public SubscriptionInfo getActiveSubscriptionInfo(int subId, @NonNull String callingPackage,
+            @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
+                callingFeatureId, "getActiveSubscriptionInfo")) {
+            throw new SecurityException("Need READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or "
+                    + "carrier privilege");
+        }
+
+        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
+            throw new IllegalArgumentException("Invalid sub id " + subId);
+        }
+
+        SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                .getSubscriptionInfoInternal(subId);
+        if (subInfo.isActive()) {
+            return conditionallyRemoveIdentifiers(subInfo.toSubscriptionInfo(), callingPackage,
+                    callingFeatureId, "getActiveSubscriptionInfo");
+        }
+        return null;
+    }
+
+    /**
+     * Get the active {@link SubscriptionInfo} associated with the iccId.
+     *
+     * @param iccId the IccId of SIM card
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return The subscription info.
+     *
+     * @throws SecurityException if the caller does not have required permissions.
+     */
+    @Override
+    @Nullable
+    @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
+    public SubscriptionInfo getActiveSubscriptionInfoForIccId(@NonNull String iccId,
+            @NonNull String callingPackage, @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        enforcePermissions("getActiveSubscriptionInfoForIccId",
+                Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            iccId = IccUtils.stripTrailingFs(iccId);
+            SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                    .getSubscriptionInfoInternalByIccId(iccId);
+
+            return (subInfo != null && subInfo.isActive()) ? subInfo.toSubscriptionInfo() : null;
+
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Get the active {@link SubscriptionInfo} associated with the logical SIM slot index.
+     *
+     * @param slotIndex the logical SIM slot index which the subscription is inserted.
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return {@link SubscriptionInfo}, null for Remote-SIMs or non-active logical SIM slot index.
+     *
+     * @throws SecurityException if the caller does not have required permissions.
+     */
+    @Override
+    @Nullable
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_STATE,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public SubscriptionInfo getActiveSubscriptionInfoForSimSlotIndex(int slotIndex,
+            @NonNull String callingPackage, @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        int subId = mSlotIndexToSubId.getOrDefault(slotIndex,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+
+        if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId,
+                callingPackage, callingFeatureId,
+                "getActiveSubscriptionInfoForSimSlotIndex")) {
+            throw new SecurityException("Need READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or "
+                    + "carrier privilege");
+
+        }
+
+        if (!SubscriptionManager.isValidSlotIndex(slotIndex)) {
+            throw new IllegalArgumentException("Invalid slot index " + slotIndex);
+        }
+
+        SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                .getSubscriptionInfoInternal(subId);
+        if (subInfo != null && subInfo.isActive()) {
+            return conditionallyRemoveIdentifiers(subInfo.toSubscriptionInfo(), callingPackage,
+                    callingFeatureId, "getActiveSubscriptionInfoForSimSlotIndex");
+        }
+
+        return null;
+    }
+
+    /**
+     * Get the SubscriptionInfo(s) of the active subscriptions. The records will be sorted
+     * by {@link SubscriptionInfo#getSimSlotIndex} then by
+     * {@link SubscriptionInfo#getSubscriptionId}.
+     *
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return Sorted list of the currently {@link SubscriptionInfo} records available on the
+     * device.
+     *
+     * @throws SecurityException if the caller does not have required permissions.
+     */
+    @Override
+    @NonNull
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_STATE,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public List<SubscriptionInfo> getActiveSubscriptionInfoList(@NonNull String callingPackage,
+            @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        // Check if the caller has READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or carrier
+        // privilege on any active subscription. The carrier app will get full subscription infos
+        // on the subs it has carrier privilege.
+        if (!TelephonyPermissions.checkReadPhoneStateOnAnyActiveSub(mContext,
+                Binder.getCallingPid(), Binder.getCallingUid(), callingPackage, callingFeatureId,
+                "getAllSubInfoList")) {
+            throw new SecurityException("Need READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or "
+                    + "carrier privilege");
+        }
+
+        return mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                .filter(SubscriptionInfoInternal::isActive)
+                // Remove the identifier if the caller does not have sufficient permission.
+                // carrier apps will get full subscription info on the subscriptions associated
+                // to them.
+                .map(subInfo -> conditionallyRemoveIdentifiers(subInfo.toSubscriptionInfo(),
+                        callingPackage, callingFeatureId, "getAllSubInfoList"))
+                .sorted(Comparator.comparing(SubscriptionInfo::getSimSlotIndex)
+                        .thenComparing(SubscriptionInfo::getSubscriptionId))
+                .collect(Collectors.toList());
+    }
+
+    /**
+     * Get the number of active {@link SubscriptionInfo}.
+     *
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return the number of active subscriptions.
+     *
+     * @throws SecurityException if the caller does not have required permissions.
+     */
+    @Override
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_STATE,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public int getActiveSubInfoCount(@NonNull String callingPackage,
+            @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        if (!TelephonyPermissions.checkReadPhoneStateOnAnyActiveSub(mContext,
+                Binder.getCallingPid(), Binder.getCallingUid(), callingPackage, callingFeatureId,
+                "getAllSubInfoList")) {
+            throw new SecurityException("Need READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or "
+                    + "carrier privilege");
+        }
+
+        final long token = Binder.clearCallingIdentity();
+        try {
+            return getActiveSubIdList(false).length;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * @return the maximum number of subscriptions this device will support at any one time.
+     */
+    @Override
+    public int getActiveSubInfoCountMax() {
+        return mTelephonyManager.getActiveModemCount();
+    }
+
+    /**
+     * Gets the SubscriptionInfo(s) of all available subscriptions, if any.
+     *
+     * Available subscriptions include active ones (those with a non-negative
+     * {@link SubscriptionInfo#getSimSlotIndex()}) as well as inactive but installed embedded
+     * subscriptions.
+     *
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return The available subscription info.
+     *
+     * @throws SecurityException if the caller does not have required permissions.
+     */
+    @Override
+    @NonNull
+    public List<SubscriptionInfo> getAvailableSubscriptionInfoList(@NonNull String callingPackage,
+            @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        enforcePermissions("getAvailableSubscriptionInfoList",
+                Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        // Now that all security checks pass, perform the operation as ourselves.
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            // Available eSIM profiles are reported by EuiccManager. However for physical SIMs if
+            // they are in inactive slot or programmatically disabled, they are still considered
+            // available. In this case we get their iccid from slot info and include their
+            // subscriptionInfos.
+            List<String> iccIds = getIccIdsOfInsertedPhysicalSims();
+
+            return mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                    .filter(subInfo -> subInfo.isActive() || iccIds.contains(subInfo.getIccId())
+                            || (mEuiccManager != null && mEuiccManager.isEnabled()
+                            && subInfo.isEmbedded()))
+                    .map(SubscriptionInfoInternal::toSubscriptionInfo)
+                    .sorted(Comparator.comparing(SubscriptionInfo::getSimSlotIndex)
+                            .thenComparing(SubscriptionInfo::getSubscriptionId))
+                    .collect(Collectors.toList());
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Gets the SubscriptionInfo(s) of all embedded subscriptions accessible to the calling app, if
+     * any.
+     *
+     * <p>Only those subscriptions for which the calling app has carrier privileges per the
+     * subscription metadata, if any, will be included in the returned list.
+     *
+     * <p>The records will be sorted by {@link SubscriptionInfo#getSimSlotIndex} then by
+     * {@link SubscriptionInfo#getSubscriptionId}.
+     *
+     * @return Sorted list of the current embedded {@link SubscriptionInfo} records available on the
+     * device which are accessible to the caller.
+     * <ul>
+     * <li>
+     *
+     * if the list is non-empty the list is sorted by {@link SubscriptionInfo#getSimSlotIndex}
+     * then by {@link SubscriptionInfo#getSubscriptionId}.
+     * </ul>
+     *
+     * @param callingPackage The package making the call.
+     *
+     * @throws SecurityException if the caller does not have required permissions.
+     */
+    @Override
+    public List<SubscriptionInfo> getAccessibleSubscriptionInfoList(
+            @NonNull String callingPackage) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        if (!mEuiccManager.isEnabled()) {
+            return null;
+        }
+
+        return mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                .map(SubscriptionInfoInternal::toSubscriptionInfo)
+                .filter(subInfo -> mSubscriptionManager
+                        .canManageSubscription(subInfo, callingPackage))
+                .sorted(Comparator.comparing(SubscriptionInfo::getSimSlotIndex)
+                        .thenComparing(SubscriptionInfo::getSubscriptionId))
+                .collect(Collectors.toList());
+    }
+
+    /**
+     * @see SubscriptionManager#requestEmbeddedSubscriptionInfoListRefresh
+     */
+    @Override
+    // TODO: Remove this after SubscriptionController is removed.
+    public void requestEmbeddedSubscriptionInfoListRefresh(int cardId) {
+        updateEmbeddedSubscriptions(List.of(cardId), null);
+    }
+
+    /**
+     * Add a new subscription info record, if needed. This should be only used for remote SIM.
+     *
+     * @param iccId ICCID of the SIM card.
+     * @param displayName human-readable name of the device the subscription corresponds to.
+     * @param slotIndex the logical SIM slot index assigned to this device.
+     * @param subscriptionType the type of subscription to be added
+     *
+     * @return 0 if success, < 0 on error
+     *
+     * @throws SecurityException if the caller does not have required permissions.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public int addSubInfo(@NonNull String iccId, @NonNull String displayName, int slotIndex,
+            @SubscriptionType int subscriptionType) {
+        log("addSubInfo: iccId=" + SubscriptionInfo.givePrintableIccid(iccId) + ", slotIndex="
+                + slotIndex + ", displayName=" + displayName + ", type="
+                + SubscriptionManager.subscriptionTypeToString(subscriptionType));
+        enforcePermissions("addSubInfo", Manifest.permission.MODIFY_PHONE_STATE);
+
+        // Now that all security checks passes, perform the operation as ourselves.
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            if (TextUtils.isEmpty(iccId)) {
+                loge("addSubInfo: null or empty iccId");
+                return -1;
+            }
+
+            iccId = IccUtils.stripTrailingFs(iccId);
+            SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                    .getSubscriptionInfoInternalByIccId(iccId);
+
+            // Check if the record exists or not.
+            if (subInfo == null) {
+                // Record does not exist.
+                if (mSlotIndexToSubId.containsKey(slotIndex)) {
+                    loge("Already a subscription on slot " + slotIndex);
+                    return -1;
+                }
+                int subId = mSubscriptionDatabaseManager.insertSubscriptionInfo(
+                        new SubscriptionInfoInternal.Builder()
+                                .setIccId(iccId)
+                                .setSimSlotIndex(slotIndex)
+                                .setDisplayName(displayName)
+                                .setType(subscriptionType)
+                                .build()
+                );
+                mSlotIndexToSubId.put(slotIndex, subId);
+            } else {
+                // Record already exists.
+                loge("Subscription record already existed.");
+                return -1;
+            }
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+        return 0;
+
+    }
+
+    /**
+     * Remove subscription info record from the subscription database.
+     *
+     * @param uniqueId This is the unique identifier for the subscription within the specific
+     * subscription type.
+     * @param subscriptionType the type of subscription to be removed.
+     *
+     * // TODO: Remove this terrible return value once SubscriptionController is removed.
+     * @return 0 if success, < 0 on error.
+     *
+     * @throws NullPointerException if {@code uniqueId} is {@code null}.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public int removeSubInfo(@NonNull String uniqueId, int subscriptionType) {
+        enforcePermissions("removeSubInfo", Manifest.permission.MODIFY_PHONE_STATE);
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                    .getSubscriptionInfoInternalByIccId(uniqueId);
+            if (subInfo == null) {
+                loge("Cannot find subscription with uniqueId " + uniqueId);
+                return -1;
+            }
+            if (subInfo.getSubscriptionType() != subscriptionType) {
+                loge("The subscription type does not match.");
+                return -1;
+            }
+            mSubscriptionDatabaseManager.removeSubscriptionInfo(subInfo.getSubscriptionId());
+            return 0;
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Set SIM icon tint color by simInfo index.
+     *
+     * @param subId the unique subscription index in database
+     * @param tint the icon tint color of the SIM
+     *
+     * @return the number of records updated
+     *
+     * @throws IllegalArgumentException if {@code subId} is invalid or the subscription does not
+     * exist.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public int setIconTint(int subId, @ColorInt int tint) {
+        enforcePermissions("setIconTint", Manifest.permission.MODIFY_PHONE_STATE);
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            if (!SubscriptionManager.isValidSubscriptionId(subId)) {
+                throw new IllegalArgumentException("Invalid sub id passed as parameter");
+            }
+
+            mSubscriptionDatabaseManager.setIconTint(subId, tint);
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Set display name of a subscription.
+     *
+     * @param displayName The display name of SIM card.
+     * @param subId The subscription id.
+     * @param nameSource The display name source.
+     *
+     * @return the number of records updated
+     *
+     * @throws IllegalArgumentException if {@code nameSource} is invalid, or {@code subId} is
+     * invalid.
+     * @throws NullPointerException if {@code displayName} is {@code null}.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public int setDisplayNameUsingSrc(@NonNull String displayName, int subId,
+            @SimDisplayNameSource int nameSource) {
+        enforcePermissions("setDisplayNameUsingSrc", Manifest.permission.MODIFY_PHONE_STATE);
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            Objects.requireNonNull(displayName, "setDisplayNameUsingSrc");
+
+            if (nameSource < SubscriptionManager.NAME_SOURCE_CARRIER_ID
+                    || nameSource > SubscriptionManager.NAME_SOURCE_SIM_PNN) {
+                throw new IllegalArgumentException("illegal name source " + nameSource);
+            }
+
+            SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                    .getSubscriptionInfoInternal(subId);
+
+            if (subInfo == null) {
+                throw new IllegalArgumentException("Cannot find subscription info with sub id "
+                        + subId);
+            }
+
+            if (getNameSourcePriority(subInfo.getDisplayNameSource())
+                    > getNameSourcePriority(nameSource)
+                    || (getNameSourcePriority(subInfo.getDisplayNameSource())
+                    == getNameSourcePriority(nameSource))
+                    && (TextUtils.equals(displayName, subInfo.getDisplayName()))) {
+                log("No need to update the display name. nameSource="
+                        + SubscriptionManager.displayNameSourceToString(nameSource)
+                        + ", existing name=" + subInfo.getDisplayName() + ", source="
+                        + SubscriptionManager.displayNameSourceToString(
+                                subInfo.getDisplayNameSource()));
+                return 0;
+            }
+
+            String nameToSet;
+            if (TextUtils.isEmpty(displayName) || displayName.trim().length() == 0) {
+                nameToSet = mTelephonyManager.getSimOperatorName(subId);
+                if (TextUtils.isEmpty(nameToSet)) {
+                    if (nameSource == SubscriptionManager.NAME_SOURCE_USER_INPUT
+                            && SubscriptionManager.isValidSlotIndex(getSlotIndex(subId))) {
+                        Resources r = Resources.getSystem();
+                        nameToSet = r.getString(R.string.default_card_name,
+                                (getSlotIndex(subId) + 1));
+                    } else {
+                        nameToSet = mContext.getString(SubscriptionManager.DEFAULT_NAME_RES);
+                    }
+                }
+            } else {
+                nameToSet = displayName;
+            }
+
+            mSubscriptionDatabaseManager.setDisplayName(subId, nameToSet);
+            mSubscriptionDatabaseManager.setDisplayNameSource(subId, nameSource);
+
+            // Update the nickname on the eUICC chip if it's an embedded subscription.
+            SubscriptionInfo sub = getSubscriptionInfo(subId);
+            if (sub != null && sub.isEmbedded()) {
+                int cardId = sub.getCardId();
+                log("Updating embedded sub nickname on cardId: " + cardId);
+                mEuiccManager.updateSubscriptionNickname(subId, nameToSet,
+                        // This PendingIntent simply fulfills the requirement to pass in a callback;
+                        // we don't care about the result (hence 0 requestCode and no action
+                        // specified on the intent).
+                        PendingIntent.getService(mContext, 0 /* requestCode */, new Intent(),
+                                PendingIntent.FLAG_IMMUTABLE /* flags */));
+            }
+
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Set phone number by subscription id.
+     *
+     * @param number the phone number of the SIM.
+     * @param subId the unique SubscriptionInfo index in database.
+     *
+     * @return the number of records updated.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     * @throws NullPointerException if {@code number} is {@code null}.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public int setDisplayNumber(@NonNull String number, int subId) {
+        enforcePermissions("setDisplayNumber", Manifest.permission.MODIFY_PHONE_STATE);
+
+        // Now that all security checks passes, perform the operation as ourselves.
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            mSubscriptionDatabaseManager.setDisplayName(subId, number);
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Set data roaming by simInfo index
+     *
+     * @param roaming 0:Don't allow data when roaming, 1:Allow data when roaming
+     * @param subId the unique SubscriptionInfo index in database
+     *
+     * @return the number of records updated
+     *
+     * @throws IllegalArgumentException if {@code subId} or {@code roaming} is not valid.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public int setDataRoaming(@DataRoamingMode int roaming, int subId) {
+        enforcePermissions("setDataRoaming", Manifest.permission.MODIFY_PHONE_STATE);
+
+        // Now that all security checks passes, perform the operation as ourselves.
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            if (roaming < 0) {
+                throw new IllegalArgumentException("Invalid roaming value " + roaming);
+            }
+
+            mSubscriptionDatabaseManager.setDataRoaming(subId, roaming);
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Switch to a certain subscription.
+     *
+     * @param opportunistic whether it’s opportunistic subscription
+     * @param subId the unique SubscriptionInfo index in database
+     * @param callingPackage The package making the call
+     *
+     * @return the number of records updated
+     *
+     * @throws IllegalArgumentException if {@code subId} is invalid.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(anyOf = {
+            Manifest.permission.MODIFY_PHONE_STATE,
+            "carrier privileges",
+    })
+    public int setOpportunistic(boolean opportunistic, int subId, @NonNull String callingPackage) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(
+                mContext, Binder.getCallingUid(), subId, true, "setOpportunistic",
+                Manifest.permission.MODIFY_PHONE_STATE);
+
+        long token = Binder.clearCallingIdentity();
+        try {
+            mSubscriptionDatabaseManager.setOpportunistic(subId, opportunistic);
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Inform SubscriptionManager that subscriptions in the list are bundled as a group. Typically
+     * it's a primary subscription and an opportunistic subscription. It should only affect
+     * multi-SIM scenarios where primary and opportunistic subscriptions can be activated together.
+     *
+     * Being in the same group means they might be activated or deactivated together, some of them
+     * may be invisible to the users, etc.
+     *
+     * Caller will either have {@link Manifest.permission#MODIFY_PHONE_STATE} permission or
+     * can manage all subscriptions in the list, according to their access rules.
+     *
+     * @param subIdList list of subId that will be in the same group.
+     * @param callingPackage The package making the call.
+     *
+     * @return groupUUID a UUID assigned to the subscription group. It returns null if fails.
+     *
+     * @throws IllegalArgumentException if {@code subId} is invalid.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(anyOf = {
+            Manifest.permission.MODIFY_PHONE_STATE,
+            "carrier privileges",
+    })
+    public ParcelUuid createSubscriptionGroup(@NonNull int[] subIdList,
+            @NonNull String callingPackage) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        Objects.requireNonNull(subIdList, "createSubscriptionGroup");
+        if (subIdList.length == 0) {
+            throw new IllegalArgumentException("Invalid subIdList " + Arrays.toString(subIdList));
+        }
+
+        // If it doesn't have modify phone state permission, or carrier privilege permission,
+        // a SecurityException will be thrown.
+        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
+                != PackageManager.PERMISSION_GRANTED && !checkCarrierPrivilegeOnSubList(
+                        subIdList, callingPackage)) {
+            throw new SecurityException("CreateSubscriptionGroup needs MODIFY_PHONE_STATE or"
+                    + " carrier privilege permission on all specified subscriptions");
+        }
+
+        long identity = Binder.clearCallingIdentity();
+
+        try {
+            // Generate a UUID.
+            ParcelUuid groupUUID = new ParcelUuid(UUID.randomUUID());
+            String uuidString = groupUUID.toString();
+
+            for (int subId : subIdList) {
+                mSubscriptionDatabaseManager.setGroupUuid(subId, uuidString);
+                mSubscriptionDatabaseManager.setGroupOwner(subId, callingPackage);
+            }
+
+            MultiSimSettingController.getInstance().notifySubscriptionGroupChanged(groupUUID);
+            return groupUUID;
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Set which subscription is preferred for cellular data. It's designed to overwrite default
+     * data subscription temporarily.
+     *
+     * @param subId which subscription is preferred to for cellular data
+     * @param needValidation whether validation is needed before switching
+     * @param callback callback upon request completion
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public void setPreferredDataSubscriptionId(int subId, boolean needValidation,
+            @Nullable ISetOpportunisticDataCallback callback) {
+        enforcePermissions("setPreferredDataSubscriptionId",
+                Manifest.permission.MODIFY_PHONE_STATE);
+        final long token = Binder.clearCallingIdentity();
+
+        try {
+            PhoneSwitcher phoneSwitcher = PhoneSwitcher.getInstance();
+            if (phoneSwitcher == null) {
+                loge("Set preferred data sub: phoneSwitcher is null.");
+                if (callback != null) {
+                    try {
+                        callback.onComplete(
+                                TelephonyManager.SET_OPPORTUNISTIC_SUB_REMOTE_SERVICE_EXCEPTION);
+                    } catch (RemoteException exception) {
+                        loge("RemoteException " + exception);
+                    }
+                }
+                return;
+            }
+
+            phoneSwitcher.trySetOpportunisticDataSubscription(subId, needValidation, callback);
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * @return The subscription id of preferred subscription for cellular data. This reflects
+     * the active modem which can serve large amount of cellular data.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
+    public int getPreferredDataSubscriptionId() {
+        enforcePermissions("getPreferredDataSubscriptionId",
+                Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        final long token = Binder.clearCallingIdentity();
+
+        try {
+            PhoneSwitcher phoneSwitcher = PhoneSwitcher.getInstance();
+            if (phoneSwitcher == null) {
+                loge("getPreferredDataSubscriptionId: PhoneSwitcher not available. Return the "
+                        + "default data sub " + getDefaultDataSubId());
+                return getDefaultDataSubId();
+            }
+
+            return phoneSwitcher.getAutoSelectedDataSubId();
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Get the opportunistic subscriptions.
+     *
+     * Callers with {@link Manifest.permission#READ_PHONE_STATE} or
+     * {@link Manifest.permission#READ_PRIVILEGED_PHONE_STATE} will have a full list of
+     * opportunistic subscriptions. Subscriptions that the carrier app has no privilege will be
+     * excluded from the list.
+     *
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return The list of opportunistic subscription info that can be accessed by the callers.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @NonNull
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_STATE,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public List<SubscriptionInfo> getOpportunisticSubscriptions(@NonNull String callingPackage,
+            @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        // Check if the caller has READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or carrier
+        // privilege on any active subscription. The carrier app will get full subscription infos
+        // on the subs it has carrier privilege.
+        if (!TelephonyPermissions.checkReadPhoneStateOnAnyActiveSub(mContext,
+                Binder.getCallingPid(), Binder.getCallingUid(), callingPackage, callingFeatureId,
+                "getOpportunisticSubscriptions")) {
+            throw new SecurityException("Need READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or "
+                    + "carrier privilege");
+        }
+
+        return mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                // callers have READ_PHONE_STATE or READ_PRIVILEGED_PHONE_STATE can get a full
+                // list. Carrier apps can only get the subscriptions they have privileged.
+                .filter(subInfo -> subInfo.isOpportunistic()
+                        && TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
+                        mContext, subInfo.getSubscriptionId(), callingPackage,
+                        callingFeatureId, "getOpportunisticSubscriptions"))
+                // Remove the identifier if the caller does not have sufficient permission.
+                // carrier apps will get full subscription info on the subscriptions associated
+                // to them.
+                .map(subInfo -> conditionallyRemoveIdentifiers(subInfo.toSubscriptionInfo(),
+                        callingPackage, callingFeatureId, "getOpportunisticSubscriptions"))
+                .sorted(Comparator.comparing(SubscriptionInfo::getSimSlotIndex)
+                        .thenComparing(SubscriptionInfo::getSubscriptionId))
+                .collect(Collectors.toList());
+    }
+
+    /**
+     * Remove a list of subscriptions from their subscription group.
+     *
+     * @param subIdList list of subId that need removing from their groups.
+     * @param groupUuid The UUID of the subscription group.
+     * @param callingPackage The package making the call.
+     *
+     * @throws SecurityException if the caller doesn't meet the requirements outlined above.
+     * @throws IllegalArgumentException if the some subscriptions in the list doesn't belong the
+     * specified group.
+     *
+     * @see SubscriptionManager#createSubscriptionGroup(List)
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public void removeSubscriptionsFromGroup(@NonNull int[] subIdList,
+            @NonNull ParcelUuid groupUuid, @NonNull String callingPackage) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        // If it doesn't have modify phone state permission, or carrier privilege permission,
+        // a SecurityException will be thrown. If it's due to invalid parameter or internal state,
+        // it will return null.
+        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
+                != PackageManager.PERMISSION_GRANTED
+                && !(checkCarrierPrivilegeOnSubList(subIdList, callingPackage)
+                && canPackageManageGroup(groupUuid, callingPackage))) {
+            throw new SecurityException("removeSubscriptionsFromGroup needs MODIFY_PHONE_STATE or"
+                    + " carrier privilege permission on all specified subscriptions.");
+        }
+
+        Objects.requireNonNull(subIdList);
+        Objects.requireNonNull(groupUuid);
+
+        if (subIdList.length == 0) {
+            throw new IllegalArgumentException("subIdList is empty.");
+        }
+
+        long identity = Binder.clearCallingIdentity();
+
+        try {
+            for (int subId : subIdList) {
+                SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                        .getSubscriptionInfoInternal(subId);
+                if (subInfo == null) {
+                    throw new IllegalArgumentException("The provided sub id " + subId
+                            + " is not valid.");
+                }
+                if (!groupUuid.toString().equals(subInfo.getGroupUuid())) {
+                    throw new IllegalArgumentException("Subscription " + subInfo.getSubscriptionId()
+                            + " doesn't belong to group " + groupUuid);
+                }
+            }
+
+            for (SubscriptionInfoInternal subInfo :
+                    mSubscriptionDatabaseManager.getAllSubscriptions()) {
+                if (IntStream.of(subIdList).anyMatch(
+                        subId -> subId == subInfo.getSubscriptionId())) {
+                    mSubscriptionDatabaseManager.setGroupUuid(subInfo.getSubscriptionId(), "");
+                    mSubscriptionDatabaseManager.setGroupOwner(subInfo.getSubscriptionId(), "");
+                } else if (subInfo.getGroupUuid().equals(groupUuid.toString())) {
+                    // Pre-T behavior. If there are still subscriptions having the same UUID, update
+                    // to the new owner.
+                    mSubscriptionDatabaseManager.setGroupOwner(
+                            subInfo.getSubscriptionId(), callingPackage);
+                }
+            }
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Add a list of subscriptions into a group.
+     *
+     * Caller should either have {@link android.Manifest.permission#MODIFY_PHONE_STATE}
+     * permission or had carrier privilege permission on the subscriptions.
+     *
+     * @param subIdList list of subId that need adding into the group
+     * @param groupUuid the groupUuid the subscriptions are being added to.
+     * @param callingPackage The package making the call.
+     *
+     * @throws SecurityException if the caller doesn't meet the requirements outlined above.
+     * @throws IllegalArgumentException if the some subscriptions in the list doesn't exist.
+     *
+     * @see SubscriptionManager#createSubscriptionGroup(List)
+     */
+    @Override
+    @RequiresPermission(anyOf = {
+            Manifest.permission.MODIFY_PHONE_STATE,
+            "carrier privileges",
+    })
+    public void addSubscriptionsIntoGroup(@NonNull int[] subIdList, @NonNull ParcelUuid groupUuid,
+            @NonNull String callingPackage) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        Objects.requireNonNull(subIdList, "subIdList");
+        if (subIdList.length == 0) {
+            throw new IllegalArgumentException("Invalid subId list");
+        }
+
+        Objects.requireNonNull(groupUuid, "groupUuid");
+        String groupUuidString = groupUuid.toString();
+        if (groupUuidString.equals(CarrierConfigManager.REMOVE_GROUP_UUID_STRING)) {
+            throw new IllegalArgumentException("Invalid groupUuid");
+        }
+
+        // If it doesn't have modify phone state permission, or carrier privilege permission,
+        // a SecurityException will be thrown.
+        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
+                != PackageManager.PERMISSION_GRANTED
+                && !(checkCarrierPrivilegeOnSubList(subIdList, callingPackage)
+                && canPackageManageGroup(groupUuid, callingPackage))) {
+            throw new SecurityException("Requires MODIFY_PHONE_STATE or carrier privilege"
+                    + " permissions on subscriptions and the group.");
+        }
+
+        long identity = Binder.clearCallingIdentity();
+
+        try {
+            for (int subId : subIdList) {
+                mSubscriptionDatabaseManager.setGroupUuid(subId, groupUuidString);
+                mSubscriptionDatabaseManager.setGroupOwner(subId, callingPackage);
+            }
+
+            MultiSimSettingController.getInstance().notifySubscriptionGroupChanged(groupUuid);
+            logl("addSubscriptionsIntoGroup: add subs " + Arrays.toString(subIdList)
+                    + " to the group.");
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Get subscriptionInfo list of subscriptions that are in the same group of given subId.
+     * See {@link #createSubscriptionGroup(int[], String)} for more details.
+     *
+     * Caller must have {@link android.Manifest.permission#READ_PHONE_STATE}
+     * or carrier privilege permission on the subscription.
+     *
+     * <p>Starting with API level 33, the caller also needs permission to access device identifiers
+     * to get the list of subscriptions associated with a group UUID.
+     * This method can be invoked if one of the following requirements is met:
+     * <ul>
+     *     <li>If the app has carrier privilege permission.
+     *     {@link TelephonyManager#hasCarrierPrivileges()}
+     *     <li>If the app has {@link android.Manifest.permission#READ_PHONE_STATE} permission and
+     *     access to device identifiers.
+     * </ul>
+     *
+     * @param groupUuid of which list of subInfo will be returned.
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return List of {@link SubscriptionInfo} that belong to the same group, including the given
+     * subscription itself. It will return an empty list if no subscription belongs to the group.
+     *
+     * @throws SecurityException if the caller doesn't meet the requirements outlined above.
+     */
+    @Override
+    @NonNull
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_STATE,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public List<SubscriptionInfo> getSubscriptionsInGroup(@NonNull ParcelUuid groupUuid,
+            @NonNull String callingPackage, @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        // If the calling app neither has carrier privileges nor READ_PHONE_STATE and access to
+        // device identifiers, it will throw a SecurityException.
+        if (CompatChanges.isChangeEnabled(REQUIRE_DEVICE_IDENTIFIERS_FOR_GROUP_UUID,
+                Binder.getCallingUid())) {
+            try {
+                if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mContext,
+                        callingPackage, callingFeatureId, "getSubscriptionsInGroup")) {
+                    EventLog.writeEvent(0x534e4554, "213902861", Binder.getCallingUid());
+                    throw new SecurityException("Need to have carrier privileges or access to "
+                            + "device identifiers to call getSubscriptionsInGroup");
+                }
+            } catch (SecurityException e) {
+                EventLog.writeEvent(0x534e4554, "213902861", Binder.getCallingUid());
+                throw e;
+            }
+        }
+
+        return mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                .map(SubscriptionInfoInternal::toSubscriptionInfo)
+                .filter(info -> groupUuid.equals(info.getGroupUuid())
+                        && (mSubscriptionManager.canManageSubscription(info, callingPackage)
+                        || TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
+                                mContext, info.getSubscriptionId(), callingPackage,
+                        callingFeatureId, "getSubscriptionsInGroup")))
+                .map(subscriptionInfo -> conditionallyRemoveIdentifiers(subscriptionInfo,
+                        callingPackage, callingFeatureId, "getSubscriptionsInGroup"))
+                .collect(Collectors.toList());
+    }
+
+    /**
+     * Get slot index associated with the subscription.
+     *
+     * @param subId The subscription id.
+     *
+     * @return Logical slot index (i.e. phone id) as a positive integer or
+     * {@link SubscriptionManager#INVALID_SIM_SLOT_INDEX} if the supplied {@code subId} doesn't have
+     * an associated slot index.
+     */
+    @Override
+    public int getSlotIndex(int subId) {
+        if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
+            subId = getDefaultSubId();
+        }
+
+        for (Map.Entry<Integer, Integer> entry : mSlotIndexToSubId.entrySet()) {
+            if (entry.getValue() == subId) return entry.getKey();
+        }
+
+        return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
+    }
+
+    /**
+     * Get the subscription id for specified slot index.
+     *
+     * @param slotIndex Logical SIM slot index.
+     * @return The subscription id. {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if SIM is
+     * absent.
+     */
+    @Override
+    public int getSubId(int slotIndex) {
+        if (slotIndex == SubscriptionManager.DEFAULT_SIM_SLOT_INDEX) {
+            slotIndex = getSlotIndex(getDefaultSubId());
+        }
+
+        // Check that we have a valid slotIndex or the slotIndex is for a remote SIM (remote SIM
+        // uses special slot index that may be invalid otherwise)
+        if (!SubscriptionManager.isValidSlotIndex(slotIndex)
+                && slotIndex != SubscriptionManager.SLOT_INDEX_FOR_REMOTE_SIM_SUB) {
+            return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+        }
+
+        return mSlotIndexToSubId.getOrDefault(slotIndex,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+    }
+
+    @Override
+    public int[] getSubIds(int slotIndex) {
+        return new int[]{getSubId(slotIndex)};
+    }
+
+    /**
+     * Update default voice, sms, and data sub id.
+     */
+    private void updateDefaultSubIds() {
+        if (getActiveSubInfoCountMax() == 0) {
+            mDefaultVoiceSubId.set(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+            mDefaultSmsSubId.set(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+            mDefaultDataSubId.set(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        }
+        if (getActiveSubInfoCountMax() == 1) {
+            int[] activeSubIds = getActiveSubIdList(false);
+            if (activeSubIds.length == 1) {
+                mDefaultVoiceSubId.set(activeSubIds[0]);
+                mDefaultSmsSubId.set(activeSubIds[0]);
+                mDefaultDataSubId.set(activeSubIds[0]);
+            } else {
+                loge("updateDefaultSubIds: Single SIM device, but active subs are more than one."
+                        + " activeSubIds=" + Arrays.toString(activeSubIds));
+            }
+        } else {
+            // TODO: Support dual sim
+        }
+
+        updateDefaultSubId();
+    }
+
+    /**
+     * Update default sub id.
+     */
+    private void updateDefaultSubId() {
+        int subId;
+        boolean isVoiceCapable = mTelephonyManager.isVoiceCapable();
+
+        if (isVoiceCapable) {
+            subId = getDefaultVoiceSubId();
+        } else {
+            subId = getDefaultDataSubId();
+        }
+
+        // If the subId is not active, use the fist active subscription's subId.
+        if (!mSlotIndexToSubId.containsValue(subId)) {
+            int[] activeSubIds = getActiveSubIdList(true);
+            if (activeSubIds.length > 0) {
+                subId = activeSubIds[0];
+                log("updateDefaultSubId: First available active sub = " + subId);
+            } else {
+                subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+            }
+        }
+
+        if (mDefaultSubId.get() != subId) {
+            int phoneId = getPhoneId(subId);
+            logl("updateDefaultSubId: Default sub id updated from " + mDefaultSubId.get() + " to "
+                    + subId + ", phoneId=" + phoneId);
+            mDefaultSubId.set(subId);
+
+            Intent intent = new Intent(SubscriptionManager.ACTION_DEFAULT_SUBSCRIPTION_CHANGED);
+            intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
+            SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId, subId);
+            mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
+        }
+    }
+
+    /**
+     * @return The default subscription id.
+     */
+    @Override
+    public int getDefaultSubId() {
+        return mDefaultSubId.get();
+    }
+
+    /**
+     * Get phone id from the subscription id. In the implementation, the logical SIM slot index
+     * is equivalent to phone id. So this method is same as {@link #getSlotIndex(int)}.
+     *
+     * @param subId The subscription id.
+     *
+     * @return The phone id.
+     */
+    @Override
+    public int getPhoneId(int subId) {
+        // slot index and phone id are equivalent in the current implementation.
+        // It is intended NOT to return DEFAULT_PHONE_INDEX any more from this method.
+        return getSlotIndex(subId);
+    }
+
+    /**
+     * @return Subscription id of the default cellular data. This reflects the user's default data
+     * choice, which might be a little bit different than the active one returned by
+     * {@link #getPreferredDataSubscriptionId()}.
+     */
+    @Override
+    public int getDefaultDataSubId() {
+        return mDefaultDataSubId.get();
+    }
+
+    /**
+     * Set the default data subscription id.
+     *
+     * @param subId The default data subscription id.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public void setDefaultDataSubId(int subId) {
+        enforcePermissions("setDefaultDataSubId", Manifest.permission.MODIFY_PHONE_STATE);
+
+        if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
+            throw new RuntimeException("setDefaultDataSubId called with DEFAULT_SUBSCRIPTION_ID");
+        }
+
+        final long token = Binder.clearCallingIdentity();
+        try {
+            int oldDefaultDataSubId = mDefaultDataSubId.get();
+            if (mDefaultDataSubId.set(subId)) {
+                SubscriptionManager.invalidateSubscriptionManagerServiceCaches();
+                logl("Default data subId changed from " + oldDefaultDataSubId + " to " + subId);
+
+                MultiSimSettingController.getInstance().notifyDefaultDataSubChanged();
+
+                Intent intent = new Intent(
+                        TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
+                intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
+                SubscriptionManager.putSubscriptionIdExtra(intent, subId);
+                mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
+
+                updateDefaultSubId();
+            }
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * @return The default subscription id for voice.
+     */
+    @Override
+    public int getDefaultVoiceSubId() {
+        return mDefaultVoiceSubId.get();
+    }
+
+    /**
+     * Set the default voice subscription id.
+     *
+     * @param subId The default SMS subscription id.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public void setDefaultVoiceSubId(int subId) {
+        enforcePermissions("setDefaultVoiceSubId", Manifest.permission.MODIFY_PHONE_STATE);
+
+        if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
+            throw new RuntimeException("setDefaultVoiceSubId called with DEFAULT_SUB_ID");
+        }
+
+        final long token = Binder.clearCallingIdentity();
+        try {
+            int oldDefaultVoiceSubId = mDefaultVoiceSubId.get();
+            if (mDefaultVoiceSubId.set(subId)) {
+                SubscriptionManager.invalidateSubscriptionManagerServiceCaches();
+                logl("Default voice subId changed from " + oldDefaultVoiceSubId + " to " + subId);
+
+                Intent intent = new Intent(
+                        TelephonyIntents.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED);
+                intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
+                SubscriptionManager.putSubscriptionIdExtra(intent, subId);
+                mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
+
+                PhoneAccountHandle newHandle = subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID
+                        ? null : mTelephonyManager.getPhoneAccountHandleForSubscriptionId(subId);
+
+                TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class);
+                if (telecomManager != null) {
+                    telecomManager.setUserSelectedOutgoingPhoneAccount(newHandle);
+                }
+
+                updateDefaultSubId();
+            }
+
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * @return The default subscription id for SMS.
+     */
+    @Override
+    public int getDefaultSmsSubId() {
+        return mDefaultSmsSubId.get();
+    }
+
+    /**
+     * Set the default SMS subscription id.
+     *
+     * @param subId The default SMS subscription id.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public void setDefaultSmsSubId(int subId) {
+        enforcePermissions("setDefaultSmsSubId", Manifest.permission.MODIFY_PHONE_STATE);
+
+        if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
+            throw new RuntimeException("setDefaultSmsSubId called with DEFAULT_SUB_ID");
+        }
+
+        final long token = Binder.clearCallingIdentity();
+        try {
+            int oldDefaultSmsSubId = mDefaultSmsSubId.get();
+            if (mDefaultSmsSubId.set(subId)) {
+                SubscriptionManager.invalidateSubscriptionManagerServiceCaches();
+                logl("Default SMS subId changed from " + oldDefaultSmsSubId + " to " + subId);
+
+                Intent intent = new Intent(
+                        SubscriptionManager.ACTION_DEFAULT_SMS_SUBSCRIPTION_CHANGED);
+                intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
+                SubscriptionManager.putSubscriptionIdExtra(intent, subId);
+                mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
+            }
+
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Get the active subscription id list.
+     *
+     * @param visibleOnly {@code true} if only includes user visible subscription's sub id.
+     *
+     * @return List of the active subscription id.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
+    public int[] getActiveSubIdList(boolean visibleOnly) {
+        enforcePermissions("getActiveSubIdList", Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        final long token = Binder.clearCallingIdentity();
+        try {
+            return mSlotIndexToSubId.values().stream()
+                    .filter(subId -> {
+                        SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                                .getSubscriptionInfoInternal(subId);
+                        return subInfo != null && (!visibleOnly || subInfo.isVisible()); })
+                    .mapToInt(x -> x)
+                    .toArray();
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Set a field in the subscription database. Note not all fields are supported.
+     *
+     * @param subId Subscription Id of Subscription.
+     * @param columnName Column name in the database. Note not all fields are supported.
+     * @param value Value to store in the database.
+     *
+     * // TODO: Remove return value after SubscriptionController is deleted.
+     * @return always 1
+     *
+     * @throws IllegalArgumentException if {@code subscriptionId} is invalid, or the field is not
+     * exposed.
+     * @throws SecurityException if callers do not hold the required permission.
+     *
+     * @see #getSubscriptionProperty(int, String, String, String)
+     * @see SimInfo for all the columns.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public int setSubscriptionProperty(int subId, @NonNull String columnName,
+            @NonNull String value) {
+        enforcePermissions("setSubscriptionProperty", Manifest.permission.MODIFY_PHONE_STATE);
+
+        final long token = Binder.clearCallingIdentity();
+        try {
+            if (!SimInfo.getAllColumns().contains(columnName)) {
+                throw new IllegalArgumentException("Invalid column name " + columnName);
+            }
+
+            // Check if the columns are allowed to be accessed through the generic
+            // getSubscriptionProperty method.
+            if (!DIRECT_ACCESS_SUBSCRIPTION_COLUMNS.contains(columnName)) {
+                throw new SecurityException("Column " + columnName + " is not allowed be directly "
+                        + "accessed through setSubscriptionProperty.");
+            }
+
+            mSubscriptionDatabaseManager.setSubscriptionProperty(subId, columnName, value);
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Get specific field in string format from the subscription info database.
+     *
+     * @param subId Subscription id of the subscription.
+     * @param columnName Column name in subscription database.
+     *
+     * @return Value in string format associated with {@code subscriptionId} and {@code columnName}
+     * from the database.
+     *
+     * @throws IllegalArgumentException if {@code subscriptionId} is invalid, or the field is not
+     * exposed.
+     * @throws SecurityException if callers do not hold the required permission.
+     *
+     * @see SimInfo for all the columns.
+     */
+    @Override
+    @NonNull
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_STATE,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public String getSubscriptionProperty(int subId, @NonNull String columnName,
+            @NonNull String callingPackage, @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        Objects.requireNonNull(columnName);
+        if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId,
+                callingPackage, callingFeatureId,
+                "getSubscriptionProperty")) {
+            throw new SecurityException("Need READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or "
+                    + "carrier privilege");
+        }
+
+        if (!SimInfo.getAllColumns().contains(columnName)) {
+            throw new IllegalArgumentException("Invalid column name " + columnName);
+        }
+
+        // Check if the columns are allowed to be accessed through the generic
+        // getSubscriptionProperty method.
+        if (!DIRECT_ACCESS_SUBSCRIPTION_COLUMNS.contains(columnName)) {
+            throw new SecurityException("Column " + columnName + " is not allowed be directly "
+                    + "accessed through getSubscriptionProperty.");
+        }
+
+        final long token = Binder.clearCallingIdentity();
+        try {
+            Object value = mSubscriptionDatabaseManager.getSubscriptionProperty(subId, columnName);
+            // The raw types of subscription database should only have 3 different types.
+            if (value instanceof Integer) {
+                return String.valueOf(value);
+            } else if (value instanceof String) {
+                return (String) value;
+            } else if (value instanceof byte[]) {
+                return Base64.encodeToString((byte[]) value, Base64.DEFAULT);
+            } else {
+                // This should not happen unless SubscriptionDatabaseManager.getSubscriptionProperty
+                // did not implement correctly.
+                throw new RuntimeException("Unexpected type " + value.getClass().getTypeName()
+                        + " was returned from SubscriptionDatabaseManager for column "
+                        + columnName);
+            }
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    @Override
+    public boolean setSubscriptionEnabled(boolean enable, int subId) {
+        enforcePermissions("setSubscriptionEnabled", Manifest.permission.MODIFY_PHONE_STATE);
+
+
+        return true;
+    }
+
+    /**
+     * Check if a subscription is active.
+     *
+     * @param subId The subscription id to check.
+     *
+     * @return {@code true} if the subscription is active.
+     *
+     * @throws IllegalArgumentException if the provided slot index is invalid.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
+    public boolean isSubscriptionEnabled(int subId) {
+        enforcePermissions("isSubscriptionEnabled",
+                Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
+            throw new IllegalArgumentException("Invalid subscription id " + subId);
+        }
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                    .getSubscriptionInfoInternal(subId);
+            return subInfo != null && subInfo.isActive();
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Get the active subscription id by logical SIM slot index.
+     *
+     * @param slotIndex The logical SIM slot index.
+     * @return The active subscription id.
+     *
+     * @throws IllegalArgumentException if the provided slot index is invalid.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
+    public int getEnabledSubscriptionId(int slotIndex) {
+        enforcePermissions("getEnabledSubscriptionId",
+                Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        if (!SubscriptionManager.isValidSlotIndex(slotIndex)) {
+            throw new IllegalArgumentException("Invalid slot index " + slotIndex);
+        }
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            return mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                    .filter(subInfo -> subInfo.isActive() && subInfo.getSimSlotIndex() == slotIndex)
+                    .mapToInt(SubscriptionInfoInternal::getSubscriptionId)
+                    .findFirst()
+                    .orElse(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Check if a subscription is active.
+     *
+     * @param subId The subscription id.
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return {@code true} if the subscription is active.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_STATE,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public boolean isActiveSubId(int subId, @NonNull String callingPackage,
+            @Nullable String callingFeatureId) {
+        if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
+                callingFeatureId, "isActiveSubId")) {
+            throw new SecurityException("Need READ_PHONE_STATE, READ_PRIVILEGED_PHONE_STATE, or "
+                    + "carrier privilege");
+        }
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                    .getSubscriptionInfoInternal(subId);
+            return subInfo != null && subInfo.isActive();
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Get active data subscription id. Active data subscription refers to the subscription
+     * currently chosen to provide cellular internet connection to the user. This may be
+     * different from getDefaultDataSubscriptionId().
+     *
+     * @return Active data subscription id if any is chosen, or
+     * SubscriptionManager.INVALID_SUBSCRIPTION_ID if not.
+     *
+     * @see android.telephony.TelephonyCallback.ActiveDataSubscriptionIdListener
+     */
+    @Override
+    public int getActiveDataSubscriptionId() {
+        final long token = Binder.clearCallingIdentity();
+        try {
+            PhoneSwitcher phoneSwitcher = PhoneSwitcher.getInstance();
+            if (phoneSwitcher != null) {
+                int activeDataSubId = phoneSwitcher.getActiveDataSubId();
+                if (SubscriptionManager.isUsableSubscriptionId(activeDataSubId)) {
+                    return activeDataSubId;
+                }
+            }
+            // If phone switcher isn't ready, or active data sub id is not available, use default
+            // sub id from settings.
+            return getDefaultDataSubId();
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Whether it's supported to disable / re-enable a subscription on a physical (non-euicc) SIM.
+     *
+     * Physical SIM refers non-euicc, or aka non-programmable SIM.
+     *
+     * It provides whether a physical SIM card can be disabled without taking it out, which is done
+     * via {@link SubscriptionManager#setSubscriptionEnabled(int, boolean)} API.
+     *
+     * @return whether can disable subscriptions on physical SIMs.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
+    public boolean canDisablePhysicalSubscription() {
+        enforcePermissions("canDisablePhysicalSubscription",
+                Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            Phone phone = PhoneFactory.getDefaultPhone();
+            return phone != null && phone.canDisablePhysicalSubscription();
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Set uicc applications being enabled or disabled.
+     *
+     * The value will be remembered on the subscription and will be applied whenever it's present.
+     * If the subscription in currently present, it will also apply the setting to modem
+     * immediately (the setting in the modem will not change until the modem receives and responds
+     * to the request, but typically this should only take a few seconds. The user visible setting
+     * available from {@link SubscriptionInfo#areUiccApplicationsEnabled()} will be updated
+     * immediately.)
+     *
+     * @param enabled whether uicc applications are enabled or disabled.
+     * @param subId which subscription to operate on.
+     *
+     * @return the number of records updated.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public int setUiccApplicationsEnabled(boolean enabled, int subId) {
+        enforcePermissions("setUiccApplicationsEnabled",
+                Manifest.permission.MODIFY_PHONE_STATE);
+
+        long identity = Binder.clearCallingIdentity();
+        try {
+            mSubscriptionDatabaseManager.setUiccApplicationsEnabled(subId, enabled);
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Set the device to device status sharing user preference for a subscription ID. The setting
+     * app uses this method to indicate with whom they wish to share device to device status
+     * information.
+     *
+     * @param sharing the status sharing preference.
+     * @param subId the unique Subscription ID in database.
+     *
+     * @return the number of records updated.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist, or the sharing
+     * preference is invalid.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public int setDeviceToDeviceStatusSharing(@DeviceToDeviceStatusSharingPreference int sharing,
+            int subId) {
+        enforcePermissions("setDeviceToDeviceStatusSharing",
+                Manifest.permission.MODIFY_PHONE_STATE);
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            if (sharing < SubscriptionManager.D2D_SHARING_DISABLED
+                    || sharing > SubscriptionManager.D2D_SHARING_ALL) {
+                throw new IllegalArgumentException("invalid sharing " + sharing);
+            }
+
+            mSubscriptionDatabaseManager.setDeviceToDeviceStatusSharingPreference(subId, sharing);
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Set the list of contacts that allow device to device status sharing for a subscription ID.
+     * The setting app uses this method to indicate with whom they wish to share device to device
+     * status information.
+     *
+     * @param contacts The list of contacts that allow device to device status sharing
+     * @param subId The unique Subscription ID in database.
+     *
+     * @throws IllegalArgumentException if {@code subId} is invalid.
+     * @throws NullPointerException if {@code contacts} is {@code null}.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MODIFY_PHONE_STATE)
+    public int setDeviceToDeviceStatusSharingContacts(@NonNull String contacts, int subId) {
+        enforcePermissions("setDeviceToDeviceStatusSharingContacts",
+                Manifest.permission.MODIFY_PHONE_STATE);
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            Objects.requireNonNull(contacts, "contacts");
+            mSubscriptionDatabaseManager.setDeviceToDeviceStatusSharingContacts(subId, contacts);
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Returns the phone number for the given {@code subscriptionId} and {@code source},
+     * or an empty string if not available.
+     *
+     * <p>General apps that need to know the phone number should use
+     * {@link SubscriptionManager#getPhoneNumber(int)} instead. This API may be suitable specific
+     * apps that needs to know the phone number from a specific source. For example, a carrier app
+     * needs to know exactly what's on {@link SubscriptionManager#PHONE_NUMBER_SOURCE_UICC UICC} and
+     * decide if the previously set phone number of source
+     * {@link SubscriptionManager#PHONE_NUMBER_SOURCE_CARRIER carrier} should be updated.
+     *
+     * <p>The API provides no guarantees of what format the number is in: the format can vary
+     * depending on the {@code source} and the network etc. Programmatic parsing should be done
+     * cautiously, for example, after formatting the number to a consistent format with
+     * {@link android.telephony.PhoneNumberUtils#formatNumberToE164(String, String)}.
+     *
+     * <p>Note the assumption is that one subscription (which usually means one SIM) has
+     * only one phone number. The multiple sources backup each other so hopefully at least one
+     * is available. For example, for a carrier that doesn't typically set phone numbers
+     * on {@link SubscriptionManager#PHONE_NUMBER_SOURCE_UICC UICC}, the source
+     * {@link SubscriptionManager#PHONE_NUMBER_SOURCE_IMS IMS} may provide one. Or, a carrier may
+     * decide to provide the phone number via source
+     * {@link SubscriptionManager#PHONE_NUMBER_SOURCE_CARRIER carrier} if neither source UICC nor
+     * IMS is available.
+     *
+     * <p>The availability and correctness of the phone number depends on the underlying source
+     * and the network etc. Additional verification is needed to use this number for
+     * security-related or other sensitive scenarios.
+     *
+     * @param subId The subscription ID.
+     * @param source The source of the phone number.
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return The phone number, or an empty string if not available.
+     *
+     * @throws IllegalArgumentException if {@code source} or {@code subId} is invalid.
+     * @throws SecurityException if the caller doesn't have permissions required.
+     *
+     * @see SubscriptionManager#PHONE_NUMBER_SOURCE_UICC
+     * @see SubscriptionManager#PHONE_NUMBER_SOURCE_CARRIER
+     * @see SubscriptionManager#PHONE_NUMBER_SOURCE_IMS
+     *
+     * @throws IllegalArgumentException if {@code subId} is invalid.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @NonNull
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_NUMBERS,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public String getPhoneNumber(int subId, @PhoneNumberSource int source,
+            @NonNull String callingPackage, @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(
+                mContext, subId, Binder.getCallingUid(), "getPhoneNumber",
+                Manifest.permission.READ_PHONE_NUMBERS,
+                Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        final long identity = Binder.clearCallingIdentity();
+
+        SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                .getSubscriptionInfoInternal(subId);
+
+        if (subInfo == null) {
+            throw new IllegalArgumentException("Invalid sub id " + subId);
+        }
+
+        try {
+            switch(source) {
+                case SubscriptionManager.PHONE_NUMBER_SOURCE_UICC:
+                    Phone phone = PhoneFactory.getPhone(getPhoneId(subId));
+                    if (phone != null) {
+                        return TextUtils.emptyIfNull(phone.getLine1Number());
+                    } else {
+                        return subInfo.getNumber();
+                    }
+                case SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER:
+                    return subInfo.getNumberFromCarrier();
+                case SubscriptionManager.PHONE_NUMBER_SOURCE_IMS:
+                    return subInfo.getNumberFromIms();
+                default:
+                    throw new IllegalArgumentException("Invalid number source " + source);
+            }
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Get phone number from first available source. The order would be
+     * {@link SubscriptionManager#PHONE_NUMBER_SOURCE_CARRIER},
+     * {@link SubscriptionManager#PHONE_NUMBER_SOURCE_UICC}, then
+     * {@link SubscriptionManager#PHONE_NUMBER_SOURCE_IMS}.
+     *
+     * @param subId The subscription ID.
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @return The phone number from the first available source.
+     *
+     * @throws IllegalArgumentException if {@code subId} is invalid.
+     * @throws SecurityException if callers do not hold the required permission.
+     */
+    @Override
+    @NonNull
+    @RequiresPermission(anyOf = {
+            Manifest.permission.READ_PHONE_NUMBERS,
+            Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
+            "carrier privileges",
+    })
+    public String getPhoneNumberFromFirstAvailableSource(int subId,
+            @NonNull String callingPackage, @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(
+                mContext, subId, Binder.getCallingUid(), "getPhoneNumberFromFirstAvailableSource",
+                Manifest.permission.READ_PHONE_NUMBERS,
+                Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            String numberFromCarrier = getPhoneNumber(subId,
+                    SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER, callingPackage,
+                    callingFeatureId);
+            if (!TextUtils.isEmpty(numberFromCarrier)) {
+                return numberFromCarrier;
+            }
+            String numberFromUicc = getPhoneNumber(
+                    subId, SubscriptionManager.PHONE_NUMBER_SOURCE_UICC, callingPackage,
+                    callingFeatureId);
+            if (!TextUtils.isEmpty(numberFromUicc)) {
+                return numberFromUicc;
+            }
+            String numberFromIms = getPhoneNumber(
+                    subId, SubscriptionManager.PHONE_NUMBER_SOURCE_IMS, callingPackage,
+                    callingFeatureId);
+            if (!TextUtils.isEmpty(numberFromIms)) {
+                return numberFromIms;
+            }
+            return "";
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Set the phone number of the subscription.
+     *
+     * @param subId The subscription id.
+     * @param source The phone number source.
+     * @param number The phone number.
+     * @param callingPackage The package making the call.
+     * @param callingFeatureId The feature in the package.
+     *
+     * @throws IllegalArgumentException {@code subId} is invalid, or {@code source} is not
+     * {@link SubscriptionManager#PHONE_NUMBER_SOURCE_CARRIER}.
+     * @throws NullPointerException if {@code number} is {@code null}.
+     */
+    @Override
+    @RequiresPermission("carrier privileges")
+    public void setPhoneNumber(int subId, @PhoneNumberSource int source, @NonNull String number,
+            @NonNull String callingPackage, @Nullable String callingFeatureId) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        if (!TelephonyPermissions.checkCarrierPrivilegeForSubId(mContext, subId)) {
+            throw new SecurityException("setPhoneNumber for CARRIER needs carrier privilege.");
+        }
+
+        if (source != SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER) {
+            throw new IllegalArgumentException("setPhoneNumber doesn't accept source "
+                    + SubscriptionManager.phoneNumberSourceToString(source));
+        }
+
+        Objects.requireNonNull(number, "number");
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            mSubscriptionDatabaseManager.setNumberFromCarrier(subId, number);
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    /**
+     * Set the Usage Setting for this subscription.
+     *
+     * @param usageSetting the usage setting for this subscription
+     * @param subId the unique SubscriptionInfo index in database
+     * @param callingPackage The package making the IPC.
+     *
+     * @throws IllegalArgumentException if the subscription does not exist, or {@code usageSetting}
+     * is invalid.
+     * @throws SecurityException if doesn't have MODIFY_PHONE_STATE or Carrier Privileges
+     */
+    @Override
+    @RequiresPermission(anyOf = {
+            Manifest.permission.MODIFY_PHONE_STATE,
+            "carrier privileges",
+    })
+    public int setUsageSetting(@UsageSetting int usageSetting, int subId,
+            @NonNull String callingPackage) {
+        // Verify that the callingPackage belongs to the calling UID
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+
+        TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(
+                mContext, Binder.getCallingUid(), subId, true, "setUsageSetting",
+                Manifest.permission.MODIFY_PHONE_STATE);
+
+        if (usageSetting < SubscriptionManager.USAGE_SETTING_DEFAULT
+                || usageSetting > SubscriptionManager.USAGE_SETTING_DATA_CENTRIC) {
+            throw new IllegalArgumentException("setUsageSetting: Invalid usage setting: "
+                    + usageSetting);
+        }
+
+        final long token = Binder.clearCallingIdentity();
+        try {
+            mSubscriptionDatabaseManager.setUsageSetting(subId, usageSetting);
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Set UserHandle for this subscription.
+     *
+     * @param userHandle the userHandle associated with the subscription
+     * Pass {@code null} user handle to clear the association
+     * @param subId the unique SubscriptionInfo index in database
+     * @return the number of records updated.
+     *
+     * @throws SecurityException if callers do not hold the required permission.
+     * @throws IllegalArgumentException if {@code subId} is invalid.
+     */
+    @Override
+    @RequiresPermission(Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION)
+    public int setSubscriptionUserHandle(@Nullable UserHandle userHandle, int subId) {
+        enforcePermissions("setSubscriptionUserHandle",
+                Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION);
+
+        if (userHandle == null) {
+            userHandle = UserHandle.of(UserHandle.USER_NULL);
+        }
+
+        long token = Binder.clearCallingIdentity();
+        try {
+            // This can throw IllegalArgumentException if the subscription does not exist.
+            mSubscriptionDatabaseManager.setUserId(subId, userHandle.getIdentifier());
+            return 1;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Get UserHandle of this subscription.
+     *
+     * @param subId the unique SubscriptionInfo index in database
+     * @return userHandle associated with this subscription
+     * or {@code null} if subscription is not associated with any user.
+     *
+     * @throws SecurityException if doesn't have required permission.
+     * @throws IllegalArgumentException if {@code subId} is invalid.
+     */
+    @Override
+    @Nullable
+    @RequiresPermission(Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION)
+    public UserHandle getSubscriptionUserHandle(int subId) {
+        enforcePermissions("getSubscriptionUserHandle",
+                Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION);
+
+        if (!mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_enable_get_subscription_user_handle)) {
+            return null;
+        }
+
+        long token = Binder.clearCallingIdentity();
+        try {
+            SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
+                    .getSubscriptionInfoInternal(subId);
+            if (subInfo == null) {
+                throw new IllegalArgumentException("getSubscriptionUserHandle: Invalid subId: "
+                        + subId);
+            }
+
+            UserHandle userHandle = UserHandle.of(subInfo.getUserId());
+            if (userHandle.getIdentifier() == UserHandle.USER_NULL) {
+                return null;
+            }
+            return userHandle;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Check if subscription and user are associated with each other.
+     *
+     * @param subscriptionId the subId of the subscription
+     * @param userHandle user handle of the user
+     * @return {@code true} if subscription is associated with user
+     * {code true} if there are no subscriptions on device
+     * else {@code false} if subscription is not associated with user.
+     *
+     * @throws SecurityException if the caller doesn't have permissions required.
+     * @throws IllegalStateException if subscription service is not available.
+     *
+     */
+    @Override
+    public boolean isSubscriptionAssociatedWithUser(int subscriptionId,
+            @NonNull UserHandle userHandle) {
+        enforcePermissions("isSubscriptionAssociatedWithUser",
+                Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION);
+
+        long token = Binder.clearCallingIdentity();
+        try {
+            // Return true if there are no subscriptions on the device.
+            List<SubscriptionInfo> subInfoList = getAllSubInfoList(
+                    mContext.getOpPackageName(), mContext.getAttributionTag());
+            if (subInfoList == null || subInfoList.isEmpty()) {
+                return true;
+            }
+
+            // Get list of subscriptions associated with this user.
+            List<SubscriptionInfo> associatedSubscriptionsList =
+                    getSubscriptionInfoListAssociatedWithUser(userHandle);
+            if (associatedSubscriptionsList.isEmpty()) {
+                return false;
+            }
+
+            // Return true if required subscription is present in associated subscriptions list.
+            for (SubscriptionInfo subInfo: associatedSubscriptionsList) {
+                if (subInfo.getSubscriptionId() == subscriptionId){
+                    return true;
+                }
+            }
+            return false;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Get list of subscriptions associated with user.
+     *
+     * If user handle is associated with some subscriptions, return subscriptionsAssociatedWithUser
+     * else return all the subscriptions which are not associated with any user.
+     *
+     * @param userHandle user handle of the user
+     * @return list of subscriptionInfo associated with the user.
+     *
+     * @throws SecurityException if the caller doesn't have permissions required.
+     * @throws IllegalStateException if subscription service is not available.
+     *
+     */
+    @Override
+    public @NonNull List<SubscriptionInfo> getSubscriptionInfoListAssociatedWithUser(
+            @NonNull UserHandle userHandle) {
+        enforcePermissions("getSubscriptionInfoListAssociatedWithUser",
+                Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION);
+
+        long token = Binder.clearCallingIdentity();
+        try {
+            List<SubscriptionInfo> subInfoList =  getAllSubInfoList(
+                    mContext.getOpPackageName(), mContext.getAttributionTag());
+            if (subInfoList == null || subInfoList.isEmpty()) {
+                return new ArrayList<>();
+            }
+
+            List<SubscriptionInfo> subscriptionsAssociatedWithUser = new ArrayList<>();
+            List<SubscriptionInfo> subscriptionsWithNoAssociation = new ArrayList<>();
+            for (SubscriptionInfo subInfo : subInfoList) {
+                int subId = subInfo.getSubscriptionId();
+                UserHandle subIdUserHandle = getSubscriptionUserHandle(subId);
+                if (userHandle.equals(subIdUserHandle)) {
+                    // Store subscriptions whose user handle matches with required user handle.
+                    subscriptionsAssociatedWithUser.add(subInfo);
+                } else if (subIdUserHandle == null) {
+                    // Store subscriptions whose user handle is set to null.
+                    subscriptionsWithNoAssociation.add(subInfo);
+                }
+            }
+
+            return subscriptionsAssociatedWithUser.isEmpty() ?
+                    subscriptionsWithNoAssociation : subscriptionsAssociatedWithUser;
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    /**
+     * Register the callback for receiving information from {@link SubscriptionManagerService}.
+     *
+     * @param callback The callback.
+     */
+    public void registerCallback(@NonNull SubscriptionManagerServiceCallback callback) {
+        mSubscriptionManagerServiceCallbacks.add(callback);
+    }
+
+    /**
+     * Unregister the previously registered {@link SubscriptionManagerServiceCallback}.
+     *
+     * @param callback The callback to unregister.
+     */
+    public void unregisterCallback(@NonNull SubscriptionManagerServiceCallback callback) {
+        mSubscriptionManagerServiceCallbacks.remove(callback);
+    }
+
+    /**
+     * Enforce callers have any of the provided permissions.
+     *
+     * @param message Message to include in the exception.
+     * @param permissions The permissions to enforce.
+     *
+     * @throws SecurityException if the caller does not have any permissions.
+     */
+    private void enforcePermissions(@Nullable String message, @NonNull String ...permissions) {
+        for (String permission : permissions) {
+            if (mContext.checkCallingOrSelfPermission(permission)
+                    == PackageManager.PERMISSION_GRANTED) {
+                return;
+            }
+        }
+        throw new SecurityException(message + ". Does not have any of the following permissions. "
+                + Arrays.toString(permissions));
+    }
+
+    /**
+     * Get the {@link SubscriptionInfoInternal} by subscription id.
+     *
+     * @param subId The subscription id.
+     *
+     * @return The subscription info. {@code null} if not found.
+     */
+    @Nullable
+    public SubscriptionInfoInternal getSubscriptionInfoInternal(int subId) {
+        return mSubscriptionDatabaseManager.getSubscriptionInfoInternal(subId);
+    }
+
+    /**
+     * Get the {@link SubscriptionInfo} by subscription id.
+     *
+     * @param subId The subscription id.
+     *
+     * @return The subscription info. {@code null} if not found.
+     */
+    @Nullable
+    public SubscriptionInfo getSubscriptionInfo(int subId) {
+        SubscriptionInfoInternal subscriptionInfoInternal = getSubscriptionInfoInternal(subId);
+        return subscriptionInfoInternal != null
+                ? subscriptionInfoInternal.toSubscriptionInfo() : null;
+    }
+
+    /**
+     * Called when eSIM becomes inactive.
+     *
+     * @param slotIndex The logical SIM slot index.
+     */
+    public void updateSimStateForInactivePort(int slotIndex) {
+        mHandler.post(() -> {
+            if (mSlotIndexToSubId.containsKey(slotIndex)) {
+                // Re-enable the UICC application , so it will be in enabled state when it becomes
+                // active again. (pre-U behavior)
+                mSubscriptionDatabaseManager.setUiccApplicationsEnabled(
+                        mSlotIndexToSubId.get(slotIndex), true);
+                updateSubscriptions(slotIndex);
+            }
+        });
+    }
+
+    /**
+     * Update SIM state. This method is supposed to be called by {@link UiccController} only.
+     *
+     * @param slotIndex The logical SIM slot index.
+     * @param simState SIM state.
+     * @param executor The executor to execute the callback.
+     * @param updateCompleteCallback The callback to call when subscription manager service
+     * completes subscription update. SIM state changed event will be broadcasted by
+     * {@link UiccController} upon receiving callback.
+     */
+    public void updateSimState(int slotIndex, @SimState int simState,
+            @Nullable @CallbackExecutor Executor executor,
+            @Nullable Runnable updateCompleteCallback) {
+        mHandler.post(() -> {
+            mSimState[slotIndex] = simState;
+            switch (simState) {
+                case TelephonyManager.SIM_STATE_ABSENT:
+                case TelephonyManager.SIM_STATE_PIN_REQUIRED:
+                case TelephonyManager.SIM_STATE_PUK_REQUIRED:
+                case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
+                case TelephonyManager.SIM_STATE_PERM_DISABLED:
+                case TelephonyManager.SIM_STATE_READY:
+                case TelephonyManager.SIM_STATE_CARD_IO_ERROR:
+                case TelephonyManager.SIM_STATE_LOADED:
+                case TelephonyManager.SIM_STATE_NOT_READY:
+                    updateSubscriptions(slotIndex);
+                    break;
+                case TelephonyManager.SIM_STATE_CARD_RESTRICTED:
+                default:
+                    // No specific things needed to be done. Just return and broadcast the SIM
+                    // states.
+                    break;
+            }
+            if (executor != null && updateCompleteCallback != null) {
+                executor.execute(updateCompleteCallback);
+            }
+        });
+    }
+
+    /**
+     * Log debug messages.
+     *
+     * @param s debug messages
+     */
+    private void log(@NonNull String s) {
+        Rlog.d(LOG_TAG, s);
+    }
+
+    /**
+     * Log error messages.
+     *
+     * @param s error messages
+     */
+    private void loge(@NonNull String s) {
+        Rlog.e(LOG_TAG, s);
+    }
+
+    /**
+     * Log verbose messages.
+     *
+     * @param s debug messages.
+     */
+    private void logv(@NonNull String s) {
+        if (VDBG) Rlog.v(LOG_TAG, s);
+    }
+
+    /**
+     * Log debug messages and also log into the local log.
+     *
+     * @param s debug messages
+     */
+    private void logl(@NonNull String s) {
+        log(s);
+        mLocalLog.log(s);
+    }
+
+    /**
+     * Dump the state of {@link SubscriptionManagerService}.
+     *
+     * @param fd File descriptor
+     * @param printWriter Print writer
+     * @param args Arguments
+     */
+    public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter printWriter,
+            @NonNull String[] args) {
+        IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, "  ");
+        pw.println(SubscriptionManagerService.class.getSimpleName() + ":");
+        pw.println("Logical SIM slot sub id mapping:");
+        pw.increaseIndent();
+        mSlotIndexToSubId.forEach((slotIndex, subId)
+                -> pw.println("Logical slot " + slotIndex + ": subId=" + subId));
+        pw.increaseIndent();
+
+        pw.println("defaultSubId=" + getDefaultSubId());
+        pw.println("defaultVoiceSubId=" + getDefaultVoiceSubId());
+        pw.println("defaultDataSubId=" + getDefaultDataSubId());
+        pw.println("activeDataSubId=" + getActiveDataSubscriptionId());
+        pw.println("defaultSmsSubId=" + getDefaultSmsSubId());
+
+        pw.println("Active subscriptions:");
+        pw.increaseIndent();
+        mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                .filter(SubscriptionInfoInternal::isActive).forEach(pw::println);
+        pw.decreaseIndent();
+        pw.println("Embedded subscriptions:");
+        pw.increaseIndent();
+        mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                .filter(SubscriptionInfoInternal::isEmbedded).forEach(pw::println);
+        pw.decreaseIndent();
+        pw.println("Opportunistic subscriptions:");
+        pw.increaseIndent();
+        mSubscriptionDatabaseManager.getAllSubscriptions().stream()
+                .filter(SubscriptionInfoInternal::isOpportunistic).forEach(pw::println);
+        pw.decreaseIndent();
+        pw.println("All subscriptions:");
+        pw.increaseIndent();
+        mSubscriptionDatabaseManager.getAllSubscriptions().forEach(pw::println);
+        pw.decreaseIndent();
+
+        if (mEuiccManager != null) {
+            pw.println("Euicc enabled=" + mEuiccManager.isEnabled());
+        }
+        pw.decreaseIndent();
+    }
+}
diff --git a/src/java/com/android/internal/telephony/uicc/AdnRecordCache.java b/src/java/com/android/internal/telephony/uicc/AdnRecordCache.java
index 21a6e37..90c9491 100644
--- a/src/java/com/android/internal/telephony/uicc/AdnRecordCache.java
+++ b/src/java/com/android/internal/telephony/uicc/AdnRecordCache.java
@@ -23,10 +23,12 @@
 import android.os.Message;
 import android.util.SparseArray;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.gsm.UsimPhoneBookManager;
 
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.Locale;
 
 /**
  * {@hide}
@@ -58,14 +60,16 @@
     static final int EVENT_UPDATE_ADN_DONE = 2;
 
     //***** Constructor
-
-
-
     AdnRecordCache(IccFileHandler fh) {
         mFh = fh;
         mUsimPhoneBookManager = new UsimPhoneBookManager(mFh, this);
     }
 
+    public AdnRecordCache(IccFileHandler fh, UsimPhoneBookManager usimPhoneBookManager) {
+        mFh = fh;
+        mUsimPhoneBookManager = usimPhoneBookManager;
+    }
+
     //***** Called from SIMRecords
 
     /**
@@ -156,14 +160,14 @@
         int extensionEF = extensionEfForEf(efid);
         if (extensionEF < 0) {
             sendErrorResponse(response, "EF is not known ADN-like EF:0x" +
-                    Integer.toHexString(efid).toUpperCase());
+                    Integer.toHexString(efid).toUpperCase(Locale.ROOT));
             return;
         }
 
         Message pendingResponse = mUserWriteResponse.get(efid);
         if (pendingResponse != null) {
             sendErrorResponse(response, "Have pending update for EF:0x" +
-                    Integer.toHexString(efid).toUpperCase());
+                    Integer.toHexString(efid).toUpperCase(Locale.ROOT));
             return;
         }
 
@@ -190,16 +194,14 @@
      */
     public void updateAdnBySearch(int efid, AdnRecord oldAdn, AdnRecord newAdn,
             String pin2, Message response) {
-
         int extensionEF;
         extensionEF = extensionEfForEf(efid);
 
         if (extensionEF < 0) {
             sendErrorResponse(response, "EF is not known ADN-like EF:0x" +
-                    Integer.toHexString(efid).toUpperCase());
+                    Integer.toHexString(efid).toUpperCase(Locale.ROOT));
             return;
         }
-
         ArrayList<AdnRecord>  oldAdnList;
 
         if (efid == EF_PBR) {
@@ -207,13 +209,11 @@
         } else {
             oldAdnList = getRecordsIfLoaded(efid);
         }
-
         if (oldAdnList == null) {
             sendErrorResponse(response, "Adn list not exist for EF:0x" +
-                    Integer.toHexString(efid).toUpperCase());
+                    Integer.toHexString(efid).toUpperCase(Locale.ROOT));
             return;
         }
-
         int index = -1;
         int count = 1;
         for (Iterator<AdnRecord> it = oldAdnList.iterator(); it.hasNext(); ) {
@@ -223,7 +223,6 @@
             }
             count++;
         }
-
         if (index == -1) {
             sendErrorResponse(response, "Adn record don't exist for " + oldAdn);
             return;
@@ -244,7 +243,7 @@
 
         if (pendingResponse != null) {
             sendErrorResponse(response, "Have pending update for EF:0x" +
-                    Integer.toHexString(efid).toUpperCase());
+                    Integer.toHexString(efid).toUpperCase(Locale.ROOT));
             return;
         }
 
@@ -307,7 +306,7 @@
             if (response != null) {
                 AsyncResult.forMessage(response).exception
                     = new RuntimeException("EF is not known ADN-like EF:0x" +
-                        Integer.toHexString(efid).toUpperCase());
+                        Integer.toHexString(efid).toUpperCase(Locale.ROOT));
                 response.sendToTarget();
             }
 
@@ -342,7 +341,6 @@
     handleMessage(Message msg) {
         AsyncResult ar;
         int efid;
-
         switch(msg.what) {
             case EVENT_LOAD_ALL_ADN_LIKE_DONE:
                 /* arg1 is efid, obj.result is ArrayList<AdnRecord>*/
@@ -381,4 +379,24 @@
                 break;
         }
     }
+
+    @VisibleForTesting
+    protected void setAdnLikeWriters(int key, ArrayList<Message> waiters) {
+        mAdnLikeWaiters.put(EF_MBDN, waiters);
+    }
+
+    @VisibleForTesting
+    protected void setAdnLikeFiles(int key, ArrayList<AdnRecord> adnRecordList) {
+        mAdnLikeFiles.put(EF_MBDN, adnRecordList);
+    }
+
+    @VisibleForTesting
+    protected void setUserWriteResponse(int key, Message message) {
+        mUserWriteResponse.put(EF_MBDN, message);
+    }
+
+    @VisibleForTesting
+    protected UsimPhoneBookManager getUsimPhoneBookManager() {
+        return mUsimPhoneBookManager;
+    }
 }
diff --git a/src/java/com/android/internal/telephony/uicc/AdnRecordLoader.java b/src/java/com/android/internal/telephony/uicc/AdnRecordLoader.java
index a688c6e..5b0a6a3 100644
--- a/src/java/com/android/internal/telephony/uicc/AdnRecordLoader.java
+++ b/src/java/com/android/internal/telephony/uicc/AdnRecordLoader.java
@@ -59,6 +59,7 @@
     static final int EVENT_EF_LINEAR_RECORD_SIZE_DONE = 4;
     static final int EVENT_UPDATE_RECORD_DONE = 5;
 
+    static final int VOICEMAIL_ALPHATAG_ARG = 1;
     //***** Constructor
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@@ -177,14 +178,34 @@
                     data = adn.buildAdnString(recordSize[0]);
 
                     if(data == null) {
-                        throw new RuntimeException("wrong ADN format",
-                                ar.exception);
+                        /**
+                         * The voicemail number saving to the SIM is in name(alphaTag) and number
+                         * format. {@link recordSize[0]} indicates the SIM EF memory size that the
+                         * sim can have to save both voicemail name and number. 14 byte of memory
+                         * is reserved to save the voicemail number and remaining memory is reserved
+                         * for the alphaTag. In case if we receive the alphaTag which is more than
+                         * the reserved memory size then SIM will throw the exception and it don't
+                         * save both the voicemail number and alphaTag. To avoid this problem, in
+                         * case alphaTag length is more we nullify the alphaTag and save the same.
+                         */
+                        if (mUserResponse.arg1 == VOICEMAIL_ALPHATAG_ARG) {
+                            adn.mAlphaTag = null;
+                            data = adn.buildAdnString(recordSize[0]);
+                        }
+                        if (data == null) {
+                            throw new RuntimeException("wrong ADN format",
+                                    ar.exception);
+                        }
                     }
 
-
-                    mFh.updateEFLinearFixed(mEf, getEFPath(mEf), mRecordNumber,
-                            data, mPin2, obtainMessage(EVENT_UPDATE_RECORD_DONE));
-
+                    // Send adn record to caller to track the changes made to alphaTag
+                    if (mUserResponse.arg1 == VOICEMAIL_ALPHATAG_ARG) {
+                        mFh.updateEFLinearFixed(mEf, getEFPath(mEf), mRecordNumber,
+                                data, mPin2, obtainMessage(EVENT_UPDATE_RECORD_DONE, adn));
+                    } else {
+                        mFh.updateEFLinearFixed(mEf, getEFPath(mEf), mRecordNumber,
+                                data, mPin2, obtainMessage(EVENT_UPDATE_RECORD_DONE));
+                    }
                     mPendingExtLoads = 1;
 
                     break;
@@ -195,7 +216,12 @@
                                 ar.exception);
                     }
                     mPendingExtLoads = 0;
-                    mResult = null;
+                    // send the adn record back to caller through the result of AsyncResult
+                    if (mUserResponse.arg1 == VOICEMAIL_ALPHATAG_ARG) {
+                        mResult =  ar.userObj;
+                    } else {
+                        mResult = null;
+                    }
                     break;
                 case EVENT_ADN_LOAD_DONE:
                     ar = (AsyncResult)(msg.obj);
diff --git a/src/java/com/android/internal/telephony/uicc/IccCardStatus.java b/src/java/com/android/internal/telephony/uicc/IccCardStatus.java
index ec07780..e2cc9e9 100644
--- a/src/java/com/android/internal/telephony/uicc/IccCardStatus.java
+++ b/src/java/com/android/internal/telephony/uicc/IccCardStatus.java
@@ -20,6 +20,7 @@
 import android.os.Build;
 import android.telephony.SubscriptionInfo;
 
+import com.android.internal.telephony.uicc.IccSlotStatus.MultipleEnabledProfilesMode;
 import com.android.internal.telephony.util.TelephonyUtils;
 import com.android.telephony.Rlog;
 
@@ -90,6 +91,30 @@
 
     public IccSlotPortMapping mSlotPortMapping;
 
+    public MultipleEnabledProfilesMode mSupportedMepMode = MultipleEnabledProfilesMode.NONE;
+
+    /**
+     * Set the MultipleEnabledProfilesMode according to the input mode.
+     */
+    public void setMultipleEnabledProfilesMode(int mode) {
+        switch(mode) {
+            case 0:
+                mSupportedMepMode = MultipleEnabledProfilesMode.NONE;
+                break;
+            case 1:
+                mSupportedMepMode = MultipleEnabledProfilesMode.MEP_A1;
+                break;
+            case 2:
+                mSupportedMepMode = MultipleEnabledProfilesMode.MEP_A2;
+                break;
+            case 3:
+                mSupportedMepMode = MultipleEnabledProfilesMode.MEP_B;
+                break;
+            default:
+                throw new RuntimeException("Unrecognized RIL_MultipleEnabledProfilesMode: " + mode);
+        }
+    }
+
     public void setCardState(int state) {
         switch(state) {
         case 0:
@@ -174,6 +199,7 @@
         sb.append(",atr=").append(atr);
         sb.append(",iccid=").append(SubscriptionInfo.givePrintableIccid(iccid));
         sb.append(",eid=").append(Rlog.pii(TelephonyUtils.IS_DEBUGGABLE, eid));
+        sb.append(",SupportedMepMode=").append(mSupportedMepMode);
         sb.append(",SlotPortMapping=").append(mSlotPortMapping);
 
         sb.append("}");
diff --git a/src/java/com/android/internal/telephony/uicc/IccConstants.java b/src/java/com/android/internal/telephony/uicc/IccConstants.java
index 5eae070..a9275be 100644
--- a/src/java/com/android/internal/telephony/uicc/IccConstants.java
+++ b/src/java/com/android/internal/telephony/uicc/IccConstants.java
@@ -45,6 +45,8 @@
     static final int EF_SST = 0x6F38;
     static final int EF_CFIS = 0x6FCB;
     static final int EF_IMG = 0x4F20;
+    static final int EF_PSISMSC = 0x6FE5;
+    static final int EF_SMSS = 0x6F43;
 
     // USIM SIM file ids from TS 131.102
     public static final int EF_PBR = 0x4F30;
@@ -84,7 +86,6 @@
     static final int EF_DOMAIN = 0x6F03;
     static final int EF_IST = 0x6F07;
     static final int EF_PCSCF = 0x6F09;
-    static final int EF_PSI = 0x6FE5;
 
     //PLMN Selection Information w/ Access Technology TS 131.102
     static final int EF_PLMN_W_ACT = 0x6F60;
diff --git a/src/java/com/android/internal/telephony/uicc/IccFileHandler.java b/src/java/com/android/internal/telephony/uicc/IccFileHandler.java
index f3b1d0b..5f8ccb4 100644
--- a/src/java/com/android/internal/telephony/uicc/IccFileHandler.java
+++ b/src/java/com/android/internal/telephony/uicc/IccFileHandler.java
@@ -22,6 +22,7 @@
 import android.os.Handler;
 import android.os.Message;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.CommandsInterface;
 
 import java.util.ArrayList;
@@ -108,7 +109,7 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     protected final String mAid;
 
-    static class LoadLinearFixedContext {
+    public static class LoadLinearFixedContext {
 
         int mEfid;
         @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@@ -145,14 +146,11 @@
             mOnLoaded = onLoaded;
             mPath = path;
         }
+    }
 
-        LoadLinearFixedContext(int efid, Message onLoaded) {
-            mEfid = efid;
-            mRecordNum = 1;
-            mLoadAll = true;
-            mOnLoaded = onLoaded;
-            mPath = null;
-        }
+    @VisibleForTesting
+    public int getEfid(LoadLinearFixedContext lc) {
+        return lc.mEfid;
     }
 
     /**
@@ -164,6 +162,13 @@
         mCi = ci;
     }
 
+    @VisibleForTesting
+    public IccFileHandler(CommandsInterface ci) {
+        mParentApp = null;
+        mAid = null;
+        mCi = ci;
+    }
+
     public void dispose() {
     }
 
@@ -267,8 +272,7 @@
      * @param path Path of the EF on the card
      * @param onLoaded ((AsnyncResult)(onLoaded.obj)).result is the size of data int
      */
-    public void getEFTransparentRecordSize(int fileid, String path, Message onLoaded) {
-        String efPath = (path == null) ? getEFPath(fileid) : path;
+    public void getEFTransparentRecordSize(int fileid, Message onLoaded) {
         Message response = obtainMessage(EVENT_GET_EF_TRANSPARENT_SIZE_DONE, fileid, 0, onLoaded);
         mCi.iccIOForApp(
                 COMMAND_GET_RESPONSE,
@@ -284,16 +288,6 @@
     }
 
     /**
-     * Get record size for a transparent EF
-     *
-     * @param fileid EF id
-     * @param onLoaded ((AsnyncResult)(onLoaded.obj)).result is the size of the data int
-     */
-    public void getEFTransparentRecordSize(int fileid, Message onLoaded) {
-        getEFTransparentRecordSize(fileid, getEFPath(fileid), onLoaded);
-    }
-
-    /**
      * Load all records from a SIM Linear Fixed EF
      *
      * @param fileid EF id
@@ -693,7 +687,7 @@
         case EF_EXT1:
         case EF_EXT2:
         case EF_EXT3:
-        case EF_PSI:
+        case EF_PSISMSC:
             return MF_SIM + DF_TELECOM;
 
         case EF_ICCID:
diff --git a/src/java/com/android/internal/telephony/uicc/IccRecords.java b/src/java/com/android/internal/telephony/uicc/IccRecords.java
index 3a7db7f..da112b1 100644
--- a/src/java/com/android/internal/telephony/uicc/IccRecords.java
+++ b/src/java/com/android/internal/telephony/uicc/IccRecords.java
@@ -30,19 +30,23 @@
 import android.telephony.SubscriptionInfo;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
+import android.util.Log;
 import android.util.Pair;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.MccTable;
+import com.android.internal.telephony.gsm.SimTlv;
 import com.android.internal.telephony.util.ArrayUtils;
 import com.android.telephony.Rlog;
 
 import java.io.FileDescriptor;
+import java.io.FileNotFoundException;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
+import java.nio.charset.Charset;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Objects;
@@ -53,8 +57,11 @@
  * {@hide}
  */
 public abstract class IccRecords extends Handler implements IccConstants {
+    private static final String LOG_TAG = "IccRecords";
     protected static final boolean DBG = true;
-    protected static final boolean VDBG = false; // STOPSHIP if true
+    private static final boolean FORCE_VERBOSE_STATE_LOGGING = false; /* stopship if true */
+    protected static final boolean VDBG =  FORCE_VERBOSE_STATE_LOGGING ||
+            Rlog.isLoggable(LOG_TAG, Log.VERBOSE);
 
     public static final int PLMN_MIN_LENGTH = CellIdentity.MCC_LENGTH
             + CellIdentity.MNC_MIN_LENGTH;
@@ -119,6 +126,10 @@
 
     protected boolean mRecordsRequested = false; // true if we've made requests for the sim records
     protected int mLockedRecordsReqReason = LOCKED_RECORDS_REQ_REASON_NONE;
+    // EF_SMSS fields tpmr invalid, min and max declarations
+    protected static final int SMSS_INVALID_TPMR = -1;
+    private static final int TPMR_MIN = 0x00;
+    private static final int TPMR_MAX = 0xFF;
 
     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PROTECTED)
     public String mIccId;  // Includes only decimals (no hex)
@@ -173,6 +184,14 @@
     protected String[] mEhplmns;
     protected String[] mFplmns;
 
+    // SIP or TEL URI [ Public Service Identity of the SM-SC]
+    // Reference: TS 31.102 section 4.5.9
+    protected String mPsiSmsc;
+
+    // EF_SMSS value which is combination of TPMR and Memory exceed flag
+    // Reference: TS 31.102 section 4.2.9
+    protected byte[] mSmssValues;
+
     CarrierTestOverride mCarrierTestOverride;
 
     //Arbitrary offset for the Handler
@@ -232,6 +251,12 @@
     // arrive and returning null to the callers.
     private static final long ICC_SIM_CHALLENGE_TIMEOUT_MILLIS = 2500;
 
+    // TAG value to retrieve EF_PSISMSC from parsed SimTlv object
+    private static final int TAG_TLV_USIM_VALUE_80 = 0x80;
+
+    // call back received on this upon EF_SMSS record update.
+    public static final int EVENT_SET_SMSS_RECORD_DONE = 201;
+
     /**
      * There are two purposes for this class. First, each instance of AuthAsyncResponse acts as a
      * lock to for calling thead to wait in getIccSimChallengeResponse(). Second, pass the IMS
@@ -954,6 +979,26 @@
                 }
                 break;
 
+            case EVENT_SET_SMSS_RECORD_DONE:
+                ar = (AsyncResult) msg.obj;
+                SmssRecord smssRecord = null;
+                if (ar.userObj != null) {
+                    smssRecord = (SmssRecord) ar.userObj;
+                }
+                if (ar.exception == null && smssRecord.getSmssValue() != null) {
+                    mSmssValues = smssRecord.getSmssValue().clone();
+                } else {
+                    loge("SIM EF_SMSS field updating error=" + ar.exception);
+                }
+                if (smssRecord != null && smssRecord.getMessage() != null) {
+                    Message message = smssRecord.getMessage();
+                    AsyncResult.forMessage(message, ar.result, ar.exception);
+                    message.sendToTarget();
+                } else {
+                    loge("smssRecord or smssRecord.getMessage() object is null");
+                }
+                break;
+
             default:
                 super.handleMessage(msg);
         }
@@ -1306,6 +1351,34 @@
         return mSmsCountOnIcc;
     }
 
+    /**
+     * parse EF PSISMSC value [3GPP TS 31.102 Section 4.5.9]
+     *
+     * @param data read from EF PSISMSC field of type byte[]
+     * @return SIP URI or tel URI of type string
+     */
+    protected String parseEfPsiSmsc(byte[] data) {
+        SimTlv tlv = new SimTlv(data, 0, data.length);
+        if (tlv.isValidObject() && tlv.getData() != null) {
+            if (tlv.getTag() == TAG_TLV_USIM_VALUE_80) {
+                return new String(tlv.getData(), Charset.forName("UTF-8"));
+            }
+        }
+        if (VDBG) {
+            log("Can't find EF PSISMSC field in SIM = " + IccUtils.bytesToHexString(data));
+        }
+        return null;
+    }
+
+    /**
+     * SMSC address read from the elementary file EF_PSISMSC
+     *
+     * @return SIP URI or tel URI of type string
+     */
+    public String getSmscIdentity() {
+        return mPsiSmsc;
+    }
+
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         pw.println("IccRecords: " + this);
         pw.println(" mDestroyed=" + mDestroyed);
@@ -1532,4 +1605,73 @@
             return "{fullName = " + fullName + ", shortName = " + shortName + "}";
         }
     }
+
+    /**
+     * Sets the elementary (EF_SMSS) field with latest last used TP-Message reference value.
+     * First byte of EF_SMSS represents the TPMR value as per the spec
+     * (Section 4.2.9 of 3GPP TS 31.102)
+     *
+     * @param tpmr: Last used TP-Message reference parameter of type int
+     * @param onComplete: android.os.Message to be notified upon completion
+     */
+    public void setSmssTpmrValue(int tpmr, Message onComplete) {
+        if(VDBG) log("setSmssTpmrValue()");
+        if (mSmssValues != null && mSmssValues.length > 0 && tpmr >= TPMR_MIN && tpmr <= TPMR_MAX) {
+            byte[] tempSmss = mSmssValues.clone();
+            tempSmss[0] = (byte) (tpmr & 0xFF);
+            SmssRecord smssRecord = createSmssRecord(onComplete, tempSmss);
+            mFh.updateEFTransparent(IccConstants.EF_SMSS, tempSmss,
+                    obtainMessage(EVENT_SET_SMSS_RECORD_DONE, smssRecord));
+        } else if (onComplete != null) {
+            loge("Failed to set EF_SMSS [TPMR] field to SIM");
+            if (mSmssValues == null || mSmssValues.length <= 0) {
+                AsyncResult.forMessage((onComplete)).exception =
+                        new FileNotFoundException("EF_SMSS file not found");
+            } else if (tpmr < TPMR_MIN || tpmr > TPMR_MAX) {
+                AsyncResult.forMessage((onComplete)).exception =
+                        new IllegalArgumentException("TPMR value is not in allowed range");
+            }
+            onComplete.sendToTarget();
+        }
+    }
+
+    /**
+     * Fetches the last used TPMR value from elementary (EF_SMSS) field. First byte of EF_SMSS
+     * represents the TPMR value as per the spec (Section 4.2.9 of 3GPP TS 31.102)
+     *
+     * @return TP-Message reference parameter of type int, -1 in case if it fails to read the
+     * EF_SMSS field from the sim.
+     */
+    public int getSmssTpmrValue() {
+        if (mSmssValues != null && mSmssValues.length > 0) {
+            return  (mSmssValues[0] & 0xFF);
+        }
+        loge("IccRecords - EF_SMSS is null");
+        return SMSS_INVALID_TPMR;
+    }
+
+    @VisibleForTesting
+    public SmssRecord createSmssRecord(Message msg, byte[] smss) {
+        return new SmssRecord(msg, smss);
+    }
+
+
+    static class SmssRecord {
+
+        private Message mMsg;
+        private byte[] mSmss;
+
+        SmssRecord (Message msg, byte[] smss) {
+            mMsg = msg;
+            mSmss = smss;
+        }
+
+        private byte[] getSmssValue() {
+            return mSmss;
+        }
+
+        private Message getMessage() {
+            return mMsg;
+        }
+    }
 }
diff --git a/src/java/com/android/internal/telephony/uicc/IccSimPortInfo.java b/src/java/com/android/internal/telephony/uicc/IccSimPortInfo.java
index 9a5e10d..7197fc8 100644
--- a/src/java/com/android/internal/telephony/uicc/IccSimPortInfo.java
+++ b/src/java/com/android/internal/telephony/uicc/IccSimPortInfo.java
@@ -16,7 +16,6 @@
 
 package com.android.internal.telephony.uicc;
 
-import android.telephony.SubscriptionInfo;
 import android.text.TextUtils;
 
 import java.util.Objects;
@@ -50,9 +49,7 @@
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder();
-        sb.append("{").append("iccid=")
-                .append(SubscriptionInfo.givePrintableIccid(mIccId)).append(",")
-                .append("logicalSlotIndex=").append(mLogicalSlotIndex).append(",")
+        sb.append("{").append("logicalSlotIndex=").append(mLogicalSlotIndex).append(",")
                 .append("portActive=").append(mPortActive)
                 .append("}");
         return sb.toString();
diff --git a/src/java/com/android/internal/telephony/uicc/IccSlotStatus.java b/src/java/com/android/internal/telephony/uicc/IccSlotStatus.java
index 96a3a33..3bbef23 100644
--- a/src/java/com/android/internal/telephony/uicc/IccSlotStatus.java
+++ b/src/java/com/android/internal/telephony/uicc/IccSlotStatus.java
@@ -30,11 +30,42 @@
     /* Added state active to check slotState in old HAL case.*/
     public static final int STATE_ACTIVE = 1;
 
+    public enum MultipleEnabledProfilesMode {
+        /**
+         * If there is no jointly supported MEP mode, set supported MEP mode to NONE.
+         */
+        NONE,
+        /**
+         * In case of MEP-A1, the ISD-R is selected on eSIM port 0 only and profiles are selected
+         * on eSIM ports 1 and higher, with the eSIM port being assigned by the LPA or platform.
+         */
+        MEP_A1,
+        /**
+         * In case of MEP-A2, the ISD-R is selected on eSIM port 0 only and profiles are selected
+         * on eSIM ports 1 and higher, with the eSIM port being assigned by the eUICC.
+         */
+        MEP_A2,
+        /**
+         * In case of MEP-B, profiles are selected on eSIM ports 0 and higher, with the ISD-R being
+         * selectable on any of these eSIM ports.
+         */
+        MEP_B;
+
+        public boolean isMepAMode() {
+            return (this == MEP_A1 || this == MEP_A2);
+        }
+
+        public boolean isMepA1Mode() {
+            return this == MEP_A1;
+        }
+    }
+
     public IccCardStatus.CardState  cardState;
     public String     atr;
     public String     eid;
 
     public IccSimPortInfo[] mSimPortInfos;
+    public MultipleEnabledProfilesMode mSupportedMepMode = MultipleEnabledProfilesMode.NONE;
 
     /**
      * Set the cardState according to the input state.
@@ -58,6 +89,28 @@
         }
     }
 
+    /**
+     * Set the MultipleEnabledProfilesMode according to the input mode.
+     */
+    public void setMultipleEnabledProfilesMode(int mode) {
+        switch(mode) {
+            case 0:
+                mSupportedMepMode = MultipleEnabledProfilesMode.NONE;
+                break;
+            case 1:
+                mSupportedMepMode = MultipleEnabledProfilesMode.MEP_A1;
+                break;
+            case 2:
+                mSupportedMepMode = MultipleEnabledProfilesMode.MEP_A2;
+                break;
+            case 3:
+                mSupportedMepMode = MultipleEnabledProfilesMode.MEP_B;
+                break;
+            default:
+                throw new RuntimeException("Unrecognized RIL_MultipleEnabledProfilesMode: " + mode);
+        }
+    }
+
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder();
@@ -72,6 +125,7 @@
         } else {
             sb.append("num_ports=null");
         }
+        sb.append(", SupportedMepMode=" + mSupportedMepMode);
         sb.append("}");
         return sb.toString();
     }
diff --git a/src/java/com/android/internal/telephony/uicc/InstallCarrierAppUtils.java b/src/java/com/android/internal/telephony/uicc/InstallCarrierAppUtils.java
index 412295d..7666f4c 100644
--- a/src/java/com/android/internal/telephony/uicc/InstallCarrierAppUtils.java
+++ b/src/java/com/android/internal/telephony/uicc/InstallCarrierAppUtils.java
@@ -38,6 +38,7 @@
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.Locale;
 
 /**
  * Utility methods for installing the carrier app when a SIM is insterted without the carrier app
@@ -178,7 +179,7 @@
      */
     @VisibleForTesting
     public static String getAppNameFromPackageName(String packageName, String mapString) {
-        packageName = packageName.toLowerCase();
+        packageName = packageName.toLowerCase(Locale.ROOT);
         final String pairDelim = "\\s*;\\s*";
         final String keyValueDelim = "\\s*:\\s*";
 
diff --git a/src/java/com/android/internal/telephony/uicc/IsimFileHandler.java b/src/java/com/android/internal/telephony/uicc/IsimFileHandler.java
index fe900cb..4d5f8d1 100644
--- a/src/java/com/android/internal/telephony/uicc/IsimFileHandler.java
+++ b/src/java/com/android/internal/telephony/uicc/IsimFileHandler.java
@@ -38,6 +38,7 @@
         case EF_DOMAIN:
         case EF_IST:
         case EF_PCSCF:
+        case EF_SMSS:
             return MF_SIM + DF_ADF;
         }
         String path = getCommonIccEFPath(efid);
diff --git a/src/java/com/android/internal/telephony/uicc/IsimServiceTable.java b/src/java/com/android/internal/telephony/uicc/IsimServiceTable.java
new file mode 100644
index 0000000..289229c
--- /dev/null
+++ b/src/java/com/android/internal/telephony/uicc/IsimServiceTable.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.uicc;
+
+public final class IsimServiceTable extends IccServiceTable {
+    private static final String TAG = "IsimServiceTable";
+    public enum IsimService {
+        PCSCF_ADDRESS,
+        GBA,                                // Generic Bootstrapping Architecture (GBA)
+        HTTP_DIGEST,
+        GBA_LOCALKEY_ESTABLISHMENT,         // GBA-based Local Key Establishment Mechanism
+        PCSCF_DISCOVERY_FOR_IMS,            // Support of P-CSCF discovery for IMS Local Break Out
+        SMS,
+        SMSR,                               // Short Message Status Reports
+        SM_OVERIP_AND_DATA_DL_VIA_SMS_PP,   // Support for SM-over-IP including data download via
+        // SMS-PP
+        COMMUNICATION_CONTROL_FOR_IMS_BY_ISIM,
+        UICC_ACCESS_TO_IMS
+    }
+
+    public IsimServiceTable(byte[] table) {
+        super(table);
+    }
+
+    public boolean isAvailable(IsimService service) {
+        return super.isAvailable(service.ordinal());
+    }
+
+    @Override
+    protected String getTag() {
+        return TAG;
+    }
+
+    @Override
+    protected Object[] getValues() {
+        return IsimService.values();
+    }
+
+    public byte[] getISIMServiceTable() {
+        return mServiceTable;
+    }
+}
diff --git a/src/java/com/android/internal/telephony/uicc/IsimUiccRecords.java b/src/java/com/android/internal/telephony/uicc/IsimUiccRecords.java
index 12284af..9591a498 100644
--- a/src/java/com/android/internal/telephony/uicc/IsimUiccRecords.java
+++ b/src/java/com/android/internal/telephony/uicc/IsimUiccRecords.java
@@ -23,7 +23,9 @@
 import android.os.Build;
 import android.os.Message;
 import android.telephony.SubscriptionManager;
+import android.util.Log;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.gsm.SimTlv;
 import com.android.telephony.Rlog;
@@ -41,7 +43,9 @@
     protected static final String LOG_TAG = "IsimUiccRecords";
 
     private static final boolean DBG = true;
-    private static final boolean VDBG = false; // STOPSHIP if true
+    private static final boolean FORCE_VERBOSE_STATE_LOGGING = false; /* stopship if true */
+    private static final boolean VDBG =  FORCE_VERBOSE_STATE_LOGGING ||
+            Rlog.isLoggable(LOG_TAG, Log.VERBOSE);
     private static final boolean DUMP_RECORDS = false;  // Note: PII is logged when this is true
                                                         // STOPSHIP if true
     public static final String INTENT_ISIM_REFRESH = "com.android.intent.isim_refresh";
@@ -69,7 +73,9 @@
                 + " mIsimDomain=" + mIsimDomain
                 + " mIsimImpu=" + Arrays.toString(mIsimImpu)
                 + " mIsimIst=" + mIsimIst
-                + " mIsimPcscf=" + Arrays.toString(mIsimPcscf)) : "");
+                + " mIsimPcscf=" + Arrays.toString(mIsimPcscf)
+                + " mPsiSmsc=" + mPsiSmsc
+                + " mSmss TPMR=" + getSmssTpmrValue()) : "");
     }
 
     public IsimUiccRecords(UiccCardApplication app, Context c, CommandsInterface ci) {
@@ -111,7 +117,6 @@
                     broadcastRefresh();
                     super.handleMessage(msg);
                     break;
-
                 default:
                     super.handleMessage(msg);   // IccRecords handles generic record load responses
 
@@ -143,6 +148,13 @@
         mFh.loadEFLinearFixedAll(EF_PCSCF, obtainMessage(
                     IccRecords.EVENT_GET_ICC_RECORD_DONE, new EfIsimPcscfLoaded()));
         mRecordsToLoad++;
+        mFh.loadEFTransparent(EF_SMSS,  obtainMessage(
+                IccRecords.EVENT_GET_ICC_RECORD_DONE, new EfIsimSmssLoaded()));
+        mRecordsToLoad++;
+
+        mFh.loadEFLinearFixed(EF_PSISMSC, 1, obtainMessage(
+                IccRecords.EVENT_GET_ICC_RECORD_DONE, new EfIsimPsiSmscLoaded()));
+        mRecordsToLoad++;
 
         if (DBG) log("fetchIsimRecords " + mRecordsToLoad + " requested: " + mRecordsRequested);
     }
@@ -212,6 +224,28 @@
             if (DUMP_RECORDS) log("EF_IST=" + mIsimIst);
         }
     }
+
+    @VisibleForTesting
+    public EfIsimIstLoaded getIsimIstObject() {
+        return new EfIsimIstLoaded();
+    }
+
+    private class EfIsimSmssLoaded implements IccRecords.IccRecordLoaded {
+
+        @Override
+        public String getEfName() {
+            return "EF_ISIM_SMSS";
+        }
+
+        @Override
+        public void onRecordLoaded(AsyncResult ar) {
+            mSmssValues = (byte[]) ar.result;
+            if (VDBG) {
+                log("IsimUiccRecords - EF_SMSS TPMR value = " + getSmssTpmrValue());
+            }
+        }
+    }
+
     private class EfIsimPcscfLoaded implements IccRecords.IccRecordLoaded {
         public String getEfName() {
             return "EF_ISIM_PCSCF";
@@ -229,6 +263,29 @@
         }
     }
 
+    private class EfIsimPsiSmscLoaded implements IccRecords.IccRecordLoaded {
+
+        @Override
+        public String getEfName() {
+            return "EF_ISIM_PSISMSC";
+        }
+
+        @Override
+        public void onRecordLoaded(AsyncResult ar) {
+            byte[] data = (byte[]) ar.result;
+            if (data != null && data.length > 0) {
+                mPsiSmsc = parseEfPsiSmsc(data);
+                if (VDBG) {
+                    log("IsimUiccRecords - EF_PSISMSC value = " + mPsiSmsc);
+                }
+            }
+        }
+    }
+
+    @VisibleForTesting
+    public EfIsimPsiSmscLoaded getPsiSmscObject() {
+        return new EfIsimPsiSmscLoaded();
+    }
     /**
      * ISIM records for IMS are stored inside a Tag-Length-Value record as a UTF-8 string
      * with tag value 0x80.
@@ -432,19 +489,28 @@
         pw.println("IsimRecords: " + this);
         pw.println(" extends:");
         super.dump(fd, pw, args);
+        pw.println(" mIsimServiceTable=" + getIsimServiceTable());
         if (DUMP_RECORDS) {
             pw.println(" mIsimImpi=" + mIsimImpi);
             pw.println(" mIsimDomain=" + mIsimDomain);
             pw.println(" mIsimImpu[]=" + Arrays.toString(mIsimImpu));
-            pw.println(" mIsimIst" + mIsimIst);
             pw.println(" mIsimPcscf" + Arrays.toString(mIsimPcscf));
+            pw.println(" mPsismsc=" + mPsiSmsc);
+            pw.println(" mSmss TPMR=" + getSmssTpmrValue());
         }
         pw.flush();
     }
 
+    // Just to return the Enums of service table to print in DUMP
+    private IsimServiceTable getIsimServiceTable() {
+        if (mIsimIst != null) {
+            byte[] istTable = IccUtils.hexStringToBytes(mIsimIst);
+            return new IsimServiceTable(istTable);
+        }
+        return null;
+    }
     @Override
     public int getVoiceMessageCount() {
         return 0; // Not applicable to Isim
     }
-
-}
+}
\ No newline at end of file
diff --git a/src/java/com/android/internal/telephony/uicc/PinStorage.java b/src/java/com/android/internal/telephony/uicc/PinStorage.java
index 1154e0f..ed16ee4 100644
--- a/src/java/com/android/internal/telephony/uicc/PinStorage.java
+++ b/src/java/com/android/internal/telephony/uicc/PinStorage.java
@@ -61,7 +61,6 @@
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.PhoneFactory;
-import com.android.internal.telephony.SubscriptionInfoUpdater;
 import com.android.internal.telephony.TelephonyStatsLog;
 import com.android.internal.telephony.nano.StoredPinProto.EncryptedPin;
 import com.android.internal.telephony.nano.StoredPinProto.StoredPin;
@@ -519,8 +518,8 @@
 
     /** Handle the update of the {@code state} of the SIM card in {@code slotId}. */
     private synchronized void onSimStatusChange(int slotId, @SimState int state) {
-        logd("SIM card/application changed[%d]: %s",
-                slotId, SubscriptionInfoUpdater.simStateString(state));
+        logd("SIM card/application changed[%d]: %s", slotId,
+                TelephonyManager.simStateToString(state));
         switch (state) {
             case TelephonyManager.SIM_STATE_ABSENT:
             case TelephonyManager.SIM_STATE_PIN_REQUIRED: {
diff --git a/src/java/com/android/internal/telephony/uicc/PlmnActRecord.java b/src/java/com/android/internal/telephony/uicc/PlmnActRecord.java
old mode 100755
new mode 100644
diff --git a/src/java/com/android/internal/telephony/uicc/PortUtils.java b/src/java/com/android/internal/telephony/uicc/PortUtils.java
new file mode 100644
index 0000000..4a18b56
--- /dev/null
+++ b/src/java/com/android/internal/telephony/uicc/PortUtils.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.uicc;
+
+import android.annotation.NonNull;
+
+import com.android.internal.telephony.uicc.IccSlotStatus.MultipleEnabledProfilesMode;
+
+/**
+ * Various methods, useful for dealing with port.
+ */
+public class PortUtils {
+
+    /**
+     * Converts the port index to compatible with the HAL.
+     *
+     * @param mepMode   supported MultipleEnabledProfilesMode
+     * @param portIndex port index
+     * @return target index according to the MultipleEnabledProfilesMode
+     */
+    public static int convertToHalPortIndex(@NonNull MultipleEnabledProfilesMode mepMode,
+            int portIndex) {
+        // In case of MEP-A1 and MEP-A2, profiles are selected on eSIM Ports 1 and higher, hence
+        // HAL expects the ports are indexed with 1, 2... etc.
+        // So inorder to compatible with HAL, shift the port index.
+        return mepMode.isMepAMode() ? ++portIndex : portIndex;
+    }
+
+    /**
+     * Converts the port index to compatible with the HAL.
+     *
+     * @param slotIndex   physical slot index corresponding to the portIndex
+     * @param portIndex port index
+     * @return target port index according to the MultipleEnabledProfilesMode
+     */
+    public static int convertToHalPortIndex(int slotIndex, int portIndex) {
+        return convertToHalPortIndex(UiccController.getInstance().getSupportedMepMode(slotIndex),
+                portIndex);
+    }
+
+    /**
+     * Converts the port index to compatible with the platform.
+     *
+     * @param slotIndex physical slot index corresponding to the portIndex
+     * @param portIndex target port index
+     * @param cardState cardState
+     * @param supportedMepMode supported MEP mode
+     * @return shifted port index according to the MultipleEnabledProfilesMode
+     */
+    public static int convertFromHalPortIndex(int slotIndex, int portIndex,
+            IccCardStatus.CardState cardState, MultipleEnabledProfilesMode supportedMepMode) {
+        // In case of MEP-A1 and MEP-A2, profiles are selected on eSIM Ports 1 and higher.
+        // But inorder to platform code MEP mode agnostic, platform always expects the ports
+        // are indexed with 0, 1... etc. Hence shift the target port index to be compatible
+        // with platform.
+
+        // When the SIM_STATUS is related to CARDSTATE_ABSENT, CardStatus will not contain proper
+        // MEP mode info, fallback onto to the supportedMepMode data available in UiccSlot.
+        MultipleEnabledProfilesMode mepMode = cardState.isCardPresent() ? supportedMepMode
+                : UiccController.getInstance().getSupportedMepMode(slotIndex);
+        return mepMode.isMepAMode() ? --portIndex : portIndex;
+    }
+}
diff --git a/src/java/com/android/internal/telephony/uicc/RuimRecords.java b/src/java/com/android/internal/telephony/uicc/RuimRecords.java
old mode 100755
new mode 100644
index 08c1573..041b5dd
--- a/src/java/com/android/internal/telephony/uicc/RuimRecords.java
+++ b/src/java/com/android/internal/telephony/uicc/RuimRecords.java
@@ -33,7 +33,6 @@
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.GsmAlphabet;
 import com.android.internal.telephony.MccTable;
-import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.cdma.sms.UserData;
 import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
 import com.android.internal.util.BitwiseInputStream;
@@ -821,7 +820,7 @@
         // TODO: The below is hacky since the SubscriptionController may not be ready at this time.
         if (!TextUtils.isEmpty(mMdn)) {
             int phoneId = mParentApp.getUiccProfile().getPhoneId();
-            int subId = SubscriptionController.getInstance().getSubIdUsingPhoneId(phoneId);
+            int subId = SubscriptionManager.getSubscriptionId(phoneId);
             if (SubscriptionManager.isValidSubscriptionId(subId)) {
                 SubscriptionManager.from(mContext).setDisplayNumber(mMdn, subId);
             } else {
diff --git a/src/java/com/android/internal/telephony/uicc/SIMRecords.java b/src/java/com/android/internal/telephony/uicc/SIMRecords.java
index b6032ec..49292b0 100644
--- a/src/java/com/android/internal/telephony/uicc/SIMRecords.java
+++ b/src/java/com/android/internal/telephony/uicc/SIMRecords.java
@@ -30,13 +30,15 @@
 import android.telephony.PhoneNumberUtils;
 import android.telephony.SmsMessage;
 import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
 import android.text.TextUtils;
+import android.util.Log;
 import android.util.Pair;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.MccTable;
 import com.android.internal.telephony.SmsConstants;
-import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.gsm.SimTlv;
 import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
 import com.android.telephony.Rlog;
@@ -54,8 +56,9 @@
     protected static final String LOG_TAG = "SIMRecords";
 
     private static final boolean CRASH_RIL = false;
-
-    private static final boolean VDBG = false;
+    private static final boolean FORCE_VERBOSE_STATE_LOGGING = false; /* stopship if true */
+    private static final boolean VDBG =  FORCE_VERBOSE_STATE_LOGGING ||
+            Rlog.isLoggable(LOG_TAG, Log.VERBOSE);
 
     // ***** Instance Variables
 
@@ -108,7 +111,9 @@
                 + " efCPHS_MWI=" + IccUtils.bytesToHexString(mEfCPHS_MWI)
                 + " mEfCff=" + IccUtils.bytesToHexString(mEfCff)
                 + " mEfCfis=" + IccUtils.bytesToHexString(mEfCfis)
-                + " getOperatorNumeric=" + getOperatorNumeric();
+                + " getOperatorNumeric=" + getOperatorNumeric()
+                + " mPsiSmsc=" + mPsiSmsc
+                + " TPMR=" + getSmssTpmrValue();
     }
 
     // ***** Constants
@@ -163,7 +168,7 @@
     private static final int EVENT_UPDATE_DONE = 14 + SIM_RECORD_EVENT_BASE;
     protected static final int EVENT_GET_PNN_DONE = 15 + SIM_RECORD_EVENT_BASE;
     protected static final int EVENT_GET_OPL_DONE = 16 + SIM_RECORD_EVENT_BASE;
-    private static final int EVENT_GET_SST_DONE = 17 + SIM_RECORD_EVENT_BASE;
+    protected static final int EVENT_GET_SST_DONE = 17 + SIM_RECORD_EVENT_BASE;
     private static final int EVENT_GET_ALL_SMS_DONE = 18 + SIM_RECORD_EVENT_BASE;
     private static final int EVENT_MARK_SMS_READ_DONE = 19 + SIM_RECORD_EVENT_BASE;
     private static final int EVENT_SET_MBDN_DONE = 20 + SIM_RECORD_EVENT_BASE;
@@ -184,6 +189,9 @@
     private static final int EVENT_GET_FPLMN_DONE = 41 + SIM_RECORD_EVENT_BASE;
     private static final int EVENT_GET_FPLMN_SIZE_DONE = 42 + SIM_RECORD_EVENT_BASE;
     private static final int EVENT_SET_FPLMN_DONE = 43 + SIM_RECORD_EVENT_BASE;
+    protected static final int EVENT_GET_SMSS_RECORD_DONE = 46 + SIM_RECORD_EVENT_BASE;
+    protected static final int EVENT_GET_PSISMSC_DONE = 47 + SIM_RECORD_EVENT_BASE;
+
     // ***** Constructor
 
     public SIMRecords(UiccCardApplication app, Context c, CommandsInterface ci) {
@@ -272,6 +280,18 @@
         return mUsimServiceTable;
     }
 
+    /**
+     * Fetches the USIM service table from UsimServiceTable
+     *
+     * @return HexString representation of USIM service table
+     */
+    public String getSimServiceTable() {
+        if (mUsimServiceTable != null) {
+            return IccUtils.bytesToHexString(mUsimServiceTable.getUSIMServiceTable());
+        }
+        return null;
+    }
+
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private int getExtFromEf(int ef) {
         int ext;
@@ -378,18 +398,20 @@
         mNewVoiceMailTag = alphaTag;
 
         AdnRecord adn = new AdnRecord(mNewVoiceMailTag, mNewVoiceMailNum);
-
         if (mMailboxIndex != 0 && mMailboxIndex != 0xff) {
 
             new AdnRecordLoader(mFh).updateEF(adn, EF_MBDN, EF_EXT6,
                     mMailboxIndex, null,
-                    obtainMessage(EVENT_SET_MBDN_DONE, onComplete));
+                    obtainMessage(EVENT_SET_MBDN_DONE, AdnRecordLoader.VOICEMAIL_ALPHATAG_ARG,
+                            0 /* ignored arg2 */, onComplete));
 
         } else if (isCphsMailboxEnabled()) {
 
             new AdnRecordLoader(mFh).updateEF(adn, EF_MAILBOX_CPHS,
                     EF_EXT1, 1, null,
-                    obtainMessage(EVENT_SET_CPHS_MAILBOX_DONE, onComplete));
+                    obtainMessage(EVENT_SET_CPHS_MAILBOX_DONE,
+                            AdnRecordLoader.VOICEMAIL_ALPHATAG_ARG,
+                            0 /* ignored arg2 */, onComplete));
 
         } else {
             AsyncResult.forMessage((onComplete)).exception =
@@ -639,7 +661,6 @@
                     " while being destroyed. Ignoring.");
             return;
         }
-
         try {
             switch (msg.what) {
                 /* IO events */
@@ -1013,10 +1034,20 @@
 
                     if (DBG) log("EVENT_SET_MBDN_DONE ex:" + ar.exception);
                     if (ar.exception == null) {
+                        /**
+                         * Check for any changes made to voicemail alphaTag while saving to SIM.
+                         * if voicemail alphaTag length is more than allowed limit of SIM EF then
+                         * null alphaTag will be saved to SIM {@code AdnRecordLoader}.
+                         */
+                        if (ar.result != null) {
+                            AdnRecord adnRecord = (AdnRecord) (ar.result);
+                            if (adnRecord != null) {
+                                mNewVoiceMailTag = adnRecord.mAlphaTag;
+                            }
+                        }
                         mVoiceMailNum = mNewVoiceMailNum;
                         mVoiceMailTag = mNewVoiceMailTag;
                     }
-
                     if (isCphsMailboxEnabled()) {
                         adn = new AdnRecord(mVoiceMailTag, mVoiceMailNum);
                         Message onCphsCompleted = (Message) ar.userObj;
@@ -1040,14 +1071,15 @@
                         new AdnRecordLoader(mFh)
                                 .updateEF(adn, EF_MAILBOX_CPHS, EF_EXT1, 1, null,
                                 obtainMessage(EVENT_SET_CPHS_MAILBOX_DONE,
-                                        onCphsCompleted));
+                                       AdnRecordLoader.VOICEMAIL_ALPHATAG_ARG,
+                                        0 /* ignored arg2 */, onCphsCompleted));
                     } else {
                         if (ar.userObj != null) {
                             CarrierConfigManager configManager = (CarrierConfigManager)
                                     mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
                             if (ar.exception != null && configManager != null) {
                                 PersistableBundle b = configManager.getConfigForSubId(
-                                        SubscriptionController.getInstance().getSubIdUsingPhoneId(
+                                        SubscriptionManager.getSubscriptionId(
                                                 mParentApp.getPhoneId()));
                                 if (b != null && b.getBoolean(
                                         CarrierConfigManager.KEY_EDITABLE_VOICEMAIL_NUMBER_BOOL)) {
@@ -1072,6 +1104,12 @@
                     isRecordLoadResponse = false;
                     ar = (AsyncResult) msg.obj;
                     if (ar.exception == null) {
+                        if (ar.result != null) {
+                            AdnRecord adnRecord = (AdnRecord) (ar.result);
+                            if (adnRecord != null) {
+                                mNewVoiceMailTag = adnRecord.mAlphaTag;
+                            }
+                        }
                         mVoiceMailNum = mNewVoiceMailNum;
                         mVoiceMailTag = mNewVoiceMailTag;
                     } else {
@@ -1291,6 +1329,35 @@
                     }
                     break;
 
+                case EVENT_GET_PSISMSC_DONE:
+                    isRecordLoadResponse = true;
+                    ar = (AsyncResult) msg.obj;
+                    if (ar.exception != null) {
+                        loge("Failed to read USIM EF_PSISMSC field error=" + ar.exception);
+                    } else {
+                        data = (byte[]) ar.result;
+                        if (data != null && data.length > 0) {
+                            mPsiSmsc = parseEfPsiSmsc(data);
+                            if (VDBG) {
+                                log("SIMRecords - EF_PSISMSC value = " + mPsiSmsc);
+                            }
+                        }
+                    }
+                    break;
+
+                case EVENT_GET_SMSS_RECORD_DONE:
+                    isRecordLoadResponse = true;
+                    ar = (AsyncResult) msg.obj;
+                    if (ar.exception != null) {
+                        loge("Failed to read USIM EF_SMSS field error=" + ar.exception);
+                    } else {
+                        mSmssValues = (byte[]) ar.result;
+                        if (VDBG) {
+                            log("SIMRecords - EF_SMSS TPMR value = " + getSmssTpmrValue());
+                        }
+                    }
+                    break;
+
                 default:
                     super.handleMessage(msg);   // IccRecords handles generic record load responses
             }
@@ -1680,12 +1747,18 @@
         mFh.getEFLinearRecordSize(EF_SMS, obtainMessage(EVENT_GET_SMS_RECORD_SIZE_DONE));
         mRecordsToLoad++;
 
+        mFh.loadEFLinearFixed(EF_PSISMSC, 1, obtainMessage(EVENT_GET_PSISMSC_DONE));
+        mRecordsToLoad++;
+
         // XXX should seek instead of examining them all
         if (false) { // XXX
             mFh.loadEFLinearFixedAll(EF_SMS, obtainMessage(EVENT_GET_ALL_SMS_DONE));
             mRecordsToLoad++;
         }
 
+        mFh.loadEFTransparent(EF_SMSS, obtainMessage(EVENT_GET_SMSS_RECORD_DONE));
+        mRecordsToLoad++;
+
         if (CRASH_RIL) {
             String sms = "0107912160130310f20404d0110041007030208054832b0120"
                          + "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
@@ -2110,6 +2183,11 @@
         log("[CSP] Value Added Service Group (0xC0), not found!");
     }
 
+    @VisibleForTesting
+    public void setMailboxIndex(int mailboxIndex) {
+        mMailboxIndex = mailboxIndex;
+    }
+
     @Override
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         pw.println("SIMRecords: " + this);
@@ -2144,6 +2222,8 @@
         pw.println(" mHplmnActRecords[]=" + Arrays.toString(mHplmnActRecords));
         pw.println(" mFplmns[]=" + Arrays.toString(mFplmns));
         pw.println(" mEhplmns[]=" + Arrays.toString(mEhplmns));
+        pw.println(" mPsismsc=" + mPsiSmsc);
+        pw.println(" TPMR=" + getSmssTpmrValue());
         pw.flush();
     }
 }
diff --git a/src/java/com/android/internal/telephony/uicc/UiccCard.java b/src/java/com/android/internal/telephony/uicc/UiccCard.java
index 689e4b7..67f120f 100644
--- a/src/java/com/android/internal/telephony/uicc/UiccCard.java
+++ b/src/java/com/android/internal/telephony/uicc/UiccCard.java
@@ -24,6 +24,7 @@
 
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.uicc.IccCardStatus.CardState;
+import com.android.internal.telephony.uicc.IccSlotStatus.MultipleEnabledProfilesMode;
 import com.android.internal.telephony.uicc.euicc.EuiccCard;
 import com.android.internal.telephony.uicc.euicc.EuiccPort;
 import com.android.telephony.Rlog;
@@ -31,6 +32,7 @@
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 
 /**
  * {@hide}
@@ -50,16 +52,19 @@
     private CardState mCardState;
     protected String mCardId;
     protected boolean mIsSupportsMultipleEnabledProfiles;
+    protected MultipleEnabledProfilesMode mSupportedMepMode;
 
-    protected HashMap<Integer, UiccPort> mUiccPorts = new HashMap<>();
+    protected LinkedHashMap<Integer, UiccPort> mUiccPorts = new LinkedHashMap<>();
     private HashMap<Integer, Integer> mPhoneIdToPortIdx = new HashMap<>();
 
     public UiccCard(Context c, CommandsInterface ci, IccCardStatus ics, int phoneId, Object lock,
-            boolean isSupportsMultipleEnabledProfiles) {
+            boolean isSupportsMultipleEnabledProfiles,
+            MultipleEnabledProfilesMode supportedMepMode) {
         if (DBG) log("Creating");
         mCardState = ics.mCardState;
         mLock = lock;
         mIsSupportsMultipleEnabledProfiles = isSupportsMultipleEnabledProfiles;
+        mSupportedMepMode = supportedMepMode;
         update(c, ci, ics, phoneId);
     }
 
@@ -109,7 +114,7 @@
                 if (port == null) {
                     if (this instanceof EuiccCard) {
                         port = new EuiccPort(c, ci, ics, phoneId, mLock, this,
-                                mIsSupportsMultipleEnabledProfiles); // eSim
+                                mIsSupportsMultipleEnabledProfiles, mSupportedMepMode); // eSim
                     } else {
                         port = new UiccPort(c, ci, ics, phoneId, mLock, this); // pSim
                     }
@@ -143,13 +148,16 @@
 
 
     /**
-     * Updates MEP(Multiple Enabled Profile) support flag.
+     * Updates MEP(Multiple Enabled Profile) support and supported mode flags.
      *
      * <p>If IccSlotStatus comes later, the number of ports reported is only known after the
      * UiccCard creation which will impact UICC MEP capability.
      */
-    public void updateSupportMultipleEnabledProfile(boolean supported) {
+    public void updateSupportMepProperties(boolean supported,
+            MultipleEnabledProfilesMode supportedMepMode) {
+        // TODO(b/262449536): Handle with single MEP flag to avoid inconsistency.
         mIsSupportsMultipleEnabledProfiles = supported;
+        mSupportedMepMode = supportedMepMode;
     }
 
     @UnsupportedAppUsage
@@ -213,7 +221,7 @@
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         pw.println("UiccCard:");
         pw.println(" mCardState=" + mCardState);
-        pw.println(" mCardId=" + mCardId);
+        pw.println(" mCardId=" + Rlog.pii(LOG_TAG, mCardId));
         pw.println(" mNumberOfPorts=" + mUiccPorts.size());
         pw.println( "mIsSupportsMultipleEnabledProfiles=" + mIsSupportsMultipleEnabledProfiles);
         pw.println();
diff --git a/src/java/com/android/internal/telephony/uicc/UiccCardApplication.java b/src/java/com/android/internal/telephony/uicc/UiccCardApplication.java
index 3839610..9454ade 100644
--- a/src/java/com/android/internal/telephony/uicc/UiccCardApplication.java
+++ b/src/java/com/android/internal/telephony/uicc/UiccCardApplication.java
@@ -59,6 +59,9 @@
      */
     public static final int AUTH_CONTEXT_EAP_SIM = PhoneConstants.AUTH_CONTEXT_EAP_SIM;
     public static final int AUTH_CONTEXT_EAP_AKA = PhoneConstants.AUTH_CONTEXT_EAP_AKA;
+    public static final int AUTH_CONTEXT_GBA_BOOTSTRAP = PhoneConstants.AUTH_CONTEXT_GBA_BOOTSTRAP;
+    public static final int AUTHTYPE_GBA_NAF_KEY_EXTERNAL =
+            PhoneConstants.AUTHTYPE_GBA_NAF_KEY_EXTERNAL;
     public static final int AUTH_CONTEXT_UNDEFINED = PhoneConstants.AUTH_CONTEXT_UNDEFINED;
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
diff --git a/src/java/com/android/internal/telephony/uicc/UiccController.java b/src/java/com/android/internal/telephony/uicc/UiccController.java
index 70cfce0..48524ff 100644
--- a/src/java/com/android/internal/telephony/uicc/UiccController.java
+++ b/src/java/com/android/internal/telephony/uicc/UiccController.java
@@ -21,6 +21,8 @@
 
 import static java.util.Arrays.copyOf;
 
+import android.Manifest;
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.BroadcastOptions;
 import android.compat.annotation.UnsupportedAppUsage;
@@ -40,6 +42,7 @@
 import android.telephony.CarrierConfigManager;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
+import android.telephony.TelephonyManager.SimState;
 import android.telephony.UiccCardInfo;
 import android.telephony.UiccPortInfo;
 import android.telephony.UiccSlotMapping;
@@ -49,14 +52,20 @@
 import android.util.Log;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.CarrierServiceBindHelper;
 import com.android.internal.telephony.CommandException;
 import com.android.internal.telephony.CommandsInterface;
+import com.android.internal.telephony.IccCard;
 import com.android.internal.telephony.IccCardConstants;
+import com.android.internal.telephony.IntentBroadcaster;
 import com.android.internal.telephony.PhoneConfigurationManager;
 import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.RadioConfig;
 import com.android.internal.telephony.SubscriptionInfoUpdater;
+import com.android.internal.telephony.TelephonyIntents;
+import com.android.internal.telephony.metrics.TelephonyMetrics;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.euicc.EuiccCard;
 import com.android.internal.telephony.util.TelephonyUtils;
 import com.android.telephony.Rlog;
@@ -140,6 +149,9 @@
     private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 10;
     // NOTE: any new EVENT_* values must be added to eventToString.
 
+    @NonNull
+    private final TelephonyManager mTelephonyManager;
+
     // this needs to be here, because on bootup we dont know which index maps to which UiccSlot
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private CommandsInterface[] mCis;
@@ -151,11 +163,25 @@
     // This maps the externally exposed card ID (int) to the internal card ID string (ICCID/EID).
     // The array index is the card ID (int).
     // This mapping exists to expose card-based functionality without exposing the EID, which is
-    // considered sensetive information.
+    // considered sensitive information.
     // mCardStrings is populated using values from the IccSlotStatus and IccCardStatus. For
     // HAL < 1.2, these do not contain the EID or the ICCID, so mCardStrings will be empty
     private ArrayList<String> mCardStrings;
 
+    /**
+     * SIM card state.
+     */
+    @NonNull
+    @SimState
+    private final int[] mSimCardState;
+
+    /**
+     * SIM application state.
+     */
+    @NonNull
+    @SimState
+    private final int[] mSimApplicationState;
+
     // This is the card ID of the default eUICC. It starts as UNINITIALIZED_CARD_ID.
     // When we load the EID (either with slot status or from the EuiccCard), we set it to the eUICC
     // with the lowest slot index.
@@ -205,6 +231,9 @@
 
     protected RegistrantList mIccChangedRegistrants = new RegistrantList();
 
+    @NonNull
+    private final CarrierServiceBindHelper mCarrierServiceBindHelper;
+
     private UiccStateChangedLauncher mLauncher;
     private RadioConfig mRadioConfig;
 
@@ -243,8 +272,13 @@
             numPhysicalSlots = mCis.length;
         }
 
+        mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
+
         mUiccSlots = new UiccSlot[numPhysicalSlots];
         mPhoneIdToSlotId = new int[mCis.length];
+        int supportedModemCount = mTelephonyManager.getSupportedModemCount();
+        mSimCardState = new int[supportedModemCount];
+        mSimApplicationState = new int[supportedModemCount];
         Arrays.fill(mPhoneIdToSlotId, INVALID_SLOT_ID);
         if (VDBG) logPhoneIdToSlotIdMapping();
         mRadioConfig = RadioConfig.getInstance();
@@ -260,6 +294,8 @@
         mCardStrings = loadCardStrings();
         mDefaultEuiccCardId = UNINITIALIZED_CARD_ID;
 
+        mCarrierServiceBindHelper = new CarrierServiceBindHelper(mContext);
+
         mEuiccSlots = mContext.getResources()
                 .getIntArray(com.android.internal.R.array.non_removable_euicc_slots);
         mHasBuiltInEuicc = hasBuiltInEuicc();
@@ -300,13 +336,11 @@
 
     @UnsupportedAppUsage
     public static UiccController getInstance() {
-        synchronized (mLock) {
-            if (mInstance == null) {
-                throw new RuntimeException(
-                        "UiccController.getInstance can't be called before make()");
-            }
-            return mInstance;
+        if (mInstance == null) {
+            throw new RuntimeException(
+                    "UiccController.getInstance can't be called before make()");
         }
+        return mInstance;
     }
 
     @UnsupportedAppUsage
@@ -739,6 +773,269 @@
         }
     }
 
+    /**
+     * Update SIM state for the inactive eSIM port.
+     *
+     * @param phoneId Previously active phone id.
+     * @param iccId ICCID of the SIM.
+     */
+    public void updateSimStateForInactivePort(int phoneId, String iccId) {
+        post(() -> {
+            if (SubscriptionManager.isValidPhoneId(phoneId)) {
+                // Mark SIM state as ABSENT on previously phoneId.
+                mTelephonyManager.setSimStateForPhone(phoneId,
+                        IccCardConstants.State.ABSENT.toString());
+            }
+
+            SubscriptionManagerService.getInstance().updateSimStateForInactivePort(phoneId);
+        });
+    }
+
+    /**
+     * Broadcast the legacy SIM state changed event.
+     *
+     * @param phoneId The phone id.
+     * @param state The legacy SIM state.
+     * @param reason The reason of SIM state change.
+     */
+    private void broadcastSimStateChanged(int phoneId, @NonNull String state,
+            @Nullable String reason) {
+        // Note: This intent is way deprecated and is only being kept around because there's no
+        // graceful way to deprecate a sticky broadcast that has a lot of listeners.
+        // DO NOT add any new extras to this broadcast -- it is not protected by any permissions.
+        Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
+        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
+        intent.putExtra(PhoneConstants.PHONE_NAME_KEY, "Phone");
+        intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE, state);
+        intent.putExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON, reason);
+        SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
+        Rlog.d(LOG_TAG, "Broadcasting intent ACTION_SIM_STATE_CHANGED " + state + " reason "
+                + reason + " for phone: " + phoneId);
+        IntentBroadcaster.getInstance().broadcastStickyIntent(mContext, intent, phoneId);
+    }
+
+    /**
+     * Broadcast SIM card state changed event.
+     *
+     * @param phoneId The phone id.
+     * @param state The SIM card state.
+     */
+    private void broadcastSimCardStateChanged(int phoneId, @SimState int state) {
+        if (state != mSimCardState[phoneId]) {
+            mSimCardState[phoneId] = state;
+            Intent intent = new Intent(TelephonyManager.ACTION_SIM_CARD_STATE_CHANGED);
+            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
+            intent.putExtra(TelephonyManager.EXTRA_SIM_STATE, state);
+            SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
+            // TODO(b/130664115) we manually populate this intent with the slotId. In the future we
+            // should do a review of whether to make this public
+            UiccSlot slot = UiccController.getInstance().getUiccSlotForPhone(phoneId);
+            int slotId = UiccController.getInstance().getSlotIdFromPhoneId(phoneId);
+            intent.putExtra(PhoneConstants.SLOT_KEY, slotId);
+            if (slot != null) {
+                intent.putExtra(PhoneConstants.PORT_KEY, slot.getPortIndexFromPhoneId(phoneId));
+            }
+            Rlog.d(LOG_TAG, "Broadcasting intent ACTION_SIM_CARD_STATE_CHANGED "
+                    + TelephonyManager.simStateToString(state) + " for phone: " + phoneId
+                    + " slot: " + slotId + " port: " + slot.getPortIndexFromPhoneId(phoneId));
+            mContext.sendBroadcast(intent, Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+            TelephonyMetrics.getInstance().updateSimState(phoneId, state);
+        }
+    }
+
+    /**
+     * Broadcast SIM application state changed event.
+     *
+     * @param phoneId The phone id.
+     * @param state The SIM application state.
+     */
+    private void broadcastSimApplicationStateChanged(int phoneId, @SimState int state) {
+        // Broadcast if the state has changed, except if old state was UNKNOWN and new is NOT_READY,
+        // because that's the initial state and a broadcast should be sent only on a transition
+        // after SIM is PRESENT. The only exception is eSIM boot profile, where NOT_READY is the
+        // terminal state.
+        boolean isUnknownToNotReady =
+                (mSimApplicationState[phoneId] == TelephonyManager.SIM_STATE_UNKNOWN
+                        && state == TelephonyManager.SIM_STATE_NOT_READY);
+        IccCard iccCard = PhoneFactory.getPhone(phoneId).getIccCard();
+        boolean emptyProfile = iccCard != null && iccCard.isEmptyProfile();
+        if (state != mSimApplicationState[phoneId] && (!isUnknownToNotReady || emptyProfile)) {
+            mSimApplicationState[phoneId] = state;
+            Intent intent = new Intent(TelephonyManager.ACTION_SIM_APPLICATION_STATE_CHANGED);
+            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
+            intent.putExtra(TelephonyManager.EXTRA_SIM_STATE, state);
+            SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
+            // TODO(b/130664115) we populate this intent with the actual slotId. In the future we
+            // should do a review of whether to make this public
+            UiccSlot slot = UiccController.getInstance().getUiccSlotForPhone(phoneId);
+            int slotId = UiccController.getInstance().getSlotIdFromPhoneId(phoneId);
+            intent.putExtra(PhoneConstants.SLOT_KEY, slotId);
+            if (slot != null) {
+                intent.putExtra(PhoneConstants.PORT_KEY, slot.getPortIndexFromPhoneId(phoneId));
+            }
+            Rlog.d(LOG_TAG, "Broadcasting intent ACTION_SIM_APPLICATION_STATE_CHANGED "
+                    + TelephonyManager.simStateToString(state)
+                    + " for phone: " + phoneId + " slot: " + slotId + "port: "
+                    + slot.getPortIndexFromPhoneId(phoneId));
+            mContext.sendBroadcast(intent, Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+            TelephonyMetrics.getInstance().updateSimState(phoneId, state);
+        }
+    }
+
+    /**
+     * Get SIM state from SIM lock reason.
+     *
+     * @param lockedReason The SIM lock reason.
+     *
+     * @return The SIM state.
+     */
+    @SimState
+    private static int getSimStateFromLockedReason(String lockedReason) {
+        switch (lockedReason) {
+            case IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN:
+                return TelephonyManager.SIM_STATE_PIN_REQUIRED;
+            case IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK:
+                return TelephonyManager.SIM_STATE_PUK_REQUIRED;
+            case IccCardConstants.INTENT_VALUE_LOCKED_NETWORK:
+                return TelephonyManager.SIM_STATE_NETWORK_LOCKED;
+            case IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED:
+                return TelephonyManager.SIM_STATE_PERM_DISABLED;
+            default:
+                Rlog.e(LOG_TAG, "Unexpected SIM locked reason " + lockedReason);
+                return TelephonyManager.SIM_STATE_UNKNOWN;
+        }
+    }
+
+    /**
+     * Broadcast SIM state events.
+     *
+     * @param phoneId The phone id.
+     * @param simState The SIM state.
+     * @param reason SIM state changed reason.
+     */
+    private void broadcastSimStateEvents(int phoneId, IccCardConstants.State simState,
+            @Nullable String reason) {
+        String legacyStringSimState = getIccStateIntentString(simState);
+        int cardState = TelephonyManager.SIM_STATE_UNKNOWN;
+        int applicationState = TelephonyManager.SIM_STATE_UNKNOWN;
+
+        switch (simState) {
+            case ABSENT:
+                cardState = TelephonyManager.SIM_STATE_ABSENT;
+                break;
+            case PIN_REQUIRED:
+            case PUK_REQUIRED:
+            case NETWORK_LOCKED:
+            case PERM_DISABLED:
+                cardState = TelephonyManager.SIM_STATE_PRESENT;
+                applicationState = getSimStateFromLockedReason(reason);
+                break;
+            case READY:
+            case NOT_READY:
+                // Both READY and NOT_READY have the same card state and application state.
+                cardState = TelephonyManager.SIM_STATE_PRESENT;
+                applicationState = TelephonyManager.SIM_STATE_NOT_READY;
+                break;
+            case CARD_IO_ERROR:
+                cardState = TelephonyManager.SIM_STATE_CARD_IO_ERROR;
+                applicationState = TelephonyManager.SIM_STATE_NOT_READY;
+                break;
+            case CARD_RESTRICTED:
+                cardState = TelephonyManager.SIM_STATE_CARD_RESTRICTED;
+                applicationState = TelephonyManager.SIM_STATE_NOT_READY;
+                break;
+            case LOADED:
+                cardState = TelephonyManager.SIM_STATE_PRESENT;
+                applicationState = TelephonyManager.SIM_STATE_LOADED;
+                break;
+            case UNKNOWN:
+            default:
+                break;
+        }
+
+        broadcastSimStateChanged(phoneId, legacyStringSimState, reason);
+        broadcastSimCardStateChanged(phoneId, cardState);
+        broadcastSimApplicationStateChanged(phoneId, applicationState);
+    }
+
+    /**
+     * Update carrier service.
+     *
+     * @param phoneId The phone id.
+     * @param simState The SIM state.
+     */
+    private void updateCarrierServices(int phoneId, @NonNull String simState) {
+        CarrierConfigManager configManager = mContext.getSystemService(CarrierConfigManager.class);
+        if (configManager != null) {
+            configManager.updateConfigForPhoneId(phoneId, simState);
+        }
+        mCarrierServiceBindHelper.updateForPhoneId(phoneId, simState);
+    }
+
+    /**
+     * Update the SIM state.
+     *
+     * @param phoneId Phone id.
+     * @param state SIM state (legacy).
+     * @param reason The reason for SIM state update.
+     */
+    public void updateSimState(int phoneId, @NonNull IccCardConstants.State state,
+            @Nullable String reason) {
+        post(() -> {
+            log("updateSimState: phoneId=" + phoneId + ", state=" + state + ", reason="
+                    + reason);
+            if (!SubscriptionManager.isValidPhoneId(phoneId)) {
+                Rlog.e(LOG_TAG, "updateInternalIccState: Invalid phone id " + phoneId);
+                return;
+            }
+
+            mTelephonyManager.setSimStateForPhone(phoneId, state.toString());
+
+            String legacySimState = getIccStateIntentString(state);
+            int simState = state.ordinal();
+            SubscriptionManagerService.getInstance().updateSimState(phoneId, simState,
+                    this::post,
+                    () -> {
+                        // The following are executed after subscription update completed in
+                        // subscription manager service.
+
+                        broadcastSimStateEvents(phoneId, state, reason);
+
+                        UiccProfile uiccProfile = getUiccProfileForPhone(phoneId);
+
+                        if (simState == TelephonyManager.SIM_STATE_READY) {
+                            // SIM_STATE_READY is not a final state.
+                            return;
+                        }
+
+                        if (simState == TelephonyManager.SIM_STATE_NOT_READY
+                                && (uiccProfile != null && !uiccProfile.isEmptyProfile())
+                                && SubscriptionManagerService.getInstance()
+                                .areUiccAppsEnabledOnCard(phoneId)) {
+                            // STATE_NOT_READY is not a final state for when both
+                            // 1) It's not an empty profile, and
+                            // 2) Its uicc applications are set to enabled.
+                            //
+                            // At this phase, we consider STATE_NOT_READY not a final state, so
+                            // return for now.
+                            log("updateSimState: SIM_STATE_NOT_READY is not a final "
+                                    + "state.");
+                            return;
+                        }
+
+                        // At this point, the SIM state must be a final state (meaning we won't
+                        // get more SIM state updates). So resolve the carrier id and update the
+                        // carrier services.
+                        log("updateSimState: resolve carrier id and update carrier "
+                                + "services.");
+                        PhoneFactory.getPhone(phoneId).resolveSubscriptionCarrierId(
+                                legacySimState);
+                        updateCarrierServices(phoneId, legacySimState);
+                    }
+            );
+        });
+    }
+
     private synchronized void onGetIccCardStatusDone(AsyncResult ar, Integer index) {
         if (ar.exception != null) {
             Rlog.e(LOG_TAG,"Error getting ICC status. "
@@ -760,10 +1057,10 @@
 
         IccCardStatus status = (IccCardStatus)ar.result;
 
-        logWithLocalLog("onGetIccCardStatusDone: phoneId " + index + " IccCardStatus: " + status);
+        logWithLocalLog("onGetIccCardStatusDone: phoneId-" + index + " IccCardStatus: " + status);
 
         int slotId = status.mSlotPortMapping.mPhysicalSlotIndex;
-        if (VDBG) log("onGetIccCardStatusDone: phoneId " + index + " physicalSlotIndex " + slotId);
+        if (VDBG) log("onGetIccCardStatusDone: phoneId-" + index + " physicalSlotIndex " + slotId);
         if (slotId == INVALID_SLOT_ID) {
             slotId = index;
         }
@@ -1453,6 +1750,18 @@
         return mUseRemovableEsimAsDefault;
     }
 
+    /**
+     * Returns the MEP mode supported by the UiccSlot associated with slotIndex.
+     * @param slotIndex physical slot index
+     * @return MultipleEnabledProfilesMode supported by the slot
+     */
+    public IccSlotStatus.MultipleEnabledProfilesMode getSupportedMepMode(int slotIndex) {
+        synchronized (mLock) {
+            UiccSlot slot = getUiccSlot(slotIndex);
+            return slot != null ? slot.getSupportedMepMode()
+                    : IccSlotStatus.MultipleEnabledProfilesMode.NONE;
+        }
+    }
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private void log(String string) {
@@ -1488,7 +1797,7 @@
         pw.println(" mIsCdmaSupported=" + isCdmaSupported(mContext));
         pw.println(" mHasBuiltInEuicc=" + mHasBuiltInEuicc);
         pw.println(" mHasActiveBuiltInEuicc=" + mHasActiveBuiltInEuicc);
-        pw.println(" mCardStrings=" + mCardStrings);
+        pw.println(" mCardStrings=" + Rlog.pii(LOG_TAG, mCardStrings));
         pw.println(" mDefaultEuiccCardId=" + mDefaultEuiccCardId);
         pw.println(" mPhoneIdToSlotId=" + Arrays.toString(mPhoneIdToSlotId));
         pw.println(" mUseRemovableEsimAsDefault=" + mUseRemovableEsimAsDefault);
diff --git a/src/java/com/android/internal/telephony/uicc/UiccPort.java b/src/java/com/android/internal/telephony/uicc/UiccPort.java
index 0152dda..ab00cab 100644
--- a/src/java/com/android/internal/telephony/uicc/UiccPort.java
+++ b/src/java/com/android/internal/telephony/uicc/UiccPort.java
@@ -21,7 +21,6 @@
 import android.os.IBinder;
 import android.os.Message;
 import android.os.RemoteException;
-import android.telephony.SubscriptionInfo;
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
@@ -363,7 +362,6 @@
         pw.println(" this=" + this);
         pw.println(" mPortIdx=" + mPortIdx);
         pw.println(" mCi=" + mCi);
-        pw.println(" mIccid=" + SubscriptionInfo.givePrintableIccid(mIccid));
         pw.println(" mPhoneId=" + mPhoneId);
         pw.println(" mPhysicalSlotIndex=" + mPhysicalSlotIndex);
         synchronized (mOpenChannelRecords) {
diff --git a/src/java/com/android/internal/telephony/uicc/UiccProfile.java b/src/java/com/android/internal/telephony/uicc/UiccProfile.java
index 2809895..7dec239 100644
--- a/src/java/com/android/internal/telephony/uicc/UiccProfile.java
+++ b/src/java/com/android/internal/telephony/uicc/UiccProfile.java
@@ -35,6 +35,7 @@
 import android.os.Binder;
 import android.os.Handler;
 import android.os.Message;
+import android.os.ParcelUuid;
 import android.os.PersistableBundle;
 import android.os.Registrant;
 import android.os.RegistrantList;
@@ -64,6 +65,8 @@
 import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.TelephonyStatsLog;
 import com.android.internal.telephony.cat.CatService;
+import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
 import com.android.internal.telephony.uicc.IccCardApplicationStatus.PersoSubState;
 import com.android.internal.telephony.uicc.IccCardStatus.CardState;
@@ -77,6 +80,7 @@
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
@@ -431,8 +435,7 @@
      * if an override is provided.
      */
     private void handleCarrierNameOverride() {
-        SubscriptionController subCon = SubscriptionController.getInstance();
-        final int subId = subCon.getSubIdUsingPhoneId(mPhoneId);
+        final int subId = SubscriptionManager.getSubscriptionId(mPhoneId);
         if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
             loge("subId not valid for Phone " + mPhoneId);
             return;
@@ -481,7 +484,7 @@
             mOperatorBrandOverrideRegistrants.notifyRegistrants();
         }
 
-        updateCarrierNameForSubscription(subCon, subId, nameSource);
+        updateCarrierNameForSubscription(subId, nameSource);
     }
 
     /**
@@ -496,8 +499,7 @@
      * MCC table
      */
     private void handleSimCountryIsoOverride() {
-        SubscriptionController subCon = SubscriptionController.getInstance();
-        final int subId = subCon.getSubIdUsingPhoneId(mPhoneId);
+        final int subId = SubscriptionManager.getSubscriptionId(mPhoneId);
         if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
             loge("subId not valid for Phone " + mPhoneId);
             return;
@@ -512,18 +514,28 @@
 
         PersistableBundle config = configLoader.getConfigForSubId(subId);
         String iso = config.getString(CarrierConfigManager.KEY_SIM_COUNTRY_ISO_OVERRIDE_STRING);
-        if (!TextUtils.isEmpty(iso) &&
-                !iso.equals(mTelephonyManager.getSimCountryIsoForPhone(mPhoneId))) {
+        if (!TextUtils.isEmpty(iso)
+                && !iso.equals(TelephonyManager.getSimCountryIsoForPhone(mPhoneId))) {
             mTelephonyManager.setSimCountryIsoForPhone(mPhoneId, iso);
-            subCon.setCountryIso(iso, subId);
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                SubscriptionManagerService.getInstance().setCountryIso(subId, iso);
+            } else {
+                SubscriptionController.getInstance().setCountryIso(iso, subId);
+            }
         }
     }
 
-    private void updateCarrierNameForSubscription(SubscriptionController subCon, int subId,
-            int nameSource) {
+    private void updateCarrierNameForSubscription(int subId, int nameSource) {
         /* update display name with carrier override */
-        SubscriptionInfo subInfo = subCon.getActiveSubscriptionInfo(
-                subId, mContext.getOpPackageName(), mContext.getAttributionTag());
+        SubscriptionInfo subInfo;
+
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            subInfo = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
+                    mContext.getOpPackageName(), mContext.getAttributionTag());
+        } else {
+            subInfo = SubscriptionController.getInstance().getActiveSubscriptionInfo(
+                    subId, mContext.getOpPackageName(), mContext.getAttributionTag());
+        }
 
         if (subInfo == null) {
             return;
@@ -534,7 +546,13 @@
 
         if (!TextUtils.isEmpty(newCarrierName) && !newCarrierName.equals(oldSubName)) {
             log("sim name[" + mPhoneId + "] = " + newCarrierName);
-            subCon.setDisplayNameUsingSrc(newCarrierName, subId, nameSource);
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                SubscriptionManagerService.getInstance().setDisplayNameUsingSrc(
+                        newCarrierName, subId, nameSource);
+            } else {
+                SubscriptionController.getInstance().setDisplayNameUsingSrc(
+                        newCarrierName, subId, nameSource);
+            }
         }
     }
 
@@ -791,8 +809,13 @@
             }
             log("setExternalState: set mPhoneId=" + mPhoneId + " mExternalState=" + mExternalState);
 
-            UiccController.updateInternalIccState(mContext, mExternalState,
-                    getIccStateReason(mExternalState), mPhoneId);
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                UiccController.getInstance().updateSimState(mPhoneId, mExternalState,
+                        getIccStateReason(mExternalState));
+            } else {
+                UiccController.updateInternalIccState(mContext, mExternalState,
+                        getIccStateReason(mExternalState), mPhoneId);
+            }
         }
     }
 
@@ -1393,7 +1416,7 @@
         Set<String> uninstalledCarrierPackages = new ArraySet<>();
         List<UiccAccessRule> accessRules = rules.getAccessRules();
         for (UiccAccessRule accessRule : accessRules) {
-            String certHexString = accessRule.getCertificateHexString().toUpperCase();
+            String certHexString = accessRule.getCertificateHexString().toUpperCase(Locale.ROOT);
             String pkgName = certPackageMap.get(certHexString);
             if (!TextUtils.isEmpty(pkgName) && !isPackageBundled(mContext, pkgName)) {
                 uninstalledCarrierPackages.add(pkgName);
@@ -1423,7 +1446,7 @@
             String[] keyValue = keyValueString.split(keyValueDelim);
 
             if (keyValue.length == 2) {
-                map.put(keyValue[0].toUpperCase(), keyValue[1]);
+                map.put(keyValue[0].toUpperCase(Locale.ROOT), keyValue[1]);
             } else {
                 loge("Incorrect length of key-value pair in carrier app allow list map.  "
                         + "Length should be exactly 2");
@@ -1695,9 +1718,36 @@
         if (TextUtils.isEmpty(iccId)) {
             return false;
         }
-        if (!SubscriptionController.getInstance().checkPhoneIdAndIccIdMatch(getPhoneId(), iccId)) {
-            loge("iccId doesn't match current active subId.");
-            return false;
+
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            int subId = SubscriptionManager.getSubscriptionId(getPhoneId());
+            SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
+                    .getSubscriptionInfoInternal(subId);
+            if (subInfo == null) {
+                loge("setOperatorBrandOverride: Cannot find subscription info for sub " + subId);
+                return false;
+            }
+
+            List<SubscriptionInfo> subInfos = new ArrayList<>();
+            subInfos.add(subInfo.toSubscriptionInfo());
+            String groupUuid = subInfo.getGroupUuid();
+            if (!TextUtils.isEmpty(groupUuid)) {
+                subInfos.addAll(SubscriptionManagerService.getInstance()
+                        .getSubscriptionsInGroup(ParcelUuid.fromString(groupUuid),
+                                mContext.getOpPackageName(), mContext.getFeatureId()));
+            }
+
+            if (subInfos.stream().noneMatch(info -> TextUtils.equals(IccUtils.stripTrailingFs(
+                    info.getIccId()), IccUtils.stripTrailingFs(iccId)))) {
+                loge("iccId doesn't match current active subId.");
+                return false;
+            }
+        } else {
+            if (!SubscriptionController.getInstance().checkPhoneIdAndIccIdMatch(
+                    getPhoneId(), iccId)) {
+                loge("iccId doesn't match current active subId.");
+                return false;
+            }
         }
 
         SharedPreferences.Editor spEditor =
diff --git a/src/java/com/android/internal/telephony/uicc/UiccSlot.java b/src/java/com/android/internal/telephony/uicc/UiccSlot.java
index 9b5b315..a395b2f 100644
--- a/src/java/com/android/internal/telephony/uicc/UiccSlot.java
+++ b/src/java/com/android/internal/telephony/uicc/UiccSlot.java
@@ -41,6 +41,7 @@
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.uicc.IccCardStatus.CardState;
+import com.android.internal.telephony.uicc.IccSlotStatus.MultipleEnabledProfilesMode;
 import com.android.internal.telephony.uicc.euicc.EuiccCard;
 import com.android.internal.telephony.util.TelephonyUtils;
 import com.android.telephony.Rlog;
@@ -87,6 +88,8 @@
     private String mEid;
     private AnswerToReset mAtr;
     private boolean mIsRemovable;
+    private MultipleEnabledProfilesMode mSupportedMepMode;
+
     // Map each available portIdx to phoneId
     private HashMap<Integer, Integer> mPortIdxToPhoneId = new HashMap<>();
     //Map each available portIdx with old radio state for state checking
@@ -102,13 +105,13 @@
         mContext = c;
         mActive = isActive;
         mCardState = null;
+        mSupportedMepMode = MultipleEnabledProfilesMode.NONE;
     }
 
     /**
      * Update slot. The main trigger for this is a change in the ICC Card status.
      */
     public void update(CommandsInterface ci, IccCardStatus ics, int phoneId, int slotIndex) {
-        if (DBG) log("cardStatus update: " + ics.toString());
         synchronized (mLock) {
             mPortIdxToPhoneId.put(ics.mSlotPortMapping.mPortIndex, phoneId);
             CardState oldState = mCardState;
@@ -116,6 +119,10 @@
             mIccIds.put(ics.mSlotPortMapping.mPortIndex, ics.iccid);
             parseAtr(ics.atr);
             mIsRemovable = isSlotRemovable(slotIndex);
+            // Update supported MEP mode in IccCardStatus if the CardState is present.
+            if (ics.mCardState.isCardPresent()) {
+                mSupportedMepMode = ics.mSupportedMepMode;
+            }
 
             int radioState = ci.getRadioState();
             if (DBG) {
@@ -148,7 +155,8 @@
 
                 if (!mIsEuicc) {
                     // Uicc does not support MEP, passing false by default.
-                    mUiccCard = new UiccCard(mContext, ci, ics, phoneId, mLock, false);
+                    mUiccCard = new UiccCard(mContext, ci, ics, phoneId, mLock, false,
+                            MultipleEnabledProfilesMode.NONE);
                 } else {
                     // The EID should be reported with the card status, but in case it's not we want
                     // to catch that here
@@ -157,7 +165,7 @@
                                 + Rlog.pii(TelephonyUtils.IS_DEBUGGABLE, ics.eid));
                     }
                     mUiccCard = new EuiccCard(mContext, ci, ics, phoneId, mLock,
-                            isMultipleEnabledProfileSupported());
+                            isMultipleEnabledProfileSupported(), getSupportedMepMode());
                 }
             } else {
                 if (mUiccCard != null) {
@@ -172,7 +180,6 @@
      * Update slot based on IccSlotStatus.
      */
     public void update(CommandsInterface[] ci, IccSlotStatus iss, int slotIndex) {
-        if (DBG) log("slotStatus update: " + iss.toString());
         synchronized (mLock) {
             IccSimPortInfo[] simPortInfos = iss.mSimPortInfos;
             CardState oldState = mCardState;
@@ -180,6 +187,7 @@
             mCardState = iss.cardState;
             mEid = iss.eid;
             mIsRemovable = isSlotRemovable(slotIndex);
+            mSupportedMepMode = iss.mSupportedMepMode;
 
             for (int i = 0; i < simPortInfos.length; i++) {
                 int phoneId = iss.mSimPortInfos[i].mLogicalSlotIndex;
@@ -187,9 +195,15 @@
                 if (!iss.mSimPortInfos[i].mPortActive) {
                     // TODO: (b/79432584) evaluate whether should broadcast card state change
                     // even if it's inactive.
-                    UiccController.updateInternalIccStateForInactivePort(mContext,
-                            mPortIdxToPhoneId.getOrDefault(i, INVALID_PHONE_ID),
-                            iss.mSimPortInfos[i].mIccId);
+                    if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                        UiccController.getInstance().updateSimStateForInactivePort(
+                                mPortIdxToPhoneId.getOrDefault(i, INVALID_PHONE_ID),
+                                iss.mSimPortInfos[i].mIccId);
+                    } else {
+                        UiccController.updateInternalIccStateForInactivePort(mContext,
+                                mPortIdxToPhoneId.getOrDefault(i, INVALID_PHONE_ID),
+                                iss.mSimPortInfos[i].mIccId);
+                    }
                     mLastRadioState.put(i, TelephonyManager.RADIO_POWER_UNAVAILABLE);
                     if (mUiccCard != null) {
                         // Dispose the port
@@ -226,7 +240,8 @@
             // Since the MEP capability is related with number ports reported, thus need to
             // update the flag after UiccCard creation.
             if (mUiccCard != null) {
-                mUiccCard.updateSupportMultipleEnabledProfile(isMultipleEnabledProfileSupported());
+                mUiccCard.updateSupportMepProperties(isMultipleEnabledProfileSupported(),
+                        getSupportedMepMode());
             }
         }
     }
@@ -321,8 +336,14 @@
             if (DBG) log("update: notify card removed");
             sendMessage(obtainMessage(EVENT_CARD_REMOVED, null));
         }
-        UiccController.updateInternalIccState(mContext, IccCardConstants.State.ABSENT,
-                null, phoneId);
+
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            UiccController.getInstance().updateSimState(phoneId, IccCardConstants.State.ABSENT,
+                    null);
+        } else {
+            UiccController.updateInternalIccState(mContext, IccCardConstants.State.ABSENT,
+                    null, phoneId);
+        }
         // no card present in the slot now; dispose card and make mUiccCard null
         nullifyUiccCard(false /* sim state is not unknown */);
         mLastRadioState.put(portIndex, TelephonyManager.RADIO_POWER_UNAVAILABLE);
@@ -569,14 +590,27 @@
     }
 
     /**
+     * Returns the supported MEP mode.
+     */
+    public MultipleEnabledProfilesMode getSupportedMepMode() {
+        synchronized (mLock) {
+            return mSupportedMepMode;
+        }
+    }
+    /**
      * Processes radio state unavailable event
      */
     public void onRadioStateUnavailable(int phoneId) {
         nullifyUiccCard(true /* sim state is unknown */);
 
         if (phoneId != INVALID_PHONE_ID) {
-            UiccController.updateInternalIccState(
-                    mContext, IccCardConstants.State.UNKNOWN, null, phoneId);
+            if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+                UiccController.getInstance().updateSimState(phoneId,
+                        IccCardConstants.State.UNKNOWN, null);
+            } else {
+                UiccController.updateInternalIccState(
+                        mContext, IccCardConstants.State.UNKNOWN, null, phoneId);
+            }
             mLastRadioState.put(getPortIndexFromPhoneId(phoneId),
                     TelephonyManager.RADIO_POWER_UNAVAILABLE);
         }
@@ -610,10 +644,11 @@
                 + isMultipleEnabledProfileSupported());
         pw.println(" mIsRemovable=" + mIsRemovable);
         pw.println(" mLastRadioState=" + mLastRadioState);
-        pw.println(" mIccIds=" + getPrintableIccIds());
+        pw.println(" mIccIds=" + Rlog.pii(TAG, getPrintableIccIds()));
         pw.println(" mPortIdxToPhoneId=" + mPortIdxToPhoneId);
         pw.println(" mEid=" + Rlog.pii(TelephonyUtils.IS_DEBUGGABLE, mEid));
         pw.println(" mCardState=" + mCardState);
+        pw.println(" mSupportedMepMode=" + mSupportedMepMode);
         if (mUiccCard != null) {
             pw.println(" mUiccCard=" + mUiccCard);
             mUiccCard.dump(fd, pw, args);
diff --git a/src/java/com/android/internal/telephony/uicc/UsimFileHandler.java b/src/java/com/android/internal/telephony/uicc/UsimFileHandler.java
old mode 100755
new mode 100644
index ff8d63d..bc46f80
--- a/src/java/com/android/internal/telephony/uicc/UsimFileHandler.java
+++ b/src/java/com/android/internal/telephony/uicc/UsimFileHandler.java
@@ -68,6 +68,7 @@
         case EF_FPLMN:
         case EF_LRPLMNSI:
         case EF_HPPLMN:
+        case EF_SMSS:
             return MF_SIM + DF_ADF;
 
         case EF_PBR:
diff --git a/src/java/com/android/internal/telephony/uicc/UsimServiceTable.java b/src/java/com/android/internal/telephony/uicc/UsimServiceTable.java
index fc58d3c..ea2bf42 100644
--- a/src/java/com/android/internal/telephony/uicc/UsimServiceTable.java
+++ b/src/java/com/android/internal/telephony/uicc/UsimServiceTable.java
@@ -157,4 +157,8 @@
     protected Object[] getValues() {
         return UsimService.values();
     }
+
+    public byte[] getUSIMServiceTable() {
+        return mServiceTable;
+    }
 }
diff --git a/src/java/com/android/internal/telephony/uicc/euicc/EuiccCard.java b/src/java/com/android/internal/telephony/uicc/euicc/EuiccCard.java
index 75bc3ba..0d98e18 100644
--- a/src/java/com/android/internal/telephony/uicc/euicc/EuiccCard.java
+++ b/src/java/com/android/internal/telephony/uicc/euicc/EuiccCard.java
@@ -27,6 +27,7 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.uicc.IccCardStatus;
+import com.android.internal.telephony.uicc.IccSlotStatus.MultipleEnabledProfilesMode;
 import com.android.internal.telephony.uicc.UiccCard;
 import com.android.internal.telephony.uicc.UiccPort;
 import com.android.internal.telephony.uicc.euicc.async.AsyncResultCallback;
@@ -43,8 +44,9 @@
     private RegistrantList mEidReadyRegistrants;
 
     public EuiccCard(Context c, CommandsInterface ci, IccCardStatus ics, int phoneId, Object lock,
-            boolean isSupportsMultipleEnabledProfiles) {
-        super(c, ci, ics, phoneId, lock, isSupportsMultipleEnabledProfiles);
+            boolean isSupportsMultipleEnabledProfiles,
+            MultipleEnabledProfilesMode supportedMepMode) {
+        super(c, ci, ics, phoneId, lock, isSupportsMultipleEnabledProfiles, supportedMepMode);
         if (TextUtils.isEmpty(ics.eid)) {
             loge("no eid given in constructor for phone " + phoneId);
             loadEidAndNotifyRegistrants();
@@ -61,11 +63,13 @@
      * UiccCard creation which will impact UICC MEP capability.
      */
     @Override
-    public void updateSupportMultipleEnabledProfile(boolean supported) {
+    public void updateSupportMepProperties(boolean supported,
+            MultipleEnabledProfilesMode supportedMepMode) {
         mIsSupportsMultipleEnabledProfiles = supported;
+        mSupportedMepMode = supportedMepMode;
         for (UiccPort port : mUiccPorts.values()) {
             if (port instanceof EuiccPort) {
-                ((EuiccPort) port).updateSupportMultipleEnabledProfile(supported);
+                ((EuiccPort) port).updateSupportMepProperties(supported, supportedMepMode);
             } else {
                 loge("eUICC card has non-euicc port object:" + port.toString());
             }
diff --git a/src/java/com/android/internal/telephony/uicc/euicc/EuiccPort.java b/src/java/com/android/internal/telephony/uicc/euicc/EuiccPort.java
index 639915a..3dd260e 100644
--- a/src/java/com/android/internal/telephony/uicc/euicc/EuiccPort.java
+++ b/src/java/com/android/internal/telephony/uicc/euicc/EuiccPort.java
@@ -35,7 +35,9 @@
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.uicc.IccCardStatus;
 import com.android.internal.telephony.uicc.IccIoResult;
+import com.android.internal.telephony.uicc.IccSlotStatus.MultipleEnabledProfilesMode;
 import com.android.internal.telephony.uicc.IccUtils;
+import com.android.internal.telephony.uicc.PortUtils;
 import com.android.internal.telephony.uicc.UiccCard;
 import com.android.internal.telephony.uicc.UiccPort;
 import com.android.internal.telephony.uicc.asn1.Asn1Decoder;
@@ -101,9 +103,6 @@
     private static final String DEV_CAP_NR5GC = "nr5gc";
     private static final String DEV_CAP_EUTRAN5GC = "eutran5gc";
 
-    private static final String ATR_ESIM_OS_V_M5 =
-            "3B9F97C00AB1FE453FC6838031E073FE211F65D002341569810F21";
-
     // These interfaces are used for simplifying the code by leveraging lambdas.
     private interface ApduRequestBuilder {
         void build(RequestBuilder requestBuilder)
@@ -128,10 +127,11 @@
     private volatile String mEid;
     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
     public boolean mIsSupportsMultipleEnabledProfiles;
-    private String mAtr;
+    private MultipleEnabledProfilesMode mSupportedMepMode;
 
     public EuiccPort(Context c, CommandsInterface ci, IccCardStatus ics, int phoneId, Object lock,
-            UiccCard card, boolean isSupportsMultipleEnabledProfiles) {
+            UiccCard card, boolean isSupportsMultipleEnabledProfiles,
+            MultipleEnabledProfilesMode supportedMepMode) {
         super(c, ci, ics, phoneId, lock, card);
         // TODO: Set supportExtendedApdu based on ATR.
         mApduSender = new ApduSender(ci, ISD_R_AID, false /* supportExtendedApdu */);
@@ -141,8 +141,8 @@
             mEid = ics.eid;
             mCardId = ics.eid;
         }
-        mAtr = ics.atr;
         mIsSupportsMultipleEnabledProfiles = isSupportsMultipleEnabledProfiles;
+        mSupportedMepMode = supportedMepMode;
     }
 
     /**
@@ -165,18 +165,19 @@
             if (!TextUtils.isEmpty(ics.eid)) {
                 mEid = ics.eid;
             }
-            mAtr = ics.atr;
             super.update(c, ci, ics, uiccCard);
         }
     }
 
     /**
-     * Updates MEP(Multiple Enabled Profile) support flag.
+     * Updates MEP(Multiple Enabled Profile) support and mode flags.
      * The flag can be updated after the port creation.
      */
-    public void updateSupportMultipleEnabledProfile(boolean supported) {
+    public void updateSupportMepProperties(boolean supported,
+            MultipleEnabledProfilesMode supportedMepMode) {
         logd("updateSupportMultipleEnabledProfile");
         mIsSupportsMultipleEnabledProfiles = supported;
+        mSupportedMepMode = supportedMepMode;
     }
 
     /**
@@ -187,13 +188,8 @@
      * @since 1.1.0 [GSMA SGP.22]
      */
     public void getAllProfiles(AsyncResultCallback<EuiccProfileInfo[]> callback, Handler handler) {
-        byte[] profileTags;
-        if (mIsSupportsMultipleEnabledProfiles) {
-            profileTags = ATR_ESIM_OS_V_M5.equals(mAtr)
-                    ? Tags.EUICC_PROFILE_MEP_TAGS : Tags.EUICC_PROFILE_MEP_TAGS_WITH_9F20;
-        } else {
-            profileTags = Tags.EUICC_PROFILE_TAGS;
-        }
+        byte[] profileTags = mIsSupportsMultipleEnabledProfiles ? Tags.EUICC_PROFILE_MEP_TAGS
+                : Tags.EUICC_PROFILE_TAGS;
         sendApdu(
                 newRequestProvider((RequestBuilder requestBuilder) ->
                         requestBuilder.addStoreData(Asn1Node.newBuilder(Tags.TAG_GET_PROFILES)
@@ -234,13 +230,8 @@
      */
     public final void getProfile(String iccid, AsyncResultCallback<EuiccProfileInfo> callback,
             Handler handler) {
-        byte[] profileTags;
-        if (mIsSupportsMultipleEnabledProfiles) {
-            profileTags = ATR_ESIM_OS_V_M5.equals(mAtr)
-                    ? Tags.EUICC_PROFILE_MEP_TAGS : Tags.EUICC_PROFILE_MEP_TAGS_WITH_9F20;
-        } else {
-            profileTags = Tags.EUICC_PROFILE_TAGS;
-        }
+        byte[] profileTags = mIsSupportsMultipleEnabledProfiles ? Tags.EUICC_PROFILE_MEP_TAGS
+                : Tags.EUICC_PROFILE_TAGS;
         sendApdu(
                 newRequestProvider((RequestBuilder requestBuilder) ->
                         requestBuilder.addStoreData(Asn1Node.newBuilder(Tags.TAG_GET_PROFILES)
@@ -319,11 +310,20 @@
         sendApduWithSimResetErrorWorkaround(
                 newRequestProvider((RequestBuilder requestBuilder) -> {
                     byte[] iccidBytes = IccUtils.bcdToBytes(padTrailingFs(iccid));
-                    requestBuilder.addStoreData(Asn1Node.newBuilder(Tags.TAG_ENABLE_PROFILE)
+                    Asn1Node.Builder builder = Asn1Node.newBuilder(Tags.TAG_ENABLE_PROFILE)
                             .addChild(Asn1Node.newBuilder(Tags.TAG_CTX_COMP_0)
                                     .addChildAsBytes(Tags.TAG_ICCID, iccidBytes))
-                            .addChildAsBoolean(Tags.TAG_CTX_1, refresh)
-                            .build().toHex());
+                            .addChildAsBoolean(Tags.TAG_CTX_1, refresh);
+                    // Port index should be added only in case of MEP-A1 mode.
+                    if (mSupportedMepMode.isMepA1Mode()) {
+                        // In case of MEP-A1 and MEP-A2, profiles are selected on eSIM Ports 1 and
+                        // higher (refer as target port). Hence, convert the portIndex to
+                        // target port index before adding.
+                        builder.addChildAsInteger(Tags.TAG_CTX_2,
+                                PortUtils.convertToHalPortIndex(mSupportedMepMode,
+                                        super.getPortIdx()));
+                    }
+                    requestBuilder.addStoreData(builder.build().toHex());
                 }),
                 response -> {
                     int result;
@@ -1262,10 +1262,8 @@
             // if the Profile is in the Enabled state on the same eSIM Port as where this
             // getProfilesInfo command was sent. So should check for enabledOnEsimPort(TAG_PORT)
             // tag and verify its value is a valid port (means port value is >=0) or not.
-            if ((profileNode.hasChild(Tags.TAG_PORT)
-                    && profileNode.getChild(Tags.TAG_PORT).asInteger() >= 0)
-                    || (profileNode.hasChild(Tags.TAG_PORT_9F20)
-                    && profileNode.getChild(Tags.TAG_PORT_9F20).asInteger() >= 0)) {
+            if (profileNode.hasChild(Tags.TAG_PORT)
+                    && profileNode.getChild(Tags.TAG_PORT).asInteger() >= 0) {
                 profileBuilder.setState(EuiccProfileInfo.PROFILE_STATE_ENABLED);
             } else {
                 // noinspection WrongConstant
diff --git a/src/java/com/android/internal/telephony/uicc/euicc/Tags.java b/src/java/com/android/internal/telephony/uicc/euicc/Tags.java
index 3aa9ef9..befc7f9 100644
--- a/src/java/com/android/internal/telephony/uicc/euicc/Tags.java
+++ b/src/java/com/android/internal/telephony/uicc/euicc/Tags.java
@@ -84,10 +84,7 @@
     static final int TAG_PROFILE_NAME = 0x92;
     static final int TAG_OPERATOR_ID = 0xB7;
     static final int TAG_CARRIER_PRIVILEGE_RULES = 0xBF76;
-    // TODO: PORT TAG(9F20 OR 9F24) will be used based on ATR Strings because not all MEP capable
-    //  devices have M5 OS. Once modem team is ready, revert back to 9F24 TAG only.
     static final int TAG_PORT = 0x9F24;
-    static final int TAG_PORT_9F20 = 0x9F20;
 
     // Tags from the RefArDo data standard - https://source.android.com/devices/tech/config/uicc
     static final int TAG_REF_AR_DO = 0xE2;
@@ -129,22 +126,5 @@
             (byte) (TAG_PORT % 256),
     };
 
-    // TAG list for Euicc Profile with 9F20 tag.
-    // TODO: This is temporary change, should be removed once all devices are upgraded to M5 OS.
-    static final byte[] EUICC_PROFILE_MEP_TAGS_WITH_9F20 = new byte[] {
-            TAG_ICCID,
-            (byte) TAG_NICKNAME,
-            (byte) TAG_SERVICE_PROVIDER_NAME,
-            (byte) TAG_PROFILE_NAME,
-            (byte) TAG_OPERATOR_ID,
-            (byte) (TAG_PROFILE_STATE / 256),
-            (byte) (TAG_PROFILE_STATE % 256),
-            (byte) TAG_PROFILE_CLASS,
-            (byte) TAG_PROFILE_POLICY_RULE,
-            (byte) (TAG_CARRIER_PRIVILEGE_RULES / 256),
-            (byte) (TAG_CARRIER_PRIVILEGE_RULES % 256),
-            (byte) (TAG_PORT_9F20 / 256),
-            (byte) (TAG_PORT_9F20 % 256),
-    };
     private Tags() {}
 }
diff --git a/src/java/com/android/internal/telephony/uicc/euicc/apdu/ApduCommand.java b/src/java/com/android/internal/telephony/uicc/euicc/apdu/ApduCommand.java
index 7a8978f..8fbeb44 100644
--- a/src/java/com/android/internal/telephony/uicc/euicc/apdu/ApduCommand.java
+++ b/src/java/com/android/internal/telephony/uicc/euicc/apdu/ApduCommand.java
@@ -43,6 +43,12 @@
     /** Command data of an APDU as defined in GlobalPlatform Card Specification v.2.3. */
     public final String cmdHex;
 
+    /**
+     * isEs10 indicates that the current streaming APDU contains an ES10 command or it is a regular
+     * APDU. (As per spec SGP.22 V3.0, ES10 commands needs to be sent over command port of MEP-A1)
+     */
+    public final boolean isEs10;
+
     /** The parameters are defined as in GlobalPlatform Card Specification v.2.3. */
     ApduCommand(int channel, int cla, int ins, int p1, int p2, int p3, String cmdHex) {
         this.channel = channel;
@@ -52,11 +58,14 @@
         this.p2 = p2;
         this.p3 = p3;
         this.cmdHex = cmdHex;
+        // TODO: Currently ApduCommand is used for ES10 commands, so updating to true by default.
+        //  Modify it in case used for non ES10 commands in future.
+        this.isEs10 = true;
     }
 
     @Override
     public String toString() {
         return "ApduCommand(channel=" + channel + ", cla=" + cla + ", ins=" + ins + ", p1=" + p1
-                + ", p2=" + p2 + ", p3=" + p3 + ", cmd=" + cmdHex + ")";
+                + ", p2=" + p2 + ", p3=" + p3 + ", cmd=" + cmdHex + ", isEs10=" + isEs10 + ")";
     }
 }
diff --git a/src/java/com/android/internal/telephony/uicc/euicc/apdu/TransmitApduLogicalChannelInvocation.java b/src/java/com/android/internal/telephony/uicc/euicc/apdu/TransmitApduLogicalChannelInvocation.java
index 09de54a..ca75beb 100644
--- a/src/java/com/android/internal/telephony/uicc/euicc/apdu/TransmitApduLogicalChannelInvocation.java
+++ b/src/java/com/android/internal/telephony/uicc/euicc/apdu/TransmitApduLogicalChannelInvocation.java
@@ -48,7 +48,8 @@
     protected void sendRequestMessage(ApduCommand command, Message msg) {
         Rlog.v(LOG_TAG, "Send: " + command);
         mCi.iccTransmitApduLogicalChannel(command.channel, command.cla | command.channel,
-                command.ins, command.p1, command.p2, command.p3, command.cmdHex, msg);
+                command.ins, command.p1, command.p2, command.p3, command.cmdHex, command.isEs10,
+                msg);
     }
 
     @Override
diff --git a/src/java/com/android/internal/telephony/util/SMSDispatcherUtil.java b/src/java/com/android/internal/telephony/util/SMSDispatcherUtil.java
index 17f2611..da429b7 100644
--- a/src/java/com/android/internal/telephony/util/SMSDispatcherUtil.java
+++ b/src/java/com/android/internal/telephony/util/SMSDispatcherUtil.java
@@ -16,12 +16,11 @@
 
 package com.android.internal.telephony.util;
 
-import com.android.internal.telephony.ImsSmsDispatcher;
-import com.android.internal.telephony.cdma.CdmaSMSDispatcher;
 import com.android.internal.telephony.GsmAlphabet.TextEncodingDetails;
-import com.android.internal.telephony.Phone;
-import com.android.internal.telephony.SmsMessageBase;
+import com.android.internal.telephony.ImsSmsDispatcher;
 import com.android.internal.telephony.SmsHeader;
+import com.android.internal.telephony.SmsMessageBase;
+import com.android.internal.telephony.cdma.CdmaSMSDispatcher;
 import com.android.internal.telephony.gsm.GsmSMSDispatcher;
 
 /**
@@ -95,6 +94,46 @@
     }
 
     /**
+     * Trigger the proper implementation for getting submit pdu for text sms based on format.
+     *
+     * @param isCdma true if cdma format should be used.
+     * @param scAddr is the service center address or null to use the current default SMSC
+     * @param destAddr the address to send the message to
+     * @param message the body of the message.
+     * @param statusReportRequested whether or not a status report is requested.
+     * @param smsHeader message header.
+     * @param priority Priority level of the message
+     *  Refer specification See 3GPP2 C.S0015-B, v2.0, table 4.5.9-1
+     *  ---------------------------------
+     *  PRIORITY      | Level of Priority
+     *  ---------------------------------
+     *      '00'      |     Normal
+     *      '01'      |     Interactive
+     *      '10'      |     Urgent
+     *      '11'      |     Emergency
+     *  ----------------------------------
+     *  Any Other values included Negative considered as Invalid Priority Indicator of the message.
+     * @param validityPeriod Validity Period of the message in mins.
+     *  Refer specification 3GPP TS 23.040 V6.8.1 section 9.2.3.12.1.
+     *  Validity Period(Minimum) -> 5 mins
+     *  Validity Period(Maximum) -> 635040 mins(i.e.63 weeks).
+     *  Any Other values included Negative considered as Invalid Validity Period of the message.
+     * @param messageRef TP Message Reference number
+     * @return the submit pdu.
+     */
+    public static SmsMessageBase.SubmitPduBase getSubmitPdu(boolean isCdma, String scAddr,
+            String destAddr, String message, boolean statusReportRequested, SmsHeader smsHeader,
+            int priority, int validityPeriod, int messageRef) {
+        if (isCdma) {
+            return getSubmitPduCdma(scAddr, destAddr, message, statusReportRequested, smsHeader,
+                    priority);
+        } else {
+            return getSubmitPduGsm(scAddr, destAddr, message, statusReportRequested,
+                    validityPeriod, messageRef);
+        }
+    }
+
+    /**
      * Gsm implementation for
      * {@link #getSubmitPdu(boolean, String, String, String, boolean)}
      *
@@ -132,6 +171,28 @@
     }
 
     /**
+     * Gsm implementation for
+     * {@link #getSubmitPdu(boolean, String, String, String, boolean, int)}
+     *
+     * @param scAddr is the service center address or null to use the current default SMSC
+     * @param destAddr the address to send the message to
+     * @param message the body of the message.
+     * @param statusReportRequested whether or not a status report is requested.
+     * @param validityPeriod Validity Period of the message in mins.
+     *  Refer specification 3GPP TS 23.040 V6.8.1 section 9.2.3.12.1.
+     *  Validity Period(Minimum) -> 5 mins
+     *  Validity Period(Maximum) -> 635040 mins(i.e.63 weeks).
+     *  Any Other values included Negative considered as Invalid Validity Period of the message.
+     * @param messageRef TP Message Reference number
+     * @return the submit pdu.
+     */
+    public static SmsMessageBase.SubmitPduBase getSubmitPduGsm(String scAddr, String destAddr,
+            String message, boolean statusReportRequested, int validityPeriod, int messageRef) {
+        return com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(scAddr, destAddr, message,
+                statusReportRequested, null, 0, 0, 0, validityPeriod, messageRef);
+    }
+
+    /**
      * Cdma implementation for
      * {@link #getSubmitPdu(boolean, String, String, String, boolean, SmsHeader)}
      *
@@ -197,6 +258,29 @@
     }
 
     /**
+     * Trigger the proper implementation for getting submit pdu for data sms based on format.
+     *
+     * @param isCdma true if cdma format should be used.
+     * @param destAddr the address to send the message to
+     * @param scAddr is the service center address or null to use the current default SMSC
+     * @param destPort the port to deliver the message to
+     * @param message the body of the message to send
+     * @param statusReportRequested whether or not a status report is requested.
+     * @param messageRef TP Message Reference number
+     * @return the submit pdu.
+     */
+    public static SmsMessageBase.SubmitPduBase getSubmitPdu(boolean isCdma, String scAddr,
+            String destAddr, int destPort, byte[] message, boolean statusReportRequested,
+            int messageRef) {
+        if (isCdma) {
+            return getSubmitPduCdma(scAddr, destAddr, destPort, message, statusReportRequested);
+        } else {
+            return getSubmitPduGsm(scAddr, destAddr, destPort, message, statusReportRequested,
+                    messageRef);
+        }
+    }
+
+    /**
      * Cdma implementation of {@link #getSubmitPdu(boolean, String, String, int, byte[], boolean)}
 
      * @param destAddr the address to send the message to
@@ -230,6 +314,22 @@
     }
 
     /**
+     * Gsm implementation of {@link #getSubmitPdu(boolean, String, String, int, byte[], boolean)}
+     *
+     * @param destAddr the address to send the message to
+     * @param scAddr is the service center address or null to use the current default SMSC
+     * @param destPort the port to deliver the message to
+     * @param message the body of the message to send
+     * @param statusReportRequested whether or not a status report is requested.
+     * @return the submit pdu.
+     */
+    public static SmsMessageBase.SubmitPduBase getSubmitPduGsm(String scAddr, String destAddr,
+            int destPort, byte[] message, boolean statusReportRequested, int messageRef) {
+        return com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(scAddr, destAddr,
+                destPort, message, statusReportRequested, messageRef);
+    }
+
+    /**
      * Calculate the number of septets needed to encode the message. This function should only be
      * called for individual segments of multipart message.
      *
diff --git a/tests/telephonytests/assets/eccdata b/tests/telephonytests/assets/eccdata
index 4c5959b..bd9ff47 100644
--- a/tests/telephonytests/assets/eccdata
+++ b/tests/telephonytests/assets/eccdata
Binary files differ
diff --git a/tests/telephonytests/assets/eccdata_input.txt b/tests/telephonytests/assets/eccdata_input.txt
index 8dcfa75..6f8632c 100644
--- a/tests/telephonytests/assets/eccdata_input.txt
+++ b/tests/telephonytests/assets/eccdata_input.txt
@@ -10,6 +10,57 @@
     types: MOUNTAIN_RESCUE
     types: MIEC
     types: AIEC
+    routing: NORMAL
+    normal_routing_mncs: "05"
+  }
+  eccs {
+    phone_number: "888"
+    types: POLICE
+    types: AMBULANCE
+    types: FIRE
+    types: MARINE_GUARD
+    types: MOUNTAIN_RESCUE
+    types: MIEC
+    types: AIEC
+    routing: NORMAL
+    normal_routing_mncs: "45"
+    normal_routing_mncs: "47"
+    normal_routing_mncs: "05"
+  }
+  eccs {
+    phone_number: "654321"
+    types: POLICE
+    types: AMBULANCE
+    types: FIRE
+    types: MARINE_GUARD
+    types: MOUNTAIN_RESCUE
+    types: MIEC
+    types: AIEC
+    routing: EMERGENCY
+    normal_routing_mncs: ""
+  }
+  eccs {
+    phone_number: "7654321"
+    types: POLICE
+    types: AMBULANCE
+    types: FIRE
+    types: MARINE_GUARD
+    types: MOUNTAIN_RESCUE
+    types: MIEC
+    types: AIEC
+    routing: NORMAL
+    normal_routing_mncs: ""
+  }
+  eccs {
+    phone_number: "4321"
+    types: POLICE
+    types: AMBULANCE
+    types: FIRE
+    types: MARINE_GUARD
+    types: MOUNTAIN_RESCUE
+    types: MIEC
+    types: AIEC
+    routing: NORMAL
   }
   ecc_fallback: "911"
 }
diff --git a/tests/telephonytests/src/android/telephony/ims/ImsRegistrationTests.java b/tests/telephonytests/src/android/telephony/ims/ImsRegistrationTests.java
index af80bd7..56ce6bc 100644
--- a/tests/telephonytests/src/android/telephony/ims/ImsRegistrationTests.java
+++ b/tests/telephonytests/src/android/telephony/ims/ImsRegistrationTests.java
@@ -16,9 +16,13 @@
 
 package android.telephony.ims;
 
+import static android.telephony.ims.RegistrationManager.SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK;
+import static android.telephony.ims.stub.ImsRegistrationImplBase.REGISTRATION_TECH_LTE;
+
 import static junit.framework.Assert.assertEquals;
 
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
@@ -147,7 +151,33 @@
         ImsReasonInfo info = new ImsReasonInfo();
         mRegistration.onDeregistered(info);
 
-        verify(mCallback).onDeregistered(eq(info));
+        verify(mCallback).onDeregistered(eq(info), anyInt(), anyInt());
+    }
+
+    @SmallTest
+    @Test
+    public void testRegistrationCallbackOnDeregisteredWithSuggestedAction() throws RemoteException {
+        ImsReasonInfo info = new ImsReasonInfo();
+        mRegistration.onDeregistered(info,
+                SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK, REGISTRATION_TECH_LTE);
+
+        verify(mCallback).onDeregistered(eq(info),
+                eq(SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK), eq(REGISTRATION_TECH_LTE));
+    }
+
+    @SmallTest
+    @Test
+    public void testRegistrationCallbackOnDeregisteredWithRegistrationAttributes()
+            throws RemoteException {
+        ImsReasonInfo info = new ImsReasonInfo();
+        SipDetails details = new SipDetails.Builder(SipDetails.METHOD_REGISTER)
+                .setCSeq(1).setSipResponseCode(200, "OK")
+                .setSipResponseReasonHeader(10, "reasonText")
+                .setCallId("testCallId").build();
+
+        mRegistration.onDeregistered(info, details);
+
+        verify(mCallback).onDeregisteredWithDetails(eq(info), anyInt(), anyInt(), eq(details));
     }
 
     @SmallTest
@@ -218,10 +248,10 @@
 
         // The original callback that has been registered should get LTE tech in disconnected
         // message
-        verify(mCallback).onDeregistered(eq(info));
+        verify(mCallback).onDeregistered(eq(info), anyInt(), anyInt());
         // A callback that has just been registered should get NONE for tech in disconnected
         // message
-        verify(mCallback2).onDeregistered(eq(info));
+        verify(mCallback2).onDeregistered(eq(info), anyInt(), anyInt());
     }
 
     @SmallTest
@@ -231,7 +261,7 @@
 
         mRegistration.onDeregistered(info);
 
-        verify(mCallback).onDeregistered(eq(info));
+        verify(mCallback).onDeregistered(eq(info), anyInt(), anyInt());
         assertEquals(ImsRegistrationImplBase.REGISTRATION_TECH_NONE,
                 mRegBinder.getRegistrationTechnology());
     }
@@ -242,7 +272,7 @@
         mRegBinder.addRegistrationCallback(mCallback2);
         // Verify that if we have never set the registration state, we do not callback immediately
         // with onUnregistered.
-        verify(mCallback2, never()).onDeregistered(any(ImsReasonInfo.class));
+        verify(mCallback2, never()).onDeregistered(any(ImsReasonInfo.class), anyInt(), anyInt());
     }
 
     @SmallTest
diff --git a/tests/telephonytests/src/android/telephony/ims/MmTelFeatureTests.java b/tests/telephonytests/src/android/telephony/ims/MmTelFeatureTests.java
index 5af871c..b002b35 100644
--- a/tests/telephonytests/src/android/telephony/ims/MmTelFeatureTests.java
+++ b/tests/telephonytests/src/android/telephony/ims/MmTelFeatureTests.java
@@ -25,6 +25,7 @@
 import static org.mockito.Mockito.verify;
 
 import android.net.Uri;
+import android.os.Bundle;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
@@ -151,13 +152,27 @@
 
         mFeature.incomingCall(session);
         ArgumentCaptor<IImsCallSession> captor = ArgumentCaptor.forClass(IImsCallSession.class);
-        verify(mListener).onIncomingCall(captor.capture(), any());
+        verify(mListener).onIncomingCall(captor.capture(), eq(null), any());
 
         assertEquals(sessionBinder, captor.getValue());
     }
 
     @SmallTest
     @Test
+    public void testNewIncomingCallReturnListener() throws Exception {
+        IImsCallSession sessionBinder = Mockito.mock(IImsCallSession.class);
+        ImsCallSessionImplBase session = new ImsCallSessionImplBase();
+        session.setServiceImpl(sessionBinder);
+        String callId = "callID";
+        Bundle extra = new Bundle();
+        mFeature.incomingCall(session, callId, extra);
+        ArgumentCaptor<IImsCallSession> captor = ArgumentCaptor.forClass(IImsCallSession.class);
+        verify(mListener).onIncomingCall(captor.capture(), eq(callId), eq(extra));
+        assertEquals(sessionBinder, captor.getValue());
+    }
+
+    @SmallTest
+    @Test
     public void testSetTtyMessageMessenger() throws Exception {
         Message resultMessage = Message.obtain(mHandler, TEST_TTY_RESULT);
         resultMessage.replyTo = mHandlerMessenger;
diff --git a/tests/telephonytests/src/android/telephony/ims/TestMmTelFeature.java b/tests/telephonytests/src/android/telephony/ims/TestMmTelFeature.java
index 9ee1e30..1269cc8 100644
--- a/tests/telephonytests/src/android/telephony/ims/TestMmTelFeature.java
+++ b/tests/telephonytests/src/android/telephony/ims/TestMmTelFeature.java
@@ -64,6 +64,11 @@
         notifyIncomingCall(c, new Bundle());
     }
 
+    public ImsCallSessionListener incomingCall(
+            ImsCallSessionImplBase c, String callId, Bundle extra) {
+        return notifyIncomingCall(c, callId, extra);
+    }
+
     @Override
     public ImsCallProfile createCallProfile(int callSessionType, int callType) {
         return super.createCallProfile(callSessionType, callType);
diff --git a/tests/telephonytests/src/com/android/internal/telephony/CallStateTest.java b/tests/telephonytests/src/com/android/internal/telephony/CallStateTest.java
new file mode 100644
index 0000000..4e319a1
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/CallStateTest.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import static org.junit.Assert.assertEquals;
+
+import android.os.Parcel;
+import android.telephony.CallQuality;
+import android.telephony.CallState;
+import android.telephony.PreciseCallState;
+import android.telephony.TelephonyManager;
+import android.telephony.ims.ImsCallProfile;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import org.junit.Test;
+
+/**
+ * Simple GTS test verifying the parceling and unparceling of CallAttributes.
+ */
+public class CallStateTest extends AndroidTestCase {
+
+
+    @SmallTest
+    public void testParcelUnparcelPreciseCallState() {
+        CallState data = new CallState.Builder(PreciseCallState.PRECISE_CALL_STATE_INCOMING)
+                .setNetworkType(TelephonyManager.NETWORK_TYPE_LTE)
+                .setCallQuality(null)
+                .setCallClassification(CallState.CALL_CLASSIFICATION_RINGING)
+                .setImsCallSessionId("1")
+                .setImsCallServiceType(ImsCallProfile.SERVICE_TYPE_NONE)
+                .setImsCallType(ImsCallProfile.CALL_TYPE_NONE).build();
+
+        Parcel parcel = Parcel.obtain();
+        data.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        CallState unparceledData = CallState.CREATOR.createFromParcel(parcel);
+        parcel.recycle();
+
+        assertEquals("PreciseCallState is not equal after parceled/unparceled",
+                data.getCallState(),
+                unparceledData.getCallState());
+    }
+
+    @SmallTest
+    public void testParcelUnparcelCallQuality() {
+        CallQuality quality = new CallQuality();
+        CallState data = new CallState.Builder(PreciseCallState.PRECISE_CALL_STATE_IDLE)
+                .setNetworkType(TelephonyManager.NETWORK_TYPE_LTE)
+                .setCallQuality(null)
+                .setCallClassification(CallState.CALL_CLASSIFICATION_FOREGROUND)
+                .setImsCallSessionId(null)
+                .setImsCallServiceType(ImsCallProfile.SERVICE_TYPE_NONE)
+                .setImsCallType(ImsCallProfile.CALL_TYPE_NONE).build();
+
+
+        Parcel parcel = Parcel.obtain();
+        data.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        CallState unparceledData = CallState.CREATOR.createFromParcel(parcel);
+        parcel.recycle();
+
+        assertNull(unparceledData.getCallQuality());
+
+        data = new CallState.Builder(PreciseCallState.PRECISE_CALL_STATE_IDLE)
+                .setNetworkType(TelephonyManager.NETWORK_TYPE_LTE)
+                .setCallQuality(quality)
+                .setCallClassification(CallState.CALL_CLASSIFICATION_FOREGROUND)
+                .setImsCallSessionId(null)
+                .setImsCallServiceType(ImsCallProfile.SERVICE_TYPE_NONE)
+                .setImsCallType(ImsCallProfile.CALL_TYPE_NONE).build();
+
+
+        parcel = Parcel.obtain();
+        data.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        unparceledData = CallState.CREATOR.createFromParcel(parcel);
+        parcel.recycle();
+
+        assertEquals("CallQuality is not equal after parceled/unparceled",
+                data.getCallQuality(),
+                unparceledData.getCallQuality());
+    }
+
+    @SmallTest
+    public void testParcelUnparcelNetworkTypeAndClassification() {
+        CallQuality quality = new CallQuality();
+        CallState data = new CallState.Builder(PreciseCallState.PRECISE_CALL_STATE_DIALING)
+                .setNetworkType(TelephonyManager.NETWORK_TYPE_LTE)
+                .setCallQuality(null)
+                .setCallClassification(CallState.CALL_CLASSIFICATION_FOREGROUND)
+                .setImsCallSessionId("3")
+                .setImsCallServiceType(ImsCallProfile.SERVICE_TYPE_NONE)
+                .setImsCallType(ImsCallProfile.CALL_TYPE_NONE).build();
+
+        Parcel parcel = Parcel.obtain();
+        data.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        CallState unparceledData = CallState.CREATOR.createFromParcel(parcel);
+        parcel.recycle();
+
+        assertEquals("NetworkType is not equal after parceled/unparceled",
+                data.getNetworkType(),
+                unparceledData.getNetworkType());
+        assertEquals("Call classification is not equal after parceled/unparceled",
+                data.getCallClassification(),
+                unparceledData.getCallClassification());
+    }
+
+    @Test
+    public void testParcelUnparcelImsCallInfo() {
+        CallQuality quality = new CallQuality();
+        CallState data = new CallState.Builder(PreciseCallState.PRECISE_CALL_STATE_DIALING)
+                .setNetworkType(TelephonyManager.NETWORK_TYPE_LTE)
+                .setCallQuality(null)
+                .setCallClassification(CallState.CALL_CLASSIFICATION_FOREGROUND)
+                .setImsCallSessionId(null)
+                .setImsCallServiceType(ImsCallProfile.SERVICE_TYPE_NORMAL)
+                .setImsCallType(ImsCallProfile.CALL_TYPE_VOICE).build();
+
+        Parcel parcel = Parcel.obtain();
+        data.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        CallState unparceledData = CallState.CREATOR.createFromParcel(parcel);
+        parcel.recycle();
+
+        assertNull(unparceledData.getImsCallSessionId());
+
+        assertEquals("Ims call service type is not equal after parceled/unparceled",
+                data.getImsCallServiceType(),
+                unparceledData.getImsCallServiceType());
+
+        assertEquals("Ims call type is not equal after parceled/unparceled",
+                data.getImsCallType(),
+                unparceledData.getImsCallType());
+
+        data = new CallState.Builder(PreciseCallState.PRECISE_CALL_STATE_ACTIVE)
+                .setNetworkType(TelephonyManager.NETWORK_TYPE_LTE)
+                .setCallQuality(quality)
+                .setCallClassification(CallState.CALL_CLASSIFICATION_FOREGROUND)
+                .setImsCallSessionId("2")
+                .setImsCallServiceType(ImsCallProfile.SERVICE_TYPE_NORMAL)
+                .setImsCallType(ImsCallProfile.CALL_TYPE_VT).build();
+
+        parcel = Parcel.obtain();
+        data.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        unparceledData = CallState.CREATOR.createFromParcel(parcel);
+        parcel.recycle();
+
+        assertEquals("Ims call session ID is not equal after parceled/unparceled",
+                data.getImsCallSessionId(),
+                unparceledData.getImsCallSessionId());
+
+        assertEquals("Ims call service type is not equal after parceled/unparceled",
+                data.getImsCallServiceType(),
+                unparceledData.getImsCallServiceType());
+
+        assertEquals("Ims call type is not equal after parceled/unparceled",
+                data.getImsCallType(),
+                unparceledData.getImsCallType());
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/CallWaitingControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/CallWaitingControllerTest.java
new file mode 100644
index 0000000..18d054d
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/CallWaitingControllerTest.java
@@ -0,0 +1,466 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.internal.telephony;
+
+import static android.telephony.CarrierConfigManager.ImsSs.CALL_WAITING_SYNC_FIRST_CHANGE;
+import static android.telephony.CarrierConfigManager.ImsSs.CALL_WAITING_SYNC_FIRST_POWER_UP;
+import static android.telephony.CarrierConfigManager.ImsSs.CALL_WAITING_SYNC_IMS_ONLY;
+import static android.telephony.CarrierConfigManager.ImsSs.CALL_WAITING_SYNC_NONE;
+import static android.telephony.CarrierConfigManager.ImsSs.CALL_WAITING_SYNC_USER_CHANGE;
+import static android.telephony.CarrierConfigManager.ImsSs.KEY_TERMINAL_BASED_CALL_WAITING_DEFAULT_ENABLED_BOOL;
+import static android.telephony.CarrierConfigManager.ImsSs.KEY_TERMINAL_BASED_CALL_WAITING_SYNC_TYPE_INT;
+import static android.telephony.CarrierConfigManager.ImsSs.KEY_UT_TERMINAL_BASED_SERVICES_INT_ARRAY;
+import static android.telephony.CarrierConfigManager.ImsSs.SUPPLEMENTARY_SERVICE_CW;
+
+import static com.android.internal.telephony.CallWaitingController.KEY_CS_SYNC;
+import static com.android.internal.telephony.CallWaitingController.KEY_STATE;
+import static com.android.internal.telephony.CallWaitingController.KEY_SUB_ID;
+import static com.android.internal.telephony.CallWaitingController.PREFERENCE_TBCW;
+import static com.android.internal.telephony.CallWaitingController.TERMINAL_BASED_ACTIVATED;
+import static com.android.internal.telephony.CallWaitingController.TERMINAL_BASED_NOT_ACTIVATED;
+import static com.android.internal.telephony.CallWaitingController.TERMINAL_BASED_NOT_SUPPORTED;
+import static com.android.internal.telephony.CommandsInterface.SERVICE_CLASS_DATA;
+import static com.android.internal.telephony.CommandsInterface.SERVICE_CLASS_NONE;
+import static com.android.internal.telephony.CommandsInterface.SERVICE_CLASS_VOICE;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.doReturn;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.os.Message;
+import android.os.PersistableBundle;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class CallWaitingControllerTest extends TelephonyTest {
+    private static final int FAKE_SUB_ID = 1;
+
+    private static final int GET_DONE = 1;
+
+    private CallWaitingController mCWC;
+    private GetTestHandler mHandler;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(this.getClass().getSimpleName());
+        mSimulatedCommands.setRadioPower(true, null);
+        mPhone.mCi = this.mSimulatedCommands;
+        doReturn(FAKE_SUB_ID).when(mPhone).getSubId();
+
+        mCWC = new CallWaitingController(mPhone);
+        logd("CallWaitingController initiated, waiting for Power on");
+        /* Make sure radio state is power on before dial.
+         * When radio state changed from off to on, CallTracker
+         * will poll result from RIL. Avoid dialing triggered at the same*/
+        processAllMessages();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mCWC = null;
+        super.tearDown();
+    }
+
+    @Test
+    @SmallTest
+    public void testSetTerminalBasedCallWaitingSupported() {
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+        PersistableBundle bundle = getConfigBundle(true, CALL_WAITING_SYNC_NONE, true);
+        mCWC.updateCarrierConfig(FAKE_SUB_ID, bundle, true);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_ACTIVATED);
+
+        mCWC.setTerminalBasedCallWaitingSupported(false);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_SUPPORTED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_SUPPORTED);
+    }
+
+    @Test
+    @SmallTest
+    public void testInitialize() {
+        mCWC.setTerminalBasedCallWaitingSupported(false);
+        setPreference(mPhone.getPhoneId(), FAKE_SUB_ID,
+                TERMINAL_BASED_ACTIVATED, CALL_WAITING_SYNC_NONE);
+        PersistableBundle bundle = getConfigBundle(true, CALL_WAITING_SYNC_NONE, true);
+        doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(anyInt());
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_ACTIVATED);
+
+        mCWC.setTerminalBasedCallWaitingSupported(false);
+        setPreference(mPhone.getPhoneId(), FAKE_SUB_ID,
+                TERMINAL_BASED_NOT_ACTIVATED, CALL_WAITING_SYNC_NONE);
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_ACTIVATED);
+
+        mCWC.setTerminalBasedCallWaitingSupported(false);
+        bundle = getConfigBundle(false, CALL_WAITING_SYNC_NONE, false);
+        doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(anyInt());
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_SUPPORTED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_SUPPORTED);
+    }
+
+    @Test
+    @SmallTest
+    public void testCarrierConfigChanged() {
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+        PersistableBundle bundle = getConfigBundle(true, CALL_WAITING_SYNC_NONE, true);
+        mCWC.updateCarrierConfig(FAKE_SUB_ID, bundle, true);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_ACTIVATED);
+
+        bundle = getConfigBundle(false, CALL_WAITING_SYNC_NONE, true);
+        mCWC.updateCarrierConfig(FAKE_SUB_ID, bundle, false);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_SUPPORTED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_SUPPORTED);
+    }
+
+
+    private static class GetTestHandler extends Handler {
+        public int[] resp;
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case GET_DONE:
+                    AsyncResult ar = (AsyncResult) msg.obj;
+                    resp = (int[]) ar.result;
+                    break;
+                default:
+            }
+        }
+
+        public void reset() {
+            resp = null;
+        }
+    }
+
+    @Test
+    @SmallTest
+    public void testGetCallWaitingSyncNone() {
+        mCWC.setTerminalBasedCallWaitingSupported(false);
+        assertFalse(mCWC.getCallWaiting(null));
+
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+        PersistableBundle bundle = getConfigBundle(true, CALL_WAITING_SYNC_NONE, true);
+        mCWC.updateCarrierConfig(FAKE_SUB_ID, bundle, true);
+
+        mHandler = new GetTestHandler();
+
+        assertTrue(mCWC.setCallWaiting(true, SERVICE_CLASS_VOICE, null));
+        mTestableLooper.processAllMessages();
+
+        assertTrue(mCWC.getCallWaiting(mHandler.obtainMessage(GET_DONE)));
+        mTestableLooper.processAllMessages();
+
+        assertNotNull(mHandler.resp);
+        assertEquals(2, mHandler.resp.length);
+        assertEquals(TERMINAL_BASED_ACTIVATED, mHandler.resp[0]);
+        assertEquals(SERVICE_CLASS_VOICE, mHandler.resp[1]);
+
+        mHandler.reset();
+
+        assertTrue(mCWC.setCallWaiting(false, SERVICE_CLASS_VOICE, null));
+        mTestableLooper.processAllMessages();
+
+        assertTrue(mCWC.getCallWaiting(mHandler.obtainMessage(GET_DONE)));
+        mTestableLooper.processAllMessages();
+
+        assertNotNull(mHandler.resp);
+        assertEquals(2, mHandler.resp.length);
+        assertEquals(TERMINAL_BASED_NOT_ACTIVATED, mHandler.resp[0]);
+        assertEquals(SERVICE_CLASS_NONE, mHandler.resp[1]);
+    }
+
+    @Test
+    @SmallTest
+    public void testSetCallWaitingSyncNone() {
+        mCWC.setTerminalBasedCallWaitingSupported(false);
+        assertFalse(mCWC.setCallWaiting(true, SERVICE_CLASS_VOICE, null));
+
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+        PersistableBundle bundle = getConfigBundle(true, CALL_WAITING_SYNC_NONE, true);
+        mCWC.updateCarrierConfig(FAKE_SUB_ID, bundle, true);
+
+        assertTrue(mCWC.setCallWaiting(true, SERVICE_CLASS_VOICE, null));
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_ACTIVATED);
+
+        assertTrue(mCWC.setCallWaiting(false, SERVICE_CLASS_VOICE | SERVICE_CLASS_DATA, null));
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_NOT_ACTIVATED);
+
+        assertFalse(mCWC.setCallWaiting(true, SERVICE_CLASS_DATA, null));
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_NOT_ACTIVATED);
+
+        assertFalse(mCWC.setCallWaiting(true, SERVICE_CLASS_NONE, null));
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_NOT_ACTIVATED);
+    }
+
+    @Test
+    @SmallTest
+    public void testSyncUserChange() {
+        mCWC.setTerminalBasedCallWaitingSupported(false);
+        setPreference(mPhone.getPhoneId(), FAKE_SUB_ID,
+                TERMINAL_BASED_ACTIVATED, CALL_WAITING_SYNC_USER_CHANGE);
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+        PersistableBundle bundle = getConfigBundle(true, CALL_WAITING_SYNC_USER_CHANGE, true);
+        mCWC.updateCarrierConfig(FAKE_SUB_ID, bundle, true);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_ACTIVATED);
+
+        mHandler = new GetTestHandler();
+
+        mSimulatedCommands.setCallWaiting(false, SERVICE_CLASS_VOICE, null);
+
+        assertTrue(mCWC.getCallWaiting(mHandler.obtainMessage(GET_DONE)));
+        mTestableLooper.processAllMessages();
+
+        assertNotNull(mHandler.resp);
+        assertEquals(2, mHandler.resp.length);
+        assertEquals(TERMINAL_BASED_NOT_ACTIVATED, mHandler.resp[0]);
+        assertEquals(SERVICE_CLASS_NONE, mHandler.resp[1]);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_NOT_ACTIVATED);
+
+        mHandler.reset();
+
+        mSimulatedCommands.setCallWaiting(true, SERVICE_CLASS_VOICE, null);
+
+        assertTrue(mCWC.getCallWaiting(mHandler.obtainMessage(GET_DONE)));
+        mTestableLooper.processAllMessages();
+
+        assertNotNull(mHandler.resp);
+        assertEquals(2, mHandler.resp.length);
+        assertEquals(TERMINAL_BASED_ACTIVATED, mHandler.resp[0]);
+        assertEquals(SERVICE_CLASS_VOICE, mHandler.resp[1]);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_ACTIVATED);
+
+        mHandler.reset();
+
+        assertTrue(mCWC.setCallWaiting(false, SERVICE_CLASS_VOICE, null));
+        mTestableLooper.processAllMessages();
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_NOT_ACTIVATED);
+    }
+
+    @Test
+    @SmallTest
+    public void testSyncFirstPowerUp() {
+        mCWC.setTerminalBasedCallWaitingSupported(false);
+        setPreference(mPhone.getPhoneId(), FAKE_SUB_ID,
+                TERMINAL_BASED_NOT_ACTIVATED, CALL_WAITING_SYNC_FIRST_POWER_UP);
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+        PersistableBundle bundle = getConfigBundle(true, CALL_WAITING_SYNC_FIRST_POWER_UP, true);
+        mCWC.updateCarrierConfig(FAKE_SUB_ID, bundle, true);
+        assertFalse(mCWC.getSyncState());
+
+        mCWC.notifyRegisteredToNetwork();
+        mTestableLooper.processAllMessages();
+
+        assertTrue(mCWC.getSyncState());
+    }
+
+    @Test
+    @SmallTest
+    public void testSyncFirstChange() {
+        mCWC.setTerminalBasedCallWaitingSupported(false);
+        setPreference(mPhone.getPhoneId(), FAKE_SUB_ID,
+                TERMINAL_BASED_NOT_ACTIVATED, CALL_WAITING_SYNC_FIRST_CHANGE);
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+        PersistableBundle bundle = getConfigBundle(true, CALL_WAITING_SYNC_FIRST_CHANGE, true);
+        mCWC.updateCarrierConfig(FAKE_SUB_ID, bundle, true);
+        mCWC.setImsRegistrationState(false);
+
+        assertFalse(mCWC.getSyncState());
+
+        mSimulatedCommands.setCallWaiting(false, SERVICE_CLASS_VOICE, null);
+        mCWC.getCallWaiting(null);
+        mTestableLooper.processAllMessages();
+
+        assertFalse(mCWC.getSyncState());
+
+        mSimulatedCommands.setCallWaiting(true, SERVICE_CLASS_VOICE, null);
+        mCWC.getCallWaiting(null);
+        mTestableLooper.processAllMessages();
+
+        assertTrue(mCWC.getSyncState());
+
+        assertTrue(mCWC.setCallWaiting(true, SERVICE_CLASS_VOICE, null));
+        mTestableLooper.processAllMessages();
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(mSimulatedCommands.mCallWaitActivated);
+
+        assertTrue(mCWC.setCallWaiting(false, SERVICE_CLASS_VOICE, null));
+        mTestableLooper.processAllMessages();
+
+        // Local setting changed, but no change in CS network.
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(mSimulatedCommands.mCallWaitActivated);
+    }
+
+    @Test
+    @SmallTest
+    public void testSyncImsOnly() {
+        mCWC.setTerminalBasedCallWaitingSupported(false);
+        setPreference(mPhone.getPhoneId(), FAKE_SUB_ID,
+                TERMINAL_BASED_ACTIVATED, CALL_WAITING_SYNC_IMS_ONLY);
+        mCWC.setTerminalBasedCallWaitingSupported(true);
+        PersistableBundle bundle = getConfigBundle(true, CALL_WAITING_SYNC_IMS_ONLY, true);
+        mCWC.updateCarrierConfig(FAKE_SUB_ID, bundle, true);
+
+        mSimulatedCommands.setCallWaiting(false, SERVICE_CLASS_VOICE, null);
+
+        // IMS is registered
+        mCWC.setImsRegistrationState(true);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_ACTIVATED);
+
+        mHandler = new GetTestHandler();
+
+        assertTrue(mCWC.getCallWaiting(mHandler.obtainMessage(GET_DONE)));
+        mTestableLooper.processAllMessages();
+
+        // result carries the service state from IMS service
+        assertNotNull(mHandler.resp);
+        assertEquals(2, mHandler.resp.length);
+        assertEquals(TERMINAL_BASED_ACTIVATED, mHandler.resp[0]);
+        assertEquals(SERVICE_CLASS_VOICE, mHandler.resp[1]);
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_ACTIVATED);
+
+        mHandler.reset();
+
+        // IMS is not registered
+        mCWC.setImsRegistrationState(false);
+
+        assertTrue(mCWC.getCallWaiting(mHandler.obtainMessage(GET_DONE)));
+        mTestableLooper.processAllMessages();
+
+        // result carries the service state from CS
+        assertNotNull(mHandler.resp);
+        assertEquals(2, mHandler.resp.length);
+        assertEquals(TERMINAL_BASED_NOT_ACTIVATED, mHandler.resp[0]);
+        assertEquals(SERVICE_CLASS_NONE, mHandler.resp[1]);
+
+        // service state not synchronized between CS and IMS
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_SUPPORTED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_ACTIVATED);
+
+        mHandler.reset();
+
+        // IMS is registered
+        mCWC.setImsRegistrationState(true);
+
+        assertTrue(mCWC.setCallWaiting(false, SERVICE_CLASS_VOICE, null));
+        mTestableLooper.processAllMessages();
+
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_NOT_ACTIVATED);
+
+        // IMS is not registered
+        mCWC.setImsRegistrationState(false);
+
+        assertTrue(mCWC.setCallWaiting(true, SERVICE_CLASS_VOICE, null));
+        mTestableLooper.processAllMessages();
+
+        assertTrue(mCWC.getCallWaiting(mHandler.obtainMessage(GET_DONE)));
+        mTestableLooper.processAllMessages();
+
+        // result carries the service state from CS
+        assertNotNull(mHandler.resp);
+        assertEquals(2, mHandler.resp.length);
+        assertEquals(TERMINAL_BASED_ACTIVATED, mHandler.resp[0]);
+        assertEquals(SERVICE_CLASS_VOICE, mHandler.resp[1]);
+
+        // service state not synchronized between CS and IMS
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(false) == TERMINAL_BASED_NOT_ACTIVATED);
+        assertTrue(mCWC.getTerminalBasedCallWaitingState(true) == TERMINAL_BASED_NOT_SUPPORTED);
+        assertTrue(retrieveStatePreference(mPhone.getSubId()) == TERMINAL_BASED_NOT_ACTIVATED);
+    }
+
+    private PersistableBundle getConfigBundle(boolean provisioned,
+            int preference, boolean defaultState) {
+        PersistableBundle bundle = new PersistableBundle();
+        bundle.putIntArray(KEY_UT_TERMINAL_BASED_SERVICES_INT_ARRAY,
+                provisioned ? new int[] { SUPPLEMENTARY_SERVICE_CW } : new int[] { });
+        bundle.putInt(KEY_TERMINAL_BASED_CALL_WAITING_SYNC_TYPE_INT, preference);
+        bundle.putBoolean(KEY_TERMINAL_BASED_CALL_WAITING_DEFAULT_ENABLED_BOOL, defaultState);
+        return bundle;
+    }
+
+    private int retrieveStatePreference(int subId) {
+        SharedPreferences sp =
+                mContext.getSharedPreferences(PREFERENCE_TBCW, Context.MODE_PRIVATE);
+        return sp.getInt(KEY_STATE + subId, TERMINAL_BASED_NOT_SUPPORTED);
+    }
+
+    private void setPreference(int phoneId, int subId, int state, int syncPreference) {
+        SharedPreferences sp =
+                mContext.getSharedPreferences(PREFERENCE_TBCW, Context.MODE_PRIVATE);
+
+        SharedPreferences.Editor editor = sp.edit();
+        editor.putInt(KEY_SUB_ID + phoneId, subId);
+        editor.putInt(KEY_STATE + subId, state);
+        editor.putInt(KEY_CS_SYNC + phoneId, syncPreference);
+        editor.apply();
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/CarrierPrivilegesTrackerTest.java b/tests/telephonytests/src/com/android/internal/telephony/CarrierPrivilegesTrackerTest.java
index 221b2b5..96c46b3 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/CarrierPrivilegesTrackerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/CarrierPrivilegesTrackerTest.java
@@ -17,8 +17,6 @@
 package com.android.internal.telephony;
 
 import static android.os.UserHandle.SYSTEM;
-import static android.telephony.CarrierConfigManager.EXTRA_SLOT_INDEX;
-import static android.telephony.CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX;
 import static android.telephony.CarrierConfigManager.KEY_CARRIER_CERTIFICATE_STRING_ARRAY;
 import static android.telephony.CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL;
 import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
@@ -76,8 +74,8 @@
 import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
 import org.mockito.InOrder;
-import org.mockito.Mock;
 
 import java.security.MessageDigest;
 import java.util.ArrayList;
@@ -129,11 +127,10 @@
                     | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
                     | PackageManager.MATCH_HIDDEN_UNTIL_INSTALLED_COMPONENTS;
 
-    @Mock private Signature mSignature;
-
     private PersistableBundle mCarrierConfigs;
     private CarrierPrivilegesTrackerTestHandler mHandler;
     private CarrierPrivilegesTracker mCarrierPrivilegesTracker;
+    private CarrierConfigManager.CarrierConfigChangeListener mCarrierConfigChangeListener;
 
     @Before
     public void setUp() throws Exception {
@@ -164,7 +161,8 @@
     private void setupCarrierConfigRules(String... rules) {
         mCarrierConfigs.putStringArray(KEY_CARRIER_CERTIFICATE_STRING_ARRAY, rules);
         mCarrierConfigs.putBoolean(KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
-        when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfigs);
+        when(mCarrierConfigManager.getConfigForSubId(eq(SUB_ID), any()))
+                .thenReturn(mCarrierConfigs);
     }
 
     private static String carrierConfigRuleString(String certificateHash, String... packageNames) {
@@ -222,8 +220,14 @@
      * #setupInstalledPackages}.
      */
     private CarrierPrivilegesTracker createCarrierPrivilegesTracker() throws Exception {
+        // Capture CarrierConfigChangeListener to emulate the carrier config change notification
+        ArgumentCaptor<CarrierConfigManager.CarrierConfigChangeListener> listenerArgumentCaptor =
+                ArgumentCaptor.forClass(CarrierConfigManager.CarrierConfigChangeListener.class);
         CarrierPrivilegesTracker cpt =
                 new CarrierPrivilegesTracker(mTestableLooper.getLooper(), mPhone, mContext);
+        verify(mCarrierConfigManager).registerCarrierConfigChangeListener(any(),
+                listenerArgumentCaptor.capture());
+        mCarrierConfigChangeListener = listenerArgumentCaptor.getAllValues().get(0);
         mTestableLooper.processAllMessages();
 
         cpt.registerCarrierPrivilegesListener(mHandler, REGISTRANT_WHAT, null);
@@ -371,7 +375,7 @@
         verifyCarrierPrivilegesChangedUpdates(List.of());
 
         // Clear UIDs
-        sendCarrierConfigChangedIntent(INVALID_SUBSCRIPTION_ID, PHONE_ID);
+        sendCarrierConfigChanged(INVALID_SUBSCRIPTION_ID, PHONE_ID);
         mTestableLooper.processAllMessages();
 
         verifyCurrentState(Set.of(), new int[0]);
@@ -389,7 +393,7 @@
         setupCarrierConfigRules(
                 carrierConfigRuleString(getHash(CERT_1)), carrierConfigRuleString(getHash(CERT_2)));
 
-        sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID);
+        sendCarrierConfigChanged(SUB_ID, PHONE_ID);
         mTestableLooper.processAllMessages();
 
         verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS);
@@ -403,7 +407,7 @@
         // Start with privileges. Incorrect phoneId shouldn't affect certs
         setupCarrierPrivilegesTrackerWithCarrierConfigUids();
 
-        sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID_INCORRECT);
+        sendCarrierConfigChanged(SUB_ID, PHONE_ID_INCORRECT);
         mTestableLooper.processAllMessages();
 
         verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS);
@@ -417,7 +421,7 @@
         // Start with privileges, verify clearing certs clears UIDs
         setupCarrierPrivilegesTrackerWithCarrierConfigUids();
 
-        sendCarrierConfigChangedIntent(INVALID_SUBSCRIPTION_ID, PHONE_ID);
+        sendCarrierConfigChanged(INVALID_SUBSCRIPTION_ID, PHONE_ID);
         mTestableLooper.processAllMessages();
 
         verifyCurrentState(Set.of(), new int[0]);
@@ -434,9 +438,10 @@
         setupCarrierPrivilegesTrackerWithCarrierConfigUids();
 
         mCarrierConfigs.putBoolean(KEY_CARRIER_CONFIG_APPLIED_BOOL, false);
-        when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfigs);
+        when(mCarrierConfigManager.getConfigForSubId(eq(SUB_ID), any()))
+                .thenReturn(mCarrierConfigs);
 
-        sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID);
+        sendCarrierConfigChanged(SUB_ID, PHONE_ID);
         mTestableLooper.processAllMessages();
 
         verifyCurrentState(Set.of(), new int[0]);
@@ -456,7 +461,7 @@
                 carrierConfigRuleString(getHash(CERT_1), PACKAGE_1),
                 carrierConfigRuleString(getHash(CERT_2), PACKAGE_1));
 
-        sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID);
+        sendCarrierConfigChanged(SUB_ID, PHONE_ID);
         mTestableLooper.processAllMessages();
 
         verifyCurrentState(Set.of(PACKAGE_1), new int[] {UID_1});
@@ -469,7 +474,7 @@
                 carrierConfigRuleString(getHash(CERT_1), PACKAGE_1),
                 carrierConfigRuleString(getHash(CERT_2), PACKAGE_1, PACKAGE_2));
 
-        sendCarrierConfigChangedIntent(SUB_ID, PHONE_ID);
+        sendCarrierConfigChanged(SUB_ID, PHONE_ID);
         mTestableLooper.processAllMessages();
 
         verifyCurrentState(PRIVILEGED_PACKAGES, PRIVILEGED_UIDS);
@@ -1130,11 +1135,9 @@
         verify(mPackageManager).queryIntentServices(any(), anyInt());
     }
 
-    private void sendCarrierConfigChangedIntent(int subId, int phoneId) {
-        mContext.sendBroadcast(
-                new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)
-                        .putExtra(EXTRA_SUBSCRIPTION_INDEX, subId)
-                        .putExtra(EXTRA_SLOT_INDEX, phoneId));
+    private void sendCarrierConfigChanged(int subId, int phoneId) {
+        mCarrierConfigChangeListener.onCarrierConfigChanged(phoneId, subId,
+                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
     }
 
     private void sendSimCardStateChangedIntent(int phoneId, int simState) {
diff --git a/tests/telephonytests/src/com/android/internal/telephony/CellSignalStrengthNrTest.java b/tests/telephonytests/src/com/android/internal/telephony/CellSignalStrengthNrTest.java
index 47f545f..11d57bf 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/CellSignalStrengthNrTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/CellSignalStrengthNrTest.java
@@ -22,7 +22,7 @@
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
-import android.hardware.radio.V1_6.NrSignalStrength;
+import android.hardware.radio.network.NrSignalStrength;
 import android.os.Parcel;
 import android.os.PersistableBundle;
 import android.telephony.CarrierConfigManager;
@@ -55,9 +55,11 @@
     private static final int CSICQI_TABLE_INDEX = 1;
     private static final ArrayList<Byte> CSICQI_REPORT =
             new ArrayList<>(Arrays.asList((byte) 3, (byte) 2, (byte) 1));
+    private static final byte[] CSICQI_REPORT_PRIMITIVE = new byte[] {(byte) 3, (byte) 2, (byte) 1};
     private static final int SSRSRP = -112;
     private static final int SSRSRQ = -13;
     private static final int SSSINR = 32;
+    private static final int TIMING_ADVANCE = 10;
 
     // Mocked classes
     ServiceState mSS;
@@ -83,7 +85,7 @@
     public void testGetMethod() {
         // GIVEN an instance of CellSignalStrengthNr
         CellSignalStrengthNr css = new CellSignalStrengthNr(CSIRSRP, CSIRSRQ, CSISINR,
-                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR);
+                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR, TIMING_ADVANCE);
 
         // THEN the get method should return correct value
         assertThat(css.getCsiRsrp()).isEqualTo(CSIRSRP);
@@ -95,20 +97,22 @@
         assertThat(css.getSsRsrq()).isEqualTo(SSRSRQ);
         assertThat(css.getSsSinr()).isEqualTo(SSSINR);
         assertThat(css.getDbm()).isEqualTo(SSRSRP);
+        assertThat(css.getTimingAdvanceMicros()).isEqualTo(TIMING_ADVANCE);
     }
 
     @Test
     public void testGetMethodWithHal() {
         // GIVEN an instance of NrSignalStrength with some positive values
         NrSignalStrength nrSignalStrength = new NrSignalStrength();
-        nrSignalStrength.base.csiRsrp = -CSIRSRP;
-        nrSignalStrength.base.csiRsrq = -CSIRSRQ;
-        nrSignalStrength.base.csiSinr = CSISINR;
+        nrSignalStrength.csiRsrp = -CSIRSRP;
+        nrSignalStrength.csiRsrq = -CSIRSRQ;
+        nrSignalStrength.csiSinr = CSISINR;
         nrSignalStrength.csiCqiTableIndex = CSICQI_TABLE_INDEX;
-        nrSignalStrength.csiCqiReport = CSICQI_REPORT;
-        nrSignalStrength.base.ssRsrp = -SSRSRP;
-        nrSignalStrength.base.ssRsrq = -SSRSRQ;
-        nrSignalStrength.base.ssSinr = SSSINR;
+        nrSignalStrength.csiCqiReport = CSICQI_REPORT_PRIMITIVE;
+        nrSignalStrength.ssRsrp = -SSRSRP;
+        nrSignalStrength.ssRsrq = -SSRSRQ;
+        nrSignalStrength.ssSinr = SSSINR;
+        nrSignalStrength.timingAdvance = TIMING_ADVANCE;
 
         // THEN the get method should return the correct value
         CellSignalStrengthNr css = RILUtils.convertHalNrSignalStrength(nrSignalStrength);
@@ -121,20 +125,22 @@
         assertThat(css.getSsRsrq()).isEqualTo(SSRSRQ);
         assertThat(css.getSsSinr()).isEqualTo(SSSINR);
         assertThat(css.getDbm()).isEqualTo(SSRSRP);
+        assertThat(css.getTimingAdvanceMicros()).isEqualTo(TIMING_ADVANCE);
     }
 
     @Test
     public void testUnavailableValueWithHal() {
         // GIVEN an instance of NrSignalStrength
         NrSignalStrength nrSignalStrength = new NrSignalStrength();
-        nrSignalStrength.base.csiRsrp = CellInfo.UNAVAILABLE;
-        nrSignalStrength.base.csiRsrq = CellInfo.UNAVAILABLE;
-        nrSignalStrength.base.csiSinr = CellInfo.UNAVAILABLE;
+        nrSignalStrength.csiRsrp = CellInfo.UNAVAILABLE;
+        nrSignalStrength.csiRsrq = CellInfo.UNAVAILABLE;
+        nrSignalStrength.csiSinr = CellInfo.UNAVAILABLE;
         nrSignalStrength.csiCqiTableIndex = CellInfo.UNAVAILABLE;
-        nrSignalStrength.csiCqiReport = new ArrayList<Byte>();
-        nrSignalStrength.base.ssRsrp = CellInfo.UNAVAILABLE;
-        nrSignalStrength.base.ssRsrq = CellInfo.UNAVAILABLE;
-        nrSignalStrength.base.ssSinr = CellInfo.UNAVAILABLE;
+        nrSignalStrength.csiCqiReport = new byte[]{};
+        nrSignalStrength.ssRsrp = CellInfo.UNAVAILABLE;
+        nrSignalStrength.ssRsrq = CellInfo.UNAVAILABLE;
+        nrSignalStrength.ssSinr = CellInfo.UNAVAILABLE;
+        nrSignalStrength.timingAdvance = CellInfo.UNAVAILABLE;
 
         // THEN the get method should return unavailable value
         CellSignalStrengthNr css = RILUtils.convertHalNrSignalStrength(nrSignalStrength);
@@ -147,15 +153,16 @@
         assertThat(css.getSsRsrq()).isEqualTo(CellInfo.UNAVAILABLE);
         assertThat(css.getSsSinr()).isEqualTo(CellInfo.UNAVAILABLE);
         assertThat(css.getDbm()).isEqualTo(CellInfo.UNAVAILABLE);
+        assertThat(css.getTimingAdvanceMicros()).isEqualTo(CellInfo.UNAVAILABLE);
     }
 
     @Test
     public void testEquals_sameParameters() {
         // GIVEN an instance of CellSignalStrengthNr and another object with the same parameters
         CellSignalStrengthNr css = new CellSignalStrengthNr(CSIRSRP, CSIRSRQ, CSISINR,
-                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR);
+                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR, TIMING_ADVANCE);
         CellSignalStrengthNr anotherCss = new CellSignalStrengthNr(CSIRSRP, CSIRSRQ, CSISINR,
-                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR);
+                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR, TIMING_ADVANCE);
 
         // THEN this two objects are equivalent
         assertThat(css).isEqualTo(anotherCss);
@@ -166,10 +173,10 @@
         // GIVEN an instance of CellSignalStrengthNr and another object with some different
         // parameters
         CellSignalStrengthNr css = new CellSignalStrengthNr(CSIRSRP, CSIRSRQ, CSISINR,
-                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR);
+                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR, TIMING_ADVANCE);
         CellSignalStrengthNr anotherCss = new CellSignalStrengthNr(ANOTHER_CSIRSRP,
                 ANOTHER_CSIRSRQ, CSISINR, CSICQI_TABLE_INDEX, CSICQI_REPORT,
-                SSRSRP, SSRSRQ, SSSINR);
+                SSRSRP, SSRSRQ, SSSINR, TIMING_ADVANCE);
 
         // THEN this two objects are different
         assertThat(css).isNotEqualTo(anotherCss);
@@ -179,7 +186,7 @@
     public void testAusLevel_validValue() {
         // GIVEN an instance of CellSignalStrengthNr with valid csirsrp
         CellSignalStrengthNr css = new CellSignalStrengthNr(CSIRSRP, CSIRSRQ, CSISINR,
-                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR);
+                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR, TIMING_ADVANCE);
 
         // THEN the asu level is in range [0, 97]
         assertThat(css.getAsuLevel()).isIn(Range.range(0, BoundType.CLOSED, 97, BoundType.CLOSED));
@@ -189,7 +196,7 @@
     public void testAsuLevel_invalidValue() {
         // GIVEN an instance of CellSignalStrengthNr with invalid csirsrp
         CellSignalStrengthNr css = new CellSignalStrengthNr(INVALID_CSIRSRP, CSIRSRQ, CSISINR,
-                CSICQI_TABLE_INDEX, CSICQI_REPORT, INVALID_SSRSRP, SSRSRQ, SSSINR);
+                CSICQI_TABLE_INDEX, CSICQI_REPORT, INVALID_SSRSRP, SSRSRQ, SSSINR, TIMING_ADVANCE);
 
         // THEN the asu level is unknown
         assertThat(css.getAsuLevel()).isEqualTo(CellSignalStrengthNr.UNKNOWN_ASU_LEVEL);
@@ -200,7 +207,7 @@
         for (int ssRsrp = -156; ssRsrp <= -31; ssRsrp++) {
             // GIVEN an instance of CellSignalStrengthNr with valid csirsrp
             CellSignalStrengthNr css = new CellSignalStrengthNr(CSIRSRP, CSIRSRQ, CSISINR,
-                    CSICQI_TABLE_INDEX, CSICQI_REPORT, ssRsrp, SSRSRQ, SSSINR);
+                    CSICQI_TABLE_INDEX, CSICQI_REPORT, ssRsrp, SSRSRQ, SSSINR, TIMING_ADVANCE);
 
             // THEN the signal level is valid
             assertThat(css.getLevel()).isIn(Range.range(
@@ -213,7 +220,7 @@
     public void testSignalLevel_invalidValue() {
         // GIVEN an instance of CellSignalStrengthNr with invalid csirsrp
         CellSignalStrengthNr css = new CellSignalStrengthNr(INVALID_CSIRSRP, CSIRSRQ, CSISINR,
-                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR);
+                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR, TIMING_ADVANCE);
 
         // THEN the signal level is unknown
         assertThat(css.getLevel()).isEqualTo(CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN);
@@ -223,7 +230,7 @@
     public void testParcel() {
         // GIVEN an instance of CellSignalStrengthNr
         CellSignalStrengthNr css = new CellSignalStrengthNr(CSIRSRP, CSIRSRQ, CSISINR,
-                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR);
+                CSICQI_TABLE_INDEX, CSICQI_REPORT, SSRSRP, SSRSRQ, SSSINR, TIMING_ADVANCE);
 
         // WHEN write the object to parcel and create another object with that parcel
         Parcel parcel = Parcel.obtain();
@@ -241,6 +248,7 @@
         assertThat(anotherCss.getSsRsrp()).isEqualTo(SSRSRP);
         assertThat(anotherCss.getSsRsrq()).isEqualTo(SSRSRQ);
         assertThat(anotherCss.getSsSinr()).isEqualTo(SSSINR);
+        assertThat(anotherCss.getTimingAdvanceMicros()).isEqualTo(TIMING_ADVANCE);
     }
 
     @Test
diff --git a/tests/telephonytests/src/com/android/internal/telephony/ConnectionTest.java b/tests/telephonytests/src/com/android/internal/telephony/ConnectionTest.java
old mode 100755
new mode 100644
index d1e643e..3949161
--- a/tests/telephonytests/src/com/android/internal/telephony/ConnectionTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/ConnectionTest.java
@@ -20,11 +20,14 @@
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 import android.os.Handler;
 import android.os.Looper;
+import com.android.internal.telephony.emergency.EmergencyNumberTracker;
 
 import org.junit.After;
 import org.junit.Before;
@@ -39,6 +42,10 @@
 
     // Mocked classes
     protected Call mCall;
+    protected GsmCdmaPhone mPhone2;  // mPhone as phone 1 is already defined in TelephonyTest.
+    protected EmergencyNumberTracker mEmergencyNumberTracker2;
+    protected Connection.PhoneFactoryProxy mPhoneFactoryProxy;
+    protected Connection mTestConnection;
 
     private class TestConnection extends Connection {
 
@@ -117,7 +124,16 @@
         super.setUp(getClass().getSimpleName());
         mCall = mock(Call.class);
         doReturn(mPhone).when(mCall).getPhone();
+        doReturn(mPhone).when(mCT).getPhone();
         replaceInstance(Handler.class, "mLooper", mCT, Looper.getMainLooper());
+
+        mPhone2 = mock(GsmCdmaPhone.class);
+        mEmergencyNumberTracker2 = mock(EmergencyNumberTracker.class);
+        doReturn(mEmergencyNumberTracker2).when(mPhone2).getEmergencyNumberTracker();
+
+        mTestConnection = new TestConnection(TEST_PHONE_TYPE);
+        mPhoneFactoryProxy = mock(Connection.PhoneFactoryProxy.class);
+        mTestConnection.setPhoneFactoryProxy(mPhoneFactoryProxy);
     }
 
     @After
@@ -156,5 +172,24 @@
         assertTrue(connection.hasKnownUserIntentEmergency());
     }
 
+    @Test
+    public void testSetEmergencyCallInfo() {
+        //Replicate Dual-SIM:
+        Phone [] phones = {mPhone, mPhone2};
+        when(mPhoneFactoryProxy.getPhones()).thenReturn(phones);
+        doReturn(1).when(mPhone).getPhoneId();
+        doReturn(2).when(mPhone2).getPhoneId();
+
+        //Replicate behavior when a number is an emergency number
+        // on the secondary SIM but not on the default SIM:
+        when(mPhone.getEmergencyNumberTracker().getEmergencyNumber(any())).thenReturn(null);
+        when(mEmergencyNumberTracker2.getEmergencyNumber(any()))
+                .thenReturn(getTestEmergencyNumber());
+
+        //Ensure the connection is considered as an emergency call:
+        mTestConnection.setEmergencyCallInfo(mCT);
+        assertTrue(mTestConnection.isEmergencyCall());
+    }
+
     // TODO Verify more methods in Connection
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java b/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java
index b8c10a5..23fc448 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java
@@ -176,6 +176,11 @@
                     mKeyValuePairs.put(request, (String)args.get("value"));
                     mNumKeyValuePairs++;
                     break;
+                case Settings.CALL_METHOD_PUT_CONFIG:
+                    logd("PUT_config called");
+                    logd("adding config flag: " + request + "-" + args.getString("value"));
+                    mFlags.put(request, args.getString("value"));
+                    break;
                 case Settings.CALL_METHOD_LIST_CONFIG:
                     logd("LIST_config: " + mFlags);
                     Bundle result = new Bundle();
@@ -349,6 +354,8 @@
                 return Context.NETWORK_POLICY_SERVICE;
             } else if (serviceClass == PowerManager.class) {
                 return Context.POWER_SERVICE;
+            } else if (serviceClass == EuiccManager.class) {
+                return Context.EUICC_SERVICE;
             }
             return super.getSystemServiceName(serviceClass);
         }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/DataSpecificRegistrationInfoTest.java b/tests/telephonytests/src/com/android/internal/telephony/DataSpecificRegistrationInfoTest.java
new file mode 100644
index 0000000..50390cc
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/DataSpecificRegistrationInfoTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNull;
+
+import android.os.Parcel;
+import android.telephony.DataSpecificRegistrationInfo;
+import android.telephony.LteVopsSupportInfo;
+
+import androidx.test.filters.SmallTest;
+
+import org.junit.Test;
+
+/** Unit tests for {@link DataSpecificRegistrationInfo}. */
+public class DataSpecificRegistrationInfoTest {
+    @Test
+    @SmallTest
+    public void testBuilder() {
+        DataSpecificRegistrationInfo dsri =
+                new DataSpecificRegistrationInfo.Builder(3)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .build();
+
+        assertEquals(3, dsri.maxDataCalls);
+        assertEquals(false, dsri.isDcNrRestricted);
+        assertEquals(true, dsri.isNrAvailable);
+        assertEquals(true, dsri.isEnDcAvailable);
+        assertNull(dsri.getVopsSupportInfo());
+
+        LteVopsSupportInfo vopsInfo = new LteVopsSupportInfo(
+                LteVopsSupportInfo.LTE_STATUS_SUPPORTED, LteVopsSupportInfo.LTE_STATUS_SUPPORTED);
+        dsri = new DataSpecificRegistrationInfo.Builder(5)
+                .setDcNrRestricted(true)
+                .setVopsSupportInfo(vopsInfo)
+                .build();
+
+        assertEquals(5, dsri.maxDataCalls);
+        assertEquals(true, dsri.isDcNrRestricted);
+        assertEquals(false, dsri.isNrAvailable);
+        assertEquals(false, dsri.isEnDcAvailable);
+        assertEquals(vopsInfo, dsri.getVopsSupportInfo());
+    }
+
+    @Test
+    @SmallTest
+    public void testParcel() {
+        DataSpecificRegistrationInfo dsri =
+                new DataSpecificRegistrationInfo.Builder(3)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setLteAttachResultType(DataSpecificRegistrationInfo.LTE_ATTACH_TYPE_COMBINED)
+                .setLteAttachExtraInfo(DataSpecificRegistrationInfo.LTE_ATTACH_EXTRA_INFO_SMS_ONLY)
+                .build();
+
+        Parcel p = Parcel.obtain();
+        dsri.writeToParcel(p, 0);
+        p.setDataPosition(0);
+
+        DataSpecificRegistrationInfo newDsri =
+                DataSpecificRegistrationInfo.CREATOR.createFromParcel(p);
+        assertEquals(dsri, newDsri);
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/DefaultPhoneNotifierTest.java b/tests/telephonytests/src/com/android/internal/telephony/DefaultPhoneNotifierTest.java
index 6a05d4f..2f4182a 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/DefaultPhoneNotifierTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/DefaultPhoneNotifierTest.java
@@ -16,6 +16,7 @@
 package com.android.internal.telephony;
 
 import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.anyInt;
 import static org.mockito.Mockito.doReturn;
@@ -30,8 +31,11 @@
 import android.telephony.PreciseDisconnectCause;
 import android.telephony.SignalStrength;
 import android.telephony.TelephonyManager;
+import android.telephony.ims.ImsCallProfile;
 import android.test.suitebuilder.annotation.SmallTest;
 
+import com.android.internal.telephony.imsphone.ImsPhoneCall;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -53,6 +57,9 @@
     GsmCdmaCall mForeGroundCall;
     GsmCdmaCall mBackGroundCall;
     GsmCdmaCall mRingingCall;
+    ImsPhoneCall mImsForeGroundCall;
+    ImsPhoneCall mImsBackGroundCall;
+    ImsPhoneCall mImsRingingCall;
 
     @Before
     public void setUp() throws Exception {
@@ -62,6 +69,9 @@
         mForeGroundCall = mock(GsmCdmaCall.class);
         mBackGroundCall = mock(GsmCdmaCall.class);
         mRingingCall = mock(GsmCdmaCall.class);
+        mImsForeGroundCall = mock(ImsPhoneCall.class);
+        mImsBackGroundCall = mock(ImsPhoneCall.class);
+        mImsRingingCall = mock(ImsPhoneCall.class);
         mDefaultPhoneNotifierUT = new DefaultPhoneNotifier(mContext);
     }
 
@@ -168,61 +178,144 @@
 
     @Test @SmallTest
     public void testNotifyPreciseCallState() throws Exception {
-
         //mock forground/background/ringing call and call state
         doReturn(Call.State.IDLE).when(mForeGroundCall).getState();
         doReturn(Call.State.IDLE).when(mBackGroundCall).getState();
         doReturn(Call.State.IDLE).when(mRingingCall).getState();
 
-        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone);
+        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone, null, null, null);
         verify(mTelephonyRegistryManager, times(0)).notifyPreciseCallState(
-                anyInt(), anyInt(), anyInt(), anyInt(), anyInt());
+                anyInt(), anyInt(), any(), any(), any(), any());
 
         doReturn(mForeGroundCall).when(mPhone).getForegroundCall();
-        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone);
+        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone, null, null, null);
         verify(mTelephonyRegistryManager, times(0)).notifyPreciseCallState(
-                anyInt(), anyInt(), anyInt(), anyInt(), anyInt());
+                anyInt(), anyInt(), any(), any(), any(), any());
 
         doReturn(mBackGroundCall).when(mPhone).getBackgroundCall();
-        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone);
+        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone, null, null, null);
         verify(mTelephonyRegistryManager, times(0)).notifyPreciseCallState(
-                anyInt(), anyInt(), anyInt(), anyInt(), anyInt());
+                anyInt(), anyInt(), any(), any(), any(), any());
 
         doReturn(mRingingCall).when(mPhone).getRingingCall();
-        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone);
-        verify(mTelephonyRegistryManager, times(1)).notifyPreciseCallState(
-                mPhone.getPhoneId(),
-                mPhone.getSubId(),
-                PreciseCallState.PRECISE_CALL_STATE_IDLE,
-                PreciseCallState.PRECISE_CALL_STATE_IDLE,
-                PreciseCallState.PRECISE_CALL_STATE_IDLE);
+        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone, null, null, null);
+        ArgumentCaptor<int[]> captor = ArgumentCaptor.forClass(int[].class);
+        int phoneId = mPhone.getPhoneId();
+        int subId = mPhone.getSubId();
+        verify(mTelephonyRegistryManager).notifyPreciseCallState(
+                eq(phoneId), eq(subId), captor.capture(), eq(null), eq(null), eq(null));
+        final int[] callStates = captor.getValue();
+        assertEquals(3, callStates.length);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_IDLE,
+                callStates[/*ringing call*/ 0]);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_IDLE,
+                callStates[/*foreground call*/ 1]);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_IDLE,
+                callStates[/*background call*/ 2]);
 
         doReturn(Call.State.ACTIVE).when(mForeGroundCall).getState();
-        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone);
-        verify(mTelephonyRegistryManager, times(1)).notifyPreciseCallState(
-                mPhone.getPhoneId(),
-                mPhone.getSubId(),
-                PreciseCallState.PRECISE_CALL_STATE_IDLE,
-                PreciseCallState.PRECISE_CALL_STATE_ACTIVE,
-                PreciseCallState.PRECISE_CALL_STATE_IDLE);
+        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone, null, null, null);
+        ArgumentCaptor<int[]> captor1 = ArgumentCaptor.forClass(int[].class);
+        phoneId = mPhone.getPhoneId();
+        subId = mPhone.getSubId();
+        verify(mTelephonyRegistryManager, times(2)).notifyPreciseCallState(
+                eq(phoneId), eq(subId), captor1.capture(), eq(null), eq(null), eq(null));
+        final int[] callStates1 = captor1.getValue();
+        assertEquals(3, callStates1.length);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_IDLE,
+                callStates1[/*ringing call*/ 0]);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_ACTIVE,
+                callStates1[/*foreground call*/ 1]);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_IDLE,
+                callStates1[/*background call*/ 2]);
 
         doReturn(Call.State.HOLDING).when(mBackGroundCall).getState();
-        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone);
-        verify(mTelephonyRegistryManager, times(1)).notifyPreciseCallState(
-                mPhone.getPhoneId(),
-                mPhone.getSubId(),
-                PreciseCallState.PRECISE_CALL_STATE_IDLE,
-                PreciseCallState.PRECISE_CALL_STATE_ACTIVE,
-                PreciseCallState.PRECISE_CALL_STATE_HOLDING);
+        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone, null, null, null);
+        ArgumentCaptor<int[]> captor2 = ArgumentCaptor.forClass(int[].class);
+        verify(mTelephonyRegistryManager, times(3)).notifyPreciseCallState(
+                eq(phoneId), eq(subId), captor2.capture(), eq(null), eq(null), eq(null));
+        final int[] callStates2 = captor2.getValue();
+        assertEquals(3, callStates2.length);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_IDLE,
+                callStates2[/*ringing call*/ 0]);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_ACTIVE,
+                callStates2[/*foreground call*/ 1]);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_HOLDING,
+                callStates2[/*background call*/ 2]);
 
         doReturn(Call.State.ALERTING).when(mRingingCall).getState();
-        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone);
+        mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone, null, null, null);
+        ArgumentCaptor<int[]> captor3 = ArgumentCaptor.forClass(int[].class);
+        verify(mTelephonyRegistryManager, times(4)).notifyPreciseCallState(
+                eq(phoneId), eq(subId), captor3.capture(), eq(null), eq(null), eq(null));
+        final int[] callStates3 = captor3.getValue();
+        assertEquals(3, callStates3.length);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_ALERTING,
+                callStates3[/*ringing call*/ 0]);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_ACTIVE,
+                callStates3[/*foreground call*/ 1]);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_HOLDING,
+                callStates3[/*background call*/ 2]);
+    }
+
+    @Test
+    public void testNotifyPreciseCallStateImsCallInfo() throws Exception {
+        //mock forground/background/ringing call and call state
+        doReturn(Call.State.ACTIVE).when(mImsForeGroundCall).getState();
+        doReturn(Call.State.HOLDING).when(mImsBackGroundCall).getState();
+        doReturn(Call.State.IDLE).when(mImsRingingCall).getState();
+
+        doReturn(mImsForeGroundCall).when(mImsPhone).getForegroundCall();
+        doReturn(mImsBackGroundCall).when(mImsPhone).getBackgroundCall();
+        doReturn(mImsRingingCall).when(mImsPhone).getRingingCall();
+
+        String[] imsCallIds = {null, "1", "2"};
+        int[] imsCallServiceTypes = {ImsCallProfile.SERVICE_TYPE_NONE,
+                ImsCallProfile.SERVICE_TYPE_NORMAL, ImsCallProfile.SERVICE_TYPE_NORMAL};
+        int[] imsCallTypes = {ImsCallProfile.CALL_TYPE_NONE,
+                ImsCallProfile.CALL_TYPE_VOICE, ImsCallProfile.CALL_TYPE_VT};
+
+        mDefaultPhoneNotifierUT
+                .notifyPreciseCallState(mImsPhone, imsCallIds, imsCallServiceTypes, imsCallTypes);
+        ArgumentCaptor<int[]> callStateCaptor = ArgumentCaptor.forClass(int[].class);
+        ArgumentCaptor<String[]> callIdCaptor = ArgumentCaptor.forClass(String[].class);
+        ArgumentCaptor<int[]> callServiceTypeCaptor = ArgumentCaptor.forClass(int[].class);
+        ArgumentCaptor<int[]> callTypeCaptor = ArgumentCaptor.forClass(int[].class);
+        int phoneId = mImsPhone.getPhoneId();
+        int subId = mImsPhone.getSubId();
         verify(mTelephonyRegistryManager, times(1)).notifyPreciseCallState(
-                mPhone.getPhoneId(),
-                mPhone.getSubId(),
-                PreciseCallState.PRECISE_CALL_STATE_ALERTING,
-                PreciseCallState.PRECISE_CALL_STATE_ACTIVE,
-                PreciseCallState.PRECISE_CALL_STATE_HOLDING);
+                eq(phoneId), eq(subId), callStateCaptor.capture(), callIdCaptor.capture(),
+                callServiceTypeCaptor.capture(), callTypeCaptor.capture());
+        final int[] callStates = callStateCaptor.getValue();
+        final String[] callIds = callIdCaptor.getValue();
+        final int[] callServiceTypes = callServiceTypeCaptor.getValue();
+        final int[] callTypes = callTypeCaptor.getValue();
+        assertEquals(3, callStates.length);
+        assertEquals(3, callIds.length);
+        assertEquals(3, callServiceTypes.length);
+        assertEquals(3, callServiceTypes.length);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_IDLE,
+                callStates[/*ringing call*/ 0]);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_ACTIVE,
+                callStates[/*foreground call*/ 1]);
+        assertEquals(PreciseCallState.PRECISE_CALL_STATE_HOLDING,
+                callStates[/*background call*/ 2]);
+
+        assertEquals("1", callIds[/*foreground call*/ 1]);
+        assertEquals("2", callIds[/*background call*/ 2]);
+        assertEquals(null, callIds[/*ringing call*/ 0]);
+        assertEquals(ImsCallProfile.SERVICE_TYPE_NORMAL,
+                callServiceTypes[/*foreground call*/ 1]);
+        assertEquals(ImsCallProfile.SERVICE_TYPE_NORMAL,
+                callServiceTypes[/*background call*/ 2]);
+        assertEquals(ImsCallProfile.SERVICE_TYPE_NONE,
+                callServiceTypes[/*ringing call*/ 0]);
+        assertEquals(ImsCallProfile.CALL_TYPE_VOICE,
+                callTypes[/*foreground call*/ 1]);
+        assertEquals(ImsCallProfile.CALL_TYPE_VT,
+                callTypes[/*background call*/ 2]);
+        assertEquals(ImsCallProfile.SERVICE_TYPE_NONE,
+                callServiceTypes[/*ringing call*/ 0]);
     }
 
     @Test @SmallTest
diff --git a/tests/telephonytests/src/com/android/internal/telephony/DisplayInfoControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/DisplayInfoControllerTest.java
new file mode 100644
index 0000000..fa00362
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/DisplayInfoControllerTest.java
@@ -0,0 +1,280 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.AsyncResult;
+import android.os.HandlerThread;
+import android.os.PersistableBundle;
+import android.telephony.AccessNetworkConstants;
+import android.telephony.CarrierConfigManager;
+import android.telephony.CellIdentity;
+import android.telephony.CellIdentityCdma;
+import android.telephony.CellIdentityLte;
+import android.telephony.LteVopsSupportInfo;
+import android.telephony.NetworkRegistrationInfo;
+import android.telephony.ServiceState;
+import android.telephony.TelephonyDisplayInfo;
+import android.telephony.TelephonyManager;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+import android.text.TextUtils;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+
+import java.util.Collections;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class DisplayInfoControllerTest extends TelephonyTest {
+
+    private static final int PHONE_ID = 0;
+    private static final String MCC = "600";
+    private static final String MNC = "01";
+    private static final String NUMERIC = MCC + MNC;
+    private static final String NETWORK = "TestNet";
+
+    private DisplayInfoController mDic;
+    private ServiceStateTracker mSst;
+    private ServiceStateTrackerTestHandler mSstHandler;
+    private SignalStrengthController mSsc;
+    private PersistableBundle mBundle;
+
+    private class ServiceStateTrackerTestHandler extends HandlerThread {
+        private ServiceStateTrackerTestHandler(String name) {
+            super(name);
+        }
+
+        @Override
+        public void onLooperPrepared() {
+            mSsc = new SignalStrengthController(mPhone);
+            doReturn(mSsc).when(mPhone).getSignalStrengthController();
+            doReturn(new ServiceState()).when(mPhone).getServiceState();
+            doReturn(NUMERIC).when(mTelephonyManager).getSimOperatorNumericForPhone(eq(PHONE_ID));
+            doReturn(NETWORK).when(mTelephonyManager).getSimOperatorNameForPhone(eq(PHONE_ID));
+
+            mSst = new ServiceStateTracker(mPhone, mSimulatedCommands);
+            doReturn(mSst).when(mPhone).getServiceStateTracker();
+            setReady(true);
+        }
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        logd("DisplayInfoControllerTest setup!");
+        super.setUp(getClass().getSimpleName());
+
+        mSstHandler = new ServiceStateTrackerTestHandler(getClass().getSimpleName());
+        mSstHandler.start();
+        waitUntilReady();
+        waitForLastHandlerAction(mSstHandler.getThreadHandler());
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mSst.removeCallbacksAndMessages(null);
+        mSst = null;
+        mSstHandler.quit();
+        mSstHandler.join();
+        mSstHandler = null;
+        mBundle = null;
+        super.tearDown();
+    }
+
+    private void sendCarrierConfigUpdate() {
+        CarrierConfigManager mockConfigManager = Mockito.mock(CarrierConfigManager.class);
+        when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
+                .thenReturn(mockConfigManager);
+        when(mockConfigManager.getConfigForSubId(anyInt())).thenReturn(mBundle);
+
+        Intent intent = new Intent().setAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
+        intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, PHONE_ID);
+        mContext.sendBroadcast(intent);
+        waitForLastHandlerAction(mSstHandler.getThreadHandler());
+    }
+
+    private static String getPlmnFromCellIdentity(final CellIdentity ci) {
+        if (ci == null || ci instanceof CellIdentityCdma) return "";
+
+        final String mcc = ci.getMccString();
+        final String mnc = ci.getMncString();
+
+        if (TextUtils.isEmpty(mcc) || TextUtils.isEmpty(mnc)) return "";
+
+        return mcc + mnc;
+    }
+
+    private void changeRegState(int state) {
+        int voiceRat = TelephonyManager.NETWORK_TYPE_LTE;
+        int dataRat = TelephonyManager.NETWORK_TYPE_LTE;
+        CellIdentityLte cid =
+                new CellIdentityLte(1, 1, 5, 1, new int[] {1, 2}, 5000,
+                        MCC, MNC, NETWORK, NETWORK, Collections.emptyList(), null);
+        LteVopsSupportInfo lteVopsSupportInfo =
+                new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED);
+        waitForLastHandlerAction(mSstHandler.getThreadHandler());
+        NetworkRegistrationInfo dataResult = new NetworkRegistrationInfo(
+                NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
+                state, dataRat, 0, false, null, cid, getPlmnFromCellIdentity(cid), 1, false, false,
+                false, lteVopsSupportInfo);
+        mSst.mPollingContext[0] = 3;
+        final String[] oldOpNamesResult = new String[] {"test", "test", NUMERIC};
+        mSst.sendMessage(
+                mSst.obtainMessage(
+                        ServiceStateTracker.EVENT_POLL_STATE_OPERATOR,
+                        new AsyncResult(mSst.mPollingContext, oldOpNamesResult, null)));
+        waitForLastHandlerAction(mSstHandler.getThreadHandler());
+        // update data reg state to be in service
+        mSst.sendMessage(
+                mSst.obtainMessage(
+                        ServiceStateTracker.EVENT_POLL_STATE_PS_CELLULAR_REGISTRATION,
+                        new AsyncResult(mSst.mPollingContext, dataResult, null)));
+        waitForLastHandlerAction(mSstHandler.getThreadHandler());
+        NetworkRegistrationInfo voiceResult = new NetworkRegistrationInfo(
+                NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
+                state, voiceRat, 0, false, null, cid, getPlmnFromCellIdentity(cid), false, 0, 0, 0);
+        mSst.sendMessage(
+                mSst.obtainMessage(
+                        ServiceStateTracker.EVENT_POLL_STATE_CS_CELLULAR_REGISTRATION,
+                        new AsyncResult(mSst.mPollingContext, voiceResult, null)));
+        waitForLastHandlerAction(mSstHandler.getThreadHandler());
+    }
+
+    @Test
+    public void testIsRoamingOverride_NonRoamingOperator() {
+        doReturn(true).when(mPhone).isPhoneTypeGsm();
+
+        mBundle = mContextFixture.getCarrierConfigBundle();
+        mBundle.putStringArray(
+                CarrierConfigManager.KEY_NON_ROAMING_OPERATOR_STRING_ARRAY, new String[] {NUMERIC});
+        sendCarrierConfigUpdate();
+
+        changeRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING);
+        ServiceState ss = mSst.getServiceState();
+
+        assertFalse(ss.getRoaming()); // home
+
+        doReturn(mSst).when(mPhone).getServiceStateTracker();
+        mDic = new DisplayInfoController(mPhone);
+        mDic.updateTelephonyDisplayInfo();
+        TelephonyDisplayInfo tdi = mDic.getTelephonyDisplayInfo();
+
+        assertFalse(tdi.isRoaming());
+    }
+
+    @Test
+    public void testIsRoamingOverride_ForceHomeNetwork() {
+        doReturn(true).when(mPhone).isPhoneTypeGsm();
+
+        mBundle = mContextFixture.getCarrierConfigBundle();
+        mBundle.putBoolean(CarrierConfigManager.KEY_FORCE_HOME_NETWORK_BOOL, true);
+        sendCarrierConfigUpdate();
+
+        changeRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING);
+        ServiceState ss = mSst.getServiceState();
+
+        assertFalse(ss.getRoaming()); // home
+
+        doReturn(mSst).when(mPhone).getServiceStateTracker();
+        mDic = new DisplayInfoController(mPhone);
+        mDic.updateTelephonyDisplayInfo();
+        TelephonyDisplayInfo tdi = mDic.getTelephonyDisplayInfo();
+
+        assertFalse(tdi.isRoaming());
+    }
+
+    @Test
+    public void testIsRoamingOverride_RoamingOperator() {
+        doReturn(true).when(mPhone).isPhoneTypeGsm();
+
+        mBundle = mContextFixture.getCarrierConfigBundle();
+        mBundle.putStringArray(
+                CarrierConfigManager.KEY_ROAMING_OPERATOR_STRING_ARRAY, new String[] {"60101"});
+        sendCarrierConfigUpdate();
+
+        changeRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING);
+        ServiceState ss1 = mSst.getServiceState();
+
+        assertTrue(ss1.getRoaming()); // roam
+
+        doReturn(mSst).when(mPhone).getServiceStateTracker();
+        mDic = new DisplayInfoController(mPhone);
+        mDic.updateTelephonyDisplayInfo();
+        TelephonyDisplayInfo tdi = mDic.getTelephonyDisplayInfo();
+
+        assertTrue(tdi.isRoaming());
+    }
+
+    @Test
+    public void testIsRoamingOverride_NonRoamingGsmOperator() {
+        doReturn(true).when(mPhone).isPhoneTypeGsm();
+
+        mBundle = mContextFixture.getCarrierConfigBundle();
+        mBundle.putStringArray(
+                CarrierConfigManager.KEY_GSM_NONROAMING_NETWORKS_STRING_ARRAY,
+                new String[] {NUMERIC});
+        sendCarrierConfigUpdate();
+
+        changeRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING);
+        ServiceState ss = mSst.getServiceState();
+
+        assertFalse(ss.getRoaming()); // home
+
+        doReturn(mSst).when(mPhone).getServiceStateTracker();
+        mDic = new DisplayInfoController(mPhone);
+        mDic.updateTelephonyDisplayInfo();
+        TelephonyDisplayInfo tdi = mDic.getTelephonyDisplayInfo();
+
+        assertFalse(tdi.isRoaming());
+    }
+
+    @Test
+    public void testIsRoamingOverride_RoamingGsmOperator() {
+        doReturn(true).when(mPhone).isPhoneTypeGsm();
+
+        mBundle = mContextFixture.getCarrierConfigBundle();
+        mBundle.putStringArray(
+                CarrierConfigManager.KEY_GSM_ROAMING_NETWORKS_STRING_ARRAY, new String[] {NUMERIC});
+        sendCarrierConfigUpdate();
+
+        changeRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
+        ServiceState ss1 = mSst.getServiceState();
+
+        assertTrue(ss1.getRoaming()); // roam
+
+        doReturn(mSst).when(mPhone).getServiceStateTracker();
+        mDic = new DisplayInfoController(mPhone);
+        mDic.updateTelephonyDisplayInfo();
+        TelephonyDisplayInfo tdi = mDic.getTelephonyDisplayInfo();
+
+        assertTrue(tdi.isRoaming());
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/FakeTelephonyProvider.java b/tests/telephonytests/src/com/android/internal/telephony/FakeTelephonyProvider.java
index 782f78e..3be8509 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/FakeTelephonyProvider.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/FakeTelephonyProvider.java
@@ -24,6 +24,7 @@
 import android.database.sqlite.SQLiteOpenHelper;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.UserHandle;
 import android.provider.BaseColumns;
 import android.provider.Telephony;
 import android.telephony.SubscriptionManager;
@@ -123,6 +124,10 @@
                     + Telephony.SimInfo.COLUMN_PORT_INDEX + " INTEGER DEFAULT -1,"
                     + Telephony.SimInfo.COLUMN_USAGE_SETTING + " INTEGER DEFAULT "
                             + SubscriptionManager.USAGE_SETTING_UNKNOWN
+                    + "," + Telephony.SimInfo.COLUMN_TP_MESSAGE_REF
+                    + "  INTEGER DEFAULT -1,"
+                    + Telephony.SimInfo.COLUMN_USER_HANDLE + " INTEGER DEFAULT "
+                    + UserHandle.USER_NULL
                     + ");";
         }
 
@@ -143,6 +148,7 @@
 
     @Override
     public Uri insert(Uri uri, ContentValues values) {
+        Log.d(TAG, "insert. values=" + values);
         SQLiteDatabase db = mDbHelper.getWritableDatabase();
         long id = db.insert("siminfo", null, values);
         return ContentUris.withAppendedId(Telephony.SimInfo.CONTENT_URI, id);
diff --git a/tests/telephonytests/src/com/android/internal/telephony/FdnUtilsTest.java b/tests/telephonytests/src/com/android/internal/telephony/FdnUtilsTest.java
new file mode 100644
index 0000000..2c48158
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/FdnUtilsTest.java
@@ -0,0 +1,166 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import com.android.internal.telephony.uicc.AdnRecord;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+
+public class FdnUtilsTest {
+
+    private ArrayList<AdnRecord> initializeFdnList() {
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        AdnRecord adnRecord = new AdnRecord(null, null);
+        // By default, every sim card holds 15 empty FDN records
+        int fdnListSize = 15;
+        for (int i = 0; i < fdnListSize; i++) {
+            fdnList.add(adnRecord);
+        }
+        return fdnList;
+    }
+
+    @Test
+    public void fdnListIsNull_returnsFalse() {
+        assertFalse(FdnUtils.isFDN( "123456789", "US", null));
+    }
+
+    @Test
+    public void fdnListIsEmpty_returnsFalse() {
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        assertFalse(FdnUtils.isFDN( "123456789", "US", fdnList));
+    }
+
+    @Test
+    public void fdnListHasOnlyDefaultRecords_returnsFalse() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+
+        assertFalse(FdnUtils.isFDN( "123456789", "US", fdnList));
+    }
+
+    @Test
+    public void fdnListHasRecordWithEmptyNumberStr_returnsFalse() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "");
+        fdnList.add(1, adnRecord);
+
+        assertFalse(FdnUtils.isFDN( "123456789", "US", fdnList));
+    }
+
+    @Test
+    public void dialStrInFdnList_returnsTrue() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "123456789");
+        fdnList.add(2, adnRecord);
+
+        assertTrue(FdnUtils.isFDN( "123456789", "US", fdnList));
+    }
+
+    @Test
+    public void dialStrNotInFdnList_returnsFalse() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "111111111");
+        fdnList.add(3, adnRecord);
+
+        assertFalse(FdnUtils.isFDN("123456788", "US", fdnList));
+    }
+
+    @Test
+    public void dialStrIsNull_returnsFalse() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "111111111");
+        fdnList.add(4, adnRecord);
+
+        assertFalse(FdnUtils.isFDN( null, "US", fdnList));
+    }
+
+    @Test
+    public void fdnEntryFirstSubStringOfDialStr_returnsTrue() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "123");
+        fdnList.add(5, adnRecord);
+
+        assertTrue(FdnUtils.isFDN( "12345", "US", fdnList));
+    }
+
+    @Test
+    public void fdnEntrySubStringOfDialStr_returnsFalse() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "123");
+        fdnList.add(5, adnRecord);
+
+        assertFalse(FdnUtils.isFDN("612345", "US", fdnList));
+    }
+
+    @Test
+    public void dialStrFirstSubStringOfFdnEntry_returnsFalse() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "12345");
+        fdnList.add(5, adnRecord);
+
+        assertFalse(FdnUtils.isFDN("123", "US", fdnList));
+    }
+
+    @Test
+    public void dialStrSubStringOfFdnEntry_returnsFalse() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "612345");
+        fdnList.add(5, adnRecord);
+
+        assertFalse(FdnUtils.isFDN("123", "US", fdnList));
+    }
+
+    @Test
+    public void dialStrWithoutCountryCode_returnsTrue() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "+16502910000");
+        fdnList.add(6, adnRecord);
+
+        assertTrue(FdnUtils.isFDN( "6502910000", "US", fdnList));
+    }
+
+    @Test
+    public void dialStrWithCountryCode_returnsTrue() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "6502910000");
+        fdnList.add(6, adnRecord);
+
+        assertTrue(FdnUtils.isFDN("+16502910000", "US", fdnList));
+    }
+
+    @Test
+    public void defaultCountryIsoIsEmpty_returnsTrue() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "650");
+        fdnList.add(6, adnRecord);
+
+        assertTrue(FdnUtils.isFDN("+16502910000", "", fdnList));
+    }
+
+    @Test
+    public void defaultCountryIsoIsEmpty_returnsFalse() {
+        ArrayList<AdnRecord> fdnList = initializeFdnList();
+        AdnRecord adnRecord = new AdnRecord(null, "+1650");
+        fdnList.add(6, adnRecord);
+
+        assertFalse(FdnUtils.isFDN("6502910000", "", fdnList));
+    }
+}
\ No newline at end of file
diff --git a/tests/telephonytests/src/com/android/internal/telephony/GsmCdmaPhoneTest.java b/tests/telephonytests/src/com/android/internal/telephony/GsmCdmaPhoneTest.java
index 9e45cc3..903d82c 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/GsmCdmaPhoneTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/GsmCdmaPhoneTest.java
@@ -16,17 +16,24 @@
 
 package com.android.internal.telephony;
 
+import static com.android.internal.telephony.CellBroadcastConfigTracker.mergeRangesAsNeeded;
 import static com.android.internal.telephony.CommandsInterface.CF_ACTION_ENABLE;
 import static com.android.internal.telephony.CommandsInterface.CF_REASON_UNCONDITIONAL;
 import static com.android.internal.telephony.Phone.EVENT_ICC_CHANGED;
+import static com.android.internal.telephony.Phone.EVENT_IMS_DEREGISTRATION_TRIGGERED;
+import static com.android.internal.telephony.Phone.EVENT_RADIO_AVAILABLE;
+import static com.android.internal.telephony.Phone.EVENT_SET_NULL_CIPHER_AND_INTEGRITY_DONE;
 import static com.android.internal.telephony.Phone.EVENT_SRVCC_STATE_CHANGED;
 import static com.android.internal.telephony.Phone.EVENT_UICC_APPS_ENABLEMENT_STATUS_CHANGED;
 import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
+import static com.android.internal.telephony.test.SimulatedCommands.FAKE_IMEI;
+import static com.android.internal.telephony.test.SimulatedCommands.FAKE_IMEISV;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.ArgumentMatchers.any;
@@ -49,36 +56,48 @@
 
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.hardware.radio.modem.ImeiInfo;
 import android.os.AsyncResult;
+import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
 import android.os.PersistableBundle;
 import android.os.Process;
 import android.os.WorkSource;
 import android.preference.PreferenceManager;
+import android.provider.DeviceConfig;
 import android.telecom.VideoProfile;
 import android.telephony.AccessNetworkConstants;
 import android.telephony.CarrierConfigManager;
+import android.telephony.CellBroadcastIdRange;
 import android.telephony.CellIdentity;
 import android.telephony.CellIdentityCdma;
 import android.telephony.CellIdentityGsm;
 import android.telephony.LinkCapacityEstimate;
 import android.telephony.NetworkRegistrationInfo;
 import android.telephony.ServiceState;
+import android.telephony.SmsCbMessage;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
+import android.telephony.ims.stub.ImsRegistrationImplBase;
 import android.test.suitebuilder.annotation.SmallTest;
 import android.testing.AndroidTestingRunner;
 import android.testing.TestableLooper;
+import android.util.Log;
 
 import androidx.test.filters.FlakyTest;
 
+import com.android.internal.telephony.cdma.CdmaSmsBroadcastConfigInfo;
+import com.android.internal.telephony.gsm.SmsBroadcastConfigInfo;
 import com.android.internal.telephony.imsphone.ImsPhone;
 import com.android.internal.telephony.test.SimulatedCommands;
 import com.android.internal.telephony.test.SimulatedCommandsVerifier;
+import com.android.internal.telephony.uicc.AdnRecord;
+import com.android.internal.telephony.uicc.AdnRecordCache;
 import com.android.internal.telephony.uicc.IccCardApplicationStatus;
 import com.android.internal.telephony.uicc.IccCardStatus;
+import com.android.internal.telephony.uicc.IccConstants;
 import com.android.internal.telephony.uicc.IccRecords;
 import com.android.internal.telephony.uicc.IccVmNotSupportedException;
 import com.android.internal.telephony.uicc.UiccController;
@@ -96,21 +115,29 @@
 import org.mockito.Mockito;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 @RunWith(AndroidTestingRunner.class)
 @TestableLooper.RunWithLooper
 public class GsmCdmaPhoneTest extends TelephonyTest {
+    private static final String LOG_TAG = "GsmCdmaPhoneTest";
     private static final String TEST_EMERGENCY_NUMBER = "555";
 
     // Mocked classes
     private Handler mTestHandler;
     private UiccSlot mUiccSlot;
     private CommandsInterface mMockCi;
+    private AdnRecordCache adnRecordCache;
 
     //mPhoneUnderTest
     private GsmCdmaPhone mPhoneUT;
 
+    // Ideally we would use TestableDeviceConfig, but that's not doable because the Settings
+    // app is not currently debuggable. For now, we use the real device config and ensure that
+    // we reset the cellular_security namespace property to its pre-test value after every test.
+    private DeviceConfig.Properties mPreTestProperties;
+
     private static final int EVENT_EMERGENCY_CALLBACK_MODE_EXIT = 1;
     private static final int EVENT_EMERGENCY_CALL_TOGGLE = 2;
     private static final int EVENT_SET_ICC_LOCK_ENABLED = 3;
@@ -134,10 +161,13 @@
     @Before
     public void setUp() throws Exception {
         super.setUp(getClass().getSimpleName());
+        mPreTestProperties = DeviceConfig.getProperties(
+                TelephonyManager.PROPERTY_ENABLE_NULL_CIPHER_TOGGLE);
         mTestHandler = Mockito.mock(Handler.class);
         mUiccSlot = Mockito.mock(UiccSlot.class);
         mUiccPort = Mockito.mock(UiccPort.class);
         mMockCi = Mockito.mock(CommandsInterface.class);
+        adnRecordCache = Mockito.mock(AdnRecordCache.class);
         doReturn(false).when(mSST).isDeviceShuttingDown();
         doReturn(true).when(mImsManager).isVolteEnabledByPlatform();
 
@@ -157,6 +187,13 @@
     public void tearDown() throws Exception {
         mPhoneUT.removeCallbacksAndMessages(null);
         mPhoneUT = null;
+        try {
+            DeviceConfig.setProperties(mPreTestProperties);
+        } catch (DeviceConfig.BadConfigException e) {
+            Log.e(LOG_TAG,
+                    "Failed to reset DeviceConfig to pre-test state. Test results may be impacted. "
+                            + e.getMessage());
+        }
         super.tearDown();
     }
 
@@ -953,7 +990,7 @@
         verify(mTelephonyManager).setBasebandVersionForPhone(eq(mPhoneUT.getPhoneId()),
                 nullable(String.class));
         // IMEI
-        assertEquals(SimulatedCommands.FAKE_IMEI, mPhoneUT.getImei());
+        assertEquals(FAKE_IMEI, mPhoneUT.getImei());
         // IMEISV
         assertEquals(SimulatedCommands.FAKE_IMEISV, mPhoneUT.getDeviceSvn());
         // radio capability
@@ -979,7 +1016,7 @@
         verify(mTelephonyManager, times(2)).setBasebandVersionForPhone(eq(mPhoneUT.getPhoneId()),
                 nullable(String.class));
         // device identity
-        assertEquals(SimulatedCommands.FAKE_IMEI, mPhoneUT.getImei());
+        assertEquals(FAKE_IMEI, mPhoneUT.getImei());
         assertEquals(SimulatedCommands.FAKE_IMEISV, mPhoneUT.getDeviceSvn());
         assertEquals(SimulatedCommands.FAKE_ESN, mPhoneUT.getEsn());
         assertEquals(SimulatedCommands.FAKE_MEID, mPhoneUT.getMeid());
@@ -1051,13 +1088,13 @@
 
         // invalid subId
         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubscriptionController).
-                getSubIdUsingPhoneId(anyInt());
+                getSubId(anyInt());
         assertEquals(false, mPhoneUT.getCallForwardingIndicator());
 
         // valid subId, sharedPreference not present
         int subId1 = 0;
         int subId2 = 1;
-        doReturn(subId1).when(mSubscriptionController).getSubIdUsingPhoneId(anyInt());
+        doReturn(subId1).when(mSubscriptionController).getSubId(anyInt());
         assertEquals(false, mPhoneUT.getCallForwardingIndicator());
 
         // old sharedPreference present
@@ -1079,7 +1116,7 @@
         assertEquals(true, mPhoneUT.getCallForwardingIndicator());
 
         // check for another subId
-        doReturn(subId2).when(mSubscriptionController).getSubIdUsingPhoneId(anyInt());
+        doReturn(subId2).when(mSubscriptionController).getSubId(anyInt());
         assertEquals(false, mPhoneUT.getCallForwardingIndicator());
 
         // set value for the new subId in sharedPreference
@@ -1088,7 +1125,7 @@
         assertEquals(true, mPhoneUT.getCallForwardingIndicator());
 
         // switching back to previous subId, stored value should still be available
-        doReturn(subId1).when(mSubscriptionController).getSubIdUsingPhoneId(anyInt());
+        doReturn(subId1).when(mSubscriptionController).getSubId(anyInt());
         assertEquals(true, mPhoneUT.getCallForwardingIndicator());
 
         // cleanup
@@ -1291,11 +1328,12 @@
     public void testSetRadioPower() throws Exception {
         mPhoneUT.setRadioPower(false);
         verify(mSST).setRadioPowerForReason(false, false, false, false,
-                Phone.RADIO_POWER_REASON_USER);
+                TelephonyManager.RADIO_POWER_REASON_USER);
 
         // Turn on radio for emergency call.
         mPhoneUT.setRadioPower(true, true, false, true);
-        verify(mSST).setRadioPowerForReason(true, true, false, true, Phone.RADIO_POWER_REASON_USER);
+        verify(mSST).setRadioPowerForReason(true, true, false, true,
+                TelephonyManager.RADIO_POWER_REASON_USER);
     }
 
     @Test
@@ -1303,12 +1341,12 @@
     public void testSetRadioPowerOnForTestEmergencyCall() {
         mPhoneUT.setRadioPower(false);
         verify(mSST).setRadioPowerForReason(false, false, false, false,
-                Phone.RADIO_POWER_REASON_USER);
+                TelephonyManager.RADIO_POWER_REASON_USER);
 
         mPhoneUT.setRadioPowerOnForTestEmergencyCall(false);
         verify(mSST).clearAllRadioOffReasons();
         verify(mSST).setRadioPowerForReason(eq(true), eq(false), anyBoolean(), eq(false),
-                eq(Phone.RADIO_POWER_REASON_USER));
+                eq(TelephonyManager.RADIO_POWER_REASON_USER));
     }
 
     @Test
@@ -1494,7 +1532,7 @@
     @SmallTest
     public void testLoadAllowedNetworksFromSubscriptionDatabase_loadTheNullValue_isLoadedTrue() {
         int subId = 1;
-        doReturn(subId).when(mSubscriptionController).getSubIdUsingPhoneId(anyInt());
+        doReturn(subId).when(mSubscriptionController).getSubId(anyInt());
 
         doReturn(null).when(mSubscriptionController).getSubscriptionProperty(anyInt(),
                 eq(SubscriptionManager.ALLOWED_NETWORK_TYPES));
@@ -1508,7 +1546,7 @@
     @SmallTest
     public void testLoadAllowedNetworksFromSubscriptionDatabase_subIdNotValid_isLoadedFalse() {
         int subId = -1;
-        doReturn(subId).when(mSubscriptionController).getSubIdUsingPhoneId(anyInt());
+        doReturn(subId).when(mSubscriptionController).getSubId(anyInt());
 
         when(mSubscriptionController.getSubscriptionProperty(anyInt(),
                 eq(SubscriptionManager.ALLOWED_NETWORK_TYPES))).thenReturn(null);
@@ -1599,7 +1637,7 @@
 
         doReturn(true).when(mTelephonyManager).isEmergencyNumber(anyString());
         doReturn(isEmergencyPerDialedSim).when(mEmergencyNumberTracker).isEmergencyNumber(
-                anyString(), anyBoolean());
+                anyString());
 
         mPhoneUT.setImsPhone(mImsPhone);
     }
@@ -1651,10 +1689,23 @@
 
 
     private SubscriptionInfo makeSubscriptionInfo(boolean isOpportunistic, int usageSetting) {
-        return new SubscriptionInfo(
-                 1, "xxxxxxxxx", 1, "Android Test", "Android Test", 0, 0, "8675309", 0,
-                null, "001", "01", "us", true, null, null, 0, isOpportunistic, null, false,
-                1, 1, 0, null, null, true, 0, usageSetting);
+        return new SubscriptionInfo.Builder()
+                .setId(1)
+                .setIccId("xxxxxxxxx")
+                .setSimSlotIndex(1)
+                .setDisplayName("Android Test")
+                .setDisplayName("Android Test")
+                .setDisplayNameSource(SubscriptionManager.NAME_SOURCE_CARRIER)
+                .setNumber("8675309")
+                .setMcc("001")
+                .setMnc("01")
+                .setCountryIso("us")
+                .setEmbedded(true)
+                .setOpportunistic(isOpportunistic)
+                .setCarrierId(1)
+                .setProfileClass(SubscriptionManager.PROFILE_CLASS_PROVISIONING)
+                .setUsageSetting(usageSetting)
+                .build();
     }
 
     @Test
@@ -1726,4 +1777,906 @@
         processAllMessages();
         verify(mMockCi, never()).setUsageSetting(any(), anyInt());
     }
+
+    public void fdnCheckSetup() {
+        // FDN check setup
+        mPhoneUT.mCi = mMockCi;
+        doReturn(adnRecordCache).when(mSimRecords).getAdnCache();
+        doReturn(mUiccProfile).when(mUiccController).getUiccProfileForPhone(anyInt());
+        doReturn(true).when(mUiccCardApplication3gpp).getIccFdnAvailable();
+        doReturn(true).when(mUiccCardApplication3gpp).getIccFdnEnabled();
+    }
+
+    @Test
+    public void testGetCallForwardingOption_FdnCheck() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+        Message message = Message.obtain(mTestHandler);
+
+        // FDN check success - no exception is returned
+        AdnRecord adnRecord = new AdnRecord(null, "*#21");
+        fdnList.add(0, adnRecord);
+        mPhoneUT.getCallForwardingOption(CommandsInterface.CF_REASON_UNCONDITIONAL, message);
+        processAllMessages();
+        verify(mMockCi).queryCallForwardStatus(eq(CommandsInterface.CF_REASON_UNCONDITIONAL),
+                eq(CommandsInterface.SERVICE_CLASS_VOICE), any(), any());
+        // FDN check failure - returns CommandException in onComplete
+        fdnList.remove(0);
+        mPhoneUT.getCallForwardingOption(CommandsInterface.CF_REASON_UNCONDITIONAL, message);
+        processAllMessages();
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(((CommandException) ar.exception).getCommandError(),
+                CommandException.Error.FDN_CHECK_FAILURE);
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testSetCallForwardingOption_FdnCheck() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+        Message message = Message.obtain(mTestHandler);
+
+        // FDN check success - no exception is returned
+        AdnRecord adnRecord = new AdnRecord(null, "**21");
+        fdnList.add(0, adnRecord);
+        mPhoneUT.setCallForwardingOption(CommandsInterface.CF_ACTION_REGISTRATION,
+                CommandsInterface.CF_REASON_UNCONDITIONAL, "123", 0,
+                message);
+        processAllMessages();
+        verify(mMockCi).setCallForward(eq(CommandsInterface.CF_ACTION_REGISTRATION),
+                eq(CommandsInterface.CF_REASON_UNCONDITIONAL),
+                eq(CommandsInterface.SERVICE_CLASS_VOICE), eq("123"), eq(0), any());
+        // FDN check failure - returns CommandException in onComplete
+        fdnList.remove(0);
+        mPhoneUT.setCallForwardingOption(CommandsInterface.CF_ACTION_REGISTRATION,
+                CommandsInterface.CF_REASON_UNCONDITIONAL, "", 0, message);
+        processAllMessages();
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(((CommandException) ar.exception).getCommandError(),
+                CommandException.Error.FDN_CHECK_FAILURE);
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testGetCallBarring_FdnCheck() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+        Message message = Message.obtain(mTestHandler);
+
+        // FDN check success - no exception is returned
+        AdnRecord adnRecord = new AdnRecord(null, "*#330");
+        fdnList.add(0, adnRecord);
+        mPhoneUT.getCallBarring(CommandsInterface.CB_FACILITY_BA_ALL, "", message,
+                CommandsInterface.SERVICE_CLASS_VOICE);
+        processAllMessages();
+        verify(mMockCi).queryFacilityLock(eq(CommandsInterface.CB_FACILITY_BA_ALL), any(),
+                eq(CommandsInterface.SERVICE_CLASS_VOICE), any());
+        // FDN check failure - returns CommandException in onComplete
+        fdnList.remove(0);
+        mPhoneUT.getCallBarring(CommandsInterface.CB_FACILITY_BA_ALL, "", message,
+                CommandsInterface.SERVICE_CLASS_VOICE);
+        processAllMessages();
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(((CommandException) ar.exception).getCommandError(),
+                CommandException.Error.FDN_CHECK_FAILURE);
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testSetCallBarring_FdnCheck() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+        Message message = Message.obtain(mTestHandler);
+
+        // FDN check success - no exception is returned
+        AdnRecord adnRecord = new AdnRecord(null, "*330");
+        fdnList.add(0, adnRecord);
+        mPhoneUT.setCallBarring(CommandsInterface.CB_FACILITY_BA_ALL, true, "",
+                message, CommandsInterface.SERVICE_CLASS_VOICE);
+        processAllMessages();
+        verify(mMockCi).setFacilityLock(eq(CommandsInterface.CB_FACILITY_BA_ALL),
+                eq(true), any(), eq(CommandsInterface.SERVICE_CLASS_VOICE), any());
+        // FDN check failure - returns CommandException in onComplete
+        fdnList.remove(0);
+        mPhoneUT.setCallBarring(CommandsInterface.CB_FACILITY_BA_ALL, true, "",
+                message, CommandsInterface.SERVICE_CLASS_VOICE);
+        processAllMessages();
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(((CommandException) ar.exception).getCommandError(),
+                CommandException.Error.FDN_CHECK_FAILURE);
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testChangeCallBarringPassword_FdnCheck() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+        Message message = Message.obtain(mTestHandler);
+
+        // FDN check success - no exception is returned
+        AdnRecord adnRecord = new AdnRecord(null, "**03*330");
+        fdnList.add(0, adnRecord);
+        mPhoneUT.changeCallBarringPassword(CommandsInterface.CB_FACILITY_BA_ALL, "",
+                "", message);
+        processAllMessages();
+        verify(mMockCi).changeBarringPassword(eq(CommandsInterface.CB_FACILITY_BA_ALL), any(),
+                any(), any());
+        // FDN check failure - returns CommandException in onComplete
+        fdnList.remove(0);
+        mPhoneUT.changeCallBarringPassword(CommandsInterface.CB_FACILITY_BA_ALL, "",
+                "", message);
+        processAllMessages();
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(((CommandException) ar.exception).getCommandError(),
+                CommandException.Error.FDN_CHECK_FAILURE);
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testGetOutgoingCallerIdDisplay_FdnCheck() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+        Message message = Message.obtain(mTestHandler);
+
+        // FDN check success - no exception is returned
+        AdnRecord adnRecord = new AdnRecord(null, "*#31");
+        fdnList.add(0, adnRecord);
+        mPhoneUT.getOutgoingCallerIdDisplay(message);
+        processAllMessages();
+        verify(mMockCi).getCLIR(any());
+        // FDN check failure - returns CommandException in onComplete
+        fdnList.remove(0);
+        mPhoneUT.getOutgoingCallerIdDisplay(message);
+        processAllMessages();
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(((CommandException) ar.exception).getCommandError(),
+                CommandException.Error.FDN_CHECK_FAILURE);
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testSetOutgoingCallerIdDisplay_FdnCheck() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+        Message message = Message.obtain(mTestHandler);
+
+        // FDN check success - no exception is returned
+        AdnRecord adnRecord = new AdnRecord(null, "*31");
+        fdnList.add(0, adnRecord);
+        mPhoneUT.setOutgoingCallerIdDisplay(CommandsInterface.CLIR_SUPPRESSION, message);
+        processAllMessages();
+        verify(mMockCi).setCLIR(eq(CommandsInterface.CLIR_SUPPRESSION), any());
+        // FDN check failure - returns CommandException in onComplete
+        fdnList.remove(0);
+        mPhoneUT.setOutgoingCallerIdDisplay(CommandsInterface.CLIR_SUPPRESSION, message);
+        processAllMessages();
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(((CommandException) ar.exception).getCommandError(),
+                CommandException.Error.FDN_CHECK_FAILURE);
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testQueryCLIP_FdnCheck() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+        Message message = Message.obtain(mTestHandler);
+
+        // FDN check success - no exception is returned
+        AdnRecord adnRecord = new AdnRecord(null, "*#30");
+        fdnList.add(0, adnRecord);
+        mPhoneUT.queryCLIP(message);
+        processAllMessages();
+        verify(mMockCi).queryCLIP(any());
+        // FDN check failure - returns CommandException in onComplete
+        fdnList.remove(0);
+        mPhoneUT.queryCLIP(message);
+        processAllMessages();
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(((CommandException) ar.exception).getCommandError(),
+                CommandException.Error.FDN_CHECK_FAILURE);
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testGetCallWaiting_FdnCheck() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+        Message message = Message.obtain(mTestHandler);
+
+        // FDN check success - no exception is returned
+        AdnRecord adnRecord = new AdnRecord(null, "*#43");
+        fdnList.add(0, adnRecord);
+        mPhoneUT.getCallWaiting(message);
+        processAllMessages();
+        verify(mMockCi).queryCallWaiting(eq(CommandsInterface.SERVICE_CLASS_NONE), any());
+        // FDN check failure - returns CommandException in onComplete
+        fdnList.remove(0);
+        mPhoneUT.getCallWaiting(message);
+        processAllMessages();
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(((CommandException) ar.exception).getCommandError(),
+                CommandException.Error.FDN_CHECK_FAILURE);
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testSetCallWaiting_FdnCheck() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+        Message message = Message.obtain(mTestHandler);
+
+        // FDN check success - no exception is returned
+        AdnRecord adnRecord = new AdnRecord(null, "*43");
+        fdnList.add(0, adnRecord);
+        mPhoneUT.setCallWaiting(true, CommandsInterface.SERVICE_CLASS_VOICE, message);
+        processAllMessages();
+        verify(mMockCi).setCallWaiting(eq(true), eq(CommandsInterface.SERVICE_CLASS_VOICE),
+                any());
+        // FDN check failure - returns CommandException in onComplete
+        fdnList.remove(0);
+        mPhoneUT.setCallWaiting(true, CommandsInterface.SERVICE_CLASS_VOICE, message);
+        processAllMessages();
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(((CommandException) ar.exception).getCommandError(),
+                CommandException.Error.FDN_CHECK_FAILURE);
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testDial_fdnCheck() throws Exception {
+        // dial setup
+        mSST.mSS = mServiceState;
+        doReturn(ServiceState.STATE_IN_SERVICE).when(mServiceState).getState();
+        mCT.mForegroundCall = mGsmCdmaCall;
+        mCT.mBackgroundCall = mGsmCdmaCall;
+        mCT.mRingingCall = mGsmCdmaCall;
+        doReturn(GsmCdmaCall.State.IDLE).when(mGsmCdmaCall).getState();
+        replaceInstance(Phone.class, "mImsPhone", mPhoneUT, mImsPhone);
+
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+
+        // FDN check success - no exception is returned
+        AdnRecord dialRecord = new AdnRecord(null, "1234567890");
+        fdnList.add(0, dialRecord);
+        Connection connection = mPhoneUT.dial("1234567890",
+                new PhoneInternalInterface.DialArgs.Builder().build());
+        verify(mCT).dialGsm(eq("1234567890"), any(PhoneInternalInterface.DialArgs.class));
+
+        // FDN check failure - returns CallStateException
+        fdnList.remove(0);
+        try {
+            connection = mPhoneUT.dial("1234567890",
+                    new PhoneInternalInterface.DialArgs.Builder().build());
+            fail("Expected CallStateException with ERROR_FDN_BLOCKED thrown.");
+        } catch (CallStateException e) {
+            assertEquals(CallStateException.ERROR_FDN_BLOCKED, e.getError());
+        }
+
+        // clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void testHandleNullCipherAndIntegrityEnabled_radioFeatureUnsupported() {
+        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_CELLULAR_SECURITY,
+                TelephonyManager.PROPERTY_ENABLE_NULL_CIPHER_TOGGLE, Boolean.TRUE.toString(),
+                false);
+        mPhoneUT.mCi = mMockCi;
+        assertFalse(mPhoneUT.isNullCipherAndIntegritySupported());
+
+        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(EVENT_RADIO_AVAILABLE,
+                new AsyncResult(null, new int[]{ServiceState.RIL_RADIO_TECHNOLOGY_GSM}, null)));
+        processAllMessages();
+
+        verify(mMockCi, times(1)).setNullCipherAndIntegrityEnabled(anyBoolean(),
+                any(Message.class));
+
+        // Some ephemeral error occurred in the modem, but the feature was supported
+        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(EVENT_SET_NULL_CIPHER_AND_INTEGRITY_DONE,
+                new AsyncResult(null, null,
+                        new CommandException(CommandException.Error.REQUEST_NOT_SUPPORTED))));
+        processAllMessages();
+        assertFalse(mPhoneUT.isNullCipherAndIntegritySupported());
+    }
+
+    @Test
+    public void testHandleNullCipherAndIntegrityEnabled_radioSupportsFeature() {
+        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_CELLULAR_SECURITY,
+                TelephonyManager.PROPERTY_ENABLE_NULL_CIPHER_TOGGLE, Boolean.TRUE.toString(),
+                false);
+        mPhoneUT.mCi = mMockCi;
+        assertFalse(mPhoneUT.isNullCipherAndIntegritySupported());
+
+        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(EVENT_RADIO_AVAILABLE,
+                new AsyncResult(null, new int[]{ServiceState.RIL_RADIO_TECHNOLOGY_GSM}, null)));
+        processAllMessages();
+
+        verify(mMockCi, times(1)).setNullCipherAndIntegrityEnabled(anyBoolean(),
+                any(Message.class));
+
+        // Some ephemeral error occurred in the modem, but the feature was supported
+        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(EVENT_SET_NULL_CIPHER_AND_INTEGRITY_DONE,
+                new AsyncResult(null, null, null)));
+        processAllMessages();
+        assertTrue(mPhoneUT.isNullCipherAndIntegritySupported());
+    }
+
+    @Test
+    public void testHandleNullCipherAndIntegrityEnabled_featureFlagOn() {
+        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_CELLULAR_SECURITY,
+                TelephonyManager.PROPERTY_ENABLE_NULL_CIPHER_TOGGLE, Boolean.TRUE.toString(),
+                false);
+        mPhoneUT.mCi = mMockCi;
+
+        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(EVENT_RADIO_AVAILABLE,
+                new AsyncResult(null, new int[]{ServiceState.RIL_RADIO_TECHNOLOGY_GSM}, null)));
+        processAllMessages();
+
+        verify(mMockCi, times(1)).setNullCipherAndIntegrityEnabled(anyBoolean(),
+                any(Message.class));
+    }
+
+    @Test
+    public void testHandleNullCipherAndIntegrityEnabled_featureFlagOff() {
+        mPhoneUT.mCi = mMockCi;
+        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_CELLULAR_SECURITY,
+                TelephonyManager.PROPERTY_ENABLE_NULL_CIPHER_TOGGLE, Boolean.FALSE.toString(),
+                false);
+
+        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(EVENT_RADIO_AVAILABLE,
+                new AsyncResult(null, new int[]{ServiceState.RIL_RADIO_TECHNOLOGY_GSM}, null)));
+        processAllMessages();
+
+        verify(mMockCi, times(0)).setNullCipherAndIntegrityEnabled(anyBoolean(),
+                any(Message.class));
+    }
+
+    public void fdnCheckCleanup() {
+        doReturn(false).when(mUiccCardApplication3gpp).getIccFdnAvailable();
+        doReturn(false).when(mUiccCardApplication3gpp).getIccFdnEnabled();
+    }
+
+    @Test
+    @SmallTest
+    public void testTriggerImsDeregistration() throws Exception {
+        replaceInstance(Phone.class, "mImsPhone", mPhoneUT, mImsPhone);
+
+        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(EVENT_IMS_DEREGISTRATION_TRIGGERED,
+                new AsyncResult(null,
+                        new int[]{ImsRegistrationImplBase.REASON_SIM_REFRESH}, null)));
+        processAllMessages();
+
+        verify(mImsPhone, times(1)).triggerImsDeregistration(
+                eq(ImsRegistrationImplBase.REASON_SIM_REFRESH));
+    }
+
+    @Test
+    public void testDomainSelectionEmergencyCallCs() throws CallStateException {
+        setupEmergencyCallScenario(false /* USE_ONLY_DIALED_SIM_ECC_LIST */,
+                false /* isEmergencyOnDialedSim */);
+
+        Bundle extras = new Bundle();
+        extras.putInt(PhoneConstants.EXTRA_DIAL_DOMAIN, NetworkRegistrationInfo.DOMAIN_CS);
+        ImsPhone.ImsDialArgs dialArgs = new ImsPhone.ImsDialArgs.Builder()
+                .setIntentExtras(extras)
+                .build();
+        mPhoneUT.dial(TEST_EMERGENCY_NUMBER, dialArgs);
+
+        verify(mCT).dialGsm(anyString(), any(PhoneInternalInterface.DialArgs.class));
+    }
+
+    @Test
+    public void testDomainSelectionEmergencyCallPs() throws CallStateException {
+        setupEmergencyCallScenario(false /* USE_ONLY_DIALED_SIM_ECC_LIST */,
+                false /* isEmergencyOnDialedSim */);
+
+        doReturn(false).when(mImsPhone).isImsAvailable();
+
+        Bundle extras = new Bundle();
+        extras.putInt(PhoneConstants.EXTRA_DIAL_DOMAIN, NetworkRegistrationInfo.DOMAIN_PS);
+        ImsPhone.ImsDialArgs dialArgs = new ImsPhone.ImsDialArgs.Builder()
+                .setIntentExtras(extras)
+                .build();
+        mPhoneUT.dial(TEST_EMERGENCY_NUMBER, dialArgs);
+
+        verify(mImsPhone).dial(anyString(), any(PhoneInternalInterface.DialArgs.class));
+    }
+
+    @Test
+    public void testDomainSelectionDialCs() throws Exception {
+        doReturn(true).when(mImsPhone).isImsAvailable();
+        doReturn(true).when(mImsManager).isVolteEnabledByPlatform();
+        doReturn(true).when(mImsManager).isEnhanced4gLteModeSettingEnabledByUser();
+        doReturn(true).when(mImsManager).isNonTtyOrTtyOnVolteEnabled();
+        doReturn(true).when(mImsPhone).isVoiceOverCellularImsEnabled();
+        doReturn(true).when(mImsPhone).isUtEnabled();
+
+        replaceInstance(Phone.class, "mImsPhone", mPhoneUT, mImsPhone);
+
+        Bundle extras = new Bundle();
+        extras.putInt(PhoneConstants.EXTRA_DIAL_DOMAIN, NetworkRegistrationInfo.DOMAIN_CS);
+        ImsPhone.ImsDialArgs dialArgs = new ImsPhone.ImsDialArgs.Builder()
+                .setIntentExtras(extras)
+                .build();
+        Connection connection = mPhoneUT.dial("1234567890", dialArgs);
+        verify(mCT).dialGsm(eq("1234567890"), any(PhoneInternalInterface.DialArgs.class));
+    }
+
+    @Test
+    public void testDomainSelectionDialPs() throws Exception {
+        mSST.mSS = mServiceState;
+        doReturn(ServiceState.STATE_IN_SERVICE).when(mServiceState).getState();
+
+        mCT.mForegroundCall = mGsmCdmaCall;
+        mCT.mBackgroundCall = mGsmCdmaCall;
+        mCT.mRingingCall = mGsmCdmaCall;
+        doReturn(GsmCdmaCall.State.IDLE).when(mGsmCdmaCall).getState();
+
+        replaceInstance(Phone.class, "mImsPhone", mPhoneUT, mImsPhone);
+
+        Bundle extras = new Bundle();
+        extras.putInt(PhoneConstants.EXTRA_DIAL_DOMAIN, NetworkRegistrationInfo.DOMAIN_PS);
+        ImsPhone.ImsDialArgs dialArgs = new ImsPhone.ImsDialArgs.Builder()
+                .setIntentExtras(extras)
+                .build();
+        Connection connection = mPhoneUT.dial("1234567890", dialArgs);
+        verify(mImsPhone).dial(eq("1234567890"), any(PhoneInternalInterface.DialArgs.class));
+    }
+
+    @Test
+    @SmallTest
+    public void testSetCellBroadcastIdRangesSuccess() throws Exception {
+        final int[][] channelValues = {
+            {0, 999}, {1000, 1003}, {1004, 0x0FFF}, {0x1000, 0x10FF}, {0x1100, 0x112F},
+            {0x1130, 0x1900}, {0x1901, 0x9FFF}, {0xA000, 0xFFFE}, {0xFFFF, 0xFFFF}};
+        List<CellBroadcastIdRange> ranges = new ArrayList<>();
+        for (int i = 0; i < channelValues.length; i++) {
+            ranges.add(new CellBroadcastIdRange(channelValues[i][0], channelValues[i][1],
+                    SmsCbMessage.MESSAGE_FORMAT_3GPP, i > 0 ? true : false));
+        }
+
+        List<SmsBroadcastConfigInfo> gsmConfigs = new ArrayList<>();
+        gsmConfigs.add(new SmsBroadcastConfigInfo(0, 999, 0, 255, false));
+        gsmConfigs.add(new SmsBroadcastConfigInfo(1000, 0xFFFF, 0, 255, true));
+
+        ArgumentCaptor<SmsBroadcastConfigInfo[]> gsmCaptor = ArgumentCaptor.forClass(
+                SmsBroadcastConfigInfo[].class);
+        ArgumentCaptor<Message> msgCaptor = ArgumentCaptor.forClass(Message.class);
+
+        mPhoneUT.mCi = mMockCi;
+
+        mPhoneUT.setCellBroadcastIdRanges(ranges, r -> assertTrue(
+                TelephonyManager.CELL_BROADCAST_RESULT_SUCCESS == r));
+        waitForMs(100);
+
+        verify(mMockCi, times(1)).setGsmBroadcastConfig(gsmCaptor.capture(), msgCaptor.capture());
+        List<SmsBroadcastConfigInfo> gsmArgs = Arrays.asList(
+                (SmsBroadcastConfigInfo[]) gsmCaptor.getValue());
+        assertEquals(gsmConfigs, gsmArgs);
+
+        Message msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(1)).setGsmBroadcastActivation(eq(true), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, never()).setCdmaBroadcastConfig(any(), any());
+        verify(mMockCi, never()).setCdmaBroadcastActivation(anyBoolean(), any());
+
+        assertEquals(mPhoneUT.getCellBroadcastIdRanges(), mergeRangesAsNeeded(ranges));
+
+        //Verify to set cdma config and activate, but no more for gsm as no change
+        for (int i = 0; i < channelValues.length; i++) {
+            ranges.add(new CellBroadcastIdRange(channelValues[i][0], channelValues[i][1],
+                    SmsCbMessage.MESSAGE_FORMAT_3GPP2, i > 0 ? true : false));
+        }
+        List<CdmaSmsBroadcastConfigInfo> cdmaConfigs = new ArrayList<>();
+        cdmaConfigs.add(new CdmaSmsBroadcastConfigInfo(0, 999, 1, false));
+        cdmaConfigs.add(new CdmaSmsBroadcastConfigInfo(1000, 0xFFFF, 1, true));
+        ArgumentCaptor<CdmaSmsBroadcastConfigInfo[]> cdmaCaptor = ArgumentCaptor.forClass(
+                CdmaSmsBroadcastConfigInfo[].class);
+
+        mPhoneUT.setCellBroadcastIdRanges(ranges, r -> assertTrue(
+                TelephonyManager.CELL_BROADCAST_RESULT_SUCCESS == r));
+        waitForMs(100);
+
+        verify(mMockCi, times(1)).setGsmBroadcastConfig(any(), any());
+        verify(mMockCi, times(1)).setCdmaBroadcastConfig(cdmaCaptor.capture(), msgCaptor.capture());
+        List<CdmaSmsBroadcastConfigInfo> cdmaArgs = Arrays.asList(
+                (CdmaSmsBroadcastConfigInfo[]) cdmaCaptor.getValue());
+        assertEquals(cdmaConfigs, cdmaArgs);
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(1)).setGsmBroadcastActivation(anyBoolean(), any());
+        verify(mMockCi, times(1)).setCdmaBroadcastActivation(eq(true), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        assertEquals(mPhoneUT.getCellBroadcastIdRanges(), mergeRangesAsNeeded(ranges));
+
+        // Verify not to set cdma or gsm config as the config is not changed
+        mPhoneUT.setCellBroadcastIdRanges(ranges, r -> assertTrue(
+                TelephonyManager.CELL_BROADCAST_RESULT_SUCCESS == r));
+        waitForMs(100);
+
+        verify(mMockCi, times(1)).setCdmaBroadcastConfig(any(), any());
+        verify(mMockCi, times(1)).setCdmaBroadcastActivation(anyBoolean(), any());
+        verify(mMockCi, times(1)).setGsmBroadcastConfig(any(), any());
+        verify(mMockCi, times(1)).setGsmBroadcastActivation(anyBoolean(), any());
+
+        assertEquals(mPhoneUT.getCellBroadcastIdRanges(), mergeRangesAsNeeded(ranges));
+
+        // Verify to reset ranges with empty ranges list
+        mPhoneUT.setCellBroadcastIdRanges(new ArrayList<>(), r -> assertTrue(
+                TelephonyManager.CELL_BROADCAST_RESULT_SUCCESS == r));
+
+        waitForMs(100);
+
+        verify(mMockCi, times(2)).setGsmBroadcastConfig(gsmCaptor.capture(), msgCaptor.capture());
+        assertEquals(0, ((SmsBroadcastConfigInfo[]) gsmCaptor.getValue()).length);
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        // Verify to deavtivate gsm broadcast on empty ranges
+        verify(mMockCi, times(1)).setGsmBroadcastActivation(eq(false), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(2)).setCdmaBroadcastConfig(cdmaCaptor.capture(), msgCaptor.capture());
+        assertEquals(0, ((CdmaSmsBroadcastConfigInfo[]) cdmaCaptor.getValue()).length);
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        processAllMessages();
+        waitForMs(100);
+
+        // Verify to deavtivate cdma broadcast on empty ranges
+        verify(mMockCi, times(1)).setCdmaBroadcastActivation(eq(false), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        processAllMessages();
+        waitForMs(100);
+
+        assertTrue(mPhoneUT.getCellBroadcastIdRanges().isEmpty());
+
+        //Verify to set gsm and cdma config then activate again
+        mPhoneUT.setCellBroadcastIdRanges(ranges, r -> assertTrue(
+                TelephonyManager.CELL_BROADCAST_RESULT_SUCCESS == r));
+
+        waitForMs(100);
+
+        verify(mMockCi, times(3)).setGsmBroadcastConfig(gsmCaptor.capture(), msgCaptor.capture());
+        gsmArgs = Arrays.asList((SmsBroadcastConfigInfo[]) gsmCaptor.getValue());
+        assertEquals(gsmConfigs, gsmArgs);
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(2)).setGsmBroadcastActivation(eq(true), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(3)).setCdmaBroadcastConfig(cdmaCaptor.capture(), msgCaptor.capture());
+        cdmaArgs = Arrays.asList((CdmaSmsBroadcastConfigInfo[]) cdmaCaptor.getValue());
+        assertEquals(cdmaConfigs, cdmaArgs);
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(2)).setCdmaBroadcastActivation(eq(true), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        assertEquals(mPhoneUT.getCellBroadcastIdRanges(), mergeRangesAsNeeded(ranges));
+    }
+
+    @Test
+    @SmallTest
+    public void testSetCellBroadcastIdRangesFailure() throws Exception {
+        List<CellBroadcastIdRange> ranges = new ArrayList<>();
+
+        // Verify to throw exception for invalid ranges
+        ranges.add(new CellBroadcastIdRange(0, 999, SmsCbMessage.MESSAGE_FORMAT_3GPP, true));
+        ranges.add(new CellBroadcastIdRange(0, 999, SmsCbMessage.MESSAGE_FORMAT_3GPP, false));
+
+        assertThrows(IllegalArgumentException.class,
+                () -> mPhoneUT.setCellBroadcastIdRanges(ranges, r -> {}));
+
+        ArgumentCaptor<Message> msgCaptor = ArgumentCaptor.forClass(Message.class);
+        ranges.clear();
+        ranges.add(new CellBroadcastIdRange(0, 999, SmsCbMessage.MESSAGE_FORMAT_3GPP, true));
+        ranges.add(new CellBroadcastIdRange(0, 999, SmsCbMessage.MESSAGE_FORMAT_3GPP2, true));
+        mPhoneUT.mCi = mMockCi;
+
+        // Verify the result on setGsmBroadcastConfig failure
+        mPhoneUT.setCellBroadcastIdRanges(ranges, r -> assertTrue(
+                TelephonyManager.CELL_BROADCAST_RESULT_FAIL_CONFIG == r));
+        waitForMs(100);
+
+        verify(mMockCi, times(1)).setGsmBroadcastConfig(any(), msgCaptor.capture());
+
+        Message msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg).exception = new RuntimeException();
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(0)).setGsmBroadcastActivation(anyBoolean(), any());
+        verify(mMockCi, times(0)).setCdmaBroadcastConfig(any(), any());
+        verify(mMockCi, times(0)).setCdmaBroadcastActivation(anyBoolean(), any());
+        assertTrue(mPhoneUT.getCellBroadcastIdRanges().isEmpty());
+
+        // Verify the result on setGsmBroadcastActivation failure
+        mPhoneUT.setCellBroadcastIdRanges(ranges, r -> assertTrue(
+                TelephonyManager.CELL_BROADCAST_RESULT_FAIL_ACTIVATION == r));
+        waitForMs(100);
+
+        verify(mMockCi, times(2)).setGsmBroadcastConfig(any(), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(1)).setGsmBroadcastActivation(anyBoolean(), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg).exception = new RuntimeException();
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(0)).setCdmaBroadcastConfig(any(), any());
+        verify(mMockCi, times(0)).setCdmaBroadcastActivation(anyBoolean(), any());
+        assertTrue(mPhoneUT.getCellBroadcastIdRanges().isEmpty());
+
+        // Verify the result on setCdmaBroadcastConfig failure
+        mPhoneUT.setCellBroadcastIdRanges(ranges, r -> assertTrue(
+                TelephonyManager.CELL_BROADCAST_RESULT_FAIL_CONFIG == r));
+        waitForMs(100);
+
+        verify(mMockCi, times(3)).setGsmBroadcastConfig(any(), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(2)).setGsmBroadcastActivation(anyBoolean(), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(1)).setCdmaBroadcastConfig(any(), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg).exception = new RuntimeException();
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(0)).setCdmaBroadcastActivation(anyBoolean(), any());
+
+        List<CellBroadcastIdRange> ranges3gpp = new ArrayList<>();
+        ranges3gpp.add(new CellBroadcastIdRange(0, 999, SmsCbMessage.MESSAGE_FORMAT_3GPP, true));
+
+        assertEquals(mPhoneUT.getCellBroadcastIdRanges(), ranges3gpp);
+
+        // Verify the result on setCdmaBroadcastActivation failure
+        mPhoneUT.setCellBroadcastIdRanges(ranges, r -> assertTrue(
+                TelephonyManager.CELL_BROADCAST_RESULT_FAIL_ACTIVATION == r));
+        waitForMs(200);
+
+        // Verify no more calls as there is no change of ranges for 3gpp
+        verify(mMockCi, times(3)).setGsmBroadcastConfig(any(), any());
+        verify(mMockCi, times(2)).setGsmBroadcastActivation(anyBoolean(), any());
+        verify(mMockCi, times(2)).setCdmaBroadcastConfig(any(), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg);
+        msg.sendToTarget();
+        waitForMs(100);
+
+        verify(mMockCi, times(1)).setCdmaBroadcastActivation(anyBoolean(), msgCaptor.capture());
+
+        msg = msgCaptor.getValue();
+        assertNotNull(msg);
+        AsyncResult.forMessage(msg).exception = new RuntimeException();
+        msg.sendToTarget();
+        waitForMs(100);
+
+        assertEquals(mPhoneUT.getCellBroadcastIdRanges(), ranges3gpp);
+    }
+
+    @Test
+    @SmallTest
+    public void testMergeCellBroadcastIdRangesAsNeeded() {
+        final int[][] channelValues = {
+                {0, 999}, {1000, 1003}, {1004, 0x0FFF}, {0x1000, 0x10FF}, {0x1100, 0x112F},
+                {0x1130, 0x1900}, {0x1901, 0x9FFF}, {0xA000, 0xFFFE}, {0xFFFF, 0xFFFF}};
+        final int[] typeValues = {
+                SmsCbMessage.MESSAGE_FORMAT_3GPP, SmsCbMessage.MESSAGE_FORMAT_3GPP2};
+        final boolean[] enabledValues = {true, false};
+
+        List<CellBroadcastIdRange> ranges = new ArrayList<>();
+        for (int i = 0; i < channelValues.length; i++) {
+            ranges.add(new CellBroadcastIdRange(channelValues[i][0], channelValues[i][1],
+                    typeValues[0], enabledValues[0]));
+        }
+
+        ranges = mergeRangesAsNeeded(ranges);
+
+        assertEquals(ranges.size(), 1);
+        assertEquals(ranges.get(0).getStartId(), channelValues[0][0]);
+        assertEquals(ranges.get(0).getEndId(), channelValues[channelValues.length - 1][0]);
+
+        // Verify not to merge the ranges with different types.
+        ranges.clear();
+        for (int i = 0; i < channelValues.length; i++) {
+            ranges.add(new CellBroadcastIdRange(channelValues[i][0], channelValues[i][1],
+                    typeValues[0], enabledValues[0]));
+            ranges.add(new CellBroadcastIdRange(channelValues[i][0], channelValues[i][1],
+                    typeValues[1], enabledValues[0]));
+        }
+
+        ranges = mergeRangesAsNeeded(ranges);
+
+        assertEquals(ranges.size(), 2);
+        assertEquals(ranges.get(0).getStartId(), channelValues[0][0]);
+        assertEquals(ranges.get(0).getEndId(), channelValues[channelValues.length - 1][0]);
+        assertEquals(ranges.get(1).getStartId(), channelValues[0][0]);
+        assertEquals(ranges.get(1).getEndId(), channelValues[channelValues.length - 1][0]);
+        assertTrue(ranges.get(0).getType() != ranges.get(1).getType());
+
+        // Verify to throw IllegalArgumentException if the same range is enabled and disabled
+        // in the range list.
+        final List<CellBroadcastIdRange> ranges2 = new ArrayList<>();
+        for (int i = 0; i < channelValues.length; i++) {
+            ranges2.add(new CellBroadcastIdRange(channelValues[i][0], channelValues[i][1],
+                    typeValues[0], enabledValues[0]));
+            ranges2.add(new CellBroadcastIdRange(channelValues[i][0], channelValues[i][1],
+                    typeValues[0], enabledValues[1]));
+        }
+
+        assertThrows(IllegalArgumentException.class, () ->
+                mergeRangesAsNeeded(ranges2));
+    }
+
+    @Test
+    public void getImeiType_primary() {
+        Message message = mPhoneUT.obtainMessage(Phone.EVENT_GET_DEVICE_IMEI_DONE);
+        ImeiInfo imeiInfo = new ImeiInfo();
+        imeiInfo.imei = FAKE_IMEI;
+        imeiInfo.svn = FAKE_IMEISV;
+        imeiInfo.type = ImeiInfo.ImeiType.PRIMARY;
+        AsyncResult.forMessage(message, imeiInfo, null);
+        mPhoneUT.handleMessage(message);
+        assertEquals(Phone.IMEI_TYPE_PRIMARY, mPhoneUT.getImeiType());
+        assertEquals(FAKE_IMEI, mPhoneUT.getImei());
+    }
+
+    @Test
+    public void getImeiType_Secondary() {
+        Message message = mPhoneUT.obtainMessage(Phone.EVENT_GET_DEVICE_IMEI_DONE);
+        ImeiInfo imeiInfo = new ImeiInfo();
+        imeiInfo.imei = FAKE_IMEI;
+        imeiInfo.svn = FAKE_IMEISV;
+        imeiInfo.type = ImeiInfo.ImeiType.SECONDARY;
+        AsyncResult.forMessage(message, imeiInfo, null);
+        mPhoneUT.handleMessage(message);
+        assertEquals(Phone.IMEI_TYPE_SECONDARY, mPhoneUT.getImeiType());
+        assertEquals(FAKE_IMEI, mPhoneUT.getImei());
+    }
+
+    @Test
+    public void getImei() {
+        assertTrue(mPhoneUT.isPhoneTypeGsm());
+        Message message = mPhoneUT.obtainMessage(Phone.EVENT_RADIO_AVAILABLE);
+        mPhoneUT.handleMessage(message);
+        verify(mSimulatedCommandsVerifier, times(2)).getImei(nullable(Message.class));
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/IccSmsInterfaceManagerTest.java b/tests/telephonytests/src/com/android/internal/telephony/IccSmsInterfaceManagerTest.java
index 67d6e5c..dc24683 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/IccSmsInterfaceManagerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/IccSmsInterfaceManagerTest.java
@@ -29,10 +29,13 @@
 import static org.junit.Assert.fail;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import com.android.internal.telephony.emergency.EmergencyNumberTracker;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -50,6 +53,9 @@
 
     // Mocked classes
     private SmsPermissions mMockSmsPermissions;
+    protected GsmCdmaPhone mPhone2;  // mPhone as phone 1 is already defined in TelephonyTest.
+    protected EmergencyNumberTracker mEmergencyNumberTracker2;
+    protected IccSmsInterfaceManager.PhoneFactoryProxy mPhoneFactoryProxy;
 
     @Before
     public void setUp() throws Exception {
@@ -57,6 +63,13 @@
         mMockSmsPermissions = mock(SmsPermissions.class);
         mIccSmsInterfaceManager = new IccSmsInterfaceManager(mPhone, mContext, mAppOpsManager,
                 mSmsDispatchersController, mMockSmsPermissions);
+
+        mPhoneFactoryProxy = mock(IccSmsInterfaceManager.PhoneFactoryProxy.class);
+        mIccSmsInterfaceManager.setPhoneFactoryProxy(mPhoneFactoryProxy);
+
+        mPhone2 = mock(GsmCdmaPhone.class);
+        mEmergencyNumberTracker2 = mock(EmergencyNumberTracker.class);
+        doReturn(mEmergencyNumberTracker2).when(mPhone2).getEmergencyNumberTracker();
     }
 
     @After
@@ -144,4 +157,22 @@
             fail("getSmscLatch.await interrupted");
         }
     }
+
+    @Test
+    public void testNotifyIfOutgoingEmergencySmsWithDualSim() {
+        //Replicate Dual-SIM:
+        Phone [] phones = {mPhone, mPhone2};
+        when(mPhoneFactoryProxy.getPhones()).thenReturn(phones);
+        doReturn(1).when(mPhone).getPhoneId();
+        doReturn(2).when(mPhone2).getPhoneId();
+
+        //Replicate behavior when a number is an emergency number
+        // on the secondary SIM but not on the default SIM:
+        when(mPhone.getEmergencyNumberTracker().getEmergencyNumber(any())).thenReturn(null);
+        when(mEmergencyNumberTracker2.getEmergencyNumber(any()))
+                .thenReturn(getTestEmergencyNumber());
+
+        mIccSmsInterfaceManager.notifyIfOutgoingEmergencySms("1234");
+        verify(mEmergencyNumberTracker2).getEmergencyNumber("1234");
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/ImsSmsDispatcherTest.java b/tests/telephonytests/src/com/android/internal/telephony/ImsSmsDispatcherTest.java
index 6251560..858fa2f 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/ImsSmsDispatcherTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/ImsSmsDispatcherTest.java
@@ -27,11 +27,14 @@
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.ArgumentMatchers.nullable;
 import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import android.content.Context;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
 import android.telephony.SmsMessage;
 import android.telephony.ims.stub.ImsSmsImplBase;
 import android.test.suitebuilder.annotation.SmallTest;
@@ -63,7 +66,9 @@
     private FeatureConnector.Listener<ImsManager> mImsManagerListener;
     private HashMap<String, Object> mTrackerData;
     private ImsSmsDispatcher mImsSmsDispatcher;
+    PersistableBundle mBundle = new PersistableBundle();
     private static final int SUB_0 = 0;
+    private static final String TAG = "ImsSmsDispatcherTest";
 
     @Before
     public void setUp() throws Exception {
@@ -86,6 +91,7 @@
         when(mSmsDispatchersController.isIms()).thenReturn(true);
         mTrackerData = new HashMap<>(1);
         when(mSmsTracker.getData()).thenReturn(mTrackerData);
+        verify(mSmsDispatchersController).setImsManager(mImsManager);
     }
 
     @After
@@ -96,6 +102,18 @@
         super.tearDown();
     }
 
+   /**
+     * Send Memory Availability Notification and verify that the token is correct.
+     */
+    @Test
+    @SmallTest
+    public void testOnMemoryAvailable() throws Exception {
+        int token = mImsSmsDispatcher.mNextToken.get();
+        //Send SMMA
+        mImsSmsDispatcher.onMemoryAvailable();
+        assertEquals(token + 1, mImsSmsDispatcher.mNextToken.get());
+        verify(mImsManager).onMemoryAvailable(eq(token + 1));
+    }
     /**
      * Send an SMS and verify that the token and PDU is correct.
      */
@@ -154,6 +172,13 @@
     @SmallTest
     public void testErrorImsRetry() throws Exception {
         int token = mImsSmsDispatcher.mNextToken.get();
+        mContextFixture.getCarrierConfigBundle().putInt(CarrierConfigManager.ImsSms
+                                .KEY_SMS_OVER_IMS_SEND_RETRY_DELAY_MILLIS_INT,
+                                2000);
+        mContextFixture.getCarrierConfigBundle().putInt(CarrierConfigManager.ImsSms
+                                                .KEY_SMS_MAX_RETRY_COUNT_OVER_IMS_INT, 3);
+        mContextFixture.getCarrierConfigBundle().putInt(CarrierConfigManager.ImsSms
+                                                .KEY_SMS_MAX_RETRY_COUNT_INT, 3);
         mTrackerData.put("pdu", com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(null,
                 "+15555551212", "Test", false).encodedMessage);
         when(mImsManager.getSmsFormat()).thenReturn(SmsMessage.FORMAT_3GPP);
@@ -225,4 +250,143 @@
                 ImsSmsImplBase.SEND_STATUS_ERROR, 0, 41);
         verify(mSmsTracker).onFailed(any(Context.class), anyInt(), eq(41));
     }
+
+    @Test
+    public void testSendSmswithMessageRef() throws Exception {
+        int token = mImsSmsDispatcher.mNextToken.get();
+        int messageRef = mImsSmsDispatcher.nextMessageRef() + 1;
+
+        when(mImsManager.getSmsFormat()).thenReturn(SmsMessage.FORMAT_3GPP);
+        when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM);
+        doReturn(mSmsUsageMonitor).when(mSmsDispatchersController).getUsageMonitor();
+
+        mImsSmsDispatcher.sendText("+15555551212", null, "MessageRef test",
+                null, null, null, null, false,
+                -1, false, -1, false, 0);
+        verify(mImsManager).sendSms(eq(token + 1), eq(messageRef), eq(SmsMessage.FORMAT_3GPP),
+                nullable(String.class), eq(false), (byte[]) any());
+    }
+
+    @Test
+    public void testFallbackGsmRetrywithMessageRef() throws Exception {
+        int token = mImsSmsDispatcher.mNextToken.get();
+        int messageRef = mImsSmsDispatcher.nextMessageRef() + 1;
+
+        when(mImsManager.getSmsFormat()).thenReturn(SmsMessage.FORMAT_3GPP);
+        when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM);
+        doReturn(mSmsUsageMonitor).when(mSmsDispatchersController).getUsageMonitor();
+
+        mImsSmsDispatcher.sendText("+15555551212", null, "MessageRef test",
+                null, null, null, null, false,
+                -1, false, -1, false, 0);
+        verify(mImsManager).sendSms(eq(token + 1), eq(messageRef), eq(SmsMessage.FORMAT_3GPP),
+                nullable(String.class), eq(false), (byte[]) any());
+
+        mImsSmsDispatcher.getSmsListener().onSendSmsResult(token + 1, messageRef,
+                ImsSmsImplBase.SEND_STATUS_ERROR_FALLBACK, 0, SmsResponse.NO_ERROR_CODE);
+        processAllMessages();
+
+        ArgumentCaptor<SMSDispatcher.SmsTracker> captor =
+                ArgumentCaptor.forClass(SMSDispatcher.SmsTracker.class);
+        verify(mSmsDispatchersController).sendRetrySms(captor.capture());
+        assertTrue(messageRef + 1 == captor.getValue().mMessageRef);
+    }
+
+    @Test
+    public void testErrorImsRetrywithMessageRef() throws Exception {
+        int token = mImsSmsDispatcher.mNextToken.get();
+        int messageRef = mImsSmsDispatcher.nextMessageRef() + 1;
+        mContextFixture.getCarrierConfigBundle().putInt(CarrierConfigManager.ImsSms
+                                                .KEY_SMS_OVER_IMS_SEND_RETRY_DELAY_MILLIS_INT,
+                                                2000);
+        mContextFixture.getCarrierConfigBundle().putInt(CarrierConfigManager.ImsSms
+                                                .KEY_SMS_MAX_RETRY_COUNT_OVER_IMS_INT, 3);
+        mContextFixture.getCarrierConfigBundle().putInt(CarrierConfigManager.ImsSms
+                                                .KEY_SMS_MAX_RETRY_COUNT_INT, 3);
+        when(mImsManager.getSmsFormat()).thenReturn(SmsMessage.FORMAT_3GPP);
+        when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM);
+        doReturn(mSmsUsageMonitor).when(mSmsDispatchersController).getUsageMonitor();
+
+        mImsSmsDispatcher.sendText("+15555551212", null, "MessageRef test",
+                null, null, null, null, false,
+                -1, false, -1, false, 0);
+        verify(mImsManager).sendSms(eq(token + 1), eq(messageRef), eq(SmsMessage.FORMAT_3GPP),
+                nullable(String.class), eq(false), (byte[]) any());
+
+        // Retry over IMS
+        mImsSmsDispatcher.getSmsListener().onSendSmsResult(token + 1, messageRef,
+                ImsSmsImplBase.SEND_STATUS_ERROR_RETRY, 0, SmsResponse.NO_ERROR_CODE);
+        waitForMs(SMSDispatcher.SEND_RETRY_DELAY + 200);
+        processAllMessages();
+
+        // Make sure tpmr value is same and retry bit set
+        ArgumentCaptor<byte[]> byteCaptor = ArgumentCaptor.forClass(byte[].class);
+        verify(mImsManager).sendSms(eq(token + 2), eq(messageRef), eq(SmsMessage.FORMAT_3GPP),
+                nullable(String.class), eq(true), byteCaptor.capture());
+        byte[] pdu = byteCaptor.getValue();
+        assertEquals(messageRef, pdu[1]);
+        assertEquals(0x04, (pdu[0] & 0x04));
+    }
+
+    @Test
+    public void testErrorImsRetrywithRetryConfig() throws Exception {
+        int token = mImsSmsDispatcher.mNextToken.get();
+        int messageRef = mImsSmsDispatcher.nextMessageRef() + 1;
+        mContextFixture.getCarrierConfigBundle().putInt(CarrierConfigManager.ImsSms
+                                                .KEY_SMS_OVER_IMS_SEND_RETRY_DELAY_MILLIS_INT,
+                                                3000);
+        mContextFixture.getCarrierConfigBundle().putInt(CarrierConfigManager.ImsSms
+                                                .KEY_SMS_MAX_RETRY_COUNT_OVER_IMS_INT, 2);
+        mContextFixture.getCarrierConfigBundle().putInt(CarrierConfigManager.ImsSms
+                                                .KEY_SMS_MAX_RETRY_COUNT_INT, 3);
+        when(mImsManager.getSmsFormat()).thenReturn(SmsMessage.FORMAT_3GPP);
+        when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM);
+        doReturn(mSmsUsageMonitor).when(mSmsDispatchersController).getUsageMonitor();
+        mImsSmsDispatcher.sendText("+15555551212", null, "Retry test",
+                null, null, null, null, false,
+                -1, false, -1, false, 0);
+        verify(mImsManager).sendSms(eq(token + 1), eq(messageRef), eq(SmsMessage.FORMAT_3GPP),
+                nullable(String.class), eq(false), (byte[]) any());
+        assertEquals(2, mImsSmsDispatcher.getMaxRetryCountOverIms());
+        assertEquals(3, mImsSmsDispatcher.getMaxSmsRetryCount());
+        assertEquals(3000, mImsSmsDispatcher.getSmsRetryDelayValue());
+        // Retry over IMS
+        mImsSmsDispatcher.getSmsListener().onSendSmsResult(token + 1, messageRef,
+                ImsSmsImplBase.SEND_STATUS_ERROR_RETRY, 0, SmsResponse.NO_ERROR_CODE);
+        waitForMs(mImsSmsDispatcher.getSmsRetryDelayValue() + 200);
+        processAllMessages();
+
+        // Make sure tpmr value is same and retry bit set
+        ArgumentCaptor<byte[]> byteCaptor = ArgumentCaptor.forClass(byte[].class);
+        verify(mImsManager).sendSms(eq(token + 2), eq(messageRef), eq(SmsMessage.FORMAT_3GPP),
+                nullable(String.class), eq(true), byteCaptor.capture());
+        byte[] pdu = byteCaptor.getValue();
+        assertEquals(messageRef, pdu[1]);
+        assertEquals(0x04, (pdu[0] & 0x04));
+        // Retry over IMS for the second time
+        mImsSmsDispatcher.getSmsListener().onSendSmsResult(token + 2, messageRef,
+                ImsSmsImplBase.SEND_STATUS_ERROR_RETRY, 0, SmsResponse.NO_ERROR_CODE);
+        waitForMs(mImsSmsDispatcher.getSmsRetryDelayValue() + 200);
+        processAllMessages();
+        // Make sure tpmr value is same and retry bit set
+        ArgumentCaptor<byte[]> byteCaptor2 = ArgumentCaptor.forClass(byte[].class);
+        verify(mImsManager).sendSms(eq(token + 3), eq(messageRef), eq(SmsMessage.FORMAT_3GPP),
+                nullable(String.class), eq(true), byteCaptor2.capture());
+        byte[] pdu2 = byteCaptor2.getValue();
+        assertEquals(messageRef, pdu2[1]);
+        assertEquals(0x04, (pdu2[0] & 0x04));
+
+        mImsSmsDispatcher.getSmsListener().onSendSmsResult(token + 3, messageRef,
+                ImsSmsImplBase.SEND_STATUS_ERROR_RETRY, 0, SmsResponse.NO_ERROR_CODE);
+        waitForMs(mImsSmsDispatcher.getSmsRetryDelayValue() + 200);
+        processAllMessages();
+        // Make sure tpmr value is same and retry bit set
+        ArgumentCaptor<SMSDispatcher.SmsTracker> captor =
+                ArgumentCaptor.forClass(SMSDispatcher.SmsTracker.class);
+        // Ensure GsmSmsDispatcher calls sendSms
+        verify(mSmsDispatchersController).sendRetrySms(captor.capture());
+
+        assertNotNull(captor.getValue());
+        assertTrue(captor.getValue().mRetryCount > 0);
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/MultiSimSettingControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/MultiSimSettingControllerTest.java
index fb7b64d..f5d617e 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/MultiSimSettingControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/MultiSimSettingControllerTest.java
@@ -25,6 +25,7 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyBoolean;
@@ -80,37 +81,54 @@
     private MultiSimSettingController mMultiSimSettingControllerUT;
     private Phone[] mPhones;
     private ParcelUuid mGroupUuid1 = new ParcelUuid(UUID.randomUUID());
+    private CarrierConfigManager.CarrierConfigChangeListener mCarrierConfigChangeListener;
 
     // Mocked classes
     private SubscriptionController mSubControllerMock;
+    private ISub mMockedIsub;
     private Phone mPhoneMock1;
     private Phone mPhoneMock2;
     private DataSettingsManager mDataSettingsManagerMock1;
     private DataSettingsManager mDataSettingsManagerMock2;
     private CommandsInterface mMockCi;
 
+    private final SubscriptionInfo mSubInfo1 = new SubscriptionInfo.Builder()
+            .setId(1)
+            .setIccId("subInfo1 IccId")
+            .setSimSlotIndex(0)
+            .setDisplayName("T-mobile")
+            .setCarrierName("T-mobile")
+            .setDisplayNameSource(SubscriptionManager.NAME_SOURCE_CARRIER)
+            .setIconTint(255)
+            .setNumber("12345")
+            .setMcc("310")
+            .setMnc("260")
+            .setCountryIso("us")
+            .build();
 
-    private final SubscriptionInfo mSubInfo1 = new SubscriptionInfo(1, "subInfo1 IccId", 0,
-            "T-mobile", "T-mobile", 0, 255, "12345", 0, null, "310", "260",
-            "156", false, null, null);
+    private SubscriptionInfo mSubInfo2 = new SubscriptionInfo.Builder(mSubInfo1)
+            .setId(2)
+            .setIccId("subInfo2 IccId")
+            .setGroupUuid(mGroupUuid1.toString())
+            .build();
 
-    private final SubscriptionInfo mSubInfo2 = new SubscriptionInfo(2, "subInfo2 IccId", 1,
-            "T-mobile", "T-mobile", 0, 255, "12345", 0, null, "310", "260",
-            "156", false, null, null, -1, false, mGroupUuid1.toString(), false,
-            TelephonyManager.UNKNOWN_CARRIER_ID, SubscriptionManager.PROFILE_CLASS_DEFAULT,
-            SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM, null, null, true, -1);
+    private SubscriptionInfo mSubInfo3 = new SubscriptionInfo.Builder(mSubInfo1)
+            .setId(3)
+            .setIccId("subInfo3 IccId")
+            .setGroupUuid(mGroupUuid1.toString())
+            .build();
 
-    private final SubscriptionInfo mSubInfo3 = new SubscriptionInfo(3, "subInfo3 IccId", -1,
-            "T-mobile", "T-mobile", 0, 255, "12345", 0, null, "310", "260",
-            "156", false, null, null, -1, false, mGroupUuid1.toString(), false,
-            TelephonyManager.UNKNOWN_CARRIER_ID, SubscriptionManager.PROFILE_CLASS_DEFAULT,
-            SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM, null, null, true, -1);
+    private SubscriptionInfo mSubInfo4 = new SubscriptionInfo.Builder(mSubInfo1)
+            .setId(4)
+            .setIccId("subInfo4 IccId")
+            .setGroupUuid(mGroupUuid1.toString())
+            .build();
 
-    private final SubscriptionInfo mSubInfo4 = new SubscriptionInfo(4, "subInfo4 IccId", -1,
-            "T-mobile", "T-mobile", 0, 255, "12345", 0, null, "310", "260",
-            "156", false, null, null, -1, false, mGroupUuid1.toString(), false,
-            TelephonyManager.UNKNOWN_CARRIER_ID, SubscriptionManager.PROFILE_CLASS_DEFAULT,
-            SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM, null, null, true, -1);
+    private void sendCarrierConfigChanged(int phoneId, int subId) {
+        mCarrierConfigChangeListener.onCarrierConfigChanged(phoneId, subId,
+                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
+    }
+
 
     @Before
     public void setUp() throws Exception {
@@ -121,6 +139,12 @@
         mDataSettingsManagerMock1 = mock(DataSettingsManager.class);
         mDataSettingsManagerMock2 = mock(DataSettingsManager.class);
         mMockCi = mock(CommandsInterface.class);
+        mMockedIsub = mock(ISub.class);
+
+        doReturn(mMockedIsub).when(mIBinder).queryLocalInterface(anyString());
+        doReturn(mPhone).when(mPhone).getImsPhone();
+        mServiceManagerMockedServices.put("isub", mIBinder);
+
         // Default configuration:
         // DSDS device.
         // Sub 1 is the default sub.
@@ -128,13 +152,24 @@
         doReturn(DUAL_SIM).when(mTelephonyManager).getPhoneCount();
         doReturn(DUAL_SIM).when(mTelephonyManager).getActiveModemCount();
         doReturn(1).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(1).when(mMockedIsub).getDefaultDataSubId();
         doReturn(1).when(mSubControllerMock).getDefaultVoiceSubId();
+        doReturn(1).when(mMockedIsub).getDefaultVoiceSubId();
         doReturn(1).when(mSubControllerMock).getDefaultSmsSubId();
+        doReturn(1).when(mMockedIsub).getDefaultSmsSubId();
         doReturn(true).when(mSubControllerMock).isActiveSubId(1);
+        doReturn(new SubscriptionInfo.Builder().setId(1).setSimSlotIndex(0).build())
+                .when(mSubControllerMock).getSubscriptionInfo(1);
         doReturn(true).when(mSubControllerMock).isActiveSubId(2);
+        doReturn(new SubscriptionInfo.Builder().setId(2).setSimSlotIndex(1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(2);
         doReturn(0).when(mSubControllerMock).getPhoneId(1);
+        doReturn(0).when(mMockedIsub).getPhoneId(1);
         doReturn(1).when(mSubControllerMock).getPhoneId(2);
+        doReturn(1).when(mMockedIsub).getPhoneId(2);
         doReturn(true).when(mSubControllerMock).isOpportunistic(5);
+        doReturn(new SubscriptionInfo.Builder().setId(5).setSimSlotIndex(1).setOpportunistic(true)
+                .build()).when(mSubControllerMock).getSubscriptionInfo(5);
         doReturn(1).when(mPhoneMock1).getSubId();
         doReturn(2).when(mPhoneMock2).getSubId();
         mPhoneMock1.mCi = mSimulatedCommands;
@@ -159,8 +194,15 @@
 
         replaceInstance(PhoneFactory.class, "sPhones", null, mPhones);
         replaceInstance(SubscriptionController.class, "sInstance", null, mSubControllerMock);
+        // Capture listener to emulate the carrier config change notification used later
+        ArgumentCaptor<CarrierConfigManager.CarrierConfigChangeListener> listenerArgumentCaptor =
+                ArgumentCaptor.forClass(CarrierConfigManager.CarrierConfigChangeListener.class);
         mMultiSimSettingControllerUT = new MultiSimSettingController(mContext, mSubControllerMock);
         processAllMessages();
+        verify(mCarrierConfigManager).registerCarrierConfigChangeListener(any(),
+                listenerArgumentCaptor.capture());
+        mCarrierConfigChangeListener = listenerArgumentCaptor.getAllValues().get(0);
+        assertNotNull(mCarrierConfigChangeListener);
     }
 
     @After
@@ -183,7 +225,10 @@
 
         // Mark sub 2 as inactive.
         doReturn(false).when(mSubControllerMock).isActiveSubId(2);
+        doReturn(new SubscriptionInfo.Builder().setId(1).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(2);
         doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(2);
+        doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mMockedIsub).getPhoneId(2);
         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mPhoneMock2).getSubId();
         List<SubscriptionInfo> infoList = Arrays.asList(mSubInfo1);
         doReturn(infoList).when(mSubControllerMock).getActiveSubscriptionInfoList(anyString(),
@@ -198,7 +243,7 @@
         verify(mSubControllerMock, never()).setDefaultSmsSubId(anyInt());
 
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(0, 1);
         processAllMessages();
 
         // Sub 1 should be default sub silently.
@@ -211,8 +256,8 @@
     @Test
     public void testSubInfoChangeAfterRadioUnavailable() throws Exception {
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
 
         // Ensure all subscription loaded only updates state once
@@ -231,9 +276,15 @@
 
         // Mark all subs as inactive.
         doReturn(false).when(mSubControllerMock).isActiveSubId(1);
+        doReturn(new SubscriptionInfo.Builder().setId(1).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(1);
         doReturn(false).when(mSubControllerMock).isActiveSubId(2);
+        doReturn(new SubscriptionInfo.Builder().setId(2).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(2);
         doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(1);
+        doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mMockedIsub).getPhoneId(1);
         doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(2);
+        doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mMockedIsub).getPhoneId(2);
         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mPhoneMock1).getSubId();
         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mPhoneMock2).getSubId();
         List<SubscriptionInfo> infoList = new ArrayList<>();
@@ -272,6 +323,8 @@
 
         // Mark sub 2 as inactive.
         doReturn(false).when(mSubControllerMock).isActiveSubId(2);
+        doReturn(new SubscriptionInfo.Builder().setId(2).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(2);
         doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(2);
         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mPhoneMock2).getSubId();
         List<SubscriptionInfo> infoList = Arrays.asList(mSubInfo1);
@@ -280,7 +333,7 @@
         doReturn(new int[]{1}).when(mSubControllerMock).getActiveSubIdList(anyBoolean());
 
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(0, 1);
         processAllMessages();
         verifyDismissIntentSent();
         clearInvocations(mContext);
@@ -288,9 +341,15 @@
         // Sub 1 should be default sub silently.
         // Sub 1 switches to sub 2 in the same slot.
         doReturn(false).when(mSubControllerMock).isActiveSubId(1);
+        doReturn(new SubscriptionInfo.Builder().setId(1).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(1);
         doReturn(true).when(mSubControllerMock).isActiveSubId(2);
+        doReturn(new SubscriptionInfo.Builder().setId(2).setSimSlotIndex(0).build())
+                .when(mSubControllerMock).getSubscriptionInfo(2);
         doReturn(0).when(mSubControllerMock).getPhoneId(2);
+        doReturn(0).when(mMockedIsub).getPhoneId(2);
         doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(1);
+        doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mMockedIsub).getPhoneId(1);
         doReturn(2).when(mPhoneMock1).getSubId();
         infoList = Arrays.asList(mSubInfo2);
         doReturn(infoList).when(mSubControllerMock).getActiveSubscriptionInfoList(anyString(),
@@ -298,7 +357,7 @@
         doReturn(new int[]{2}).when(mSubControllerMock).getActiveSubIdList(anyBoolean());
 
         mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 2);
+        sendCarrierConfigChanged(0, 2);
         processAllMessages();
 
         // Sub 1 should be default sub silently.
@@ -313,7 +372,10 @@
     public void testActivatingSecondSub() throws Exception {
         // Mark sub 2 as inactive.
         doReturn(false).when(mSubControllerMock).isActiveSubId(2);
+        doReturn(new SubscriptionInfo.Builder().setId(1).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(2);
         doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(2);
+        doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mMockedIsub).getPhoneId(1);
         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mPhoneMock2).getSubId();
         List<SubscriptionInfo> infoList = Arrays.asList(mSubInfo1);
         doReturn(infoList).when(mSubControllerMock).getActiveSubscriptionInfoList(anyString(),
@@ -321,7 +383,7 @@
         doReturn(new int[]{1}).when(mSubControllerMock).getActiveSubIdList(anyBoolean());
 
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(0, 1);
         processAllMessages();
 
         // Sub 1 should be default sub silently.
@@ -334,7 +396,10 @@
         clearInvocations(mSubControllerMock);
         clearInvocations(mContext);
         doReturn(true).when(mSubControllerMock).isActiveSubId(2);
+        doReturn(new SubscriptionInfo.Builder().setId(2).setSimSlotIndex(1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(2);
         doReturn(1).when(mSubControllerMock).getPhoneId(2);
+        doReturn(1).when(mMockedIsub).getPhoneId(2);
         doReturn(2).when(mPhoneMock2).getSubId();
         infoList = Arrays.asList(mSubInfo1, mSubInfo2);
         doReturn(infoList).when(mSubControllerMock).getActiveSubscriptionInfoList(anyString(),
@@ -342,7 +407,7 @@
         doReturn(new int[]{1, 2}).when(mSubControllerMock).getActiveSubIdList(anyBoolean());
 
         mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
 
         // Intent should be broadcast to ask default data selection.
@@ -355,8 +420,13 @@
         // Switch from sub 2 to sub 3 in phone[1]. This should again trigger default data selection
         // dialog.
         doReturn(false).when(mSubControllerMock).isActiveSubId(2);
+        doReturn(new SubscriptionInfo.Builder().setId(2).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(2);
         doReturn(true).when(mSubControllerMock).isActiveSubId(3);
+        doReturn(new SubscriptionInfo.Builder().setId(3).setSimSlotIndex(1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(3);
         doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(2);
+        doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mMockedIsub).getPhoneId(2);
         doReturn(1).when(mSubControllerMock).getPhoneId(3);
         doReturn(3).when(mPhoneMock2).getSubId();
         infoList = Arrays.asList(mSubInfo1, mSubInfo3);
@@ -365,7 +435,7 @@
         doReturn(new int[]{1, 3}).when(mSubControllerMock).getActiveSubIdList(anyBoolean());
 
         mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 3);
+        sendCarrierConfigChanged(1, 3);
         processAllMessages();
 
         // Intent should be broadcast to ask default data selection.
@@ -377,13 +447,13 @@
 
     @Test
     @SmallTest
-    public void testSimpleDsds() {
+    public void testSimpleDsds() throws Exception {
         doReturn(true).when(mPhoneMock1).isUserDataEnabled();
         doReturn(true).when(mPhoneMock2).isUserDataEnabled();
         // After initialization, sub 2 should have mobile data off.
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
         verify(mDataSettingsManagerMock2).setDataEnabled(
                 TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
@@ -395,6 +465,7 @@
 
         // Changing default data to sub 2 should trigger disabling data on sub 1.
         doReturn(2).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(2).when(mMockedIsub).getDefaultDataSubId();
         mMultiSimSettingControllerUT.notifyDefaultDataSubChanged();
         processAllMessages();
         verify(mDataSettingsManagerMock1).setDataEnabled(
@@ -407,14 +478,17 @@
         // Taking out SIM 1.
         clearInvocations(mSubControllerMock);
         doReturn(false).when(mSubControllerMock).isActiveSubId(1);
+        doReturn(new SubscriptionInfo.Builder().setId(1).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(1);
         List<SubscriptionInfo> infoList = Arrays.asList(mSubInfo2);
         doReturn(infoList).when(mSubControllerMock).getActiveSubscriptionInfoList(anyString(),
                 nullable(String.class));
         doReturn(new int[]{2}).when(mSubControllerMock).getActiveSubIdList(anyBoolean());
         mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(
-                1, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
         processAllMessages();
+        sendCarrierConfigChanged(1, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        processAllMessages();
+
         verify(mSubControllerMock).setDefaultDataSubId(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
         verify(mSubControllerMock).setDefaultSmsSubId(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
         verify(mSubControllerMock, never()).setDefaultVoiceSubId(anyInt());
@@ -429,16 +503,17 @@
 
     @Test
     @SmallTest
-    public void testSimpleDsdsFirstBoot() {
+    public void testSimpleDsdsFirstBoot() throws Exception {
         // at first boot default is not set
         doReturn(-1).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(-1).when(mMockedIsub).getDefaultDataSubId();
 
         doReturn(true).when(mPhoneMock1).isUserDataEnabled();
         doReturn(true).when(mPhoneMock2).isUserDataEnabled();
         // After initialization, sub 2 should have mobile data off.
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
         verify(mDataSettingsManagerMock1).setDataEnabled(
                 TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
@@ -456,6 +531,7 @@
 
         // Setting default data should not trigger any more setDataEnabled().
         doReturn(2).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(2).when(mMockedIsub).getDefaultDataSubId();
         mMultiSimSettingControllerUT.notifyDefaultDataSubChanged();
         processAllMessages();
         verify(mDataSettingsManagerMock1, times(1))
@@ -466,9 +542,10 @@
 
     @Test
     @SmallTest
-    public void testSimpleDsdsInSuW() {
+    public void testSimpleDsdsInSuW() throws Exception {
         // at first boot default is not set
         doReturn(-1).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(-1).when(mMockedIsub).getDefaultDataSubId();
 
         doReturn(true).when(mPhoneMock1).isUserDataEnabled();
         doReturn(true).when(mPhoneMock2).isUserDataEnabled();
@@ -477,8 +554,8 @@
                 Settings.Global.DEVICE_PROVISIONED, 0);
         // After initialization, sub 2 should have mobile data off.
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
         verify(mDataSettingsManagerMock1).setDataEnabled(
                 TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
@@ -495,21 +572,24 @@
 
     @Test
     @SmallTest
-    public void testDsdsGrouping() {
+    public void testDsdsGrouping() throws Exception {
         doReturn(2).when(mSubControllerMock).getDefaultDataSubId();
         doReturn(false).when(mPhoneMock1).isUserDataEnabled();
         doReturn(true).when(mPhoneMock2).isUserDataEnabled();
         GlobalSettingsHelper.setBoolean(mContext, Settings.Global.MOBILE_DATA, 2, true);
         GlobalSettingsHelper.setBoolean(mContext, Settings.Global.DATA_ROAMING, 2, false);
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
 
         // Create subscription grouping.
         doReturn(mGroupUuid1).when(mSubControllerMock).getGroupUuid(2);
         doReturn(mGroupUuid1).when(mSubControllerMock).getGroupUuid(3);
         doReturn(mGroupUuid1).when(mSubControllerMock).getGroupUuid(4);
+        mSubInfo2 = new SubscriptionInfo.Builder(mSubInfo2).setSimSlotIndex(1).build();
+        mSubInfo3 = new SubscriptionInfo.Builder(mSubInfo3).setSimSlotIndex(-1).build();
+        mSubInfo4 = new SubscriptionInfo.Builder(mSubInfo4).setSimSlotIndex(-1).build();
         doReturn(Arrays.asList(mSubInfo2, mSubInfo3, mSubInfo4)).when(mSubControllerMock)
                 .getSubscriptionsInGroup(any(), anyString(), nullable(String.class));
         mMultiSimSettingControllerUT.notifySubscriptionGroupChanged(mGroupUuid1);
@@ -529,6 +609,7 @@
 
         // Making sub 1 default data sub should result in disabling data on sub 2, 3, 4.
         doReturn(1).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(1).when(mMockedIsub).getDefaultDataSubId();
         mMultiSimSettingControllerUT.notifyDefaultDataSubChanged();
         processAllMessages();
         verify(mDataSettingsManagerMock2).setDataEnabled(
@@ -546,15 +627,18 @@
         // Default data and default sms should become subscription 3.
         clearInvocations(mSubControllerMock);
         doReturn(2).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(2).when(mMockedIsub).getDefaultDataSubId();
         doReturn(2).when(mSubControllerMock).getDefaultSmsSubId();
+        doReturn(2).when(mMockedIsub).getDefaultSmsSubId();
         doReturn(1).when(mSubControllerMock).getDefaultVoiceSubId();
+        doReturn(1).when(mMockedIsub).getDefaultVoiceSubId();
         List<SubscriptionInfo> infoList = Arrays.asList(mSubInfo1, mSubInfo3);
         doReturn(infoList).when(mSubControllerMock).getActiveSubscriptionInfoList(anyString(),
                 nullable(String.class));
         doReturn(new int[]{1, 3}).when(mSubControllerMock).getActiveSubIdList(anyBoolean());
 
         mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 3);
+        sendCarrierConfigChanged(1, 3);
         processAllMessages();
 
         verify(mSubControllerMock).setDefaultDataSubId(3);
@@ -569,6 +653,8 @@
     public void testCbrs() throws Exception {
         replaceInstance(SubscriptionInfo.class, "mIsOpportunistic", mSubInfo1, true);
         doReturn(true).when(mSubControllerMock).isOpportunistic(1);
+        doReturn(new SubscriptionInfo.Builder(mSubInfo1).setOpportunistic(true).build())
+                .when(mSubControllerMock).getSubscriptionInfo(1);
         doReturn(true).when(mPhoneMock1).isUserDataEnabled();
         doReturn(true).when(mPhoneMock2).isUserDataEnabled();
         GlobalSettingsHelper.setBoolean(mContext, Settings.Global.DATA_ROAMING, 2, false);
@@ -576,8 +662,8 @@
         // Notify subscriptions ready. Sub 2 should become the default. But shouldn't turn off
         // data of oppt sub 1.
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
         verify(mSubControllerMock).setDefaultDataSubId(2);
         verify(mDataSettingsManagerMock1, never()).setDataEnabled(
@@ -588,6 +674,7 @@
         clearInvocations(mDataSettingsManagerMock1);
         clearInvocations(mDataSettingsManagerMock2);
         doReturn(2).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(2).when(mMockedIsub).getDefaultDataSubId();
         // Toggle data on sub 1 or sub 2. Nothing should happen as they are independent.
         mMultiSimSettingControllerUT.notifyUserDataEnabled(1, false);
         mMultiSimSettingControllerUT.notifyUserDataEnabled(1, true);
@@ -615,8 +702,10 @@
     public void testGroupedCbrs() throws Exception {
         // Mark sub 1 as opportunistic.
         replaceInstance(SubscriptionInfo.class, "mIsOpportunistic", mSubInfo1, true);
-        replaceInstance(SubscriptionInfo.class, "mGroupUUID", mSubInfo1, mGroupUuid1);
+        replaceInstance(SubscriptionInfo.class, "mGroupUuid", mSubInfo1, mGroupUuid1);
         doReturn(true).when(mSubControllerMock).isOpportunistic(1);
+        doReturn(new SubscriptionInfo.Builder(mSubInfo1).setOpportunistic(true).build())
+                .when(mSubControllerMock).getSubscriptionInfo(1);
         // Make opportunistic sub 1 and sub 2 data enabled.
         doReturn(true).when(mPhoneMock1).isUserDataEnabled();
         doReturn(true).when(mPhoneMock2).isUserDataEnabled();
@@ -624,8 +713,8 @@
 
         // Notify subscriptions ready. Sub 2 should become the default, as sub 1 is opportunistic.
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
         verify(mSubControllerMock).setDefaultDataSubId(2);
 
@@ -657,7 +746,7 @@
     @SmallTest
     public void testGroupedPrimaryRemoved() throws Exception {
         // Create subscription grouping of subs 1 and 2.
-        replaceInstance(SubscriptionInfo.class, "mGroupUUID", mSubInfo1, mGroupUuid1);
+        replaceInstance(SubscriptionInfo.class, "mGroupUuid", mSubInfo1, mGroupUuid1);
         doReturn(mGroupUuid1).when(mSubControllerMock).getGroupUuid(1);
         doReturn(mGroupUuid1).when(mSubControllerMock).getGroupUuid(2);
         doReturn(Arrays.asList(mSubInfo1, mSubInfo2)).when(mSubControllerMock)
@@ -665,8 +754,8 @@
 
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
         mMultiSimSettingControllerUT.notifySubscriptionGroupChanged(mGroupUuid1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
 
         // Defaults not touched, sub 1 is already default.
@@ -675,15 +764,17 @@
         // Take out SIM 1.
         clearInvocations(mSubControllerMock);
         doReturn(false).when(mSubControllerMock).isActiveSubId(1);
+        doReturn(new SubscriptionInfo.Builder().setId(1).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(1);
         doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(1);
+        doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mMockedIsub).getPhoneId(1);
         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mPhoneMock1).getSubId();
         List<SubscriptionInfo> infoList = Arrays.asList(mSubInfo2);
         doReturn(infoList).when(mSubControllerMock).getActiveSubscriptionInfoList(anyString(),
                 nullable(String.class));
         doReturn(new int[]{2}).when(mSubControllerMock).getActiveSubIdList(anyBoolean());
         mMultiSimSettingControllerUT.notifySubscriptionInfoChanged();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(
-                0, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        sendCarrierConfigChanged(0, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
         processAllMessages();
 
         // Sub 2 should be made the default sub silently.
@@ -703,17 +794,18 @@
     @SmallTest
     public void testGroupedPrimarySubscriptions() throws Exception {
         doReturn(1).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(1).when(mMockedIsub).getDefaultDataSubId();
         doReturn(true).when(mPhoneMock1).isUserDataEnabled();
         doReturn(false).when(mPhoneMock2).isUserDataEnabled();
         GlobalSettingsHelper.setBoolean(mContext, Settings.Global.MOBILE_DATA, 1, true);
         GlobalSettingsHelper.setBoolean(mContext, Settings.Global.DATA_ROAMING, 1, false);
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
 
         // Create subscription grouping.
-        replaceInstance(SubscriptionInfo.class, "mGroupUUID", mSubInfo1, mGroupUuid1);
+        replaceInstance(SubscriptionInfo.class, "mGroupUuid", mSubInfo1, mGroupUuid1);
         doReturn(Arrays.asList(mSubInfo1, mSubInfo2)).when(mSubControllerMock)
                 .getSubscriptionsInGroup(any(), anyString(), nullable(String.class));
         mMultiSimSettingControllerUT.notifySubscriptionGroupChanged(mGroupUuid1);
@@ -744,11 +836,11 @@
         processAllMessages();
         verify(mDataSettingsManagerMock2, never()).setDataEnabled(
                 TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(0, 1);
         processAllMessages();
         verify(mDataSettingsManagerMock2, never()).setDataEnabled(
                 TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
         verify(mDataSettingsManagerMock2).setDataEnabled(
                 TelephonyManager.DATA_ENABLED_REASON_USER, false, PHONE_PACKAGE);
@@ -756,9 +848,15 @@
         // Switch from sub 2 to sub 3 in phone[1].
         clearInvocations(mSubControllerMock);
         doReturn(false).when(mSubControllerMock).isActiveSubId(2);
+        doReturn(new SubscriptionInfo.Builder().setId(2).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(2);
         doReturn(true).when(mSubControllerMock).isActiveSubId(3);
+        doReturn(new SubscriptionInfo.Builder().setId(3).setSimSlotIndex(1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(3);
         doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(2);
+        doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mMockedIsub).getPhoneId(2);
         doReturn(1).when(mSubControllerMock).getPhoneId(3);
+        doReturn(1).when(mMockedIsub).getPhoneId(3);
         doReturn(3).when(mPhoneMock2).getSubId();
         List<SubscriptionInfo> infoList = Arrays.asList(mSubInfo1, mSubInfo3);
         doReturn(infoList).when(mSubControllerMock).getActiveSubscriptionInfoList(anyString(),
@@ -770,7 +868,7 @@
         processAllMessages();
         verify(mContext, never()).sendBroadcast(any());
 
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 3);
+        sendCarrierConfigChanged(1, 3);
         processAllMessages();
         // Intent should be broadcast to ask default data selection.
         Intent intent = captureBroadcastIntent();
@@ -785,9 +883,14 @@
     public void testGroupChangeOnInactiveSub_shouldNotMarkAsDefaultDataSub() throws Exception {
         // Make sub1 and sub3 as active sub.
         doReturn(false).when(mSubControllerMock).isActiveSubId(2);
+        doReturn(new SubscriptionInfo.Builder().setId(2).setSimSlotIndex(-1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(2);
         doReturn(true).when(mSubControllerMock).isActiveSubId(3);
+        doReturn(new SubscriptionInfo.Builder().setId(3).setSimSlotIndex(1).build())
+                .when(mSubControllerMock).getSubscriptionInfo(3);
         doReturn(SubscriptionManager.INVALID_PHONE_INDEX).when(mSubControllerMock).getPhoneId(2);
         doReturn(1).when(mSubControllerMock).getPhoneId(3);
+        doReturn(1).when(mMockedIsub).getPhoneId(3);
         doReturn(3).when(mPhoneMock2).getSubId();
         List<SubscriptionInfo> infoList = Arrays.asList(mSubInfo1, mSubInfo3);
         doReturn(infoList).when(mSubControllerMock).getActiveSubscriptionInfoList(anyString(),
@@ -798,6 +901,7 @@
 
         // Sub 3 and sub 2's mobile data are enabled, and sub 3 is the default data sub.
         doReturn(3).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(3).when(mMockedIsub).getDefaultDataSubId();
         GlobalSettingsHelper.setBoolean(mContext, Settings.Global.MOBILE_DATA, 1, false);
         GlobalSettingsHelper.setBoolean(mContext, Settings.Global.MOBILE_DATA, 2, true);
         GlobalSettingsHelper.setBoolean(mContext, Settings.Global.MOBILE_DATA, 3, true);
@@ -806,12 +910,14 @@
         // Sub 2 should have mobile data off, but it shouldn't happen until carrier configs are
         // loaded on both subscriptions.
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 3);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 3);
         processAllMessages();
 
         // Mark sub3 as oppt and notify grouping
         doReturn(true).when(mSubControllerMock).isOpportunistic(3);
+        doReturn(new SubscriptionInfo.Builder().setId(3).setSimSlotIndex(0).setOpportunistic(true)
+                .build()).when(mSubControllerMock).getSubscriptionInfo(3);
         mMultiSimSettingControllerUT.notifySubscriptionGroupChanged(mGroupUuid1);
         processAllMessages();
         // Shouldn't mark sub 2 as default data, as sub 2 is in active.
@@ -827,10 +933,9 @@
         doReturn(true).when(mPhoneMock2).isUserDataEnabled();
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
         processAllMessages();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(0, 1);
         // Notify carrier config change on phone1 without specifying subId.
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1,
-                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        sendCarrierConfigChanged(1, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
         processAllMessages();
         // Nothing should happen as carrier config is not ready for sub 2.
         verify(mDataSettingsManagerMock2, never()).setDataEnabled(
@@ -838,12 +943,12 @@
 
         // Still notify carrier config without specifying subId2, but this time subController
         // and CarrierConfigManager have subId 2 active and ready.
-        doReturn(new int[] {2}).when(mSubControllerMock).getSubId(1);
+        doReturn(2).when(mSubControllerMock).getSubId(1);
+        doReturn(2).when(mMockedIsub).getSubId(1);
         CarrierConfigManager cm = (CarrierConfigManager) mContext.getSystemService(
                 mContext.CARRIER_CONFIG_SERVICE);
         doReturn(new PersistableBundle()).when(cm).getConfigForSubId(2);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1,
-                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        sendCarrierConfigChanged(1, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
         processAllMessages();
         // This time user data should be disabled on phone1.
         verify(mDataSettingsManagerMock2).setDataEnabled(
@@ -891,9 +996,10 @@
     @SmallTest
     public void testVoiceDataSmsAutoFallback() throws Exception {
         doReturn(1).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(1).when(mMockedIsub).getDefaultDataSubId();
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0,1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(2, 3);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(2, 2);
         processAllMessages();
         verify(mSubControllerMock, never()).setDefaultDataSubId(anyInt());
         verify(mSubControllerMock, never()).getActiveSubInfoCountMax();
@@ -909,8 +1015,8 @@
         doReturn(true).when(resources).getBoolean(
                 com.android.internal.R.bool.config_voice_data_sms_auto_fallback);
         mMultiSimSettingControllerUT.notifyAllSubscriptionLoaded();
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(0,1);
-        mMultiSimSettingControllerUT.notifyCarrierConfigChanged(1, 2);
+        sendCarrierConfigChanged(0, 1);
+        sendCarrierConfigChanged(1, 2);
         processAllMessages();
         verify(mSubControllerMock).getActiveSubInfoCountMax();
         verify(mSubControllerMock).setDefaultDataSubId(anyInt());
@@ -932,8 +1038,9 @@
     @Test
     public void onSubscriptionGroupChanged_allActiveSubArePartOfGroup() throws Exception {
         doReturn(3).when(mSubControllerMock).getDefaultDataSubId();
+        doReturn(3).when(mMockedIsub).getDefaultDataSubId();
         // Create subscription grouping of subs 1 and 2.
-        replaceInstance(SubscriptionInfo.class, "mGroupUUID", mSubInfo1, mGroupUuid1);
+        replaceInstance(SubscriptionInfo.class, "mGroupUuid", mSubInfo1, mGroupUuid1);
         doReturn(mGroupUuid1).when(mSubControllerMock).getGroupUuid(1);
         doReturn(mGroupUuid1).when(mSubControllerMock).getGroupUuid(2);
         GlobalSettingsHelper.setBoolean(mContext, Settings.Global.MOBILE_DATA, 1, true);
diff --git a/tests/telephonytests/src/com/android/internal/telephony/NetworkRegistrationInfoTest.java b/tests/telephonytests/src/com/android/internal/telephony/NetworkRegistrationInfoTest.java
index 3d5379c..8dc8ea7 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/NetworkRegistrationInfoTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/NetworkRegistrationInfoTest.java
@@ -18,6 +18,7 @@
 
 import static junit.framework.Assert.assertEquals;
 
+import android.compat.testing.PlatformCompatChangeRule;
 import android.os.Parcel;
 import android.telephony.AccessNetworkConstants;
 import android.telephony.CellIdentityLte;
@@ -27,13 +28,21 @@
 
 import androidx.test.filters.SmallTest;
 
+import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
+import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
+
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TestRule;
 
 import java.util.Arrays;
 
 /** Unit tests for {@link NetworkRegistrationInfo}. */
 public class NetworkRegistrationInfoTest {
 
+    @Rule
+    public TestRule compatChangeRule = new PlatformCompatChangeRule();
+
     @Test
     @SmallTest
     public void testParcel() {
@@ -85,6 +94,49 @@
         nri.setRoamingType(ServiceState.ROAMING_TYPE_NOT_ROAMING);
         assertEquals(NetworkRegistrationInfo.REGISTRATION_STATE_HOME, nri.getRegistrationState());
         assertEquals(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING,
-                nri.getInitialRegistrationState());
+                nri.getNetworkRegistrationState());
+    }
+
+    @Test
+    @DisableCompatChanges({NetworkRegistrationInfo.RETURN_REGISTRATION_STATE_EMERGENCY})
+    public void testReturnRegistrationStateEmergencyDisabled() {
+        // LTE
+        NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
+                .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_EMERGENCY)
+                .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_LTE)
+                .build();
+
+        assertEquals(NetworkRegistrationInfo.REGISTRATION_STATE_DENIED, nri.getRegistrationState());
+
+        // NR
+        nri = new NetworkRegistrationInfo.Builder()
+                .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_EMERGENCY)
+                .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_NR)
+                .build();
+
+        assertEquals(NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING,
+                nri.getRegistrationState());
+    }
+
+    @Test
+    @EnableCompatChanges({NetworkRegistrationInfo.RETURN_REGISTRATION_STATE_EMERGENCY})
+    public void testReturnRegistrationStateEmergencyEnabled() {
+        // LTE
+        NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
+                .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_EMERGENCY)
+                .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_LTE)
+                .build();
+
+        assertEquals(NetworkRegistrationInfo.REGISTRATION_STATE_EMERGENCY,
+                nri.getRegistrationState());
+
+        // NR
+        nri = new NetworkRegistrationInfo.Builder()
+                .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_EMERGENCY)
+                .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_NR)
+                .build();
+
+        assertEquals(NetworkRegistrationInfo.REGISTRATION_STATE_EMERGENCY,
+                nri.getRegistrationState());
     }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/NetworkTypeControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/NetworkTypeControllerTest.java
index 20c5d87..74164a2 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/NetworkTypeControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/NetworkTypeControllerTest.java
@@ -50,7 +50,6 @@
 
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
@@ -61,21 +60,9 @@
 
 @RunWith(AndroidTestingRunner.class)
 @TestableLooper.RunWithLooper
-@Ignore("b/240911460")
 public class NetworkTypeControllerTest extends TelephonyTest {
-    // private constants copied over from NetworkTypeController
-    private static final int EVENT_DATA_RAT_CHANGED = 2;
-    private static final int EVENT_NR_STATE_CHANGED = 3;
-    private static final int EVENT_NR_FREQUENCY_CHANGED = 4;
-    private static final int EVENT_PHYSICAL_LINK_STATUS_CHANGED = 5;
-    private static final int EVENT_PHYSICAL_CHANNEL_CONFIG_NOTIF_CHANGED = 6;
-    private static final int EVENT_RADIO_OFF_OR_UNAVAILABLE = 10;
-    private static final int EVENT_PREFERRED_NETWORK_MODE_CHANGED = 11;
-    private static final int EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED = 13;
-
     private NetworkTypeController mNetworkTypeController;
     private PersistableBundle mBundle;
-    private DataNetworkControllerCallback mDataNetworkControllerCallback;
 
     private IState getCurrentState() throws Exception {
         Method method = StateMachine.class.getDeclaredMethod("getCurrentState");
@@ -104,7 +91,7 @@
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_CONFIGURATION_STRING,
                 "connected_mmwave:5G_Plus,connected:5G,not_restricted_rrc_idle:5G,"
                         + "not_restricted_rrc_con:5G");
-        mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_CAPABLE_PCO_ID_INT, 0xFF03);
+        mBundle.putInt(CarrierConfigManager.KEY_LTE_PLUS_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
         broadcastCarrierConfigs();
 
         replaceInstance(Handler.class, "mLooper", mDisplayInfoController, Looper.myLooper());
@@ -116,12 +103,6 @@
         doReturn(new int[] {0}).when(mServiceState).getCellBandwidths();
         mNetworkTypeController = new NetworkTypeController(mPhone, mDisplayInfoController);
         processAllMessages();
-
-        ArgumentCaptor<DataNetworkControllerCallback> dataNetworkControllerCallbackCaptor =
-                ArgumentCaptor.forClass(DataNetworkControllerCallback.class);
-        verify(mDataNetworkController).registerDataNetworkControllerCallback(
-                dataNetworkControllerCallbackCaptor.capture());
-        mDataNetworkControllerCallback = dataNetworkControllerCallbackCaptor.getValue();
     }
 
     @After
@@ -169,7 +150,6 @@
     @Test
     public void testUpdateOverrideNetworkTypeNrSa() throws Exception {
         // not NR
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
         updateOverrideNetworkType();
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
@@ -184,7 +164,6 @@
                 anyInt(), anyInt());
 
         updateOverrideNetworkType();
-
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
                 mNetworkTypeController.getOverrideNetworkType());
     }
@@ -192,7 +171,6 @@
     @Test
     public void testUpdateOverrideNetworkTypeNrSaMmwave() throws Exception {
         // not NR
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
         updateOverrideNetworkType();
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
@@ -208,7 +186,6 @@
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
 
         updateOverrideNetworkType();
-
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
                 mNetworkTypeController.getOverrideNetworkType());
     }
@@ -262,9 +239,7 @@
     @Test
     public void testTransitionToCurrentStateLegacy() throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_HSPAP).when(mServiceState).getDataNetworkType();
-
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("legacy", getCurrentState().getName());
     }
@@ -272,10 +247,8 @@
     @Test
     public void testTransitionToCurrentStateRestricted() throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_RESTRICTED).when(mServiceState).getNrState();
-
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("restricted", getCurrentState().getName());
     }
@@ -283,11 +256,10 @@
     @Test
     public void testTransitionToCurrentStateIdle() throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
+        mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
                 new AsyncResult(null, DataCallResponse.LINK_STATUS_DORMANT, null));
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
     }
@@ -299,11 +271,11 @@
         mNetworkTypeController = new NetworkTypeController(mPhone, mDisplayInfoController);
         processAllMessages();
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
+
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
         setPhysicalLinkStatus(false);
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED);
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED */);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
     }
@@ -318,25 +290,22 @@
         broadcastCarrierConfigs();
         processAllMessages();
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
+
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
+        mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
                 new AsyncResult(null, DataCallResponse.LINK_STATUS_DORMANT, null));
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
-
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
-
         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
     }
 
     @Test
     public void testTransitionToCurrentStateLteConnected() throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
+        mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
                 new AsyncResult(null, DataCallResponse.LINK_STATUS_ACTIVE, null));
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
     }
@@ -350,11 +319,11 @@
         broadcastCarrierConfigs();
         processAllMessages();
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
+
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
         setPhysicalLinkStatus(true);
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED);
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED */);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
     }
@@ -370,24 +339,21 @@
         broadcastCarrierConfigs();
         processAllMessages();
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
+
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
+        mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
                 new AsyncResult(null, DataCallResponse.LINK_STATUS_ACTIVE, null));
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
-
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
-
         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
     }
 
     @Test
     public void testTransitionToCurrentStateNrConnected() throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
 
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("connected", getCurrentState().getName());
     }
@@ -395,11 +361,10 @@
     @Test
     public void testTransitionToCurrentStateNrConnectedMmwave() throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
 
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("connected_mmwave", getCurrentState().getName());
     }
@@ -408,7 +373,6 @@
     public void testTransitionToCurrentStateNrConnectedMmwaveWithAdditionalBandAndNoMmwave()
             throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_HIGH).when(mServiceState).getNrFrequencyRange();
         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
@@ -422,7 +386,8 @@
         doReturn(lastPhysicalChannelConfigList).when(mSST).getPhysicalChannelConfigList();
         broadcastCarrierConfigs();
 
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED */);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("connected_mmwave", getCurrentState().getName());
     }
@@ -431,7 +396,6 @@
     public void testTransitionToCurrentStateNrConnectedWithNoAdditionalBandAndNoMmwave()
             throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_HIGH).when(mServiceState).getNrFrequencyRange();
         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
@@ -445,7 +409,8 @@
         doReturn(lastPhysicalChannelConfigList).when(mSST).getPhysicalChannelConfigList();
         broadcastCarrierConfigs();
 
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED */);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("connected", getCurrentState().getName());
     }
@@ -453,13 +418,11 @@
     @Test
     public void testTransitionToCurrentStateNrConnectedWithNrAdvancedCapable() throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
-        mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_CAPABLE_PCO_ID_INT, 0);
         broadcastCarrierConfigs();
 
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("connected_mmwave", getCurrentState().getName());
     }
@@ -468,13 +431,17 @@
     public void testTransitionToCurrentStateNrConnectedWithPcoAndNoNrAdvancedCapable()
             throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_CAPABLE_PCO_ID_INT, 0xFF03);
         broadcastCarrierConfigs();
-        mDataNetworkControllerCallback.onNrAdvancedCapableByPcoChanged(false);
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+
+        ArgumentCaptor<DataNetworkControllerCallback> dataNetworkControllerCallbackCaptor =
+                ArgumentCaptor.forClass(DataNetworkControllerCallback.class);
+        verify(mDataNetworkController).registerDataNetworkControllerCallback(
+                dataNetworkControllerCallbackCaptor.capture());
+        DataNetworkControllerCallback callback = dataNetworkControllerCallbackCaptor.getValue();
+        callback.onNrAdvancedCapableByPcoChanged(false);
         processAllMessages();
         assertEquals("connected", getCurrentState().getName());
     }
@@ -483,14 +450,17 @@
     public void testTransitionToCurrentStateNrConnectedWithWrongPcoAndNoNrAdvancedCapable()
             throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_CAPABLE_PCO_ID_INT, 0xFF00);
         broadcastCarrierConfigs();
 
-        mDataNetworkControllerCallback.onNrAdvancedCapableByPcoChanged(false);
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        ArgumentCaptor<DataNetworkControllerCallback> dataNetworkControllerCallbackCaptor =
+                ArgumentCaptor.forClass(DataNetworkControllerCallback.class);
+        verify(mDataNetworkController).registerDataNetworkControllerCallback(
+                dataNetworkControllerCallbackCaptor.capture());
+        DataNetworkControllerCallback callback = dataNetworkControllerCallbackCaptor.getValue();
+        callback.onNrAdvancedCapableByPcoChanged(false);
         processAllMessages();
         assertEquals("connected", getCurrentState().getName());
     }
@@ -499,15 +469,17 @@
     public void testTransitionToCurrentStateNrConnectedWithNrAdvancedCapableAndPco()
             throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_CAPABLE_PCO_ID_INT, 0xFF03);
         broadcastCarrierConfigs();
 
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
-        processAllMessages();
-        mDataNetworkControllerCallback.onNrAdvancedCapableByPcoChanged(true);
+        ArgumentCaptor<DataNetworkControllerCallback> dataNetworkControllerCallbackCaptor =
+                ArgumentCaptor.forClass(DataNetworkControllerCallback.class);
+        verify(mDataNetworkController).registerDataNetworkControllerCallback(
+                dataNetworkControllerCallbackCaptor.capture());
+        DataNetworkControllerCallback callback = dataNetworkControllerCallbackCaptor.getValue();
+        callback.onNrAdvancedCapableByPcoChanged(true);
         processAllMessages();
         assertEquals("connected_mmwave", getCurrentState().getName());
     }
@@ -515,10 +487,9 @@
     @Test
     public void testEventDataRatChanged() throws Exception {
         testTransitionToCurrentStateLegacy();
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
 
-        mNetworkTypeController.sendMessage(EVENT_DATA_RAT_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("connected", getCurrentState().getName());
     }
@@ -528,7 +499,7 @@
         testTransitionToCurrentStateNrConnected();
         doReturn(NetworkRegistrationInfo.NR_STATE_RESTRICTED).when(mServiceState).getNrState();
 
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("restricted", getCurrentState().getName());
     }
@@ -539,9 +510,8 @@
         testTransitionToCurrentStateNrConnectedMmwave();
         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
 
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
-
         assertEquals("connected", getCurrentState().getName());
     }
 
@@ -551,23 +521,20 @@
         testTransitionToCurrentStateNrConnected();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
 
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
-
         assertEquals("connected_mmwave", getCurrentState().getName());
     }
 
     @Test
     public void testNrPhysicalChannelChangeFromNrConnectedMmwaveToLteConnected() throws Exception {
         testTransitionToCurrentStateNrConnectedMmwave();
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
-                new AsyncResult(null, DataCallResponse.LINK_STATUS_ACTIVE, null));
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
-        processAllMessages();
 
+        mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
+                new AsyncResult(null, DataCallResponse.LINK_STATUS_ACTIVE, null));
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
+        processAllMessages();
         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
     }
 
@@ -579,15 +546,12 @@
         mNetworkTypeController = new NetworkTypeController(mPhone, mDisplayInfoController);
         processAllMessages();
         testTransitionToCurrentStateNrConnectedMmwave();
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
         setPhysicalLinkStatus(true);
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED);
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED */);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
 
         processAllMessages();
-
         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
     }
 
@@ -603,31 +567,27 @@
         broadcastCarrierConfigs();
         processAllMessages();
         testTransitionToCurrentStateNrConnectedMmwave();
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
+        mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
                 new AsyncResult(null, DataCallResponse.LINK_STATUS_ACTIVE, null));
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
 
         processAllMessages();
-
         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
     }
 
     @Test
     public void testEventPhysicalChannelChangeFromLteToLteCaInLegacyState() throws Exception {
         testTransitionToCurrentStateLegacy();
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         updateOverrideNetworkType();
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
                 mNetworkTypeController.getOverrideNetworkType());
 
         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
         doReturn(new int[] {30000}).when(mServiceState).getCellBandwidths();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED);
+        mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED */);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
-
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
                 mNetworkTypeController.getOverrideNetworkType());
     }
@@ -641,11 +601,10 @@
         broadcastCarrierConfigs();
 
         // Transition to LTE connected state
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
+        mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
                 new AsyncResult(null, DataCallResponse.LINK_STATUS_ACTIVE, null));
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
@@ -654,9 +613,9 @@
         // LTE -> LTE+
         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
         doReturn(new int[] {30000}).when(mServiceState).getCellBandwidths();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED);
+        mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED */);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
-
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
                 mNetworkTypeController.getOverrideNetworkType());
     }
@@ -670,11 +629,10 @@
         broadcastCarrierConfigs();
 
         // Transition to idle state
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
+        mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
                 new AsyncResult(null, DataCallResponse.LINK_STATUS_DORMANT, null));
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
@@ -683,9 +641,9 @@
         // LTE -> LTE+
         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
         doReturn(new int[] {30000}).when(mServiceState).getCellBandwidths();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED);
+        mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED */);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
-
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
                 mNetworkTypeController.getOverrideNetworkType());
     }
@@ -694,11 +652,10 @@
     public void testEventPhysicalLinkStatusChanged() throws Exception {
         testTransitionToCurrentStateLteConnected();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
+        mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
                 new AsyncResult(null, DataCallResponse.LINK_STATUS_DORMANT, null));
 
         processAllMessages();
-
         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
     }
 
@@ -712,10 +669,9 @@
         testTransitionToCurrentStateLteConnectedSupportPhysicalChannelConfig1_6();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         setPhysicalLinkStatus(false);
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED);
-
+        mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED */);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
-
         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
     }
 
@@ -731,11 +687,10 @@
         processAllMessages();
         testTransitionToCurrentStateLteConnected_usingUserDataForRrcDetection();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_LINK_STATUS_CHANGED,
+        mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
                 new AsyncResult(null, DataCallResponse.LINK_STATUS_DORMANT, null));
 
         processAllMessages();
-
         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
     }
 
@@ -745,7 +700,7 @@
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
 
-        mNetworkTypeController.sendMessage(EVENT_PHYSICAL_CHANNEL_CONFIG_NOTIF_CHANGED,
+        mNetworkTypeController.sendMessage(5 /* EVENT_PHYSICAL_CHANNEL_CONFIG_NOTIF_CHANGED */,
                 new AsyncResult(null, false, null));
         processAllMessages();
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
@@ -759,9 +714,7 @@
                 mNetworkTypeController.getOverrideNetworkType());
 
         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
-        doReturn(TelephonyManager.NETWORK_TYPE_UNKNOWN).when(mServiceState).getDataNetworkType();
-
-        mNetworkTypeController.sendMessage(EVENT_RADIO_OFF_OR_UNAVAILABLE);
+        mNetworkTypeController.sendMessage(9 /* EVENT_RADIO_OFF_OR_UNAVAILABLE */);
         processAllMessages();
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
                 mNetworkTypeController.getOverrideNetworkType());
@@ -778,7 +731,7 @@
                 TelephonyManager.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA)).when(
                 mPhone).getCachedAllowedNetworkTypesBitmask();
 
-        mNetworkTypeController.sendMessage(EVENT_PREFERRED_NETWORK_MODE_CHANGED);
+        mNetworkTypeController.sendMessage(10 /* EVENT_PREFERRED_NETWORK_MODE_CHANGED */);
         processAllMessages();
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
                 mNetworkTypeController.getOverrideNetworkType());
@@ -786,7 +739,6 @@
 
     @Test
     public void testPrimaryTimerExpire() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
@@ -798,13 +750,13 @@
 
         // should trigger 10 second timer
         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // timer expires
         moveTimeForward(10 * 1000);
@@ -813,12 +765,11 @@
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
     @Test
     public void testPrimaryTimerDeviceIdleMode() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
@@ -829,7 +780,7 @@
                 new Handler(Looper.myLooper()));
         doReturn(pm).when(mContext).getSystemService(Context.POWER_SERVICE);
         doReturn(true).when(powerManager).isDeviceIdleMode();
-        mNetworkTypeController.sendMessage(17 /*EVENT_DEVICE_IDLE_MODE_CHANGED*/);
+        mNetworkTypeController.sendMessage(12 /* EVENT_DEVICE_IDLE_MODE_CHANGED */);
 
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
@@ -837,18 +788,17 @@
 
         // should not trigger timer
         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
     @Test
     public void testPrimaryTimerReset() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
@@ -860,17 +810,17 @@
 
         // trigger 10 second timer after disconnecting from NR
         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // reconnect to NR in the middle of the timer
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
 
         // timer expires
         moveTimeForward(10 * 1000);
@@ -880,12 +830,11 @@
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
     @Test
     public void testPrimaryTimerReset_theNetworkModeWithoutNr() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
@@ -903,19 +852,18 @@
         // trigger 10 second timer after disconnecting from NR, and then it does the timer reset
         // since the network mode without the NR capability.
         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         // timer should be reset.
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
     @Test
     public void testPrimaryTimerExpireMmwave() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
@@ -928,13 +876,13 @@
 
         // should trigger 10 second timer
         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // timer expires
         moveTimeForward(10 * 1000);
@@ -943,12 +891,11 @@
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
     @Test
     public void testPrimaryTimerResetMmwave() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
@@ -961,17 +908,17 @@
 
         // trigger 10 second timer after disconnecting from NR
         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // reconnect to NR in the middle of the timer
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
 
         // timer expires
         moveTimeForward(10 * 1000);
@@ -981,12 +928,11 @@
         assertEquals("connected_mmwave", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
     @Test
     public void testSecondaryTimerExpire() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
@@ -1000,13 +946,13 @@
 
         // should trigger 10 second primary timer
         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // primary timer expires
         moveTimeForward(10 * 1000);
@@ -1016,7 +962,7 @@
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // secondary timer expires
         moveTimeForward(30 * 1000);
@@ -1025,12 +971,11 @@
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
     @Test
     public void testSecondaryTimerReset() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
@@ -1044,13 +989,13 @@
 
         // should trigger 10 second primary timer
         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // primary timer expires
         moveTimeForward(10 * 1000);
@@ -1060,11 +1005,11 @@
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // reconnect to NR in the middle of the timer
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_NR_STATE_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
 
         // secondary timer expires
         moveTimeForward(30 * 1000);
@@ -1074,12 +1019,11 @@
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
     @Test
     public void testSecondaryTimerExpireMmwave() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
@@ -1094,13 +1038,13 @@
 
         // should trigger 10 second primary timer
         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // primary timer expires
         moveTimeForward(10 * 1000);
@@ -1110,7 +1054,7 @@
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // secondary timer expires
         moveTimeForward(30 * 1000);
@@ -1119,12 +1063,11 @@
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
     @Test
     public void testSecondaryTimerResetMmwave() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
@@ -1139,13 +1082,13 @@
 
         // should trigger 10 second primary timer
         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // primary timer expires
         moveTimeForward(10 * 1000);
@@ -1155,11 +1098,11 @@
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // reconnect to NR in the middle of the timer
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
 
         // secondary timer expires
         moveTimeForward(30 * 1000);
@@ -1169,12 +1112,11 @@
         assertEquals("connected_mmwave", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
     @Test
     public void testNrTimerResetIn3g() throws Exception {
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
@@ -1189,13 +1131,13 @@
 
         // should trigger 10 second primary timer
         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
-        mNetworkTypeController.sendMessage(EVENT_NR_FREQUENCY_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("connected", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertTrue(mNetworkTypeController.is5GHysteresisActive());
+        assertTrue(mNetworkTypeController.areAnyTimersActive());
 
         // rat is UMTS, should stop timer
         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
@@ -1204,21 +1146,21 @@
                 .build();
         doReturn(nri).when(mServiceState).getNetworkRegistrationInfo(anyInt(), anyInt());
         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
-        mNetworkTypeController.sendMessage(EVENT_DATA_RAT_CHANGED);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
 
         assertEquals("legacy", getCurrentState().getName());
         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
                 mNetworkTypeController.getOverrideNetworkType());
-        assertFalse(mNetworkTypeController.is5GHysteresisActive());
+        assertFalse(mNetworkTypeController.areAnyTimersActive());
     }
 
-    private void setPhysicalLinkStatus(Boolean state) {
+    private void setPhysicalLinkStatus(boolean state) {
         List<PhysicalChannelConfig> lastPhysicalChannelConfigList = new ArrayList<>();
-        // If PhysicalChannelConfigList is empty, PhysicalLinkStatus is DcController
-        // .PHYSICAL_LINK_NOT_ACTIVE
-        // If PhysicalChannelConfigList is not empty, PhysicalLinkStatus is DcController
-        // .PHYSICAL_LINK_ACTIVE
+        // If PhysicalChannelConfigList is empty, PhysicalLinkStatus is
+        // DataCallResponse.LINK_STATUS_DORMANT
+        // If PhysicalChannelConfigList is not empty, PhysicalLinkStatus is
+        // DataCallResponse.LINK_STATUS_ACTIVE
 
         if (state) {
             PhysicalChannelConfig physicalChannelConfig = new PhysicalChannelConfig.Builder()
@@ -1233,14 +1175,13 @@
     @Test
     public void testTransitionToCurrentStateNrConnectedWithLowBandwidth() throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         doReturn(new int[] {19999}).when(mServiceState).getCellBandwidths();
         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
         broadcastCarrierConfigs();
 
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("connected", getCurrentState().getName());
     }
@@ -1248,7 +1189,6 @@
     @Test
     public void testTransitionToCurrentStateNrConnectedWithHighBandwidth() throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         List<PhysicalChannelConfig> lastPhysicalChannelConfigList = new ArrayList<>();
@@ -1260,7 +1200,7 @@
         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
         broadcastCarrierConfigs();
 
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIG_CHANGED */);
         processAllMessages();
         assertEquals("connected_mmwave", getCurrentState().getName());
     }
@@ -1269,7 +1209,6 @@
     public void testTransitionToCurrentStateNrConnectedWithHighBandwidthIncludingLte()
             throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         List<PhysicalChannelConfig> lastPhysicalChannelConfigList = new ArrayList<>();
@@ -1296,14 +1235,13 @@
     @Test
     public void testNrAdvancedDisabledWhileRoaming() throws Exception {
         assertEquals("DefaultState", getCurrentState().getName());
-        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mServiceState).getDataNetworkType();
         doReturn(true).when(mServiceState).getDataRoaming();
         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
         mBundle.putBoolean(CarrierConfigManager.KEY_ENABLE_NR_ADVANCED_WHILE_ROAMING_BOOL, false);
         broadcastCarrierConfigs();
 
-        mNetworkTypeController.sendMessage(NetworkTypeController.EVENT_UPDATE);
+        mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
         processAllMessages();
         assertEquals("connected", getCurrentState().getName());
     }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/PhoneConfigurationManagerTest.java b/tests/telephonytests/src/com/android/internal/telephony/PhoneConfigurationManagerTest.java
index 5771326..81b9ff1 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/PhoneConfigurationManagerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/PhoneConfigurationManagerTest.java
@@ -138,6 +138,27 @@
 
     @Test
     @SmallTest
+    public void testConfigureAndGetMaxActiveVoiceSubscriptions() throws Exception {
+        init(2);
+        assertEquals(1, mPcm.getStaticPhoneCapability().getMaxActiveVoiceSubscriptions());
+
+        PhoneCapability dualActiveVoiceSubCapability = new PhoneCapability.Builder(
+                PhoneCapability.DEFAULT_DSDS_CAPABILITY)
+                .setMaxActiveVoiceSubscriptions(2)
+                .build();
+
+        ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
+        verify(mMockRadioConfig).getPhoneCapability(captor.capture());
+        Message msg = captor.getValue();
+        AsyncResult.forMessage(msg, dualActiveVoiceSubCapability, null);
+        msg.sendToTarget();
+        processAllMessages();
+
+        assertEquals(2, mPcm.getStaticPhoneCapability().getMaxActiveVoiceSubscriptions());
+    }
+
+    @Test
+    @SmallTest
     public void testSwitchMultiSimConfig_notDsdsCapable_shouldFail() throws Exception {
         init(1);
         assertEquals(PhoneCapability.DEFAULT_SSSS_CAPABILITY, mPcm.getStaticPhoneCapability());
diff --git a/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberUtilsTest.java b/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberUtilsTest.java
index b7d2913..dd59e28 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberUtilsTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberUtilsTest.java
@@ -645,55 +645,27 @@
     @Test
     @Ignore
     public void testIsEmergencyNumber() {
-        // There are two parallel sets of tests here: one for the
-        // regular isEmergencyNumber() method, and the other for
-        // isPotentialEmergencyNumber().
-        //
         // (The difference is that isEmergencyNumber() will return true
         // only if the specified number exactly matches an actual
-        // emergency number, but isPotentialEmergencyNumber() will
-        // return true if the specified number simply starts with the
-        // same digits as any actual emergency number.)
+        // emergency number
 
         // Tests for isEmergencyNumber():
-        assertTrue(PhoneNumberUtils.isEmergencyNumber("911", "US"));
-        assertTrue(PhoneNumberUtils.isEmergencyNumber("112", "US"));
+        assertTrue(PhoneNumberUtils.isEmergencyNumber("911"));
+        assertTrue(PhoneNumberUtils.isEmergencyNumber("112"));
         // The next two numbers are not valid phone numbers in the US,
         // so do not count as emergency numbers (but they *are* "potential"
         // emergency numbers; see below.)
-        assertFalse(PhoneNumberUtils.isEmergencyNumber("91112345", "US"));
-        assertFalse(PhoneNumberUtils.isEmergencyNumber("11212345", "US"));
+        assertFalse(PhoneNumberUtils.isEmergencyNumber("91112345"));
+        assertFalse(PhoneNumberUtils.isEmergencyNumber("11212345"));
         // A valid mobile phone number from Singapore shouldn't be classified as an emergency number
         // in Singapore, as 911 is not an emergency number there.
-        assertFalse(PhoneNumberUtils.isEmergencyNumber("91121234", "SG"));
+        assertFalse(PhoneNumberUtils.isEmergencyNumber("91121234"));
         // A valid fixed-line phone number from Brazil shouldn't be classified as an emergency number
         // in Brazil, as 112 is not an emergency number there.
-        assertFalse(PhoneNumberUtils.isEmergencyNumber("1121234567", "BR"));
+        assertFalse(PhoneNumberUtils.isEmergencyNumber("1121234567"));
         // A valid local phone number from Brazil shouldn't be classified as an emergency number in
         // Brazil.
-        assertFalse(PhoneNumberUtils.isEmergencyNumber("91112345", "BR"));
-
-        // Tests for isPotentialEmergencyNumber():
-        // These first two are obviously emergency numbers:
-        assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("911", "US"));
-        assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("112", "US"));
-        // The next two numbers are not valid phone numbers in the US, but can be used to trick the
-        // system to dial 911 and 112, which are emergency numbers in the US. For the purpose of
-        // addressing that, they are also classified as "potential" emergency numbers in the US.
-        assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("91112345", "US"));
-        assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("11212345", "US"));
-
-        // A valid mobile phone number from Singapore shouldn't be classified as an emergency number
-        // in Singapore, as 911 is not an emergency number there.
-        // This test fails on devices that have ecclist property preloaded with 911.
-        // assertFalse(PhoneNumberUtils.isPotentialEmergencyNumber("91121234", "SG"));
-
-        // A valid fixed-line phone number from Brazil shouldn't be classified as an emergency number
-        // in Brazil, as 112 is not an emergency number there.
-        assertFalse(PhoneNumberUtils.isPotentialEmergencyNumber("1121234567", "BR"));
-        // A valid local phone number from Brazil shouldn't be classified as an emergency number in
-        // Brazil.
-        assertFalse(PhoneNumberUtils.isPotentialEmergencyNumber("91112345", "BR"));
+        assertFalse(PhoneNumberUtils.isEmergencyNumber("91112345"));
     }
 
     @SmallTest
diff --git a/tests/telephonytests/src/com/android/internal/telephony/PhoneSubInfoControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/PhoneSubInfoControllerTest.java
index 606bcec..e4c1466 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/PhoneSubInfoControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/PhoneSubInfoControllerTest.java
@@ -16,6 +16,9 @@
 package com.android.internal.telephony;
 
 import static android.Manifest.permission.READ_PHONE_STATE;
+import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE;
+import static android.telephony.TelephonyManager.APPTYPE_ISIM;
+import static android.telephony.TelephonyManager.APPTYPE_USIM;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
@@ -33,15 +36,27 @@
 import android.content.pm.PackageManager;
 import android.os.Binder;
 import android.os.Build;
+import android.os.RemoteException;
 import android.test.suitebuilder.annotation.SmallTest;
 
+import com.android.internal.telephony.uicc.IsimUiccRecords;
+import com.android.internal.telephony.uicc.SIMRecords;
+import com.android.internal.telephony.uicc.UiccCardApplication;
+import com.android.internal.telephony.uicc.UiccPort;
+import com.android.internal.telephony.uicc.UiccProfile;
+
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 public class PhoneSubInfoControllerTest extends TelephonyTest {
     private static final String FEATURE_ID = "myfeatureId";
+    private static final String PSI_SMSC_TEL1 = "tel:+91123456789";
+    private static final String PSI_SMSC_SIP1 = "sip:+1234567890@abc.pc.operetor1.com;user=phone";
+    private static final String PSI_SMSC_TEL2 = "tel:+91987654321";
+    private static final String PSI_SMSC_SIP2 = "sip:+19876543210@dcf.pc.operetor2.com;user=phone";
 
     private PhoneSubInfoController mPhoneSubInfoControllerUT;
     private AppOpsManager mAppOsMgr;
@@ -922,4 +937,234 @@
         assertEquals("VM_SIM_1", mPhoneSubInfoControllerUT
                 .getVoiceMailAlphaTagForSubscriber(1, TAG, FEATURE_ID));
     }
-}
+
+    private void setUpInitials() {
+        UiccPort uiccPort1 = Mockito.mock(UiccPort.class);
+        UiccProfile uiccProfile1 = Mockito.mock(UiccProfile.class);
+        UiccCardApplication uiccCardApplication1 = Mockito.mock(UiccCardApplication.class);
+        SIMRecords simRecords1 = Mockito.mock(SIMRecords.class);
+        IsimUiccRecords isimUiccRecords1 = Mockito.mock(IsimUiccRecords.class);
+
+        doReturn(uiccPort1).when(mPhone).getUiccPort();
+        doReturn(uiccProfile1).when(uiccPort1).getUiccProfile();
+        doReturn(uiccCardApplication1).when(uiccProfile1).getApplicationByType(anyInt());
+        doReturn(simRecords1).when(uiccCardApplication1).getIccRecords();
+        doReturn(isimUiccRecords1).when(uiccCardApplication1).getIccRecords();
+        doReturn(PSI_SMSC_TEL1).when(simRecords1).getSmscIdentity();
+        doReturn(PSI_SMSC_TEL1).when(isimUiccRecords1).getSmscIdentity();
+
+        doReturn(mUiccPort).when(mSecondPhone).getUiccPort();
+        doReturn(mUiccProfile).when(mUiccPort).getUiccProfile();
+        doReturn(mUiccCardApplicationIms).when(mUiccProfile).getApplicationByType(anyInt());
+        doReturn(mSimRecords).when(mUiccCardApplicationIms).getIccRecords();
+        doReturn(mIsimUiccRecords).when(mUiccCardApplicationIms).getIccRecords();
+        doReturn(PSI_SMSC_TEL2).when(mSimRecords).getSmscIdentity();
+        doReturn(PSI_SMSC_TEL2).when(mIsimUiccRecords).getSmscIdentity();
+    }
+
+    @Test
+    public void testGetSmscIdentityForTelUri() {
+        try {
+            setUpInitials();
+            assertEquals(PSI_SMSC_TEL1, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(0, APPTYPE_ISIM).toString());
+            assertEquals(PSI_SMSC_TEL1, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(0, APPTYPE_USIM).toString());
+            assertEquals(PSI_SMSC_TEL2, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(1, APPTYPE_ISIM).toString());
+            assertEquals(PSI_SMSC_TEL2, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(1, APPTYPE_USIM).toString());
+        } catch (RemoteException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Test
+    public void testGetSmscIdentityForSipUri() {
+        try {
+            UiccPort uiccPort1 = Mockito.mock(UiccPort.class);
+            UiccProfile uiccProfile1 = Mockito.mock(UiccProfile.class);
+            UiccCardApplication uiccCardApplication1 = Mockito.mock(UiccCardApplication.class);
+            SIMRecords simRecords1 = Mockito.mock(SIMRecords.class);
+            IsimUiccRecords isimUiccRecords1 = Mockito.mock(IsimUiccRecords.class);
+
+            doReturn(uiccPort1).when(mPhone).getUiccPort();
+            doReturn(uiccProfile1).when(uiccPort1).getUiccProfile();
+            doReturn(uiccCardApplication1).when(uiccProfile1).getApplicationByType(anyInt());
+            doReturn(simRecords1).when(uiccCardApplication1).getIccRecords();
+            doReturn(isimUiccRecords1).when(uiccCardApplication1).getIccRecords();
+            doReturn(PSI_SMSC_SIP1).when(simRecords1).getSmscIdentity();
+            doReturn(PSI_SMSC_SIP1).when(isimUiccRecords1).getSmscIdentity();
+
+            doReturn(mUiccPort).when(mSecondPhone).getUiccPort();
+            doReturn(mUiccProfile).when(mUiccPort).getUiccProfile();
+            doReturn(mUiccCardApplicationIms).when(mUiccProfile).getApplicationByType(anyInt());
+            doReturn(mSimRecords).when(mUiccCardApplicationIms).getIccRecords();
+            doReturn(mIsimUiccRecords).when(mUiccCardApplicationIms).getIccRecords();
+            doReturn(PSI_SMSC_SIP2).when(mSimRecords).getSmscIdentity();
+            doReturn(PSI_SMSC_SIP2).when(mIsimUiccRecords).getSmscIdentity();
+
+            assertEquals(PSI_SMSC_SIP1, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(0, APPTYPE_ISIM).toString());
+            assertEquals(PSI_SMSC_SIP1, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(0, APPTYPE_USIM).toString());
+            assertEquals(PSI_SMSC_SIP2, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(1, APPTYPE_ISIM).toString());
+            assertEquals(PSI_SMSC_SIP2, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(1, APPTYPE_USIM).toString());
+        } catch (RemoteException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Test
+    public void testGetSmscIdentityWithOutPermissions() {
+        setUpInitials();
+
+        //case 1: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION
+        mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);
+        try {
+            mPhoneSubInfoControllerUT.getSmscIdentity(0, APPTYPE_ISIM);
+            Assert.fail("expected Security Exception Thrown");
+        } catch (Exception ex) {
+            assertTrue(ex instanceof SecurityException);
+            assertTrue(ex.getMessage().contains("getSmscIdentity"));
+        }
+
+        try {
+            mPhoneSubInfoControllerUT.getSmscIdentity(1, APPTYPE_ISIM);
+            Assert.fail("expected Security Exception Thrown");
+        } catch (Exception ex) {
+            assertTrue(ex instanceof SecurityException);
+            assertTrue(ex.getMessage().contains("getSmscIdentity"));
+        }
+
+        try {
+            mPhoneSubInfoControllerUT.getSmscIdentity(0, APPTYPE_USIM);
+            Assert.fail("expected Security Exception Thrown");
+        } catch (Exception ex) {
+            assertTrue(ex instanceof SecurityException);
+            assertTrue(ex.getMessage().contains("getSmscIdentity"));
+        }
+
+        try {
+            mPhoneSubInfoControllerUT.getSmscIdentity(1, APPTYPE_USIM);
+            Assert.fail("expected Security Exception Thrown");
+        } catch (Exception ex) {
+            assertTrue(ex instanceof SecurityException);
+            assertTrue(ex.getMessage().contains("getSmscIdentity"));
+        }
+
+        //case 2: no READ_PRIVILEGED_PHONE_STATE
+        mContextFixture.addCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE);
+        doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp(
+                eq(AppOpsManager.OPSTR_READ_PHONE_STATE), anyInt(), eq(TAG), eq(FEATURE_ID),
+                nullable(String.class));
+
+        try {
+            assertEquals(PSI_SMSC_TEL1, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(0, APPTYPE_ISIM).toString());
+            assertEquals(PSI_SMSC_TEL1, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(0, APPTYPE_USIM).toString());
+            assertEquals(PSI_SMSC_TEL2, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(1, APPTYPE_ISIM).toString());
+            assertEquals(PSI_SMSC_TEL2, mPhoneSubInfoControllerUT
+                    .getSmscIdentity(1, APPTYPE_USIM).toString());
+        } catch (RemoteException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Test
+    public void testGetSimServiceTable() throws RemoteException {
+        String refSst = "1234567";
+        doReturn(mUiccPort).when(mPhone).getUiccPort();
+        doReturn(mUiccProfile).when(mUiccPort).getUiccProfile();
+        doReturn(mUiccCardApplicationIms).when(mUiccProfile).getApplicationByType(anyInt());
+        doReturn(mSimRecords).when(mUiccCardApplicationIms).getIccRecords();
+
+        doReturn(refSst).when(mSimRecords).getSimServiceTable();
+
+        String resultSst = mPhoneSubInfoControllerUT.getSimServiceTable(anyInt(), anyInt());
+        assertEquals(refSst, resultSst);
+    }
+
+    @Test
+    public void testGetSimServiceTableEmpty() throws RemoteException {
+        String refSst = null;
+        doReturn(mUiccPort).when(mPhone).getUiccPort();
+        doReturn(mUiccProfile).when(mUiccPort).getUiccProfile();
+        doReturn(mUiccCardApplicationIms).when(mUiccProfile).getApplicationByType(anyInt());
+        doReturn(mSimRecords).when(mUiccCardApplicationIms).getIccRecords();
+
+        doReturn(refSst).when(mSimRecords).getSimServiceTable();
+
+        String resultSst = mPhoneSubInfoControllerUT.getSimServiceTable(anyInt(), anyInt());
+        assertEquals(refSst, resultSst);
+    }
+
+    @Test
+    public void testGetSstWhenNoUiccPort() throws RemoteException {
+            String refSst = "1234567";
+            doReturn(null).when(mPhone).getUiccPort();
+            doReturn(mUiccProfile).when(mUiccPort).getUiccProfile();
+            doReturn(mUiccCardApplicationIms).when(mUiccProfile).getApplicationByType(anyInt());
+            doReturn(mSimRecords).when(mUiccCardApplicationIms).getIccRecords();
+
+            doReturn(refSst).when(mSimRecords).getSimServiceTable();
+
+            String resultSst = mPhoneSubInfoControllerUT.getSimServiceTable(anyInt(), anyInt());
+            assertEquals(null, resultSst);
+    }
+
+    @Test
+    public void testGetSstWhenNoUiccProfile() throws RemoteException {
+        String refSst = "1234567";
+        doReturn(mUiccPort).when(mPhone).getUiccPort();
+        doReturn(null).when(mUiccPort).getUiccProfile();
+        doReturn(mUiccCardApplicationIms).when(mUiccProfile).getApplicationByType(anyInt());
+        doReturn(mSimRecords).when(mUiccCardApplicationIms).getIccRecords();
+
+        doReturn(refSst).when(mSimRecords).getSimServiceTable();
+
+        String resultSst = mPhoneSubInfoControllerUT.getSimServiceTable(anyInt(), anyInt());
+        assertEquals(null, resultSst);
+    }
+
+    @Test
+    public void testGetSstWhenNoUiccApplication() throws RemoteException {
+        String refSst = "1234567";
+        doReturn(mUiccPort).when(mPhone).getUiccPort();
+        doReturn(mUiccProfile).when(mUiccPort).getUiccProfile();
+        doReturn(null).when(mUiccProfile).getApplicationByType(anyInt());
+        doReturn(mSimRecords).when(mUiccCardApplicationIms).getIccRecords();
+
+        doReturn(refSst).when(mSimRecords).getSimServiceTable();
+
+        String resultSst = mPhoneSubInfoControllerUT.getSimServiceTable(anyInt(), anyInt());
+        assertEquals(null, resultSst);
+    }
+
+    @Test
+    public void testGetSimServiceTableWithOutPermissions() throws RemoteException {
+        String refSst = "1234567";
+        doReturn(mUiccPort).when(mPhone).getUiccPort();
+        doReturn(mUiccProfile).when(mUiccPort).getUiccProfile();
+        doReturn(mUiccCardApplicationIms).when(mUiccProfile).getApplicationByType(anyInt());
+        doReturn(mSimRecords).when(mUiccCardApplicationIms).getIccRecords();
+
+        doReturn(refSst).when(mSimRecords).getSimServiceTable();
+
+        mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);
+        try {
+            mPhoneSubInfoControllerUT.getSimServiceTable(anyInt(), anyInt());
+            Assert.fail("expected Security Exception Thrown");
+        } catch (Exception ex) {
+            assertTrue(ex instanceof SecurityException);
+            assertTrue(ex.getMessage().contains("getSimServiceTable"));
+        }
+
+        mContextFixture.addCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE);
+        assertEquals(refSst, mPhoneSubInfoControllerUT.getSimServiceTable(anyInt(), anyInt()));
+    }
+}
\ No newline at end of file
diff --git a/tests/telephonytests/src/com/android/internal/telephony/RILTest.java b/tests/telephonytests/src/com/android/internal/telephony/RILTest.java
index 311fe20..f5c09ca 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/RILTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/RILTest.java
@@ -16,6 +16,12 @@
 
 package com.android.internal.telephony;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_DATA;
+import static android.telephony.TelephonyManager.HAL_SERVICE_MODEM;
+import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
+import static android.telephony.TelephonyManager.HAL_SERVICE_RADIO;
+import static android.telephony.TelephonyManager.HAL_SERVICE_SIM;
+
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_ALLOW_DATA;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE;
@@ -27,6 +33,7 @@
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_DATA_REGISTRATION_STATE;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_DELETE_SMS_ON_SIM;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_DEVICE_IDENTITY;
+import static com.android.internal.telephony.RILConstants.RIL_REQUEST_DEVICE_IMEI;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_DTMF;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_ENABLE_UICC_APPLICATIONS;
 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION;
@@ -90,6 +97,7 @@
 import static junit.framework.Assert.assertNotNull;
 import static junit.framework.Assert.assertNull;
 import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.fail;
 
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
@@ -120,12 +128,14 @@
 import android.net.ConnectivityManager;
 import android.net.InetAddresses;
 import android.net.LinkAddress;
+import android.os.AsyncResult;
 import android.os.Handler;
 import android.os.IPowerManager;
 import android.os.IThermalService;
 import android.os.Looper;
 import android.os.Message;
 import android.os.PowerManager;
+import android.os.RemoteException;
 import android.os.WorkSource;
 import android.service.carrier.CarrierIdentifier;
 import android.telephony.AccessNetworkConstants;
@@ -172,6 +182,7 @@
 import com.android.internal.telephony.uicc.IccCardApplicationStatus.PersoSubState;
 
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -183,8 +194,10 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.function.Consumer;
 
@@ -205,14 +218,16 @@
     private RadioDataProxy mDataProxy;
     private RadioNetworkProxy mNetworkProxy;
     private RadioSimProxy mSimProxy;
+    private RadioModemProxy mRadioModemProxy;
 
-    private HalVersion mRadioVersionV10 = new HalVersion(1, 0);
-    private HalVersion mRadioVersionV11 = new HalVersion(1, 1);
-    private HalVersion mRadioVersionV12 = new HalVersion(1, 2);
-    private HalVersion mRadioVersionV13 = new HalVersion(1, 3);
-    private HalVersion mRadioVersionV14 = new HalVersion(1, 4);
-    private HalVersion mRadioVersionV15 = new HalVersion(1, 5);
-    private HalVersion mRadioVersionV16 = new HalVersion(1, 6);
+    private Map<Integer, HalVersion> mHalVersionV10 = new HashMap<>();
+    private Map<Integer, HalVersion> mHalVersionV11 = new HashMap<>();
+    private Map<Integer, HalVersion> mHalVersionV12 = new HashMap<>();
+    private Map<Integer, HalVersion> mHalVersionV13 = new HashMap<>();
+    private Map<Integer, HalVersion> mHalVersionV14 = new HashMap<>();
+    private Map<Integer, HalVersion> mHalVersionV15 = new HashMap<>();
+    private Map<Integer, HalVersion> mHalVersionV16 = new HashMap<>();
+    private Map<Integer, HalVersion> mHalVersionV21 = new HashMap<>();
 
     private RIL mRILInstance;
     private RIL mRILUnderTest;
@@ -300,6 +315,7 @@
         mDataProxy = mock(RadioDataProxy.class);
         mNetworkProxy = mock(RadioNetworkProxy.class);
         mSimProxy = mock(RadioSimProxy.class);
+        mRadioModemProxy = mock(RadioModemProxy.class);
         try {
             TelephonyDevController.create();
         } catch (RuntimeException e) {
@@ -316,10 +332,11 @@
         doReturn(powerManager).when(context).getSystemService(Context.POWER_SERVICE);
         doReturn(new ApplicationInfo()).when(context).getApplicationInfo();
         SparseArray<RadioServiceProxy> proxies = new SparseArray<>();
-        proxies.put(RIL.RADIO_SERVICE, null);
-        proxies.put(RIL.DATA_SERVICE, mDataProxy);
-        proxies.put(RIL.NETWORK_SERVICE, mNetworkProxy);
-        proxies.put(RIL.SIM_SERVICE, mSimProxy);
+        proxies.put(HAL_SERVICE_RADIO, null);
+        proxies.put(HAL_SERVICE_DATA, mDataProxy);
+        proxies.put(HAL_SERVICE_NETWORK, mNetworkProxy);
+        proxies.put(HAL_SERVICE_SIM, mSimProxy);
+        proxies.put(HAL_SERVICE_MODEM, mRadioModemProxy);
         mRILInstance = new RIL(context,
                 RadioAccessFamily.getRafFromNetworkType(RILConstants.PREFERRED_NETWORK_MODE),
                 Phone.PREFERRED_CDMA_SUBSCRIPTION, 0, proxies);
@@ -331,11 +348,24 @@
                 eq(RadioNetworkProxy.class), any());
         doReturn(mSimProxy).when(mRILUnderTest).getRadioServiceProxy(eq(RadioSimProxy.class),
                 any());
+        doReturn(mRadioModemProxy).when(mRILUnderTest).getRadioServiceProxy(
+                eq(RadioModemProxy.class), any());
         doReturn(false).when(mDataProxy).isEmpty();
         doReturn(false).when(mNetworkProxy).isEmpty();
         doReturn(false).when(mSimProxy).isEmpty();
+        doReturn(false).when(mRadioModemProxy).isEmpty();
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV10);
+            for (int service = RIL.MIN_SERVICE_IDX; service <= RIL.MAX_SERVICE_IDX; service++) {
+                mHalVersionV10.put(service, new HalVersion(1, 0));
+                mHalVersionV11.put(service, new HalVersion(1, 1));
+                mHalVersionV12.put(service, new HalVersion(1, 2));
+                mHalVersionV13.put(service, new HalVersion(1, 3));
+                mHalVersionV14.put(service, new HalVersion(1, 4));
+                mHalVersionV15.put(service, new HalVersion(1, 5));
+                mHalVersionV16.put(service, new HalVersion(1, 6));
+                mHalVersionV21.put(service, new HalVersion(2, 1));
+            }
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV10);
         } catch (Exception e) {
         }
     }
@@ -493,7 +523,7 @@
 
         // Make radio version 1.5 to support the operation.
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV15);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV15);
         } catch (Exception e) {
         }
 
@@ -531,7 +561,7 @@
 
         // Make radio version 1.5 to support the operation.
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV15);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV15);
         } catch (Exception e) {
         }
 
@@ -711,7 +741,7 @@
     public void testStartNetworkScanWithUnsupportedResponse() throws Exception {
         // Use Radio HAL v1.5
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV15);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV15);
         } catch (Exception e) {
         }
         NetworkScanRequest nsr = getNetworkScanRequestForTesting();
@@ -738,7 +768,7 @@
     public void testGetVoiceRegistrationStateWithUnsupportedResponse() throws Exception {
         // Use Radio HAL v1.5
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV15);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV15);
         } catch (Exception e) {
         }
         mRILUnderTest.getVoiceRegistrationState(obtainMessage());
@@ -773,7 +803,7 @@
     public void testGetDataRegistrationStateWithUnsupportedResponse() throws Exception {
         // Use Radio HAL v1.5
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV15);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV15);
         } catch (Exception e) {
         }
 
@@ -818,7 +848,7 @@
 
         // Use Radio HAL v1.6
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV16);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV16);
         } catch (Exception e) {
         }
 
@@ -861,7 +891,7 @@
     public void testSendSMS_1_6() throws Exception {
         // Use Radio HAL v1.6
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV16);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV16);
         } catch (Exception e) {
         }
         String smscPdu = "smscPdu";
@@ -893,7 +923,7 @@
     public void testSendSMSExpectMore_1_6() throws Exception {
         // Use Radio HAL v1.6
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV16);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV16);
         } catch (Exception e) {
         }
         String smscPdu = "smscPdu";
@@ -912,7 +942,7 @@
     public void testSendCdmaSMS_1_6() throws Exception {
         // Use Radio HAL v1.6
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV16);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV16);
         } catch (Exception e) {
         }
         byte[] pdu = "000010020000000000000000000000000000000000".getBytes();
@@ -928,7 +958,7 @@
     public void testSendCdmaSMSExpectMore_1_6() throws Exception {
         // Use Radio HAL v1.6
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV16);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV16);
         } catch (Exception e) {
         }
         byte[] pdu = "000010020000000000000000000000000000000000".getBytes();
@@ -1475,7 +1505,7 @@
 
         // Make radio version 1.5 to support the operation.
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV15);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV15);
         } catch (Exception e) {
         }
         mRILUnderTest.getBarringInfo(obtainMessage());
@@ -2838,7 +2868,7 @@
 
         // Make radio version 1.5 to support the operation.
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV15);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV15);
         } catch (Exception e) {
         }
         mRILUnderTest.enableUiccApplications(false, obtainMessage());
@@ -2855,7 +2885,7 @@
 
         // Make radio version 1.5 to support the operation.
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV15);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV15);
         } catch (Exception e) {
         }
         mRILUnderTest.areUiccApplicationsEnabled(obtainMessage());
@@ -2902,7 +2932,7 @@
     public void testGetSlicingConfig() throws Exception {
         // Use Radio HAL v1.6
         try {
-            replaceInstance(RIL.class, "mRadioVersion", mRILUnderTest, mRadioVersionV16);
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV16);
         } catch (Exception e) {
         }
         mRILUnderTest.getSlicingConfig(obtainMessage());
@@ -2910,4 +2940,31 @@
         verifyRILResponse_1_6(
                 mRILUnderTest, mSerialNumberCaptor.getValue(), RIL_REQUEST_GET_SLICING_CONFIG);
     }
+
+    @Test
+    public void getImei() throws RemoteException {
+        try {
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV21);
+        } catch (Exception e) {
+            fail();
+        }
+        mRILUnderTest.getImei(obtainMessage());
+        verify(mRadioModemProxy, atLeast(1)).getImei(mSerialNumberCaptor.capture());
+        verifyRILResponse(mRILUnderTest, mSerialNumberCaptor.getValue(), RIL_REQUEST_DEVICE_IMEI);
+    }
+
+    @Test
+    public void getImeiNotSupported() {
+        try {
+            replaceInstance(RIL.class, "mHalVersion", mRILUnderTest, mHalVersionV16);
+        } catch (Exception e) {
+            fail();
+        }
+        Message message = obtainMessage();
+        mRILUnderTest.getImei(message);
+        AsyncResult ar = (AsyncResult) message.obj;
+        Assert.assertEquals(null, ar.result);
+        Assert.assertNotNull(ar.exception.getMessage());
+        Assert.assertEquals("REQUEST_NOT_SUPPORTED", ar.exception.getMessage());
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/ServiceStateTrackerTest.java b/tests/telephonytests/src/com/android/internal/telephony/ServiceStateTrackerTest.java
index 3833f7d..12bf05d 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/ServiceStateTrackerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/ServiceStateTrackerTest.java
@@ -464,30 +464,37 @@
     @Test
     @MediumTest
     public void testSetRadioPowerForReason() {
+        testSetRadioPowerForReason(TelephonyManager.RADIO_POWER_REASON_THERMAL);
+        testSetRadioPowerForReason(TelephonyManager.RADIO_POWER_REASON_NEARBY_DEVICE);
+        testSetRadioPowerForReason(TelephonyManager.RADIO_POWER_REASON_CARRIER);
+    }
+
+    private void testSetRadioPowerForReason(int reason) {
         // Radio does not turn on if off for other reason and not emergency call.
         assertTrue(mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_ON);
         assertTrue(sst.getRadioPowerOffReasons().isEmpty());
-        sst.setRadioPowerForReason(false, false, false, false, Phone.RADIO_POWER_REASON_THERMAL);
-        assertTrue(sst.getRadioPowerOffReasons().contains(Phone.RADIO_POWER_REASON_THERMAL));
+        sst.setRadioPowerForReason(false, false, false, false, reason);
+        assertTrue(sst.getRadioPowerOffReasons().contains(reason));
         assertTrue(sst.getRadioPowerOffReasons().size() == 1);
         waitForLastHandlerAction(mSSTTestHandler.getThreadHandler());
         assertTrue(mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_OFF);
-        sst.setRadioPowerForReason(true, false, false, false, Phone.RADIO_POWER_REASON_USER);
-        assertTrue(sst.getRadioPowerOffReasons().contains(Phone.RADIO_POWER_REASON_THERMAL));
+        sst.setRadioPowerForReason(true, false, false, false,
+                TelephonyManager.RADIO_POWER_REASON_USER);
+        assertTrue(sst.getRadioPowerOffReasons().contains(reason));
         assertTrue(sst.getRadioPowerOffReasons().size() == 1);
         waitForLastHandlerAction(mSSTTestHandler.getThreadHandler());
         assertTrue(mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_OFF);
 
         // Radio power state reason is removed and radio turns on if turned on for same reason it
         // had been turned off for.
-        sst.setRadioPowerForReason(true, false, false, false, Phone.RADIO_POWER_REASON_THERMAL);
+        sst.setRadioPowerForReason(true, false, false, false, reason);
         assertTrue(sst.getRadioPowerOffReasons().isEmpty());
         waitForLastHandlerAction(mSSTTestHandler.getThreadHandler());
         assertTrue(mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_ON);
 
         // Turn radio off, then successfully turn radio on for emergency call.
-        sst.setRadioPowerForReason(false, false, false, false, Phone.RADIO_POWER_REASON_THERMAL);
-        assertTrue(sst.getRadioPowerOffReasons().contains(Phone.RADIO_POWER_REASON_THERMAL));
+        sst.setRadioPowerForReason(false, false, false, false, reason);
+        assertTrue(sst.getRadioPowerOffReasons().contains(reason));
         assertTrue(sst.getRadioPowerOffReasons().size() == 1);
         waitForLastHandlerAction(mSSTTestHandler.getThreadHandler());
         assertTrue(mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_OFF);
@@ -499,33 +506,83 @@
 
     @Test
     @MediumTest
-    public void testSetRadioPowerFromCarrier() {
+    public void testSetRadioPowerForMultipleReasons() {
+        assertTrue(mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_ON);
+        assertTrue(sst.getRadioPowerOffReasons().isEmpty());
+
+        // Turn off radio
+        turnRadioOffForReason(TelephonyManager.RADIO_POWER_REASON_USER, 1);
+        turnRadioOffForReason(TelephonyManager.RADIO_POWER_REASON_THERMAL, 2);
+        turnRadioOffForReason(TelephonyManager.RADIO_POWER_REASON_CARRIER, 3);
+        turnRadioOffForReason(TelephonyManager.RADIO_POWER_REASON_NEARBY_DEVICE, 4);
+
+        // Turn on radio
+        turnRadioOnForReason(TelephonyManager.RADIO_POWER_REASON_NEARBY_DEVICE, 3,
+                TelephonyManager.RADIO_POWER_OFF);
+        turnRadioOnForReason(TelephonyManager.RADIO_POWER_REASON_THERMAL, 2,
+                TelephonyManager.RADIO_POWER_OFF);
+        turnRadioOnForReason(TelephonyManager.RADIO_POWER_REASON_CARRIER, 1,
+                TelephonyManager.RADIO_POWER_OFF);
+        turnRadioOnForReason(TelephonyManager.RADIO_POWER_REASON_USER, 0,
+                TelephonyManager.RADIO_POWER_ON);
+    }
+
+    private void turnRadioOffForReason(int reason, int powerOffReasonSize) {
+        sst.setRadioPowerForReason(false, false, false, false, reason);
+        assertTrue(sst.getRadioPowerOffReasons().contains(reason));
+        assertTrue(sst.getRadioPowerOffReasons().size() == powerOffReasonSize);
+        waitForLastHandlerAction(mSSTTestHandler.getThreadHandler());
+        assertTrue(mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_OFF);
+    }
+
+    private void turnRadioOnForReason(int reason, int powerOffReasonSize,
+            int expectedRadioPowerState) {
+        sst.setRadioPowerForReason(true, false, false, false, reason);
+        assertFalse(sst.getRadioPowerOffReasons().contains(reason));
+        assertTrue(sst.getRadioPowerOffReasons().size() == powerOffReasonSize);
+        waitForLastHandlerAction(mSSTTestHandler.getThreadHandler());
+        assertTrue(mSimulatedCommands.getRadioState() == expectedRadioPowerState);
+    }
+
+    @Test
+    @MediumTest
+    public void testSetRadioPowerForReasonCarrier() {
         // Carrier disable radio power
-        sst.setRadioPowerFromCarrier(false);
+        sst.setRadioPowerForReason(false, false, false, false,
+                TelephonyManager.RADIO_POWER_REASON_CARRIER);
         waitForLastHandlerAction(mSSTTestHandler.getThreadHandler());
         assertFalse(mSimulatedCommands.getRadioState()
                 == TelephonyManager.RADIO_POWER_ON);
-        assertTrue(sst.getDesiredPowerState());
+        assertFalse(sst.getDesiredPowerState());
         assertFalse(sst.getPowerStateFromCarrier());
+        assertTrue(sst.getRadioPowerOffReasons().contains(
+                TelephonyManager.RADIO_POWER_REASON_CARRIER));
+        assertTrue(sst.getRadioPowerOffReasons().size() == 1);
 
         // User toggle radio power will not overrides carrier settings
         sst.setRadioPower(true);
         waitForLastHandlerAction(mSSTTestHandler.getThreadHandler());
         assertFalse(mSimulatedCommands.getRadioState()
                 == TelephonyManager.RADIO_POWER_ON);
-        assertTrue(sst.getDesiredPowerState());
+        assertFalse(sst.getDesiredPowerState());
         assertFalse(sst.getPowerStateFromCarrier());
+        assertTrue(sst.getRadioPowerOffReasons().contains(
+                TelephonyManager.RADIO_POWER_REASON_CARRIER));
+        assertTrue(sst.getRadioPowerOffReasons().size() == 1);
 
         // Carrier re-enable radio power
-        sst.setRadioPowerFromCarrier(true);
+        sst.setRadioPowerForReason(true, false, false, false,
+                TelephonyManager.RADIO_POWER_REASON_CARRIER);
         waitForLastHandlerAction(mSSTTestHandler.getThreadHandler());
         assertTrue(mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_ON);
         assertTrue(sst.getDesiredPowerState());
         assertTrue(sst.getPowerStateFromCarrier());
+        assertTrue(sst.getRadioPowerOffReasons().isEmpty());
 
         // User toggle radio power off (airplane mode) and set carrier on
         sst.setRadioPower(false);
-        sst.setRadioPowerFromCarrier(true);
+        sst.setRadioPowerForReason(true, false, false, false,
+                TelephonyManager.RADIO_POWER_REASON_CARRIER);
         waitForLastHandlerAction(mSSTTestHandler.getThreadHandler());
         assertFalse(mSimulatedCommands.getRadioState()
                 == TelephonyManager.RADIO_POWER_ON);
@@ -2414,7 +2471,7 @@
 
     @Test
     @SmallTest
-    public void testGetMdn() throws Exception {
+    public void testGetMdn() {
         doReturn(false).when(mPhone).isPhoneTypeGsm();
         doReturn(false).when(mPhone).isPhoneTypeCdma();
         doReturn(true).when(mPhone).isPhoneTypeCdmaLte();
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SignalStrengthControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/SignalStrengthControllerTest.java
index 50a1b96..f72bf98 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SignalStrengthControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SignalStrengthControllerTest.java
@@ -18,7 +18,9 @@
 
 import static android.telephony.SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP;
 import static android.telephony.SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI;
+import static android.telephony.SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP;
 import static android.telephony.SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR;
+import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
 
 import static com.google.common.truth.Truth.assertThat;
 
@@ -26,6 +28,7 @@
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -112,6 +115,7 @@
                         -97, /* SIGNAL_STRENGTH_GOOD */
                         -89,  /* SIGNAL_STRENGTH_GREAT */
                 });
+        mBundle.putInt(CarrierConfigManager.KEY_GERAN_RSSI_HYSTERESIS_DB_INT, 6);
         // Support EUTRAN with RSRP
         mBundle.putInt(CarrierConfigManager.KEY_PARAMETERS_USED_FOR_LTE_SIGNAL_BAR_INT,
                 1 /* USE_RSRP */);
@@ -122,6 +126,7 @@
                         -95, /* SIGNAL_STRENGTH_GOOD */
                         -85,  /* SIGNAL_STRENGTH_GREAT */
                 });
+        mBundle.putInt(CarrierConfigManager.KEY_EUTRAN_RSRP_HYSTERESIS_DB_INT, 3);
         // Support NR with SSRSRP
         mBundle.putInt(CarrierConfigManager.KEY_PARAMETERS_USE_FOR_5G_NR_SIGNAL_BAR_INT,
                 1 /* USE_SSRSRP */);
@@ -132,6 +137,7 @@
                         -80, /* SIGNAL_STRENGTH_GOOD */
                         -64,  /* SIGNAL_STRENGTH_GREAT */
                 });
+        mBundle.putInt(CarrierConfigManager.KEY_NGRAN_SSRSRP_HYSTERESIS_DB_INT, 1);
         // By default, NR with SSRSRQ and SSSINR is not supported
         mBundle.putIntArray(CarrierConfigManager.KEY_5G_NR_SSRSRQ_THRESHOLDS_INT_ARRAY,
                 new int[] {
@@ -505,6 +511,212 @@
     }
 
     @Test
+    public void testSetMinimumHysteresisDb_FromThresholdDelta() {
+        final int[] consolidatedThresholdList = new int[] {-120, -116, -113, -112};
+
+        SignalThresholdInfo info =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
+                        .setSignalMeasurementType(SIGNAL_MEASUREMENT_TYPE_RSSI)
+                        .setThresholds(new int[] {-113}, true)
+                        .setHysteresisDb(2)
+                        .build();
+        SignalStrengthUpdateRequest request =
+                createTestSignalStrengthUpdateRequest(
+                        info,
+                        false /* shouldReportWhileIdle*/,
+                        false /* shouldReportSystemWhileIdle */);
+        mSsc.setSignalStrengthUpdateRequest(
+                ACTIVE_SUB_ID, CALLING_UID, request, Message.obtain(mHandler));
+        processAllMessages();
+
+        int minHysteresis =
+                mSsc.getMinimumHysteresisDb(true,
+                        AccessNetworkConstants.AccessNetworkType.GERAN,
+                        SIGNAL_MEASUREMENT_TYPE_RSSI,
+                        consolidatedThresholdList);
+        assertEquals(1, minHysteresis);
+        mSsc.clearSignalStrengthUpdateRequest(
+                ACTIVE_SUB_ID, CALLING_UID, request, Message.obtain(mHandler));
+        processAllMessages();
+    }
+
+    @Test
+    public void testSetMinimumHysteresisDb_FromSignalThresholdRequest() {
+        final int[] consolidatedThresholdList = new int[] {-120, -116, -112, -108};
+
+        SignalThresholdInfo info =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
+                        .setSignalMeasurementType(SIGNAL_MEASUREMENT_TYPE_RSRP)
+                        .setThresholds(new int[] {-113}, true)
+                        .setHysteresisDb(3)
+                        .build();
+        SignalStrengthUpdateRequest request =
+                createTestSignalStrengthUpdateRequest(
+                        info,
+                        false /* shouldReportWhileIdle*/,
+                        false /* shouldReportSystemWhileIdle */);
+        mSsc.setSignalStrengthUpdateRequest(
+                ACTIVE_SUB_ID, CALLING_UID, request, Message.obtain(mHandler));
+        processAllMessages();
+
+        int minHysteresis =
+                mSsc.getMinimumHysteresisDb(true,
+                        AccessNetworkConstants.AccessNetworkType.EUTRAN,
+                        SIGNAL_MEASUREMENT_TYPE_RSRP,
+                        consolidatedThresholdList);
+        assertEquals(3, minHysteresis);
+
+        mSsc.clearSignalStrengthUpdateRequest(
+                ACTIVE_SUB_ID, CALLING_UID, request, Message.obtain(mHandler));
+        processAllMessages();
+    }
+
+    @Test
+    public void testSetMinimumHysteresisDb_FromCarrierConfig() {
+        final int[] consolidatedThresholdList = new int[] {-120, -115, -108, -103};
+
+        SignalThresholdInfo info =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
+                        .setSignalMeasurementType(SIGNAL_MEASUREMENT_TYPE_SSRSRP)
+                        .setThresholds(new int[] {-113}, true)
+                        .setHysteresisDb(6)
+                        .build();
+        SignalStrengthUpdateRequest request =
+                createTestSignalStrengthUpdateRequest(
+                        info,
+                        false /* shouldReportWhileIdle*/,
+                        false /* shouldReportSystemWhileIdle */);
+        mSsc.setSignalStrengthUpdateRequest(
+                ACTIVE_SUB_ID, CALLING_UID, request, Message.obtain(mHandler));
+        processAllMessages();
+
+        int minHysteresis =
+                mSsc.getMinimumHysteresisDb(true,
+                        AccessNetworkConstants.AccessNetworkType.NGRAN,
+                        SIGNAL_MEASUREMENT_TYPE_SSRSRP,
+                        consolidatedThresholdList);
+        assertEquals(1, minHysteresis);
+        mSsc.clearSignalStrengthUpdateRequest(
+                ACTIVE_SUB_ID, CALLING_UID, request, Message.obtain(mHandler));
+        processAllMessages();
+    }
+
+    @Test
+    public void testSetHysteresisDb_WithCarrierConfigValue() {
+        when(mPhone.isDeviceIdle()).thenReturn(true);
+        when(mPhone.getSubId()).thenReturn(ACTIVE_SUB_ID);
+
+        mBundle.putInt(CarrierConfigManager.KEY_GERAN_RSSI_HYSTERESIS_DB_INT, 5);
+        mBundle.putInt(CarrierConfigManager.KEY_EUTRAN_RSRP_HYSTERESIS_DB_INT, 3);
+        mBundle.putInt(CarrierConfigManager.KEY_NGRAN_SSRSRP_HYSTERESIS_DB_INT, 2);
+        sendCarrierConfigUpdate();
+
+        ArgumentCaptor<List<SignalThresholdInfo>> signalThresholdInfoCaptor =
+                ArgumentCaptor.forClass(List.class);
+        verify(mSimulatedCommandsVerifier, atLeastOnce())
+                .setSignalStrengthReportingCriteria(signalThresholdInfoCaptor.capture(), isNull());
+        List<SignalThresholdInfo> capturedInfos = signalThresholdInfoCaptor.getAllValues().get(0);
+        assertThat(capturedInfos).isNotEmpty();
+
+        for (SignalThresholdInfo signalThresholdInfo : capturedInfos) {
+            if (signalThresholdInfo.getSignalMeasurementType() == SIGNAL_MEASUREMENT_TYPE_RSRP) {
+                assertEquals(3, signalThresholdInfo.getHysteresisDb());
+            }
+            if (signalThresholdInfo.getSignalMeasurementType() == SIGNAL_MEASUREMENT_TYPE_RSSI) {
+                assertEquals(5, signalThresholdInfo.getHysteresisDb());
+            }
+            if (signalThresholdInfo.getSignalMeasurementType() == SIGNAL_MEASUREMENT_TYPE_SSRSRP) {
+                assertEquals(2, signalThresholdInfo.getHysteresisDb());
+            }
+        }
+        reset(mSimulatedCommandsVerifier);
+    }
+
+    @Test
+    public void testSetHysteresisDb_BetweenCarrierConfigSignalThresholdInfoThresholdDelta() {
+        SignalThresholdInfo info =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
+                        .setSignalMeasurementType(SIGNAL_MEASUREMENT_TYPE_SSRSRP)
+                        .setThresholds(new int[] {-116}, true)
+                        .setHysteresisDb(3)
+                        .build();
+        SignalStrengthUpdateRequest request =
+                createTestSignalStrengthUpdateRequest(
+                        info,
+                        false /* shouldReportWhileIdle*/,
+                        false /* shouldReportSystemWhileIdle */);
+        mSsc.setSignalStrengthUpdateRequest(
+                ACTIVE_SUB_ID, CALLING_UID, request, Message.obtain(mHandler));
+        processAllMessages();
+
+        reset(mSimulatedCommandsVerifier);
+        when(mPhone.isDeviceIdle()).thenReturn(false);
+        when(mPhone.getSubId()).thenReturn(ACTIVE_SUB_ID);
+        mBundle.putIntArray(CarrierConfigManager.KEY_5G_NR_SSRSRP_THRESHOLDS_INT_ARRAY,
+                new int[] {
+                        -113, /* SIGNAL_STRENGTH_POOR */
+                        -107, /* SIGNAL_STRENGTH_MODERATE */
+                        -100, /* SIGNAL_STRENGTH_GOOD */
+                        -95,  /* SIGNAL_STRENGTH_GREAT */
+                });
+
+        mBundle.putInt(CarrierConfigManager.KEY_PARAMETERS_USE_FOR_5G_NR_SIGNAL_BAR_INT,
+                1 /* USE_SSRSRP */);
+        mBundle.putInt(CarrierConfigManager.KEY_NGRAN_SSRSRP_HYSTERESIS_DB_INT, 4);
+        sendCarrierConfigUpdate();
+
+        ArgumentCaptor<List<SignalThresholdInfo>> signalThresholdInfoCaptor =
+                ArgumentCaptor.forClass(List.class);
+        verify(mSimulatedCommandsVerifier, atLeastOnce())
+                .setSignalStrengthReportingCriteria(signalThresholdInfoCaptor.capture(), isNull());
+        List<SignalThresholdInfo> capturedInfos = signalThresholdInfoCaptor.getAllValues().get(0);
+        assertThat(capturedInfos).isNotEmpty();
+
+        for (SignalThresholdInfo signalThresholdInfo : capturedInfos) {
+            if (signalThresholdInfo.getSignalMeasurementType() == SIGNAL_MEASUREMENT_TYPE_SSRSRP) {
+                assertEquals(4,
+                        mBundle.getInt(CarrierConfigManager.KEY_NGRAN_SSRSRP_HYSTERESIS_DB_INT));
+                assertEquals(3, signalThresholdInfo.getHysteresisDb());
+            }
+        }
+    }
+
+    @Test
+    public void testSetHysteresisDb_WithInvalidCarrierConfigValue() {
+        when(mPhone.isDeviceIdle()).thenReturn(true);
+        when(mPhone.getSubId()).thenReturn(ACTIVE_SUB_ID);
+
+        mBundle.putInt(CarrierConfigManager.KEY_GERAN_RSSI_HYSTERESIS_DB_INT, -4);
+        mBundle.putInt(CarrierConfigManager.KEY_EUTRAN_RSRP_HYSTERESIS_DB_INT, -5);
+        mBundle.putInt(CarrierConfigManager.KEY_NGRAN_SSRSRP_HYSTERESIS_DB_INT, -2);
+        sendCarrierConfigUpdate();
+
+        ArgumentCaptor<List<SignalThresholdInfo>> signalThresholdInfoCaptor =
+                ArgumentCaptor.forClass(List.class);
+        verify(mSimulatedCommandsVerifier, atLeastOnce())
+                .setSignalStrengthReportingCriteria(signalThresholdInfoCaptor.capture(), isNull());
+        List<SignalThresholdInfo> capturedInfos = signalThresholdInfoCaptor.getAllValues().get(0);
+        assertThat(capturedInfos).isNotEmpty();
+
+        for (SignalThresholdInfo signalThresholdInfo : capturedInfos) {
+            if (signalThresholdInfo.getSignalMeasurementType() == SIGNAL_MEASUREMENT_TYPE_RSRP) {
+                assertEquals(2, signalThresholdInfo.getHysteresisDb());
+            }
+            if (signalThresholdInfo.getSignalMeasurementType() == SIGNAL_MEASUREMENT_TYPE_RSSI) {
+                assertEquals(2, signalThresholdInfo.getHysteresisDb());
+            }
+            if (signalThresholdInfo.getSignalMeasurementType() == SIGNAL_MEASUREMENT_TYPE_SSRSRP) {
+                assertEquals(2, signalThresholdInfo.getHysteresisDb());
+            }
+        }
+        reset(mSimulatedCommandsVerifier);
+    }
+
+    @Test
     public void testLteSignalStrengthReportingCriteria_convertRssnrUnitFromTenDbToDB() {
         SignalStrength ss = new SignalStrength(
                 new CellSignalStrengthCdma(),
@@ -741,7 +953,7 @@
             }
         }
         // Only check on RADIO hal 1.5 and above to make it less flaky
-        if (mPhone.getHalVersion().greaterOrEqual(RIL.RADIO_HAL_VERSION_1_5)) {
+        if (mPhone.getHalVersion(HAL_SERVICE_NETWORK).greaterOrEqual(RIL.RADIO_HAL_VERSION_1_5)) {
             assertThat(expectedNonEmptyThreshold).isEqualTo(actualNonEmptyThreshold);
         }
     }
@@ -789,4 +1001,4 @@
         }
         return builder.build();
     }
-}
+}
\ No newline at end of file
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SignalStrengthTest.java b/tests/telephonytests/src/com/android/internal/telephony/SignalStrengthTest.java
index f15845c..96184c5 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SignalStrengthTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SignalStrengthTest.java
@@ -98,7 +98,7 @@
                 new CellSignalStrengthWcdma(-94, 4, -102, -5),
                 new CellSignalStrengthTdscdma(-95, 2, -103),
                 new CellSignalStrengthLte(-85, -91, -6, -10, 1, 12, 1),
-                new CellSignalStrengthNr(-91, -6, 3, 1, NrCqiReport, -80, -7, 4));
+                new CellSignalStrengthNr(-91, -6, 3, 1, NrCqiReport, -80, -7, 4, 1));
         assertParcelingIsLossless(s);
 
         PersistableBundle bundle = new PersistableBundle();
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SignalThresholdInfoTest.java b/tests/telephonytests/src/com/android/internal/telephony/SignalThresholdInfoTest.java
index b282c55..e22d7ab 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SignalThresholdInfoTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SignalThresholdInfoTest.java
@@ -42,69 +42,94 @@
 public class SignalThresholdInfoTest extends TestCase {
     private static final int HYSTERESIS_DB = 2;
     private static final int HYSTERESIS_MS = 30;
-    private static final int[] SSRSRP_THRESHOLDS = new int[]{-120, -100, -80, -60};
+    private static final int[] SSRSRP_THRESHOLDS = new int[] {-120, -100, -80, -60};
 
     // Map of SignalMeasurementType to invalid thresholds edge values.
     // Each invalid value will be constructed with a thresholds array to test separately.
-    private static final Map<Integer, List<Integer>> INVALID_THRESHOLDS_MAP = Map.of(
-            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI,
-            List.of(SignalThresholdInfo.SIGNAL_RSSI_MIN_VALUE - 1,
-                    SignalThresholdInfo.SIGNAL_RSSI_MAX_VALUE + 1),
-            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP,
-            List.of(SignalThresholdInfo.SIGNAL_RSCP_MIN_VALUE - 1,
-                    SignalThresholdInfo.SIGNAL_RSCP_MAX_VALUE + 1),
-            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP,
-            List.of(SignalThresholdInfo.SIGNAL_RSRP_MIN_VALUE - 1,
-                    SignalThresholdInfo.SIGNAL_RSRP_MAX_VALUE + 1),
-            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ,
-            List.of(SignalThresholdInfo.SIGNAL_RSRQ_MIN_VALUE - 1,
-                    SignalThresholdInfo.SIGNAL_RSRQ_MAX_VALUE + 1),
-            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR,
-            List.of(SignalThresholdInfo.SIGNAL_RSSNR_MIN_VALUE - 1,
-                    SignalThresholdInfo.SIGNAL_RSSNR_MAX_VALUE + 1),
-            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP,
-            List.of(SignalThresholdInfo.SIGNAL_SSRSRP_MIN_VALUE - 1,
-                    SignalThresholdInfo.SIGNAL_SSRSRP_MAX_VALUE + 1),
-            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ,
-            List.of(SignalThresholdInfo.SIGNAL_SSRSRQ_MIN_VALUE - 1,
-                    SignalThresholdInfo.SIGNAL_SSRSRQ_MAX_VALUE + 1),
-            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR,
-            List.of(SignalThresholdInfo.SIGNAL_SSSINR_MIN_VALUE - 1,
-                    SignalThresholdInfo.SIGNAL_SSSINR_MAX_VALUE + 1)
-    );
+    private static final Map<Integer, List<Integer>> INVALID_THRESHOLDS_MAP =
+            Map.of(
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI,
+                    List.of(
+                            SignalThresholdInfo.SIGNAL_RSSI_MIN_VALUE - 1,
+                            SignalThresholdInfo.SIGNAL_RSSI_MAX_VALUE + 1),
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP,
+                    List.of(
+                            SignalThresholdInfo.SIGNAL_RSCP_MIN_VALUE - 1,
+                            SignalThresholdInfo.SIGNAL_RSCP_MAX_VALUE + 1),
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP,
+                    List.of(
+                            SignalThresholdInfo.SIGNAL_RSRP_MIN_VALUE - 1,
+                            SignalThresholdInfo.SIGNAL_RSRP_MAX_VALUE + 1),
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ,
+                    List.of(
+                            SignalThresholdInfo.SIGNAL_RSRQ_MIN_VALUE - 1,
+                            SignalThresholdInfo.SIGNAL_RSRQ_MAX_VALUE + 1),
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR,
+                    List.of(
+                            SignalThresholdInfo.SIGNAL_RSSNR_MIN_VALUE - 1,
+                            SignalThresholdInfo.SIGNAL_RSSNR_MAX_VALUE + 1),
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP,
+                    List.of(
+                            SignalThresholdInfo.SIGNAL_SSRSRP_MIN_VALUE - 1,
+                            SignalThresholdInfo.SIGNAL_SSRSRP_MAX_VALUE + 1),
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ,
+                    List.of(
+                            SignalThresholdInfo.SIGNAL_SSRSRQ_MIN_VALUE - 1,
+                            SignalThresholdInfo.SIGNAL_SSRSRQ_MAX_VALUE + 1),
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR,
+                    List.of(
+                            SignalThresholdInfo.SIGNAL_SSSINR_MIN_VALUE - 1,
+                            SignalThresholdInfo.SIGNAL_SSSINR_MAX_VALUE + 1),
+                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_ECNO,
+                    List.of(
+                            SignalThresholdInfo.SIGNAL_ECNO_MIN_VALUE - 1,
+                            SignalThresholdInfo.SIGNAL_ECNO_MAX_VALUE + 1));
 
     // Map of RAN to allowed SignalMeasurementType set.
     // RAN/TYPE pair will be used to verify the validation of the combo
-    private static final Map<Integer, Set<Integer>> VALID_RAN_TO_MEASUREMENT_TYPE_MAP = Map.of(
-            AccessNetworkConstants.AccessNetworkType.GERAN,
-            Set.of(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI),
-            AccessNetworkConstants.AccessNetworkType.CDMA2000,
-            Set.of(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI),
-            AccessNetworkConstants.AccessNetworkType.UTRAN,
-            Set.of(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP),
-            AccessNetworkConstants.AccessNetworkType.EUTRAN,
-            Set.of(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP,
-                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ,
-                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR),
-            AccessNetworkConstants.AccessNetworkType.NGRAN,
-            Set.of(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP,
-                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ,
-                    SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR)
-    );
+    private static final Map<Integer, Set<Integer>> VALID_RAN_TO_MEASUREMENT_TYPE_MAP =
+            Map.of(
+                    AccessNetworkConstants.AccessNetworkType.GERAN,
+                    Set.of(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI),
+                    AccessNetworkConstants.AccessNetworkType.CDMA2000,
+                    Set.of(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI),
+                    AccessNetworkConstants.AccessNetworkType.UTRAN,
+                    Set.of(
+                            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP,
+                            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_ECNO),
+                    AccessNetworkConstants.AccessNetworkType.EUTRAN,
+                    Set.of(
+                            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP,
+                            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ,
+                            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR),
+                    AccessNetworkConstants.AccessNetworkType.NGRAN,
+                    Set.of(
+                            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP,
+                            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ,
+                            SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR));
 
     // Deliberately picking up the max/min value in each range to test the edge cases
-    private final int[] mRssiThresholds = new int[]{-113, -103, -97, -51};
-    private final int[] mRscpThresholds = new int[]{-120, -105, -95, -25};
-    private final int[] mRsrpThresholds = new int[]{-140, -118, -108, -44};
-    private final int[] mRsrqThresholds = new int[]{-34, -17, -14, 3};
-    private final int[] mRssnrThresholds = new int[]{-20, 10, 20, 30};
-    private final int[] mSsrsrpThresholds = new int[]{-140, -118, -98, -44};
-    private final int[] mSsrsrqThresholds = new int[]{-43, -17, -14, 20};
-    private final int[] mSssinrThresholds = new int[]{-23, -16, -10, 40};
+    private final int[] mRssiThresholds = new int[] {-113, -103, -97, -51};
+    private final int[] mRscpThresholds = new int[] {-120, -105, -95, -25};
+    private final int[] mRsrpThresholds = new int[] {-140, -118, -108, -44};
+    private final int[] mRsrqThresholds = new int[] {-34, -17, -14, 3};
+    private final int[] mRssnrThresholds = new int[] {-20, 10, 20, 30};
+    private final int[] mSsrsrpThresholds = new int[] {-140, -118, -98, -44};
+    private final int[] mSsrsrqThresholds = new int[] {-43, -17, -14, 20};
+    private final int[] mSssinrThresholds = new int[] {-23, -16, -10, 40};
+    private final int[] mEcnoThresholds = new int[] {-24, -16, -8, 1};
 
-    private final int[][] mThresholds = {mRssiThresholds, mRscpThresholds, mRsrpThresholds,
-            mRsrqThresholds, mRssnrThresholds, mSsrsrpThresholds, mSsrsrqThresholds,
-            mSssinrThresholds};
+    private final int[][] mThresholds = {
+        mRssiThresholds,
+        mRscpThresholds,
+        mRsrpThresholds,
+        mRsrqThresholds,
+        mRssnrThresholds,
+        mSsrsrpThresholds,
+        mSsrsrqThresholds,
+        mSssinrThresholds,
+        mEcnoThresholds
+    };
 
     @Test
     @SmallTest
@@ -120,12 +145,14 @@
                         .setIsEnabled(false)
                         .build();
 
-        assertEquals(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP,
+        assertEquals(
+                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP,
                 signalThresholdInfo.getSignalMeasurementType());
         assertEquals(HYSTERESIS_MS, signalThresholdInfo.getHysteresisMs());
         assertEquals(HYSTERESIS_DB, signalThresholdInfo.getHysteresisDb());
-        assertEquals(Arrays.toString(SSRSRP_THRESHOLDS), Arrays.toString(
-                signalThresholdInfo.getThresholds()));
+        assertEquals(
+                Arrays.toString(SSRSRP_THRESHOLDS),
+                Arrays.toString(signalThresholdInfo.getThresholds()));
         assertFalse(signalThresholdInfo.isEnabled());
     }
 
@@ -160,68 +187,127 @@
     @SmallTest
     public void testGetSignalThresholdInfo() {
         ArrayList<SignalThresholdInfo> stList = new ArrayList<>();
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
-                .setHysteresisMs(0)
-                .setHysteresisDb(0)
-                .setThresholds(new int[]{}, true /*isSystem*/)
-                .setIsEnabled(false)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
-                .setHysteresisMs(HYSTERESIS_MS).setHysteresisDb(HYSTERESIS_DB)
-                .setThresholds(mRssiThresholds)
-                .setIsEnabled(false)
-                .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
+                        .setHysteresisMs(0)
+                        .setHysteresisDb(0)
+                        .setThresholds(new int[] {}, true /*isSystem*/)
+                        .setIsEnabled(false)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mRssiThresholds)
+                        .setIsEnabled(false)
+                        .build());
 
-        assertThat(stList.get(0).getThresholds()).isEqualTo(new int[]{});
-        assertThat(stList.get(1).getSignalMeasurementType()).isEqualTo(
-                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI);
+        assertThat(stList.get(0).getThresholds()).isEqualTo(new int[] {});
+        assertThat(stList.get(1).getSignalMeasurementType())
+                .isEqualTo(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI);
         assertThat(stList.get(1).getThresholds()).isEqualTo(mRssiThresholds);
+        assertThat(stList.get(0).getHysteresisDb())
+                .isEqualTo(SignalThresholdInfo.HYSTERESIS_DB_MINIMUM);
+        assertThat(stList.get(1).getHysteresisDb())
+                .isEqualTo(HYSTERESIS_DB);
     }
 
     @Test
     @SmallTest
     public void testEqualsSignalThresholdInfo() {
-        final int[] dummyThresholds = new int[]{-100, -90, -70, -60};
-        final int[] dummyThreholdsDisordered = new int[]{-60, -90, -100, -70};
-        SignalThresholdInfo st1 = new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(1).setSignalMeasurementType(1)
-                .setHysteresisMs(HYSTERESIS_MS).setHysteresisDb(HYSTERESIS_DB)
-                .setThresholds(mRssiThresholds).setIsEnabled(false)
-                .build();
-        SignalThresholdInfo st2 = new SignalThresholdInfo.Builder().setRadioAccessNetworkType(2)
-                .setSignalMeasurementType(2).setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB).setThresholds(mRssiThresholds).setIsEnabled(false)
-                .build();
-        SignalThresholdInfo st3 = new SignalThresholdInfo.Builder().setRadioAccessNetworkType(1)
-                .setSignalMeasurementType(1).setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB).setThresholds(dummyThresholds).setIsEnabled(false)
-                .build();
-        SignalThresholdInfo st4 = new SignalThresholdInfo.Builder().setRadioAccessNetworkType(1)
-                .setSignalMeasurementType(1).setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB).setThresholds(mRssiThresholds).setIsEnabled(false)
-                .build();
-        SignalThresholdInfo st5 = new SignalThresholdInfo.Builder().setRadioAccessNetworkType(1)
-                .setSignalMeasurementType(1).setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB).setThresholds(dummyThreholdsDisordered)
-                .setIsEnabled(false).build();
+        final int[] dummyThresholds = new int[] {-100, -90, -70, -60};
+        final int[] dummyThresholdsDisordered = new int[] {-60, -90, -100, -70};
+        SignalThresholdInfo st1 =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(1)
+                        .setSignalMeasurementType(1)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mRssiThresholds)
+                        .setIsEnabled(false)
+                        .build();
+        SignalThresholdInfo st2 =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(2)
+                        .setSignalMeasurementType(2)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mRssiThresholds)
+                        .setIsEnabled(false)
+                        .build();
+        SignalThresholdInfo st3 =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(1)
+                        .setSignalMeasurementType(1)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(dummyThresholds)
+                        .setIsEnabled(false)
+                        .build();
+        SignalThresholdInfo st4 =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(1)
+                        .setSignalMeasurementType(1)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mRssiThresholds)
+                        .setIsEnabled(false)
+                        .build();
+        SignalThresholdInfo st5 =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(1)
+                        .setSignalMeasurementType(1)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(dummyThresholdsDisordered)
+                        .setIsEnabled(false)
+                        .build();
 
-        //Return true if all SignalThresholdInfo values match.
+        // Return true if all SignalThresholdInfo values match.
         assertTrue(st1.equals(st1));
         assertFalse(st1.equals(st2));
         assertFalse(st1.equals(st3));
         assertTrue(st1.equals(st4));
-        //Threshold values ordering doesn't matter
+        // Threshold values ordering doesn't matter
         assertTrue(st3.equals(st5));
-        //Return false if the object of argument is other than SignalThresholdInfo.
+        // Return false if the object of argument is other than SignalThresholdInfo.
         assertFalse(st1.equals(new String("test")));
     }
 
     @Test
     @SmallTest
+    public void testHysteresisDbSettings_WithValidRange() {
+        SignalThresholdInfo st1 =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
+                        .setThresholds(new int[] {}, true)
+                        .build();
+        SignalThresholdInfo st2 =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
+                        .setThresholds(new int[] {}, true)
+                        .setHysteresisDb(3)
+                        .build();
+        SignalThresholdInfo st3 =
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
+                        .setThresholds(new int[] {}, true)
+                        .setHysteresisDb(1)
+                        .build();
+        assertThat(st1.getHysteresisDb()).isEqualTo(HYSTERESIS_DB);
+        assertThat(st2.getHysteresisDb()).isEqualTo(3);
+        assertThat(st3.getHysteresisDb()).isEqualTo(1);
+    }
+
+    @Test
+    @SmallTest
     public void testBuilderWithValidParameters() {
         ArrayList<SignalThresholdInfo> stList = buildSignalThresholdInfoWithPublicFields();
 
@@ -229,7 +315,7 @@
             SignalThresholdInfo st = stList.get(i);
             assertThat(st.getThresholds()).isEqualTo(mThresholds[i]);
             assertThat(st.getHysteresisMs()).isEqualTo(SignalThresholdInfo.HYSTERESIS_MS_DISABLED);
-            assertThat(st.getHysteresisDb()).isEqualTo(SignalThresholdInfo.HYSTERESIS_DB_DISABLED);
+            assertThat(st.getHysteresisDb()).isEqualTo(HYSTERESIS_DB);
             assertFalse(st.isEnabled());
         }
     }
@@ -238,33 +324,46 @@
     @SmallTest
     public void testBuilderWithInvalidParameter() {
         // Invalid signal measurement type
-        int[] invalidSignalMeasurementTypes = new int[]{-1, 0, 9};
+        int[] invalidSignalMeasurementTypes = new int[] {-1, 0, 9};
         for (int signalMeasurementType : invalidSignalMeasurementTypes) {
             buildWithInvalidParameterThrowException(
-                    AccessNetworkConstants.AccessNetworkType.GERAN, signalMeasurementType,
-                    new int[]{-1});
+                    AccessNetworkConstants.AccessNetworkType.GERAN,
+                    signalMeasurementType,
+                    new int[] {-1}, 2);
         }
 
         // Null thresholds array
-        buildWithInvalidParameterThrowException(AccessNetworkConstants.AccessNetworkType.GERAN,
-                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI, null);
+        buildWithInvalidParameterThrowException(
+                AccessNetworkConstants.AccessNetworkType.GERAN,
+                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI,
+                null, 0);
 
         // Empty thresholds
-        buildWithInvalidParameterThrowException(AccessNetworkConstants.AccessNetworkType.GERAN,
-                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI, new int[]{});
-
+        buildWithInvalidParameterThrowException(
+                AccessNetworkConstants.AccessNetworkType.GERAN,
+                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI,
+                new int[] {}, 5);
 
         // Too long thresholds array
-        buildWithInvalidParameterThrowException(AccessNetworkConstants.AccessNetworkType.GERAN,
+        buildWithInvalidParameterThrowException(
+                AccessNetworkConstants.AccessNetworkType.GERAN,
                 SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI,
-                new int[]{-100, -90, -70, -60, -58});
+                new int[] {-100, -90, -70, -60, -58}, 3);
+
+        // Test Hysteresis Db invalid Range
+        buildWithInvalidParameterThrowException(
+                AccessNetworkConstants.AccessNetworkType.GERAN,
+                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI,
+                new int[] {-100, -90, -70, -60}, -1);
 
         // Thresholds value out of range
         for (int signalMeasurementType : INVALID_THRESHOLDS_MAP.keySet()) {
             List<Integer> invalidThresholds = INVALID_THRESHOLDS_MAP.get(signalMeasurementType);
             for (int threshold : invalidThresholds) {
-                buildWithInvalidParameterThrowException(getValidRan(signalMeasurementType),
-                        signalMeasurementType, new int[]{threshold});
+                buildWithInvalidParameterThrowException(
+                        getValidRan(signalMeasurementType),
+                        signalMeasurementType,
+                        new int[] {threshold}, 1);
             }
         }
 
@@ -272,21 +371,23 @@
         for (int ran : VALID_RAN_TO_MEASUREMENT_TYPE_MAP.keySet()) {
             Set validTypes = VALID_RAN_TO_MEASUREMENT_TYPE_MAP.get(ran);
             for (int type = SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI;
-                    type <= SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR; type++) {
+                    type <= SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_ECNO;
+                    type++) {
                 if (!validTypes.contains(type)) {
-                    buildWithInvalidParameterThrowException(ran, type, new int[]{-1});
+                    buildWithInvalidParameterThrowException(ran, type, new int[] {-1}, 2);
                 }
             }
         }
     }
 
-    private void buildWithInvalidParameterThrowException(int ran, int signalMeasurementType,
-            int[] thresholds) {
+    private void buildWithInvalidParameterThrowException(
+            int ran, int signalMeasurementType, int[] thresholds, int hysteresisDb) {
         try {
             new SignalThresholdInfo.Builder()
                     .setRadioAccessNetworkType(ran)
                     .setSignalMeasurementType(signalMeasurementType)
                     .setThresholds(thresholds)
+                    .setHysteresisDb(hysteresisDb)
                     .build();
             fail("exception expected");
         } catch (IllegalArgumentException | NullPointerException expected) {
@@ -296,68 +397,90 @@
     private ArrayList<SignalThresholdInfo> buildSignalThresholdInfoWithAllFields() {
         ArrayList<SignalThresholdInfo> stList = new ArrayList<>();
 
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
-                .setHysteresisMs(HYSTERESIS_MS).setHysteresisDb(HYSTERESIS_DB)
-                .setThresholds(mRssiThresholds).setIsEnabled(false)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.UTRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP)
-                .setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB)
-                .setThresholds(mRscpThresholds)
-                .setIsEnabled(false)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP)
-                .setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB)
-                .setThresholds(mRsrpThresholds)
-                .setIsEnabled(false)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ)
-                .setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB)
-                .setThresholds(mRsrqThresholds)
-                .setIsEnabled(false)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR)
-                .setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB)
-                .setThresholds(mRssnrThresholds)
-                .setIsEnabled(false)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP)
-                .setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB)
-                .setThresholds(mSsrsrpThresholds)
-                .setIsEnabled(false)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ)
-                .setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB)
-                .setThresholds(mSsrsrqThresholds)
-                .setIsEnabled(false)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR)
-                .setHysteresisMs(HYSTERESIS_MS)
-                .setHysteresisDb(HYSTERESIS_DB)
-                .setThresholds(mSssinrThresholds)
-                .setIsEnabled(false)
-                .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mRssiThresholds)
+                        .setIsEnabled(false)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.UTRAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mRscpThresholds)
+                        .setIsEnabled(false)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mRsrpThresholds)
+                        .setIsEnabled(false)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mRsrqThresholds)
+                        .setIsEnabled(false)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mRssnrThresholds)
+                        .setIsEnabled(false)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
+                        .setSignalMeasurementType(
+                                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mSsrsrpThresholds)
+                        .setIsEnabled(false)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
+                        .setSignalMeasurementType(
+                                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mSsrsrqThresholds)
+                        .setIsEnabled(false)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
+                        .setSignalMeasurementType(
+                                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mSssinrThresholds)
+                        .setIsEnabled(false)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.UTRAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_ECNO)
+                        .setHysteresisMs(HYSTERESIS_MS)
+                        .setHysteresisDb(HYSTERESIS_DB)
+                        .setThresholds(mEcnoThresholds)
+                        .setIsEnabled(false)
+                        .build());
 
         return stList;
     }
@@ -365,46 +488,63 @@
     private ArrayList<SignalThresholdInfo> buildSignalThresholdInfoWithPublicFields() {
         ArrayList<SignalThresholdInfo> stList = new ArrayList<>();
 
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
-                .setThresholds(mRssiThresholds)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.UTRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP)
-                .setThresholds(mRscpThresholds)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP)
-                .setThresholds(mRsrpThresholds)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ)
-                .setThresholds(mRsrqThresholds)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR)
-                .setThresholds(mRssnrThresholds)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP)
-                .setThresholds(mSsrsrpThresholds)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ)
-                .setThresholds(mSsrsrqThresholds)
-                .build());
-        stList.add(new SignalThresholdInfo.Builder()
-                .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
-                .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR)
-                .setThresholds(mSssinrThresholds)
-                .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.GERAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI)
+                        .setThresholds(mRssiThresholds)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.UTRAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP)
+                        .setThresholds(mRscpThresholds)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP)
+                        .setThresholds(mRsrpThresholds)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ)
+                        .setThresholds(mRsrqThresholds)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.EUTRAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR)
+                        .setThresholds(mRssnrThresholds)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
+                        .setSignalMeasurementType(
+                                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP)
+                        .setThresholds(mSsrsrpThresholds)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
+                        .setSignalMeasurementType(
+                                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ)
+                        .setThresholds(mSsrsrqThresholds)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.NGRAN)
+                        .setSignalMeasurementType(
+                                SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR)
+                        .setThresholds(mSssinrThresholds)
+                        .build());
+        stList.add(
+                new SignalThresholdInfo.Builder()
+                        .setRadioAccessNetworkType(AccessNetworkConstants.AccessNetworkType.UTRAN)
+                        .setSignalMeasurementType(SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_ECNO)
+                        .setThresholds(mEcnoThresholds)
+                        .build());
 
         return stList;
     }
@@ -418,6 +558,7 @@
             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI:
                 return AccessNetworkConstants.AccessNetworkType.GERAN;
             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP:
+            case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_ECNO:
                 return AccessNetworkConstants.AccessNetworkType.UTRAN;
             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP:
             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ:
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SimulatedCommands.java b/tests/telephonytests/src/com/android/internal/telephony/SimulatedCommands.java
index 6a075da..3ca921b 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SimulatedCommands.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SimulatedCommands.java
@@ -21,6 +21,7 @@
 import android.hardware.radio.V1_0.DataRegStateResult;
 import android.hardware.radio.V1_0.SetupDataCallResult;
 import android.hardware.radio.V1_0.VoiceRegStateResult;
+import android.hardware.radio.modem.ImeiInfo;
 import android.net.KeepalivePacketData;
 import android.net.LinkProperties;
 import android.os.AsyncResult;
@@ -66,6 +67,8 @@
 import com.android.internal.telephony.RILUtils;
 import com.android.internal.telephony.RadioCapability;
 import com.android.internal.telephony.SmsResponse;
+import com.android.internal.telephony.SrvccConnection;
+import com.android.internal.telephony.test.SimulatedCommandsVerifier;
 import com.android.internal.telephony.UUSInfo;
 import com.android.internal.telephony.cdma.CdmaSmsBroadcastConfigInfo;
 import com.android.internal.telephony.gsm.SmsBroadcastConfigInfo;
@@ -186,8 +189,14 @@
     public boolean mSetRadioPowerForEmergencyCall;
     public boolean mSetRadioPowerAsSelectedPhoneForEmergencyCall;
 
+    public boolean mCallWaitActivated = false;
+    private SrvccConnection[] mSrvccConnections;
+
     // mode for Icc Sim Authentication
     private int mAuthenticationMode;
+
+    private int[] mImsRegistrationInfo = new int[4];
+
     //***** Constructor
     public
     SimulatedCommands() {
@@ -1432,6 +1441,14 @@
 
     @Override
     public void queryCallWaiting(int serviceClass, Message response) {
+        if (response != null && serviceClass == SERVICE_CLASS_NONE) {
+            int[] r = new int[2];
+            r[0] = (mCallWaitActivated ? 1 : 0);
+            r[1] = (mCallWaitActivated ? SERVICE_CLASS_VOICE : SERVICE_CLASS_NONE);
+            resultSuccess(response, r);
+            return;
+        }
+
         unimplemented(response);
     }
 
@@ -1440,11 +1457,15 @@
      * @param serviceClass is a sum of SERVICE_CLASS_*
      * @param response is callback message
      */
-
     @Override
     public void setCallWaiting(boolean enable, int serviceClass,
             Message response) {
-        unimplemented(response);
+        if ((serviceClass & SERVICE_CLASS_VOICE) == SERVICE_CLASS_VOICE) {
+            mCallWaitActivated = enable;
+        }
+        if (response != null) {
+            resultSuccess(response, null);
+        }
     }
 
     /**
@@ -1792,6 +1813,16 @@
     }
 
     @Override
+    public void getImei(Message response) {
+        SimulatedCommandsVerifier.getInstance().getImei(response);
+        ImeiInfo imeiInfo = new ImeiInfo();
+        imeiInfo.imei = FAKE_IMEI;
+        imeiInfo.svn = FAKE_IMEISV;
+        imeiInfo.type = ImeiInfo.ImeiType.SECONDARY;
+        resultSuccess(response, imeiInfo);
+    }
+
+    @Override
     public void
     getCDMASubscription(Message result) {
         String ret[] = new String[5];
@@ -2153,6 +2184,11 @@
     }
 
     @Override
+    public void iccTransmitApduLogicalChannel(int channel, int cla, int instruction,
+            int p1, int p2, int p3, String data, boolean isEs10Command, Message response) {
+    }
+
+    @Override
     public void iccTransmitApduBasicChannel(int cla, int instruction, int p1, int p2,
             int p3, String data, Message response) {
         unimplemented(response);
@@ -2558,4 +2594,26 @@
         PcoData response = new PcoData(cid, bearerProto, pcoId, contents);
         mPcoDataRegistrants.notifyRegistrants(new AsyncResult(null, response, null));
     }
+
+    @Override
+    public void setSrvccCallInfo(SrvccConnection[] srvccConnections, Message result) {
+        mSrvccConnections = srvccConnections;
+    }
+
+    public SrvccConnection[] getSrvccConnections() {
+        return mSrvccConnections;
+    }
+
+    @Override
+    public void updateImsRegistrationInfo(int regState,
+            int imsRadioTech, int suggestedAction, int capabilities, Message result) {
+        mImsRegistrationInfo[0] = regState;
+        mImsRegistrationInfo[1] = imsRadioTech;
+        mImsRegistrationInfo[2] = suggestedAction;
+        mImsRegistrationInfo[3] = capabilities;
+    }
+
+    public int[] getImsRegistrationInfo() {
+        return mImsRegistrationInfo;
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SimulatedCommandsVerifier.java b/tests/telephonytests/src/com/android/internal/telephony/SimulatedCommandsVerifier.java
index 60add09..f565006 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SimulatedCommandsVerifier.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SimulatedCommandsVerifier.java
@@ -1160,6 +1160,11 @@
     }
 
     @Override
+    public void getImei(Message response) {
+
+    }
+
+    @Override
     public void getCDMASubscription(Message response) {
 
     }
@@ -1310,6 +1315,13 @@
     }
 
     @Override
+    public void iccTransmitApduLogicalChannel(int channel, int cla, int instruction, int p1,
+                                              int p2, int p3, String data,
+                                              boolean isEs10Command, Message response) {
+
+    }
+
+    @Override
     public void iccTransmitApduBasicChannel(int cla, int instruction, int p1, int p2, int p3,
                                             String data, Message response) {
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SmsControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/SmsControllerTest.java
new file mode 100644
index 0000000..ab29076
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/SmsControllerTest.java
@@ -0,0 +1,210 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.uicc.AdnRecord;
+import com.android.internal.telephony.uicc.AdnRecordCache;
+import com.android.internal.telephony.uicc.IccConstants;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+
+import java.util.ArrayList;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class SmsControllerTest extends TelephonyTest {
+
+    // Mocked classes
+    private AdnRecordCache mAdnRecordCache;
+
+    // SmsController under test
+    private SmsController mSmsControllerUT;
+    private final String smscAddrStr = "+1206313004";
+    private String mCallingPackage;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(getClass().getSimpleName());
+        mAdnRecordCache = Mockito.mock(AdnRecordCache.class);
+        mSmsControllerUT = new SmsController(mContext);
+        mCallingPackage = mContext.getOpPackageName();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mAdnRecordCache = null;
+        super.tearDown();
+    }
+
+    private void fdnCheckSetup() {
+        // FDN check setup
+        doReturn(mAdnRecordCache).when(mSimRecords).getAdnCache();
+        doReturn(mUiccProfile).when(mUiccController).getUiccProfileForPhone(anyInt());
+        doReturn(true).when(mUiccCardApplication3gpp).getIccFdnAvailable();
+        doReturn(true).when(mUiccCardApplication3gpp).getIccFdnEnabled();
+        doReturn(false).when(mTelephonyManager).isEmergencyNumber(anyString());
+        doReturn("us").when(mTelephonyManager).getSimCountryIso();
+        doReturn(smscAddrStr).when(mIccSmsInterfaceManager).getSmscAddressFromIccEf(anyString());
+    }
+
+    private void fdnCheckCleanup() {
+        doReturn(false).when(mUiccCardApplication3gpp).getIccFdnAvailable();
+        doReturn(false).when(mUiccCardApplication3gpp).getIccFdnEnabled();
+    }
+
+    @Test
+    public void isNumberBlockedByFdn_fdnListHasBothDestAddrAndSmscAddr() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(mAdnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+
+        // FDN list has both destination addr and smsc addr
+        AdnRecord smscAddrRecord = new AdnRecord(null, smscAddrStr);
+        AdnRecord destAddrRecord = new AdnRecord(null, "1234");
+        fdnList.add(0, smscAddrRecord);
+        fdnList.add(1, destAddrRecord);
+
+        // Returns false as list contains both dest addr and smsc addr
+        assertFalse(mSmsControllerUT.isNumberBlockedByFDN(1, "1234",
+                mCallingPackage));
+
+        // Clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void isNumberBlockedByFdn_fdnListHasDestAddr() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(mAdnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+
+        // FDN list has only destination addr
+        AdnRecord destAddrRecord = new AdnRecord(null, "1234");
+        fdnList.add(0, destAddrRecord);
+
+        // Returns true as list does not contain smsc addr
+        assertTrue(mSmsControllerUT.isNumberBlockedByFDN(1, "1234", mCallingPackage));
+
+        // Clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void isNumberBlockedByFdn_fdnListHasSmscAddr() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(mAdnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+
+        // FDN list has both destination addr and smsc addr
+        AdnRecord smscAddrRecord = new AdnRecord(null, smscAddrStr);
+        fdnList.add(0, smscAddrRecord);
+
+        // Returns true as list does not contain dest addr
+        assertTrue(mSmsControllerUT.isNumberBlockedByFDN(1, "1234", mCallingPackage));
+
+        // Clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void isNumberBlockedByFdn_destAddrIsEmergencyNumber() {
+        // FDN check setup
+        fdnCheckSetup();
+        ArrayList<AdnRecord> fdnList = new ArrayList<>();
+        doReturn(fdnList).when(mAdnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);
+
+        doReturn(true).when(mTelephonyManager).isEmergencyNumber(anyString());
+        // Returns false as dest addr is emergency number
+        assertFalse(mSmsControllerUT.isNumberBlockedByFDN(1, "1234",
+                mCallingPackage));
+
+        // Clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void isNumberBlockedByFdn_fdnDisabled() {
+        // FDN check setup
+        fdnCheckSetup();
+
+        doReturn(false).when(mUiccCardApplication3gpp).getIccFdnEnabled();
+        // Returns false as fdn is not enabled
+        assertFalse(mSmsControllerUT.isNumberBlockedByFDN(1, "1234",
+                mCallingPackage));
+
+        // Clean up
+        fdnCheckCleanup();
+    }
+
+    @Test
+    public void sendVisualVoicemailSmsForSubscriber_phoneIsNotInEcm() {
+        assertFalse(mPhone.isInEcm());
+        int subId = 1;
+        doReturn(true).when(mSubscriptionManager)
+                .isSubscriptionAssociatedWithUser(eq(subId), any());
+
+        mSmsControllerUT.sendVisualVoicemailSmsForSubscriber(mCallingPackage,null ,
+                subId, null, 0, null, null);
+        verify(mIccSmsInterfaceManager).sendTextWithSelfPermissions(any(),
+                any(), any(), any(), any(), any(), any(), eq(false), eq(true));
+    }
+
+    @Test
+    public void sendVisualVoicemailSmsForSubscriber_phoneIsInEcm() {
+        doReturn(true).when(mPhone).isInEcm();
+
+        mSmsControllerUT.sendVisualVoicemailSmsForSubscriber(mCallingPackage,null ,
+                1, null, 0, null, null);
+        verify(mIccSmsInterfaceManager, never()).sendTextWithSelfPermissions(any(),
+                any(), any(), any(), any(), any(), any(), eq(false), eq(true));
+
+        doReturn(false).when(mPhone).isInEcm();
+    }
+
+    @Test
+    public void sendsendTextForSubscriberTest() {
+        int subId = 1;
+        doReturn(true).when(mSubscriptionManager)
+                .isSubscriptionAssociatedWithUser(eq(subId), any());
+
+        mSmsControllerUT.sendTextForSubscriber(subId, mCallingPackage, null, "1234",
+                null, "text", null, null, false, 0L, true, true);
+        verify(mIccSmsInterfaceManager, Mockito.times(1))
+                .sendText(mCallingPackage, "1234", null, "text", null, null, false, 0L, true);
+    }
+
+}
\ No newline at end of file
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SmsDispatchersControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/SmsDispatchersControllerTest.java
index 6ef8508..427963b 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SmsDispatchersControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SmsDispatchersControllerTest.java
@@ -19,6 +19,8 @@
 import static com.android.internal.telephony.SmsResponse.NO_ERROR_CODE;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
@@ -28,13 +30,24 @@
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.eq;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import android.annotation.Nullable;
 import android.app.ActivityManager;
+import android.app.PendingIntent;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.AsyncResult;
+import android.os.Looper;
 import android.os.Message;
 import android.provider.Telephony.Sms.Intents;
+import android.telephony.DisconnectCause;
+import android.telephony.DomainSelectionService;
+import android.telephony.NetworkRegistrationInfo;
+import android.telephony.PhoneNumberUtils;
 import android.telephony.SmsManager;
 import android.test.FlakyTest;
 import android.test.suitebuilder.annotation.SmallTest;
@@ -42,36 +55,140 @@
 import android.testing.TestableLooper;
 import android.util.Singleton;
 
+import com.android.ims.ImsManager;
+import com.android.internal.telephony.domainselection.DomainSelectionConnection;
+import com.android.internal.telephony.domainselection.EmergencySmsDomainSelectionConnection;
+import com.android.internal.telephony.domainselection.SmsDomainSelectionConnection;
+import com.android.internal.telephony.uicc.IccUtils;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.concurrent.CompletableFuture;
 
 @RunWith(AndroidTestingRunner.class)
 @TestableLooper.RunWithLooper
 public class SmsDispatchersControllerTest extends TelephonyTest {
+    /**
+     * Inherits the SmsDispatchersController to verify the protected methods.
+     */
+    private static class TestSmsDispatchersController extends SmsDispatchersController {
+        TestSmsDispatchersController(Phone phone, SmsStorageMonitor storageMonitor,
+                SmsUsageMonitor usageMonitor, Looper looper) {
+            super(phone, storageMonitor, usageMonitor, looper);
+        }
+
+        public DomainSelectionConnectionHolder testGetDomainSelectionConnectionHolder(
+                boolean emergency) {
+            return getDomainSelectionConnectionHolder(emergency);
+        }
+
+        public void testSendData(String callingPackage, String destAddr, String scAddr,
+                int destPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent,
+                boolean isForVvm) {
+            sendData(callingPackage, destAddr, scAddr,
+                    destPort, data, sentIntent, deliveryIntent, isForVvm);
+        }
+
+        public void testSendMultipartText(String destAddr, String scAddr,
+                ArrayList<String> parts, ArrayList<PendingIntent> sentIntents,
+                ArrayList<PendingIntent> deliveryIntents, Uri messageUri, String callingPkg,
+                boolean persistMessage, int priority, boolean expectMore, int validityPeriod,
+                long messageId) {
+            sendMultipartText(destAddr, scAddr, parts, sentIntents, deliveryIntents, messageUri,
+                    callingPkg, persistMessage, priority, expectMore, validityPeriod, messageId);
+        }
+    }
+
+    /**
+     * Inherits the SMSDispatcher to verify the abstract or protected methods.
+     */
+    protected abstract static class TestSmsDispatcher extends SMSDispatcher {
+        public TestSmsDispatcher(Phone phone, SmsDispatchersController smsDispatchersController) {
+            super(phone, smsDispatchersController);
+        }
+
+        @Override
+        public void sendData(String callingPackage, String destAddr, String scAddr, int destPort,
+                byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent,
+                boolean isForVvm) {
+            super.sendData(callingPackage, destAddr, scAddr, destPort,
+                    data, sentIntent, deliveryIntent, isForVvm);
+        }
+
+        @Override
+        public void sendSms(SmsTracker tracker) {
+        }
+
+        @Override
+        public String getFormat() {
+            return SmsConstants.FORMAT_3GPP;
+        }
+    }
+
+    /**
+     * Inherits the SMSDispatcher to verify the protected methods.
+     */
+    protected static class TestImsSmsDispatcher extends ImsSmsDispatcher {
+        public TestImsSmsDispatcher(Phone phone, SmsDispatchersController smsDispatchersController,
+                FeatureConnectorFactory factory) {
+            super(phone, smsDispatchersController, factory);
+        }
+
+        @Override
+        public void sendData(String callingPackage, String destAddr, String scAddr, int destPort,
+                byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent,
+                boolean isForVvm) {
+            super.sendData(callingPackage, destAddr, scAddr, destPort,
+                    data, sentIntent, deliveryIntent, isForVvm);
+        }
+
+        @Override
+        public String getFormat() {
+            return SmsConstants.FORMAT_3GPP;
+        }
+    }
+
+    private static final String ACTION_TEST_SMS_SENT = "TEST_SMS_SENT";
+
     // Mocked classes
     private SMSDispatcher.SmsTracker mTracker;
+    private PendingIntent mSentIntent;
+    private TestImsSmsDispatcher mImsSmsDispatcher;
+    private TestSmsDispatcher mGsmSmsDispatcher;
+    private TestSmsDispatcher mCdmaSmsDispatcher;
+    private SmsDomainSelectionConnection mSmsDsc;
+    private EmergencySmsDomainSelectionConnection mEmergencySmsDsc;
 
-    private SmsDispatchersController mSmsDispatchersController;
+    private TestSmsDispatchersController mSmsDispatchersController;
     private boolean mInjectionCallbackTriggered = false;
+    private CompletableFuture<Integer> mDscFuture;
 
     @Before
     public void setUp() throws Exception {
         super.setUp(getClass().getSimpleName());
         mTracker = mock(SMSDispatcher.SmsTracker.class);
         setupMockPackagePermissionChecks();
-
-        mSmsDispatchersController = new SmsDispatchersController(mPhone, mSmsStorageMonitor,
-            mSmsUsageMonitor);
+        mSmsDispatchersController = new TestSmsDispatchersController(mPhone, mSmsStorageMonitor,
+            mSmsUsageMonitor, mTestableLooper.getLooper());
+        setUpDomainSelectionConnectionAsNotSupported();
         processAllMessages();
     }
 
     @After
     public void tearDown() throws Exception {
+        mImsSmsDispatcher = null;
+        mGsmSmsDispatcher = null;
+        mCdmaSmsDispatcher = null;
+        mSmsDsc = null;
+        mEmergencySmsDsc = null;
+        mDscFuture = null;
         mSmsDispatchersController.dispose();
         mSmsDispatchersController = null;
         super.tearDown();
@@ -91,6 +208,30 @@
         assertTrue(mSmsDispatchersController.isIms());
     }
 
+    @Test @SmallTest
+    public void testReportSmsMemoryStatus() throws Exception {
+        int eventReportMemoryStatusDone = 3;
+        SmsStorageMonitor smsStorageMonnitor = new SmsStorageMonitor(mPhone);
+        Message result = smsStorageMonnitor.obtainMessage(eventReportMemoryStatusDone);
+        ImsSmsDispatcher mImsSmsDispatcher = Mockito.mock(ImsSmsDispatcher.class);
+        mSmsDispatchersController.setImsSmsDispatcher(mImsSmsDispatcher);
+        mSmsDispatchersController.reportSmsMemoryStatus(result);
+        AsyncResult ar = (AsyncResult) result.obj;
+        verify(mImsSmsDispatcher).onMemoryAvailable();
+        assertNull(ar.exception);
+    }
+
+    @Test @SmallTest
+    public void testReportSmsMemoryStatusFailure() throws Exception {
+        int eventReportMemoryStatusDone = 3;
+        SmsStorageMonitor smsStorageMonnitor = new SmsStorageMonitor(mPhone);
+        Message result = smsStorageMonnitor.obtainMessage(eventReportMemoryStatusDone);
+        mSmsDispatchersController.setImsSmsDispatcher(null);
+        mSmsDispatchersController.reportSmsMemoryStatus(result);
+        AsyncResult ar = (AsyncResult) result.obj;
+        assertNotNull(ar.exception);
+    }
+
     @Test @SmallTest @FlakyTest
     public void testSendImsGmsTest() throws Exception {
         switchImsSmsFormat(PhoneConstants.PHONE_TYPE_GSM);
@@ -179,6 +320,225 @@
         assertEquals(true, mInjectionCallbackTriggered);
     }
 
+    @Test @SmallTest
+    public void testSendImsGmsTestWithSmsc() {
+        IccSmsInterfaceManager iccSmsInterfaceManager = Mockito.mock(IccSmsInterfaceManager.class);
+        when(mPhone.getIccSmsInterfaceManager()).thenReturn(iccSmsInterfaceManager);
+        when(iccSmsInterfaceManager.getSmscAddressFromIccEf("com.android.messaging"))
+                .thenReturn("222");
+        switchImsSmsFormat(PhoneConstants.PHONE_TYPE_GSM);
+
+        mSmsDispatchersController.sendText("111", null /*scAddr*/, TAG,
+                null, null, null, "com.android.messaging",
+                false, -1, false, -1, false, 0L);
+        byte[] smscbyte = PhoneNumberUtils.networkPortionToCalledPartyBCDWithLength(
+                "222");
+        String smsc = IccUtils.bytesToHexString(smscbyte);
+        verify(mSimulatedCommandsVerifier).sendImsGsmSms(eq(smsc), anyString(),
+                anyInt(), anyInt(), any(Message.class));
+    }
+
+    @Test
+    @SmallTest
+    public void testSendDataWhenDomainPs() throws Exception {
+        sendDataWithDomainSelection(NetworkRegistrationInfo.DOMAIN_PS, false);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendDataWhenDomainCsAndCdma() throws Exception {
+        when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_CDMA);
+        sendDataWithDomainSelection(NetworkRegistrationInfo.DOMAIN_PS, true);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendDataWhenDomainCsAndGsm() throws Exception {
+        when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM);
+        sendDataWithDomainSelection(NetworkRegistrationInfo.DOMAIN_PS, false);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendTextWhenDomainPs() throws Exception {
+        sendTextWithDomainSelection(NetworkRegistrationInfo.DOMAIN_PS, false);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendTextWhenDomainCsAndCdma() throws Exception {
+        when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_CDMA);
+        sendTextWithDomainSelection(NetworkRegistrationInfo.DOMAIN_PS, true);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendTextWhenDomainCsAndGsm() throws Exception {
+        when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM);
+        sendTextWithDomainSelection(NetworkRegistrationInfo.DOMAIN_PS, false);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendMultipartTextWhenDomainPs() throws Exception {
+        sendMultipartTextWithDomainSelection(NetworkRegistrationInfo.DOMAIN_PS, false);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendMultipartTextWhenDomainCsAndCdma() throws Exception {
+        when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_CDMA);
+        sendMultipartTextWithDomainSelection(NetworkRegistrationInfo.DOMAIN_PS, true);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendMultipartTextWhenDomainCsAndGsm() throws Exception {
+        when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM);
+        sendMultipartTextWithDomainSelection(NetworkRegistrationInfo.DOMAIN_PS, false);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendRetrySmsWhenDomainPs() throws Exception {
+        sendRetrySmsWithDomainSelection(NetworkRegistrationInfo.DOMAIN_PS,
+                PhoneConstants.PHONE_TYPE_GSM, SmsConstants.FORMAT_3GPP);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendRetrySmsWhenDomainCsAndCdma() throws Exception {
+        sendRetrySmsWithDomainSelection(NetworkRegistrationInfo.DOMAIN_CS,
+                PhoneConstants.PHONE_TYPE_CDMA, SmsConstants.FORMAT_3GPP2);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendRetrySmsWhenDomainCsAndGsm() throws Exception {
+        sendRetrySmsWithDomainSelection(NetworkRegistrationInfo.DOMAIN_CS,
+                PhoneConstants.PHONE_TYPE_GSM, SmsConstants.FORMAT_3GPP);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendRetrySmsWhenImsAlreadyUsedAndCdma() throws Exception {
+        sendRetrySmsWhenImsAlreadyUsed(PhoneConstants.PHONE_TYPE_CDMA, SmsConstants.FORMAT_3GPP2);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendRetrySmsWhenImsAlreadyUsedAndGsm() throws Exception {
+        sendRetrySmsWhenImsAlreadyUsed(PhoneConstants.PHONE_TYPE_GSM, SmsConstants.FORMAT_3GPP);
+    }
+
+    @Test
+    @SmallTest
+    public void testSendEmergencyTextWhenDomainPs() throws Exception {
+        setUpDomainSelectionConnection();
+        setUpSmsDispatchers();
+
+        mSmsDispatchersController.sendText("911", "2222", "text", mSentIntent, null, null,
+                "test-app", false, 0, false, 10, false, 1L, false);
+
+        SmsDispatchersController.DomainSelectionConnectionHolder holder =
+                mSmsDispatchersController.testGetDomainSelectionConnectionHolder(true);
+        verify(mEmergencySmsDsc).requestDomainSelection(any(), any());
+        assertNotNull(holder);
+        assertNotNull(holder.getConnection());
+        assertTrue(holder.isEmergency());
+        assertTrue(holder.isDomainSelectionRequested());
+        assertEquals(1, holder.getPendingRequests().size());
+
+        mDscFuture.complete(NetworkRegistrationInfo.DOMAIN_PS);
+        processAllMessages();
+
+        verify(mEmergencySmsDsc).finishSelection();
+        verify(mImsSmsDispatcher).sendText(eq("911"), eq("2222"), eq("text"), eq(mSentIntent),
+                any(), any(), eq("test-app"), eq(false), eq(0), eq(false), eq(10), eq(false),
+                eq(1L), eq(false));
+        assertNull(holder.getConnection());
+        assertFalse(holder.isDomainSelectionRequested());
+        assertEquals(0, holder.getPendingRequests().size());
+    }
+
+    @Test
+    @SmallTest
+    public void testNotifyDomainSelectionTerminated() throws Exception {
+        setUpDomainSelectionConnection();
+        setUpSmsDispatchers();
+
+        mSmsDispatchersController.sendText("1111", "2222", "text", mSentIntent, null, null,
+                "test-app", false, 0, false, 10, false, 1L, false);
+
+        SmsDispatchersController.DomainSelectionConnectionHolder holder =
+                mSmsDispatchersController.testGetDomainSelectionConnectionHolder(false);
+        ArgumentCaptor<DomainSelectionConnection.DomainSelectionConnectionCallback> captor =
+                ArgumentCaptor.forClass(
+                        DomainSelectionConnection.DomainSelectionConnectionCallback.class);
+        verify(mSmsDsc).requestDomainSelection(any(), captor.capture());
+        assertNotNull(holder);
+        assertNotNull(holder.getConnection());
+        assertTrue(holder.isDomainSelectionRequested());
+        assertEquals(1, holder.getPendingRequests().size());
+
+        DomainSelectionConnection.DomainSelectionConnectionCallback callback = captor.getValue();
+        assertNotNull(callback);
+
+        mSmsDispatchersController.post(() -> {
+            callback.onSelectionTerminated(DisconnectCause.LOCAL);
+        });
+        processAllMessages();
+
+        verify(mSmsDsc, never()).finishSelection();
+        assertNull(holder.getConnection());
+        assertFalse(holder.isDomainSelectionRequested());
+        assertEquals(0, holder.getPendingRequests().size());
+
+        // We can use the IntentReceiver for receiving the sent result, but it can be reported as
+        // a flaky test since sometimes broadcasts can take a long time if the system is under load.
+        // At this point, we couldn't use the PendingIntent as a mock because it's a final class
+        // so this test checks the method in the IActivityManager when the PendingIntent#send(int)
+        // is called.
+        verify(mIActivityManager).sendIntentSender(any(), any(), any(),
+                eq(SmsManager.RESULT_ERROR_GENERIC_FAILURE), any(), any(), any(), any(), any());
+    }
+
+    @Test
+    @SmallTest
+    public void testSendTextContinuously() throws Exception {
+        setUpDomainSelectionConnection();
+        setUpSmsDispatchers();
+
+        mSmsDispatchersController.sendText("1111", "2222", "text", mSentIntent, null, null,
+                "test-app", false, 0, false, 10, false, 1L, false);
+
+        SmsDispatchersController.DomainSelectionConnectionHolder holder =
+                mSmsDispatchersController.testGetDomainSelectionConnectionHolder(false);
+        assertNotNull(holder);
+        assertNotNull(holder.getConnection());
+        assertTrue(holder.isDomainSelectionRequested());
+        assertEquals(1, holder.getPendingRequests().size());
+
+        mSmsDispatchersController.sendText("1111", "2222", "text", mSentIntent, null, null,
+                "test-app", false, 0, false, 10, false, 1L, false);
+
+        verify(mSmsDsc).requestDomainSelection(any(), any());
+        assertNotNull(holder.getConnection());
+        assertTrue(holder.isDomainSelectionRequested());
+        assertEquals(2, holder.getPendingRequests().size());
+
+        mDscFuture.complete(NetworkRegistrationInfo.DOMAIN_PS);
+        processAllMessages();
+
+        verify(mSmsDsc).finishSelection();
+        verify(mImsSmsDispatcher, times(2)).sendText(eq("1111"), eq("2222"), eq("text"),
+                eq(mSentIntent), any(), any(), eq("test-app"), eq(false), eq(0), eq(false), eq(10),
+                eq(false), eq(1L), eq(false));
+        assertNull(holder.getConnection());
+        assertFalse(holder.isDomainSelectionRequested());
+        assertEquals(0, holder.getPendingRequests().size());
+    }
+
     private void switchImsSmsFormat(int phoneType) {
         mSimulatedCommands.setImsRegistrationState(new int[]{1, phoneType});
         mSimulatedCommands.notifyImsNetworkStateChanged();
@@ -186,4 +546,248 @@
         processAllMessages();
         assertTrue(mSmsDispatchersController.isIms());
     }
+
+    @Test
+    public void testSetImsManager() {
+        ImsManager imsManager = mock(ImsManager.class);
+        assertTrue(mSmsDispatchersController.setImsManager(imsManager));
+    }
+
+    private void setUpDomainSelectionConnectionAsNotSupported() {
+        mSmsDispatchersController.setDomainSelectionResolverProxy(
+                new SmsDispatchersController.DomainSelectionResolverProxy() {
+                    @Override
+                    @Nullable
+                    public DomainSelectionConnection getDomainSelectionConnection(Phone phone,
+                            @DomainSelectionService.SelectorType int selectorType,
+                            boolean isEmergency) {
+                        return null;
+                    }
+
+                    @Override
+                    public boolean isDomainSelectionSupported() {
+                        return false;
+                    }
+                });
+    }
+
+    private void setUpDomainSelectionConnection()  {
+        mEmergencySmsDsc = Mockito.mock(EmergencySmsDomainSelectionConnection.class);
+        mSmsDsc = Mockito.mock(SmsDomainSelectionConnection.class);
+        mSmsDispatchersController.setDomainSelectionResolverProxy(
+                new SmsDispatchersController.DomainSelectionResolverProxy() {
+                    @Override
+                    @Nullable
+                    public DomainSelectionConnection getDomainSelectionConnection(Phone phone,
+                            @DomainSelectionService.SelectorType int selectorType,
+                            boolean isEmergency) {
+                        return isEmergency ? mEmergencySmsDsc : mSmsDsc;
+                    }
+
+                    @Override
+                    public boolean isDomainSelectionSupported() {
+                        return true;
+                    }
+                });
+
+        mDscFuture = new CompletableFuture<>();
+        when(mSmsDsc.requestDomainSelection(
+                any(DomainSelectionService.SelectionAttributes.class),
+                any(DomainSelectionConnection.DomainSelectionConnectionCallback.class)))
+                .thenReturn(mDscFuture);
+        when(mEmergencySmsDsc.requestDomainSelection(
+                any(DomainSelectionService.SelectionAttributes.class),
+                any(DomainSelectionConnection.DomainSelectionConnectionCallback.class)))
+                .thenReturn(mDscFuture);
+    }
+
+    private void setUpSmsDispatchers() throws Exception {
+        mImsSmsDispatcher = Mockito.mock(TestImsSmsDispatcher.class);
+        mGsmSmsDispatcher = Mockito.mock(TestSmsDispatcher.class);
+        mCdmaSmsDispatcher = Mockito.mock(TestSmsDispatcher.class);
+
+        replaceInstance(SmsDispatchersController.class, "mImsSmsDispatcher",
+                mSmsDispatchersController, mImsSmsDispatcher);
+        replaceInstance(SmsDispatchersController.class, "mGsmDispatcher",
+                mSmsDispatchersController, mGsmSmsDispatcher);
+        replaceInstance(SmsDispatchersController.class, "mCdmaDispatcher",
+                mSmsDispatchersController, mCdmaSmsDispatcher);
+
+        when(mTelephonyManager.isEmergencyNumber(eq("911"))).thenReturn(true);
+
+        mSentIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 0,
+                new Intent(ACTION_TEST_SMS_SENT), PendingIntent.FLAG_MUTABLE);
+    }
+
+    private void sendDataWithDomainSelection(@NetworkRegistrationInfo.Domain int domain,
+            boolean isCdmaMo) throws Exception {
+        setUpDomainSelectionConnection();
+        setUpSmsDispatchers();
+
+        byte[] data = new byte[] { 0x01 };
+        mSmsDispatchersController.testSendData(
+                "test-app", "1111", "2222", 8080, data, mSentIntent, null, false);
+
+        SmsDispatchersController.DomainSelectionConnectionHolder holder =
+                mSmsDispatchersController.testGetDomainSelectionConnectionHolder(false);
+        verify(mSmsDsc).requestDomainSelection(any(), any());
+        assertNotNull(holder);
+        assertNotNull(holder.getConnection());
+        assertTrue(holder.isDomainSelectionRequested());
+        assertEquals(1, holder.getPendingRequests().size());
+
+        mDscFuture.complete(domain);
+        processAllMessages();
+
+        verify(mSmsDsc).finishSelection();
+        if (domain == NetworkRegistrationInfo.DOMAIN_PS) {
+            verify(mImsSmsDispatcher).sendData(eq("test-app"), eq("1111"), eq("2222"), eq(8080),
+                    eq(data), eq(mSentIntent), any(), eq(false));
+        } else if (isCdmaMo) {
+            verify(mCdmaSmsDispatcher).sendData(eq("test-app"), eq("1111"), eq("2222"), eq(8080),
+                    eq(data), eq(mSentIntent), any(), eq(false));
+        } else {
+            verify(mGsmSmsDispatcher).sendData(eq("test-app"), eq("1111"), eq("2222"), eq(8080),
+                    eq(data), eq(mSentIntent), any(), eq(false));
+        }
+        assertNull(holder.getConnection());
+        assertFalse(holder.isDomainSelectionRequested());
+        assertEquals(0, holder.getPendingRequests().size());
+    }
+
+    private void sendTextWithDomainSelection(@NetworkRegistrationInfo.Domain int domain,
+            boolean isCdmaMo) throws Exception {
+        setUpDomainSelectionConnection();
+        setUpSmsDispatchers();
+
+        mSmsDispatchersController.sendText("1111", "2222", "text", mSentIntent, null, null,
+                "test-app", false, 0, false, 10, false, 1L, false);
+
+        SmsDispatchersController.DomainSelectionConnectionHolder holder =
+                mSmsDispatchersController.testGetDomainSelectionConnectionHolder(false);
+        verify(mSmsDsc).requestDomainSelection(any(), any());
+        assertNotNull(holder);
+        assertNotNull(holder.getConnection());
+        assertTrue(holder.isDomainSelectionRequested());
+        assertEquals(1, holder.getPendingRequests().size());
+
+        mDscFuture.complete(domain);
+        processAllMessages();
+
+        verify(mSmsDsc).finishSelection();
+        if (domain == NetworkRegistrationInfo.DOMAIN_PS) {
+            verify(mImsSmsDispatcher).sendText(eq("1111"), eq("2222"), eq("text"), eq(mSentIntent),
+                    any(), any(), eq("test-app"), eq(false), eq(0), eq(false), eq(10), eq(false),
+                    eq(1L), eq(false));
+        } else if (isCdmaMo) {
+            verify(mCdmaSmsDispatcher).sendText(eq("1111"), eq("2222"), eq("text"), eq(mSentIntent),
+                    any(), any(), eq("test-app"), eq(false), eq(0), eq(false), eq(10), eq(false),
+                    eq(1L), eq(false));
+        } else {
+            verify(mGsmSmsDispatcher).sendText(eq("1111"), eq("2222"), eq("text"), eq(mSentIntent),
+                    any(), any(), eq("test-app"), eq(false), eq(0), eq(false), eq(10), eq(false),
+                    eq(1L), eq(false));
+        }
+        assertNull(holder.getConnection());
+        assertFalse(holder.isDomainSelectionRequested());
+        assertEquals(0, holder.getPendingRequests().size());
+    }
+
+    private void sendMultipartTextWithDomainSelection(@NetworkRegistrationInfo.Domain int domain,
+            boolean isCdmaMo) throws Exception {
+        setUpDomainSelectionConnection();
+        setUpSmsDispatchers();
+
+        ArrayList<String> parts = new ArrayList<>();
+        ArrayList<PendingIntent> sentIntents = new ArrayList<>();
+        ArrayList<PendingIntent> deliveryIntents = new ArrayList<>();
+        mSmsDispatchersController.testSendMultipartText("1111", "2222", parts, sentIntents,
+                deliveryIntents, null, "test-app", false, 0, false, 10, 1L);
+
+        SmsDispatchersController.DomainSelectionConnectionHolder holder =
+                mSmsDispatchersController.testGetDomainSelectionConnectionHolder(false);
+        verify(mSmsDsc).requestDomainSelection(any(), any());
+        assertNotNull(holder);
+        assertNotNull(holder.getConnection());
+        assertTrue(holder.isDomainSelectionRequested());
+        assertEquals(1, holder.getPendingRequests().size());
+
+        mDscFuture.complete(domain);
+        processAllMessages();
+
+        verify(mSmsDsc).finishSelection();
+        if (domain == NetworkRegistrationInfo.DOMAIN_PS) {
+            verify(mImsSmsDispatcher).sendMultipartText(eq("1111"), eq("2222"), eq(parts),
+                    eq(sentIntents), eq(deliveryIntents), any(), eq("test-app"), eq(false), eq(0),
+                    eq(false), eq(10), eq(1L));
+        } else if (isCdmaMo) {
+            verify(mCdmaSmsDispatcher).sendMultipartText(eq("1111"), eq("2222"), eq(parts),
+                    eq(sentIntents), eq(deliveryIntents), any(), eq("test-app"), eq(false), eq(0),
+                    eq(false), eq(10), eq(1L));
+        } else {
+            verify(mGsmSmsDispatcher).sendMultipartText(eq("1111"), eq("2222"), eq(parts),
+                    eq(sentIntents), eq(deliveryIntents), any(), eq("test-app"), eq(false), eq(0),
+                    eq(false), eq(10), eq(1L));
+        }
+        assertNull(holder.getConnection());
+        assertFalse(holder.isDomainSelectionRequested());
+        assertEquals(0, holder.getPendingRequests().size());
+    }
+
+    private void sendRetrySmsWithDomainSelection(@NetworkRegistrationInfo.Domain int domain,
+            int phoneType, String smsFormat) throws Exception {
+        setUpDomainSelectionConnection();
+        setUpSmsDispatchers();
+        when(mPhone.getPhoneType()).thenReturn(phoneType);
+        when(mImsSmsDispatcher.getFormat()).thenReturn(SmsConstants.FORMAT_3GPP);
+        when(mCdmaSmsDispatcher.getFormat()).thenReturn(SmsConstants.FORMAT_3GPP2);
+        when(mGsmSmsDispatcher.getFormat()).thenReturn(SmsConstants.FORMAT_3GPP);
+        replaceInstance(SMSDispatcher.SmsTracker.class, "mFormat", mTracker, smsFormat);
+
+        mSmsDispatchersController.sendRetrySms(mTracker);
+
+        SmsDispatchersController.DomainSelectionConnectionHolder holder =
+                mSmsDispatchersController.testGetDomainSelectionConnectionHolder(false);
+        verify(mSmsDsc).requestDomainSelection(any(), any());
+        assertNotNull(holder);
+        assertNotNull(holder.getConnection());
+        assertTrue(holder.isDomainSelectionRequested());
+        assertEquals(1, holder.getPendingRequests().size());
+
+        mDscFuture.complete(domain);
+        processAllMessages();
+
+        verify(mSmsDsc).finishSelection();
+        if (domain == NetworkRegistrationInfo.DOMAIN_PS) {
+            verify(mImsSmsDispatcher).sendSms(eq(mTracker));
+        } else if (SmsConstants.FORMAT_3GPP2.equals(smsFormat)) {
+            verify(mCdmaSmsDispatcher).sendSms(eq(mTracker));
+        } else {
+            verify(mGsmSmsDispatcher).sendSms(eq(mTracker));
+        }
+        assertNull(holder.getConnection());
+        assertFalse(holder.isDomainSelectionRequested());
+        assertEquals(0, holder.getPendingRequests().size());
+    }
+
+    private void sendRetrySmsWhenImsAlreadyUsed(int phoneType, String smsFormat) throws Exception {
+        setUpDomainSelectionConnection();
+        setUpSmsDispatchers();
+        when(mPhone.getPhoneType()).thenReturn(phoneType);
+        when(mImsSmsDispatcher.getFormat()).thenReturn(SmsConstants.FORMAT_3GPP);
+        when(mCdmaSmsDispatcher.getFormat()).thenReturn(SmsConstants.FORMAT_3GPP2);
+        when(mGsmSmsDispatcher.getFormat()).thenReturn(SmsConstants.FORMAT_3GPP);
+        replaceInstance(SMSDispatcher.SmsTracker.class, "mFormat", mTracker, smsFormat);
+        mTracker.mUsesImsServiceForIms = true;
+
+        mSmsDispatchersController.sendRetrySms(mTracker);
+
+        verify(mSmsDsc, never()).requestDomainSelection(any(), any());
+
+        if (SmsConstants.FORMAT_3GPP2.equals(smsFormat)) {
+            verify(mCdmaSmsDispatcher).sendSms(eq(mTracker));
+        } else {
+            verify(mGsmSmsDispatcher).sendSms(eq(mTracker));
+        }
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SmsStorageMonitorTest.java b/tests/telephonytests/src/com/android/internal/telephony/SmsStorageMonitorTest.java
index 3eace63..b6775eb 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SmsStorageMonitorTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SmsStorageMonitorTest.java
@@ -20,8 +20,11 @@
 
 import static org.junit.Assert.*;
 import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyInt;
 
 import android.content.Intent;
+import android.content.res.Resources;
 import android.os.Message;
 import android.provider.Telephony;
 import android.test.suitebuilder.annotation.MediumTest;
@@ -34,6 +37,8 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+
 
 @RunWith(AndroidTestingRunner.class)
 @TestableLooper.RunWithLooper
@@ -138,6 +143,41 @@
     }
 
     @Test @SmallTest
+    public void testReportSmsMemoryStatusToIms() {
+        Resources mockResources = Mockito.mock(Resources.class);
+        doReturn(mockResources).when(mContext).getResources();
+        doReturn(true).when(mockResources).getBoolean(anyInt());
+        doReturn(true).when(mIccSmsInterfaceManager.mDispatchersController).isIms();
+
+        mSimulatedCommands.notifyRadioOn();
+        processAllMessages();
+
+        verify(mSimulatedCommandsVerifier, never()).reportSmsMemoryStatus(anyBoolean(),
+                any(Message.class));
+
+        // Send DEVICE_STORAGE_FULL
+        mContextFixture.getTestDouble().sendBroadcast(
+                new Intent(Intent.ACTION_DEVICE_STORAGE_FULL));
+        processAllMessages();
+
+        verify(mSimulatedCommandsVerifier).reportSmsMemoryStatus(eq(false), any(Message.class));
+        assertFalse(mSmsStorageMonitor.isStorageAvailable());
+
+        mSimulatedCommands.notifyRadioOn();
+        processAllMessages();
+
+        verify(mSimulatedCommandsVerifier).reportSmsMemoryStatus(eq(false), any(Message.class));
+
+        // Send DEVICE_STORAGE_NOT_FULL
+        mContextFixture.getTestDouble().sendBroadcast(
+                new Intent(Intent.ACTION_DEVICE_STORAGE_NOT_FULL));
+        processAllMessages();
+
+        verify(mIccSmsInterfaceManager.mDispatchersController)
+                .reportSmsMemoryStatus(any(Message.class));
+    }
+
+    @Test @SmallTest
     public void testReportSmsMemoryStatusDuringRetry() {
         mSimulatedCommands.setReportSmsMemoryStatusFailResponse(true);
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SubscriptionControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/SubscriptionControllerTest.java
index 38a2454..019a717 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SubscriptionControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SubscriptionControllerTest.java
@@ -20,6 +20,8 @@
 import static com.android.internal.telephony.SubscriptionController.REQUIRE_DEVICE_IDENTIFIERS_FOR_GROUP_UUID;
 import static com.android.internal.telephony.uicc.IccCardStatus.CardState.CARDSTATE_PRESENT;
 
+import static com.google.common.truth.Truth.assertThat;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
@@ -33,7 +35,6 @@
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.anyLong;
 import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.atLeast;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.doThrow;
@@ -49,6 +50,7 @@
 import android.content.ContentValues;
 import android.content.Intent;
 import android.content.pm.PackageManager;
+import android.content.res.Resources;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
@@ -196,7 +198,8 @@
 
         int slotID = 0;
         //insert one Subscription Info
-        mSubscriptionControllerUT.addSubInfoRecord("test", slotID);
+        mSubscriptionControllerUT.addSubInfo("test", null, slotID,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
 
         //verify there is one sim
         assertEquals(1, mSubscriptionControllerUT.getAllSubInfoCount(mCallingPackage,
@@ -284,12 +287,6 @@
         assertEquals(iconTint, subInfo.getIconTint());
         assertEquals(disNum, subInfo.getNumber());
         assertEquals(isOpportunistic, subInfo.isOpportunistic());
-
-        /* verify broadcast intent */
-        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
-        verify(mContext, atLeast(1)).sendBroadcast(captorIntent.capture());
-        assertEquals(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED,
-                captorIntent.getValue().getAction());
     }
 
     @Test @SmallTest
@@ -309,14 +306,7 @@
                 .getActiveSubscriptionInfo(subID, mCallingPackage, mCallingFeature);
         assertNotNull(subInfo);
         assertEquals(disName, subInfo.getDisplayName());
-        assertEquals(nameSource, subInfo.getNameSource());
-
-        /* verify broadcast intent */
-        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
-        verify(mContext, atLeast(1)).sendBroadcast(captorIntent.capture());
-        assertEquals(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED,
-                captorIntent.getValue().getAction());
-
+        assertEquals(nameSource, subInfo.getDisplayNameSource());
     }
 
     private void setSimEmbedded(boolean isEmbedded) throws Exception {
@@ -350,7 +340,7 @@
 
         assertNotNull(subInfo);
         assertEquals(DISPLAY_NAME, subInfo.getDisplayName());
-        assertEquals(nameSource, subInfo.getNameSource());
+        assertEquals(nameSource, subInfo.getDisplayNameSource());
     }
 
     @Test @SmallTest
@@ -377,7 +367,7 @@
 
         assertNotNull(subInfo);
         assertEquals(DISPLAY_NAME, subInfo.getDisplayName());
-        assertEquals(nameSource, subInfo.getNameSource());
+        assertEquals(nameSource, subInfo.getDisplayNameSource());
     }
 
     @Test @SmallTest
@@ -404,7 +394,7 @@
 
         assertNotNull(subInfo);
         assertEquals(DISPLAY_NAME, subInfo.getDisplayName());
-        assertEquals(nameSource, subInfo.getNameSource());
+        assertEquals(nameSource, subInfo.getDisplayNameSource());
     }
 
     @Test @SmallTest
@@ -435,7 +425,7 @@
 
         assertNotNull(subInfo);
         assertEquals(DISPLAY_NAME, subInfo.getDisplayName());
-        assertEquals(nameSource, subInfo.getNameSource());
+        assertEquals(nameSource, subInfo.getDisplayNameSource());
     }
 
     @Test @SmallTest
@@ -461,7 +451,7 @@
 
         assertNotNull(subInfo);
         assertEquals(DISPLAY_NAME, subInfo.getDisplayName());
-        assertEquals(nameSource, subInfo.getNameSource());
+        assertEquals(nameSource, subInfo.getDisplayNameSource());
     }
 
     @Test @SmallTest
@@ -486,13 +476,13 @@
 
         assertNotNull(subInfo);
         assertEquals(DISPLAY_NAME, subInfo.getDisplayName());
-        assertEquals(nameSource, subInfo.getNameSource());
+        assertEquals(nameSource, subInfo.getDisplayNameSource());
     }
 
     @Test @SmallTest
     public void testIsExistingNameSourceStillValid_pnnIsNotNull_returnTrue() {
         when((mMockSubscriptionInfo).getSubscriptionId()).thenReturn(FAKE_SUBID);
-        when(mMockSubscriptionInfo.getNameSource())
+        when(mMockSubscriptionInfo.getDisplayNameSource())
                 .thenReturn(SubscriptionManager.NAME_SOURCE_SIM_PNN);
         when(mPhone.getPlmn()).thenReturn("testing_pnn");
 
@@ -502,7 +492,7 @@
     @Test @SmallTest
     public void testIsExistingNameSourceStillValid_spnIsNotNull_returnTrue() {
         when((mMockSubscriptionInfo).getSubscriptionId()).thenReturn(FAKE_SUBID);
-        when(mMockSubscriptionInfo.getNameSource())
+        when(mMockSubscriptionInfo.getDisplayNameSource())
                 .thenReturn(SubscriptionManager.NAME_SOURCE_SIM_SPN);
         when(mUiccController.getUiccProfileForPhone(anyInt())).thenReturn(mUiccProfile);
         when(mUiccProfile.getServiceProviderName()).thenReturn("testing_spn");
@@ -514,7 +504,7 @@
     public void testIsExistingNameSourceStillValid_simIsEmbedded_returnTrue() {
         when(mMockSubscriptionInfo.isEmbedded()).thenReturn(true);
         when((mMockSubscriptionInfo).getSubscriptionId()).thenReturn(FAKE_SUBID);
-        when(mMockSubscriptionInfo.getNameSource())
+        when(mMockSubscriptionInfo.getDisplayNameSource())
                 .thenReturn(SubscriptionManager.NAME_SOURCE_CARRIER);
 
         assertTrue(mSubscriptionControllerUT.isExistingNameSourceStillValid(mMockSubscriptionInfo));
@@ -524,7 +514,7 @@
     public void testIsExistingNameSourceStillValid_carrierConfigIsNull_returnTrue() {
         when(mMockSubscriptionInfo.isEmbedded()).thenReturn(false);
         when((mMockSubscriptionInfo).getSubscriptionId()).thenReturn(FAKE_SUBID);
-        when(mMockSubscriptionInfo.getNameSource())
+        when(mMockSubscriptionInfo.getDisplayNameSource())
                 .thenReturn(SubscriptionManager.NAME_SOURCE_CARRIER);
         when(mCarrierConfigManager.getConfigForSubId(FAKE_SUBID)).thenReturn(null);
 
@@ -535,7 +525,7 @@
     public void testIsExistingNameSourceStillValid_carrierNameOverrideIsTrue_returnTrue() {
         when(mMockSubscriptionInfo.isEmbedded()).thenReturn(false);
         when((mMockSubscriptionInfo).getSubscriptionId()).thenReturn(FAKE_SUBID);
-        when(mMockSubscriptionInfo.getNameSource())
+        when(mMockSubscriptionInfo.getDisplayNameSource())
                 .thenReturn(SubscriptionManager.NAME_SOURCE_CARRIER);
         mCarrierConfigs.putBoolean(CarrierConfigManager.KEY_CARRIER_NAME_OVERRIDE_BOOL, true);
 
@@ -546,7 +536,7 @@
     public void testIsExistingNameSourceStillValid_spnIsNullAndCarrierNameIsNotNull_returnTrue() {
         when(mMockSubscriptionInfo.isEmbedded()).thenReturn(false);
         when((mMockSubscriptionInfo).getSubscriptionId()).thenReturn(FAKE_SUBID);
-        when(mMockSubscriptionInfo.getNameSource())
+        when(mMockSubscriptionInfo.getDisplayNameSource())
                 .thenReturn(SubscriptionManager.NAME_SOURCE_CARRIER);
         when(mUiccController.getUiccProfileForPhone(anyInt())).thenReturn(mUiccProfile);
         when(mUiccProfile.getServiceProviderName()).thenReturn(null);
@@ -595,12 +585,6 @@
         assertNotNull(subInfo);
         assertEquals(Integer.parseInt(mCcMncVERIZON.substring(0, 3)), subInfo.getMcc());
         assertEquals(Integer.parseInt(mCcMncVERIZON.substring(3)), subInfo.getMnc());
-
-         /* verify broadcast intent */
-        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
-        verify(mContext, atLeast(1)).sendBroadcast(captorIntent.capture());
-        assertEquals(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED,
-                captorIntent.getValue().getAction());
     }
 
     @Test @SmallTest
@@ -613,12 +597,6 @@
                 .getActiveSubscriptionInfo(1, mCallingPackage, mCallingFeature);
         assertNotNull(subInfo);
         assertEquals(carrierId, subInfo.getCarrierId());
-
-         /* verify broadcast intent */
-        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
-        verify(mContext, atLeast(1)).sendBroadcast(captorIntent.capture());
-        assertEquals(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED,
-                captorIntent.getValue().getAction());
     }
 
     @Test
@@ -785,7 +763,8 @@
                 .notifyOpportunisticSubscriptionInfoChanged();
 
         testInsertSim();
-        mSubscriptionControllerUT.addSubInfoRecord("test2", 0);
+        mSubscriptionControllerUT.addSubInfo("test2", null, 0,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
 
         // Neither sub1 or sub2 are opportunistic. So getOpportunisticSubscriptions
         // should return empty list and no callback triggered.
@@ -990,7 +969,9 @@
     @SmallTest
     public void testSetSubscriptionGroupWithModifyPermission() throws Exception {
         testInsertSim();
-        mSubscriptionControllerUT.addSubInfoRecord("test2", 0);
+        mSubscriptionControllerUT.addSubInfo("test2", null, 0,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+
         mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);
         mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE);
 
@@ -1053,7 +1034,9 @@
         testInsertSim();
         // Adding a second profile and mark as embedded.
         // TODO b/123300875 slot index 1 is not expected to be valid
-        mSubscriptionControllerUT.addSubInfoRecord("test2", 1);
+        mSubscriptionControllerUT.addSubInfo("test2", null, 1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+
         ContentValues values = new ContentValues();
         values.put(SubscriptionManager.IS_EMBEDDED, 1);
         mFakeTelephonyProvider.update(SubscriptionManager.CONTENT_URI, values,
@@ -1095,7 +1078,9 @@
         mContextFixture.addCallingOrSelfPermission(
                 android.Manifest.permission.MODIFY_PHONE_STATE);
         // TODO b/123300875 slot index 1 is not expected to be valid
-        mSubscriptionControllerUT.addSubInfoRecord("test3", 1);
+        mSubscriptionControllerUT.addSubInfo("test3", null, 1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+
         mContextFixture.removeCallingOrSelfPermission(
                 android.Manifest.permission.MODIFY_PHONE_STATE);
         // As sub2 is inactive, it will checks carrier privilege against access rules in the db.
@@ -1115,7 +1100,9 @@
         testInsertSim();
         // Adding a second profile and mark as embedded.
         // TODO b/123300875 slot index 1 is not expected to be valid
-        mSubscriptionControllerUT.addSubInfoRecord("test2", 1);
+        mSubscriptionControllerUT.addSubInfo("test2", null, 1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+
         ContentValues values = new ContentValues();
         values.put(SubscriptionManager.IS_EMBEDDED, 1);
         mFakeTelephonyProvider.update(SubscriptionManager.CONTENT_URI, values,
@@ -1168,7 +1155,9 @@
         testInsertSim();
         // Adding a second profile and mark as embedded.
         // TODO b/123300875 slot index 1 is not expected to be valid
-        mSubscriptionControllerUT.addSubInfoRecord("test2", 1);
+        mSubscriptionControllerUT.addSubInfo("test2", null, 1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+
         ContentValues values = new ContentValues();
         values.put(SubscriptionManager.IS_EMBEDDED, 1);
         mFakeTelephonyProvider.update(SubscriptionManager.CONTENT_URI, values,
@@ -1239,7 +1228,8 @@
 
         testInsertSim();
         // Adding a second profile and mark as embedded.
-        mSubscriptionControllerUT.addSubInfoRecord("test2", 0);
+        mSubscriptionControllerUT.addSubInfo("test2", null, 0,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
 
         ContentValues values = new ContentValues();
         values.put(SubscriptionManager.IS_EMBEDDED, 1);
@@ -1290,7 +1280,9 @@
     public void testSetSubscriptionGroup() throws Exception {
         testInsertSim();
         // Adding a second profile and mark as embedded.
-        mSubscriptionControllerUT.addSubInfoRecord("test2", 1);
+        mSubscriptionControllerUT.addSubInfo("test2", null, 1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+
         ContentValues values = new ContentValues();
         values.put(SubscriptionManager.IS_EMBEDDED, 1);
         mFakeTelephonyProvider.update(SubscriptionManager.CONTENT_URI, values,
@@ -1378,8 +1370,10 @@
     @SmallTest
     public void testGetActiveSubIdList() throws Exception {
         // TODO b/123300875 slot index 1 is not expected to be valid
-        mSubscriptionControllerUT.addSubInfoRecord("123", 1);   // sub 1
-        mSubscriptionControllerUT.addSubInfoRecord("456", 0);   // sub 2
+        mSubscriptionControllerUT.addSubInfo("123", null, 1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM); // sub 1
+        mSubscriptionControllerUT.addSubInfo("456", null, 0,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM); // sub 2
 
         int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
         // Make sure the return sub ids are sorted by slot index
@@ -1642,7 +1636,9 @@
         // the ICC ID and phone number.
         testInsertSim();
         doReturn(2).when(mTelephonyManager).getPhoneCount();
-        mSubscriptionControllerUT.addSubInfoRecord("test2", 1);
+        mSubscriptionControllerUT.addSubInfo("test2", null, 1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+
         int firstSubId = getFirstSubId();
         int secondSubId = getSubIdAtIndex(1);
         mSubscriptionControllerUT.setDisplayNumber(DISPLAY_NUMBER, secondSubId);
@@ -1669,7 +1665,8 @@
         // the ICC ID and phone number.
         testInsertSim();
         doReturn(2).when(mTelephonyManager).getPhoneCount();
-        mSubscriptionControllerUT.addSubInfoRecord("test2", 1);
+        mSubscriptionControllerUT.addSubInfo("test2", null, 1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
         int firstSubId = getFirstSubId();
         int secondSubId = getSubIdAtIndex(1);
         setupIdentifierCarrierPrivilegesTest();
@@ -1755,7 +1752,7 @@
         } catch (SecurityException expected) {
         }
 
-        // only has the device identifiers permission
+        // only has the USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER permission
         setIdentifierAccess(true);
         try {
             mSubscriptionControllerUT.getSubscriptionsInGroup(groupUuid, mCallingPackage,
@@ -1768,10 +1765,13 @@
         // only has the READ_PHONE_STATE permission
         setIdentifierAccess(false);
         setReadPhoneState();
-        List<SubscriptionInfo> subInfoList = mSubscriptionControllerUT.getSubscriptionsInGroup(
-                groupUuid, mCallingPackage, mCallingFeature);
-        assertNotNull(subInfoList);
-        assertTrue(subInfoList.isEmpty());
+        try {
+            mSubscriptionControllerUT.getSubscriptionsInGroup(groupUuid, mCallingPackage,
+                    mCallingFeature);
+            fail("getSubscriptionsInGroup should fail when invoked with no "
+                    + "USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER permissions");
+        } catch (SecurityException expected) {
+        }
     }
 
     @Test
@@ -1993,8 +1993,10 @@
     @SmallTest
     public void testGetAvailableSubscriptionList() throws Exception {
         // TODO b/123300875 slot index 1 is not expected to be valid
-        mSubscriptionControllerUT.addSubInfoRecord("123", 1);   // sub 1
-        mSubscriptionControllerUT.addSubInfoRecord("456", 0);   // sub 2
+        mSubscriptionControllerUT.addSubInfo("123", null, 1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM); // sub 1
+        mSubscriptionControllerUT.addSubInfo("456", null, 0,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM); // sub 2
 
         List<SubscriptionInfo> infoList = mSubscriptionControllerUT
                 .getAvailableSubscriptionInfoList(mCallingPackage, mCallingFeature);
@@ -2028,8 +2030,10 @@
     @SmallTest
     public void testGetAvailableSubscriptionList_withTrailingF() throws Exception {
         // TODO b/123300875 slot index 1 is not expected to be valid
-        mSubscriptionControllerUT.addSubInfoRecord("123", 1);   // sub 1
-        mSubscriptionControllerUT.addSubInfoRecord("456", 0);   // sub 2
+        mSubscriptionControllerUT.addSubInfo("123", null, 1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM); // sub 1
+        mSubscriptionControllerUT.addSubInfo("456", null, 0,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM); // sub 2
 
         // Remove "123" from active sim list but have it inserted.
         UiccSlot[] uiccSlots = {mUiccSlot};
@@ -2076,7 +2080,8 @@
     @Test
     public void testSetSubscriptionEnabled_disableActivePsim_cardIdWithTrailingF() {
         String iccId = "123F";
-        mSubscriptionControllerUT.addSubInfoRecord(iccId, 0);
+        mSubscriptionControllerUT.addSubInfo(iccId, null, 0,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
         mSubscriptionControllerUT.registerForUiccAppsEnabled(mHandler, 0, null, false);
         UiccSlotInfo slot = getFakeUiccSlotInfo(true, 0, iccId + "FF", iccId);
         UiccSlotInfo[] uiccSlotInfos = {slot};
@@ -2114,11 +2119,186 @@
             fail("Unexpected exception: " + e);
         }
 
-        mSubscriptionControllerUT.addSubInfoRecord("test3",
-                SubscriptionManager.INVALID_SIM_SLOT_INDEX);
+        mSubscriptionControllerUT.addSubInfo("test3", null,
+                SubscriptionManager.INVALID_SIM_SLOT_INDEX,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
 
         assertTrue(mSubscriptionControllerUT.checkPhoneIdAndIccIdMatch(0, "test"));
         assertTrue(mSubscriptionControllerUT.checkPhoneIdAndIccIdMatch(0, "test2"));
         assertFalse(mSubscriptionControllerUT.checkPhoneIdAndIccIdMatch(0, "test3"));
     }
-}
+
+    @Test
+    @SmallTest
+    public void testMessageRefDBFetchAndUpdate() throws Exception {
+        testInsertSim();
+        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
+        assertTrue(subIds != null && subIds.length != 0);
+        final int subId = subIds[0];
+        SubscriptionController.getInstance().updateMessageRef(subId, 201);
+        int messageRef = SubscriptionController.getInstance().getMessageRef(subId);
+        assertTrue("201 :", messageRef == 201);
+    }
+
+    @Test
+    public void setSubscriptionUserHandle_withoutPermission() {
+        testInsertSim();
+        enableGetSubscriptionUserHandle();
+        /* Get SUB ID */
+        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
+        assertTrue(subIds != null && subIds.length != 0);
+        final int subId = subIds[0];
+        mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);
+
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionControllerUT.setSubscriptionUserHandle(
+                        UserHandle.of(UserHandle.USER_SYSTEM), subId));
+    }
+
+    @Test
+    public void setGetSubscriptionUserHandle_userHandleNull() {
+        testInsertSim();
+        enableGetSubscriptionUserHandle();
+        /* Get SUB ID */
+        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
+        assertTrue(subIds != null && subIds.length != 0);
+        final int subId = subIds[0];
+
+        mSubscriptionControllerUT.setSubscriptionUserHandle(null, subId);
+
+        assertThat(mSubscriptionControllerUT.getSubscriptionUserHandle(subId))
+                .isEqualTo(null);
+    }
+
+    @Test
+    public void setSubscriptionUserHandle_invalidSubId() {
+        enableGetSubscriptionUserHandle();
+
+        assertThrows(IllegalArgumentException.class,
+                () -> mSubscriptionControllerUT.setSubscriptionUserHandle(
+                        UserHandle.of(UserHandle.USER_SYSTEM),
+                        SubscriptionManager.DEFAULT_SUBSCRIPTION_ID));
+    }
+
+    @Test
+    public void setGetSubscriptionUserHandle_withValidUserHandleAndSubId() {
+        testInsertSim();
+        enableGetSubscriptionUserHandle();
+        /* Get SUB ID */
+        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
+        assertTrue(subIds != null && subIds.length != 0);
+        final int subId = subIds[0];
+
+        mSubscriptionControllerUT.setSubscriptionUserHandle(
+                UserHandle.of(UserHandle.USER_SYSTEM), subId);
+
+        assertThat(mSubscriptionControllerUT.getSubscriptionUserHandle(subId))
+                .isEqualTo(UserHandle.of(UserHandle.USER_SYSTEM));
+    }
+
+    @Test
+    public void getSubscriptionUserHandle_withoutPermission() {
+        testInsertSim();
+        enableGetSubscriptionUserHandle();
+        /* Get SUB ID */
+        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
+        assertTrue(subIds != null && subIds.length != 0);
+        final int subId = subIds[0];
+        mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);
+
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionControllerUT.getSubscriptionUserHandle(subId));
+    }
+
+    @Test
+    public void getSubscriptionUserHandle_invalidSubId() {
+        enableGetSubscriptionUserHandle();
+
+        assertThrows(IllegalArgumentException.class,
+                () -> mSubscriptionControllerUT.getSubscriptionUserHandle(
+                        SubscriptionManager.DEFAULT_SUBSCRIPTION_ID));
+    }
+
+    @Test
+    public void isSubscriptionAssociatedWithUser_withoutPermission() {
+        mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);
+
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionControllerUT.isSubscriptionAssociatedWithUser(1,
+                        UserHandle.of(UserHandle.USER_SYSTEM)));
+    }
+
+    @Test
+    public void isSubscriptionAssociatedWithUser_noSubscription() {
+        // isSubscriptionAssociatedWithUser should return true if there are no active subscriptions.
+        assertThat(mSubscriptionControllerUT.isSubscriptionAssociatedWithUser(1,
+                UserHandle.of(UserHandle.USER_SYSTEM))).isEqualTo(true);
+    }
+
+    @Test
+    public void isSubscriptionAssociatedWithUser_unknownSubId() {
+        testInsertSim();
+        /* Get SUB ID */
+        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
+        assertTrue(subIds != null && subIds.length != 0);
+        int unknownSubId = 123;
+
+        assertThat(mSubscriptionControllerUT.isSubscriptionAssociatedWithUser(unknownSubId,
+                UserHandle.of(UserHandle.USER_SYSTEM))).isEqualTo(false);
+    }
+
+    @Test
+    public void isSubscriptionAssociatedWithUser_userAssociatedWithSubscription() {
+        testInsertSim();
+        /* Get SUB ID */
+        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
+        assertTrue(subIds != null && subIds.length != 0);
+        final int subId = subIds[0];
+
+        mSubscriptionControllerUT.setSubscriptionUserHandle(
+                UserHandle.of(UserHandle.USER_SYSTEM), subId);
+
+        assertThat(mSubscriptionControllerUT.isSubscriptionAssociatedWithUser(subId,
+                UserHandle.of(UserHandle.USER_SYSTEM))).isEqualTo(true);
+    }
+
+    @Test
+    public void isSubscriptionAssociatedWithUser_userNotAssociatedWithSubscription() {
+        testInsertSim();
+        enableGetSubscriptionUserHandle();
+        /* Get SUB ID */
+        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList(/*visibleOnly*/false);
+        assertTrue(subIds != null && subIds.length != 0);
+        final int subId = subIds[0];
+
+        mSubscriptionControllerUT.setSubscriptionUserHandle(UserHandle.of(UserHandle.USER_SYSTEM),
+                subId);
+
+        assertThat(mSubscriptionControllerUT.isSubscriptionAssociatedWithUser(subId,
+                UserHandle.of(10))).isEqualTo(false);
+    }
+
+
+    @Test
+    public void getSubscriptionInfoListAssociatedWithUser_withoutPermission() {
+        mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);
+
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionControllerUT.getSubscriptionInfoListAssociatedWithUser(
+                        UserHandle.of(UserHandle.USER_SYSTEM)));
+    }
+
+    @Test
+    public void getSubscriptionInfoListAssociatedWithUser_noSubscription() {
+        List<SubscriptionInfo> associatedSubInfoList = mSubscriptionControllerUT
+                .getSubscriptionInfoListAssociatedWithUser(UserHandle.of(UserHandle.USER_SYSTEM));
+        assertThat(associatedSubInfoList.size()).isEqualTo(0);
+    }
+
+    private void enableGetSubscriptionUserHandle() {
+        Resources mResources = mock(Resources.class);
+        doReturn(true).when(mResources).getBoolean(
+                eq(com.android.internal.R.bool.config_enable_get_subscription_user_handle));
+        doReturn(mResources).when(mContext).getResources();
+    }
+}
\ No newline at end of file
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoTest.java b/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoTest.java
index 3f85435..3bafe4d 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoTest.java
@@ -15,12 +15,11 @@
  */
 package com.android.internal.telephony;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
+import static com.google.common.truth.Truth.assertThat;
 
 import android.os.Parcel;
 import android.telephony.SubscriptionInfo;
-import android.test.suitebuilder.annotation.SmallTest;
+import android.telephony.SubscriptionManager;
 
 import org.junit.After;
 import org.junit.Before;
@@ -38,78 +37,63 @@
 
     @Before
     public void setUp() throws Exception {
-        mSubscriptionInfoUT = new SubscriptionInfo(1, "890126042XXXXXXXXXXX", 0, "T-mobile",
-                "T-mobile", 0, 255, "12345", 0, null, "310", "260", "156", false, null, null);
-        mSubscriptionInfoUT.setAssociatedPlmns(EHPLMNS, HPLMNS);
+        mSubscriptionInfoUT = new SubscriptionInfo.Builder()
+                .setId(1)
+                .setIccId("890126042XXXXXXXXXXX")
+                .setSimSlotIndex(0)
+                .setDisplayName("T-mobile")
+                .setCarrierName("T-mobile")
+                .setDisplayNameSource(SubscriptionManager.NAME_SOURCE_CARRIER_ID)
+                .setIconTint(255)
+                .setNumber("12345")
+                .setDataRoaming(SubscriptionManager.DATA_ROAMING_DISABLE)
+                .setMcc("310")
+                .setMnc("260")
+                .setEhplmns(EHPLMNS)
+                .setHplmns(HPLMNS)
+                .setCountryIso("us")
+                .build();
     }
 
     @Test
-    @SmallTest
     public void testSubProperties() {
-        assertEquals(260, mSubscriptionInfoUT.getMnc());
-        assertEquals(310, mSubscriptionInfoUT.getMcc());
-        assertEquals("12345", mSubscriptionInfoUT.getNumber());
-        assertEquals(0, mSubscriptionInfoUT.getDataRoaming());
-        assertEquals("T-mobile", mSubscriptionInfoUT.getDisplayName());
-        assertEquals("T-mobile", mSubscriptionInfoUT.getCarrierName());
-        assertEquals("156", mSubscriptionInfoUT.getCountryIso());
-        assertEquals(255, mSubscriptionInfoUT.getIconTint());
-        assertEquals(0, mSubscriptionInfoUT.getNameSource());
-        assertEquals(1, mSubscriptionInfoUT.getSubscriptionId());
-        assertEquals(0, mSubscriptionInfoUT.getSimSlotIndex());
-        assertEquals("890126042XXXXXXXXXXX", mSubscriptionInfoUT.getIccId());
+        assertThat(mSubscriptionInfoUT.getMcc()).isEqualTo(310);
+        assertThat(mSubscriptionInfoUT.getMccString()).isEqualTo("310");
+        assertThat(mSubscriptionInfoUT.getMnc()).isEqualTo(260);
+        assertThat(mSubscriptionInfoUT.getMncString()).isEqualTo("260");
+        assertThat(mSubscriptionInfoUT.getNumber()).isEqualTo("12345");
+        assertThat(mSubscriptionInfoUT.getDataRoaming()).isEqualTo(0);
+        assertThat(mSubscriptionInfoUT.getDisplayName().toString()).isEqualTo("T-mobile");
+        assertThat(mSubscriptionInfoUT.getCarrierName().toString()).isEqualTo("T-mobile");
+        assertThat(mSubscriptionInfoUT.getCountryIso()).isEqualTo("us");
+        assertThat(mSubscriptionInfoUT.getIconTint()).isEqualTo(255);
+        assertThat(mSubscriptionInfoUT.getDisplayNameSource()).isEqualTo(0);
+        assertThat(mSubscriptionInfoUT.getSubscriptionId()).isEqualTo(1);
+        assertThat(mSubscriptionInfoUT.getSimSlotIndex()).isEqualTo(0);
+        assertThat(mSubscriptionInfoUT.getIccId()).isEqualTo("890126042XXXXXXXXXXX");
     }
 
     @Test
-    @SmallTest
-    public void testSetGetCarrierName() {
-        assertEquals("T-mobile", mSubscriptionInfoUT.getCarrierName());
-        mSubscriptionInfoUT.setCarrierName("Verizon");
-        assertEquals("Verizon", mSubscriptionInfoUT.getCarrierName());
-    }
-
-    @Test
-    @SmallTest
-    public void testSetGetDisplayName() {
-        assertEquals("T-mobile", mSubscriptionInfoUT.getDisplayName());
-        mSubscriptionInfoUT.setDisplayName("Verizon");
-        assertEquals("Verizon", mSubscriptionInfoUT.getDisplayName());
-    }
-
-    @Test
-    @SmallTest
-    public void testSetGetIconTint() {
-        assertEquals(255, mSubscriptionInfoUT.getIconTint());
-        mSubscriptionInfoUT.setIconTint(0);
-        assertEquals(0, mSubscriptionInfoUT.getIconTint());
-    }
-
-    @Test
-    @SmallTest
     public void testParcelUnparcel() {
         Parcel p = Parcel.obtain();
         mSubscriptionInfoUT.writeToParcel(p, 0);
         p.setDataPosition(0);
         SubscriptionInfo copy = SubscriptionInfo.CREATOR.createFromParcel(p);
-        assertEquals(mSubscriptionInfoUT, copy);
+        assertThat(mSubscriptionInfoUT).isEqualTo(copy);
     }
 
     @Test
-    @SmallTest
     public void testEquals() {
-        SubscriptionInfo copiedInfo = new SubscriptionInfo(1, "890126042XXXXXXXXXXX", 0,
-                "T-mobile", "T-mobile", 0, 255, "12345", 0, null,
-                "310", "260", "156", false, null, null);
-        copiedInfo.setAssociatedPlmns(EHPLMNS, HPLMNS);
-        SubscriptionInfo differentDisplayName = new SubscriptionInfo(1, "890126042XXXXXXXXXXX", 0,
-                "AT&T", "T-mobile", 0, 255, "12345", 0, null,
-                "310", "260", "156", false, null, null);
-        SubscriptionInfo differentSubId = new SubscriptionInfo(2, "890126042XXXXXXXXXXX", 0,
-                "AT&T", "T-mobile", 0, 255, "12345", 0, null,
-                "310", "260", "156", false, null, null);
+        SubscriptionInfo copiedInfo = new SubscriptionInfo.Builder(mSubscriptionInfoUT).build();
+        SubscriptionInfo differentDisplayName = new SubscriptionInfo.Builder(mSubscriptionInfoUT)
+                .setDisplayName("Different display name")
+                .build();
+        SubscriptionInfo differentSubId = new SubscriptionInfo.Builder(mSubscriptionInfoUT)
+                .setId(1234)
+                .build();
 
-        assertEquals(mSubscriptionInfoUT, copiedInfo);
-        assertNotEquals(mSubscriptionInfoUT, differentDisplayName);
-        assertNotEquals(mSubscriptionInfoUT, differentSubId);
+        assertThat(mSubscriptionInfoUT).isEqualTo(copiedInfo);
+        assertThat(mSubscriptionInfoUT).isNotEqualTo(differentDisplayName);
+        assertThat(mSubscriptionInfoUT).isNotEqualTo(differentSubId);
     }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoUpdaterTest.java b/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoUpdaterTest.java
index c669810..868b53a 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoUpdaterTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoUpdaterTest.java
@@ -151,7 +151,7 @@
                 });
 
         doReturn(mUserInfo).when(mIActivityManager).getCurrentUser();
-        doReturn(new int[]{FAKE_SUB_ID_1}).when(mSubscriptionController).getSubId(0);
+        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubId(0);
         doReturn(new int[]{FAKE_SUB_ID_1}).when(mSubscriptionManager).getActiveSubscriptionIdList();
         ((MockContentResolver) mContext.getContentResolver()).addProvider(
                 SubscriptionManager.CONTENT_URI.getAuthority(),
@@ -601,13 +601,16 @@
 
         List<SubscriptionInfo> subInfoList = new ArrayList<>();
         // 1: not embedded, but has matching iccid with an embedded subscription.
-        subInfoList.add(new SubscriptionInfo(
-                        0, "1", 0, "", "", 0, 0, "", 0, null, "0", "0", "", false /* isEmbedded */,
-                        null /* accessRules */, null));
+        subInfoList.add(new SubscriptionInfo.Builder()
+                .setSimSlotIndex(0)
+                .setIccId("1")
+                .build());
         // 2: embedded but no longer present.
-        subInfoList.add(new SubscriptionInfo(
-                0, "2", 0, "", "", 0, 0, "", 0, null, "0", "0", "", true /* isEmbedded */,
-                null /* accessRules */, null));
+        subInfoList.add(new SubscriptionInfo.Builder()
+                .setSimSlotIndex(0)
+                .setIccId("2")
+                .setEmbedded(true)
+                .build());
 
         when(mSubscriptionController.getSubscriptionInfoListForEmbeddedSubscriptionUpdate(
                 new String[] { "1", "3"}, false /* removable */)).thenReturn(subInfoList);
@@ -655,13 +658,16 @@
 
         List<SubscriptionInfo> subInfoList = new ArrayList<>();
         // 1: not embedded, but has matching iccid with an embedded subscription.
-        subInfoList.add(new SubscriptionInfo(
-                0, "1", 0, "", "", 0, 0, "", 0, null, "0", "0", "", false /* isEmbedded */,
-                null /* accessRules */, null));
+        subInfoList.add(new SubscriptionInfo.Builder()
+                .setSimSlotIndex(0)
+                .setIccId("1")
+                .build());
         // 2: embedded.
-        subInfoList.add(new SubscriptionInfo(
-                0, "2", 0, "", "", 0, 0, "", 0, null, "0", "0", "", true /* isEmbedded */,
-                null /* accessRules */, null));
+        subInfoList.add(new SubscriptionInfo.Builder()
+                .setSimSlotIndex(0)
+                .setIccId("2")
+                .setEmbedded(true)
+                .build());
 
         when(mSubscriptionController.getSubscriptionInfoListForEmbeddedSubscriptionUpdate(
                 new String[0], false /* removable */)).thenReturn(subInfoList);
@@ -689,9 +695,10 @@
 
         List<SubscriptionInfo> subInfoList = new ArrayList<>();
         // 1: not embedded.
-        subInfoList.add(new SubscriptionInfo(
-                0, "1", 0, "", "", 0, 0, "", 0, null, "0", "0", "", false /* isEmbedded */,
-                null /* accessRules */, null));
+        subInfoList.add(new SubscriptionInfo.Builder()
+                .setSimSlotIndex(0)
+                .setIccId("1")
+                .build());
 
         when(mSubscriptionController.getSubscriptionInfoListForEmbeddedSubscriptionUpdate(
                 new String[0], false /* removable */)).thenReturn(subInfoList);
@@ -743,7 +750,7 @@
         final int phoneId = mPhone.getPhoneId();
         String carrierPackageName = "FakeCarrierPackageName";
 
-        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubId(phoneId);
         doReturn(mSubInfo).when(mSubscriptionController).getSubscriptionInfo(eq(FAKE_SUB_ID_1));
         doReturn(carrierPackageName).when(mTelephonyManager)
                 .getCarrierServicePackageNameForLogicalSlot(eq(phoneId));
@@ -768,7 +775,7 @@
                 true, "");
         String carrierPackageName = "FakeCarrierPackageName";
 
-        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubId(phoneId);
         doReturn(mSubInfo).when(mSubscriptionController).getSubscriptionInfo(eq(FAKE_SUB_ID_1));
         doReturn(false).when(mSubInfo).isOpportunistic();
         doReturn(carrierPackageName).when(mTelephonyManager)
@@ -800,7 +807,7 @@
 
         String carrierPackageName = "FakeCarrierPackageName";
 
-        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubId(phoneId);
         doReturn(mSubInfo).when(mSubscriptionController).getSubscriptionInfo(eq(FAKE_SUB_ID_1));
         doReturn(true).when(mSubInfo).isOpportunistic();
         doReturn(carrierPackageName).when(mTelephonyManager)
@@ -834,7 +841,7 @@
 
         doReturn(true).when(mSubscriptionController).canPackageManageGroup(
                 ParcelUuid.fromString("11111111-2222-3333-4444-555555555555"), carrierPackageName);
-        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubId(phoneId);
         doReturn(mSubInfo).when(mSubscriptionController).getSubscriptionInfo(eq(FAKE_SUB_ID_1));
         doReturn(carrierPackageName).when(mTelephonyManager)
                 .getCarrierServicePackageNameForLogicalSlot(eq(phoneId));
@@ -869,7 +876,7 @@
 
         doReturn(true).when(mSubscriptionController).canPackageManageGroup(
                 ParcelUuid.fromString("11111111-2222-3333-4444-555555555555"), carrierPackageName);
-        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubId(phoneId);
         doReturn(mSubInfo).when(mSubscriptionController).getSubscriptionInfo(eq(FAKE_SUB_ID_1));
         doReturn(ParcelUuid.fromString("11111111-2222-3333-4444-555555555555"))
             .when(mSubInfo).getGroupUuid();
@@ -963,7 +970,7 @@
         setupUsageSettingResources();
 
         // Setup subscription
-        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubId(phoneId);
         doReturn(mSubInfo).when(mSubscriptionController).getSubscriptionInfo(eq(FAKE_SUB_ID_1));
         doReturn(null).when(mSubInfo).getGroupUuid();
         doReturn(false).when(mSubInfo).isOpportunistic();
@@ -1010,7 +1017,7 @@
 
         String carrierPackageName = "FakeCarrierPackageName";
 
-        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(FAKE_SUB_ID_1).when(mSubscriptionController).getSubId(phoneId);
         doReturn(mSubInfo).when(mSubscriptionController).getSubscriptionInfo(eq(FAKE_SUB_ID_1));
         doReturn(false).when(mSubInfo).isOpportunistic();
         doReturn(carrierPackageName).when(mTelephonyManager)
diff --git a/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java b/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java
index cf7cadb..71c6773 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java
@@ -25,7 +25,9 @@
 import static android.telephony.TelephonyManager.RADIO_POWER_UNAVAILABLE;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
@@ -35,9 +37,11 @@
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import android.Manifest;
 import android.content.Intent;
 import android.content.pm.UserInfo;
 import android.net.LinkProperties;
+import android.os.Build;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.UserHandle;
@@ -108,6 +112,8 @@
     private CellLocation mCellLocation;
     private List<CellInfo> mCellInfo;
     private BarringInfo mBarringInfo = null;
+    private CellIdentity mCellIdentityForRegiFail;
+    private int mRegistrationFailReason;
 
     // All events contribute to TelephonyRegistry#isPhoneStatePermissionRequired
     private static final Set<Integer> READ_PHONE_STATE_EVENTS;
@@ -176,7 +182,8 @@
             TelephonyCallback.CellLocationListener,
             TelephonyCallback.ServiceStateListener,
             TelephonyCallback.CellInfoListener,
-            TelephonyCallback.BarringInfoListener {
+            TelephonyCallback.BarringInfoListener,
+            TelephonyCallback.RegistrationFailedListener {
         // This class isn't mockable to get invocation counts because the IBinder is null and
         // crashes the TelephonyRegistry. Make a cheesy verify(times()) alternative.
         public AtomicInteger invocationCount = new AtomicInteger(0);
@@ -250,6 +257,15 @@
             invocationCount.incrementAndGet();
             mBarringInfo = barringInfo;
         }
+
+        public void onRegistrationFailed(@android.annotation.NonNull CellIdentity cellIdentity,
+                @android.annotation.NonNull String chosenPlmn,
+                @NetworkRegistrationInfo.Domain int domain,
+                int causeCode, int additionalCauseCode) {
+            invocationCount.incrementAndGet();
+            mCellIdentityForRegiFail = cellIdentity;
+            mRegistrationFailReason = causeCode;
+        }
     }
 
     private void addTelephonyRegistryService() {
@@ -897,24 +913,48 @@
     }
 
     @Test
-    public void testBarringInfoChanged() {
+    public void testBarringInfoChangedWithLocationFinePermission() throws Exception {
+        checkBarringInfoWithLocationPermission(Manifest.permission.ACCESS_FINE_LOCATION);
+    }
+
+    @Test
+    public void testBarringInfoChangedLocationCoarsePermission() throws Exception {
+        checkBarringInfoWithLocationPermission(Manifest.permission.ACCESS_COARSE_LOCATION);
+    }
+
+    @Test
+    public void testBarringInfoChangedWithoutLocationPermission() throws Exception {
+        checkBarringInfoWithLocationPermission(null);
+    }
+
+    private void checkBarringInfoWithLocationPermission(String permission) throws Exception {
         // Return a slotIndex / phoneId of 0 for all sub ids given.
         doReturn(mMockSubInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(anyInt());
         doReturn(0/*slotIndex*/).when(mMockSubInfo).getSimSlotIndex();
         doReturn(true).when(mLocationManager).isLocationEnabledForUser(any(UserHandle.class));
 
+        mApplicationInfo.targetSdkVersion = Build.VERSION_CODES.TIRAMISU;
+        doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(anyString(), anyInt());
+        mContextFixture.addCallingOrSelfPermission("");
+        mContextFixture.addCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE);
+        mContextFixture.addCallingOrSelfPermission(
+                android.Manifest.permission.READ_PRECISE_PHONE_STATE);
+        if (permission != null) {
+            mContextFixture.addCallingOrSelfPermission(permission);
+        }
+
         final int subId = 1;
         int[] events = {TelephonyCallback.EVENT_BARRING_INFO_CHANGED};
         SparseArray<BarringInfo.BarringServiceInfo> bsi = new SparseArray(1);
-        bsi.set(BarringInfo.BARRING_SERVICE_TYPE_MO_DATA,
+        bsi.set(BarringInfo.BARRING_SERVICE_TYPE_MMTEL_VOICE,
                 new BarringInfo.BarringServiceInfo(
                         BarringInfo.BarringServiceInfo.BARRING_TYPE_CONDITIONAL,
                         false /*isConditionallyBarred*/,
                         30 /*conditionalBarringFactor*/,
                         10 /*conditionalBarringTimeSeconds*/));
-        BarringInfo info = new BarringInfo(new CellIdentityLte(), bsi);
-
-        // Registering for info causes Barring Info to be sent to caller
+        BarringInfo info = new BarringInfo(
+                new CellIdentityLte(777, 333, 12345, 222, 13579), bsi);
+        // 1. Register listener which requires location access.
         mTelephonyRegistry.listenWithEventList(false, false, subId, mContext.getOpPackageName(),
                 mContext.getAttributionTag(), mTelephonyCallback.callback, events, true);
         processAllMessages();
@@ -925,12 +965,115 @@
         mTelephonyRegistry.notifyBarringInfoChanged(0, subId, info);
         processAllMessages();
         assertEquals(2, mTelephonyCallback.invocationCount.get());
-        assertEquals(mBarringInfo, info);
+        assertEquals(mBarringInfo
+                        .getBarringServiceInfo(BarringInfo.BARRING_SERVICE_TYPE_MMTEL_VOICE),
+                info.getBarringServiceInfo(BarringInfo.BARRING_SERVICE_TYPE_MMTEL_VOICE));
+        String log = mBarringInfo.toString();
+        assertTrue(log.contains("777"));
+        assertTrue(log.contains("333"));
+        if (permission != null && permission.equals(Manifest.permission.ACCESS_FINE_LOCATION)) {
+            assertTrue(log.contains("12345"));
+            assertTrue(log.contains("222"));
+            assertTrue(log.contains("13579"));
+        } else {
+            assertFalse(log.contains("12345"));
+            assertFalse(log.contains("222"));
+            assertFalse(log.contains("13579"));
+        }
 
         // Duplicate BarringInfo notifications do not trigger callback
         mTelephonyRegistry.notifyBarringInfoChanged(0, subId, info);
         processAllMessages();
         assertEquals(2, mTelephonyCallback.invocationCount.get());
+
+        mTelephonyRegistry.listenWithEventList(true, true, subId, mContext.getOpPackageName(),
+                mContext.getAttributionTag(), mTelephonyCallback.callback, new int[0], true);
+        // 2. Register listener renounces location access.
+        mTelephonyRegistry.listenWithEventList(true, true, subId, mContext.getOpPackageName(),
+                mContext.getAttributionTag(), mTelephonyCallback.callback, events, true);
+        processAllMessages();
+        // check receiving barring info without location info.
+        assertEquals(3, mTelephonyCallback.invocationCount.get());
+        assertNotNull(mBarringInfo);
+        assertEquals(mBarringInfo
+                        .getBarringServiceInfo(BarringInfo.BARRING_SERVICE_TYPE_MMTEL_VOICE),
+                info.getBarringServiceInfo(BarringInfo.BARRING_SERVICE_TYPE_MMTEL_VOICE));
+        log = mBarringInfo.toString();
+        assertTrue(log.contains("777"));
+        assertTrue(log.contains("333"));
+        assertFalse(log.contains("12345"));
+        assertFalse(log.contains("222"));
+        assertFalse(log.contains("13579"));
+    }
+
+    @Test
+    public void testRegistrationFailedEventWithLocationFinePermission() throws Exception {
+        checkRegistrationFailedEventWithLocationPermission(
+                Manifest.permission.ACCESS_FINE_LOCATION);
+    }
+    @Test
+    public void testRegistrationFailedEventWithLocationCoarsePermission() throws Exception {
+        checkRegistrationFailedEventWithLocationPermission(
+                Manifest.permission.ACCESS_COARSE_LOCATION);
+    }
+
+    @Test
+    public void testRegistrationFailedEventWithoutLocationPermission() throws Exception {
+        checkRegistrationFailedEventWithLocationPermission(null);
+    }
+
+    private void checkRegistrationFailedEventWithLocationPermission(String permission)
+            throws Exception {
+        // Return a slotIndex / phoneId of 0 for all sub ids given.
+        doReturn(mMockSubInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(anyInt());
+        doReturn(0/*slotIndex*/).when(mMockSubInfo).getSimSlotIndex();
+        doReturn(true).when(mLocationManager).isLocationEnabledForUser(any(UserHandle.class));
+
+        mApplicationInfo.targetSdkVersion = Build.VERSION_CODES.TIRAMISU;
+        doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(anyString(), anyInt());
+        mContextFixture.addCallingOrSelfPermission("");
+        mContextFixture.addCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE);
+        mContextFixture.addCallingOrSelfPermission(
+                android.Manifest.permission.READ_PRECISE_PHONE_STATE);
+        if (permission != null) {
+            mContextFixture.addCallingOrSelfPermission(permission);
+        }
+
+        final int subId = 1;
+        int[] events = {TelephonyCallback.EVENT_REGISTRATION_FAILURE};
+        CellIdentity cellIdentity =
+                new CellIdentityLte(777, 333, 12345, 227, 13579);
+
+        // 1. Register listener which requires location access.
+        mTelephonyRegistry.listenWithEventList(false, false, subId, mContext.getOpPackageName(),
+                mContext.getAttributionTag(), mTelephonyCallback.callback, events, true);
+        processAllMessages();
+        int invocationCount = mTelephonyCallback.invocationCount.get();
+        // Updating the RegistrationFailed info to be updated
+        mTelephonyRegistry.notifyRegistrationFailed(
+                0, subId, cellIdentity, "88888", 1, 333, 22);
+        processAllMessages();
+        assertEquals(invocationCount + 1, mTelephonyCallback.invocationCount.get());
+        if (permission != null && permission.equals(Manifest.permission.ACCESS_FINE_LOCATION)) {
+            assertEquals(cellIdentity, mCellIdentityForRegiFail);
+        } else {
+            assertEquals(cellIdentity.sanitizeLocationInfo(), mCellIdentityForRegiFail);
+        }
+        assertEquals(333, mRegistrationFailReason);
+        mTelephonyRegistry.listenWithEventList(true, true, subId, mContext.getOpPackageName(),
+                mContext.getAttributionTag(), mTelephonyCallback.callback, new int[0], true);
+
+        // 2. Register listener which renounces location access.
+        mTelephonyRegistry.listenWithEventList(true, true, subId, mContext.getOpPackageName(),
+                mContext.getAttributionTag(), mTelephonyCallback.callback, events, true);
+        invocationCount = mTelephonyCallback.invocationCount.get();
+        // Updating the RegistrationFailed info to be updated
+        mTelephonyRegistry.notifyRegistrationFailed(
+                0, subId, cellIdentity, "88888", 1, 555, 22);
+        processAllMessages();
+        assertEquals(invocationCount + 1, mTelephonyCallback.invocationCount.get());
+        assertEquals(cellIdentity.sanitizeLocationInfo(), mCellIdentityForRegiFail);
+        assertEquals(555, mRegistrationFailReason);
     }
 
     /**
@@ -1070,7 +1213,8 @@
                 anyString(), any())).thenReturn(true);
         TelephonyDisplayInfo displayInfo = new TelephonyDisplayInfo(
                 TelephonyManager.NETWORK_TYPE_LTE,
-                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED);
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
+                false);
 
         // Notify with invalid subId on default phone. Should NOT trigger callback.
         mTelephonyRegistry.notifyDisplayInfoChanged(0, INVALID_SUBSCRIPTION_ID, displayInfo);
@@ -1096,10 +1240,12 @@
                 anyString(), any())).thenReturn(false);
         TelephonyDisplayInfo displayInfo = new TelephonyDisplayInfo(
                 TelephonyManager.NETWORK_TYPE_LTE,
-                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED);
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
+                false);
         TelephonyDisplayInfo expectDisplayInfo = new TelephonyDisplayInfo(
                 TelephonyManager.NETWORK_TYPE_LTE,
-                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE);
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE,
+                false);
 
         // Notify with invalid subId on default phone. Should NOT trigger callback.
         mTelephonyRegistry.notifyDisplayInfoChanged(0, INVALID_SUBSCRIPTION_ID, displayInfo);
@@ -1121,10 +1267,12 @@
         int[] events = {TelephonyCallback.EVENT_DISPLAY_INFO_CHANGED};
         TelephonyDisplayInfo displayInfo = new TelephonyDisplayInfo(
                 TelephonyManager.NETWORK_TYPE_LTE,
-                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED);
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
+                false);
         TelephonyDisplayInfo expectDisplayInfo = new TelephonyDisplayInfo(
                 TelephonyManager.NETWORK_TYPE_LTE,
-                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE);
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE,
+                false);
         TelephonyCallback telephonyCallback2 = new TelephonyCallbackWrapper() {
             @Override
             public void onDisplayInfoChanged(TelephonyDisplayInfo displayInfoNotify) {
@@ -1165,7 +1313,7 @@
         final int subId = 1;
 
         // Return a slotIndex / phoneId of 0 for subId 1.
-        doReturn(new int[] {subId}).when(mSubscriptionController).getSubId(phoneId);
+        doReturn(subId).when(mSubscriptionController).getSubId(phoneId);
         doReturn(mMockSubInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(subId);
         doReturn(phoneId).when(mMockSubInfo).getSimSlotIndex();
         mServiceManagerMockedServices.put("isub", mSubscriptionController);
diff --git a/tests/telephonytests/src/com/android/internal/telephony/TelephonyTest.java b/tests/telephonytests/src/com/android/internal/telephony/TelephonyTest.java
index 109a983..9465bb6 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/TelephonyTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/TelephonyTest.java
@@ -97,13 +97,13 @@
 import com.android.internal.telephony.data.AccessNetworksManager;
 import com.android.internal.telephony.data.CellularNetworkValidator;
 import com.android.internal.telephony.data.DataConfigManager;
-import com.android.internal.telephony.data.DataEnabledOverride;
 import com.android.internal.telephony.data.DataNetworkController;
 import com.android.internal.telephony.data.DataProfileManager;
 import com.android.internal.telephony.data.DataRetryManager;
 import com.android.internal.telephony.data.DataServiceManager;
 import com.android.internal.telephony.data.DataSettingsManager;
 import com.android.internal.telephony.data.LinkBandwidthEstimator;
+import com.android.internal.telephony.data.PhoneSwitcher;
 import com.android.internal.telephony.emergency.EmergencyNumberTracker;
 import com.android.internal.telephony.imsphone.ImsExternalCallTracker;
 import com.android.internal.telephony.imsphone.ImsPhone;
@@ -111,6 +111,7 @@
 import com.android.internal.telephony.metrics.ImsStats;
 import com.android.internal.telephony.metrics.MetricsCollector;
 import com.android.internal.telephony.metrics.PersistAtomsStorage;
+import com.android.internal.telephony.metrics.ServiceStateStats;
 import com.android.internal.telephony.metrics.SmsStats;
 import com.android.internal.telephony.metrics.VoiceCallSessionStats;
 import com.android.internal.telephony.test.SimulatedCommands;
@@ -213,6 +214,7 @@
     protected RuimRecords mRuimRecords;
     protected IsimUiccRecords mIsimUiccRecords;
     protected ProxyController mProxyController;
+    protected PhoneSwitcher mPhoneSwitcher;
     protected Singleton<IActivityManager> mIActivityManagerSingleton;
     protected IActivityManager mIActivityManager;
     protected IIntentSender mIIntentSender;
@@ -237,7 +239,6 @@
     protected SubscriptionInfoUpdater mSubInfoRecordUpdater;
     protected LocaleTracker mLocaleTracker;
     protected RestrictedState mRestrictedState;
-    protected DataEnabledOverride mDataEnabledOverride;
     protected PhoneConfigurationManager mPhoneConfigurationManager;
     protected CellularNetworkValidator mCellularNetworkValidator;
     protected UiccCard mUiccCard;
@@ -261,6 +262,7 @@
     protected CellLocation mCellLocation;
     protected DataServiceManager mMockedWwanDataServiceManager;
     protected DataServiceManager mMockedWlanDataServiceManager;
+    protected ServiceStateStats mServiceStateStats;
 
     // Initialized classes
     protected ActivityManager mActivityManager;
@@ -443,6 +445,7 @@
         mRuimRecords = Mockito.mock(RuimRecords.class);
         mIsimUiccRecords = Mockito.mock(IsimUiccRecords.class);
         mProxyController = Mockito.mock(ProxyController.class);
+        mPhoneSwitcher = Mockito.mock(PhoneSwitcher.class);
         mIActivityManagerSingleton = Mockito.mock(Singleton.class);
         mIActivityManager = Mockito.mock(IActivityManager.class);
         mIIntentSender = Mockito.mock(IIntentSender.class);
@@ -467,7 +470,6 @@
         mSubInfoRecordUpdater = Mockito.mock(SubscriptionInfoUpdater.class);
         mLocaleTracker = Mockito.mock(LocaleTracker.class);
         mRestrictedState = Mockito.mock(RestrictedState.class);
-        mDataEnabledOverride = Mockito.mock(DataEnabledOverride.class);
         mPhoneConfigurationManager = Mockito.mock(PhoneConfigurationManager.class);
         mCellularNetworkValidator = Mockito.mock(CellularNetworkValidator.class);
         mUiccCard = Mockito.mock(UiccCard.class);
@@ -491,6 +493,7 @@
         mCellLocation = Mockito.mock(CellLocation.class);
         mMockedWwanDataServiceManager = Mockito.mock(DataServiceManager.class);
         mMockedWlanDataServiceManager = Mockito.mock(DataServiceManager.class);
+        mServiceStateStats = Mockito.mock(ServiceStateStats.class);
 
         TelephonyManager.disableServiceHandleCaching();
         PropertyInvalidatedCache.disableForTestMode();
@@ -694,7 +697,7 @@
         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_LTE).when(mServiceState)
                 .getRilDataRadioTechnology();
         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_LTE,
-                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE))
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE, false))
                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
         doReturn(mPhone).when(mCT).getPhone();
         doReturn(mImsEcbm).when(mImsManager).getEcbmInterface();
@@ -710,6 +713,7 @@
 
         doReturn(TelephonyManager.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
         doReturn(mServiceState).when(mSST).getServiceState();
+        doReturn(mServiceStateStats).when(mSST).getServiceStateStats();
         mSST.mSS = mServiceState;
         mSST.mRestrictedState = mRestrictedState;
         mServiceManagerMockedServices.put("connectivity_metrics_logger", mConnMetLoggerBinder);
@@ -719,13 +723,10 @@
         doReturn(new int[]{AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
                 AccessNetworkConstants.TRANSPORT_TYPE_WLAN})
                 .when(mAccessNetworksManager).getAvailableTransports();
-        doReturn(new int[]{AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
-                AccessNetworkConstants.TRANSPORT_TYPE_WLAN})
-                .when(mAccessNetworksManager).getAvailableTransports();
         doReturn(true).when(mDataSettingsManager).isDataEnabled();
         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
                 anyInt(), anyInt());
-        doReturn(RIL.RADIO_HAL_VERSION_2_0).when(mPhone).getHalVersion();
+        doReturn(RIL.RADIO_HAL_VERSION_2_0).when(mPhone).getHalVersion(anyInt());
         doReturn(2).when(mSignalStrength).getLevel();
 
         // WiFi
@@ -809,6 +810,7 @@
         replaceInstance(CdmaSubscriptionSourceManager.class, "sInstance", null, mCdmaSSM);
         replaceInstance(SubscriptionController.class, "sInstance", null, mSubscriptionController);
         replaceInstance(ProxyController.class, "sProxyController", null, mProxyController);
+        replaceInstance(PhoneSwitcher.class, "sPhoneSwitcher", null, mPhoneSwitcher);
         replaceInstance(ActivityManager.class, "IActivityManagerSingleton", null,
                 mIActivityManagerSingleton);
         replaceInstance(CdmaSubscriptionSourceManager.class,
diff --git a/tests/telephonytests/src/com/android/internal/telephony/cat/CATServiceTest.java b/tests/telephonytests/src/com/android/internal/telephony/cat/CATServiceTest.java
new file mode 100644
index 0000000..6bc31da
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/cat/CATServiceTest.java
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.cat;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.anyLong;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.telephony.SmsMessage;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.ProxyController;
+import com.android.internal.telephony.SmsController;
+import com.android.internal.telephony.TelephonyTest;
+import com.android.internal.telephony.uicc.IccCardApplicationStatus;
+import com.android.internal.telephony.uicc.IccCardStatus;
+import com.android.internal.telephony.uicc.IccFileHandler;
+import com.android.internal.telephony.uicc.IccIoResult;
+import com.android.internal.telephony.uicc.IccUtils;
+import com.android.internal.telephony.uicc.UiccCard;
+import com.android.internal.telephony.uicc.UiccProfile;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class CATServiceTest extends TelephonyTest {
+
+    //Mocked Classes
+    @Mock
+    private RilMessageDecoder mRilMessageDecoder;
+    private IccFileHandler mIccFileHandler;
+    private SmsController mSmsController;
+    private CommandDetails mCommandDetails;
+    private CatService mCatService;
+    private IccCardStatus mIccCardStatus;
+    private IccIoResult mIccIoResult;
+
+    private String mData =
+            "D059810301130082028183051353656E64696E672072657175657374202E2E2E0607911989548056780B"
+                    + "3051FF05812143F500F6082502700000201115001500BFFF01BA23C2169EA9B02D7A7FBAA0"
+                    + "DAABFEE8B8DE9DA06DCD234E";
+    private byte[] mRawdata = IccUtils.hexStringToBytes(mData);
+    private List<ComprehensionTlv> mCtlvs;
+
+    public CATServiceTest() {
+        super();
+    }
+
+    private IccCardApplicationStatus composeUiccApplicationStatus(
+            IccCardApplicationStatus.AppType appType,
+            IccCardApplicationStatus.AppState appState, String aid) {
+        IccCardApplicationStatus mIccCardAppStatus = new IccCardApplicationStatus();
+        mIccCardAppStatus.aid = aid;
+        mIccCardAppStatus.app_type = appType;
+        mIccCardAppStatus.aid = aid;
+        mIccCardAppStatus.app_type = appType;
+        mIccCardAppStatus.app_state = appState;
+        mIccCardAppStatus.pin1 = mIccCardAppStatus.pin2 =
+                IccCardStatus.PinState.PINSTATE_ENABLED_VERIFIED;
+        return mIccCardAppStatus;
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(getClass().getSimpleName());
+        mRilMessageDecoder = mock(RilMessageDecoder.class);
+        mIccFileHandler = mock(IccFileHandler.class);
+        mSmsController = mock(SmsController.class);
+        mIccCardStatus = mock(IccCardStatus.class);
+        mProxyController = mock(ProxyController.class);
+        mUiccCard = mock(UiccCard.class);
+        IccCardApplicationStatus umtsApp = composeUiccApplicationStatus(
+                IccCardApplicationStatus.AppType.APPTYPE_USIM,
+                IccCardApplicationStatus.AppState.APPSTATE_UNKNOWN, "0xA2");
+        mIccCardStatus.mApplications = new IccCardApplicationStatus[]{umtsApp};
+        mIccCardStatus.mCdmaSubscriptionAppIndex =
+                mIccCardStatus.mImsSubscriptionAppIndex =
+                        mIccCardStatus.mGsmUmtsSubscriptionAppIndex = -1;
+        mIccIoResult = new IccIoResult(0x90, 0x00, IccUtils.hexStringToBytes("FF40"));
+        mSimulatedCommands.setIccIoResultForApduLogicalChannel(mIccIoResult);
+        mUiccProfile = new UiccProfile(mContext, mSimulatedCommands, mIccCardStatus,
+                0 /* phoneId */, mUiccCard, new Object());
+        processAllMessages();
+        logd("Created UiccProfile");
+        processAllMessages();
+        mCatService = CatService.getInstance(mSimulatedCommands, mUiccController.mContext,
+                mUiccProfile, mUiccController.getSlotIdFromPhoneId(0));
+        logd("Created CATService");
+        createCommandDetails();
+        createComprehensionTlvList();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mUiccProfile = null;
+        mCatService = null;
+        mCtlvs = null;
+        mProxyController = null;
+        mRilMessageDecoder = null;
+        mCommandDetails = null;
+        super.tearDown();
+    }
+
+    private void createCommandDetails() {
+        mCommandDetails = mock(CommandDetails.class);
+        mCommandDetails.compRequired = true;
+        mCommandDetails.commandNumber = 1;
+        mCommandDetails.typeOfCommand = 19;
+        mCommandDetails.commandQualifier = 0;
+    }
+
+    private void createComprehensionTlvList() {
+        ComprehensionTlv ctlv1 = new ComprehensionTlv(1, false, 3, mRawdata, 4);
+        ComprehensionTlv ctlv2 = new ComprehensionTlv(2, false, 2, mRawdata, 9);
+        ComprehensionTlv ctlv3 = new ComprehensionTlv(5, false, 19, mRawdata, 13);
+        ComprehensionTlv ctlv4 = new ComprehensionTlv(6, false, 7, mRawdata, 34);
+        ComprehensionTlv ctlv5 = new ComprehensionTlv(11, false, 48, mRawdata, 43);
+        mCtlvs = new ArrayList<>();
+        mCtlvs.add(ctlv1);
+        mCtlvs.add(ctlv2);
+        mCtlvs.add(ctlv3);
+        mCtlvs.add(ctlv4);
+        mCtlvs.add(ctlv5);
+    }
+
+    @Test
+    public void testSendSmsCommandParams() throws Exception {
+        ComprehensionTlv ctlv = new ComprehensionTlv(11, false, 48, mRawdata, 43);
+        SmsMessage smsMessage = ValueParser.retrieveTpduAsSmsMessage(ctlv);
+        assertNotNull(smsMessage);
+        assertEquals("12345", smsMessage.getRecipientAddress());
+    }
+
+    @Test
+    public void testSendSTKSmsViaCatService() {
+        CommandParams cmdPrms = new CommandParams(mCommandDetails);
+        when(mProxyController.getSmsController()).thenReturn(mSmsController);
+        mCatService.sendStkSms("test", "12345", 1, cmdPrms, mProxyController);
+        verify(mSmsController, Mockito.times(1)).sendTextForSubscriber(anyInt(),
+                anyString(), nullable(String.class), anyString(), nullable(String.class),
+                anyString(), Mockito.anyObject(), any(), eq(false), anyLong(), eq(true), eq(true));
+    }
+
+    @Test
+    public void testprocessSMSEventNotify() throws Exception {
+        CommandParamsFactory cmdPF = CommandParamsFactory.getInstance(mRilMessageDecoder,
+                mIccFileHandler, mContext);
+        assertEquals(false, cmdPF.processSMSEventNotify(mCommandDetails, mCtlvs));
+    }
+
+    @Test
+    public void testSkipFdnCheckforSTKSmsViaCatService() {
+        CommandParams cmdPrms = new CommandParams(mCommandDetails);
+        when(mProxyController.getSmsController()).thenReturn(mSmsController);
+        mCatService.sendStkSms("test", "12345", 1, cmdPrms, mProxyController);
+        verify(mSmsController, Mockito.times(0)).isNumberBlockedByFDN(1, "12345",
+                "com.android.internal.telephony");
+    }
+
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/cdma/CdmaInboundSmsHandlerTest.java b/tests/telephonytests/src/com/android/internal/telephony/cdma/CdmaInboundSmsHandlerTest.java
index 5df94e5..3445939 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/cdma/CdmaInboundSmsHandlerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/cdma/CdmaInboundSmsHandlerTest.java
@@ -57,11 +57,6 @@
 import com.android.internal.util.IState;
 import com.android.internal.util.StateMachine;
 
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
@@ -69,6 +64,11 @@
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
 @RunWith(AndroidTestingRunner.class)
 @TestableLooper.RunWithLooper
 public class CdmaInboundSmsHandlerTest extends TelephonyTest {
@@ -157,7 +157,7 @@
                 Telephony.Sms.CONTENT_URI.getAuthority(), mContentProvider);
 
         mCdmaInboundSmsHandler = CdmaInboundSmsHandler.makeInboundSmsHandler(mContext,
-            mSmsStorageMonitor, mPhone, null);
+            mSmsStorageMonitor, mPhone, null, mTestableLooper.getLooper());
         monitorTestableLooper(new TestableLooper(mCdmaInboundSmsHandler.getHandler().getLooper()));
         processAllMessages();
     }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/AccessNetworksManagerTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/AccessNetworksManagerTest.java
index c8eee8c..68e1fb2 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/data/AccessNetworksManagerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/data/AccessNetworksManagerTest.java
@@ -18,7 +18,6 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
-import static org.junit.Assume.assumeFalse;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.anyString;
@@ -32,8 +31,10 @@
 import android.content.IntentFilter;
 import android.content.pm.ServiceInfo;
 import android.net.NetworkCapabilities;
+import android.os.AsyncResult;
 import android.os.IBinder;
 import android.os.Looper;
+import android.os.Message;
 import android.telephony.AccessNetworkConstants;
 import android.telephony.AccessNetworkConstants.AccessNetworkType;
 import android.telephony.NetworkService;
@@ -113,7 +114,7 @@
         processAllMessages();
         replaceInstance(AccessNetworksManager.class, "mDataConfigManager",
                 mAccessNetworksManager, mMockedDataConfigManager);
-        assumeFalse(mAccessNetworksManager.isInLegacyMode());
+
         logd("-setUp");
     }
 
@@ -150,6 +151,32 @@
     }
 
     @Test
+    public void testGuideTransportTypeForEmergencyDataNetwork() throws Exception {
+        doAnswer(invocation -> {
+            int accessNetwork = AccessNetworkType.UNKNOWN;
+            if (invocation.getArguments()[1].equals(AccessNetworkConstants.TRANSPORT_TYPE_WLAN)) {
+                accessNetwork = AccessNetworkType.IWLAN;
+            } else if (invocation.getArguments()[1]
+                    .equals(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)) {
+                accessNetwork = AccessNetworkType.EUTRAN;
+            }
+            mQnsCallback.onQualifiedNetworkTypesChanged(ApnSetting.TYPE_EMERGENCY,
+                    new int[]{accessNetwork});
+            return null;
+        }).when(mMockedQns).reportEmergencyDataNetworkPreferredTransportChanged(anyInt(), anyInt());
+
+        AsyncResult asyncResult =
+                new AsyncResult(null, AccessNetworkConstants.TRANSPORT_TYPE_WLAN, null);
+        Message msg = this.mAccessNetworksManager
+                .obtainMessage(1 /* EVENT_GUIDE_TRANSPORT_TYPE_FOR_EMERGENCY */, asyncResult);
+        mAccessNetworksManager.sendMessage(msg);
+        processAllMessages();
+
+        assertThat(mAccessNetworksManager.getPreferredTransport(ApnSetting.TYPE_EMERGENCY))
+                .isEqualTo(AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
+    }
+
+    @Test
     public void testEmptyNetworkTypes() throws Exception {
         testQualifiedNetworkTypesChanged();
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/CellularNetworkValidatorTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/CellularNetworkValidatorTest.java
index 511b2df..3773756 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/data/CellularNetworkValidatorTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/data/CellularNetworkValidatorTest.java
@@ -490,6 +490,7 @@
         CarrierConfigManager carrierConfigManager = (CarrierConfigManager)
                 mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
         PersistableBundle bundle = carrierConfigManager.getConfigForSubId(anyInt());
-        bundle.putLong(CarrierConfigManager.KEY_DATA_SWITCH_VALIDATION_MIN_GAP_LONG, ttl);
+        bundle.putLong(CarrierConfigManager.KEY_DATA_SWITCH_VALIDATION_MIN_INTERVAL_MILLIS_LONG,
+                ttl);
     }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/DataConfigManagerTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/DataConfigManagerTest.java
index 7b0443f..f312808 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/data/DataConfigManagerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/data/DataConfigManagerTest.java
@@ -18,7 +18,12 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.when;
+
 import android.os.Looper;
+import android.os.PersistableBundle;
 import android.testing.AndroidTestingRunner;
 import android.testing.TestableLooper;
 
@@ -33,11 +38,14 @@
 @TestableLooper.RunWithLooper
 public class DataConfigManagerTest extends TelephonyTest {
     private DataConfigManager mDataConfigManagerUT;
+    private PersistableBundle mBundle;
 
     @Before
     public void setUp() throws Exception {
         logd("DataConfigManagerTest +Setup!");
         super.setUp(getClass().getSimpleName());
+        mBundle = mContextFixture.getCarrierConfigBundle();
+        when(mCarrierConfigManager.getConfigForSubId(anyInt(), any())).thenReturn(mBundle);
         mDataConfigManagerUT = new DataConfigManager(mPhone, Looper.myLooper());
         logd("DataConfigManagerTest -Setup!");
     }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/DataEnabledOverrideTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/DataEnabledOverrideTest.java
deleted file mode 100644
index 252274c..0000000
--- a/tests/telephonytests/src/com/android/internal/telephony/data/DataEnabledOverrideTest.java
+++ /dev/null
@@ -1,218 +0,0 @@
-/*
- * Copyright (C) 2019 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.internal.telephony.data;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-import static org.mockito.Mockito.doReturn;
-
-import android.telephony.data.ApnSetting;
-import android.test.suitebuilder.annotation.SmallTest;
-
-import com.android.internal.telephony.PhoneConstants;
-import com.android.internal.telephony.TelephonyTest;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-public class DataEnabledOverrideTest extends TelephonyTest {
-
-    @Before
-    public void setUp() throws Exception {
-        super.setUp(getClass().getSimpleName());
-    }
-
-    @After
-    public void tearDown() throws Exception {
-        super.tearDown();
-    }
-
-    @Test
-    @SmallTest
-    public void testCreateByRules() throws Exception {
-        DataEnabledOverride deo1 = new DataEnabledOverride(
-                "mms=nonDefault, default=inVoiceCall&nonDefault");
-        DataEnabledOverride deo2 = new DataEnabledOverride(
-                "mms=nonDefault, default=inVoiceCall");
-        DataEnabledOverride deo3 = new DataEnabledOverride(
-                "default=inVoiceCall&nonDefault, mms=nonDefault");
-        assertEquals(deo1, deo3);
-        assertNotEquals(deo1, deo2);
-    }
-
-    @Test
-    @SmallTest
-    public void testOverrideEnabled() throws Exception {
-        DataEnabledOverride dataEnabledOverride = new DataEnabledOverride(
-                "mms=nonDefault, default=inVoiceCall&nonDefault");
-        doReturn(1).when(mPhone).getSubId();
-        doReturn(2).when(mSubscriptionController).getDefaultSmsSubId();
-        assertTrue(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_MMS));
-
-        doReturn(PhoneConstants.State.IDLE).when(mPhone).getState();
-
-        assertFalse(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_DEFAULT));
-
-        doReturn(PhoneConstants.State.OFFHOOK).when(mPhone).getState();
-
-        assertTrue(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_DEFAULT));
-    }
-
-    @Test
-    @SmallTest
-    public void testGetRules() throws Exception {
-        DataEnabledOverride dataEnabledOverride = new DataEnabledOverride(
-                "mms=nonDefault, default=inVoiceCall&nonDefault");
-        String rules = dataEnabledOverride.getRules();
-        assertEquals(dataEnabledOverride, new DataEnabledOverride(rules));
-    }
-
-    @Test
-    @SmallTest
-    public void testUpdateRules() throws Exception {
-        DataEnabledOverride dataEnabledOverride = new DataEnabledOverride(
-                "mms=nonDefault, default=inVoiceCall&nonDefault");
-        doReturn(1).when(mPhone).getSubId();
-        doReturn(2).when(mSubscriptionController).getDefaultSmsSubId();
-        assertTrue(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_MMS));
-
-        doReturn(PhoneConstants.State.IDLE).when(mPhone).getState();
-
-        assertFalse(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_DEFAULT));
-
-        doReturn(PhoneConstants.State.OFFHOOK).when(mPhone).getState();
-
-        assertTrue(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_DEFAULT));
-
-        dataEnabledOverride.updateRules("");
-
-        assertFalse(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_MMS));
-        assertFalse(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_DEFAULT));
-    }
-
-    @Test
-    @SmallTest
-    public void testAlwaysEnabled() throws Exception {
-        DataEnabledOverride dataEnabledOverride = new DataEnabledOverride(
-                "mms   =unconditionally,    default=  unconditionally ,   ");
-        assertTrue(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_MMS));
-        assertTrue(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_DEFAULT));
-
-        dataEnabledOverride.updateRules("");
-
-        assertFalse(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_MMS));
-        assertFalse(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_DEFAULT));
-    }
-
-    @Test
-    @SmallTest
-    public void testAllApnTypesInRule() throws Exception {
-        DataEnabledOverride dataEnabledOverride = new DataEnabledOverride("*=inVoiceCall");
-        doReturn(PhoneConstants.State.OFFHOOK).when(mPhone).getState();
-
-        assertTrue(dataEnabledOverride.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_FOTA));
-    }
-
-    @Test
-    @SmallTest
-    public void testInvalidRules() throws Exception {
-        try {
-            DataEnabledOverride dataEnabledOverride = new DataEnabledOverride(
-                    "default=xyz");
-            fail("Invalid conditions but not threw IllegalArgumentException.");
-        } catch (IllegalArgumentException ex) {
-
-        }
-
-        try {
-            DataEnabledOverride dataEnabledOverride = new DataEnabledOverride(
-                    "mms=");
-            fail("Invalid conditions but not threw IllegalArgumentException.");
-        } catch (IllegalArgumentException ex) {
-
-        }
-
-        try {
-            DataEnabledOverride dataEnabledOverride = new DataEnabledOverride(
-                    "abc=nonDefault");
-            fail("Invalid APN type but not threw IllegalArgumentException.");
-        } catch (IllegalArgumentException ex) {
-
-        }
-
-        try {
-            DataEnabledOverride dataEnabledOverride = new DataEnabledOverride(
-                    " =nonDefault");
-            fail("Invalid APN type but not threw IllegalArgumentException.");
-        } catch (IllegalArgumentException ex) {
-
-        }
-
-        try {
-            DataEnabledOverride dataEnabledOverride = new DataEnabledOverride(
-                    "Invalid rule");
-            fail("Invalid rule but not threw IllegalArgumentException.");
-        } catch (IllegalArgumentException ex) {
-
-        }
-    }
-
-    @Test
-    @SmallTest
-    public void testSetAlwaysAllowMms() throws Exception {
-        DataEnabledOverride deo = new DataEnabledOverride("");
-        deo.setAlwaysAllowMms(true);
-        assertTrue(deo.shouldOverrideDataEnabledSettings(mPhone, ApnSetting.TYPE_MMS));
-        deo.setAlwaysAllowMms(false);
-        assertFalse(deo.shouldOverrideDataEnabledSettings(mPhone, ApnSetting.TYPE_MMS));
-    }
-
-    @Test
-    @SmallTest
-    public void testSetDataAllowedInVoiceCall() throws Exception {
-        DataEnabledOverride deo = new DataEnabledOverride("");
-        deo.setDataAllowedInVoiceCall(true);
-        assertFalse(deo.getRules(), deo.shouldOverrideDataEnabledSettings(mPhone,
-                ApnSetting.TYPE_DEFAULT));
-        assertTrue(deo.isDataAllowedInVoiceCall());
-        doReturn(1).when(mPhone).getSubId();
-        doReturn(2).when(mSubscriptionController).getDefaultSmsSubId();
-
-        doReturn(PhoneConstants.State.OFFHOOK).when(mPhone).getState();
-        deo.setDataAllowedInVoiceCall(false);
-        assertFalse(deo.getRules(), deo.shouldOverrideDataEnabledSettings(
-                mPhone, ApnSetting.TYPE_DEFAULT));
-        assertFalse(deo.isDataAllowedInVoiceCall());
-    }
-}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/DataNetworkControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/DataNetworkControllerTest.java
index d5abf0f..cd50dfb 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/data/DataNetworkControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/data/DataNetworkControllerTest.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony.data;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_DATA;
+
 import static com.android.internal.telephony.data.DataNetworkController.DataNetworkControllerCallback;
 import static com.android.internal.telephony.data.DataNetworkController.NetworkRequestList;
 
@@ -41,7 +43,6 @@
 import static org.mockito.Mockito.when;
 
 import android.annotation.NonNull;
-import android.content.Intent;
 import android.net.ConnectivityManager;
 import android.net.InetAddresses;
 import android.net.LinkAddress;
@@ -73,6 +74,7 @@
 import android.telephony.PreciseDataConnectionState;
 import android.telephony.ServiceState;
 import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
 import android.telephony.SubscriptionPlan;
 import android.telephony.TelephonyDisplayInfo;
 import android.telephony.TelephonyManager;
@@ -103,6 +105,7 @@
 import com.android.internal.telephony.MultiSimSettingController;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.RIL;
 import com.android.internal.telephony.TelephonyTest;
 import com.android.internal.telephony.data.AccessNetworksManager.AccessNetworksManagerCallback;
@@ -162,6 +165,7 @@
     private final SparseArray<RegistrantList> mDataCallListChangedRegistrants = new SparseArray<>();
     private DataNetworkController mDataNetworkControllerUT;
     private PersistableBundle mCarrierConfig;
+    private CarrierConfigManager.CarrierConfigChangeListener mCarrierConfigChangeListener;
 
     private AccessNetworksManagerCallback mAccessNetworksManagerCallback;
     private LinkBandwidthEstimatorCallback mLinkBandwidthEstimatorCallback;
@@ -468,9 +472,10 @@
 
     private void carrierConfigChanged() {
         // Trigger carrier config reloading
-        Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
-        intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, 0);
-        mContext.sendBroadcast(intent);
+        mCarrierConfigChangeListener.onCarrierConfigChanged(0 /* logicalSlotIndex */,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID,
+                TelephonyManager.UNKNOWN_CARRIER_ID, TelephonyManager.UNKNOWN_CARRIER_ID);
+
         processAllMessages();
     }
 
@@ -506,9 +511,13 @@
 
     private void serviceStateChanged(@NetworkType int networkType,
             @RegistrationState int regState) {
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED))
+                .build();
 
         serviceStateChanged(networkType, regState, regState,
                 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, dsri);
@@ -538,9 +547,13 @@
             @RegistrationState int dataRegState, @RegistrationState int voiceRegState,
             @RegistrationState int iwlanRegState, DataSpecificRegistrationInfo dsri) {
         if (dsri == null) {
-            dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                    new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
-                            LteVopsSupportInfo.LTE_STATUS_SUPPORTED));
+            dsri = new DataSpecificRegistrationInfo.Builder(8)
+                    .setNrAvailable(true)
+                    .setEnDcAvailable(true)
+                    .setVopsSupportInfo(new LteVopsSupportInfo(
+                            LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
+                            LteVopsSupportInfo.LTE_STATUS_SUPPORTED))
+                    .build();
         }
 
         ServiceState ss = new ServiceState();
@@ -712,8 +725,8 @@
         doReturn(true).when(mSST).getPowerStateFromCarrier();
         doReturn(true).when(mSST).isConcurrentVoiceAndDataAllowed();
         doReturn(PhoneConstants.State.IDLE).when(mCT).getState();
-        doReturn("").when(mSubscriptionController).getDataEnabledOverrideRules(anyInt());
-        doReturn(true).when(mSubscriptionController).setDataEnabledOverrideRules(
+        doReturn("").when(mSubscriptionController).getEnabledMobileDataPolicies(anyInt());
+        doReturn(true).when(mSubscriptionController).setEnabledMobileDataPolicies(
                 anyInt(), anyString());
 
         List<SubscriptionInfo> infoList = new ArrayList<>();
@@ -721,7 +734,8 @@
         doReturn(infoList).when(mSubscriptionController).getSubscriptionsInGroup(
                 any(), any(), any());
         doReturn(true).when(mSubscriptionController).isActiveSubId(anyInt());
-        doReturn(0).when(mSubscriptionController).getPhoneId(anyInt());
+        doReturn(0).when(mSubscriptionController).getPhoneId(1);
+        doReturn(1).when(mSubscriptionController).getPhoneId(2);
 
         for (int transport : new int[]{AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
                 AccessNetworkConstants.TRANSPORT_TYPE_WLAN}) {
@@ -764,6 +778,9 @@
 
         doReturn(-1).when(mPhone).getSubId();
 
+        // Capture listener to emulate the carrier config change notification used later
+        ArgumentCaptor<CarrierConfigManager.CarrierConfigChangeListener> listenerArgumentCaptor =
+                ArgumentCaptor.forClass(CarrierConfigManager.CarrierConfigChangeListener.class);
         // Note that creating a "real" data network controller will also result in creating
         // real DataRetryManager, DataConfigManager, etc...Normally in unit test we should isolate
         // other modules and make them mocked, but only focusing on testing the unit we would like
@@ -771,6 +788,10 @@
         // between DataNetworkController and its sub-modules, we intend to make those modules "real"
         // as well, except some modules below we replaced with mocks.
         mDataNetworkControllerUT = new DataNetworkController(mPhone, Looper.myLooper());
+        verify(mCarrierConfigManager).registerCarrierConfigChangeListener(any(),
+                listenerArgumentCaptor.capture());
+        mCarrierConfigChangeListener = listenerArgumentCaptor.getAllValues().get(0);
+        assertThat(mCarrierConfigChangeListener).isNotNull();
         doReturn(mDataNetworkControllerUT).when(mPhone).getDataNetworkController();
 
         doReturn(1).when(mPhone).getSubId();
@@ -1194,16 +1215,32 @@
 
     @Test
     public void testDataNetworkControllerCallback() throws Exception {
+        Field field = DataNetworkController.class.getDeclaredField(
+                "mDataNetworkControllerCallbacks");
+        field.setAccessible(true);
+        Set<DataNetworkControllerCallback> dataNetworkControllerCallbacks =
+                (Set<DataNetworkControllerCallback>) field.get(mDataNetworkControllerUT);
+
+        // Verify register callback
         mDataNetworkControllerUT.registerDataNetworkControllerCallback(
                 mMockedDataNetworkControllerCallback);
+        TelephonyNetworkRequest request = createNetworkRequest(
+                NetworkCapabilities.NET_CAPABILITY_INTERNET);
+        mDataNetworkControllerUT.addNetworkRequest(request);
         processAllMessages();
-        testSetupDataNetwork();
         verify(mMockedDataNetworkControllerCallback).onAnyDataNetworkExistingChanged(eq(true));
         verify(mMockedDataNetworkControllerCallback).onInternetDataNetworkConnected(any());
 
-        mDataNetworkControllerUT.unregisterDataNetworkControllerCallback(
-                mMockedDataNetworkControllerCallback);
+        int countOfCallbacks = dataNetworkControllerCallbacks.size();
+
+        // Verify unregister callback
+        mDataNetworkControllerUT.removeNetworkRequest(request);
         processAllMessages();
+        getDataNetworks().get(0).tearDown(DataNetwork
+                .TEAR_DOWN_REASON_CONNECTIVITY_SERVICE_UNWANTED);
+        processAllFutureMessages();
+
+        assertEquals(countOfCallbacks - 1, dataNetworkControllerCallbacks.size());
     }
 
     @Test
@@ -1577,7 +1614,8 @@
         mDataNetworkControllerUT.getDataSettingsManager().setDataEnabled(
                 TelephonyManager.DATA_ENABLED_REASON_USER, false, mContext.getOpPackageName());
         // Always allow MMS
-        mDataNetworkControllerUT.getDataSettingsManager().setAlwaysAllowMmsData(true);
+        mDataNetworkControllerUT.getDataSettingsManager().setMobileDataPolicy(TelephonyManager
+                .MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED, true);
         processAllMessages();
         mDataNetworkControllerUT.addNetworkRequest(
                 createNetworkRequest(NetworkCapabilities.NET_CAPABILITY_MMS));
@@ -1590,7 +1628,8 @@
         verifyNoConnectedNetworkHasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL);
 
         // Remove MMS data enabled override
-        mDataNetworkControllerUT.getDataSettingsManager().setAlwaysAllowMmsData(false);
+        mDataNetworkControllerUT.getDataSettingsManager().setMobileDataPolicy(TelephonyManager
+                .MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED, false);
         processAllMessages();
 
         // Make sure MMS is torn down when the override is disabled.
@@ -1608,7 +1647,8 @@
         serviceStateChanged(TelephonyManager.NETWORK_TYPE_LTE,
                 NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING);
         // Always allow MMS
-        mDataNetworkControllerUT.getDataSettingsManager().setAlwaysAllowMmsData(true);
+        mDataNetworkControllerUT.getDataSettingsManager().setMobileDataPolicy(TelephonyManager
+                .MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED, true);
         processAllMessages();
 
         mDataNetworkControllerUT.addNetworkRequest(
@@ -1621,6 +1661,89 @@
     }
 
     @Test
+    public void testIsDataEnabledOverriddenForApnDataDuringCall() throws Exception {
+        doReturn(1).when(mPhone).getSubId();
+        doReturn(2).when(mSubscriptionController).getDefaultDataSubId();
+        // Data disabled
+        mDataNetworkControllerUT.getDataSettingsManager().setDataEnabled(
+                TelephonyManager.DATA_ENABLED_REASON_USER, false, mContext.getOpPackageName());
+
+        // Enable during data call mobile policy
+        mDataNetworkControllerUT.getDataSettingsManager().setMobileDataPolicy(TelephonyManager
+                .MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL, true);
+        processAllMessages();
+
+        // No active phone call
+        doReturn(PhoneConstants.State.IDLE).when(mPhone).getState();
+        mDataNetworkControllerUT.addNetworkRequest(
+                createNetworkRequest(NetworkCapabilities.NET_CAPABILITY_INTERNET));
+        processAllMessages();
+
+        // Verify no internet connection due to no active phone call
+        verifyNoConnectedNetworkHasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+
+        // Phone ringing
+        doReturn(PhoneConstants.State.RINGING).when(mPhone).getState();
+        mDataNetworkControllerUT.addNetworkRequest(
+                createNetworkRequest(NetworkCapabilities.NET_CAPABILITY_INTERNET));
+        processAllMessages();
+
+        // Verify internet connection
+        verifyConnectedNetworkHasCapabilities(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+
+        // Disable during data call mobile policy
+        mDataNetworkControllerUT.getDataSettingsManager().setMobileDataPolicy(TelephonyManager
+                .MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL, false);
+        processAllMessages();
+
+        // Verify no internet connection
+        verifyNoConnectedNetworkHasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+    }
+
+    @Test
+    public void testIsDataEnabledOverriddenForApnAutoDataSwitch() throws Exception {
+        // Assume phone2 is the default data phone
+        Phone phone2 = Mockito.mock(Phone.class);
+        replaceInstance(PhoneFactory.class, "sPhones", null, new Phone[]{mPhone, phone2});
+        doReturn(2).when(mSubscriptionController).getDefaultDataSubId();
+
+        // Data disabled on nonDDS
+        mDataNetworkControllerUT.getDataSettingsManager().setDataEnabled(
+                TelephonyManager.DATA_ENABLED_REASON_USER, false, mContext.getOpPackageName());
+
+        // Enable auto data switch mobile policy
+        mDataNetworkControllerUT.getDataSettingsManager().setMobileDataPolicy(TelephonyManager
+                .MOBILE_DATA_POLICY_AUTO_DATA_SWITCH, true);
+        processAllMessages();
+
+        // use disabled data on DDS
+        doReturn(false).when(phone2).isUserDataEnabled();
+        mDataNetworkControllerUT.addNetworkRequest(
+                createNetworkRequest(NetworkCapabilities.NET_CAPABILITY_INTERNET));
+        processAllMessages();
+
+        // Verify no internet connection
+        verifyNoConnectedNetworkHasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+
+        // use enabled data on DDS
+        doReturn(true).when(phone2).isUserDataEnabled();
+        mDataNetworkControllerUT.addNetworkRequest(
+                createNetworkRequest(NetworkCapabilities.NET_CAPABILITY_INTERNET));
+        processAllMessages();
+
+        // Verify internet connection
+        verifyConnectedNetworkHasCapabilities(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+
+        // Disable auto data switch mobile policy
+        mDataNetworkControllerUT.getDataSettingsManager().setMobileDataPolicy(TelephonyManager
+                .MOBILE_DATA_POLICY_AUTO_DATA_SWITCH, false);
+        processAllMessages();
+
+        // Verify no internet connection
+        verifyNoConnectedNetworkHasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+    }
+
+    @Test
     public void testUnmeteredRequestPreferredOnIwlan() throws Exception {
         // Preferred on cellular
         doReturn(AccessNetworkConstants.TRANSPORT_TYPE_WWAN).when(mAccessNetworksManager)
@@ -1817,7 +1940,7 @@
 
         // Change data network type to NR
         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_NR,
-                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE))
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE, false))
                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
         dataNetwork.sendMessage(13/*EVENT_DISPLAY_INFO_CHANGED*/);
         processAllMessages();
@@ -1860,7 +1983,7 @@
 
         // Change data network type to NR
         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_NR,
-                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE))
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE, false))
                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
         dataNetwork.sendMessage(13/*EVENT_DISPLAY_INFO_CHANGED*/);
         processAllMessages();
@@ -1916,7 +2039,7 @@
 
         // Change data network type to NR
         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_NR,
-                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE))
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE, false))
                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
         dataNetwork.sendMessage(13/*EVENT_DISPLAY_INFO_CHANGED*/);
         processAllMessages();
@@ -2940,9 +3063,13 @@
 
     @Test
     public void testNonVoPSNoIMSSetup() throws Exception {
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED))
+                .build();
         serviceStateChanged(TelephonyManager.NETWORK_TYPE_LTE,
                 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, dsri);
 
@@ -2962,9 +3089,13 @@
         carrierConfigChanged();
 
         // VOPS not supported
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED))
+                .build();
         serviceStateChanged(TelephonyManager.NETWORK_TYPE_LTE,
                 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, dsri);
 
@@ -2975,9 +3106,13 @@
         verifyNoConnectedNetworkHasCapability(NetworkCapabilities.NET_CAPABILITY_IMS);
 
         // VoPS supported
-        dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED));
+        dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED))
+                .build();
         serviceStateChanged(TelephonyManager.NETWORK_TYPE_LTE,
                 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, dsri);
 
@@ -3352,7 +3487,7 @@
     @Test
     public void testHandoverDataNetworkSourceOos() throws Exception {
         testSetupImsDataNetwork();
-        // Configured handover is allowed from OOS to 4G/5G/IWLAN.
+        // Configured handover is disallowed from OOS to 4G/5G/IWLAN.
         mCarrierConfig.putStringArray(
                 CarrierConfigManager.KEY_IWLAN_HANDOVER_POLICY_STRING_ARRAY,
                 new String[]{
@@ -3417,9 +3552,13 @@
     public void testHandoverDataNetworkNonVops() throws Exception {
         ServiceState ss = new ServiceState();
 
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED))
+                .build();
 
         ss.addNetworkRegistrationInfo(new NetworkRegistrationInfo.Builder()
                 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
@@ -3480,9 +3619,13 @@
 
         ServiceState ss = new ServiceState();
 
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED))
+                .build();
 
         ss.addNetworkRegistrationInfo(new NetworkRegistrationInfo.Builder()
                 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
@@ -3540,9 +3683,13 @@
     public void testNonMmtelImsHandoverDataNetworkNonVops() throws Exception {
         ServiceState ss = new ServiceState();
 
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED))
+                .build();
 
         ss.addNetworkRegistrationInfo(new NetworkRegistrationInfo.Builder()
                 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
@@ -3608,9 +3755,13 @@
         ServiceState ss = new ServiceState();
 
         // VoPS network
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED))
+                .build();
 
         ss.addNetworkRegistrationInfo(new NetworkRegistrationInfo.Builder()
                 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
@@ -3655,9 +3806,13 @@
 
         ss = new ServiceState();
         // Non VoPS network
-        dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED));
+        dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED))
+                .build();
 
         ss.addNetworkRegistrationInfo(new NetworkRegistrationInfo.Builder()
                 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
@@ -3709,9 +3864,13 @@
         carrierConfigChanged();
 
         // VoPS supported
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED))
+                .build();
         serviceStateChanged(TelephonyManager.NETWORK_TYPE_LTE,
                 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, dsri);
 
@@ -3723,9 +3882,13 @@
 
         doReturn(PhoneConstants.State.OFFHOOK).when(mCT).getState();
 
-        dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED));
+        dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED))
+                .build();
         serviceStateChanged(TelephonyManager.NETWORK_TYPE_LTE,
                 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, dsri);
 
@@ -3751,7 +3914,7 @@
         }).when(mMockedWwanDataServiceManager).deactivateDataCall(
                 anyInt(), anyInt(), any(Message.class));
         // Simulate old devices
-        doReturn(RIL.RADIO_HAL_VERSION_1_6).when(mPhone).getHalVersion();
+        doReturn(RIL.RADIO_HAL_VERSION_1_6).when(mPhone).getHalVersion(HAL_SERVICE_DATA);
 
         testSetupDataNetwork();
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/DataNetworkTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/DataNetworkTest.java
index bf5a42d..135f771 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/data/DataNetworkTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/data/DataNetworkTest.java
@@ -365,8 +365,7 @@
                 .setQosBearerSessions(new ArrayList<>())
                 .setTrafficDescriptors(new ArrayList<>())
                 .build();
-        mDataNetworkUT.sendMessage(7/*EVENT_TEAR_DOWN_NETWORK*/,
-                1/*TEAR_DOWN_REASON_CONNECTIVITY_SERVICE_UNWANTED*/);
+        mDataNetworkUT.tearDown(1/*TEAR_DOWN_REASON_CONNECTIVITY_SERVICE_UNWANTED*/);
         mDataNetworkUT.sendMessage(8/*EVENT_DATA_STATE_CHANGED*/,
                 new AsyncResult(transport, new ArrayList<>(Arrays.asList(response)), null));
         processAllMessages();
@@ -471,9 +470,13 @@
 
     @Test
     public void testCreateDataNetworkWhenOos() throws Exception {
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED))
+                .build();
         // Out of service
         serviceStateChanged(TelephonyManager.NETWORK_TYPE_LTE,
                 NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING, dsri);
@@ -513,9 +516,13 @@
     public void testRecreateAgentWhenOos() throws Exception {
         testCreateDataNetwork();
 
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED))
+                .build();
         // Out of service
         serviceStateChanged(TelephonyManager.NETWORK_TYPE_LTE,
                 NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING, dsri);
@@ -591,7 +598,7 @@
         processAllMessages();
 
         verify(mDataNetworkCallback).onDisconnected(eq(mDataNetworkUT),
-                eq(DataFailCause.RADIO_NOT_AVAILABLE));
+                eq(DataFailCause.RADIO_NOT_AVAILABLE), eq(DataNetwork.TEAR_DOWN_REASON_NONE));
     }
 
     @Test
@@ -849,8 +856,9 @@
                 anyInt());
         verify(mMockedWwanDataServiceManager).deactivateDataCall(eq(123),
                 eq(DataService.REQUEST_REASON_NORMAL), any(Message.class));
-        verify(mDataNetworkCallback).onDisconnected(eq(mDataNetworkUT), eq(
-                DataFailCause.EMM_DETACHED));
+        verify(mDataNetworkCallback).onDisconnected(eq(mDataNetworkUT),
+                eq(DataFailCause.EMM_DETACHED),
+                eq(DataNetwork.TEAR_DOWN_REASON_CONNECTIVITY_SERVICE_UNWANTED));
 
         ArgumentCaptor<PreciseDataConnectionState> pdcsCaptor =
                 ArgumentCaptor.forClass(PreciseDataConnectionState.class);
@@ -963,7 +971,7 @@
         verify(mMockedWlanDataServiceManager).deactivateDataCall(eq(123),
                 eq(DataService.REQUEST_REASON_NORMAL), any(Message.class));
         verify(mDataNetworkCallback).onDisconnected(eq(mDataNetworkUT), eq(
-                DataFailCause.EMM_DETACHED));
+                DataFailCause.EMM_DETACHED), anyInt() /* tear down reason */);
 
         ArgumentCaptor<PreciseDataConnectionState> pdcsCaptor =
                 ArgumentCaptor.forClass(PreciseDataConnectionState.class);
@@ -1131,7 +1139,7 @@
         verify(mVcnManager).addVcnNetworkPolicyChangeListener(any(Executor.class),
                 any(VcnNetworkPolicyChangeListener.class));
         verify(mSST).registerForCssIndicatorChanged(any(Handler.class), anyInt(), eq(null));
-        verify(mSST).registerForServiceStateChanged(any(Handler.class), anyInt());
+        verify(mSST).registerForServiceStateChanged(any(Handler.class), anyInt(), any());
         verify(mCT).registerForVoiceCallStarted(any(Handler.class), anyInt(), eq(null));
         verify(mCT).registerForVoiceCallEnded(any(Handler.class), anyInt(), eq(null));
         verify(mImsCT).registerForVoiceCallStarted(any(Handler.class), anyInt(), eq(null));
@@ -1237,7 +1245,7 @@
         processAllMessages();
 
         verify(mDataNetworkCallback).onDisconnected(eq(mDataNetworkUT), eq(
-                DataFailCause.RADIO_NOT_AVAILABLE));
+                DataFailCause.RADIO_NOT_AVAILABLE), eq(DataNetwork.TEAR_DOWN_REASON_NONE));
         assertThat(mDataNetworkUT.isConnected()).isFalse();
     }
 
@@ -1363,9 +1371,13 @@
 
     @Test
     public void testMovingToNonVops() throws Exception {
-        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED));
+        DataSpecificRegistrationInfo dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_SUPPORTED))
+                .build();
         serviceStateChanged(TelephonyManager.NETWORK_TYPE_LTE,
                 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, dsri);
         testCreateImsDataNetwork();
@@ -1373,9 +1385,13 @@
         assertThat(mDataNetworkUT.getNetworkCapabilities().hasCapability(
                 NetworkCapabilities.NET_CAPABILITY_MMTEL)).isTrue();
 
-        dsri = new DataSpecificRegistrationInfo(8, false, true, true,
-                new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
-                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED));
+        dsri = new DataSpecificRegistrationInfo.Builder(8)
+                .setNrAvailable(true)
+                .setEnDcAvailable(true)
+                .setVopsSupportInfo(new LteVopsSupportInfo(
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED,
+                        LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED))
+                .build();
         logd("Trigger non VoPS");
         serviceStateChanged(TelephonyManager.NETWORK_TYPE_LTE,
                 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, dsri);
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/DataSettingsManagerTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/DataSettingsManagerTest.java
index db543e5..4984879 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/data/DataSettingsManagerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/data/DataSettingsManagerTest.java
@@ -16,10 +16,16 @@
 
 package com.android.internal.telephony.data;
 
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
 import android.os.Looper;
 import android.os.PersistableBundle;
@@ -33,8 +39,11 @@
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
 import org.mockito.Mockito;
 
+import java.util.Set;
+
 @RunWith(AndroidTestingRunner.class)
 @TestableLooper.RunWithLooper
 public class DataSettingsManagerTest extends TelephonyTest {
@@ -54,7 +63,9 @@
         mBundle = mContextFixture.getCarrierConfigBundle();
         doReturn(true).when(mDataConfigManager).isConfigCarrierSpecific();
 
-        doReturn("").when(mSubscriptionController).getDataEnabledOverrideRules(anyInt());
+        doReturn("").when(mSubscriptionController).getEnabledMobileDataPolicies(anyInt());
+        doReturn(true).when(mSubscriptionController).setEnabledMobileDataPolicies(
+                anyInt(), anyString());
 
         mDataSettingsManagerUT = new DataSettingsManager(mPhone, mDataNetworkController,
                 Looper.myLooper(), mMockedDataSettingsManagerCallback);
@@ -68,6 +79,38 @@
     }
 
     @Test
+    public void testMobileDataPolicyParsing() {
+        //Valid new data policy
+        Set<Integer> policies = mDataSettingsManagerUT.getMobileDataPolicyEnabled("1, 2");
+        assertThat(policies.size()).isEqualTo(2);
+        Set<Integer> policies2 = mDataSettingsManagerUT.getMobileDataPolicyEnabled(",2");
+        assertThat(policies2.size()).isEqualTo(1);
+        Set<Integer> policies3 = mDataSettingsManagerUT.getMobileDataPolicyEnabled("");
+        assertThat(policies3.size()).isEqualTo(0);
+
+        // Invalid
+        Set<Integer> invalid = mDataSettingsManagerUT.getMobileDataPolicyEnabled(
+                "nonExistent, 1, 2");
+        assertThat(invalid.size()).isEqualTo(2);
+
+        Set<Integer> invalid2 = mDataSettingsManagerUT.getMobileDataPolicyEnabled(
+                "nonExistent ,,");
+        assertThat(invalid2.size()).isEqualTo(0);
+    }
+
+    @Test
+    public void testGetPolicies() {
+        mDataSettingsManagerUT.setMobileDataPolicy(1, true);
+        mDataSettingsManagerUT.setMobileDataPolicy(2, true);
+        processAllMessages();
+
+        ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
+        verify(mSubscriptionController, times(2))
+                .setEnabledMobileDataPolicies(anyInt(), stringArgumentCaptor.capture());
+        assertEquals("1,2", stringArgumentCaptor.getValue());
+    }
+
+    @Test
     public void testDefaultDataRoamingEnabled() {
         doReturn(true).when(mDataConfigManager).isDataRoamingEnabledByDefault();
         mDataSettingsManagerUT.setDefaultDataRoamingEnabled();
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/DataStallRecoveryManagerTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/DataStallRecoveryManagerTest.java
index de0998d..a8290af 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/data/DataStallRecoveryManagerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/data/DataStallRecoveryManagerTest.java
@@ -78,7 +78,7 @@
                 })
                 .when(mDataStallRecoveryManagerCallback)
                 .invokeFromExecutor(any(Runnable.class));
-        doReturn("").when(mSubscriptionController).getDataEnabledOverrideRules(anyInt());
+        doReturn("").when(mSubscriptionController).getEnabledMobileDataPolicies(anyInt());
 
         mDataStallRecoveryManager =
                 new DataStallRecoveryManager(
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/PhoneSwitcherTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/PhoneSwitcherTest.java
index 6465705..5ff51b6 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/data/PhoneSwitcherTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/data/PhoneSwitcherTest.java
@@ -28,10 +28,6 @@
 import static android.telephony.ims.stub.ImsRegistrationImplBase.REGISTRATION_TECH_LTE;
 
 import static com.android.internal.telephony.data.PhoneSwitcher.ECBM_DEFAULT_DATA_SWITCH_BASE_TIME_MS;
-import static com.android.internal.telephony.data.PhoneSwitcher.EVENT_DATA_ENABLED_CHANGED;
-import static com.android.internal.telephony.data.PhoneSwitcher.EVENT_IMS_RADIO_TECH_CHANGED;
-import static com.android.internal.telephony.data.PhoneSwitcher.EVENT_MULTI_SIM_CONFIG_CHANGED;
-import static com.android.internal.telephony.data.PhoneSwitcher.EVENT_PRECISE_CALL_STATE_CHANGED;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -41,6 +37,7 @@
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.clearInvocations;
 import static org.mockito.Mockito.doAnswer;
@@ -62,10 +59,12 @@
 import android.os.Looper;
 import android.os.Message;
 import android.os.Messenger;
+import android.telephony.AccessNetworkConstants;
+import android.telephony.NetworkRegistrationInfo;
 import android.telephony.PhoneCapability;
+import android.telephony.ServiceState;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
-import android.telephony.data.ApnSetting;
 import android.test.suitebuilder.annotation.SmallTest;
 import android.testing.AndroidTestingRunner;
 import android.testing.TestableLooper;
@@ -75,8 +74,10 @@
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.GsmCdmaCall;
 import com.android.internal.telephony.ISetOpportunisticDataCallback;
+import com.android.internal.telephony.ISub;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneFactory;
+import com.android.internal.telephony.ServiceStateTracker;
 import com.android.internal.telephony.TelephonyIntents;
 import com.android.internal.telephony.TelephonyTest;
 
@@ -86,6 +87,8 @@
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
 
+import java.lang.reflect.Field;
+import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.LinkedBlockingQueue;
 
@@ -95,14 +98,19 @@
     private static final int ACTIVE_PHONE_SWITCH = 1;
     private static final int EVENT_RADIO_ON = 108;
     private static final int EVENT_MODEM_COMMAND_DONE = 112;
+    private static final int EVENT_EVALUATE_AUTO_SWITCH = 111;
+    private static final int EVENT_IMS_RADIO_TECH_CHANGED = 120;
+    private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 117;
+    private static final int EVENT_PRECISE_CALL_STATE_CHANGED = 109;
+    private static final int EVENT_SERVICE_STATE_CHANGED = 114;
 
     // Mocked classes
     CompletableFuture<Boolean> mFuturePhone;
     private CommandsInterface mCommandsInterface0;
     private CommandsInterface mCommandsInterface1;
     private Phone mPhone2; // mPhone as phone 1 is already defined in TelephonyTest.
+    private ServiceStateTracker mSST2;
     private Phone mImsPhone;
-    // TODO: Add logic for DataSettingsManager
     private DataSettingsManager mDataSettingsManager2;
     private Handler mActivePhoneSwitchHandler;
     private GsmCdmaCall mActiveCall;
@@ -114,12 +122,15 @@
     private ISetOpportunisticDataCallback mSetOpptDataCallback2;
     PhoneSwitcher.ImsRegTechProvider mMockImsRegTechProvider;
     private SubscriptionInfo mSubscriptionInfo;
+    private ISub mMockedIsub;
 
-    private PhoneSwitcher mPhoneSwitcher;
+    private PhoneSwitcher mPhoneSwitcherUT;
     private SubscriptionManager.OnSubscriptionsChangedListener mSubChangedListener;
     private ConnectivityManager mConnectivityManager;
     // The messenger of PhoneSwitcher used to receive network requests.
     private Messenger mNetworkProviderMessenger = null;
+    private Map<Integer, DataSettingsManager.DataSettingsManagerCallback>
+            mDataSettingsManagerCallbacks;
     private int mDefaultDataSub = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
     private int[][] mSlotIndexToSubId;
     private boolean[] mDataAllowed;
@@ -134,6 +145,7 @@
         mCommandsInterface0 = mock(CommandsInterface.class);
         mCommandsInterface1 = mock(CommandsInterface.class);
         mPhone2 = mock(Phone.class); // mPhone as phone 1 is already defined in TelephonyTest.
+        mSST2 = mock(ServiceStateTracker.class);
         mImsPhone = mock(Phone.class);
         mDataSettingsManager2 = mock(DataSettingsManager.class);
         mActivePhoneSwitchHandler = mock(Handler.class);
@@ -146,6 +158,7 @@
         mSetOpptDataCallback2 = mock(ISetOpportunisticDataCallback.class);
         mMockImsRegTechProvider = mock(PhoneSwitcher.ImsRegTechProvider.class);
         mSubscriptionInfo = mock(SubscriptionInfo.class);
+        mMockedIsub = mock(ISub.class);
 
         PhoneCapability phoneCapability = new PhoneCapability(1, 1, null, false, new int[0]);
         doReturn(phoneCapability).when(mPhoneConfigurationManager).getCurrentPhoneCapability();
@@ -162,11 +175,16 @@
 
         replaceInstance(Phone.class, "mCi", mPhone, mCommandsInterface0);
         replaceInstance(Phone.class, "mCi", mPhone2, mCommandsInterface1);
+
+        doReturn(1).when(mMockedIsub).getDefaultDataSubId();
+        doReturn(mMockedIsub).when(mIBinder).queryLocalInterface(anyString());
+        doReturn(mPhone).when(mPhone).getImsPhone();
+        mServiceManagerMockedServices.put("isub", mIBinder);
     }
 
     @After
     public void tearDown() throws Exception {
-        mPhoneSwitcher = null;
+        mPhoneSwitcherUT = null;
         mSubChangedListener = null;
         mConnectivityManager = null;
         mNetworkProviderMessenger = null;
@@ -182,19 +200,19 @@
         initialize();
 
         // verify nothing has been done while there are no inputs
-        assertFalse("data allowed initially", mDataAllowed[0]);
+        assertTrue("data should be always allowed for emergency", mDataAllowed[0]);
         assertFalse("data allowed initially", mDataAllowed[1]);
 
         NetworkRequest internetNetworkRequest = addInternetNetworkRequest(null, 50);
 
-        assertFalse("phone active after request", mPhoneSwitcher
+        assertFalse("phone active after request", mPhoneSwitcherUT
                 .shouldApplyNetworkRequest(
                         new TelephonyNetworkRequest(internetNetworkRequest, mPhone), 0));
 
         // not registered yet - shouldn't inc
         verify(mActivePhoneSwitchHandler, never()).sendMessageAtTime(any(), anyLong());
 
-        mPhoneSwitcher.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
+        mPhoneSwitcherUT.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
                 ACTIVE_PHONE_SWITCH, null);
 
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
@@ -209,7 +227,7 @@
         processAllMessages();
 
         AsyncResult res = new AsyncResult(1, null,  null);
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mActivePhoneSwitchHandler);
@@ -230,7 +248,7 @@
         // 1 lose default via default sub change
         setDefaultDataSubId(1);
 
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mActivePhoneSwitchHandler);
@@ -240,7 +258,7 @@
         mSubChangedListener.onSubscriptionsChanged();
         processAllMessages();
 
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mActivePhoneSwitchHandler);
@@ -250,7 +268,7 @@
         // 2 gain default via default sub change
         setDefaultDataSubId(0);
 
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mActivePhoneSwitchHandler);
@@ -261,7 +279,7 @@
         setSlotIndexToSubId(0, 2);
         mSubChangedListener.onSubscriptionsChanged();
         processAllMessages();
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
 
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
@@ -274,7 +292,7 @@
         mSubChangedListener.onSubscriptionsChanged();
         processAllMessages();
 
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mActivePhoneSwitchHandler);
@@ -284,17 +302,17 @@
         // 5 lose default network request
         releaseNetworkRequest(internetNetworkRequest);
 
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mActivePhoneSwitchHandler);
-        assertFalse("data allowed", mDataAllowed[0]);
+        assertTrue("data not allowed", mDataAllowed[0]);
         assertFalse("data allowed", mDataAllowed[1]);
 
         // 6 gain subscription-specific request
         NetworkRequest specificInternetRequest = addInternetNetworkRequest(0, 50);
 
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mActivePhoneSwitchHandler);
@@ -306,7 +324,7 @@
         mSubChangedListener.onSubscriptionsChanged();
         processAllMessages();
 
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mActivePhoneSwitchHandler);
@@ -318,7 +336,7 @@
         mSubChangedListener.onSubscriptionsChanged();
         processAllMessages();
 
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mActivePhoneSwitchHandler);
@@ -328,11 +346,11 @@
         // 9 lose subscription-specific request
         releaseNetworkRequest(specificInternetRequest);
 
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(1)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mActivePhoneSwitchHandler);
-        assertFalse("data allowed", mDataAllowed[0]);
+        assertTrue("data not allowed", mDataAllowed[0]);
         assertFalse("data allowed", mDataAllowed[1]);
 
         // 10 don't switch phones when in emergency mode
@@ -356,6 +374,254 @@
 //        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
     }
 
+    /** Test Data Auto Switch **/
+
+    /**
+     * Trigger conditions
+     * 1. service state changes
+     * 2. data setting changes
+     *      - user toggle data
+     *      - user toggle auto switch feature
+     * 3. default network changes
+     *      - current network lost
+     *      - network become active on non-cellular network
+     * 4. subscription changes
+     *      - slot/sub mapping changes
+     */
+    @Test
+    @SmallTest
+    public void testAutoDataSwitchCancelScenario_onPrimary() throws Exception {
+        initialize();
+        // Phone 0 has sub 1, phone 1 has sub 2.
+        // Sub 1 is default data sub.
+        setSlotIndexToSubId(0, 1);
+        setSlotIndexToSubId(1, 2);
+        setDefaultDataSubId(1);
+
+        // 0. When all conditions met
+        prepareIdealAutoSwitchCondition();
+        processAllFutureMessages();
+
+        // Verify attempting to switch
+        verify(mCellularNetworkValidator).validate(eq(2), anyLong(), eq(false),
+                eq(mPhoneSwitcherUT.mValidationCallback));
+        doReturn(true).when(mCellularNetworkValidator).isValidating();
+
+        // 1. Service state becomes not ideal - primary is available again
+        clearInvocations(mCellularNetworkValidator);
+        serviceStateChanged(0, NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
+        processAllFutureMessages();
+
+        verify(mCellularNetworkValidator).stopValidation();
+
+        // 2.1 User data disabled on primary SIM
+        prepareIdealAutoSwitchCondition();
+        processAllFutureMessages();
+        clearInvocations(mCellularNetworkValidator);
+        doReturn(false).when(mPhone).isUserDataEnabled();
+        mDataSettingsManagerCallbacks.get(0).onDataEnabledChanged(false, 123 , "");
+        processAllFutureMessages();
+
+        verify(mCellularNetworkValidator).stopValidation();
+
+        // 2.2 Auto switch feature is disabled
+        prepareIdealAutoSwitchCondition();
+        processAllFutureMessages();
+        clearInvocations(mCellularNetworkValidator);
+        doReturn(false).when(mPhone2).isDataAllowed();
+        mDataSettingsManagerCallbacks.get(1).onDataEnabledChanged(false, 123 , "");
+        processAllFutureMessages();
+
+        verify(mCellularNetworkValidator).stopValidation();
+
+        // 3.1 No default network
+        prepareIdealAutoSwitchCondition();
+        processAllFutureMessages();
+        clearInvocations(mCellularNetworkValidator);
+        doReturn(new NetworkCapabilities()
+                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI))
+                .when(mConnectivityManager).getNetworkCapabilities(any());
+        mPhoneSwitcherUT.sendEmptyMessage(EVENT_EVALUATE_AUTO_SWITCH);
+        processAllFutureMessages();
+
+        verify(mCellularNetworkValidator).stopValidation();
+    }
+
+    public void testAutoSwitchToSecondarySucceed() {
+        prepareIdealAutoSwitchCondition();
+        processAllFutureMessages();
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(true, 2);
+        processAllMessages();
+        // Confirm auto switched to secondary sub Id 1/phone 0
+        assertEquals(2, mPhoneSwitcherUT.getActiveDataSubId());
+    }
+
+    @Test
+    @SmallTest
+    public void testAutoDataSwitch_switchBackToPrimary() throws Exception {
+        initialize();
+        // Phone 0 has sub 1, phone 1 has sub 2.
+        // Sub 1 is default data sub.
+        setSlotIndexToSubId(0, 1);
+        setSlotIndexToSubId(1, 2);
+        setDefaultDataSubId(1);
+
+        testAutoSwitchToSecondarySucceed();
+        // 1.1 service state changes - primary becomes available, need validation pass to switch
+        serviceStateChanged(0, NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING);
+        processAllFutureMessages();
+        verify(mCellularNetworkValidator).validate(eq(1), anyLong(), eq(false),
+                eq(mPhoneSwitcherUT.mValidationCallback));
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(false, 1);
+        processAllMessages();
+
+        assertEquals(2, mPhoneSwitcherUT.getActiveDataSubId()); // since validation failed
+
+        serviceStateChanged(0, NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
+        processAllFutureMessages();
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(true, 1);
+        processAllMessages();
+
+        assertEquals(1, mPhoneSwitcherUT.getActiveDataSubId()); // since validation passed
+
+        testAutoSwitchToSecondarySucceed();
+        // 1.2 service state changes - secondary becomes unavailable, NO need validation
+        // The later validation requirement overrides the previous
+        serviceStateChanged(0, NetworkRegistrationInfo.REGISTRATION_STATE_HOME/*need validate*/);
+        serviceStateChanged(1, NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING/*no need*/);
+        processAllFutureMessages();
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(false, 1);
+        processAllMessages();
+
+        assertEquals(1, mPhoneSwitcherUT.getActiveDataSubId()); // since no need validation
+
+        testAutoSwitchToSecondarySucceed();
+        // 2.1 User data disabled on primary SIM
+        clearInvocations(mCellularNetworkValidator);
+        doReturn(false).when(mPhone).isUserDataEnabled();
+        mDataSettingsManagerCallbacks.get(0).onDataEnabledChanged(false, 123 , "");
+        processAllFutureMessages();
+
+        assertEquals(1, mPhoneSwitcherUT.getActiveDataSubId()); // since no need validation
+        verify(mCellularNetworkValidator, never()).validate(eq(1), anyLong(), eq(false),
+                eq(mPhoneSwitcherUT.mValidationCallback));
+
+        testAutoSwitchToSecondarySucceed();
+        // 2.2 Auto switch feature is disabled
+        clearInvocations(mCellularNetworkValidator);
+        doReturn(false).when(mPhone2).isDataAllowed();
+        mDataSettingsManagerCallbacks.get(0).onDataEnabledChanged(false, 123 , "");
+        processAllFutureMessages();
+
+        assertEquals(1, mPhoneSwitcherUT.getActiveDataSubId()); // since no need validation
+        verify(mCellularNetworkValidator, never()).validate(eq(1), anyLong(), eq(false),
+                eq(mPhoneSwitcherUT.mValidationCallback));
+
+        testAutoSwitchToSecondarySucceed();
+        // 3.1 Default network is active on non-cellular transport
+        clearInvocations(mCellularNetworkValidator);
+        doReturn(new NetworkCapabilities()
+                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI))
+                .when(mConnectivityManager).getNetworkCapabilities(any());
+        mPhoneSwitcherUT.sendEmptyMessage(EVENT_EVALUATE_AUTO_SWITCH);
+        processAllFutureMessages();
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(false, 1);
+        processAllMessages();
+
+        assertEquals(1, mPhoneSwitcherUT.getActiveDataSubId()); // since no need validation
+    }
+
+    @Test
+    @SmallTest
+    public void testAutoDataSwitchCancel_onSecondary() throws Exception {
+        initialize();
+        // Phone 0 has sub 1, phone 1 has sub 2.
+        // Sub 1 is default data sub.
+        setSlotIndexToSubId(0, 1);
+        setSlotIndexToSubId(1, 2);
+        setDefaultDataSubId(1);
+
+        testAutoSwitchToSecondarySucceed();
+        clearInvocations(mCellularNetworkValidator);
+        // attempts the switch back due to secondary becomes ROAMING
+        serviceStateChanged(1, NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING);
+        processAllFutureMessages();
+
+        verify(mCellularNetworkValidator).validate(eq(1), anyLong(), eq(false),
+                eq(mPhoneSwitcherUT.mValidationCallback));
+        doReturn(true).when(mCellularNetworkValidator).isValidating();
+
+        // cancel the switch back attempt due to secondary back to HOME
+        serviceStateChanged(1, NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
+        processAllMessages();
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(true, 1);
+        processAllMessages();
+
+        assertEquals(2, mPhoneSwitcherUT.getActiveDataSubId());
+        verify(mCellularNetworkValidator).stopValidation();
+    }
+
+    @Test
+    @SmallTest
+    public void testAutoDataSwitchRetry() throws Exception {
+        initialize();
+        // Phone 0 has sub 1, phone 1 has sub 2.
+        // Sub 1 is default data sub.
+        setSlotIndexToSubId(0, 1);
+        setSlotIndexToSubId(1, 2);
+        setDefaultDataSubId(1);
+
+        prepareIdealAutoSwitchCondition();
+        processAllFutureMessages();
+
+        // Verify attempting to switch
+        verify(mCellularNetworkValidator).validate(eq(2), anyLong(), eq(false),
+                eq(mPhoneSwitcherUT.mValidationCallback));
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(false, 2);
+        processAllMessages();
+
+        assertTrue(mPhoneSwitcherUT.hasMessages(EVENT_EVALUATE_AUTO_SWITCH));
+
+        processAllFutureMessages();
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(true, 2);
+        processAllFutureMessages();
+
+        assertEquals(2, mPhoneSwitcherUT.getActiveDataSubId());
+    }
+
+    @Test
+    @SmallTest
+    public void testAutoDataSwitchSetNotification() throws Exception {
+        SubscriptionInfo mockedInfo = mock(SubscriptionInfo.class);
+        doReturn(false).when(mockedInfo).isOpportunistic();
+        doReturn(mockedInfo).when(mSubscriptionController).getSubscriptionInfo(anyInt());
+        initialize();
+        // Phone 0 has sub 1, phone 1 has sub 2.
+        // Sub 1 is default data sub.
+        setSlotIndexToSubId(0, 1);
+        setSlotIndexToSubId(1, 2);
+        setDefaultDataSubId(1);
+
+        testAutoSwitchToSecondarySucceed();
+        clearInvocations(mSubscriptionController);
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, new AsyncResult(1, null,  null))
+                .sendToTarget();
+        processAllMessages();
+        verify(mSubscriptionController).getSubscriptionInfo(2);
+
+        // switch back to primary
+        clearInvocations(mSubscriptionController);
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, new AsyncResult(0, null,  null))
+                .sendToTarget();
+        processAllMessages();
+        verify(mSubscriptionController, never()).getSubscriptionInfo(1);
+
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, new AsyncResult(1, null,  null))
+                .sendToTarget();
+        processAllMessages();
+        verify(mSubscriptionController, never()).getSubscriptionInfo(2);
+    }
+
     /**
      * Test a multi-sim case with limited active phones:
      * - lose default via default sub change
@@ -381,7 +647,7 @@
         setSlotIndexToSubId(0, 0);
         setSlotIndexToSubId(1, 1);
         setDefaultDataSubId(0);
-        mPhoneSwitcher.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
+        mPhoneSwitcherUT.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
                 ACTIVE_PHONE_SWITCH, null);
         processAllMessages();
         // verify initial conditions
@@ -394,7 +660,7 @@
         addMmsNetworkRequest(1);
 
         AsyncResult res = new AsyncResult(1, null,  null);
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
 
         // After gain of network request, mActivePhoneSwitchHandler should be notified 2 times.
@@ -460,18 +726,18 @@
         assertTrue(mDataAllowed[0]);
 
         // Set sub 2 as preferred sub should make phone 1 activated and phone 0 deactivated.
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, false, null);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, false, null);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onNetworkAvailable(null, 2);
+        mPhoneSwitcherUT.mValidationCallback.onNetworkAvailable(null, 2);
         processAllMessages();
         assertFalse(mDataAllowed[0]);
         assertTrue(mDataAllowed[1]);
 
         // Unset preferred sub should make default data sub (phone 0 / sub 1) activated again.
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(
                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, false, null);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onNetworkAvailable(null, 1);
+        mPhoneSwitcherUT.mValidationCallback.onNetworkAvailable(null, 1);
         processAllMessages();
         assertTrue(mDataAllowed[0]);
         assertFalse(mDataAllowed[1]);
@@ -482,11 +748,65 @@
      * The following events can set preferred data subId with priority in the order of
      * 1. Emergency call
      * 2. Voice call (when data during call feature is enabled).
-     * 3. CBRS requests
+     * 3. CBRS requests OR Auto switch requests - only one case applies at a time
      */
     @Test
     @SmallTest
-    public void testSetPreferredDataCasePriority() throws Exception {
+    public void testSetPreferredDataCasePriority_CbrsWaitsForVoiceCall() throws Exception {
+        initialize();
+        setAllPhonesInactive();
+
+        // Phone 0 has sub 1, phone 1 has sub 2.
+        // Sub 1 is default data sub.
+        // Both are active subscriptions are active sub, as they are in both active slots.
+        setSlotIndexToSubId(0, 1);
+        setSlotIndexToSubId(1, 2);
+        // single visible sub, as the other one is CBRS
+        doReturn(new int[1]).when(mSubscriptionController).getActiveSubIdList(true);
+        setDefaultDataSubId(1);
+
+        // Notify phoneSwitcher about default data sub and default network request.
+        NetworkRequest internetRequest = addInternetNetworkRequest(null, 50);
+        // Phone 0 (sub 1) should be activated as it has default data sub.
+        assertEquals(1, mPhoneSwitcherUT.getActiveDataSubId());
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
+
+        // Set sub 2 as preferred sub should make phone 1 activated and phone 0 deactivated.
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, false, null);
+        processAllMessages();
+        mPhoneSwitcherUT.mValidationCallback.onNetworkAvailable(null, 2);
+        // A higher priority event occurring E.g. Phone1 has active IMS call on LTE.
+        doReturn(mImsPhone).when(mPhone).getImsPhone();
+        doReturn(true).when(mPhone).isUserDataEnabled();
+        doReturn(true).when(mPhone).isDataAllowed();
+        mockImsRegTech(0, REGISTRATION_TECH_LTE);
+        notifyPhoneAsInCall(mPhone);
+
+        // switch shouldn't occur due to the higher priority event
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
+        assertEquals(1, mPhoneSwitcherUT.getActiveDataSubId());
+        assertEquals(2, mPhoneSwitcherUT.getAutoSelectedDataSubId());
+
+        // The higher priority event ends, time to switch to auto selected subId.
+        notifyPhoneAsInactive(mPhone);
+
+        assertEquals(2, mPhoneSwitcherUT.getActiveDataSubId());
+        assertEquals(2, mPhoneSwitcherUT.getAutoSelectedDataSubId());
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
+    }
+
+    @Test
+    @SmallTest
+    public void testSetPreferredData_NoAutoSwitchWhenCbrs() throws Exception {
         initialize();
         setAllPhonesInactive();
 
@@ -497,43 +817,14 @@
         setSlotIndexToSubId(1, 2);
         setDefaultDataSubId(1);
 
-        // Notify phoneSwitcher about default data sub and default network request.
-        NetworkRequest internetRequest = addInternetNetworkRequest(null, 50);
-        // Phone 0 (sub 1) should be activated as it has default data sub.
-        assertEquals(1, mPhoneSwitcher.getActiveDataSubId());
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
-                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
-                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
+        clearInvocations(mCellularNetworkValidator);
+        doReturn(new int[1]).when(mSubscriptionController).getActiveSubIdList(true);
+        prepareIdealAutoSwitchCondition();
+        processAllFutureMessages();
 
-        // Set sub 2 as preferred sub should make phone 1 activated and phone 0 deactivated.
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, false, null);
-        processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onNetworkAvailable(null, 2);
-        // A higher priority event occurring E.g. Phone1 has active IMS call on LTE.
-        doReturn(mImsPhone).when(mPhone).getImsPhone();
-        doReturn(true).when(mDataSettingsManager).isDataEnabled(ApnSetting.TYPE_DEFAULT);
-        mockImsRegTech(1, REGISTRATION_TECH_LTE);
-        notifyPhoneAsInCall(mPhone);
-
-        // switch shouldn't occur due to the higher priority event
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
-                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
-                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
-        assertEquals(1, mPhoneSwitcher.getActiveDataSubId());
-        assertEquals(2, mPhoneSwitcher.getAutoSelectedDataSubId());
-
-        // The higher priority event ends, time to switch to auto selected subId.
-        notifyPhoneAsInactive(mPhone);
-
-        assertEquals(2, mPhoneSwitcher.getActiveDataSubId());
-        assertEquals(2, mPhoneSwitcher.getAutoSelectedDataSubId());
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
-                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
-                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-
+        verify(mCellularNetworkValidator, never()).validate(eq(2), anyLong(), eq(false),
+                eq(mPhoneSwitcherUT.mValidationCallback));
+        assertEquals(1, mPhoneSwitcherUT.getActiveDataSubId());
     }
 
     @Test
@@ -541,9 +832,9 @@
     public void testSetPreferredDataModemCommand() throws Exception {
         doReturn(true).when(mMockRadioConfig).isSetPreferredDataCommandSupported();
         initialize();
-        mPhoneSwitcher.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
+        mPhoneSwitcherUT.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
                 ACTIVE_PHONE_SWITCH, null);
-        mPhoneSwitcher.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
+        mPhoneSwitcherUT.registerForActivePhoneSwitch(mActivePhoneSwitchHandler,
                 ACTIVE_PHONE_SWITCH, null);
         verify(mActivePhoneSwitchHandler, times(2)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mMockRadioConfig);
@@ -558,7 +849,7 @@
         // Phone 0 (sub 1) should be preferred data phone as it has default data sub.
         verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
         AsyncResult res = new AsyncResult(1, null,  null);
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(2)).sendMessageAtTime(any(), anyLong());
         clearInvocations(mMockRadioConfig);
@@ -570,55 +861,55 @@
         NetworkRequest mmsRequest = addMmsNetworkRequest(2);
         verify(mMockRadioConfig, never()).setPreferredDataModem(anyInt(), any());
         verify(mActivePhoneSwitchHandler, never()).sendMessageAtTime(any(), anyLong());
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(mmsRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(mmsRequest, mPhone), 1));
 
         // Set sub 2 as preferred sub should make phone 1 preferredDataModem
         doReturn(true).when(mSubscriptionController).isOpportunistic(2);
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, false, null);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, false, null);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onNetworkAvailable(null, 2);
+        mPhoneSwitcherUT.mValidationCallback.onNetworkAvailable(null, 2);
         processAllMessages();
         verify(mMockRadioConfig).setPreferredDataModem(eq(1), any());
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(2)).sendMessageAtTime(any(), anyLong());
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(mmsRequest, mPhone), 0));
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(mmsRequest, mPhone), 1));
 
         clearInvocations(mMockRadioConfig);
         clearInvocations(mActivePhoneSwitchHandler);
 
         // Unset preferred sub should make phone0 preferredDataModem again.
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(
                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, false, null);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onNetworkAvailable(null, 1);
+        mPhoneSwitcherUT.mValidationCallback.onNetworkAvailable(null, 1);
         processAllMessages();
 
         verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         verify(mActivePhoneSwitchHandler, times(2)).sendMessageAtTime(any(), anyLong());
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(mmsRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(mmsRequest, mPhone), 1));
 
         // SetDataAllowed should never be triggered.
@@ -627,7 +918,7 @@
 
         // Set preferred data modem should be triggered after radio on or available.
         clearInvocations(mMockRadioConfig);
-        Message.obtain(mPhoneSwitcher, EVENT_RADIO_ON, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_RADIO_ON, res).sendToTarget();
         processAllMessages();
         verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
     }
@@ -648,42 +939,42 @@
         setDefaultDataSubId(1);
 
         // Phone 0 (sub 1) should be activated as it has default data sub.
-        assertEquals(0, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(0, mPhoneSwitcherUT.getPreferredDataPhoneId());
 
         // Set sub 2 as preferred sub should make phone 1 activated and phone 0 deactivated.
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, true, null);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, true, null);
         processAllMessages();
         verify(mCellularNetworkValidator).validate(eq(2), anyLong(), eq(false),
-                eq(mPhoneSwitcher.mValidationCallback));
+                eq(mPhoneSwitcherUT.mValidationCallback));
         // Validation failed. Preferred data sub should remain 1, data phone should remain 0.
-        mPhoneSwitcher.mValidationCallback.onValidationDone(false, 2);
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(false, 2);
         processAllMessages();
-        assertEquals(0, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(0, mPhoneSwitcherUT.getPreferredDataPhoneId());
 
         // Validation succeeds. Preferred data sub changes to 2, data phone changes to 1.
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, true, null);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, true, null);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onValidationDone(true, 2);
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(true, 2);
         processAllMessages();
-        assertEquals(1, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(1, mPhoneSwitcherUT.getPreferredDataPhoneId());
 
         // Switching data back to primary (subId 1) with customized validation timeout.
         long timeout = 1234;
         mContextFixture.getCarrierConfigBundle().putLong(
                 KEY_DATA_SWITCH_VALIDATION_TIMEOUT_LONG, timeout);
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(
                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, true, null);
         processAllMessages();
         verify(mCellularNetworkValidator).validate(eq(1), eq(timeout), eq(false),
-                eq(mPhoneSwitcher.mValidationCallback));
-        mPhoneSwitcher.mValidationCallback.onValidationDone(true, 1);
+                eq(mPhoneSwitcherUT.mValidationCallback));
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(true, 1);
         processAllMessages();
-        assertEquals(0, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(0, mPhoneSwitcherUT.getPreferredDataPhoneId());
     }
 
     private void mockImsRegTech(int phoneId, int regTech) {
         doReturn(regTech).when(mMockImsRegTechProvider).get(any(), eq(phoneId));
-        mPhoneSwitcher.mImsRegTechProvider = mMockImsRegTechProvider;
+        mPhoneSwitcherUT.mImsRegTechProvider = mMockImsRegTechProvider;
     }
 
     @Test
@@ -701,17 +992,17 @@
         processAllMessages();
 
         // Phone 0 should be the default data phoneId.
-        assertEquals(0, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(0, mPhoneSwitcherUT.getPreferredDataPhoneId());
 
         // Phone2 has active IMS call on LTE. And data of DEFAULT apn is enabled. This should
         // trigger data switch.
         doReturn(mImsPhone).when(mPhone2).getImsPhone();
-        doReturn(true).when(mDataSettingsManager2).isDataEnabled(ApnSetting.TYPE_DEFAULT);
+        doReturn(true).when(mPhone2).isDataAllowed();
         mockImsRegTech(1, REGISTRATION_TECH_LTE);
         notifyPhoneAsInCall(mImsPhone);
 
-        // Phone 1 should become the default data phone.
-        assertEquals(1, mPhoneSwitcher.getPreferredDataPhoneId());
+        // Phone 1 should become the preferred data phone.
+        assertEquals(1, mPhoneSwitcherUT.getPreferredDataPhoneId());
     }
 
     @Test
@@ -730,17 +1021,17 @@
         processAllMessages();
 
         // Phone 0 should be the default data phoneId.
-        assertEquals(0, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(0, mPhoneSwitcherUT.getPreferredDataPhoneId());
 
         // Phone2 has active IMS call on LTE. And data of DEFAULT apn is enabled. This should
         // trigger data switch.
         doReturn(mImsPhone).when(mPhone2).getImsPhone();
-        doReturn(true).when(mDataSettingsManager2).isDataEnabled(ApnSetting.TYPE_DEFAULT);
+        doReturn(true).when(mPhone2).isDataAllowed();
         mockImsRegTech(1, REGISTRATION_TECH_LTE);
         notifyPhoneAsInDial(mImsPhone);
 
-        // Phone 1 should become the default data phone.
-        assertEquals(1, mPhoneSwitcher.getPreferredDataPhoneId());
+        // Phone2 should be preferred data phone
+        assertEquals(1, mPhoneSwitcherUT.getPreferredDataPhoneId());
     }
     @Test
     @SmallTest
@@ -758,17 +1049,17 @@
         processAllMessages();
 
         // Phone 0 should be the default data phoneId.
-        assertEquals(0, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(0, mPhoneSwitcherUT.getPreferredDataPhoneId());
 
         // Phone2 has active IMS call on LTE. And data of DEFAULT apn is enabled. This should
         // trigger data switch.
         doReturn(mImsPhone).when(mPhone2).getImsPhone();
-        doReturn(true).when(mDataSettingsManager2).isDataEnabled(ApnSetting.TYPE_DEFAULT);
+        doReturn(true).when(mPhone2).isDataAllowed();
         mockImsRegTech(1, REGISTRATION_TECH_LTE);
         notifyPhoneAsInIncomingCall(mImsPhone);
 
-        // Phone 1 should become the default data phone.
-        assertEquals(1, mPhoneSwitcher.getPreferredDataPhoneId());
+        // Phone 1 should become the preferred data phone.
+        assertEquals(1, mPhoneSwitcherUT.getPreferredDataPhoneId());
     }
 
     @Test
@@ -786,16 +1077,16 @@
         processAllMessages();
 
         // Phone 0 should be the default data phoneId.
-        assertEquals(0, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(0, mPhoneSwitcherUT.getPreferredDataPhoneId());
 
         // Phone2 has active call, but data is turned off. So no data switching should happen.
         doReturn(mImsPhone).when(mPhone2).getImsPhone();
-        doReturn(true).when(mDataSettingsManager2).isDataEnabled(ApnSetting.TYPE_DEFAULT);
+        doReturn(true).when(mPhone2).isDataAllowed();
         mockImsRegTech(1, REGISTRATION_TECH_IWLAN);
         notifyPhoneAsInCall(mImsPhone);
 
         // Phone 0 should remain the default data phone.
-        assertEquals(0, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(0, mPhoneSwitcherUT.getPreferredDataPhoneId());
     }
 
     @Test
@@ -813,17 +1104,18 @@
         processAllMessages();
 
         // Phone 0 should be the default data phoneId.
-        assertEquals(0, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(0, mPhoneSwitcherUT.getPreferredDataPhoneId());
 
         // Phone 1 has active IMS call on CROSS_SIM. And data of DEFAULT apn is enabled. This should
         // not trigger data switch.
         doReturn(mImsPhone).when(mPhone2).getImsPhone();
-        doReturn(true).when(mDataSettingsManager2).isDataEnabled(ApnSetting.TYPE_DEFAULT);
+        doReturn(true).when(mPhone).isUserDataEnabled();
+        doReturn(true).when(mPhone2).isDataAllowed();
         mockImsRegTech(1, REGISTRATION_TECH_CROSS_SIM);
         notifyPhoneAsInCall(mImsPhone);
 
         // Phone 0 should remain the default data phone.
-        assertEquals(0, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(0, mPhoneSwitcherUT.getPreferredDataPhoneId());
 
         // Phone 1 has has handed over the call to LTE. And data of DEFAULT apn is enabled.
         // This should trigger data switch.
@@ -831,7 +1123,7 @@
         notifyImsRegistrationTechChange(mPhone2);
 
         // Phone 1 should become the default data phone.
-        assertEquals(1, mPhoneSwitcher.getPreferredDataPhoneId());
+        assertEquals(1, mPhoneSwitcherUT.getPreferredDataPhoneId());
     }
 
     @Test
@@ -846,9 +1138,9 @@
         setSlotIndexToSubId(1, 2);
         setDefaultDataSubId(1);
         NetworkRequest internetRequest = addInternetNetworkRequest(null, 50);
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
         clearInvocations(mMockRadioConfig);
         setAllPhonesInactive();
@@ -858,38 +1150,99 @@
         notifyDataEnabled(false);
         notifyPhoneAsInCall(mPhone2);
         verify(mMockRadioConfig, never()).setPreferredDataModem(anyInt(), any());
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
 
-        // Phone2 has active call. So data switch to it.
+        // Phone2 has active call, and data is on. So data switch to it.
+        doReturn(true).when(mPhone).isUserDataEnabled();
         notifyDataEnabled(true);
         verify(mMockRadioConfig).setPreferredDataModem(eq(1), any());
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
         clearInvocations(mMockRadioConfig);
 
-        // Phone2 call ended. So data switch back to default data sub.
+        // Phone2(nDDS) call ended. But Phone1 having cross-SIM call. Don't switch.
+        mockImsRegTech(0, REGISTRATION_TECH_CROSS_SIM);
+        notifyPhoneAsInIncomingCall(mPhone);
         notifyPhoneAsInactive(mPhone2);
-        verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
-                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        verify(mMockRadioConfig, never()).setPreferredDataModem(anyInt(), any());
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
-        clearInvocations(mMockRadioConfig);
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
 
-        // Phone2 has holding call, but data is turned off. So no data switching should happen.
-        notifyPhoneAsInHoldingCall(mPhone2);
-        verify(mMockRadioConfig).setPreferredDataModem(eq(1), any());
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
-                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        // Phone(DDS) call ended.
+        // Honor auto data switch's suggestion: if DDS is OOS, auto switch to Phone2(nDDS).
+        serviceStateChanged(1, NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
+        serviceStateChanged(0, NetworkRegistrationInfo
+                .REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING);
+        doReturn(null).when(mConnectivityManager).getNetworkCapabilities(any());
+        notifyPhoneAsInactive(mPhone);
+
+        // verify immediately switch back to DDS upon call ends
+        verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
+
+        // verify the attempt to do auto data switch to Phone2(nDDS)
+        processAllFutureMessages();
+        verify(mCellularNetworkValidator).validate(eq(2), anyLong(), eq(false),
+                eq(mPhoneSwitcherUT.mValidationCallback));
+
+        // Phone2 has holding call on VoWifi, no need to switch data
+        clearInvocations(mMockRadioConfig);
+        mockImsRegTech(1, REGISTRATION_TECH_IWLAN);
+        notifyPhoneAsInHoldingCall(mPhone2);
+        verify(mMockRadioConfig, never()).setPreferredDataModem(anyInt(), any());
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
     }
 
+    @Test
+    @SmallTest
+    public void testDataEnabledChangedDuringVoiceCall() throws Exception {
+        doReturn(true).when(mMockRadioConfig).isSetPreferredDataCommandSupported();
+        initialize();
+        // Phone 0 has sub 1, phone 1 has sub 2.
+        // Sub 1 is default data sub.
+        // Both are active subscriptions are active sub, as they are in both active slots.
+        setSlotIndexToSubId(0, 1);
+        setSlotIndexToSubId(1, 2);
+        setDefaultDataSubId(1);
+        NetworkRequest internetRequest = addInternetNetworkRequest(null, 50);
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
+        clearInvocations(mMockRadioConfig);
+        setAllPhonesInactive();
+        // Initialization done.
+
+        // Phone2 has active call and data is on. So switch to nDDS Phone2
+        notifyDataEnabled(true);
+        notifyPhoneAsInCall(mPhone2);
+        verify(mMockRadioConfig).setPreferredDataModem(eq(1), any());
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
+
+        // During the active call, user turns off data, should immediately switch back to DDS
+        notifyDataEnabled(false);
+        verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 0));
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
+                new TelephonyNetworkRequest(internetRequest, mPhone), 1));
+    }
 
     @Test
     @SmallTest
@@ -903,16 +1256,16 @@
         setSlotIndexToSubId(1, 2);
         setDefaultDataSubId(1);
         NetworkRequest internetRequest = addInternetNetworkRequest(2, 50);
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
 
         // Restricted network request will should be applied.
         internetRequest = addInternetNetworkRequest(2, 50, true);
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
     }
 
@@ -928,7 +1281,7 @@
         clearInvocations(mMockRadioConfig);
 
         // override the phone ID in prep for emergency call
-        mPhoneSwitcher.overrideDefaultDataForEmergency(1, 1, mFuturePhone);
+        mPhoneSwitcherUT.overrideDefaultDataForEmergency(1, 1, mFuturePhone);
         sendPreferredDataSuccessResult(1);
         processAllMessages();
         verify(mFuturePhone).complete(true);
@@ -949,7 +1302,7 @@
         clearInvocations(mMockRadioConfig);
 
         // override the phone ID in prep for emergency call
-        mPhoneSwitcher.overrideDefaultDataForEmergency(0, 1, mFuturePhone);
+        mPhoneSwitcherUT.overrideDefaultDataForEmergency(0, 1, mFuturePhone);
         processAllMessages();
         // The radio command should never be called because the DDS hasn't changed.
         verify(mMockRadioConfig, never()).setPreferredDataModem(eq(0), any());
@@ -973,7 +1326,7 @@
 
         // override the phone ID in prep for emergency call
 
-        mPhoneSwitcher.overrideDefaultDataForEmergency(1, 1, mFuturePhone);
+        mPhoneSwitcherUT.overrideDefaultDataForEmergency(1, 1, mFuturePhone);
         sendPreferredDataSuccessResult(1);
         processAllMessages();
         verify(mFuturePhone).complete(true);
@@ -988,7 +1341,7 @@
         processAllMessages();
         verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
         AsyncResult res = new AsyncResult(1, null,  null);
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
 
         // Make sure the correct broadcast is sent out for the phone ID
@@ -1010,7 +1363,7 @@
         clearInvocations(mTelephonyRegistryManager);
 
         // override the phone ID in prep for emergency call
-        mPhoneSwitcher.overrideDefaultDataForEmergency(1, 1, mFuturePhone);
+        mPhoneSwitcherUT.overrideDefaultDataForEmergency(1, 1, mFuturePhone);
         sendPreferredDataSuccessResult(1);
         processAllMessages();
         verify(mFuturePhone).complete(true);
@@ -1039,7 +1392,7 @@
         processAllMessages();
         verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
         AsyncResult res = new AsyncResult(1, null,  null);
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
         // Make sure the correct broadcast is sent out for the phone ID
         verify(mTelephonyRegistryManager).notifyActiveDataSubIdChanged(eq(1));
@@ -1060,7 +1413,7 @@
         clearInvocations(mTelephonyRegistryManager);
 
         // override the phone ID in prep for emergency call
-        mPhoneSwitcher.overrideDefaultDataForEmergency(1, 1, mFuturePhone);
+        mPhoneSwitcherUT.overrideDefaultDataForEmergency(1, 1, mFuturePhone);
         sendPreferredDataSuccessResult(1);
         processAllMessages();
         verify(mFuturePhone).complete(true);
@@ -1070,7 +1423,7 @@
         processAllMessages();
         verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
         AsyncResult res = new AsyncResult(1, null,  null);
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
 
         // Make sure the correct broadcast is sent out for the phone ID
@@ -1095,7 +1448,7 @@
         LinkedBlockingQueue<Boolean> queue = new LinkedBlockingQueue<>();
         CompletableFuture<Boolean> futurePhone = new CompletableFuture<>();
         futurePhone.whenComplete((r, error) -> queue.offer(r));
-        mPhoneSwitcher.overrideDefaultDataForEmergency(1, 1, futurePhone);
+        mPhoneSwitcherUT.overrideDefaultDataForEmergency(1, 1, futurePhone);
         sendPreferredDataSuccessResult(1);
         processAllMessages();
         Boolean result = queue.poll();
@@ -1105,7 +1458,7 @@
         // try override the phone ID again while there is an existing override for a different phone
         futurePhone = new CompletableFuture<>();
         futurePhone.whenComplete((r, error) -> queue.offer(r));
-        mPhoneSwitcher.overrideDefaultDataForEmergency(0, 1, futurePhone);
+        mPhoneSwitcherUT.overrideDefaultDataForEmergency(0, 1, futurePhone);
         processAllMessages();
         result = queue.poll();
         assertNotNull(result);
@@ -1121,7 +1474,7 @@
         processAllMessages();
         verify(mMockRadioConfig).setPreferredDataModem(eq(0), any());
         AsyncResult res = new AsyncResult(1, null,  null);
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res).sendToTarget();
         processAllMessages();
 
         // Make sure the correct broadcast is sent out for the phone ID
@@ -1141,66 +1494,65 @@
         // Both are active subscriptions are active sub, as they are in both active slots.
         setSlotIndexToSubId(0, 1);
         setSlotIndexToSubId(1, 2);
-        setDefaultDataSubId(1);
 
         // Switch to primary before a primary is selected/inactive.
         setDefaultDataSubId(-1);
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(
                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, false, mSetOpptDataCallback1);
         processAllMessages();
 
         assertEquals(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID,
-                mPhoneSwitcher.getAutoSelectedDataSubId());
+                mPhoneSwitcherUT.getAutoSelectedDataSubId());
         verify(mSetOpptDataCallback1).onComplete(SET_OPPORTUNISTIC_SUB_INACTIVE_SUBSCRIPTION);
 
         // once the primary is selected, it becomes the active sub.
         setDefaultDataSubId(2);
-        assertEquals(2, mPhoneSwitcher.getActiveDataSubId());
+        assertEquals(2, mPhoneSwitcherUT.getActiveDataSubId());
 
         setDefaultDataSubId(1);
         // Validating on sub 10 which is inactive.
         clearInvocations(mSetOpptDataCallback1);
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(10, true, mSetOpptDataCallback1);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(10, true, mSetOpptDataCallback1);
         processAllMessages();
         verify(mSetOpptDataCallback1).onComplete(SET_OPPORTUNISTIC_SUB_INACTIVE_SUBSCRIPTION);
 
         // Switch to active subId without validating. Should always succeed.
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, false, mSetOpptDataCallback1);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, false, mSetOpptDataCallback1);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onNetworkAvailable(null, 2);
+        mPhoneSwitcherUT.mValidationCallback.onNetworkAvailable(null, 2);
         processAllMessages();
         verify(mSetOpptDataCallback1).onComplete(SET_OPPORTUNISTIC_SUB_SUCCESS);
 
         // Validating on sub 1 and fails.
         clearInvocations(mSetOpptDataCallback1);
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(1, true, mSetOpptDataCallback1);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(1, true, mSetOpptDataCallback1);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onValidationDone(false, 1);
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(false, 1);
         processAllMessages();
         verify(mSetOpptDataCallback1).onComplete(SET_OPPORTUNISTIC_SUB_VALIDATION_FAILED);
 
         // Validating on sub 2 and succeeds.
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, true, mSetOpptDataCallback2);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, true, mSetOpptDataCallback2);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onValidationDone(true, 2);
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(true, 2);
         processAllMessages();
         verify(mSetOpptDataCallback2).onComplete(SET_OPPORTUNISTIC_SUB_SUCCESS);
 
         // Switching data back to primary and validation fails.
         clearInvocations(mSetOpptDataCallback2);
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(
                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, true, mSetOpptDataCallback2);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onValidationDone(false, 1);
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(false, 1);
         processAllMessages();
         verify(mSetOpptDataCallback1).onComplete(SET_OPPORTUNISTIC_SUB_VALIDATION_FAILED);
 
         // Switching data back to primary and succeeds.
         clearInvocations(mSetOpptDataCallback2);
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(
                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, true, mSetOpptDataCallback2);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onValidationDone(true, 1);
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(true, 1);
         processAllMessages();
         verify(mSetOpptDataCallback2).onComplete(SET_OPPORTUNISTIC_SUB_SUCCESS);
 
@@ -1208,36 +1560,36 @@
         clearInvocations(mSetOpptDataCallback1);
         clearInvocations(mSetOpptDataCallback2);
         clearInvocations(mCellularNetworkValidator);
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, true, mSetOpptDataCallback1);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, true, mSetOpptDataCallback1);
         processAllMessages();
         verify(mCellularNetworkValidator).validate(eq(2), anyLong(), eq(false),
-                eq(mPhoneSwitcher.mValidationCallback));
+                eq(mPhoneSwitcherUT.mValidationCallback));
         doReturn(true).when(mCellularNetworkValidator).isValidating();
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, true, mSetOpptDataCallback2);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, true, mSetOpptDataCallback2);
         processAllMessages();
         verify(mSetOpptDataCallback1).onComplete(SET_OPPORTUNISTIC_SUB_VALIDATION_FAILED);
         verify(mSetOpptDataCallback2, never()).onComplete(anyInt());
         // Validation succeeds.
         doReturn(false).when(mCellularNetworkValidator).isValidating();
-        mPhoneSwitcher.mValidationCallback.onValidationDone(true, 2);
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(true, 2);
         processAllMessages();
         verify(mSetOpptDataCallback2).onComplete(SET_OPPORTUNISTIC_SUB_SUCCESS);
 
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(
                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, false, null);
         processAllMessages();
-        mPhoneSwitcher.mValidationCallback.onNetworkAvailable(null, 1);
+        mPhoneSwitcherUT.mValidationCallback.onNetworkAvailable(null, 1);
         processAllMessages();
         clearInvocations(mSetOpptDataCallback1);
         clearInvocations(mSetOpptDataCallback2);
         clearInvocations(mCellularNetworkValidator);
         // Back to back call, call 1 to switch to subId 2, call 2 to switch back.
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, true, mSetOpptDataCallback1);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, true, mSetOpptDataCallback1);
         processAllMessages();
         verify(mCellularNetworkValidator).validate(eq(2), anyLong(), eq(false),
-                eq(mPhoneSwitcher.mValidationCallback));
+                eq(mPhoneSwitcherUT.mValidationCallback));
         doReturn(true).when(mCellularNetworkValidator).isValidating();
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(
                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, true, mSetOpptDataCallback2);
         processAllMessages();
         // Call 1 should be cancelled and failed. Call 2 return success immediately as there's no
@@ -1262,7 +1614,7 @@
 
         setNumPhones(2, 2);
         AsyncResult result = new AsyncResult(null, 2, null);
-        Message.obtain(mPhoneSwitcher, EVENT_MULTI_SIM_CONFIG_CHANGED, result).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MULTI_SIM_CONFIG_CHANGED, result).sendToTarget();
         processAllMessages();
 
         verify(mPhone2).registerForEmergencyCallToggle(any(), anyInt(), any());
@@ -1286,28 +1638,28 @@
         setSlotIndexToSubId(1, 2);
         setDefaultDataSubId(1);
         NetworkRequest internetRequest = addInternetNetworkRequest(null, 50);
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
         clearInvocations(mMockRadioConfig);
         setAllPhonesInactive();
         // Initialization done.
 
         doReturn(true).when(mSubscriptionController).isOpportunistic(2);
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, false, mSetOpptDataCallback1);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, false, mSetOpptDataCallback1);
         processAllMessages();
         verify(mCellularNetworkValidator).validate(eq(2), anyLong(), eq(false),
-                eq(mPhoneSwitcher.mValidationCallback));
+                eq(mPhoneSwitcherUT.mValidationCallback));
         doReturn(true).when(mCellularNetworkValidator).isValidating();
 
         // Network available on different sub. Should do nothing.
-        mPhoneSwitcher.mValidationCallback.onNetworkAvailable(null, 1);
+        mPhoneSwitcherUT.mValidationCallback.onNetworkAvailable(null, 1);
         processAllMessages();
         verify(mMockRadioConfig, never()).setPreferredDataModem(anyInt(), any());
 
         // Network available on corresponding sub. Should confirm switch.
-        mPhoneSwitcher.mValidationCallback.onNetworkAvailable(null, 2);
+        mPhoneSwitcherUT.mValidationCallback.onNetworkAvailable(null, 2);
         processAllMessages();
         verify(mMockRadioConfig).setPreferredDataModem(eq(1), any());
     }
@@ -1324,28 +1676,28 @@
         setSlotIndexToSubId(1, 2);
         setDefaultDataSubId(1);
         NetworkRequest internetRequest = addInternetNetworkRequest(null, 50);
-        assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 0));
-        assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+        assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                 new TelephonyNetworkRequest(internetRequest, mPhone), 1));
         clearInvocations(mMockRadioConfig);
         setAllPhonesInactive();
         // Initialization done.
 
         doReturn(true).when(mSubscriptionController).isOpportunistic(2);
-        mPhoneSwitcher.trySetOpportunisticDataSubscription(2, false, mSetOpptDataCallback1);
+        mPhoneSwitcherUT.trySetOpportunisticDataSubscription(2, false, mSetOpptDataCallback1);
         processAllMessages();
         verify(mCellularNetworkValidator).validate(eq(2), anyLong(), eq(false),
-                eq(mPhoneSwitcher.mValidationCallback));
+                eq(mPhoneSwitcherUT.mValidationCallback));
         doReturn(true).when(mCellularNetworkValidator).isValidating();
 
         // Validation failed on different sub. Should do nothing.
-        mPhoneSwitcher.mValidationCallback.onValidationDone(false, 1);
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(false, 1);
         processAllMessages();
         verify(mMockRadioConfig, never()).setPreferredDataModem(anyInt(), any());
 
         // Network available on corresponding sub. Should confirm switch.
-        mPhoneSwitcher.mValidationCallback.onValidationDone(false, 2);
+        mPhoneSwitcherUT.mValidationCallback.onValidationDone(false, 2);
         processAllMessages();
         verify(mMockRadioConfig).setPreferredDataModem(eq(1), any());
     }
@@ -1363,16 +1715,15 @@
         // modem retry not invoked.
         AsyncResult res1 = new AsyncResult(0, null,
                 new CommandException(CommandException.Error.INVALID_SIM_STATE));
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res1).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res1).sendToTarget();
         processAllMessages();
         moveTimeForward(5000);
         processAllMessages();
         verify(mMockRadioConfig, times(0)).setPreferredDataModem(eq(0), any());
 
-        doReturn(0).when(mSubscriptionController).getPhoneId(anyInt());
         AsyncResult res2 = new AsyncResult(0, null,
                 new CommandException(CommandException.Error.NETWORK_NOT_READY));
-        Message.obtain(mPhoneSwitcher, EVENT_MODEM_COMMAND_DONE, res2).sendToTarget();
+        Message.obtain(mPhoneSwitcherUT, EVENT_MODEM_COMMAND_DONE, res2).sendToTarget();
         processAllMessages();
         moveTimeForward(5000);
         processAllMessages();
@@ -1405,6 +1756,45 @@
 
     /* Private utility methods start here */
 
+    private void prepareIdealAutoSwitchCondition() {
+        // 1. service state changes
+        serviceStateChanged(1, NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
+        serviceStateChanged(0, NetworkRegistrationInfo
+                .REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING);
+
+        // 2.1 User data enabled on primary SIM
+        doReturn(true).when(mPhone).isUserDataEnabled();
+
+        // 2.2 Auto switch feature is enabled
+        doReturn(true).when(mPhone2).isDataAllowed();
+
+        // 3.1 No default network
+        doReturn(null).when(mConnectivityManager).getNetworkCapabilities(any());
+
+        mPhoneSwitcherUT.sendEmptyMessage(EVENT_EVALUATE_AUTO_SWITCH);
+    }
+
+    private void serviceStateChanged(int phoneId,
+            @NetworkRegistrationInfo.RegistrationState int dataRegState) {
+
+        ServiceState ss = new ServiceState();
+
+        ss.addNetworkRegistrationInfo(new NetworkRegistrationInfo.Builder()
+                .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
+                .setRegistrationState(dataRegState)
+                .setDomain(NetworkRegistrationInfo.DOMAIN_PS)
+                .build());
+
+        ss.setDataRoamingFromRegistration(dataRegState
+                == NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING);
+
+        doReturn(ss).when(mPhones[phoneId]).getServiceState();
+
+        Message msg = mPhoneSwitcherUT.obtainMessage(EVENT_SERVICE_STATE_CHANGED);
+        msg.obj = new AsyncResult(phoneId, null, null);
+        mPhoneSwitcherUT.sendMessage(msg);
+    }
+
     private void setAllPhonesInactive() {
         doReturn(mInactiveCall).when(mPhone).getForegroundCall();
         doReturn(mInactiveCall).when(mPhone).getBackgroundCall();
@@ -1419,43 +1809,47 @@
 
     private void notifyPhoneAsInCall(Phone phone) {
         doReturn(mActiveCall).when(phone).getForegroundCall();
-        mPhoneSwitcher.sendEmptyMessage(EVENT_PRECISE_CALL_STATE_CHANGED);
+        mPhoneSwitcherUT.sendEmptyMessage(EVENT_PRECISE_CALL_STATE_CHANGED);
         processAllMessages();
     }
 
     private void notifyPhoneAsInDial(Phone phone) {
         doReturn(mDialCall).when(phone).getForegroundCall();
-        mPhoneSwitcher.sendEmptyMessage(EVENT_PRECISE_CALL_STATE_CHANGED);
+        mPhoneSwitcherUT.sendEmptyMessage(EVENT_PRECISE_CALL_STATE_CHANGED);
         processAllMessages();
     }
 
     private void notifyPhoneAsInIncomingCall(Phone phone) {
         doReturn(mIncomingCall).when(phone).getForegroundCall();
-        mPhoneSwitcher.sendEmptyMessage(EVENT_PRECISE_CALL_STATE_CHANGED);
+        mPhoneSwitcherUT.sendEmptyMessage(EVENT_PRECISE_CALL_STATE_CHANGED);
         processAllMessages();
     }
 
     private void notifyPhoneAsInHoldingCall(Phone phone) {
         doReturn(mHoldingCall).when(phone).getBackgroundCall();
-        mPhoneSwitcher.sendEmptyMessage(EVENT_PRECISE_CALL_STATE_CHANGED);
+        mPhoneSwitcherUT.sendEmptyMessage(EVENT_PRECISE_CALL_STATE_CHANGED);
         processAllMessages();
     }
 
     private void notifyPhoneAsInactive(Phone phone) {
         doReturn(mInactiveCall).when(phone).getForegroundCall();
-        mPhoneSwitcher.sendEmptyMessage(EVENT_PRECISE_CALL_STATE_CHANGED);
+        mPhoneSwitcherUT.sendEmptyMessage(EVENT_PRECISE_CALL_STATE_CHANGED);
         processAllMessages();
     }
 
     private void notifyDataEnabled(boolean dataEnabled) {
-        doReturn(dataEnabled).when(mDataSettingsManager).isDataEnabled(anyInt());
-        doReturn(dataEnabled).when(mDataSettingsManager2).isDataEnabled(anyInt());
-        mPhoneSwitcher.sendEmptyMessage(EVENT_DATA_ENABLED_CHANGED);
+        doReturn(true).when(mPhone).isUserDataEnabled();
+        doReturn(dataEnabled).when(mDataSettingsManager).isDataEnabled();
+        doReturn(dataEnabled).when(mPhone2).isDataAllowed();
+        mDataSettingsManagerCallbacks.get(0).onDataEnabledChanged(dataEnabled, 123 , "");
+        if (mDataSettingsManagerCallbacks.size() > 1) {
+            mDataSettingsManagerCallbacks.get(1).onDataEnabledChanged(dataEnabled, 123, "");
+        }
         processAllMessages();
     }
 
     private void notifyImsRegistrationTechChange(Phone phone) {
-        mPhoneSwitcher.sendEmptyMessage(EVENT_IMS_RADIO_TECH_CHANGED);
+        mPhoneSwitcherUT.sendEmptyMessage(EVENT_IMS_RADIO_TECH_CHANGED);
         processAllMessages();
     }
 
@@ -1505,10 +1899,10 @@
         for (int i = 0; i < mActiveModemCount; i++) {
             if (defaultDataSub == (i + 1)) {
                 // sub id is always phoneId+1 for testing
-                assertTrue(mPhoneSwitcher.shouldApplyNetworkRequest(
+                assertTrue(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                         new TelephonyNetworkRequest(internetRequest, mPhone), i));
             } else {
-                assertFalse(mPhoneSwitcher.shouldApplyNetworkRequest(
+                assertFalse(mPhoneSwitcherUT.shouldApplyNetworkRequest(
                         new TelephonyNetworkRequest(internetRequest, mPhone), i));
             }
         }
@@ -1528,9 +1922,21 @@
         initializeTelRegistryMock();
         initializeConnManagerMock();
 
-        mPhoneSwitcher = new PhoneSwitcher(mMaxDataAttachModemCount, mContext, Looper.myLooper());
+        mPhoneSwitcherUT = new PhoneSwitcher(mMaxDataAttachModemCount, mContext, Looper.myLooper());
         processAllMessages();
 
+        Field field = PhoneSwitcher.class.getDeclaredField("mDataSettingsManagerCallbacks");
+        field.setAccessible(true);
+        mDataSettingsManagerCallbacks =
+                (Map<Integer, DataSettingsManager.DataSettingsManagerCallback>)
+                        field.get(mPhoneSwitcherUT);
+
+        int deviceConfigValue = 10000;
+        field = PhoneSwitcher.class.getDeclaredField(
+                "mAutoDataSwitchAvailabilityStabilityTimeThreshold");
+        field.setAccessible(true);
+        field.setInt(mPhoneSwitcherUT, deviceConfigValue);
+
         verify(mTelephonyRegistryManager).addOnSubscriptionsChangedListener(any(), any());
     }
 
@@ -1544,6 +1950,7 @@
         doReturn(1).when(mPhone2).getPhoneId();
         doReturn(true).when(mPhone2).isUserDataEnabled();
         doReturn(mDataSettingsManager2).when(mPhone2).getDataSettingsManager();
+        doReturn(mSST2).when(mPhone2).getServiceStateTracker();
         for (int i = 0; i < supportedModemCount; i++) {
             mSlotIndexToSubId[i] = new int[1];
             mSlotIndexToSubId[i][0] = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
@@ -1619,8 +2026,13 @@
      * Capture mNetworkProviderMessenger so that testing can request or release
      * network requests on PhoneSwitcher.
      */
-    private void initializeSubControllerMock() {
+    private void initializeSubControllerMock() throws Exception {
         doReturn(mDefaultDataSub).when(mSubscriptionController).getDefaultDataSubId();
+        doReturn(mDefaultDataSub).when(mMockedIsub).getDefaultDataSubId();
+        doReturn(0).when(mSubscriptionController).getPhoneId(1);
+        doReturn(0).when(mMockedIsub).getPhoneId(1);
+        doReturn(1).when(mSubscriptionController).getPhoneId(2);
+        doReturn(1).when(mMockedIsub).getPhoneId(2);
         doAnswer(invocation -> {
             int phoneId = (int) invocation.getArguments()[0];
             if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
@@ -1630,7 +2042,18 @@
             } else {
                 return mSlotIndexToSubId[phoneId][0];
             }
-        }).when(mSubscriptionController).getSubIdUsingPhoneId(anyInt());
+        }).when(mSubscriptionController).getSubId(anyInt());
+
+        doAnswer(invocation -> {
+            int phoneId = (int) invocation.getArguments()[0];
+            if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
+                return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+            } else if (phoneId == SubscriptionManager.DEFAULT_PHONE_INDEX) {
+                return mSlotIndexToSubId[0][0];
+            } else {
+                return mSlotIndexToSubId[phoneId][0];
+            }
+        }).when(mMockedIsub).getSubId(anyInt());
 
         doAnswer(invocation -> {
             int subId = (int) invocation.getArguments()[0];
@@ -1642,16 +2065,28 @@
             }
             return false;
         }).when(mSubscriptionController).isActiveSubId(anyInt());
+        doReturn(new int[mSlotIndexToSubId.length]).when(mSubscriptionController)
+                .getActiveSubIdList(true);
     }
 
-    private void setDefaultDataSubId(int defaultDataSub) {
+    private void setDefaultDataSubId(int defaultDataSub) throws Exception {
         mDefaultDataSub = defaultDataSub;
         doReturn(mDefaultDataSub).when(mSubscriptionController).getDefaultDataSubId();
+        doReturn(mDefaultDataSub).when(mMockedIsub).getDefaultDataSubId();
+        if (defaultDataSub == 1) {
+            doReturn(true).when(mPhone).isUserDataEnabled();
+            doReturn(false).when(mPhone2).isUserDataEnabled();
+        } else {
+            doReturn(false).when(mPhone).isUserDataEnabled();
+            doReturn(true).when(mPhone2).isUserDataEnabled();
+        }
         sendDefaultDataSubChanged();
     }
 
     private void setSlotIndexToSubId(int slotId, int subId) {
         mSlotIndexToSubId[slotId][0] = subId;
+        Phone phone = slotId == 0 ? mPhone : mPhone2;
+        doReturn(subId).when(phone).getSubId();
     }
 
     /**
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/TelephonyNetworkFactoryTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/TelephonyNetworkFactoryTest.java
index bcc63f3..b7e6b59 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/data/TelephonyNetworkFactoryTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/data/TelephonyNetworkFactoryTest.java
@@ -22,6 +22,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doReturn;
@@ -41,13 +42,13 @@
 
 import androidx.test.filters.FlakyTest;
 
+import com.android.internal.telephony.ISub;
 import com.android.internal.telephony.RadioConfig;
 import com.android.internal.telephony.TelephonyTest;
 import com.android.telephony.Rlog;
 
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
@@ -64,6 +65,7 @@
     // Mocked classes
     PhoneSwitcher mPhoneSwitcher;
     private RadioConfig mMockRadioConfig;
+    private ISub mMockedIsub;
 
     private String mTestName = "";
 
@@ -145,8 +147,13 @@
         super.setUp(getClass().getSimpleName());
         mPhoneSwitcher = mock(PhoneSwitcher.class);
         mMockRadioConfig = mock(RadioConfig.class);
+        mMockedIsub = mock(ISub.class);
         replaceInstance(RadioConfig.class, "sRadioConfig", null, mMockRadioConfig);
 
+        doReturn(mMockedIsub).when(mIBinder).queryLocalInterface(anyString());
+        doReturn(mPhone).when(mPhone).getImsPhone();
+        mServiceManagerMockedServices.put("isub", mIBinder);
+
         mContextFixture.putStringArrayResource(com.android.internal.R.array.networkAttributes,
                 new String[]{"wifi,1,1,1,-1,true", "mobile,0,0,0,-1,true",
                         "mobile_mms,2,0,2,60000,true", "mobile_supl,3,0,2,60000,true",
@@ -206,7 +213,7 @@
         doAnswer(invocation -> {
             final NetworkCapabilities capabilitiesFilter =
                     mTelephonyNetworkFactoryUT.makeNetworkFilter(
-                            mSubscriptionController.getSubIdUsingPhoneId(0));
+                            mSubscriptionController.getSubId(0));
             for (final TelephonyNetworkRequest request : mAllNetworkRequestSet) {
                 final int message = request.canBeSatisfiedBy(capabilitiesFilter)
                         ? CMD_REQUEST_NETWORK : CMD_CANCEL_REQUEST;
@@ -231,7 +238,8 @@
         createMockedTelephonyComponents();
 
         doReturn(false).when(mPhoneSwitcher).shouldApplyNetworkRequest(any(), anyInt());
-        doReturn(subId).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(subId).when(mSubscriptionController).getSubId(phoneId);
+        doReturn(subId).when(mMockedIsub).getSubId(phoneId);
         // fake onSubscriptionChangedListener being triggered.
         mTelephonyNetworkFactoryUT.mInternalHandler.sendEmptyMessage(
                 TelephonyNetworkFactory.EVENT_SUBSCRIPTION_CHANGED);
@@ -293,7 +301,6 @@
      */
     @Test
     @SmallTest
-    @Ignore("b/256052233")
     public void testRequests() throws Exception {
         mTestName = "testActive";
         final int numberOfPhones = 2;
@@ -305,7 +312,8 @@
 
         createMockedTelephonyComponents();
 
-        doReturn(subId).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(subId).when(mSubscriptionController).getSubId(phoneId);
+        doReturn(subId).when(mMockedIsub).getSubId(phoneId);
         mTelephonyNetworkFactoryUT.mInternalHandler.sendEmptyMessage(
                 TelephonyNetworkFactory.EVENT_SUBSCRIPTION_CHANGED);
         processAllMessages();
@@ -319,7 +327,8 @@
         processAllMessages();
         assertEquals(1, mNetworkRequestList.size());
 
-        doReturn(altSubId).when(mSubscriptionController).getSubIdUsingPhoneId(altPhoneId);
+        doReturn(altSubId).when(mSubscriptionController).getSubId(altPhoneId);
+        doReturn(altSubId).when(mMockedIsub).getSubId(altPhoneId);
         processAllMessages();
         assertEquals(1, mNetworkRequestList.size());
 
@@ -334,7 +343,8 @@
         processAllMessages();
         assertEquals(1, mNetworkRequestList.size());
 
-        doReturn(unusedSubId).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(unusedSubId).when(mSubscriptionController).getSubId(phoneId);
+        doReturn(unusedSubId).when(mMockedIsub).getSubId(phoneId);
         mTelephonyNetworkFactoryUT.mInternalHandler.sendEmptyMessage(
                 TelephonyNetworkFactory.EVENT_SUBSCRIPTION_CHANGED);
         processAllMessages();
@@ -344,7 +354,8 @@
         processAllMessages();
         assertEquals(0, mNetworkRequestList.size());
 
-        doReturn(subId).when(mSubscriptionController).getSubIdUsingPhoneId(phoneId);
+        doReturn(subId).when(mSubscriptionController).getSubId(phoneId);
+        doReturn(subId).when(mMockedIsub).getSubId(phoneId);
         mTelephonyNetworkFactoryUT.mInternalHandler.sendEmptyMessage(
                 TelephonyNetworkFactory.EVENT_SUBSCRIPTION_CHANGED);
         processAllMessages();
diff --git a/tests/telephonytests/src/com/android/internal/telephony/domainselection/DomainSelectionConnectionTest.java b/tests/telephonytests/src/com/android/internal/telephony/domainselection/DomainSelectionConnectionTest.java
new file mode 100644
index 0000000..ce59cc6
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/domainselection/DomainSelectionConnectionTest.java
@@ -0,0 +1,311 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.internal.telephony.domainselection;
+
+import static android.telephony.AccessNetworkConstants.AccessNetworkType.EUTRAN;
+import static android.telephony.AccessNetworkConstants.AccessNetworkType.UTRAN;
+import static android.telephony.DomainSelectionService.SCAN_TYPE_NO_PREFERENCE;
+import static android.telephony.DomainSelectionService.SELECTOR_TYPE_CALLING;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+
+import android.os.AsyncResult;
+import android.os.CancellationSignal;
+import android.os.Handler;
+import android.telephony.DomainSelectionService;
+import android.telephony.DomainSelector;
+import android.telephony.EmergencyRegResult;
+import android.telephony.TransportSelectorCallback;
+import android.telephony.WwanSelectorCallback;
+import android.telephony.ims.ImsReasonInfo;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.CallFailCause;
+import com.android.internal.telephony.TelephonyTest;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executor;
+import java.util.function.Consumer;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class DomainSelectionConnectionTest extends TelephonyTest {
+
+    private static final String TELECOM_CALL_ID1 = "TC1";
+
+    private DomainSelectionController mDomainSelectionController;
+    private DomainSelectionConnection.DomainSelectionConnectionCallback mConnectionCallback;
+    private DomainSelectionConnection mDsc;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(this.getClass().getSimpleName());
+
+        mDomainSelectionController = Mockito.mock(DomainSelectionController.class);
+        mConnectionCallback =
+                Mockito.mock(DomainSelectionConnection.DomainSelectionConnectionCallback.class);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mDsc = null;
+        super.tearDown();
+    }
+
+    @Test
+    @SmallTest
+    public void testTransportSelectorCallback() {
+        mDsc = new DomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true,
+                mDomainSelectionController);
+
+        TransportSelectorCallback transportCallback = mDsc.getTransportSelectorCallback();
+
+        assertNotNull(transportCallback);
+    }
+
+    @Test
+    @SmallTest
+    public void testSelectDomain() {
+        mDsc = new DomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true,
+                mDomainSelectionController);
+
+        TransportSelectorCallback transportCallback = mDsc.getTransportSelectorCallback();
+
+        DomainSelectionService.SelectionAttributes attr = getSelectionAttributes(
+                mPhone.getPhoneId(), mPhone.getSubId(), SELECTOR_TYPE_CALLING, true,
+                false, 0, null, null, null, null);
+
+        mDsc.selectDomain(attr);
+
+        verify(mDomainSelectionController).selectDomain(any(), eq(transportCallback));
+    }
+
+    @Test
+    @SmallTest
+    public void testWwanSelectorCallback() throws Exception {
+        mDsc = new DomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true,
+                mDomainSelectionController);
+
+        TransportSelectorCallback transportCallback = mDsc.getTransportSelectorCallback();
+
+        assertNotNull(transportCallback);
+
+        WwanSelectorCallback wwanCallback = null;
+        wwanCallback = transportCallback.onWwanSelected();
+
+        assertNotNull(wwanCallback);
+    }
+
+    @Test
+    @SmallTest
+    public void testWwanSelectorCallbackAsync() throws Exception {
+        mDsc = new DomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true,
+                mDomainSelectionController);
+        replaceInstance(DomainSelectionConnection.class, "mWwanSelectedExecutor",
+                mDsc, new Executor() {
+                    public void execute(Runnable command) {
+                        command.run();
+                    }
+                });
+
+        TransportSelectorCallback transportCallback = mDsc.getTransportSelectorCallback();
+
+        assertNotNull(transportCallback);
+
+        Consumer<WwanSelectorCallback> consumer = Mockito.mock(Consumer.class);
+        transportCallback.onWwanSelected(consumer);
+
+        verify(consumer).accept(any());
+    }
+
+    @Test
+    @SmallTest
+    public void testWwanSelectorCallbackOnRequestEmergencyNetworkScan() throws Exception {
+        mDsc = new DomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true,
+                mDomainSelectionController);
+
+        TransportSelectorCallback transportCallback = mDsc.getTransportSelectorCallback();
+
+        assertNotNull(transportCallback);
+
+        WwanSelectorCallback wwanCallback = transportCallback.onWwanSelected();
+
+        assertNotNull(wwanCallback);
+
+        replaceInstance(DomainSelectionConnection.class, "mLooper",
+                mDsc, mTestableLooper.getLooper());
+        List<Integer> preferredNetworks = new ArrayList<>();
+        preferredNetworks.add(EUTRAN);
+        preferredNetworks.add(UTRAN);
+        int scanType = SCAN_TYPE_NO_PREFERENCE;
+        Consumer<EmergencyRegResult> consumer = Mockito.mock(Consumer.class);
+
+        wwanCallback.onRequestEmergencyNetworkScan(preferredNetworks, scanType, null, consumer);
+
+        ArgumentCaptor<Handler> handlerCaptor = ArgumentCaptor.forClass(Handler.class);
+        ArgumentCaptor<Integer> eventCaptor = ArgumentCaptor.forClass(Integer.class);
+
+        verify(mPhone).registerForEmergencyNetworkScan(
+                handlerCaptor.capture(), eventCaptor.capture(), any());
+
+        int[] expectedPreferredNetworks = new int[] { EUTRAN, UTRAN };
+
+        verify(mPhone).triggerEmergencyNetworkScan(eq(expectedPreferredNetworks),
+                eq(scanType), any());
+
+        Handler handler = handlerCaptor.getValue();
+        int event = eventCaptor.getValue();
+
+        assertNotNull(handler);
+
+        doReturn(new Executor() {
+            public void execute(Runnable r) {
+                r.run();
+            }
+        }).when(mDomainSelectionController).getDomainSelectionServiceExecutor();
+        EmergencyRegResult regResult =
+                new EmergencyRegResult(UTRAN, 0, 0, true, false, 0, 0, "", "", "");
+        handler.sendMessage(handler.obtainMessage(event, new AsyncResult(null, regResult, null)));
+        processAllMessages();
+
+        verify(consumer).accept(eq(regResult));
+    }
+
+    @Test
+    @SmallTest
+    public void testWwanSelectorCallbackOnRequestEmergencyNetworkScanAndCancel() throws Exception {
+        mDsc = new DomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true,
+                mDomainSelectionController);
+
+        TransportSelectorCallback transportCallback = mDsc.getTransportSelectorCallback();
+
+        assertNotNull(transportCallback);
+
+        WwanSelectorCallback wwanCallback = transportCallback.onWwanSelected();
+
+        assertNotNull(wwanCallback);
+
+        replaceInstance(DomainSelectionConnection.class, "mLooper",
+                mDsc, mTestableLooper.getLooper());
+        CancellationSignal signal = new CancellationSignal();
+        wwanCallback.onRequestEmergencyNetworkScan(new ArrayList<>(),
+                SCAN_TYPE_NO_PREFERENCE, signal, Mockito.mock(Consumer.class));
+
+        verify(mPhone).registerForEmergencyNetworkScan(any(), anyInt(), any());
+        verify(mPhone).triggerEmergencyNetworkScan(any(), anyInt(), any());
+
+        signal.cancel();
+
+        verify(mPhone).cancelEmergencyNetworkScan(eq(false), any());
+    }
+
+    @Test
+    @SmallTest
+    public void testDomainSelectorCancelSelection() throws Exception {
+        mDsc = new DomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true,
+                mDomainSelectionController);
+
+        TransportSelectorCallback transportCallback = mDsc.getTransportSelectorCallback();
+
+        assertNotNull(transportCallback);
+
+        DomainSelector domainSelector = Mockito.mock(DomainSelector.class);
+        transportCallback.onCreated(domainSelector);
+
+        mDsc.cancelSelection();
+
+        verify(domainSelector).cancelSelection();
+    }
+
+    @Test
+    @SmallTest
+    public void testDomainSelectorReselectDomain() throws Exception {
+        mDsc = new DomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true,
+                mDomainSelectionController);
+
+        TransportSelectorCallback transportCallback = mDsc.getTransportSelectorCallback();
+
+        assertNotNull(transportCallback);
+
+        DomainSelector domainSelector = Mockito.mock(DomainSelector.class);
+        transportCallback.onCreated(domainSelector);
+
+        DomainSelectionService.SelectionAttributes attr = getSelectionAttributes(
+                mPhone.getPhoneId(), mPhone.getSubId(), SELECTOR_TYPE_CALLING, true,
+                false, CallFailCause.ERROR_UNSPECIFIED, null, null, null, null);
+
+        CompletableFuture<Integer> future = mDsc.reselectDomain(attr);
+
+        assertNotNull(future);
+        assertFalse(future.isDone());
+
+        verify(domainSelector).reselectDomain(any());
+    }
+
+    @Test
+    @SmallTest
+    public void testDomainSelectorFinishSelection() throws Exception {
+        mDsc = new DomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true,
+                mDomainSelectionController);
+
+        TransportSelectorCallback transportCallback = mDsc.getTransportSelectorCallback();
+
+        assertNotNull(transportCallback);
+
+        DomainSelector domainSelector = Mockito.mock(DomainSelector.class);
+        transportCallback.onCreated(domainSelector);
+
+        mDsc.finishSelection();
+
+        verify(domainSelector).finishSelection();
+    }
+
+    private DomainSelectionService.SelectionAttributes getSelectionAttributes(
+            int slotId, int subId, int selectorType, boolean isEmergency,
+            boolean exited, int callFailCause, String callId, String number,
+            ImsReasonInfo imsReasonInfo, EmergencyRegResult regResult) {
+        DomainSelectionService.SelectionAttributes.Builder builder =
+                new DomainSelectionService.SelectionAttributes.Builder(
+                        slotId, subId, selectorType)
+                .setEmergency(isEmergency)
+                .setExitedFromAirplaneMode(exited)
+                .setCsDisconnectCause(callFailCause);
+
+        if (callId != null) builder.setCallId(callId);
+        if (number != null) builder.setNumber(number);
+        if (imsReasonInfo != null) builder.setPsDisconnectCause(imsReasonInfo);
+        if (regResult != null) builder.setEmergencyRegResult(regResult);
+
+        return builder.build();
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/domainselection/DomainSelectionResolverTest.java b/tests/telephonytests/src/com/android/internal/telephony/domainselection/DomainSelectionResolverTest.java
new file mode 100644
index 0000000..2c65b50
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/domainselection/DomainSelectionResolverTest.java
@@ -0,0 +1,178 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import static android.telephony.DomainSelectionService.SELECTOR_TYPE_CALLING;
+import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
+
+import static com.android.internal.telephony.RIL.RADIO_HAL_VERSION_2_0;
+import static com.android.internal.telephony.RIL.RADIO_HAL_VERSION_2_1;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.telephony.DomainSelectionService;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.HalVersion;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.TelephonyTest;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+
+/**
+ * Unit tests for DomainSelectionResolver.
+ */
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class DomainSelectionResolverTest extends TelephonyTest {
+    // Mock classes
+    private DomainSelectionController mDsController;
+    private DomainSelectionConnection mDsConnection;
+    private DomainSelectionService mDsService;
+
+    private DomainSelectionResolver mDsResolver;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(getClass().getSimpleName());
+
+        mDsController = Mockito.mock(DomainSelectionController.class);
+        mDsConnection = Mockito.mock(DomainSelectionConnection.class);
+        mDsService = Mockito.mock(DomainSelectionService.class);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mDsResolver = null;
+        mDsService = null;
+        mDsConnection = null;
+        mDsController = null;
+        super.tearDown();
+    }
+
+    @Test
+    @SmallTest
+    public void testGetInstance() throws IllegalStateException {
+        assertThrows(IllegalStateException.class, () -> {
+            DomainSelectionResolver.getInstance();
+        });
+
+        DomainSelectionResolver.make(mContext, true);
+        DomainSelectionResolver resolver = DomainSelectionResolver.getInstance();
+
+        assertNotNull(resolver);
+    }
+
+    @Test
+    @SmallTest
+    public void testIsDomainSelectionSupportedWhenDeviceConfigDisabled() {
+        setUpResolver(false, RADIO_HAL_VERSION_2_1);
+
+        assertFalse(mDsResolver.isDomainSelectionSupported());
+    }
+
+    @Test
+    @SmallTest
+    public void testIsDomainSelectionSupportedWhenHalVersionLessThan20() {
+        setUpResolver(true, RADIO_HAL_VERSION_2_0);
+
+        assertFalse(mDsResolver.isDomainSelectionSupported());
+    }
+
+    @Test
+    @SmallTest
+    public void testIsDomainSelectionSupported() {
+        setUpResolver(true, RADIO_HAL_VERSION_2_1);
+
+        assertTrue(mDsResolver.isDomainSelectionSupported());
+    }
+
+    @Test
+    @SmallTest
+    public void testGetDomainSelectionConnectionWhenNotInitialized() throws Exception {
+        setUpResolver(true, RADIO_HAL_VERSION_2_1);
+
+        assertThrows(IllegalStateException.class, () -> {
+            mDsResolver.getDomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true);
+        });
+    }
+
+    @Test
+    @SmallTest
+    public void testGetDomainSelectionConnectionWhenPhoneNull() throws Exception {
+        setUpResolver(true, RADIO_HAL_VERSION_2_1);
+        mDsResolver.initialize(mDsService);
+        assertNull(mDsResolver.getDomainSelectionConnection(null, SELECTOR_TYPE_CALLING, true));
+    }
+
+    @Test
+    @SmallTest
+    public void testGetDomainSelectionConnectionWhenImsNotAvailable() throws Exception {
+        setUpResolver(true, RADIO_HAL_VERSION_2_1);
+        mDsResolver.initialize(mDsService);
+        when(mPhone.isImsAvailable()).thenReturn(false);
+
+        assertNull(mDsResolver.getDomainSelectionConnection(mPhone, SELECTOR_TYPE_CALLING, true));
+    }
+
+    @Test
+    @SmallTest
+    public void testGetDomainSelectionConnection() throws Exception {
+        setUpResolver(true, RADIO_HAL_VERSION_2_1);
+        setUpController();
+        mDsResolver.initialize(mDsService);
+        when(mPhone.isImsAvailable()).thenReturn(true);
+
+        assertNotNull(mDsResolver.getDomainSelectionConnection(
+                mPhone, SELECTOR_TYPE_CALLING, true));
+    }
+
+    private void setUpResolver(boolean deviceConfigEnabled, HalVersion halVersion) {
+        mDsResolver = new DomainSelectionResolver(mContext, deviceConfigEnabled);
+        when(mPhone.getHalVersion(eq(HAL_SERVICE_NETWORK))).thenReturn(halVersion);
+    }
+
+    private void setUpController() {
+        mDsResolver.setDomainSelectionControllerFactory(
+                new DomainSelectionResolver.DomainSelectionControllerFactory() {
+                    @Override
+                    public DomainSelectionController create(Context context,
+                            DomainSelectionService service) {
+                        return mDsController;
+                    }
+                });
+
+        when(mDsController.getDomainSelectionConnection(any(Phone.class), anyInt(), anyBoolean()))
+                .thenReturn(mDsConnection);
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/domainselection/EmergencyCallDomainSelectionConnectionTest.java b/tests/telephonytests/src/com/android/internal/telephony/domainselection/EmergencyCallDomainSelectionConnectionTest.java
new file mode 100644
index 0000000..f646d7e
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/domainselection/EmergencyCallDomainSelectionConnectionTest.java
@@ -0,0 +1,219 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.internal.telephony.domainselection;
+
+import static android.telephony.AccessNetworkConstants.AccessNetworkType.EUTRAN;
+import static android.telephony.AccessNetworkConstants.AccessNetworkType.UTRAN;
+import static android.telephony.AccessNetworkConstants.TRANSPORT_TYPE_WLAN;
+import static android.telephony.AccessNetworkConstants.TRANSPORT_TYPE_WWAN;
+import static android.telephony.DisconnectCause.ERROR_UNSPECIFIED;
+import static android.telephony.NetworkRegistrationInfo.DOMAIN_CS;
+import static android.telephony.NetworkRegistrationInfo.DOMAIN_PS;
+import static android.telephony.NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN;
+
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_WLAN;
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_WWAN;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+
+import android.telephony.DomainSelectionService;
+import android.telephony.EmergencyRegResult;
+import android.telephony.NetworkRegistrationInfo;
+import android.telephony.TransportSelectorCallback;
+import android.telephony.WwanSelectorCallback;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.TelephonyTest;
+import com.android.internal.telephony.data.AccessNetworksManager;
+import com.android.internal.telephony.emergency.EmergencyStateTracker;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+
+import java.util.concurrent.CompletableFuture;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class EmergencyCallDomainSelectionConnectionTest extends TelephonyTest {
+
+    private static final String TELECOM_CALL_ID1 = "TC1";
+
+    private DomainSelectionController mDomainSelectionController;
+    private DomainSelectionConnection.DomainSelectionConnectionCallback mConnectionCallback;
+    private EmergencyCallDomainSelectionConnection mEcDsc;
+    private AccessNetworksManager mAnm;
+    private TransportSelectorCallback mTransportCallback;
+    private EmergencyStateTracker mEmergencyStateTracker;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(this.getClass().getSimpleName());
+
+        mDomainSelectionController = Mockito.mock(DomainSelectionController.class);
+        mConnectionCallback =
+                Mockito.mock(DomainSelectionConnection.DomainSelectionConnectionCallback.class);
+        mEmergencyStateTracker = Mockito.mock(EmergencyStateTracker.class);
+        mAnm = Mockito.mock(AccessNetworksManager.class);
+        doReturn(mAnm).when(mPhone).getAccessNetworksManager();
+        mEcDsc = new EmergencyCallDomainSelectionConnection(mPhone,
+                mDomainSelectionController, mEmergencyStateTracker);
+        mTransportCallback = mEcDsc.getTransportSelectorCallback();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mEcDsc = null;
+        super.tearDown();
+    }
+
+    @Test
+    @SmallTest
+    public void testSelectDomainWifi() throws Exception {
+        doReturn(TRANSPORT_TYPE_WLAN).when(mAnm).getPreferredTransport(anyInt());
+        replaceInstance(EmergencyCallDomainSelectionConnection.class,
+                "mEmergencyStateTracker", mEcDsc, mEmergencyStateTracker);
+
+        EmergencyRegResult regResult = new EmergencyRegResult(
+                EUTRAN, REGISTRATION_STATE_UNKNOWN,
+                NetworkRegistrationInfo.DOMAIN_PS,
+                true, false, 0, 0, "", "", "");
+
+        DomainSelectionService.SelectionAttributes attr =
+                EmergencyCallDomainSelectionConnection.getSelectionAttributes(
+                        mPhone.getPhoneId(), mPhone.getSubId(), false,
+                        TELECOM_CALL_ID1, "911", 0, null, regResult);
+
+        CompletableFuture<Integer> future =
+                mEcDsc.createEmergencyConnection(attr, mConnectionCallback);
+
+        assertNotNull(future);
+        assertFalse(future.isDone());
+
+        verify(mDomainSelectionController).selectDomain(any(), any());
+
+        mTransportCallback.onWlanSelected();
+
+        assertTrue(future.isDone());
+        assertEquals((long) DOMAIN_PS, (long) future.get());
+        verify(mEmergencyStateTracker).onEmergencyTransportChanged(MODE_EMERGENCY_WLAN);
+    }
+
+    @Test
+    @SmallTest
+    public void testSelectDomainCs() throws Exception {
+        doReturn(TRANSPORT_TYPE_WWAN).when(mAnm).getPreferredTransport(anyInt());
+        replaceInstance(EmergencyCallDomainSelectionConnection.class,
+                "mEmergencyStateTracker", mEcDsc, mEmergencyStateTracker);
+
+        EmergencyRegResult regResult = new EmergencyRegResult(
+                UTRAN, REGISTRATION_STATE_UNKNOWN,
+                NetworkRegistrationInfo.DOMAIN_CS,
+                true, false, 0, 0, "", "", "");
+
+        DomainSelectionService.SelectionAttributes attr =
+                EmergencyCallDomainSelectionConnection.getSelectionAttributes(
+                        mPhone.getPhoneId(), mPhone.getSubId(), false,
+                        TELECOM_CALL_ID1, "911", 0, null, regResult);
+
+        CompletableFuture<Integer> future =
+                mEcDsc.createEmergencyConnection(attr, mConnectionCallback);
+
+        assertNotNull(future);
+        assertFalse(future.isDone());
+
+        verify(mDomainSelectionController).selectDomain(any(), any());
+
+        WwanSelectorCallback wwanCallback = null;
+        wwanCallback = mTransportCallback.onWwanSelected();
+
+        assertFalse(future.isDone());
+        verify(mEmergencyStateTracker).onEmergencyTransportChanged(MODE_EMERGENCY_WWAN);
+
+        wwanCallback.onDomainSelected(DOMAIN_CS);
+
+        assertTrue(future.isDone());
+        assertEquals((long) DOMAIN_CS, (long) future.get());
+    }
+
+    @Test
+    @SmallTest
+    public void testSelectDomainPs() throws Exception {
+        doReturn(TRANSPORT_TYPE_WWAN).when(mAnm).getPreferredTransport(anyInt());
+        replaceInstance(EmergencyCallDomainSelectionConnection.class,
+                "mEmergencyStateTracker", mEcDsc, mEmergencyStateTracker);
+
+        EmergencyRegResult regResult = new EmergencyRegResult(
+                EUTRAN, REGISTRATION_STATE_UNKNOWN,
+                NetworkRegistrationInfo.DOMAIN_PS,
+                true, true, 0, 0, "", "", "");
+
+        DomainSelectionService.SelectionAttributes attr =
+                EmergencyCallDomainSelectionConnection.getSelectionAttributes(
+                        mPhone.getPhoneId(), mPhone.getSubId(), false,
+                        TELECOM_CALL_ID1, "911", 0, null, regResult);
+
+        CompletableFuture<Integer> future =
+                mEcDsc.createEmergencyConnection(attr, mConnectionCallback);
+
+        assertNotNull(future);
+        assertFalse(future.isDone());
+
+        verify(mDomainSelectionController).selectDomain(any(), any());
+
+        WwanSelectorCallback wwanCallback = null;
+        wwanCallback = mTransportCallback.onWwanSelected();
+
+        assertFalse(future.isDone());
+        verify(mEmergencyStateTracker).onEmergencyTransportChanged(MODE_EMERGENCY_WWAN);
+
+        wwanCallback.onDomainSelected(DOMAIN_PS);
+
+        assertTrue(future.isDone());
+        assertEquals((long) DOMAIN_PS, (long) future.get());
+    }
+
+    @Test
+    @SmallTest
+    public void testOnSelectionTerminated() throws Exception {
+        EmergencyRegResult regResult = new EmergencyRegResult(
+                EUTRAN, REGISTRATION_STATE_UNKNOWN,
+                NetworkRegistrationInfo.DOMAIN_PS,
+                true, true, 0, 0, "", "", "");
+
+        DomainSelectionService.SelectionAttributes attr =
+                EmergencyCallDomainSelectionConnection.getSelectionAttributes(
+                        mPhone.getPhoneId(), mPhone.getSubId(), false,
+                        TELECOM_CALL_ID1, "911", 0, null, regResult);
+
+        mEcDsc.createEmergencyConnection(attr, mConnectionCallback);
+        mTransportCallback.onSelectionTerminated(ERROR_UNSPECIFIED);
+
+        verify(mConnectionCallback).onSelectionTerminated(eq(ERROR_UNSPECIFIED));
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/domainselection/EmergencySmsDomainSelectionConnectionTest.java b/tests/telephonytests/src/com/android/internal/telephony/domainselection/EmergencySmsDomainSelectionConnectionTest.java
new file mode 100644
index 0000000..ff951d5
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/domainselection/EmergencySmsDomainSelectionConnectionTest.java
@@ -0,0 +1,399 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_WLAN;
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_WWAN;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.telephony.AccessNetworkConstants;
+import android.telephony.DomainSelectionService;
+import android.telephony.DomainSelector;
+import android.telephony.NetworkRegistrationInfo;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.TelephonyTest;
+import com.android.internal.telephony.data.AccessNetworksManager;
+import com.android.internal.telephony.emergency.EmergencyStateTracker;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Unit tests for EmergencySmsDomainSelectionConnection.
+ */
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class EmergencySmsDomainSelectionConnectionTest extends TelephonyTest {
+    private DomainSelectionController mDsController;
+    private DomainSelectionConnection.DomainSelectionConnectionCallback mDscCallback;
+    private DomainSelector mDomainSelector;
+    private EmergencyStateTracker mEmergencyStateTracker;
+
+    private Handler mHandler;
+    private AccessNetworksManager mAnm;
+    private DomainSelectionService.SelectionAttributes mDsAttr;
+    private EmergencySmsDomainSelectionConnection mDsConnection;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(this.getClass().getSimpleName());
+
+        if (Looper.myLooper() == null) {
+            Looper.prepare();
+        }
+
+        mHandler = new Handler(Looper.myLooper());
+        mDsController = Mockito.mock(DomainSelectionController.class);
+        mDscCallback = Mockito.mock(
+                DomainSelectionConnection.DomainSelectionConnectionCallback.class);
+        mDomainSelector = Mockito.mock(DomainSelector.class);
+        mEmergencyStateTracker = Mockito.mock(EmergencyStateTracker.class);
+        mAnm = Mockito.mock(AccessNetworksManager.class);
+        when(mPhone.getAccessNetworksManager()).thenReturn(mAnm);
+
+        mDsConnection = new EmergencySmsDomainSelectionConnection(
+                mPhone, mDsController, mEmergencyStateTracker);
+        mDsConnection.getTransportSelectorCallback().onCreated(mDomainSelector);
+        mDsAttr = new DomainSelectionService.SelectionAttributes.Builder(
+                mPhone.getPhoneId(), mPhone.getSubId(), DomainSelectionService.SELECTOR_TYPE_SMS)
+                .setEmergency(true)
+                .build();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mDomainSelector = null;
+        mDsAttr = null;
+        mDsConnection = null;
+        mDscCallback = null;
+        mDsController = null;
+        mEmergencyStateTracker = null;
+        super.tearDown();
+    }
+
+    @Test
+    @SmallTest
+    public void testOnWlanSelected() throws Exception {
+        when(mAnm.getPreferredTransport(anyInt()))
+                .thenReturn(AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onWlanSelected();
+        processAllMessages();
+
+        assertTrue(future.isDone());
+        verify(mEmergencyStateTracker).onEmergencyTransportChanged(eq(MODE_EMERGENCY_WLAN));
+    }
+
+    @Test
+    @SmallTest
+    public void testOnWlanSelectedWithDifferentTransportType() throws Exception {
+        when(mAnm.getPreferredTransport(anyInt())).thenReturn(
+                AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
+                AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onWlanSelected();
+        processAllMessages();
+
+        ArgumentCaptor<Handler> handlerCaptor = ArgumentCaptor.forClass(Handler.class);
+        ArgumentCaptor<Integer> msgCaptor = ArgumentCaptor.forClass(Integer.class);
+        verify(mEmergencyStateTracker).onEmergencyTransportChanged(eq(MODE_EMERGENCY_WLAN));
+        verify(mAnm).registerForQualifiedNetworksChanged(
+                handlerCaptor.capture(), msgCaptor.capture());
+        verify(mPhone).notifyEmergencyDomainSelected(
+                eq(AccessNetworkConstants.TRANSPORT_TYPE_WLAN));
+
+        Handler handler = handlerCaptor.getValue();
+        Integer msg = msgCaptor.getValue();
+        handler.handleMessage(Message.obtain(handler, msg.intValue()));
+        processAllMessages();
+
+        assertTrue(future.isDone());
+        verify(mAnm).unregisterForQualifiedNetworksChanged(any(Handler.class));
+    }
+
+    @Test
+    @SmallTest
+    public void testOnWlanSelectedWithDifferentTransportTypeWhilePreferredTransportChanged()
+            throws Exception {
+        when(mAnm.getPreferredTransport(anyInt())).thenReturn(
+                AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
+                AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onWlanSelected();
+        // When onWlanSelected() is called again,
+        // it will be ignored because the change of preferred transport is in progress.
+        // => onEmergencyTransportChanged() is called only once.
+        mDsConnection.onWlanSelected();
+        processAllMessages();
+
+        ArgumentCaptor<Handler> handlerCaptor = ArgumentCaptor.forClass(Handler.class);
+        ArgumentCaptor<Integer> msgCaptor = ArgumentCaptor.forClass(Integer.class);
+        verify(mEmergencyStateTracker).onEmergencyTransportChanged(eq(MODE_EMERGENCY_WLAN));
+        verify(mAnm).registerForQualifiedNetworksChanged(
+                handlerCaptor.capture(), msgCaptor.capture());
+        verify(mPhone).notifyEmergencyDomainSelected(
+                eq(AccessNetworkConstants.TRANSPORT_TYPE_WLAN));
+
+        Handler handler = handlerCaptor.getValue();
+        Integer msg = msgCaptor.getValue();
+        handler.handleMessage(Message.obtain(handler, msg.intValue()));
+        processAllMessages();
+
+        assertTrue(future.isDone());
+        verify(mAnm).unregisterForQualifiedNetworksChanged(any(Handler.class));
+    }
+
+    @Test
+    @SmallTest
+    public void testOnWwanSelected() throws Exception {
+        mDsConnection.onWwanSelected();
+
+        verify(mEmergencyStateTracker).onEmergencyTransportChanged(eq(MODE_EMERGENCY_WWAN));
+    }
+
+    @Test
+    @SmallTest
+    public void testOnDomainSelectedPs() throws Exception {
+        when(mAnm.getPreferredTransport(anyInt()))
+                .thenReturn(AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_PS);
+        processAllMessages();
+
+        assertTrue(future.isDone());
+    }
+
+    @Test
+    @SmallTest
+    public void testOnDomainSelectedPsWithDifferentTransportType() throws Exception {
+        when(mAnm.getPreferredTransport(anyInt())).thenReturn(
+                AccessNetworkConstants.TRANSPORT_TYPE_WLAN,
+                AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_PS);
+        processAllMessages();
+
+        ArgumentCaptor<Handler> handlerCaptor = ArgumentCaptor.forClass(Handler.class);
+        ArgumentCaptor<Integer> msgCaptor = ArgumentCaptor.forClass(Integer.class);
+        verify(mAnm).registerForQualifiedNetworksChanged(
+                handlerCaptor.capture(), msgCaptor.capture());
+        verify(mPhone).notifyEmergencyDomainSelected(
+                eq(AccessNetworkConstants.TRANSPORT_TYPE_WWAN));
+
+        Handler handler = handlerCaptor.getValue();
+        Integer msg = msgCaptor.getValue();
+        handler.handleMessage(Message.obtain(handler, msg.intValue()));
+        processAllMessages();
+
+        assertTrue(future.isDone());
+        verify(mAnm).unregisterForQualifiedNetworksChanged(any(Handler.class));
+    }
+
+    @Test
+    @SmallTest
+    public void testOnDomainSelectedPsWithDifferentTransportTypeAndNotChanged() throws Exception {
+        when(mAnm.getPreferredTransport(anyInt())).thenReturn(
+                AccessNetworkConstants.TRANSPORT_TYPE_WLAN,
+                AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_PS);
+        processAllMessages();
+
+        ArgumentCaptor<Handler> handlerCaptor = ArgumentCaptor.forClass(Handler.class);
+        ArgumentCaptor<Integer> msgCaptor = ArgumentCaptor.forClass(Integer.class);
+        verify(mAnm).registerForQualifiedNetworksChanged(
+                handlerCaptor.capture(), msgCaptor.capture());
+        verify(mPhone).notifyEmergencyDomainSelected(
+                eq(AccessNetworkConstants.TRANSPORT_TYPE_WWAN));
+
+        Handler handler = handlerCaptor.getValue();
+        Integer msg = msgCaptor.getValue();
+        handler.handleMessage(Message.obtain(handler, msg.intValue()));
+        processAllMessages();
+
+        assertFalse(future.isDone());
+        verify(mAnm, never()).unregisterForQualifiedNetworksChanged(any(Handler.class));
+    }
+
+    @Test
+    @SmallTest
+    public void testOnDomainSelectedPsWithDifferentTransportTypeWhilePreferredTransportChanged()
+            throws Exception {
+        when(mAnm.getPreferredTransport(anyInt())).thenReturn(
+                AccessNetworkConstants.TRANSPORT_TYPE_WLAN,
+                AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_PS);
+        // When onDomainSelected() is called again with the different domain,
+        // it will be ignored because the change of preferred transport is in progress.
+        // => The domain selection result is DOMAIN_PS.
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_CS);
+        processAllMessages();
+
+        ArgumentCaptor<Handler> handlerCaptor = ArgumentCaptor.forClass(Handler.class);
+        ArgumentCaptor<Integer> msgCaptor = ArgumentCaptor.forClass(Integer.class);
+        verify(mAnm).registerForQualifiedNetworksChanged(
+                handlerCaptor.capture(), msgCaptor.capture());
+        verify(mPhone).notifyEmergencyDomainSelected(
+                eq(AccessNetworkConstants.TRANSPORT_TYPE_WWAN));
+
+        Handler handler = handlerCaptor.getValue();
+        Integer msg = msgCaptor.getValue();
+        handler.handleMessage(Message.obtain(handler, msg.intValue()));
+        processAllMessages();
+
+        assertTrue(future.isDone());
+        verify(mAnm).unregisterForQualifiedNetworksChanged(any(Handler.class));
+    }
+
+    @Test
+    @SmallTest
+    public void testOnDomainSelectedCs() throws Exception {
+        when(mAnm.getPreferredTransport(anyInt()))
+                .thenReturn(AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_CS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_CS);
+        processAllMessages();
+
+        assertTrue(future.isDone());
+    }
+
+    @Test
+    @SmallTest
+    public void testFinishSelection() throws Exception {
+        when(mAnm.getPreferredTransport(anyInt())).thenReturn(
+                AccessNetworkConstants.TRANSPORT_TYPE_WLAN,
+                AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_PS);
+        processAllMessages();
+
+        ArgumentCaptor<Handler> handlerCaptor = ArgumentCaptor.forClass(Handler.class);
+        ArgumentCaptor<Integer> msgCaptor = ArgumentCaptor.forClass(Integer.class);
+        verify(mAnm).registerForQualifiedNetworksChanged(
+                handlerCaptor.capture(), msgCaptor.capture());
+        verify(mPhone).notifyEmergencyDomainSelected(
+                eq(AccessNetworkConstants.TRANSPORT_TYPE_WWAN));
+
+        mDsConnection.finishSelection();
+        processAllMessages();
+
+        assertFalse(future.isDone());
+        verify(mAnm).unregisterForQualifiedNetworksChanged(any(Handler.class));
+        verify(mDomainSelector).cancelSelection();
+    }
+
+    @Test
+    @SmallTest
+    public void testFinishSelectionAfterDomainSelectionCompleted() throws Exception {
+        when(mAnm.getPreferredTransport(anyInt())).thenReturn(
+                AccessNetworkConstants.TRANSPORT_TYPE_WLAN,
+                AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_PS);
+        processAllMessages();
+
+        ArgumentCaptor<Handler> handlerCaptor = ArgumentCaptor.forClass(Handler.class);
+        ArgumentCaptor<Integer> msgCaptor = ArgumentCaptor.forClass(Integer.class);
+        verify(mAnm).registerForQualifiedNetworksChanged(
+                handlerCaptor.capture(), msgCaptor.capture());
+        verify(mPhone).notifyEmergencyDomainSelected(
+                eq(AccessNetworkConstants.TRANSPORT_TYPE_WWAN));
+
+        Handler handler = handlerCaptor.getValue();
+        Integer msg = msgCaptor.getValue();
+        handler.handleMessage(Message.obtain(handler, msg.intValue()));
+        processAllMessages();
+        mDsConnection.finishSelection();
+
+        assertTrue(future.isDone());
+        // This method should be invoked one time.
+        verify(mAnm).unregisterForQualifiedNetworksChanged(any(Handler.class));
+        verify(mDomainSelector).finishSelection();
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/domainselection/NormalCallDomainSelectionConnectionTest.java b/tests/telephonytests/src/com/android/internal/telephony/domainselection/NormalCallDomainSelectionConnectionTest.java
new file mode 100644
index 0000000..1db2c99
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/domainselection/NormalCallDomainSelectionConnectionTest.java
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import static android.telephony.DisconnectCause.ERROR_UNSPECIFIED;
+import static android.telephony.DomainSelectionService.SELECTOR_TYPE_CALLING;
+import static android.telephony.NetworkRegistrationInfo.DOMAIN_CS;
+import static android.telephony.NetworkRegistrationInfo.DOMAIN_PS;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+
+import android.telephony.DomainSelectionService;
+import android.telephony.TransportSelectorCallback;
+import android.telephony.WwanSelectorCallback;
+import android.telephony.ims.ImsReasonInfo;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.TelephonyTest;
+import com.android.internal.telephony.data.AccessNetworksManager;
+import com.android.internal.telephony.domainselection.DomainSelectionConnection;
+import com.android.internal.telephony.domainselection.DomainSelectionController;
+import com.android.internal.telephony.domainselection.NormalCallDomainSelectionConnection;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.concurrent.CompletableFuture;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class NormalCallDomainSelectionConnectionTest extends TelephonyTest {
+
+    private static final String TELECOM_CALL_ID1 = "TC1";
+
+    @Mock
+    private DomainSelectionController mMockDomainSelectionController;
+    @Mock
+    private DomainSelectionConnection.DomainSelectionConnectionCallback mMockConnectionCallback;
+    @Mock
+    private AccessNetworksManager mMockAccessNetworksManager;
+
+    private TransportSelectorCallback mTransportCallback;
+    private NormalCallDomainSelectionConnection mNormalCallDomainSelectionConnection;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(this.getClass().getSimpleName());
+        MockitoAnnotations.initMocks(this);
+        doReturn(mMockAccessNetworksManager).when(mPhone).getAccessNetworksManager();
+        mNormalCallDomainSelectionConnection =
+                new NormalCallDomainSelectionConnection(mPhone, mMockDomainSelectionController);
+        mTransportCallback = mNormalCallDomainSelectionConnection.getTransportSelectorCallback();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mNormalCallDomainSelectionConnection = null;
+        super.tearDown();
+    }
+
+    @Test
+    @SmallTest
+    public void testSelectDomainWifi() throws Exception {
+        DomainSelectionService.SelectionAttributes attributes =
+                NormalCallDomainSelectionConnection.getSelectionAttributes(mPhone.getPhoneId(),
+                        mPhone.getSubId(), TELECOM_CALL_ID1, "123", false, 0, null);
+
+        CompletableFuture<Integer> future =
+                mNormalCallDomainSelectionConnection
+                        .createNormalConnection(attributes, mMockConnectionCallback);
+
+        assertNotNull(future);
+        assertFalse(future.isDone());
+
+        verify(mMockDomainSelectionController).selectDomain(any(), any());
+
+        mTransportCallback.onWlanSelected();
+
+        assertTrue(future.isDone());
+        assertEquals((long) DOMAIN_PS, (long) future.get());
+    }
+
+    @Test
+    @SmallTest
+    public void testSelectDomainCs() throws Exception {
+        DomainSelectionService.SelectionAttributes attributes =
+                NormalCallDomainSelectionConnection.getSelectionAttributes(mPhone.getPhoneId(),
+                        mPhone.getSubId(), TELECOM_CALL_ID1, "123", false, 0, null);
+
+        CompletableFuture<Integer> future =
+                mNormalCallDomainSelectionConnection
+                        .createNormalConnection(attributes, mMockConnectionCallback);
+
+        assertNotNull(future);
+        assertFalse(future.isDone());
+
+        verify(mMockDomainSelectionController).selectDomain(any(), any());
+
+        WwanSelectorCallback wwanCallback = mTransportCallback.onWwanSelected();
+
+        assertFalse(future.isDone());
+        wwanCallback.onDomainSelected(DOMAIN_CS);
+
+        assertTrue(future.isDone());
+        assertEquals((long) DOMAIN_CS, (long) future.get());
+    }
+
+    @Test
+    @SmallTest
+    public void testSelectDomainPs() throws Exception {
+        DomainSelectionService.SelectionAttributes attributes =
+                NormalCallDomainSelectionConnection.getSelectionAttributes(mPhone.getPhoneId(),
+                        mPhone.getSubId(), TELECOM_CALL_ID1, "123", false, 0, null);
+
+        CompletableFuture<Integer> future =
+                mNormalCallDomainSelectionConnection
+                        .createNormalConnection(attributes, mMockConnectionCallback);
+
+        assertNotNull(future);
+        assertFalse(future.isDone());
+
+        verify(mMockDomainSelectionController).selectDomain(any(), any());
+
+        WwanSelectorCallback wwanCallback = mTransportCallback.onWwanSelected();
+
+        assertFalse(future.isDone());
+        wwanCallback.onDomainSelected(DOMAIN_PS);
+
+        assertTrue(future.isDone());
+        assertEquals((long) DOMAIN_PS, (long) future.get());
+    }
+
+    @Test
+    @SmallTest
+    public void testOnSelectionTerminated() throws Exception {
+        DomainSelectionService.SelectionAttributes attributes =
+                NormalCallDomainSelectionConnection.getSelectionAttributes(mPhone.getPhoneId(),
+                        mPhone.getSubId(), TELECOM_CALL_ID1, "123", false, 0, null);
+
+        CompletableFuture<Integer> future = mNormalCallDomainSelectionConnection
+                .createNormalConnection(attributes, mMockConnectionCallback);
+        mTransportCallback.onSelectionTerminated(ERROR_UNSPECIFIED);
+
+        verify(mMockConnectionCallback).onSelectionTerminated(eq(ERROR_UNSPECIFIED));
+        assertNotNull(future);
+    }
+
+    @Test
+    public void testGetSelectionAttributes() throws Exception {
+        ImsReasonInfo imsReasonInfo = new ImsReasonInfo();
+        DomainSelectionService.SelectionAttributes attributes =
+                NormalCallDomainSelectionConnection.getSelectionAttributes(1, 2,
+                        TELECOM_CALL_ID1, "123", false, 10, imsReasonInfo);
+
+        assertEquals(1, attributes.getSlotId());
+        assertEquals(2, attributes.getSubId());
+        assertEquals(TELECOM_CALL_ID1, attributes.getCallId());
+        assertEquals("123", attributes.getNumber());
+        assertEquals(false, attributes.isVideoCall());
+        assertEquals(false, attributes.isEmergency());
+        assertEquals(SELECTOR_TYPE_CALLING, attributes.getSelectorType());
+        assertEquals(10, attributes.getCsDisconnectCause());
+        assertEquals(imsReasonInfo, attributes.getPsDisconnectCause());
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/domainselection/SmsDomainSelectionConnectionTest.java b/tests/telephonytests/src/com/android/internal/telephony/domainselection/SmsDomainSelectionConnectionTest.java
new file mode 100644
index 0000000..77d8367
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/domainselection/SmsDomainSelectionConnectionTest.java
@@ -0,0 +1,212 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.domainselection;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
+
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.telephony.DisconnectCause;
+import android.telephony.DomainSelectionService;
+import android.telephony.DomainSelector;
+import android.telephony.NetworkRegistrationInfo;
+import android.telephony.TransportSelectorCallback;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.TestableLooper;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.internal.telephony.Phone;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Unit tests for SmsDomainSelectionConnection.
+ */
+@RunWith(AndroidJUnit4.class)
+public class SmsDomainSelectionConnectionTest {
+    private static final int SLOT_ID = 0;
+    private static final int SUB_ID = 1;
+
+    @Mock private Phone mPhone;
+    @Mock private DomainSelectionController mDsController;
+    @Mock private DomainSelectionConnection.DomainSelectionConnectionCallback mDscCallback;
+    @Mock private DomainSelector mDomainSelector;
+
+    private Handler mHandler;
+    private TestableLooper mTestableLooper;
+    private DomainSelectionService.SelectionAttributes mDsAttr;
+    private SmsDomainSelectionConnection mDsConnection;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        HandlerThread handlerThread = new HandlerThread(
+                SmsDomainSelectionConnectionTest.class.getSimpleName());
+        handlerThread.start();
+
+        mHandler = new Handler(handlerThread.getLooper());
+        mDsConnection = new SmsDomainSelectionConnection(mPhone, mDsController);
+        mDsConnection.getTransportSelectorCallback().onCreated(mDomainSelector);
+        mDsAttr = new DomainSelectionService.SelectionAttributes.Builder(
+                SLOT_ID, SUB_ID, DomainSelectionService.SELECTOR_TYPE_SMS).build();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (mTestableLooper != null) {
+            mTestableLooper.destroy();
+            mTestableLooper = null;
+        }
+
+        if (mHandler != null) {
+            mHandler.getLooper().quit();
+            mHandler = null;
+        }
+
+        mDomainSelector = null;
+        mDsAttr = null;
+        mDsConnection = null;
+        mDscCallback = null;
+        mDsController = null;
+        mPhone = null;
+    }
+
+    @Test
+    @SmallTest
+    public void testRequestDomainSelection() {
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+
+        assertNotNull(future);
+        verify(mDsController).selectDomain(eq(mDsAttr), any(TransportSelectorCallback.class));
+    }
+
+    @Test
+    @SmallTest
+    public void testOnWlanSelected() throws Exception {
+        setUpTestableLooper();
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onWlanSelected();
+        processAllMessages();
+
+        assertTrue(future.isDone());
+    }
+
+    @Test
+    @SmallTest
+    public void testOnSelectionTerminated() {
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        mDsConnection.onSelectionTerminated(DisconnectCause.LOCAL);
+
+        assertFalse(future.isDone());
+        verify(mDscCallback).onSelectionTerminated(eq(DisconnectCause.LOCAL));
+    }
+
+    @Test
+    @SmallTest
+    public void testOnDomainSelectedPs() throws Exception {
+        setUpTestableLooper();
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_PS);
+        processAllMessages();
+
+        assertTrue(future.isDone());
+    }
+
+    @Test
+    @SmallTest
+    public void testOnDomainSelectedCs() throws Exception {
+        setUpTestableLooper();
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_CS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_CS);
+        processAllMessages();
+
+        assertTrue(future.isDone());
+    }
+
+    @Test
+    @SmallTest
+    public void testFinishSelection() throws Exception {
+        setUpTestableLooper();
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.onDomainSelected(NetworkRegistrationInfo.DOMAIN_PS);
+        processAllMessages();
+        mDsConnection.finishSelection();
+
+        verify(mDomainSelector).finishSelection();
+    }
+
+    @Test
+    @SmallTest
+    public void testCancelSelection() throws Exception {
+        CompletableFuture<Integer> future =
+                mDsConnection.requestDomainSelection(mDsAttr, mDscCallback);
+        future.thenAcceptAsync((domain) -> {
+            assertEquals(Integer.valueOf(NetworkRegistrationInfo.DOMAIN_PS), domain);
+        }, mHandler::post);
+
+        mDsConnection.finishSelection();
+
+        verify(mDomainSelector).cancelSelection();
+    }
+
+    private void setUpTestableLooper() throws Exception {
+        mTestableLooper = new TestableLooper(mHandler.getLooper());
+    }
+
+    private void processAllMessages() {
+        while (!mTestableLooper.getLooper().getQueue().isIdle()) {
+            mTestableLooper.processAllMessages();
+        }
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyNumberTest.java b/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyNumberTest.java
index 1273148..5653209 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyNumberTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyNumberTest.java
@@ -261,7 +261,7 @@
                 EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
                 EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
 
-        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2));
+        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2, false));
     }
 
 
@@ -284,7 +284,7 @@
                 EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
                 EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
 
-        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2));
+        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2, false));
     }
 
     public void testSameEmergencyNumberDifferentMnc() throws Exception {
@@ -306,7 +306,7 @@
                 EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
                 EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
 
-        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2));
+        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2, false));
     }
 
     public void testSameEmergencyNumberDifferentCategories() throws Exception {
@@ -328,7 +328,7 @@
                 EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
                 EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
 
-        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2));
+        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2, false));
     }
 
     public void testSameEmergencyNumberDifferentUrns() throws Exception {
@@ -357,7 +357,7 @@
                 EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
                 EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
 
-        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2));
+        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2, false));
     }
 
     public void testSameEmergencyNumberCallRouting() throws Exception {
@@ -379,7 +379,36 @@
                 EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
                 EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
 
-        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2));
+        /* case 1: Check routing is not checked when comparing the same numbers. As routing will
+        be unknown for all numbers apart from DB. Check merge when both are not from DB then
+        routing value is merged from first number. */
+        assertTrue(EmergencyNumber.areSameEmergencyNumbers(num1, num2, false));
+        assertEquals(num1, EmergencyNumber.mergeSameEmergencyNumbers(num1, num2));
+
+        num2 = new EmergencyNumber(
+                "911",
+                "jp",
+                "30",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_UNSPECIFIED,
+                new ArrayList<String>(),
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+
+        /* case 1: Check routing is not checked when comparing the same numbers. Check merge when
+        one of the number is from DB then routing value is merged from DB number. Along with
+        source value is masked with both*/
+        assertTrue(EmergencyNumber.areSameEmergencyNumbers(num1, num2, false));
+
+        num2 = new EmergencyNumber(
+                "911",
+                "jp",
+                "30",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_UNSPECIFIED,
+                new ArrayList<String>(),
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING
+                | EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+        assertEquals(num2, EmergencyNumber.mergeSameEmergencyNumbers(num1, num2));
     }
 
     public void testSameEmergencyNumberDifferentSource() throws Exception {
@@ -401,7 +430,7 @@
                 EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
                 EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
 
-        assertTrue(EmergencyNumber.areSameEmergencyNumbers(num1, num2));
+        assertTrue(EmergencyNumber.areSameEmergencyNumbers(num1, num2, false));
     }
 
     public void testSameEmergencyNumberDifferentSourceTestOrNot() throws Exception {
@@ -423,7 +452,7 @@
                 EmergencyNumber.EMERGENCY_NUMBER_SOURCE_TEST,
                 EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
 
-        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2));
+        assertFalse(EmergencyNumber.areSameEmergencyNumbers(num1, num2, false));
     }
 
     public void testMergeSameNumbersInEmergencyNumberListWithDifferentSources() throws Exception {
@@ -595,4 +624,246 @@
 
         assertEquals(outputNumberList, inputNumberList);
     }
+
+    public void testMergeSameNumbersEmergencyNumberListByDeterminingFields() throws Exception {
+        List<String> urn1 = new ArrayList<>();
+        urn1.add("sos");
+
+        List<String> urn2 = new ArrayList<>();
+        urn2.add("sos:ambulance");
+
+        List<EmergencyNumber> inputNumberList = new ArrayList<>();
+        EmergencyNumber num1 = new EmergencyNumber(
+                "110",
+                "jp",
+                "30",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_UNSPECIFIED,
+                new ArrayList<String>(),
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+
+        EmergencyNumber num2 = new EmergencyNumber(
+                "110",
+                "jp",
+                "30",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_POLICE,
+                urn1,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+        EmergencyNumber num3 = new EmergencyNumber(
+                "911",
+                "us",
+                "30",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_UNSPECIFIED,
+                new ArrayList<String>(),
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+        EmergencyNumber num4 = new EmergencyNumber(
+                "911",
+                "us",
+                "30",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_AMBULANCE,
+                urn2,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY);
+
+        EmergencyNumber num5 = new EmergencyNumber(
+                "112",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_FIRE_BRIGADE,
+                urn2,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+
+        EmergencyNumber num6 = new EmergencyNumber(
+                "112",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_UNSPECIFIED,
+                urn1,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_SIM,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+        EmergencyNumber num7 = new EmergencyNumber(
+                "108",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_FIRE_BRIGADE,
+                urn2,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_MODEM_CONFIG,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+        EmergencyNumber num8 = new EmergencyNumber(
+                "108",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_UNSPECIFIED,
+                new ArrayList<String>(),
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_SIM,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+        EmergencyNumber num9 = new EmergencyNumber(
+                "102",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_FIRE_BRIGADE,
+                urn2,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+        EmergencyNumber num10 = new EmergencyNumber(
+                "102",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_MOUNTAIN_RESCUE,
+                urn1,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_SIM,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+        EmergencyNumber num11 = new EmergencyNumber(
+                "100",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_MIEC,
+                urn2,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY);
+
+        EmergencyNumber num12 = new EmergencyNumber(
+                "100",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_POLICE,
+                new ArrayList<String>(),
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_SIM,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+
+        EmergencyNumber num13 = new EmergencyNumber(
+                "200",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_UNSPECIFIED,
+                new ArrayList<String>(),
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY);
+
+        EmergencyNumber num14 = new EmergencyNumber(
+                "200",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_POLICE,
+                new ArrayList<String>(),
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_MODEM_CONFIG,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+        inputNumberList.add(num1);
+        inputNumberList.add(num2);
+        inputNumberList.add(num3);
+        inputNumberList.add(num4);
+        inputNumberList.add(num5);
+        inputNumberList.add(num6);
+        inputNumberList.add(num7);
+        inputNumberList.add(num8);
+        inputNumberList.add(num9);
+        inputNumberList.add(num10);
+        inputNumberList.add(num11);
+        inputNumberList.add(num12);
+        inputNumberList.add(num13);
+        inputNumberList.add(num14);
+
+        EmergencyNumber mergeOfNum1AndNum2 = new EmergencyNumber(
+                "110",
+                "jp",
+                "30",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_POLICE,
+                urn1,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE
+                | EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+
+        EmergencyNumber mergeOfNum3AndNum4 = new EmergencyNumber(
+                "911",
+                "us",
+                "30",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_AMBULANCE,
+                urn2,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE
+                | EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY);
+
+        List<String> mergedUrns1And2 = new ArrayList<>();
+        mergedUrns1And2.add("sos");
+        mergedUrns1And2.add("sos:ambulance");
+
+        EmergencyNumber mergeOfNum5AndNum6 = new EmergencyNumber(
+                "112",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_FIRE_BRIGADE,
+                mergedUrns1And2,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE
+                | EmergencyNumber.EMERGENCY_NUMBER_SOURCE_SIM,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+
+        EmergencyNumber mergeOfNum7AndNum8 = new EmergencyNumber(
+                "108",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_FIRE_BRIGADE,
+                urn2,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_MODEM_CONFIG
+                | EmergencyNumber.EMERGENCY_NUMBER_SOURCE_SIM,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+        EmergencyNumber mergeOfNum11AndNum12 = new EmergencyNumber(
+                "100",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_POLICE,
+                urn2,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_SIM
+                | EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY);
+
+        List<String> mergedUrns2And1 = new ArrayList<>();
+        mergedUrns2And1.add("sos:ambulance");
+        mergedUrns2And1.add("sos");
+
+        EmergencyNumber mergeOfNum9AndNum10 = new EmergencyNumber(
+                "102",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_FIRE_BRIGADE,
+                mergedUrns2And1,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING
+                | EmergencyNumber.EMERGENCY_NUMBER_SOURCE_SIM,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+
+        EmergencyNumber mergeOfNum13AndNum14 = new EmergencyNumber(
+                "200",
+                "in",
+                "",
+                EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_POLICE,
+                new ArrayList<String>(),
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_MODEM_CONFIG
+                | EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY);
+
+        List<EmergencyNumber> outputNumberList = new ArrayList<>();
+        outputNumberList.add(mergeOfNum1AndNum2);
+        outputNumberList.add(mergeOfNum3AndNum4);
+        outputNumberList.add(mergeOfNum5AndNum6);
+        outputNumberList.add(mergeOfNum7AndNum8);
+        outputNumberList.add(mergeOfNum9AndNum10);
+        outputNumberList.add(mergeOfNum11AndNum12);
+        outputNumberList.add(mergeOfNum13AndNum14);
+        Collections.sort(outputNumberList);
+
+        EmergencyNumber.mergeSameNumbersInEmergencyNumberList(inputNumberList, true);
+        assertEquals(outputNumberList, inputNumberList);
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyNumberTrackerTest.java b/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyNumberTrackerTest.java
index 39ff133..c2d0b40 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyNumberTrackerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyNumberTrackerTest.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony.emergency;
 
+import static android.telephony.TelephonyManager.HAL_SERVICE_VOICE;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
@@ -26,6 +28,9 @@
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 
+import android.content.Context;
+import android.content.res.AssetManager;
+import android.content.res.Resources;
 import android.os.AsyncResult;
 import android.os.Environment;
 import android.os.ParcelFileDescriptor;
@@ -73,6 +78,7 @@
     private static final String EMERGENCY_NUMBER_DB_OTA_FILE = "eccdata_ota";
     private static final int CONFIG_UNIT_TEST_EMERGENCY_NUMBER_DB_VERSION = 99999;
     private static final String CONFIG_EMERGENCY_NUMBER_ADDRESS = "54321";
+    private static final String CONFIG_EMERGENCY_DUPLICATE_NUMBER = "4321";
     private static final String CONFIG_EMERGENCY_NUMBER_COUNTRY = "us";
     private static final String CONFIG_EMERGENCY_NUMBER_MNC = "";
     private static final String NON_3GPP_EMERGENCY_TEST_NUMBER = "9876543";
@@ -115,6 +121,8 @@
 
     private File mLocalDownloadDirectory;
     private ShortNumberInfo mShortNumberInfo;
+    private Context mMockContext;
+    private Resources mResources;
 
     @Before
     public void setUp() throws Exception {
@@ -124,14 +132,18 @@
         mSubControllerMock = mock(SubscriptionController.class);
         mPhone2 = mock(Phone.class);
         mContext = InstrumentationRegistry.getTargetContext();
+        mMockContext = mock(Context.class);
+        mResources = mock(Resources.class);
 
         doReturn(mContext).when(mPhone).getContext();
         doReturn(0).when(mPhone).getPhoneId();
         doReturn(SUB_ID_PHONE_1).when(mPhone).getSubId();
+        doReturn(new HalVersion(1, 4)).when(mPhone).getHalVersion(HAL_SERVICE_VOICE);
 
         doReturn(mContext).when(mPhone2).getContext();
         doReturn(1).when(mPhone2).getPhoneId();
         doReturn(SUB_ID_PHONE_2).when(mPhone2).getSubId();
+        doReturn(new HalVersion(1, 4)).when(mPhone2).getHalVersion(HAL_SERVICE_VOICE);
 
         initializeEmergencyNumberListTestSamples();
         mEmergencyNumberTrackerMock = new EmergencyNumberTracker(mPhone, mSimulatedCommands);
@@ -142,6 +154,9 @@
         // Copy an OTA file to the test directory to similate the OTA mechanism
         simulateOtaEmergencyNumberDb(mPhone);
 
+        AssetManager am = new AssetManager.Builder().build();
+        doReturn(am).when(mMockContext).getAssets();
+
         processAllMessages();
         logd("EmergencyNumberTrackerTest -Setup!");
     }
@@ -182,6 +197,14 @@
             EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
             EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
         mEmergencyNumberListTestSample.add(emergencyNumberForTest);
+
+        emergencyNumberForTest = new EmergencyNumber(
+                CONFIG_EMERGENCY_DUPLICATE_NUMBER, CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+        mEmergencyNumberListTestSample.add(emergencyNumberForTest);
     }
 
     private void sendEmergencyNumberListFromRadio() {
@@ -268,6 +291,11 @@
         }
     }
 
+    private boolean hasDbEmergencyNumbers(List<EmergencyNumber> subList,
+            List<EmergencyNumber> list) {
+        return list.containsAll(subList);
+    }
+
     private boolean hasDbEmergencyNumber(EmergencyNumber number, List<EmergencyNumber> list) {
         return list.contains(number);
     }
@@ -368,8 +396,8 @@
     @Test
     public void testIsEmergencyNumber_FallbackToShortNumberXml_NoSims() throws Exception {
         // Set up the Hal version as 1.4
-        doReturn(new HalVersion(1, 4)).when(mPhone).getHalVersion();
-        doReturn(new HalVersion(1, 4)).when(mPhone2).getHalVersion();
+        doReturn(new HalVersion(1, 4)).when(mPhone).getHalVersion(HAL_SERVICE_VOICE);
+        doReturn(new HalVersion(1, 4)).when(mPhone2).getHalVersion(HAL_SERVICE_VOICE);
 
         setDsdsPhones();
         replaceInstance(SubscriptionController.class, "sInstance", null, mSubControllerMock);
@@ -388,7 +416,7 @@
         processAllMessages();
 
         replaceInstance(ShortNumberInfo.class, "INSTANCE", null, mShortNumberInfo);
-        mEmergencyNumberTrackerMock.isEmergencyNumber(NON_3GPP_EMERGENCY_TEST_NUMBER, true);
+        mEmergencyNumberTrackerMock.isEmergencyNumber(NON_3GPP_EMERGENCY_TEST_NUMBER);
 
         //verify that we fall back to shortnumber xml when there are no SIMs
         verify(mShortNumberInfo).isEmergencyNumber(NON_3GPP_EMERGENCY_TEST_NUMBER, "JP");
@@ -406,8 +434,8 @@
 
     private void testIsEmergencyNumber_NoFallbackToShortNumberXml(int numSims) throws Exception {
         // Set up the Hal version as 1.4
-        doReturn(new HalVersion(1, 4)).when(mPhone).getHalVersion();
-        doReturn(new HalVersion(1, 4)).when(mPhone2).getHalVersion();
+        doReturn(new HalVersion(1, 4)).when(mPhone).getHalVersion(HAL_SERVICE_VOICE);
+        doReturn(new HalVersion(1, 4)).when(mPhone2).getHalVersion(HAL_SERVICE_VOICE);
 
         assertTrue((numSims > 0 && numSims < 3));
         setDsdsPhones();
@@ -436,7 +464,7 @@
         processAllMessages();
 
         replaceInstance(ShortNumberInfo.class, "INSTANCE", null, mShortNumberInfo);
-        mEmergencyNumberTrackerMock.isEmergencyNumber(NON_3GPP_EMERGENCY_TEST_NUMBER, true);
+        mEmergencyNumberTrackerMock.isEmergencyNumber(NON_3GPP_EMERGENCY_TEST_NUMBER);
 
         //verify we do not use ShortNumber xml
         verify(mShortNumberInfo, never()).isEmergencyNumber(anyString(), anyString());
@@ -480,14 +508,21 @@
      */
     @Test
     public void testUsingEmergencyNumberDatabaseWheneverHal_1_3() {
-        doReturn(new HalVersion(1, 3)).when(mPhone).getHalVersion();
+        doReturn(new HalVersion(1, 3)).when(mPhone).getHalVersion(HAL_SERVICE_VOICE);
+        doReturn(mMockContext).when(mPhone).getContext();
+        doReturn(mResources).when(mMockContext).getResources();
+        doReturn(true).when(mResources).getBoolean(
+                com.android.internal.R.bool.ignore_emergency_number_routing_from_db);
 
-        sendEmergencyNumberPrefix(mEmergencyNumberTrackerMock);
-        mEmergencyNumberTrackerMock.updateEmergencyCountryIsoAllPhones("us");
+        EmergencyNumberTracker emergencyNumberTracker = new EmergencyNumberTracker(
+                mPhone, mSimulatedCommands);
+
+        sendEmergencyNumberPrefix(emergencyNumberTracker);
+        emergencyNumberTracker.updateEmergencyCountryIsoAllPhones("us");
         processAllMessages();
 
         boolean hasDatabaseNumber = false;
-        for (EmergencyNumber number : mEmergencyNumberTrackerMock.getEmergencyNumberList()) {
+        for (EmergencyNumber number : emergencyNumberTracker.getEmergencyNumberList()) {
             if (number.isFromSources(EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE)) {
                 hasDatabaseNumber = true;
                 break;
@@ -501,13 +536,232 @@
      */
     @Test
     public void testUsingEmergencyNumberDatabaseWheneverHal_1_4() {
-        doReturn(new HalVersion(1, 4)).when(mPhone).getHalVersion();
+        doReturn(mMockContext).when(mPhone).getContext();
+        doReturn(mContext.getAssets()).when(mMockContext).getAssets();
+        doReturn(mResources).when(mMockContext).getResources();
+        doReturn(true).when(mResources).getBoolean(
+                com.android.internal.R.bool.ignore_emergency_number_routing_from_db);
 
-        sendEmergencyNumberPrefix(mEmergencyNumberTrackerMock);
-        mEmergencyNumberTrackerMock.updateEmergencyCountryIsoAllPhones("us");
+        EmergencyNumberTracker emergencyNumberTrackerMock = new EmergencyNumberTracker(
+                mPhone, mSimulatedCommands);
+        emergencyNumberTrackerMock.sendMessage(
+                emergencyNumberTrackerMock.obtainMessage(
+                        1 /* EVENT_UNSOL_EMERGENCY_NUMBER_LIST */,
+                        new AsyncResult(null, mEmergencyNumberListTestSample, null)));
+        sendEmergencyNumberPrefix(emergencyNumberTrackerMock);
+        emergencyNumberTrackerMock.updateEmergencyCountryIsoAllPhones("us");
         processAllMessages();
+        /* case 1: check DB number exist or not */
         assertTrue(hasDbEmergencyNumber(CONFIG_EMERGENCY_NUMBER,
-                mEmergencyNumberTrackerMock.getEmergencyNumberList()));
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        /* case 2: since ignore_emergency_routing_from_db is true. check for all DB numbers with
+        routing value as unknown by ignoring DB value */
+        List<EmergencyNumber> completeEmergencyNumberList = new ArrayList<>();
+        EmergencyNumber emergencyNumber = new EmergencyNumber(
+                "888", CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+        completeEmergencyNumberList.add(emergencyNumber);
+
+        emergencyNumber = new EmergencyNumber(
+                "54321", CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+        completeEmergencyNumberList.add(emergencyNumber);
+
+        emergencyNumber = new EmergencyNumber(
+                "654321", CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+        completeEmergencyNumberList.add(emergencyNumber);
+
+        emergencyNumber = new EmergencyNumber(
+                "7654321", CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+        completeEmergencyNumberList.add(emergencyNumber);
+
+        assertTrue(hasDbEmergencyNumbers(completeEmergencyNumberList,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        /* case 3: check the routing type of merged duplicate numbers
+            between DB number and radio list. */
+        EmergencyNumber duplicateEmergencyNumber = new EmergencyNumber(
+                CONFIG_EMERGENCY_DUPLICATE_NUMBER, CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE
+                | EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+        assertTrue(hasDbEmergencyNumber(duplicateEmergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+    }
+
+    @Test
+    public void testUsingEmergencyNumberDatabaseWithRouting() {
+        doReturn(mMockContext).when(mPhone).getContext();
+        doReturn(mContext.getAssets()).when(mMockContext).getAssets();
+        doReturn(mResources).when(mMockContext).getResources();
+        doReturn("05").when(mCellIdentity).getMncString();
+        doReturn(false).when(mResources).getBoolean(
+                com.android.internal.R.bool.ignore_emergency_number_routing_from_db);
+
+        EmergencyNumberTracker emergencyNumberTrackerMock = new EmergencyNumberTracker(
+                mPhone, mSimulatedCommands);
+        emergencyNumberTrackerMock.sendMessage(
+                emergencyNumberTrackerMock.obtainMessage(
+                        1 /* EVENT_UNSOL_EMERGENCY_NUMBER_LIST */,
+                        new AsyncResult(null, mEmergencyNumberListTestSample, null)));
+        sendEmergencyNumberPrefix(emergencyNumberTrackerMock);
+        emergencyNumberTrackerMock.updateEmergencyCountryIsoAllPhones("us");
+        processAllMessages();
+
+        // case 1: check DB number with normal routing true and for mnc 05
+        EmergencyNumber emergencyNumber = new EmergencyNumber(
+                CONFIG_EMERGENCY_NUMBER_ADDRESS, CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                    "05", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                            CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                                    EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                                            EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        // case 2: check DB number with normal routing true in multiple mnc 05, 45, 47
+        emergencyNumber = new EmergencyNumber(
+                "888", CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                    "05", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                            CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                                    EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                                            EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        doReturn("47").when(mCellIdentity).getMncString();
+        emergencyNumber = new EmergencyNumber(
+                "888", CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                    "47", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                        CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                            EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                                EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        emergencyNumber = new EmergencyNumber(
+                CONFIG_EMERGENCY_NUMBER_ADDRESS, CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                    "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                            CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                                    EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                                            EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY);
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        /* case 3: check DB number with normal routing false and for mnc 05,
+            but current cell identity is 04 */
+        doReturn("04").when(mCellIdentity).getMncString();
+        emergencyNumber = new EmergencyNumber(
+                CONFIG_EMERGENCY_NUMBER_ADDRESS, CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                    "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                            CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                                    EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                                            EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY);
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        // case 4: check DB number with normal routing false
+        emergencyNumber = new EmergencyNumber(
+                "654321", CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                    "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                            CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                                    EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                                            EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY);
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        // case 5: check DB number with normal routing true & empty mnc
+        emergencyNumber = new EmergencyNumber(
+                "7654321", CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                    "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                            CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                                    EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                                            EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        /* case 6: check DB number with normal routing true & empty mnc. But same number exist
+            in radio list. In merge DB routing should be used */
+        emergencyNumber = new EmergencyNumber(
+                CONFIG_EMERGENCY_DUPLICATE_NUMBER, CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE
+                | EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
+                EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+    }
+
+    @Test
+    public void testUsingEmergencyNumberDatabaseWithRoutingInOOS() {
+        doReturn(mMockContext).when(mPhone).getContext();
+        doReturn(mContext.getAssets()).when(mMockContext).getAssets();
+        doReturn(mResources).when(mMockContext).getResources();
+        doReturn(false).when(mResources).getBoolean(
+                com.android.internal.R.bool.ignore_emergency_number_routing_from_db);
+
+        EmergencyNumberTracker emergencyNumberTrackerMock = new EmergencyNumberTracker(
+                mPhone, mSimulatedCommands);
+        emergencyNumberTrackerMock.sendMessage(
+                emergencyNumberTrackerMock.obtainMessage(
+                        1 /* EVENT_UNSOL_EMERGENCY_NUMBER_LIST */,
+                        new AsyncResult(null, mEmergencyNumberListTestSample, null)));
+        sendEmergencyNumberPrefix(emergencyNumberTrackerMock);
+        emergencyNumberTrackerMock.updateEmergencyCountryIsoAllPhones("us");
+        processAllMessages();
+
+        // Check routing when cellidentity is null, which is oos
+        doReturn(null).when(mPhone).getCurrentCellIdentity();
+        EmergencyNumber emergencyNumber = new EmergencyNumber(
+                CONFIG_EMERGENCY_NUMBER_ADDRESS, CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                    "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                            CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                                    EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                                            EmergencyNumber.EMERGENCY_CALL_ROUTING_UNKNOWN);
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        // Check routing when cellidentity is 04, which is not part of normal routing mncs
+        doReturn(mCellIdentity).when(mPhone).getCurrentCellIdentity();
+        doReturn("04").when(mCellIdentity).getMncString();
+        emergencyNumber = new EmergencyNumber(
+                CONFIG_EMERGENCY_NUMBER_ADDRESS, CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                    "", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                            CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                                    EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                                            EmergencyNumber.EMERGENCY_CALL_ROUTING_EMERGENCY);
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
+
+        // Check routing when cellidentity is 05, which is part of normal routing mncs
+        doReturn("05").when(mCellIdentity).getMncString();
+        emergencyNumber = new EmergencyNumber(
+                CONFIG_EMERGENCY_NUMBER_ADDRESS, CONFIG_EMERGENCY_NUMBER_COUNTRY,
+                    "05", CONFIG_EMERGENCY_NUMBER_SERVICE_CATEGORIES,
+                            CONFIG_EMERGENCY_NUMBER_SERVICE_URNS,
+                                    EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE,
+                                            EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
+        assertTrue(hasDbEmergencyNumber(emergencyNumber,
+                emergencyNumberTrackerMock.getEmergencyNumberList()));
     }
 
     /**
@@ -516,7 +770,7 @@
     @Test
     public void testOtaEmergencyNumberDatabase() {
         // Set up the Hal version as 1.4 to apply emergency number database
-        doReturn(new HalVersion(1, 4)).when(mPhone).getHalVersion();
+        doReturn(new HalVersion(1, 4)).when(mPhone).getHalVersion(HAL_SERVICE_VOICE);
 
         sendEmergencyNumberPrefix(mEmergencyNumberTrackerMock);
         mEmergencyNumberTrackerMock.updateEmergencyCountryIsoAllPhones("");
diff --git a/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyStateTrackerTest.java b/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyStateTrackerTest.java
new file mode 100644
index 0000000..ef77984
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/emergency/EmergencyStateTrackerTest.java
@@ -0,0 +1,508 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.emergency;
+
+import static com.android.internal.telephony.emergency.EmergencyConstants.MODE_EMERGENCY_WWAN;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyBoolean;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.os.AsyncResult;
+import android.os.Build;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.provider.Settings;
+import android.telephony.CarrierConfigManager;
+import android.telephony.DisconnectCause;
+import android.telephony.EmergencyRegResult;
+import android.telephony.ServiceState;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.Call;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.TelephonyTest;
+import com.android.internal.telephony.ServiceStateTracker;
+import com.android.internal.telephony.data.PhoneSwitcher;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Consumer;
+
+/**
+ * Unit tests for EmergencyStateTracker
+ */
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class EmergencyStateTrackerTest extends TelephonyTest {
+
+    private static final String TEST_CALL_ID = "00001";
+
+    @Mock EmergencyStateTracker.PhoneFactoryProxy mPhoneFactoryProxy;
+    @Mock EmergencyStateTracker.PhoneSwitcherProxy mPhoneSwitcherProxy;
+    @Mock EmergencyStateTracker.TelephonyManagerProxy mTelephonyManagerProxy;
+    @Mock PhoneSwitcher mPhoneSwitcher;
+    @Mock RadioOnHelper mRadioOnHelper;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(getClass().getSimpleName());
+        MockitoAnnotations.initMocks(this);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    @Test
+    @SmallTest
+    public void getInstance_notInitializedTillMake() throws IllegalStateException {
+        assertThrows(IllegalStateException.class, () -> {
+            EmergencyStateTracker.getInstance();
+        });
+
+        EmergencyStateTracker.make(mContext, true);
+
+        assertNotNull(EmergencyStateTracker.getInstance());
+    }
+
+    @Test
+    @SmallTest
+    public void getInstance_returnsSameInstance() {
+        EmergencyStateTracker.make(mContext, true);
+        EmergencyStateTracker instance1 = EmergencyStateTracker.getInstance();
+        EmergencyStateTracker instance2 = EmergencyStateTracker.getInstance();
+
+        assertSame(instance1, instance2);
+    }
+
+    /**
+     * Test that the EmergencyStateTracker turns on radio, performs a DDS switch and sets emergency
+     * mode switch when we are not roaming and the carrier only supports SUPL over the data plane.
+     */
+    @Test
+    @SmallTest
+    public void startEmergencyCall_radioOff_turnOnRadioSwitchDdsAndSetEmergencyMode() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                true /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phones and set radio off
+        Phone testPhone = setupTestPhoneForEmergencyCall(false /* isRoaming */,
+                false /* isRadioOn */);
+        CarrierConfigManager cfgManager = (CarrierConfigManager) mContext
+                .getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putStringArray(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_DATA_PLANE_ONLY_ROAMING_PLMN_STRING_ARRAY,
+                null);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putInt(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_CONTROL_PLANE_SUPPORT_INT,
+                CarrierConfigManager.Gps.SUPL_EMERGENCY_MODE_TYPE_DP_ONLY);
+        cfgManager.getConfigForSubId(testPhone.getSubId())
+                .putString(CarrierConfigManager.Gps.KEY_ES_EXTENSION_SEC_STRING, "150");
+        // Spy is used to capture consumer in delayDialForDdsSwitch
+        EmergencyStateTracker spyEst = spy(emergencyStateTracker);
+
+        CompletableFuture<Integer> unused = spyEst.startEmergencyCall(testPhone, TEST_CALL_ID,
+                false);
+
+        // startEmergencyCall should trigger radio on
+        ArgumentCaptor<RadioOnStateListener.Callback> callback = ArgumentCaptor
+                .forClass(RadioOnStateListener.Callback.class);
+        verify(mRadioOnHelper).triggerRadioOnAndListen(callback.capture(), eq(true), eq(testPhone),
+                eq(false));
+        // isOkToCall() should return true once radio is on
+        assertFalse(callback.getValue().isOkToCall(testPhone, ServiceState.STATE_OUT_OF_SERVICE));
+        when(mSST.isRadioOn()).thenReturn(true);
+        assertTrue(callback.getValue().isOkToCall(testPhone, ServiceState.STATE_OUT_OF_SERVICE));
+        // Once radio on is complete, trigger delay dial
+        callback.getValue().onComplete(null, true);
+        ArgumentCaptor<Consumer<Boolean>> completeConsumer = ArgumentCaptor
+                .forClass(Consumer.class);
+        verify(spyEst).delayDialForDdsSwitch(eq(testPhone), completeConsumer.capture());
+        verify(mPhoneSwitcher).overrideDefaultDataForEmergency(eq(0) /* phoneId */ ,
+                eq(150) /* extensionTime */, any());
+        // After dds switch completes successfully, set emergency mode
+        completeConsumer.getValue().accept(true);
+        verify(testPhone).setEmergencyMode(eq(MODE_EMERGENCY_WWAN), any());
+    }
+
+    /**
+     * Test that if startEmergencyCall fails to turn on radio, then it's future completes with
+     * DisconnectCause.POWER_OFF.
+     */
+    @Test
+    @SmallTest
+    public void startEmergencyCall_radioOnFails_returnsDisconnectCausePowerOff() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                true /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phones and set radio off
+        Phone testPhone = setupTestPhoneForEmergencyCall(false /* isRoaming */,
+                false /* isRadioOn */);
+
+        CompletableFuture<Integer> future = emergencyStateTracker.startEmergencyCall(testPhone,
+                TEST_CALL_ID, false);
+
+        // startEmergencyCall should trigger radio on
+        ArgumentCaptor<RadioOnStateListener.Callback> callback = ArgumentCaptor
+                .forClass(RadioOnStateListener.Callback.class);
+        verify(mRadioOnHelper).triggerRadioOnAndListen(callback.capture(), eq(true), eq(testPhone),
+                eq(false));
+        // Verify future completes with DisconnectCause.POWER_OFF if radio not ready
+        CompletableFuture<Void> unused = future.thenAccept((result) -> {
+            assertEquals((Integer) result, (Integer) DisconnectCause.POWER_OFF);
+        });
+        callback.getValue().onComplete(null, false /* isRadioReady */);
+    }
+
+    /**
+     * Test that the EmergencyStateTracker does not perform a DDS switch when the carrier supports
+     * control-plane fallback. Radio is set to on so RadioOnHelper not triggered.
+     */
+    @Test
+    @SmallTest
+    public void startEmergencyCall_cpFallback_noDdsSwitch() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                true /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phones and set radio on
+        Phone testPhone = setupTestPhoneForEmergencyCall(false /* isRoaming */,
+                true /* isRadioOn */);
+        CarrierConfigManager cfgManager = (CarrierConfigManager) mContext
+                .getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putStringArray(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_DATA_PLANE_ONLY_ROAMING_PLMN_STRING_ARRAY,
+                null);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putInt(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_CONTROL_PLANE_SUPPORT_INT,
+                CarrierConfigManager.Gps.SUPL_EMERGENCY_MODE_TYPE_CP_FALLBACK);
+        cfgManager.getConfigForSubId(testPhone.getSubId())
+                .putString(CarrierConfigManager.Gps.KEY_ES_EXTENSION_SEC_STRING, "0");
+
+        CompletableFuture<Integer> unused = emergencyStateTracker.startEmergencyCall(testPhone,
+                TEST_CALL_ID, false);
+
+        // Radio already on so shouldn't trigger this
+        verify(mRadioOnHelper, never()).triggerRadioOnAndListen(any(), anyBoolean(), any(),
+                anyBoolean());
+        // Carrier supports control-plane fallback, so no DDS switch
+        verify(mPhoneSwitcher, never()).overrideDefaultDataForEmergency(anyInt(), anyInt(), any());
+    }
+
+    /**
+     * Test that the EmergencyStateTracker does not perform a DDS switch if the non-DDS supports
+     * SUPL.
+     */
+    @Test
+    @SmallTest
+    public void startEmergencyCall_supportsSuplOnNonDds_noDdsSwitch() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                false /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phones
+        Phone testPhone = setupTestPhoneForEmergencyCall(false /* isRoaming */,
+                true /* isRadioOn */);
+        CarrierConfigManager cfgManager = (CarrierConfigManager) mContext
+                .getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putStringArray(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_DATA_PLANE_ONLY_ROAMING_PLMN_STRING_ARRAY,
+                null);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putInt(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_CONTROL_PLANE_SUPPORT_INT,
+                CarrierConfigManager.Gps.SUPL_EMERGENCY_MODE_TYPE_DP_ONLY);
+        cfgManager.getConfigForSubId(testPhone.getSubId())
+                .putString(CarrierConfigManager.Gps.KEY_ES_EXTENSION_SEC_STRING, "0");
+
+        CompletableFuture<Integer> unused = emergencyStateTracker.startEmergencyCall(testPhone,
+                TEST_CALL_ID, false);
+
+        // non-DDS supports SUPL, so no DDS switch
+        verify(mPhoneSwitcher, never()).overrideDefaultDataForEmergency(anyInt(), anyInt(), any());
+    }
+
+    /**
+     * Test that the EmergencyStateTracker does not perform a DDS switch when the carrier does not
+     * support control-plane fallback while roaming.
+     */
+    @Test
+    @SmallTest
+    public void startEmergencyCall_roaming_noDdsSwitch() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                true /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phones
+        Phone testPhone = setupTestPhoneForEmergencyCall(true /* isRoaming */,
+                true /* isRadioOn */);
+        CarrierConfigManager cfgManager = (CarrierConfigManager) mContext
+                .getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putStringArray(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_DATA_PLANE_ONLY_ROAMING_PLMN_STRING_ARRAY,
+                null);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putInt(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_CONTROL_PLANE_SUPPORT_INT,
+                CarrierConfigManager.Gps.SUPL_EMERGENCY_MODE_TYPE_DP_ONLY);
+        cfgManager.getConfigForSubId(testPhone.getSubId())
+                .putString(CarrierConfigManager.Gps.KEY_ES_EXTENSION_SEC_STRING, "0");
+
+        CompletableFuture<Integer> unused = emergencyStateTracker.startEmergencyCall(testPhone,
+                TEST_CALL_ID, false);
+
+        // Is roaming, so no DDS switch
+        verify(mPhoneSwitcher, never()).overrideDefaultDataForEmergency(anyInt(), anyInt(), any());
+    }
+
+    /**
+     * Test that the EmergencyStateTracker does perform a DDS switch even though the carrier
+     * supports control-plane fallback and the roaming partner is configured to look like a home
+     * network.
+     */
+    @Test
+    @SmallTest
+    public void startEmergencyCall_roamingCarrierConfig_switchDds() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                true /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phones
+        Phone testPhone = setupTestPhoneForEmergencyCall(false /* isRoaming */,
+                true /* isRadioOn */);
+        // Setup voice roaming scenario
+        String testRoamingOperator = "001001";
+        testPhone.getServiceState().setOperatorName("TestTel", "TestTel", testRoamingOperator);
+        String[] roamingPlmns = new String[1];
+        roamingPlmns[0] = testRoamingOperator;
+        CarrierConfigManager cfgManager = (CarrierConfigManager) mContext
+                .getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putStringArray(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_DATA_PLANE_ONLY_ROAMING_PLMN_STRING_ARRAY,
+                roamingPlmns);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putInt(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_CONTROL_PLANE_SUPPORT_INT,
+                CarrierConfigManager.Gps.SUPL_EMERGENCY_MODE_TYPE_CP_FALLBACK);
+        cfgManager.getConfigForSubId(testPhone.getSubId())
+                .putString(CarrierConfigManager.Gps.KEY_ES_EXTENSION_SEC_STRING, "0");
+
+        CompletableFuture<Integer> unused = emergencyStateTracker.startEmergencyCall(testPhone,
+                TEST_CALL_ID, false);
+
+        // Verify DDS switch
+        verify(mPhoneSwitcher).overrideDefaultDataForEmergency(eq(0) /* phoneId */,
+                eq(0) /* extensionTime */, any());
+    }
+
+    /**
+     * Test that the EmergencyStateTracker does perform a DDS switch even though the carrier
+     * supports control-plane fallback if we are roaming and the roaming partner is configured to
+     * use data plane only SUPL.
+     */
+    @Test
+    @SmallTest
+    public void startEmergencyCall_roamingCarrierConfigWhileRoaming_switchDds() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                true /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phones
+        Phone testPhone = setupTestPhoneForEmergencyCall(true /* isRoaming */,
+                true /* isRadioOn */);
+        // Setup voice roaming scenario
+        String testRoamingOperator = "001001";
+        testPhone.getServiceState().setOperatorName("TestTel", "TestTel", testRoamingOperator);
+        String[] roamingPlmns = new String[1];
+        roamingPlmns[0] = testRoamingOperator;
+        CarrierConfigManager cfgManager = (CarrierConfigManager) mContext
+                .getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putStringArray(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_DATA_PLANE_ONLY_ROAMING_PLMN_STRING_ARRAY,
+                roamingPlmns);
+        cfgManager.getConfigForSubId(testPhone.getSubId()).putInt(
+                CarrierConfigManager.Gps.KEY_ES_SUPL_CONTROL_PLANE_SUPPORT_INT,
+                CarrierConfigManager.Gps.SUPL_EMERGENCY_MODE_TYPE_CP_FALLBACK);
+        cfgManager.getConfigForSubId(testPhone.getSubId())
+                .putString(CarrierConfigManager.Gps.KEY_ES_EXTENSION_SEC_STRING, "0");
+
+        CompletableFuture<Integer> unused = emergencyStateTracker.startEmergencyCall(testPhone,
+                TEST_CALL_ID, false);
+
+        // Verify DDS switch
+        verify(mPhoneSwitcher).overrideDefaultDataForEmergency(eq(0) /* phoneId */,
+                eq(0) /* extensionTime */, any());
+    }
+
+    /**
+     * Test that once EmergencyStateTracker handler receives set emergency mode done message it sets
+     * IsInEmergencyCall to true, sets LastEmergencyRegResult and completes future with
+     * DisconnectCause.NOT_DISCONNECTED.
+     */
+    @Test
+    @SmallTest
+    public void setEmergencyModeDone_notifiesListenersAndCompletesFuture() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                true /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phone
+        Phone testPhone = setupTestPhoneForEmergencyCall(true /* isRoaming */,
+                true /* isRadioOn */);
+        // Call startEmergencyCall() to set testPhone
+        CompletableFuture<Integer> future = emergencyStateTracker.startEmergencyCall(testPhone,
+                TEST_CALL_ID, false);
+        // Verify future completes with DisconnectCause.NOT_DISCONNECTED
+        CompletableFuture<Void> unused = future.thenAccept((result) -> {
+            assertEquals((Integer) result, (Integer) DisconnectCause.NOT_DISCONNECTED);
+        });
+        assertFalse(emergencyStateTracker.isInEmergencyCall());
+        Handler handler = emergencyStateTracker.getHandler();
+        Message msg = new Message();
+        EmergencyRegResult regResult = new EmergencyRegResult(0, 0, 0, true, false, 0, 1, "testMcc",
+                "testMnc", "testIso");
+        AsyncResult ar = new AsyncResult(msg, regResult, null);
+        msg.obj = ar;
+
+        msg.what = EmergencyStateTracker.MSG_SET_EMERGENCY_MODE_DONE;
+        handler.handleMessage(msg);
+
+        assertTrue(emergencyStateTracker.isInEmergencyCall());
+        assertTrue(emergencyStateTracker.getEmergencyRegResult().equals(regResult));
+    }
+
+    /**
+     * Test that once EmergencyStateTracker handler receives message to exit emergency mode, it sets
+     * IsInEmergencyCall to false.
+     */
+    @Test
+    @SmallTest
+    public void exitEmergencyModeDone_isInEmergencyCallFalse() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                true /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phone
+        Phone testPhone = setupTestPhoneForEmergencyCall(true /* isRoaming */,
+                true /* isRadioOn */);
+        // Call startEmergencyCall() to set testPhone
+        CompletableFuture<Integer> unused = emergencyStateTracker.startEmergencyCall(testPhone,
+                TEST_CALL_ID, false);
+        Handler handler = emergencyStateTracker.getHandler();
+        Message msg = new Message();
+        EmergencyRegResult regResult = new EmergencyRegResult(0, 0, 0, true, false, 0, 1, "testMcc",
+                "testMnc", "testIso");
+        AsyncResult ar = new AsyncResult(msg, regResult, null);
+        msg.obj = ar;
+        // Send message to set isInEmergencyCall to true
+        msg.what = EmergencyStateTracker.MSG_SET_EMERGENCY_MODE_DONE;
+        handler.handleMessage(msg);
+        assertTrue(emergencyStateTracker.isInEmergencyCall());
+
+        msg.what = EmergencyStateTracker.MSG_EXIT_EMERGENCY_MODE_DONE;
+        handler.handleMessage(msg);
+
+        assertFalse(emergencyStateTracker.isInEmergencyCall());
+    }
+
+    /**
+     * Test that once EmergencyStateTracker ends call, it exits emergency mode.
+     */
+    @Test
+    @SmallTest
+    public void endCall_exitsEmergencyMode() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                true /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phones
+        Phone testPhone = setupTestPhoneForEmergencyCall(true /* isRoaming */,
+                true /* isRadioOn */);
+        // Call startEmergencyCall() to set testPhone
+        CompletableFuture<Integer> unused = emergencyStateTracker.startEmergencyCall(testPhone,
+                TEST_CALL_ID, false);
+
+        emergencyStateTracker.endCall("testId");
+
+        verify(testPhone).exitEmergencyMode(any());
+    }
+
+    /**
+     * Test that onEmergencyTransportChanged sets the new emergency mode.
+     */
+    @Test
+    @SmallTest
+    public void onEmergencyTransportChanged_setsEmergencyMode() {
+        EmergencyStateTracker emergencyStateTracker = setupEmergencyStateTracker(
+                true /* isSuplDdsSwitchRequiredForEmergencyCall */);
+        // Create test Phones
+        Phone testPhone = setupTestPhoneForEmergencyCall(true /* isRoaming */,
+                true /* isRadioOn */);
+        // Call startEmergencyCall() to set testPhone
+        CompletableFuture<Integer> unused = emergencyStateTracker.startEmergencyCall(testPhone,
+                TEST_CALL_ID, false);
+
+        emergencyStateTracker.onEmergencyTransportChanged(MODE_EMERGENCY_WWAN);
+
+        verify(testPhone).setEmergencyMode(eq(MODE_EMERGENCY_WWAN), any());
+    }
+
+    private EmergencyStateTracker setupEmergencyStateTracker(
+            boolean isSuplDdsSwitchRequiredForEmergencyCall) {
+        doReturn(mPhoneSwitcher).when(mPhoneSwitcherProxy).getPhoneSwitcher();
+        return new EmergencyStateTracker(mContext, Looper.getMainLooper(),
+                isSuplDdsSwitchRequiredForEmergencyCall, mPhoneFactoryProxy, mPhoneSwitcherProxy,
+                mTelephonyManagerProxy, mRadioOnHelper);
+    }
+
+    private Phone setupTestPhoneForEmergencyCall(boolean isRoaming, boolean isRadioOn) {
+        Phone testPhone0 = makeTestPhone(0 /* phoneId */, ServiceState.STATE_IN_SERVICE,
+                false /* isEmergencyOnly */);
+        Phone testPhone1 = makeTestPhone(1 /* phoneId */, ServiceState.STATE_OUT_OF_SERVICE,
+                false /* isEmergencyOnly */);
+        List<Phone> phones = new ArrayList<>(2);
+        phones.add(testPhone0);
+        phones.add(testPhone1);
+        doReturn(isRadioOn).when(testPhone0).isRadioOn();
+        doReturn(isRadioOn).when(testPhone1).isRadioOn();
+        Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0);
+        doReturn(2).when(mTelephonyManagerProxy).getPhoneCount();
+        when(mPhoneFactoryProxy.getPhones()).thenReturn(phones.toArray(new Phone[phones.size()]));
+        testPhone0.getServiceState().setRoaming(isRoaming);
+        return testPhone0;
+    }
+
+    private Phone makeTestPhone(int phoneId, int serviceState, boolean isEmergencyOnly) {
+        Phone phone = mock(Phone.class);
+        ServiceState testServiceState = new ServiceState();
+        testServiceState.setState(serviceState);
+        testServiceState.setEmergencyOnly(isEmergencyOnly);
+        when(phone.getContext()).thenReturn(mContext);
+        when(phone.getServiceState()).thenReturn(testServiceState);
+        when(phone.getPhoneId()).thenReturn(phoneId);
+        when(phone.getSubId()).thenReturn(0);
+        when(phone.getServiceStateTracker()).thenReturn(mSST);
+        return phone;
+    }
+}
\ No newline at end of file
diff --git a/tests/telephonytests/src/com/android/internal/telephony/emergency/RadioOnStateListenerTest.java b/tests/telephonytests/src/com/android/internal/telephony/emergency/RadioOnStateListenerTest.java
new file mode 100644
index 0000000..7ba6f01
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/emergency/RadioOnStateListenerTest.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.emergency;
+
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.telephony.ServiceState;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.internal.telephony.CommandsInterface;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.TelephonyTest;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/**
+ * Tests the RadioOnStateListener, which listens to one Phone and waits until its service state
+ * changes to accepting emergency calls or in service. If it can not find a tower to camp onto for
+ * emergency calls, then it will fail after a timeout period.
+ */
+@RunWith(AndroidJUnit4.class)
+public class RadioOnStateListenerTest extends TelephonyTest {
+
+    private static final long TIMEOUT_MS = 1000;
+
+    @Mock Phone mMockPhone;
+    @Mock RadioOnStateListener.Callback mCallback;
+    @Mock CommandsInterface mMockCi;
+    RadioOnStateListener mListener;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(getClass().getSimpleName());
+        MockitoAnnotations.initMocks(this);
+        mListener = new RadioOnStateListener();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mListener.setTimeBetweenRetriesMillis(5000);
+        mListener.setMaxNumRetries(5);
+        mListener.getHandler().removeCallbacksAndMessages(null);
+        // Wait for the queue to clear...
+        waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS /* ms timeout */);
+        mListener = null;
+        super.tearDown();
+    }
+
+    /**
+     * Ensure that we successfully register for the ServiceState changed messages in Telephony.
+     */
+    @Test
+    public void testRegisterForCallback() {
+        mMockPhone.mCi = mMockCi;
+        mListener.waitForRadioOn(mMockPhone, mCallback, false, false);
+
+        waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+
+        verify(mMockPhone).unregisterForServiceStateChanged(any(Handler.class));
+        verify(mMockPhone).registerForServiceStateChanged(any(Handler.class),
+                eq(RadioOnStateListener.MSG_SERVICE_STATE_CHANGED), isNull());
+
+        verify(mMockCi).registerForOffOrNotAvailable(any(Handler.class),
+                eq(RadioOnStateListener.MSG_RADIO_OFF_OR_NOT_AVAILABLE), isNull());
+    }
+
+    /**
+     * {@link RadioOnStateListener.Callback#isOkToCall(Phone, int)} returns true, so we are
+     * expecting {@link RadioOnStateListener.Callback#onComplete(RadioOnStateListener, boolean)} to
+     * return true.
+     */
+    @Test
+    public void testPhoneChangeState_OkToCallTrue() {
+        ServiceState state = new ServiceState();
+        state.setState(ServiceState.STATE_IN_SERVICE);
+        when(mMockPhone.getServiceState()).thenReturn(state);
+        when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE);
+        when(mCallback.isOkToCall(eq(mMockPhone), anyInt())).thenReturn(true);
+        mMockPhone.mCi = mMockCi;
+        mListener.waitForRadioOn(mMockPhone, mCallback, false, false);
+        waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+
+        mListener.getHandler().obtainMessage(RadioOnStateListener.MSG_SERVICE_STATE_CHANGED,
+                new AsyncResult(null, state, null)).sendToTarget();
+
+        waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+        verify(mCallback).onComplete(eq(mListener), eq(true));
+    }
+
+    /**
+     * We never receive a
+     * {@link RadioOnStateListener.Callback#onComplete(RadioOnStateListener, boolean)} because
+     * {@link RadioOnStateListener.Callback#isOkToCall(Phone, int)} returns false.
+     */
+    @Test
+    public void testPhoneChangeState_NoOkToCall_Timeout() {
+        ServiceState state = new ServiceState();
+        state.setState(ServiceState.STATE_OUT_OF_SERVICE);
+        when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE);
+        when(mCallback.isOkToCall(eq(mMockPhone), anyInt())).thenReturn(false);
+        when(mMockPhone.getServiceState()).thenReturn(state);
+        mMockPhone.mCi = mMockCi;
+        mListener.waitForRadioOn(mMockPhone, mCallback, false, false);
+        waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+
+        mListener.getHandler().obtainMessage(RadioOnStateListener.MSG_SERVICE_STATE_CHANGED,
+                new AsyncResult(null, state, null)).sendToTarget();
+
+        waitForHandlerAction(mListener.getHandler(), TIMEOUT_MS);
+        verify(mCallback, never()).onComplete(any(RadioOnStateListener.class), anyBoolean());
+    }
+
+    /**
+     * Tests {@link RadioOnStateListener.Callback#isOkToCall(Phone, int)} returning false and
+     * hitting the max number of retries. This should result in
+     * {@link RadioOnStateListener.Callback#onComplete(RadioOnStateListener, boolean)} returning
+     * false.
+     */
+    @Test
+    public void testTimeout_RetryFailure() {
+        ServiceState state = new ServiceState();
+        state.setState(ServiceState.STATE_POWER_OFF);
+        when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE);
+        when(mMockPhone.getServiceState()).thenReturn(state);
+        when(mCallback.isOkToCall(eq(mMockPhone), anyInt())).thenReturn(false);
+        mListener.setTimeBetweenRetriesMillis(0/* ms */);
+        mListener.setMaxNumRetries(2);
+
+        // Wait for the timer to expire and check state manually in onRetryTimeout
+        mMockPhone.mCi = mMockCi;
+        mListener.waitForRadioOn(mMockPhone, mCallback, false, false);
+        waitForDelayedHandlerAction(mListener.getHandler(), TIMEOUT_MS /* delay */, TIMEOUT_MS);
+
+        verify(mCallback).onComplete(eq(mListener), eq(false));
+        verify(mMockPhone, times(2)).setRadioPower(eq(true), eq(false), eq(false), eq(false));
+    }
+
+    @Test
+    public void testTimeout_RetryFailure_ForEmergency() {
+        ServiceState state = new ServiceState();
+        state.setState(ServiceState.STATE_POWER_OFF);
+        when(mMockPhone.getState()).thenReturn(PhoneConstants.State.IDLE);
+        when(mMockPhone.getServiceState()).thenReturn(state);
+        when(mCallback.isOkToCall(eq(mMockPhone), anyInt())).thenReturn(false);
+        mListener.setTimeBetweenRetriesMillis(0/* ms */);
+        mListener.setMaxNumRetries(2);
+
+        // Wait for the timer to expire and check state manually in onRetryTimeout
+        mMockPhone.mCi = mMockCi;
+        mListener.waitForRadioOn(mMockPhone, mCallback, true, true);
+        waitForDelayedHandlerAction(mListener.getHandler(), TIMEOUT_MS /* delay */, TIMEOUT_MS );
+
+        verify(mCallback).onComplete(eq(mListener), eq(false));
+        verify(mMockPhone, times(2)).setRadioPower(eq(true), eq(true), eq(true), eq(false));
+    }
+}
\ No newline at end of file
diff --git a/tests/telephonytests/src/com/android/internal/telephony/euicc/EuiccControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/euicc/EuiccControllerTest.java
index 491c690..62b6653 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/euicc/EuiccControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/euicc/EuiccControllerTest.java
@@ -128,7 +128,6 @@
     private static final int SUBSCRIPTION_ID = 12345;
     private static final String ICC_ID = "54321";
     private static final int CARD_ID = 25;
-    private static final int PORT_INDEX = 0;
 
     // Mocked classes
     private EuiccConnector mMockConnector;
@@ -319,8 +318,8 @@
                 SUBSCRIPTION, false /* complete */, null /* result */);
         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
                 0 /* detailedCode */);
-        verify(mMockConnector).getDownloadableSubscriptionMetadata(anyInt(), any(), anyBoolean(),
-                any());
+        verify(mMockConnector).getDownloadableSubscriptionMetadata(anyInt(), anyInt(), any(),
+                anyBoolean(), anyBoolean(), any());
     }
 
     @Test
@@ -787,6 +786,70 @@
     }
 
     @Test
+    public void testGetResolvedPortIndexForSubscriptionSwitchWithOutMEP() throws Exception {
+        setUpUiccSlotData();
+        assertEquals(TelephonyManager.DEFAULT_PORT_INDEX,
+                mController.getResolvedPortIndexForSubscriptionSwitch(CARD_ID));
+    }
+
+    @Test
+    public void testGetResolvedPortIndexForSubscriptionSwitchWithMEP() throws Exception {
+        setUpUiccSlotDataWithMEP();
+        when(mUiccSlot.getPortList()).thenReturn(new int[]{0, 1});
+        when(mUiccSlot.isPortActive(TelephonyManager.DEFAULT_PORT_INDEX)).thenReturn(false);
+        when(mUiccSlot.isPortActive(1)).thenReturn(true);
+        when(mTelephonyManager.getActiveModemCount()).thenReturn(2);
+        assertEquals(1, mController.getResolvedPortIndexForSubscriptionSwitch(CARD_ID));
+    }
+
+    @Test
+    public void testGetResolvedPortIndexForSubscriptionSwitchWithUiccSlotNull() throws Exception {
+        assertEquals(TelephonyManager.DEFAULT_PORT_INDEX,
+                mController.getResolvedPortIndexForSubscriptionSwitch(CARD_ID));
+    }
+
+    @Test
+    public void testGetResolvedPortIndexForSubscriptionSwitchWithPsimActiveAndSS()
+            throws Exception {
+        when(mUiccController.getUiccSlot(anyInt())).thenReturn(mUiccSlot);
+        when(mUiccSlot.isRemovable()).thenReturn(true);
+        when(mUiccSlot.isEuicc()).thenReturn(false);
+        when(mUiccSlot.isActive()).thenReturn(true);
+        when(mTelephonyManager.getActiveModemCount()).thenReturn(1);
+        assertEquals(TelephonyManager.DEFAULT_PORT_INDEX,
+                mController.getResolvedPortIndexForSubscriptionSwitch(CARD_ID));
+    }
+
+    @Test
+    public void testGetResolvedPortIndexForSubscriptionSwitchWithPsimInActiveAndSS()
+            throws Exception {
+        setUpUiccSlotDataWithMEP();
+        when(mUiccSlot.getPortList()).thenReturn(new int[]{0});
+        when(mUiccSlot.isPortActive(TelephonyManager.DEFAULT_PORT_INDEX)).thenReturn(true);
+        when(mTelephonyManager.getActiveModemCount()).thenReturn(1);
+        assertEquals(TelephonyManager.DEFAULT_PORT_INDEX,
+                mController.getResolvedPortIndexForSubscriptionSwitch(CARD_ID));
+    }
+
+    @Test
+    public void testgetResolvedPortIndexForDisableSubscriptionForNoActiveSubscription()
+            throws Exception {
+        when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(null);
+        assertEquals(-1,
+                mController.getResolvedPortIndexForDisableSubscription(
+                        CARD_ID, PACKAGE_NAME, true));
+    }
+
+    @Test
+    public void testgetResolvedPortIndexForDisableSubscriptionForActiveSubscriptions()
+            throws Exception {
+        setActiveSubscriptionInfoInMEPMode();
+        assertEquals(1,
+                mController.getResolvedPortIndexForDisableSubscription(
+                        CARD_ID, PACKAGE_NAME, false));
+    }
+
+    @Test
     public void testDeleteSubscription_noPrivileges() throws Exception {
         setHasWriteEmbeddedPermission(false);
         prepareOperationSubscription(false /* hasPrivileges */);
@@ -1233,6 +1296,11 @@
         when(mUiccSlot.isMultipleEnabledProfileSupported()).thenReturn(false);
     }
 
+    private void setUpUiccSlotDataWithMEP() {
+        when(mUiccController.getUiccSlot(anyInt())).thenReturn(mUiccSlot);
+        when(mUiccSlot.isMultipleEnabledProfileSupported()).thenReturn(true);
+    }
+
     private void setGetEidPermissions(
             boolean hasPhoneStatePrivileged, boolean hasCarrierPrivileges) throws Exception {
         doReturn(hasPhoneStatePrivileged
@@ -1258,10 +1326,17 @@
 
     private void setHasCarrierPrivilegesOnActiveSubscription(boolean hasPrivileges)
             throws Exception {
-        SubscriptionInfo subInfo = new SubscriptionInfo(
-                0, "", 0, "", "", 0, 0, "", 0, null, "", "", "", true /* isEmbedded */,
-                hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null, "", CARD_ID,
-                false, null, false, 0, 0, 0, null, null, true, 0);
+        SubscriptionInfo.Builder builder = new SubscriptionInfo.Builder()
+                .setSimSlotIndex(0)
+                .setPortIndex(mTelephonyManager.DEFAULT_PORT_INDEX)
+                .setDisplayNameSource(SubscriptionManager.NAME_SOURCE_CARRIER_ID)
+                .setEmbedded(true);
+        if (hasPrivileges) {
+            builder.setNativeAccessRules(new UiccAccessRule[] { ACCESS_RULE });
+        }
+        builder.setCardId(CARD_ID);
+        SubscriptionInfo subInfo = builder.build();
+
         when(mSubscriptionManager.canManageSubscription(subInfo, PACKAGE_NAME)).thenReturn(
                 hasPrivileges);
         when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(
@@ -1287,14 +1362,18 @@
         cardInfos.add(cardInfo2);
         when(mTelephonyManager.getUiccCardsInfo()).thenReturn(cardInfos);
 
-        SubscriptionInfo subInfo1 = new SubscriptionInfo(
-                0, "", 0, "", "", 0, 0, "", 0, null, "", "", "", true /* isEmbedded */,
-                hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null, "", CARD_ID,
-                false, null, false, 0, 0, 0, null, null, true, 0);
-        SubscriptionInfo subInfo2 = new SubscriptionInfo(
-                0, "", 0, "", "", 0, 0, "", 0, null, "", "", "", true /* isEmbedded */,
-                hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null, "",
-                1 /* cardId */, false, null, false, 0, 0, 0, null, null, true, 0);
+        SubscriptionInfo subInfo1 = new SubscriptionInfo.Builder()
+                .setNativeAccessRules(hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null)
+                .setEmbedded(true)
+                .setCardId(CARD_ID)
+                .setPortIndex(mTelephonyManager.DEFAULT_PORT_INDEX)
+                .build();
+        SubscriptionInfo subInfo2 = new SubscriptionInfo.Builder()
+                .setNativeAccessRules(hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null)
+                .setEmbedded(true)
+                .setCardId(2)
+                .setPortIndex(TelephonyManager.DEFAULT_PORT_INDEX)
+                .build();
         when(mSubscriptionManager.canManageSubscription(subInfo1, PACKAGE_NAME)).thenReturn(
                 hasPrivileges);
         when(mSubscriptionManager.canManageSubscription(subInfo2, PACKAGE_NAME)).thenReturn(
@@ -1303,11 +1382,34 @@
         when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(subInfos);
     }
 
+    private void setActiveSubscriptionInfoInMEPMode()
+            throws Exception {
+        SubscriptionInfo subInfo1 = new SubscriptionInfo.Builder()
+                .setEmbedded(true)
+                .setCardId(CARD_ID)
+                .setPortIndex(TelephonyManager.DEFAULT_PORT_INDEX)
+                .build();
+        SubscriptionInfo subInfo2 = new SubscriptionInfo.Builder()
+                .setEmbedded(true)
+                .setCardId(CARD_ID)
+                .setPortIndex(1)
+                .build();
+        when(mSubscriptionManager.canManageSubscription(subInfo1, PACKAGE_NAME)).thenReturn(
+                false);
+        when(mSubscriptionManager.canManageSubscription(subInfo2, PACKAGE_NAME)).thenReturn(
+                true);
+        ArrayList<SubscriptionInfo> subInfos = new ArrayList<>(Arrays.asList(subInfo1, subInfo2));
+        when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(subInfos);
+    }
+
     private void prepareOperationSubscription(boolean hasPrivileges) throws Exception {
-        SubscriptionInfo subInfo = new SubscriptionInfo(
-                SUBSCRIPTION_ID, ICC_ID, 0, "", "", 0, 0, "", 0, null, "0", "0", "",
-                true /* isEmbedded */, hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null,
-                null);
+        SubscriptionInfo subInfo = new SubscriptionInfo.Builder()
+                .setId(SUBSCRIPTION_ID)
+                .setIccId(ICC_ID)
+                .setNativeAccessRules(hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null)
+                .setEmbedded(true)
+                .setCardId(CARD_ID)
+                .build();
         when(mSubscriptionManager.canManageSubscription(subInfo, PACKAGE_NAME)).thenReturn(
                 hasPrivileges);
         when(mSubscriptionManager.getAvailableSubscriptionInfoList()).thenReturn(
@@ -1390,7 +1492,7 @@
             @Override
             public Void answer(InvocationOnMock invocation) throws Exception {
                 EuiccConnector.GetMetadataCommandCallback cb = invocation
-                        .getArgument(3 /* resultCallback */);
+                        .getArgument(5 /* resultCallback */);
                 if (complete) {
                     cb.onGetMetadataComplete(CARD_ID, result);
                 } else {
@@ -1398,8 +1500,8 @@
                 }
                 return null;
             }
-        }).when(mMockConnector).getDownloadableSubscriptionMetadata(anyInt(), any(), anyBoolean(),
-                any());
+        }).when(mMockConnector).getDownloadableSubscriptionMetadata(anyInt(), anyInt(), any(),
+                anyBoolean(), anyBoolean(), any());
     }
 
     private void callGetDownloadableSubscriptionMetadata(DownloadableSubscription subscription,
diff --git a/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmInboundSmsHandlerTest.java b/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmInboundSmsHandlerTest.java
index a847a24..7a51640 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmInboundSmsHandlerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmInboundSmsHandlerTest.java
@@ -55,9 +55,9 @@
 import android.testing.AndroidTestingRunner;
 import android.testing.TestableLooper;
 
-import androidx.test.filters.FlakyTest;
 import androidx.test.filters.MediumTest;
 
+import com.android.ims.ImsManager;
 import com.android.internal.telephony.FakeSmsContentProvider;
 import com.android.internal.telephony.InboundSmsHandler;
 import com.android.internal.telephony.InboundSmsTracker;
@@ -192,9 +192,9 @@
                 InboundSmsHandler.SOURCE_NOT_INJECTED);
         doReturn(mInboundSmsTracker).when(mTelephonyComponentFactory)
                 .makeInboundSmsTracker(any(Context.class), nullable(byte[].class), anyLong(),
-                anyInt(), anyBoolean(),
-                anyBoolean(), nullable(String.class), nullable(String.class),
-                nullable(String.class), anyBoolean(), anyInt(), anyInt());
+                        anyInt(), anyBoolean(),
+                        anyBoolean(), nullable(String.class), nullable(String.class),
+                        nullable(String.class), anyBoolean(), anyInt(), anyInt());
 
         createInboundSmsTrackerMultiSim();
 
@@ -203,12 +203,11 @@
                 Telephony.Sms.CONTENT_URI.getAuthority(), mContentProvider);
 
         mGsmInboundSmsHandler = GsmInboundSmsHandler.makeInboundSmsHandler(mContext,
-                mSmsStorageMonitor, mPhone);
+                mSmsStorageMonitor, mPhone, mTestableLooper.getLooper());
         mSmsFilters = new ArrayList<>();
         mSmsFilters.add(mSmsFilter);
         mSmsFilters.add(mSmsFilter2);
         mGsmInboundSmsHandler.setSmsFiltersForTesting(mSmsFilters);
-        monitorTestableLooper(new TestableLooper(mGsmInboundSmsHandler.getHandler().getLooper()));
 
         doReturn(mGsmInboundSmsHandler).when(mPhone).getInboundSmsHandler(false);
         doReturn(mCdmaInboundSmsHandler).when(mPhone).getInboundSmsHandler(true);
@@ -310,7 +309,6 @@
         processAllMessages();
     }
 
-    @FlakyTest
     @Test
     @MediumTest
     public void testNewSms() {
@@ -330,7 +328,6 @@
         verifySmsFiltersInvoked(times(1));
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testNewSmsFromBlockedNumber_noBroadcastsSent() {
@@ -348,7 +345,6 @@
         verifySmsFiltersInvoked(times(1));
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testNewSmsWithUserLocked_notificationShown() {
@@ -375,7 +371,6 @@
                 any(Notification.class));
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testNewSmsFromBlockedNumberWithUserLocked_noNotificationShown() {
@@ -405,7 +400,6 @@
                 any(Notification.class));
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testNewSms_filterInvoked_noBroadcastsSent() {
@@ -431,7 +425,6 @@
                 anyBoolean(), anyBoolean(), Mockito.<List<InboundSmsHandler.SmsFilter>>any());
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testNewSms_filterChaining_noBroadcastsSent() {
@@ -483,7 +476,6 @@
         assertEquals("IdleState", getCurrentState().getName());
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testClass0Sms() {
@@ -515,7 +507,6 @@
         verifySmsFiltersInvoked(times(1));
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testBroadcastSms() {
@@ -556,7 +547,6 @@
         verifySmsFiltersInvoked(times(2));
     }
 
-    @FlakyTest
     @Test
     @MediumTest
     public void testInjectSms() {
@@ -617,7 +607,6 @@
                 InboundSmsHandler.SOURCE_NOT_INJECTED);
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testMultiPartSmsWithIncompleteWAP() {
@@ -678,12 +667,13 @@
         assertEquals("IdleState", getCurrentState().getName());
         // verify there are three segments in the db and only one of them is not marked as deleted.
         assertEquals(3, mContentProvider.getNumRows());
-        assertEquals(1, mContentProvider.query(sRawUri, null, "deleted=0", null, null).getCount());
 
+        Cursor c = mContentProvider.query(sRawUri, null, "deleted=0", null, null);
+        assertEquals(1, c.getCount());
         verifySmsFiltersInvoked(times(1));
+        c.close();
     }
 
-    @FlakyTest
     @Test
     @MediumTest
     public void testMultiPartSms() {
@@ -757,7 +747,6 @@
         assertEquals("IdleState", getCurrentState().getName());
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testMultiPartIncompleteSms() {
@@ -820,9 +809,9 @@
         // State machine should go back to idle
         assertEquals("IdleState", getCurrentState().getName());
         verifySmsFiltersInvoked(never());
+        c.close();
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testMultiPartSmsWithInvalidSeqNumber() {
@@ -882,7 +871,6 @@
         verifySmsFiltersInvoked(never());
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testMultipartSmsFromBlockedNumber_noBroadcastsSent() {
@@ -921,7 +909,6 @@
         verifySmsFiltersInvoked(times(1));
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testMultipartSmsFromBlockedEmail_noBroadcastsSent() {
@@ -977,7 +964,6 @@
         verifySmsFiltersInvoked(times(1));
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testMultipartSms_filterInvoked_noBroadcastsSent() {
@@ -1027,7 +1013,6 @@
                 anyBoolean(), anyBoolean(), Mockito.<List<InboundSmsHandler.SmsFilter>>any());
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testBroadcastUndeliveredUserLocked() throws Exception {
@@ -1085,7 +1070,6 @@
         verifySmsFiltersInvoked(times(1));
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testBroadcastUndeliveredUserUnlocked() throws Exception {
@@ -1123,7 +1107,6 @@
         verifySmsFiltersInvoked(times(1));
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testBroadcastUndeliveredDeleted() throws Exception {
@@ -1164,7 +1147,6 @@
         verifySmsFiltersInvoked(never());
     }
 
-    @FlakyTest
     @Test
     @MediumTest
     public void testBroadcastUndeliveredMultiPart() throws Exception {
@@ -1180,7 +1162,7 @@
         //return InboundSmsTracker objects corresponding to the 2 parts
         doReturn(mInboundSmsTrackerPart1).doReturn(mInboundSmsTrackerPart2).
                 when(mTelephonyComponentFactory).makeInboundSmsTracker(any(Context.class),
-                any(Cursor.class), anyBoolean());
+                        any(Cursor.class), anyBoolean());
 
         SmsBroadcastUndelivered.initialize(mContext, mGsmInboundSmsHandler, mCdmaInboundSmsHandler);
         // wait for ScanRawTableThread
@@ -1191,7 +1173,6 @@
         verifySmsFiltersInvoked(times(1));
     }
 
-    @FlakyTest // temporarily disabled, see b/182498318
     @Test
     @MediumTest
     public void testBroadcastUndeliveredMultiSim() throws Exception {
@@ -1251,4 +1232,12 @@
         assertEquals("IdleState", getCurrentState().getName());
 
     }
+
+    @Test
+    public void testSetImsManager() {
+        ImsManager imsManager = Mockito.mock(ImsManager.class);
+        transitionFromStartupToIdle();
+        assertTrue(mGsmInboundSmsHandler.setImsManager(imsManager));
+    }
 }
+
diff --git a/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmMmiCodeTest.java b/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmMmiCodeTest.java
index aaee7b9..1c1ca0f 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmMmiCodeTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmMmiCodeTest.java
@@ -16,10 +16,10 @@
 
 package com.android.internal.telephony.gsm;
 
-import static junit.framework.Assert.fail;
-
 import static com.google.common.truth.Truth.assertThat;
 
+import static junit.framework.Assert.fail;
+
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.Mockito.doReturn;
@@ -42,6 +42,7 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import java.util.ArrayList;
 import java.util.concurrent.Executor;
 
 /**
@@ -108,6 +109,209 @@
         }
     }
 
+    // The main purpose of this test is to list all the valid use cases of getControlStrings().
+    @Test
+    public void testGetControlStrings() {
+        // Test if controlStrings list is empty when inputs are null
+        ArrayList<String> controlStrings = GsmMmiCode.getControlStrings(null,
+                null);
+        assertThat(controlStrings).isEmpty();
+
+        // Test control strings of Call Forwarding Unconditional
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_CFU);
+        assertThat(controlStrings).containsExactly("*#21", "*#002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_REGISTRATION,
+                SsData.ServiceType.SS_CFU);
+        assertThat(controlStrings).containsExactly("**21", "**002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_CFU);
+        assertThat(controlStrings).containsExactly("#21", "#002");
+
+        // Test control strings of Call Forwarding Busy
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_CF_BUSY);
+        assertThat(controlStrings).containsExactly("*#67", "*#004", "*#002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_REGISTRATION,
+                SsData.ServiceType.SS_CF_BUSY);
+        assertThat(controlStrings).containsExactly("**67", "**004", "**002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_CF_BUSY);
+        assertThat(controlStrings).containsExactly("#67", "#004", "#002");
+
+        // Test control strings of Call Forwarding No Reply
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_CF_NO_REPLY);
+        assertThat(controlStrings).containsExactly("*#61", "*#004", "*#002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_REGISTRATION,
+                SsData.ServiceType.SS_CF_NO_REPLY);
+        assertThat(controlStrings).containsExactly("**61", "**004", "**002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_CF_NO_REPLY);
+        assertThat(controlStrings).containsExactly("#61", "#004", "#002");
+
+        // Test control strings of Call Forwarding Not Reachable
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_CF_NOT_REACHABLE);
+        assertThat(controlStrings).containsExactly("*#62", "*#004", "*#002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_REGISTRATION,
+                SsData.ServiceType.SS_CF_NOT_REACHABLE);
+        assertThat(controlStrings).containsExactly("**62", "**004", "**002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_CF_NOT_REACHABLE);
+        assertThat(controlStrings).containsExactly("#62", "#004", "#002");
+
+        // Test control strings of Call Forwarding All
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_CF_ALL);
+        assertThat(controlStrings).containsExactly("*#002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_REGISTRATION,
+                SsData.ServiceType.SS_CF_ALL);
+        assertThat(controlStrings).containsExactly("**002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_CF_ALL);
+        assertThat(controlStrings).containsExactly("#002");
+
+        // Test control strings of Call Forwarding ALl Conditional
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_CF_ALL_CONDITIONAL);
+        assertThat(controlStrings).containsExactly("*#004", "*#002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_REGISTRATION,
+                SsData.ServiceType.SS_CF_ALL_CONDITIONAL);
+        assertThat(controlStrings).containsExactly("**004", "**002");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_CF_ALL_CONDITIONAL);
+        assertThat(controlStrings).containsExactly("#004", "#002");
+
+        // Test control strings of CLIP
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_CLIP);
+        assertThat(controlStrings).containsExactly("*#30");
+
+        // Test control strings of CLIR
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_CLIR);
+        assertThat(controlStrings).containsExactly("*#31");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_ACTIVATION,
+                SsData.ServiceType.SS_CLIR);
+        assertThat(controlStrings).containsExactly("*31");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_CLIR);
+        assertThat(controlStrings).containsExactly("#31");
+
+        // Test control strings of Call Waiting
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_WAIT);
+        assertThat(controlStrings).containsExactly("*#43");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_ACTIVATION,
+                SsData.ServiceType.SS_WAIT);
+        assertThat(controlStrings).containsExactly("*43");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_WAIT);
+        assertThat(controlStrings).containsExactly("#43");
+
+        // Test control strings of BAOC
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_BAOC);
+        assertThat(controlStrings).containsExactly("*#33", "*#330", "*#333");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_ACTIVATION,
+                SsData.ServiceType.SS_BAOC);
+        assertThat(controlStrings).containsExactly("*33", "*330", "*333");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_BAOC);
+        assertThat(controlStrings).containsExactly("#33", "#330", "#333");
+
+        // Test control strings of BAOIC
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_BAOIC);
+        assertThat(controlStrings).containsExactly("*#331", "*#330", "*#333");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_ACTIVATION,
+                SsData.ServiceType.SS_BAOIC);
+        assertThat(controlStrings).containsExactly("*331", "*330", "*333");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_BAOIC);
+        assertThat(controlStrings).containsExactly("#331", "#330", "#333");
+
+        // Test control strings of BAOICxH
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_BAOIC_EXC_HOME);
+        assertThat(controlStrings).containsExactly("*#332", "*#330", "*#333");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_ACTIVATION,
+                SsData.ServiceType.SS_BAOIC_EXC_HOME);
+        assertThat(controlStrings).containsExactly("*332", "*330", "*333");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_BAOIC_EXC_HOME);
+        assertThat(controlStrings).containsExactly("#332", "#330", "#333");
+
+        // Test control strings of BAIC
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_BAIC);
+        assertThat(controlStrings).containsExactly("*#35", "*#330", "*#353");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_ACTIVATION,
+                SsData.ServiceType.SS_BAIC);
+        assertThat(controlStrings).containsExactly("*35", "*330", "*353");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_BAIC);
+        assertThat(controlStrings).containsExactly("#35", "#330", "#353");
+
+        // Test control strings of BAICr
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_BAIC_ROAMING);
+        assertThat(controlStrings).containsExactly("*#351", "*#330", "*#353");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_ACTIVATION,
+                SsData.ServiceType.SS_BAIC_ROAMING);
+        assertThat(controlStrings).containsExactly("*351", "*330", "*353");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_BAIC_ROAMING);
+        assertThat(controlStrings).containsExactly("#351", "#330", "#353");
+
+        // Test control strings of BA_ALL
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_ALL_BARRING);
+        assertThat(controlStrings).containsExactly("*#330");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_ACTIVATION,
+                SsData.ServiceType.SS_ALL_BARRING);
+        assertThat(controlStrings).containsExactly("*330");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_ALL_BARRING);
+        assertThat(controlStrings).containsExactly( "#330");
+
+        // Test control strings of BA_MO
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_OUTGOING_BARRING);
+        assertThat(controlStrings).containsExactly("*#333", "*#330");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_ACTIVATION,
+                SsData.ServiceType.SS_OUTGOING_BARRING);
+        assertThat(controlStrings).containsExactly("*333", "*330");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_OUTGOING_BARRING);
+        assertThat(controlStrings).containsExactly( "#333", "#330");
+
+        // Test control strings of BA_MT
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_INTERROGATION,
+                SsData.ServiceType.SS_INCOMING_BARRING);
+        assertThat(controlStrings).containsExactly("*#353", "*#330");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_ACTIVATION,
+                SsData.ServiceType.SS_INCOMING_BARRING);
+        assertThat(controlStrings).containsExactly("*353", "*330");
+        controlStrings = GsmMmiCode.getControlStrings(SsData.RequestType.SS_DEACTIVATION,
+                SsData.ServiceType.SS_INCOMING_BARRING);
+        assertThat(controlStrings).containsExactly( "#353", "#330");
+    }
+
+    @Test
+    public void testGetControlStringsForPwd() {
+        // Test if controlStrings list is empty when inputs are null
+        ArrayList<String> controlStrings = GsmMmiCode.getControlStringsForPwd(null,
+                null);
+        assertThat(controlStrings).isEmpty();
+
+        // Test control strings of Call Barring Change Password
+        controlStrings = GsmMmiCode.getControlStringsForPwd(
+                SsData.RequestType.SS_REGISTRATION, SsData.ServiceType.SS_ALL_BARRING);
+        assertThat(controlStrings).containsExactly("**03*330");
+    }
+
     @Test
     public void testOperationNotSupported() {
         // Contrived; this is just to get a simple instance of the class.
diff --git a/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmSmsDispatcherTest.java b/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmSmsDispatcherTest.java
index 19cacf0..4bf5415 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmSmsDispatcherTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmSmsDispatcherTest.java
@@ -66,9 +66,12 @@
 import com.android.internal.telephony.ISub;
 import com.android.internal.telephony.SMSDispatcher;
 import com.android.internal.telephony.SmsDispatchersController;
+import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.TelephonyTest;
 import com.android.internal.telephony.TelephonyTestUtils;
 import com.android.internal.telephony.TestApplication;
+import com.android.internal.telephony.uicc.IccUtils;
+import com.android.internal.telephony.uicc.IsimUiccRecords;
 
 import org.junit.After;
 import org.junit.Before;
@@ -228,7 +231,8 @@
         restoreInstance(Singleton.class, "mInstance", mIActivityManagerSingleton);
         restoreInstance(ActivityManager.class, "IActivityManagerSingleton", null);
         Context realContext = TestApplication.getAppContext();
-        realContext.registerReceiver(mTestReceiver, new IntentFilter(TEST_INTENT));
+        realContext.registerReceiver(mTestReceiver, new IntentFilter(TEST_INTENT),
+                Context.RECEIVER_EXPORTED);
     }
 
     @Test
@@ -373,6 +377,7 @@
 
     @Test
     @SmallTest
+    @Ignore("b/256282780")
     public void testSendSmsByCarrierApp() throws Exception {
         mockCarrierApp();
         mockCarrierAppStubResults(CarrierMessagingService.SEND_STATUS_OK,
@@ -447,6 +452,7 @@
 
     @Test
     @SmallTest
+    @Ignore("b/256282780")
     public void testSendMultipartSmsByCarrierApp() throws Exception {
         mockCarrierApp();
         mockCarrierAppStubResults(CarrierMessagingService.SEND_STATUS_OK,
@@ -503,4 +509,74 @@
         verify(mSimulatedCommandsVerifier).sendSMS(anyString(), anyString(),
                 any(Message.class));
     }
+
+
+    @Test
+    public void testSendTextWithMessageRef() throws Exception {
+        int messageRef = mGsmSmsDispatcher.nextMessageRef() + 1;
+        mGsmSmsDispatcher.sendText("111", "222" /*scAddr*/, TAG,
+                null, null, null, null, false, -1, false, -1, false, 0L);
+
+        ArgumentCaptor<String> pduCaptor = ArgumentCaptor.forClass(String.class);
+        verify(mSimulatedCommandsVerifier).sendSMS(anyString(), pduCaptor.capture(),
+                any(Message.class));
+        byte[] pdu = IccUtils.hexStringToBytes(pduCaptor.getValue());
+        assertEquals(messageRef, pdu[1]);
+    }
+
+    @Test
+    public void testSendMultipartWithMessageRef() throws Exception {
+        ArrayList<String> parts = new ArrayList<>();
+        parts.add("segment1");
+        parts.add("segment2");
+        parts.add("segment3");
+        int messageRef = mGsmSmsDispatcher.nextMessageRef() + parts.size();
+        mGsmSmsDispatcher.sendMultipartText("6501002000" /*destAddr*/, "222" /*scAddr*/, parts,
+                null, null, null, null, false, -1, false, -1, 0L);
+        waitForMs(150);
+        ArgumentCaptor<String> pduCaptor = ArgumentCaptor.forClass(String.class);
+
+        verify(mSimulatedCommandsVerifier, times(parts.size() - 1)).sendSMSExpectMore(anyString(),
+                anyString(),
+                any(Message.class));
+        verify(mSimulatedCommandsVerifier).sendSMS(anyString(), pduCaptor.capture(),
+                any(Message.class));
+        byte[] pdu = IccUtils.hexStringToBytes(pduCaptor.getValue());
+        assertEquals(messageRef, pdu[1]);
+    }
+
+    @Test
+    public void testSendTextWithMessageRefNegativeBoundaryCondition() throws Exception {
+        mIsimUiccRecords = new IsimUiccRecords(mUiccCardApplication3gpp, mContext,
+                mSimulatedCommands);
+        doReturn(mIsimUiccRecords).when(mPhone).getIccRecords();
+        Message msg = mGsmSmsDispatcher.obtainMessage(17);
+        mPhone.getIccRecords().setSmssTpmrValue(-1, msg);
+        SubscriptionController.getInstance().updateMessageRef(mPhone.getSubId(), -1);
+        mGsmSmsDispatcher.sendText("111", "222" /*scAddr*/, TAG,
+                null, null, null, null, false, -1, false, -1, false, 0L);
+
+        ArgumentCaptor<String> pduCaptor = ArgumentCaptor.forClass(String.class);
+        verify(mSimulatedCommandsVerifier).sendSMS(anyString(), pduCaptor.capture(),
+                any(Message.class));
+        byte[] pdu = IccUtils.hexStringToBytes(pduCaptor.getValue());
+        assertEquals(0, pdu[1]);
+    }
+
+    @Test
+    public void testSendTextWithMessageRefMaxBoundaryCondition() throws Exception {
+        mIsimUiccRecords = new IsimUiccRecords(mUiccCardApplication3gpp, mContext,
+                mSimulatedCommands);
+        doReturn(mIsimUiccRecords).when(mPhone).getIccRecords();
+        Message msg = mGsmSmsDispatcher.obtainMessage(17);
+        mPhone.getIccRecords().setSmssTpmrValue(255, msg);
+        mGsmSmsDispatcher.sendText("111", "222" /*scAddr*/, TAG,
+                null, null, null, null, false, -1, false, -1, false, 0L);
+
+        ArgumentCaptor<String> pduCaptor = ArgumentCaptor.forClass(String.class);
+        verify(mSimulatedCommandsVerifier).sendSMS(anyString(), pduCaptor.capture(),
+                any(Message.class));
+        byte[] pdu = IccUtils.hexStringToBytes(pduCaptor.getValue());
+        assertEquals(0, pdu[1]);
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/gsm/UsimDataDownloadHandlerTest.java b/tests/telephonytests/src/com/android/internal/telephony/gsm/UsimDataDownloadHandlerTest.java
new file mode 100644
index 0000000..c6041c8
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/gsm/UsimDataDownloadHandlerTest.java
@@ -0,0 +1,403 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.gsm;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+
+import android.content.res.Resources;
+import android.os.AsyncResult;
+import android.os.Message;
+import android.telephony.ims.stub.ImsSmsImplBase;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.ims.ImsManager;
+import com.android.internal.telephony.CommandException;
+import com.android.internal.telephony.CommandsInterface;
+import com.android.internal.telephony.GsmCdmaPhone;
+import com.android.internal.telephony.InboundSmsHandler;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneFactory;
+import com.android.internal.telephony.TelephonyTest;
+import com.android.internal.telephony.uicc.IccIoResult;
+import com.android.internal.telephony.uicc.IccUtils;
+import com.android.internal.util.HexDump;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class UsimDataDownloadHandlerTest extends TelephonyTest {
+    // Mocked classes
+    private CommandsInterface mMockCi;
+    protected GsmCdmaPhone mMockPhone;
+    private ImsManager mMockImsManager;
+    private Resources mMockResources;
+
+    private UsimDataDownloadHandler mUsimDataDownloadHandler;
+    private Phone mPhoneObj;
+    private Phone[] mPhoneslist;
+    private int mPhoneId;
+    private int mSlotId = 0;
+    private int mToken = 0;
+    private int mSmsSource = 1;
+    private byte[] mTpdu;
+    private byte[] mSmsAckPdu;
+    //Envelope is created as per TS.131.111 for SMS-PP data downwnload operation
+    private String mEnvelope = "D11502028281060591896745F306068199201269231300";
+    //SMS TPDU for Class2 according to TS.123.040
+    private String mPdu = "07914151551512f221110A81785634121000000666B2996C2603";
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(getClass().getSimpleName());
+        mMockCi = Mockito.mock(CommandsInterface.class);
+        mMockPhone = Mockito.mock(GsmCdmaPhone.class);
+        mMockImsManager = Mockito.mock(ImsManager.class);
+        mMockResources = Mockito.mock(Resources.class);
+
+        mPhoneslist = new Phone[] {mMockPhone};
+        mTpdu = HexDump.hexStringToByteArray(mPdu);
+
+        //Use reflection to mock
+        replaceInstance(PhoneFactory.class, "sPhones", null, mPhoneslist);
+        replaceInstance(PhoneFactory.class, "sPhone", null, mMockPhone);
+
+        mPhoneObj = PhoneFactory.getPhone(mSlotId);
+        assertNotNull(mPhoneObj);
+        mPhoneId = mPhoneObj.getPhoneId();
+        doReturn(mSmsStats).when(mPhoneObj).getSmsStats();
+
+        //new UsimDataDownloadHandlerThread(TAG).start();
+        //waitUntilReady();
+        mUsimDataDownloadHandler = new UsimDataDownloadHandler(mMockCi, mPhoneId);
+        mUsimDataDownloadHandler.setImsManager(mMockImsManager);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mUsimDataDownloadHandler = null;
+        mPhoneslist = null;
+        super.tearDown();
+    }
+
+    @Test
+    public void sendEnvelopeForException() throws Exception {
+        setSmsPPSimConfig(false);
+        com.android.internal.telephony.gsm.SmsMessage sms =
+                com.android.internal.telephony.gsm.SmsMessage.createFromPdu(mTpdu);
+
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    AsyncResult.forMessage(response, null, new CommandException(
+                                CommandException.Error.OPERATION_NOT_ALLOWED));
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mMockCi)
+                .sendEnvelopeWithStatus(anyString(), any(Message.class));
+
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        verify(mMockCi).acknowledgeLastIncomingGsmSms(false,
+                CommandsInterface.GSM_SMS_FAIL_CAUSE_USIM_DATA_DOWNLOAD_ERROR, null);
+
+        setSmsPPSimConfig(true);
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        //mTpdu[9] holds messageReference value
+        verify(mMockImsManager).acknowledgeSms(mToken, mTpdu[9],
+                ImsSmsImplBase.DELIVER_STATUS_ERROR_GENERIC);
+    }
+
+    @Test
+    public void sendEnvelopeDataDownloadSuccess() throws Exception {
+        setSmsPPSimConfig(true);
+        com.android.internal.telephony.gsm.SmsMessage sms =
+                com.android.internal.telephony.gsm.SmsMessage.createFromPdu(mTpdu);
+        mSmsAckPdu = createSmsAckPdu(true, mEnvelope, sms);
+
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    IccIoResult iir = new IccIoResult(0x90, 0x00,
+                            IccUtils.hexStringToBytes(mEnvelope));
+                    AsyncResult.forMessage(response, iir, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mMockCi)
+                .sendEnvelopeWithStatus(anyString(), any(Message.class));
+
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        //mTpdu[9] holds messageReference value
+        verify(mMockImsManager).acknowledgeSms(mToken, mTpdu[9],
+                ImsSmsImplBase.DELIVER_STATUS_OK, mSmsAckPdu);
+
+        setSmsPPSimConfig(false);
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        verify(mMockCi).acknowledgeIncomingGsmSmsWithPdu(true,
+                IccUtils.bytesToHexString(mSmsAckPdu), null);
+    }
+
+    @Test
+    public void sendEnvelopeDataDownloadFailed() throws Exception {
+        setSmsPPSimConfig(false);
+        com.android.internal.telephony.gsm.SmsMessage sms =
+                com.android.internal.telephony.gsm.SmsMessage.createFromPdu(mTpdu);
+
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    IccIoResult iir = new IccIoResult(0x93, 0x00,
+                            IccUtils.hexStringToBytes(mEnvelope));
+                    AsyncResult.forMessage(response, iir, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mMockCi)
+                .sendEnvelopeWithStatus(anyString(), any(Message.class));
+
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        verify(mMockCi).acknowledgeLastIncomingGsmSms(false,
+                CommandsInterface.GSM_SMS_FAIL_CAUSE_USIM_APP_TOOLKIT_BUSY, null);
+
+        setSmsPPSimConfig(true);
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        //mTpdu[9] holds messageReference value
+        verify(mMockImsManager).acknowledgeSms(mToken, mTpdu[9],
+                ImsSmsImplBase.DELIVER_STATUS_ERROR_GENERIC);
+    }
+
+    @Test
+    public void sendEnvelopeForSw1_62() throws Exception {
+        setSmsPPSimConfig(false);
+        com.android.internal.telephony.gsm.SmsMessage sms =
+                com.android.internal.telephony.gsm.SmsMessage.createFromPdu(mTpdu);
+        mSmsAckPdu = createSmsAckPdu(false, mEnvelope, sms);
+
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    IccIoResult iir = new IccIoResult(0x62, 0x63,
+                            IccUtils.hexStringToBytes(mEnvelope));
+                    AsyncResult.forMessage(response, iir, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mMockCi)
+                .sendEnvelopeWithStatus(anyString(), any(Message.class));
+
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        verify(mMockCi).acknowledgeIncomingGsmSmsWithPdu(false,
+                IccUtils.bytesToHexString(mSmsAckPdu), null);
+
+        setSmsPPSimConfig(true);
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        //mTpdu[9] holds messageReference value
+        verify(mMockImsManager).acknowledgeSms(mToken, mTpdu[9],
+                ImsSmsImplBase.DELIVER_STATUS_OK, mSmsAckPdu);
+    }
+
+    @Test
+    public void smsCompleteForException() throws Exception {
+        setSmsPPSimConfig(false);
+
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(3);
+                    AsyncResult.forMessage(response, null, new CommandException(
+                                CommandException.Error.OPERATION_NOT_ALLOWED));
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mMockCi)
+                .writeSmsToSim(anyInt(), anyString(), anyString(), any(Message.class));
+
+        int[] responseInfo = {mSmsSource, mTpdu[9], mToken};
+        Message msg = mUsimDataDownloadHandler.obtainMessage(3 /* EVENT_WRITE_SMS_COMPLETE */,
+                responseInfo);
+        AsyncResult.forMessage(msg, null, new CommandException(
+                                CommandException.Error.OPERATION_NOT_ALLOWED));
+        mUsimDataDownloadHandler.handleMessage(msg);
+        verify(mMockCi).acknowledgeLastIncomingGsmSms(false,
+                CommandsInterface.GSM_SMS_FAIL_CAUSE_UNSPECIFIED_ERROR, null);
+
+        setSmsPPSimConfig(true);
+        mUsimDataDownloadHandler.handleMessage(msg);
+        //mTpdu[9] holds messageReference value
+        verify(mMockImsManager).acknowledgeSms(mToken, mTpdu[9],
+                ImsSmsImplBase.DELIVER_STATUS_ERROR_GENERIC);
+    }
+
+    @Test
+    public void smsComplete() throws Exception {
+        setSmsPPSimConfig(true);
+
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(3);
+                    IccIoResult iir = new IccIoResult(0x90, 0x00,
+                            IccUtils.hexStringToBytes(mEnvelope));
+                    AsyncResult.forMessage(response, iir, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mMockCi)
+                .writeSmsToSim(anyInt(), anyString(), anyString(), any(Message.class));
+
+        int[] responseInfo = {mSmsSource, mTpdu[9], mToken};
+        Message msg = mUsimDataDownloadHandler.obtainMessage(3 /* EVENT_WRITE_SMS_COMPLETE */,
+                responseInfo);
+        AsyncResult.forMessage(msg, null, null);
+        mUsimDataDownloadHandler.handleMessage(msg);
+        //mTpdu[9] holds messageReference value
+        verify(mMockImsManager).acknowledgeSms(mToken, mTpdu[9], ImsSmsImplBase.DELIVER_STATUS_OK);
+
+        setSmsPPSimConfig(false);
+        mUsimDataDownloadHandler.handleMessage(msg);
+        verify(mMockCi).acknowledgeLastIncomingGsmSms(true, 0, null);
+    }
+
+    @Test
+    public void failureEnvelopeResponse() throws Exception {
+        setSmsPPSimConfig(false);
+        com.android.internal.telephony.gsm.SmsMessage sms =
+                com.android.internal.telephony.gsm.SmsMessage.createFromPdu(mTpdu);
+
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    IccIoResult iir = new IccIoResult(0x62, 0x63,
+                            IccUtils.hexStringToBytes(null)); //ForNullResponseBytes
+                    AsyncResult.forMessage(response, iir, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mMockCi)
+                .sendEnvelopeWithStatus(anyString(), any(Message.class));
+
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        verify(mMockCi).acknowledgeLastIncomingGsmSms(false,
+                CommandsInterface.GSM_SMS_FAIL_CAUSE_USIM_DATA_DOWNLOAD_ERROR, null);
+
+        setSmsPPSimConfig(true);
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        //mTpdu[9] holds messageReference value
+        verify(mMockImsManager).acknowledgeSms(mToken, mTpdu[9],
+                ImsSmsImplBase.DELIVER_STATUS_ERROR_GENERIC);
+    }
+
+    @Test
+    public void successEnvelopeResponse() throws Exception {
+        setSmsPPSimConfig(false);
+        com.android.internal.telephony.gsm.SmsMessage sms =
+                com.android.internal.telephony.gsm.SmsMessage.createFromPdu(mTpdu);
+
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    IccIoResult iir = new IccIoResult(0x90, 0x00,
+                            IccUtils.hexStringToBytes(null)); //ForNullResponseBytes
+                    AsyncResult.forMessage(response, iir, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mMockCi)
+                .sendEnvelopeWithStatus(anyString(), any(Message.class));
+
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        verify(mMockCi).acknowledgeLastIncomingGsmSms(true, 0, null);
+
+        setSmsPPSimConfig(true);
+        mUsimDataDownloadHandler.startDataDownload(sms,
+                 InboundSmsHandler.SOURCE_INJECTED_FROM_IMS, mToken);
+        processAllMessages();
+        //mTpdu[9] holds messageReference value
+        verify(mMockImsManager).acknowledgeSms(mToken, mTpdu[9], ImsSmsImplBase.DELIVER_STATUS_OK);
+    }
+
+    //To set "config_smppsim_response_via_ims" for testing purpose
+    private void setSmsPPSimConfig(boolean config) {
+        mUsimDataDownloadHandler.setResourcesForTest(mMockResources);
+        doReturn(config).when(mMockResources).getBoolean(
+                com.android.internal.R.bool.config_smppsim_response_via_ims);
+    }
+
+    private byte[] createSmsAckPdu(boolean success, String envelope, SmsMessage smsMessage) {
+        byte[] responseBytes = IccUtils.hexStringToBytes(envelope);
+        int dcs = 0x00;
+        int pid = smsMessage.getProtocolIdentifier();
+        byte[] smsAckPdu;
+        int index = 0;
+        if (success) {
+            smsAckPdu = new byte[responseBytes.length + 5];
+            smsAckPdu[index++] = 0x00;
+            smsAckPdu[index++] = 0x07;
+        } else {
+            smsAckPdu = new byte[responseBytes.length + 6];
+            smsAckPdu[index++] = 0x00;
+            smsAckPdu[index++] = (byte)
+                    CommandsInterface.GSM_SMS_FAIL_CAUSE_USIM_DATA_DOWNLOAD_ERROR;
+            smsAckPdu[index++] = 0x07;
+        }
+
+        smsAckPdu[index++] = (byte) pid;
+        smsAckPdu[index++] = (byte) dcs;
+
+        if (((dcs & 0x8C) == 0x00) || ((dcs & 0xF4) == 0xF0)) {
+            int septetCount = responseBytes.length * 8 / 7;
+            smsAckPdu[index++] = (byte) septetCount;
+        } else {
+            smsAckPdu[index++] = (byte) responseBytes.length;
+        }
+
+        System.arraycopy(responseBytes, 0, smsAckPdu, index, responseBytes.length);
+        return smsAckPdu;
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/ims/ImsEnablementTrackerTest.java b/tests/telephonytests/src/com/android/internal/telephony/ims/ImsEnablementTrackerTest.java
new file mode 100644
index 0000000..b3b9052
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/ims/ImsEnablementTrackerTest.java
@@ -0,0 +1,581 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.ims;
+
+import static junit.framework.Assert.assertTrue;
+
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
+import android.os.Handler;
+import android.os.Looper;
+import android.os.RemoteException;
+import android.telephony.ims.aidl.IImsServiceController;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+
+/**
+ * Unit tests for ImsEnablementTracker
+ */
+@RunWith(AndroidJUnit4.class)
+public class ImsEnablementTrackerTest extends ImsTestBase {
+
+    private static final int SLOT_1 = 0;
+    private static final int SUB_1 = 11;
+
+    private static final int SLOT_2 = 1;
+    private static final int SUB_2 = 22;
+    private static final long TEST_REQUEST_THROTTLE_TIME_MS = 1000L;
+    // Mocked classes
+    @Mock
+    IImsServiceController mMockServiceControllerBinder;
+
+    private TestableImsEnablementTracker mTracker;
+    private Handler mHandler;
+
+    private static class TestableImsEnablementTracker extends ImsEnablementTracker {
+        private long mLastImsOperationTimeMs = 0L;
+        TestableImsEnablementTracker(Looper looper, IImsServiceController controller, int state,
+                int numSlots) {
+            super(looper, controller, state, numSlots);
+        }
+
+        @Override
+        protected long getLastOperationTimeMillis() {
+            return mLastImsOperationTimeMs;
+        }
+
+        private void setLastOperationTimeMillis(long timeMills) {
+            mLastImsOperationTimeMs = timeMills;
+        }
+    }
+
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+
+    @After
+    @Override
+    public void tearDown() throws Exception {
+        // Make sure the handler is empty before finishing the test.
+        waitForHandlerAction(mHandler, TEST_REQUEST_THROTTLE_TIME_MS);
+        mTracker = null;
+        super.tearDown();
+    }
+
+    @SmallTest
+    @Test
+    public void testEnableCommandInDefaultState() throws RemoteException {
+        // Verify that when the enable command is received in the Default state and enableIms
+        // is called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DEFAULT);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+         // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verify(mMockServiceControllerBinder).enableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testDisableCommandInDefaultState() throws RemoteException {
+        // Verify that when the disable command is received in the Default state and disableIms
+        // is called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DEFAULT);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verify(mMockServiceControllerBinder).disableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testResetCommandInDefaultState() throws RemoteException {
+        // Verify that when reset command is received in the Default state, it should be ignored.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DEFAULT);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.resetIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verifyZeroInteractions(mMockServiceControllerBinder);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DEFAULT));
+    }
+
+    @SmallTest
+    @Test
+    public void testEnableCommandInEnabledState() throws RemoteException {
+        // Verify that received the enable command is not handle in the Enabled state,
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_ENABLED);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, mTracker.getRemainThrottleTime(),
+                mTracker.getRemainThrottleTime() + 100);
+        verify(mMockServiceControllerBinder, never()).enableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testDisableCommandInEnabledState() throws RemoteException {
+        // Verify that when the disable command is received in the Enabled state and disableIms
+        // is called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_ENABLED);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verify(mMockServiceControllerBinder).disableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testResetCommandInEnabledState() throws RemoteException {
+        // Verify that when the reset command is received in the Enabled state and disableIms
+        // and enableIms are called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_ENABLED);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.resetIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verify(mMockServiceControllerBinder).disableIms(eq(SLOT_1), eq(SUB_1));
+        // The disableIms was called. So set the last operation time to current.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        waitForHandlerActionDelayed(mHandler, TEST_REQUEST_THROTTLE_TIME_MS,
+                TEST_REQUEST_THROTTLE_TIME_MS + 100);
+        verify(mMockServiceControllerBinder).enableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testDisableCommandInDisabledState() throws RemoteException {
+        // Verify that when disable command is received in the Disabled state, it should be ignored.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DISABLED);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verifyZeroInteractions(mMockServiceControllerBinder);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testEnableCommandInDisabledState() throws RemoteException {
+        // Verify that when the enable command is received in the Disabled state and enableIms
+        // is called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DISABLED);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verify(mMockServiceControllerBinder).enableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testEnableCommandWithoutTimeoutInDisableState() throws RemoteException {
+        // Verify that when the enable command is received in the Disabled state. After throttle
+        // time expired, the enableIms is called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DISABLED);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, mTracker.getRemainThrottleTime(),
+                mTracker.getRemainThrottleTime() + 100);
+        verify(mMockServiceControllerBinder).enableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testResetCommandInDisabledState() throws RemoteException {
+        // Verify that the reset command is received in the Disabled state and it`s not handled.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DISABLED);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.resetIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verifyZeroInteractions(mMockServiceControllerBinder);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testEnableCommandInDisablingState() throws RemoteException {
+        // Verify that when enable command is received in the Disabling state, it should be ignored.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DISABLING);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verifyZeroInteractions(mMockServiceControllerBinder);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testDisablingMessageInDisablingState() throws RemoteException {
+        // Verify that when the internal disable message is received in the Disabling state and
+        // disableIms is called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DISABLING);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.startStateMachineAsConnected(SLOT_1);
+
+        waitForHandlerActionDelayed(mHandler, mTracker.getRemainThrottleTime(),
+                mTracker.getRemainThrottleTime() + 100);
+        verify(mMockServiceControllerBinder).disableIms(anyInt(), anyInt());
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testResetCommandInDisablingState() throws RemoteException {
+        // Verify when the reset command is received in the Disabling state the disableIms and
+        // enableIms are called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DISABLING);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.resetIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, mTracker.getRemainThrottleTime(),
+                mTracker.getRemainThrottleTime() + 100);
+        verify(mMockServiceControllerBinder).disableIms(eq(SLOT_1), eq(SUB_1));
+        // The disableIms was called. So set the last operation time to current.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        waitForHandlerActionDelayed(mHandler, mTracker.getRemainThrottleTime(),
+                mTracker.getRemainThrottleTime() + 100);
+        verify(mMockServiceControllerBinder).enableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testEnablingMessageInEnablingState() throws RemoteException {
+        // Verify that when the internal enable message is received in the Enabling state and
+        // enableIms is called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_ENABLING);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verify(mMockServiceControllerBinder).enableIms(anyInt(), anyInt());
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testDisableCommandInEnablingState() throws RemoteException {
+        // Verify that when the disable command is received in the Enabling state and
+        // clear pending message and disableIms is not called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_ENABLING);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verify(mMockServiceControllerBinder, never()).disableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testResetCommandWithEnablingState() throws RemoteException {
+        // Verify that when reset command is received in the Enabling state, it should be ignored.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_ENABLING);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.resetIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verifyZeroInteractions(mMockServiceControllerBinder);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLING));
+    }
+
+    @SmallTest
+    @Test
+    public void testEnableCommandInResettingState() throws RemoteException {
+        // Verify that when the enable command is received in the Resetting state and
+        // enableIms is not called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_RESETTING);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verifyZeroInteractions(mMockServiceControllerBinder);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_RESETTING));
+    }
+
+    @SmallTest
+    @Test
+    public void testDisableCommandInResettingState() throws RemoteException {
+        // Verify that when the disable command is received in the Resetting state and
+        // disableIms is called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_RESETTING);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, mTracker.getRemainThrottleTime(),
+                mTracker.getRemainThrottleTime() + 100);
+        verify(mMockServiceControllerBinder).disableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testResettingMessageInResettingState() throws RemoteException {
+        // Verify that when the internal reset message is received in the Resetting state and
+        // disableIms and enableIms are called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_RESETTING);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        verify(mMockServiceControllerBinder).disableIms(anyInt(), anyInt());
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        waitForHandlerActionDelayed(mHandler, mTracker.getRemainThrottleTime(),
+                mTracker.getRemainThrottleTime() + 100);
+        verify(mMockServiceControllerBinder).enableIms(anyInt(), anyInt());
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testConsecutiveCommandInEnabledState() throws RemoteException {
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_ENABLED);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLING));
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLING));
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, mTracker.getRemainThrottleTime(),
+                mTracker.getRemainThrottleTime() + 100);
+        verify(mMockServiceControllerBinder, times(1)).disableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testConsecutiveCommandInDisabledState() throws RemoteException {
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DISABLED);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        // Set the last operation time to current to verify the message with delay.
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLING));
+
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+
+        mTracker.resetIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, mTracker.getRemainThrottleTime(),
+                mTracker.getRemainThrottleTime() + 100);
+        verify(mMockServiceControllerBinder, times(1)).enableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testSubIdChangeToInvalidAndEnableCommand() throws RemoteException {
+        // Verify that when the enable command is received in the Default state and enableIms
+        // is called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_ENABLED);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.subIdChangedToInvalid(SLOT_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DEFAULT));
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verify(mMockServiceControllerBinder).enableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+    }
+
+    @SmallTest
+    @Test
+    public void testEnableCommandWithDifferentSlotId() throws RemoteException {
+        // Verify that when the enable command is received in the Default state and enableIms
+        // is called.
+        mTracker = createTracker(mMockServiceControllerBinder, mTracker.STATE_IMS_DEFAULT, 2);
+        mTracker.startStateMachineAsConnected(SLOT_1);
+        mHandler = mTracker.getHandler(SLOT_1);
+        mTracker.startStateMachineAsConnected(SLOT_2);
+        Handler handlerForSlot2 = mTracker.getHandler(SLOT_2);
+        // Wait for a while for the state machine to be ready.
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+
+        mTracker.enableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, 100, 150);
+        verify(mMockServiceControllerBinder).enableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+        assertTrue(mTracker.isState(SLOT_2, mTracker.STATE_IMS_DEFAULT));
+
+        mTracker.enableIms(SLOT_2, SUB_2);
+        waitForHandlerActionDelayed(handlerForSlot2, 100, 150);
+        verify(mMockServiceControllerBinder).enableIms(eq(SLOT_2), eq(SUB_2));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_ENABLED));
+        assertTrue(mTracker.isState(SLOT_2, mTracker.STATE_IMS_ENABLED));
+
+        mTracker.setNumOfSlots(1);
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.disableIms(SLOT_1, SUB_1);
+        waitForHandlerActionDelayed(mHandler, mTracker.getRemainThrottleTime(),
+                mTracker.getRemainThrottleTime() + 100);
+        verify(mMockServiceControllerBinder).disableIms(eq(SLOT_1), eq(SUB_1));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+
+        mTracker.setNumOfSlots(2);
+        mTracker.setLastOperationTimeMillis(System.currentTimeMillis());
+        mTracker.disableIms(SLOT_2, SUB_2);
+        waitForHandlerActionDelayed(handlerForSlot2, 100, 150);
+        verify(mMockServiceControllerBinder).disableIms(eq(SLOT_2), eq(SUB_2));
+        assertTrue(mTracker.isState(SLOT_1, mTracker.STATE_IMS_DISABLED));
+        assertTrue(mTracker.isState(SLOT_2, mTracker.STATE_IMS_DISABLED));
+    }
+
+    private TestableImsEnablementTracker createTracker(IImsServiceController binder, int state) {
+        TestableImsEnablementTracker tracker = new TestableImsEnablementTracker(
+                Looper.getMainLooper(), binder, state, 1);
+        return tracker;
+    }
+
+    private TestableImsEnablementTracker createTracker(IImsServiceController binder, int state,
+            int numSlots) {
+        TestableImsEnablementTracker tracker = new TestableImsEnablementTracker(
+                Looper.getMainLooper(), binder, state, numSlots);
+        return tracker;
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsCallInfoTrackerTest.java b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsCallInfoTrackerTest.java
new file mode 100644
index 0000000..e3fc6d3
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsCallInfoTrackerTest.java
@@ -0,0 +1,436 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.internal.telephony.imsphone;
+
+import static android.telephony.AccessNetworkConstants.AccessNetworkType.EUTRAN;
+
+import static junit.framework.Assert.assertNotNull;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import android.telephony.ServiceState;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.Call;
+import com.android.internal.telephony.TelephonyTest;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class ImsCallInfoTrackerTest extends TelephonyTest {
+
+    private ImsCallInfoTracker mImsCallInfoTracker;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(getClass().getSimpleName());
+
+        mImsCallInfoTracker = new ImsCallInfoTracker(mImsPhone);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    @Test
+    public void testDialingNormalCall() throws Exception {
+        ArgumentCaptor<List<ImsCallInfo>> captor = ArgumentCaptor.forClass(List.class);
+
+        ImsPhoneConnection c = getConnection(Call.State.DIALING, false);
+        mImsCallInfoTracker.addImsCallStatus(c);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(captor.capture(), any());
+
+        List<ImsCallInfo> imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        ImsCallInfo info = imsCallInfos.get(0);
+
+        assertNotNull(info);
+        assertEquals(1, info.getIndex());
+        assertEquals(Call.State.DIALING, info.getCallState());
+        assertFalse(info.isIncoming());
+        assertFalse(info.isEmergencyCall());
+        assertEquals(EUTRAN, info.getCallRadioTech());
+        assertFalse(info.isHeldByRemote());
+    }
+
+    @Test
+    public void testDialingEmergencyCall() throws Exception {
+        ArgumentCaptor<List<ImsCallInfo>> captor = ArgumentCaptor.forClass(List.class);
+
+        ImsPhoneConnection c = getConnection(Call.State.DIALING, true);
+        mImsCallInfoTracker.addImsCallStatus(c);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(captor.capture(), any());
+
+        List<ImsCallInfo> imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        ImsCallInfo info = imsCallInfos.get(0);
+
+        assertNotNull(info);
+        assertEquals(1, info.getIndex());
+        assertEquals(Call.State.DIALING, info.getCallState());
+        assertFalse(info.isIncoming());
+        assertTrue(info.isEmergencyCall());
+        assertEquals(EUTRAN, info.getCallRadioTech());
+        assertFalse(info.isHeldByRemote());
+    }
+
+    @Test
+    public void testIncomingCall() throws Exception {
+        ArgumentCaptor<List<ImsCallInfo>> captor = ArgumentCaptor.forClass(List.class);
+
+        ImsPhoneConnection c = getConnection(Call.State.INCOMING, false);
+        mImsCallInfoTracker.addImsCallStatus(c);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(captor.capture(), any());
+
+        List<ImsCallInfo> imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        ImsCallInfo info = imsCallInfos.get(0);
+
+        assertNotNull(info);
+        assertEquals(1, info.getIndex());
+        assertEquals(Call.State.INCOMING, info.getCallState());
+        assertTrue(info.isIncoming());
+        assertFalse(info.isEmergencyCall());
+        assertEquals(EUTRAN, info.getCallRadioTech());
+        assertFalse(info.isHeldByRemote());
+
+        // Answer the call
+        doReturn(Call.State.ACTIVE).when(c).getState();
+        mImsCallInfoTracker.updateImsCallStatus(c);
+
+        verify(mImsPhone, times(2)).updateImsCallStatus(captor.capture(), any());
+
+        imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        info = imsCallInfos.get(0);
+
+        assertNotNull(info);
+        assertEquals(1, info.getIndex());
+        assertEquals(Call.State.ACTIVE, info.getCallState());
+
+        // Hold the call
+        doReturn(Call.State.HOLDING).when(c).getState();
+        mImsCallInfoTracker.updateImsCallStatus(c);
+
+        verify(mImsPhone, times(3)).updateImsCallStatus(captor.capture(), any());
+
+        imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        info = imsCallInfos.get(0);
+
+        assertNotNull(info);
+        assertEquals(1, info.getIndex());
+        assertEquals(Call.State.HOLDING, info.getCallState());
+
+        // Disconnect the call
+        doReturn(Call.State.DISCONNECTING).when(c).getState();
+        mImsCallInfoTracker.updateImsCallStatus(c);
+
+        verify(mImsPhone, times(4)).updateImsCallStatus(captor.capture(), any());
+
+        imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        info = imsCallInfos.get(0);
+
+        assertNotNull(info);
+        assertEquals(1, info.getIndex());
+        assertEquals(Call.State.DISCONNECTING, info.getCallState());
+
+        // Call disconnected
+        doReturn(Call.State.DISCONNECTED).when(c).getState();
+        mImsCallInfoTracker.updateImsCallStatus(c);
+
+        verify(mImsPhone, times(5)).updateImsCallStatus(captor.capture(), any());
+
+        imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        info = imsCallInfos.get(0);
+
+        assertNotNull(info);
+        assertEquals(1, info.getIndex());
+        assertEquals(Call.State.IDLE, info.getCallState());
+    }
+
+    @Test
+    public void testMultiCalls() throws Exception {
+        ArgumentCaptor<List<ImsCallInfo>> captor = ArgumentCaptor.forClass(List.class);
+
+        ImsPhoneConnection c1 = getConnection(Call.State.INCOMING, false);
+        mImsCallInfoTracker.addImsCallStatus(c1);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(captor.capture(), any());
+
+        doReturn(Call.State.ACTIVE).when(c1).getState();
+        mImsCallInfoTracker.updateImsCallStatus(c1);
+
+        verify(mImsPhone, times(2)).updateImsCallStatus(captor.capture(), any());
+
+        List<ImsCallInfo> imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        // 1st call
+        ImsCallInfo info1 = imsCallInfos.get(0);
+
+        assertNotNull(info1);
+        assertEquals(1, info1.getIndex());
+        assertEquals(Call.State.ACTIVE, info1.getCallState());
+
+        // Add 2nd WAITING call
+        ImsPhoneConnection c2 = getConnection(Call.State.WAITING, false);
+        mImsCallInfoTracker.addImsCallStatus(c2);
+
+        verify(mImsPhone, times(3)).updateImsCallStatus(captor.capture(), any());
+
+        imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(2, imsCallInfos.size());
+
+        // 1st call
+        info1 = imsCallInfos.get(0);
+
+        assertNotNull(info1);
+        assertEquals(1, info1.getIndex());
+        assertEquals(Call.State.ACTIVE, info1.getCallState());
+
+        // 2nd call
+        ImsCallInfo info2 = imsCallInfos.get(1);
+
+        assertNotNull(info2);
+        assertEquals(2, info2.getIndex());
+        assertEquals(Call.State.WAITING, info2.getCallState());
+        assertTrue(info2.isIncoming());
+
+        // Disconnect 1st call
+        doReturn(Call.State.DISCONNECTED).when(c1).getState();
+        mImsCallInfoTracker.updateImsCallStatus(c1);
+
+        verify(mImsPhone, times(4)).updateImsCallStatus(captor.capture(), any());
+
+        imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(2, imsCallInfos.size());
+
+        // 1st call
+        info1 = imsCallInfos.get(0);
+
+        assertNotNull(info1);
+        assertEquals(1, info1.getIndex());
+        assertEquals(Call.State.IDLE, info1.getCallState());
+
+        // 2nd call
+        info2 = imsCallInfos.get(1);
+
+        assertNotNull(info2);
+        assertEquals(2, info2.getIndex());
+        assertEquals(Call.State.WAITING, info2.getCallState());
+
+        // Answer WAITING call
+        doReturn(Call.State.ACTIVE).when(c2).getState();
+        mImsCallInfoTracker.updateImsCallStatus(c2);
+
+        verify(mImsPhone, times(5)).updateImsCallStatus(captor.capture(), any());
+
+        imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        // 2nd call
+        info2 = imsCallInfos.get(0);
+
+        assertNotNull(info2);
+        assertEquals(2, info2.getIndex());
+        assertEquals(Call.State.ACTIVE, info2.getCallState());
+    }
+
+    @Test
+    public void testHeldByRemote() throws Exception {
+        ArgumentCaptor<List<ImsCallInfo>> captor = ArgumentCaptor.forClass(List.class);
+
+        ImsPhoneConnection c = getConnection(Call.State.INCOMING, false);
+        mImsCallInfoTracker.addImsCallStatus(c);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(captor.capture(), any());
+
+        doReturn(Call.State.ACTIVE).when(c).getState();
+        mImsCallInfoTracker.updateImsCallStatus(c);
+
+        verify(mImsPhone, times(2)).updateImsCallStatus(captor.capture(), any());
+
+        // Hold received
+        mImsCallInfoTracker.updateImsCallStatus(c, true, false);
+
+        verify(mImsPhone, times(3)).updateImsCallStatus(captor.capture(), any());
+
+        List<ImsCallInfo> imsCallInfos = captor.getValue();
+
+        assertEquals(1, imsCallInfos.size());
+
+        ImsCallInfo info = imsCallInfos.get(0);
+
+        assertNotNull(info);
+        assertEquals(1, info.getIndex());
+        assertEquals(Call.State.ACTIVE, info.getCallState());
+        assertTrue(info.isHeldByRemote());
+
+        // Resume received
+        mImsCallInfoTracker.updateImsCallStatus(c, false, true);
+
+        verify(mImsPhone, times(4)).updateImsCallStatus(captor.capture(), any());
+
+        imsCallInfos = captor.getValue();
+
+        assertEquals(1, imsCallInfos.size());
+
+        info = imsCallInfos.get(0);
+
+        assertNotNull(info);
+        assertEquals(1, info.getIndex());
+        assertEquals(Call.State.ACTIVE, info.getCallState());
+        assertFalse(info.isHeldByRemote());
+    }
+
+    @Test
+    public void testSortImsCallInfo() throws Exception {
+        List<ImsCallInfo> imsCallInfos = new ArrayList<>();
+        imsCallInfos.add(new ImsCallInfo(2));
+        imsCallInfos.add(new ImsCallInfo(1));
+
+        assertEquals(2, imsCallInfos.get(0).getIndex());
+        assertEquals(1, imsCallInfos.get(1).getIndex());
+
+        ImsCallInfoTracker.sort(imsCallInfos);
+
+        assertEquals(1, imsCallInfos.get(0).getIndex());
+        assertEquals(2, imsCallInfos.get(1).getIndex());
+    }
+
+    @Test
+    public void testSrvccCompleted() throws Exception {
+        ArgumentCaptor<List<ImsCallInfo>> captor = ArgumentCaptor.forClass(List.class);
+
+        ImsPhoneConnection c = getConnection(Call.State.DIALING, false);
+        mImsCallInfoTracker.addImsCallStatus(c);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(captor.capture(), any());
+
+        List<ImsCallInfo> imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        mImsCallInfoTracker.notifySrvccCompleted();
+
+        verify(mImsPhone, times(2)).updateImsCallStatus(captor.capture(), any());
+
+        imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(0, imsCallInfos.size());
+    }
+
+    @Test
+    public void testClearAllOrphanedConnections() throws Exception {
+        ArgumentCaptor<List<ImsCallInfo>> captor = ArgumentCaptor.forClass(List.class);
+
+        ImsPhoneConnection c = getConnection(Call.State.DIALING, false);
+        mImsCallInfoTracker.addImsCallStatus(c);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(captor.capture(), any());
+
+        List<ImsCallInfo> imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        mImsCallInfoTracker.clearAllOrphanedConnections();
+
+        verify(mImsPhone, times(2)).updateImsCallStatus(captor.capture(), any());
+
+        imsCallInfos = captor.getValue();
+
+        assertNotNull(imsCallInfos);
+        assertEquals(1, imsCallInfos.size());
+
+        ImsCallInfo info = imsCallInfos.get(0);
+
+        assertNotNull(info);
+        assertEquals(1, info.getIndex());
+        assertEquals(Call.State.IDLE, info.getCallState());
+    }
+
+    private ImsPhoneConnection getConnection(Call.State state, boolean isEmergency) {
+        ImsPhoneConnection c = mock(ImsPhoneConnection.class);
+        doReturn(isEmergency).when(c).isEmergencyCall();
+        doReturn(state).when(c).getState();
+        doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_LTE).when(c).getCallRadioTech();
+        switch (state) {
+            case INCOMING:
+            case WAITING:
+                doReturn(true).when(c).isIncoming();
+                break;
+            default:
+                doReturn(false).when(c).isIncoming();
+        }
+
+        return c;
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneCallTest.java b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneCallTest.java
index c2db93f..13748e8 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneCallTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneCallTest.java
@@ -18,6 +18,7 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.eq;
@@ -27,6 +28,8 @@
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import android.telephony.ims.ImsCallProfile;
+import android.telephony.ims.ImsCallSession;
 import android.telephony.ims.ImsStreamMediaProfile;
 import android.test.suitebuilder.annotation.SmallTest;
 
@@ -258,6 +261,51 @@
 
     @Test
     @SmallTest
+    public void testGetCallSessionId() {
+        doReturn(mImsCall).when(mConnection1).getImsCall();
+        ImsCallSession imsForegroundCallSession = mock(ImsCallSession.class);
+        doReturn(imsForegroundCallSession).when(mImsCall).getSession();
+        doReturn("1").when(imsForegroundCallSession).getCallId();
+        mImsCallUT.attach(mConnection1, Call.State.ACTIVE);
+        assertEquals("1", mImsCallUT.getCallSessionId());
+        doReturn(null).when(mImsCall).getSession();
+        assertNull(mImsCallUT.getCallSessionId());
+        doReturn(null).when(mConnection1).getImsCall();
+        assertNull(mImsCallUT.getCallSessionId());
+        mImsCallUT.detach(mConnection1);
+        assertNull(mImsCallUT.getCallSessionId());
+    }
+
+    @Test
+    @SmallTest
+    public void testGetServiceType() {
+        doReturn(mImsCall).when(mConnection1).getImsCall();
+        mImsCallUT.attach(mConnection1, Call.State.ACTIVE);
+        doReturn(false).when(mConnection1).isEmergencyCall();
+        assertEquals(ImsCallProfile.SERVICE_TYPE_NORMAL, mImsCallUT.getServiceType());
+        doReturn(true).when(mConnection1).isEmergencyCall();
+        assertEquals(ImsCallProfile.SERVICE_TYPE_EMERGENCY, mImsCallUT.getServiceType());
+        mImsCallUT.detach(mConnection1);
+        assertEquals(ImsCallProfile.SERVICE_TYPE_NONE, mImsCallUT.getServiceType());
+    }
+
+    @Test
+    @SmallTest
+    public void testGetCallType() {
+        doReturn(mImsCall).when(mConnection1).getImsCall();
+        mImsCallUT.attach(mConnection1, Call.State.ACTIVE);
+        doReturn(false).when(mImsCall).isVideoCall();
+        assertEquals(ImsCallProfile.CALL_TYPE_VOICE, mImsCallUT.getCallType());
+        doReturn(true).when(mImsCall).isVideoCall();
+        assertEquals(ImsCallProfile.CALL_TYPE_VT, mImsCallUT.getCallType());
+        doReturn(null).when(mConnection1).getImsCall();
+        assertEquals(ImsCallProfile.CALL_TYPE_NONE, mImsCallUT.getCallType());
+        mImsCallUT.detach(mConnection1);
+        assertEquals(ImsCallProfile.CALL_TYPE_NONE, mImsCallUT.getCallType());
+    }
+
+    @Test
+    @SmallTest
     public void testSetMute() {
         doReturn(mImsCall).when(mConnection1).getImsCall();
         mImsCallUT.attach(mConnection1, Call.State.ACTIVE);
diff --git a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneCallTrackerTest.java b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneCallTrackerTest.java
index 4f1bdac..0af95e8 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneCallTrackerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneCallTrackerTest.java
@@ -21,6 +21,23 @@
 import static android.net.NetworkStats.SET_FOREGROUND;
 import static android.net.NetworkStats.TAG_NONE;
 import static android.net.NetworkStats.UID_ALL;
+import static android.telephony.CarrierConfigManager.ImsVoice.ALERTING_SRVCC_SUPPORT;
+import static android.telephony.CarrierConfigManager.ImsVoice.BASIC_SRVCC_SUPPORT;
+import static android.telephony.CarrierConfigManager.ImsVoice.MIDCALL_SRVCC_SUPPORT;
+import static android.telephony.CarrierConfigManager.ImsVoice.PREALERTING_SRVCC_SUPPORT;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_ACTIVE;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_ALERTING;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_INCOMING;
+import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_INCOMING_SETUP;
+import static android.telephony.TelephonyManager.SRVCC_STATE_HANDOVER_CANCELED;
+import static android.telephony.TelephonyManager.SRVCC_STATE_HANDOVER_COMPLETED;
+import static android.telephony.TelephonyManager.SRVCC_STATE_HANDOVER_FAILED;
+import static android.telephony.TelephonyManager.SRVCC_STATE_HANDOVER_STARTED;
+import static android.telephony.emergency.EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_AMBULANCE;
+import static android.telephony.ims.ImsStreamMediaProfile.DIRECTION_INACTIVE;
+import static android.telephony.ims.ImsStreamMediaProfile.DIRECTION_SEND_RECEIVE;
+import static android.telephony.ims.feature.MmTelFeature.IMS_TRAFFIC_DIRECTION_INCOMING;
+import static android.telephony.ims.feature.MmTelFeature.IMS_TRAFFIC_DIRECTION_OUTGOING;
 
 import static com.android.testutils.NetworkStatsUtilsKt.assertNetworkStatsEquals;
 
@@ -58,17 +75,20 @@
 import android.content.pm.PackageManager;
 import android.net.NetworkStats;
 import android.net.NetworkStats.Entry;
+import android.net.Uri;
 import android.net.netstats.provider.INetworkStatsProviderCallback;
 import android.os.Bundle;
 import android.os.Message;
 import android.os.PersistableBundle;
 import android.os.RemoteException;
 import android.telecom.VideoProfile;
+import android.telephony.AccessNetworkConstants;
 import android.telephony.CarrierConfigManager;
 import android.telephony.DisconnectCause;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.ServiceState;
 import android.telephony.TelephonyManager;
+import android.telephony.emergency.EmergencyNumber;
 import android.telephony.ims.ImsCallProfile;
 import android.telephony.ims.ImsCallSession;
 import android.telephony.ims.ImsConferenceState;
@@ -76,6 +96,9 @@
 import android.telephony.ims.ImsReasonInfo;
 import android.telephony.ims.ImsStreamMediaProfile;
 import android.telephony.ims.RtpHeaderExtensionType;
+import android.telephony.ims.SrvccCall;
+import android.telephony.ims.aidl.IImsTrafficSessionCallback;
+import android.telephony.ims.aidl.ISrvccStartedCallback;
 import android.telephony.ims.feature.ImsFeature;
 import android.telephony.ims.feature.MmTelFeature;
 import android.telephony.ims.stub.ImsRegistrationImplBase;
@@ -91,13 +114,16 @@
 import com.android.ims.ImsConfig;
 import com.android.ims.ImsException;
 import com.android.ims.ImsManager;
+import com.android.ims.internal.ConferenceParticipant;
 import com.android.ims.internal.IImsCallSession;
 import com.android.internal.telephony.Call;
 import com.android.internal.telephony.CallStateException;
 import com.android.internal.telephony.CommandsInterface;
 import com.android.internal.telephony.Connection;
 import com.android.internal.telephony.IccCardConstants;
+import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.SrvccConnection;
 import com.android.internal.telephony.TelephonyTest;
 import com.android.internal.telephony.d2d.RtpTransport;
 import com.android.internal.telephony.imsphone.ImsPhoneCallTracker.VtDataUsageProvider;
@@ -109,9 +135,12 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Set;
 import java.util.concurrent.Executor;
 
@@ -125,6 +154,7 @@
     private ImsCall.Listener mImsCallListener;
     private ImsCall mImsCall;
     private ImsCall mSecondImsCall;
+    private ISrvccStartedCallback mSrvccStartedCallback;
     private BroadcastReceiver mBroadcastReceiver;
     private Bundle mBundle = new Bundle();
     private static final int SUB_0 = 0;
@@ -140,6 +170,7 @@
     private ImsPhoneConnection mImsPhoneConnection;
     private INetworkStatsProviderCallback mVtDataUsageProviderCb;
     private ImsPhoneCallTracker.ConnectorFactory mConnectorFactory;
+    private CommandsInterface mMockCi;
 
     private final Executor mExecutor = Runnable::run;
 
@@ -200,6 +231,7 @@
         mSecondImsCall = spy(new ImsCall(mContext, mImsCallProfile));
         mImsPhoneConnectionListener = mock(ImsPhoneConnection.Listener.class);
         mImsPhoneConnection = mock(ImsPhoneConnection.class);
+        mMockCi = mock(CommandsInterface.class);
         imsCallMocking(mImsCall);
         imsCallMocking(mSecondImsCall);
         doReturn(ImsFeature.STATE_READY).when(mImsManager).getImsServiceState();
@@ -532,7 +564,7 @@
         assertEquals(PhoneConstants.State.IDLE, mCTUT.getState());
         assertFalse(mCTUT.mRingingCall.isRinging());
         // mock a MT call
-        mMmTelListener.onIncomingCall(mock(IImsCallSession.class), Bundle.EMPTY);
+        mMmTelListener.onIncomingCall(mock(IImsCallSession.class), null, Bundle.EMPTY);
         verify(mImsPhone, times(1)).notifyNewRingingConnection((Connection) any());
         verify(mImsPhone, times(1)).notifyIncomingRing();
         assertEquals(PhoneConstants.State.RINGING, mCTUT.getState());
@@ -684,7 +716,7 @@
             ex.printStackTrace();
             Assert.fail("unexpected exception thrown" + ex.getMessage());
         }
-        mMmTelListener.onIncomingCall(mock(IImsCallSession.class), Bundle.EMPTY);
+        mMmTelListener.onIncomingCall(mock(IImsCallSession.class), null, Bundle.EMPTY);
 
         verify(mImsPhone, times(2)).notifyNewRingingConnection((Connection) any());
         verify(mImsPhone, times(2)).notifyIncomingRing();
@@ -722,7 +754,7 @@
             ex.printStackTrace();
             Assert.fail("unexpected exception thrown" + ex.getMessage());
         }
-        mMmTelListener.onIncomingCall(mock(IImsCallSession.class), Bundle.EMPTY);
+        mMmTelListener.onIncomingCall(mock(IImsCallSession.class), null, Bundle.EMPTY);
 
         verify(mImsPhone, times(2)).notifyNewRingingConnection((Connection) any());
         verify(mImsPhone, times(2)).notifyIncomingRing();
@@ -914,7 +946,7 @@
         try {
             doReturn(mSecondImsCall).when(mImsManager).takeCall(any(IImsCallSession.class),
                     any(ImsCall.Listener.class));
-            mMmTelListener.onIncomingCall(mock(IImsCallSession.class), Bundle.EMPTY);
+            mMmTelListener.onIncomingCall(mock(IImsCallSession.class), null, Bundle.EMPTY);
             mCTUT.acceptCall(ImsCallProfile.CALL_TYPE_VOICE);
         } catch (Exception ex) {
             ex.printStackTrace();
@@ -1429,6 +1461,45 @@
         verify(mImsPhone, never()).startOnHoldTone(nullable(Connection.class));
     }
 
+    @Test
+    @SmallTest
+    public void testSendAnbrQuery() throws Exception {
+        logd("ImsPhoneCallTracker testSendAnbrQuery");
+
+        replaceInstance(Phone.class, "mCi", mPhone, mMockCi);
+        //establish a MT call
+        testImsMTCallAccept();
+
+        ImsPhoneConnection connection = mCTUT.mForegroundCall.getFirstConnection();
+        ImsCall imsCall = connection.getImsCall();
+        imsCall.getImsCallSessionListenerProxy().callSessionSendAnbrQuery(1, 1, 24400);
+
+        verify(mMockCi, times(1)).sendAnbrQuery(eq(1), eq(1), eq(24400), any());
+
+        // Disconnecting and then Disconnected
+        mCTUT.hangup(connection);
+        mImsCallListener.onCallTerminated(imsCall,
+                new ImsReasonInfo(ImsReasonInfo.CODE_USER_TERMINATED, 0));
+    }
+
+    @Test
+    @SmallTest
+    public void testTriggerNotifyAnbr() throws Exception {
+        logd("ImsPhoneCallTracker testTriggerNotifyAnbr");
+
+        testImsMTCallAccept();
+        ImsPhoneConnection connection = mCTUT.mForegroundCall.getFirstConnection();
+        ImsCall imsCall = connection.getImsCall();
+
+        mCTUT.triggerNotifyAnbr(1, 1, 24400);
+        verify(mImsCall, times(1)).callSessionNotifyAnbr(eq(1), eq(1), eq(24400));
+
+        // Disconnecting and then Disconnected
+        mCTUT.hangup(connection);
+        mImsCallListener.onCallTerminated(imsCall,
+                new ImsReasonInfo(ImsReasonInfo.CODE_USER_TERMINATED, 0));
+    }
+
     /**
      * Verifies that a remote hold tone is played when the call is remotely held and the media
      * direction is inactive (i.e. the audio stream is not playing, so we should play the tone).
@@ -1689,7 +1760,7 @@
         assertTrue(mCTUT.mForegroundCall.isRingbackTonePlaying());
 
         // Move the connection to the handover state.
-        mCTUT.notifySrvccState(Call.SrvccState.COMPLETED);
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_COMPLETED);
 
         assertFalse(mCTUT.mForegroundCall.isRingbackTonePlaying());
     }
@@ -1720,7 +1791,7 @@
         }
 
         // Move the connection to the handover state.
-        mCTUT.notifySrvccState(Call.SrvccState.COMPLETED);
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_COMPLETED);
         // Ensure we are no longer tracking hold.
         assertFalse(mCTUT.isHoldOrSwapInProgress());
     }
@@ -1732,7 +1803,7 @@
         assertEquals(PhoneConstants.State.IDLE, mCTUT.getState());
         assertFalse(mCTUT.mRingingCall.isRinging());
         // mock a MT call
-        mMmTelListener.onIncomingCall(mock(IImsCallSession.class), Bundle.EMPTY);
+        mMmTelListener.onIncomingCall(mock(IImsCallSession.class), null, Bundle.EMPTY);
         verify(mImsPhone, times(1)).notifyNewRingingConnection((Connection) any());
         verify(mImsPhone, times(1)).notifyIncomingRing();
         assertEquals(PhoneConstants.State.RINGING, mCTUT.getState());
@@ -1743,7 +1814,7 @@
         connection.addListener(mImsPhoneConnectionListener);
 
         // Move the connection to the handover state.
-        mCTUT.notifySrvccState(Call.SrvccState.COMPLETED);
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_COMPLETED);
         assertEquals(1, mCTUT.mHandoverCall.getConnections().size());
 
         // No need to go through all the rigamarole of the mocked termination we normally do; we
@@ -1770,7 +1841,7 @@
         assertEquals(PhoneConstants.State.IDLE, mCTUT.getState());
         assertFalse(mCTUT.mRingingCall.isRinging());
         // mock a MT call
-        mMmTelListener.onIncomingCall(mock(IImsCallSession.class), Bundle.EMPTY);
+        mMmTelListener.onIncomingCall(mock(IImsCallSession.class), null, Bundle.EMPTY);
         verify(mImsPhone, times(1)).notifyNewRingingConnection((Connection) any());
         verify(mImsPhone, times(1)).notifyIncomingRing();
         assertEquals(PhoneConstants.State.RINGING, mCTUT.getState());
@@ -1781,7 +1852,7 @@
         connection.addListener(mImsPhoneConnectionListener);
 
         // Move the connection to the handover state.
-        mCTUT.notifySrvccState(Call.SrvccState.COMPLETED);
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_COMPLETED);
         assertEquals(1, mCTUT.mHandoverCall.getConnections().size());
 
         // Make sure the tracker states it's idle.
@@ -1892,7 +1963,7 @@
         startOutgoingCall();
 
         // Move the connection to the handover state.
-        mCTUT.notifySrvccState(Call.SrvccState.COMPLETED);
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_COMPLETED);
 
         try {
             // When trigger CallSessionUpdated after Srvcc completes, checking no exception.
@@ -1926,6 +1997,572 @@
         ImsPhoneConnection connection2 = placeCall();
     }
 
+    @Test
+    @SmallTest
+    public void testConvertToSrvccConnectionInfoNotSupported() throws Exception {
+        // setup ImsPhoneCallTracker's mConnections
+        ImsPhoneConnection activeMO = getImsPhoneConnection(Call.State.ACTIVE, "1234", false);
+        ImsPhoneConnection heldMT = getImsPhoneConnection(Call.State.HOLDING, "5678", true);
+
+        ArrayList<ImsPhoneConnection> connections = new ArrayList<ImsPhoneConnection>();
+        replaceInstance(ImsPhoneCallTracker.class, "mConnections", mCTUT, connections);
+        connections.add(activeMO);
+        connections.add(heldMT);
+
+        ImsCallProfile activeProfile = getImsCallProfileForSrvccSync("activeCall", activeMO, false);
+        ImsCallProfile heldProfile = getImsCallProfileForSrvccSync("heldCall", heldMT, false);
+
+        // setup the response of notifySrvccStarted
+        List<SrvccCall> profiles = new ArrayList<>();
+
+        SrvccConnection[] srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNull(srvccConnections);
+
+        // active call
+        SrvccCall srvccProfile = new SrvccCall(
+                "activeCall", PRECISE_CALL_STATE_ACTIVE, activeProfile);
+        profiles.add(srvccProfile);
+
+        PersistableBundle bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {});
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNull(srvccConnections);
+    }
+
+    @Test
+    @SmallTest
+    public void testConvertToSrvccConnectionInfoBasicSrvcc() throws Exception {
+        // setup ImsPhoneCallTracker's mConnections
+        ImsPhoneConnection activeMO = getImsPhoneConnection(Call.State.ACTIVE, "1234", false);
+        ImsPhoneConnection heldMT = getImsPhoneConnection(Call.State.HOLDING, "5678", true);
+
+        ArrayList<ImsPhoneConnection> connections = new ArrayList<ImsPhoneConnection>();
+        replaceInstance(ImsPhoneCallTracker.class, "mConnections", mCTUT, connections);
+        connections.add(activeMO);
+        connections.add(heldMT);
+
+        ImsCallProfile activeProfile = getImsCallProfileForSrvccSync("activeCall", activeMO, false);
+        ImsCallProfile heldProfile = getImsCallProfileForSrvccSync("heldCall", heldMT, false);
+
+        // setup the response of notifySrvccStarted
+        List<SrvccCall> profiles = new ArrayList<>();
+
+        // active call
+        SrvccCall srvccProfile = new SrvccCall(
+                "activeCall", PRECISE_CALL_STATE_ACTIVE, activeProfile);
+        profiles.add(srvccProfile);
+
+        PersistableBundle bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {
+                        BASIC_SRVCC_SUPPORT,
+                });
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        SrvccConnection[] srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNotNull(srvccConnections);
+        assertTrue(srvccConnections.length == 1);
+        assertTrue(srvccConnections[0].getState() == Call.State.ACTIVE);
+        assertEquals("1234", srvccConnections[0].getNumber());
+    }
+
+    @Test
+    @SmallTest
+    public void testConvertToSrvccConnectionInfoMoAlerting() throws Exception {
+        // setup ImsPhoneCallTracker's mConnections
+        ImsPhoneConnection alertingMO = getImsPhoneConnection(Call.State.ALERTING, "1234", false);
+
+        ArrayList<ImsPhoneConnection> connections = new ArrayList<ImsPhoneConnection>();
+        replaceInstance(ImsPhoneCallTracker.class, "mConnections", mCTUT, connections);
+        connections.add(alertingMO);
+
+        ImsCallProfile alertingProfile = getImsCallProfileForSrvccSync("alertingCall", null, true);
+
+        // setup the response of notifySrvccStarted
+        List<SrvccCall> profiles = new ArrayList<>();
+
+        // alerting call, with local ringback tone
+        SrvccCall srvccProfile = new SrvccCall(
+                "alertingCall", PRECISE_CALL_STATE_ALERTING, alertingProfile);
+        profiles.add(srvccProfile);
+
+        PersistableBundle bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {
+                        BASIC_SRVCC_SUPPORT,
+                });
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        SrvccConnection[] srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNull(srvccConnections);
+
+        bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {
+                        BASIC_SRVCC_SUPPORT,
+                        ALERTING_SRVCC_SUPPORT,
+                });
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNotNull(srvccConnections);
+        assertTrue(srvccConnections.length == 1);
+        assertTrue(srvccConnections[0].getState() == Call.State.ALERTING);
+        assertTrue(srvccConnections[0].getRingbackToneType() == SrvccConnection.TONE_LOCAL);
+
+        profiles.clear();
+
+        // alerting call, with network ringback tone
+        alertingProfile = getImsCallProfileForSrvccSync("alertingCall", null, false);
+
+        srvccProfile = new SrvccCall(
+                "alertingCall", PRECISE_CALL_STATE_ALERTING, alertingProfile);
+        profiles.add(srvccProfile);
+
+        srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNotNull(srvccConnections);
+        assertTrue(srvccConnections.length == 1);
+        assertTrue(srvccConnections[0].getState() == Call.State.ALERTING);
+        assertTrue(srvccConnections[0].getRingbackToneType() == SrvccConnection.TONE_NETWORK);
+    }
+
+    @Test
+    @SmallTest
+    public void testConvertToSrvccConnectionInfoMtAlerting() throws Exception {
+        // setup ImsPhoneCallTracker's mConnections
+        ImsPhoneConnection alertingMT = getImsPhoneConnection(Call.State.INCOMING, "1234", false);
+
+        ArrayList<ImsPhoneConnection> connections = new ArrayList<ImsPhoneConnection>();
+        replaceInstance(ImsPhoneCallTracker.class, "mConnections", mCTUT, connections);
+        connections.add(alertingMT);
+
+        ImsCallProfile incomingProfile =
+                getImsCallProfileForSrvccSync("incomingCall", alertingMT, false);
+
+        // setup the response of notifySrvccStarted
+        List<SrvccCall> profiles = new ArrayList<>();
+
+        SrvccCall srvccProfile = new SrvccCall(
+                "incomingCall", PRECISE_CALL_STATE_INCOMING, incomingProfile);
+        profiles.add(srvccProfile);
+
+        PersistableBundle bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {
+                        BASIC_SRVCC_SUPPORT,
+                });
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        SrvccConnection[] srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNull(srvccConnections);
+
+        bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {
+                        ALERTING_SRVCC_SUPPORT,
+                });
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNotNull(srvccConnections);
+        assertTrue(srvccConnections.length == 1);
+        assertTrue(srvccConnections[0].getState() == Call.State.INCOMING);
+    }
+
+    @Test
+    @SmallTest
+    public void testConvertToSrvccConnectionInfoMtPreAlerting() throws Exception {
+        // setup the response of notifySrvccStarted
+        List<SrvccCall> profiles = new ArrayList<>();
+
+        ImsCallProfile incomingProfile = getImsCallProfileForSrvccSync("incomingCall", null, false);
+
+        SrvccCall srvccProfile = new SrvccCall(
+                "incomingCallSetup", PRECISE_CALL_STATE_INCOMING_SETUP, incomingProfile);
+        profiles.add(srvccProfile);
+
+        PersistableBundle bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {
+                        BASIC_SRVCC_SUPPORT,
+                        ALERTING_SRVCC_SUPPORT,
+                });
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        SrvccConnection[] srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNull(srvccConnections);
+
+        bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {
+                        BASIC_SRVCC_SUPPORT,
+                        ALERTING_SRVCC_SUPPORT,
+                        PREALERTING_SRVCC_SUPPORT,
+                });
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNotNull(srvccConnections);
+        assertTrue(srvccConnections.length == 1);
+        assertTrue(srvccConnections[0].getState() == Call.State.INCOMING);
+        assertTrue(srvccConnections[0].getSubState() == SrvccConnection.SUBSTATE_PREALERTING);
+    }
+
+    @Test
+    @SmallTest
+    public void testNotifySrvccStateStarted() throws Exception {
+        PersistableBundle bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {
+                        BASIC_SRVCC_SUPPORT,
+                });
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        mSrvccStartedCallback = null;
+        doAnswer(new Answer<Void>() {
+            @Override
+            public Void answer(InvocationOnMock invocation) throws Throwable {
+                mSrvccStartedCallback = (ISrvccStartedCallback) invocation.getArguments()[0];
+                return null;
+            }
+        }).when(mImsManager).notifySrvccStarted(any(ISrvccStartedCallback.class));
+
+        verify(mImsManager, times(0)).notifySrvccStarted(any());
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_STARTED);
+        verify(mImsManager, times(1)).notifySrvccStarted(any());
+        assertNotNull(mSrvccStartedCallback);
+
+        // setup ImsPhoneCallTracker's mConnections
+        ImsPhoneConnection activeMO = getImsPhoneConnection(Call.State.ACTIVE, "1234", false);
+
+        ArrayList<ImsPhoneConnection> connections = new ArrayList<ImsPhoneConnection>();
+        replaceInstance(ImsPhoneCallTracker.class, "mConnections", mCTUT, connections);
+        connections.add(activeMO);
+
+        ImsCallProfile activeProfile = getImsCallProfileForSrvccSync("activeCall", activeMO, false);
+
+        // setup the response of notifySrvccStarted
+        List<SrvccCall> profiles = new ArrayList<>();
+
+        // active call
+        SrvccCall srvccProfile = new SrvccCall(
+                "activeCall", PRECISE_CALL_STATE_ACTIVE, activeProfile);
+        profiles.add(srvccProfile);
+
+        mSrvccStartedCallback.onSrvccCallNotified(profiles);
+        SrvccConnection[] srvccConnections = mSimulatedCommands.getSrvccConnections();
+
+        assertNotNull(srvccConnections);
+        assertTrue(srvccConnections.length == 1);
+        assertTrue(srvccConnections[0].getState() == Call.State.ACTIVE);
+        assertEquals("1234", srvccConnections[0].getNumber());
+    }
+
+    @Test
+    @SmallTest
+    public void testNotifySrvccStateFailed() throws Exception {
+        verify(mImsManager, times(0)).notifySrvccFailed();
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_FAILED);
+        verify(mImsManager, times(1)).notifySrvccFailed();
+    }
+
+    @Test
+    @SmallTest
+    public void testNotifySrvccStateCanceled() throws Exception {
+        verify(mImsManager, times(0)).notifySrvccCanceled();
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_CANCELED);
+        verify(mImsManager, times(1)).notifySrvccCanceled();
+    }
+
+    @Test
+    @SmallTest
+    public void testNotifySrvccStateCompleted() throws Exception {
+        verify(mImsManager, times(0)).notifySrvccCompleted();
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_COMPLETED);
+        verify(mImsManager, times(1)).notifySrvccCompleted();
+    }
+
+    @Test
+    @SmallTest
+    public void testConvertToSrvccConnectionInfoConferenceCall() throws Exception {
+        // setup ImsPhoneCallTracker's mConnections
+        ImsPhoneConnection activeMO = getImsPhoneConnection(Call.State.ACTIVE, "1234", false);
+
+        ArrayList<ImsPhoneConnection> connections = new ArrayList<ImsPhoneConnection>();
+        replaceInstance(ImsPhoneCallTracker.class, "mConnections", mCTUT, connections);
+        connections.add(activeMO);
+
+        List<ConferenceParticipant> participants = new ArrayList<ConferenceParticipant>();
+        participants.add(new ConferenceParticipant(Uri.parse("tel:1234"), "", null,
+                  android.telecom.Connection.STATE_ACTIVE,
+                  android.telecom.Call.Details.DIRECTION_INCOMING));
+        participants.add(new ConferenceParticipant(Uri.parse("tel:5678"), "", null,
+                  android.telecom.Connection.STATE_ACTIVE,
+                  android.telecom.Call.Details.DIRECTION_OUTGOING));
+
+        ImsCallProfile activeProfile = getImsCallProfileForSrvccSync("activeCall",
+                activeMO, false, participants);
+
+        // setup the response of notifySrvccStarted
+        List<SrvccCall> profiles = new ArrayList<>();
+
+        SrvccConnection[] srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNull(srvccConnections);
+
+        // active call
+        SrvccCall srvccProfile = new SrvccCall(
+                "activeCall", PRECISE_CALL_STATE_ACTIVE, activeProfile);
+        profiles.add(srvccProfile);
+
+        PersistableBundle bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {
+                        BASIC_SRVCC_SUPPORT,
+                });
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNotNull(srvccConnections);
+        assertTrue(srvccConnections.length == 1);
+        assertTrue(srvccConnections[0].getState() == Call.State.ACTIVE);
+        assertFalse(srvccConnections[0].isMultiParty());
+        assertEquals("1234", srvccConnections[0].getNumber());
+
+        bundle = mContextFixture.getCarrierConfigBundle();
+        bundle.putIntArray(
+                CarrierConfigManager.ImsVoice.KEY_SRVCC_TYPE_INT_ARRAY,
+                new int[] {
+                        BASIC_SRVCC_SUPPORT,
+                        MIDCALL_SRVCC_SUPPORT
+                });
+        mCTUT.updateCarrierConfigCache(bundle);
+
+        srvccConnections = mCTUT.convertToSrvccConnectionInfo(profiles);
+        assertNotNull(srvccConnections);
+        assertTrue(srvccConnections.length == 2);
+
+        assertTrue(srvccConnections[0].getState() == Call.State.ACTIVE);
+        assertTrue(srvccConnections[0].isMultiParty());
+        assertTrue(srvccConnections[0].isIncoming());
+        assertEquals("1234", srvccConnections[0].getNumber());
+
+        assertTrue(srvccConnections[1].getState() == Call.State.ACTIVE);
+        assertTrue(srvccConnections[1].isMultiParty());
+        assertFalse(srvccConnections[1].isIncoming());
+        assertEquals("5678", srvccConnections[1].getNumber());
+    }
+
+    /**
+     * Verifies that the expected access network tech and IMS features are notified
+     * to ImsPhone when capabilities are changed.
+     */
+    @Test
+    @SmallTest
+    public void testUpdateImsRegistrationInfo() {
+        // LTE is registered.
+        doReturn(ImsRegistrationImplBase.REGISTRATION_TECH_LTE).when(
+                mImsManager).getRegistrationTech();
+
+        // enable Voice and Video
+        MmTelFeature.MmTelCapabilities caps = new MmTelFeature.MmTelCapabilities();
+        caps.addCapabilities(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE);
+        caps.addCapabilities(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO);
+        mCapabilityCallback.onCapabilitiesStatusChanged(caps);
+        processAllMessages();
+
+        verify(mImsPhone, times(1)).updateImsRegistrationInfo(
+                eq(CommandsInterface.IMS_MMTEL_CAPABILITY_VOICE
+                        | CommandsInterface.IMS_MMTEL_CAPABILITY_VIDEO));
+
+        // enable SMS
+        caps = new MmTelFeature.MmTelCapabilities();
+        caps.addCapabilities(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_SMS);
+        mCapabilityCallback.onCapabilitiesStatusChanged(caps);
+        processAllMessages();
+
+        verify(mImsPhone, times(1)).updateImsRegistrationInfo(
+                eq(CommandsInterface.IMS_MMTEL_CAPABILITY_SMS));
+    }
+
+    @Test
+    @SmallTest
+    public void testDomainSelectionAlternateService() {
+        startOutgoingCall();
+        ImsPhoneConnection c = mCTUT.mForegroundCall.getFirstConnection();
+        mImsCallProfile.setEmergencyServiceCategories(EMERGENCY_SERVICE_CATEGORY_AMBULANCE);
+        mImsCallListener.onCallStartFailed(mSecondImsCall,
+                new ImsReasonInfo(ImsReasonInfo.CODE_SIP_ALTERNATE_EMERGENCY_CALL, -1));
+        processAllMessages();
+        EmergencyNumber emergencyNumber = c.getEmergencyNumberInfo();
+        assertNotNull(emergencyNumber);
+        assertEquals(EMERGENCY_SERVICE_CATEGORY_AMBULANCE,
+                emergencyNumber.getEmergencyServiceCategoryBitmask());
+    }
+
+    @Test
+    public void testUpdateImsCallStatusIncoming() throws Exception {
+        // Incoming call
+        ImsPhoneConnection connection = setupRingingConnection();
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(any(), any());
+
+        // Disconnect the call
+        mImsCallListener.onCallTerminated(connection.getImsCall(),
+                new ImsReasonInfo(ImsReasonInfo.CODE_USER_TERMINATED_BY_REMOTE, 0));
+
+        verify(mImsPhone, times(2)).updateImsCallStatus(any(), any());
+    }
+
+    @Test
+    public void testUpdateImsCallStatus() throws Exception {
+        // Dialing
+        ImsPhoneConnection connection = placeCall();
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(any(), any());
+
+        // Alerting
+        ImsCall imsCall = connection.getImsCall();
+        imsCall.getImsCallSessionListenerProxy().callSessionProgressing(imsCall.getSession(),
+                new ImsStreamMediaProfile());
+
+        verify(mImsPhone, times(2)).updateImsCallStatus(any(), any());
+
+        // Active
+        imsCall.getImsCallSessionListenerProxy().callSessionStarted(imsCall.getSession(),
+                new ImsCallProfile());
+
+        verify(mImsPhone, times(3)).updateImsCallStatus(any(), any());
+
+        // Held by remote
+        mCTUT.onCallHoldReceived(imsCall);
+
+        verify(mImsPhone, times(4)).updateImsCallStatus(any(), any());
+
+        // Resumed by remote
+        mImsCallListener.onCallResumeReceived(imsCall);
+
+        verify(mImsPhone, times(5)).updateImsCallStatus(any(), any());
+
+        // Disconnecting and then Disconnected
+        mCTUT.hangup(connection);
+        mImsCallListener.onCallTerminated(imsCall,
+                new ImsReasonInfo(ImsReasonInfo.CODE_USER_TERMINATED, 0));
+
+        verify(mImsPhone, times(7)).updateImsCallStatus(any(), any());
+    }
+
+    @Test
+    public void testUpdateImsCallStatusSrvccCompleted() throws Exception {
+        // Incoming call
+        setupRingingConnection();
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(any(), any());
+
+        // no interaction when SRVCC has started, failed, or canceled.
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_STARTED);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(any(), any());
+
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_FAILED);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(any(), any());
+
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_CANCELED);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(any(), any());
+
+        // interaction when SRVCC has completed
+        mCTUT.notifySrvccState(SRVCC_STATE_HANDOVER_COMPLETED);
+
+        verify(mImsPhone, times(2)).updateImsCallStatus(any(), any());
+    }
+
+    @Test
+    public void testClearAllOrphanedConnectionInfo() throws Exception {
+        verify(mImsPhone, times(0)).updateImsCallStatus(any(), any());
+
+        mConnectorListener.connectionUnavailable(FeatureConnector.UNAVAILABLE_REASON_DISCONNECTED);
+
+        verify(mImsPhone, times(1)).updateImsCallStatus(any(), any());
+    }
+
+    /** Verifies that the request from ImsService is passed to ImsPhone as expected. */
+    @Test
+    @SmallTest
+    public void testStartAndStopImsTrafficSession() {
+        IImsTrafficSessionCallback binder = Mockito.mock(IImsTrafficSessionCallback.class);
+        mMmTelListener.onStartImsTrafficSession(1, MmTelFeature.IMS_TRAFFIC_TYPE_EMERGENCY,
+                AccessNetworkConstants.AccessNetworkType.EUTRAN,
+                IMS_TRAFFIC_DIRECTION_OUTGOING, binder);
+        verify(mImsPhone, times(1)).startImsTraffic(eq(1),
+                eq(MmTelFeature.IMS_TRAFFIC_TYPE_EMERGENCY),
+                eq(AccessNetworkConstants.AccessNetworkType.EUTRAN),
+                eq(IMS_TRAFFIC_DIRECTION_OUTGOING), any());
+
+        mMmTelListener.onStopImsTrafficSession(1);
+        verify(mImsPhone, times(1)).stopImsTraffic(eq(1), any());
+
+        mMmTelListener.onStartImsTrafficSession(2, MmTelFeature.IMS_TRAFFIC_TYPE_EMERGENCY_SMS,
+                AccessNetworkConstants.AccessNetworkType.IWLAN,
+                IMS_TRAFFIC_DIRECTION_OUTGOING, binder);
+        verify(mImsPhone, times(1)).startImsTraffic(eq(2),
+                eq(MmTelFeature.IMS_TRAFFIC_TYPE_EMERGENCY_SMS),
+                eq(AccessNetworkConstants.AccessNetworkType.IWLAN),
+                eq(IMS_TRAFFIC_DIRECTION_OUTGOING), any());
+
+        mMmTelListener.onStartImsTrafficSession(3, MmTelFeature.IMS_TRAFFIC_TYPE_VOICE,
+                AccessNetworkConstants.AccessNetworkType.EUTRAN,
+                IMS_TRAFFIC_DIRECTION_INCOMING, binder);
+        verify(mImsPhone, times(1)).startImsTraffic(eq(3),
+                eq(MmTelFeature.IMS_TRAFFIC_TYPE_VOICE),
+                eq(AccessNetworkConstants.AccessNetworkType.EUTRAN),
+                eq(IMS_TRAFFIC_DIRECTION_INCOMING), any());
+
+        mMmTelListener.onStartImsTrafficSession(4, MmTelFeature.IMS_TRAFFIC_TYPE_VIDEO,
+                AccessNetworkConstants.AccessNetworkType.EUTRAN,
+                IMS_TRAFFIC_DIRECTION_OUTGOING, binder);
+        verify(mImsPhone, times(1)).startImsTraffic(eq(4),
+                eq(MmTelFeature.IMS_TRAFFIC_TYPE_VIDEO),
+                eq(AccessNetworkConstants.AccessNetworkType.EUTRAN),
+                eq(IMS_TRAFFIC_DIRECTION_OUTGOING), any());
+
+        mMmTelListener.onStartImsTrafficSession(5, MmTelFeature.IMS_TRAFFIC_TYPE_SMS,
+                AccessNetworkConstants.AccessNetworkType.EUTRAN,
+                IMS_TRAFFIC_DIRECTION_OUTGOING, binder);
+        verify(mImsPhone, times(1)).startImsTraffic(eq(5),
+                eq(MmTelFeature.IMS_TRAFFIC_TYPE_SMS),
+                eq(AccessNetworkConstants.AccessNetworkType.EUTRAN),
+                eq(IMS_TRAFFIC_DIRECTION_OUTGOING), any());
+
+        mMmTelListener.onStartImsTrafficSession(6, MmTelFeature.IMS_TRAFFIC_TYPE_REGISTRATION,
+                AccessNetworkConstants.AccessNetworkType.EUTRAN,
+                IMS_TRAFFIC_DIRECTION_OUTGOING, binder);
+        verify(mImsPhone, times(1)).startImsTraffic(eq(6),
+                eq(MmTelFeature.IMS_TRAFFIC_TYPE_REGISTRATION),
+                eq(AccessNetworkConstants.AccessNetworkType.EUTRAN),
+                eq(IMS_TRAFFIC_DIRECTION_OUTGOING), any());
+
+        mMmTelListener.onModifyImsTrafficSession(6,
+                AccessNetworkConstants.AccessNetworkType.IWLAN);
+        verify(mImsPhone, times(1)).startImsTraffic(eq(6),
+                eq(MmTelFeature.IMS_TRAFFIC_TYPE_REGISTRATION),
+                eq(AccessNetworkConstants.AccessNetworkType.IWLAN),
+                eq(IMS_TRAFFIC_DIRECTION_OUTGOING), any());
+    }
+
     private void sendCarrierConfigChanged() {
         Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
         intent.putExtra(CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX, mPhone.getSubId());
@@ -2001,5 +2638,45 @@
         }
         return connection;
     }
+
+    private ImsPhoneConnection getImsPhoneConnection(Call.State state,
+            String number, boolean isIncoming) {
+        ImsPhoneCall call = mock(ImsPhoneCall.class);
+        doReturn(state).when(call).getState();
+
+        ImsPhoneConnection c = mock(ImsPhoneConnection.class);
+        doReturn(state).when(c).getState();
+        doReturn(isIncoming).when(c).isIncoming();
+        doReturn(call).when(c).getCall();
+        doReturn(number).when(c).getAddress();
+
+        return c;
+    }
+
+    private ImsCallProfile getImsCallProfileForSrvccSync(String callId,
+            ImsPhoneConnection c, boolean localTone) {
+        return getImsCallProfileForSrvccSync(callId, c, localTone, null);
+    }
+
+    private ImsCallProfile getImsCallProfileForSrvccSync(String callId,
+            ImsPhoneConnection c, boolean localTone, List<ConferenceParticipant> participants) {
+        ImsStreamMediaProfile mediaProfile = new ImsStreamMediaProfile(0,
+                localTone ? DIRECTION_INACTIVE : DIRECTION_SEND_RECEIVE, 0, 0, 0);
+        ImsCallProfile profile = new ImsCallProfile(0, 0, null, mediaProfile);
+
+        if (c != null) {
+            ImsCallSession session = mock(ImsCallSession.class);
+            doReturn(callId).when(session).getCallId();
+
+            ImsCall imsCall = mock(ImsCall.class);
+            doReturn(profile).when(imsCall).getCallProfile();
+            doReturn(session).when(imsCall).getCallSession();
+            doReturn(participants).when(imsCall).getConferenceParticipants();
+
+            doReturn(imsCall).when(c).getImsCall();
+        }
+
+        return profile;
+    }
 }
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneConnectionTest.java b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneConnectionTest.java
index 9779cfa..396466c 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneConnectionTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneConnectionTest.java
@@ -58,6 +58,7 @@
 import com.android.internal.telephony.GsmCdmaCall;
 import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.TelephonyTest;
+import com.android.internal.telephony.imsphone.ImsPhone.ImsDialArgs;
 import com.android.internal.telephony.metrics.TelephonyMetrics;
 import com.android.internal.telephony.metrics.VoiceCallSessionStats;
 
@@ -144,7 +145,8 @@
 
         logd("Testing initial state of MO ImsPhoneConnection");
         mConnectionUT = new ImsPhoneConnection(mImsPhone, String.format("+1 (700).555-41NN%c1234",
-                PhoneNumberUtils.PAUSE), mImsCT, mForeGroundCall, false, false);
+                PhoneNumberUtils.PAUSE), mImsCT, mForeGroundCall, false, false,
+                new ImsDialArgs.Builder().build());
         assertEquals(PhoneConstants.PRESENTATION_ALLOWED, mConnectionUT.getNumberPresentation());
         assertEquals(PhoneConstants.PRESENTATION_ALLOWED, mConnectionUT.getCnapNamePresentation());
         assertEquals("+1 (700).555-41NN,1234", mConnectionUT.getOrigDialString());
@@ -157,7 +159,7 @@
     public void testImsUpdateStateForeGround() {
         // MO Foreground Connection dailing -> active
         mConnectionUT = new ImsPhoneConnection(mImsPhone, "+1 (700).555-41NN1234", mImsCT,
-                mForeGroundCall, false, false);
+                mForeGroundCall, false, false, new ImsDialArgs.Builder().build());
         // initially in dialing state
         doReturn(Call.State.DIALING).when(mForeGroundCall).getState();
         assertTrue(mConnectionUT.update(mImsCall, Call.State.ACTIVE));
@@ -172,7 +174,7 @@
     public void testUpdateCodec() {
         // MO Foreground Connection dailing -> active
         mConnectionUT = new ImsPhoneConnection(mImsPhone, "+1 (700).555-41NN1234", mImsCT,
-                mForeGroundCall, false, false);
+                mForeGroundCall, false, false, new ImsDialArgs.Builder().build());
         doReturn(Call.State.ACTIVE).when(mForeGroundCall).getState();
         assertTrue(mConnectionUT.updateMediaCapabilities(mImsCall));
     }
@@ -195,7 +197,7 @@
     @SmallTest
     public void testImsUpdateStatePendingHold() {
         mConnectionUT = new ImsPhoneConnection(mImsPhone, "+1 (700).555-41NN1234", mImsCT,
-                mForeGroundCall, false, false);
+                mForeGroundCall, false, false, new ImsDialArgs.Builder().build());
         doReturn(true).when(mImsCall).isPendingHold();
         assertFalse(mConnectionUT.update(mImsCall, Call.State.ACTIVE));
         verify(mForeGroundCall, times(0)).update(eq(mConnectionUT), eq(mImsCall),
@@ -240,7 +242,8 @@
     @SmallTest
     public void testPostDialWait() {
         mConnectionUT = new ImsPhoneConnection(mImsPhone, String.format("+1 (700).555-41NN%c1234",
-                PhoneNumberUtils.WAIT), mImsCT, mForeGroundCall, false, false);
+                PhoneNumberUtils.WAIT), mImsCT, mForeGroundCall, false, false,
+                new ImsDialArgs.Builder().build());
         doReturn(Call.State.DIALING).when(mForeGroundCall).getState();
         doAnswer(new Answer() {
             @Override
@@ -263,7 +266,8 @@
     @MediumTest
     public void testPostDialPause() {
         mConnectionUT = new ImsPhoneConnection(mImsPhone, String.format("+1 (700).555-41NN%c1234",
-                PhoneNumberUtils.PAUSE), mImsCT, mForeGroundCall, false, false);
+                PhoneNumberUtils.PAUSE), mImsCT, mForeGroundCall, false, false,
+                new ImsDialArgs.Builder().build());
         doReturn(Call.State.DIALING).when(mForeGroundCall).getState();
         doAnswer(new Answer() {
             @Override
@@ -385,7 +389,7 @@
                 {"12345*00000", "12346", "12346"}};
         for (String[] testAddress : testAddressMappingSet) {
             mConnectionUT = new ImsPhoneConnection(mImsPhone, testAddress[0], mImsCT,
-                    mForeGroundCall, false, false);
+                    mForeGroundCall, false, false, new ImsDialArgs.Builder().build());
             mConnectionUT.setIsIncoming(true);
             mImsCallProfile.setCallExtra(ImsCallProfile.EXTRA_OI, testAddress[1]);
             mConnectionUT.updateAddressDisplay(mImsCall);
@@ -403,7 +407,7 @@
         String updateAddress = "6789";
 
         mConnectionUT = new ImsPhoneConnection(mImsPhone, inputAddress, mImsCT, mForeGroundCall,
-                false, false);
+                false, false, new ImsDialArgs.Builder().build());
         mConnectionUT.setIsIncoming(false);
         mImsCallProfile.setCallExtra(ImsCallProfile.EXTRA_OI, updateAddress);
         mConnectionUT.updateAddressDisplay(mImsCall);
diff --git a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneMmiCodeTest.java b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneMmiCodeTest.java
index 3cbf8bd..d8173a2 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneMmiCodeTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneMmiCodeTest.java
@@ -29,6 +29,7 @@
 import android.os.PersistableBundle;
 import android.telephony.CarrierConfigManager;
 import android.telephony.ServiceState;
+import android.test.suitebuilder.annotation.SmallTest;
 import android.testing.AndroidTestingRunner;
 import android.testing.TestableLooper;
 
@@ -126,6 +127,7 @@
      * Ensure that when an operation is not supported that the correct message is returned.
      */
     @Test
+    @SmallTest
     public void testOperationNotSupported() {
         mImsPhoneMmiCode = ImsPhoneMmiCode.newNetworkInitiatedUssd(null, true, mImsPhoneUT);
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneTest.java b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneTest.java
index 17a63dd..4b8cbab 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneTest.java
@@ -22,9 +22,19 @@
 import static android.telephony.CarrierConfigManager.USSD_OVER_CS_PREFERRED;
 import static android.telephony.CarrierConfigManager.USSD_OVER_IMS_ONLY;
 import static android.telephony.CarrierConfigManager.USSD_OVER_IMS_PREFERRED;
+import static android.telephony.ims.RegistrationManager.SUGGESTED_ACTION_NONE;
+import static android.telephony.ims.RegistrationManager.SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK;
+import static android.telephony.ims.RegistrationManager.SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK_WITH_TIMEOUT;
+import static android.telephony.ims.stub.ImsRegistrationImplBase.REGISTRATION_TECH_3G;
+import static android.telephony.ims.stub.ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN;
+import static android.telephony.ims.stub.ImsRegistrationImplBase.REGISTRATION_TECH_LTE;
+import static android.telephony.ims.stub.ImsRegistrationImplBase.REGISTRATION_TECH_NR;
 
 import static com.android.internal.telephony.CommandsInterface.CF_ACTION_ENABLE;
 import static com.android.internal.telephony.CommandsInterface.CF_REASON_UNCONDITIONAL;
+import static com.android.internal.telephony.CommandsInterface.IMS_MMTEL_CAPABILITY_SMS;
+import static com.android.internal.telephony.CommandsInterface.IMS_MMTEL_CAPABILITY_VIDEO;
+import static com.android.internal.telephony.CommandsInterface.IMS_MMTEL_CAPABILITY_VOICE;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -65,6 +75,7 @@
 import android.telephony.TelephonyManager;
 import android.telephony.ims.ImsCallProfile;
 import android.telephony.ims.ImsReasonInfo;
+import android.telephony.ims.ImsRegistrationAttributes;
 import android.telephony.ims.RegistrationManager;
 import android.telephony.ims.stub.ImsRegistrationImplBase;
 import android.telephony.ims.stub.ImsUtImplBase;
@@ -734,7 +745,6 @@
     @Test
     @SmallTest
     public void testRoamingToAirplanModeIwlanInService() throws Exception {
-        doReturn(true).when(mAccessNetworksManager).isInLegacyMode();
         doReturn(PhoneConstants.State.IDLE).when(mImsCT).getState();
         doReturn(true).when(mPhone).isRadioOn();
 
@@ -762,7 +772,6 @@
     @Test
     @SmallTest
     public void testRoamingToOutOfService() throws Exception {
-        doReturn(true).when(mAccessNetworksManager).isInLegacyMode();
         doReturn(PhoneConstants.State.IDLE).when(mImsCT).getState();
         doReturn(true).when(mPhone).isRadioOn();
 
@@ -786,83 +795,6 @@
     }
 
     @Test
-    @SmallTest
-    public void testRoamingChangeForLteInLegacyMode() throws Exception {
-        doReturn(true).when(mAccessNetworksManager).isInLegacyMode();
-        doReturn(PhoneConstants.State.IDLE).when(mImsCT).getState();
-        doReturn(true).when(mPhone).isRadioOn();
-
-        //roaming - data registration only on LTE
-        Message m = getServiceStateChangedMessage(getServiceStateDataOnly(
-                ServiceState.RIL_RADIO_TECHNOLOGY_LTE, ServiceState.STATE_IN_SERVICE, true));
-        // Inject the message synchronously instead of waiting for the thread to do it.
-        mImsPhoneUT.handleMessage(m);
-        m.recycle();
-
-        verify(mImsManager, times(1)).setWfcMode(anyInt(), eq(true));
-
-        // not roaming - data registration on LTE
-        m = getServiceStateChangedMessage(getServiceStateDataOnly(
-                ServiceState.RIL_RADIO_TECHNOLOGY_LTE, ServiceState.STATE_IN_SERVICE, false));
-        mImsPhoneUT.handleMessage(m);
-        m.recycle();
-
-        verify(mImsManager, times(1)).setWfcMode(anyInt(), eq(false));
-    }
-
-    @Test
-    @SmallTest
-    public void testDataOnlyRoamingCellToIWlanInLegacyMode() throws Exception {
-        doReturn(true).when(mAccessNetworksManager).isInLegacyMode();
-        doReturn(PhoneConstants.State.IDLE).when(mImsCT).getState();
-        doReturn(true).when(mPhone).isRadioOn();
-
-        //roaming - data registration only on LTE
-        Message m = getServiceStateChangedMessage(getServiceStateDataOnly(
-                ServiceState.RIL_RADIO_TECHNOLOGY_LTE, ServiceState.STATE_IN_SERVICE, true));
-        // Inject the message synchronously instead of waiting for the thread to do it.
-        mImsPhoneUT.handleMessage(m);
-        m.recycle();
-
-        verify(mImsManager, times(1)).setWfcMode(anyInt(), eq(true));
-
-        // not roaming - data registration onto IWLAN
-        m = getServiceStateChangedMessage(getServiceStateDataOnly(
-                ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN, ServiceState.STATE_IN_SERVICE, false));
-        mImsPhoneUT.handleMessage(m);
-        m.recycle();
-
-        // Verify that it hasn't been called again.
-        verify(mImsManager, times(1)).setWfcMode(anyInt(), anyBoolean());
-    }
-
-    @Test
-    @SmallTest
-    public void testCellVoiceDataChangeToWlanInLegacyMode() throws Exception {
-        doReturn(true).when(mAccessNetworksManager).isInLegacyMode();
-        doReturn(PhoneConstants.State.IDLE).when(mImsCT).getState();
-        doReturn(true).when(mPhone).isRadioOn();
-
-        //roaming - voice/data registration on LTE
-        ServiceState ss = getServiceStateDataAndVoice(
-                ServiceState.RIL_RADIO_TECHNOLOGY_LTE, ServiceState.STATE_IN_SERVICE, true);
-        Message m = getServiceStateChangedMessage(ss);
-        // Inject the message synchronously instead of waiting for the thread to do it.
-        mImsPhoneUT.handleMessage(m);
-
-        verify(mImsManager, times(1)).setWfcMode(anyInt(), eq(true));
-
-        // roaming - voice LTE, data registration onto IWLAN
-        modifyServiceStateData(ss, ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN,
-                ServiceState.STATE_IN_SERVICE, false);
-        mImsPhoneUT.handleMessage(m);
-        m.recycle();
-
-        // Verify that it hasn't been called again.
-        verify(mImsManager, times(1)).setWfcMode(anyInt(), anyBoolean());
-    }
-
-    @Test
     public void testNonNullTrackersInImsPhone() throws Exception {
         assertNotNull(mImsPhoneUT.getEmergencyNumberTracker());
         assertNotNull(mImsPhoneUT.getServiceStateTracker());
@@ -1068,6 +1000,329 @@
         mContextFixture.addCallingOrSelfPermission("");
     }
 
+    /**
+     * Verifies that valid radio technology is passed to RIL
+     * when IMS registration state changes to registered.
+     */
+    @Test
+    @SmallTest
+    public void testUpdateImsRegistrationInfoRadioTech() {
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+
+        int[] regInfo = mSimulatedCommands.getImsRegistrationInfo();
+        assertNotNull(regInfo);
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        RegistrationManager.RegistrationCallback registrationCallback =
+                mImsPhoneUT.getImsMmTelRegistrationCallback();
+
+        ImsRegistrationAttributes attr = new ImsRegistrationAttributes.Builder(
+                REGISTRATION_TECH_LTE).build();
+        registrationCallback.onRegistered(attr);
+        mImsPhoneUT.updateImsRegistrationInfo(IMS_MMTEL_CAPABILITY_VOICE);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_LTE
+                && regInfo[3] == IMS_MMTEL_CAPABILITY_VOICE);
+
+        // reset the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // duplicated notification with the same radio technology
+        attr = new ImsRegistrationAttributes.Builder(REGISTRATION_TECH_LTE).build();
+        registrationCallback.onRegistered(attr);
+
+        // verify that there is no change in SimulatedCommands
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // radio technology changed
+        attr = new ImsRegistrationAttributes.Builder(REGISTRATION_TECH_NR).build();
+        registrationCallback.onRegistered(attr);
+
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_NR
+                && regInfo[3] == IMS_MMTEL_CAPABILITY_VOICE);
+
+        // reset the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // duplicated notification with the same radio technology
+        attr = new ImsRegistrationAttributes.Builder(REGISTRATION_TECH_NR).build();
+        registrationCallback.onRegistered(attr);
+
+        // verify that there is no change in SimulatedCommands
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // radio technology changed
+        attr = new ImsRegistrationAttributes.Builder(REGISTRATION_TECH_IWLAN).build();
+        registrationCallback.onRegistered(attr);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_IWLAN
+                && regInfo[3] == IMS_MMTEL_CAPABILITY_VOICE);
+
+        // reset the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // duplicated notification with the same radio technology
+        attr = new ImsRegistrationAttributes.Builder(REGISTRATION_TECH_IWLAN).build();
+        registrationCallback.onRegistered(attr);
+
+        // verify that there is no change in SimulatedCommands
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // radio technology changed
+        attr = new ImsRegistrationAttributes.Builder(REGISTRATION_TECH_3G).build();
+        registrationCallback.onRegistered(attr);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_3G
+                && regInfo[3] == IMS_MMTEL_CAPABILITY_VOICE);
+
+        // reset the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // duplicated notification with the same radio technology
+        attr = new ImsRegistrationAttributes.Builder(REGISTRATION_TECH_3G).build();
+        registrationCallback.onRegistered(attr);
+
+        // verify that there is no change in SimulatedCommands
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+    }
+
+    /**
+     * Verifies that valid capabilities is passed to RIL
+     * when IMS registration state changes to registered.
+     */
+    @Test
+    @SmallTest
+    public void testUpdateImsRegistrationInfoCapabilities() {
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+
+        int[] regInfo = mSimulatedCommands.getImsRegistrationInfo();
+        assertNotNull(regInfo);
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        RegistrationManager.RegistrationCallback registrationCallback =
+                mImsPhoneUT.getImsMmTelRegistrationCallback();
+
+        ImsRegistrationAttributes attr = new ImsRegistrationAttributes.Builder(
+                REGISTRATION_TECH_LTE).build();
+        registrationCallback.onRegistered(attr);
+        mImsPhoneUT.updateImsRegistrationInfo(IMS_MMTEL_CAPABILITY_VOICE);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_LTE
+                && regInfo[3] == IMS_MMTEL_CAPABILITY_VOICE);
+
+        // reset the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // duplicated notification with the same capability
+        mImsPhoneUT.updateImsRegistrationInfo(IMS_MMTEL_CAPABILITY_VOICE);
+
+        // verify that there is no change in SimulatedCommands
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // capability changed
+        mImsPhoneUT.updateImsRegistrationInfo(IMS_MMTEL_CAPABILITY_VIDEO);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_LTE
+                && regInfo[3] == IMS_MMTEL_CAPABILITY_VIDEO);
+
+        // reset the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // duplicated notification with the same capability
+        mImsPhoneUT.updateImsRegistrationInfo(IMS_MMTEL_CAPABILITY_VIDEO);
+
+        // verify that there is no change in SimulatedCommands
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // capability changed
+        mImsPhoneUT.updateImsRegistrationInfo(IMS_MMTEL_CAPABILITY_SMS);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_LTE
+                && regInfo[3] == IMS_MMTEL_CAPABILITY_SMS);
+
+        // reset the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // duplicated notification with the same capability
+        mImsPhoneUT.updateImsRegistrationInfo(IMS_MMTEL_CAPABILITY_SMS);
+
+        // verify that there is no change in SimulatedCommands
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+
+        // capability changed, but no capability
+        mImsPhoneUT.updateImsRegistrationInfo(IMS_MMTEL_CAPABILITY_SMS);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        // verify that there is no change in SimulatedCommands.
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[3] == 0);
+    }
+
+    /**
+     * Verifies that valid state and reason is passed to RIL
+     * when IMS registration state changes to unregistered.
+     */
+    @Test
+    @SmallTest
+    public void testUpdateImsRegistrationInfo() {
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+
+        int[] regInfo = mSimulatedCommands.getImsRegistrationInfo();
+        assertNotNull(regInfo);
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[2] == 0);
+
+        RegistrationManager.RegistrationCallback registrationCallback =
+                mImsPhoneUT.getImsMmTelRegistrationCallback();
+
+        ImsReasonInfo reasonInfo = new ImsReasonInfo(ImsReasonInfo.CODE_REGISTRATION_ERROR,
+                ImsReasonInfo.CODE_UNSPECIFIED, "");
+
+        // unregistered with fatal error
+        registrationCallback.onUnregistered(reasonInfo,
+                SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK, REGISTRATION_TECH_NR);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_NR
+                && regInfo[2] == SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK);
+
+        // reset the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[2] == 0);
+
+        // unregistered with fatal error but rat changed
+        registrationCallback.onUnregistered(reasonInfo,
+                SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK, REGISTRATION_TECH_LTE);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_LTE
+                && regInfo[2] == SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK);
+
+        // reset the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[2] == 0);
+
+        // duplicated notification with the same suggested action
+        registrationCallback.onUnregistered(reasonInfo,
+                SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK, REGISTRATION_TECH_LTE);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        // verify that there is no update in the SimulatedCommands
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[2] == 0);
+
+        // unregistered with repeated error
+        registrationCallback.onUnregistered(reasonInfo,
+                SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK_WITH_TIMEOUT,
+                REGISTRATION_TECH_LTE);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_LTE
+                && regInfo[2] == SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK_WITH_TIMEOUT);
+
+        // reset the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(0, 0, 0, 0, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[2] == 0);
+
+        // duplicated notification with the same suggested action
+        registrationCallback.onUnregistered(reasonInfo,
+                SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK_WITH_TIMEOUT,
+                REGISTRATION_TECH_LTE);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        // verify that there is no update in the SimulatedCommands
+        assertTrue(regInfo[0] == 0 && regInfo[1] == 0 && regInfo[2] == 0);
+
+        // unregistered with temporary error
+        registrationCallback.onUnregistered(reasonInfo,
+                SUGGESTED_ACTION_NONE, REGISTRATION_TECH_LTE);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_LTE
+                && regInfo[2] == SUGGESTED_ACTION_NONE);
+
+        // verifies that reason codes except ImsReasonInfo.CODE_REGISTRATION_ERROR are discarded.
+        reasonInfo = new ImsReasonInfo(ImsReasonInfo.CODE_LOCAL_NETWORK_NO_SERVICE,
+                ImsReasonInfo.CODE_UNSPECIFIED, "");
+        registrationCallback.onUnregistered(reasonInfo,
+                SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK, REGISTRATION_TECH_NR);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED
+                && regInfo[1] == REGISTRATION_TECH_NR
+                && regInfo[2] == SUGGESTED_ACTION_NONE);
+
+        // change the registration info saved in the SimulatedCommands
+        mSimulatedCommands.updateImsRegistrationInfo(1, 1, 1, 1, null);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        assertTrue(regInfo[0] == 1 && regInfo[1] == 1 && regInfo[2] == 1);
+
+        // duplicated notification with the same suggested action
+        registrationCallback.onUnregistered(reasonInfo,
+                SUGGESTED_ACTION_NONE, REGISTRATION_TECH_NR);
+        regInfo = mSimulatedCommands.getImsRegistrationInfo();
+
+        // verify that there is no update in the SimulatedCommands
+        assertTrue(regInfo[0] == 1 && regInfo[1] == 1 && regInfo[2] == 1);
+    }
+
     private ServiceState getServiceStateDataAndVoice(int rat, int regState, boolean isRoaming) {
         ServiceState ss = new ServiceState();
         ss.setStateOutOfService();
diff --git a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsRegistrationCallbackHelperTest.java b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsRegistrationCallbackHelperTest.java
index cce0646..93fbfdc 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsRegistrationCallbackHelperTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsRegistrationCallbackHelperTest.java
@@ -16,10 +16,16 @@
 
 package com.android.internal.telephony.imsphone;
 
+import static android.telephony.ims.RegistrationManager.SUGGESTED_ACTION_NONE;
+import static android.telephony.ims.RegistrationManager.SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK;
+import static android.telephony.ims.stub.ImsRegistrationImplBase.REGISTRATION_TECH_LTE;
+import static android.telephony.ims.stub.ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Matchers.anyInt;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
@@ -27,8 +33,10 @@
 import android.net.Uri;
 import android.telephony.AccessNetworkConstants.AccessNetworkType;
 import android.telephony.ims.ImsReasonInfo;
+import android.telephony.ims.ImsRegistrationAttributes;
 import android.telephony.ims.RegistrationManager;
 import android.telephony.ims.RegistrationManager.RegistrationCallback;
+import android.telephony.ims.stub.ImsRegistrationImplBase;
 import android.test.suitebuilder.annotation.SmallTest;
 
 import com.android.internal.telephony.TelephonyTest;
@@ -121,11 +129,13 @@
 
         // When onRegistered is called, the registration state should be
         // REGISTRATION_STATE_REGISTERED
-        callback.onRegistered(AccessNetworkType.IWLAN);
+        ImsRegistrationAttributes attr = new ImsRegistrationAttributes.Builder(
+                ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN).build();
+        callback.onRegistered(attr);
 
         assertEquals(RegistrationManager.REGISTRATION_STATE_REGISTERED,
                 mRegistrationCallbackHelper.getImsRegistrationState());
-        verify(mMockRegistrationUpdate).handleImsRegistered(anyInt());
+        verify(mMockRegistrationUpdate).handleImsRegistered(attr);
     }
 
     @Test
@@ -158,7 +168,25 @@
         // The registration state should be REGISTRATION_STATE_NOT_REGISTERED
         assertEquals(RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED,
                 mRegistrationCallbackHelper.getImsRegistrationState());
-        verify(mMockRegistrationUpdate).handleImsUnregistered(reasonInfo);
+        verify(mMockRegistrationUpdate).handleImsUnregistered(eq(reasonInfo),
+                eq(SUGGESTED_ACTION_NONE), eq(REGISTRATION_TECH_NONE));
+    }
+
+    @Test
+    @SmallTest
+    public void testImsUnRegisteredWithSuggestedAction() {
+        // Verify the RegistrationCallback should not be null
+        RegistrationCallback callback = mRegistrationCallbackHelper.getCallback();
+        assertNotNull(callback);
+
+        ImsReasonInfo reasonInfo = new ImsReasonInfo(ImsReasonInfo.CODE_REGISTRATION_ERROR, 0);
+        callback.onUnregistered(reasonInfo, SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK,
+                REGISTRATION_TECH_LTE);
+
+        assertEquals(RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED,
+                mRegistrationCallbackHelper.getImsRegistrationState());
+        verify(mMockRegistrationUpdate).handleImsUnregistered(eq(reasonInfo),
+                eq(SUGGESTED_ACTION_TRIGGER_PLMN_BLOCK), eq(REGISTRATION_TECH_LTE));
     }
 
     @Test
diff --git a/tests/telephonytests/src/com/android/internal/telephony/metrics/MetricsCollectorTest.java b/tests/telephonytests/src/com/android/internal/telephony/metrics/MetricsCollectorTest.java
index a4e2574..5886422 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/metrics/MetricsCollectorTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/metrics/MetricsCollectorTest.java
@@ -18,6 +18,7 @@
 
 import static com.android.internal.telephony.TelephonyStatsLog.CELLULAR_DATA_SERVICE_SWITCH;
 import static com.android.internal.telephony.TelephonyStatsLog.CELLULAR_SERVICE_STATE;
+import static com.android.internal.telephony.TelephonyStatsLog.OUTGOING_SHORT_CODE_SMS;
 import static com.android.internal.telephony.TelephonyStatsLog.SIM_SLOT_STATE;
 import static com.android.internal.telephony.TelephonyStatsLog.SUPPORTED_RADIO_ACCESS_FAMILY;
 import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_RAT_USAGE;
@@ -43,6 +44,7 @@
 import com.android.internal.telephony.TelephonyTest;
 import com.android.internal.telephony.nano.PersistAtomsProto.CellularDataServiceSwitch;
 import com.android.internal.telephony.nano.PersistAtomsProto.CellularServiceState;
+import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingShortCodeSms;
 import com.android.internal.telephony.nano.PersistAtomsProto.VoiceCallRatUsage;
 import com.android.internal.telephony.nano.PersistAtomsProto.VoiceCallSession;
 import com.android.internal.telephony.uicc.IccCardStatus.CardState;
@@ -412,4 +414,45 @@
         assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
         // TODO(b/153196254): verify atom contents
     }
-}
+
+    @Test
+    public void onPullAtom_outgoingShortCodeSms_empty() {
+        doReturn(new OutgoingShortCodeSms[0]).when(mPersistAtomsStorage)
+                .getOutgoingShortCodeSms(anyLong());
+        List<StatsEvent> actualAtoms = new ArrayList<>();
+
+        int result = mMetricsCollector.onPullAtom(OUTGOING_SHORT_CODE_SMS, actualAtoms);
+
+        assertThat(actualAtoms).hasSize(0);
+        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
+    }
+
+    @Test
+    public void onPullAtom_outgoingShortCodeSms_tooFrequent() {
+        doReturn(null).when(mPersistAtomsStorage).getOutgoingShortCodeSms(anyLong());
+        List<StatsEvent> actualAtoms = new ArrayList<>();
+
+        int result = mMetricsCollector.onPullAtom(OUTGOING_SHORT_CODE_SMS, actualAtoms);
+
+        assertThat(actualAtoms).hasSize(0);
+        assertThat(result).isEqualTo(StatsManager.PULL_SKIP);
+        verify(mPersistAtomsStorage, times(1))
+                .getOutgoingShortCodeSms(eq(MIN_COOLDOWN_MILLIS));
+        verifyNoMoreInteractions(mPersistAtomsStorage);
+    }
+
+    @Test
+    public void onPullAtom_outgoingShortCodeSms_multipleSms() {
+        OutgoingShortCodeSms outgoingShortCodeSms = new OutgoingShortCodeSms();
+        doReturn(new OutgoingShortCodeSms[] {outgoingShortCodeSms, outgoingShortCodeSms,
+                outgoingShortCodeSms, outgoingShortCodeSms})
+                .when(mPersistAtomsStorage)
+                .getOutgoingShortCodeSms(anyLong());
+        List<StatsEvent> actualAtoms = new ArrayList<>();
+
+        int result = mMetricsCollector.onPullAtom(OUTGOING_SHORT_CODE_SMS, actualAtoms);
+
+        assertThat(actualAtoms).hasSize(4);
+        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
+    }
+}
\ No newline at end of file
diff --git a/tests/telephonytests/src/com/android/internal/telephony/metrics/PersistAtomsStorageTest.java b/tests/telephonytests/src/com/android/internal/telephony/metrics/PersistAtomsStorageTest.java
index e46b822..be2257b 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/metrics/PersistAtomsStorageTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/metrics/PersistAtomsStorageTest.java
@@ -44,6 +44,7 @@
 import static org.mockito.Mockito.inOrder;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
 import android.annotation.Nullable;
 import android.content.Context;
@@ -68,6 +69,7 @@
 import com.android.internal.telephony.nano.PersistAtomsProto.ImsRegistrationServiceDescStats;
 import com.android.internal.telephony.nano.PersistAtomsProto.ImsRegistrationStats;
 import com.android.internal.telephony.nano.PersistAtomsProto.ImsRegistrationTermination;
+import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingShortCodeSms;
 import com.android.internal.telephony.nano.PersistAtomsProto.PersistAtoms;
 import com.android.internal.telephony.nano.PersistAtomsProto.PresenceNotifyEvent;
 import com.android.internal.telephony.nano.PersistAtomsProto.RcsAcsProvisioningStats;
@@ -224,6 +226,10 @@
     private SipTransportSession mSipTransportSession2;
     private SipTransportSession[] mSipTransportSession;
 
+    private OutgoingShortCodeSms mOutgoingShortCodeSms1;
+    private OutgoingShortCodeSms mOutgoingShortCodeSms2;
+    private OutgoingShortCodeSms[] mOutgoingShortCodeSms;
+
     private void makeTestData() {
         // MO call with SRVCC (LTE to UMTS)
         mCall1Proto = new VoiceCallSession();
@@ -859,6 +865,19 @@
 
         mSipTransportSession =
                 new SipTransportSession[] {mSipTransportSession1, mSipTransportSession2};
+
+        mOutgoingShortCodeSms1 = new OutgoingShortCodeSms();
+        mOutgoingShortCodeSms1.category = 1;
+        mOutgoingShortCodeSms1.xmlVersion = 30;
+        mOutgoingShortCodeSms1.shortCodeSmsCount = 1;
+
+        mOutgoingShortCodeSms2 = new OutgoingShortCodeSms();
+        mOutgoingShortCodeSms2.category = 2;
+        mOutgoingShortCodeSms2.xmlVersion  = 31;
+        mOutgoingShortCodeSms2.shortCodeSmsCount = 1;
+
+        mOutgoingShortCodeSms = new OutgoingShortCodeSms[] {mOutgoingShortCodeSms1,
+                mOutgoingShortCodeSms2};
     }
 
     private static class TestablePersistAtomsStorage extends PersistAtomsStorage {
@@ -997,6 +1016,9 @@
         mSipTransportSession1 = null;
         mSipTransportSession2 = null;
         mSipTransportSession = null;
+        mOutgoingShortCodeSms1 = null;
+        mOutgoingShortCodeSms2 = null;
+        mOutgoingShortCodeSms = null;
         super.tearDown();
     }
 
@@ -3399,6 +3421,121 @@
     }
 
     @Test
+    public void addOutgoingShortCodeSms_emptyProto() throws Exception {
+        createEmptyTestFile();
+
+        mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
+        mPersistAtomsStorage.addOutgoingShortCodeSms(mOutgoingShortCodeSms1);
+        mPersistAtomsStorage.incTimeMillis(100L);
+
+        // OutgoingShortCodeSms should be added successfully, changes should be saved.
+        verifyCurrentStateSavedToFileOnce();
+        OutgoingShortCodeSms[] expectedList = new OutgoingShortCodeSms[] {mOutgoingShortCodeSms1};
+        assertProtoArrayEquals(expectedList,
+                mPersistAtomsStorage.getOutgoingShortCodeSms(0L));
+    }
+
+    @Test
+    public void addOutgoingShortCodeSms_withExistingEntries() throws Exception {
+        createEmptyTestFile();
+
+        mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
+        mPersistAtomsStorage.addOutgoingShortCodeSms(mOutgoingShortCodeSms1);
+        mPersistAtomsStorage.addOutgoingShortCodeSms(mOutgoingShortCodeSms2);
+        mPersistAtomsStorage.incTimeMillis(100L);
+
+        // OutgoingShortCodeSms should be added successfully.
+        verifyCurrentStateSavedToFileOnce();
+        OutgoingShortCodeSms[] expectedList = new OutgoingShortCodeSms[] {mOutgoingShortCodeSms1,
+                mOutgoingShortCodeSms2};
+        assertProtoArrayEqualsIgnoringOrder(expectedList,
+                mPersistAtomsStorage.getOutgoingShortCodeSms(0L));
+    }
+
+    @Test
+    public void addOutgoingShortCodeSms_updateExistingEntries() throws Exception {
+        createTestFile(START_TIME_MILLIS);
+
+        // Add copy of mOutgoingShortCodeSms1.
+        mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
+        mPersistAtomsStorage.addOutgoingShortCodeSms(copyOf(mOutgoingShortCodeSms1));
+        mPersistAtomsStorage.incTimeMillis(100L);
+
+        // mOutgoingShortCodeSms1's short code sms count should be increased by 1.
+        verifyCurrentStateSavedToFileOnce();
+        OutgoingShortCodeSms newOutgoingShortCodeSms1 = copyOf(mOutgoingShortCodeSms1);
+        newOutgoingShortCodeSms1.shortCodeSmsCount = 2;
+        OutgoingShortCodeSms[] expectedList = new OutgoingShortCodeSms[] {newOutgoingShortCodeSms1,
+                mOutgoingShortCodeSms2};
+        assertProtoArrayEqualsIgnoringOrder(expectedList,
+                mPersistAtomsStorage.getOutgoingShortCodeSms(0L));
+    }
+
+    @Test
+    public void addOutgoingShortCodeSms_tooManyEntries() throws Exception {
+        createEmptyTestFile();
+
+        mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
+
+        // Store mOutgoingShortCodeSms1 11 times.
+        for (int i = 0; i < 11; i++) {
+            mPersistAtomsStorage.addOutgoingShortCodeSms(mOutgoingShortCodeSms1);
+            mPersistAtomsStorage.incTimeMillis(100L);
+        }
+        // Store mOutgoingShortCodeSms2 1 time.
+        mPersistAtomsStorage.addOutgoingShortCodeSms(mOutgoingShortCodeSms2);
+
+        verifyCurrentStateSavedToFileOnce();
+        OutgoingShortCodeSms[] result = mPersistAtomsStorage
+                .getOutgoingShortCodeSms(0L);
+        assertHasStatsAndCount(result, mOutgoingShortCodeSms1, 11);
+        assertHasStatsAndCount(result, mOutgoingShortCodeSms2, 1);
+    }
+
+    @Test
+    public void getOutgoingShortCodeSms_tooFrequent() throws Exception {
+        createTestFile(START_TIME_MILLIS);
+
+        mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
+        // Pull interval less than minimum.
+        mPersistAtomsStorage.incTimeMillis(50L);
+        OutgoingShortCodeSms[] outgoingShortCodeSmsList = mPersistAtomsStorage
+                .getOutgoingShortCodeSms(100L);
+        // Should be denied.
+        assertNull(outgoingShortCodeSmsList);
+    }
+
+    @Test
+    public void getOutgoingShortCodeSms_withSavedAtoms() throws Exception {
+        createTestFile(START_TIME_MILLIS);
+
+        mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
+        mPersistAtomsStorage.incTimeMillis(100L);
+        OutgoingShortCodeSms[] outgoingShortCodeSmsList1 = mPersistAtomsStorage
+                .getOutgoingShortCodeSms(50L);
+        mPersistAtomsStorage.incTimeMillis(100L);
+        OutgoingShortCodeSms[] outgoingShortCodeSmsList2 = mPersistAtomsStorage
+                .getOutgoingShortCodeSms(50L);
+
+        // First set of results should be equal to file contents.
+        OutgoingShortCodeSms[] expectedOutgoingShortCodeSmsList =
+                new OutgoingShortCodeSms[] {mOutgoingShortCodeSms1, mOutgoingShortCodeSms2};
+        assertProtoArrayEqualsIgnoringOrder(expectedOutgoingShortCodeSmsList,
+                outgoingShortCodeSmsList1);
+        // Second set of results should be empty.
+        assertProtoArrayEquals(new OutgoingShortCodeSms[0], outgoingShortCodeSmsList2);
+        // Corresponding pull timestamp should be updated and saved.
+        assertEquals(START_TIME_MILLIS + 200L, mPersistAtomsStorage
+                .getAtomsProto().outgoingShortCodeSmsPullTimestampMillis);
+        InOrder inOrder = inOrder(mTestFileOutputStream);
+        assertEquals(START_TIME_MILLIS + 100L,
+                getAtomsWritten(inOrder).outgoingShortCodeSmsPullTimestampMillis);
+        assertEquals(START_TIME_MILLIS + 200L,
+                getAtomsWritten(inOrder).outgoingShortCodeSmsPullTimestampMillis);
+        inOrder.verifyNoMoreInteractions();
+    }
+
+    @Test
     @SmallTest
     public void clearAtoms() throws Exception {
         createTestFile(START_TIME_MILLIS);
@@ -3469,6 +3606,8 @@
         atoms.sipMessageResponse = mSipMessageResponse;
         atoms.sipTransportSessionPullTimestampMillis = lastPullTimeMillis;
         atoms.sipTransportSession = mSipTransportSession;
+        atoms.outgoingShortCodeSms = mOutgoingShortCodeSms;
+        atoms.outgoingShortCodeSmsPullTimestampMillis = lastPullTimeMillis;
         FileOutputStream stream = new FileOutputStream(mTestFile);
         stream.write(PersistAtoms.toByteArray(atoms));
         stream.close();
@@ -3587,6 +3726,11 @@
         return SipTransportSession.parseFrom(MessageNano.toByteArray(source));
     }
 
+    private static OutgoingShortCodeSms copyOf(OutgoingShortCodeSms source)
+            throws Exception {
+        return OutgoingShortCodeSms.parseFrom(MessageNano.toByteArray(source));
+    }
+
     private void assertAllPullTimestampEquals(long timestamp) {
         assertEquals(
                 timestamp,
@@ -3866,4 +4010,18 @@
         }
         assertEquals(expectedCount, actualCount);
     }
+
+    private static void assertHasStatsAndCount(
+            OutgoingShortCodeSms[] outgoingShortCodeSmsList,
+            @Nullable OutgoingShortCodeSms expectedOutgoingShortCodeSms, int expectedCount) {
+        assertNotNull(outgoingShortCodeSmsList);
+        int actualCount = -1;
+        for (OutgoingShortCodeSms outgoingShortCodeSms : outgoingShortCodeSmsList) {
+            if (outgoingShortCodeSms.category == expectedOutgoingShortCodeSms.category
+                    && outgoingShortCodeSms.xmlVersion == expectedOutgoingShortCodeSms.xmlVersion) {
+                actualCount = outgoingShortCodeSms.shortCodeSmsCount;
+            }
+        }
+        assertEquals(expectedCount, actualCount);
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/metrics/ServiceStateStatsTest.java b/tests/telephonytests/src/com/android/internal/telephony/metrics/ServiceStateStatsTest.java
index 8406bc5..8885aa4 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/metrics/ServiceStateStatsTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/metrics/ServiceStateStatsTest.java
@@ -16,6 +16,13 @@
 
 package com.android.internal.telephony.metrics;
 
+import static android.telephony.TelephonyManager.DATA_CONNECTED;
+import static android.telephony.TelephonyManager.DATA_UNKNOWN;
+
+import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_CS;
+import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_IMS;
+import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_UNKNOWN;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotSame;
@@ -35,10 +42,6 @@
 import android.telephony.TelephonyManager;
 import android.test.suitebuilder.annotation.SmallTest;
 
-import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_CS;
-import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_IMS;
-import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_UNKNOWN;
-
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.TelephonyTest;
 import com.android.internal.telephony.nano.PersistAtomsProto.CellularDataServiceSwitch;
@@ -109,6 +112,9 @@
         mockWwanPsRat(TelephonyManager.NETWORK_TYPE_LTE);
         doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mImsStats).getImsVoiceRadioTech();
 
+        doReturn(DATA_CONNECTED).when(mDataNetworkController).getInternetDataNetworkState();
+        doReturn(mDataNetworkController).when(mSecondPhone).getDataNetworkController();
+
         mServiceStateStats = new TestableServiceStateStats(mPhone);
     }
 
@@ -143,6 +149,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -155,6 +162,7 @@
         doReturn(TelephonyManager.NETWORK_TYPE_UNKNOWN).when(mServiceState).getDataNetworkType();
         mockWwanCsRat(TelephonyManager.NETWORK_TYPE_UNKNOWN);
         mockWwanPsRat(TelephonyManager.NETWORK_TYPE_UNKNOWN);
+        doReturn(DATA_UNKNOWN).when(mDataNetworkController).getInternetDataNetworkState();
         mServiceStateStats.onServiceStateChanged(mServiceState);
 
         mServiceStateStats.incTimeMillis(100L);
@@ -176,6 +184,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(false, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -221,6 +230,7 @@
         doReturn(CardState.CARDSTATE_ABSENT).when(mPhysicalSlot0).getCardState();
         mockLimitedService(TelephonyManager.NETWORK_TYPE_UMTS);
         doReturn(-1).when(mPhone).getCarrierId();
+        doReturn(DATA_UNKNOWN).when(mDataNetworkController).getInternetDataNetworkState();
         mServiceStateStats.onServiceStateChanged(mServiceState);
 
         mServiceStateStats.incTimeMillis(100L);
@@ -242,6 +252,7 @@
         assertEquals(-1, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(true, state.isEmergencyOnly);
+        assertEquals(false, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -256,6 +267,7 @@
         mockWwanCsRat(TelephonyManager.NETWORK_TYPE_UNKNOWN);
         mockWwanPsRat(TelephonyManager.NETWORK_TYPE_UNKNOWN);
         doReturn(-1).when(mPhone).getCarrierId();
+        doReturn(DATA_UNKNOWN).when(mDataNetworkController).getInternetDataNetworkState();
         mServiceStateStats.onServiceStateChanged(mServiceState);
 
         mServiceStateStats.incTimeMillis(100L);
@@ -277,6 +289,7 @@
         assertEquals(-1, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(false, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -308,6 +321,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = captor.getAllValues().get(1);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
@@ -319,6 +333,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -355,6 +370,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = captor.getAllValues().get(1);
         assertEquals(TelephonyManager.NETWORK_TYPE_IWLAN, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_UNKNOWN, state.dataRat);
@@ -366,6 +382,50 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(200L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
+        verifyNoMoreInteractions(mPersistAtomsStorage);
+    }
+
+    @Test
+    @SmallTest
+    public void onInternetDataNetworkDisconnected() throws Exception {
+         // Using default service state for LTE
+        mServiceStateStats.onServiceStateChanged(mServiceState);
+
+        mServiceStateStats.incTimeMillis(100L);
+        mServiceStateStats.onInternetDataNetworkDisconnected();
+        mServiceStateStats.incTimeMillis(200L);
+        mServiceStateStats.conclude();
+
+        ArgumentCaptor<CellularServiceState> captor =
+                ArgumentCaptor.forClass(CellularServiceState.class);
+        verify(mPersistAtomsStorage, times(2))
+                .addCellularServiceStateAndCellularDataServiceSwitch(captor.capture(), eq(null));
+        assertNotSame(captor.getAllValues().get(0), captor.getAllValues().get(1));
+        CellularServiceState state = captor.getAllValues().get(0);
+        assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.voiceRat);
+        assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
+        assertEquals(ServiceState.ROAMING_TYPE_NOT_ROAMING, state.voiceRoamingType);
+        assertEquals(ServiceState.ROAMING_TYPE_NOT_ROAMING, state.dataRoamingType);
+        assertFalse(state.isEndc);
+        assertEquals(0, state.simSlotIndex);
+        assertFalse(state.isMultiSim);
+        assertEquals(CARRIER1_ID, state.carrierId);
+        assertEquals(100L, state.totalTimeMillis);
+        assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
+        state = captor.getAllValues().get(1);
+        assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.voiceRat);
+        assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
+        assertEquals(ServiceState.ROAMING_TYPE_NOT_ROAMING, state.voiceRoamingType);
+        assertEquals(ServiceState.ROAMING_TYPE_NOT_ROAMING, state.dataRoamingType);
+        assertFalse(state.isEndc);
+        assertEquals(0, state.simSlotIndex);
+        assertFalse(state.isMultiSim);
+        assertEquals(CARRIER1_ID, state.carrierId);
+        assertEquals(200L, state.totalTimeMillis);
+        assertEquals(false, state.isEmergencyOnly);
+        assertEquals(false, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -394,6 +454,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -431,6 +492,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = serviceStateCaptor.getAllValues().get(1);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
@@ -442,6 +504,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         CellularDataServiceSwitch serviceSwitch = serviceSwitchCaptor.getAllValues().get(0);
         assertEquals(TelephonyManager.NETWORK_TYPE_UNKNOWN, serviceSwitch.ratFrom);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, serviceSwitch.ratTo);
@@ -482,6 +545,7 @@
         assertFalse(state.isMultiSim);
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
+        assertEquals(true, state.isInternetPdnUp);
         state = captor.getAllValues().get(1);
         assertEquals(TelephonyManager.NETWORK_TYPE_IWLAN, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
@@ -492,6 +556,7 @@
         assertFalse(state.isMultiSim);
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
+        assertEquals(true, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -520,6 +585,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(0L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -560,6 +626,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = captor.getAllValues().get(1);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
@@ -571,6 +638,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(200L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = captor.getAllValues().get(2);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
@@ -582,6 +650,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(400L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = captor.getAllValues().get(3);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
@@ -593,6 +662,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(800L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -637,6 +707,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = captor.getAllValues().get(1);
         assertEquals(TelephonyManager.NETWORK_TYPE_UNKNOWN, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_UNKNOWN, state.dataRat);
@@ -648,6 +719,7 @@
         assertEquals(-1, state.carrierId);
         assertEquals(5000L, state.totalTimeMillis);
         assertEquals(true, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = captor.getAllValues().get(2);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
@@ -659,6 +731,7 @@
         assertEquals(CARRIER2_ID, state.carrierId);
         assertEquals(200L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
@@ -703,6 +776,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = serviceStateCaptor.getAllValues().get(1);
         assertEquals(TelephonyManager.NETWORK_TYPE_UMTS, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_UMTS, state.dataRat);
@@ -714,6 +788,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(200L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = serviceStateCaptor.getAllValues().get(2);
         assertEquals(TelephonyManager.NETWORK_TYPE_UMTS, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_UMTS, state.dataRat);
@@ -725,6 +800,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(400L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         CellularDataServiceSwitch serviceSwitch = serviceSwitchCaptor.getAllValues().get(0);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, serviceSwitch.ratFrom);
         assertEquals(TelephonyManager.NETWORK_TYPE_UMTS, serviceSwitch.ratTo);
@@ -781,6 +857,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = serviceStateCaptor.getAllValues().get(1);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
@@ -792,6 +869,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = serviceStateCaptor.getAllValues().get(2);
         assertEquals(TelephonyManager.NETWORK_TYPE_UMTS, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_UMTS, state.dataRat);
@@ -803,6 +881,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(200L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = serviceStateCaptor.getAllValues().get(3);
         assertEquals(TelephonyManager.NETWORK_TYPE_UMTS, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_UMTS, state.dataRat);
@@ -814,6 +893,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(200L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         CellularDataServiceSwitch serviceSwitch = serviceSwitchCaptor.getAllValues().get(0);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, serviceSwitch.ratFrom);
         assertEquals(TelephonyManager.NETWORK_TYPE_UMTS, serviceSwitch.ratTo);
@@ -874,6 +954,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(100L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         state = captor.getAllValues().get(1);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.voiceRat);
         assertEquals(TelephonyManager.NETWORK_TYPE_LTE, state.dataRat);
@@ -885,6 +966,7 @@
         assertEquals(CARRIER1_ID, state.carrierId);
         assertEquals(200L, state.totalTimeMillis);
         assertEquals(false, state.isEmergencyOnly);
+        assertEquals(true, state.isInternetPdnUp);
         verifyNoMoreInteractions(mPersistAtomsStorage);
     }
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/metrics/VoiceCallSessionStatsTest.java b/tests/telephonytests/src/com/android/internal/telephony/metrics/VoiceCallSessionStatsTest.java
index fb556e6..2ca0b16 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/metrics/VoiceCallSessionStatsTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/metrics/VoiceCallSessionStatsTest.java
@@ -77,7 +77,6 @@
 
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
@@ -927,7 +926,6 @@
 
     @Test
     @SmallTest
-    @Ignore("b/256234604")
     public void singleImsCall_ratSwitchToUnknown() {
         setServiceState(mServiceState, TelephonyManager.NETWORK_TYPE_LTE);
         doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mImsStats).getImsVoiceRadioTech();
diff --git a/tests/telephonytests/src/com/android/internal/telephony/nitz/NitzStateMachineImplTest.java b/tests/telephonytests/src/com/android/internal/telephony/nitz/NitzStateMachineImplTest.java
index 2ac0c98..580d533 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/nitz/NitzStateMachineImplTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/nitz/NitzStateMachineImplTest.java
@@ -910,7 +910,8 @@
             suggestedTimes.set(timeSuggestion);
             if (timeSuggestion.getUnixEpochTime() != null) {
                 // The fake time service just uses the latest suggestion.
-                mFakeDeviceState.currentTimeMillis = timeSuggestion.getUnixEpochTime().getValue();
+                mFakeDeviceState.currentTimeMillis =
+                        timeSuggestion.getUnixEpochTime().getUnixEpochTimeMillis();
             }
         }
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/subscription/SubscriptionDatabaseManagerTest.java b/tests/telephonytests/src/com/android/internal/telephony/subscription/SubscriptionDatabaseManagerTest.java
new file mode 100644
index 0000000..3f3a2c6
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/subscription/SubscriptionDatabaseManagerTest.java
@@ -0,0 +1,1640 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.subscription;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.common.truth.Truth.assertWithMessage;
+
+import static org.junit.Assert.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import android.annotation.NonNull;
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.database.MatrixCursor;
+import android.net.Uri;
+import android.os.Looper;
+import android.provider.Telephony;
+import android.provider.Telephony.SimInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.UiccAccessRule;
+import android.telephony.ims.ImsMmTelManager;
+import android.test.mock.MockContentProvider;
+import android.test.mock.MockContentResolver;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.TelephonyTest;
+import com.android.internal.telephony.subscription.SubscriptionDatabaseManager.SubscriptionDatabaseManagerCallback;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class SubscriptionDatabaseManagerTest extends TelephonyTest {
+    static final String FAKE_ICCID1 = "123456";
+    static final String FAKE_ICCID2 = "456789";
+    static final String FAKE_PHONE_NUMBER1 = "6502530000";
+    static final String FAKE_PHONE_NUMBER2 = "4089961010";
+    static final String FAKE_CARRIER_NAME1 = "A-Mobile";
+    static final String FAKE_CARRIER_NAME2 = "B-Mobile";
+    static final int FAKE_COLOR1 = 1;
+    static final int FAKE_COLOR2 = 3;
+    static final int FAKE_CARRIER_ID1 = 1234;
+    static final int FAKE_CARRIER_ID2 = 5678;
+    static final String FAKE_COUNTRY_CODE1 = "TW";
+    static final String FAKE_COUNTRY_CODE2 = "US";
+    static final String FAKE_MCC1 = "466";
+    static final String FAKE_MCC2 = "310";
+    static final String FAKE_MNC1 = "01";
+    static final String FAKE_MNC2 = "410";
+    static final String FAKE_EHPLMNS1 = "46602,46603";
+    static final String FAKE_EHPLMNS2 = "310411,310412";
+    static final String FAKE_HPLMNS1 = "46601,46604";
+    static final String FAKE_HPLMNS2 = "310410,310413";
+    static final byte[] FAKE_NATIVE_ACCESS_RULES1 = UiccAccessRule.encodeRules(
+            new UiccAccessRule[]{new UiccAccessRule(new byte[] {}, "package1", 12345L)});
+    static final byte[] FAKE_NATIVE_ACCESS_RULES2 = UiccAccessRule.encodeRules(
+            new UiccAccessRule[]{new UiccAccessRule(new byte[] {}, "package2", 45678L)});
+    static final byte[] FAKE_CARRIER_CONFIG_ACCESS_RULES1 = UiccAccessRule.encodeRules(
+            new UiccAccessRule[]{new UiccAccessRule(new byte[] {}, "package1", 54321L)});
+    static final byte[] FAKE_CARRIER_CONFIG_ACCESS_RULES2 = UiccAccessRule.encodeRules(
+            new UiccAccessRule[]{new UiccAccessRule(new byte[] {}, "package2", 84954L)});
+    static final String FAKE_UUID1 = "a684e31a-5998-4670-abdd-0561252c58a5";
+    static final String FAKE_UUID2 = "cf6d7a9d-e712-4b3c-a600-7a2d4961b5b9";
+    static final String FAKE_OWNER1 = "owner1";
+    static final String FAKE_OWNER2 = "owner2";
+    static final String FAKE_MOBILE_DATA_POLICY1 = "1,2";
+    static final String FAKE_MOBILE_DATA_POLICY2 = "1";
+    static final String FAKE_IMSI1 = "1234";
+    static final String FAKE_IMSI2 = "5678";
+    static final byte[] FAKE_RCS_CONFIG1 = new byte[]{0x01, 0x02, 0x03};
+    static final byte[] FAKE_RCS_CONFIG2 = new byte[]{0x04, 0x05, 0x06};
+    static final String FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS1 = "carrier=123456, power=3";
+    static final String FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS2 = "user=1256, enable_2g=3";
+    static final String FAKE_CONTACT1 = "John Smith, Tesla Forrest";
+    static final String FAKE_CONTACT2 = "Mary Jane, Teresa Mill";
+    static final int FAKE_TP_MESSAGE_REFERENCE1 = 123;
+    static final int FAKE_TP_MESSAGE_REFERENCE2 = 456;
+    static final int FAKE_USER_ID1 = 10;
+    static final int FAKE_USER_ID2 = 11;
+
+    static final SubscriptionInfoInternal FAKE_SUBSCRIPTION_INFO1 =
+            new SubscriptionInfoInternal.Builder()
+                    .setId(1)
+                    .setIccId(FAKE_ICCID1)
+                    .setSimSlotIndex(0)
+                    .setDisplayName(FAKE_CARRIER_NAME1)
+                    .setCarrierName(FAKE_CARRIER_NAME1)
+                    .setDisplayNameSource(SubscriptionManager.NAME_SOURCE_SIM_SPN)
+                    .setIconTint(FAKE_COLOR1)
+                    .setNumber(FAKE_PHONE_NUMBER1)
+                    .setDataRoaming(SubscriptionManager.DATA_ROAMING_ENABLE)
+                    .setMcc(FAKE_MCC1)
+                    .setMnc(FAKE_MNC1)
+                    .setEhplmns(FAKE_EHPLMNS1)
+                    .setHplmns(FAKE_HPLMNS1)
+                    .setEmbedded(1)
+                    .setCardString(FAKE_ICCID1)
+                    .setCardId(1)
+                    .setNativeAccessRules(FAKE_NATIVE_ACCESS_RULES1)
+                    .setCarrierConfigAccessRules(FAKE_CARRIER_CONFIG_ACCESS_RULES1)
+                    .setRemovableEmbedded(0)
+                    .setEnhanced4GModeEnabled(1)
+                    .setVideoTelephonyEnabled(1)
+                    .setWifiCallingEnabled(1)
+                    .setWifiCallingMode(ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED)
+                    .setWifiCallingModeForRoaming(ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED)
+                    .setWifiCallingEnabledForRoaming(1)
+                    .setOpportunistic(0)
+                    .setGroupUuid(FAKE_UUID1)
+                    .setCountryIso(FAKE_COUNTRY_CODE1)
+                    .setCarrierId(FAKE_CARRIER_ID1)
+                    .setProfileClass(SubscriptionManager.PROFILE_CLASS_OPERATIONAL)
+                    .setType(SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM)
+                    .setGroupOwner(FAKE_OWNER1)
+                    .setEnabledMobileDataPolicies(FAKE_MOBILE_DATA_POLICY1)
+                    .setImsi(FAKE_IMSI1)
+                    .setUiccApplicationsEnabled(1)
+                    .setRcsUceEnabled(1)
+                    .setCrossSimCallingEnabled(1)
+                    .setRcsConfig(FAKE_RCS_CONFIG1)
+                    .setAllowedNetworkTypesForReasons(FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS1)
+                    .setDeviceToDeviceStatusSharingPreference(
+                            SubscriptionManager.D2D_SHARING_ALL_CONTACTS)
+                    .setVoImsOptInEnabled(1)
+                    .setDeviceToDeviceStatusSharingContacts(FAKE_CONTACT1)
+                    .setNrAdvancedCallingEnabled(1)
+                    .setNumberFromCarrier(FAKE_PHONE_NUMBER1)
+                    .setNumberFromIms(FAKE_PHONE_NUMBER1)
+                    .setPortIndex(0)
+                    .setUsageSetting(SubscriptionManager.USAGE_SETTING_DEFAULT)
+                    .setLastUsedTPMessageReference(FAKE_TP_MESSAGE_REFERENCE1)
+                    .setUserId(FAKE_USER_ID1)
+                    .setGroupDisabled(false)
+                    .build();
+
+    static final SubscriptionInfoInternal FAKE_SUBSCRIPTION_INFO2 =
+            new SubscriptionInfoInternal.Builder()
+                    .setId(2)
+                    .setIccId(FAKE_ICCID2)
+                    .setSimSlotIndex(1)
+                    .setDisplayName(FAKE_CARRIER_NAME2)
+                    .setCarrierName(FAKE_CARRIER_NAME2)
+                    .setDisplayNameSource(SubscriptionManager.NAME_SOURCE_CARRIER)
+                    .setIconTint(FAKE_COLOR2)
+                    .setNumber(FAKE_PHONE_NUMBER2)
+                    .setDataRoaming(SubscriptionManager.DATA_ROAMING_DISABLE)
+                    .setMcc(FAKE_MCC2)
+                    .setMnc(FAKE_MNC2)
+                    .setEhplmns(FAKE_EHPLMNS2)
+                    .setHplmns(FAKE_HPLMNS2)
+                    .setEmbedded(0)
+                    .setCardString(FAKE_ICCID2)
+                    .setCardId(2)
+                    .setNativeAccessRules(FAKE_NATIVE_ACCESS_RULES2)
+                    .setCarrierConfigAccessRules(FAKE_CARRIER_CONFIG_ACCESS_RULES2)
+                    .setRemovableEmbedded(1)
+                    .setEnhanced4GModeEnabled(0)
+                    .setVideoTelephonyEnabled(0)
+                    .setWifiCallingEnabled(0)
+                    .setWifiCallingMode(ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED)
+                    .setWifiCallingModeForRoaming(ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED)
+                    .setWifiCallingEnabledForRoaming(0)
+                    .setOpportunistic(1)
+                    .setGroupUuid(FAKE_UUID2)
+                    .setCountryIso(FAKE_COUNTRY_CODE2)
+                    .setCarrierId(FAKE_CARRIER_ID2)
+                    .setProfileClass(SubscriptionManager.PROFILE_CLASS_PROVISIONING)
+                    .setType(SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM)
+                    .setGroupOwner(FAKE_OWNER2)
+                    .setEnabledMobileDataPolicies(FAKE_MOBILE_DATA_POLICY2)
+                    .setImsi(FAKE_IMSI2)
+                    .setUiccApplicationsEnabled(0)
+                    .setRcsUceEnabled(0)
+                    .setCrossSimCallingEnabled(0)
+                    .setRcsConfig(FAKE_RCS_CONFIG2)
+                    .setAllowedNetworkTypesForReasons(FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS2)
+                    .setDeviceToDeviceStatusSharingPreference(
+                            SubscriptionManager.D2D_SHARING_SELECTED_CONTACTS)
+                    .setVoImsOptInEnabled(0)
+                    .setDeviceToDeviceStatusSharingContacts(FAKE_CONTACT2)
+                    .setNrAdvancedCallingEnabled(0)
+                    .setNumberFromCarrier(FAKE_PHONE_NUMBER2)
+                    .setNumberFromIms(FAKE_PHONE_NUMBER2)
+                    .setPortIndex(1)
+                    .setUsageSetting(SubscriptionManager.USAGE_SETTING_DATA_CENTRIC)
+                    .setLastUsedTPMessageReference(FAKE_TP_MESSAGE_REFERENCE2)
+                    .setUserId(FAKE_USER_ID2)
+                    .setGroupDisabled(false)
+                    .build();
+
+    private SubscriptionDatabaseManager mDatabaseManagerUT;
+
+    private final SubscriptionProvider mSubscriptionProvider = new SubscriptionProvider();
+
+    //mock
+    private SubscriptionDatabaseManagerCallback mSubscriptionDatabaseManagerCallback;
+
+    static class SubscriptionProvider extends MockContentProvider {
+        private final List<ContentValues> mDatabase = new ArrayList<>();
+
+        private final List<String> mAllColumns;
+
+        SubscriptionProvider() {
+            mAllColumns = SimInfo.getAllColumns();
+        }
+
+        @Override
+        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
+                String sortOrder) {
+            logd("SubscriptionProvider: query. uri=" + uri);
+            if (!SimInfo.CONTENT_URI.equals(uri)) {
+                throw new UnsupportedOperationException("Unsupported uri=" + uri);
+            }
+            if (projection != null || selection != null || selectionArgs != null) {
+                throw new UnsupportedOperationException("Only support full database query. "
+                        + "projection=" + Arrays.toString(projection) + ", selection=" + selection
+                        + ", selectionArgs=" + Arrays.toString(selectionArgs));
+            }
+
+            MatrixCursor mc = new MatrixCursor(mAllColumns.toArray(new String[0]));
+
+            // Only support full database query
+            for (int row = 0; row < mDatabase.size(); row++) {
+                List<Object> values = new ArrayList<>();
+                for (String column : mAllColumns) {
+                    values.add(mDatabase.get(row).get(column));
+                }
+                mc.addRow(values);
+            }
+
+            return mc;
+        }
+
+        @Override
+        public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
+            if (!uri.isPathPrefixMatch(SimInfo.CONTENT_URI)) {
+                throw new UnsupportedOperationException("Unsupported uri=" + uri);
+            }
+
+            int subId = Integer.parseInt(uri.getLastPathSegment());
+            logd("update: subId=" + subId + ", contentValues=" + values);
+
+            ContentValues existingValues = mDatabase.stream()
+                    .filter(contentValues -> contentValues.get(
+                            SimInfo.COLUMN_UNIQUE_KEY_SUBSCRIPTION_ID).equals(subId))
+                    .findFirst()
+                    .orElse(null);
+            if (existingValues == null) {
+                throw new IllegalArgumentException("Invalid sub id " + subId);
+            }
+
+            for (Map.Entry<String, Object> entry : values.valueSet()) {
+                String column = entry.getKey();
+                Object value = entry.getValue();
+                if (!mAllColumns.contains(column)) {
+                    throw new IllegalArgumentException("Update with unknown column " + column);
+                }
+                existingValues.putObject(column, value);
+            }
+            return 1;
+        }
+
+        @Override
+        public int delete(Uri uri, String selection, String[] selectionArgs) {
+            if (!uri.isPathPrefixMatch(SimInfo.CONTENT_URI)) {
+                throw new UnsupportedOperationException("Unsupported uri=" + uri);
+            }
+
+            logd("delete: uri=" + uri + ", selection=" + selection + ", selectionArgs="
+                    + Arrays.toString(selectionArgs));
+            if (!selection.equals(SimInfo.COLUMN_UNIQUE_KEY_SUBSCRIPTION_ID + "=?")) {
+                throw new UnsupportedOperationException("Only support delete by sub id.");
+            }
+
+            int rowsRemoved = 0;
+            for (String selectionArg : selectionArgs) {
+                int subId = Integer.parseInt(selectionArg);
+                // Clear it to null instead of removing it.
+                rowsRemoved += mDatabase.removeIf(contentValues -> contentValues.get(
+                        SimInfo.COLUMN_UNIQUE_KEY_SUBSCRIPTION_ID).equals(subId)) ? 1 : 0;
+            }
+            return rowsRemoved;
+        }
+
+        @Override
+        public Uri insert(Uri uri, ContentValues values) {
+            logd("SubscriptionProvider: insert. uri=" + uri + ", values=" + values);
+            if (!SimInfo.CONTENT_URI.equals(uri)) {
+                throw new UnsupportedOperationException("Unsupported uri=" + uri);
+            }
+
+            for (String column : values.keySet()) {
+                if (!mAllColumns.contains(column)) {
+                    throw new IllegalArgumentException("Insert with unknown column " + column);
+                }
+            }
+            // The last row's subId + 1
+            int subId;
+            if (mDatabase.isEmpty()) {
+                subId = 1;
+            } else {
+                subId = (int) mDatabase.get(mDatabase.size() - 1)
+                        .get(SimInfo.COLUMN_UNIQUE_KEY_SUBSCRIPTION_ID) + 1;
+            }
+            values.put(SimInfo.COLUMN_UNIQUE_KEY_SUBSCRIPTION_ID, subId);
+            mDatabase.add(values);
+            return ContentUris.withAppendedId(SimInfo.CONTENT_URI, subId);
+        }
+    }
+
+    private void loadFromDatabase() throws Exception {
+        Method method = SubscriptionDatabaseManager.class.getDeclaredMethod("loadFromDatabase");
+        method.setAccessible(true);
+        method.invoke(mDatabaseManagerUT);
+        processAllMessages();
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        logd("SubscriptionDatabaseManagerTest +Setup!");
+        super.setUp(getClass().getSimpleName());
+        mSubscriptionDatabaseManagerCallback =
+                Mockito.mock(SubscriptionDatabaseManagerCallback.class);
+        doAnswer(invocation -> {
+            ((Runnable) invocation.getArguments()[0]).run();
+            return null;
+        }).when(mSubscriptionDatabaseManagerCallback).invokeFromExecutor(any(Runnable.class));
+
+        ((MockContentResolver) mContext.getContentResolver()).addProvider(
+                Telephony.Carriers.CONTENT_URI.getAuthority(), mSubscriptionProvider);
+        doReturn(1).when(mUiccController).convertToPublicCardId(eq(FAKE_ICCID1));
+        doReturn(2).when(mUiccController).convertToPublicCardId(eq(FAKE_ICCID2));
+        mDatabaseManagerUT = new SubscriptionDatabaseManager(mContext, Looper.myLooper(),
+                mSubscriptionDatabaseManagerCallback);
+        logd("SubscriptionDatabaseManagerTest -Setup!");
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    /**
+     * Verify the subscription from the cache and database is same as the provided one.
+     *
+     * @param subInfo The subscription to compare.
+     */
+    private void verifySubscription(@NonNull SubscriptionInfoInternal subInfo) throws Exception {
+        int subId = subInfo.getSubscriptionId();
+        // Verify the cache value is same as the inserted one.
+        assertWithMessage("Subscription info cache value is different.")
+                .that(mDatabaseManagerUT.getSubscriptionInfoInternal(subId)).isEqualTo(subInfo);
+
+        // Load subscription info from the database.
+        loadFromDatabase();
+        // Verify the database value is same as the inserted one.
+        assertWithMessage("Subscription info database value is different.")
+                .that(mDatabaseManagerUT.getSubscriptionInfoInternal(subId)).isEqualTo(subInfo);
+    }
+
+    /**
+     * Insert a subscription info into the database and verify it's in the cache and database.
+     *
+     * @param subInfo The subscription info to insert.
+     * @return The inserted subscription info.
+     */
+    private SubscriptionInfoInternal insertSubscriptionAndVerify(
+            @NonNull SubscriptionInfoInternal subInfo) throws Exception {
+        int subId = mDatabaseManagerUT.insertSubscriptionInfo(
+                new SubscriptionInfoInternal.Builder(subInfo)
+                        .setId(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
+                        .build());
+        assertThat(SubscriptionManager.isValidSubscriptionId(subId)).isTrue();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setId(subId).build();
+        verifySubscription(subInfo);
+        return subInfo;
+    }
+
+    @Test
+    public void testGetAllColumns() throws Exception {
+        Field[] declaredFields = SimInfo.class.getDeclaredFields();
+        List<String> columnNames = new ArrayList<>();
+        for (Field field : declaredFields) {
+            if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("COLUMN_")) {
+                columnNames.add((String) field.get(null));
+            }
+        }
+        // When you add a new column in Telephony.SimInfo, did you remember to modify
+        // Telephony.SimInfo.getAllColumns() as well?
+        assertThat(SimInfo.getAllColumns()).containsExactlyElementsIn(columnNames);
+    }
+
+    @Test
+    public void testInsertSubscription() throws Exception {
+        assertThat(insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1).getSubscriptionId())
+                .isEqualTo(1);
+        processAllMessages();
+        verify(mSubscriptionDatabaseManagerCallback).onSubscriptionChanged(eq(1));
+        Mockito.clearInvocations(mSubscriptionDatabaseManagerCallback);
+
+        assertThat(insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO2).getSubscriptionId())
+                .isEqualTo(2);
+        processAllMessages();
+        verify(mSubscriptionDatabaseManagerCallback).onSubscriptionChanged(eq(2));
+    }
+
+    @Test
+    public void testUpdateSubscription() throws Exception {
+        SubscriptionInfoInternal subInfo = new SubscriptionInfoInternal
+                .Builder(FAKE_SUBSCRIPTION_INFO2)
+                .setId(1)
+                .build();
+
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.updateSubscription(subInfo));
+
+        assertThat(insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1).getSubscriptionId())
+                .isEqualTo(1);
+        mDatabaseManagerUT.updateSubscription(subInfo);
+        processAllMessages();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+        Mockito.clearInvocations(mSubscriptionDatabaseManagerCallback);
+
+        // Same sub info again. Should not trigger callback
+        mDatabaseManagerUT.updateSubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, never()).onSubscriptionChanged(anyInt());
+    }
+
+    @Test
+    public void testUpdateSubscriptionSync() throws Exception {
+        mContextFixture.putBooleanResource(com.android.internal.R.bool
+                .config_subscription_database_async_update, false);
+        mDatabaseManagerUT = new SubscriptionDatabaseManager(mContext, Looper.myLooper(),
+                mSubscriptionDatabaseManagerCallback);
+
+        assertThat(insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1).getSubscriptionId())
+                .isEqualTo(1);
+        SubscriptionInfoInternal subInfo = new SubscriptionInfoInternal
+                .Builder(FAKE_SUBSCRIPTION_INFO2)
+                .setId(1)
+                .build();
+        mDatabaseManagerUT.updateSubscription(subInfo);
+
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+        Mockito.clearInvocations(mSubscriptionDatabaseManagerCallback);
+
+        // Same sub info again. Should not trigger callback
+        mDatabaseManagerUT.updateSubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, never()).onSubscriptionChanged(anyInt());
+    }
+
+    @Test
+    public void testUpdateIccId() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setIccId(1, FAKE_ICCID2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setIccId(subInfo.getSubscriptionId(), FAKE_ICCID2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setIccId(FAKE_ICCID2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_ICC_ID)).isEqualTo(FAKE_ICCID2);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_ICC_ID, FAKE_ICCID1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getIccId())
+                .isEqualTo(FAKE_ICCID1);
+    }
+
+    @Test
+    public void testUpdateSimSlotIndex() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setSimSlotIndex(1,
+                        SubscriptionManager.INVALID_SIM_SLOT_INDEX));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setSimSlotIndex(subInfo.getSubscriptionId(),
+                SubscriptionManager.INVALID_SIM_SLOT_INDEX);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setSimSlotIndex(
+                SubscriptionManager.INVALID_SIM_SLOT_INDEX).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_SIM_SLOT_INDEX))
+                .isEqualTo(SubscriptionManager.INVALID_SIM_SLOT_INDEX);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_SIM_SLOT_INDEX, 123);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getSimSlotIndex())
+                .isEqualTo(123);
+    }
+
+    @Test
+    public void testUpdateDisplayName() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setDisplayName(1, FAKE_CARRIER_NAME2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setDisplayName(subInfo.getSubscriptionId(), FAKE_CARRIER_NAME2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setDisplayName(
+                FAKE_CARRIER_NAME2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_DISPLAY_NAME))
+                .isEqualTo(FAKE_CARRIER_NAME2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_DISPLAY_NAME, FAKE_CARRIER_NAME1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getDisplayName())
+                .isEqualTo(FAKE_CARRIER_NAME1);
+    }
+
+    @Test
+    public void testUpdateCarrierName() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setCarrierName(1, FAKE_CARRIER_NAME2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setCarrierName(subInfo.getSubscriptionId(), FAKE_CARRIER_NAME2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setCarrierName(
+                FAKE_CARRIER_NAME2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_CARRIER_NAME))
+                .isEqualTo(FAKE_CARRIER_NAME2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_CARRIER_NAME, FAKE_CARRIER_NAME1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getCarrierName())
+                .isEqualTo(FAKE_CARRIER_NAME1);
+    }
+
+    @Test
+    public void testUpdateDisplayNameSource() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setDisplayNameSource(1,
+                        SubscriptionManager.NAME_SOURCE_USER_INPUT));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setDisplayNameSource(subInfo.getSubscriptionId(),
+                SubscriptionManager.NAME_SOURCE_USER_INPUT);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setDisplayNameSource(
+                SubscriptionManager.NAME_SOURCE_USER_INPUT).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_NAME_SOURCE))
+                .isEqualTo(SubscriptionManager.NAME_SOURCE_USER_INPUT);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_NAME_SOURCE, SubscriptionManager.NAME_SOURCE_SIM_PNN);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getDisplayNameSource())
+                .isEqualTo(SubscriptionManager.NAME_SOURCE_SIM_PNN);
+    }
+
+    @Test
+    public void testUpdateIconTint() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setIconTint(1, FAKE_COLOR2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setIconTint(subInfo.getSubscriptionId(), FAKE_COLOR2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setIconTint(FAKE_COLOR2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_COLOR))
+                .isEqualTo(FAKE_COLOR2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_COLOR, FAKE_COLOR1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getIconTint())
+                .isEqualTo(FAKE_COLOR1);
+    }
+
+    @Test
+    public void testUpdateNumber() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setNumber(1, FAKE_PHONE_NUMBER2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setNumber(subInfo.getSubscriptionId(), FAKE_PHONE_NUMBER2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setNumber(FAKE_PHONE_NUMBER2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_NUMBER))
+                .isEqualTo(FAKE_PHONE_NUMBER2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_NUMBER, FAKE_PHONE_NUMBER1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getNumber())
+                .isEqualTo(FAKE_PHONE_NUMBER1);
+    }
+
+    @Test
+    public void testUpdateDataRoaming() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setDataRoaming(1,
+                        SubscriptionManager.DATA_ROAMING_DISABLE));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setDataRoaming(subInfo.getSubscriptionId(),
+                SubscriptionManager.DATA_ROAMING_DISABLE);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setDataRoaming(SubscriptionManager.DATA_ROAMING_DISABLE).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_DATA_ROAMING))
+                .isEqualTo(SubscriptionManager.DATA_ROAMING_DISABLE);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_DATA_ROAMING, SubscriptionManager.DATA_ROAMING_ENABLE);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getDataRoaming())
+                .isEqualTo(SubscriptionManager.DATA_ROAMING_ENABLE);
+    }
+
+    @Test
+    public void testUpdateMcc() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setMcc(1, FAKE_MCC2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setMcc(subInfo.getSubscriptionId(), FAKE_MCC2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setMcc(FAKE_MCC2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_MCC_STRING))
+                .isEqualTo(FAKE_MCC2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_MCC_STRING, FAKE_MCC1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getMcc())
+                .isEqualTo(FAKE_MCC1);
+    }
+
+    @Test
+    public void testUpdateMnc() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setMnc(1, FAKE_MNC2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setMnc(subInfo.getSubscriptionId(), FAKE_MNC2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setMnc(FAKE_MNC2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_MNC_STRING))
+                .isEqualTo(FAKE_MNC2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_MNC_STRING, FAKE_MNC1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getMnc())
+                .isEqualTo(FAKE_MNC1);
+    }
+
+    @Test
+    public void testUpdateEhplmns() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setEhplmns(1, FAKE_EHPLMNS2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setEhplmns(subInfo.getSubscriptionId(), FAKE_EHPLMNS2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setEhplmns(FAKE_EHPLMNS2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_EHPLMNS))
+                .isEqualTo(FAKE_EHPLMNS2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_EHPLMNS, FAKE_EHPLMNS1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getEhplmns())
+                .isEqualTo(FAKE_EHPLMNS1);
+    }
+
+    @Test
+    public void testUpdateHplmns() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setHplmns(1, FAKE_HPLMNS2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setHplmns(subInfo.getSubscriptionId(), FAKE_HPLMNS2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setHplmns(FAKE_HPLMNS2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_HPLMNS))
+                .isEqualTo(FAKE_HPLMNS2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_HPLMNS, FAKE_HPLMNS1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getHplmns())
+                .isEqualTo(FAKE_HPLMNS1);
+    }
+
+    @Test
+    public void testUpdateEmbedded() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setEmbedded(1, false));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setEmbedded(subInfo.getSubscriptionId(), false);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setEmbedded(0).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_IS_EMBEDDED))
+                .isEqualTo(0);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_IS_EMBEDDED, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getEmbedded())
+                .isEqualTo(1);
+    }
+
+    @Test
+    public void testUpdateCardString() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setCardString(1, FAKE_ICCID2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setCardString(subInfo.getSubscriptionId(), FAKE_ICCID2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setCardString(FAKE_ICCID2)
+                .setCardId(2)
+                .build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_CARD_ID))
+                .isEqualTo(FAKE_ICCID2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_CARD_ID, FAKE_ICCID1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getCardString())
+                .isEqualTo(FAKE_ICCID1);
+    }
+
+    @Test
+    public void testUpdateNativeAccessRules() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setNativeAccessRules(1, FAKE_NATIVE_ACCESS_RULES2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setNativeAccessRules(subInfo.getSubscriptionId(),
+                FAKE_NATIVE_ACCESS_RULES2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setNativeAccessRules(FAKE_NATIVE_ACCESS_RULES2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_ACCESS_RULES))
+                .isEqualTo(FAKE_NATIVE_ACCESS_RULES2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_ACCESS_RULES, FAKE_NATIVE_ACCESS_RULES1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getNativeAccessRules())
+                .isEqualTo(FAKE_NATIVE_ACCESS_RULES1);
+    }
+
+    @Test
+    public void testUpdateCarrierConfigAccessRules() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setCarrierConfigAccessRules(1,
+                        FAKE_CARRIER_CONFIG_ACCESS_RULES2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setCarrierConfigAccessRules(subInfo.getSubscriptionId(),
+                FAKE_CARRIER_CONFIG_ACCESS_RULES2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setCarrierConfigAccessRules(FAKE_CARRIER_CONFIG_ACCESS_RULES2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_ACCESS_RULES_FROM_CARRIER_CONFIGS))
+                .isEqualTo(FAKE_CARRIER_CONFIG_ACCESS_RULES2);
+        mDatabaseManagerUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_ACCESS_RULES_FROM_CARRIER_CONFIGS,
+                FAKE_CARRIER_CONFIG_ACCESS_RULES2);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getCarrierConfigAccessRules())
+                .isEqualTo(FAKE_CARRIER_CONFIG_ACCESS_RULES2);
+    }
+
+    @Test
+    public void testUpdateRemovableEmbedded() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setRemovableEmbedded(1, 1));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setRemovableEmbedded(subInfo.getSubscriptionId(), 1);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setRemovableEmbedded(1).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_IS_REMOVABLE))
+                .isEqualTo(1);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_IS_REMOVABLE, 0);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getRemovableEmbedded())
+                .isEqualTo(0);
+    }
+
+    @Test
+    public void testUpdateEnhanced4GModeEnabled() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setEnhanced4GModeEnabled(1, 0));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setEnhanced4GModeEnabled(subInfo.getSubscriptionId(), 0);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setEnhanced4GModeEnabled(0).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED)).isEqualTo(0);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getEnhanced4GModeEnabled())
+                .isEqualTo(1);
+    }
+
+    @Test
+    public void testUpdateVideoTelephonyEnabled() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setVideoTelephonyEnabled(1, 0));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setVideoTelephonyEnabled(subInfo.getSubscriptionId(), 0);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setVideoTelephonyEnabled(0).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_VT_IMS_ENABLED)).isEqualTo(0);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_VT_IMS_ENABLED, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getVideoTelephonyEnabled())
+                .isEqualTo(1);
+    }
+
+    @Test
+    public void testUpdateWifiCallingEnabled() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setWifiCallingEnabled(1, 0));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setWifiCallingEnabled(subInfo.getSubscriptionId(), 0);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setWifiCallingEnabled(0).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_WFC_IMS_ENABLED))
+                .isEqualTo(0);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_WFC_IMS_ENABLED, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getWifiCallingEnabled())
+                .isEqualTo(1);
+    }
+
+    @Test
+    public void testUpdateWifiCallingMode() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setWifiCallingMode(
+                        1, ImsMmTelManager.WIFI_MODE_WIFI_ONLY));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setWifiCallingMode(subInfo.getSubscriptionId(),
+                ImsMmTelManager.WIFI_MODE_WIFI_ONLY);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setWifiCallingMode(ImsMmTelManager.WIFI_MODE_WIFI_ONLY).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_WFC_IMS_MODE))
+                .isEqualTo(ImsMmTelManager.WIFI_MODE_WIFI_ONLY);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_WFC_IMS_MODE,
+                ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getWifiCallingMode())
+                .isEqualTo(ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED);
+    }
+
+    @Test
+    public void testUpdateWifiCallingModeForRoaming() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setWifiCallingModeForRoaming(
+                        1, ImsMmTelManager.WIFI_MODE_WIFI_ONLY));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setWifiCallingModeForRoaming(subInfo.getSubscriptionId(),
+                ImsMmTelManager.WIFI_MODE_WIFI_ONLY);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setWifiCallingModeForRoaming(ImsMmTelManager.WIFI_MODE_WIFI_ONLY).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_WFC_IMS_ROAMING_MODE))
+                .isEqualTo(ImsMmTelManager.WIFI_MODE_WIFI_ONLY);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_WFC_IMS_ROAMING_MODE,
+                ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getWifiCallingModeForRoaming())
+                .isEqualTo(ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED);
+    }
+
+    @Test
+    public void testUpdateWifiCallingEnabledForRoaming() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setWifiCallingEnabledForRoaming(1, 0));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setWifiCallingEnabledForRoaming(subInfo.getSubscriptionId(), 0);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setWifiCallingEnabledForRoaming(0).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_WFC_IMS_ROAMING_ENABLED)).isEqualTo(0);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_WFC_IMS_ROAMING_ENABLED, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getWifiCallingEnabledForRoaming()).isEqualTo(1);
+    }
+
+    @Test
+    public void testUpdateVoImsOptInEnabled() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setVoImsOptInEnabled(1, 0));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setVoImsOptInEnabled(subInfo.getSubscriptionId(), 0);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setVoImsOptInEnabled(0).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_VOIMS_OPT_IN_STATUS)).isEqualTo(0);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_VOIMS_OPT_IN_STATUS, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getVoImsOptInEnabled()).isEqualTo(1);
+    }
+
+
+    @Test
+    public void testUpdateOpportunistic() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setOpportunistic(1, 1));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setOpportunistic(subInfo.getSubscriptionId(), 1);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setOpportunistic(1).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_IS_OPPORTUNISTIC)).isEqualTo(1);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_IS_OPPORTUNISTIC, 0);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getOpportunistic()).isEqualTo(0);
+    }
+
+    @Test
+    public void testUpdateGroupUuid() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setGroupUuid(1, FAKE_UUID2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setGroupUuid(subInfo.getSubscriptionId(), FAKE_UUID2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo).setGroupUuid(FAKE_UUID2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_GROUP_UUID)).isEqualTo(FAKE_UUID2);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_GROUP_UUID, FAKE_UUID1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getGroupUuid()).isEqualTo(FAKE_UUID1);
+    }
+
+    @Test
+    public void testUpdateCountryIso() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setCountryIso(1, FAKE_COUNTRY_CODE2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setCountryIso(subInfo.getSubscriptionId(), FAKE_COUNTRY_CODE2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setCountryIso(FAKE_COUNTRY_CODE2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_ISO_COUNTRY_CODE)).isEqualTo(FAKE_COUNTRY_CODE2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_ISO_COUNTRY_CODE, FAKE_COUNTRY_CODE1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getCountryIso()).isEqualTo(FAKE_COUNTRY_CODE1);
+    }
+
+    @Test
+    public void testUpdateCarrierId() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setCarrierId(1, FAKE_CARRIER_ID2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setCarrierId(subInfo.getSubscriptionId(), FAKE_CARRIER_ID2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setCarrierId(FAKE_CARRIER_ID2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_CARRIER_ID))
+                .isEqualTo(FAKE_CARRIER_ID2);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_CARRIER_ID, FAKE_CARRIER_ID1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getCarrierId())
+                .isEqualTo(FAKE_CARRIER_ID1);
+    }
+
+    @Test
+    public void testUpdateProfileClass() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setProfileClass(
+                        1, SubscriptionManager.PROFILE_CLASS_TESTING));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setProfileClass(subInfo.getSubscriptionId(),
+                SubscriptionManager.PROFILE_CLASS_TESTING);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setProfileClass(SubscriptionManager.PROFILE_CLASS_TESTING).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_PROFILE_CLASS))
+                .isEqualTo(SubscriptionManager.PROFILE_CLASS_TESTING);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_PROFILE_CLASS,
+                SubscriptionManager.PROFILE_CLASS_PROVISIONING);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getProfileClass())
+                .isEqualTo(SubscriptionManager.PROFILE_CLASS_PROVISIONING);
+    }
+
+    @Test
+    public void testUpdateSubscriptionType() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setSubscriptionType(
+                        1, SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setSubscriptionType(subInfo.getSubscriptionId(),
+                SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setType(SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_SUBSCRIPTION_TYPE))
+                .isEqualTo(SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_SUBSCRIPTION_TYPE,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getSubscriptionType())
+                .isEqualTo(SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+    }
+
+    @Test
+    public void testUpdateGroupOwner() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setGroupOwner(1, FAKE_OWNER2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setGroupOwner(subInfo.getSubscriptionId(), FAKE_OWNER2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setGroupOwner(FAKE_OWNER2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_GROUP_OWNER))
+                .isEqualTo(FAKE_OWNER2);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_GROUP_OWNER, FAKE_OWNER1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getGroupOwner())
+                .isEqualTo(FAKE_OWNER1);
+    }
+
+    @Test
+    public void testUpdateEnabledMobileDataPolicies() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setEnabledMobileDataPolicies(1, FAKE_MOBILE_DATA_POLICY2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setEnabledMobileDataPolicies(subInfo.getSubscriptionId(),
+                FAKE_MOBILE_DATA_POLICY2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setEnabledMobileDataPolicies(FAKE_MOBILE_DATA_POLICY2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES)).isEqualTo(FAKE_MOBILE_DATA_POLICY2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES, FAKE_MOBILE_DATA_POLICY1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getEnabledMobileDataPolicies())
+                .isEqualTo(FAKE_MOBILE_DATA_POLICY1);
+    }
+
+    @Test
+    public void testUpdateImsi() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setImsi(1, FAKE_IMSI2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setImsi(subInfo.getSubscriptionId(), FAKE_IMSI2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setImsi(FAKE_IMSI2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_IMSI))
+                .isEqualTo(FAKE_IMSI2);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_IMSI, FAKE_IMSI1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getImsi())
+                .isEqualTo(FAKE_IMSI1);
+    }
+
+    @Test
+    public void testUpdateUiccApplicationsEnabled() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setUiccApplicationsEnabled(1, 0));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setUiccApplicationsEnabled(subInfo.getSubscriptionId(), 0);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setUiccApplicationsEnabled(0).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_UICC_APPLICATIONS_ENABLED)).isEqualTo(0);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_UICC_APPLICATIONS_ENABLED, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getUiccApplicationsEnabled())
+                .isEqualTo(1);
+    }
+
+    @Test
+    public void testUpdateRcsUceEnabled() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setRcsUceEnabled(1, 0));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setRcsUceEnabled(subInfo.getSubscriptionId(), 0);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setRcsUceEnabled(0).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_IMS_RCS_UCE_ENABLED)).isEqualTo(0);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_IMS_RCS_UCE_ENABLED, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getRcsUceEnabled())
+                .isEqualTo(1);
+    }
+
+    @Test
+    public void testUpdateCrossSimCallingEnabled() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setCrossSimCallingEnabled(1, 0));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setCrossSimCallingEnabled(subInfo.getSubscriptionId(), 0);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setCrossSimCallingEnabled(0).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED)).isEqualTo(0);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getCrossSimCallingEnabled())
+                .isEqualTo(1);
+    }
+
+    @Test
+    public void testUpdateRcsConfig() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setRcsConfig(1, FAKE_RCS_CONFIG2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setRcsConfig(subInfo.getSubscriptionId(), FAKE_RCS_CONFIG2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setRcsConfig(FAKE_RCS_CONFIG2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_RCS_CONFIG)).isEqualTo(FAKE_RCS_CONFIG2);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_RCS_CONFIG, FAKE_RCS_CONFIG1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getRcsConfig())
+                .isEqualTo(FAKE_RCS_CONFIG1);
+    }
+
+    @Test
+    public void testUpdateAllowedNetworkTypesForReasons() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setAllowedNetworkTypesForReasons(
+                        1, FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setAllowedNetworkTypesForReasons(subInfo.getSubscriptionId(),
+                FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setAllowedNetworkTypesForReasons(FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_ALLOWED_NETWORK_TYPES_FOR_REASONS))
+                .isEqualTo(FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS2);
+        mDatabaseManagerUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_ALLOWED_NETWORK_TYPES_FOR_REASONS,
+                FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getAllowedNetworkTypesForReasons())
+                .isEqualTo(FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS1);
+    }
+
+    @Test
+    public void testUpdateDeviceToDeviceStatusSharingPreference() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setDeviceToDeviceStatusSharingPreference(
+                        1, SubscriptionManager.D2D_SHARING_DISABLED));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setDeviceToDeviceStatusSharingPreference(subInfo.getSubscriptionId(),
+                SubscriptionManager.D2D_SHARING_DISABLED);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setDeviceToDeviceStatusSharingPreference(
+                        SubscriptionManager.D2D_SHARING_DISABLED).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_D2D_STATUS_SHARING))
+                .isEqualTo(SubscriptionManager.D2D_SHARING_DISABLED);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_D2D_STATUS_SHARING,
+                SubscriptionManager.D2D_SHARING_ALL);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getDeviceToDeviceStatusSharingPreference())
+                .isEqualTo(SubscriptionManager.D2D_SHARING_ALL);
+    }
+
+    @Test
+    public void testUpdateNrAdvancedCallingEnabled() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setNrAdvancedCallingEnabled(1, 0));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setNrAdvancedCallingEnabled(subInfo.getSubscriptionId(), 0);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setNrAdvancedCallingEnabled(0).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED)).isEqualTo(0);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getNrAdvancedCallingEnabled()).isEqualTo(1);
+    }
+
+    @Test
+    public void testUpdateNumberFromCarrier() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setNumberFromCarrier(1, FAKE_PHONE_NUMBER2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setNumberFromCarrier(subInfo.getSubscriptionId(), FAKE_PHONE_NUMBER2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setNumberFromCarrier(FAKE_PHONE_NUMBER2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER)).isEqualTo(FAKE_PHONE_NUMBER2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_PHONE_NUMBER_SOURCE_CARRIER, FAKE_PHONE_NUMBER1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getNumberFromCarrier()).isEqualTo(FAKE_PHONE_NUMBER1);
+    }
+
+    @Test
+    public void testUpdateNumberFromIms() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setNumberFromIms(1, FAKE_PHONE_NUMBER2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setNumberFromIms(subInfo.getSubscriptionId(), FAKE_PHONE_NUMBER2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setNumberFromIms(FAKE_PHONE_NUMBER2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(
+                1, SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS)).isEqualTo(FAKE_PHONE_NUMBER2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_PHONE_NUMBER_SOURCE_IMS, FAKE_PHONE_NUMBER1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getNumberFromIms()).isEqualTo(FAKE_PHONE_NUMBER1);
+    }
+
+    @Test
+    public void testUpdatePortIndex() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setPortIndex(1, 1));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setPortIndex(subInfo.getSubscriptionId(), 1);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setPortIndex(1).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_PORT_INDEX))
+                .isEqualTo(1);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_PORT_INDEX, 2);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getPortIndex())
+                .isEqualTo(2);
+    }
+
+    @Test
+    public void testUpdateUsageSetting() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setUsageSetting(
+                        1, SubscriptionManager.USAGE_SETTING_VOICE_CENTRIC));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setUsageSetting(subInfo.getSubscriptionId(),
+                SubscriptionManager.USAGE_SETTING_VOICE_CENTRIC);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setUsageSetting(SubscriptionManager.USAGE_SETTING_VOICE_CENTRIC).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_USAGE_SETTING))
+                .isEqualTo(SubscriptionManager.USAGE_SETTING_VOICE_CENTRIC);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_USAGE_SETTING, SubscriptionManager.USAGE_SETTING_DATA_CENTRIC);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1).getUsageSetting())
+                .isEqualTo(SubscriptionManager.USAGE_SETTING_DATA_CENTRIC);
+    }
+
+    @Test
+    public void testUpdateLastUsedTPMessageReference() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setLastUsedTPMessageReference(
+                        1, FAKE_TP_MESSAGE_REFERENCE2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setLastUsedTPMessageReference(subInfo.getSubscriptionId(),
+                FAKE_TP_MESSAGE_REFERENCE2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setLastUsedTPMessageReference(FAKE_TP_MESSAGE_REFERENCE2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_TP_MESSAGE_REF))
+                .isEqualTo(FAKE_TP_MESSAGE_REFERENCE2);
+        mDatabaseManagerUT.setSubscriptionProperty(
+                1, SimInfo.COLUMN_TP_MESSAGE_REF, FAKE_TP_MESSAGE_REFERENCE1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getLastUsedTPMessageReference()).isEqualTo(FAKE_TP_MESSAGE_REFERENCE1);
+    }
+
+    @Test
+    public void testUpdateUserId() throws Exception {
+        // exception is expected if there is nothing in the database.
+        assertThrows(IllegalArgumentException.class,
+                () -> mDatabaseManagerUT.setUserId(1, FAKE_USER_ID2));
+
+        SubscriptionInfoInternal subInfo = insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        mDatabaseManagerUT.setUserId(subInfo.getSubscriptionId(), FAKE_USER_ID2);
+        processAllMessages();
+
+        subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                .setUserId(FAKE_USER_ID2).build();
+        verifySubscription(subInfo);
+        verify(mSubscriptionDatabaseManagerCallback, times(2)).onSubscriptionChanged(eq(1));
+
+        assertThat(mDatabaseManagerUT.getSubscriptionProperty(1, SimInfo.COLUMN_USER_HANDLE))
+                .isEqualTo(FAKE_USER_ID2);
+        mDatabaseManagerUT.setSubscriptionProperty(1, SimInfo.COLUMN_USER_HANDLE, FAKE_USER_ID1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getUserId()).isEqualTo(FAKE_USER_ID1);
+    }
+
+    @Test
+    public void testUpdateSubscriptionsInGroup() throws Exception {
+        insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO2);
+        // Two subs are now in the same group
+        mDatabaseManagerUT.setGroupUuid(2, FAKE_UUID1);
+
+        mDatabaseManagerUT.setWifiCallingEnabled(1, 1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .isWifiCallingEnabled()).isTrue();
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(2)
+                .isWifiCallingEnabled()).isTrue();
+
+        mDatabaseManagerUT.setWifiCallingEnabled(1, 0);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .isWifiCallingEnabled()).isFalse();
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(2)
+                .isWifiCallingEnabled()).isFalse();
+
+        mDatabaseManagerUT.setUserId(1, 5678);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getUserId()).isEqualTo(5678);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(2)
+                .getUserId()).isEqualTo(5678);
+
+        mDatabaseManagerUT.setWifiCallingEnabledForRoaming(1, 0);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .isWifiCallingEnabledForRoaming()).isFalse();
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(2)
+                .isWifiCallingEnabledForRoaming()).isFalse();
+
+        mDatabaseManagerUT.setDisplayName(1, "Pokemon");
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getDisplayName()).isEqualTo("Pokemon");
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(2)
+                .getDisplayName()).isEqualTo("Pokemon");
+
+        // ICCID is not the field that will be synced to all subs in the group.
+        mDatabaseManagerUT.setIccId(1, "0987");
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)
+                .getIccId()).isEqualTo("0987");
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(2)
+                .getIccId()).isEqualTo(FAKE_ICCID2);
+    }
+
+    @Test
+    public void testRemoveSubscriptionInfo() throws Exception {
+        insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO2);
+        Mockito.clearInvocations(mSubscriptionDatabaseManagerCallback);
+
+        mDatabaseManagerUT.removeSubscriptionInfo(1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)).isNull();
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(2))
+                .isEqualTo(FAKE_SUBSCRIPTION_INFO2);
+        verify(mSubscriptionDatabaseManagerCallback).onSubscriptionChanged(eq(1));
+
+        // Insert a new one. Should become sub 3.
+        Mockito.clearInvocations(mSubscriptionDatabaseManagerCallback);
+        insertSubscriptionAndVerify(FAKE_SUBSCRIPTION_INFO1);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)).isNull();
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(2))
+                .isEqualTo(FAKE_SUBSCRIPTION_INFO2);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(3))
+                .isEqualTo(new SubscriptionInfoInternal.Builder(FAKE_SUBSCRIPTION_INFO1)
+                        .setId(3).build());
+        verify(mSubscriptionDatabaseManagerCallback).onSubscriptionChanged(eq(3));
+
+        Mockito.clearInvocations(mSubscriptionDatabaseManagerCallback);
+        mDatabaseManagerUT.removeSubscriptionInfo(2);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)).isNull();
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(2)).isNull();
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(3))
+                .isEqualTo(new SubscriptionInfoInternal.Builder(FAKE_SUBSCRIPTION_INFO1)
+                        .setId(3).build());
+        verify(mSubscriptionDatabaseManagerCallback).onSubscriptionChanged(eq(2));
+
+        Mockito.clearInvocations(mSubscriptionDatabaseManagerCallback);
+        mDatabaseManagerUT.removeSubscriptionInfo(3);
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(1)).isNull();
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(2)).isNull();
+        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(3)).isNull();
+        verify(mSubscriptionDatabaseManagerCallback).onSubscriptionChanged(eq(3));
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/subscription/SubscriptionInfoInternalTest.java b/tests/telephonytests/src/com/android/internal/telephony/subscription/SubscriptionInfoInternalTest.java
new file mode 100644
index 0000000..7c78106
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/subscription/SubscriptionInfoInternalTest.java
@@ -0,0 +1,272 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.internal.telephony.subscription;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.os.ParcelUuid;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.UiccAccessRule;
+import android.telephony.ims.ImsMmTelManager;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class SubscriptionInfoInternalTest {
+    private final SubscriptionInfoInternal mSubInfo =
+            new SubscriptionInfoInternal.Builder()
+                    .setId(1)
+                    .setIccId(SubscriptionDatabaseManagerTest.FAKE_ICCID1)
+                    .setSimSlotIndex(0)
+                    .setDisplayName(SubscriptionDatabaseManagerTest.FAKE_CARRIER_NAME1)
+                    .setCarrierName(SubscriptionDatabaseManagerTest.FAKE_CARRIER_NAME1)
+                    .setDisplayNameSource(SubscriptionManager.NAME_SOURCE_SIM_SPN)
+                    .setIconTint(SubscriptionDatabaseManagerTest.FAKE_COLOR1)
+                    .setNumber(SubscriptionDatabaseManagerTest.FAKE_PHONE_NUMBER1)
+                    .setDataRoaming(SubscriptionManager.DATA_ROAMING_ENABLE)
+                    .setMcc(SubscriptionDatabaseManagerTest.FAKE_MCC1)
+                    .setMnc(SubscriptionDatabaseManagerTest.FAKE_MNC1)
+                    .setEhplmns(SubscriptionDatabaseManagerTest.FAKE_EHPLMNS1)
+                    .setHplmns(SubscriptionDatabaseManagerTest.FAKE_HPLMNS1)
+                    .setEmbedded(1)
+                    .setCardString(SubscriptionDatabaseManagerTest.FAKE_ICCID1)
+                    .setCardId(1)
+                    .setNativeAccessRules(SubscriptionDatabaseManagerTest
+                            .FAKE_NATIVE_ACCESS_RULES1)
+                    .setCarrierConfigAccessRules(SubscriptionDatabaseManagerTest
+                            .FAKE_CARRIER_CONFIG_ACCESS_RULES1)
+                    .setRemovableEmbedded(0)
+                    .setEnhanced4GModeEnabled(1)
+                    .setVideoTelephonyEnabled(1)
+                    .setWifiCallingEnabled(1)
+                    .setWifiCallingMode(ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED)
+                    .setWifiCallingModeForRoaming(ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED)
+                    .setWifiCallingEnabledForRoaming(1)
+                    .setOpportunistic(0)
+                    .setGroupUuid(SubscriptionDatabaseManagerTest.FAKE_UUID1)
+                    .setCountryIso(SubscriptionDatabaseManagerTest.FAKE_COUNTRY_CODE1)
+                    .setCarrierId(SubscriptionDatabaseManagerTest.FAKE_CARRIER_ID1)
+                    .setProfileClass(SubscriptionManager.PROFILE_CLASS_OPERATIONAL)
+                    .setType(SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM)
+                    .setGroupOwner(SubscriptionDatabaseManagerTest.FAKE_OWNER1)
+                    .setEnabledMobileDataPolicies(SubscriptionDatabaseManagerTest
+                            .FAKE_MOBILE_DATA_POLICY1)
+                    .setImsi(SubscriptionDatabaseManagerTest.FAKE_IMSI1)
+                    .setUiccApplicationsEnabled(1)
+                    .setRcsUceEnabled(1)
+                    .setCrossSimCallingEnabled(1)
+                    .setRcsConfig(SubscriptionDatabaseManagerTest.FAKE_RCS_CONFIG1)
+                    .setAllowedNetworkTypesForReasons(SubscriptionDatabaseManagerTest
+                            .FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS1)
+                    .setDeviceToDeviceStatusSharingPreference(
+                            SubscriptionManager.D2D_SHARING_ALL_CONTACTS)
+                    .setVoImsOptInEnabled(1)
+                    .setDeviceToDeviceStatusSharingContacts(
+                            SubscriptionDatabaseManagerTest.FAKE_CONTACT1)
+                    .setNrAdvancedCallingEnabled(1)
+                    .setNumberFromCarrier(SubscriptionDatabaseManagerTest.FAKE_PHONE_NUMBER1)
+                    .setNumberFromIms(SubscriptionDatabaseManagerTest.FAKE_PHONE_NUMBER1)
+                    .setPortIndex(0)
+                    .setUsageSetting(SubscriptionManager.USAGE_SETTING_DEFAULT)
+                    .setLastUsedTPMessageReference(SubscriptionDatabaseManagerTest
+                            .FAKE_TP_MESSAGE_REFERENCE1)
+                    .setUserId(SubscriptionDatabaseManagerTest.FAKE_USER_ID1)
+                    .setGroupDisabled(false)
+                    .build();
+
+    private final SubscriptionInfoInternal mSubInfoNull =
+            new SubscriptionInfoInternal.Builder()
+                    .setId(1)
+                    .setIccId("123")
+                    .setSimSlotIndex(0)
+                    .setDisplayName("")
+                    .setCarrierName("")
+                    .setNumber("")
+                    .setMcc("")
+                    .setMnc("")
+                    .setEhplmns("")
+                    .setHplmns("")
+                    .setEmbedded(1)
+                    .setCardId(1)
+                    .setNativeAccessRules(new byte[0])
+                    .setCarrierConfigAccessRules(new byte[0])
+                    .setGroupUuid("")
+                    .setCountryIso("")
+                    .setGroupOwner("")
+                    .setEnabledMobileDataPolicies("")
+                    .setImsi("")
+                    .setRcsConfig(new byte[0])
+                    .setAllowedNetworkTypesForReasons("")
+                    .setDeviceToDeviceStatusSharingContacts("")
+                    .build();
+
+    @Test
+    public void testSubscriptionInfoInternalSetAndGet() {
+        assertThat(mSubInfo.getSubscriptionId()).isEqualTo(1);
+        assertThat(mSubInfo.getIccId()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_ICCID1);
+        assertThat(mSubInfo.getSimSlotIndex()).isEqualTo(0);
+        assertThat(mSubInfo.getDisplayName()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_CARRIER_NAME1);
+        assertThat(mSubInfo.getCarrierName()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_CARRIER_NAME1);
+        assertThat(mSubInfo.getDisplayNameSource()).isEqualTo(
+                SubscriptionManager.NAME_SOURCE_SIM_SPN);
+        assertThat(mSubInfo.getIconTint()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_COLOR1);
+        assertThat(mSubInfo.getNumber()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_PHONE_NUMBER1);
+        assertThat(mSubInfo.getDataRoaming()).isEqualTo(SubscriptionManager.DATA_ROAMING_ENABLE);
+        assertThat(mSubInfo.getMcc()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_MCC1);
+        assertThat(mSubInfo.getMnc()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_MNC1);
+        assertThat(mSubInfo.getEhplmns()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_EHPLMNS1);
+        assertThat(mSubInfo.getHplmns()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_HPLMNS1);
+        assertThat(mSubInfo.getEmbedded()).isEqualTo(1);
+        assertThat(mSubInfo.getCardString()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_ICCID1);
+        assertThat(mSubInfo.getCardId()).isEqualTo(1);
+        assertThat(mSubInfo.getNativeAccessRules()).isEqualTo(SubscriptionDatabaseManagerTest
+                .FAKE_NATIVE_ACCESS_RULES1);
+        assertThat(mSubInfo.getCarrierConfigAccessRules()).isEqualTo(SubscriptionDatabaseManagerTest
+                .FAKE_CARRIER_CONFIG_ACCESS_RULES1);
+        assertThat(mSubInfo.getRemovableEmbedded()).isEqualTo(0);
+        assertThat(mSubInfo.getEnhanced4GModeEnabled()).isEqualTo(1);
+        assertThat(mSubInfo.getVideoTelephonyEnabled()).isEqualTo(1);
+        assertThat(mSubInfo.getWifiCallingEnabled()).isEqualTo(1);
+        assertThat(mSubInfo.getWifiCallingMode()).isEqualTo(
+                ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED);
+        assertThat(mSubInfo.getWifiCallingModeForRoaming()).isEqualTo(
+                ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED);
+        assertThat(mSubInfo.getOpportunistic()).isEqualTo(0);
+        assertThat(mSubInfo.getGroupUuid()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_UUID1);
+        assertThat(mSubInfo.getCountryIso()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_COUNTRY_CODE1);
+        assertThat(mSubInfo.getCarrierId()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_CARRIER_ID1);
+        assertThat(mSubInfo.getProfileClass()).isEqualTo(
+                SubscriptionManager.PROFILE_CLASS_OPERATIONAL);
+        assertThat(mSubInfo.getSubscriptionType()).isEqualTo(
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+        assertThat(mSubInfo.getGroupOwner()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_OWNER1);
+        assertThat(mSubInfo.getEnabledMobileDataPolicies()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_MOBILE_DATA_POLICY1);
+        assertThat(mSubInfo.getImsi()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_IMSI1);
+        assertThat(mSubInfo.getUiccApplicationsEnabled()).isEqualTo(1);
+        assertThat(mSubInfo.getRcsUceEnabled()).isEqualTo(1);
+        assertThat(mSubInfo.getCrossSimCallingEnabled()).isEqualTo(1);
+        assertThat(mSubInfo.getRcsConfig()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_RCS_CONFIG1);
+        assertThat(mSubInfo.getAllowedNetworkTypesForReasons()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_ALLOWED_NETWORK_TYPES_FOR_REASONS1);
+        assertThat(mSubInfo.getVoImsOptInEnabled()).isEqualTo(1);
+        assertThat(mSubInfo.getDeviceToDeviceStatusSharingContacts()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_CONTACT1);
+        assertThat(mSubInfo.getNrAdvancedCallingEnabled()).isEqualTo(1);
+        assertThat(mSubInfo.getNumberFromCarrier()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_PHONE_NUMBER1);
+        assertThat(mSubInfo.getNumberFromIms()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_PHONE_NUMBER1);
+        assertThat(mSubInfo.getPortIndex()).isEqualTo(
+                SubscriptionManager.USAGE_SETTING_DEFAULT);
+        assertThat(mSubInfo.getLastUsedTPMessageReference()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_TP_MESSAGE_REFERENCE1);
+        assertThat(mSubInfo.getUserId()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_USER_ID1);
+        assertThat(mSubInfo.isGroupDisabled()).isFalse();
+    }
+
+    @Test
+    public void testEquals() {
+        SubscriptionInfoInternal another = new SubscriptionInfoInternal.Builder(mSubInfo).build();
+        assertThat(another).isEqualTo(mSubInfo);
+    }
+
+    @Test
+    public void testConvertToSubscriptionInfo() {
+        SubscriptionInfo subInfo = mSubInfo.toSubscriptionInfo();
+
+        assertThat(subInfo.getSubscriptionId()).isEqualTo(1);
+        assertThat(subInfo.getIccId()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_ICCID1);
+        assertThat(subInfo.getSimSlotIndex()).isEqualTo(0);
+        assertThat(subInfo.getDisplayName()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_CARRIER_NAME1);
+        assertThat(subInfo.getCarrierName()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_CARRIER_NAME1);
+        assertThat(subInfo.getDisplayNameSource()).isEqualTo(
+                SubscriptionManager.NAME_SOURCE_SIM_SPN);
+        assertThat(subInfo.getIconTint()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_COLOR1);
+        assertThat(subInfo.getNumber()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_PHONE_NUMBER1);
+        assertThat(subInfo.getDataRoaming()).isEqualTo(SubscriptionManager.DATA_ROAMING_ENABLE);
+        assertThat(subInfo.getMccString()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_MCC1);
+        assertThat(subInfo.getMncString()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_MNC1);
+        assertThat(subInfo.getEhplmns()).isEqualTo(Arrays.asList(
+                SubscriptionDatabaseManagerTest.FAKE_EHPLMNS1.split(",")));
+        assertThat(subInfo.getHplmns()).isEqualTo(Arrays.asList(
+                SubscriptionDatabaseManagerTest.FAKE_HPLMNS1.split(",")));
+        assertThat(subInfo.isEmbedded()).isTrue();
+        assertThat(subInfo.getCardString()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_ICCID1);
+        assertThat(subInfo.getCardId()).isEqualTo(1);
+
+
+        List<UiccAccessRule> rules = new ArrayList<>();
+
+        rules.addAll(Arrays.asList(UiccAccessRule.decodeRules(
+                SubscriptionDatabaseManagerTest.FAKE_NATIVE_ACCESS_RULES1)));
+        rules.addAll(Arrays.asList(UiccAccessRule.decodeRules(
+                SubscriptionDatabaseManagerTest.FAKE_CARRIER_CONFIG_ACCESS_RULES1)));
+
+        assertThat(subInfo.getAccessRules()).containsExactlyElementsIn(rules);
+
+        assertThat(subInfo.isOpportunistic()).isFalse();
+        assertThat(subInfo.getGroupUuid()).isEqualTo(ParcelUuid.fromString(
+                SubscriptionDatabaseManagerTest.FAKE_UUID1));
+        assertThat(subInfo.getCountryIso()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_COUNTRY_CODE1);
+        assertThat(subInfo.getCarrierId()).isEqualTo(
+                SubscriptionDatabaseManagerTest.FAKE_CARRIER_ID1);
+        assertThat(subInfo.getProfileClass()).isEqualTo(
+                SubscriptionManager.PROFILE_CLASS_OPERATIONAL);
+        assertThat(subInfo.getSubscriptionType()).isEqualTo(
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+        assertThat(subInfo.getGroupOwner()).isEqualTo(SubscriptionDatabaseManagerTest.FAKE_OWNER1);
+        assertThat(subInfo.areUiccApplicationsEnabled()).isTrue();
+        assertThat(subInfo.getPortIndex()).isEqualTo(
+                SubscriptionManager.USAGE_SETTING_DEFAULT);
+        assertThat(subInfo.isGroupDisabled()).isFalse();
+    }
+
+    @Test
+    public void testNullability() {
+        SubscriptionInfo subInfoNull = mSubInfoNull.toSubscriptionInfo();
+        assertThat(subInfoNull.getSubscriptionId()).isEqualTo(1);
+        assertThat(subInfoNull.getIccId()).isEqualTo("123");
+        assertThat(subInfoNull.getSimSlotIndex()).isEqualTo(0);
+        assertThat(subInfoNull.getDisplayName()).isEqualTo("");
+        assertThat(subInfoNull.getCarrierName()).isEqualTo("");
+        assertThat(subInfoNull.getNumber()).isEqualTo("");
+        assertThat(subInfoNull.getMccString()).isEqualTo("");
+        assertThat(subInfoNull.getMncString()).isEqualTo("");
+        assertThat(subInfoNull.getEhplmns()).isEmpty();
+        assertThat(subInfoNull.getHplmns()).isEmpty();
+        assertThat(subInfoNull.isEmbedded()).isTrue();
+        assertThat(subInfoNull.getCardId()).isEqualTo(1);
+        assertThat(subInfoNull.getAccessRules()).isNull();
+        assertThat(subInfoNull.getGroupUuid()).isNull();
+        assertThat(subInfoNull.getCountryIso()).isEqualTo("");
+        assertThat(subInfoNull.getGroupOwner()).isEqualTo("");
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/subscription/SubscriptionManagerServiceTest.java b/tests/telephonytests/src/com/android/internal/telephony/subscription/SubscriptionManagerServiceTest.java
new file mode 100644
index 0000000..ebd4ad2
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/subscription/SubscriptionManagerServiceTest.java
@@ -0,0 +1,1684 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.subscription;
+
+import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_CARRIER_ID1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_CARRIER_ID2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_CARRIER_NAME1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_CARRIER_NAME2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_CONTACT1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_CONTACT2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_COUNTRY_CODE2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_ICCID1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_ICCID2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_MCC1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_MCC2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_MNC1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_MNC2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_MOBILE_DATA_POLICY1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_MOBILE_DATA_POLICY2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_NATIVE_ACCESS_RULES1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_NATIVE_ACCESS_RULES2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_PHONE_NUMBER1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_PHONE_NUMBER2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_RCS_CONFIG1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_RCS_CONFIG2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_SUBSCRIPTION_INFO1;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_SUBSCRIPTION_INFO2;
+import static com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.FAKE_UUID1;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import android.Manifest;
+import android.annotation.NonNull;
+import android.app.AppOpsManager;
+import android.app.PropertyInvalidatedCache;
+import android.compat.testing.PlatformCompatChangeRule;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.res.Resources;
+import android.os.Build;
+import android.os.Bundle;
+import android.os.Looper;
+import android.os.ParcelUuid;
+import android.os.UserHandle;
+import android.provider.Settings;
+import android.provider.Telephony;
+import android.provider.Telephony.SimInfo;
+import android.service.carrier.CarrierIdentifier;
+import android.service.euicc.EuiccProfileInfo;
+import android.service.euicc.EuiccService;
+import android.service.euicc.GetEuiccProfileInfoListResult;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.UiccAccessRule;
+import android.test.mock.MockContentResolver;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+import android.util.Base64;
+
+import com.android.internal.telephony.ContextFixture;
+import com.android.internal.telephony.TelephonyIntents;
+import com.android.internal.telephony.TelephonyTest;
+import com.android.internal.telephony.euicc.EuiccController;
+import com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.SubscriptionProvider;
+import com.android.internal.telephony.subscription.SubscriptionManagerService.SubscriptionManagerServiceCallback;
+
+import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestRule;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class SubscriptionManagerServiceTest extends TelephonyTest {
+
+    private static final String CALLING_PACKAGE = "calling_package";
+
+    private static final String CALLING_FEATURE = "calling_feature";
+
+    private SubscriptionManagerService mSubscriptionManagerServiceUT;
+
+    private final SubscriptionProvider mSubscriptionProvider = new SubscriptionProvider();
+
+    private static final UserHandle FAKE_USER_HANDLE = new UserHandle(12);
+
+    // mocked
+    private SubscriptionManagerServiceCallback mMockedSubscriptionManagerServiceCallback;
+    private EuiccController mEuiccController;
+
+    @Rule
+    public TestRule compatChangeRule = new PlatformCompatChangeRule();
+
+    @Before
+    public void setUp() throws Exception {
+        logd("SubscriptionManagerServiceTest +Setup!");
+        super.setUp(getClass().getSimpleName());
+        mContextFixture.addSystemFeature(PackageManager.FEATURE_TELEPHONY_EUICC);
+        setupMocksForTelephonyPermissions(Build.VERSION_CODES.UPSIDE_DOWN_CAKE);
+        PropertyInvalidatedCache.disableForCurrentProcess("cache_key.is_compat_change_enabled");
+
+        doReturn(true).when(mTelephonyManager).isVoiceCapable();
+        mEuiccController = Mockito.mock(EuiccController.class);
+        replaceInstance(EuiccController.class, "sInstance", null, mEuiccController);
+        mMockedSubscriptionManagerServiceCallback = Mockito.mock(
+                SubscriptionManagerServiceCallback.class);
+        ((MockContentResolver) mContext.getContentResolver()).addProvider(
+                Telephony.Carriers.CONTENT_URI.getAuthority(), mSubscriptionProvider);
+        mSubscriptionManagerServiceUT = new SubscriptionManagerService(mContext, Looper.myLooper());
+
+        doAnswer(invocation -> {
+            ((Runnable) invocation.getArguments()[0]).run();
+            return null;
+        }).when(mMockedSubscriptionManagerServiceCallback).invokeFromExecutor(any(Runnable.class));
+
+        mSubscriptionManagerServiceUT.registerCallback(mMockedSubscriptionManagerServiceCallback);
+        // Database loading is on a different thread. Need to wait a bit.
+        waitForMs(100);
+        processAllFutureMessages();
+
+        // Revoke all permissions.
+        mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);
+        doReturn(AppOpsManager.MODE_DEFAULT).when(mAppOpsManager).noteOpNoThrow(anyString(),
+                anyInt(), nullable(String.class), nullable(String.class), nullable(String.class));
+        setIdentifierAccess(false);
+        setPhoneNumberAccess(PackageManager.PERMISSION_DENIED);
+
+        logd("SubscriptionManagerServiceTest -Setup!");
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.MULTI_SIM_VOICE_CALL_SUBSCRIPTION,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.MULTI_SIM_DATA_CALL_SUBSCRIPTION,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.MULTI_SIM_SMS_SUBSCRIPTION,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        super.tearDown();
+    }
+
+    /**
+     * Insert the subscription info to the database.
+     *
+     * @param subInfo The subscription to be inserted.
+     * @return The new sub id.
+     */
+    private int insertSubscription(@NonNull SubscriptionInfoInternal subInfo) {
+        try {
+            mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+            subInfo = new SubscriptionInfoInternal.Builder(subInfo)
+                    .setId(SubscriptionManager.INVALID_SUBSCRIPTION_ID).build();
+
+            Field field = SubscriptionManagerService.class.getDeclaredField(
+                    "mSubscriptionDatabaseManager");
+            field.setAccessible(true);
+            SubscriptionDatabaseManager sdbm =
+                    (SubscriptionDatabaseManager) field.get(mSubscriptionManagerServiceUT);
+
+            Class[] cArgs = new Class[1];
+            cArgs[0] = SubscriptionInfoInternal.class;
+            Method method = SubscriptionDatabaseManager.class.getDeclaredMethod(
+                    "insertSubscriptionInfo", cArgs);
+            method.setAccessible(true);
+            int subId = (int) method.invoke(sdbm, subInfo);
+
+            // Insertion is sync, but the onSubscriptionChanged callback is handled by the handler.
+            processAllMessages();
+
+            Class<?> WatchedMapClass = Class.forName("com.android.internal.telephony.subscription"
+                    + ".SubscriptionManagerService$WatchedMap");
+            field = SubscriptionManagerService.class.getDeclaredField("mSlotIndexToSubId");
+            field.setAccessible(true);
+            Object map = field.get(mSubscriptionManagerServiceUT);
+            cArgs = new Class[2];
+            cArgs[0] = Object.class;
+            cArgs[1] = Object.class;
+
+            method = WatchedMapClass.getDeclaredMethod("put", cArgs);
+            method.setAccessible(true);
+            method.invoke(map, subInfo.getSimSlotIndex(), subId);
+            mContextFixture.removeCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+            processAllMessages();
+            verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(subId));
+            Mockito.clearInvocations(mMockedSubscriptionManagerServiceCallback);
+            return subId;
+        } catch (Exception e) {
+            fail("Failed to insert subscription. e=" + e);
+        }
+        return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+    }
+
+    private void enableGetSubscriptionUserHandle() {
+        Resources mResources = mock(Resources.class);
+        doReturn(true).when(mResources).getBoolean(
+                eq(com.android.internal.R.bool.config_enable_get_subscription_user_handle));
+        doReturn(mResources).when(mContext).getResources();
+    }
+
+    @Test
+    public void testAddSubInfo() {
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        mSubscriptionManagerServiceUT.addSubInfo(FAKE_ICCID1, FAKE_CARRIER_NAME1,
+                0, SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+        processAllMessages();
+
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo.getIccId()).isEqualTo(FAKE_ICCID1);
+        assertThat(subInfo.getDisplayName()).isEqualTo(FAKE_CARRIER_NAME1);
+        assertThat(subInfo.getSimSlotIndex()).isEqualTo(0);
+        assertThat(subInfo.getSubscriptionType()).isEqualTo(
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+    }
+
+    @Test
+    public void testSetMccMnc() {
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        mSubscriptionManagerServiceUT.addSubInfo(FAKE_ICCID1, FAKE_CARRIER_NAME1,
+                0, SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+        processAllMessages();
+
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+        Mockito.clearInvocations(mMockedSubscriptionManagerServiceCallback);
+        mSubscriptionManagerServiceUT.setMccMnc(1, FAKE_MCC2 + FAKE_MNC2);
+        processAllMessages();
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getMcc()).isEqualTo(FAKE_MCC2);
+        assertThat(subInfo.getMnc()).isEqualTo(FAKE_MNC2);
+        verify(mMockedSubscriptionManagerServiceCallback, times(2)).onSubscriptionChanged(eq(1));
+    }
+
+    @Test
+    public void testSetCountryIso() {
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        mSubscriptionManagerServiceUT.addSubInfo(FAKE_ICCID1, FAKE_CARRIER_NAME1,
+                0, SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+        processAllMessages();
+
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+        Mockito.clearInvocations(mMockedSubscriptionManagerServiceCallback);
+        mSubscriptionManagerServiceUT.setCountryIso(1, FAKE_COUNTRY_CODE2);
+        processAllMessages();
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getCountryIso()).isEqualTo(FAKE_COUNTRY_CODE2);
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+    }
+
+    @Test
+    public void testSetCarrierId() {
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        mSubscriptionManagerServiceUT.addSubInfo(FAKE_ICCID1, FAKE_CARRIER_NAME1,
+                0, SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+        processAllMessages();
+
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+        Mockito.clearInvocations(mMockedSubscriptionManagerServiceCallback);
+        mSubscriptionManagerServiceUT.setCarrierId(1, FAKE_CARRIER_ID2);
+        processAllMessages();
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getCarrierId()).isEqualTo(FAKE_CARRIER_ID2);
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+    }
+
+    @Test
+    public void testSetPhoneNumber() {
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        mSubscriptionManagerServiceUT.addSubInfo(FAKE_ICCID1, FAKE_CARRIER_NAME1,
+                0, SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM);
+        processAllMessages();
+
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+        Mockito.clearInvocations(mMockedSubscriptionManagerServiceCallback);
+
+        // Caller does not have carrier privilege
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionManagerServiceUT.setPhoneNumber(1,
+                        SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER, FAKE_PHONE_NUMBER2,
+                        CALLING_PACKAGE, CALLING_FEATURE));
+
+        // Grant carrier privilege
+        setCarrierPrivilegesForSubId(true, 1);
+
+        // Source IMS is not acceptable
+        assertThrows(IllegalArgumentException.class,
+                () -> mSubscriptionManagerServiceUT.setPhoneNumber(1,
+                        SubscriptionManager.PHONE_NUMBER_SOURCE_IMS, FAKE_PHONE_NUMBER2,
+                        CALLING_PACKAGE, CALLING_FEATURE));
+
+        mSubscriptionManagerServiceUT.setPhoneNumber(1,
+                SubscriptionManager.PHONE_NUMBER_SOURCE_CARRIER, FAKE_PHONE_NUMBER2,
+                CALLING_PACKAGE, CALLING_FEATURE);
+        processAllMessages();
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getNumberFromCarrier()).isEqualTo(FAKE_PHONE_NUMBER2);
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+    }
+
+    @Test
+    public void testGetAllSubInfoList() {
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        doReturn(new int[]{1, 2}).when(mSubscriptionManager).getCompleteActiveSubscriptionIdList();
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(FAKE_SUBSCRIPTION_INFO2);
+
+        // Should throw security exception if the caller does not have permission.
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionManagerServiceUT.getAllSubInfoList(
+                        CALLING_PACKAGE, CALLING_FEATURE));
+
+        // Grant carrier privilege for sub 1
+        setCarrierPrivilegesForSubId(true, 1);
+        // Grant carrier privilege for sub 2
+        setCarrierPrivilegesForSubId(true, 2);
+
+        List<SubscriptionInfo> subInfos = mSubscriptionManagerServiceUT.getAllSubInfoList(
+                CALLING_PACKAGE, CALLING_FEATURE);
+        assertThat(subInfos).hasSize(2);
+
+        assertThat(subInfos.get(0)).isEqualTo(FAKE_SUBSCRIPTION_INFO1.toSubscriptionInfo());
+
+        assertThat(subInfos.get(1)).isEqualTo(FAKE_SUBSCRIPTION_INFO2.toSubscriptionInfo());
+
+        // Revoke carrier privilege for sub 2
+        setCarrierPrivilegesForSubId(false, 2);
+
+        subInfos = mSubscriptionManagerServiceUT.getAllSubInfoList(
+                CALLING_PACKAGE, CALLING_FEATURE);
+        // Should only have access to one sub.
+        assertThat(subInfos).hasSize(1);
+
+        assertThat(subInfos.get(0).getIccId()).isEqualTo(FAKE_ICCID1);
+        assertThat(subInfos.get(0).getCardString()).isEqualTo(FAKE_ICCID1);
+        assertThat(subInfos.get(0).getNumber()).isEqualTo(FAKE_PHONE_NUMBER1);
+
+        // Grant READ_PHONE_STATE permission
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE);
+        // Grant identifier access
+        setIdentifierAccess(true);
+        // Revoke carrier privileges.
+        setCarrierPrivilegesForSubId(false, 1);
+        setCarrierPrivilegesForSubId(false, 2);
+
+        subInfos = mSubscriptionManagerServiceUT.getAllSubInfoList(
+                CALLING_PACKAGE, CALLING_FEATURE);
+        assertThat(subInfos).hasSize(2);
+
+        assertThat(subInfos.get(0).getIccId()).isEqualTo(FAKE_ICCID1);
+        assertThat(subInfos.get(0).getCardString()).isEqualTo(FAKE_ICCID1);
+        // Phone number should be empty
+        assertThat(subInfos.get(0).getNumber()).isEmpty();
+        assertThat(subInfos.get(1).getIccId()).isEqualTo(FAKE_ICCID2);
+        assertThat(subInfos.get(1).getCardString()).isEqualTo(FAKE_ICCID2);
+        // Phone number should be empty
+        assertThat(subInfos.get(1).getNumber()).isEmpty();
+
+        // Grant phone number access
+        doReturn(PackageManager.PERMISSION_GRANTED).when(mMockLegacyPermissionManager)
+                .checkPhoneNumberAccess(anyString(), anyString(), anyString(), anyInt(), anyInt());
+
+        subInfos = mSubscriptionManagerServiceUT.getAllSubInfoList(
+                CALLING_PACKAGE, CALLING_FEATURE);
+        assertThat(subInfos).hasSize(2);
+        assertThat(subInfos.get(0).getNumber()).isEqualTo(FAKE_PHONE_NUMBER1);
+        assertThat(subInfos.get(1).getNumber()).isEqualTo(FAKE_PHONE_NUMBER2);
+    }
+
+    @Test
+    @EnableCompatChanges({SubscriptionManagerService.REQUIRE_DEVICE_IDENTIFIERS_FOR_GROUP_UUID})
+    public void testGetSubscriptionsInGroup() {
+        doReturn(new int[]{1, 2}).when(mSubscriptionManager).getCompleteActiveSubscriptionIdList();
+
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        SubscriptionInfoInternal anotherSubInfo =
+                new SubscriptionInfoInternal.Builder(FAKE_SUBSCRIPTION_INFO2)
+                        .setGroupUuid(FAKE_UUID1)
+                        .build();
+        insertSubscription(anotherSubInfo);
+
+        // Throw exception is the new behavior.
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionManagerServiceUT.getSubscriptionsInGroup(
+                        ParcelUuid.fromString(FAKE_UUID1), CALLING_PACKAGE, CALLING_FEATURE));
+
+        // Grant carrier privilege on sub 1 and 2
+        setCarrierPrivilegesForSubId(true, 1);
+        setCarrierPrivilegesForSubId(true, 2);
+        List<SubscriptionInfo> subInfos = mSubscriptionManagerServiceUT.getSubscriptionsInGroup(
+                ParcelUuid.fromString(FAKE_UUID1), CALLING_PACKAGE, CALLING_FEATURE);
+
+        assertThat(subInfos).hasSize(2);
+        assertThat(subInfos.get(0)).isEqualTo(FAKE_SUBSCRIPTION_INFO1.toSubscriptionInfo());
+        assertThat(subInfos.get(1)).isEqualTo(anotherSubInfo.toSubscriptionInfo());
+
+        // Grant READ_PHONE_STATE permission
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE);
+        setIdentifierAccess(false);
+        setCarrierPrivilegesForSubId(false, 1);
+        setCarrierPrivilegesForSubId(false, 2);
+        doNothing().when(mContext).enforcePermission(
+                eq(android.Manifest.permission.READ_PHONE_STATE), anyInt(), anyInt(), anyString());
+
+        // Throw exception is the new behavior. Only has READ_PHONE_STATE is not enough. Need
+        // identifier access as well.
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionManagerServiceUT.getSubscriptionsInGroup(
+                        ParcelUuid.fromString(FAKE_UUID1), CALLING_PACKAGE, CALLING_FEATURE));
+
+        // Grant identifier access
+        setIdentifierAccess(true);
+        // Grant phone number access
+        setPhoneNumberAccess(PackageManager.PERMISSION_GRANTED);
+
+        subInfos = mSubscriptionManagerServiceUT.getSubscriptionsInGroup(
+                ParcelUuid.fromString(FAKE_UUID1), CALLING_PACKAGE, CALLING_FEATURE);
+
+        assertThat(subInfos).hasSize(2);
+        assertThat(subInfos).containsExactlyElementsIn(
+                List.of(FAKE_SUBSCRIPTION_INFO1.toSubscriptionInfo(),
+                        anotherSubInfo.toSubscriptionInfo()));
+    }
+
+    @Test
+    public void testGetAvailableSubscriptionInfoList() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        SubscriptionInfoInternal anotherSubInfo =
+                new SubscriptionInfoInternal.Builder(FAKE_SUBSCRIPTION_INFO2)
+                        .setSimSlotIndex(SubscriptionManager.INVALID_SIM_SLOT_INDEX)
+                        .setType(SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM)
+                        .build();
+        insertSubscription(anotherSubInfo);
+
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionManagerServiceUT.getAvailableSubscriptionInfoList(
+                        CALLING_PACKAGE, CALLING_FEATURE));
+        // Grant carrier privilege for sub 1
+        setCarrierPrivilegesForSubId(true, 1);
+
+        // Not yet planned for carrier apps to access this API.
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionManagerServiceUT.getAvailableSubscriptionInfoList(
+                        CALLING_PACKAGE, CALLING_FEATURE));
+
+        // Grant READ_PHONE_STATE permission, which is not enough for this API.
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE);
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionManagerServiceUT.getAvailableSubscriptionInfoList(
+                        CALLING_PACKAGE, CALLING_FEATURE));
+
+        // Grant READ_PRIVILEGED_PHONE_STATE permission
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        List<SubscriptionInfo> subInfos = mSubscriptionManagerServiceUT
+                .getAvailableSubscriptionInfoList(CALLING_PACKAGE, CALLING_FEATURE);
+        assertThat(subInfos).hasSize(1);
+        assertThat(subInfos.get(0)).isEqualTo(FAKE_SUBSCRIPTION_INFO1.toSubscriptionInfo());
+    }
+
+    @Test
+    public void testSetDefaultVoiceSubId() throws Exception {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(FAKE_SUBSCRIPTION_INFO2);
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionManagerServiceUT.setDefaultVoiceSubId(1));
+
+        // Grant MODIFY_PHONE_STATE permission
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        mSubscriptionManagerServiceUT.setDefaultVoiceSubId(1);
+
+        assertThat(Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.MULTI_SIM_VOICE_CALL_SUBSCRIPTION)).isEqualTo(1);
+        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
+        verify(mContext, times(2)).sendStickyBroadcastAsUser(
+                captorIntent.capture(), eq(UserHandle.ALL));
+
+        Intent intent = captorIntent.getAllValues().get(0);
+        assertThat(intent.getAction()).isEqualTo(
+                TelephonyIntents.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED);
+
+        Bundle b = intent.getExtras();
+
+        assertThat(b.containsKey(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX)).isTrue();
+        assertThat(b.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX)).isEqualTo(1);
+
+        intent = captorIntent.getAllValues().get(1);
+        assertThat(intent.getAction()).isEqualTo(
+                SubscriptionManager.ACTION_DEFAULT_SUBSCRIPTION_CHANGED);
+
+        b = intent.getExtras();
+
+        assertThat(b.containsKey(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX)).isTrue();
+        assertThat(b.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX)).isEqualTo(1);
+    }
+
+    @Test
+    public void testSetDefaultDataSubId() throws Exception {
+        doReturn(false).when(mTelephonyManager).isVoiceCapable();
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(FAKE_SUBSCRIPTION_INFO2);
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionManagerServiceUT.setDefaultDataSubId(1));
+
+        // Grant MODIFY_PHONE_STATE permission
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        mSubscriptionManagerServiceUT.setDefaultDataSubId(1);
+
+        assertThat(Settings.Global.getInt(mContext.getContentResolver(),
+                        Settings.Global.MULTI_SIM_DATA_CALL_SUBSCRIPTION)).isEqualTo(1);
+        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
+        verify(mContext, times(2)).sendStickyBroadcastAsUser(
+                captorIntent.capture(), eq(UserHandle.ALL));
+
+        Intent intent = captorIntent.getAllValues().get(0);
+        assertThat(intent.getAction()).isEqualTo(
+                TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
+
+        Bundle b = intent.getExtras();
+
+        assertThat(b.containsKey(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX)).isTrue();
+        assertThat(b.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX)).isEqualTo(1);
+
+        intent = captorIntent.getAllValues().get(1);
+        assertThat(intent.getAction()).isEqualTo(
+                SubscriptionManager.ACTION_DEFAULT_SUBSCRIPTION_CHANGED);
+
+        b = intent.getExtras();
+
+        assertThat(b.containsKey(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX)).isTrue();
+        assertThat(b.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX)).isEqualTo(1);
+    }
+
+    @Test
+    public void testSetDefaultSmsSubId() throws Exception {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(FAKE_SUBSCRIPTION_INFO2);
+
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class,
+                () -> mSubscriptionManagerServiceUT.setDefaultSmsSubId(1));
+
+        // Grant MODIFY_PHONE_STATE permission
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        mSubscriptionManagerServiceUT.setDefaultSmsSubId(1);
+
+        assertThat(Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.MULTI_SIM_SMS_SUBSCRIPTION)).isEqualTo(1);
+        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
+        verify(mContext).sendStickyBroadcastAsUser(captorIntent.capture(), eq(UserHandle.ALL));
+
+        Intent intent = captorIntent.getValue();
+        assertThat(intent.getAction()).isEqualTo(
+                SubscriptionManager.ACTION_DEFAULT_SMS_SUBSCRIPTION_CHANGED);
+
+        Bundle b = intent.getExtras();
+
+        assertThat(b.containsKey(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX)).isTrue();
+        assertThat(b.getInt(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX)).isEqualTo(1);
+    }
+
+    @Test
+    public void testIsActiveSubId() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(new SubscriptionInfoInternal.Builder(FAKE_SUBSCRIPTION_INFO2)
+                .setSimSlotIndex(SubscriptionManager.INVALID_SIM_SLOT_INDEX).build());
+
+        // Should fail without READ_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .isActiveSubId(1, CALLING_PACKAGE, CALLING_FEATURE));
+
+        // Grant READ_PRIVILEGED_PHONE_STATE permission for insertion.
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.isActiveSubId(
+                1, CALLING_PACKAGE, CALLING_FEATURE)).isTrue();
+        assertThat(mSubscriptionManagerServiceUT.isActiveSubId(
+                2, CALLING_PACKAGE, CALLING_FEATURE)).isFalse();
+    }
+
+    @Test
+    public void testGetActiveSubscriptionInfoList() {
+        doReturn(new int[]{1}).when(mSubscriptionManager).getCompleteActiveSubscriptionIdList();
+        // Grant MODIFY_PHONE_STATE permission for insertion.
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(new SubscriptionInfoInternal.Builder(FAKE_SUBSCRIPTION_INFO2)
+                .setSimSlotIndex(SubscriptionManager.INVALID_SIM_SLOT_INDEX).build());
+        // Remove MODIFY_PHONE_STATE
+        mContextFixture.removeCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // Should fail without READ_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getActiveSubscriptionInfoList(CALLING_PACKAGE, CALLING_FEATURE));
+
+        // Grant READ_PHONE_STATE permission for insertion.
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE);
+
+        List<SubscriptionInfo> subInfos = mSubscriptionManagerServiceUT
+                .getActiveSubscriptionInfoList(CALLING_PACKAGE, CALLING_FEATURE);
+        assertThat(subInfos).hasSize(1);
+        assertThat(subInfos.get(0).getIccId()).isEmpty();
+        assertThat(subInfos.get(0).getCardString()).isEmpty();
+        assertThat(subInfos.get(0).getNumber()).isEmpty();
+        assertThat(subInfos.get(0).getGroupUuid()).isNull();
+
+        // Grant carrier privilege
+        setCarrierPrivilegesForSubId(true, 1);
+
+        subInfos = mSubscriptionManagerServiceUT
+                .getActiveSubscriptionInfoList(CALLING_PACKAGE, CALLING_FEATURE);
+        assertThat(subInfos).hasSize(1);
+        assertThat(subInfos.get(0)).isEqualTo(FAKE_SUBSCRIPTION_INFO1.toSubscriptionInfo());
+    }
+
+    @Test
+    public void testGetActiveSubscriptionInfoForSimSlotIndex() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(FAKE_SUBSCRIPTION_INFO2);
+
+        // Should fail without READ_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getActiveSubscriptionInfoForSimSlotIndex(0, CALLING_PACKAGE, CALLING_FEATURE));
+
+        // Grant READ_PHONE_STATE permission for insertion.
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE);
+        SubscriptionInfo subInfo = mSubscriptionManagerServiceUT
+                .getActiveSubscriptionInfoForSimSlotIndex(0, CALLING_PACKAGE,
+                        CALLING_FEATURE);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getSubscriptionId()).isEqualTo(1);
+        assertThat(subInfo.getIccId()).isEmpty();
+        assertThat(subInfo.getNumber()).isEmpty();
+
+        // Grant carrier privilege for sub 1
+        setCarrierPrivilegesForSubId(true, 1);
+        subInfo = mSubscriptionManagerServiceUT.getActiveSubscriptionInfoForSimSlotIndex(
+                0, CALLING_PACKAGE, CALLING_FEATURE);
+        assertThat(subInfo).isEqualTo(FAKE_SUBSCRIPTION_INFO1.toSubscriptionInfo());
+    }
+
+    @Test
+    public void testUpdateEmbeddedSubscriptions() {
+        EuiccProfileInfo profileInfo1 = new EuiccProfileInfo.Builder(FAKE_ICCID1)
+                .setIccid(FAKE_ICCID1)
+                .setNickname(FAKE_CARRIER_NAME1)
+                .setProfileClass(SubscriptionManager.PROFILE_CLASS_OPERATIONAL)
+                .setCarrierIdentifier(new CarrierIdentifier(FAKE_MCC1, FAKE_MNC1, null, null, null,
+                        null, FAKE_CARRIER_ID1, FAKE_CARRIER_ID1))
+                .setUiccAccessRule(Arrays.asList(UiccAccessRule.decodeRules(
+                        FAKE_NATIVE_ACCESS_RULES1)))
+                .build();
+        EuiccProfileInfo profileInfo2 = new EuiccProfileInfo.Builder(FAKE_ICCID2)
+                .setIccid(FAKE_ICCID2)
+                .setNickname(FAKE_CARRIER_NAME2)
+                .setProfileClass(SubscriptionManager.PROFILE_CLASS_OPERATIONAL)
+                .setCarrierIdentifier(new CarrierIdentifier(FAKE_MCC2, FAKE_MNC2, null, null, null,
+                        null, FAKE_CARRIER_ID2, FAKE_CARRIER_ID2))
+                .setUiccAccessRule(Arrays.asList(UiccAccessRule.decodeRules(
+                        FAKE_NATIVE_ACCESS_RULES2)))
+                .build();
+
+        GetEuiccProfileInfoListResult result = new GetEuiccProfileInfoListResult(
+                EuiccService.RESULT_OK, new EuiccProfileInfo[]{profileInfo1}, false);
+        doReturn(result).when(mEuiccController).blockingGetEuiccProfileInfoList(eq(1));
+        result = new GetEuiccProfileInfoListResult(EuiccService.RESULT_OK,
+                new EuiccProfileInfo[]{profileInfo2}, false);
+        doReturn(result).when(mEuiccController).blockingGetEuiccProfileInfoList(eq(2));
+        doReturn(FAKE_ICCID1).when(mUiccController).convertToCardString(eq(1));
+        doReturn(FAKE_ICCID2).when(mUiccController).convertToCardString(eq(2));
+
+        mSubscriptionManagerServiceUT.updateEmbeddedSubscriptions(List.of(1, 2), null);
+        processAllMessages();
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo.getSubscriptionId()).isEqualTo(1);
+        assertThat(subInfo.getSimSlotIndex()).isEqualTo(SubscriptionManager.INVALID_SIM_SLOT_INDEX);
+        assertThat(subInfo.getIccId()).isEqualTo(FAKE_ICCID1);
+        assertThat(subInfo.getDisplayName()).isEqualTo(FAKE_CARRIER_NAME1);
+        assertThat(subInfo.getDisplayNameSource()).isEqualTo(
+                SubscriptionManager.NAME_SOURCE_CARRIER);
+        assertThat(subInfo.getMcc()).isEqualTo(FAKE_MCC1);
+        assertThat(subInfo.getMnc()).isEqualTo(FAKE_MNC1);
+        assertThat(subInfo.getProfileClass()).isEqualTo(
+                SubscriptionManager.PROFILE_CLASS_OPERATIONAL);
+        assertThat(subInfo.isEmbedded()).isTrue();
+        assertThat(subInfo.isRemovableEmbedded()).isFalse();
+        assertThat(subInfo.getNativeAccessRules()).isEqualTo(FAKE_NATIVE_ACCESS_RULES1);
+
+        subInfo = mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2);
+        assertThat(subInfo.getSubscriptionId()).isEqualTo(2);
+        assertThat(subInfo.getSimSlotIndex()).isEqualTo(SubscriptionManager.INVALID_SIM_SLOT_INDEX);
+        assertThat(subInfo.getIccId()).isEqualTo(FAKE_ICCID2);
+        assertThat(subInfo.getDisplayName()).isEqualTo(FAKE_CARRIER_NAME2);
+        assertThat(subInfo.getDisplayNameSource()).isEqualTo(
+                SubscriptionManager.NAME_SOURCE_CARRIER);
+        assertThat(subInfo.getMcc()).isEqualTo(FAKE_MCC2);
+        assertThat(subInfo.getMnc()).isEqualTo(FAKE_MNC2);
+        assertThat(subInfo.getProfileClass()).isEqualTo(
+                SubscriptionManager.PROFILE_CLASS_OPERATIONAL);
+        assertThat(subInfo.isEmbedded()).isTrue();
+        assertThat(subInfo.isRemovableEmbedded()).isFalse();
+        assertThat(subInfo.getNativeAccessRules()).isEqualTo(FAKE_NATIVE_ACCESS_RULES2);
+    }
+
+    @Test
+    public void testGetActiveSubscriptionInfo() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without READ_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getActiveSubscriptionInfo(1, CALLING_PACKAGE, CALLING_FEATURE));
+
+        // Grant READ_PHONE_STATE permission for insertion.
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE);
+        SubscriptionInfo subInfo = mSubscriptionManagerServiceUT
+                .getActiveSubscriptionInfoForSimSlotIndex(0, CALLING_PACKAGE,
+                        CALLING_FEATURE);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getSubscriptionId()).isEqualTo(1);
+        assertThat(subInfo.getIccId()).isEmpty();
+        assertThat(subInfo.getNumber()).isEmpty();
+
+        // Grant carrier privilege for sub 1
+        setCarrierPrivilegesForSubId(true, 1);
+        subInfo = mSubscriptionManagerServiceUT.getActiveSubscriptionInfoForSimSlotIndex(
+                0, CALLING_PACKAGE, CALLING_FEATURE);
+        assertThat(subInfo).isEqualTo(FAKE_SUBSCRIPTION_INFO1.toSubscriptionInfo());
+    }
+
+    @Test
+    public void testSetDisplayNameUsingSrc() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setDisplayNameUsingSrc(FAKE_CARRIER_NAME2, 1,
+                        SubscriptionManager.NAME_SOURCE_CARRIER_ID));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // Carrier ID name source should have lower priority. Should not be able to update the
+        // display name.
+        assertThat(mSubscriptionManagerServiceUT.setDisplayNameUsingSrc(FAKE_CARRIER_NAME2,
+                1, SubscriptionManager.NAME_SOURCE_CARRIER_ID)).isEqualTo(0);
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfo(1).getDisplayName())
+                .isEqualTo(FAKE_CARRIER_NAME1);
+
+        // User input display name should have highest priority.
+        assertThat(mSubscriptionManagerServiceUT.setDisplayNameUsingSrc(FAKE_CARRIER_NAME2,
+                1, SubscriptionManager.NAME_SOURCE_USER_INPUT)).isEqualTo(1);
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfo(1).getDisplayName())
+                .isEqualTo(FAKE_CARRIER_NAME2);
+    }
+
+    @Test
+    public void testGetActiveSubInfoCount() {
+        doReturn(new int[]{1, 2}).when(mSubscriptionManager).getCompleteActiveSubscriptionIdList();
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(FAKE_SUBSCRIPTION_INFO2);
+
+        // Should fail without READ_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getActiveSubInfoCount(CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getActiveSubInfoCount(
+                CALLING_PACKAGE, CALLING_FEATURE)).isEqualTo(2);
+    }
+
+    @Test
+    public void testSetIconTint() throws Exception {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setIconTint(1, 12345));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        mSubscriptionManagerServiceUT.setIconTint(1, 12345);
+        processAllMessages();
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getIconTint()).isEqualTo(12345);
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+    }
+
+    @Test
+    public void testGetActiveSubscriptionInfoForIccId() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without READ_PRIVILEGED_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getActiveSubscriptionInfoForIccId(FAKE_ICCID1, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        SubscriptionInfo subInfo = mSubscriptionManagerServiceUT.getActiveSubscriptionInfoForIccId(
+                FAKE_ICCID1, CALLING_PACKAGE, CALLING_FEATURE);
+        assertThat(subInfo).isEqualTo(FAKE_SUBSCRIPTION_INFO1.toSubscriptionInfo());
+    }
+
+    @Test
+    public void testGetAccessibleSubscriptionInfoList() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        doReturn(false).when(mEuiccManager).isEnabled();
+        assertThat(mSubscriptionManagerServiceUT.getAccessibleSubscriptionInfoList(
+                CALLING_PACKAGE)).isNull();
+
+        doReturn(true).when(mEuiccManager).isEnabled();
+        assertThat(mSubscriptionManagerServiceUT.getAccessibleSubscriptionInfoList(
+                CALLING_PACKAGE)).isEmpty();
+
+        doReturn(true).when(mSubscriptionManager).canManageSubscription(
+                eq(FAKE_SUBSCRIPTION_INFO1.toSubscriptionInfo()), eq(CALLING_PACKAGE));
+
+        assertThat(mSubscriptionManagerServiceUT.getAccessibleSubscriptionInfoList(
+                CALLING_PACKAGE)).isEqualTo(List.of(FAKE_SUBSCRIPTION_INFO1.toSubscriptionInfo()));
+    }
+
+    @Test
+    public void testIsSubscriptionEnabled() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without READ_PRIVILEGED_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .isSubscriptionEnabled(1));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.isSubscriptionEnabled(1)).isTrue();
+        assertThat(mSubscriptionManagerServiceUT.isSubscriptionEnabled(2)).isFalse();
+    }
+
+    @Test
+    public void testGetEnabledSubscriptionId() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without READ_PRIVILEGED_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getEnabledSubscriptionId(0));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThrows(IllegalArgumentException.class, () -> mSubscriptionManagerServiceUT
+                .getEnabledSubscriptionId(SubscriptionManager.INVALID_SIM_SLOT_INDEX));
+
+        doReturn(2).when(mTelephonyManager).getActiveModemCount();
+        assertThat(mSubscriptionManagerServiceUT.getEnabledSubscriptionId(0)).isEqualTo(1);
+        assertThat(mSubscriptionManagerServiceUT.getEnabledSubscriptionId(1)).isEqualTo(
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        assertThrows(IllegalArgumentException.class, () -> mSubscriptionManagerServiceUT
+                .getEnabledSubscriptionId(2));
+    }
+
+    @Test
+    public void testGetActiveDataSubscriptionId() {
+        doReturn(12345).when(mPhoneSwitcher).getActiveDataSubId();
+        assertThat(mSubscriptionManagerServiceUT.getActiveDataSubscriptionId()).isEqualTo(12345);
+    }
+
+    @Test
+    public void testSetGetSubscriptionUserHandle() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        enableGetSubscriptionUserHandle();
+
+        // Should fail without MANAGE_SUBSCRIPTION_USER_ASSOCIATION
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionUserHandle(FAKE_USER_HANDLE, 1));
+
+        mContextFixture.addCallingOrSelfPermission(
+                Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION);
+        mSubscriptionManagerServiceUT.setSubscriptionUserHandle(FAKE_USER_HANDLE, 1);
+
+        processAllMessages();
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getUserId()).isEqualTo(FAKE_USER_HANDLE.getIdentifier());
+
+        mContextFixture.removeCallingOrSelfPermission(
+                Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION);
+        // Should fail without MANAGE_SUBSCRIPTION_USER_ASSOCIATION
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getSubscriptionUserHandle(1));
+
+        mContextFixture.addCallingOrSelfPermission(
+                Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION);
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionUserHandle(1))
+                .isEqualTo(FAKE_USER_HANDLE);
+    }
+
+    @Test
+    public void testIsSubscriptionAssociatedWithUser() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        enableGetSubscriptionUserHandle();
+
+        // Should fail without MANAGE_SUBSCRIPTION_USER_ASSOCIATION
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .isSubscriptionAssociatedWithUser(1, FAKE_USER_HANDLE));
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getSubscriptionInfoListAssociatedWithUser(FAKE_USER_HANDLE));
+
+        mContextFixture.addCallingOrSelfPermission(
+                Manifest.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION);
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        mSubscriptionManagerServiceUT.setSubscriptionUserHandle(FAKE_USER_HANDLE, 1);
+        processAllMessages();
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+
+        List<SubscriptionInfo> associatedSubInfoList = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoListAssociatedWithUser(FAKE_USER_HANDLE);
+        assertThat(associatedSubInfoList.size()).isEqualTo(1);
+        assertThat(associatedSubInfoList.get(0).getSubscriptionId()).isEqualTo(1);
+
+        assertThat(mSubscriptionManagerServiceUT.isSubscriptionAssociatedWithUser(1,
+                FAKE_USER_HANDLE)).isEqualTo(true);
+    }
+
+    @Test
+    public void testSetUsageSetting() {
+        doReturn(new int[]{1}).when(mSubscriptionManager).getCompleteActiveSubscriptionIdList();
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setUsageSetting(SubscriptionManager.USAGE_SETTING_VOICE_CENTRIC, 1,
+                        CALLING_PACKAGE));
+
+        // Grant carrier privilege
+        setCarrierPrivilegesForSubId(true, 1);
+        mSubscriptionManagerServiceUT.setUsageSetting(
+                SubscriptionManager.USAGE_SETTING_VOICE_CENTRIC, 1, CALLING_PACKAGE);
+
+        processAllMessages();
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getUsageSetting()).isEqualTo(
+                SubscriptionManager.USAGE_SETTING_VOICE_CENTRIC);
+    }
+
+    @Test
+    public void testSetDisplayNumber() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setDisplayNumber(FAKE_PHONE_NUMBER2, 1));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        mSubscriptionManagerServiceUT.setDisplayNumber(FAKE_PHONE_NUMBER2, 1);
+        processAllMessages();
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getDisplayName()).isEqualTo(FAKE_PHONE_NUMBER2);
+    }
+
+    @Test
+    public void testSetOpportunistic() {
+        doReturn(new int[]{1}).when(mSubscriptionManager).getCompleteActiveSubscriptionIdList();
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setOpportunistic(true, 1, CALLING_PACKAGE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        mSubscriptionManagerServiceUT.setOpportunistic(true, 1, CALLING_PACKAGE);
+        processAllMessages();
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.isOpportunistic()).isTrue();
+    }
+
+    @Test
+    public void testGetOpportunisticSubscriptions() {
+        testSetOpportunistic();
+        insertSubscription(FAKE_SUBSCRIPTION_INFO2);
+
+        // Should fail without READ_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getOpportunisticSubscriptions(CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE);
+
+        setIdentifierAccess(true);
+        setPhoneNumberAccess(PackageManager.PERMISSION_GRANTED);
+
+        List<SubscriptionInfo> subInfos = mSubscriptionManagerServiceUT
+                .getOpportunisticSubscriptions(CALLING_PACKAGE, CALLING_FEATURE);
+        assertThat(subInfos).hasSize(2);
+        assertThat(subInfos.get(0)).isEqualTo(new SubscriptionInfoInternal
+                .Builder(FAKE_SUBSCRIPTION_INFO1).setOpportunistic(1).build().toSubscriptionInfo());
+        assertThat(subInfos.get(1)).isEqualTo(FAKE_SUBSCRIPTION_INFO2.toSubscriptionInfo());
+    }
+
+    @Test
+    public void testSetPreferredDataSubscriptionId() {
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setPreferredDataSubscriptionId(1, false, null));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        mSubscriptionManagerServiceUT.setPreferredDataSubscriptionId(1, false, null);
+        verify(mPhoneSwitcher).trySetOpportunisticDataSubscription(eq(1), eq(false), eq(null));
+    }
+
+    @Test
+    public void testGetPreferredDataSubscriptionId() {
+        // Should fail without READ_PRIVILEGED_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getPreferredDataSubscriptionId());
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        doReturn(12345).when(mPhoneSwitcher).getAutoSelectedDataSubId();
+        assertThat(mSubscriptionManagerServiceUT.getPreferredDataSubscriptionId()).isEqualTo(12345);
+    }
+
+    @Test
+    public void testAddSubscriptionsIntoGroup() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(FAKE_SUBSCRIPTION_INFO2);
+
+        ParcelUuid newUuid = ParcelUuid.fromString("6adbc864-691c-45dc-b698-8fc9a2176fae");
+        String newOwner = "new owner";
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .addSubscriptionsIntoGroup(new int[]{1, 2}, newUuid, CALLING_PACKAGE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        mSubscriptionManagerServiceUT.addSubscriptionsIntoGroup(
+                new int[]{1, 2}, newUuid, newOwner);
+
+        SubscriptionInfo subInfo = mSubscriptionManagerServiceUT.getSubscriptionInfo(1);
+        assertThat(subInfo.getGroupUuid()).isEqualTo(newUuid);
+        assertThat(subInfo.getGroupOwner()).isEqualTo(newOwner);
+
+        subInfo = mSubscriptionManagerServiceUT.getSubscriptionInfo(2);
+        assertThat(subInfo.getGroupUuid()).isEqualTo(newUuid);
+        assertThat(subInfo.getGroupOwner()).isEqualTo(newOwner);
+    }
+
+    @Test
+    public void testSetDeviceToDeviceStatusSharing() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setDeviceToDeviceStatusSharing(SubscriptionManager.D2D_SHARING_SELECTED_CONTACTS,
+                        1));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        mSubscriptionManagerServiceUT.setDeviceToDeviceStatusSharing(
+                SubscriptionManager.D2D_SHARING_SELECTED_CONTACTS, 1);
+        processAllMessages();
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getDeviceToDeviceStatusSharingPreference()).isEqualTo(
+                SubscriptionManager.D2D_SHARING_SELECTED_CONTACTS);
+    }
+
+    @Test
+    public void testSetDeviceToDeviceStatusSharingContacts() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setDeviceToDeviceStatusSharingContacts(FAKE_CONTACT2, 1));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        mSubscriptionManagerServiceUT.setDeviceToDeviceStatusSharingContacts(FAKE_CONTACT2, 1);
+        processAllMessages();
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.getDeviceToDeviceStatusSharingContacts()).isEqualTo(FAKE_CONTACT2);
+    }
+
+    @Test
+    public void testGetPhoneNumberFromFirstAvailableSource() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without phone number access
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .getPhoneNumberFromFirstAvailableSource(1, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PHONE_NUMBERS);
+
+        assertThat(mSubscriptionManagerServiceUT.getPhoneNumberFromFirstAvailableSource(
+                1, CALLING_PACKAGE, CALLING_FEATURE)).isEqualTo(FAKE_PHONE_NUMBER1);
+    }
+
+    @Test
+    public void testSetUiccApplicationsEnabled() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        // Should fail without MODIFY_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setUiccApplicationsEnabled(false, 1));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        mSubscriptionManagerServiceUT.setUiccApplicationsEnabled(false, 1);
+        processAllMessages();
+        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
+
+        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1);
+        assertThat(subInfo).isNotNull();
+        assertThat(subInfo.areUiccApplicationsEnabled()).isFalse();
+    }
+
+    @Test
+    public void testCanDisablePhysicalSubscription() {
+        // Should fail without READ_PRIVILEGED_PHONE_STATE
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .canDisablePhysicalSubscription());
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        doReturn(false).when(mPhone).canDisablePhysicalSubscription();
+        assertThat(mSubscriptionManagerServiceUT.canDisablePhysicalSubscription()).isFalse();
+
+        doReturn(true).when(mPhone).canDisablePhysicalSubscription();
+        assertThat(mSubscriptionManagerServiceUT.canDisablePhysicalSubscription()).isTrue();
+    }
+
+    @Test
+    public void testSetGetEnhanced4GModeEnabled() throws Exception {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("1");
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_ENHANCED_4G_MODE_ENABLED
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_ENHANCED_4G_MODE_ENABLED, "0");
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getEnhanced4GModeEnabled()).isEqualTo(0);
+    }
+
+    @Test
+    public void testSetGetVideoTelephonyEnabled() throws Exception {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_VT_IMS_ENABLED, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_VT_IMS_ENABLED, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("1");
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_VT_IMS_ENABLED, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_VT_IMS_ENABLED
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_VT_IMS_ENABLED, "0");
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getVideoTelephonyEnabled()).isEqualTo(0);
+    }
+
+    @Test
+    public void testSetGetWifiCallingEnabled() throws Exception {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_WFC_IMS_ENABLED, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_WFC_IMS_ENABLED, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("1");
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_WFC_IMS_ENABLED, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_WFC_IMS_ENABLED
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_WFC_IMS_ENABLED, "0");
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getWifiCallingEnabled()).isEqualTo(0);
+    }
+
+    @Test
+    public void testSetGetWifiCallingMode() throws Exception {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_WFC_IMS_MODE, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_WFC_IMS_MODE, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("1");
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_WFC_IMS_MODE, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_WFC_IMS_MODE
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_WFC_IMS_MODE, "0");
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getWifiCallingMode()).isEqualTo(0);
+    }
+
+    @Test
+    public void testSetGetWifiCallingModeForRoaming() throws Exception {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_WFC_IMS_ROAMING_MODE, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_WFC_IMS_ROAMING_MODE, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("2");
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_WFC_IMS_ROAMING_MODE, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_WFC_IMS_ROAMING_MODE
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_WFC_IMS_ROAMING_MODE, "0");
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getWifiCallingModeForRoaming()).isEqualTo(0);
+    }
+
+    @Test
+    public void testSetGetEnabledMobileDataPolicies() throws Exception {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES, CALLING_PACKAGE,
+                        CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo(FAKE_MOBILE_DATA_POLICY1);
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_ENABLED_MOBILE_DATA_POLICIES
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_ENABLED_MOBILE_DATA_POLICIES, FAKE_MOBILE_DATA_POLICY2);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getEnabledMobileDataPolicies()).isEqualTo(FAKE_MOBILE_DATA_POLICY2);
+    }
+
+    @Test
+    public void testSetGetRcsUceEnabled() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_IMS_RCS_UCE_ENABLED, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_IMS_RCS_UCE_ENABLED, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("1");
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_IMS_RCS_UCE_ENABLED, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_IMS_RCS_UCE_ENABLED
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_IMS_RCS_UCE_ENABLED, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("1");
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_IMS_RCS_UCE_ENABLED, "0");
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getRcsUceEnabled()).isEqualTo(0);
+    }
+
+    @Test
+    public void testSetGetCrossSimCallingEnabled() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED, CALLING_PACKAGE,
+                        CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("1");
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_CROSS_SIM_CALLING_ENABLED
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_CROSS_SIM_CALLING_ENABLED, "0");
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getCrossSimCallingEnabled()).isEqualTo(0);
+    }
+
+    @Test
+    public void testSetGetRcsConfig() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_RCS_CONFIG, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_RCS_CONFIG, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo(Base64.encodeToString(FAKE_RCS_CONFIG1, Base64.DEFAULT));
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_RCS_CONFIG, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_RCS_CONFIG
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_RCS_CONFIG,
+                Base64.encodeToString(FAKE_RCS_CONFIG2, Base64.DEFAULT));
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getRcsConfig()).isEqualTo(FAKE_RCS_CONFIG2);
+    }
+
+    @Test
+    public void testSetGetDeviceToDeviceStatusSharingPreference() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_D2D_STATUS_SHARING, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_D2D_STATUS_SHARING, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("1");
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_D2D_STATUS_SHARING, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_D2D_STATUS_SHARING
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_D2D_STATUS_SHARING, "0");
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getDeviceToDeviceStatusSharingPreference()).isEqualTo(0);
+    }
+
+    @Test
+    public void testSetGetVoImsOptInEnabled() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_VOIMS_OPT_IN_STATUS, CALLING_PACKAGE, CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_VOIMS_OPT_IN_STATUS, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("1");
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_VOIMS_OPT_IN_STATUS, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_VOIMS_OPT_IN_STATUS
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_VOIMS_OPT_IN_STATUS, "0");
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getVoImsOptInEnabled()).isEqualTo(0);
+    }
+
+    @Test
+    public void testSetGetDeviceToDeviceStatusSharingContacts() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS, CALLING_PACKAGE,
+                        CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS, CALLING_PACKAGE,
+                CALLING_FEATURE)).isEqualTo(FAKE_CONTACT1);
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS,
+                        FAKE_CONTACT2));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_D2D_STATUS_SHARING_SELECTED_CONTACTS, FAKE_CONTACT2);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getDeviceToDeviceStatusSharingContacts()).isEqualTo(FAKE_CONTACT2);
+    }
+
+    @Test
+    public void testSetGetNrAdvancedCallingEnabled() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        assertThrows(SecurityException.class, () ->
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                        SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED, CALLING_PACKAGE,
+                        CALLING_FEATURE));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionProperty(1,
+                SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED, CALLING_PACKAGE, CALLING_FEATURE))
+                .isEqualTo("1");
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED, "0"));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        // COLUMN_NR_ADVANCED_CALLING_ENABLED
+        mSubscriptionManagerServiceUT.setSubscriptionProperty(1,
+                SimInfo.COLUMN_NR_ADVANCED_CALLING_ENABLED, "0");
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                .getNrAdvancedCallingEnabled()).isEqualTo(0);
+    }
+
+    @Test
+    public void testSetSubscriptionPropertyInvalidField() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+
+        assertThrows(IllegalArgumentException.class, () -> mSubscriptionManagerServiceUT
+                .setSubscriptionProperty(1, "hahahaha", "0"));
+    }
+
+    @Test
+    public void testGetNonAccessibleFields() throws Exception {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+
+        Field field = SubscriptionManagerService.class.getDeclaredField(
+                "DIRECT_ACCESS_SUBSCRIPTION_COLUMNS");
+        field.setAccessible(true);
+        Set<String> accessibleColumns = (Set<String>) field.get(null);
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+
+        for (String column : SimInfo.getAllColumns()) {
+            if (accessibleColumns.contains(column)) {
+                mSubscriptionManagerServiceUT.getSubscriptionProperty(1, column,
+                        CALLING_PACKAGE, CALLING_FEATURE);
+            } else {
+                assertThrows(SecurityException.class, () ->
+                        mSubscriptionManagerServiceUT.getSubscriptionProperty(1, column,
+                                CALLING_PACKAGE, CALLING_FEATURE));
+            }
+        }
+    }
+
+    @Test
+    public void testSyncToGroup() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(FAKE_SUBSCRIPTION_INFO2);
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        mSubscriptionManagerServiceUT.createSubscriptionGroup(new int[]{1, 2}, CALLING_PACKAGE);
+
+        mSubscriptionManagerServiceUT.syncGroupedSetting(1);
+        processAllMessages();
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2).getIconTint())
+                .isEqualTo(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                        .getIconTint());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2).getDataRoaming())
+                .isEqualTo(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(1)
+                        .getDataRoaming());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getEnhanced4GModeEnabled()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getEnhanced4GModeEnabled());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getVideoTelephonyEnabled()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getVideoTelephonyEnabled());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getWifiCallingEnabled()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getWifiCallingEnabled());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getWifiCallingMode()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getWifiCallingMode());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getWifiCallingModeForRoaming()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getWifiCallingModeForRoaming());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getWifiCallingEnabledForRoaming()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getWifiCallingEnabledForRoaming());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getEnabledMobileDataPolicies()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getEnabledMobileDataPolicies());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getUiccApplicationsEnabled()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getUiccApplicationsEnabled());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getRcsUceEnabled()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getRcsUceEnabled());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getCrossSimCallingEnabled()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getCrossSimCallingEnabled());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getRcsConfig()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getRcsConfig());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getDeviceToDeviceStatusSharingPreference()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getDeviceToDeviceStatusSharingPreference());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getVoImsOptInEnabled()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getVoImsOptInEnabled());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getDeviceToDeviceStatusSharingContacts()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getDeviceToDeviceStatusSharingContacts());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getNrAdvancedCallingEnabled()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getNrAdvancedCallingEnabled());
+
+        assertThat(mSubscriptionManagerServiceUT.getSubscriptionInfoInternal(2)
+                .getUserId()).isEqualTo(mSubscriptionManagerServiceUT
+                .getSubscriptionInfoInternal(1).getUserId());
+    }
+
+    @Test
+    public void testRemoveSubInfo() {
+        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
+        insertSubscription(FAKE_SUBSCRIPTION_INFO2);
+
+        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
+                .removeSubInfo(FAKE_ICCID1, SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM));
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.removeSubInfo(FAKE_ICCID1,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM)).isEqualTo(0);
+        assertThat(mSubscriptionManagerServiceUT.removeSubInfo(FAKE_ICCID2,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM)).isEqualTo(0);
+
+        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
+        assertThat(mSubscriptionManagerServiceUT.getAllSubInfoList(
+                CALLING_PACKAGE, CALLING_FEATURE).isEmpty()).isTrue();
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/AdnRecordCacheTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/AdnRecordCacheTest.java
new file mode 100644
index 0000000..c040b9e
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/AdnRecordCacheTest.java
@@ -0,0 +1,340 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.uicc;
+
+import static com.android.internal.telephony.uicc.IccConstants.EF_ADN;
+import static com.android.internal.telephony.uicc.IccConstants.EF_MBDN;
+import static com.android.internal.telephony.uicc.IccConstants.EF_PBR;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.os.Message;
+import android.os.test.TestLooper;
+
+import com.android.internal.telephony.CommandException;
+import com.android.internal.telephony.TelephonyTest;
+import com.android.internal.telephony.gsm.UsimPhoneBookManager;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+
+public class AdnRecordCacheTest extends TelephonyTest {
+
+    private AdnRecordCacheUT mAdnRecordCache;
+    private TestLooper mTestLooper;
+    private Handler mTestHandler;
+    private IccFileHandler mFhMock;
+    private UsimPhoneBookManager mUsimPhoneBookManager;
+
+    @SuppressWarnings("ClassCanBeStatic")
+    private class AdnRecordCacheUT extends AdnRecordCache {
+        AdnRecordCacheUT(IccFileHandler fh, UsimPhoneBookManager usimPhoneBookManager) {
+            super(fh, usimPhoneBookManager);
+        }
+
+        protected void setAdnLikeWriters(int key, ArrayList<Message> waiters) {
+            super.setAdnLikeWriters(key, waiters);
+        }
+
+        protected void setAdnLikeFiles(int key, ArrayList<AdnRecord> adnRecordList) {
+            super.setAdnLikeFiles(key, adnRecordList);
+        }
+
+        protected void setUserWriteResponse(int key, Message message) {
+            super.setUserWriteResponse(key, message);
+        }
+
+        protected UsimPhoneBookManager getUsimPhoneBookManager() {
+            return super.getUsimPhoneBookManager();
+        }
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(getClass().getSimpleName());
+        // Mocked classes
+        mFhMock = mock(IccFileHandler.class);
+        mUsimPhoneBookManager = mock(UsimPhoneBookManager.class);
+        mTestLooper = new TestLooper();
+        mTestHandler = new Handler(mTestLooper.getLooper());
+        mTestHandler.post(
+                () -> mAdnRecordCache = new AdnRecordCacheUT(mFhMock, mUsimPhoneBookManager));
+        mTestLooper.dispatchAll();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (mTestLooper != null) {
+            mTestLooper.dispatchAll();
+            mTestLooper = null;
+        }
+        mTestHandler.removeCallbacksAndMessages(null);
+        mTestHandler = null;
+        mAdnRecordCache = null;
+        super.tearDown();
+    }
+
+    @Test
+    public void resetTest() {
+        Message message1 = Message.obtain(mTestHandler);
+        Message message2 = Message.obtain(mTestHandler);
+
+        // test data to create mAdnLikeWaiters
+        ArrayList<Message> waiters = new ArrayList<>();
+        waiters.add(message1);
+        mAdnRecordCache.setAdnLikeWriters(EF_MBDN, waiters);
+
+        // test data to create mAdnLikeFiles
+        setAdnLikeFiles(EF_MBDN);
+
+        // test data to create mUserWriteResponse
+        mAdnRecordCache.setUserWriteResponse(EF_MBDN, message2);
+
+        mAdnRecordCache.reset();
+
+        mTestLooper.dispatchAll();
+        AsyncResult ar1 = (AsyncResult) message1.obj;
+        AsyncResult ar2 = (AsyncResult) message2.obj;
+        Assert.assertTrue(ar1.exception.toString().contains("AdnCache reset"));
+        Assert.assertTrue(ar2.exception.toString().contains("AdnCace reset"));
+    }
+
+    @Test
+    public void updateAdnByIndexEfException() {
+        int efId = 0x6FC5;
+        Message message = Message.obtain(mTestHandler);
+        mAdnRecordCache.updateAdnByIndex(efId, null, 0, null, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        Assert.assertNotNull(ar.exception);
+        assertTrue((ar.exception.toString().contains("EF is not known ADN-like EF:0x6FC5")));
+    }
+
+    @Test
+    public void updateAdnByIndex_WriteResponseException() {
+        int efId = EF_MBDN;
+        Message message = Message.obtain(mTestHandler);
+        Message message2 = Message.obtain(mTestHandler);
+        AdnRecord adnRecord = new AdnRecord("AlphaTag", "123456789");
+        // test data to create mUserWriteResponse
+        mAdnRecordCache.setUserWriteResponse(efId, message2);
+        mAdnRecordCache.updateAdnByIndex(efId, adnRecord, 0, null, message);
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        Assert.assertNotNull(ar.exception);
+        assertTrue((ar.exception.toString().contains("Have pending update for EF:0x6FC7")));
+    }
+
+    @Test
+    public void updateAdnByIndex() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, "success2", null);
+                    response.sendToTarget();
+                    return response;
+                })
+                .when(mFhMock)
+                .getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        Assert.assertNotNull(message);
+        AdnRecord adnRecord = new AdnRecord("AlphaTag", "123456789");
+        // test data to create mUserWriteResponse
+        mAdnRecordCache.updateAdnByIndex(EF_MBDN, adnRecord, 0, null, message);
+        mTestLooper.startAutoDispatch();
+        verify(mFhMock, times(1)).getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void updateAdnBySearch_EfException() {
+        int efId = 0x6FC5;
+        Message message = Message.obtain(mTestHandler);
+        mAdnRecordCache.updateAdnBySearch(efId, null, null, null, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        Assert.assertNotNull(ar.exception);
+        assertTrue((ar.exception.toString().contains("EF is not known ADN-like EF:0x6FC5")));
+    }
+
+    @Test
+    public void updateAdnBySearch_Exception() {
+        Message message = Message.obtain(mTestHandler);
+        mAdnRecordCache.updateAdnBySearch(EF_MBDN, null, null, null, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        Assert.assertNotNull(ar.exception);
+        assertTrue((ar.exception.toString().contains("Adn list not exist for EF:0x6FC7")));
+    }
+
+    @Test
+    public void updateAdnBySearch_AdnListError() {
+        int efId = EF_MBDN;
+        setAdnLikeFiles(efId);
+        Message message = Message.obtain(mTestHandler);
+        AdnRecord oldAdn = new AdnRecord("oldAlphaTag", "123456789");
+        mAdnRecordCache.updateAdnBySearch(efId, oldAdn, null, null, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        Assert.assertNotNull(ar.exception);
+        assertTrue((ar.exception.toString().contains(
+                "Adn record don't exist for ADN Record 'oldAlphaTag'")));
+    }
+
+    @Test
+    public void updateAdnBySearch_PendingUpdate() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, "success2", null);
+                    response.sendToTarget();
+                    return response;
+                })
+                .when(mFhMock)
+                .getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+
+        int efId = EF_MBDN;
+        setAdnLikeFiles(efId);
+        Message message = Message.obtain(mTestHandler);
+        AdnRecord oldAdn = new AdnRecord("AlphaTag", "123456789");
+        mAdnRecordCache.updateAdnBySearch(efId, oldAdn, null, null, message);
+        mTestLooper.dispatchAll();
+
+        verify(mFhMock, times(1)).getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void updateAdnBySearch() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, "success", null);
+                    response.sendToTarget();
+                    return response;
+                })
+                .when(mFhMock)
+                .getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+
+        int efId = EF_MBDN;
+        setAdnLikeFiles(efId);
+        Message message = Message.obtain(mTestHandler);
+        AdnRecord oldAdn = new AdnRecord("AlphaTag", "123456789");
+        mAdnRecordCache.updateAdnBySearch(efId, oldAdn, null, null, message);
+        mTestLooper.dispatchAll();
+
+        verify(mFhMock, times(1)).getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+    }
+
+
+    @Test
+    public void updateAdnBySearch_AdnException() {
+        doReturn(null).when(mUsimPhoneBookManager).loadEfFilesFromUsim();
+        Message message = Message.obtain(mTestHandler);
+        AdnRecord oldAdn = new AdnRecord("oldAlphaTag", "123456789");
+        mAdnRecordCache.updateAdnBySearch(EF_PBR, oldAdn, null, null, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        Assert.assertNotNull(ar.exception);
+        assertTrue((ar.exception.toString().contains("Adn list not exist for EF:0x4F30")));
+    }
+
+    @Test
+    public void requestLoadAllAdnLike_AlreadyLoadedEf() {
+        int efId = EF_MBDN;
+        setAdnLikeFiles(efId);
+        Message message = Message.obtain(mTestHandler);
+        mAdnRecordCache.requestLoadAllAdnLike(efId, 0, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        Assert.assertNull(ar.exception);
+        Assert.assertNotNull(ar.result);
+    }
+
+    @Test
+    public void requestLoadAllAdnLike_AlreadyLoadingEf() {
+        int efId = EF_MBDN;
+        // test data to create mAdnLikeWaiters
+        Message message = Message.obtain(mTestHandler);
+        ArrayList<Message> waiters = new ArrayList<>();
+        waiters.add(message);
+        mAdnRecordCache.setAdnLikeWriters(efId, waiters);
+        mAdnRecordCache.requestLoadAllAdnLike(efId, 0, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        Assert.assertNull(ar);
+    }
+
+    @Test
+    public void requestLoadAllAdnLike_NotKnownEf() {
+        Message message = Message.obtain(mTestHandler);
+        mAdnRecordCache.requestLoadAllAdnLike(EF_MBDN, -1, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        Assert.assertTrue(ar.exception.toString().contains("EF is not known ADN-like EF:0x"));
+    }
+
+    @Test
+    public void requestLoadAllAdnLike() {
+                doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, null, new CommandException(
+                            CommandException.Error.REQUEST_NOT_SUPPORTED));
+                    response.sendToTarget();
+                    return response;
+                })
+                .when(mFhMock)
+                .loadEFLinearFixedAll(anyInt(), anyString(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mAdnRecordCache.requestLoadAllAdnLike(EF_ADN, 0x6FC8, message);
+        mTestLooper.dispatchAll();
+
+        verify(mFhMock, times(1)).loadEFLinearFixedAll(anyInt(), anyString(), any(Message.class));
+    }
+
+    private void setAdnLikeFiles(int ef) {
+        // test data to create mAdnLikeFiles
+        ArrayList<AdnRecord> adnRecordList = new ArrayList<>();
+        AdnRecord adnRecord = new AdnRecord("AlphaTag", "123456789");
+        adnRecordList.add(adnRecord);
+        mAdnRecordCache.setAdnLikeFiles(ef, adnRecordList);
+    }
+}
\ No newline at end of file
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/IccFileHandlerTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/IccFileHandlerTest.java
new file mode 100644
index 0000000..63e68e2
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/IccFileHandlerTest.java
@@ -0,0 +1,537 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.uicc;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.os.Message;
+import android.os.test.TestLooper;
+import android.util.Log;
+
+import com.android.internal.telephony.CommandException;
+import com.android.internal.telephony.CommandsInterface;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.concurrent.CountDownLatch;
+
+public class IccFileHandlerTest {
+    CommandsInterface mCi;
+    IccFileHandler mIccFileHandler;
+    private TestLooper mTestLooper;
+    private Handler mTestHandler;
+
+    @Before
+    public void setUp() throws Exception {
+        mCi = mock(CommandsInterface.class);
+        mTestLooper = new TestLooper();
+        mTestHandler = new Handler(mTestLooper.getLooper());
+        mTestHandler.post(
+                () -> mIccFileHandler = new IccFileHandler(mCi) {
+                    @Override
+                    protected String getEFPath(int efid) {
+                        switch (efid) {
+                            case 0x4f30:
+                            case 0x4f3a:
+                                return "3F007F105F3A";
+                        }
+                        return "";
+                    }
+
+                    @Override
+                    protected void logd(String s) {
+                        Log.d("IccFileHandlerTest", s);
+                    }
+
+                    @Override
+                    protected void loge(String s) {
+                        Log.d("IccFileHandlerTest", s);
+                    }
+                });
+
+
+        mTestLooper.dispatchAll();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (mTestLooper != null) {
+            mTestLooper.dispatchAll();
+            mTestLooper = null;
+        }
+        mTestHandler.removeCallbacksAndMessages(null);
+        mTestHandler = null;
+        mIccFileHandler = null;
+        mCi = null;
+    }
+
+    @Test
+    public void loadEFLinearFixed_WithNullPath() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFLinearFixed(0, "", 0, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void loadEFLinearFixed() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFLinearFixed(0, 0, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void loadEFImgLinearFixed() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFImgLinearFixed(0, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void getEFLinearRecordSize_WithNullPath() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.getEFLinearRecordSize(0, "", message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void getEFLinearRecordSize() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.getEFLinearRecordSize(0, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void getEFTransparentRecordSize() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.getEFTransparentRecordSize(0, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void loadEFLinearFixedAll_FileNotFoundAtGetRecord() throws InterruptedException {
+        int efId = 0x4f30;
+        final CountDownLatch latch = new CountDownLatch(1);
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    IccIoResult iir = new IccIoResult(0x94, 0x00,
+                            IccUtils.hexStringToBytes(null));
+                    AsyncResult.forMessage(response, iir, null);
+                    mTestHandler.postDelayed(latch::countDown, 100);
+                    response.sendToTarget();
+                    return null;
+                }).when(mCi).iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(),
+                anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFLinearFixedAll(efId, null, message);
+        mTestLooper.startAutoDispatch();
+        latch.await(5, java.util.concurrent.TimeUnit.SECONDS);
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertNotNull(ar);
+        assertTrue(ar.exception instanceof IccFileNotFound);
+    }
+
+    @Test
+    public void loadEFLinearFixedAll_FileNotFoundAtReadRecord() throws InterruptedException {
+        int efid = 0x4f30;
+        final CountDownLatch latch = new CountDownLatch(2);
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    IccIoResult iir = null;
+                    if (response.what == 6) {
+                        iir = new IccIoResult(0x90, 0x00, IccUtils.hexStringToBytes(
+                                "000000454F30040000FFFF01020145"));
+                        latch.countDown();
+                    } else if (response.what == 7) {
+                        iir = new IccIoResult(0x94, 0x00, IccUtils.hexStringToBytes(null));
+                        mTestHandler.postDelayed(latch::countDown, 100);
+                    }
+                    AsyncResult.forMessage(response, iir, null);
+                    response.sendToTarget();
+                    return null;
+                }).when(mCi).iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(),
+                anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFLinearFixedAll(efid, null, message);
+        mTestLooper.startAutoDispatch();
+        latch.await(5, java.util.concurrent.TimeUnit.SECONDS);
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertNotNull(ar);
+        assertTrue(ar.exception instanceof IccFileNotFound);
+    }
+
+    @Test
+    public void loadEFLinearFixedAll_ExceptionAtGetRecord() throws InterruptedException {
+        int efid = 0x4f30;
+        final CountDownLatch latch = new CountDownLatch(1);
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, null, new CommandException(
+                            CommandException.Error.OPERATION_NOT_ALLOWED));
+                    response.sendToTarget();
+                    mTestHandler.postDelayed(latch::countDown, 100);
+                    return null;
+                }).when(mCi).iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(),
+                anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFLinearFixedAll(efid, null, message);
+        mTestLooper.startAutoDispatch();
+        latch.await(5, java.util.concurrent.TimeUnit.SECONDS);
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertSame(CommandException.Error.OPERATION_NOT_ALLOWED,
+                ((CommandException) ar.exception).getCommandError());
+        assertNull(ar.result);
+    }
+
+    @Test
+    public void loadEFLinearFixedAll_ExceptionAtReadRecord() throws InterruptedException {
+        int efid = 0x4f30;
+        final CountDownLatch latch = new CountDownLatch(2);
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    String hexString = null;
+                    IccIoResult iir = null;
+                    if (response.what == 6) {
+                        hexString = "000000454F30040000FFFF01020145";
+                        iir = new IccIoResult(0x90, 0x00, IccUtils.hexStringToBytes(hexString));
+                        AsyncResult.forMessage(response, iir, null);
+                        latch.countDown();
+                    } else if (response.what == 7) {
+                        AsyncResult.forMessage(response, null, new CommandException(
+                                CommandException.Error.OPERATION_NOT_ALLOWED));
+                        mTestHandler.postDelayed(latch::countDown, 100);
+                    }
+                    response.sendToTarget();
+                    mTestHandler.postDelayed(latch::countDown, 100);
+                    return null;
+                }).when(mCi).iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(),
+                anyInt(),
+                isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFLinearFixedAll(efid, null, message);
+        mTestLooper.startAutoDispatch();
+        latch.await(5, java.util.concurrent.TimeUnit.SECONDS);
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertSame(CommandException.Error.OPERATION_NOT_ALLOWED,
+                ((CommandException) ar.exception).getCommandError());
+        assertNull(ar.result);
+    }
+
+    @Test
+    public void loadEFLinearFixedAll_FullLoop() throws InterruptedException {
+        int efid = 0x4f30;
+        final CountDownLatch latch = new CountDownLatch(2);
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    String hexString = null;
+                    if (response.what == 6) {
+                        hexString = "000000454F30040000FFFF01020145";
+                        latch.countDown();
+                    } else if (response.what == 7) {
+                        try {
+                            IccFileHandler.LoadLinearFixedContext lc =
+                                    (IccFileHandler.LoadLinearFixedContext) response.obj;
+                            if (mIccFileHandler.getEfid(lc) == efid) {
+                                hexString =
+                                        "A814C0034F3A01C1034F3202C5034F0904C9034F2109A90FC3034F611"
+                                                + "5C4034F1108CA034F5017AA0FC2034F4A03C7034F4B06CB"
+                                                + "034F4F16FFFFFFFFFFFFFFFFFFFFFFFFFF";
+                            }
+                            mTestHandler.postDelayed(latch::countDown, 100);
+
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                            Log.e("UsimFH", e.getMessage());
+                        }
+
+                    }
+                    IccIoResult iir = new IccIoResult(0x90, 0x00,
+                            IccUtils.hexStringToBytes(hexString));
+                    AsyncResult.forMessage(response, iir, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFLinearFixedAll(efid, null, message);
+        mTestLooper.startAutoDispatch();
+        latch.await(5, java.util.concurrent.TimeUnit.SECONDS);
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertNotNull(ar);
+        ArrayList<byte[]> results = (ArrayList<byte[]>) ar.result;
+        assertEquals(
+                "A814C0034F3A01C1034F3202C5034F0904C9034F2109A90FC3034F6115C4034F1108CA03"
+                        + "4F5017AA0FC2034F4A03C7034F4B06CB034F4F16FFFFFFFFFFFFFFFFFFFFFFFFFF",
+                IccUtils.bytesToHexString(results.get(0)));
+        verify(mCi, times(2)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void loadEFLinearFixedAll_WithNullPath() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFLinearFixedAll(0, "", message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void loadEFLinearFixedAll() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFLinearFixedAll(0, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void loadEFTransparent() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFTransparent(0, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void loadEFTransparent_WithZeroSize() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFTransparent(0, 0, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void loadEFImgTransparent() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        isNull(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.loadEFImgTransparent(0, 0, 0, 0, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), isNull(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void updateEFLinearFixed_WithNullPath() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        anyString(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.updateEFLinearFixed(0, "", 0, new byte[10], null, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), anyString(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void updateEFLinearFixed() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        anyString(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.updateEFLinearFixed(0, 0, new byte[10], null, message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), anyString(), isNull(), isNull(), any(Message.class));
+    }
+
+    @Test
+    public void updateEFTransparent() {
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(9);
+                    AsyncResult.forMessage(response, "Success", null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mCi)
+                .iccIOForApp(anyInt(), anyInt(), anyString(), anyInt(), anyInt(), anyInt(),
+                        anyString(), isNull(), isNull(), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIccFileHandler.updateEFTransparent(0, new byte[10], message);
+        verify(mCi, times(1)).iccIOForApp(anyInt(), anyInt(), anyString(),
+                anyInt(), anyInt(), anyInt(), anyString(), isNull(), isNull(), any(Message.class));
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/IccIoResultTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/IccIoResultTest.java
new file mode 100644
index 0000000..b3e0a85
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/IccIoResultTest.java
@@ -0,0 +1,452 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.telephony.uicc;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class IccIoResultTest {
+
+    @Test
+    public void check0x90_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x90, 0x00, new byte[10]);
+        String resultStr = iccIoResult.toString();
+
+        Assert.assertTrue(resultStr != null && (!resultStr.contains("Error")));
+    }
+
+    @Test
+    public void check0x91_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x91, 0x00, new byte[10]);
+        String resultStr = iccIoResult.toString();
+
+        Assert.assertTrue(resultStr != null && (!resultStr.contains("Error")));
+    }
+
+    @Test
+    public void check0x9E_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x9E, 0x00, new byte[10]);
+        String resultStr = iccIoResult.toString();
+
+        Assert.assertTrue(resultStr != null && (!resultStr.contains("Error")));
+    }
+
+    @Test
+    public void check0x9F_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x9F, 0x00, new byte[10]);
+        String resultStr = iccIoResult.toString();
+
+        Assert.assertTrue(resultStr != null && (!resultStr.contains("Error")));
+    }
+
+    @Test
+    public void check0x94_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x94, 0x00, new byte[10]);
+        String resultStr = iccIoResult.toString();
+
+        Assert.assertTrue(resultStr != null && (resultStr.contains("no EF selected")));
+
+        iccIoResult = new IccIoResult(0x94, 0x02, new byte[10]);
+        resultStr = iccIoResult.toString();
+
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("out f range (invalid address)")));
+
+        iccIoResult = new IccIoResult(0x94, 0x04, new byte[10]);
+        resultStr = iccIoResult.toString();
+
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("file ID not found/pattern not found")));
+
+        iccIoResult = new IccIoResult(0x94, 0x08, new byte[10]);
+        resultStr = iccIoResult.toString();
+
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("file is inconsistent with the command")));
+    }
+
+    @Test
+    public void check0x98_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x98, 0x00, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("unknown")));
+
+        iccIoResult = new IccIoResult(0x98, 0x02, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("no CHV initialized")));
+
+        iccIoResult = new IccIoResult(0x98, 0x04, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("access condition not fulfilled")));
+
+        iccIoResult = new IccIoResult(0x98, 0x08, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("in contradiction with CHV status")));
+
+        iccIoResult = new IccIoResult(0x98, 0x10, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "in contradiction with invalidation status")));
+
+        iccIoResult = new IccIoResult(0x98, 0x40, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "unsuccessful CHV verification, no attempt left")));
+
+        iccIoResult = new IccIoResult(0x98, 0x50, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "increase cannot be performed, Max value reached")));
+
+        iccIoResult = new IccIoResult(0x98, 0x62, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "authentication error, application specific")));
+
+        iccIoResult = new IccIoResult(0x98, 0x64, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "authentication error, security context not supported")));
+
+        iccIoResult = new IccIoResult(0x98, 0x65, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("key freshness failure")));
+
+        iccIoResult = new IccIoResult(0x98, 0x66, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "authentication error, no memory space available")));
+
+        iccIoResult = new IccIoResult(0x98, 0x67, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "authentication error, no memory space available in EF_MUK")));
+    }
+
+    @Test
+    public void check0x61_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x61, 0x20, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("more response bytes available")));
+    }
+
+    @Test
+    public void check0x62_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x62, 0x00, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("no information given")));
+
+        iccIoResult = new IccIoResult(0x62, 0x81, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains(
+                        "part of returned data may be corrupted")));
+
+        iccIoResult = new IccIoResult(0x62, 0x82, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "end of file/record reached before reading Le bytes")));
+
+        iccIoResult = new IccIoResult(0x62, 0x83, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("selected file invalidated")));
+
+        iccIoResult = new IccIoResult(0x62, 0x84, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("selected file in termination state")));
+
+        iccIoResult = new IccIoResult(0x62, 0xF1, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("more data available")));
+
+        iccIoResult = new IccIoResult(0x62, 0xF2, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "more data available and proactive command pending")));
+
+        iccIoResult = new IccIoResult(0x62, 0xF3, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("response data available")));
+
+        iccIoResult = new IccIoResult(0x62, 0xF4, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("unknown")));
+    }
+
+    @Test
+    public void check0x63_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x63, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains(
+                        "command successful but after using an internal update retry routine 0 "
+                                + "times")));
+
+        iccIoResult = new IccIoResult(0x63, 0xF1, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("more data expected")));
+
+        iccIoResult = new IccIoResult(0x63, 0xF2, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains(
+                        "more data expected and proactive command pending")));
+
+        iccIoResult = new IccIoResult(0x63, 0xF3, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("unknown")));
+    }
+
+    @Test
+    public void check0x64_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x64, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("unknown")));
+
+        iccIoResult = new IccIoResult(0x64, 0x00, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("no information given")));
+    }
+
+    @Test
+    public void check0x65_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x65, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("unknown")));
+
+        iccIoResult = new IccIoResult(0x65, 0x00, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue((resultStr != null) && (resultStr.contains(
+                "no information given, state of non-volatile memory changed")));
+
+        iccIoResult = new IccIoResult(0x65, 0x81, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("memory problem")));
+    }
+
+    @Test
+    public void check0x67_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x67, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "the interpretation of this status word is command dependent")));
+
+        iccIoResult = new IccIoResult(0x67, 0x00, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "incorrect parameter P3")));
+    }
+
+    @Test
+    public void check0x68_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x68, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("unknown")));
+
+        iccIoResult = new IccIoResult(0x68, 0x00, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("no information given")));
+
+        iccIoResult = new IccIoResult(0x68, 0x81, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("logical channel not supported")));
+
+        iccIoResult = new IccIoResult(0x68, 0x82, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("secure messaging not supported")));
+    }
+
+    @Test
+    public void check0x69_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x69, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("unknown")));
+
+        iccIoResult = new IccIoResult(0x69, 0x00, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("no information given")));
+
+        iccIoResult = new IccIoResult(0x69, 0x81, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains(
+                        "command incompatible with file structure")));
+
+        iccIoResult = new IccIoResult(0x69, 0x82, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("security status not satisfied")));
+
+        iccIoResult = new IccIoResult(0x69, 0x83, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("authentication/PIN method blocked")));
+
+        iccIoResult = new IccIoResult(0x69, 0x84, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("referenced data invalidated")));
+
+        iccIoResult = new IccIoResult(0x69, 0x85, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("conditions of use not satisfied")));
+
+        iccIoResult = new IccIoResult(0x69, 0x86, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue((resultStr != null) && (resultStr.contains(
+                "command not allowed (no EF selected)")));
+
+        iccIoResult = new IccIoResult(0x69, 0x89, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue((resultStr != null) && (resultStr.contains(
+                "command not allowed - secure channel - security not satisfied")));
+    }
+
+    @Test
+    public void check0x6A_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x6A, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("unknown")));
+
+        iccIoResult = new IccIoResult(0x6A, 0x80, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "incorrect parameters in the data field")));
+
+        iccIoResult = new IccIoResult(0x6A, 0x81, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("function not supported")));
+
+        iccIoResult = new IccIoResult(0x6A, 0x82, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("file not found")));
+
+        iccIoResult = new IccIoResult(0x6A, 0x83, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("record not found")));
+
+        iccIoResult = new IccIoResult(0x6A, 0x84, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("not enough memory space")));
+
+        iccIoResult = new IccIoResult(0x6A, 0x86, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                (resultStr != null) && (resultStr.contains("incorrect parameters P1 to P2")));
+
+        iccIoResult = new IccIoResult(0x6A, 0x87, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue((resultStr != null) && (resultStr.contains(
+                "lc inconsistent with P1 to P2")));
+
+        iccIoResult = new IccIoResult(0x6A, 0x88, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue((resultStr != null) && (resultStr.contains(
+                "referenced data not found")));
+    }
+
+    @Test
+    public void check0x6B_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x6B, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(
+                resultStr != null && (resultStr.contains("incorrect parameter P1 or P2")));
+    }
+
+    @Test
+    public void check0x6C_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x6C, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains("wrong length, retry with ")));
+    }
+
+    @Test
+    public void check0x6D_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x6D, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "unknown instruction code given in the command")));
+    }
+
+    @Test
+    public void check0x6E_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x6E, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "wrong instruction class given in the command")));
+    }
+
+    @Test
+    public void check0x6F_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x6F, 0xC0, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "the interpretation of this status word is command dependent")));
+
+        iccIoResult = new IccIoResult(0x6F, 0x00, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "technical problem with no diagnostic given")));
+    }
+
+    @Test
+    public void check0x92_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x92, 0x00, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "command successful but after using an internal update retry routine")));
+
+        iccIoResult = new IccIoResult(0x92, 0x40, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "memory problem")));
+
+        iccIoResult = new IccIoResult(0x92, 0x41, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "unknown")));
+    }
+
+    @Test
+    public void check0x93_ErrorCodeParsing() {
+        IccIoResult iccIoResult = new IccIoResult(0x93, 0x00, new byte[10]);
+        String resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "SIM Application Toolkit is busy. Command cannot be executed"
+                        + " at present, further normal commands are allowed")));
+
+        iccIoResult = new IccIoResult(0x93, 0x41, new byte[10]);
+        resultStr = iccIoResult.toString();
+        Assert.assertTrue(resultStr != null && (resultStr.contains(
+                "unknown")));
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/IccPhoneBookInterfaceManagerTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/IccPhoneBookInterfaceManagerTest.java
old mode 100755
new mode 100644
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/IsimUiccRecordsTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/IsimUiccRecordsTest.java
index b97ae1f..6f4666c 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/IsimUiccRecordsTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/IsimUiccRecordsTest.java
@@ -30,13 +30,23 @@
 package com.android.internal.telephony.uicc;
 
 import static org.junit.Assert.assertEquals;
-import static org.mockito.Mockito.*;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 
+import android.content.Context;
 import android.content.Intent;
 import android.os.AsyncResult;
-import android.os.HandlerThread;
+import android.os.Handler;
 import android.os.Message;
+import android.os.test.TestLooper;
 
+import com.android.internal.telephony.CommandException;
+import com.android.internal.telephony.CommandsInterface;
+import com.android.internal.telephony.GsmAlphabet;
 import com.android.internal.telephony.TelephonyTest;
 
 import org.junit.After;
@@ -46,31 +56,48 @@
 
 public class IsimUiccRecordsTest extends TelephonyTest {
 
-    private IsimUiccRecords mIsimUiccRecords;
+    // Mocked classes
+    private IccFileHandler mFhMock;
+    private TestLooper mTestLooper;
+    private Handler mTestHandler;
+    private IsimUiccRecordsUT mIsimUiccRecordsUT;
 
-    private class IsimUiccRecordsTestHandler extends HandlerThread {
-        private IsimUiccRecordsTestHandler(String name) {
-            super(name);
-        }
-
-        @Override
-        public void onLooperPrepared() {
-            mIsimUiccRecords = new IsimUiccRecords(mUiccCardApplication3gpp, mContext, mSimulatedCommands);
-            setReady(true);
+    @SuppressWarnings("ClassCanBeStatic")
+    private class IsimUiccRecordsUT extends IsimUiccRecords {
+        IsimUiccRecordsUT(UiccCardApplication app, Context c,
+                CommandsInterface ci, IccFileHandler mFhMock) {
+            super(app, c, ci);
+            mFh = mFhMock;
         }
     }
 
     @Before
     public void setUp() throws Exception {
         super.setUp(getClass().getSimpleName());
-        new IsimUiccRecordsTestHandler(TAG).start();
-        waitUntilReady();
+        mFhMock = mock(IccFileHandler.class);
+        mTestLooper = new TestLooper();
+        mTestHandler = new Handler(mTestLooper.getLooper());
+        mTestHandler.post(
+                () -> {
+                    mIsimUiccRecordsUT =
+                            new IsimUiccRecordsUT(
+                                    mUiccCardApplication3gpp,
+                                    mContext,
+                                    mSimulatedCommands,
+                                    mFhMock);
+                });
+        mTestLooper.dispatchAll();
     }
 
     @After
     public void tearDown() throws Exception {
-        mIsimUiccRecords.dispose();
-        mIsimUiccRecords = null;
+        if (mTestLooper != null) {
+            mTestLooper.dispatchAll();
+            mTestLooper = null;
+        }
+        mTestHandler.removeCallbacksAndMessages(null);
+        mTestHandler = null;
+        mIsimUiccRecordsUT = null;
         super.tearDown();
     }
 
@@ -79,12 +106,274 @@
         Message msg = new Message();
         msg.what = IccRecords.EVENT_REFRESH;
         msg.obj = new AsyncResult(null, null, null);
-        mIsimUiccRecords.handleMessage(msg);
+        mIsimUiccRecordsUT.handleMessage(msg);
         ArgumentCaptor<Intent> intentCapture = ArgumentCaptor.forClass(Intent.class);
         verify(mContext).sendBroadcast(intentCapture.capture());
-
         assertEquals(
-            ((Intent) intentCapture.getValue()).getAction(), IsimUiccRecords.INTENT_ISIM_REFRESH);
+                ((Intent) intentCapture.getValue()).getAction(),
+                IsimUiccRecords.INTENT_ISIM_REFRESH);
     }
 
-}
+    @Test
+    public void testPsiSmscTelValue() {
+        // Testing smsc successfully reading case
+        String smscTest = "tel:+13123149810";
+        String hexSmsc =
+                "801074656C3A2B3133313233313439383130FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+                        + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
+        byte[] smscBytes = getStringToByte(hexSmsc);
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IccRecords.EVENT_GET_ICC_RECORD_DONE, mIsimUiccRecordsUT.getPsiSmscObject());
+        AsyncResult.forMessage(message, smscBytes, null);
+        mIsimUiccRecordsUT.handleMessage(message);
+        assertEquals(smscTest, mIsimUiccRecordsUT.getSmscIdentity());
+    }
+
+    private byte[] getStringToByte(String hexSmsc) {
+        byte[] smscBytes = IccUtils.hexStringToBytes(hexSmsc);
+        return smscBytes;
+    }
+
+    @Test
+    public void testGetPsiSmscSipValue() {
+        // Testing smsc successfully reading case
+        String smscTest = "sip:+12063130004@msg.pc.t-mobile.com;user=phone";
+        byte[] smscBytes = getStringToByte(
+                "802F7369703A2B3132303633313330303034406D73672E70632E742D6D6F62696C6"
+                        + "52E636F6D3B757365723D70686F6E65FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+                        + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+                        + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IccRecords.EVENT_GET_ICC_RECORD_DONE, mIsimUiccRecordsUT.getPsiSmscObject());
+        AsyncResult.forMessage(message, smscBytes, null);
+        mIsimUiccRecordsUT.handleMessage(message);
+        assertEquals(smscTest, mIsimUiccRecordsUT.getSmscIdentity());
+    }
+
+    @Test
+    public void testGetPsiSmscValueException() {
+        // Testing smsc exception handling case
+        String hexSmsc =
+                "801074656C3A2B3133313233313439383130FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+                        + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
+        byte[] smscBytes = getStringToByte(hexSmsc);
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IccRecords.EVENT_GET_ICC_RECORD_DONE, mIsimUiccRecordsUT.getPsiSmscObject());
+        AsyncResult.forMessage(message, smscBytes,
+                new CommandException(
+                        CommandException.Error.OPERATION_NOT_ALLOWED));
+        mIsimUiccRecordsUT.handleMessage(message);
+        assertEquals(null, mIsimUiccRecordsUT.getSmscIdentity());
+    }
+
+    @Test
+    public void testGetPsiSmscValueInvalidObject() {
+        // Testing smsc invalid data handling case
+        String smscTest = "tel:+13123149810";
+        byte[] smscBytes = GsmAlphabet.stringToGsm8BitPacked(smscTest);
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IccRecords.EVENT_GET_ICC_RECORD_DONE, mIsimUiccRecordsUT.getPsiSmscObject());
+        AsyncResult.forMessage(message, smscBytes,
+                new CommandException(
+                        CommandException.Error.OPERATION_NOT_ALLOWED));
+        mIsimUiccRecordsUT.handleMessage(message);
+        assertEquals(null, mIsimUiccRecordsUT.getSmscIdentity());
+    }
+
+    @Test
+    public void testGetSmssTpmrValue() {
+        // Testing tpmr successfully reading case
+        byte[] smss = new byte[2];
+        int tpmr = 10;
+        smss[0] = (byte) (tpmr & 0xFF);
+        IccRecords.SmssRecord smssRecord = mIsimUiccRecordsUT.createSmssRecord(null, smss);
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IsimUiccRecords.EVENT_SET_SMSS_RECORD_DONE, smssRecord);
+        AsyncResult.forMessage(message, null, null);
+        mIsimUiccRecordsUT.handleMessage(message);
+        assertEquals(tpmr, mIsimUiccRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testGetSmssTpmrValueException() {
+        // Testing tpmr reading fail case [ exception case ]
+        byte[] smss = new byte[2];
+        int tpmr = 10;
+        smss[0] = (byte) (tpmr & 0xFF);
+        IccRecords.SmssRecord smssRecord = mIsimUiccRecordsUT.createSmssRecord(null, smss);
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IsimUiccRecords.EVENT_SET_SMSS_RECORD_DONE, smssRecord);
+        AsyncResult.forMessage(message, null,
+                new CommandException(
+                        CommandException.Error.OPERATION_NOT_ALLOWED));
+        mIsimUiccRecordsUT.handleMessage(message);
+        assertEquals(-1, mIsimUiccRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testGetSmssTpmrValueExtreme() {
+        // Testing extreme tpmr value case [ fails ]
+        byte[] smss = new byte[2];
+        int tpmr = 400;
+        smss[0] = (byte) (tpmr & 0xFF);
+        IccRecords.SmssRecord smssRecord = mIsimUiccRecordsUT.createSmssRecord(null, smss);
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IsimUiccRecords.EVENT_SET_SMSS_RECORD_DONE, smssRecord);
+        AsyncResult.forMessage(message, null, null);
+        mIsimUiccRecordsUT.handleMessage(message);
+        assertEquals(144, mIsimUiccRecordsUT.getSmssTpmrValue());
+    }
+
+    private void setValidSmssValue() {
+        // preset/ initialize smssvalue
+        byte[] smss = new byte[2];
+        int tpmr = 10;
+        smss[0] = (byte) (tpmr & 0xFF);
+        IccRecords.SmssRecord smssRecord = mIsimUiccRecordsUT.createSmssRecord(null, smss);
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IsimUiccRecords.EVENT_SET_SMSS_RECORD_DONE, smssRecord);
+        AsyncResult.forMessage(message, null, null);
+        mIsimUiccRecordsUT.handleMessage(message);
+        assertEquals(tpmr, mIsimUiccRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testSetSmssTpmrValue() {
+        // Testing tpmr successfully setting case
+        setValidSmssValue();
+        int updateTpmr = 30;
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, true, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mIsimUiccRecordsUT.setSmssTpmrValue(updateTpmr, message);
+        mTestLooper.dispatchAll();
+        assertEquals(updateTpmr, mIsimUiccRecordsUT.getSmssTpmrValue());
+    }
+
+
+    @Test
+    public void testSetSmssTpmrValueException() {
+        // Testing exception while setting TPMR [ with out setting initial value]
+        int updateTpmr = 30;
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, true, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+        Message message = Message.obtain(mTestHandler);
+        mIsimUiccRecordsUT.setSmssTpmrValue(updateTpmr, message);
+        mTestLooper.dispatchAll();
+        assertEquals(-1, mIsimUiccRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testSetSmssTpmrValueException2() {
+        // Testing exception while setting TPMR
+        setValidSmssValue();
+        int updateTpmr = 30;
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+
+                    AsyncResult.forMessage(response, true, new CommandException(
+                            CommandException.Error.OPERATION_NOT_ALLOWED));
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+        Message message = Message.obtain(mTestHandler);
+        mIsimUiccRecordsUT.setSmssTpmrValue(updateTpmr, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertTrue(((CommandException) ar.exception).getCommandError() ==
+                CommandException.Error.OPERATION_NOT_ALLOWED);
+    }
+
+    @Test
+    public void testGetSimServiceTable() {
+        // reading sim service table successfully case
+        byte[] sst = new byte[9];
+        for (int i = 0; i < sst.length; i++) {
+            if (i % 2 == 0) {
+                sst[i] = 0;
+            } else {
+                sst[i] = 1;
+            }
+        }
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IccRecords.EVENT_GET_ICC_RECORD_DONE, mIsimUiccRecordsUT.getIsimIstObject());
+        AsyncResult ar = AsyncResult.forMessage(message, sst, null);
+        mIsimUiccRecordsUT.handleMessage(message);
+        String mockSst = IccUtils.bytesToHexString(sst);
+        String resultSst = mIsimUiccRecordsUT.getIsimIst();
+        assertEquals(mockSst, resultSst);
+    }
+
+    @Test
+    public void testGetSimServiceTableException() {
+        // sim service table exception handling case
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IccRecords.EVENT_GET_ICC_RECORD_DONE, mIsimUiccRecordsUT.getIsimIstObject());
+        AsyncResult ar = AsyncResult.forMessage(message,  null, new CommandException(
+                CommandException.Error.OPERATION_NOT_ALLOWED));
+        mIsimUiccRecordsUT.handleMessage(message);
+        String resultSst = mIsimUiccRecordsUT.getIsimIst();
+        assertEquals(null, resultSst);
+    }
+
+    @Test
+    public void testGetSsimServiceTableLessTableSize() {
+        // The less IST table size will not give any problem
+        byte[] sst = new byte[5];
+        for (int i = 0; i < sst.length; i++) {
+            if (i % 2 == 0) {
+                sst[i] = 0;
+            } else {
+                sst[i] = 1;
+            }
+        }
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IccRecords.EVENT_GET_ICC_RECORD_DONE, mIsimUiccRecordsUT.getIsimIstObject());
+        AsyncResult ar = AsyncResult.forMessage(message, sst, null);
+        mIsimUiccRecordsUT.handleMessage(message);
+        String mockSst = IccUtils.bytesToHexString(sst);
+        String resultSst = mIsimUiccRecordsUT.getIsimIst();
+        assertEquals(mockSst, resultSst);
+    }
+
+    @Test
+    public void testGetSsimServiceTableLargeTableSize() {
+        // The Big IST table size will not give any problem [ in feature the table may grows]
+        byte[] sst = new byte[99];
+        for (int i = 0; i < sst.length; i++) {
+            if (i % 2 == 0) {
+                sst[i] = 0;
+            } else {
+                sst[i] = 1;
+            }
+        }
+        Message message = mIsimUiccRecordsUT.obtainMessage(
+                IccRecords.EVENT_GET_ICC_RECORD_DONE, mIsimUiccRecordsUT.getIsimIstObject());
+        AsyncResult ar = AsyncResult.forMessage(message, sst, null);
+        mIsimUiccRecordsUT.handleMessage(message);
+        String mockSst = IccUtils.bytesToHexString(sst);
+        String resultSst = mIsimUiccRecordsUT.getIsimIst();
+        assertEquals(mockSst, resultSst);
+    }
+
+}
\ No newline at end of file
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/PortUtilsTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/PortUtilsTest.java
new file mode 100644
index 0000000..69d9a7d
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/PortUtilsTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.internal.telephony.uicc;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.doReturn;
+
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
+
+import com.android.internal.telephony.TelephonyTest;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
+public class PortUtilsTest extends TelephonyTest {
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp(getClass().getSimpleName());
+        doReturn(IccSlotStatus.MultipleEnabledProfilesMode.NONE)
+                .when(mUiccController).getSupportedMepMode(anyInt());
+        processAllMessages();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    @Test
+    public void testConvertToHalPortIndex() {
+        assertEquals(0, PortUtils.convertToHalPortIndex(0, 0));
+        doReturn(IccSlotStatus.MultipleEnabledProfilesMode.MEP_A1)
+                .when(mUiccController).getSupportedMepMode(anyInt());
+        assertEquals(1, PortUtils.convertToHalPortIndex(0, 0));
+    }
+
+    @Test
+    public void testConvertFromHalPortIndex() {
+        assertEquals(0, PortUtils.convertFromHalPortIndex(0, 1,
+                IccCardStatus.CardState.CARDSTATE_PRESENT,
+                IccSlotStatus.MultipleEnabledProfilesMode.MEP_A1));
+        assertEquals(1, PortUtils.convertFromHalPortIndex(0, 1,
+                IccCardStatus.CardState.CARDSTATE_PRESENT,
+                IccSlotStatus.MultipleEnabledProfilesMode.MEP_B));
+        assertEquals(1, PortUtils.convertFromHalPortIndex(0, 1,
+                IccCardStatus.CardState.CARDSTATE_ABSENT,
+                IccSlotStatus.MultipleEnabledProfilesMode.MEP_A1));
+        doReturn(IccSlotStatus.MultipleEnabledProfilesMode.MEP_A1)
+                .when(mUiccController).getSupportedMepMode(anyInt());
+        assertEquals(0, PortUtils.convertFromHalPortIndex(0, 1,
+                IccCardStatus.CardState.CARDSTATE_ABSENT,
+                IccSlotStatus.MultipleEnabledProfilesMode.NONE));
+    }
+}
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/RuimRecordsTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/RuimRecordsTest.java
old mode 100755
new mode 100644
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/SIMRecordsTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/SIMRecordsTest.java
index 9183235..79cd6a4 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/SIMRecordsTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/SIMRecordsTest.java
@@ -22,9 +22,11 @@
 import static org.junit.Assert.fail;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.isNull;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 
 import android.content.Context;
@@ -41,15 +43,19 @@
 import com.android.internal.telephony.TelephonyTest;
 import com.android.internal.telephony.uicc.IccRecords.OperatorPlmnInfo;
 import com.android.internal.telephony.uicc.IccRecords.PlmnNetworkName;
+import com.android.telephony.Rlog;
 
 import org.junit.After;
 import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 
 @RunWith(AndroidJUnit4.class)
 public class SIMRecordsTest extends TelephonyTest {
@@ -169,37 +175,37 @@
 
     private void setUpSetForbiddenPlmnsTests() {
         doAnswer(
-            invocation -> {
-                Message response = invocation.getArgument(1);
-                AsyncResult.forMessage(response, EF_SIZE, null);
-                response.sendToTarget();
-                return null;
-            })
-            .when(mFhMock)
-            .getEFTransparentRecordSize(anyInt(), any(Message.class));
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    AsyncResult.forMessage(response, EF_SIZE, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .getEFTransparentRecordSize(anyInt(), any(Message.class));
         doAnswer(
-            invocation -> {
-                Message response = invocation.getArgument(2);
-                AsyncResult.forMessage(response, true, null);
-                response.sendToTarget();
-                return null;
-            })
-            .when(mFhMock)
-            .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, true, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
     }
 
     @Test
     public void testGetForbiddenPlmns() {
         doAnswer(
-            invocation -> {
-                Message response = invocation.getArgument(1);
-                byte[] encodedFplmn = IccUtils.encodeFplmns(SHORT_FPLMNS_LIST, EF_SIZE);
-                AsyncResult.forMessage(response, encodedFplmn, null);
-                response.sendToTarget();
-                return null;
-            })
-            .when(mFhMock)
-            .loadEFTransparent(eq(SIMRecords.EF_FPLMN), any(Message.class));
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    byte[] encodedFplmn = IccUtils.encodeFplmns(SHORT_FPLMNS_LIST, EF_SIZE);
+                    AsyncResult.forMessage(response, encodedFplmn, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .loadEFTransparent(eq(SIMRecords.EF_FPLMN), any(Message.class));
 
         Message message = Message.obtain(mTestHandler);
         mSIMRecordsUT.getForbiddenPlmns(message);
@@ -214,15 +220,15 @@
     @Test
     public void testGetForbiddenPlmnsException() {
         doAnswer(
-            invocation -> {
-                Message response = invocation.getArgument(1);
-                AsyncResult.forMessage(response, null, new CommandException(
-                        CommandException.Error.OPERATION_NOT_ALLOWED));
-                response.sendToTarget();
-                return null;
-            })
-            .when(mFhMock)
-            .loadEFTransparent(eq(SIMRecords.EF_FPLMN), any(Message.class));
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    AsyncResult.forMessage(response, null, new CommandException(
+                            CommandException.Error.OPERATION_NOT_ALLOWED));
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .loadEFTransparent(eq(SIMRecords.EF_FPLMN), any(Message.class));
 
         Message message = Message.obtain(mTestHandler);
         mSIMRecordsUT.getForbiddenPlmns(message);
@@ -238,14 +244,14 @@
     @Test
     public void testGetForbiddenPlmnsNull() {
         doAnswer(
-            invocation -> {
-                Message response = invocation.getArgument(1);
-                AsyncResult.forMessage(response);
-                response.sendToTarget();
-                return null;
-            })
-            .when(mFhMock)
-            .loadEFTransparent(eq(SIMRecords.EF_FPLMN), any(Message.class));
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    AsyncResult.forMessage(response);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .loadEFTransparent(eq(SIMRecords.EF_FPLMN), any(Message.class));
 
         Message message = Message.obtain(mTestHandler);
         mSIMRecordsUT.getForbiddenPlmns(message);
@@ -259,15 +265,15 @@
     @Test
     public void testGetForbiddenPlmnsEmptyList() {
         doAnswer(
-            invocation -> {
-                Message response = invocation.getArgument(1);
-                byte[] encodedFplmn = IccUtils.encodeFplmns(EMPTY_FPLMN_LIST, EF_SIZE);
-                AsyncResult.forMessage(response, encodedFplmn, null);
-                response.sendToTarget();
-                return null;
-            })
-            .when(mFhMock)
-            .loadEFTransparent(eq(SIMRecords.EF_FPLMN), any(Message.class));
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    byte[] encodedFplmn = IccUtils.encodeFplmns(EMPTY_FPLMN_LIST, EF_SIZE);
+                    AsyncResult.forMessage(response, encodedFplmn, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .loadEFTransparent(eq(SIMRecords.EF_FPLMN), any(Message.class));
 
         Message message = Message.obtain(mTestHandler);
         mSIMRecordsUT.getForbiddenPlmns(message);
@@ -282,14 +288,14 @@
     @Test
     public void testGetForbiddenPlmnsInvalidLength() {
         doAnswer(
-            invocation -> {
-                Message response = invocation.getArgument(1);
-                AsyncResult.forMessage(response, new byte[] { (byte) 0xFF, (byte) 0xFF }, null);
-                response.sendToTarget();
-                return null;
-            })
-            .when(mFhMock)
-            .loadEFTransparent(eq(SIMRecords.EF_FPLMN), any(Message.class));
+                invocation -> {
+                    Message response = invocation.getArgument(1);
+                    AsyncResult.forMessage(response, new byte[]{(byte) 0xFF, (byte) 0xFF}, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .loadEFTransparent(eq(SIMRecords.EF_FPLMN), any(Message.class));
 
         Message message = Message.obtain(mTestHandler);
         mSIMRecordsUT.getForbiddenPlmns(message);
@@ -313,7 +319,7 @@
         targetPnns.add(new PlmnNetworkName(name, null));
 
         Message message = mSIMRecordsUT.obtainMessage(SIMRecords.EVENT_GET_PNN_DONE);
-        AsyncResult ar = AsyncResult.forMessage(message, rawPnns, null);
+        AsyncResult.forMessage(message, rawPnns, null);
         mSIMRecordsUT.handleMessage(message);
         List<PlmnNetworkName> parsedPnns = Arrays.asList(mSIMRecordsUT.getPnns());
 
@@ -321,23 +327,215 @@
     }
 
     private static byte[] encodePnn(String name) {
-        byte[] gsm7BitName = new byte[] {};
+        byte[] gsm7BitName = new byte[]{};
         try {
             gsm7BitName = GsmAlphabet.stringToGsm7BitPacked(name);
             gsm7BitName[0] = (byte) (name.length() % 8 | 0x80);
         } catch (Exception ex) {
             fail("SimRecordsTest: GsmAlphabet.stringToGsm7BitPacked() exception:" + ex);
         }
-
         byte[] encodedName = new byte[gsm7BitName.length + 2];
         encodedName[0] = 0x43;
         encodedName[1] = (byte) gsm7BitName.length;
         System.arraycopy(gsm7BitName, 0, encodedName, 2, gsm7BitName.length);
-
         return encodedName;
     }
 
     @Test
+    public void testGetSmssTpmrValue() {
+        // Testing tpmr successfully reading case
+        byte[] smss = new byte[2];
+        int tpmr = 10;
+        smss[0] = (byte) tpmr;
+        Message message = mSIMRecordsUT.obtainMessage(
+                SIMRecords.EVENT_GET_SMSS_RECORD_DONE, smss);
+        AsyncResult.forMessage(message, smss, null);
+        mSIMRecordsUT.handleMessage(message);
+        assertEquals(tpmr, mSIMRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testGetSmssTpmrValueException() {
+        // Testing tpmr exception case
+        byte[] smss = new byte[2];
+        int tpmr = 10;
+        smss[0] = (byte) tpmr;
+        Message message = mSIMRecordsUT.obtainMessage(
+                SIMRecords.EVENT_GET_SMSS_RECORD_DONE, smss);
+        AsyncResult.forMessage(message, smss, new CommandException(
+                CommandException.Error.OPERATION_NOT_ALLOWED));
+        mSIMRecordsUT.handleMessage(message);
+        assertEquals(-1, mSIMRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testSetSmssTpmrValue() {
+        // Testing tpmr successfully updating case
+        setValidSmssValue();
+        int updateTpmr = 30;
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, true, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setSmssTpmrValue(updateTpmr, message);
+        mTestLooper.dispatchAll();
+        assertEquals(updateTpmr, mSIMRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testSetSmssTpmrNegativevalue() {
+        // Testing tpmr successfully updating case
+        setValidSmssValue();
+        int updateTpmr = -2;
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, true, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setSmssTpmrValue(updateTpmr, message);
+        mTestLooper.dispatchAll();
+        // 10 is previous set value
+        assertEquals(10, mSIMRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testSetSmssTpmrHighvalue() {
+        // Testing tpmr successfully updating case
+        setValidSmssValue();
+        int updateTpmr = 256;
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, true, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setSmssTpmrValue(updateTpmr, message);
+        mTestLooper.dispatchAll();
+        // 10 is previous set value
+        assertEquals(10, mSIMRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testSetSmssTpmrMaxValue() {
+        // Testing tpmr successfully updating case
+        setValidSmssValue();
+        int updateTpmr = 255;
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, true, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setSmssTpmrValue(updateTpmr, message);
+        mTestLooper.dispatchAll();
+        assertEquals(updateTpmr, mSIMRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testSetSmssTpmrValueException() {
+        // Testing exception while setting TPMR [ with out setting initial value]
+        int updateTpmr = 30;
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, true, null);
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setSmssTpmrValue(updateTpmr, message);
+        mTestLooper.dispatchAll();
+        assertEquals(-1, mSIMRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
+    public void testSetSmssTpmrValueException2() {
+        // Testing exception while setting TPMR
+        setValidSmssValue();
+        int updateTpmr = 30;
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+
+                    AsyncResult.forMessage(response, true, new CommandException(
+                            CommandException.Error.OPERATION_NOT_ALLOWED));
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setSmssTpmrValue(updateTpmr, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof CommandException);
+        assertTrue(((CommandException) ar.exception).getCommandError() ==
+                CommandException.Error.OPERATION_NOT_ALLOWED);
+    }
+
+    @Test
+    public void testSetSmssTpmrLargeValue() {
+        // Testing Large TPMR value setting
+        setValidSmssValue();
+        int updateTpmr = 300;
+        doAnswer(
+                invocation -> {
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, true, new CommandException(
+                            CommandException.Error.OPERATION_NOT_ALLOWED));
+                    response.sendToTarget();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFTransparent(anyInt(), any(byte[].class), any(Message.class));
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setSmssTpmrValue(updateTpmr, message);
+        mTestLooper.dispatchAll();
+
+        AsyncResult ar = (AsyncResult) message.obj;
+        assertTrue(ar.exception instanceof IllegalArgumentException);
+    }
+
+    private void setValidSmssValue() {
+        // preset/ initialize smssvalue
+        byte[] smss = new byte[2];
+        int tpmr = 10;
+        smss[0] = (byte) (tpmr & 0xFF);
+        IccRecords.SmssRecord smssRecord = mSIMRecordsUT.createSmssRecord(null, smss);
+        Message message = mSIMRecordsUT.obtainMessage(
+                IsimUiccRecords.EVENT_SET_SMSS_RECORD_DONE, smssRecord);
+        AsyncResult.forMessage(message, null, null);
+        mSIMRecordsUT.handleMessage(message);
+        assertEquals(tpmr, mSIMRecordsUT.getSmssTpmrValue());
+    }
+
+    @Test
     public void testGetEfOpl() {
         ArrayList<byte[]> rawOpl = new ArrayList<byte[]>();
         List<OperatorPlmnInfo> targetOpl = new ArrayList<OperatorPlmnInfo>();
@@ -352,7 +550,7 @@
         targetOpl.add(new OperatorPlmnInfo(plmn, lacTacStart, lacTacEnd, pnnIndex));
 
         Message message = mSIMRecordsUT.obtainMessage(SIMRecords.EVENT_GET_OPL_DONE);
-        AsyncResult ar = AsyncResult.forMessage(message, rawOpl, null);
+        AsyncResult.forMessage(message, rawOpl, null);
         mSIMRecordsUT.handleMessage(message);
         List<OperatorPlmnInfo> parsedOpl = Arrays.asList(mSIMRecordsUT.getOpl());
 
@@ -368,7 +566,7 @@
         targetOpl.add(new OperatorPlmnInfo(plmn, lacTacStart, lacTacEnd, pnnIndex));
 
         message = mSIMRecordsUT.obtainMessage(SIMRecords.EVENT_GET_OPL_DONE);
-        ar = AsyncResult.forMessage(message, rawOpl, null);
+        AsyncResult.forMessage(message, rawOpl, null);
         mSIMRecordsUT.handleMessage(message);
         parsedOpl = Arrays.asList(mSIMRecordsUT.getOpl());
 
@@ -383,7 +581,7 @@
         rawOpl.add(encodeOpl(plmn, lacTacStart, lacTacEnd, pnnIndex));
 
         message = mSIMRecordsUT.obtainMessage(SIMRecords.EVENT_GET_OPL_DONE);
-        ar = AsyncResult.forMessage(message, rawOpl, null);
+        AsyncResult.forMessage(message, rawOpl, null);
         mSIMRecordsUT.handleMessage(message);
         parsedOpl = Arrays.asList(mSIMRecordsUT.getOpl());
 
@@ -405,7 +603,341 @@
         data[5] = (byte) (lacTacEnd >>> 8);
         data[6] = (byte) lacTacEnd;
         data[7] = (byte) pnnIndex;
-
         return data;
     }
+
+    @Test
+    public void testGetPsiSmscTelValue() {
+        // Testing smsc successfully reading case
+        String smscTest = "tel:+13123149810";
+        String hexSmsc =
+                "801074656C3A2B3133313233313439383130FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+                        + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
+        byte[] smscBytes = getStringToByte(hexSmsc);
+        Message message = mSIMRecordsUT.obtainMessage(
+                SIMRecords.EVENT_GET_PSISMSC_DONE, smscBytes);
+        AsyncResult.forMessage(message, smscBytes, null);
+        mSIMRecordsUT.handleMessage(message);
+        assertEquals(smscTest, mSIMRecordsUT.getSmscIdentity());
+    }
+
+    private byte[] getStringToByte(String hexSmsc) {
+        byte[] smscBytes = IccUtils.hexStringToBytes(hexSmsc);
+        return smscBytes;
+    }
+
+    @Test
+    public void testGetPsiSmscSipValue() {
+        // Testing smsc successfully reading case
+        String smscTest = "sip:+12063130004@msg.pc.t-mobile.com;user=phone";
+        byte[] smscBytes = getStringToByte(
+                "802F7369703A2B3132303633313330303034406D73672E70632E742D6D6F62696C6"
+                        + "52E636F6D3B757365723D70686F6E65FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+                        + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+                        + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+        Message message = mSIMRecordsUT.obtainMessage(
+                SIMRecords.EVENT_GET_PSISMSC_DONE, smscBytes);
+        AsyncResult.forMessage(message, smscBytes, null);
+        mSIMRecordsUT.handleMessage(message);
+        assertEquals(smscTest, mSIMRecordsUT.getSmscIdentity());
+    }
+
+    @Test
+    public void testGetPsiSmscException() {
+        // Testing smsc exception handling case
+        String hexSmsc =
+                "801074656C3A2B3133313233313439383130FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+                        + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
+        byte[] smscBytes = getStringToByte(hexSmsc);
+        Message message = mSIMRecordsUT.obtainMessage(
+                SIMRecords.EVENT_GET_PSISMSC_DONE, smscBytes);
+        AsyncResult ar = AsyncResult.forMessage(message, smscBytes,
+                new CommandException(
+                        CommandException.Error.OPERATION_NOT_ALLOWED));
+        mSIMRecordsUT.handleMessage(message);
+        assertTrue(ar.exception instanceof CommandException);
+        assertEquals(null, mSIMRecordsUT.getSmscIdentity());
+    }
+
+    @Test
+    public void testGetPsiSmscValueInvalidObject() {
+        // Testing smsc invalid data handling case
+        String smscTest = "tel:+13123149810";
+        byte[] smscBytes = GsmAlphabet.stringToGsm8BitPacked(smscTest);
+        Message message = mSIMRecordsUT.obtainMessage(
+                SIMRecords.EVENT_GET_PSISMSC_DONE, smscBytes);
+        AsyncResult ar = AsyncResult.forMessage(message, smscBytes,
+                new CommandException(
+                        CommandException.Error.OPERATION_NOT_ALLOWED));
+        mSIMRecordsUT.handleMessage(message);
+        assertEquals(null, mSIMRecordsUT.getSmscIdentity());
+        assertTrue(ar.exception instanceof CommandException);
+    }
+
+    @Test
+    public void testGetSimServiceTable() {
+        // reading sim service table successfully case
+        byte[] sst = new byte[111];
+        for (int i = 0; i < sst.length; i++) {
+            if (i % 2 == 0) {
+                sst[i] = 0;
+            } else {
+                sst[i] = 1;
+            }
+        }
+        Message message = mSIMRecordsUT.obtainMessage(SIMRecords.EVENT_GET_SST_DONE);
+        AsyncResult ar = AsyncResult.forMessage(message, sst, null);
+        mSIMRecordsUT.handleMessage(message);
+        String mockSst = IccUtils.bytesToHexString(sst);
+        String resultSst = mSIMRecordsUT.getSimServiceTable();
+        assertEquals(mockSst, resultSst);
+    }
+
+    @Test
+    public void testGetSimServiceTableException() {
+        // sim service table exception handling case
+        Message message = mSIMRecordsUT.obtainMessage(SIMRecords.EVENT_GET_SST_DONE);
+        AsyncResult ar = AsyncResult.forMessage(message, null, new CommandException(
+                CommandException.Error.OPERATION_NOT_ALLOWED));
+        mSIMRecordsUT.handleMessage(message);
+        String resultSst = mSIMRecordsUT.getSimServiceTable();
+        assertEquals(null, resultSst);
+    }
+
+    @Test
+    public void testGetSsimServiceTableLessTableSize() {
+        // sim service table reading case
+        byte[] sst = new byte[12];
+        for (int i = 0; i < sst.length; i++) {
+            if (i % 2 == 0) {
+                sst[i] = 0;
+            } else {
+                sst[i] = 1;
+            }
+        }
+        Message message = mSIMRecordsUT.obtainMessage(SIMRecords.EVENT_GET_SST_DONE);
+        AsyncResult ar = AsyncResult.forMessage(message, sst, null);
+        mSIMRecordsUT.handleMessage(message);
+        String mockSst = IccUtils.bytesToHexString(sst);
+        String resultSst = mSIMRecordsUT.getSimServiceTable();
+        assertEquals(mockSst, resultSst);
+    }
+
+    @Test
+    @Ignore("b/256282468")
+    public void testSetVoiceMailNumber() throws InterruptedException {
+
+        String voiceMailNumber = "1234567890";
+        String alphaTag = "Voicemail";
+        final CountDownLatch latch = new CountDownLatch(2);
+        doAnswer(
+                invocation -> {
+                    int[] result = new int[3];
+                    result[0] = 32;
+                    result[1] = 32;
+                    result[2] = 1;
+                    Rlog.d("SIMRecordsTest", "Executing the test invocation1");
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, result, null);
+                    response.sendToTarget();
+                    latch.countDown();
+                    return null;
+                })
+                .when(mFhMock)
+                .getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+
+        doAnswer(
+                invocation -> {
+                    int[] result = new int[3];
+                    result[0] = 32;
+                    result[1] = 32;
+                    result[2] = 1;
+                    Rlog.d("SIMRecordsTest", "Executing the test invocation2");
+                    Message response = invocation.getArgument(5);
+                    AsyncResult.forMessage(response, result, null);
+                    response.sendToTarget();
+                    latch.countDown();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFLinearFixed(anyInt(), eq(null), anyInt(), any(byte[].class),
+                        eq(null), any(Message.class));
+
+
+        mSIMRecordsUT.setMailboxIndex(1);
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setVoiceMailNumber(alphaTag, voiceMailNumber, message);
+        latch.await(5, TimeUnit.SECONDS);
+        mTestLooper.startAutoDispatch();
+        verify(mFhMock, times(1)).getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+        verify(mFhMock, times(1)).updateEFLinearFixed(anyInt(), eq(null),
+                anyInt(), any(byte[].class), eq(null), any(Message.class));
+
+        assertEquals(voiceMailNumber, mSIMRecordsUT.getVoiceMailNumber());
+        assertEquals(alphaTag, mSIMRecordsUT.getVoiceMailAlphaTag());
+    }
+
+    @Test
+    @Ignore("b/256282468")
+    public void testSetVoiceMailNumberBigAlphatag() throws InterruptedException {
+
+        String voiceMailNumber = "1234567890";
+        String alphaTag = "VoicemailAlphaTag-VoicemailAlphaTag";
+        final CountDownLatch latch = new CountDownLatch(2);
+        doAnswer(
+                invocation -> {
+                    int[] result = new int[3];
+                    result[0] = 32;
+                    result[1] = 32;
+                    result[2] = 1;
+                    Rlog.d("SIMRecordsTest", "Executing the test invocation1");
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, result, null);
+                    response.sendToTarget();
+                    latch.countDown();
+                    return null;
+                })
+                .when(mFhMock)
+                .getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+
+        doAnswer(
+                invocation -> {
+                    int[] result = new int[3];
+                    result[0] = 32;
+                    result[1] = 32;
+                    result[2] = 1;
+                    Rlog.d("SIMRecordsTest", "Executing the test invocation2");
+                    Message response = invocation.getArgument(5);
+                    AsyncResult.forMessage(response, result, null);
+                    response.sendToTarget();
+                    latch.countDown();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFLinearFixed(anyInt(), eq(null), anyInt(), any(byte[].class),
+                        eq(null), any(Message.class));
+
+
+        mSIMRecordsUT.setMailboxIndex(1);
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setVoiceMailNumber(alphaTag, voiceMailNumber, message);
+        latch.await(8, TimeUnit.SECONDS);
+        mTestLooper.startAutoDispatch();
+        verify(mFhMock, times(1)).getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+        verify(mFhMock, times(1)).updateEFLinearFixed(anyInt(), eq(null),
+                anyInt(), any(byte[].class), eq(null), any(Message.class));
+
+        //if attempt to save bugAlphatag which sim don't support so we will make it null
+        assertEquals(null, mSIMRecordsUT.getVoiceMailAlphaTag());
+        assertEquals(voiceMailNumber, mSIMRecordsUT.getVoiceMailNumber());
+    }
+
+    @Test
+    @Ignore("b/256282468")
+    public void testSetVoiceMailNumberUtf16Alphatag() throws InterruptedException {
+
+        String voiceMailNumber = "1234567890";
+        String alphaTag = "หมายเลขข้อความเสียง"; // Messagerie vocale
+        final CountDownLatch latch = new CountDownLatch(2);
+        doAnswer(
+                invocation -> {
+                    int[] result = new int[3];
+                    result[0] = 32;
+                    result[1] = 32;
+                    result[2] = 1;
+                    Rlog.d("SIMRecordsTest", "Executing the test invocation1");
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, result, null);
+                    response.sendToTarget();
+                    latch.countDown();
+                    return null;
+                })
+                .when(mFhMock)
+                .getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+
+        doAnswer(
+                invocation -> {
+                    int[] result = new int[3];
+                    result[0] = 32;
+                    result[1] = 32;
+                    result[2] = 1;
+                    Rlog.d("SIMRecordsTest", "Executing the test invocation2");
+                    Message response = invocation.getArgument(5);
+                    AsyncResult.forMessage(response, result, null);
+                    response.sendToTarget();
+                    latch.countDown();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFLinearFixed(anyInt(), eq(null), anyInt(), any(byte[].class),
+                        eq(null), any(Message.class));
+
+
+        mSIMRecordsUT.setMailboxIndex(1);
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setVoiceMailNumber(alphaTag, voiceMailNumber, message);
+        latch.await(5, TimeUnit.SECONDS);
+        mTestLooper.startAutoDispatch();
+        verify(mFhMock, times(1)).getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+        verify(mFhMock, times(1)).updateEFLinearFixed(anyInt(), eq(null),
+                anyInt(), any(byte[].class), eq(null), any(Message.class));
+
+        assertEquals(voiceMailNumber, mSIMRecordsUT.getVoiceMailNumber());
+        //if attempt to save bugAlphatag which sim don't support so we will make it null
+        assertEquals(null, mSIMRecordsUT.getVoiceMailAlphaTag());
+    }
+
+    @Test
+    @Ignore("b/256282468")
+    public void testSetVoiceMailNullNumber() throws InterruptedException {
+
+        String voiceMailNumber = null;
+        String alphaTag = "VoicemailAlphaTag"; // Messagerie vocale
+        final CountDownLatch latch = new CountDownLatch(2);
+        doAnswer(
+                invocation -> {
+                    int[] result = new int[3];
+                    result[0] = 32;
+                    result[1] = 32;
+                    result[2] = 1;
+                    Rlog.d("SIMRecordsTest", "Executing the test invocation1");
+                    Message response = invocation.getArgument(2);
+                    AsyncResult.forMessage(response, result, null);
+                    response.sendToTarget();
+                    latch.countDown();
+                    return null;
+                })
+                .when(mFhMock)
+                .getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+
+        doAnswer(
+                invocation -> {
+                    int[] result = new int[3];
+                    result[0] = 32;
+                    result[1] = 32;
+                    result[2] = 1;
+                    Rlog.d("SIMRecordsTest", "Executing the test invocation2");
+                    Message response = invocation.getArgument(5);
+                    AsyncResult.forMessage(response, result, null);
+                    response.sendToTarget();
+                    latch.countDown();
+                    return null;
+                })
+                .when(mFhMock)
+                .updateEFLinearFixed(anyInt(), eq(null), anyInt(), any(byte[].class),
+                        eq(null), any(Message.class));
+
+
+        mSIMRecordsUT.setMailboxIndex(1);
+        Message message = Message.obtain(mTestHandler);
+        mSIMRecordsUT.setVoiceMailNumber(alphaTag, voiceMailNumber, message);
+        latch.await(5, TimeUnit.SECONDS);
+        mTestLooper.startAutoDispatch();
+        verify(mFhMock, times(1)).getEFLinearRecordSize(anyInt(), isNull(), any(Message.class));
+        verify(mFhMock, times(1)).updateEFLinearFixed(anyInt(), eq(null),
+                anyInt(), any(byte[].class), eq(null), any(Message.class));
+
+        assertEquals(null, mSIMRecordsUT.getVoiceMailNumber());
+        assertEquals(alphaTag, mSIMRecordsUT.getVoiceMailAlphaTag());
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccCardTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccCardTest.java
index b30c3a7..0ed112d 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccCardTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccCardTest.java
@@ -61,7 +61,7 @@
         mIccIoResult = new IccIoResult(0x90, 0x00, IccUtils.hexStringToBytes("FF40"));
         mSimulatedCommands.setIccIoResultForApduLogicalChannel(mIccIoResult);
         mUiccCard = new UiccCard(mContext, mSimulatedCommands, mIccCardStatus, 0 /* phoneId */,
-            new Object(), false);
+            new Object(), false, IccSlotStatus.MultipleEnabledProfilesMode.NONE);
         processAllMessages();
         logd("create UiccCard");
     }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccControllerTest.java
index 0344e57..2ab23f3 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccControllerTest.java
@@ -104,6 +104,8 @@
 
         doReturn(PHONE_COUNT).when(mTelephonyManager).getPhoneCount();
         doReturn(PHONE_COUNT).when(mTelephonyManager).getSimCount();
+        doReturn(IccSlotStatus.MultipleEnabledProfilesMode.NONE)
+                .when(mMockSlot).getSupportedMepMode();
         // set number of slots to 1
         mContextFixture.putIntResource(com.android.internal.R.integer.config_num_physical_slots, 1);
 
@@ -114,6 +116,8 @@
         mIccCardStatus.mCdmaSubscriptionAppIndex =
                 mIccCardStatus.mImsSubscriptionAppIndex =
                         mIccCardStatus.mGsmUmtsSubscriptionAppIndex = -1;
+        mIccCardStatus.mCardState = IccCardStatus.CardState.CARDSTATE_PRESENT;
+        mIccCardStatus.mSupportedMepMode = IccSlotStatus.MultipleEnabledProfilesMode.NONE;
         mSimulatedCommands.setIccCardStatus(mIccCardStatus);
         // for testing we pretend slotIndex is set. In reality it would be invalid on older versions
         // (before 1.2) of hal
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccProfileTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccProfileTest.java
index 856639a..e139e95 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccProfileTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccProfileTest.java
@@ -46,6 +46,7 @@
 
 import androidx.test.filters.SmallTest;
 
+import com.android.internal.telephony.ISub;
 import com.android.internal.telephony.IccCardConstants.State;
 import com.android.internal.telephony.TelephonyTest;
 import com.android.internal.telephony.cat.CatService;
@@ -78,6 +79,7 @@
     private Handler mMockedHandler;
     private UiccCard mUiccCard;
     private SubscriptionInfo mSubscriptionInfo;
+    private ISub mMockedIsub;
 
     private IccCardApplicationStatus composeUiccApplicationStatus(
             IccCardApplicationStatus.AppType appType,
@@ -99,6 +101,13 @@
         mMockedHandler = mock(Handler.class);
         mUiccCard = mock(UiccCard.class);
         mSubscriptionInfo = mock(SubscriptionInfo.class);
+        mMockedIsub = mock(ISub.class);
+
+        doReturn(mMockedIsub).when(mIBinder).queryLocalInterface(anyString());
+        mServiceManagerMockedServices.put("isub", mIBinder);
+
+        doReturn(1).when(mMockedIsub).getSubId(0);
+
          /* initially there are no application available, but the array should not be empty. */
         IccCardApplicationStatus umtsApp = composeUiccApplicationStatus(
                 IccCardApplicationStatus.AppType.APPTYPE_USIM,
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccSlotTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccSlotTest.java
index 7bc737b..6aab7ad 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccSlotTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccSlotTest.java
@@ -511,4 +511,51 @@
         assertTrue("EuiccCard should be removable", mUiccSlot.isRemovable());
     }
 
+    @Test
+    @SmallTest
+    public void testMultipleEnabledProfilesData() {
+        IccSlotStatus iss = new IccSlotStatus();
+        IccSimPortInfo simPortInfo1 = new IccSimPortInfo();
+        simPortInfo1.mPortActive = false;
+        simPortInfo1.mLogicalSlotIndex = -1;
+        simPortInfo1.mIccId = "fake-iccid";
+
+        IccSimPortInfo simPortInfo2 = new IccSimPortInfo();
+        simPortInfo2.mPortActive = true;
+        simPortInfo2.mLogicalSlotIndex = 0;
+        simPortInfo2.mIccId = "fake-iccid";
+
+        iss.mSimPortInfos = new IccSimPortInfo[] {simPortInfo1, simPortInfo2};
+        iss.cardState = IccCardStatus.CardState.CARDSTATE_PRESENT;
+        iss.atr = "3B9F97C00AB1FE453FC6838031E073FE211F65D002341569810F21";
+        iss.setMultipleEnabledProfilesMode(3);
+
+
+        // initial state
+        assertEquals(IccCardStatus.CardState.CARDSTATE_ABSENT, mUiccSlot.getCardState());
+        assertEquals(IccSlotStatus.MultipleEnabledProfilesMode.NONE,
+                mUiccSlot.getSupportedMepMode());
+        assertFalse(mUiccSlot.isMultipleEnabledProfileSupported());
+
+        // update slot to inactive
+        mUiccSlot.update(null, iss, 0 /* slotIndex */);
+
+        // assert on updated values
+        assertNull(mUiccSlot.getUiccCard());
+        assertEquals(IccCardStatus.CardState.CARDSTATE_PRESENT, mUiccSlot.getCardState());
+        assertTrue(mUiccSlot.isMultipleEnabledProfileSupported());
+        assertEquals(IccSlotStatus.MultipleEnabledProfilesMode.MEP_B,
+                mUiccSlot.getSupportedMepMode());
+
+        iss.mSimPortInfos = new IccSimPortInfo[] {simPortInfo1};
+        iss.setMultipleEnabledProfilesMode(1); // Set MEP mode to MEP-A1
+
+        // update port info and MEP mode
+        mUiccSlot.update(null, iss, 0 /* slotIndex */);
+
+        // assert on updated values
+        assertFalse(mUiccSlot.isMultipleEnabledProfileSupported());
+        assertEquals(IccSlotStatus.MultipleEnabledProfilesMode.MEP_A1,
+                mUiccSlot.getSupportedMepMode());
+    }
 }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccStateChangedLauncherTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccStateChangedLauncherTest.java
index 18247d3..aa0b0d9 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccStateChangedLauncherTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/UiccStateChangedLauncherTest.java
@@ -99,7 +99,8 @@
 
         // The first broadcast should be sent after initialization.
         UiccCard card = new UiccCard(mContext, mSimulatedCommands,
-                makeCardStatus(CardState.CARDSTATE_PRESENT), 0 /* phoneId */, new Object(), false);
+                makeCardStatus(CardState.CARDSTATE_PRESENT), 0 /* phoneId */, new Object(), false,
+                IccSlotStatus.MultipleEnabledProfilesMode.NONE);
         when(UiccController.getInstance().getUiccCardForPhone(0)).thenReturn(card);
         uiccLauncher.handleMessage(msg);
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/EuiccCardTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/EuiccCardTest.java
index 79c4af4..c5eff90 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/EuiccCardTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/EuiccCardTest.java
@@ -18,6 +18,7 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
@@ -34,6 +35,7 @@
 import com.android.internal.telephony.uicc.IccCardApplicationStatus;
 import com.android.internal.telephony.uicc.IccCardStatus;
 import com.android.internal.telephony.uicc.IccSlotPortMapping;
+import com.android.internal.telephony.uicc.IccSlotStatus;
 import com.android.internal.telephony.uicc.euicc.apdu.LogicalChannelMocker;
 import com.android.internal.telephony.uicc.euicc.async.AsyncResultCallback;
 
@@ -94,7 +96,8 @@
 
         mEuiccCard =
             new EuiccCard(mContext, mMockCi, mMockIccCardStatus,
-                0 /* phoneId */, new Object(), false) {
+                0 /* phoneId */, new Object(), false,
+                    IccSlotStatus.MultipleEnabledProfilesMode.NONE) {
 
                 @Override
                 protected void loadEidAndNotifyRegistrants() {}
@@ -133,7 +136,8 @@
     public void testPassEidInConstructor() {
         mMockIccCardStatus.eid = "1A2B3C4D";
         mEuiccCard = new EuiccCard(mContextFixture.getTestDouble(), mMockCi,
-                mMockIccCardStatus, 0 /* phoneId */, new Object(), false);
+                mMockIccCardStatus, 0 /* phoneId */, new Object(), false,
+                IccSlotStatus.MultipleEnabledProfilesMode.NONE);
 
         final int eventEidReady = 0;
         Handler handler = new Handler(Looper.myLooper()) {
@@ -154,7 +158,8 @@
         int channel = mockLogicalChannelResponses("BF3E065A041A2B3C4D9000");
         mHandler.post(() -> {
             mEuiccCard = new EuiccCard(mContextFixture.getTestDouble(), mMockCi,
-                    mMockIccCardStatus, 0 /* phoneId */, new Object(), false);
+                    mMockIccCardStatus, 0 /* phoneId */, new Object(), false,
+                    IccSlotStatus.MultipleEnabledProfilesMode.NONE);
         });
         processAllMessages();
 
@@ -188,7 +193,7 @@
     private void verifyStoreData(int channel, String command) {
         verify(mMockCi, times(1))
                 .iccTransmitApduLogicalChannel(eq(channel), eq(0x80 | channel), eq(0xE2), eq(0x91),
-                        eq(0), eq(command.length() / 2), eq(command), any());
+                        eq(0), eq(command.length() / 2), eq(command), anyBoolean(), any());
     }
 
     private int mockLogicalChannelResponses(Object... responses) {
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/EuiccPortTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/EuiccPortTest.java
index 90163cf..2f8c19e 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/EuiccPortTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/EuiccPortTest.java
@@ -22,6 +22,7 @@
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
@@ -49,6 +50,7 @@
 import com.android.internal.telephony.uicc.IccCardApplicationStatus;
 import com.android.internal.telephony.uicc.IccCardStatus;
 import com.android.internal.telephony.uicc.IccSlotPortMapping;
+import com.android.internal.telephony.uicc.IccSlotStatus;
 import com.android.internal.telephony.uicc.IccUtils;
 import com.android.internal.telephony.uicc.asn1.Asn1Node;
 import com.android.internal.telephony.uicc.asn1.InvalidAsn1DataException;
@@ -118,7 +120,8 @@
         mMockIccCardStatus.mSlotPortMapping = new IccSlotPortMapping();
         mEuiccPort =
             new EuiccPort(mContext, mMockCi, mMockIccCardStatus,
-                0 /* phoneId */, new Object(), mEuiccCard, false) {
+                0 /* phoneId */, new Object(), mEuiccCard, false,
+                    IccSlotStatus.MultipleEnabledProfilesMode.NONE) {
                 @Override
                 protected byte[] getDeviceId() {
                     return IccUtils.bcdToBytes("987654321012345");
@@ -180,7 +183,7 @@
         assertEquals(1, profiles.length);
         assertEquals("98760000000000543210", profiles[0].getIccid());
         assertEquals(EuiccProfileInfo.PROFILE_STATE_ENABLED, profiles[0].getState());
-        verifyStoreData(channel, "BF2D0F5C0D5A909192B79F709599BF769F20");
+        verifyStoreData(channel, "BF2D0F5C0D5A909192B79F709599BF769F24");
     }
 
     @Test
@@ -203,7 +206,7 @@
     @Test
     public void testEnabledOnEsimPort_GetAllProfiles() {
         int channel = mockLogicalChannelResponses(
-                "BF2D18A016E3145A0A896700000000004523019F7001009F2001019000");
+                "BF2D18A016E3145A0A896700000000004523019F7001009F2401019000");
 
         ResultCaptor<EuiccProfileInfo[]> resultCaptor = new ResultCaptor<>();
         mEuiccPort.mIsSupportsMultipleEnabledProfiles = true; // MEP capable
@@ -218,7 +221,7 @@
         // which is valid port. So the state should be enabled.
         // (As per MEP state and enabledOnEsimPort concept)
         assertEquals(EuiccProfileInfo.PROFILE_STATE_ENABLED, profiles[0].getState());
-        verifyStoreData(channel, "BF2D0F5C0D5A909192B79F709599BF769F20");
+        verifyStoreData(channel, "BF2D0F5C0D5A909192B79F709599BF769F24");
     }
 
     @Test
@@ -235,7 +238,7 @@
         EuiccProfileInfo[] profiles = resultCaptor.result;
         assertEquals(1, profiles.length);
         assertEquals(EuiccProfileInfo.PROFILE_STATE_DISABLED, profiles[0].getState());
-        verifyStoreData(channel, "BF2D0F5C0D5A909192B79F709599BF769F20");
+        verifyStoreData(channel, "BF2D0F5C0D5A909192B79F709599BF769F24");
     }
 
     @Test
@@ -375,6 +378,23 @@
     }
 
     @Test
+    public void testSwitchToProfile_MepA1() {
+        int channel = mockLogicalChannelResponses("BF31038001039000");
+
+        ResultCaptor<Void> resultCaptor = new ResultCaptor<>();
+        mMockIccCardStatus.mSlotPortMapping.mPortIndex = 1;
+        mEuiccPort.updateSupportMepProperties(true,
+                IccSlotStatus.MultipleEnabledProfilesMode.MEP_A1);
+        mEuiccPort.update(mContext, mMockCi, mMockIccCardStatus, mEuiccCard);
+        mEuiccPort.switchToProfile("98760000000000543210", true, resultCaptor, mHandler);
+        processAllMessages();
+
+        assertEquals(3, ((EuiccCardErrorException) resultCaptor.exception).getErrorCode());
+        // In case of MEP-A1, verify portIndex is shifted or not.
+        verifyStoreData(channel, "BF3114A00C5A0A896700000000004523018101FF820102");
+    }
+
+    @Test
     public void testGetEid() {
         int channel = mockLogicalChannelResponses("BF3E065A041A2B3C4D9000");
 
@@ -884,7 +904,7 @@
         verify(mMockCi, never())
                 .iccTransmitApduLogicalChannel(
                         eq(channel), anyInt(), anyInt(), anyInt(), anyInt(), anyInt(), any(),
-                        any());
+                        anyBoolean(), any());
     }
 
     @Test
@@ -917,7 +937,7 @@
         verify(mMockCi, never())
                 .iccTransmitApduLogicalChannel(
                         eq(channel), anyInt(), anyInt(), anyInt(), anyInt(), anyInt(), any(),
-                        any());
+                        anyBoolean(), any());
     }
 
     @Test
@@ -1175,7 +1195,7 @@
     private void verifyStoreData(int channel, String command) {
         verify(mMockCi, times(1))
                 .iccTransmitApduLogicalChannel(eq(channel), eq(0x80 | channel), eq(0xE2), eq(0x91),
-                        eq(0), eq(command.length() / 2), eq(command), any());
+                        eq(0), eq(command.length() / 2), eq(command), anyBoolean(), any());
     }
 
     private int mockLogicalChannelResponses(Object... responses) {
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/apdu/ApduSenderTest.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/apdu/ApduSenderTest.java
index 2b9e767..e637ec0 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/apdu/ApduSenderTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/apdu/ApduSenderTest.java
@@ -20,6 +20,7 @@
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
@@ -149,7 +150,7 @@
 
         assertEquals("A1A1A1", IccUtils.bytesToHexString(mResponseCaptor.response));
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(0), eq("a"), any());
+                eq(3), eq(0), eq("a"), anyBoolean(), any());
     }
 
     @Test
@@ -169,13 +170,13 @@
 
         assertEquals("A4", IccUtils.bytesToHexString(mResponseCaptor.response));
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(0), eq("a"), any());
+                eq(3), eq(0), eq("a"), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(1), eq("ab"), any());
+                eq(3), eq(1), eq("ab"), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(0), eq(""), any());
+                eq(3), eq(0), eq(""), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(0x81), eq(0xE2), eq(0x91),
-                eq(0), eq(2), eq("abcd"), any());
+                eq(0), eq(2), eq("abcd"), anyBoolean(), any());
     }
 
     @Test
@@ -196,11 +197,11 @@
 
         assertEquals("A3", IccUtils.bytesToHexString(mResponseCaptor.response));
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(0), eq("a"), any());
+                eq(3), eq(0), eq("a"), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(1), eq("ab"), any());
+                eq(3), eq(1), eq("ab"), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(0), eq(""), any());
+                eq(3), eq(0), eq(""), anyBoolean(), any());
     }
 
     @Test
@@ -216,11 +217,11 @@
 
         assertEquals("A1A1A1B2B2B2B2C3C3", IccUtils.bytesToHexString(mResponseCaptor.response));
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(0), eq("a"), any());
+                eq(3), eq(0), eq("a"), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel), eq(0xC0), eq(0),
-                eq(0), eq(4), eq(""), any());
+                eq(0), eq(4), eq(""), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel), eq(0xC0), eq(0),
-                eq(0), eq(2), eq(""), any());
+                eq(0), eq(2), eq(""), anyBoolean(), any());
     }
 
     @Test
@@ -244,15 +245,15 @@
 
         assertEquals("C3", IccUtils.bytesToHexString(mResponseCaptor.response));
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(0), eq("a"), any());
+                eq(3), eq(0), eq("a"), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(0), eq("b"), any());
+                eq(3), eq(0), eq("b"), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(0x81), eq(0xE2), eq(0x11),
-                eq(0), eq(0xFF), eq(s1), any());
+                eq(0), eq(0xFF), eq(s1), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(0x81), eq(0xE2), eq(0x11),
-                eq(1), eq(0xFF), eq(s2), any());
+                eq(1), eq(0xFF), eq(s2), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(0x81), eq(0xE2), eq(0x91),
-                eq(2), eq(16), eq(s3), any());
+                eq(2), eq(16), eq(s3), anyBoolean(), any());
     }
 
     @Test
@@ -272,9 +273,9 @@
 
         assertEquals("B2222B", IccUtils.bytesToHexString(mResponseCaptor.response));
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(0x81), eq(0xE2), eq(0x11),
-                eq(0), eq(0xFF), eq(s1), any());
+                eq(0), eq(0xFF), eq(s1), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(0x81), eq(0xE2), eq(0x91),
-                eq(1), eq(0xFF), eq(s2), any());
+                eq(1), eq(0xFF), eq(s2), anyBoolean(), any());
     }
 
     @Test
@@ -290,7 +291,7 @@
 
         assertEquals("B2222B", IccUtils.bytesToHexString(mResponseCaptor.response));
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(0x81), eq(0xE2), eq(0x91),
-                eq(0), eq(0), eq(""), any());
+                eq(0), eq(0), eq(""), anyBoolean(), any());
     }
 
     @Test
@@ -313,13 +314,13 @@
 
         assertEquals(0x6985, ((ApduException) mResponseCaptor.exception).getApduStatus());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(channel | 10), eq(1), eq(2),
-                eq(3), eq(0), eq("a"), any());
+                eq(3), eq(0), eq("a"), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(0x81), eq(0xE2), eq(0x11),
-                eq(0), eq(0xFF), eq(s1), any());
+                eq(0), eq(0xFF), eq(s1), anyBoolean(), any());
         verify(mMockCi).iccTransmitApduLogicalChannel(eq(channel), eq(0x81), eq(0xE2), eq(0x11),
-                eq(1), eq(0xFF), eq(s2), any());
+                eq(1), eq(0xFF), eq(s2), anyBoolean(), any());
         verify(mMockCi, never()).iccTransmitApduLogicalChannel(eq(channel), eq(0x81), eq(0xE2),
-                eq(0x91), eq(2), eq(16), eq(s3), any());
+                eq(0x91), eq(2), eq(16), eq(s3), anyBoolean(), any());
     }
 
     @Test
diff --git a/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/apdu/LogicalChannelMocker.java b/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/apdu/LogicalChannelMocker.java
index e9796a1..e7bceee 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/apdu/LogicalChannelMocker.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/uicc/euicc/apdu/LogicalChannelMocker.java
@@ -16,6 +16,7 @@
 
 package com.android.internal.telephony.uicc.euicc.apdu;
 
+import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
@@ -70,26 +71,44 @@
             @Override
             public Object answer(InvocationOnMock invocation) throws Throwable {
                 Object responseObject = responseObjects[mIndex++];
-                boolean isException = responseObject instanceof Throwable;
-                int sw1 = 0;
-                int sw2 = 0;
-                String hex = responseObject.toString();
-                if (!isException) {
-                    int l = hex.length();
-                    sw1 = Integer.parseInt(hex.substring(l - 4, l - 2), 16);
-                    sw2 = Integer.parseInt(hex.substring(l - 2), 16);
-                    hex = hex.substring(0, l - 4);
-                }
-                IccIoResult result = isException ? null : new IccIoResult(sw1, sw2, hex);
-                Throwable exception = isException ? (Throwable) responseObject : null;
-
-                Message msg = response.getValue();
-                AsyncResult.forMessage(msg, result, exception);
-                msg.sendToTarget();
+                mockIccTransmitApduLogicalChannelResponse(response, responseObject);
                 return null;
             }
         }).when(mockCi).iccTransmitApduLogicalChannel(eq(channel), anyInt(), anyInt(), anyInt(),
                 anyInt(), anyInt(), anyString(), response.capture());
+
+        doAnswer(new Answer() {
+            private int mIndex = 0;
+
+            @Override
+            public Object answer(InvocationOnMock invocation) throws Throwable {
+                Object responseObject = responseObjects[mIndex++];
+                mockIccTransmitApduLogicalChannelResponse(response, responseObject);
+                return null;
+            }
+        }).when(mockCi).iccTransmitApduLogicalChannel(eq(channel), anyInt(), anyInt(), anyInt(),
+                anyInt(), anyInt(), anyString(), anyBoolean(), response.capture());
+    }
+
+    private static void mockIccTransmitApduLogicalChannelResponse(ArgumentCaptor<Message> response,
+            Object responseObject) throws Throwable {
+
+        boolean isException = responseObject instanceof Throwable;
+        int sw1 = 0;
+        int sw2 = 0;
+        String hex = responseObject.toString();
+        if (!isException) {
+            int l = hex.length();
+            sw1 = Integer.parseInt(hex.substring(l - 4, l - 2), 16);
+            sw2 = Integer.parseInt(hex.substring(l - 2), 16);
+            hex = hex.substring(0, l - 4);
+        }
+        IccIoResult result = isException ? null : new IccIoResult(sw1, sw2, hex);
+        Throwable exception = isException ? (Throwable) responseObject : null;
+
+        Message msg = response.getValue();
+        AsyncResult.forMessage(msg, result, exception);
+        msg.sendToTarget();
     }
 
     public static void mockCloseLogicalChannel(CommandsInterface mockCi, int channel) {