Support hiding roaming icon for display purposes am: 041adf1af1

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

Change-Id: Ia68d29739679b9efad66f79dc4bf8905faf7b341
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/src/java/com/android/internal/telephony/DisplayInfoController.java b/src/java/com/android/internal/telephony/DisplayInfoController.java
index 567331d..c8f2248 100644
--- a/src/java/com/android/internal/telephony/DisplayInfoController.java
+++ b/src/java/com/android/internal/telephony/DisplayInfoController.java
@@ -19,9 +19,11 @@
 import android.annotation.NonNull;
 import android.os.Handler;
 import android.os.Message;
+import android.os.PersistableBundle;
 import android.os.Registrant;
 import android.os.RegistrantList;
 import android.telephony.AnomalyReporter;
+import android.telephony.CarrierConfigManager;
 import android.telephony.ServiceState;
 import android.telephony.TelephonyDisplayInfo;
 import android.telephony.TelephonyManager;
@@ -66,34 +68,53 @@
 
     /** Event for service state changed (roaming). */
     private static final int EVENT_SERVICE_STATE_CHANGED = 1;
+    /** Event for carrier config changed. */
+    private static final int EVENT_CARRIER_CONFIG_CHANGED = 2;
 
-    private final Phone mPhone;
-    private final NetworkTypeController mNetworkTypeController;
-    private final RegistrantList mTelephonyDisplayInfoChangedRegistrants = new RegistrantList();
-    private @NonNull TelephonyDisplayInfo mTelephonyDisplayInfo;
-    private @NonNull ServiceState mServiceState;
+    @NonNull private final Phone mPhone;
+    @NonNull private final NetworkTypeController mNetworkTypeController;
+    @NonNull private final RegistrantList mTelephonyDisplayInfoChangedRegistrants =
+            new RegistrantList();
+    @NonNull private TelephonyDisplayInfo mTelephonyDisplayInfo;
+    @NonNull private ServiceState mServiceState;
+    @NonNull private PersistableBundle mConfigs;
 
