Merge "[Telephony Setting] Add Preferred Network observer for Auto Select Network Mode" into rvc-dev
diff --git a/res/xml/mobile_network_settings.xml b/res/xml/mobile_network_settings.xml
index b49791c..f025f70 100644
--- a/res/xml/mobile_network_settings.xml
+++ b/res/xml/mobile_network_settings.xml
@@ -196,7 +196,7 @@
         <PreferenceCategory
             android:key="network_operators_category_key"
             android:title="@string/network_operator_category"
-            settings:controller="com.android.settings.widget.PreferenceCategoryController">
+            settings:controller="com.android.settings.network.telephony.NetworkPreferenceCategoryController">
 
             <SwitchPreference
                 android:key="auto_select_key"
diff --git a/src/com/android/settings/network/PreferredNetworkModeContentObserver.java b/src/com/android/settings/network/PreferredNetworkModeContentObserver.java
new file mode 100644
index 0000000..9d07eb4
--- /dev/null
+++ b/src/com/android/settings/network/PreferredNetworkModeContentObserver.java
@@ -0,0 +1,66 @@
+/*
+ * 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.settings.network;
+
+import android.content.Context;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.os.Handler;
+import android.provider.Settings;
+
+import androidx.annotation.VisibleForTesting;
+
+/**
+ * {@link ContentObserver} to listen to Preferred Network Mode change
+ */
+public class PreferredNetworkModeContentObserver extends ContentObserver {
+    @VisibleForTesting
+    OnPreferredNetworkModeChangedListener mListener;
+
+    public PreferredNetworkModeContentObserver(Handler handler) {
+        super(handler);
+    }
+
+    public void setPreferredNetworkModeChangedListener(OnPreferredNetworkModeChangedListener lsn) {
+        mListener = lsn;
+    }
+
+    @Override
+    public void onChange(boolean selfChange) {
+        super.onChange(selfChange);
+        if (mListener != null) {
+            mListener.onPreferredNetworkModeChanged();
+        }
+    }
+
+    public void register(Context context, int subId) {
+        final Uri uri = Settings.Global.getUriFor(
+                Settings.Global.PREFERRED_NETWORK_MODE + subId);
+        context.getContentResolver().registerContentObserver(uri, false, this);
+    }
+
+    public void unregister(Context context) {
+        context.getContentResolver().unregisterContentObserver(this);
+    }
+
+    /**
+     * Listener for update of Preferred Network Mode change
+     */
+    public interface OnPreferredNetworkModeChangedListener {
+        void onPreferredNetworkModeChanged();
+    }
+}
diff --git a/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java b/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
index d2ff6e8..c1afb77 100644
--- a/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
+++ b/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
@@ -20,7 +20,6 @@
 import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
 
 import android.content.Context;
-import android.database.ContentObserver;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.PersistableBundle;
@@ -38,6 +37,7 @@
 import androidx.preference.PreferenceScreen;
 
 import com.android.settings.R;
+import com.android.settings.network.PreferredNetworkModeContentObserver;
 import com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants;
 
 import java.util.ArrayList;
@@ -52,22 +52,28 @@
         ListPreference.OnPreferenceChangeListener, LifecycleObserver {
 
     private static final String LOG_TAG = "EnabledNetworkMode";
-    private ContentObserver mPreferredNetworkModeObserver;
+    private PreferredNetworkModeContentObserver mPreferredNetworkModeObserver;
     private Preference mPreference;
+    private PreferenceScreen mPreferenceScreen;
     private TelephonyManager mTelephonyManager;
     private CarrierConfigManager mCarrierConfigManager;
     private PreferenceEntriesBuilder mBuilder;
 
     public EnabledNetworkModePreferenceController(Context context, String key) {
         super(context, key);
-        mPreferredNetworkModeObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
-            @Override
-            public void onChange(boolean selfChange) {
-                if (mPreference != null) {
-                    updateState(mPreference);
-                }
-            }
-        };
+        mPreferredNetworkModeObserver = new PreferredNetworkModeContentObserver(
+                new Handler(Looper.getMainLooper()));
+        mPreferredNetworkModeObserver.setPreferredNetworkModeChangedListener(
+                () -> updatePreference());
+    }
+
+    private void updatePreference() {
+        if (mPreferenceScreen != null) {
+            displayPreference(mPreferenceScreen);
+        }
+        if (mPreference != null) {
+            updateState(mPreference);
+        }
     }
 
     @Override
