Fixes related to manul provisioning

Fix Callforwarding indication for operator name updation

Callforwarding and message waiting indication should be updated
when any status is getting updated for some subid.
Change-Id Iaa527f9fbc5650c5926bc29112f57fb30b5a01d4

Fix NPE due to uninitialized map.
Change-Id I0bd6af6277719c906985371871eed95123c208e6

Fix do not create a PhoneAccount for the slot without SIM card

* When the slot has no SIM card, the dummy subId now is no longer
negative number, so the TelecomAccountRegistry will create a PhoneAccount
for the slot without SIM card too.
Check whether the slot has SIM card and it is provisoned. if yes, add a
PhoneAccount for the slot with SIM card.
* Set the SIM as default OutgoingPhoneAccount when single SIM is inserted.
* Do not update default voice sub when device shutdown
 is in progress and when SIM power down due to APM
Change-Id Ibaed043f93a74d69e990594b4bb17d8e8c140a27

Fix no service notification for manual search mode.

If SIM card is deactivated and network selection is chosen
as manual search mode, an incorrect indication 'No service'
shown to user.Here the No service is shown as a result of
Out of Service state caused by sim deactivation.
Fix: Check for Out of Service state caused by SIM deactivation
and dont show user the no service indication for this case.
Reset CFI/MWI on sim deactivation
When the sim is deactivated update the call forwarding indicator
and message waiting indicator icon on UI.
Change-Id I18c03bde425069e21ff16238d72761efba111993

MSIM: Fix for default voice subscription change

- If there is a delay in reading FullIccSerialNumber value
  for one of the SIM, the default voice preference is reset
  to the SIM for which the serial number is already read.
- Remove getFullIccSerialNumber() condition as check for
  provision status & mSubscriptionManager.isActiveSubId()
  are sufficient to determine SIM records availability
CR-Fixed: 2058977
Change-Id: I60acd98832a1d9fac3d2384867fb274b1796f5b0

Change-Id: I839929c738aa71755f1b1f71301788968c21762c
Issue: FP3-A13#56
CRs-Fixed: 2179165
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index e63ea7b..961c46d 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -36,6 +36,8 @@
 import android.os.Handler;
 import android.os.Message;
 import android.os.PersistableBundle;
+import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.os.SystemClock;
 import android.os.SystemProperties;
 import android.os.UserHandle;
@@ -61,6 +63,7 @@
 
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneFactory;
+import com.android.internal.telephony.SubscriptionController;
 import com.android.internal.telephony.RILConstants;
 import com.android.internal.telephony.TelephonyCapabilities;
 import com.android.internal.telephony.util.NotificationChannelController;
@@ -72,6 +75,8 @@
 import java.util.List;
 import java.util.Set;
 
