diff --git a/src/com/android/settings/wifi/addappnetworks/AddAppNetworksActivity.java b/src/com/android/settings/wifi/addappnetworks/AddAppNetworksActivity.java index 298857ec3b4..79d5002f592 100644 --- a/src/com/android/settings/wifi/addappnetworks/AddAppNetworksActivity.java +++ b/src/com/android/settings/wifi/addappnetworks/AddAppNetworksActivity.java @@ -24,6 +24,7 @@ import android.view.Window; import android.view.WindowManager; import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; @@ -77,10 +78,13 @@ public class AddAppNetworksActivity extends FragmentActivity { getIntent().getParcelableArrayListExtra(Settings.EXTRA_WIFI_CONFIGURATION_LIST)); final FragmentManager fragmentManager = getSupportFragmentManager(); - if (fragmentManager.findFragmentByTag(TAG) == null) { - final AddAppNetworksFragment fragment = new AddAppNetworksFragment(); + Fragment fragment = fragmentManager.findFragmentByTag(TAG); + if (fragment == null) { + fragment = new AddAppNetworksFragment(); fragment.setArguments(mBundle); fragmentManager.beginTransaction().add(R.id.main_content, fragment, TAG).commit(); + } else { + ((AddAppNetworksFragment) fragment).createContent(mBundle); } } } diff --git a/src/com/android/settings/wifi/addappnetworks/AddAppNetworksFragment.java b/src/com/android/settings/wifi/addappnetworks/AddAppNetworksFragment.java index 80e14dc9975..d180df7db61 100644 --- a/src/com/android/settings/wifi/addappnetworks/AddAppNetworksFragment.java +++ b/src/com/android/settings/wifi/addappnetworks/AddAppNetworksFragment.java @@ -184,7 +184,19 @@ public class AddAppNetworksFragment extends InstrumentedFragment { createContent(bundle); } - private void createContent(Bundle bundle) { + /** + * Updates the UI contents to be aligned with the parameters in Bundle. This API may be called + * by the Activity directly when get a new intent. + */ + @VisibleForTesting + void createContent(Bundle bundle) { + // For new intent case, if device is saving those networks specified in old intent, just + // ignore this new intent for preventing status error. + if (mSaveButton != null && !mSaveButton.isEnabled()) { + Log.d(TAG, "Network saving, ignore new intent"); + return; + } + mAllSpecifiedNetworksList = bundle.getParcelableArrayList(Settings.EXTRA_WIFI_CONFIGURATION_LIST); @@ -199,7 +211,7 @@ public class AddAppNetworksFragment extends InstrumentedFragment { // Initial the result arry. initializeResultCodeArray(); // Filter the saved networks, and prepare a not saved networks list for UI to present. - mUiToRequestedList = filterSavedNetworks(mWifiManager.getPrivilegedConfiguredNetworks()); + filterSavedNetworks(mWifiManager.getPrivilegedConfiguredNetworks()); // If all the specific networks are all exist, we just need to finish with result. if (mUiToRequestedList.size() == 0) { @@ -209,8 +221,11 @@ public class AddAppNetworksFragment extends InstrumentedFragment { if (mAllSpecifiedNetworksList.size() == 1) { mIsSingleNetwork = true; - // Set the multiple networks related layout as GONE. + // Set the multiple networks related layout to be gone, and the single network layout + // items to be visible. mLayoutView.findViewById(R.id.multiple_networks).setVisibility(View.GONE); + mLayoutView.findViewById(R.id.single_network).setVisibility(View.VISIBLE); + // Show signal icon for single network case. setSingleNetworkSignalIcon(); // Show the SSID of the proposed network. @@ -221,13 +236,21 @@ public class AddAppNetworksFragment extends InstrumentedFragment { } else { // Multiple networks request case. mIsSingleNetwork = false; - // Set the single network related layout as GONE. + // Set the single network related layout to be gone, and the multiple networks layout + // items to be visible. mLayoutView.findViewById(R.id.single_network).setVisibility(View.GONE); - // Prepare a UI adapter and set to UI listview. - final ListView uiNetworkListView = mLayoutView.findViewById(R.id.config_list); - mUiConfigurationItemAdapter = new UiConfigurationItemAdapter(mActivity, - com.android.settingslib.R.layout.preference_access_point, mUiToRequestedList); - uiNetworkListView.setAdapter(mUiConfigurationItemAdapter); + mLayoutView.findViewById(R.id.multiple_networks).setVisibility(View.VISIBLE); + + if (mUiConfigurationItemAdapter == null) { + // Prepare a UI adapter and set to UI listview. + final ListView uiNetworkListView = mLayoutView.findViewById(R.id.config_list); + mUiConfigurationItemAdapter = new UiConfigurationItemAdapter(mActivity, + com.android.settingslib.R.layout.preference_access_point, + mUiToRequestedList); + uiNetworkListView.setAdapter(mUiConfigurationItemAdapter); + } else { + mUiConfigurationItemAdapter.notifyDataSetChanged(); + } } // Assigns caller app icon, title, and summary. @@ -269,13 +292,17 @@ public class AddAppNetworksFragment extends InstrumentedFragment { /** * For the APP specified networks, filter saved ones and mark those saved as existed. And - * finally return a new UiConfigurationItem list, which contains those new or need to be - * updated networks, back to caller for creating UI to user. + * prepare a new UiConfigurationItem list, which contains those new or need to be updated + * networks, for creating UI to user. */ @VisibleForTesting - ArrayList filterSavedNetworks( + void filterSavedNetworks( List savedWifiConfigurations) { - ArrayList uiToRequestedList = new ArrayList<>(); + if (mUiToRequestedList == null) { + mUiToRequestedList = new ArrayList<>(); + } else { + mUiToRequestedList.clear(); + } boolean foundInSavedList; int networkPositionInBundle = 0; @@ -327,12 +354,10 @@ public class AddAppNetworksFragment extends InstrumentedFragment { // Prepare to add to UI list to show to user UiConfigurationItem uiConfigurationIcon = new UiConfigurationItem(displayedSsid, specifiecConfig, networkPositionInBundle); - uiToRequestedList.add(uiConfigurationIcon); + mUiToRequestedList.add(uiConfigurationIcon); } networkPositionInBundle++; } - - return uiToRequestedList; } private void setSingleNetworkSignalIcon() { @@ -573,6 +598,10 @@ public class AddAppNetworksFragment extends InstrumentedFragment { } private void finishWithResult(int resultCode, List resultArrayList) { + if (mActivity == null) { + return; + } + if (resultArrayList != null) { Intent intent = new Intent(); intent.putIntegerArrayListExtra(Settings.EXTRA_WIFI_CONFIGURATION_RESULT_LIST, diff --git a/tests/robotests/src/com/android/settings/wifi/addappnetworks/AddAppNetworksFragmentTest.java b/tests/robotests/src/com/android/settings/wifi/addappnetworks/AddAppNetworksFragmentTest.java index 065186be746..cf28c9d0699 100644 --- a/tests/robotests/src/com/android/settings/wifi/addappnetworks/AddAppNetworksFragmentTest.java +++ b/tests/robotests/src/com/android/settings/wifi/addappnetworks/AddAppNetworksFragmentTest.java @@ -129,8 +129,7 @@ public class AddAppNetworksFragmentTest { mAddAppNetworksFragment.mResultCodeArrayList = mFakedResultArrayList; // Act - mAddAppNetworksFragment.mUiToRequestedList = mAddAppNetworksFragment.filterSavedNetworks( - mFakeSavedNetworksList); + mAddAppNetworksFragment.filterSavedNetworks(mFakeSavedNetworksList); // Assert assertThat(mAddAppNetworksFragment.mUiToRequestedList).hasSize(1); @@ -146,6 +145,23 @@ public class AddAppNetworksFragmentTest { SettingsEnums.PANEL_ADD_WIFI_NETWORKS); } + @Test + public void getThreeNetworksNewIntent_shouldHaveThreeItemsInUiList() { + addOneSpecifiedNetworkConfig(mNewWpaConfigEntry); + setUpBundle(mFakedSpecifiedNetworksList); + setupFragment(); + + // Add two more networks and update framework bundle. + addOneSpecifiedNetworkConfig(mNewWpaConfigEntry); + addOneSpecifiedNetworkConfig(mNewOpenConfigEntry); + setUpBundle(mFakedSpecifiedNetworksList); + Bundle bundle = mAddAppNetworksFragment.getArguments(); + mAddAppNetworksFragment.createContent(bundle); + + // Ui list should contain 3 networks. + assertThat(mAddAppNetworksFragment.mUiToRequestedList).hasSize(3); + } + private void addOneSavedNetworkConfig(@NonNull WifiConfiguration wifiConfiguration) { if (mFakeSavedNetworksList == null) { mFakeSavedNetworksList = new ArrayList<>();