[Settings] Stop network mode query when changing config

Changing the configuration of network mode could take a long time.
In some cases, it blocks the query operation.
This is a change to stop query operation from execution during the time
when configuration is changing.

Bug: 227431634
Test: local
Change-Id: Ifeb16e7e9a0ec3de970fe588a1cce6d31fbba9cd
diff --git a/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java b/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
index 58afeb0..ed8dc24 100644
--- a/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
+++ b/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
@@ -52,6 +52,8 @@
 import java.util.List;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
 
 /**
  * Preference controller for "Auto Select Network"
@@ -70,11 +72,17 @@
     ProgressDialog mProgressDialog;
     @VisibleForTesting
     SwitchPreference mSwitchPreference;
+    private AtomicBoolean mUpdatingConfig;
+    private int mCacheOfModeStatus;
+    private AtomicLong mRecursiveUpdate;
 
     public AutoSelectPreferenceController(Context context, String key) {
         super(context, key);
         mTelephonyManager = context.getSystemService(TelephonyManager.class);
         mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+        mRecursiveUpdate = new AtomicLong();
+        mUpdatingConfig = new AtomicBoolean();
+        mCacheOfModeStatus = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
         mListeners = new ArrayList<>();
         mUiHandler = new Handler(Looper.getMainLooper());
         mAllowedNetworkTypesListener = new AllowedNetworkTypesListener(
@@ -88,7 +96,9 @@
             displayPreference(mPreferenceScreen);
         }
         if (mSwitchPreference != null) {
+            mRecursiveUpdate.getAndIncrement();
             updateState(mSwitchPreference);
+            mRecursiveUpdate.decrementAndGet();
         }
     }
 
@@ -118,8 +128,13 @@
 
     @Override
     public boolean isChecked() {
-        return mTelephonyManager.getNetworkSelectionMode()
-                == TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
+        if (!mUpdatingConfig.get()) {
+            mCacheOfModeStatus = mTelephonyManager.getNetworkSelectionMode();
+            for (OnNetworkSelectModeListener lsn : mListeners) {
+                lsn.onNetworkSelectModeUpdated(mCacheOfModeStatus);
+            }
+        }
+        return mCacheOfModeStatus == TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
     }
 
     @Override
@@ -147,6 +162,10 @@
 
     @Override
     public boolean setChecked(boolean isChecked) {
+        if (mRecursiveUpdate.get() != 0) {
+            // Changing from software are allowed and changing presentation only.
+            return true;
+        }
         if (isChecked) {
             setAutomaticSelectionMode();
         } else {
@@ -171,18 +190,17 @@
         }
         return ThreadUtils.postOnBackgroundThread(() -> {
             // set network selection mode in background
+            mUpdatingConfig.set(true);
             mTelephonyManager.setNetworkSelectionModeAutomatic();
-            final int mode = mTelephonyManager.getNetworkSelectionMode();
+            mUpdatingConfig.set(false);
 
             //Update UI in UI thread
             final long durationMillis = SystemClock.elapsedRealtime() - startMillis;
             mUiHandler.postDelayed(() -> {
+                mRecursiveUpdate.getAndIncrement();
                 mSwitchPreference.setEnabled(true);
-                mSwitchPreference.setChecked(
-                        mode == TelephonyManager.NETWORK_SELECTION_MODE_AUTO);
-                for (OnNetworkSelectModeListener lsn : mListeners) {
-                    lsn.onNetworkSelectModeChanged();
-                }
+                mSwitchPreference.setChecked(isChecked());
+                mRecursiveUpdate.decrementAndGet();
                 dismissProgressBar();
             }, Math.max(MINIMUM_DIALOG_TIME_MILLIS - durationMillis, 0));
         });
@@ -232,11 +250,11 @@
     }
 
     /**
-     * Callback when network select mode is changed
+     * Callback when network select mode might get updated
      *
      * @see TelephonyManager#getNetworkSelectionMode()
      */
     public interface OnNetworkSelectModeListener {
-        void onNetworkSelectModeChanged();
+        void onNetworkSelectModeUpdated(int mode);
     }
 }
diff --git a/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java b/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
index 54f5ce1..4c5dd95 100644
--- a/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
+++ b/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
@@ -48,11 +48,13 @@
     private Preference mPreference;
     private PreferenceScreen mPreferenceScreen;
     private AllowedNetworkTypesListener mAllowedNetworkTypesListener;
+    private int mCacheOfModeStatus;
 
     public OpenNetworkSelectPagePreferenceController(Context context, String key) {
         super(context, key);
         mTelephonyManager = context.getSystemService(TelephonyManager.class);
         mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+        mCacheOfModeStatus = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
         mAllowedNetworkTypesListener = new AllowedNetworkTypesListener(
                 context.getMainExecutor());
         mAllowedNetworkTypesListener.setAllowedNetworkTypesListener(
@@ -96,7 +98,7 @@
     @Override
     public void updateState(Preference preference) {
         super.updateState(preference);
-        preference.setEnabled(mTelephonyManager.getNetworkSelectionMode()
+        preference.setEnabled(mCacheOfModeStatus
                 != TelephonyManager.NETWORK_SELECTION_MODE_AUTO);
 
         Intent intent = new Intent();
@@ -125,7 +127,10 @@
     }
 
     @Override
-    public void onNetworkSelectModeChanged() {
-        updateState(mPreference);
+    public void onNetworkSelectModeUpdated(int mode) {
+        mCacheOfModeStatus = mode;
+        if (mPreference != null) {
+            updateState(mPreference);
+        }
     }
 }
diff --git a/tests/unit/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java
index f7e3111..53ae1b9 100644
--- a/tests/unit/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java
+++ b/tests/unit/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java
@@ -93,15 +93,18 @@
 
         mPreference = new Preference(mContext);
         mController = new OpenNetworkSelectPagePreferenceController(mContext,
-                "open_network_select");
+                "open_network_select") {
+            @Override
+            public void updateState(Preference preference) {
+                super.updateState(mPreference);
+            }
+        };
         mController.init(mLifecycle, SUB_ID);
     }
 
     @Test
     public void updateState_modeAuto_disabled() {
-        when(mTelephonyManager.getNetworkSelectionMode()).thenReturn(
-                TelephonyManager.NETWORK_SELECTION_MODE_AUTO);
-
+        mController.onNetworkSelectModeUpdated(TelephonyManager.NETWORK_SELECTION_MODE_AUTO);
         mController.updateState(mPreference);
 
         assertThat(mPreference.isEnabled()).isFalse();