@@ -94,20 +100,18 @@
 
     @OnLifecycleEvent(ON_START)
     public void onStart() {
-        mContext.getContentResolver().registerContentObserver(
-                Settings.Global.getUriFor(Settings.Global.PREFERRED_NETWORK_MODE + mSubId),
-                true,
-                mPreferredNetworkModeObserver);
+        mPreferredNetworkModeObserver.register(mContext, mSubId);
     }
 
     @OnLifecycleEvent(ON_STOP)
     public void onStop() {
-        mContext.getContentResolver().unregisterContentObserver(mPreferredNetworkModeObserver);
+        mPreferredNetworkModeObserver.unregister(mContext);
     }
 
     @Override
     public void displayPreference(PreferenceScreen screen) {
         super.displayPreference(screen);
+        mPreferenceScreen = screen;
         mPreference = screen.findPreference(getPreferenceKey());
     }
 
diff --git a/src/com/android/settings/network/telephony/MobileNetworkSettings.java b/src/com/android/settings/network/telephony/MobileNetworkSettings.java
index 6ca556a..4103447 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkSettings.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkSettings.java
@@ -44,7 +44,6 @@
 import com.android.settings.network.telephony.gsm.AutoSelectPreferenceController;
 import com.android.settings.network.telephony.gsm.OpenNetworkSelectPagePreferenceController;
 import com.android.settings.search.BaseSearchIndexProvider;
-import com.android.settings.widget.PreferenceCategoryController;
 import com.android.settingslib.core.AbstractPreferenceController;
 import com.android.settingslib.search.SearchIndexable;
 
@@ -155,14 +154,13 @@
                 use(WifiCallingPreferenceController.class).init(mSubId);
 
         final OpenNetworkSelectPagePreferenceController openNetworkSelectPagePreferenceController =
-                use(OpenNetworkSelectPagePreferenceController.class).init(mSubId);
+                use(OpenNetworkSelectPagePreferenceController.class).init(getLifecycle(), mSubId);
         final AutoSelectPreferenceController autoSelectPreferenceController =
                 use(AutoSelectPreferenceController.class)
-                        .init(mSubId)
+                        .init(getLifecycle(), mSubId)
                         .addListener(openNetworkSelectPagePreferenceController);
-        use(PreferenceCategoryController.class).setChildren(
-                Arrays.asList(autoSelectPreferenceController));
-
+        use(NetworkPreferenceCategoryController.class).init(getLifecycle(), mSubId)
+                .setChildren(Arrays.asList(autoSelectPreferenceController));
         mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class);
         mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId);
         mCdmaSubscriptionPreferenceController = use(CdmaSubscriptionPreferenceController.class);
diff --git a/src/com/android/settings/network/telephony/NetworkPreferenceCategoryController.java b/src/com/android/settings/network/telephony/NetworkPreferenceCategoryController.java
new file mode 100644
index 0000000..042eb5f
--- /dev/null
+++ b/src/com/android/settings/network/telephony/NetworkPreferenceCategoryController.java
@@ -0,0 +1,80 @@
+/*
+ * 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.settings.network.telephony;
+
+import static androidx.lifecycle.Lifecycle.Event.ON_START;
+import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
+
+import android.content.Context;
+import android.os.Handler;
+import android.os.Looper;
+import android.telephony.SubscriptionManager;
+
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.OnLifecycleEvent;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.network.PreferredNetworkModeContentObserver;
+import com.android.settings.widget.PreferenceCategoryController;
+
+/**
+ * Preference controller for "Network" category
+ */
+public class NetworkPreferenceCategoryController extends PreferenceCategoryController
+        implements LifecycleObserver {
+
+    private PreferenceScreen mPreferenceScreen;
+    private PreferredNetworkModeContentObserver mPreferredNetworkModeObserver;
+    protected int mSubId;
+
+    public NetworkPreferenceCategoryController(Context context, String key) {
+        super(context, key);
+        mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+        mPreferredNetworkModeObserver = new PreferredNetworkModeContentObserver(
+                new Handler(Looper.getMainLooper()));
+        mPreferredNetworkModeObserver.setPreferredNetworkModeChangedListener(
+                () -> updatePreference());
+    }
+
+    private void updatePreference() {
+        displayPreference(mPreferenceScreen);
+    }
+
+    @OnLifecycleEvent(ON_START)
+    public void onStart() {
+        mPreferredNetworkModeObserver.register(mContext, mSubId);
+    }
+
+    @OnLifecycleEvent(ON_STOP)
+    public void onStop() {
+        mPreferredNetworkModeObserver.unregister(mContext);
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mPreferenceScreen = screen;
+    }
+
+    public NetworkPreferenceCategoryController init(Lifecycle lifecycle, int subId) {
+        mSubId = subId;
+
+        lifecycle.addObserver(this);
+        return this;
+    }
+}
diff --git a/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java b/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
index 2eda9d9..361f58e 100644
--- a/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
+++ b/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
@@ -16,6 +16,9 @@
 
 package com.android.settings.network.telephony.gsm;
 