-    public DisplayInfoController(Phone phone) {
+    public DisplayInfoController(@NonNull Phone phone) {
         mPhone = phone;
         mLogTag = "DIC-" + mPhone.getPhoneId();
+        mServiceState = mPhone.getServiceStateTracker().getServiceState();
+        mConfigs = new PersistableBundle();
+        try {
+            mConfigs = mPhone.getContext().getSystemService(CarrierConfigManager.class)
+                    .getConfigForSubId(mPhone.getSubId(),
+                            CarrierConfigManager.KEY_SHOW_ROAMING_INDICATOR_BOOL);
+        } catch (Exception ignored) {
+            // CarrierConfigLoader might not be available yet.
+            // Once it's available, configs will be updated through the listener.
+        }
+        mPhone.getServiceStateTracker()
+                .registerForServiceStateChanged(this, EVENT_SERVICE_STATE_CHANGED, null);
+        mPhone.getContext().getSystemService(CarrierConfigManager.class)
+                .registerCarrierConfigChangeListener(Runnable::run,
+                        (slotIndex, subId, carrierId, specificCarrierId) -> {
+                            if (slotIndex == mPhone.getPhoneId()) {
+                                obtainMessage(EVENT_CARRIER_CONFIG_CHANGED).sendToTarget();
+                            }
+                        });
         mTelephonyDisplayInfo = new TelephonyDisplayInfo(
                 TelephonyManager.NETWORK_TYPE_UNKNOWN,
-                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE);
+                TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
+                false);
         mNetworkTypeController = new NetworkTypeController(phone, this);
+        // EVENT_UPDATE will transition from DefaultState to the current state
+        // and update the TelephonyDisplayInfo based on the current state.
         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 @NonNull TelephonyDisplayInfo getTelephonyDisplayInfo() {
+    @NonNull public TelephonyDisplayInfo getTelephonyDisplayInfo() {
         return mTelephonyDisplayInfo;
     }
 
@@ -105,7 +126,7 @@
         TelephonyDisplayInfo newDisplayInfo = new TelephonyDisplayInfo(
                 mNetworkTypeController.getDataNetworkType(),
                 mNetworkTypeController.getOverrideNetworkType(),
-                mServiceState.getRoaming());
+                isRoaming());
         if (!newDisplayInfo.equals(mTelephonyDisplayInfo)) {
             logl("TelephonyDisplayInfo changed from " + mTelephonyDisplayInfo + " to "
                     + newDisplayInfo);
@@ -117,6 +138,23 @@
     }
 
     /**
+     * Determine the roaming status for icon display only.
+     * If this is {@code true}, the roaming indicator will be shown, and if this is {@code false},
+     * the roaming indicator will not be shown.
+     * To get the actual roaming status, use {@link ServiceState#getRoaming()} instead.
+     *
+     * @return Whether the device is considered roaming for display purposes.
+     */
+    private boolean isRoaming() {
+        boolean roaming = mServiceState.getRoaming();
+        if (roaming && !mConfigs.getBoolean(CarrierConfigManager.KEY_SHOW_ROAMING_INDICATOR_BOOL)) {
+            logl("Override roaming for display due to carrier configs.");
+            roaming = false;
+        }
+        return roaming;
+    }
+
+    /**
      * Validate the display info and trigger anomaly report if needed.
      *
      * @param displayInfo The display info to validate.
@@ -175,6 +213,13 @@
                 log("ServiceState updated, isRoaming=" + mServiceState.getRoaming());
                 updateTelephonyDisplayInfo();
                 break;
+            case EVENT_CARRIER_CONFIG_CHANGED:
+                mConfigs = mPhone.getContext().getSystemService(CarrierConfigManager.class)
+                        .getConfigForSubId(mPhone.getSubId(),
+                                CarrierConfigManager.KEY_SHOW_ROAMING_INDICATOR_BOOL);
+                log("Carrier configs updated: " + mConfigs);
+                updateTelephonyDisplayInfo();
+                break;
         }
     }
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/DisplayInfoControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/DisplayInfoControllerTest.java
index f729b80..abe2873 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/DisplayInfoControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/DisplayInfoControllerTest.java
@@ -54,7 +54,6 @@
 @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";
@@ -64,7 +63,6 @@
     private DisplayInfoController mDic;
     private ServiceStateTracker mSst;
     private ServiceStateTrackerTestHandler mSstHandler;
-    private SignalStrengthController mSsc;
     private PersistableBundle mBundle;
     private CarrierConfigManager.CarrierConfigChangeListener mCarrierConfigChangeListener;
 
@@ -75,8 +73,8 @@
 
         @Override
         public void onLooperPrepared() {
-            mSsc = new SignalStrengthController(mPhone);
-            doReturn(mSsc).when(mPhone).getSignalStrengthController();
+            SignalStrengthController ssc = new SignalStrengthController(mPhone);
+            doReturn(ssc).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));
@@ -103,6 +101,7 @@
 
         doReturn((Executor) Runnable::run).when(mContext).getMainExecutor();
         mBundle = mContextFixture.getCarrierConfigBundle();
+        mBundle.putBoolean(CarrierConfigManager.KEY_SHOW_ROAMING_INDICATOR_BOOL, true);
         mSstHandler = new ServiceStateTrackerTestHandler(getClass().getSimpleName());
         mSstHandler.start();
         waitUntilReady();
@@ -281,4 +280,25 @@
 
         assertTrue(tdi.isRoaming());
     }
+
+    @Test
+    public void testIsRoamingOverride_HideRoamingIndicator() {
+        doReturn(true).when(mPhone).isPhoneTypeGsm();
+        mBundle.putStringArray(
+                CarrierConfigManager.KEY_GSM_ROAMING_NETWORKS_STRING_ARRAY, new String[] {NUMERIC});
+        mBundle.putBoolean(CarrierConfigManager.KEY_SHOW_ROAMING_INDICATOR_BOOL, false);
+        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();
+
+        assertFalse(tdi.isRoaming()); // display override
+    }
 }