Make P2P Settings channel static

P2P Settings creates a P2P channel but does not close the channel if
there is a P2P connection. This causes multiple channels to accumulate
if P2P Settings is repeatedly opened and closed. Making the channel
static will ensure that each time a new P2P Settings activity is
created, the existing open channel will be used.

Bug: 259364357
Test: build
Change-Id: I999b108bc3e3c22519398a55b503078c1069cef1
This commit is contained in:
Quang Luong
2022-11-22 18:44:25 +00:00
parent 2a1f7cb453
commit b6c2ed6759
2 changed files with 33 additions and 33 deletions

View File

@@ -76,7 +76,7 @@ public class WifiP2pSettings extends DashboardFragment
private final IntentFilter mIntentFilter = new IntentFilter();
@VisibleForTesting WifiP2pManager mWifiP2pManager;
@VisibleForTesting WifiP2pManager.Channel mChannel;
@VisibleForTesting static WifiP2pManager.Channel sChannel;
@VisibleForTesting OnClickListener mRenameListener;
@VisibleForTesting OnClickListener mDisconnectListener;
@VisibleForTesting OnClickListener mCancelConnectListener;
@@ -150,8 +150,8 @@ public class WifiP2pSettings extends DashboardFragment
// Requesting our own device info as an app holding the NETWORK_SETTINGS permission
// ensures that the MAC address will be available in the result.
if (DBG) Log.d(TAG, "This device changed. Requesting device info.");
if (mWifiP2pManager != null && mChannel != null) {
mWifiP2pManager.requestDeviceInfo(mChannel, WifiP2pSettings.this);
if (mWifiP2pManager != null && sChannel != null) {
mWifiP2pManager.requestDeviceInfo(sChannel, WifiP2pSettings.this);
}
} else if (WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION.equals(action)) {
int discoveryState = intent.getIntExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE,
@@ -163,8 +163,8 @@ public class WifiP2pSettings extends DashboardFragment
updateSearchMenu(false);
}
} else if (WifiP2pManager.ACTION_WIFI_P2P_PERSISTENT_GROUPS_CHANGED.equals(action)) {
if (mWifiP2pManager != null && mChannel != null) {
mWifiP2pManager.requestPersistentGroupInfo(mChannel, WifiP2pSettings.this);
if (mWifiP2pManager != null && sChannel != null) {
mWifiP2pManager.requestPersistentGroupInfo(sChannel, WifiP2pSettings.this);
}
}
}
@@ -239,7 +239,7 @@ public class WifiP2pSettings extends DashboardFragment
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
if (mWifiP2pManager != null && mChannel != null) {
if (mWifiP2pManager != null && sChannel != null) {
String name = mDeviceNameText.getText().toString();
if (name != null) {
for (int i = 0; i < name.length(); i++) {
@@ -253,7 +253,7 @@ public class WifiP2pSettings extends DashboardFragment
}
}
}
mWifiP2pManager.setDeviceName(mChannel,
mWifiP2pManager.setDeviceName(sChannel,
mDeviceNameText.getText().toString(),
new WifiP2pManager.ActionListener() {
public void onSuccess() {
@@ -275,8 +275,8 @@ public class WifiP2pSettings extends DashboardFragment
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
if (mWifiP2pManager != null && mChannel != null) {
mWifiP2pManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() {
if (mWifiP2pManager != null && sChannel != null) {
mWifiP2pManager.removeGroup(sChannel, new WifiP2pManager.ActionListener() {
public void onSuccess() {
if (DBG) Log.d(TAG, " remove group success");
}
@@ -294,8 +294,8 @@ public class WifiP2pSettings extends DashboardFragment
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
if (mWifiP2pManager != null && mChannel != null) {
mWifiP2pManager.cancelConnect(mChannel,
if (mWifiP2pManager != null && sChannel != null) {
mWifiP2pManager.cancelConnect(sChannel,
new WifiP2pManager.ActionListener() {
public void onSuccess() {
if (DBG) Log.d(TAG, " cancel connect success");
@@ -314,10 +314,10 @@ public class WifiP2pSettings extends DashboardFragment
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
if (mWifiP2pManager != null && mChannel != null) {
if (mWifiP2pManager != null && sChannel != null) {
if (mSelectedGroup != null) {
if (DBG) Log.d(TAG, " deleting group " + mSelectedGroup.getGroupName());
mWifiP2pManager.deletePersistentGroup(mChannel,
mWifiP2pManager.deletePersistentGroup(sChannel,
mSelectedGroup.getNetworkId(),
new WifiP2pManager.ActionListener() {
public void onSuccess() {
@@ -357,8 +357,8 @@ public class WifiP2pSettings extends DashboardFragment
if (mWifiP2pManager != null && initChannel()) {
// Register receiver after make sure channel exist
getActivity().registerReceiver(mReceiver, mIntentFilter);
mWifiP2pManager.requestPeers(mChannel, WifiP2pSettings.this);
mWifiP2pManager.requestDeviceInfo(mChannel, WifiP2pSettings.this);
mWifiP2pManager.requestPeers(sChannel, WifiP2pSettings.this);
mWifiP2pManager.requestDeviceInfo(sChannel, WifiP2pSettings.this);
mIsIgnoreInitConnectionInfoCallback = false;
}
}
@@ -366,12 +366,12 @@ public class WifiP2pSettings extends DashboardFragment
@Override
public void onStop() {
super.onStop();
if (mWifiP2pManager != null && mChannel != null) {
mWifiP2pManager.stopPeerDiscovery(mChannel, null);
if (mWifiP2pManager != null && sChannel != null) {
mWifiP2pManager.stopPeerDiscovery(sChannel, null);
if (!mLastGroupFormed) {
// Close the channel when p2p doesn't connected.
mChannel.close();
mChannel = null;
sChannel.close();
sChannel = null;
}
}
getActivity().unregisterReceiver(mReceiver);
@@ -445,8 +445,8 @@ public class WifiP2pSettings extends DashboardFragment
config.wps.setup = WpsInfo.DISPLAY;
}
}
if (mWifiP2pManager != null && mChannel != null) {
mWifiP2pManager.connect(mChannel, config,
if (mWifiP2pManager != null && sChannel != null) {
mWifiP2pManager.connect(sChannel, config,
new WifiP2pManager.ActionListener() {
public void onSuccess() {
if (DBG) Log.d(TAG, " connect success");
@@ -617,9 +617,9 @@ public class WifiP2pSettings extends DashboardFragment
}
private void onDeviceAvailable() {
mWifiP2pManager.requestNetworkInfo(mChannel, networkInfo -> {
if (mChannel == null) return;
mWifiP2pManager.requestConnectionInfo(mChannel, wifip2pinfo -> {
mWifiP2pManager.requestNetworkInfo(sChannel, networkInfo -> {
if (sChannel == null) return;
mWifiP2pManager.requestConnectionInfo(sChannel, wifip2pinfo -> {
if (!mIsIgnoreInitConnectionInfoCallback) {
if (networkInfo.isConnected()) {
if (DBG) {
@@ -652,8 +652,8 @@ public class WifiP2pSettings extends DashboardFragment
}
private void startSearch() {
if (mWifiP2pManager != null && mChannel != null && !mWifiP2pSearching) {
mWifiP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
if (mWifiP2pManager != null && sChannel != null && !mWifiP2pSearching) {
mWifiP2pManager.discoverPeers(sChannel, new WifiP2pManager.ActionListener() {
public void onSuccess() {
}
public void onFailure(int reason) {
@@ -664,14 +664,14 @@ public class WifiP2pSettings extends DashboardFragment
}
private boolean initChannel() {
if (mChannel != null) {
if (sChannel != null) {
return true;
}
if (mWifiP2pManager != null) {
mChannel = mWifiP2pManager.initialize(getActivity().getApplicationContext(),
sChannel = mWifiP2pManager.initialize(getActivity().getApplicationContext(),
getActivity().getMainLooper(), null);
}
if (mChannel == null) {
if (sChannel == null) {
Log.e(TAG, "Failed to set up connection with wifi p2p service");
return false;
}

View File

@@ -338,7 +338,7 @@ public class WifiP2pSettingsTest {
public void onStop_notLastGroupFormed_shouldCloseChannel() {
mFragment.onStop();
assertThat(mFragment.mChannel).isNull();
assertThat(mFragment.sChannel).isNull();
}
@Test
@@ -355,7 +355,7 @@ public class WifiP2pSettingsTest {
verify(mWifiP2pManager, times(1)).stopPeerDiscovery(any(), any());
mFragment.onStart();
assertThat(mFragment.mChannel).isNotNull();
assertThat(mFragment.sChannel).isNotNull();
}
@Test
@@ -526,7 +526,7 @@ public class WifiP2pSettingsTest {
@Test
public void onCreateView_withNullP2pManager_shouldGetP2pManagerAgain() {
mFragment.mChannel = null; // Reset channel to re-test onCreateView flow
mFragment.sChannel = null; // Reset channel to re-test onCreateView flow
mFragment.mWifiP2pManager = null;
mFragment.onCreateView(LayoutInflater.from(mContext), null, new Bundle());
@@ -537,7 +537,7 @@ public class WifiP2pSettingsTest {
@Test
public void onCreateView_withNullChannel_shouldSetP2pManagerNull() {
doReturn(null).when(mWifiP2pManager).initialize(any(), any(), any());
mFragment.mChannel = null; // Reset channel to re-test onCreateView flow
mFragment.sChannel = null; // Reset channel to re-test onCreateView flow
mFragment.onCreateView(LayoutInflater.from(mContext), null, new Bundle());
assertThat(mFragment.mWifiP2pManager).isNull();