+import static androidx.lifecycle.Lifecycle.Event.ON_START;
+import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
+
 import android.app.ProgressDialog;
 import android.app.settings.SettingsEnums;
 import android.content.Context;
@@ -30,12 +33,16 @@
 import android.telephony.TelephonyManager;
 
 import androidx.annotation.VisibleForTesting;
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.OnLifecycleEvent;
 import androidx.preference.Preference;
 import androidx.preference.PreferenceScreen;
 import androidx.preference.SwitchPreference;
 
 import com.android.settings.R;
 import com.android.settings.core.SubSettingLauncher;
+import com.android.settings.network.PreferredNetworkModeContentObserver;
 import com.android.settings.network.telephony.MobileNetworkUtils;
 import com.android.settings.network.telephony.NetworkSelectSettings;
 import com.android.settings.network.telephony.TelephonyTogglePreferenceController;
@@ -48,10 +55,13 @@
 /**
  * Preference controller for "Auto Select Network"
  */
-public class AutoSelectPreferenceController extends TelephonyTogglePreferenceController {
+public class AutoSelectPreferenceController extends TelephonyTogglePreferenceController
+        implements LifecycleObserver{
     private static final long MINIMUM_DIALOG_TIME_MILLIS = TimeUnit.SECONDS.toMillis(1);
 
     private final Handler mUiHandler;
+    private PreferenceScreen mPreferenceScreen;
+    private PreferredNetworkModeContentObserver mPreferredNetworkModeObserver;
     private TelephonyManager mTelephonyManager;
     private boolean mOnlyAutoSelectInHome;
     private List<OnNetworkSelectModeListener> mListeners;
@@ -66,6 +76,28 @@
         mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
         mListeners = new ArrayList<>();
         mUiHandler = new Handler(Looper.getMainLooper());
+        mPreferredNetworkModeObserver = new PreferredNetworkModeContentObserver(mUiHandler);
+        mPreferredNetworkModeObserver.setPreferredNetworkModeChangedListener(
+                () -> updatePreference());
+    }
+
+    private void updatePreference() {
+        if (mPreferenceScreen != null) {
+            displayPreference(mPreferenceScreen);
+        }
+        if (mSwitchPreference != null) {
+            updateState(mSwitchPreference);
+        }
+    }
+
+    @OnLifecycleEvent(ON_START)
+    public void onStart() {
+        mPreferredNetworkModeObserver.register(mContext, mSubId);
+    }
+
+    @OnLifecycleEvent(ON_STOP)
+    public void onStop() {
+        mPreferredNetworkModeObserver.unregister(mContext);
     }
 
     @Override
@@ -78,6 +110,7 @@
     @Override
     public void displayPreference(PreferenceScreen screen) {
         super.displayPreference(screen);
+        mPreferenceScreen = screen;
         mSwitchPreference = screen.findPreference(getPreferenceKey());
     }
 
@@ -142,7 +175,7 @@
         }
     }
 
