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

Unverified Commit 36dae8f9 authored by Li Sun's avatar Li Sun Committed by Michael Bestas
Browse files

WifiDisplayController: handle preexisting p2p connection status

Handle the status that there is an existing p2p-group already formed
when starting to connect to wifi display.

There are two cases:
- if the desired device is the same with the already connected device,
  start to listen to the remote display.
- if the desired device is different, disconnect the old one and reconnect
  the new one.

CRs-Fixed: 2293770
Change-Id: Icacc59d38fb81d6a6f812043c2798760f82bc606
parent 8329e5e7
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
@@ -552,6 +552,10 @@ final class WifiDisplayController implements DumpUtils.Dump {
            return;
        }

        if (handlePreExistingConnection(device)) {
            Slog.i(TAG, "Already handle the preexisting P2P connection status");
            return;
        }
        mDesiredDevice = device;
        mConnectionRetriesLeft = CONNECT_MAX_RETRIES;
        updateConnection();
@@ -933,6 +937,12 @@ final class WifiDisplayController implements DumpUtils.Dump {
                disconnect();
            }

            if (mDesiredDevice != null) {
                Slog.i(TAG, "Reconnect new device: " + mDesiredDevice.deviceName);
                updateConnection();
                return;
            }

            // After disconnection for a group, for some reason we have a tendency
            // to get a peer change notification with an empty list of peers.
            // Perform a fresh scan.
@@ -1050,6 +1060,42 @@ final class WifiDisplayController implements DumpUtils.Dump {
                mAdvertisedDisplayFlags);
    }

    private boolean handlePreExistingConnection(final WifiP2pDevice device) {
        if (mNetworkInfo == null || !mNetworkInfo.isConnected() || mWifiDisplayCertMode) {
            return false;
        }
        if (DEBUG) Slog.i(TAG, "Handle the preexisting P2P connection status");
        mWifiP2pManager.requestGroupInfo(mWifiP2pChannel, new GroupInfoListener() {
            @Override
            public void onGroupInfoAvailable(WifiP2pGroup info) {
                if (info == null) {
                    return;
                }
                if (contains(info, device)) {
                    if (DEBUG) Slog.i(TAG, "Already connected to the desired device: "
                            + device.deviceName);
                    updateConnection();
                    handleConnectionChanged(mNetworkInfo);
                } else {
                    mWifiP2pManager.removeGroup(mWifiP2pChannel, new ActionListener() {
                        @Override
                        public void onSuccess() {
                            Slog.i(TAG, "Disconnect the old device");
                        }

                        @Override
                        public void onFailure(int reason) {
                            Slog.w(TAG, "Failed to disconnect the old device: reason=" + reason);
                        }
                    });
                }
            }
        });
        mDesiredDevice = device;
        mConnectionRetriesLeft = CONNECT_MAX_RETRIES;
        return true;
    }

    private static Inet4Address getInterfaceAddress(WifiP2pGroup info) {
        NetworkInterface iface;
        try {