Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 42ee738f authored by DingFei Song's avatar DingFei Song Committed by Quang Luong
Browse files

Only one channel is reserved for WifiP2pSettings



Every time we enter WifiP2pSettings, a channel will be created,
but we should keep only one channel if p2p connected.

Bug: 259364357

Change-Id: Ie3c1b1281d74332cd5df720fb13476097f3f5df9
Merged-In: I999b108bc3e3c22519398a55b503078c1069cef1
Signed-off-by: default avatarDingFei Song <v-dingfeisong@xiaomi.corp-partner.google.com>
parent 8ae657bd
Loading
Loading
Loading
Loading
+30 −30
Original line number Diff line number Diff line
@@ -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;
@@ -145,8 +145,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,
@@ -158,8 +158,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);
                }
            }
        }
@@ -234,7 +234,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++) {
@@ -248,7 +248,7 @@ public class WifiP2pSettings extends DashboardFragment
                                }
                            }
                        }
                        mWifiP2pManager.setDeviceName(mChannel,
                        mWifiP2pManager.setDeviceName(sChannel,
                                mDeviceNameText.getText().toString(),
                                new WifiP2pManager.ActionListener() {
                            public void onSuccess() {
@@ -270,8 +270,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");
                            }
@@ -289,8 +289,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");
@@ -309,10 +309,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() {
@@ -352,12 +352,12 @@ 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;
            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) {
@@ -377,8 +377,8 @@ public class WifiP2pSettings extends DashboardFragment
    @Override
    public void onPause() {
        super.onPause();
        if (mWifiP2pManager != null && mChannel != null) {
            mWifiP2pManager.stopPeerDiscovery(mChannel, null);
        if (mWifiP2pManager != null && sChannel != null) {
            mWifiP2pManager.stopPeerDiscovery(sChannel, null);
        }
        getActivity().unregisterReceiver(mReceiver);
    }
@@ -386,11 +386,11 @@ public class WifiP2pSettings extends DashboardFragment
    @Override
    public void onStop() {
        super.onStop();
        if (mWifiP2pManager != null && mChannel != null) {
        if (mWifiP2pManager != null && sChannel != null) {
            if (!mLastGroupFormed) {
                // Close the channel when p2p doesn't connected.
                mChannel.close();
                mChannel = null;
                sChannel.close();
                sChannel = null;
            }
        }
    }
@@ -463,8 +463,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");
@@ -646,8 +646,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) {
@@ -658,14 +658,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;
        }
+4 −4
Original line number Diff line number Diff line
@@ -336,7 +336,7 @@ public class WifiP2pSettingsTest {
    public void onStop_notLastGroupFormed_shouldCloseChannel() {
        mFragment.onStop();

        assertThat(mFragment.mChannel).isNull();
        assertThat(mFragment.sChannel).isNull();
    }

    @Test
@@ -353,7 +353,7 @@ public class WifiP2pSettingsTest {
        verify(mWifiP2pManager, times(1)).stopPeerDiscovery(any(), any());

        mFragment.onResume();
        assertThat(mFragment.mChannel).isNotNull();
        assertThat(mFragment.sChannel).isNotNull();
    }

    @Test
@@ -524,7 +524,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());
@@ -535,7 +535,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();