-    public AutoSelectPreferenceController init(int subId) {
+    public AutoSelectPreferenceController init(Lifecycle lifecycle, int subId) {
         mSubId = subId;
         mTelephonyManager = mContext.getSystemService(TelephonyManager.class)
                 .createForSubscriptionId(mSubId);
@@ -153,6 +186,7 @@
                 CarrierConfigManager.KEY_ONLY_AUTO_SELECT_IN_HOME_NETWORK_BOOL)
                 : false;
 
+        lifecycle.addObserver(this);
         return this;
     }
 
diff --git a/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java b/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
index 8c3928d..2cc5bf1 100644
--- a/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
+++ b/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
@@ -16,38 +16,64 @@
 
 package com.android.settings.network.telephony.gsm;
 
+import static androidx.lifecycle.Lifecycle.Event.ON_START;
+import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
+
 import android.app.settings.SettingsEnums;
 import android.content.Context;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
 import android.provider.Settings;
 import android.telephony.ServiceState;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.OnLifecycleEvent;
 import androidx.preference.Preference;
 import androidx.preference.PreferenceScreen;
 
 import com.android.settings.R;
 import com.android.settings.core.SubSettingLauncher;
+import com.android.settings.network.PreferredNetworkModeContentObserver;
 import com.android.settings.network.telephony.MobileNetworkUtils;
 import com.android.settings.network.telephony.NetworkSelectSettings;
 import com.android.settings.network.telephony.TelephonyBasePreferenceController;
 
+
 /**
  * Preference controller for "Open network select"
  */
 public class OpenNetworkSelectPagePreferenceController extends
         TelephonyBasePreferenceController implements
-        AutoSelectPreferenceController.OnNetworkSelectModeListener {
+        AutoSelectPreferenceController.OnNetworkSelectModeListener, LifecycleObserver {
 
     private TelephonyManager mTelephonyManager;
     private Preference mPreference;
+    private PreferenceScreen mPreferenceScreen;
+    private PreferredNetworkModeContentObserver mPreferredNetworkModeObserver;
 
     public OpenNetworkSelectPagePreferenceController(Context context, String key) {
         super(context, key);
         mTelephonyManager = context.getSystemService(TelephonyManager.class);
         mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+        mPreferredNetworkModeObserver = new PreferredNetworkModeContentObserver(
+                new Handler(Looper.getMainLooper()));
+        mPreferredNetworkModeObserver.setPreferredNetworkModeChangedListener(
+                () -> updatePreference());
+
+    }
+
+    private void updatePreference() {
+        if (mPreferenceScreen != null) {
+            displayPreference(mPreferenceScreen);
+        }
+        if (mPreference != null) {
+            updateState(mPreference);
+        }
     }
 
     @Override
@@ -57,9 +83,20 @@
                 : CONDITIONALLY_UNAVAILABLE;
     }
 
+    @OnLifecycleEvent(ON_START)
+    public void onStart() {
+        mPreferredNetworkModeObserver.register(mContext, mSubId);
+    }
+
+    @OnLifecycleEvent(ON_STOP)
+    public void onStop() {
+        mPreferredNetworkModeObserver.unregister(mContext);
+    }
+
     @Override
     public void displayPreference(PreferenceScreen screen) {
         super.displayPreference(screen);
+        mPreferenceScreen = screen;
         mPreference = screen.findPreference(getPreferenceKey());
     }
 
@@ -97,10 +134,11 @@
         return false;
     }
 
