Improve Logic for Detecting Carrier Load Complete

-Add empty PersistableBundles to allow CarrierConfigLoader to
 operate as though loads are all completed after bind/load times
 out OR fails from either the carrier service package OR the system
 default package.

-Fix the case where carrier/system default keys hadn't loaded but
 a bundle would report that CONFIG_APPLIED=true if there are test
 overrides; this could cause the system to proceed during SIM change
 in test scenarios despite the complete configs not having been
 loaded.

Bug: 176523196
Test: atest TeleServiceTests // and additional manual test
Change-Id: Id6434de52f7b1c69a0ceda037c5a96d29c1c7641
diff --git a/src/com/android/phone/CarrierConfigLoader.java b/src/com/android/phone/CarrierConfigLoader.java
index 37a0618..831b537 100644
--- a/src/com/android/phone/CarrierConfigLoader.java
+++ b/src/com/android/phone/CarrierConfigLoader.java
@@ -278,6 +278,9 @@
                                     obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
                                     BIND_TIMEOUT_MILLIS);
                         } else {
+                            // Put a stub bundle in place so that the rest of the logic continues
+                            // smoothly.
+                            mConfigFromDefaultApp[phoneId] = new PersistableBundle();
                             // Send broadcast if bind fails.
                             notifySubscriptionInfoUpdater(phoneId);
                             // TODO: We *must* call unbindService even if bindService returns false.
@@ -359,6 +362,8 @@
                         unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
                         broadcastConfigChangedIntent(phoneId);
                     }
+                    // Put a stub bundle in place so that the rest of the logic continues smoothly.
+                    mConfigFromDefaultApp[phoneId] = new PersistableBundle();
                     notifySubscriptionInfoUpdater(phoneId);
                     break;
                 }
@@ -402,6 +407,9 @@
                                     obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
                                     BIND_TIMEOUT_MILLIS);
                         } else {
+                            // Put a stub bundle in place so that the rest of the logic continues
+                            // smoothly.
+                            mConfigFromCarrierApp[phoneId] = new PersistableBundle();
                             // Send broadcast if bind fails.
                             broadcastConfigChangedIntent(phoneId);
                             loge("Bind to carrier app: " + carrierPackageName + " fails");
@@ -471,7 +479,8 @@
 
                 case EVENT_BIND_CARRIER_TIMEOUT:
                 case EVENT_FETCH_CARRIER_TIMEOUT: {
-                    loge("Bind/fetch from carrier app timeout");
+                    loge("Bind/fetch from carrier app timeout, package="
+                            + getCarrierPackageForPhoneId(phoneId));
                     removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
                     // If we attempted to bind to the app, but the service connection is null due to
                     // the race condition that clear config event happens before bind/fetch complete
@@ -482,6 +491,8 @@
                         unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
                         broadcastConfigChangedIntent(phoneId);
                     }
+                    // Put a stub bundle in place so that the rest of the logic continues smoothly.
+                    mConfigFromCarrierApp[phoneId] = new PersistableBundle();
                     notifySubscriptionInfoUpdater(phoneId);
                     break;
                 }
@@ -848,6 +859,7 @@
     }
 
     /** Returns the package name of a priveleged carrier app, or null if there is none. */
+    @Nullable
     private String getCarrierPackageForPhoneId(int phoneId) {
         List<String> carrierPackageNames = TelephonyManager.from(mContext)
                 .getCarrierPackageNamesForIntentAndPhone(
@@ -1167,25 +1179,27 @@
             PersistableBundle config = mConfigFromDefaultApp[phoneId];
             if (config != null) {
                 retConfig.putAll(config);
-                if (getCarrierPackageForPhoneId(phoneId) == null) {
-                    retConfig.putBoolean(
-                            CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
-                }
             }
             config = mConfigFromCarrierApp[phoneId];
             if (config != null) {
                 retConfig.putAll(config);
-                retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
             }
             config = mPersistentOverrideConfigs[phoneId];
             if (config != null) {
                 retConfig.putAll(config);
-                retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
             }
             config = mOverrideConfigs[phoneId];
             if (config != null) {
                 retConfig.putAll(config);
             }
+            // Ignore the theoretical case of the default app not being present since that won't
+            // work in CarrierConfigLoader today.
+            final boolean allConfigsApplied =
+                    (mConfigFromCarrierApp[phoneId] != null
+                        || getCarrierPackageForPhoneId(phoneId) == null)
+                    && mConfigFromDefaultApp[phoneId] != null;
+            retConfig.putBoolean(
+                    CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
         } else {
             if (mNoSimConfig != null) {
                 retConfig.putAll(mNoSimConfig);