+import org.codeaurora.internal.IExtTelephony;
+
 /**
  * NotificationManager-related utility code for the Phone app.
  *
@@ -283,7 +288,7 @@
         Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
         mMwiVisible.put(subId, visible);
 
-        if (visible) {
+        if (visible && isUiccCardProvisioned(subId)) {
             if (phone == null) {
                 Log.w(LOG_TAG, "Found null phone for: " + subId);
                 return;
@@ -528,7 +533,7 @@
      */
     /* package */ void updateCfi(int subId, boolean visible, boolean isRefresh) {
         logi("updateCfi: subId= " + subId + ", visible=" + (visible ? "Y" : "N"));
-        if (visible) {
+        if (visible && isUiccCardProvisioned(subId)) {
             // If Unconditional Call Forwarding (forward all calls) for VOICE
             // is enabled, just show a notification.  We'll default to expanded
             // view for now, so the there is less confusion about the icon.  If
@@ -968,6 +973,26 @@
         return SystemClock.elapsedRealtime();
     }
 
+    private boolean isUiccCardProvisioned(int subId) {
+        final int PROVISIONED = 1;
+        final int INVALID_STATE = -1;
+        int provisionStatus = INVALID_STATE;
+        IExtTelephony mExtTelephony = IExtTelephony.Stub
+                .asInterface(ServiceManager.getService("extphone"));
+        int slotId = SubscriptionController.getInstance().getSlotIndex(subId);
+        try {
+            //get current provision state of the SIM.
+            provisionStatus = mExtTelephony.getCurrentUiccCardProvisioningStatus(slotId);
+        } catch (RemoteException ex) {
+            provisionStatus = INVALID_STATE;
+            if (DBG) log("Failed to get status for slotId: "+ slotId +" Exception: " + ex);
+        } catch (NullPointerException ex) {
+            provisionStatus = INVALID_STATE;
+            if (DBG) log("Failed to get status for slotId: "+ slotId +" Exception: " + ex);
+        }
+        return provisionStatus == PROVISIONED;
+   }
+
     // TODO(b/243010310): merge methods below with Settings#MobileNetworkUtils and optimize them.
     // The methods below are copied from com.android.settings.network.telephony.MobileNetworkUtils
     // to make sure the network selection unavailable notification should not show when network
diff --git a/src/com/android/services/telephony/TelecomAccountRegistry.java b/src/com/android/services/telephony/TelecomAccountRegistry.java
index a9b4c71..a711036 100644
--- a/src/com/android/services/telephony/TelecomAccountRegistry.java
+++ b/src/com/android/services/telephony/TelecomAccountRegistry.java
@@ -37,6 +37,8 @@
 import android.os.HandlerThread;
 import android.os.Looper;
 import android.os.PersistableBundle;
+import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.provider.Telephony;
@@ -70,11 +72,14 @@
 import com.android.telephony.Rlog;
 
 import java.util.Arrays;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Optional;
 import java.util.function.Predicate;
 
+import org.codeaurora.internal.IExtTelephony;
+
 /**
  * Owns all data we have registered with Telecom including handling dynamic addition and
  * removal of SIMs and SIP accounts.
@@ -117,6 +122,15 @@
 
     private Handler mHandler;
 
+    // Flag which decides whether SIM should power down due to APM,
+    private static final String APM_SIM_NOT_PWDN_PROPERTY = "persist.vendor.radio.apm_sim_not_pwdn";
+
+    private enum Count {
+        ZERO,
+        ONE,
+        TWO
+    }
+
     final class AccountEntry implements PstnPhoneCapabilitiesNotifier.Listener {
         private final Phone mPhone;
         private PhoneAccount mAccount;
@@ -1537,13 +1551,43 @@
 
         final boolean phoneAccountsEnabled = mContext.getResources().getBoolean(
                 R.bool.config_pstn_phone_accounts_enabled);
+        int activeCount = 0;
+        int activeSubscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
 
         synchronized (mAccountsLock) {
             try {
                 if (phoneAccountsEnabled) {
+                    // states we are interested in from what
+                    // IExtTelephony.getCurrentUiccCardProvisioningStatus()can return
+                    final int PROVISIONED = 1;
+                    final int INVALID_STATE = -1;
+
                     for (Phone phone : phones) {
+                        int provisionStatus = PROVISIONED;
                         int subscriptionId = phone.getSubId();
-                        Log.i(this, "setupAccounts: Phone with subscription id %d", subscriptionId);
+                        int slotId = phone.getPhoneId();
+
+                        if (mTelephonyManager.getPhoneCount() > 1) {
+                            IExtTelephony mExtTelephony = IExtTelephony.Stub
+                                    .asInterface(ServiceManager.getService("extphone"));
+                            try {
+                                //get current provision state of the SIM.
+                                provisionStatus =
+                                        mExtTelephony.getCurrentUiccCardProvisioningStatus(slotId);
+                            } catch (RemoteException ex) {
+                                provisionStatus = INVALID_STATE;
+                                Log.w(this, "Failed to get status , slotId: "+ slotId +" Exception: "
+                                        + ex);
+                            } catch (NullPointerException ex) {
+                                provisionStatus = INVALID_STATE;
+                                Log.w(this, "Failed to get status , slotId: "+ slotId +" Exception: "
+                                        + ex);
+                            }
+                        }
+
+                        Log.d(this, "Phone with subscription id: " + subscriptionId +
+                                " slotId: " + slotId + " provisionStatus: " + provisionStatus);
+
                         // setupAccounts can be called multiple times during service changes.
                         // Don't add an account if the Icc has not been set yet.
                         if (!SubscriptionManager.isValidSubscriptionId(subscriptionId)