-    public OpenNetworkSelectPagePreferenceController init(int subId) {
+    public OpenNetworkSelectPagePreferenceController init(Lifecycle lifecycle, int subId) {
         mSubId = subId;
         mTelephonyManager = mContext.getSystemService(TelephonyManager.class)
                 .createForSubscriptionId(mSubId);
+        lifecycle.addObserver(this);
         return this;
     }
 
diff --git a/tests/robotests/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java b/tests/robotests/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java
new file mode 100644
index 0000000..657b8b3
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java
@@ -0,0 +1,82 @@
+/*
+ * 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.settings.network;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.provider.Settings;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(RobolectricTestRunner.class)
+public class PreferredNetworkModeContentObserverTest {
+
+    private static final int SUB_ID = 1;
+
+    @Mock
+    private ContentResolver mResolver;
+    @Mock
+    private Context mContext;
+    @Mock
+    private PreferredNetworkModeContentObserver.OnPreferredNetworkModeChangedListener mListener;
+
+    private PreferredNetworkModeContentObserver mPreferredNetworkModeContentObserver;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+		when(mContext.getContentResolver()).thenReturn(mResolver);
+        mPreferredNetworkModeContentObserver =
+                spy(new PreferredNetworkModeContentObserver(null));
+    }
+
+    @Test
+    public void onChange_shouldCallListener() {
+        mPreferredNetworkModeContentObserver.mListener = mListener;
+        mPreferredNetworkModeContentObserver.onChange(true);
+
+        verify(mListener).onPreferredNetworkModeChanged();
+    }
+
+    @Test
+    public void register_shouldRegisterContentObserver() {
+        mPreferredNetworkModeContentObserver.register(mContext, SUB_ID);
+
+        verify(mResolver).registerContentObserver(
+            Settings.Global.getUriFor(Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID), false,
+            mPreferredNetworkModeContentObserver);
+    }
+
+    @Test
+    public void unregister_shouldUnregisterContentObserver() {
+        mPreferredNetworkModeContentObserver.unregister(mContext);
+
+        verify(mResolver).unregisterContentObserver(mPreferredNetworkModeContentObserver);
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceControllerTest.java
index 0ae2dc6..d1a794d 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceControllerTest.java
@@ -30,9 +30,11 @@
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 
+import androidx.lifecycle.LifecycleOwner;
 import androidx.preference.SwitchPreference;
 
 import com.android.settings.R;
+import com.android.settingslib.core.lifecycle.Lifecycle;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -62,12 +64,16 @@
     private AutoSelectPreferenceController mController;
     private SwitchPreference mSwitchPreference;
     private Context mContext;
+    private LifecycleOwner mLifecycleOwner;
+    private Lifecycle mLifecycle;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
         mContext = spy(RuntimeEnvironment.application);
+        mLifecycleOwner = () -> mLifecycle;
+        mLifecycle = new Lifecycle(mLifecycleOwner);
         when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
         when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
         when(mContext.getSystemService(CarrierConfigManager.class)).thenReturn(
@@ -83,7 +89,7 @@
         mController = new AutoSelectPreferenceController(mContext, "auto_select");
         mController.mProgressDialog = mProgressDialog;
         mController.mSwitchPreference = mSwitchPreference;
-        mController.init(SUB_ID);
+        mController.init(mLifecycle, SUB_ID);
     }
 
     @Test
@@ -125,6 +131,6 @@
         when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(null);
 
         // Should not crash
-        mController.init(SUB_ID);
+        mController.init(mLifecycle, SUB_ID);
     }
 }
diff --git a/tests/robotests/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java
index 5c18220..60de5aa 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java
@@ -17,7 +17,7 @@
 package com.android.settings.network.telephony.gsm;
 
 import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.doReturn;
+
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
@@ -29,11 +29,11 @@
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 
+import androidx.lifecycle.LifecycleOwner;
 import androidx.preference.Preference;
 
 import com.android.settings.R;
-import com.android.settings.network.telephony.MobileNetworkUtils;
-import java.util.Arrays;
+import com.android.settingslib.core.lifecycle.Lifecycle;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -43,6 +43,8 @@
 import org.robolectric.RobolectricTestRunner;
 import org.robolectric.RuntimeEnvironment;
 
+import java.util.Arrays;
+
 @RunWith(RobolectricTestRunner.class)
 public class OpenNetworkSelectPagePreferenceControllerTest {
     private static final int SUB_ID = 2;
@@ -63,12 +65,16 @@
     private OpenNetworkSelectPagePreferenceController mController;
     private Preference mPreference;
     private Context mContext;
+    private LifecycleOwner mLifecycleOwner;
+    private Lifecycle mLifecycle;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
         mContext = spy(RuntimeEnvironment.application);
+        mLifecycleOwner = () -> mLifecycle;
+        mLifecycle = new Lifecycle(mLifecycleOwner);
         when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
         when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
         when(mContext.getSystemService(CarrierConfigManager.class)).thenReturn(
@@ -92,7 +98,7 @@
         mPreference = new Preference(mContext);
         mController = new OpenNetworkSelectPagePreferenceController(mContext,
                 "open_network_select");
-        mController.init(SUB_ID);
+        mController.init(mLifecycle, SUB_ID);
     }
 
     @Test