Merge Android 12 QPR3 ab/8391262

Bug: 226662282
Merged-In: I50e3649c21bb83ee9130fa98a71560fe261198a9
Change-Id: I2a97159d0de32a6262bbace7bb82b09897447bdb
diff --git a/src/com/android/settings/network/InternetResetHelper.java b/src/com/android/settings/network/InternetResetHelper.java
index d6c5476..253cf56 100644
--- a/src/com/android/settings/network/InternetResetHelper.java
+++ b/src/com/android/settings/network/InternetResetHelper.java
@@ -204,7 +204,7 @@
     protected void resumePreferences() {
         if (mIsRecoveryReady && mMobileNetworkController != null) {
             Log.d(TAG, "Resume the Mobile Network controller");
-            mMobileNetworkController.hidePreference(false /* hide */, false /* immediately */);
+            mMobileNetworkController.hidePreference(false /* hide */, true /* immediately */);
         }
         if (mIsWifiReady && mWifiTogglePreferences != null) {
             Log.d(TAG, "Resume the Wi-Fi preferences");
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index f0ae90e..7f41f36 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -190,6 +190,8 @@
 
     // Worker thread used for WifiPickerTracker work
     private HandlerThread mWorkerThread;
+    private Handler mMainHandler;
+    private Handler mWorkerHandler;
 
     @VisibleForTesting
     WifiPickerTracker mWifiPickerTracker;
@@ -290,11 +292,13 @@
                 return SystemClock.elapsedRealtime();
             }
         };
