p2p: Close the channel when onStop is called.

P2P dialog is moved to an indenpendent activity, it will push Settings
to background and trigger onPause(). As P2P dialog is a Dialog Activity,
it won't trigger onStop, but SoftAp and NAN activity will. Moving
channel closing to onStop to adapt to new P2P dialog design.

Bug: 219406778
Test: make RunSettingsRoboTests ROBOTEST_FILTER=WifiP2pSettingsTest
      create a p2p connection between 2 devices with this build.
Change-Id: I2fab5aa1021ec1a993f811c7310079db9d7f03c0
This commit is contained in:
Jimmy Chen
2022-02-23 16:12:01 +08:00
parent 1adba841e1
commit 78a0f6f98e
2 changed files with 15 additions and 3 deletions

View File

@@ -375,13 +375,20 @@ public class WifiP2pSettings extends DashboardFragment
super.onPause(); super.onPause();
if (mWifiP2pManager != null && mChannel != null) { if (mWifiP2pManager != null && mChannel != null) {
mWifiP2pManager.stopPeerDiscovery(mChannel, null); mWifiP2pManager.stopPeerDiscovery(mChannel, null);
}
getActivity().unregisterReceiver(mReceiver);
}
@Override
public void onStop() {
super.onStop();
if (mWifiP2pManager != null && mChannel != null) {
if (!mLastGroupFormed) { if (!mLastGroupFormed) {
// Close the channel when p2p doesn't connected. // Close the channel when p2p doesn't connected.
mChannel.close(); mChannel.close();
mChannel = null; mChannel = null;
} }
} }
getActivity().unregisterReceiver(mReceiver);
} }
@Override @Override

View File

@@ -319,12 +319,18 @@ public class WifiP2pSettingsTest {
assertThat(mFragment.onCreateDialog(-1 /* id */)).isNull(); assertThat(mFragment.onCreateDialog(-1 /* id */)).isNull();
} }
@Test
public void onStop_notLastGroupFormed_shouldCloseChannel() {
mFragment.onStop();
assertThat(mFragment.mChannel).isNull();
}
@Test @Test
public void peerDiscovery_whenOnPause_shouldStop() { public void peerDiscovery_whenOnPause_shouldStop() {
mFragment.onPause(); mFragment.onPause();
verify(mWifiP2pManager, times(1)).stopPeerDiscovery(any(), any()); verify(mWifiP2pManager, times(1)).stopPeerDiscovery(any(), any());
assertThat(mFragment.mChannel).isNull();
} }
@Test @Test
@@ -332,7 +338,6 @@ public class WifiP2pSettingsTest {
mFragment.onPause(); mFragment.onPause();
verify(mWifiP2pManager, times(1)).stopPeerDiscovery(any(), any()); verify(mWifiP2pManager, times(1)).stopPeerDiscovery(any(), any());
assertThat(mFragment.mChannel).isNull();
mFragment.onResume(); mFragment.onResume();
assertThat(mFragment.mChannel).isNotNull(); assertThat(mFragment.mChannel).isNotNull();