Merge "Only one channel is reserved for WifiP2pSettings"

This commit is contained in:
Treehugger Robot
2022-11-29 23:44:22 +00:00
committed by Gerrit Code Review
2 changed files with 34 additions and 34 deletions

View File

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

View File

@@ -336,7 +336,7 @@ public class WifiP2pSettingsTest {
public void onStop_notLastGroupFormed_shouldCloseChannel() { public void onStop_notLastGroupFormed_shouldCloseChannel() {
mFragment.onStop(); mFragment.onStop();
assertThat(mFragment.mChannel).isNull(); assertThat(mFragment.sChannel).isNull();
} }
@Test @Test
@@ -353,7 +353,7 @@ public class WifiP2pSettingsTest {
verify(mWifiP2pManager, times(1)).stopPeerDiscovery(any(), any()); verify(mWifiP2pManager, times(1)).stopPeerDiscovery(any(), any());
mFragment.onResume(); mFragment.onResume();
assertThat(mFragment.mChannel).isNotNull(); assertThat(mFragment.sChannel).isNotNull();
} }
@Test @Test
@@ -524,7 +524,7 @@ public class WifiP2pSettingsTest {
@Test @Test
public void onCreateView_withNullP2pManager_shouldGetP2pManagerAgain() { 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.mWifiP2pManager = null;
mFragment.onCreateView(LayoutInflater.from(mContext), null, new Bundle()); mFragment.onCreateView(LayoutInflater.from(mContext), null, new Bundle());
@@ -535,7 +535,7 @@ public class WifiP2pSettingsTest {
@Test @Test
public void onCreateView_withNullChannel_shouldSetP2pManagerNull() { public void onCreateView_withNullChannel_shouldSetP2pManagerNull() {
doReturn(null).when(mWifiP2pManager).initialize(any(), any(), any()); 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()); mFragment.onCreateView(LayoutInflater.from(mContext), null, new Bundle());
assertThat(mFragment.mWifiP2pManager).isNull(); assertThat(mFragment.mWifiP2pManager).isNull();