+
+        mMainHandler = new Handler(Looper.getMainLooper());
+        mWorkerHandler = mWorkerThread.getThreadHandler();
         mWifiPickerTracker = FeatureFactory.getFactory(context)
                 .getWifiTrackerLibProvider()
                 .createWifiPickerTracker(getSettingsLifecycle(), context,
-                        new Handler(Looper.getMainLooper()),
-                        mWorkerThread.getThreadHandler(),
+                        mMainHandler, mWorkerHandler,
                         elapsedRealtimeClock,
                         MAX_SCAN_AGE_MILLIS,
                         SCAN_INTERVAL_MILLIS,
@@ -366,6 +370,10 @@
         if (mWifiEnabler != null) {
             mWifiEnabler.teardownSwitchController();
         }
+
+        // remove all msg and callback in main handler and worker handler
+        mMainHandler.removeCallbacksAndMessages(null);
+        mWorkerHandler.removeCallbacksAndMessages(null);
         mWorkerThread.quit();
 
         super.onDestroyView();
@@ -650,7 +658,7 @@
     /** Called when the state of Wifi has changed. */
     @Override
     public void onWifiStateChanged() {
-        if (mIsRestricted) {
+        if (mIsRestricted || isFinishingOrDestroyed()) {
             return;
         }
         final int wifiState = mWifiPickerTracker.getWifiState();
@@ -688,6 +696,10 @@
 
     @Override
     public void onWifiEntriesChanged() {
+        if (isFinishingOrDestroyed()) {
+            return;
+        }
+
         if (mIsWifiEntryListStale) {
             mIsWifiEntryListStale = false;
             updateWifiEntryPreferences();
diff --git a/src/com/android/settings/wifi/p2p/WifiP2pSettings.java b/src/com/android/settings/wifi/p2p/WifiP2pSettings.java
index c1365dd..435c3e9 100644
--- a/src/com/android/settings/wifi/p2p/WifiP2pSettings.java
+++ b/src/com/android/settings/wifi/p2p/WifiP2pSettings.java
@@ -47,6 +47,7 @@
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
+import android.view.ViewGroup;
 import android.widget.EditText;
 import android.widget.Toast;
 
@@ -199,7 +200,10 @@
     }
 
     @Override
-    public void onActivityCreated(Bundle savedInstanceState) {
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+                             Bundle savedInstanceState) {
+        final View root = super.onCreateView(inflater, container, savedInstanceState);
+
         final Activity activity = getActivity();
         if (mWifiP2pManager == null) {
             mWifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
@@ -332,8 +336,7 @@
                 }
             }
         };
-
-        super.onActivityCreated(savedInstanceState);
+        return root;
     }
 
     @Override
diff --git a/tests/robotests/src/com/android/settings/wifi/WifiSettingsTest.java b/tests/robotests/src/com/android/settings/wifi/WifiSettingsTest.java
index 5f2f564..abed447 100644
--- a/tests/robotests/src/com/android/settings/wifi/WifiSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/WifiSettingsTest.java
@@ -277,6 +277,11 @@
 
     @Test
     public void onWifiEntriesChanged_shouldChangeNextButtonState() {
+        final FragmentActivity activity = mock(FragmentActivity.class);
+        doReturn(false).when(activity).isFinishing();
+        doReturn(false).when(activity).isDestroyed();
+        doReturn(activity).when(mWifiSettings).getActivity();
+
         mWifiSettings.onWifiEntriesChanged();
 
         verify(mWifiSettings).changeNextButtonState(anyBoolean());
diff --git a/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2pSettingsTest.java b/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2pSettingsTest.java
index 796cdef..78e696d 100644
--- a/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2pSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2pSettingsTest.java
@@ -39,7 +39,7 @@
 import android.net.wifi.p2p.WifiP2pManager;
 import android.os.Bundle;
 import android.view.MenuItem;
-
+import android.view.LayoutInflater;
 import androidx.fragment.app.FragmentActivity;
 import androidx.fragment.app.FragmentManager;
 import androidx.fragment.app.FragmentTransaction;
@@ -110,30 +110,30 @@
     }
 
     @Test
-    public void onActivityCreate_withNullBundle_canNotGetValue() {
-        mFragment.onActivityCreated(null);
+    public void onCreateView_withNullBundle_canNotGetValue() {
+        mFragment.onCreateView(LayoutInflater.from(mContext), null, null);
 
         assertThat(mFragment.mSelectedWifiPeer).isNull();
     }
 
     @Test
-    public void onActivityCreate_withDeviceName_shouldGetDeviceName() {
+    public void onCreateView_withDeviceName_shouldGetDeviceName() {
         final String fakeDeviceName = "fakename";
         final Bundle bundle = new Bundle();
         bundle.putString(WifiP2pSettings.SAVE_DEVICE_NAME, fakeDeviceName);
 
-        mFragment.onActivityCreated(bundle);
+        mFragment.onCreateView(LayoutInflater.from(mContext), null, bundle);
 
         assertThat(mFragment.mSavedDeviceName).isEqualTo(fakeDeviceName);
     }
 
     @Test
-    public void onActivityCreate_withGroupName_shouldGetGroupName() {
+    public void onCreateView_withGroupName_shouldGetGroupName() {
         final String fakeGroupName = "fakegroup";
         final Bundle bundle = new Bundle();
         bundle.putString(WifiP2pSettings.SAVE_SELECTED_GROUP, fakeGroupName);
 
-        mFragment.onActivityCreated(bundle);
+        mFragment.onCreateView(LayoutInflater.from(mContext), null, bundle);
 
         assertThat(mFragment.mSelectedGroupName).isEqualTo(fakeGroupName);
         assertThat(mFragment.mSavedDeviceName).isNull();
@@ -279,7 +279,7 @@
         final String fakeDeviceName = "fakeName";
         final Bundle bundle = new Bundle();
         bundle.putString(WifiP2pSettings.SAVE_DEVICE_NAME, fakeDeviceName);
-        mFragment.onActivityCreated(bundle);
+        mFragment.onCreateView(LayoutInflater.from(mContext), null, bundle);
         final Dialog dialog = mFragment.onCreateDialog(WifiP2pSettings.DIALOG_RENAME);
 
         mFragment.mRenameListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
@@ -292,7 +292,8 @@
         final String fakeDeviceName = "wrongName***";
         final Bundle bundle = new Bundle();
         bundle.putString(WifiP2pSettings.SAVE_DEVICE_NAME, fakeDeviceName);
-        mFragment.onActivityCreated(bundle);
+        mFragment.onCreateView(LayoutInflater.from(mContext), null, bundle);
+
         final Dialog dialog = mFragment.onCreateDialog(WifiP2pSettings.DIALOG_RENAME);
 
         mFragment.mRenameListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
@@ -394,7 +395,7 @@
         final String fakeDeviceName = "fakeName";
         final Bundle createBundle = new Bundle();
         createBundle.putString(WifiP2pSettings.SAVE_DEVICE_NAME, fakeDeviceName);
-        mFragment.onActivityCreated(createBundle);
+        mFragment.onCreateView(LayoutInflater.from(mContext), null, createBundle);
         final Bundle outBundle = new Bundle();
         final Dialog dialog = mFragment.onCreateDialog(WifiP2pSettings.DIALOG_RENAME);
 
@@ -430,7 +431,7 @@
         doReturn(groupList).when(wifiP2pGroupList).getGroupList();
         final Bundle bundle = new Bundle();
         bundle.putString(WifiP2pSettings.SAVE_SELECTED_GROUP, fakeGroupName);
-        mFragment.onActivityCreated(bundle);
+        mFragment.onCreateView(LayoutInflater.from(mContext), null, bundle);
 
         mFragment.onPersistentGroupInfoAvailable(wifiP2pGroupList);
 
@@ -503,20 +504,20 @@
     }
 
     @Test
-    public void onActivityCreate_withNullP2pManager_shouldGetP2pManagerAgain() {
-        mFragment.mChannel = null; // Reset channel to re-test onActivityCreated flow
+    public void onCreateView_withNullP2pManager_shouldGetP2pManagerAgain() {
+        mFragment.mChannel = null; // Reset channel to re-test onCreateView flow
         mFragment.mWifiP2pManager = null;
 
-        mFragment.onActivityCreated(new Bundle());
+        mFragment.onCreateView(LayoutInflater.from(mContext), null, new Bundle());
 
         assertThat(mFragment.mWifiP2pManager).isNotNull();
     }
 
     @Test
-    public void onActivityCreate_withNullChannel_shouldSetP2pManagerNull() {
+    public void onCreateView_withNullChannel_shouldSetP2pManagerNull() {
         doReturn(null).when(mWifiP2pManager).initialize(any(), any(), any());
-        mFragment.mChannel = null; // Reset channel to re-test onActivityCreated flow
-        mFragment.onActivityCreated(new Bundle());
+        mFragment.mChannel = null; // Reset channel to re-test onCreateView flow
+        mFragment.onCreateView(LayoutInflater.from(mContext), null, new Bundle());
 
         assertThat(mFragment.mWifiP2pManager).isNull();
     }
diff --git a/tests/unit/src/com/android/settings/network/InternetResetHelperTest.java b/tests/unit/src/com/android/settings/network/InternetResetHelperTest.java
index 00ca8e1..3dbaa91 100644
--- a/tests/unit/src/com/android/settings/network/InternetResetHelperTest.java
+++ b/tests/unit/src/com/android/settings/network/InternetResetHelperTest.java
@@ -217,7 +217,7 @@
         // Show resetting preference
         assertThat(mResettingPreference.isVisible()).isTrue();
         // Show Mobile Network controller
-        verify(mMobileNetworkController).hidePreference(false /* hide */, false /* immediately*/);
+        verify(mMobileNetworkController).hidePreference(false /* hide */, true /* immediately*/);
         // Hide Wi-Fi preferences
         assertThat(mWifiTogglePreferences.isVisible()).isFalse();
         assertThat(mConnectedWifiEntryPreferences.isVisible()).isFalse();
@@ -240,7 +240,7 @@
         assertThat(mWifiEntryPreferences.isVisible()).isTrue();
         // Hide Mobile Network controller
         verify(mMobileNetworkController, never())
-                .hidePreference(false /* hide */, false /* immediately*/);
+                .hidePreference(false /* hide */, true /* immediately*/);
     }
 
     @Test
@@ -251,7 +251,7 @@
         mInternetResetHelper.resumePreferences();
 
         // Show subsystem preferences
-        verify(mMobileNetworkController).hidePreference(false, false);
+        verify(mMobileNetworkController).hidePreference(false, true);
         assertThat(mWifiTogglePreferences.isVisible()).isTrue();
         assertThat(mConnectedWifiEntryPreferences.isVisible()).isTrue();
         assertThat(mWifiEntryPreferences.isVisible()).isTrue();